1 /* $OpenBSD: tls_config.c,v 1.57 2019/11/16 06:44:33 beck Exp $ */ 2 /* 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/stat.h> 19 20 #include <ctype.h> 21 #include <errno.h> 22 #include <fcntl.h> 23 #include <pthread.h> 24 #include <stdlib.h> 25 #include <unistd.h> 26 27 #include <tls.h> 28 29 #include "tls_internal.h" 30 31 static const char default_ca_file[] = TLS_DEFAULT_CA_FILE; 32 33 const char * 34 tls_default_ca_cert_file(void) 35 { 36 return default_ca_file; 37 } 38 39 int 40 tls_config_load_file(struct tls_error *error, const char *filetype, 41 const char *filename, char **buf, size_t *len) 42 { 43 struct stat st; 44 int fd = -1; 45 ssize_t n; 46 47 free(*buf); 48 *buf = NULL; 49 *len = 0; 50 51 if ((fd = open(filename, O_RDONLY)) == -1) { 52 tls_error_set(error, "failed to open %s file '%s'", 53 filetype, filename); 54 goto err; 55 } 56 if (fstat(fd, &st) != 0) { 57 tls_error_set(error, "failed to stat %s file '%s'", 58 filetype, filename); 59 goto err; 60 } 61 if (st.st_size < 0) 62 goto err; 63 *len = (size_t)st.st_size; 64 if ((*buf = malloc(*len)) == NULL) { 65 tls_error_set(error, "failed to allocate buffer for " 66 "%s file", filetype); 67 goto err; 68 } 69 n = read(fd, *buf, *len); 70 if (n < 0 || (size_t)n != *len) { 71 tls_error_set(error, "failed to read %s file '%s'", 72 filetype, filename); 73 goto err; 74 } 75 close(fd); 76 return 0; 77 78 err: 79 if (fd != -1) 80 close(fd); 81 freezero(*buf, *len); 82 *buf = NULL; 83 *len = 0; 84 85 return -1; 86 } 87 88 struct tls_config * 89 tls_config_new_internal(void) 90 { 91 struct tls_config *config; 92 unsigned char sid[TLS_MAX_SESSION_ID_LENGTH]; 93 94 if ((config = calloc(1, sizeof(*config))) == NULL) 95 return (NULL); 96 97 if (pthread_mutex_init(&config->mutex, NULL) != 0) 98 goto err; 99 100 config->refcount = 1; 101 config->session_fd = -1; 102 103 if ((config->keypair = tls_keypair_new()) == NULL) 104 goto err; 105 106 /* 107 * Default configuration. 108 */ 109 if (tls_config_set_dheparams(config, "none") != 0) 110 goto err; 111 if (tls_config_set_ecdhecurves(config, "default") != 0) 112 goto err; 113 if (tls_config_set_ciphers(config, "secure") != 0) 114 goto err; 115 116 if (tls_config_set_protocols(config, TLS_PROTOCOLS_DEFAULT) != 0) 117 goto err; 118 if (tls_config_set_verify_depth(config, 6) != 0) 119 goto err; 120 121 /* 122 * Set session ID context to a random value. For the simple case 123 * of a single process server this is good enough. For multiprocess 124 * servers the session ID needs to be set by the caller. 125 */ 126 arc4random_buf(sid, sizeof(sid)); 127 if (tls_config_set_session_id(config, sid, sizeof(sid)) != 0) 128 goto err; 129 config->ticket_keyrev = arc4random(); 130 config->ticket_autorekey = 1; 131 132 tls_config_prefer_ciphers_server(config); 133 134 tls_config_verify(config); 135 136 return (config); 137 138 err: 139 tls_config_free(config); 140 return (NULL); 141 } 142 143 struct tls_config * 144 tls_config_new(void) 145 { 146 if (tls_init() == -1) 147 return (NULL); 148 149 return tls_config_new_internal(); 150 } 151 152 void 153 tls_config_free(struct tls_config *config) 154 { 155 struct tls_keypair *kp, *nkp; 156 int refcount; 157 158 if (config == NULL) 159 return; 160 161 pthread_mutex_lock(&config->mutex); 162 refcount = --config->refcount; 163 pthread_mutex_unlock(&config->mutex); 164 165 if (refcount > 0) 166 return; 167 168 for (kp = config->keypair; kp != NULL; kp = nkp) { 169 nkp = kp->next; 170 tls_keypair_free(kp); 171 } 172 173 free(config->error.msg); 174 175 free(config->alpn); 176 free((char *)config->ca_mem); 177 free((char *)config->ca_path); 178 free((char *)config->ciphers); 179 free((char *)config->crl_mem); 180 free(config->ecdhecurves); 181 182 free(config); 183 } 184 185 static void 186 tls_config_keypair_add(struct tls_config *config, struct tls_keypair *keypair) 187 { 188 struct tls_keypair *kp; 189 190 kp = config->keypair; 191 while (kp->next != NULL) 192 kp = kp->next; 193 194 kp->next = keypair; 195 } 196 197 const char * 198 tls_config_error(struct tls_config *config) 199 { 200 return config->error.msg; 201 } 202 203 void 204 tls_config_clear_keys(struct tls_config *config) 205 { 206 struct tls_keypair *kp; 207 208 for (kp = config->keypair; kp != NULL; kp = kp->next) 209 tls_keypair_clear_key(kp); 210 } 211 212 int 213 tls_config_parse_protocols(uint32_t *protocols, const char *protostr) 214 { 215 uint32_t proto, protos = 0; 216 char *s, *p, *q; 217 int negate; 218 219 if (protostr == NULL) { 220 *protocols = TLS_PROTOCOLS_DEFAULT; 221 return (0); 222 } 223 224 if ((s = strdup(protostr)) == NULL) 225 return (-1); 226 227 q = s; 228 while ((p = strsep(&q, ",:")) != NULL) { 229 while (*p == ' ' || *p == '\t') 230 p++; 231 232 negate = 0; 233 if (*p == '!') { 234 negate = 1; 235 p++; 236 } 237 238 if (negate && protos == 0) 239 protos = TLS_PROTOCOLS_ALL; 240 241 proto = 0; 242 if (strcasecmp(p, "all") == 0 || 243 strcasecmp(p, "legacy") == 0) 244 proto = TLS_PROTOCOLS_ALL; 245 else if (strcasecmp(p, "default") == 0 || 246 strcasecmp(p, "secure") == 0) 247 proto = TLS_PROTOCOLS_DEFAULT; 248 if (strcasecmp(p, "tlsv1") == 0) 249 proto = TLS_PROTOCOL_TLSv1; 250 else if (strcasecmp(p, "tlsv1.0") == 0) 251 proto = TLS_PROTOCOL_TLSv1_0; 252 else if (strcasecmp(p, "tlsv1.1") == 0) 253 proto = TLS_PROTOCOL_TLSv1_1; 254 else if (strcasecmp(p, "tlsv1.2") == 0) 255 proto = TLS_PROTOCOL_TLSv1_2; 256 257 if (proto == 0) { 258 free(s); 259 return (-1); 260 } 261 262 if (negate) 263 protos &= ~proto; 264 else 265 protos |= proto; 266 } 267 268 *protocols = protos; 269 270 free(s); 271 272 return (0); 273 } 274 275 static int 276 tls_config_parse_alpn(struct tls_config *config, const char *alpn, 277 char **alpn_data, size_t *alpn_len) 278 { 279 size_t buf_len, i, len; 280 char *buf = NULL; 281 char *s = NULL; 282 char *p, *q; 283 284 free(*alpn_data); 285 *alpn_data = NULL; 286 *alpn_len = 0; 287 288 if ((buf_len = strlen(alpn) + 1) > 65535) { 289 tls_config_set_errorx(config, "alpn too large"); 290 goto err; 291 } 292 293 if ((buf = malloc(buf_len)) == NULL) { 294 tls_config_set_errorx(config, "out of memory"); 295 goto err; 296 } 297 298 if ((s = strdup(alpn)) == NULL) { 299 tls_config_set_errorx(config, "out of memory"); 300 goto err; 301 } 302 303 i = 0; 304 q = s; 305 while ((p = strsep(&q, ",")) != NULL) { 306 if ((len = strlen(p)) == 0) { 307 tls_config_set_errorx(config, 308 "alpn protocol with zero length"); 309 goto err; 310 } 311 if (len > 255) { 312 tls_config_set_errorx(config, 313 "alpn protocol too long"); 314 goto err; 315 } 316 buf[i++] = len & 0xff; 317 memcpy(&buf[i], p, len); 318 i += len; 319 } 320 321 free(s); 322 323 *alpn_data = buf; 324 *alpn_len = buf_len; 325 326 return (0); 327 328 err: 329 free(buf); 330 free(s); 331 332 return (-1); 333 } 334 335 int 336 tls_config_set_alpn(struct tls_config *config, const char *alpn) 337 { 338 return tls_config_parse_alpn(config, alpn, &config->alpn, 339 &config->alpn_len); 340 } 341 342 static int 343 tls_config_add_keypair_file_internal(struct tls_config *config, 344 const char *cert_file, const char *key_file, const char *ocsp_file) 345 { 346 struct tls_keypair *keypair; 347 348 if ((keypair = tls_keypair_new()) == NULL) 349 return (-1); 350 if (tls_keypair_set_cert_file(keypair, &config->error, cert_file) != 0) 351 goto err; 352 if (tls_keypair_set_key_file(keypair, &config->error, key_file) != 0) 353 goto err; 354 if (ocsp_file != NULL && 355 tls_keypair_set_ocsp_staple_file(keypair, &config->error, 356 ocsp_file) != 0) 357 goto err; 358 359 tls_config_keypair_add(config, keypair); 360 361 return (0); 362 363 err: 364 tls_keypair_free(keypair); 365 return (-1); 366 } 367 368 static int 369 tls_config_add_keypair_mem_internal(struct tls_config *config, const uint8_t *cert, 370 size_t cert_len, const uint8_t *key, size_t key_len, 371 const uint8_t *staple, size_t staple_len) 372 { 373 struct tls_keypair *keypair; 374 375 if ((keypair = tls_keypair_new()) == NULL) 376 return (-1); 377 if (tls_keypair_set_cert_mem(keypair, &config->error, cert, cert_len) != 0) 378 goto err; 379 if (tls_keypair_set_key_mem(keypair, &config->error, key, key_len) != 0) 380 goto err; 381 if (staple != NULL && 382 tls_keypair_set_ocsp_staple_mem(keypair, &config->error, staple, 383 staple_len) != 0) 384 goto err; 385 386 tls_config_keypair_add(config, keypair); 387 388 return (0); 389 390 err: 391 tls_keypair_free(keypair); 392 return (-1); 393 } 394 395 int 396 tls_config_add_keypair_mem(struct tls_config *config, const uint8_t *cert, 397 size_t cert_len, const uint8_t *key, size_t key_len) 398 { 399 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key, 400 key_len, NULL, 0); 401 } 402 403 int 404 tls_config_add_keypair_file(struct tls_config *config, 405 const char *cert_file, const char *key_file) 406 { 407 return tls_config_add_keypair_file_internal(config, cert_file, 408 key_file, NULL); 409 } 410 411 int 412 tls_config_add_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert, 413 size_t cert_len, const uint8_t *key, size_t key_len, const uint8_t *staple, 414 size_t staple_len) 415 { 416 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key, 417 key_len, staple, staple_len); 418 } 419 420 int 421 tls_config_add_keypair_ocsp_file(struct tls_config *config, 422 const char *cert_file, const char *key_file, const char *ocsp_file) 423 { 424 return tls_config_add_keypair_file_internal(config, cert_file, 425 key_file, ocsp_file); 426 } 427 428 int 429 tls_config_set_ca_file(struct tls_config *config, const char *ca_file) 430 { 431 return tls_config_load_file(&config->error, "CA", ca_file, 432 &config->ca_mem, &config->ca_len); 433 } 434 435 int 436 tls_config_set_ca_path(struct tls_config *config, const char *ca_path) 437 { 438 return tls_set_string(&config->ca_path, ca_path); 439 } 440 441 int 442 tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len) 443 { 444 return tls_set_mem(&config->ca_mem, &config->ca_len, ca, len); 445 } 446 447 int 448 tls_config_set_cert_file(struct tls_config *config, const char *cert_file) 449 { 450 return tls_keypair_set_cert_file(config->keypair, &config->error, 451 cert_file); 452 } 453 454 int 455 tls_config_set_cert_mem(struct tls_config *config, const uint8_t *cert, 456 size_t len) 457 { 458 return tls_keypair_set_cert_mem(config->keypair, &config->error, 459 cert, len); 460 } 461 462 int 463 tls_config_set_ciphers(struct tls_config *config, const char *ciphers) 464 { 465 SSL_CTX *ssl_ctx = NULL; 466 467 if (ciphers == NULL || 468 strcasecmp(ciphers, "default") == 0 || 469 strcasecmp(ciphers, "secure") == 0) 470 ciphers = TLS_CIPHERS_DEFAULT; 471 else if (strcasecmp(ciphers, "compat") == 0) 472 ciphers = TLS_CIPHERS_COMPAT; 473 else if (strcasecmp(ciphers, "legacy") == 0) 474 ciphers = TLS_CIPHERS_LEGACY; 475 else if (strcasecmp(ciphers, "all") == 0 || 476 strcasecmp(ciphers, "insecure") == 0) 477 ciphers = TLS_CIPHERS_ALL; 478 479 if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) { 480 tls_config_set_errorx(config, "out of memory"); 481 goto err; 482 } 483 if (SSL_CTX_set_cipher_list(ssl_ctx, ciphers) != 1) { 484 tls_config_set_errorx(config, "no ciphers for '%s'", ciphers); 485 goto err; 486 } 487 488 SSL_CTX_free(ssl_ctx); 489 return tls_set_string(&config->ciphers, ciphers); 490 491 err: 492 SSL_CTX_free(ssl_ctx); 493 return -1; 494 } 495 496 int 497 tls_config_set_crl_file(struct tls_config *config, const char *crl_file) 498 { 499 return tls_config_load_file(&config->error, "CRL", crl_file, 500 &config->crl_mem, &config->crl_len); 501 } 502 503 int 504 tls_config_set_crl_mem(struct tls_config *config, const uint8_t *crl, 505 size_t len) 506 { 507 return tls_set_mem(&config->crl_mem, &config->crl_len, crl, len); 508 } 509 510 int 511 tls_config_set_dheparams(struct tls_config *config, const char *params) 512 { 513 int keylen; 514 515 if (params == NULL || strcasecmp(params, "none") == 0) 516 keylen = 0; 517 else if (strcasecmp(params, "auto") == 0) 518 keylen = -1; 519 else if (strcasecmp(params, "legacy") == 0) 520 keylen = 1024; 521 else { 522 tls_config_set_errorx(config, "invalid dhe param '%s'", params); 523 return (-1); 524 } 525 526 config->dheparams = keylen; 527 528 return (0); 529 } 530 531 int 532 tls_config_set_ecdhecurve(struct tls_config *config, const char *curve) 533 { 534 if (curve == NULL || 535 strcasecmp(curve, "none") == 0 || 536 strcasecmp(curve, "auto") == 0) { 537 curve = TLS_ECDHE_CURVES; 538 } else if (strchr(curve, ',') != NULL || strchr(curve, ':') != NULL) { 539 tls_config_set_errorx(config, "invalid ecdhe curve '%s'", 540 curve); 541 return (-1); 542 } 543 544 return tls_config_set_ecdhecurves(config, curve); 545 } 546 547 int 548 tls_config_set_ecdhecurves(struct tls_config *config, const char *curves) 549 { 550 int *curves_list = NULL, *curves_new; 551 size_t curves_num = 0; 552 char *cs = NULL; 553 char *p, *q; 554 int rv = -1; 555 int nid; 556 557 free(config->ecdhecurves); 558 config->ecdhecurves = NULL; 559 config->ecdhecurves_len = 0; 560 561 if (curves == NULL || strcasecmp(curves, "default") == 0) 562 curves = TLS_ECDHE_CURVES; 563 564 if ((cs = strdup(curves)) == NULL) { 565 tls_config_set_errorx(config, "out of memory"); 566 goto err; 567 } 568 569 q = cs; 570 while ((p = strsep(&q, ",:")) != NULL) { 571 while (*p == ' ' || *p == '\t') 572 p++; 573 574 nid = OBJ_sn2nid(p); 575 if (nid == NID_undef) 576 nid = OBJ_ln2nid(p); 577 if (nid == NID_undef) 578 nid = EC_curve_nist2nid(p); 579 if (nid == NID_undef) { 580 tls_config_set_errorx(config, 581 "invalid ecdhe curve '%s'", p); 582 goto err; 583 } 584 585 if ((curves_new = reallocarray(curves_list, curves_num + 1, 586 sizeof(int))) == NULL) { 587 tls_config_set_errorx(config, "out of memory"); 588 goto err; 589 } 590 curves_list = curves_new; 591 curves_list[curves_num] = nid; 592 curves_num++; 593 } 594 595 config->ecdhecurves = curves_list; 596 config->ecdhecurves_len = curves_num; 597 curves_list = NULL; 598 599 rv = 0; 600 601 err: 602 free(cs); 603 free(curves_list); 604 605 return (rv); 606 } 607 608 int 609 tls_config_set_key_file(struct tls_config *config, const char *key_file) 610 { 611 return tls_keypair_set_key_file(config->keypair, &config->error, 612 key_file); 613 } 614 615 int 616 tls_config_set_key_mem(struct tls_config *config, const uint8_t *key, 617 size_t len) 618 { 619 return tls_keypair_set_key_mem(config->keypair, &config->error, 620 key, len); 621 } 622 623 static int 624 tls_config_set_keypair_file_internal(struct tls_config *config, 625 const char *cert_file, const char *key_file, const char *ocsp_file) 626 { 627 if (tls_config_set_cert_file(config, cert_file) != 0) 628 return (-1); 629 if (tls_config_set_key_file(config, key_file) != 0) 630 return (-1); 631 if (ocsp_file != NULL && 632 tls_config_set_ocsp_staple_file(config, ocsp_file) != 0) 633 return (-1); 634 635 return (0); 636 } 637 638 static int 639 tls_config_set_keypair_mem_internal(struct tls_config *config, const uint8_t *cert, 640 size_t cert_len, const uint8_t *key, size_t key_len, 641 const uint8_t *staple, size_t staple_len) 642 { 643 if (tls_config_set_cert_mem(config, cert, cert_len) != 0) 644 return (-1); 645 if (tls_config_set_key_mem(config, key, key_len) != 0) 646 return (-1); 647 if ((staple != NULL) && 648 (tls_config_set_ocsp_staple_mem(config, staple, staple_len) != 0)) 649 return (-1); 650 651 return (0); 652 } 653 654 int 655 tls_config_set_keypair_file(struct tls_config *config, 656 const char *cert_file, const char *key_file) 657 { 658 return tls_config_set_keypair_file_internal(config, cert_file, key_file, 659 NULL); 660 } 661 662 int 663 tls_config_set_keypair_mem(struct tls_config *config, const uint8_t *cert, 664 size_t cert_len, const uint8_t *key, size_t key_len) 665 { 666 return tls_config_set_keypair_mem_internal(config, cert, cert_len, 667 key, key_len, NULL, 0); 668 } 669 670 int 671 tls_config_set_keypair_ocsp_file(struct tls_config *config, 672 const char *cert_file, const char *key_file, const char *ocsp_file) 673 { 674 return tls_config_set_keypair_file_internal(config, cert_file, key_file, 675 ocsp_file); 676 } 677 678 int 679 tls_config_set_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert, 680 size_t cert_len, const uint8_t *key, size_t key_len, 681 const uint8_t *staple, size_t staple_len) 682 { 683 return tls_config_set_keypair_mem_internal(config, cert, cert_len, 684 key, key_len, staple, staple_len); 685 } 686 687 688 int 689 tls_config_set_protocols(struct tls_config *config, uint32_t protocols) 690 { 691 config->protocols = protocols; 692 693 return (0); 694 } 695 696 int 697 tls_config_set_session_fd(struct tls_config *config, int session_fd) 698 { 699 struct stat sb; 700 mode_t mugo; 701 702 if (session_fd == -1) { 703 config->session_fd = session_fd; 704 return (0); 705 } 706 707 if (fstat(session_fd, &sb) == -1) { 708 tls_config_set_error(config, "failed to stat session file"); 709 return (-1); 710 } 711 if (!S_ISREG(sb.st_mode)) { 712 tls_config_set_errorx(config, 713 "session file is not a regular file"); 714 return (-1); 715 } 716 717 if (sb.st_uid != getuid()) { 718 tls_config_set_errorx(config, "session file has incorrect " 719 "owner (uid %i != %i)", sb.st_uid, getuid()); 720 return (-1); 721 } 722 mugo = sb.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO); 723 if (mugo != (S_IRUSR|S_IWUSR)) { 724 tls_config_set_errorx(config, "session file has incorrect " 725 "permissions (%o != 600)", mugo); 726 return (-1); 727 } 728 729 config->session_fd = session_fd; 730 731 return (0); 732 } 733 734 int 735 tls_config_set_verify_depth(struct tls_config *config, int verify_depth) 736 { 737 config->verify_depth = verify_depth; 738 739 return (0); 740 } 741 742 void 743 tls_config_prefer_ciphers_client(struct tls_config *config) 744 { 745 config->ciphers_server = 0; 746 } 747 748 void 749 tls_config_prefer_ciphers_server(struct tls_config *config) 750 { 751 config->ciphers_server = 1; 752 } 753 754 void 755 tls_config_insecure_noverifycert(struct tls_config *config) 756 { 757 config->verify_cert = 0; 758 } 759 760 void 761 tls_config_insecure_noverifyname(struct tls_config *config) 762 { 763 config->verify_name = 0; 764 } 765 766 void 767 tls_config_insecure_noverifytime(struct tls_config *config) 768 { 769 config->verify_time = 0; 770 } 771 772 void 773 tls_config_verify(struct tls_config *config) 774 { 775 config->verify_cert = 1; 776 config->verify_name = 1; 777 config->verify_time = 1; 778 } 779 780 void 781 tls_config_ocsp_require_stapling(struct tls_config *config) 782 { 783 config->ocsp_require_stapling = 1; 784 } 785 786 void 787 tls_config_verify_client(struct tls_config *config) 788 { 789 config->verify_client = 1; 790 } 791 792 void 793 tls_config_verify_client_optional(struct tls_config *config) 794 { 795 config->verify_client = 2; 796 } 797 798 void 799 tls_config_skip_private_key_check(struct tls_config *config) 800 { 801 config->skip_private_key_check = 1; 802 } 803 804 int 805 tls_config_set_ocsp_staple_file(struct tls_config *config, const char *staple_file) 806 { 807 return tls_keypair_set_ocsp_staple_file(config->keypair, &config->error, 808 staple_file); 809 } 810 811 int 812 tls_config_set_ocsp_staple_mem(struct tls_config *config, const uint8_t *staple, 813 size_t len) 814 { 815 return tls_keypair_set_ocsp_staple_mem(config->keypair, &config->error, 816 staple, len); 817 } 818 819 int 820 tls_config_set_session_id(struct tls_config *config, 821 const unsigned char *session_id, size_t len) 822 { 823 if (len > TLS_MAX_SESSION_ID_LENGTH) { 824 tls_config_set_errorx(config, "session ID too large"); 825 return (-1); 826 } 827 memset(config->session_id, 0, sizeof(config->session_id)); 828 memcpy(config->session_id, session_id, len); 829 return (0); 830 } 831 832 int 833 tls_config_set_session_lifetime(struct tls_config *config, int lifetime) 834 { 835 if (lifetime > TLS_MAX_SESSION_TIMEOUT) { 836 tls_config_set_errorx(config, "session lifetime too large"); 837 return (-1); 838 } 839 if (lifetime != 0 && lifetime < TLS_MIN_SESSION_TIMEOUT) { 840 tls_config_set_errorx(config, "session lifetime too small"); 841 return (-1); 842 } 843 844 config->session_lifetime = lifetime; 845 return (0); 846 } 847 848 int 849 tls_config_add_ticket_key(struct tls_config *config, uint32_t keyrev, 850 unsigned char *key, size_t keylen) 851 { 852 struct tls_ticket_key newkey; 853 int i; 854 855 if (TLS_TICKET_KEY_SIZE != keylen || 856 sizeof(newkey.aes_key) + sizeof(newkey.hmac_key) > keylen) { 857 tls_config_set_errorx(config, 858 "wrong amount of ticket key data"); 859 return (-1); 860 } 861 862 keyrev = htonl(keyrev); 863 memset(&newkey, 0, sizeof(newkey)); 864 memcpy(newkey.key_name, &keyrev, sizeof(keyrev)); 865 memcpy(newkey.aes_key, key, sizeof(newkey.aes_key)); 866 memcpy(newkey.hmac_key, key + sizeof(newkey.aes_key), 867 sizeof(newkey.hmac_key)); 868 newkey.time = time(NULL); 869 870 for (i = 0; i < TLS_NUM_TICKETS; i++) { 871 struct tls_ticket_key *tk = &config->ticket_keys[i]; 872 if (memcmp(newkey.key_name, tk->key_name, 873 sizeof(tk->key_name)) != 0) 874 continue; 875 876 /* allow re-entry of most recent key */ 877 if (i == 0 && memcmp(newkey.aes_key, tk->aes_key, 878 sizeof(tk->aes_key)) == 0 && memcmp(newkey.hmac_key, 879 tk->hmac_key, sizeof(tk->hmac_key)) == 0) 880 return (0); 881 tls_config_set_errorx(config, "ticket key already present"); 882 return (-1); 883 } 884 885 memmove(&config->ticket_keys[1], &config->ticket_keys[0], 886 sizeof(config->ticket_keys) - sizeof(config->ticket_keys[0])); 887 config->ticket_keys[0] = newkey; 888 889 config->ticket_autorekey = 0; 890 891 return (0); 892 } 893 894 int 895 tls_config_ticket_autorekey(struct tls_config *config) 896 { 897 unsigned char key[TLS_TICKET_KEY_SIZE]; 898 int rv; 899 900 arc4random_buf(key, sizeof(key)); 901 rv = tls_config_add_ticket_key(config, config->ticket_keyrev++, key, 902 sizeof(key)); 903 config->ticket_autorekey = 1; 904 return (rv); 905 } 906