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