1 /* $OpenBSD: t1_lib.c,v 1.165.4.1 2020/08/10 18:59:47 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 *session_id, CBS *ticket, 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 long 142 tls1_default_timeout(void) 143 { 144 /* 2 hours, the 24 hours mentioned in the TLSv1 spec 145 * is way too long for http, the cache would over fill */ 146 return (60 * 60 * 2); 147 } 148 149 int 150 tls1_new(SSL *s) 151 { 152 if (!ssl3_new(s)) 153 return (0); 154 s->method->internal->ssl_clear(s); 155 return (1); 156 } 157 158 void 159 tls1_free(SSL *s) 160 { 161 if (s == NULL) 162 return; 163 164 free(s->internal->tlsext_session_ticket); 165 ssl3_free(s); 166 } 167 168 void 169 tls1_clear(SSL *s) 170 { 171 ssl3_clear(s); 172 s->version = s->method->internal->version; 173 } 174 175 static int nid_list[] = { 176 NID_sect163k1, /* sect163k1 (1) */ 177 NID_sect163r1, /* sect163r1 (2) */ 178 NID_sect163r2, /* sect163r2 (3) */ 179 NID_sect193r1, /* sect193r1 (4) */ 180 NID_sect193r2, /* sect193r2 (5) */ 181 NID_sect233k1, /* sect233k1 (6) */ 182 NID_sect233r1, /* sect233r1 (7) */ 183 NID_sect239k1, /* sect239k1 (8) */ 184 NID_sect283k1, /* sect283k1 (9) */ 185 NID_sect283r1, /* sect283r1 (10) */ 186 NID_sect409k1, /* sect409k1 (11) */ 187 NID_sect409r1, /* sect409r1 (12) */ 188 NID_sect571k1, /* sect571k1 (13) */ 189 NID_sect571r1, /* sect571r1 (14) */ 190 NID_secp160k1, /* secp160k1 (15) */ 191 NID_secp160r1, /* secp160r1 (16) */ 192 NID_secp160r2, /* secp160r2 (17) */ 193 NID_secp192k1, /* secp192k1 (18) */ 194 NID_X9_62_prime192v1, /* secp192r1 (19) */ 195 NID_secp224k1, /* secp224k1 (20) */ 196 NID_secp224r1, /* secp224r1 (21) */ 197 NID_secp256k1, /* secp256k1 (22) */ 198 NID_X9_62_prime256v1, /* secp256r1 (23) */ 199 NID_secp384r1, /* secp384r1 (24) */ 200 NID_secp521r1, /* secp521r1 (25) */ 201 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */ 202 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */ 203 NID_brainpoolP512r1, /* brainpoolP512r1 (28) */ 204 NID_X25519, /* X25519 (29) */ 205 }; 206 207 #if 0 208 static const uint8_t ecformats_list[] = { 209 TLSEXT_ECPOINTFORMAT_uncompressed, 210 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, 211 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 212 }; 213 #endif 214 215 static const uint8_t ecformats_default[] = { 216 TLSEXT_ECPOINTFORMAT_uncompressed, 217 }; 218 219 #if 0 220 static const uint16_t eccurves_list[] = { 221 29, /* X25519 (29) */ 222 14, /* sect571r1 (14) */ 223 13, /* sect571k1 (13) */ 224 25, /* secp521r1 (25) */ 225 28, /* brainpoolP512r1 (28) */ 226 11, /* sect409k1 (11) */ 227 12, /* sect409r1 (12) */ 228 27, /* brainpoolP384r1 (27) */ 229 24, /* secp384r1 (24) */ 230 9, /* sect283k1 (9) */ 231 10, /* sect283r1 (10) */ 232 26, /* brainpoolP256r1 (26) */ 233 22, /* secp256k1 (22) */ 234 23, /* secp256r1 (23) */ 235 8, /* sect239k1 (8) */ 236 6, /* sect233k1 (6) */ 237 7, /* sect233r1 (7) */ 238 20, /* secp224k1 (20) */ 239 21, /* secp224r1 (21) */ 240 4, /* sect193r1 (4) */ 241 5, /* sect193r2 (5) */ 242 18, /* secp192k1 (18) */ 243 19, /* secp192r1 (19) */ 244 1, /* sect163k1 (1) */ 245 2, /* sect163r1 (2) */ 246 3, /* sect163r2 (3) */ 247 15, /* secp160k1 (15) */ 248 16, /* secp160r1 (16) */ 249 17, /* secp160r2 (17) */ 250 }; 251 #endif 252 253 static const uint16_t eccurves_client_default[] = { 254 29, /* X25519 (29) */ 255 23, /* secp256r1 (23) */ 256 24, /* secp384r1 (24) */ 257 25, /* secp521r1 (25) */ 258 }; 259 260 static const uint16_t eccurves_server_default[] = { 261 29, /* X25519 (29) */ 262 23, /* secp256r1 (23) */ 263 24, /* secp384r1 (24) */ 264 }; 265 266 int 267 tls1_ec_curve_id2nid(const uint16_t curve_id) 268 { 269 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ 270 if ((curve_id < 1) || 271 ((unsigned int)curve_id > sizeof(nid_list) / sizeof(nid_list[0]))) 272 return 0; 273 return nid_list[curve_id - 1]; 274 } 275 276 uint16_t 277 tls1_ec_nid2curve_id(const int nid) 278 { 279 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ 280 switch (nid) { 281 case NID_sect163k1: /* sect163k1 (1) */ 282 return 1; 283 case NID_sect163r1: /* sect163r1 (2) */ 284 return 2; 285 case NID_sect163r2: /* sect163r2 (3) */ 286 return 3; 287 case NID_sect193r1: /* sect193r1 (4) */ 288 return 4; 289 case NID_sect193r2: /* sect193r2 (5) */ 290 return 5; 291 case NID_sect233k1: /* sect233k1 (6) */ 292 return 6; 293 case NID_sect233r1: /* sect233r1 (7) */ 294 return 7; 295 case NID_sect239k1: /* sect239k1 (8) */ 296 return 8; 297 case NID_sect283k1: /* sect283k1 (9) */ 298 return 9; 299 case NID_sect283r1: /* sect283r1 (10) */ 300 return 10; 301 case NID_sect409k1: /* sect409k1 (11) */ 302 return 11; 303 case NID_sect409r1: /* sect409r1 (12) */ 304 return 12; 305 case NID_sect571k1: /* sect571k1 (13) */ 306 return 13; 307 case NID_sect571r1: /* sect571r1 (14) */ 308 return 14; 309 case NID_secp160k1: /* secp160k1 (15) */ 310 return 15; 311 case NID_secp160r1: /* secp160r1 (16) */ 312 return 16; 313 case NID_secp160r2: /* secp160r2 (17) */ 314 return 17; 315 case NID_secp192k1: /* secp192k1 (18) */ 316 return 18; 317 case NID_X9_62_prime192v1: /* secp192r1 (19) */ 318 return 19; 319 case NID_secp224k1: /* secp224k1 (20) */ 320 return 20; 321 case NID_secp224r1: /* secp224r1 (21) */ 322 return 21; 323 case NID_secp256k1: /* secp256k1 (22) */ 324 return 22; 325 case NID_X9_62_prime256v1: /* secp256r1 (23) */ 326 return 23; 327 case NID_secp384r1: /* secp384r1 (24) */ 328 return 24; 329 case NID_secp521r1: /* secp521r1 (25) */ 330 return 25; 331 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */ 332 return 26; 333 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */ 334 return 27; 335 case NID_brainpoolP512r1: /* brainpoolP512r1 (28) */ 336 return 28; 337 case NID_X25519: /* X25519 (29) */ 338 return 29; 339 default: 340 return 0; 341 } 342 } 343 344 /* 345 * Return the appropriate format list. If client_formats is non-zero, return 346 * the client/session formats. Otherwise return the custom format list if one 347 * exists, or the default formats if a custom list has not been specified. 348 */ 349 void 350 tls1_get_formatlist(SSL *s, int client_formats, const uint8_t **pformats, 351 size_t *pformatslen) 352 { 353 if (client_formats != 0) { 354 *pformats = SSI(s)->tlsext_ecpointformatlist; 355 *pformatslen = SSI(s)->tlsext_ecpointformatlist_length; 356 return; 357 } 358 359 *pformats = s->internal->tlsext_ecpointformatlist; 360 *pformatslen = s->internal->tlsext_ecpointformatlist_length; 361 if (*pformats == NULL) { 362 *pformats = ecformats_default; 363 *pformatslen = sizeof(ecformats_default); 364 } 365 } 366 367 /* 368 * Return the appropriate group list. If client_groups is non-zero, return 369 * the client/session groups. Otherwise return the custom group list if one 370 * exists, or the default groups if a custom list has not been specified. 371 */ 372 void 373 tls1_get_group_list(SSL *s, int client_groups, const uint16_t **pgroups, 374 size_t *pgroupslen) 375 { 376 if (client_groups != 0) { 377 *pgroups = SSI(s)->tlsext_supportedgroups; 378 *pgroupslen = SSI(s)->tlsext_supportedgroups_length; 379 return; 380 } 381 382 *pgroups = s->internal->tlsext_supportedgroups; 383 *pgroupslen = s->internal->tlsext_supportedgroups_length; 384 if (*pgroups != NULL) 385 return; 386 387 if (!s->server) { 388 *pgroups = eccurves_client_default; 389 *pgroupslen = sizeof(eccurves_client_default) / 2; 390 } else { 391 *pgroups = eccurves_server_default; 392 *pgroupslen = sizeof(eccurves_server_default) / 2; 393 } 394 } 395 396 int 397 tls1_set_groups(uint16_t **out_group_ids, size_t *out_group_ids_len, 398 const int *groups, size_t ngroups) 399 { 400 uint16_t *group_ids; 401 size_t i; 402 403 group_ids = calloc(ngroups, sizeof(uint16_t)); 404 if (group_ids == NULL) 405 return 0; 406 407 for (i = 0; i < ngroups; i++) { 408 group_ids[i] = tls1_ec_nid2curve_id(groups[i]); 409 if (group_ids[i] == 0) { 410 free(group_ids); 411 return 0; 412 } 413 } 414 415 free(*out_group_ids); 416 *out_group_ids = group_ids; 417 *out_group_ids_len = ngroups; 418 419 return 1; 420 } 421 422 int 423 tls1_set_group_list(uint16_t **out_group_ids, size_t *out_group_ids_len, 424 const char *groups) 425 { 426 uint16_t *new_group_ids, *group_ids = NULL; 427 size_t ngroups = 0; 428 char *gs, *p, *q; 429 int nid; 430 431 if ((gs = strdup(groups)) == NULL) 432 return 0; 433 434 q = gs; 435 while ((p = strsep(&q, ":")) != NULL) { 436 nid = OBJ_sn2nid(p); 437 if (nid == NID_undef) 438 nid = OBJ_ln2nid(p); 439 if (nid == NID_undef) 440 nid = EC_curve_nist2nid(p); 441 if (nid == NID_undef) 442 goto err; 443 444 if ((new_group_ids = reallocarray(group_ids, ngroups + 1, 445 sizeof(uint16_t))) == NULL) 446 goto err; 447 group_ids = new_group_ids; 448 449 group_ids[ngroups] = tls1_ec_nid2curve_id(nid); 450 if (group_ids[ngroups] == 0) 451 goto err; 452 453 ngroups++; 454 } 455 456 free(gs); 457 free(*out_group_ids); 458 *out_group_ids = group_ids; 459 *out_group_ids_len = ngroups; 460 461 return 1; 462 463 err: 464 free(gs); 465 free(group_ids); 466 467 return 0; 468 } 469 470 /* Check that a curve is one of our preferences. */ 471 int 472 tls1_check_curve(SSL *s, const uint16_t curve_id) 473 { 474 const uint16_t *groups; 475 size_t groupslen, i; 476 477 tls1_get_group_list(s, 0, &groups, &groupslen); 478 479 for (i = 0; i < groupslen; i++) { 480 if (groups[i] == curve_id) 481 return (1); 482 } 483 return (0); 484 } 485 486 int 487 tls1_get_shared_curve(SSL *s) 488 { 489 size_t preflen, supplen, i, j; 490 const uint16_t *pref, *supp; 491 unsigned long server_pref; 492 493 /* Cannot do anything on the client side. */ 494 if (s->server == 0) 495 return (NID_undef); 496 497 /* Return first preference shared curve. */ 498 server_pref = (s->internal->options & SSL_OP_CIPHER_SERVER_PREFERENCE); 499 tls1_get_group_list(s, (server_pref == 0), &pref, &preflen); 500 tls1_get_group_list(s, (server_pref != 0), &supp, &supplen); 501 502 for (i = 0; i < preflen; i++) { 503 for (j = 0; j < supplen; j++) { 504 if (pref[i] == supp[j]) 505 return (tls1_ec_curve_id2nid(pref[i])); 506 } 507 } 508 return (NID_undef); 509 } 510 511 /* For an EC key set TLS ID and required compression based on parameters. */ 512 static int 513 tls1_set_ec_id(uint16_t *curve_id, uint8_t *comp_id, EC_KEY *ec) 514 { 515 const EC_GROUP *grp; 516 const EC_METHOD *meth; 517 int is_prime = 0; 518 int nid, id; 519 520 if (ec == NULL) 521 return (0); 522 523 /* Determine if it is a prime field. */ 524 if ((grp = EC_KEY_get0_group(ec)) == NULL) 525 return (0); 526 if ((meth = EC_GROUP_method_of(grp)) == NULL) 527 return (0); 528 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field) 529 is_prime = 1; 530 531 /* Determine curve ID. */ 532 nid = EC_GROUP_get_curve_name(grp); 533 id = tls1_ec_nid2curve_id(nid); 534 535 /* If we have an ID set it, otherwise set arbitrary explicit curve. */ 536 if (id != 0) 537 *curve_id = id; 538 else 539 *curve_id = is_prime ? 0xff01 : 0xff02; 540 541 /* Specify the compression identifier. */ 542 if (comp_id != NULL) { 543 if (EC_KEY_get0_public_key(ec) == NULL) 544 return (0); 545 546 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { 547 *comp_id = is_prime ? 548 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime : 549 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; 550 } else { 551 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; 552 } 553 } 554 return (1); 555 } 556 557 /* Check that an EC key is compatible with extensions. */ 558 static int 559 tls1_check_ec_key(SSL *s, const uint16_t *curve_id, const uint8_t *comp_id) 560 { 561 size_t groupslen, formatslen, i; 562 const uint16_t *groups; 563 const uint8_t *formats; 564 565 /* 566 * Check point formats extension if present, otherwise everything 567 * is supported (see RFC4492). 568 */ 569 tls1_get_formatlist(s, 1, &formats, &formatslen); 570 if (comp_id != NULL && formats != NULL) { 571 for (i = 0; i < formatslen; i++) { 572 if (formats[i] == *comp_id) 573 break; 574 } 575 if (i == formatslen) 576 return (0); 577 } 578 579 /* 580 * Check curve list if present, otherwise everything is supported. 581 */ 582 tls1_get_group_list(s, 1, &groups, &groupslen); 583 if (curve_id != NULL && groups != NULL) { 584 for (i = 0; i < groupslen; i++) { 585 if (groups[i] == *curve_id) 586 break; 587 } 588 if (i == groupslen) 589 return (0); 590 } 591 592 return (1); 593 } 594 595 /* Check EC server key is compatible with client extensions. */ 596 int 597 tls1_check_ec_server_key(SSL *s) 598 { 599 CERT_PKEY *cpk = s->cert->pkeys + SSL_PKEY_ECC; 600 uint16_t curve_id; 601 uint8_t comp_id; 602 EVP_PKEY *pkey; 603 int rv; 604 605 if (cpk->x509 == NULL || cpk->privatekey == NULL) 606 return (0); 607 if ((pkey = X509_get_pubkey(cpk->x509)) == NULL) 608 return (0); 609 rv = tls1_set_ec_id(&curve_id, &comp_id, pkey->pkey.ec); 610 EVP_PKEY_free(pkey); 611 if (rv != 1) 612 return (0); 613 614 return tls1_check_ec_key(s, &curve_id, &comp_id); 615 } 616 617 int 618 ssl_check_clienthello_tlsext_early(SSL *s) 619 { 620 int ret = SSL_TLSEXT_ERR_NOACK; 621 int al = SSL_AD_UNRECOGNIZED_NAME; 622 623 /* The handling of the ECPointFormats extension is done elsewhere, namely in 624 * ssl3_choose_cipher in s3_lib.c. 625 */ 626 /* The handling of the EllipticCurves extension is done elsewhere, namely in 627 * ssl3_choose_cipher in s3_lib.c. 628 */ 629 630 if (s->ctx != NULL && s->ctx->internal->tlsext_servername_callback != 0) 631 ret = s->ctx->internal->tlsext_servername_callback(s, &al, 632 s->ctx->internal->tlsext_servername_arg); 633 else if (s->initial_ctx != NULL && s->initial_ctx->internal->tlsext_servername_callback != 0) 634 ret = s->initial_ctx->internal->tlsext_servername_callback(s, &al, 635 s->initial_ctx->internal->tlsext_servername_arg); 636 637 switch (ret) { 638 case SSL_TLSEXT_ERR_ALERT_FATAL: 639 ssl3_send_alert(s, SSL3_AL_FATAL, al); 640 return -1; 641 case SSL_TLSEXT_ERR_ALERT_WARNING: 642 ssl3_send_alert(s, SSL3_AL_WARNING, al); 643 return 1; 644 case SSL_TLSEXT_ERR_NOACK: 645 s->internal->servername_done = 0; 646 default: 647 return 1; 648 } 649 } 650 651 int 652 ssl_check_clienthello_tlsext_late(SSL *s) 653 { 654 int ret = SSL_TLSEXT_ERR_OK; 655 int al = 0; /* XXX gcc3 */ 656 657 /* If status request then ask callback what to do. 658 * Note: this must be called after servername callbacks in case 659 * the certificate has changed, and must be called after the cipher 660 * has been chosen because this may influence which certificate is sent 661 */ 662 if ((s->tlsext_status_type != -1) && 663 s->ctx && s->ctx->internal->tlsext_status_cb) { 664 int r; 665 CERT_PKEY *certpkey; 666 certpkey = ssl_get_server_send_pkey(s); 667 /* If no certificate can't return certificate status */ 668 if (certpkey == NULL) { 669 s->internal->tlsext_status_expected = 0; 670 return 1; 671 } 672 /* Set current certificate to one we will use so 673 * SSL_get_certificate et al can pick it up. 674 */ 675 s->cert->key = certpkey; 676 r = s->ctx->internal->tlsext_status_cb(s, 677 s->ctx->internal->tlsext_status_arg); 678 switch (r) { 679 /* We don't want to send a status request response */ 680 case SSL_TLSEXT_ERR_NOACK: 681 s->internal->tlsext_status_expected = 0; 682 break; 683 /* status request response should be sent */ 684 case SSL_TLSEXT_ERR_OK: 685 if (s->internal->tlsext_ocsp_resp) 686 s->internal->tlsext_status_expected = 1; 687 else 688 s->internal->tlsext_status_expected = 0; 689 break; 690 /* something bad happened */ 691 case SSL_TLSEXT_ERR_ALERT_FATAL: 692 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 693 al = SSL_AD_INTERNAL_ERROR; 694 goto err; 695 } 696 } else 697 s->internal->tlsext_status_expected = 0; 698 699 err: 700 switch (ret) { 701 case SSL_TLSEXT_ERR_ALERT_FATAL: 702 ssl3_send_alert(s, SSL3_AL_FATAL, al); 703 return -1; 704 case SSL_TLSEXT_ERR_ALERT_WARNING: 705 ssl3_send_alert(s, SSL3_AL_WARNING, al); 706 return 1; 707 default: 708 return 1; 709 } 710 } 711 712 int 713 ssl_check_serverhello_tlsext(SSL *s) 714 { 715 int ret = SSL_TLSEXT_ERR_NOACK; 716 int al = SSL_AD_UNRECOGNIZED_NAME; 717 718 ret = SSL_TLSEXT_ERR_OK; 719 720 if (s->ctx != NULL && s->ctx->internal->tlsext_servername_callback != 0) 721 ret = s->ctx->internal->tlsext_servername_callback(s, &al, 722 s->ctx->internal->tlsext_servername_arg); 723 else if (s->initial_ctx != NULL && s->initial_ctx->internal->tlsext_servername_callback != 0) 724 ret = s->initial_ctx->internal->tlsext_servername_callback(s, &al, 725 s->initial_ctx->internal->tlsext_servername_arg); 726 727 /* If we've requested certificate status and we wont get one 728 * tell the callback 729 */ 730 if ((s->tlsext_status_type != -1) && !(s->internal->tlsext_status_expected) && 731 s->ctx && s->ctx->internal->tlsext_status_cb) { 732 int r; 733 /* Set resp to NULL, resplen to -1 so callback knows 734 * there is no response. 735 */ 736 free(s->internal->tlsext_ocsp_resp); 737 s->internal->tlsext_ocsp_resp = NULL; 738 s->internal->tlsext_ocsp_resplen = -1; 739 r = s->ctx->internal->tlsext_status_cb(s, 740 s->ctx->internal->tlsext_status_arg); 741 if (r == 0) { 742 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 743 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 744 } 745 if (r < 0) { 746 al = SSL_AD_INTERNAL_ERROR; 747 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 748 } 749 } 750 751 switch (ret) { 752 case SSL_TLSEXT_ERR_ALERT_FATAL: 753 ssl3_send_alert(s, SSL3_AL_FATAL, al); 754 755 return -1; 756 case SSL_TLSEXT_ERR_ALERT_WARNING: 757 ssl3_send_alert(s, SSL3_AL_WARNING, al); 758 759 return 1; 760 case SSL_TLSEXT_ERR_NOACK: 761 s->internal->servername_done = 0; 762 default: 763 return 1; 764 } 765 } 766 767 /* Since the server cache lookup is done early on in the processing of the 768 * ClientHello, and other operations depend on the result, we need to handle 769 * any TLS session ticket extension at the same time. 770 * 771 * session_id: a CBS containing the session ID. 772 * ext_block: a CBS for the ClientHello extensions block. 773 * ret: (output) on return, if a ticket was decrypted, then this is set to 774 * point to the resulting session. 775 * 776 * If s->internal->tls_session_secret_cb is set then we are expecting a pre-shared key 777 * ciphersuite, in which case we have no use for session tickets and one will 778 * never be decrypted, nor will s->internal->tlsext_ticket_expected be set to 1. 779 * 780 * Returns: 781 * -1: fatal error, either from parsing or decrypting the ticket. 782 * 0: no ticket was found (or was ignored, based on settings). 783 * 1: a zero length extension was found, indicating that the client supports 784 * session tickets but doesn't currently have one to offer. 785 * 2: either s->internal->tls_session_secret_cb was set, or a ticket was offered but 786 * couldn't be decrypted because of a non-fatal error. 787 * 3: a ticket was successfully decrypted and *ret was set. 788 * 789 * Side effects: 790 * Sets s->internal->tlsext_ticket_expected to 1 if the server will have to issue 791 * a new session ticket to the client because the client indicated support 792 * (and s->internal->tls_session_secret_cb is NULL) but the client either doesn't have 793 * a session ticket or we couldn't use the one it gave us, or if 794 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. 795 * Otherwise, s->internal->tlsext_ticket_expected is set to 0. 796 */ 797 int 798 tls1_process_ticket(SSL *s, CBS *session_id, CBS *ext_block, SSL_SESSION **ret) 799 { 800 CBS extensions, ext_data; 801 uint16_t ext_type = 0; 802 int r; 803 804 s->internal->tlsext_ticket_expected = 0; 805 *ret = NULL; 806 807 /* 808 * If tickets disabled behave as if no ticket present to permit stateful 809 * resumption. 810 */ 811 if (SSL_get_options(s) & SSL_OP_NO_TICKET) 812 return 0; 813 814 /* 815 * An empty extensions block is valid, but obviously does not contain 816 * a session ticket. 817 */ 818 if (CBS_len(ext_block) == 0) 819 return 0; 820 821 if (!CBS_get_u16_length_prefixed(ext_block, &extensions)) 822 return -1; 823 824 while (CBS_len(&extensions) > 0) { 825 if (!CBS_get_u16(&extensions, &ext_type) || 826 !CBS_get_u16_length_prefixed(&extensions, &ext_data)) 827 return -1; 828 829 if (ext_type == TLSEXT_TYPE_session_ticket) 830 break; 831 } 832 833 if (ext_type != TLSEXT_TYPE_session_ticket) 834 return 0; 835 836 if (CBS_len(&ext_data) == 0) { 837 /* 838 * The client will accept a ticket but does not currently 839 * have one. 840 */ 841 s->internal->tlsext_ticket_expected = 1; 842 return 1; 843 } 844 845 if (s->internal->tls_session_secret_cb != NULL) { 846 /* 847 * Indicate that the ticket could not be decrypted rather than 848 * generating the session from ticket now, trigger abbreviated 849 * handshake based on external mechanism to calculate the master 850 * secret later. 851 */ 852 return 2; 853 } 854 855 r = tls_decrypt_ticket(s, session_id, &ext_data, ret); 856 switch (r) { 857 case 2: /* ticket couldn't be decrypted */ 858 s->internal->tlsext_ticket_expected = 1; 859 return 2; 860 case 3: /* ticket was decrypted */ 861 return r; 862 case 4: /* ticket decrypted but need to renew */ 863 s->internal->tlsext_ticket_expected = 1; 864 return 3; 865 default: /* fatal error */ 866 return -1; 867 } 868 } 869 870 /* tls_decrypt_ticket attempts to decrypt a session ticket. 871 * 872 * session_id: a CBS containing the session ID. 873 * ticket: a CBS containing the body of the session ticket extension. 874 * psess: (output) on return, if a ticket was decrypted, then this is set to 875 * point to the resulting session. 876 * 877 * Returns: 878 * -1: fatal error, either from parsing or decrypting the ticket. 879 * 2: the ticket couldn't be decrypted. 880 * 3: a ticket was successfully decrypted and *psess was set. 881 * 4: same as 3, but the ticket needs to be renewed. 882 */ 883 static int 884 tls_decrypt_ticket(SSL *s, CBS *session_id, CBS *ticket, SSL_SESSION **psess) 885 { 886 CBS ticket_name, ticket_iv, ticket_encdata, ticket_hmac; 887 SSL_SESSION *sess = NULL; 888 unsigned char *sdec = NULL; 889 size_t sdec_len = 0; 890 size_t session_id_len; 891 const unsigned char *p; 892 unsigned char hmac[EVP_MAX_MD_SIZE]; 893 HMAC_CTX *hctx = NULL; 894 EVP_CIPHER_CTX *cctx = NULL; 895 SSL_CTX *tctx = s->initial_ctx; 896 int slen, hlen; 897 int renew_ticket = 0; 898 int ret = -1; 899 900 *psess = NULL; 901 902 if (!CBS_get_bytes(ticket, &ticket_name, 16)) 903 goto derr; 904 905 /* 906 * Initialize session ticket encryption and HMAC contexts. 907 */ 908 if ((cctx = EVP_CIPHER_CTX_new()) == NULL) 909 goto err; 910 if ((hctx = HMAC_CTX_new()) == NULL) 911 goto err; 912 913 if (tctx->internal->tlsext_ticket_key_cb != NULL) { 914 int rv; 915 916 /* 917 * The API guarantees EVP_MAX_IV_LENGTH bytes of space for 918 * the iv to tlsext_ticket_key_cb(). Since the total space 919 * required for a session cookie is never less than this, 920 * this check isn't too strict. The exact check comes later. 921 */ 922 if (CBS_len(ticket) < EVP_MAX_IV_LENGTH) 923 goto derr; 924 925 if ((rv = tctx->internal->tlsext_ticket_key_cb(s, 926 (unsigned char *)CBS_data(&ticket_name), 927 (unsigned char *)CBS_data(ticket), cctx, hctx, 0)) < 0) 928 goto err; 929 if (rv == 0) 930 goto derr; 931 if (rv == 2) 932 renew_ticket = 1; 933 934 /* 935 * Now that the cipher context is initialised, we can extract 936 * the IV since its length is known. 937 */ 938 if (!CBS_get_bytes(ticket, &ticket_iv, 939 EVP_CIPHER_CTX_iv_length(cctx))) 940 goto derr; 941 } else { 942 /* Check that the key name matches. */ 943 if (!CBS_mem_equal(&ticket_name, 944 tctx->internal->tlsext_tick_key_name, 945 sizeof(tctx->internal->tlsext_tick_key_name))) 946 goto derr; 947 if (!CBS_get_bytes(ticket, &ticket_iv, 948 EVP_CIPHER_iv_length(EVP_aes_128_cbc()))) 949 goto derr; 950 if (!EVP_DecryptInit_ex(cctx, EVP_aes_128_cbc(), NULL, 951 tctx->internal->tlsext_tick_aes_key, CBS_data(&ticket_iv))) 952 goto err; 953 if (!HMAC_Init_ex(hctx, tctx->internal->tlsext_tick_hmac_key, 954 sizeof(tctx->internal->tlsext_tick_hmac_key), EVP_sha256(), 955 NULL)) 956 goto err; 957 } 958 959 /* 960 * Attempt to process session ticket. 961 */ 962 963 if ((hlen = HMAC_size(hctx)) < 0) 964 goto err; 965 966 if (hlen > CBS_len(ticket)) 967 goto derr; 968 if (!CBS_get_bytes(ticket, &ticket_encdata, CBS_len(ticket) - hlen)) 969 goto derr; 970 if (!CBS_get_bytes(ticket, &ticket_hmac, hlen)) 971 goto derr; 972 if (CBS_len(ticket) != 0) 973 goto err; 974 975 /* Check HMAC of encrypted ticket. */ 976 if (HMAC_Update(hctx, CBS_data(&ticket_name), 977 CBS_len(&ticket_name)) <= 0) 978 goto err; 979 if (HMAC_Update(hctx, CBS_data(&ticket_iv), 980 CBS_len(&ticket_iv)) <= 0) 981 goto err; 982 if (HMAC_Update(hctx, CBS_data(&ticket_encdata), 983 CBS_len(&ticket_encdata)) <= 0) 984 goto err; 985 if (HMAC_Final(hctx, hmac, &hlen) <= 0) 986 goto err; 987 988 if (!CBS_mem_equal(&ticket_hmac, hmac, hlen)) 989 goto derr; 990 991 /* Attempt to decrypt session data. */ 992 sdec_len = CBS_len(&ticket_encdata); 993 if ((sdec = calloc(1, sdec_len)) == NULL) 994 goto err; 995 if (EVP_DecryptUpdate(cctx, sdec, &slen, CBS_data(&ticket_encdata), 996 CBS_len(&ticket_encdata)) <= 0) 997 goto derr; 998 if (EVP_DecryptFinal_ex(cctx, sdec + slen, &hlen) <= 0) 999 goto derr; 1000 1001 slen += hlen; 1002 1003 /* 1004 * For session parse failures, indicate that we need to send a new 1005 * ticket. 1006 */ 1007 p = sdec; 1008 if ((sess = d2i_SSL_SESSION(NULL, &p, slen)) == NULL) 1009 goto derr; 1010 1011 /* 1012 * The session ID, if non-empty, is used by some clients to detect that 1013 * the ticket has been accepted. So we copy it to the session structure. 1014 * If it is empty set length to zero as required by standard. 1015 */ 1016 if (!CBS_write_bytes(session_id, sess->session_id, 1017 sizeof(sess->session_id), &session_id_len)) 1018 goto err; 1019 sess->session_id_length = (unsigned int)session_id_len; 1020 1021 *psess = sess; 1022 sess = NULL; 1023 1024 if (renew_ticket) 1025 ret = 4; 1026 else 1027 ret = 3; 1028 1029 goto done; 1030 1031 derr: 1032 ret = 2; 1033 goto done; 1034 1035 err: 1036 ret = -1; 1037 goto done; 1038 1039 done: 1040 freezero(sdec, sdec_len); 1041 EVP_CIPHER_CTX_free(cctx); 1042 HMAC_CTX_free(hctx); 1043 SSL_SESSION_free(sess); 1044 1045 if (ret == 2) 1046 ERR_clear_error(); 1047 1048 return ret; 1049 } 1050