1 /* $OpenBSD: t1_lib.c,v 1.122 2017/07/24 17:39:43 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_tlsext.h" 123 124 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen, 125 const unsigned char *sess_id, int sesslen, 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(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 static 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 curve list. If client_curves is non-zero, return 365 * the client/session curves. Otherwise return the custom curve list if one 366 * exists, or the default curves if a custom list has not been specified. 367 */ 368 static void 369 tls1_get_curvelist(SSL *s, int client_curves, const uint16_t **pcurves, 370 size_t *pcurveslen) 371 { 372 if (client_curves != 0) { 373 *pcurves = SSI(s)->tlsext_supportedgroups; 374 *pcurveslen = SSI(s)->tlsext_supportedgroups_length; 375 return; 376 } 377 378 *pcurves = s->internal->tlsext_supportedgroups; 379 *pcurveslen = s->internal->tlsext_supportedgroups_length; 380 if (*pcurves == NULL) { 381 *pcurves = eccurves_default; 382 *pcurveslen = 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_groups_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 *curves; 465 size_t curveslen, i; 466 467 tls1_get_curvelist(s, 0, &curves, &curveslen); 468 469 for (i = 0; i < curveslen; i++) { 470 if (curves[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_curvelist(s, (server_pref == 0), &pref, &preflen); 490 tls1_get_curvelist(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 curveslen, formatslen, i; 552 const uint16_t *curves; 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_curvelist(s, 1, &curves, &curveslen); 573 if (curve_id != NULL && curves != NULL) { 574 for (i = 0; i < curveslen; i++) { 575 if (curves[i] == *curve_id) 576 break; 577 } 578 if (i == curveslen) 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 /* Check EC temporary key is compatible with client extensions. */ 608 int 609 tls1_check_ec_tmp_key(SSL *s) 610 { 611 EC_KEY *ec = s->cert->ecdh_tmp; 612 uint16_t curve_id; 613 614 if (s->cert->ecdh_tmp_auto != 0) { 615 /* Need a shared curve. */ 616 if (tls1_get_shared_curve(s) != NID_undef) 617 return (1); 618 return (0); 619 } 620 621 if (ec == NULL) { 622 if (s->cert->ecdh_tmp_cb != NULL) 623 return (1); 624 return (0); 625 } 626 if (tls1_set_ec_id(&curve_id, NULL, ec) != 1) 627 return (0); 628 629 return tls1_check_ec_key(s, &curve_id, NULL); 630 } 631 632 /* 633 * List of supported signature algorithms and hashes. Should make this 634 * customisable at some point, for now include everything we support. 635 */ 636 637 static unsigned char tls12_sigalgs[] = { 638 TLSEXT_hash_sha512, TLSEXT_signature_rsa, 639 TLSEXT_hash_sha512, TLSEXT_signature_dsa, 640 TLSEXT_hash_sha512, TLSEXT_signature_ecdsa, 641 #ifndef OPENSSL_NO_GOST 642 TLSEXT_hash_streebog_512, TLSEXT_signature_gostr12_512, 643 #endif 644 645 TLSEXT_hash_sha384, TLSEXT_signature_rsa, 646 TLSEXT_hash_sha384, TLSEXT_signature_dsa, 647 TLSEXT_hash_sha384, TLSEXT_signature_ecdsa, 648 649 TLSEXT_hash_sha256, TLSEXT_signature_rsa, 650 TLSEXT_hash_sha256, TLSEXT_signature_dsa, 651 TLSEXT_hash_sha256, TLSEXT_signature_ecdsa, 652 653 #ifndef OPENSSL_NO_GOST 654 TLSEXT_hash_streebog_256, TLSEXT_signature_gostr12_256, 655 TLSEXT_hash_gost94, TLSEXT_signature_gostr01, 656 #endif 657 658 TLSEXT_hash_sha224, TLSEXT_signature_rsa, 659 TLSEXT_hash_sha224, TLSEXT_signature_dsa, 660 TLSEXT_hash_sha224, TLSEXT_signature_ecdsa, 661 662 TLSEXT_hash_sha1, TLSEXT_signature_rsa, 663 TLSEXT_hash_sha1, TLSEXT_signature_dsa, 664 TLSEXT_hash_sha1, TLSEXT_signature_ecdsa, 665 }; 666 667 int 668 tls12_get_req_sig_algs(SSL *s, unsigned char *p) 669 { 670 size_t slen = sizeof(tls12_sigalgs); 671 672 if (p) 673 memcpy(p, tls12_sigalgs, slen); 674 return (int)slen; 675 } 676 677 unsigned char * 678 ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) 679 { 680 int extdatalen = 0; 681 unsigned char *ret = p; 682 int using_ecc = 0; 683 size_t len; 684 CBB cbb; 685 686 /* See if we support any ECC ciphersuites. */ 687 if (s->version != DTLS1_VERSION && s->version >= TLS1_VERSION) { 688 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); 689 unsigned long alg_k, alg_a; 690 int i; 691 692 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { 693 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); 694 695 alg_k = c->algorithm_mkey; 696 alg_a = c->algorithm_auth; 697 698 if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) { 699 using_ecc = 1; 700 break; 701 } 702 } 703 } 704 705 ret += 2; 706 if (ret >= limit) 707 return NULL; /* this really never occurs, but ... */ 708 709 if (!CBB_init_fixed(&cbb, ret, limit - ret)) 710 return NULL; 711 if (!tlsext_clienthello_build(s, &cbb)) { 712 CBB_cleanup(&cbb); 713 return NULL; 714 } 715 if (!CBB_finish(&cbb, NULL, &len)) { 716 CBB_cleanup(&cbb); 717 return NULL; 718 } 719 if (len > (limit - ret)) 720 return NULL; 721 ret += len; 722 723 if (using_ecc) { 724 size_t curveslen, formatslen, lenmax; 725 const uint16_t *curves; 726 const uint8_t *formats; 727 int i; 728 729 /* 730 * Add TLS extension ECPointFormats to the ClientHello message. 731 */ 732 tls1_get_formatlist(s, 0, &formats, &formatslen); 733 734 if ((size_t)(limit - ret) < 5) 735 return NULL; 736 737 lenmax = limit - ret - 5; 738 if (formatslen > lenmax) 739 return NULL; 740 if (formatslen > 255) { 741 SSLerror(s, ERR_R_INTERNAL_ERROR); 742 return NULL; 743 } 744 745 s2n(TLSEXT_TYPE_ec_point_formats, ret); 746 s2n(formatslen + 1, ret); 747 *(ret++) = (unsigned char)formatslen; 748 memcpy(ret, formats, formatslen); 749 ret += formatslen; 750 751 /* 752 * Add TLS extension EllipticCurves to the ClientHello message. 753 */ 754 tls1_get_curvelist(s, 0, &curves, &curveslen); 755 756 if ((size_t)(limit - ret) < 6) 757 return NULL; 758 759 lenmax = limit - ret - 6; 760 if (curveslen * 2 > lenmax) 761 return NULL; 762 if (curveslen * 2 > 65532) { 763 SSLerror(s, ERR_R_INTERNAL_ERROR); 764 return NULL; 765 } 766 767 s2n(TLSEXT_TYPE_elliptic_curves, ret); 768 s2n((curveslen * 2) + 2, ret); 769 770 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for 771 * elliptic_curve_list, but the examples use two bytes. 772 * https://www1.ietf.org/mail-archive/web/tls/current/msg00538.html 773 * resolves this to two bytes. 774 */ 775 s2n(curveslen * 2, ret); 776 for (i = 0; i < curveslen; i++) 777 s2n(curves[i], ret); 778 } 779 780 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) { 781 int ticklen; 782 if (!s->internal->new_session && s->session && s->session->tlsext_tick) 783 ticklen = s->session->tlsext_ticklen; 784 else if (s->session && s->internal->tlsext_session_ticket && 785 s->internal->tlsext_session_ticket->data) { 786 ticklen = s->internal->tlsext_session_ticket->length; 787 s->session->tlsext_tick = malloc(ticklen); 788 if (!s->session->tlsext_tick) 789 return NULL; 790 memcpy(s->session->tlsext_tick, 791 s->internal->tlsext_session_ticket->data, ticklen); 792 s->session->tlsext_ticklen = ticklen; 793 } else 794 ticklen = 0; 795 if (ticklen == 0 && s->internal->tlsext_session_ticket && 796 s->internal->tlsext_session_ticket->data == NULL) 797 goto skip_ext; 798 /* Check for enough room 2 for extension type, 2 for len 799 * rest for ticket 800 */ 801 if ((size_t)(limit - ret) < 4 + ticklen) 802 return NULL; 803 s2n(TLSEXT_TYPE_session_ticket, ret); 804 805 s2n(ticklen, ret); 806 if (ticklen) { 807 memcpy(ret, s->session->tlsext_tick, ticklen); 808 ret += ticklen; 809 } 810 } 811 skip_ext: 812 813 if (TLS1_get_client_version(s) >= TLS1_2_VERSION) { 814 if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6) 815 return NULL; 816 817 s2n(TLSEXT_TYPE_signature_algorithms, ret); 818 s2n(sizeof(tls12_sigalgs) + 2, ret); 819 s2n(sizeof(tls12_sigalgs), ret); 820 memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs)); 821 ret += sizeof(tls12_sigalgs); 822 } 823 824 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp && 825 s->version != DTLS1_VERSION) { 826 int i; 827 long extlen, idlen, itmp; 828 OCSP_RESPID *id; 829 830 idlen = 0; 831 for (i = 0; i < sk_OCSP_RESPID_num(s->internal->tlsext_ocsp_ids); i++) { 832 id = sk_OCSP_RESPID_value(s->internal->tlsext_ocsp_ids, i); 833 itmp = i2d_OCSP_RESPID(id, NULL); 834 if (itmp <= 0) 835 return NULL; 836 idlen += itmp + 2; 837 } 838 839 if (s->internal->tlsext_ocsp_exts) { 840 extlen = i2d_X509_EXTENSIONS(s->internal->tlsext_ocsp_exts, NULL); 841 if (extlen < 0) 842 return NULL; 843 } else 844 extlen = 0; 845 846 if ((size_t)(limit - ret) < 7 + extlen + idlen) 847 return NULL; 848 s2n(TLSEXT_TYPE_status_request, ret); 849 if (extlen + idlen > 0xFFF0) 850 return NULL; 851 s2n(extlen + idlen + 5, ret); 852 *(ret++) = TLSEXT_STATUSTYPE_ocsp; 853 s2n(idlen, ret); 854 for (i = 0; i < sk_OCSP_RESPID_num(s->internal->tlsext_ocsp_ids); i++) { 855 /* save position of id len */ 856 unsigned char *q = ret; 857 id = sk_OCSP_RESPID_value(s->internal->tlsext_ocsp_ids, i); 858 /* skip over id len */ 859 ret += 2; 860 itmp = i2d_OCSP_RESPID(id, &ret); 861 /* write id len */ 862 s2n(itmp, q); 863 } 864 s2n(extlen, ret); 865 if (extlen > 0) 866 i2d_X509_EXTENSIONS(s->internal->tlsext_ocsp_exts, &ret); 867 } 868 869 if (s->ctx->internal->next_proto_select_cb && 870 !S3I(s)->tmp.finish_md_len) { 871 /* The client advertises an emtpy extension to indicate its 872 * support for Next Protocol Negotiation */ 873 if ((size_t)(limit - ret) < 4) 874 return NULL; 875 s2n(TLSEXT_TYPE_next_proto_neg, ret); 876 s2n(0, ret); 877 } 878 879 if (s->internal->alpn_client_proto_list != NULL && 880 S3I(s)->tmp.finish_md_len == 0) { 881 if ((size_t)(limit - ret) < 882 6 + s->internal->alpn_client_proto_list_len) 883 return (NULL); 884 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); 885 s2n(2 + s->internal->alpn_client_proto_list_len, ret); 886 s2n(s->internal->alpn_client_proto_list_len, ret); 887 memcpy(ret, s->internal->alpn_client_proto_list, 888 s->internal->alpn_client_proto_list_len); 889 ret += s->internal->alpn_client_proto_list_len; 890 } 891 892 #ifndef OPENSSL_NO_SRTP 893 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) { 894 int el; 895 896 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); 897 898 if ((size_t)(limit - ret) < 4 + el) 899 return NULL; 900 901 s2n(TLSEXT_TYPE_use_srtp, ret); 902 s2n(el, ret); 903 904 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) { 905 SSLerror(s, ERR_R_INTERNAL_ERROR); 906 return NULL; 907 } 908 ret += el; 909 } 910 #endif 911 912 /* 913 * Add padding to workaround bugs in F5 terminators. 914 * See https://tools.ietf.org/html/draft-agl-tls-padding-03 915 * 916 * Note that this seems to trigger issues with IronPort SMTP 917 * appliances. 918 * 919 * NB: because this code works out the length of all existing 920 * extensions it MUST always appear last. 921 */ 922 if (s->internal->options & SSL_OP_TLSEXT_PADDING) { 923 int hlen = ret - (unsigned char *)s->internal->init_buf->data; 924 925 /* 926 * The code in s23_clnt.c to build ClientHello messages 927 * includes the 5-byte record header in the buffer, while the 928 * code in s3_clnt.c does not. 929 */ 930 if (S3I(s)->hs.state == SSL23_ST_CW_CLNT_HELLO_A) 931 hlen -= 5; 932 if (hlen > 0xff && hlen < 0x200) { 933 hlen = 0x200 - hlen; 934 if (hlen >= 4) 935 hlen -= 4; 936 else 937 hlen = 0; 938 939 s2n(TLSEXT_TYPE_padding, ret); 940 s2n(hlen, ret); 941 memset(ret, 0, hlen); 942 ret += hlen; 943 } 944 } 945 946 if ((extdatalen = ret - p - 2) == 0) 947 return p; 948 949 s2n(extdatalen, p); 950 return ret; 951 } 952 953 unsigned char * 954 ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) 955 { 956 int using_ecc, extdatalen = 0; 957 unsigned long alg_a, alg_k; 958 unsigned char *ret = p; 959 int next_proto_neg_seen; 960 size_t len; 961 CBB cbb; 962 963 alg_a = S3I(s)->hs.new_cipher->algorithm_auth; 964 alg_k = S3I(s)->hs.new_cipher->algorithm_mkey; 965 using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) && 966 SSI(s)->tlsext_ecpointformatlist != NULL; 967 968 ret += 2; 969 if (ret >= limit) 970 return NULL; /* this really never occurs, but ... */ 971 972 if (!CBB_init_fixed(&cbb, ret, limit - ret)) 973 return NULL; 974 if (!tlsext_serverhello_build(s, &cbb)) { 975 CBB_cleanup(&cbb); 976 return NULL; 977 } 978 if (!CBB_finish(&cbb, NULL, &len)) { 979 CBB_cleanup(&cbb); 980 return NULL; 981 } 982 if (len > (limit - ret)) 983 return NULL; 984 ret += len; 985 986 if (using_ecc && s->version != DTLS1_VERSION) { 987 const unsigned char *formats; 988 size_t formatslen, lenmax; 989 990 /* 991 * Add TLS extension ECPointFormats to the ServerHello message. 992 */ 993 tls1_get_formatlist(s, 0, &formats, &formatslen); 994 995 if ((size_t)(limit - ret) < 5) 996 return NULL; 997 998 lenmax = limit - ret - 5; 999 if (formatslen > lenmax) 1000 return NULL; 1001 if (formatslen > 255) { 1002 SSLerror(s, ERR_R_INTERNAL_ERROR); 1003 return NULL; 1004 } 1005 1006 s2n(TLSEXT_TYPE_ec_point_formats, ret); 1007 s2n(formatslen + 1, ret); 1008 *(ret++) = (unsigned char)formatslen; 1009 memcpy(ret, formats, formatslen); 1010 ret += formatslen; 1011 } 1012 1013 /* 1014 * Currently the server should not respond with a SupportedCurves 1015 * extension. 1016 */ 1017 1018 if (s->internal->tlsext_ticket_expected && 1019 !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { 1020 if ((size_t)(limit - ret) < 4) 1021 return NULL; 1022 1023 s2n(TLSEXT_TYPE_session_ticket, ret); 1024 s2n(0, ret); 1025 } 1026 1027 if (s->internal->tlsext_status_expected) { 1028 if ((size_t)(limit - ret) < 4) 1029 return NULL; 1030 1031 s2n(TLSEXT_TYPE_status_request, ret); 1032 s2n(0, ret); 1033 } 1034 1035 #ifndef OPENSSL_NO_SRTP 1036 if (SSL_IS_DTLS(s) && s->internal->srtp_profile) { 1037 int el; 1038 1039 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); 1040 1041 if ((size_t)(limit - ret) < 4 + el) 1042 return NULL; 1043 1044 s2n(TLSEXT_TYPE_use_srtp, ret); 1045 s2n(el, ret); 1046 1047 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) { 1048 SSLerror(s, ERR_R_INTERNAL_ERROR); 1049 return NULL; 1050 } 1051 ret += el; 1052 } 1053 #endif 1054 1055 if (((S3I(s)->hs.new_cipher->id & 0xFFFF) == 0x80 || 1056 (S3I(s)->hs.new_cipher->id & 0xFFFF) == 0x81) && 1057 (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) { 1058 static const unsigned char cryptopro_ext[36] = { 1059 0xfd, 0xe8, /*65000*/ 1060 0x00, 0x20, /*32 bytes length*/ 1061 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, 1062 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, 1063 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, 1064 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 1065 }; 1066 if ((size_t)(limit - ret) < sizeof(cryptopro_ext)) 1067 return NULL; 1068 memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext)); 1069 ret += sizeof(cryptopro_ext); 1070 } 1071 1072 next_proto_neg_seen = S3I(s)->next_proto_neg_seen; 1073 S3I(s)->next_proto_neg_seen = 0; 1074 if (next_proto_neg_seen && s->ctx->internal->next_protos_advertised_cb) { 1075 const unsigned char *npa; 1076 unsigned int npalen; 1077 int r; 1078 1079 r = s->ctx->internal->next_protos_advertised_cb(s, &npa, &npalen, 1080 s->ctx->internal->next_protos_advertised_cb_arg); 1081 if (r == SSL_TLSEXT_ERR_OK) { 1082 if ((size_t)(limit - ret) < 4 + npalen) 1083 return NULL; 1084 s2n(TLSEXT_TYPE_next_proto_neg, ret); 1085 s2n(npalen, ret); 1086 memcpy(ret, npa, npalen); 1087 ret += npalen; 1088 S3I(s)->next_proto_neg_seen = 1; 1089 } 1090 } 1091 1092 if (S3I(s)->alpn_selected != NULL) { 1093 const unsigned char *selected = S3I(s)->alpn_selected; 1094 unsigned int len = S3I(s)->alpn_selected_len; 1095 1096 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) 1097 return (NULL); 1098 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); 1099 s2n(3 + len, ret); 1100 s2n(1 + len, ret); 1101 *ret++ = len; 1102 memcpy(ret, selected, len); 1103 ret += len; 1104 } 1105 1106 if ((extdatalen = ret - p - 2) == 0) 1107 return p; 1108 1109 s2n(extdatalen, p); 1110 return ret; 1111 } 1112 1113 /* 1114 * tls1_alpn_handle_client_hello is called to process the ALPN extension in a 1115 * ClientHello. 1116 * data: the contents of the extension, not including the type and length. 1117 * data_len: the number of bytes in data. 1118 * al: a pointer to the alert value to send in the event of a non-zero 1119 * return. 1120 * returns: 1 on success. 1121 */ 1122 static int 1123 tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data, 1124 unsigned int data_len, int *al) 1125 { 1126 CBS cbs, proto_name_list, alpn; 1127 const unsigned char *selected; 1128 unsigned char selected_len; 1129 int r; 1130 1131 if (s->ctx->internal->alpn_select_cb == NULL) 1132 return (1); 1133 1134 if (data_len < 2) 1135 goto parse_error; 1136 1137 CBS_init(&cbs, data, data_len); 1138 1139 /* 1140 * data should contain a uint16 length followed by a series of 8-bit, 1141 * length-prefixed strings. 1142 */ 1143 if (!CBS_get_u16_length_prefixed(&cbs, &alpn) || 1144 CBS_len(&alpn) < 2 || 1145 CBS_len(&cbs) != 0) 1146 goto parse_error; 1147 1148 /* Validate data before sending to callback. */ 1149 CBS_dup(&alpn, &proto_name_list); 1150 while (CBS_len(&proto_name_list) > 0) { 1151 CBS proto_name; 1152 1153 if (!CBS_get_u8_length_prefixed(&proto_name_list, &proto_name) || 1154 CBS_len(&proto_name) == 0) 1155 goto parse_error; 1156 } 1157 1158 r = s->ctx->internal->alpn_select_cb(s, &selected, &selected_len, 1159 CBS_data(&alpn), CBS_len(&alpn), 1160 s->ctx->internal->alpn_select_cb_arg); 1161 if (r == SSL_TLSEXT_ERR_OK) { 1162 free(S3I(s)->alpn_selected); 1163 if ((S3I(s)->alpn_selected = malloc(selected_len)) == NULL) { 1164 *al = SSL_AD_INTERNAL_ERROR; 1165 return (-1); 1166 } 1167 memcpy(S3I(s)->alpn_selected, selected, selected_len); 1168 S3I(s)->alpn_selected_len = selected_len; 1169 } 1170 1171 return (1); 1172 1173 parse_error: 1174 *al = SSL_AD_DECODE_ERROR; 1175 return (0); 1176 } 1177 1178 int 1179 ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, 1180 int n, int *al) 1181 { 1182 unsigned short type; 1183 unsigned short size; 1184 unsigned short len; 1185 unsigned char *data = *p; 1186 unsigned char *end = d + n; 1187 int sigalg_seen = 0; 1188 CBS cbs; 1189 1190 s->internal->servername_done = 0; 1191 s->tlsext_status_type = -1; 1192 S3I(s)->renegotiate_seen = 0; 1193 S3I(s)->next_proto_neg_seen = 0; 1194 free(S3I(s)->alpn_selected); 1195 S3I(s)->alpn_selected = NULL; 1196 s->internal->srtp_profile = NULL; 1197 1198 if (data == end) 1199 goto ri_check; 1200 1201 if (end - data < 2) 1202 goto err; 1203 n2s(data, len); 1204 1205 if (end - data != len) 1206 goto err; 1207 1208 while (end - data >= 4) { 1209 n2s(data, type); 1210 n2s(data, size); 1211 1212 if (end - data < size) 1213 goto err; 1214 1215 if (s->internal->tlsext_debug_cb) 1216 s->internal->tlsext_debug_cb(s, 0, type, data, size, 1217 s->internal->tlsext_debug_arg); 1218 1219 CBS_init(&cbs, data, size); 1220 if (!tlsext_clienthello_parse_one(s, &cbs, type, al)) 1221 return 0; 1222 1223 if (type == TLSEXT_TYPE_ec_point_formats && 1224 s->version != DTLS1_VERSION) { 1225 unsigned char *sdata = data; 1226 size_t formatslen; 1227 uint8_t *formats; 1228 1229 if (size < 1) { 1230 *al = TLS1_AD_DECODE_ERROR; 1231 return 0; 1232 } 1233 formatslen = *(sdata++); 1234 if (formatslen != size - 1) { 1235 *al = TLS1_AD_DECODE_ERROR; 1236 return 0; 1237 } 1238 1239 if (!s->internal->hit) { 1240 free(SSI(s)->tlsext_ecpointformatlist); 1241 SSI(s)->tlsext_ecpointformatlist = NULL; 1242 SSI(s)->tlsext_ecpointformatlist_length = 0; 1243 1244 if ((formats = reallocarray(NULL, formatslen, 1245 sizeof(uint8_t))) == NULL) { 1246 *al = TLS1_AD_INTERNAL_ERROR; 1247 return 0; 1248 } 1249 memcpy(formats, sdata, formatslen); 1250 SSI(s)->tlsext_ecpointformatlist = formats; 1251 SSI(s)->tlsext_ecpointformatlist_length = 1252 formatslen; 1253 } 1254 } else if (type == TLSEXT_TYPE_elliptic_curves && 1255 s->version != DTLS1_VERSION) { 1256 unsigned char *sdata = data; 1257 size_t curveslen, i; 1258 uint16_t *curves; 1259 1260 if (size < 2) { 1261 *al = TLS1_AD_DECODE_ERROR; 1262 return 0; 1263 } 1264 n2s(sdata, curveslen); 1265 if (curveslen != size - 2 || curveslen % 2 != 0) { 1266 *al = TLS1_AD_DECODE_ERROR; 1267 return 0; 1268 } 1269 curveslen /= 2; 1270 1271 if (!s->internal->hit) { 1272 if (SSI(s)->tlsext_supportedgroups) { 1273 *al = TLS1_AD_DECODE_ERROR; 1274 return 0; 1275 } 1276 SSI(s)->tlsext_supportedgroups_length = 0; 1277 if ((curves = reallocarray(NULL, curveslen, 1278 sizeof(uint16_t))) == NULL) { 1279 *al = TLS1_AD_INTERNAL_ERROR; 1280 return 0; 1281 } 1282 for (i = 0; i < curveslen; i++) 1283 n2s(sdata, curves[i]); 1284 SSI(s)->tlsext_supportedgroups = curves; 1285 SSI(s)->tlsext_supportedgroups_length = curveslen; 1286 } 1287 } else if (type == TLSEXT_TYPE_session_ticket) { 1288 if (s->internal->tls_session_ticket_ext_cb && 1289 !s->internal->tls_session_ticket_ext_cb(s, data, size, s->internal->tls_session_ticket_ext_cb_arg)) { 1290 *al = TLS1_AD_INTERNAL_ERROR; 1291 return 0; 1292 } 1293 } else if (type == TLSEXT_TYPE_signature_algorithms) { 1294 int dsize; 1295 if (sigalg_seen || size < 2) { 1296 *al = SSL_AD_DECODE_ERROR; 1297 return 0; 1298 } 1299 sigalg_seen = 1; 1300 n2s(data, dsize); 1301 size -= 2; 1302 if (dsize != size || dsize & 1) { 1303 *al = SSL_AD_DECODE_ERROR; 1304 return 0; 1305 } 1306 if (!tls1_process_sigalgs(s, data, dsize)) { 1307 *al = SSL_AD_DECODE_ERROR; 1308 return 0; 1309 } 1310 } else if (type == TLSEXT_TYPE_status_request && 1311 s->version != DTLS1_VERSION) { 1312 1313 if (size < 5) { 1314 *al = SSL_AD_DECODE_ERROR; 1315 return 0; 1316 } 1317 1318 s->tlsext_status_type = *data++; 1319 size--; 1320 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { 1321 const unsigned char *sdata; 1322 int dsize; 1323 /* Read in responder_id_list */ 1324 n2s(data, dsize); 1325 size -= 2; 1326 if (dsize > size) { 1327 *al = SSL_AD_DECODE_ERROR; 1328 return 0; 1329 } 1330 1331 /* 1332 * We remove any OCSP_RESPIDs from a 1333 * previous handshake to prevent 1334 * unbounded memory growth. 1335 */ 1336 sk_OCSP_RESPID_pop_free(s->internal->tlsext_ocsp_ids, 1337 OCSP_RESPID_free); 1338 s->internal->tlsext_ocsp_ids = NULL; 1339 if (dsize > 0) { 1340 s->internal->tlsext_ocsp_ids = 1341 sk_OCSP_RESPID_new_null(); 1342 if (s->internal->tlsext_ocsp_ids == NULL) { 1343 *al = SSL_AD_INTERNAL_ERROR; 1344 return 0; 1345 } 1346 } 1347 1348 while (dsize > 0) { 1349 OCSP_RESPID *id; 1350 int idsize; 1351 if (dsize < 4) { 1352 *al = SSL_AD_DECODE_ERROR; 1353 return 0; 1354 } 1355 n2s(data, idsize); 1356 dsize -= 2 + idsize; 1357 size -= 2 + idsize; 1358 if (dsize < 0) { 1359 *al = SSL_AD_DECODE_ERROR; 1360 return 0; 1361 } 1362 sdata = data; 1363 data += idsize; 1364 id = d2i_OCSP_RESPID(NULL, 1365 &sdata, idsize); 1366 if (!id) { 1367 *al = SSL_AD_DECODE_ERROR; 1368 return 0; 1369 } 1370 if (data != sdata) { 1371 OCSP_RESPID_free(id); 1372 *al = SSL_AD_DECODE_ERROR; 1373 return 0; 1374 } 1375 if (!sk_OCSP_RESPID_push( 1376 s->internal->tlsext_ocsp_ids, id)) { 1377 OCSP_RESPID_free(id); 1378 *al = SSL_AD_INTERNAL_ERROR; 1379 return 0; 1380 } 1381 } 1382 1383 /* Read in request_extensions */ 1384 if (size < 2) { 1385 *al = SSL_AD_DECODE_ERROR; 1386 return 0; 1387 } 1388 n2s(data, dsize); 1389 size -= 2; 1390 if (dsize != size) { 1391 *al = SSL_AD_DECODE_ERROR; 1392 return 0; 1393 } 1394 sdata = data; 1395 if (dsize > 0) { 1396 sk_X509_EXTENSION_pop_free(s->internal->tlsext_ocsp_exts, 1397 X509_EXTENSION_free); 1398 1399 s->internal->tlsext_ocsp_exts = 1400 d2i_X509_EXTENSIONS(NULL, 1401 &sdata, dsize); 1402 if (!s->internal->tlsext_ocsp_exts || 1403 (data + dsize != sdata)) { 1404 *al = SSL_AD_DECODE_ERROR; 1405 return 0; 1406 } 1407 } 1408 } else { 1409 /* We don't know what to do with any other type 1410 * so ignore it. 1411 */ 1412 s->tlsext_status_type = -1; 1413 } 1414 } 1415 else if (type == TLSEXT_TYPE_next_proto_neg && 1416 S3I(s)->tmp.finish_md_len == 0 && 1417 S3I(s)->alpn_selected == NULL) { 1418 /* We shouldn't accept this extension on a 1419 * renegotiation. 1420 * 1421 * s->internal->new_session will be set on renegotiation, but we 1422 * probably shouldn't rely that it couldn't be set on 1423 * the initial renegotation too in certain cases (when 1424 * there's some other reason to disallow resuming an 1425 * earlier session -- the current code won't be doing 1426 * anything like that, but this might change). 1427 1428 * A valid sign that there's been a previous handshake 1429 * in this connection is if S3I(s)->tmp.finish_md_len > 1430 * 0. (We are talking about a check that will happen 1431 * in the Hello protocol round, well before a new 1432 * Finished message could have been computed.) */ 1433 S3I(s)->next_proto_neg_seen = 1; 1434 } 1435 else if (type == 1436 TLSEXT_TYPE_application_layer_protocol_negotiation && 1437 s->ctx->internal->alpn_select_cb != NULL && 1438 S3I(s)->tmp.finish_md_len == 0) { 1439 if (tls1_alpn_handle_client_hello(s, data, 1440 size, al) != 1) 1441 return (0); 1442 /* ALPN takes precedence over NPN. */ 1443 S3I(s)->next_proto_neg_seen = 0; 1444 } 1445 1446 /* session ticket processed earlier */ 1447 #ifndef OPENSSL_NO_SRTP 1448 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { 1449 if (ssl_parse_clienthello_use_srtp_ext(s, data, size, al)) 1450 return 0; 1451 } 1452 #endif 1453 1454 data += size; 1455 } 1456 1457 /* Spurious data on the end */ 1458 if (data != end) 1459 goto err; 1460 1461 *p = data; 1462 1463 ri_check: 1464 1465 /* Need RI if renegotiating */ 1466 1467 if (!S3I(s)->renegotiate_seen && s->internal->renegotiate) { 1468 *al = SSL_AD_HANDSHAKE_FAILURE; 1469 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1470 return 0; 1471 } 1472 1473 return 1; 1474 1475 err: 1476 *al = SSL_AD_DECODE_ERROR; 1477 return 0; 1478 } 1479 1480 /* 1481 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No 1482 * elements of zero length are allowed and the set of elements must exactly fill 1483 * the length of the block. 1484 */ 1485 static char 1486 ssl_next_proto_validate(const unsigned char *d, unsigned int len) 1487 { 1488 CBS npn, value; 1489 1490 CBS_init(&npn, d, len); 1491 while (CBS_len(&npn) > 0) { 1492 if (!CBS_get_u8_length_prefixed(&npn, &value) || 1493 CBS_len(&value) == 0) 1494 return 0; 1495 } 1496 return 1; 1497 } 1498 1499 int 1500 ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, size_t n, int *al) 1501 { 1502 unsigned short type; 1503 unsigned short size; 1504 unsigned short len; 1505 unsigned char *data = *p; 1506 unsigned char *end = *p + n; 1507 CBS cbs; 1508 1509 S3I(s)->renegotiate_seen = 0; 1510 S3I(s)->next_proto_neg_seen = 0; 1511 free(S3I(s)->alpn_selected); 1512 S3I(s)->alpn_selected = NULL; 1513 1514 if (data == end) 1515 goto ri_check; 1516 1517 if (end - data < 2) 1518 goto err; 1519 n2s(data, len); 1520 1521 if (end - data != len) 1522 goto err; 1523 1524 while (end - data >= 4) { 1525 n2s(data, type); 1526 n2s(data, size); 1527 1528 if (end - data < size) 1529 goto err; 1530 1531 if (s->internal->tlsext_debug_cb) 1532 s->internal->tlsext_debug_cb(s, 1, type, data, size, 1533 s->internal->tlsext_debug_arg); 1534 1535 CBS_init(&cbs, data, size); 1536 if (!tlsext_serverhello_parse_one(s, &cbs, type, al)) 1537 return 0; 1538 1539 if (type == TLSEXT_TYPE_ec_point_formats && 1540 s->version != DTLS1_VERSION) { 1541 unsigned char *sdata = data; 1542 size_t formatslen; 1543 uint8_t *formats; 1544 1545 if (size < 1) { 1546 *al = TLS1_AD_DECODE_ERROR; 1547 return 0; 1548 } 1549 formatslen = *(sdata++); 1550 if (formatslen != size - 1) { 1551 *al = TLS1_AD_DECODE_ERROR; 1552 return 0; 1553 } 1554 1555 if (!s->internal->hit) { 1556 free(SSI(s)->tlsext_ecpointformatlist); 1557 SSI(s)->tlsext_ecpointformatlist = NULL; 1558 SSI(s)->tlsext_ecpointformatlist_length = 0; 1559 1560 if ((formats = reallocarray(NULL, formatslen, 1561 sizeof(uint8_t))) == NULL) { 1562 *al = TLS1_AD_INTERNAL_ERROR; 1563 return 0; 1564 } 1565 memcpy(formats, sdata, formatslen); 1566 SSI(s)->tlsext_ecpointformatlist = formats; 1567 SSI(s)->tlsext_ecpointformatlist_length = 1568 formatslen; 1569 } 1570 } 1571 else if (type == TLSEXT_TYPE_session_ticket) { 1572 if (s->internal->tls_session_ticket_ext_cb && 1573 !s->internal->tls_session_ticket_ext_cb(s, data, size, s->internal->tls_session_ticket_ext_cb_arg)) { 1574 *al = TLS1_AD_INTERNAL_ERROR; 1575 return 0; 1576 } 1577 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || (size > 0)) { 1578 *al = TLS1_AD_UNSUPPORTED_EXTENSION; 1579 return 0; 1580 } 1581 s->internal->tlsext_ticket_expected = 1; 1582 } 1583 else if (type == TLSEXT_TYPE_status_request && 1584 s->version != DTLS1_VERSION) { 1585 /* MUST be empty and only sent if we've requested 1586 * a status request message. 1587 */ 1588 if ((s->tlsext_status_type == -1) || (size > 0)) { 1589 *al = TLS1_AD_UNSUPPORTED_EXTENSION; 1590 return 0; 1591 } 1592 /* Set flag to expect CertificateStatus message */ 1593 s->internal->tlsext_status_expected = 1; 1594 } 1595 else if (type == TLSEXT_TYPE_next_proto_neg && 1596 S3I(s)->tmp.finish_md_len == 0) { 1597 unsigned char *selected; 1598 unsigned char selected_len; 1599 1600 /* We must have requested it. */ 1601 if (s->ctx->internal->next_proto_select_cb == NULL) { 1602 *al = TLS1_AD_UNSUPPORTED_EXTENSION; 1603 return 0; 1604 } 1605 /* The data must be valid */ 1606 if (!ssl_next_proto_validate(data, size)) { 1607 *al = TLS1_AD_DECODE_ERROR; 1608 return 0; 1609 } 1610 if (s->ctx->internal->next_proto_select_cb(s, &selected, 1611 &selected_len, data, size, 1612 s->ctx->internal->next_proto_select_cb_arg) != 1613 SSL_TLSEXT_ERR_OK) { 1614 *al = TLS1_AD_INTERNAL_ERROR; 1615 return 0; 1616 } 1617 s->internal->next_proto_negotiated = malloc(selected_len); 1618 if (!s->internal->next_proto_negotiated) { 1619 *al = TLS1_AD_INTERNAL_ERROR; 1620 return 0; 1621 } 1622 memcpy(s->internal->next_proto_negotiated, selected, selected_len); 1623 s->internal->next_proto_negotiated_len = selected_len; 1624 S3I(s)->next_proto_neg_seen = 1; 1625 } 1626 else if (type == 1627 TLSEXT_TYPE_application_layer_protocol_negotiation) { 1628 unsigned int len; 1629 1630 /* We must have requested it. */ 1631 if (s->internal->alpn_client_proto_list == NULL) { 1632 *al = TLS1_AD_UNSUPPORTED_EXTENSION; 1633 return 0; 1634 } 1635 if (size < 4) { 1636 *al = TLS1_AD_DECODE_ERROR; 1637 return (0); 1638 } 1639 1640 /* The extension data consists of: 1641 * uint16 list_length 1642 * uint8 proto_length; 1643 * uint8 proto[proto_length]; */ 1644 len = ((unsigned int)data[0]) << 8 | 1645 ((unsigned int)data[1]); 1646 if (len != (unsigned int)size - 2) { 1647 *al = TLS1_AD_DECODE_ERROR; 1648 return (0); 1649 } 1650 len = data[2]; 1651 if (len != (unsigned int)size - 3) { 1652 *al = TLS1_AD_DECODE_ERROR; 1653 return (0); 1654 } 1655 free(S3I(s)->alpn_selected); 1656 S3I(s)->alpn_selected = malloc(len); 1657 if (S3I(s)->alpn_selected == NULL) { 1658 *al = TLS1_AD_INTERNAL_ERROR; 1659 return (0); 1660 } 1661 memcpy(S3I(s)->alpn_selected, data + 3, len); 1662 S3I(s)->alpn_selected_len = len; 1663 1664 } 1665 #ifndef OPENSSL_NO_SRTP 1666 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { 1667 if (ssl_parse_serverhello_use_srtp_ext(s, data, 1668 size, al)) 1669 return 0; 1670 } 1671 #endif 1672 1673 data += size; 1674 1675 } 1676 1677 if (data != end) { 1678 *al = SSL_AD_DECODE_ERROR; 1679 return 0; 1680 } 1681 1682 *p = data; 1683 1684 ri_check: 1685 1686 /* Determine if we need to see RI. Strictly speaking if we want to 1687 * avoid an attack we should *always* see RI even on initial server 1688 * hello because the client doesn't see any renegotiation during an 1689 * attack. However this would mean we could not connect to any server 1690 * which doesn't support RI so for the immediate future tolerate RI 1691 * absence on initial connect only. 1692 */ 1693 if (!S3I(s)->renegotiate_seen && 1694 !(s->internal->options & SSL_OP_LEGACY_SERVER_CONNECT)) { 1695 *al = SSL_AD_HANDSHAKE_FAILURE; 1696 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1697 return 0; 1698 } 1699 1700 return 1; 1701 1702 err: 1703 *al = SSL_AD_DECODE_ERROR; 1704 return 0; 1705 } 1706 1707 int 1708 ssl_check_clienthello_tlsext_early(SSL *s) 1709 { 1710 int ret = SSL_TLSEXT_ERR_NOACK; 1711 int al = SSL_AD_UNRECOGNIZED_NAME; 1712 1713 /* The handling of the ECPointFormats extension is done elsewhere, namely in 1714 * ssl3_choose_cipher in s3_lib.c. 1715 */ 1716 /* The handling of the EllipticCurves extension is done elsewhere, namely in 1717 * ssl3_choose_cipher in s3_lib.c. 1718 */ 1719 1720 if (s->ctx != NULL && s->ctx->internal->tlsext_servername_callback != 0) 1721 ret = s->ctx->internal->tlsext_servername_callback(s, &al, 1722 s->ctx->internal->tlsext_servername_arg); 1723 else if (s->initial_ctx != NULL && s->initial_ctx->internal->tlsext_servername_callback != 0) 1724 ret = s->initial_ctx->internal->tlsext_servername_callback(s, &al, 1725 s->initial_ctx->internal->tlsext_servername_arg); 1726 1727 switch (ret) { 1728 case SSL_TLSEXT_ERR_ALERT_FATAL: 1729 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1730 return -1; 1731 case SSL_TLSEXT_ERR_ALERT_WARNING: 1732 ssl3_send_alert(s, SSL3_AL_WARNING, al); 1733 return 1; 1734 case SSL_TLSEXT_ERR_NOACK: 1735 s->internal->servername_done = 0; 1736 default: 1737 return 1; 1738 } 1739 } 1740 1741 int 1742 ssl_check_clienthello_tlsext_late(SSL *s) 1743 { 1744 int ret = SSL_TLSEXT_ERR_OK; 1745 int al = 0; /* XXX gcc3 */ 1746 1747 /* If status request then ask callback what to do. 1748 * Note: this must be called after servername callbacks in case 1749 * the certificate has changed, and must be called after the cipher 1750 * has been chosen because this may influence which certificate is sent 1751 */ 1752 if ((s->tlsext_status_type != -1) && 1753 s->ctx && s->ctx->internal->tlsext_status_cb) { 1754 int r; 1755 CERT_PKEY *certpkey; 1756 certpkey = ssl_get_server_send_pkey(s); 1757 /* If no certificate can't return certificate status */ 1758 if (certpkey == NULL) { 1759 s->internal->tlsext_status_expected = 0; 1760 return 1; 1761 } 1762 /* Set current certificate to one we will use so 1763 * SSL_get_certificate et al can pick it up. 1764 */ 1765 s->cert->key = certpkey; 1766 r = s->ctx->internal->tlsext_status_cb(s, 1767 s->ctx->internal->tlsext_status_arg); 1768 switch (r) { 1769 /* We don't want to send a status request response */ 1770 case SSL_TLSEXT_ERR_NOACK: 1771 s->internal->tlsext_status_expected = 0; 1772 break; 1773 /* status request response should be sent */ 1774 case SSL_TLSEXT_ERR_OK: 1775 if (s->internal->tlsext_ocsp_resp) 1776 s->internal->tlsext_status_expected = 1; 1777 else 1778 s->internal->tlsext_status_expected = 0; 1779 break; 1780 /* something bad happened */ 1781 case SSL_TLSEXT_ERR_ALERT_FATAL: 1782 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 1783 al = SSL_AD_INTERNAL_ERROR; 1784 goto err; 1785 } 1786 } else 1787 s->internal->tlsext_status_expected = 0; 1788 1789 err: 1790 switch (ret) { 1791 case SSL_TLSEXT_ERR_ALERT_FATAL: 1792 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1793 return -1; 1794 case SSL_TLSEXT_ERR_ALERT_WARNING: 1795 ssl3_send_alert(s, SSL3_AL_WARNING, al); 1796 return 1; 1797 default: 1798 return 1; 1799 } 1800 } 1801 1802 int 1803 ssl_check_serverhello_tlsext(SSL *s) 1804 { 1805 int ret = SSL_TLSEXT_ERR_NOACK; 1806 int al = SSL_AD_UNRECOGNIZED_NAME; 1807 1808 /* If we are client and using an elliptic curve cryptography cipher 1809 * suite, then if server returns an EC point formats lists extension 1810 * it must contain uncompressed. 1811 */ 1812 unsigned long alg_k = S3I(s)->hs.new_cipher->algorithm_mkey; 1813 unsigned long alg_a = S3I(s)->hs.new_cipher->algorithm_auth; 1814 if ((s->internal->tlsext_ecpointformatlist != NULL) && 1815 (s->internal->tlsext_ecpointformatlist_length > 0) && 1816 (SSI(s)->tlsext_ecpointformatlist != NULL) && 1817 (SSI(s)->tlsext_ecpointformatlist_length > 0) && 1818 ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { 1819 /* we are using an ECC cipher */ 1820 size_t i; 1821 unsigned char *list; 1822 int found_uncompressed = 0; 1823 list = SSI(s)->tlsext_ecpointformatlist; 1824 for (i = 0; i < SSI(s)->tlsext_ecpointformatlist_length; i++) { 1825 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { 1826 found_uncompressed = 1; 1827 break; 1828 } 1829 } 1830 if (!found_uncompressed) { 1831 SSLerror(s, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); 1832 return -1; 1833 } 1834 } 1835 ret = SSL_TLSEXT_ERR_OK; 1836 1837 if (s->ctx != NULL && s->ctx->internal->tlsext_servername_callback != 0) 1838 ret = s->ctx->internal->tlsext_servername_callback(s, &al, 1839 s->ctx->internal->tlsext_servername_arg); 1840 else if (s->initial_ctx != NULL && s->initial_ctx->internal->tlsext_servername_callback != 0) 1841 ret = s->initial_ctx->internal->tlsext_servername_callback(s, &al, 1842 s->initial_ctx->internal->tlsext_servername_arg); 1843 1844 /* If we've requested certificate status and we wont get one 1845 * tell the callback 1846 */ 1847 if ((s->tlsext_status_type != -1) && !(s->internal->tlsext_status_expected) && 1848 s->ctx && s->ctx->internal->tlsext_status_cb) { 1849 int r; 1850 /* Set resp to NULL, resplen to -1 so callback knows 1851 * there is no response. 1852 */ 1853 free(s->internal->tlsext_ocsp_resp); 1854 s->internal->tlsext_ocsp_resp = NULL; 1855 s->internal->tlsext_ocsp_resplen = -1; 1856 r = s->ctx->internal->tlsext_status_cb(s, 1857 s->ctx->internal->tlsext_status_arg); 1858 if (r == 0) { 1859 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 1860 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 1861 } 1862 if (r < 0) { 1863 al = SSL_AD_INTERNAL_ERROR; 1864 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 1865 } 1866 } 1867 1868 switch (ret) { 1869 case SSL_TLSEXT_ERR_ALERT_FATAL: 1870 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1871 1872 return -1; 1873 case SSL_TLSEXT_ERR_ALERT_WARNING: 1874 ssl3_send_alert(s, SSL3_AL_WARNING, al); 1875 1876 return 1; 1877 case SSL_TLSEXT_ERR_NOACK: 1878 s->internal->servername_done = 0; 1879 default: 1880 return 1; 1881 } 1882 } 1883 1884 /* Since the server cache lookup is done early on in the processing of the 1885 * ClientHello, and other operations depend on the result, we need to handle 1886 * any TLS session ticket extension at the same time. 1887 * 1888 * session_id: points at the session ID in the ClientHello. This code will 1889 * read past the end of this in order to parse out the session ticket 1890 * extension, if any. 1891 * len: the length of the session ID. 1892 * limit: a pointer to the first byte after the ClientHello. 1893 * ret: (output) on return, if a ticket was decrypted, then this is set to 1894 * point to the resulting session. 1895 * 1896 * If s->internal->tls_session_secret_cb is set then we are expecting a pre-shared key 1897 * ciphersuite, in which case we have no use for session tickets and one will 1898 * never be decrypted, nor will s->internal->tlsext_ticket_expected be set to 1. 1899 * 1900 * Returns: 1901 * -1: fatal error, either from parsing or decrypting the ticket. 1902 * 0: no ticket was found (or was ignored, based on settings). 1903 * 1: a zero length extension was found, indicating that the client supports 1904 * session tickets but doesn't currently have one to offer. 1905 * 2: either s->internal->tls_session_secret_cb was set, or a ticket was offered but 1906 * couldn't be decrypted because of a non-fatal error. 1907 * 3: a ticket was successfully decrypted and *ret was set. 1908 * 1909 * Side effects: 1910 * Sets s->internal->tlsext_ticket_expected to 1 if the server will have to issue 1911 * a new session ticket to the client because the client indicated support 1912 * (and s->internal->tls_session_secret_cb is NULL) but the client either doesn't have 1913 * a session ticket or we couldn't use the one it gave us, or if 1914 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. 1915 * Otherwise, s->internal->tlsext_ticket_expected is set to 0. 1916 */ 1917 int 1918 tls1_process_ticket(SSL *s, const unsigned char *session, int session_len, 1919 const unsigned char *limit, SSL_SESSION **ret) 1920 { 1921 /* Point after session ID in client hello */ 1922 CBS session_id, cookie, cipher_list, compress_algo, extensions; 1923 1924 *ret = NULL; 1925 s->internal->tlsext_ticket_expected = 0; 1926 1927 /* If tickets disabled behave as if no ticket present 1928 * to permit stateful resumption. 1929 */ 1930 if (SSL_get_options(s) & SSL_OP_NO_TICKET) 1931 return 0; 1932 if (!limit) 1933 return 0; 1934 1935 if (limit < session) 1936 return -1; 1937 1938 CBS_init(&session_id, session, limit - session); 1939 1940 /* Skip past the session id */ 1941 if (!CBS_skip(&session_id, session_len)) 1942 return -1; 1943 1944 /* Skip past DTLS cookie */ 1945 if (SSL_IS_DTLS(s)) { 1946 if (!CBS_get_u8_length_prefixed(&session_id, &cookie)) 1947 return -1; 1948 } 1949 1950 /* Skip past cipher list */ 1951 if (!CBS_get_u16_length_prefixed(&session_id, &cipher_list)) 1952 return -1; 1953 1954 /* Skip past compression algorithm list */ 1955 if (!CBS_get_u8_length_prefixed(&session_id, &compress_algo)) 1956 return -1; 1957 1958 /* Now at start of extensions */ 1959 if (CBS_len(&session_id) == 0) 1960 return 0; 1961 if (!CBS_get_u16_length_prefixed(&session_id, &extensions)) 1962 return -1; 1963 1964 while (CBS_len(&extensions) > 0) { 1965 CBS ext_data; 1966 uint16_t ext_type; 1967 1968 if (!CBS_get_u16(&extensions, &ext_type) || 1969 !CBS_get_u16_length_prefixed(&extensions, &ext_data)) 1970 return -1; 1971 1972 if (ext_type == TLSEXT_TYPE_session_ticket) { 1973 int r; 1974 if (CBS_len(&ext_data) == 0) { 1975 /* The client will accept a ticket but doesn't 1976 * currently have one. */ 1977 s->internal->tlsext_ticket_expected = 1; 1978 return 1; 1979 } 1980 if (s->internal->tls_session_secret_cb) { 1981 /* Indicate that the ticket couldn't be 1982 * decrypted rather than generating the session 1983 * from ticket now, trigger abbreviated 1984 * handshake based on external mechanism to 1985 * calculate the master secret later. */ 1986 return 2; 1987 } 1988 1989 r = tls_decrypt_ticket(s, CBS_data(&ext_data), 1990 CBS_len(&ext_data), session, session_len, ret); 1991 1992 switch (r) { 1993 case 2: /* ticket couldn't be decrypted */ 1994 s->internal->tlsext_ticket_expected = 1; 1995 return 2; 1996 case 3: /* ticket was decrypted */ 1997 return r; 1998 case 4: /* ticket decrypted but need to renew */ 1999 s->internal->tlsext_ticket_expected = 1; 2000 return 3; 2001 default: /* fatal error */ 2002 return -1; 2003 } 2004 } 2005 } 2006 return 0; 2007 } 2008 2009 /* tls_decrypt_ticket attempts to decrypt a session ticket. 2010 * 2011 * etick: points to the body of the session ticket extension. 2012 * eticklen: the length of the session tickets extenion. 2013 * sess_id: points at the session ID. 2014 * sesslen: the length of the session ID. 2015 * psess: (output) on return, if a ticket was decrypted, then this is set to 2016 * point to the resulting session. 2017 * 2018 * Returns: 2019 * -1: fatal error, either from parsing or decrypting the ticket. 2020 * 2: the ticket couldn't be decrypted. 2021 * 3: a ticket was successfully decrypted and *psess was set. 2022 * 4: same as 3, but the ticket needs to be renewed. 2023 */ 2024 static int 2025 tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen, 2026 const unsigned char *sess_id, int sesslen, SSL_SESSION **psess) 2027 { 2028 SSL_SESSION *sess; 2029 unsigned char *sdec; 2030 const unsigned char *p; 2031 int slen, mlen, renew_ticket = 0; 2032 unsigned char tick_hmac[EVP_MAX_MD_SIZE]; 2033 HMAC_CTX hctx; 2034 EVP_CIPHER_CTX ctx; 2035 SSL_CTX *tctx = s->initial_ctx; 2036 2037 /* 2038 * The API guarantees EVP_MAX_IV_LENGTH bytes of space for 2039 * the iv to tlsext_ticket_key_cb(). Since the total space 2040 * required for a session cookie is never less than this, 2041 * this check isn't too strict. The exact check comes later. 2042 */ 2043 if (eticklen < 16 + EVP_MAX_IV_LENGTH) 2044 return 2; 2045 2046 /* Initialize session ticket encryption and HMAC contexts */ 2047 HMAC_CTX_init(&hctx); 2048 EVP_CIPHER_CTX_init(&ctx); 2049 if (tctx->internal->tlsext_ticket_key_cb) { 2050 unsigned char *nctick = (unsigned char *)etick; 2051 int rv = tctx->internal->tlsext_ticket_key_cb(s, 2052 nctick, nctick + 16, &ctx, &hctx, 0); 2053 if (rv < 0) { 2054 HMAC_CTX_cleanup(&hctx); 2055 EVP_CIPHER_CTX_cleanup(&ctx); 2056 return -1; 2057 } 2058 if (rv == 0) { 2059 HMAC_CTX_cleanup(&hctx); 2060 EVP_CIPHER_CTX_cleanup(&ctx); 2061 return 2; 2062 } 2063 if (rv == 2) 2064 renew_ticket = 1; 2065 } else { 2066 /* Check key name matches */ 2067 if (timingsafe_memcmp(etick, 2068 tctx->internal->tlsext_tick_key_name, 16)) 2069 return 2; 2070 HMAC_Init_ex(&hctx, tctx->internal->tlsext_tick_hmac_key, 2071 16, tlsext_tick_md(), NULL); 2072 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, 2073 tctx->internal->tlsext_tick_aes_key, etick + 16); 2074 } 2075 2076 /* 2077 * Attempt to process session ticket, first conduct sanity and 2078 * integrity checks on ticket. 2079 */ 2080 mlen = HMAC_size(&hctx); 2081 if (mlen < 0) { 2082 HMAC_CTX_cleanup(&hctx); 2083 EVP_CIPHER_CTX_cleanup(&ctx); 2084 return -1; 2085 } 2086 2087 /* Sanity check ticket length: must exceed keyname + IV + HMAC */ 2088 if (eticklen <= 16 + EVP_CIPHER_CTX_iv_length(&ctx) + mlen) { 2089 HMAC_CTX_cleanup(&hctx); 2090 EVP_CIPHER_CTX_cleanup(&ctx); 2091 return 2; 2092 } 2093 eticklen -= mlen; 2094 2095 /* Check HMAC of encrypted ticket */ 2096 if (HMAC_Update(&hctx, etick, eticklen) <= 0 || 2097 HMAC_Final(&hctx, tick_hmac, NULL) <= 0) { 2098 HMAC_CTX_cleanup(&hctx); 2099 EVP_CIPHER_CTX_cleanup(&ctx); 2100 return -1; 2101 } 2102 2103 HMAC_CTX_cleanup(&hctx); 2104 if (timingsafe_memcmp(tick_hmac, etick + eticklen, mlen)) { 2105 EVP_CIPHER_CTX_cleanup(&ctx); 2106 return 2; 2107 } 2108 2109 /* Attempt to decrypt session data */ 2110 /* Move p after IV to start of encrypted ticket, update length */ 2111 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); 2112 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); 2113 sdec = malloc(eticklen); 2114 if (sdec == NULL || 2115 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen) <= 0) { 2116 free(sdec); 2117 EVP_CIPHER_CTX_cleanup(&ctx); 2118 return -1; 2119 } 2120 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) { 2121 free(sdec); 2122 EVP_CIPHER_CTX_cleanup(&ctx); 2123 return 2; 2124 } 2125 slen += mlen; 2126 EVP_CIPHER_CTX_cleanup(&ctx); 2127 p = sdec; 2128 2129 sess = d2i_SSL_SESSION(NULL, &p, slen); 2130 free(sdec); 2131 if (sess) { 2132 /* The session ID, if non-empty, is used by some clients to 2133 * detect that the ticket has been accepted. So we copy it to 2134 * the session structure. If it is empty set length to zero 2135 * as required by standard. 2136 */ 2137 if (sesslen) 2138 memcpy(sess->session_id, sess_id, sesslen); 2139 sess->session_id_length = sesslen; 2140 *psess = sess; 2141 if (renew_ticket) 2142 return 4; 2143 else 2144 return 3; 2145 } 2146 ERR_clear_error(); 2147 /* For session parse failure, indicate that we need to send a new 2148 * ticket. */ 2149 return 2; 2150 } 2151 2152 /* Tables to translate from NIDs to TLS v1.2 ids */ 2153 2154 typedef struct { 2155 int nid; 2156 int id; 2157 } tls12_lookup; 2158 2159 static tls12_lookup tls12_md[] = { 2160 {NID_md5, TLSEXT_hash_md5}, 2161 {NID_sha1, TLSEXT_hash_sha1}, 2162 {NID_sha224, TLSEXT_hash_sha224}, 2163 {NID_sha256, TLSEXT_hash_sha256}, 2164 {NID_sha384, TLSEXT_hash_sha384}, 2165 {NID_sha512, TLSEXT_hash_sha512}, 2166 {NID_id_GostR3411_94, TLSEXT_hash_gost94}, 2167 {NID_id_tc26_gost3411_2012_256, TLSEXT_hash_streebog_256}, 2168 {NID_id_tc26_gost3411_2012_512, TLSEXT_hash_streebog_512} 2169 }; 2170 2171 static tls12_lookup tls12_sig[] = { 2172 {EVP_PKEY_RSA, TLSEXT_signature_rsa}, 2173 {EVP_PKEY_DSA, TLSEXT_signature_dsa}, 2174 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}, 2175 {EVP_PKEY_GOSTR01, TLSEXT_signature_gostr01}, 2176 }; 2177 2178 static int 2179 tls12_find_id(int nid, tls12_lookup *table, size_t tlen) 2180 { 2181 size_t i; 2182 for (i = 0; i < tlen; i++) { 2183 if (table[i].nid == nid) 2184 return table[i].id; 2185 } 2186 return -1; 2187 } 2188 2189 int 2190 tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md) 2191 { 2192 int sig_id, md_id; 2193 if (!md) 2194 return 0; 2195 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, 2196 sizeof(tls12_md) / sizeof(tls12_lookup)); 2197 if (md_id == -1) 2198 return 0; 2199 sig_id = tls12_get_sigid(pk); 2200 if (sig_id == -1) 2201 return 0; 2202 p[0] = (unsigned char)md_id; 2203 p[1] = (unsigned char)sig_id; 2204 return 1; 2205 } 2206 2207 int 2208 tls12_get_sigid(const EVP_PKEY *pk) 2209 { 2210 return tls12_find_id(pk->type, tls12_sig, 2211 sizeof(tls12_sig) / sizeof(tls12_lookup)); 2212 } 2213 2214 const EVP_MD * 2215 tls12_get_hash(unsigned char hash_alg) 2216 { 2217 switch (hash_alg) { 2218 case TLSEXT_hash_sha1: 2219 return EVP_sha1(); 2220 case TLSEXT_hash_sha224: 2221 return EVP_sha224(); 2222 case TLSEXT_hash_sha256: 2223 return EVP_sha256(); 2224 case TLSEXT_hash_sha384: 2225 return EVP_sha384(); 2226 case TLSEXT_hash_sha512: 2227 return EVP_sha512(); 2228 #ifndef OPENSSL_NO_GOST 2229 case TLSEXT_hash_gost94: 2230 return EVP_gostr341194(); 2231 case TLSEXT_hash_streebog_256: 2232 return EVP_streebog256(); 2233 case TLSEXT_hash_streebog_512: 2234 return EVP_streebog512(); 2235 #endif 2236 default: 2237 return NULL; 2238 } 2239 } 2240 2241 /* Set preferred digest for each key type */ 2242 2243 int 2244 tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize) 2245 { 2246 int idx; 2247 const EVP_MD *md; 2248 CERT *c = s->cert; 2249 CBS cbs; 2250 2251 /* Extension ignored for inappropriate versions */ 2252 if (!SSL_USE_SIGALGS(s)) 2253 return 1; 2254 2255 /* Should never happen */ 2256 if (!c || dsize < 0) 2257 return 0; 2258 2259 CBS_init(&cbs, data, dsize); 2260 2261 c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL; 2262 c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL; 2263 c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL; 2264 c->pkeys[SSL_PKEY_ECC].digest = NULL; 2265 c->pkeys[SSL_PKEY_GOST01].digest = NULL; 2266 2267 while (CBS_len(&cbs) > 0) { 2268 uint8_t hash_alg, sig_alg; 2269 2270 if (!CBS_get_u8(&cbs, &hash_alg) || 2271 !CBS_get_u8(&cbs, &sig_alg)) { 2272 /* Should never happen */ 2273 return 0; 2274 } 2275 2276 switch (sig_alg) { 2277 case TLSEXT_signature_rsa: 2278 idx = SSL_PKEY_RSA_SIGN; 2279 break; 2280 case TLSEXT_signature_dsa: 2281 idx = SSL_PKEY_DSA_SIGN; 2282 break; 2283 case TLSEXT_signature_ecdsa: 2284 idx = SSL_PKEY_ECC; 2285 break; 2286 case TLSEXT_signature_gostr01: 2287 case TLSEXT_signature_gostr12_256: 2288 case TLSEXT_signature_gostr12_512: 2289 idx = SSL_PKEY_GOST01; 2290 break; 2291 default: 2292 continue; 2293 } 2294 2295 if (c->pkeys[idx].digest == NULL) { 2296 md = tls12_get_hash(hash_alg); 2297 if (md) { 2298 c->pkeys[idx].digest = md; 2299 if (idx == SSL_PKEY_RSA_SIGN) 2300 c->pkeys[SSL_PKEY_RSA_ENC].digest = md; 2301 } 2302 } 2303 2304 } 2305 2306 /* Set any remaining keys to default values. NOTE: if alg is not 2307 * supported it stays as NULL. 2308 */ 2309 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest) 2310 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1(); 2311 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) { 2312 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); 2313 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); 2314 } 2315 if (!c->pkeys[SSL_PKEY_ECC].digest) 2316 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); 2317 #ifndef OPENSSL_NO_GOST 2318 if (!c->pkeys[SSL_PKEY_GOST01].digest) 2319 c->pkeys[SSL_PKEY_GOST01].digest = EVP_gostr341194(); 2320 #endif 2321 return 1; 2322 } 2323