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