1 /* 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <openssl/objects.h> 13 #include <openssl/evp.h> 14 #include <openssl/hmac.h> 15 #include <openssl/ocsp.h> 16 #include <openssl/conf.h> 17 #include <openssl/x509v3.h> 18 #include <openssl/dh.h> 19 #include <openssl/bn.h> 20 #include "internal/nelem.h" 21 #include "ssl_locl.h" 22 #include <openssl/ct.h> 23 24 SSL3_ENC_METHOD const TLSv1_enc_data = { 25 tls1_enc, 26 tls1_mac, 27 tls1_setup_key_block, 28 tls1_generate_master_secret, 29 tls1_change_cipher_state, 30 tls1_final_finish_mac, 31 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, 32 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, 33 tls1_alert_code, 34 tls1_export_keying_material, 35 0, 36 ssl3_set_handshake_header, 37 tls_close_construct_packet, 38 ssl3_handshake_write 39 }; 40 41 SSL3_ENC_METHOD const TLSv1_1_enc_data = { 42 tls1_enc, 43 tls1_mac, 44 tls1_setup_key_block, 45 tls1_generate_master_secret, 46 tls1_change_cipher_state, 47 tls1_final_finish_mac, 48 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, 49 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, 50 tls1_alert_code, 51 tls1_export_keying_material, 52 SSL_ENC_FLAG_EXPLICIT_IV, 53 ssl3_set_handshake_header, 54 tls_close_construct_packet, 55 ssl3_handshake_write 56 }; 57 58 SSL3_ENC_METHOD const TLSv1_2_enc_data = { 59 tls1_enc, 60 tls1_mac, 61 tls1_setup_key_block, 62 tls1_generate_master_secret, 63 tls1_change_cipher_state, 64 tls1_final_finish_mac, 65 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, 66 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, 67 tls1_alert_code, 68 tls1_export_keying_material, 69 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF 70 | SSL_ENC_FLAG_TLS1_2_CIPHERS, 71 ssl3_set_handshake_header, 72 tls_close_construct_packet, 73 ssl3_handshake_write 74 }; 75 76 SSL3_ENC_METHOD const TLSv1_3_enc_data = { 77 tls13_enc, 78 tls1_mac, 79 tls13_setup_key_block, 80 tls13_generate_master_secret, 81 tls13_change_cipher_state, 82 tls13_final_finish_mac, 83 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, 84 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, 85 tls13_alert_code, 86 tls13_export_keying_material, 87 SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, 88 ssl3_set_handshake_header, 89 tls_close_construct_packet, 90 ssl3_handshake_write 91 }; 92 93 long tls1_default_timeout(void) 94 { 95 /* 96 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for 97 * http, the cache would over fill 98 */ 99 return (60 * 60 * 2); 100 } 101 102 int tls1_new(SSL *s) 103 { 104 if (!ssl3_new(s)) 105 return 0; 106 if (!s->method->ssl_clear(s)) 107 return 0; 108 109 return 1; 110 } 111 112 void tls1_free(SSL *s) 113 { 114 OPENSSL_free(s->ext.session_ticket); 115 ssl3_free(s); 116 } 117 118 int tls1_clear(SSL *s) 119 { 120 if (!ssl3_clear(s)) 121 return 0; 122 123 if (s->method->version == TLS_ANY_VERSION) 124 s->version = TLS_MAX_VERSION; 125 else 126 s->version = s->method->version; 127 128 return 1; 129 } 130 131 #ifndef OPENSSL_NO_EC 132 133 /* 134 * Table of curve information. 135 * Do not delete entries or reorder this array! It is used as a lookup 136 * table: the index of each entry is one less than the TLS curve id. 137 */ 138 static const TLS_GROUP_INFO nid_list[] = { 139 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */ 140 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */ 141 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */ 142 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */ 143 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */ 144 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */ 145 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */ 146 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */ 147 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */ 148 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */ 149 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */ 150 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */ 151 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */ 152 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */ 153 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */ 154 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */ 155 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */ 156 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */ 157 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */ 158 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */ 159 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */ 160 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */ 161 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */ 162 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */ 163 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */ 164 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */ 165 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */ 166 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */ 167 {EVP_PKEY_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */ 168 {EVP_PKEY_X448, 224, TLS_CURVE_CUSTOM}, /* X448 (30) */ 169 }; 170 171 static const unsigned char ecformats_default[] = { 172 TLSEXT_ECPOINTFORMAT_uncompressed, 173 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, 174 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 175 }; 176 177 /* The default curves */ 178 static const uint16_t eccurves_default[] = { 179 29, /* X25519 (29) */ 180 23, /* secp256r1 (23) */ 181 30, /* X448 (30) */ 182 25, /* secp521r1 (25) */ 183 24, /* secp384r1 (24) */ 184 }; 185 186 static const uint16_t suiteb_curves[] = { 187 TLSEXT_curve_P_256, 188 TLSEXT_curve_P_384 189 }; 190 191 const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id) 192 { 193 /* ECC curves from RFC 4492 and RFC 7027 */ 194 if (group_id < 1 || group_id > OSSL_NELEM(nid_list)) 195 return NULL; 196 return &nid_list[group_id - 1]; 197 } 198 199 static uint16_t tls1_nid2group_id(int nid) 200 { 201 size_t i; 202 for (i = 0; i < OSSL_NELEM(nid_list); i++) { 203 if (nid_list[i].nid == nid) 204 return (uint16_t)(i + 1); 205 } 206 return 0; 207 } 208 209 /* 210 * Set *pgroups to the supported groups list and *pgroupslen to 211 * the number of groups supported. 212 */ 213 void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups, 214 size_t *pgroupslen) 215 { 216 217 /* For Suite B mode only include P-256, P-384 */ 218 switch (tls1_suiteb(s)) { 219 case SSL_CERT_FLAG_SUITEB_128_LOS: 220 *pgroups = suiteb_curves; 221 *pgroupslen = OSSL_NELEM(suiteb_curves); 222 break; 223 224 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: 225 *pgroups = suiteb_curves; 226 *pgroupslen = 1; 227 break; 228 229 case SSL_CERT_FLAG_SUITEB_192_LOS: 230 *pgroups = suiteb_curves + 1; 231 *pgroupslen = 1; 232 break; 233 234 default: 235 if (s->ext.supportedgroups == NULL) { 236 *pgroups = eccurves_default; 237 *pgroupslen = OSSL_NELEM(eccurves_default); 238 } else { 239 *pgroups = s->ext.supportedgroups; 240 *pgroupslen = s->ext.supportedgroups_len; 241 } 242 break; 243 } 244 } 245 246 /* See if curve is allowed by security callback */ 247 int tls_curve_allowed(SSL *s, uint16_t curve, int op) 248 { 249 const TLS_GROUP_INFO *cinfo = tls1_group_id_lookup(curve); 250 unsigned char ctmp[2]; 251 252 if (cinfo == NULL) 253 return 0; 254 # ifdef OPENSSL_NO_EC2M 255 if (cinfo->flags & TLS_CURVE_CHAR2) 256 return 0; 257 # endif 258 ctmp[0] = curve >> 8; 259 ctmp[1] = curve & 0xff; 260 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)ctmp); 261 } 262 263 /* Return 1 if "id" is in "list" */ 264 static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen) 265 { 266 size_t i; 267 for (i = 0; i < listlen; i++) 268 if (list[i] == id) 269 return 1; 270 return 0; 271 } 272 273 /*- 274 * For nmatch >= 0, return the id of the |nmatch|th shared group or 0 275 * if there is no match. 276 * For nmatch == -1, return number of matches 277 * For nmatch == -2, return the id of the group to use for 278 * a tmp key, or 0 if there is no match. 279 */ 280 uint16_t tls1_shared_group(SSL *s, int nmatch) 281 { 282 const uint16_t *pref, *supp; 283 size_t num_pref, num_supp, i; 284 int k; 285 286 /* Can't do anything on client side */ 287 if (s->server == 0) 288 return 0; 289 if (nmatch == -2) { 290 if (tls1_suiteb(s)) { 291 /* 292 * For Suite B ciphersuite determines curve: we already know 293 * these are acceptable due to previous checks. 294 */ 295 unsigned long cid = s->s3->tmp.new_cipher->id; 296 297 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) 298 return TLSEXT_curve_P_256; 299 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) 300 return TLSEXT_curve_P_384; 301 /* Should never happen */ 302 return 0; 303 } 304 /* If not Suite B just return first preference shared curve */ 305 nmatch = 0; 306 } 307 /* 308 * If server preference set, our groups are the preference order 309 * otherwise peer decides. 310 */ 311 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { 312 tls1_get_supported_groups(s, &pref, &num_pref); 313 tls1_get_peer_groups(s, &supp, &num_supp); 314 } else { 315 tls1_get_peer_groups(s, &pref, &num_pref); 316 tls1_get_supported_groups(s, &supp, &num_supp); 317 } 318 319 for (k = 0, i = 0; i < num_pref; i++) { 320 uint16_t id = pref[i]; 321 322 if (!tls1_in_list(id, supp, num_supp) 323 || !tls_curve_allowed(s, id, SSL_SECOP_CURVE_SHARED)) 324 continue; 325 if (nmatch == k) 326 return id; 327 k++; 328 } 329 if (nmatch == -1) 330 return k; 331 /* Out of range (nmatch > k). */ 332 return 0; 333 } 334 335 int tls1_set_groups(uint16_t **pext, size_t *pextlen, 336 int *groups, size_t ngroups) 337 { 338 uint16_t *glist; 339 size_t i; 340 /* 341 * Bitmap of groups included to detect duplicates: only works while group 342 * ids < 32 343 */ 344 unsigned long dup_list = 0; 345 346 if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) { 347 SSLerr(SSL_F_TLS1_SET_GROUPS, ERR_R_MALLOC_FAILURE); 348 return 0; 349 } 350 for (i = 0; i < ngroups; i++) { 351 unsigned long idmask; 352 uint16_t id; 353 /* TODO(TLS1.3): Convert for DH groups */ 354 id = tls1_nid2group_id(groups[i]); 355 idmask = 1L << id; 356 if (!id || (dup_list & idmask)) { 357 OPENSSL_free(glist); 358 return 0; 359 } 360 dup_list |= idmask; 361 glist[i] = id; 362 } 363 OPENSSL_free(*pext); 364 *pext = glist; 365 *pextlen = ngroups; 366 return 1; 367 } 368 369 # define MAX_CURVELIST OSSL_NELEM(nid_list) 370 371 typedef struct { 372 size_t nidcnt; 373 int nid_arr[MAX_CURVELIST]; 374 } nid_cb_st; 375 376 static int nid_cb(const char *elem, int len, void *arg) 377 { 378 nid_cb_st *narg = arg; 379 size_t i; 380 int nid; 381 char etmp[20]; 382 if (elem == NULL) 383 return 0; 384 if (narg->nidcnt == MAX_CURVELIST) 385 return 0; 386 if (len > (int)(sizeof(etmp) - 1)) 387 return 0; 388 memcpy(etmp, elem, len); 389 etmp[len] = 0; 390 nid = EC_curve_nist2nid(etmp); 391 if (nid == NID_undef) 392 nid = OBJ_sn2nid(etmp); 393 if (nid == NID_undef) 394 nid = OBJ_ln2nid(etmp); 395 if (nid == NID_undef) 396 return 0; 397 for (i = 0; i < narg->nidcnt; i++) 398 if (narg->nid_arr[i] == nid) 399 return 0; 400 narg->nid_arr[narg->nidcnt++] = nid; 401 return 1; 402 } 403 404 /* Set groups based on a colon separate list */ 405 int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str) 406 { 407 nid_cb_st ncb; 408 ncb.nidcnt = 0; 409 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) 410 return 0; 411 if (pext == NULL) 412 return 1; 413 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt); 414 } 415 /* Return group id of a key */ 416 static uint16_t tls1_get_group_id(EVP_PKEY *pkey) 417 { 418 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); 419 const EC_GROUP *grp; 420 421 if (ec == NULL) 422 return 0; 423 grp = EC_KEY_get0_group(ec); 424 return tls1_nid2group_id(EC_GROUP_get_curve_name(grp)); 425 } 426 427 /* Check a key is compatible with compression extension */ 428 static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey) 429 { 430 const EC_KEY *ec; 431 const EC_GROUP *grp; 432 unsigned char comp_id; 433 size_t i; 434 435 /* If not an EC key nothing to check */ 436 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) 437 return 1; 438 ec = EVP_PKEY_get0_EC_KEY(pkey); 439 grp = EC_KEY_get0_group(ec); 440 441 /* Get required compression id */ 442 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { 443 comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; 444 } else if (SSL_IS_TLS13(s)) { 445 /* 446 * ec_point_formats extension is not used in TLSv1.3 so we ignore 447 * this check. 448 */ 449 return 1; 450 } else { 451 int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp)); 452 453 if (field_type == NID_X9_62_prime_field) 454 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; 455 else if (field_type == NID_X9_62_characteristic_two_field) 456 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; 457 else 458 return 0; 459 } 460 /* 461 * If point formats extension present check it, otherwise everything is 462 * supported (see RFC4492). 463 */ 464 if (s->session->ext.ecpointformats == NULL) 465 return 1; 466 467 for (i = 0; i < s->session->ext.ecpointformats_len; i++) { 468 if (s->session->ext.ecpointformats[i] == comp_id) 469 return 1; 470 } 471 return 0; 472 } 473 474 /* Check a group id matches preferences */ 475 int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups) 476 { 477 const uint16_t *groups; 478 size_t groups_len; 479 480 if (group_id == 0) 481 return 0; 482 483 /* Check for Suite B compliance */ 484 if (tls1_suiteb(s) && s->s3->tmp.new_cipher != NULL) { 485 unsigned long cid = s->s3->tmp.new_cipher->id; 486 487 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { 488 if (group_id != TLSEXT_curve_P_256) 489 return 0; 490 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { 491 if (group_id != TLSEXT_curve_P_384) 492 return 0; 493 } else { 494 /* Should never happen */ 495 return 0; 496 } 497 } 498 499 if (check_own_groups) { 500 /* Check group is one of our preferences */ 501 tls1_get_supported_groups(s, &groups, &groups_len); 502 if (!tls1_in_list(group_id, groups, groups_len)) 503 return 0; 504 } 505 506 if (!tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_CHECK)) 507 return 0; 508 509 /* For clients, nothing more to check */ 510 if (!s->server) 511 return 1; 512 513 /* Check group is one of peers preferences */ 514 tls1_get_peer_groups(s, &groups, &groups_len); 515 516 /* 517 * RFC 4492 does not require the supported elliptic curves extension 518 * so if it is not sent we can just choose any curve. 519 * It is invalid to send an empty list in the supported groups 520 * extension, so groups_len == 0 always means no extension. 521 */ 522 if (groups_len == 0) 523 return 1; 524 return tls1_in_list(group_id, groups, groups_len); 525 } 526 527 void tls1_get_formatlist(SSL *s, const unsigned char **pformats, 528 size_t *num_formats) 529 { 530 /* 531 * If we have a custom point format list use it otherwise use default 532 */ 533 if (s->ext.ecpointformats) { 534 *pformats = s->ext.ecpointformats; 535 *num_formats = s->ext.ecpointformats_len; 536 } else { 537 *pformats = ecformats_default; 538 /* For Suite B we don't support char2 fields */ 539 if (tls1_suiteb(s)) 540 *num_formats = sizeof(ecformats_default) - 1; 541 else 542 *num_formats = sizeof(ecformats_default); 543 } 544 } 545 546 /* 547 * Check cert parameters compatible with extensions: currently just checks EC 548 * certificates have compatible curves and compression. 549 */ 550 static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md) 551 { 552 uint16_t group_id; 553 EVP_PKEY *pkey; 554 pkey = X509_get0_pubkey(x); 555 if (pkey == NULL) 556 return 0; 557 /* If not EC nothing to do */ 558 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) 559 return 1; 560 /* Check compression */ 561 if (!tls1_check_pkey_comp(s, pkey)) 562 return 0; 563 group_id = tls1_get_group_id(pkey); 564 /* 565 * For a server we allow the certificate to not be in our list of supported 566 * groups. 567 */ 568 if (!tls1_check_group_id(s, group_id, !s->server)) 569 return 0; 570 /* 571 * Special case for suite B. We *MUST* sign using SHA256+P-256 or 572 * SHA384+P-384. 573 */ 574 if (check_ee_md && tls1_suiteb(s)) { 575 int check_md; 576 size_t i; 577 CERT *c = s->cert; 578 579 /* Check to see we have necessary signing algorithm */ 580 if (group_id == TLSEXT_curve_P_256) 581 check_md = NID_ecdsa_with_SHA256; 582 else if (group_id == TLSEXT_curve_P_384) 583 check_md = NID_ecdsa_with_SHA384; 584 else 585 return 0; /* Should never happen */ 586 for (i = 0; i < c->shared_sigalgslen; i++) { 587 if (check_md == c->shared_sigalgs[i]->sigandhash) 588 return 1;; 589 } 590 return 0; 591 } 592 return 1; 593 } 594 595 /* 596 * tls1_check_ec_tmp_key - Check EC temporary key compatibility 597 * @s: SSL connection 598 * @cid: Cipher ID we're considering using 599 * 600 * Checks that the kECDHE cipher suite we're considering using 601 * is compatible with the client extensions. 602 * 603 * Returns 0 when the cipher can't be used or 1 when it can. 604 */ 605 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) 606 { 607 /* If not Suite B just need a shared group */ 608 if (!tls1_suiteb(s)) 609 return tls1_shared_group(s, 0) != 0; 610 /* 611 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other 612 * curves permitted. 613 */ 614 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) 615 return tls1_check_group_id(s, TLSEXT_curve_P_256, 1); 616 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) 617 return tls1_check_group_id(s, TLSEXT_curve_P_384, 1); 618 619 return 0; 620 } 621 622 #else 623 624 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) 625 { 626 return 1; 627 } 628 629 #endif /* OPENSSL_NO_EC */ 630 631 /* Default sigalg schemes */ 632 static const uint16_t tls12_sigalgs[] = { 633 #ifndef OPENSSL_NO_EC 634 TLSEXT_SIGALG_ecdsa_secp256r1_sha256, 635 TLSEXT_SIGALG_ecdsa_secp384r1_sha384, 636 TLSEXT_SIGALG_ecdsa_secp521r1_sha512, 637 TLSEXT_SIGALG_ed25519, 638 TLSEXT_SIGALG_ed448, 639 #endif 640 641 TLSEXT_SIGALG_rsa_pss_pss_sha256, 642 TLSEXT_SIGALG_rsa_pss_pss_sha384, 643 TLSEXT_SIGALG_rsa_pss_pss_sha512, 644 TLSEXT_SIGALG_rsa_pss_rsae_sha256, 645 TLSEXT_SIGALG_rsa_pss_rsae_sha384, 646 TLSEXT_SIGALG_rsa_pss_rsae_sha512, 647 648 TLSEXT_SIGALG_rsa_pkcs1_sha256, 649 TLSEXT_SIGALG_rsa_pkcs1_sha384, 650 TLSEXT_SIGALG_rsa_pkcs1_sha512, 651 652 #ifndef OPENSSL_NO_EC 653 TLSEXT_SIGALG_ecdsa_sha224, 654 TLSEXT_SIGALG_ecdsa_sha1, 655 #endif 656 TLSEXT_SIGALG_rsa_pkcs1_sha224, 657 TLSEXT_SIGALG_rsa_pkcs1_sha1, 658 #ifndef OPENSSL_NO_DSA 659 TLSEXT_SIGALG_dsa_sha224, 660 TLSEXT_SIGALG_dsa_sha1, 661 662 TLSEXT_SIGALG_dsa_sha256, 663 TLSEXT_SIGALG_dsa_sha384, 664 TLSEXT_SIGALG_dsa_sha512, 665 #endif 666 #ifndef OPENSSL_NO_GOST 667 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, 668 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, 669 TLSEXT_SIGALG_gostr34102001_gostr3411, 670 #endif 671 }; 672 673 #ifndef OPENSSL_NO_EC 674 static const uint16_t suiteb_sigalgs[] = { 675 TLSEXT_SIGALG_ecdsa_secp256r1_sha256, 676 TLSEXT_SIGALG_ecdsa_secp384r1_sha384 677 }; 678 #endif 679 680 static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { 681 #ifndef OPENSSL_NO_EC 682 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, 683 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, 684 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1}, 685 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, 686 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, 687 NID_ecdsa_with_SHA384, NID_secp384r1}, 688 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, 689 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, 690 NID_ecdsa_with_SHA512, NID_secp521r1}, 691 {"ed25519", TLSEXT_SIGALG_ed25519, 692 NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519, 693 NID_undef, NID_undef}, 694 {"ed448", TLSEXT_SIGALG_ed448, 695 NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448, 696 NID_undef, NID_undef}, 697 {NULL, TLSEXT_SIGALG_ecdsa_sha224, 698 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, 699 NID_ecdsa_with_SHA224, NID_undef}, 700 {NULL, TLSEXT_SIGALG_ecdsa_sha1, 701 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, 702 NID_ecdsa_with_SHA1, NID_undef}, 703 #endif 704 {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256, 705 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, 706 NID_undef, NID_undef}, 707 {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384, 708 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, 709 NID_undef, NID_undef}, 710 {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512, 711 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, 712 NID_undef, NID_undef}, 713 {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256, 714 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, 715 NID_undef, NID_undef}, 716 {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384, 717 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, 718 NID_undef, NID_undef}, 719 {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512, 720 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, 721 NID_undef, NID_undef}, 722 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, 723 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, 724 NID_sha256WithRSAEncryption, NID_undef}, 725 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, 726 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, 727 NID_sha384WithRSAEncryption, NID_undef}, 728 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, 729 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, 730 NID_sha512WithRSAEncryption, NID_undef}, 731 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224, 732 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, 733 NID_sha224WithRSAEncryption, NID_undef}, 734 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, 735 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, 736 NID_sha1WithRSAEncryption, NID_undef}, 737 #ifndef OPENSSL_NO_DSA 738 {NULL, TLSEXT_SIGALG_dsa_sha256, 739 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, 740 NID_dsa_with_SHA256, NID_undef}, 741 {NULL, TLSEXT_SIGALG_dsa_sha384, 742 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, 743 NID_undef, NID_undef}, 744 {NULL, TLSEXT_SIGALG_dsa_sha512, 745 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, 746 NID_undef, NID_undef}, 747 {NULL, TLSEXT_SIGALG_dsa_sha224, 748 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, 749 NID_undef, NID_undef}, 750 {NULL, TLSEXT_SIGALG_dsa_sha1, 751 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, 752 NID_dsaWithSHA1, NID_undef}, 753 #endif 754 #ifndef OPENSSL_NO_GOST 755 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, 756 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, 757 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, 758 NID_undef, NID_undef}, 759 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, 760 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, 761 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, 762 NID_undef, NID_undef}, 763 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, 764 NID_id_GostR3411_94, SSL_MD_GOST94_IDX, 765 NID_id_GostR3410_2001, SSL_PKEY_GOST01, 766 NID_undef, NID_undef} 767 #endif 768 }; 769 /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ 770 static const SIGALG_LOOKUP legacy_rsa_sigalg = { 771 "rsa_pkcs1_md5_sha1", 0, 772 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, 773 EVP_PKEY_RSA, SSL_PKEY_RSA, 774 NID_undef, NID_undef 775 }; 776 777 /* 778 * Default signature algorithm values used if signature algorithms not present. 779 * From RFC5246. Note: order must match certificate index order. 780 */ 781 static const uint16_t tls_default_sigalg[] = { 782 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ 783 0, /* SSL_PKEY_RSA_PSS_SIGN */ 784 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ 785 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ 786 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ 787 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */ 788 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */ 789 0, /* SSL_PKEY_ED25519 */ 790 0, /* SSL_PKEY_ED448 */ 791 }; 792 793 /* Lookup TLS signature algorithm */ 794 static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg) 795 { 796 size_t i; 797 const SIGALG_LOOKUP *s; 798 799 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); 800 i++, s++) { 801 if (s->sigalg == sigalg) 802 return s; 803 } 804 return NULL; 805 } 806 /* Lookup hash: return 0 if invalid or not enabled */ 807 int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd) 808 { 809 const EVP_MD *md; 810 if (lu == NULL) 811 return 0; 812 /* lu->hash == NID_undef means no associated digest */ 813 if (lu->hash == NID_undef) { 814 md = NULL; 815 } else { 816 md = ssl_md(lu->hash_idx); 817 if (md == NULL) 818 return 0; 819 } 820 if (pmd) 821 *pmd = md; 822 return 1; 823 } 824 825 /* 826 * Check if key is large enough to generate RSA-PSS signature. 827 * 828 * The key must greater than or equal to 2 * hash length + 2. 829 * SHA512 has a hash length of 64 bytes, which is incompatible 830 * with a 128 byte (1024 bit) key. 831 */ 832 #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2) 833 static int rsa_pss_check_min_key_size(const RSA *rsa, const SIGALG_LOOKUP *lu) 834 { 835 const EVP_MD *md; 836 837 if (rsa == NULL) 838 return 0; 839 if (!tls1_lookup_md(lu, &md) || md == NULL) 840 return 0; 841 if (RSA_size(rsa) < RSA_PSS_MINIMUM_KEY_SIZE(md)) 842 return 0; 843 return 1; 844 } 845 846 /* 847 * Return a signature algorithm for TLS < 1.2 where the signature type 848 * is fixed by the certificate type. 849 */ 850 static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx) 851 { 852 if (idx == -1) { 853 if (s->server) { 854 size_t i; 855 856 /* Work out index corresponding to ciphersuite */ 857 for (i = 0; i < SSL_PKEY_NUM; i++) { 858 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i); 859 860 if (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) { 861 idx = i; 862 break; 863 } 864 } 865 866 /* 867 * Some GOST ciphersuites allow more than one signature algorithms 868 * */ 869 if (idx == SSL_PKEY_GOST01 && s->s3->tmp.new_cipher->algorithm_auth != SSL_aGOST01) { 870 int real_idx; 871 872 for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01; 873 real_idx--) { 874 if (s->cert->pkeys[real_idx].privatekey != NULL) { 875 idx = real_idx; 876 break; 877 } 878 } 879 } 880 } else { 881 idx = s->cert->key - s->cert->pkeys; 882 } 883 } 884 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) 885 return NULL; 886 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { 887 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]); 888 889 if (!tls1_lookup_md(lu, NULL)) 890 return NULL; 891 return lu; 892 } 893 return &legacy_rsa_sigalg; 894 } 895 /* Set peer sigalg based key type */ 896 int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey) 897 { 898 size_t idx; 899 const SIGALG_LOOKUP *lu; 900 901 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) 902 return 0; 903 lu = tls1_get_legacy_sigalg(s, idx); 904 if (lu == NULL) 905 return 0; 906 s->s3->tmp.peer_sigalg = lu; 907 return 1; 908 } 909 910 size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs) 911 { 912 /* 913 * If Suite B mode use Suite B sigalgs only, ignore any other 914 * preferences. 915 */ 916 #ifndef OPENSSL_NO_EC 917 switch (tls1_suiteb(s)) { 918 case SSL_CERT_FLAG_SUITEB_128_LOS: 919 *psigs = suiteb_sigalgs; 920 return OSSL_NELEM(suiteb_sigalgs); 921 922 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: 923 *psigs = suiteb_sigalgs; 924 return 1; 925 926 case SSL_CERT_FLAG_SUITEB_192_LOS: 927 *psigs = suiteb_sigalgs + 1; 928 return 1; 929 } 930 #endif 931 /* 932 * We use client_sigalgs (if not NULL) if we're a server 933 * and sending a certificate request or if we're a client and 934 * determining which shared algorithm to use. 935 */ 936 if ((s->server == sent) && s->cert->client_sigalgs != NULL) { 937 *psigs = s->cert->client_sigalgs; 938 return s->cert->client_sigalgslen; 939 } else if (s->cert->conf_sigalgs) { 940 *psigs = s->cert->conf_sigalgs; 941 return s->cert->conf_sigalgslen; 942 } else { 943 *psigs = tls12_sigalgs; 944 return OSSL_NELEM(tls12_sigalgs); 945 } 946 } 947 948 /* 949 * Check signature algorithm is consistent with sent supported signature 950 * algorithms and if so set relevant digest and signature scheme in 951 * s. 952 */ 953 int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey) 954 { 955 const uint16_t *sent_sigs; 956 const EVP_MD *md = NULL; 957 char sigalgstr[2]; 958 size_t sent_sigslen, i, cidx; 959 int pkeyid = EVP_PKEY_id(pkey); 960 const SIGALG_LOOKUP *lu; 961 962 /* Should never happen */ 963 if (pkeyid == -1) 964 return -1; 965 if (SSL_IS_TLS13(s)) { 966 /* Disallow DSA for TLS 1.3 */ 967 if (pkeyid == EVP_PKEY_DSA) { 968 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG, 969 SSL_R_WRONG_SIGNATURE_TYPE); 970 return 0; 971 } 972 /* Only allow PSS for TLS 1.3 */ 973 if (pkeyid == EVP_PKEY_RSA) 974 pkeyid = EVP_PKEY_RSA_PSS; 975 } 976 lu = tls1_lookup_sigalg(sig); 977 /* 978 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type 979 * is consistent with signature: RSA keys can be used for RSA-PSS 980 */ 981 if (lu == NULL 982 || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224)) 983 || (pkeyid != lu->sig 984 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { 985 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG, 986 SSL_R_WRONG_SIGNATURE_TYPE); 987 return 0; 988 } 989 /* Check the sigalg is consistent with the key OID */ 990 if (!ssl_cert_lookup_by_nid(EVP_PKEY_id(pkey), &cidx) 991 || lu->sig_idx != (int)cidx) { 992 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG, 993 SSL_R_WRONG_SIGNATURE_TYPE); 994 return 0; 995 } 996 997 #ifndef OPENSSL_NO_EC 998 if (pkeyid == EVP_PKEY_EC) { 999 1000 /* Check point compression is permitted */ 1001 if (!tls1_check_pkey_comp(s, pkey)) { 1002 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 1003 SSL_F_TLS12_CHECK_PEER_SIGALG, 1004 SSL_R_ILLEGAL_POINT_COMPRESSION); 1005 return 0; 1006 } 1007 1008 /* For TLS 1.3 or Suite B check curve matches signature algorithm */ 1009 if (SSL_IS_TLS13(s) || tls1_suiteb(s)) { 1010 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); 1011 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); 1012 1013 if (lu->curve != NID_undef && curve != lu->curve) { 1014 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 1015 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); 1016 return 0; 1017 } 1018 } 1019 if (!SSL_IS_TLS13(s)) { 1020 /* Check curve matches extensions */ 1021 if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { 1022 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 1023 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); 1024 return 0; 1025 } 1026 if (tls1_suiteb(s)) { 1027 /* Check sigalg matches a permissible Suite B value */ 1028 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 1029 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { 1030 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 1031 SSL_F_TLS12_CHECK_PEER_SIGALG, 1032 SSL_R_WRONG_SIGNATURE_TYPE); 1033 return 0; 1034 } 1035 } 1036 } 1037 } else if (tls1_suiteb(s)) { 1038 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, 1039 SSL_R_WRONG_SIGNATURE_TYPE); 1040 return 0; 1041 } 1042 #endif 1043 1044 /* Check signature matches a type we sent */ 1045 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); 1046 for (i = 0; i < sent_sigslen; i++, sent_sigs++) { 1047 if (sig == *sent_sigs) 1048 break; 1049 } 1050 /* Allow fallback to SHA1 if not strict mode */ 1051 if (i == sent_sigslen && (lu->hash != NID_sha1 1052 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { 1053 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, 1054 SSL_R_WRONG_SIGNATURE_TYPE); 1055 return 0; 1056 } 1057 if (!tls1_lookup_md(lu, &md)) { 1058 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, 1059 SSL_R_UNKNOWN_DIGEST); 1060 return 0; 1061 } 1062 if (md != NULL) { 1063 /* 1064 * Make sure security callback allows algorithm. For historical 1065 * reasons we have to pass the sigalg as a two byte char array. 1066 */ 1067 sigalgstr[0] = (sig >> 8) & 0xff; 1068 sigalgstr[1] = sig & 0xff; 1069 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, 1070 EVP_MD_size(md) * 4, EVP_MD_type(md), 1071 (void *)sigalgstr)) { 1072 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG, 1073 SSL_R_WRONG_SIGNATURE_TYPE); 1074 return 0; 1075 } 1076 } 1077 /* Store the sigalg the peer uses */ 1078 s->s3->tmp.peer_sigalg = lu; 1079 return 1; 1080 } 1081 1082 int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) 1083 { 1084 if (s->s3->tmp.peer_sigalg == NULL) 1085 return 0; 1086 *pnid = s->s3->tmp.peer_sigalg->sig; 1087 return 1; 1088 } 1089 1090 /* 1091 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't 1092 * supported, doesn't appear in supported signature algorithms, isn't supported 1093 * by the enabled protocol versions or by the security level. 1094 * 1095 * This function should only be used for checking which ciphers are supported 1096 * by the client. 1097 * 1098 * Call ssl_cipher_disabled() to check that it's enabled or not. 1099 */ 1100 int ssl_set_client_disabled(SSL *s) 1101 { 1102 s->s3->tmp.mask_a = 0; 1103 s->s3->tmp.mask_k = 0; 1104 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); 1105 if (ssl_get_min_max_version(s, &s->s3->tmp.min_ver, 1106 &s->s3->tmp.max_ver, NULL) != 0) 1107 return 0; 1108 #ifndef OPENSSL_NO_PSK 1109 /* with PSK there must be client callback set */ 1110 if (!s->psk_client_callback) { 1111 s->s3->tmp.mask_a |= SSL_aPSK; 1112 s->s3->tmp.mask_k |= SSL_PSK; 1113 } 1114 #endif /* OPENSSL_NO_PSK */ 1115 #ifndef OPENSSL_NO_SRP 1116 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { 1117 s->s3->tmp.mask_a |= SSL_aSRP; 1118 s->s3->tmp.mask_k |= SSL_kSRP; 1119 } 1120 #endif 1121 return 1; 1122 } 1123 1124 /* 1125 * ssl_cipher_disabled - check that a cipher is disabled or not 1126 * @s: SSL connection that you want to use the cipher on 1127 * @c: cipher to check 1128 * @op: Security check that you want to do 1129 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 1130 * 1131 * Returns 1 when it's disabled, 0 when enabled. 1132 */ 1133 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe) 1134 { 1135 if (c->algorithm_mkey & s->s3->tmp.mask_k 1136 || c->algorithm_auth & s->s3->tmp.mask_a) 1137 return 1; 1138 if (s->s3->tmp.max_ver == 0) 1139 return 1; 1140 if (!SSL_IS_DTLS(s)) { 1141 int min_tls = c->min_tls; 1142 1143 /* 1144 * For historical reasons we will allow ECHDE to be selected by a server 1145 * in SSLv3 if we are a client 1146 */ 1147 if (min_tls == TLS1_VERSION && ecdhe 1148 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) 1149 min_tls = SSL3_VERSION; 1150 1151 if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver)) 1152 return 1; 1153 } 1154 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver) 1155 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) 1156 return 1; 1157 1158 return !ssl_security(s, op, c->strength_bits, 0, (void *)c); 1159 } 1160 1161 int tls_use_ticket(SSL *s) 1162 { 1163 if ((s->options & SSL_OP_NO_TICKET)) 1164 return 0; 1165 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); 1166 } 1167 1168 int tls1_set_server_sigalgs(SSL *s) 1169 { 1170 size_t i; 1171 1172 /* Clear any shared signature algorithms */ 1173 OPENSSL_free(s->cert->shared_sigalgs); 1174 s->cert->shared_sigalgs = NULL; 1175 s->cert->shared_sigalgslen = 0; 1176 /* Clear certificate validity flags */ 1177 for (i = 0; i < SSL_PKEY_NUM; i++) 1178 s->s3->tmp.valid_flags[i] = 0; 1179 /* 1180 * If peer sent no signature algorithms check to see if we support 1181 * the default algorithm for each certificate type 1182 */ 1183 if (s->s3->tmp.peer_cert_sigalgs == NULL 1184 && s->s3->tmp.peer_sigalgs == NULL) { 1185 const uint16_t *sent_sigs; 1186 size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); 1187 1188 for (i = 0; i < SSL_PKEY_NUM; i++) { 1189 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); 1190 size_t j; 1191 1192 if (lu == NULL) 1193 continue; 1194 /* Check default matches a type we sent */ 1195 for (j = 0; j < sent_sigslen; j++) { 1196 if (lu->sigalg == sent_sigs[j]) { 1197 s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN; 1198 break; 1199 } 1200 } 1201 } 1202 return 1; 1203 } 1204 1205 if (!tls1_process_sigalgs(s)) { 1206 SSLfatal(s, SSL_AD_INTERNAL_ERROR, 1207 SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR); 1208 return 0; 1209 } 1210 if (s->cert->shared_sigalgs != NULL) 1211 return 1; 1212 1213 /* Fatal error if no shared signature algorithms */ 1214 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS, 1215 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); 1216 return 0; 1217 } 1218 1219 /*- 1220 * Gets the ticket information supplied by the client if any. 1221 * 1222 * hello: The parsed ClientHello data 1223 * ret: (output) on return, if a ticket was decrypted, then this is set to 1224 * point to the resulting session. 1225 */ 1226 SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, 1227 SSL_SESSION **ret) 1228 { 1229 size_t size; 1230 RAW_EXTENSION *ticketext; 1231 1232 *ret = NULL; 1233 s->ext.ticket_expected = 0; 1234 1235 /* 1236 * If tickets disabled or not supported by the protocol version 1237 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful 1238 * resumption. 1239 */ 1240 if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) 1241 return SSL_TICKET_NONE; 1242 1243 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; 1244 if (!ticketext->present) 1245 return SSL_TICKET_NONE; 1246 1247 size = PACKET_remaining(&ticketext->data); 1248 1249 return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, 1250 hello->session_id, hello->session_id_len, ret); 1251 } 1252 1253 /*- 1254 * tls_decrypt_ticket attempts to decrypt a session ticket. 1255 * 1256 * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are 1257 * expecting a pre-shared key ciphersuite, in which case we have no use for 1258 * session tickets and one will never be decrypted, nor will 1259 * s->ext.ticket_expected be set to 1. 1260 * 1261 * Side effects: 1262 * Sets s->ext.ticket_expected to 1 if the server will have to issue 1263 * a new session ticket to the client because the client indicated support 1264 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have 1265 * a session ticket or we couldn't use the one it gave us, or if 1266 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. 1267 * Otherwise, s->ext.ticket_expected is set to 0. 1268 * 1269 * etick: points to the body of the session ticket extension. 1270 * eticklen: the length of the session tickets extension. 1271 * sess_id: points at the session ID. 1272 * sesslen: the length of the session ID. 1273 * psess: (output) on return, if a ticket was decrypted, then this is set to 1274 * point to the resulting session. 1275 */ 1276 SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick, 1277 size_t eticklen, const unsigned char *sess_id, 1278 size_t sesslen, SSL_SESSION **psess) 1279 { 1280 SSL_SESSION *sess = NULL; 1281 unsigned char *sdec; 1282 const unsigned char *p; 1283 int slen, renew_ticket = 0, declen; 1284 SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER; 1285 size_t mlen; 1286 unsigned char tick_hmac[EVP_MAX_MD_SIZE]; 1287 HMAC_CTX *hctx = NULL; 1288 EVP_CIPHER_CTX *ctx = NULL; 1289 SSL_CTX *tctx = s->session_ctx; 1290 1291 if (eticklen == 0) { 1292 /* 1293 * The client will accept a ticket but doesn't currently have 1294 * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3 1295 */ 1296 ret = SSL_TICKET_EMPTY; 1297 goto end; 1298 } 1299 if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) { 1300 /* 1301 * Indicate that the ticket couldn't be decrypted rather than 1302 * generating the session from ticket now, trigger 1303 * abbreviated handshake based on external mechanism to 1304 * calculate the master secret later. 1305 */ 1306 ret = SSL_TICKET_NO_DECRYPT; 1307 goto end; 1308 } 1309 1310 /* Need at least keyname + iv */ 1311 if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { 1312 ret = SSL_TICKET_NO_DECRYPT; 1313 goto end; 1314 } 1315 1316 /* Initialize session ticket encryption and HMAC contexts */ 1317 hctx = HMAC_CTX_new(); 1318 if (hctx == NULL) { 1319 ret = SSL_TICKET_FATAL_ERR_MALLOC; 1320 goto end; 1321 } 1322 ctx = EVP_CIPHER_CTX_new(); 1323 if (ctx == NULL) { 1324 ret = SSL_TICKET_FATAL_ERR_MALLOC; 1325 goto end; 1326 } 1327 if (tctx->ext.ticket_key_cb) { 1328 unsigned char *nctick = (unsigned char *)etick; 1329 int rv = tctx->ext.ticket_key_cb(s, nctick, 1330 nctick + TLSEXT_KEYNAME_LENGTH, 1331 ctx, hctx, 0); 1332 if (rv < 0) { 1333 ret = SSL_TICKET_FATAL_ERR_OTHER; 1334 goto end; 1335 } 1336 if (rv == 0) { 1337 ret = SSL_TICKET_NO_DECRYPT; 1338 goto end; 1339 } 1340 if (rv == 2) 1341 renew_ticket = 1; 1342 } else { 1343 /* Check key name matches */ 1344 if (memcmp(etick, tctx->ext.tick_key_name, 1345 TLSEXT_KEYNAME_LENGTH) != 0) { 1346 ret = SSL_TICKET_NO_DECRYPT; 1347 goto end; 1348 } 1349 if (HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key, 1350 sizeof(tctx->ext.secure->tick_hmac_key), 1351 EVP_sha256(), NULL) <= 0 1352 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, 1353 tctx->ext.secure->tick_aes_key, 1354 etick + TLSEXT_KEYNAME_LENGTH) <= 0) { 1355 ret = SSL_TICKET_FATAL_ERR_OTHER; 1356 goto end; 1357 } 1358 if (SSL_IS_TLS13(s)) 1359 renew_ticket = 1; 1360 } 1361 /* 1362 * Attempt to process session ticket, first conduct sanity and integrity 1363 * checks on ticket. 1364 */ 1365 mlen = HMAC_size(hctx); 1366 if (mlen == 0) { 1367 ret = SSL_TICKET_FATAL_ERR_OTHER; 1368 goto end; 1369 } 1370 1371 /* Sanity check ticket length: must exceed keyname + IV + HMAC */ 1372 if (eticklen <= 1373 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { 1374 ret = SSL_TICKET_NO_DECRYPT; 1375 goto end; 1376 } 1377 eticklen -= mlen; 1378 /* Check HMAC of encrypted ticket */ 1379 if (HMAC_Update(hctx, etick, eticklen) <= 0 1380 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { 1381 ret = SSL_TICKET_FATAL_ERR_OTHER; 1382 goto end; 1383 } 1384 1385 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { 1386 ret = SSL_TICKET_NO_DECRYPT; 1387 goto end; 1388 } 1389 /* Attempt to decrypt session data */ 1390 /* Move p after IV to start of encrypted ticket, update length */ 1391 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); 1392 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); 1393 sdec = OPENSSL_malloc(eticklen); 1394 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, 1395 (int)eticklen) <= 0) { 1396 OPENSSL_free(sdec); 1397 ret = SSL_TICKET_FATAL_ERR_OTHER; 1398 goto end; 1399 } 1400 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { 1401 OPENSSL_free(sdec); 1402 ret = SSL_TICKET_NO_DECRYPT; 1403 goto end; 1404 } 1405 slen += declen; 1406 p = sdec; 1407 1408 sess = d2i_SSL_SESSION(NULL, &p, slen); 1409 slen -= p - sdec; 1410 OPENSSL_free(sdec); 1411 if (sess) { 1412 /* Some additional consistency checks */ 1413 if (slen != 0) { 1414 SSL_SESSION_free(sess); 1415 sess = NULL; 1416 ret = SSL_TICKET_NO_DECRYPT; 1417 goto end; 1418 } 1419 /* 1420 * The session ID, if non-empty, is used by some clients to detect 1421 * that the ticket has been accepted. So we copy it to the session 1422 * structure. If it is empty set length to zero as required by 1423 * standard. 1424 */ 1425 if (sesslen) { 1426 memcpy(sess->session_id, sess_id, sesslen); 1427 sess->session_id_length = sesslen; 1428 } 1429 if (renew_ticket) 1430 ret = SSL_TICKET_SUCCESS_RENEW; 1431 else 1432 ret = SSL_TICKET_SUCCESS; 1433 goto end; 1434 } 1435 ERR_clear_error(); 1436 /* 1437 * For session parse failure, indicate that we need to send a new ticket. 1438 */ 1439 ret = SSL_TICKET_NO_DECRYPT; 1440 1441 end: 1442 EVP_CIPHER_CTX_free(ctx); 1443 HMAC_CTX_free(hctx); 1444 1445 /* 1446 * If set, the decrypt_ticket_cb() is called unless a fatal error was 1447 * detected above. The callback is responsible for checking |ret| before it 1448 * performs any action 1449 */ 1450 if (s->session_ctx->decrypt_ticket_cb != NULL 1451 && (ret == SSL_TICKET_EMPTY 1452 || ret == SSL_TICKET_NO_DECRYPT 1453 || ret == SSL_TICKET_SUCCESS 1454 || ret == SSL_TICKET_SUCCESS_RENEW)) { 1455 size_t keyname_len = eticklen; 1456 int retcb; 1457 1458 if (keyname_len > TLSEXT_KEYNAME_LENGTH) 1459 keyname_len = TLSEXT_KEYNAME_LENGTH; 1460 retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len, 1461 ret, 1462 s->session_ctx->ticket_cb_data); 1463 switch (retcb) { 1464 case SSL_TICKET_RETURN_ABORT: 1465 ret = SSL_TICKET_FATAL_ERR_OTHER; 1466 break; 1467 1468 case SSL_TICKET_RETURN_IGNORE: 1469 ret = SSL_TICKET_NONE; 1470 SSL_SESSION_free(sess); 1471 sess = NULL; 1472 break; 1473 1474 case SSL_TICKET_RETURN_IGNORE_RENEW: 1475 if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT) 1476 ret = SSL_TICKET_NO_DECRYPT; 1477 /* else the value of |ret| will already do the right thing */ 1478 SSL_SESSION_free(sess); 1479 sess = NULL; 1480 break; 1481 1482 case SSL_TICKET_RETURN_USE: 1483 case SSL_TICKET_RETURN_USE_RENEW: 1484 if (ret != SSL_TICKET_SUCCESS 1485 && ret != SSL_TICKET_SUCCESS_RENEW) 1486 ret = SSL_TICKET_FATAL_ERR_OTHER; 1487 else if (retcb == SSL_TICKET_RETURN_USE) 1488 ret = SSL_TICKET_SUCCESS; 1489 else 1490 ret = SSL_TICKET_SUCCESS_RENEW; 1491 break; 1492 1493 default: 1494 ret = SSL_TICKET_FATAL_ERR_OTHER; 1495 } 1496 } 1497 1498 if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) { 1499 switch (ret) { 1500 case SSL_TICKET_NO_DECRYPT: 1501 case SSL_TICKET_SUCCESS_RENEW: 1502 case SSL_TICKET_EMPTY: 1503 s->ext.ticket_expected = 1; 1504 } 1505 } 1506 1507 *psess = sess; 1508 1509 return ret; 1510 } 1511 1512 /* Check to see if a signature algorithm is allowed */ 1513 static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu) 1514 { 1515 unsigned char sigalgstr[2]; 1516 int secbits; 1517 1518 /* See if sigalgs is recognised and if hash is enabled */ 1519 if (!tls1_lookup_md(lu, NULL)) 1520 return 0; 1521 /* DSA is not allowed in TLS 1.3 */ 1522 if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) 1523 return 0; 1524 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */ 1525 if (!s->server && !SSL_IS_DTLS(s) && s->s3->tmp.min_ver >= TLS1_3_VERSION 1526 && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX 1527 || lu->hash_idx == SSL_MD_MD5_IDX 1528 || lu->hash_idx == SSL_MD_SHA224_IDX)) 1529 return 0; 1530 1531 /* See if public key algorithm allowed */ 1532 if (ssl_cert_is_disabled(lu->sig_idx)) 1533 return 0; 1534 1535 if (lu->sig == NID_id_GostR3410_2012_256 1536 || lu->sig == NID_id_GostR3410_2012_512 1537 || lu->sig == NID_id_GostR3410_2001) { 1538 /* We never allow GOST sig algs on the server with TLSv1.3 */ 1539 if (s->server && SSL_IS_TLS13(s)) 1540 return 0; 1541 if (!s->server 1542 && s->method->version == TLS_ANY_VERSION 1543 && s->s3->tmp.max_ver >= TLS1_3_VERSION) { 1544 int i, num; 1545 STACK_OF(SSL_CIPHER) *sk; 1546 1547 /* 1548 * We're a client that could negotiate TLSv1.3. We only allow GOST 1549 * sig algs if we could negotiate TLSv1.2 or below and we have GOST 1550 * ciphersuites enabled. 1551 */ 1552 1553 if (s->s3->tmp.min_ver >= TLS1_3_VERSION) 1554 return 0; 1555 1556 sk = SSL_get_ciphers(s); 1557 num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0; 1558 for (i = 0; i < num; i++) { 1559 const SSL_CIPHER *c; 1560 1561 c = sk_SSL_CIPHER_value(sk, i); 1562 /* Skip disabled ciphers */ 1563 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) 1564 continue; 1565 1566 if ((c->algorithm_mkey & SSL_kGOST) != 0) 1567 break; 1568 } 1569 if (i == num) 1570 return 0; 1571 } 1572 } 1573 1574 if (lu->hash == NID_undef) 1575 return 1; 1576 /* Security bits: half digest bits */ 1577 secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4; 1578 /* Finally see if security callback allows it */ 1579 sigalgstr[0] = (lu->sigalg >> 8) & 0xff; 1580 sigalgstr[1] = lu->sigalg & 0xff; 1581 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); 1582 } 1583 1584 /* 1585 * Get a mask of disabled public key algorithms based on supported signature 1586 * algorithms. For example if no signature algorithm supports RSA then RSA is 1587 * disabled. 1588 */ 1589 1590 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) 1591 { 1592 const uint16_t *sigalgs; 1593 size_t i, sigalgslen; 1594 uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA; 1595 /* 1596 * Go through all signature algorithms seeing if we support any 1597 * in disabled_mask. 1598 */ 1599 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); 1600 for (i = 0; i < sigalgslen; i++, sigalgs++) { 1601 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs); 1602 const SSL_CERT_LOOKUP *clu; 1603 1604 if (lu == NULL) 1605 continue; 1606 1607 clu = ssl_cert_lookup_by_idx(lu->sig_idx); 1608 if (clu == NULL) 1609 continue; 1610 1611 /* If algorithm is disabled see if we can enable it */ 1612 if ((clu->amask & disabled_mask) != 0 1613 && tls12_sigalg_allowed(s, op, lu)) 1614 disabled_mask &= ~clu->amask; 1615 } 1616 *pmask_a |= disabled_mask; 1617 } 1618 1619 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, 1620 const uint16_t *psig, size_t psiglen) 1621 { 1622 size_t i; 1623 int rv = 0; 1624 1625 for (i = 0; i < psiglen; i++, psig++) { 1626 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig); 1627 1628 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) 1629 continue; 1630 if (!WPACKET_put_bytes_u16(pkt, *psig)) 1631 return 0; 1632 /* 1633 * If TLS 1.3 must have at least one valid TLS 1.3 message 1634 * signing algorithm: i.e. neither RSA nor SHA1/SHA224 1635 */ 1636 if (rv == 0 && (!SSL_IS_TLS13(s) 1637 || (lu->sig != EVP_PKEY_RSA 1638 && lu->hash != NID_sha1 1639 && lu->hash != NID_sha224))) 1640 rv = 1; 1641 } 1642 if (rv == 0) 1643 SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); 1644 return rv; 1645 } 1646 1647 /* Given preference and allowed sigalgs set shared sigalgs */ 1648 static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig, 1649 const uint16_t *pref, size_t preflen, 1650 const uint16_t *allow, size_t allowlen) 1651 { 1652 const uint16_t *ptmp, *atmp; 1653 size_t i, j, nmatch = 0; 1654 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { 1655 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp); 1656 1657 /* Skip disabled hashes or signature algorithms */ 1658 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) 1659 continue; 1660 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { 1661 if (*ptmp == *atmp) { 1662 nmatch++; 1663 if (shsig) 1664 *shsig++ = lu; 1665 break; 1666 } 1667 } 1668 } 1669 return nmatch; 1670 } 1671 1672 /* Set shared signature algorithms for SSL structures */ 1673 static int tls1_set_shared_sigalgs(SSL *s) 1674 { 1675 const uint16_t *pref, *allow, *conf; 1676 size_t preflen, allowlen, conflen; 1677 size_t nmatch; 1678 const SIGALG_LOOKUP **salgs = NULL; 1679 CERT *c = s->cert; 1680 unsigned int is_suiteb = tls1_suiteb(s); 1681 1682 OPENSSL_free(c->shared_sigalgs); 1683 c->shared_sigalgs = NULL; 1684 c->shared_sigalgslen = 0; 1685 /* If client use client signature algorithms if not NULL */ 1686 if (!s->server && c->client_sigalgs && !is_suiteb) { 1687 conf = c->client_sigalgs; 1688 conflen = c->client_sigalgslen; 1689 } else if (c->conf_sigalgs && !is_suiteb) { 1690 conf = c->conf_sigalgs; 1691 conflen = c->conf_sigalgslen; 1692 } else 1693 conflen = tls12_get_psigalgs(s, 0, &conf); 1694 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { 1695 pref = conf; 1696 preflen = conflen; 1697 allow = s->s3->tmp.peer_sigalgs; 1698 allowlen = s->s3->tmp.peer_sigalgslen; 1699 } else { 1700 allow = conf; 1701 allowlen = conflen; 1702 pref = s->s3->tmp.peer_sigalgs; 1703 preflen = s->s3->tmp.peer_sigalgslen; 1704 } 1705 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); 1706 if (nmatch) { 1707 if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) { 1708 SSLerr(SSL_F_TLS1_SET_SHARED_SIGALGS, ERR_R_MALLOC_FAILURE); 1709 return 0; 1710 } 1711 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); 1712 } else { 1713 salgs = NULL; 1714 } 1715 c->shared_sigalgs = salgs; 1716 c->shared_sigalgslen = nmatch; 1717 return 1; 1718 } 1719 1720 int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen) 1721 { 1722 unsigned int stmp; 1723 size_t size, i; 1724 uint16_t *buf; 1725 1726 size = PACKET_remaining(pkt); 1727 1728 /* Invalid data length */ 1729 if (size == 0 || (size & 1) != 0) 1730 return 0; 1731 1732 size >>= 1; 1733 1734 if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) { 1735 SSLerr(SSL_F_TLS1_SAVE_U16, ERR_R_MALLOC_FAILURE); 1736 return 0; 1737 } 1738 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) 1739 buf[i] = stmp; 1740 1741 if (i != size) { 1742 OPENSSL_free(buf); 1743 return 0; 1744 } 1745 1746 OPENSSL_free(*pdest); 1747 *pdest = buf; 1748 *pdestlen = size; 1749 1750 return 1; 1751 } 1752 1753 int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert) 1754 { 1755 /* Extension ignored for inappropriate versions */ 1756 if (!SSL_USE_SIGALGS(s)) 1757 return 1; 1758 /* Should never happen */ 1759 if (s->cert == NULL) 1760 return 0; 1761 1762 if (cert) 1763 return tls1_save_u16(pkt, &s->s3->tmp.peer_cert_sigalgs, 1764 &s->s3->tmp.peer_cert_sigalgslen); 1765 else 1766 return tls1_save_u16(pkt, &s->s3->tmp.peer_sigalgs, 1767 &s->s3->tmp.peer_sigalgslen); 1768 1769 } 1770 1771 /* Set preferred digest for each key type */ 1772 1773 int tls1_process_sigalgs(SSL *s) 1774 { 1775 size_t i; 1776 uint32_t *pvalid = s->s3->tmp.valid_flags; 1777 CERT *c = s->cert; 1778 1779 if (!tls1_set_shared_sigalgs(s)) 1780 return 0; 1781 1782 for (i = 0; i < SSL_PKEY_NUM; i++) 1783 pvalid[i] = 0; 1784 1785 for (i = 0; i < c->shared_sigalgslen; i++) { 1786 const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i]; 1787 int idx = sigptr->sig_idx; 1788 1789 /* Ignore PKCS1 based sig algs in TLSv1.3 */ 1790 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) 1791 continue; 1792 /* If not disabled indicate we can explicitly sign */ 1793 if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx)) 1794 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; 1795 } 1796 return 1; 1797 } 1798 1799 int SSL_get_sigalgs(SSL *s, int idx, 1800 int *psign, int *phash, int *psignhash, 1801 unsigned char *rsig, unsigned char *rhash) 1802 { 1803 uint16_t *psig = s->s3->tmp.peer_sigalgs; 1804 size_t numsigalgs = s->s3->tmp.peer_sigalgslen; 1805 if (psig == NULL || numsigalgs > INT_MAX) 1806 return 0; 1807 if (idx >= 0) { 1808 const SIGALG_LOOKUP *lu; 1809 1810 if (idx >= (int)numsigalgs) 1811 return 0; 1812 psig += idx; 1813 if (rhash != NULL) 1814 *rhash = (unsigned char)((*psig >> 8) & 0xff); 1815 if (rsig != NULL) 1816 *rsig = (unsigned char)(*psig & 0xff); 1817 lu = tls1_lookup_sigalg(*psig); 1818 if (psign != NULL) 1819 *psign = lu != NULL ? lu->sig : NID_undef; 1820 if (phash != NULL) 1821 *phash = lu != NULL ? lu->hash : NID_undef; 1822 if (psignhash != NULL) 1823 *psignhash = lu != NULL ? lu->sigandhash : NID_undef; 1824 } 1825 return (int)numsigalgs; 1826 } 1827 1828 int SSL_get_shared_sigalgs(SSL *s, int idx, 1829 int *psign, int *phash, int *psignhash, 1830 unsigned char *rsig, unsigned char *rhash) 1831 { 1832 const SIGALG_LOOKUP *shsigalgs; 1833 if (s->cert->shared_sigalgs == NULL 1834 || idx < 0 1835 || idx >= (int)s->cert->shared_sigalgslen 1836 || s->cert->shared_sigalgslen > INT_MAX) 1837 return 0; 1838 shsigalgs = s->cert->shared_sigalgs[idx]; 1839 if (phash != NULL) 1840 *phash = shsigalgs->hash; 1841 if (psign != NULL) 1842 *psign = shsigalgs->sig; 1843 if (psignhash != NULL) 1844 *psignhash = shsigalgs->sigandhash; 1845 if (rsig != NULL) 1846 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); 1847 if (rhash != NULL) 1848 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); 1849 return (int)s->cert->shared_sigalgslen; 1850 } 1851 1852 /* Maximum possible number of unique entries in sigalgs array */ 1853 #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) 1854 1855 typedef struct { 1856 size_t sigalgcnt; 1857 /* TLSEXT_SIGALG_XXX values */ 1858 uint16_t sigalgs[TLS_MAX_SIGALGCNT]; 1859 } sig_cb_st; 1860 1861 static void get_sigorhash(int *psig, int *phash, const char *str) 1862 { 1863 if (strcmp(str, "RSA") == 0) { 1864 *psig = EVP_PKEY_RSA; 1865 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { 1866 *psig = EVP_PKEY_RSA_PSS; 1867 } else if (strcmp(str, "DSA") == 0) { 1868 *psig = EVP_PKEY_DSA; 1869 } else if (strcmp(str, "ECDSA") == 0) { 1870 *psig = EVP_PKEY_EC; 1871 } else { 1872 *phash = OBJ_sn2nid(str); 1873 if (*phash == NID_undef) 1874 *phash = OBJ_ln2nid(str); 1875 } 1876 } 1877 /* Maximum length of a signature algorithm string component */ 1878 #define TLS_MAX_SIGSTRING_LEN 40 1879 1880 static int sig_cb(const char *elem, int len, void *arg) 1881 { 1882 sig_cb_st *sarg = arg; 1883 size_t i; 1884 const SIGALG_LOOKUP *s; 1885 char etmp[TLS_MAX_SIGSTRING_LEN], *p; 1886 int sig_alg = NID_undef, hash_alg = NID_undef; 1887 if (elem == NULL) 1888 return 0; 1889 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) 1890 return 0; 1891 if (len > (int)(sizeof(etmp) - 1)) 1892 return 0; 1893 memcpy(etmp, elem, len); 1894 etmp[len] = 0; 1895 p = strchr(etmp, '+'); 1896 /* 1897 * We only allow SignatureSchemes listed in the sigalg_lookup_tbl; 1898 * if there's no '+' in the provided name, look for the new-style combined 1899 * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP. 1900 * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and 1901 * rsa_pss_rsae_* that differ only by public key OID; in such cases 1902 * we will pick the _rsae_ variant, by virtue of them appearing earlier 1903 * in the table. 1904 */ 1905 if (p == NULL) { 1906 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); 1907 i++, s++) { 1908 if (s->name != NULL && strcmp(etmp, s->name) == 0) { 1909 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; 1910 break; 1911 } 1912 } 1913 if (i == OSSL_NELEM(sigalg_lookup_tbl)) 1914 return 0; 1915 } else { 1916 *p = 0; 1917 p++; 1918 if (*p == 0) 1919 return 0; 1920 get_sigorhash(&sig_alg, &hash_alg, etmp); 1921 get_sigorhash(&sig_alg, &hash_alg, p); 1922 if (sig_alg == NID_undef || hash_alg == NID_undef) 1923 return 0; 1924 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); 1925 i++, s++) { 1926 if (s->hash == hash_alg && s->sig == sig_alg) { 1927 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; 1928 break; 1929 } 1930 } 1931 if (i == OSSL_NELEM(sigalg_lookup_tbl)) 1932 return 0; 1933 } 1934 1935 /* Reject duplicates */ 1936 for (i = 0; i < sarg->sigalgcnt - 1; i++) { 1937 if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { 1938 sarg->sigalgcnt--; 1939 return 0; 1940 } 1941 } 1942 return 1; 1943 } 1944 1945 /* 1946 * Set supported signature algorithms based on a colon separated list of the 1947 * form sig+hash e.g. RSA+SHA512:DSA+SHA512 1948 */ 1949 int tls1_set_sigalgs_list(CERT *c, const char *str, int client) 1950 { 1951 sig_cb_st sig; 1952 sig.sigalgcnt = 0; 1953 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) 1954 return 0; 1955 if (c == NULL) 1956 return 1; 1957 return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); 1958 } 1959 1960 int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen, 1961 int client) 1962 { 1963 uint16_t *sigalgs; 1964 1965 if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) { 1966 SSLerr(SSL_F_TLS1_SET_RAW_SIGALGS, ERR_R_MALLOC_FAILURE); 1967 return 0; 1968 } 1969 memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs)); 1970 1971 if (client) { 1972 OPENSSL_free(c->client_sigalgs); 1973 c->client_sigalgs = sigalgs; 1974 c->client_sigalgslen = salglen; 1975 } else { 1976 OPENSSL_free(c->conf_sigalgs); 1977 c->conf_sigalgs = sigalgs; 1978 c->conf_sigalgslen = salglen; 1979 } 1980 1981 return 1; 1982 } 1983 1984 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) 1985 { 1986 uint16_t *sigalgs, *sptr; 1987 size_t i; 1988 1989 if (salglen & 1) 1990 return 0; 1991 if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) { 1992 SSLerr(SSL_F_TLS1_SET_SIGALGS, ERR_R_MALLOC_FAILURE); 1993 return 0; 1994 } 1995 for (i = 0, sptr = sigalgs; i < salglen; i += 2) { 1996 size_t j; 1997 const SIGALG_LOOKUP *curr; 1998 int md_id = *psig_nids++; 1999 int sig_id = *psig_nids++; 2000 2001 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); 2002 j++, curr++) { 2003 if (curr->hash == md_id && curr->sig == sig_id) { 2004 *sptr++ = curr->sigalg; 2005 break; 2006 } 2007 } 2008 2009 if (j == OSSL_NELEM(sigalg_lookup_tbl)) 2010 goto err; 2011 } 2012 2013 if (client) { 2014 OPENSSL_free(c->client_sigalgs); 2015 c->client_sigalgs = sigalgs; 2016 c->client_sigalgslen = salglen / 2; 2017 } else { 2018 OPENSSL_free(c->conf_sigalgs); 2019 c->conf_sigalgs = sigalgs; 2020 c->conf_sigalgslen = salglen / 2; 2021 } 2022 2023 return 1; 2024 2025 err: 2026 OPENSSL_free(sigalgs); 2027 return 0; 2028 } 2029 2030 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) 2031 { 2032 int sig_nid; 2033 size_t i; 2034 if (default_nid == -1) 2035 return 1; 2036 sig_nid = X509_get_signature_nid(x); 2037 if (default_nid) 2038 return sig_nid == default_nid ? 1 : 0; 2039 for (i = 0; i < c->shared_sigalgslen; i++) 2040 if (sig_nid == c->shared_sigalgs[i]->sigandhash) 2041 return 1; 2042 return 0; 2043 } 2044 2045 /* Check to see if a certificate issuer name matches list of CA names */ 2046 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) 2047 { 2048 X509_NAME *nm; 2049 int i; 2050 nm = X509_get_issuer_name(x); 2051 for (i = 0; i < sk_X509_NAME_num(names); i++) { 2052 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) 2053 return 1; 2054 } 2055 return 0; 2056 } 2057 2058 /* 2059 * Check certificate chain is consistent with TLS extensions and is usable by 2060 * server. This servers two purposes: it allows users to check chains before 2061 * passing them to the server and it allows the server to check chains before 2062 * attempting to use them. 2063 */ 2064 2065 /* Flags which need to be set for a certificate when strict mode not set */ 2066 2067 #define CERT_PKEY_VALID_FLAGS \ 2068 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) 2069 /* Strict mode flags */ 2070 #define CERT_PKEY_STRICT_FLAGS \ 2071 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ 2072 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) 2073 2074 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, 2075 int idx) 2076 { 2077 int i; 2078 int rv = 0; 2079 int check_flags = 0, strict_mode; 2080 CERT_PKEY *cpk = NULL; 2081 CERT *c = s->cert; 2082 uint32_t *pvalid; 2083 unsigned int suiteb_flags = tls1_suiteb(s); 2084 /* idx == -1 means checking server chains */ 2085 if (idx != -1) { 2086 /* idx == -2 means checking client certificate chains */ 2087 if (idx == -2) { 2088 cpk = c->key; 2089 idx = (int)(cpk - c->pkeys); 2090 } else 2091 cpk = c->pkeys + idx; 2092 pvalid = s->s3->tmp.valid_flags + idx; 2093 x = cpk->x509; 2094 pk = cpk->privatekey; 2095 chain = cpk->chain; 2096 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; 2097 /* If no cert or key, forget it */ 2098 if (!x || !pk) 2099 goto end; 2100 } else { 2101 size_t certidx; 2102 2103 if (!x || !pk) 2104 return 0; 2105 2106 if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL) 2107 return 0; 2108 idx = certidx; 2109 pvalid = s->s3->tmp.valid_flags + idx; 2110 2111 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) 2112 check_flags = CERT_PKEY_STRICT_FLAGS; 2113 else 2114 check_flags = CERT_PKEY_VALID_FLAGS; 2115 strict_mode = 1; 2116 } 2117 2118 if (suiteb_flags) { 2119 int ok; 2120 if (check_flags) 2121 check_flags |= CERT_PKEY_SUITEB; 2122 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); 2123 if (ok == X509_V_OK) 2124 rv |= CERT_PKEY_SUITEB; 2125 else if (!check_flags) 2126 goto end; 2127 } 2128 2129 /* 2130 * Check all signature algorithms are consistent with signature 2131 * algorithms extension if TLS 1.2 or later and strict mode. 2132 */ 2133 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { 2134 int default_nid; 2135 int rsign = 0; 2136 if (s->s3->tmp.peer_cert_sigalgs != NULL 2137 || s->s3->tmp.peer_sigalgs != NULL) { 2138 default_nid = 0; 2139 /* If no sigalgs extension use defaults from RFC5246 */ 2140 } else { 2141 switch (idx) { 2142 case SSL_PKEY_RSA: 2143 rsign = EVP_PKEY_RSA; 2144 default_nid = NID_sha1WithRSAEncryption; 2145 break; 2146 2147 case SSL_PKEY_DSA_SIGN: 2148 rsign = EVP_PKEY_DSA; 2149 default_nid = NID_dsaWithSHA1; 2150 break; 2151 2152 case SSL_PKEY_ECC: 2153 rsign = EVP_PKEY_EC; 2154 default_nid = NID_ecdsa_with_SHA1; 2155 break; 2156 2157 case SSL_PKEY_GOST01: 2158 rsign = NID_id_GostR3410_2001; 2159 default_nid = NID_id_GostR3411_94_with_GostR3410_2001; 2160 break; 2161 2162 case SSL_PKEY_GOST12_256: 2163 rsign = NID_id_GostR3410_2012_256; 2164 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; 2165 break; 2166 2167 case SSL_PKEY_GOST12_512: 2168 rsign = NID_id_GostR3410_2012_512; 2169 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; 2170 break; 2171 2172 default: 2173 default_nid = -1; 2174 break; 2175 } 2176 } 2177 /* 2178 * If peer sent no signature algorithms extension and we have set 2179 * preferred signature algorithms check we support sha1. 2180 */ 2181 if (default_nid > 0 && c->conf_sigalgs) { 2182 size_t j; 2183 const uint16_t *p = c->conf_sigalgs; 2184 for (j = 0; j < c->conf_sigalgslen; j++, p++) { 2185 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p); 2186 2187 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) 2188 break; 2189 } 2190 if (j == c->conf_sigalgslen) { 2191 if (check_flags) 2192 goto skip_sigs; 2193 else 2194 goto end; 2195 } 2196 } 2197 /* Check signature algorithm of each cert in chain */ 2198 if (!tls1_check_sig_alg(c, x, default_nid)) { 2199 if (!check_flags) 2200 goto end; 2201 } else 2202 rv |= CERT_PKEY_EE_SIGNATURE; 2203 rv |= CERT_PKEY_CA_SIGNATURE; 2204 for (i = 0; i < sk_X509_num(chain); i++) { 2205 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { 2206 if (check_flags) { 2207 rv &= ~CERT_PKEY_CA_SIGNATURE; 2208 break; 2209 } else 2210 goto end; 2211 } 2212 } 2213 } 2214 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ 2215 else if (check_flags) 2216 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; 2217 skip_sigs: 2218 /* Check cert parameters are consistent */ 2219 if (tls1_check_cert_param(s, x, 1)) 2220 rv |= CERT_PKEY_EE_PARAM; 2221 else if (!check_flags) 2222 goto end; 2223 if (!s->server) 2224 rv |= CERT_PKEY_CA_PARAM; 2225 /* In strict mode check rest of chain too */ 2226 else if (strict_mode) { 2227 rv |= CERT_PKEY_CA_PARAM; 2228 for (i = 0; i < sk_X509_num(chain); i++) { 2229 X509 *ca = sk_X509_value(chain, i); 2230 if (!tls1_check_cert_param(s, ca, 0)) { 2231 if (check_flags) { 2232 rv &= ~CERT_PKEY_CA_PARAM; 2233 break; 2234 } else 2235 goto end; 2236 } 2237 } 2238 } 2239 if (!s->server && strict_mode) { 2240 STACK_OF(X509_NAME) *ca_dn; 2241 int check_type = 0; 2242 switch (EVP_PKEY_id(pk)) { 2243 case EVP_PKEY_RSA: 2244 check_type = TLS_CT_RSA_SIGN; 2245 break; 2246 case EVP_PKEY_DSA: 2247 check_type = TLS_CT_DSS_SIGN; 2248 break; 2249 case EVP_PKEY_EC: 2250 check_type = TLS_CT_ECDSA_SIGN; 2251 break; 2252 } 2253 if (check_type) { 2254 const uint8_t *ctypes = s->s3->tmp.ctype; 2255 size_t j; 2256 2257 for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) { 2258 if (*ctypes == check_type) { 2259 rv |= CERT_PKEY_CERT_TYPE; 2260 break; 2261 } 2262 } 2263 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) 2264 goto end; 2265 } else { 2266 rv |= CERT_PKEY_CERT_TYPE; 2267 } 2268 2269 ca_dn = s->s3->tmp.peer_ca_names; 2270 2271 if (!sk_X509_NAME_num(ca_dn)) 2272 rv |= CERT_PKEY_ISSUER_NAME; 2273 2274 if (!(rv & CERT_PKEY_ISSUER_NAME)) { 2275 if (ssl_check_ca_name(ca_dn, x)) 2276 rv |= CERT_PKEY_ISSUER_NAME; 2277 } 2278 if (!(rv & CERT_PKEY_ISSUER_NAME)) { 2279 for (i = 0; i < sk_X509_num(chain); i++) { 2280 X509 *xtmp = sk_X509_value(chain, i); 2281 if (ssl_check_ca_name(ca_dn, xtmp)) { 2282 rv |= CERT_PKEY_ISSUER_NAME; 2283 break; 2284 } 2285 } 2286 } 2287 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) 2288 goto end; 2289 } else 2290 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; 2291 2292 if (!check_flags || (rv & check_flags) == check_flags) 2293 rv |= CERT_PKEY_VALID; 2294 2295 end: 2296 2297 if (TLS1_get_version(s) >= TLS1_2_VERSION) 2298 rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); 2299 else 2300 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; 2301 2302 /* 2303 * When checking a CERT_PKEY structure all flags are irrelevant if the 2304 * chain is invalid. 2305 */ 2306 if (!check_flags) { 2307 if (rv & CERT_PKEY_VALID) { 2308 *pvalid = rv; 2309 } else { 2310 /* Preserve sign and explicit sign flag, clear rest */ 2311 *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; 2312 return 0; 2313 } 2314 } 2315 return rv; 2316 } 2317 2318 /* Set validity of certificates in an SSL structure */ 2319 void tls1_set_cert_validity(SSL *s) 2320 { 2321 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); 2322 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN); 2323 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); 2324 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); 2325 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); 2326 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); 2327 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); 2328 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); 2329 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448); 2330 } 2331 2332 /* User level utility function to check a chain is suitable */ 2333 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) 2334 { 2335 return tls1_check_chain(s, x, pk, chain, -1); 2336 } 2337 2338 #ifndef OPENSSL_NO_DH 2339 DH *ssl_get_auto_dh(SSL *s) 2340 { 2341 int dh_secbits = 80; 2342 if (s->cert->dh_tmp_auto == 2) 2343 return DH_get_1024_160(); 2344 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { 2345 if (s->s3->tmp.new_cipher->strength_bits == 256) 2346 dh_secbits = 128; 2347 else 2348 dh_secbits = 80; 2349 } else { 2350 if (s->s3->tmp.cert == NULL) 2351 return NULL; 2352 dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey); 2353 } 2354 2355 if (dh_secbits >= 128) { 2356 DH *dhp = DH_new(); 2357 BIGNUM *p, *g; 2358 if (dhp == NULL) 2359 return NULL; 2360 g = BN_new(); 2361 if (g == NULL || !BN_set_word(g, 2)) { 2362 DH_free(dhp); 2363 BN_free(g); 2364 return NULL; 2365 } 2366 if (dh_secbits >= 192) 2367 p = BN_get_rfc3526_prime_8192(NULL); 2368 else 2369 p = BN_get_rfc3526_prime_3072(NULL); 2370 if (p == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { 2371 DH_free(dhp); 2372 BN_free(p); 2373 BN_free(g); 2374 return NULL; 2375 } 2376 return dhp; 2377 } 2378 if (dh_secbits >= 112) 2379 return DH_get_2048_224(); 2380 return DH_get_1024_160(); 2381 } 2382 #endif 2383 2384 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) 2385 { 2386 int secbits = -1; 2387 EVP_PKEY *pkey = X509_get0_pubkey(x); 2388 if (pkey) { 2389 /* 2390 * If no parameters this will return -1 and fail using the default 2391 * security callback for any non-zero security level. This will 2392 * reject keys which omit parameters but this only affects DSA and 2393 * omission of parameters is never (?) done in practice. 2394 */ 2395 secbits = EVP_PKEY_security_bits(pkey); 2396 } 2397 if (s) 2398 return ssl_security(s, op, secbits, 0, x); 2399 else 2400 return ssl_ctx_security(ctx, op, secbits, 0, x); 2401 } 2402 2403 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) 2404 { 2405 /* Lookup signature algorithm digest */ 2406 int secbits, nid, pknid; 2407 /* Don't check signature if self signed */ 2408 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) 2409 return 1; 2410 if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) 2411 secbits = -1; 2412 /* If digest NID not defined use signature NID */ 2413 if (nid == NID_undef) 2414 nid = pknid; 2415 if (s) 2416 return ssl_security(s, op, secbits, nid, x); 2417 else 2418 return ssl_ctx_security(ctx, op, secbits, nid, x); 2419 } 2420 2421 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) 2422 { 2423 if (vfy) 2424 vfy = SSL_SECOP_PEER; 2425 if (is_ee) { 2426 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) 2427 return SSL_R_EE_KEY_TOO_SMALL; 2428 } else { 2429 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) 2430 return SSL_R_CA_KEY_TOO_SMALL; 2431 } 2432 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) 2433 return SSL_R_CA_MD_TOO_WEAK; 2434 return 1; 2435 } 2436 2437 /* 2438 * Check security of a chain, if |sk| includes the end entity certificate then 2439 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending 2440 * one to the peer. Return values: 1 if ok otherwise error code to use 2441 */ 2442 2443 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) 2444 { 2445 int rv, start_idx, i; 2446 if (x == NULL) { 2447 x = sk_X509_value(sk, 0); 2448 start_idx = 1; 2449 } else 2450 start_idx = 0; 2451 2452 rv = ssl_security_cert(s, NULL, x, vfy, 1); 2453 if (rv != 1) 2454 return rv; 2455 2456 for (i = start_idx; i < sk_X509_num(sk); i++) { 2457 x = sk_X509_value(sk, i); 2458 rv = ssl_security_cert(s, NULL, x, vfy, 0); 2459 if (rv != 1) 2460 return rv; 2461 } 2462 return 1; 2463 } 2464 2465 /* 2466 * For TLS 1.2 servers check if we have a certificate which can be used 2467 * with the signature algorithm "lu" and return index of certificate. 2468 */ 2469 2470 static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu) 2471 { 2472 int sig_idx = lu->sig_idx; 2473 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx); 2474 2475 /* If not recognised or not supported by cipher mask it is not suitable */ 2476 if (clu == NULL 2477 || (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) == 0 2478 || (clu->nid == EVP_PKEY_RSA_PSS 2479 && (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0)) 2480 return -1; 2481 2482 return s->s3->tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1; 2483 } 2484 2485 /* 2486 * Returns true if |s| has a usable certificate configured for use 2487 * with signature scheme |sig|. 2488 * "Usable" includes a check for presence as well as applying 2489 * the signature_algorithm_cert restrictions sent by the peer (if any). 2490 * Returns false if no usable certificate is found. 2491 */ 2492 static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx) 2493 { 2494 const SIGALG_LOOKUP *lu; 2495 int mdnid, pknid; 2496 size_t i; 2497 2498 /* TLS 1.2 callers can override lu->sig_idx, but not TLS 1.3 callers. */ 2499 if (idx == -1) 2500 idx = sig->sig_idx; 2501 if (!ssl_has_cert(s, idx)) 2502 return 0; 2503 if (s->s3->tmp.peer_cert_sigalgs != NULL) { 2504 for (i = 0; i < s->s3->tmp.peer_cert_sigalgslen; i++) { 2505 lu = tls1_lookup_sigalg(s->s3->tmp.peer_cert_sigalgs[i]); 2506 if (lu == NULL 2507 || !X509_get_signature_info(s->cert->pkeys[idx].x509, &mdnid, 2508 &pknid, NULL, NULL)) 2509 continue; 2510 /* 2511 * TODO this does not differentiate between the 2512 * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not 2513 * have a chain here that lets us look at the key OID in the 2514 * signing certificate. 2515 */ 2516 if (mdnid == lu->hash && pknid == lu->sig) 2517 return 1; 2518 } 2519 return 0; 2520 } 2521 return 1; 2522 } 2523 2524 /* 2525 * Choose an appropriate signature algorithm based on available certificates 2526 * Sets chosen certificate and signature algorithm. 2527 * 2528 * For servers if we fail to find a required certificate it is a fatal error, 2529 * an appropriate error code is set and a TLS alert is sent. 2530 * 2531 * For clients fatalerrs is set to 0. If a certificate is not suitable it is not 2532 * a fatal error: we will either try another certificate or not present one 2533 * to the server. In this case no error is set. 2534 */ 2535 int tls_choose_sigalg(SSL *s, int fatalerrs) 2536 { 2537 const SIGALG_LOOKUP *lu = NULL; 2538 int sig_idx = -1; 2539 2540 s->s3->tmp.cert = NULL; 2541 s->s3->tmp.sigalg = NULL; 2542 2543 if (SSL_IS_TLS13(s)) { 2544 size_t i; 2545 #ifndef OPENSSL_NO_EC 2546 int curve = -1; 2547 #endif 2548 2549 /* Look for a certificate matching shared sigalgs */ 2550 for (i = 0; i < s->cert->shared_sigalgslen; i++) { 2551 lu = s->cert->shared_sigalgs[i]; 2552 sig_idx = -1; 2553 2554 /* Skip SHA1, SHA224, DSA and RSA if not PSS */ 2555 if (lu->hash == NID_sha1 2556 || lu->hash == NID_sha224 2557 || lu->sig == EVP_PKEY_DSA 2558 || lu->sig == EVP_PKEY_RSA) 2559 continue; 2560 /* Check that we have a cert, and signature_algorithms_cert */ 2561 if (!tls1_lookup_md(lu, NULL) || !has_usable_cert(s, lu, -1)) 2562 continue; 2563 if (lu->sig == EVP_PKEY_EC) { 2564 #ifndef OPENSSL_NO_EC 2565 if (curve == -1) { 2566 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey); 2567 2568 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); 2569 } 2570 if (lu->curve != NID_undef && curve != lu->curve) 2571 continue; 2572 #else 2573 continue; 2574 #endif 2575 } else if (lu->sig == EVP_PKEY_RSA_PSS) { 2576 /* validate that key is large enough for the signature algorithm */ 2577 EVP_PKEY *pkey; 2578 2579 pkey = s->cert->pkeys[lu->sig_idx].privatekey; 2580 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu)) 2581 continue; 2582 } 2583 break; 2584 } 2585 if (i == s->cert->shared_sigalgslen) { 2586 if (!fatalerrs) 2587 return 1; 2588 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG, 2589 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); 2590 return 0; 2591 } 2592 } else { 2593 /* If ciphersuite doesn't require a cert nothing to do */ 2594 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aCERT)) 2595 return 1; 2596 if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys)) 2597 return 1; 2598 2599 if (SSL_USE_SIGALGS(s)) { 2600 size_t i; 2601 if (s->s3->tmp.peer_sigalgs != NULL) { 2602 #ifndef OPENSSL_NO_EC 2603 int curve; 2604 2605 /* For Suite B need to match signature algorithm to curve */ 2606 if (tls1_suiteb(s)) { 2607 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey); 2608 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); 2609 } else { 2610 curve = -1; 2611 } 2612 #endif 2613 2614 /* 2615 * Find highest preference signature algorithm matching 2616 * cert type 2617 */ 2618 for (i = 0; i < s->cert->shared_sigalgslen; i++) { 2619 lu = s->cert->shared_sigalgs[i]; 2620 2621 if (s->server) { 2622 if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1) 2623 continue; 2624 } else { 2625 int cc_idx = s->cert->key - s->cert->pkeys; 2626 2627 sig_idx = lu->sig_idx; 2628 if (cc_idx != sig_idx) 2629 continue; 2630 } 2631 /* Check that we have a cert, and sig_algs_cert */ 2632 if (!has_usable_cert(s, lu, sig_idx)) 2633 continue; 2634 if (lu->sig == EVP_PKEY_RSA_PSS) { 2635 /* validate that key is large enough for the signature algorithm */ 2636 EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey; 2637 2638 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu)) 2639 continue; 2640 } 2641 #ifndef OPENSSL_NO_EC 2642 if (curve == -1 || lu->curve == curve) 2643 #endif 2644 break; 2645 } 2646 if (i == s->cert->shared_sigalgslen) { 2647 if (!fatalerrs) 2648 return 1; 2649 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 2650 SSL_F_TLS_CHOOSE_SIGALG, 2651 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); 2652 return 0; 2653 } 2654 } else { 2655 /* 2656 * If we have no sigalg use defaults 2657 */ 2658 const uint16_t *sent_sigs; 2659 size_t sent_sigslen; 2660 2661 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { 2662 if (!fatalerrs) 2663 return 1; 2664 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG, 2665 ERR_R_INTERNAL_ERROR); 2666 return 0; 2667 } 2668 2669 /* Check signature matches a type we sent */ 2670 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); 2671 for (i = 0; i < sent_sigslen; i++, sent_sigs++) { 2672 if (lu->sigalg == *sent_sigs 2673 && has_usable_cert(s, lu, lu->sig_idx)) 2674 break; 2675 } 2676 if (i == sent_sigslen) { 2677 if (!fatalerrs) 2678 return 1; 2679 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 2680 SSL_F_TLS_CHOOSE_SIGALG, 2681 SSL_R_WRONG_SIGNATURE_TYPE); 2682 return 0; 2683 } 2684 } 2685 } else { 2686 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { 2687 if (!fatalerrs) 2688 return 1; 2689 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG, 2690 ERR_R_INTERNAL_ERROR); 2691 return 0; 2692 } 2693 } 2694 } 2695 if (sig_idx == -1) 2696 sig_idx = lu->sig_idx; 2697 s->s3->tmp.cert = &s->cert->pkeys[sig_idx]; 2698 s->cert->key = s->s3->tmp.cert; 2699 s->s3->tmp.sigalg = lu; 2700 return 1; 2701 } 2702 2703 int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode) 2704 { 2705 if (mode != TLSEXT_max_fragment_length_DISABLED 2706 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { 2707 SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH, 2708 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); 2709 return 0; 2710 } 2711 2712 ctx->ext.max_fragment_len_mode = mode; 2713 return 1; 2714 } 2715 2716 int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode) 2717 { 2718 if (mode != TLSEXT_max_fragment_length_DISABLED 2719 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { 2720 SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH, 2721 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); 2722 return 0; 2723 } 2724 2725 ssl->ext.max_fragment_len_mode = mode; 2726 return 1; 2727 } 2728 2729 uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session) 2730 { 2731 return session->ext.max_fragment_len_mode; 2732 } 2733