1 /* $OpenBSD: t1_lib.c,v 1.176 2020/09/12 17:25:11 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 /* ==================================================================== 59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111 112 #include <stdio.h> 113 114 #include <openssl/evp.h> 115 #include <openssl/hmac.h> 116 #include <openssl/objects.h> 117 #include <openssl/ocsp.h> 118 119 #include "ssl_locl.h" 120 121 #include "bytestring.h" 122 #include "ssl_sigalgs.h" 123 #include "ssl_tlsext.h" 124 125 static int tls_decrypt_ticket(SSL *s, CBS *ticket, int *alert, 126 SSL_SESSION **psess); 127 128 SSL3_ENC_METHOD TLSv1_enc_data = { 129 .enc_flags = 0, 130 }; 131 132 SSL3_ENC_METHOD TLSv1_1_enc_data = { 133 .enc_flags = SSL_ENC_FLAG_EXPLICIT_IV, 134 }; 135 136 SSL3_ENC_METHOD TLSv1_2_enc_data = { 137 .enc_flags = SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS| 138 SSL_ENC_FLAG_SHA256_PRF|SSL_ENC_FLAG_TLS1_2_CIPHERS, 139 }; 140 141 int 142 tls1_new(SSL *s) 143 { 144 if (!ssl3_new(s)) 145 return (0); 146 s->method->internal->ssl_clear(s); 147 return (1); 148 } 149 150 void 151 tls1_free(SSL *s) 152 { 153 if (s == NULL) 154 return; 155 156 free(s->internal->tlsext_session_ticket); 157 ssl3_free(s); 158 } 159 160 void 161 tls1_clear(SSL *s) 162 { 163 ssl3_clear(s); 164 s->version = s->method->internal->version; 165 } 166 167 static int nid_list[] = { 168 NID_sect163k1, /* sect163k1 (1) */ 169 NID_sect163r1, /* sect163r1 (2) */ 170 NID_sect163r2, /* sect163r2 (3) */ 171 NID_sect193r1, /* sect193r1 (4) */ 172 NID_sect193r2, /* sect193r2 (5) */ 173 NID_sect233k1, /* sect233k1 (6) */ 174 NID_sect233r1, /* sect233r1 (7) */ 175 NID_sect239k1, /* sect239k1 (8) */ 176 NID_sect283k1, /* sect283k1 (9) */ 177 NID_sect283r1, /* sect283r1 (10) */ 178 NID_sect409k1, /* sect409k1 (11) */ 179 NID_sect409r1, /* sect409r1 (12) */ 180 NID_sect571k1, /* sect571k1 (13) */ 181 NID_sect571r1, /* sect571r1 (14) */ 182 NID_secp160k1, /* secp160k1 (15) */ 183 NID_secp160r1, /* secp160r1 (16) */ 184 NID_secp160r2, /* secp160r2 (17) */ 185 NID_secp192k1, /* secp192k1 (18) */ 186 NID_X9_62_prime192v1, /* secp192r1 (19) */ 187 NID_secp224k1, /* secp224k1 (20) */ 188 NID_secp224r1, /* secp224r1 (21) */ 189 NID_secp256k1, /* secp256k1 (22) */ 190 NID_X9_62_prime256v1, /* secp256r1 (23) */ 191 NID_secp384r1, /* secp384r1 (24) */ 192 NID_secp521r1, /* secp521r1 (25) */ 193 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */ 194 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */ 195 NID_brainpoolP512r1, /* brainpoolP512r1 (28) */ 196 NID_X25519, /* X25519 (29) */ 197 }; 198 199 #if 0 200 static const uint8_t ecformats_list[] = { 201 TLSEXT_ECPOINTFORMAT_uncompressed, 202 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, 203 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 204 }; 205 #endif 206 207 static const uint8_t ecformats_default[] = { 208 TLSEXT_ECPOINTFORMAT_uncompressed, 209 }; 210 211 #if 0 212 static const uint16_t eccurves_list[] = { 213 29, /* X25519 (29) */ 214 14, /* sect571r1 (14) */ 215 13, /* sect571k1 (13) */ 216 25, /* secp521r1 (25) */ 217 28, /* brainpoolP512r1 (28) */ 218 11, /* sect409k1 (11) */ 219 12, /* sect409r1 (12) */ 220 27, /* brainpoolP384r1 (27) */ 221 24, /* secp384r1 (24) */ 222 9, /* sect283k1 (9) */ 223 10, /* sect283r1 (10) */ 224 26, /* brainpoolP256r1 (26) */ 225 22, /* secp256k1 (22) */ 226 23, /* secp256r1 (23) */ 227 8, /* sect239k1 (8) */ 228 6, /* sect233k1 (6) */ 229 7, /* sect233r1 (7) */ 230 20, /* secp224k1 (20) */ 231 21, /* secp224r1 (21) */ 232 4, /* sect193r1 (4) */ 233 5, /* sect193r2 (5) */ 234 18, /* secp192k1 (18) */ 235 19, /* secp192r1 (19) */ 236 1, /* sect163k1 (1) */ 237 2, /* sect163r1 (2) */ 238 3, /* sect163r2 (3) */ 239 15, /* secp160k1 (15) */ 240 16, /* secp160r1 (16) */ 241 17, /* secp160r2 (17) */ 242 }; 243 #endif 244 245 static const uint16_t eccurves_client_default[] = { 246 29, /* X25519 (29) */ 247 23, /* secp256r1 (23) */ 248 24, /* secp384r1 (24) */ 249 25, /* secp521r1 (25) */ 250 }; 251 252 static const uint16_t eccurves_server_default[] = { 253 29, /* X25519 (29) */ 254 23, /* secp256r1 (23) */ 255 24, /* secp384r1 (24) */ 256 }; 257 258 int 259 tls1_ec_curve_id2nid(const uint16_t curve_id) 260 { 261 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ 262 if ((curve_id < 1) || 263 ((unsigned int)curve_id > sizeof(nid_list) / sizeof(nid_list[0]))) 264 return 0; 265 return nid_list[curve_id - 1]; 266 } 267 268 uint16_t 269 tls1_ec_nid2curve_id(const int nid) 270 { 271 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ 272 switch (nid) { 273 case NID_sect163k1: /* sect163k1 (1) */ 274 return 1; 275 case NID_sect163r1: /* sect163r1 (2) */ 276 return 2; 277 case NID_sect163r2: /* sect163r2 (3) */ 278 return 3; 279 case NID_sect193r1: /* sect193r1 (4) */ 280 return 4; 281 case NID_sect193r2: /* sect193r2 (5) */ 282 return 5; 283 case NID_sect233k1: /* sect233k1 (6) */ 284 return 6; 285 case NID_sect233r1: /* sect233r1 (7) */ 286 return 7; 287 case NID_sect239k1: /* sect239k1 (8) */ 288 return 8; 289 case NID_sect283k1: /* sect283k1 (9) */ 290 return 9; 291 case NID_sect283r1: /* sect283r1 (10) */ 292 return 10; 293 case NID_sect409k1: /* sect409k1 (11) */ 294 return 11; 295 case NID_sect409r1: /* sect409r1 (12) */ 296 return 12; 297 case NID_sect571k1: /* sect571k1 (13) */ 298 return 13; 299 case NID_sect571r1: /* sect571r1 (14) */ 300 return 14; 301 case NID_secp160k1: /* secp160k1 (15) */ 302 return 15; 303 case NID_secp160r1: /* secp160r1 (16) */ 304 return 16; 305 case NID_secp160r2: /* secp160r2 (17) */ 306 return 17; 307 case NID_secp192k1: /* secp192k1 (18) */ 308 return 18; 309 case NID_X9_62_prime192v1: /* secp192r1 (19) */ 310 return 19; 311 case NID_secp224k1: /* secp224k1 (20) */ 312 return 20; 313 case NID_secp224r1: /* secp224r1 (21) */ 314 return 21; 315 case NID_secp256k1: /* secp256k1 (22) */ 316 return 22; 317 case NID_X9_62_prime256v1: /* secp256r1 (23) */ 318 return 23; 319 case NID_secp384r1: /* secp384r1 (24) */ 320 return 24; 321 case NID_secp521r1: /* secp521r1 (25) */ 322 return 25; 323 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */ 324 return 26; 325 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */ 326 return 27; 327 case NID_brainpoolP512r1: /* brainpoolP512r1 (28) */ 328 return 28; 329 case NID_X25519: /* X25519 (29) */ 330 return 29; 331 default: 332 return 0; 333 } 334 } 335 336 /* 337 * Return the appropriate format list. If client_formats is non-zero, return 338 * the client/session formats. Otherwise return the custom format list if one 339 * exists, or the default formats if a custom list has not been specified. 340 */ 341 void 342 tls1_get_formatlist(SSL *s, int client_formats, const uint8_t **pformats, 343 size_t *pformatslen) 344 { 345 if (client_formats != 0) { 346 *pformats = SSI(s)->tlsext_ecpointformatlist; 347 *pformatslen = SSI(s)->tlsext_ecpointformatlist_length; 348 return; 349 } 350 351 *pformats = s->internal->tlsext_ecpointformatlist; 352 *pformatslen = s->internal->tlsext_ecpointformatlist_length; 353 if (*pformats == NULL) { 354 *pformats = ecformats_default; 355 *pformatslen = sizeof(ecformats_default); 356 } 357 } 358 359 /* 360 * Return the appropriate group list. If client_groups is non-zero, return 361 * the client/session groups. Otherwise return the custom group list if one 362 * exists, or the default groups if a custom list has not been specified. 363 */ 364 void 365 tls1_get_group_list(SSL *s, int client_groups, const uint16_t **pgroups, 366 size_t *pgroupslen) 367 { 368 if (client_groups != 0) { 369 *pgroups = SSI(s)->tlsext_supportedgroups; 370 *pgroupslen = SSI(s)->tlsext_supportedgroups_length; 371 return; 372 } 373 374 *pgroups = s->internal->tlsext_supportedgroups; 375 *pgroupslen = s->internal->tlsext_supportedgroups_length; 376 if (*pgroups != NULL) 377 return; 378 379 if (!s->server) { 380 *pgroups = eccurves_client_default; 381 *pgroupslen = sizeof(eccurves_client_default) / 2; 382 } else { 383 *pgroups = eccurves_server_default; 384 *pgroupslen = sizeof(eccurves_server_default) / 2; 385 } 386 } 387 388 int 389 tls1_set_groups(uint16_t **out_group_ids, size_t *out_group_ids_len, 390 const int *groups, size_t ngroups) 391 { 392 uint16_t *group_ids; 393 size_t i; 394 395 group_ids = calloc(ngroups, sizeof(uint16_t)); 396 if (group_ids == NULL) 397 return 0; 398 399 for (i = 0; i < ngroups; i++) { 400 group_ids[i] = tls1_ec_nid2curve_id(groups[i]); 401 if (group_ids[i] == 0) { 402 free(group_ids); 403 return 0; 404 } 405 } 406 407 free(*out_group_ids); 408 *out_group_ids = group_ids; 409 *out_group_ids_len = ngroups; 410 411 return 1; 412 } 413 414 int 415 tls1_set_group_list(uint16_t **out_group_ids, size_t *out_group_ids_len, 416 const char *groups) 417 { 418 uint16_t *new_group_ids, *group_ids = NULL; 419 size_t ngroups = 0; 420 char *gs, *p, *q; 421 int nid; 422 423 if ((gs = strdup(groups)) == NULL) 424 return 0; 425 426 q = gs; 427 while ((p = strsep(&q, ":")) != NULL) { 428 nid = OBJ_sn2nid(p); 429 if (nid == NID_undef) 430 nid = OBJ_ln2nid(p); 431 if (nid == NID_undef) 432 nid = EC_curve_nist2nid(p); 433 if (nid == NID_undef) 434 goto err; 435 436 if ((new_group_ids = reallocarray(group_ids, ngroups + 1, 437 sizeof(uint16_t))) == NULL) 438 goto err; 439 group_ids = new_group_ids; 440 441 group_ids[ngroups] = tls1_ec_nid2curve_id(nid); 442 if (group_ids[ngroups] == 0) 443 goto err; 444 445 ngroups++; 446 } 447 448 free(gs); 449 free(*out_group_ids); 450 *out_group_ids = group_ids; 451 *out_group_ids_len = ngroups; 452 453 return 1; 454 455 err: 456 free(gs); 457 free(group_ids); 458 459 return 0; 460 } 461 462 /* Check that a curve is one of our preferences. */ 463 int 464 tls1_check_curve(SSL *s, const uint16_t curve_id) 465 { 466 const uint16_t *groups; 467 size_t groupslen, i; 468 469 tls1_get_group_list(s, 0, &groups, &groupslen); 470 471 for (i = 0; i < groupslen; i++) { 472 if (groups[i] == curve_id) 473 return (1); 474 } 475 return (0); 476 } 477 478 int 479 tls1_get_shared_curve(SSL *s) 480 { 481 size_t preflen, supplen, i, j; 482 const uint16_t *pref, *supp; 483 unsigned long server_pref; 484 485 /* Cannot do anything on the client side. */ 486 if (s->server == 0) 487 return (NID_undef); 488 489 /* Return first preference shared curve. */ 490 server_pref = (s->internal->options & SSL_OP_CIPHER_SERVER_PREFERENCE); 491 tls1_get_group_list(s, (server_pref == 0), &pref, &preflen); 492 tls1_get_group_list(s, (server_pref != 0), &supp, &supplen); 493 494 for (i = 0; i < preflen; i++) { 495 for (j = 0; j < supplen; j++) { 496 if (pref[i] == supp[j]) 497 return (tls1_ec_curve_id2nid(pref[i])); 498 } 499 } 500 return (NID_undef); 501 } 502 503 /* For an EC key set TLS ID and required compression based on parameters. */ 504 static int 505 tls1_set_ec_id(uint16_t *curve_id, uint8_t *comp_id, EC_KEY *ec) 506 { 507 const EC_GROUP *grp; 508 const EC_METHOD *meth; 509 int prime_field; 510 int nid; 511 512 if (ec == NULL) 513 return (0); 514 515 /* Determine whether the curve is defined over a prime field. */ 516 if ((grp = EC_KEY_get0_group(ec)) == NULL) 517 return (0); 518 if ((meth = EC_GROUP_method_of(grp)) == NULL) 519 return (0); 520 prime_field = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field); 521 522 /* Determine curve ID - NID_undef results in a curve ID of zero. */ 523 nid = EC_GROUP_get_curve_name(grp); 524 /* If we have an ID set it, otherwise set arbitrary explicit curve. */ 525 if ((*curve_id = tls1_ec_nid2curve_id(nid)) == 0) 526 *curve_id = prime_field ? 0xff01 : 0xff02; 527 528 if (comp_id == NULL) 529 return (1); 530 531 /* Specify the compression identifier. */ 532 if (EC_KEY_get0_public_key(ec) == NULL) 533 return (0); 534 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; 535 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { 536 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; 537 if (prime_field) 538 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; 539 } 540 541 return (1); 542 } 543 544 /* Check that an EC key is compatible with extensions. */ 545 static int 546 tls1_check_ec_key(SSL *s, const uint16_t *curve_id, const uint8_t *comp_id) 547 { 548 size_t groupslen, formatslen, i; 549 const uint16_t *groups; 550 const uint8_t *formats; 551 552 /* 553 * Check point formats extension if present, otherwise everything 554 * is supported (see RFC4492). 555 */ 556 tls1_get_formatlist(s, 1, &formats, &formatslen); 557 if (comp_id != NULL && formats != NULL) { 558 for (i = 0; i < formatslen; i++) { 559 if (formats[i] == *comp_id) 560 break; 561 } 562 if (i == formatslen) 563 return (0); 564 } 565 566 /* 567 * Check curve list if present, otherwise everything is supported. 568 */ 569 tls1_get_group_list(s, 1, &groups, &groupslen); 570 if (curve_id != NULL && groups != NULL) { 571 for (i = 0; i < groupslen; i++) { 572 if (groups[i] == *curve_id) 573 break; 574 } 575 if (i == groupslen) 576 return (0); 577 } 578 579 return (1); 580 } 581 582 /* Check EC server key is compatible with client extensions. */ 583 int 584 tls1_check_ec_server_key(SSL *s) 585 { 586 CERT_PKEY *cpk = s->cert->pkeys + SSL_PKEY_ECC; 587 uint16_t curve_id; 588 uint8_t comp_id; 589 EVP_PKEY *pkey; 590 int rv; 591 592 if (cpk->x509 == NULL || cpk->privatekey == NULL) 593 return (0); 594 if ((pkey = X509_get_pubkey(cpk->x509)) == NULL) 595 return (0); 596 rv = tls1_set_ec_id(&curve_id, &comp_id, pkey->pkey.ec); 597 EVP_PKEY_free(pkey); 598 if (rv != 1) 599 return (0); 600 601 return tls1_check_ec_key(s, &curve_id, &comp_id); 602 } 603 604 int 605 ssl_check_clienthello_tlsext_early(SSL *s) 606 { 607 int ret = SSL_TLSEXT_ERR_NOACK; 608 int al = SSL_AD_UNRECOGNIZED_NAME; 609 610 /* The handling of the ECPointFormats extension is done elsewhere, namely in 611 * ssl3_choose_cipher in s3_lib.c. 612 */ 613 /* The handling of the EllipticCurves extension is done elsewhere, namely in 614 * ssl3_choose_cipher in s3_lib.c. 615 */ 616 617 if (s->ctx != NULL && s->ctx->internal->tlsext_servername_callback != 0) 618 ret = s->ctx->internal->tlsext_servername_callback(s, &al, 619 s->ctx->internal->tlsext_servername_arg); 620 else if (s->initial_ctx != NULL && s->initial_ctx->internal->tlsext_servername_callback != 0) 621 ret = s->initial_ctx->internal->tlsext_servername_callback(s, &al, 622 s->initial_ctx->internal->tlsext_servername_arg); 623 624 switch (ret) { 625 case SSL_TLSEXT_ERR_ALERT_FATAL: 626 ssl3_send_alert(s, SSL3_AL_FATAL, al); 627 return -1; 628 case SSL_TLSEXT_ERR_ALERT_WARNING: 629 ssl3_send_alert(s, SSL3_AL_WARNING, al); 630 return 1; 631 case SSL_TLSEXT_ERR_NOACK: 632 default: 633 return 1; 634 } 635 } 636 637 int 638 ssl_check_clienthello_tlsext_late(SSL *s) 639 { 640 int ret = SSL_TLSEXT_ERR_OK; 641 int al = 0; /* XXX gcc3 */ 642 643 /* If status request then ask callback what to do. 644 * Note: this must be called after servername callbacks in case 645 * the certificate has changed, and must be called after the cipher 646 * has been chosen because this may influence which certificate is sent 647 */ 648 if ((s->tlsext_status_type != -1) && 649 s->ctx && s->ctx->internal->tlsext_status_cb) { 650 int r; 651 CERT_PKEY *certpkey; 652 certpkey = ssl_get_server_send_pkey(s); 653 /* If no certificate can't return certificate status */ 654 if (certpkey == NULL) { 655 s->internal->tlsext_status_expected = 0; 656 return 1; 657 } 658 /* Set current certificate to one we will use so 659 * SSL_get_certificate et al can pick it up. 660 */ 661 s->cert->key = certpkey; 662 r = s->ctx->internal->tlsext_status_cb(s, 663 s->ctx->internal->tlsext_status_arg); 664 switch (r) { 665 /* We don't want to send a status request response */ 666 case SSL_TLSEXT_ERR_NOACK: 667 s->internal->tlsext_status_expected = 0; 668 break; 669 /* status request response should be sent */ 670 case SSL_TLSEXT_ERR_OK: 671 if (s->internal->tlsext_ocsp_resp) 672 s->internal->tlsext_status_expected = 1; 673 else 674 s->internal->tlsext_status_expected = 0; 675 break; 676 /* something bad happened */ 677 case SSL_TLSEXT_ERR_ALERT_FATAL: 678 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 679 al = SSL_AD_INTERNAL_ERROR; 680 goto err; 681 } 682 } else 683 s->internal->tlsext_status_expected = 0; 684 685 err: 686 switch (ret) { 687 case SSL_TLSEXT_ERR_ALERT_FATAL: 688 ssl3_send_alert(s, SSL3_AL_FATAL, al); 689 return -1; 690 case SSL_TLSEXT_ERR_ALERT_WARNING: 691 ssl3_send_alert(s, SSL3_AL_WARNING, al); 692 return 1; 693 default: 694 return 1; 695 } 696 } 697 698 int 699 ssl_check_serverhello_tlsext(SSL *s) 700 { 701 int ret = SSL_TLSEXT_ERR_NOACK; 702 int al = SSL_AD_UNRECOGNIZED_NAME; 703 704 ret = SSL_TLSEXT_ERR_OK; 705 706 if (s->ctx != NULL && s->ctx->internal->tlsext_servername_callback != 0) 707 ret = s->ctx->internal->tlsext_servername_callback(s, &al, 708 s->ctx->internal->tlsext_servername_arg); 709 else if (s->initial_ctx != NULL && s->initial_ctx->internal->tlsext_servername_callback != 0) 710 ret = s->initial_ctx->internal->tlsext_servername_callback(s, &al, 711 s->initial_ctx->internal->tlsext_servername_arg); 712 713 /* If we've requested certificate status and we wont get one 714 * tell the callback 715 */ 716 if ((s->tlsext_status_type != -1) && !(s->internal->tlsext_status_expected) && 717 s->ctx && s->ctx->internal->tlsext_status_cb) { 718 int r; 719 720 free(s->internal->tlsext_ocsp_resp); 721 s->internal->tlsext_ocsp_resp = NULL; 722 s->internal->tlsext_ocsp_resp_len = 0; 723 724 r = s->ctx->internal->tlsext_status_cb(s, 725 s->ctx->internal->tlsext_status_arg); 726 if (r == 0) { 727 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 728 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 729 } 730 if (r < 0) { 731 al = SSL_AD_INTERNAL_ERROR; 732 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 733 } 734 } 735 736 switch (ret) { 737 case SSL_TLSEXT_ERR_ALERT_FATAL: 738 ssl3_send_alert(s, SSL3_AL_FATAL, al); 739 return -1; 740 case SSL_TLSEXT_ERR_ALERT_WARNING: 741 ssl3_send_alert(s, SSL3_AL_WARNING, al); 742 return 1; 743 case SSL_TLSEXT_ERR_NOACK: 744 default: 745 return 1; 746 } 747 } 748 749 /* Since the server cache lookup is done early on in the processing of the 750 * ClientHello, and other operations depend on the result, we need to handle 751 * any TLS session ticket extension at the same time. 752 * 753 * ext_block: a CBS for the ClientHello extensions block. 754 * ret: (output) on return, if a ticket was decrypted, then this is set to 755 * point to the resulting session. 756 * 757 * If s->internal->tls_session_secret_cb is set then we are expecting a pre-shared key 758 * ciphersuite, in which case we have no use for session tickets and one will 759 * never be decrypted, nor will s->internal->tlsext_ticket_expected be set to 1. 760 * 761 * Returns: 762 * TLS1_TICKET_FATAL_ERROR: error from parsing or decrypting the ticket. 763 * TLS1_TICKET_NONE: no ticket was found (or was ignored, based on settings). 764 * TLS1_TICKET_EMPTY: a zero length extension was found, indicating that the 765 * client supports session tickets but doesn't currently have one to offer. 766 * TLS1_TICKET_NOT_DECRYPTED: either s->internal->tls_session_secret_cb was 767 * set, or a ticket was offered but couldn't be decrypted because of a 768 * non-fatal error. 769 * TLS1_TICKET_DECRYPTED: a ticket was successfully decrypted and *ret was set. 770 * 771 * Side effects: 772 * Sets s->internal->tlsext_ticket_expected to 1 if the server will have to issue 773 * a new session ticket to the client because the client indicated support 774 * (and s->internal->tls_session_secret_cb is NULL) but the client either doesn't have 775 * a session ticket or we couldn't use the one it gave us, or if 776 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. 777 * Otherwise, s->internal->tlsext_ticket_expected is set to 0. 778 */ 779 int 780 tls1_process_ticket(SSL *s, CBS *ext_block, int *alert, SSL_SESSION **ret) 781 { 782 CBS extensions, ext_data; 783 uint16_t ext_type = 0; 784 785 s->internal->tlsext_ticket_expected = 0; 786 *ret = NULL; 787 788 /* 789 * If tickets disabled behave as if no ticket present to permit stateful 790 * resumption. 791 */ 792 if (SSL_get_options(s) & SSL_OP_NO_TICKET) 793 return TLS1_TICKET_NONE; 794 795 /* 796 * An empty extensions block is valid, but obviously does not contain 797 * a session ticket. 798 */ 799 if (CBS_len(ext_block) == 0) 800 return TLS1_TICKET_NONE; 801 802 if (!CBS_get_u16_length_prefixed(ext_block, &extensions)) { 803 *alert = SSL_AD_DECODE_ERROR; 804 return TLS1_TICKET_FATAL_ERROR; 805 } 806 807 while (CBS_len(&extensions) > 0) { 808 if (!CBS_get_u16(&extensions, &ext_type) || 809 !CBS_get_u16_length_prefixed(&extensions, &ext_data)) { 810 *alert = SSL_AD_DECODE_ERROR; 811 return TLS1_TICKET_FATAL_ERROR; 812 } 813 814 if (ext_type == TLSEXT_TYPE_session_ticket) 815 break; 816 } 817 818 if (ext_type != TLSEXT_TYPE_session_ticket) 819 return TLS1_TICKET_NONE; 820 821 if (CBS_len(&ext_data) == 0) { 822 /* 823 * The client will accept a ticket but does not currently 824 * have one. 825 */ 826 s->internal->tlsext_ticket_expected = 1; 827 return TLS1_TICKET_EMPTY; 828 } 829 830 if (s->internal->tls_session_secret_cb != NULL) { 831 /* 832 * Indicate that the ticket could not be decrypted rather than 833 * generating the session from ticket now, trigger abbreviated 834 * handshake based on external mechanism to calculate the master 835 * secret later. 836 */ 837 return TLS1_TICKET_NOT_DECRYPTED; 838 } 839 840 return tls_decrypt_ticket(s, &ext_data, alert, ret); 841 } 842 843 /* tls_decrypt_ticket attempts to decrypt a session ticket. 844 * 845 * ticket: a CBS containing the body of the session ticket extension. 846 * psess: (output) on return, if a ticket was decrypted, then this is set to 847 * point to the resulting session. 848 * 849 * Returns: 850 * TLS1_TICKET_FATAL_ERROR: error from parsing or decrypting the ticket. 851 * TLS1_TICKET_NOT_DECRYPTED: the ticket couldn't be decrypted. 852 * TLS1_TICKET_DECRYPTED: a ticket was decrypted and *psess was set. 853 */ 854 static int 855 tls_decrypt_ticket(SSL *s, CBS *ticket, int *alert, SSL_SESSION **psess) 856 { 857 CBS ticket_name, ticket_iv, ticket_encdata, ticket_hmac; 858 SSL_SESSION *sess = NULL; 859 unsigned char *sdec = NULL; 860 size_t sdec_len = 0; 861 const unsigned char *p; 862 unsigned char hmac[EVP_MAX_MD_SIZE]; 863 HMAC_CTX *hctx = NULL; 864 EVP_CIPHER_CTX *cctx = NULL; 865 SSL_CTX *tctx = s->initial_ctx; 866 int slen, hlen; 867 int alert_desc = SSL_AD_INTERNAL_ERROR; 868 int ret = TLS1_TICKET_FATAL_ERROR; 869 870 *psess = NULL; 871 872 if (!CBS_get_bytes(ticket, &ticket_name, 16)) 873 goto derr; 874 875 /* 876 * Initialize session ticket encryption and HMAC contexts. 877 */ 878 if ((cctx = EVP_CIPHER_CTX_new()) == NULL) 879 goto err; 880 if ((hctx = HMAC_CTX_new()) == NULL) 881 goto err; 882 883 if (tctx->internal->tlsext_ticket_key_cb != NULL) { 884 int rv; 885 886 /* 887 * The API guarantees EVP_MAX_IV_LENGTH bytes of space for 888 * the iv to tlsext_ticket_key_cb(). Since the total space 889 * required for a session cookie is never less than this, 890 * this check isn't too strict. The exact check comes later. 891 */ 892 if (CBS_len(ticket) < EVP_MAX_IV_LENGTH) 893 goto derr; 894 895 if ((rv = tctx->internal->tlsext_ticket_key_cb(s, 896 (unsigned char *)CBS_data(&ticket_name), 897 (unsigned char *)CBS_data(ticket), cctx, hctx, 0)) < 0) 898 goto err; 899 if (rv == 0) 900 goto derr; 901 if (rv == 2) { 902 /* Renew ticket. */ 903 s->internal->tlsext_ticket_expected = 1; 904 } 905 906 /* 907 * Now that the cipher context is initialised, we can extract 908 * the IV since its length is known. 909 */ 910 if (!CBS_get_bytes(ticket, &ticket_iv, 911 EVP_CIPHER_CTX_iv_length(cctx))) 912 goto derr; 913 } else { 914 /* Check that the key name matches. */ 915 if (!CBS_mem_equal(&ticket_name, 916 tctx->internal->tlsext_tick_key_name, 917 sizeof(tctx->internal->tlsext_tick_key_name))) 918 goto derr; 919 if (!CBS_get_bytes(ticket, &ticket_iv, 920 EVP_CIPHER_iv_length(EVP_aes_128_cbc()))) 921 goto derr; 922 if (!EVP_DecryptInit_ex(cctx, EVP_aes_128_cbc(), NULL, 923 tctx->internal->tlsext_tick_aes_key, CBS_data(&ticket_iv))) 924 goto err; 925 if (!HMAC_Init_ex(hctx, tctx->internal->tlsext_tick_hmac_key, 926 sizeof(tctx->internal->tlsext_tick_hmac_key), EVP_sha256(), 927 NULL)) 928 goto err; 929 } 930 931 /* 932 * Attempt to process session ticket. 933 */ 934 935 if ((hlen = HMAC_size(hctx)) < 0) 936 goto err; 937 938 if (hlen > CBS_len(ticket)) 939 goto derr; 940 if (!CBS_get_bytes(ticket, &ticket_encdata, CBS_len(ticket) - hlen)) 941 goto derr; 942 if (!CBS_get_bytes(ticket, &ticket_hmac, hlen)) 943 goto derr; 944 if (CBS_len(ticket) != 0) { 945 alert_desc = SSL_AD_DECODE_ERROR; 946 goto err; 947 } 948 949 /* Check HMAC of encrypted ticket. */ 950 if (HMAC_Update(hctx, CBS_data(&ticket_name), 951 CBS_len(&ticket_name)) <= 0) 952 goto err; 953 if (HMAC_Update(hctx, CBS_data(&ticket_iv), 954 CBS_len(&ticket_iv)) <= 0) 955 goto err; 956 if (HMAC_Update(hctx, CBS_data(&ticket_encdata), 957 CBS_len(&ticket_encdata)) <= 0) 958 goto err; 959 if (HMAC_Final(hctx, hmac, &hlen) <= 0) 960 goto err; 961 962 if (!CBS_mem_equal(&ticket_hmac, hmac, hlen)) 963 goto derr; 964 965 /* Attempt to decrypt session data. */ 966 sdec_len = CBS_len(&ticket_encdata); 967 if ((sdec = calloc(1, sdec_len)) == NULL) 968 goto err; 969 if (EVP_DecryptUpdate(cctx, sdec, &slen, CBS_data(&ticket_encdata), 970 CBS_len(&ticket_encdata)) <= 0) 971 goto derr; 972 if (EVP_DecryptFinal_ex(cctx, sdec + slen, &hlen) <= 0) 973 goto derr; 974 975 slen += hlen; 976 977 /* 978 * For session parse failures, indicate that we need to send a new 979 * ticket. 980 */ 981 p = sdec; 982 if ((sess = d2i_SSL_SESSION(NULL, &p, slen)) == NULL) 983 goto derr; 984 *psess = sess; 985 sess = NULL; 986 987 ret = TLS1_TICKET_DECRYPTED; 988 goto done; 989 990 derr: 991 ERR_clear_error(); 992 s->internal->tlsext_ticket_expected = 1; 993 ret = TLS1_TICKET_NOT_DECRYPTED; 994 goto done; 995 996 err: 997 *alert = alert_desc; 998 ret = TLS1_TICKET_FATAL_ERROR; 999 goto done; 1000 1001 done: 1002 freezero(sdec, sdec_len); 1003 EVP_CIPHER_CTX_free(cctx); 1004 HMAC_CTX_free(hctx); 1005 SSL_SESSION_free(sess); 1006 1007 return ret; 1008 } 1009