2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
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"
22 #include <openssl/ct.h>
24 SSL3_ENC_METHOD const TLSv1_enc_data = {
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,
34 tls1_export_keying_material,
36 ssl3_set_handshake_header,
37 tls_close_construct_packet,
41 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
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,
51 tls1_export_keying_material,
52 SSL_ENC_FLAG_EXPLICIT_IV,
53 ssl3_set_handshake_header,
54 tls_close_construct_packet,
58 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
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,
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,
76 SSL3_ENC_METHOD const TLSv1_3_enc_data = {
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,
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,
93 long tls1_default_timeout(void)
96 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
97 * http, the cache would over fill
106 if (!s->method->ssl_clear(s))
112 void tls1_free(SSL *s)
114 OPENSSL_free(s->ext.session_ticket);
118 int tls1_clear(SSL *s)
123 if (s->method->version == TLS_ANY_VERSION)
124 s->version = TLS_MAX_VERSION;
126 s->version = s->method->version;
131 #ifndef OPENSSL_NO_EC
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.
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) */
171 static const unsigned char ecformats_default[] = {
172 TLSEXT_ECPOINTFORMAT_uncompressed,
173 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
174 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
177 /* The default curves */
178 static const uint16_t eccurves_default[] = {
179 29, /* X25519 (29) */
180 23, /* secp256r1 (23) */
182 25, /* secp521r1 (25) */
183 24, /* secp384r1 (24) */
186 static const uint16_t suiteb_curves[] = {
191 const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)
193 /* ECC curves from RFC 4492 and RFC 7027 */
194 if (group_id < 1 || group_id > OSSL_NELEM(nid_list))
196 return &nid_list[group_id - 1];
199 static uint16_t tls1_nid2group_id(int nid)
202 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
203 if (nid_list[i].nid == nid)
204 return (uint16_t)(i + 1);
210 * Set *pgroups to the supported groups list and *pgroupslen to
211 * the number of groups supported.
213 void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
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);
224 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
225 *pgroups = suiteb_curves;
229 case SSL_CERT_FLAG_SUITEB_192_LOS:
230 *pgroups = suiteb_curves + 1;
235 if (s->ext.supportedgroups == NULL) {
236 *pgroups = eccurves_default;
237 *pgroupslen = OSSL_NELEM(eccurves_default);
239 *pgroups = s->ext.supportedgroups;
240 *pgroupslen = s->ext.supportedgroups_len;
246 /* See if curve is allowed by security callback */
247 int tls_curve_allowed(SSL *s, uint16_t curve, int op)
249 const TLS_GROUP_INFO *cinfo = tls1_group_id_lookup(curve);
250 unsigned char ctmp[2];
254 # ifdef OPENSSL_NO_EC2M
255 if (cinfo->flags & TLS_CURVE_CHAR2)
258 ctmp[0] = curve >> 8;
259 ctmp[1] = curve & 0xff;
260 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)ctmp);
263 /* Return 1 if "id" is in "list" */
264 static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
267 for (i = 0; i < listlen; i++)
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.
280 uint16_t tls1_shared_group(SSL *s, int nmatch)
282 const uint16_t *pref, *supp;
283 size_t num_pref, num_supp, i;
286 /* Can't do anything on client side */
290 if (tls1_suiteb(s)) {
292 * For Suite B ciphersuite determines curve: we already know
293 * these are acceptable due to previous checks.
295 unsigned long cid = s->s3->tmp.new_cipher->id;
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 */
304 /* If not Suite B just return first preference shared curve */
308 * If server preference set, our groups are the preference order
309 * otherwise peer decides.
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);
315 tls1_get_peer_groups(s, &pref, &num_pref);
316 tls1_get_supported_groups(s, &supp, &num_supp);
319 for (k = 0, i = 0; i < num_pref; i++) {
320 uint16_t id = pref[i];
322 if (!tls1_in_list(id, supp, num_supp)
323 || !tls_curve_allowed(s, id, SSL_SECOP_CURVE_SHARED))
331 /* Out of range (nmatch > k). */
335 int tls1_set_groups(uint16_t **pext, size_t *pextlen,
336 int *groups, size_t ngroups)
341 * Bitmap of groups included to detect duplicates: only works while group
344 unsigned long dup_list = 0;
347 SSLerr(SSL_F_TLS1_SET_GROUPS, SSL_R_BAD_LENGTH);
350 if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) {
351 SSLerr(SSL_F_TLS1_SET_GROUPS, ERR_R_MALLOC_FAILURE);
354 for (i = 0; i < ngroups; i++) {
355 unsigned long idmask;
357 /* TODO(TLS1.3): Convert for DH groups */
358 id = tls1_nid2group_id(groups[i]);
360 if (!id || (dup_list & idmask)) {
373 # define MAX_CURVELIST OSSL_NELEM(nid_list)
377 int nid_arr[MAX_CURVELIST];
380 static int nid_cb(const char *elem, int len, void *arg)
382 nid_cb_st *narg = arg;
388 if (narg->nidcnt == MAX_CURVELIST)
390 if (len > (int)(sizeof(etmp) - 1))
392 memcpy(etmp, elem, len);
394 nid = EC_curve_nist2nid(etmp);
395 if (nid == NID_undef)
396 nid = OBJ_sn2nid(etmp);
397 if (nid == NID_undef)
398 nid = OBJ_ln2nid(etmp);
399 if (nid == NID_undef)
401 for (i = 0; i < narg->nidcnt; i++)
402 if (narg->nid_arr[i] == nid)
404 narg->nid_arr[narg->nidcnt++] = nid;
408 /* Set groups based on a colon separate list */
409 int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str)
413 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
417 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
419 /* Return group id of a key */
420 static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
422 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
427 grp = EC_KEY_get0_group(ec);
428 return tls1_nid2group_id(EC_GROUP_get_curve_name(grp));
431 /* Check a key is compatible with compression extension */
432 static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
436 unsigned char comp_id;
439 /* If not an EC key nothing to check */
440 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
442 ec = EVP_PKEY_get0_EC_KEY(pkey);
443 grp = EC_KEY_get0_group(ec);
445 /* Get required compression id */
446 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
447 comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
448 } else if (SSL_IS_TLS13(s)) {
450 * ec_point_formats extension is not used in TLSv1.3 so we ignore
455 int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp));
457 if (field_type == NID_X9_62_prime_field)
458 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
459 else if (field_type == NID_X9_62_characteristic_two_field)
460 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
465 * If point formats extension present check it, otherwise everything is
466 * supported (see RFC4492).
468 if (s->session->ext.ecpointformats == NULL)
471 for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
472 if (s->session->ext.ecpointformats[i] == comp_id)
478 /* Check a group id matches preferences */
479 int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups)
481 const uint16_t *groups;
487 /* Check for Suite B compliance */
488 if (tls1_suiteb(s) && s->s3->tmp.new_cipher != NULL) {
489 unsigned long cid = s->s3->tmp.new_cipher->id;
491 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
492 if (group_id != TLSEXT_curve_P_256)
494 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
495 if (group_id != TLSEXT_curve_P_384)
498 /* Should never happen */
503 if (check_own_groups) {
504 /* Check group is one of our preferences */
505 tls1_get_supported_groups(s, &groups, &groups_len);
506 if (!tls1_in_list(group_id, groups, groups_len))
510 if (!tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
513 /* For clients, nothing more to check */
517 /* Check group is one of peers preferences */
518 tls1_get_peer_groups(s, &groups, &groups_len);
521 * RFC 4492 does not require the supported elliptic curves extension
522 * so if it is not sent we can just choose any curve.
523 * It is invalid to send an empty list in the supported groups
524 * extension, so groups_len == 0 always means no extension.
528 return tls1_in_list(group_id, groups, groups_len);
531 void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
535 * If we have a custom point format list use it otherwise use default
537 if (s->ext.ecpointformats) {
538 *pformats = s->ext.ecpointformats;
539 *num_formats = s->ext.ecpointformats_len;
541 *pformats = ecformats_default;
542 /* For Suite B we don't support char2 fields */
544 *num_formats = sizeof(ecformats_default) - 1;
546 *num_formats = sizeof(ecformats_default);
551 * Check cert parameters compatible with extensions: currently just checks EC
552 * certificates have compatible curves and compression.
554 static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
558 pkey = X509_get0_pubkey(x);
561 /* If not EC nothing to do */
562 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
564 /* Check compression */
565 if (!tls1_check_pkey_comp(s, pkey))
567 group_id = tls1_get_group_id(pkey);
569 * For a server we allow the certificate to not be in our list of supported
572 if (!tls1_check_group_id(s, group_id, !s->server))
575 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
578 if (check_ee_md && tls1_suiteb(s)) {
583 /* Check to see we have necessary signing algorithm */
584 if (group_id == TLSEXT_curve_P_256)
585 check_md = NID_ecdsa_with_SHA256;
586 else if (group_id == TLSEXT_curve_P_384)
587 check_md = NID_ecdsa_with_SHA384;
589 return 0; /* Should never happen */
590 for (i = 0; i < c->shared_sigalgslen; i++) {
591 if (check_md == c->shared_sigalgs[i]->sigandhash)
600 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
602 * @cid: Cipher ID we're considering using
604 * Checks that the kECDHE cipher suite we're considering using
605 * is compatible with the client extensions.
607 * Returns 0 when the cipher can't be used or 1 when it can.
609 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
611 /* If not Suite B just need a shared group */
613 return tls1_shared_group(s, 0) != 0;
615 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
618 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
619 return tls1_check_group_id(s, TLSEXT_curve_P_256, 1);
620 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
621 return tls1_check_group_id(s, TLSEXT_curve_P_384, 1);
628 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
633 #endif /* OPENSSL_NO_EC */
635 /* Default sigalg schemes */
636 static const uint16_t tls12_sigalgs[] = {
637 #ifndef OPENSSL_NO_EC
638 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
639 TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
640 TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
641 TLSEXT_SIGALG_ed25519,
645 TLSEXT_SIGALG_rsa_pss_pss_sha256,
646 TLSEXT_SIGALG_rsa_pss_pss_sha384,
647 TLSEXT_SIGALG_rsa_pss_pss_sha512,
648 TLSEXT_SIGALG_rsa_pss_rsae_sha256,
649 TLSEXT_SIGALG_rsa_pss_rsae_sha384,
650 TLSEXT_SIGALG_rsa_pss_rsae_sha512,
652 TLSEXT_SIGALG_rsa_pkcs1_sha256,
653 TLSEXT_SIGALG_rsa_pkcs1_sha384,
654 TLSEXT_SIGALG_rsa_pkcs1_sha512,
656 #ifndef OPENSSL_NO_EC
657 TLSEXT_SIGALG_ecdsa_sha224,
658 TLSEXT_SIGALG_ecdsa_sha1,
660 TLSEXT_SIGALG_rsa_pkcs1_sha224,
661 TLSEXT_SIGALG_rsa_pkcs1_sha1,
662 #ifndef OPENSSL_NO_DSA
663 TLSEXT_SIGALG_dsa_sha224,
664 TLSEXT_SIGALG_dsa_sha1,
666 TLSEXT_SIGALG_dsa_sha256,
667 TLSEXT_SIGALG_dsa_sha384,
668 TLSEXT_SIGALG_dsa_sha512,
670 #ifndef OPENSSL_NO_GOST
671 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
672 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
673 TLSEXT_SIGALG_gostr34102001_gostr3411,
677 #ifndef OPENSSL_NO_EC
678 static const uint16_t suiteb_sigalgs[] = {
679 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
680 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
684 static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
685 #ifndef OPENSSL_NO_EC
686 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
687 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
688 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
689 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
690 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
691 NID_ecdsa_with_SHA384, NID_secp384r1},
692 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
693 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
694 NID_ecdsa_with_SHA512, NID_secp521r1},
695 {"ed25519", TLSEXT_SIGALG_ed25519,
696 NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
697 NID_undef, NID_undef},
698 {"ed448", TLSEXT_SIGALG_ed448,
699 NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448,
700 NID_undef, NID_undef},
701 {NULL, TLSEXT_SIGALG_ecdsa_sha224,
702 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
703 NID_ecdsa_with_SHA224, NID_undef},
704 {NULL, TLSEXT_SIGALG_ecdsa_sha1,
705 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
706 NID_ecdsa_with_SHA1, NID_undef},
708 {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,
709 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
710 NID_undef, NID_undef},
711 {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,
712 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
713 NID_undef, NID_undef},
714 {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,
715 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
716 NID_undef, NID_undef},
717 {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256,
718 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
719 NID_undef, NID_undef},
720 {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384,
721 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
722 NID_undef, NID_undef},
723 {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512,
724 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
725 NID_undef, NID_undef},
726 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
727 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
728 NID_sha256WithRSAEncryption, NID_undef},
729 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
730 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
731 NID_sha384WithRSAEncryption, NID_undef},
732 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
733 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
734 NID_sha512WithRSAEncryption, NID_undef},
735 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
736 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
737 NID_sha224WithRSAEncryption, NID_undef},
738 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
739 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
740 NID_sha1WithRSAEncryption, NID_undef},
741 #ifndef OPENSSL_NO_DSA
742 {NULL, TLSEXT_SIGALG_dsa_sha256,
743 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
744 NID_dsa_with_SHA256, NID_undef},
745 {NULL, TLSEXT_SIGALG_dsa_sha384,
746 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
747 NID_undef, NID_undef},
748 {NULL, TLSEXT_SIGALG_dsa_sha512,
749 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
750 NID_undef, NID_undef},
751 {NULL, TLSEXT_SIGALG_dsa_sha224,
752 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
753 NID_undef, NID_undef},
754 {NULL, TLSEXT_SIGALG_dsa_sha1,
755 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
756 NID_dsaWithSHA1, NID_undef},
758 #ifndef OPENSSL_NO_GOST
759 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
760 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
761 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
762 NID_undef, NID_undef},
763 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
764 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
765 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
766 NID_undef, NID_undef},
767 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
768 NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
769 NID_id_GostR3410_2001, SSL_PKEY_GOST01,
770 NID_undef, NID_undef}
773 /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
774 static const SIGALG_LOOKUP legacy_rsa_sigalg = {
775 "rsa_pkcs1_md5_sha1", 0,
776 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
777 EVP_PKEY_RSA, SSL_PKEY_RSA,
782 * Default signature algorithm values used if signature algorithms not present.
783 * From RFC5246. Note: order must match certificate index order.
785 static const uint16_t tls_default_sigalg[] = {
786 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
787 0, /* SSL_PKEY_RSA_PSS_SIGN */
788 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
789 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
790 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
791 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
792 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */
793 0, /* SSL_PKEY_ED25519 */
794 0, /* SSL_PKEY_ED448 */
797 /* Lookup TLS signature algorithm */
798 static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
801 const SIGALG_LOOKUP *s;
803 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
805 if (s->sigalg == sigalg)
810 /* Lookup hash: return 0 if invalid or not enabled */
811 int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
816 /* lu->hash == NID_undef means no associated digest */
817 if (lu->hash == NID_undef) {
820 md = ssl_md(lu->hash_idx);
830 * Check if key is large enough to generate RSA-PSS signature.
832 * The key must greater than or equal to 2 * hash length + 2.
833 * SHA512 has a hash length of 64 bytes, which is incompatible
834 * with a 128 byte (1024 bit) key.
836 #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2)
837 static int rsa_pss_check_min_key_size(const RSA *rsa, const SIGALG_LOOKUP *lu)
843 if (!tls1_lookup_md(lu, &md) || md == NULL)
845 if (RSA_size(rsa) < RSA_PSS_MINIMUM_KEY_SIZE(md))
851 * Return a signature algorithm for TLS < 1.2 where the signature type
852 * is fixed by the certificate type.
854 static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
860 /* Work out index corresponding to ciphersuite */
861 for (i = 0; i < SSL_PKEY_NUM; i++) {
862 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
864 if (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) {
871 * Some GOST ciphersuites allow more than one signature algorithms
873 if (idx == SSL_PKEY_GOST01 && s->s3->tmp.new_cipher->algorithm_auth != SSL_aGOST01) {
876 for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01;
878 if (s->cert->pkeys[real_idx].privatekey != NULL) {
885 idx = s->cert->key - s->cert->pkeys;
888 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
890 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
891 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
893 if (!tls1_lookup_md(lu, NULL))
897 return &legacy_rsa_sigalg;
899 /* Set peer sigalg based key type */
900 int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
903 const SIGALG_LOOKUP *lu;
905 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
907 lu = tls1_get_legacy_sigalg(s, idx);
910 s->s3->tmp.peer_sigalg = lu;
914 size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
917 * If Suite B mode use Suite B sigalgs only, ignore any other
920 #ifndef OPENSSL_NO_EC
921 switch (tls1_suiteb(s)) {
922 case SSL_CERT_FLAG_SUITEB_128_LOS:
923 *psigs = suiteb_sigalgs;
924 return OSSL_NELEM(suiteb_sigalgs);
926 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
927 *psigs = suiteb_sigalgs;
930 case SSL_CERT_FLAG_SUITEB_192_LOS:
931 *psigs = suiteb_sigalgs + 1;
936 * We use client_sigalgs (if not NULL) if we're a server
937 * and sending a certificate request or if we're a client and
938 * determining which shared algorithm to use.
940 if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
941 *psigs = s->cert->client_sigalgs;
942 return s->cert->client_sigalgslen;
943 } else if (s->cert->conf_sigalgs) {
944 *psigs = s->cert->conf_sigalgs;
945 return s->cert->conf_sigalgslen;
947 *psigs = tls12_sigalgs;
948 return OSSL_NELEM(tls12_sigalgs);
953 * Check signature algorithm is consistent with sent supported signature
954 * algorithms and if so set relevant digest and signature scheme in
957 int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
959 const uint16_t *sent_sigs;
960 const EVP_MD *md = NULL;
962 size_t sent_sigslen, i, cidx;
963 int pkeyid = EVP_PKEY_id(pkey);
964 const SIGALG_LOOKUP *lu;
966 /* Should never happen */
969 if (SSL_IS_TLS13(s)) {
970 /* Disallow DSA for TLS 1.3 */
971 if (pkeyid == EVP_PKEY_DSA) {
972 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
973 SSL_R_WRONG_SIGNATURE_TYPE);
976 /* Only allow PSS for TLS 1.3 */
977 if (pkeyid == EVP_PKEY_RSA)
978 pkeyid = EVP_PKEY_RSA_PSS;
980 lu = tls1_lookup_sigalg(sig);
982 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
983 * is consistent with signature: RSA keys can be used for RSA-PSS
986 || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
987 || (pkeyid != lu->sig
988 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
989 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
990 SSL_R_WRONG_SIGNATURE_TYPE);
993 /* Check the sigalg is consistent with the key OID */
994 if (!ssl_cert_lookup_by_nid(EVP_PKEY_id(pkey), &cidx)
995 || lu->sig_idx != (int)cidx) {
996 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
997 SSL_R_WRONG_SIGNATURE_TYPE);
1001 #ifndef OPENSSL_NO_EC
1002 if (pkeyid == EVP_PKEY_EC) {
1004 /* Check point compression is permitted */
1005 if (!tls1_check_pkey_comp(s, pkey)) {
1006 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1007 SSL_F_TLS12_CHECK_PEER_SIGALG,
1008 SSL_R_ILLEGAL_POINT_COMPRESSION);
1012 /* For TLS 1.3 or Suite B check curve matches signature algorithm */
1013 if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
1014 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1015 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
1017 if (lu->curve != NID_undef && curve != lu->curve) {
1018 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1019 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1023 if (!SSL_IS_TLS13(s)) {
1024 /* Check curve matches extensions */
1025 if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) {
1026 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1027 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1030 if (tls1_suiteb(s)) {
1031 /* Check sigalg matches a permissible Suite B value */
1032 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
1033 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
1034 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1035 SSL_F_TLS12_CHECK_PEER_SIGALG,
1036 SSL_R_WRONG_SIGNATURE_TYPE);
1041 } else if (tls1_suiteb(s)) {
1042 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1043 SSL_R_WRONG_SIGNATURE_TYPE);
1048 /* Check signature matches a type we sent */
1049 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1050 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
1051 if (sig == *sent_sigs)
1054 /* Allow fallback to SHA1 if not strict mode */
1055 if (i == sent_sigslen && (lu->hash != NID_sha1
1056 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1057 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1058 SSL_R_WRONG_SIGNATURE_TYPE);
1061 if (!tls1_lookup_md(lu, &md)) {
1062 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1063 SSL_R_UNKNOWN_DIGEST);
1068 * Make sure security callback allows algorithm. For historical
1069 * reasons we have to pass the sigalg as a two byte char array.
1071 sigalgstr[0] = (sig >> 8) & 0xff;
1072 sigalgstr[1] = sig & 0xff;
1073 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1074 EVP_MD_size(md) * 4, EVP_MD_type(md),
1075 (void *)sigalgstr)) {
1076 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1077 SSL_R_WRONG_SIGNATURE_TYPE);
1081 /* Store the sigalg the peer uses */
1082 s->s3->tmp.peer_sigalg = lu;
1086 int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
1088 if (s->s3->tmp.peer_sigalg == NULL)
1090 *pnid = s->s3->tmp.peer_sigalg->sig;
1095 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1096 * supported, doesn't appear in supported signature algorithms, isn't supported
1097 * by the enabled protocol versions or by the security level.
1099 * This function should only be used for checking which ciphers are supported
1102 * Call ssl_cipher_disabled() to check that it's enabled or not.
1104 int ssl_set_client_disabled(SSL *s)
1106 s->s3->tmp.mask_a = 0;
1107 s->s3->tmp.mask_k = 0;
1108 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1109 if (ssl_get_min_max_version(s, &s->s3->tmp.min_ver,
1110 &s->s3->tmp.max_ver, NULL) != 0)
1112 #ifndef OPENSSL_NO_PSK
1113 /* with PSK there must be client callback set */
1114 if (!s->psk_client_callback) {
1115 s->s3->tmp.mask_a |= SSL_aPSK;
1116 s->s3->tmp.mask_k |= SSL_PSK;
1118 #endif /* OPENSSL_NO_PSK */
1119 #ifndef OPENSSL_NO_SRP
1120 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1121 s->s3->tmp.mask_a |= SSL_aSRP;
1122 s->s3->tmp.mask_k |= SSL_kSRP;
1129 * ssl_cipher_disabled - check that a cipher is disabled or not
1130 * @s: SSL connection that you want to use the cipher on
1131 * @c: cipher to check
1132 * @op: Security check that you want to do
1133 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
1135 * Returns 1 when it's disabled, 0 when enabled.
1137 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
1139 if (c->algorithm_mkey & s->s3->tmp.mask_k
1140 || c->algorithm_auth & s->s3->tmp.mask_a)
1142 if (s->s3->tmp.max_ver == 0)
1144 if (!SSL_IS_DTLS(s)) {
1145 int min_tls = c->min_tls;
1148 * For historical reasons we will allow ECHDE to be selected by a server
1149 * in SSLv3 if we are a client
1151 if (min_tls == TLS1_VERSION && ecdhe
1152 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
1153 min_tls = SSL3_VERSION;
1155 if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver))
1158 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
1159 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
1162 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1165 int tls_use_ticket(SSL *s)
1167 if ((s->options & SSL_OP_NO_TICKET))
1169 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1172 int tls1_set_server_sigalgs(SSL *s)
1176 /* Clear any shared signature algorithms */
1177 OPENSSL_free(s->cert->shared_sigalgs);
1178 s->cert->shared_sigalgs = NULL;
1179 s->cert->shared_sigalgslen = 0;
1180 /* Clear certificate validity flags */
1181 for (i = 0; i < SSL_PKEY_NUM; i++)
1182 s->s3->tmp.valid_flags[i] = 0;
1184 * If peer sent no signature algorithms check to see if we support
1185 * the default algorithm for each certificate type
1187 if (s->s3->tmp.peer_cert_sigalgs == NULL
1188 && s->s3->tmp.peer_sigalgs == NULL) {
1189 const uint16_t *sent_sigs;
1190 size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1192 for (i = 0; i < SSL_PKEY_NUM; i++) {
1193 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
1198 /* Check default matches a type we sent */
1199 for (j = 0; j < sent_sigslen; j++) {
1200 if (lu->sigalg == sent_sigs[j]) {
1201 s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN;
1209 if (!tls1_process_sigalgs(s)) {
1210 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1211 SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR);
1214 if (s->cert->shared_sigalgs != NULL)
1217 /* Fatal error if no shared signature algorithms */
1218 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS,
1219 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
1224 * Gets the ticket information supplied by the client if any.
1226 * hello: The parsed ClientHello data
1227 * ret: (output) on return, if a ticket was decrypted, then this is set to
1228 * point to the resulting session.
1230 SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
1234 RAW_EXTENSION *ticketext;
1237 s->ext.ticket_expected = 0;
1240 * If tickets disabled or not supported by the protocol version
1241 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
1244 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
1245 return SSL_TICKET_NONE;
1247 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
1248 if (!ticketext->present)
1249 return SSL_TICKET_NONE;
1251 size = PACKET_remaining(&ticketext->data);
1253 return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
1254 hello->session_id, hello->session_id_len, ret);
1258 * tls_decrypt_ticket attempts to decrypt a session ticket.
1260 * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are
1261 * expecting a pre-shared key ciphersuite, in which case we have no use for
1262 * session tickets and one will never be decrypted, nor will
1263 * s->ext.ticket_expected be set to 1.
1266 * Sets s->ext.ticket_expected to 1 if the server will have to issue
1267 * a new session ticket to the client because the client indicated support
1268 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1269 * a session ticket or we couldn't use the one it gave us, or if
1270 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1271 * Otherwise, s->ext.ticket_expected is set to 0.
1273 * etick: points to the body of the session ticket extension.
1274 * eticklen: the length of the session tickets extension.
1275 * sess_id: points at the session ID.
1276 * sesslen: the length of the session ID.
1277 * psess: (output) on return, if a ticket was decrypted, then this is set to
1278 * point to the resulting session.
1280 SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1281 size_t eticklen, const unsigned char *sess_id,
1282 size_t sesslen, SSL_SESSION **psess)
1284 SSL_SESSION *sess = NULL;
1285 unsigned char *sdec;
1286 const unsigned char *p;
1287 int slen, renew_ticket = 0, declen;
1288 SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;
1290 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1291 HMAC_CTX *hctx = NULL;
1292 EVP_CIPHER_CTX *ctx = NULL;
1293 SSL_CTX *tctx = s->session_ctx;
1295 if (eticklen == 0) {
1297 * The client will accept a ticket but doesn't currently have
1298 * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3
1300 ret = SSL_TICKET_EMPTY;
1303 if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) {
1305 * Indicate that the ticket couldn't be decrypted rather than
1306 * generating the session from ticket now, trigger
1307 * abbreviated handshake based on external mechanism to
1308 * calculate the master secret later.
1310 ret = SSL_TICKET_NO_DECRYPT;
1314 /* Need at least keyname + iv */
1315 if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) {
1316 ret = SSL_TICKET_NO_DECRYPT;
1320 /* Initialize session ticket encryption and HMAC contexts */
1321 hctx = HMAC_CTX_new();
1323 ret = SSL_TICKET_FATAL_ERR_MALLOC;
1326 ctx = EVP_CIPHER_CTX_new();
1328 ret = SSL_TICKET_FATAL_ERR_MALLOC;
1331 if (tctx->ext.ticket_key_cb) {
1332 unsigned char *nctick = (unsigned char *)etick;
1333 int rv = tctx->ext.ticket_key_cb(s, nctick,
1334 nctick + TLSEXT_KEYNAME_LENGTH,
1337 ret = SSL_TICKET_FATAL_ERR_OTHER;
1341 ret = SSL_TICKET_NO_DECRYPT;
1347 /* Check key name matches */
1348 if (memcmp(etick, tctx->ext.tick_key_name,
1349 TLSEXT_KEYNAME_LENGTH) != 0) {
1350 ret = SSL_TICKET_NO_DECRYPT;
1353 if (HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
1354 sizeof(tctx->ext.secure->tick_hmac_key),
1355 EVP_sha256(), NULL) <= 0
1356 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
1357 tctx->ext.secure->tick_aes_key,
1358 etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
1359 ret = SSL_TICKET_FATAL_ERR_OTHER;
1362 if (SSL_IS_TLS13(s))
1366 * Attempt to process session ticket, first conduct sanity and integrity
1369 mlen = HMAC_size(hctx);
1371 ret = SSL_TICKET_FATAL_ERR_OTHER;
1375 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1377 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
1378 ret = SSL_TICKET_NO_DECRYPT;
1382 /* Check HMAC of encrypted ticket */
1383 if (HMAC_Update(hctx, etick, eticklen) <= 0
1384 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
1385 ret = SSL_TICKET_FATAL_ERR_OTHER;
1389 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
1390 ret = SSL_TICKET_NO_DECRYPT;
1393 /* Attempt to decrypt session data */
1394 /* Move p after IV to start of encrypted ticket, update length */
1395 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1396 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1397 sdec = OPENSSL_malloc(eticklen);
1398 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
1399 (int)eticklen) <= 0) {
1401 ret = SSL_TICKET_FATAL_ERR_OTHER;
1404 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
1406 ret = SSL_TICKET_NO_DECRYPT;
1412 sess = d2i_SSL_SESSION(NULL, &p, slen);
1416 /* Some additional consistency checks */
1418 SSL_SESSION_free(sess);
1420 ret = SSL_TICKET_NO_DECRYPT;
1424 * The session ID, if non-empty, is used by some clients to detect
1425 * that the ticket has been accepted. So we copy it to the session
1426 * structure. If it is empty set length to zero as required by
1430 memcpy(sess->session_id, sess_id, sesslen);
1431 sess->session_id_length = sesslen;
1434 ret = SSL_TICKET_SUCCESS_RENEW;
1436 ret = SSL_TICKET_SUCCESS;
1441 * For session parse failure, indicate that we need to send a new ticket.
1443 ret = SSL_TICKET_NO_DECRYPT;
1446 EVP_CIPHER_CTX_free(ctx);
1447 HMAC_CTX_free(hctx);
1450 * If set, the decrypt_ticket_cb() is called unless a fatal error was
1451 * detected above. The callback is responsible for checking |ret| before it
1452 * performs any action
1454 if (s->session_ctx->decrypt_ticket_cb != NULL
1455 && (ret == SSL_TICKET_EMPTY
1456 || ret == SSL_TICKET_NO_DECRYPT
1457 || ret == SSL_TICKET_SUCCESS
1458 || ret == SSL_TICKET_SUCCESS_RENEW)) {
1459 size_t keyname_len = eticklen;
1462 if (keyname_len > TLSEXT_KEYNAME_LENGTH)
1463 keyname_len = TLSEXT_KEYNAME_LENGTH;
1464 retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len,
1466 s->session_ctx->ticket_cb_data);
1468 case SSL_TICKET_RETURN_ABORT:
1469 ret = SSL_TICKET_FATAL_ERR_OTHER;
1472 case SSL_TICKET_RETURN_IGNORE:
1473 ret = SSL_TICKET_NONE;
1474 SSL_SESSION_free(sess);
1478 case SSL_TICKET_RETURN_IGNORE_RENEW:
1479 if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT)
1480 ret = SSL_TICKET_NO_DECRYPT;
1481 /* else the value of |ret| will already do the right thing */
1482 SSL_SESSION_free(sess);
1486 case SSL_TICKET_RETURN_USE:
1487 case SSL_TICKET_RETURN_USE_RENEW:
1488 if (ret != SSL_TICKET_SUCCESS
1489 && ret != SSL_TICKET_SUCCESS_RENEW)
1490 ret = SSL_TICKET_FATAL_ERR_OTHER;
1491 else if (retcb == SSL_TICKET_RETURN_USE)
1492 ret = SSL_TICKET_SUCCESS;
1494 ret = SSL_TICKET_SUCCESS_RENEW;
1498 ret = SSL_TICKET_FATAL_ERR_OTHER;
1502 if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) {
1504 case SSL_TICKET_NO_DECRYPT:
1505 case SSL_TICKET_SUCCESS_RENEW:
1506 case SSL_TICKET_EMPTY:
1507 s->ext.ticket_expected = 1;
1516 /* Check to see if a signature algorithm is allowed */
1517 static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu)
1519 unsigned char sigalgstr[2];
1522 /* See if sigalgs is recognised and if hash is enabled */
1523 if (!tls1_lookup_md(lu, NULL))
1525 /* DSA is not allowed in TLS 1.3 */
1526 if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
1528 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
1529 if (!s->server && !SSL_IS_DTLS(s) && s->s3->tmp.min_ver >= TLS1_3_VERSION
1530 && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
1531 || lu->hash_idx == SSL_MD_MD5_IDX
1532 || lu->hash_idx == SSL_MD_SHA224_IDX))
1535 /* See if public key algorithm allowed */
1536 if (ssl_cert_is_disabled(lu->sig_idx))
1539 if (lu->sig == NID_id_GostR3410_2012_256
1540 || lu->sig == NID_id_GostR3410_2012_512
1541 || lu->sig == NID_id_GostR3410_2001) {
1542 /* We never allow GOST sig algs on the server with TLSv1.3 */
1543 if (s->server && SSL_IS_TLS13(s))
1546 && s->method->version == TLS_ANY_VERSION
1547 && s->s3->tmp.max_ver >= TLS1_3_VERSION) {
1549 STACK_OF(SSL_CIPHER) *sk;
1552 * We're a client that could negotiate TLSv1.3. We only allow GOST
1553 * sig algs if we could negotiate TLSv1.2 or below and we have GOST
1554 * ciphersuites enabled.
1557 if (s->s3->tmp.min_ver >= TLS1_3_VERSION)
1560 sk = SSL_get_ciphers(s);
1561 num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0;
1562 for (i = 0; i < num; i++) {
1563 const SSL_CIPHER *c;
1565 c = sk_SSL_CIPHER_value(sk, i);
1566 /* Skip disabled ciphers */
1567 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
1570 if ((c->algorithm_mkey & SSL_kGOST) != 0)
1578 if (lu->hash == NID_undef)
1580 /* Security bits: half digest bits */
1581 secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4;
1582 /* Finally see if security callback allows it */
1583 sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
1584 sigalgstr[1] = lu->sigalg & 0xff;
1585 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
1589 * Get a mask of disabled public key algorithms based on supported signature
1590 * algorithms. For example if no signature algorithm supports RSA then RSA is
1594 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
1596 const uint16_t *sigalgs;
1597 size_t i, sigalgslen;
1598 uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
1600 * Go through all signature algorithms seeing if we support any
1603 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
1604 for (i = 0; i < sigalgslen; i++, sigalgs++) {
1605 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs);
1606 const SSL_CERT_LOOKUP *clu;
1611 clu = ssl_cert_lookup_by_idx(lu->sig_idx);
1615 /* If algorithm is disabled see if we can enable it */
1616 if ((clu->amask & disabled_mask) != 0
1617 && tls12_sigalg_allowed(s, op, lu))
1618 disabled_mask &= ~clu->amask;
1620 *pmask_a |= disabled_mask;
1623 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
1624 const uint16_t *psig, size_t psiglen)
1629 for (i = 0; i < psiglen; i++, psig++) {
1630 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig);
1632 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
1634 if (!WPACKET_put_bytes_u16(pkt, *psig))
1637 * If TLS 1.3 must have at least one valid TLS 1.3 message
1638 * signing algorithm: i.e. neither RSA nor SHA1/SHA224
1640 if (rv == 0 && (!SSL_IS_TLS13(s)
1641 || (lu->sig != EVP_PKEY_RSA
1642 && lu->hash != NID_sha1
1643 && lu->hash != NID_sha224)))
1647 SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
1651 /* Given preference and allowed sigalgs set shared sigalgs */
1652 static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
1653 const uint16_t *pref, size_t preflen,
1654 const uint16_t *allow, size_t allowlen)
1656 const uint16_t *ptmp, *atmp;
1657 size_t i, j, nmatch = 0;
1658 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
1659 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp);
1661 /* Skip disabled hashes or signature algorithms */
1662 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
1664 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
1665 if (*ptmp == *atmp) {
1676 /* Set shared signature algorithms for SSL structures */
1677 static int tls1_set_shared_sigalgs(SSL *s)
1679 const uint16_t *pref, *allow, *conf;
1680 size_t preflen, allowlen, conflen;
1682 const SIGALG_LOOKUP **salgs = NULL;
1684 unsigned int is_suiteb = tls1_suiteb(s);
1686 OPENSSL_free(c->shared_sigalgs);
1687 c->shared_sigalgs = NULL;
1688 c->shared_sigalgslen = 0;
1689 /* If client use client signature algorithms if not NULL */
1690 if (!s->server && c->client_sigalgs && !is_suiteb) {
1691 conf = c->client_sigalgs;
1692 conflen = c->client_sigalgslen;
1693 } else if (c->conf_sigalgs && !is_suiteb) {
1694 conf = c->conf_sigalgs;
1695 conflen = c->conf_sigalgslen;
1697 conflen = tls12_get_psigalgs(s, 0, &conf);
1698 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
1701 allow = s->s3->tmp.peer_sigalgs;
1702 allowlen = s->s3->tmp.peer_sigalgslen;
1706 pref = s->s3->tmp.peer_sigalgs;
1707 preflen = s->s3->tmp.peer_sigalgslen;
1709 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
1711 if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) {
1712 SSLerr(SSL_F_TLS1_SET_SHARED_SIGALGS, ERR_R_MALLOC_FAILURE);
1715 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
1719 c->shared_sigalgs = salgs;
1720 c->shared_sigalgslen = nmatch;
1724 int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
1730 size = PACKET_remaining(pkt);
1732 /* Invalid data length */
1733 if (size == 0 || (size & 1) != 0)
1738 if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) {
1739 SSLerr(SSL_F_TLS1_SAVE_U16, ERR_R_MALLOC_FAILURE);
1742 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
1750 OPENSSL_free(*pdest);
1757 int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert)
1759 /* Extension ignored for inappropriate versions */
1760 if (!SSL_USE_SIGALGS(s))
1762 /* Should never happen */
1763 if (s->cert == NULL)
1767 return tls1_save_u16(pkt, &s->s3->tmp.peer_cert_sigalgs,
1768 &s->s3->tmp.peer_cert_sigalgslen);
1770 return tls1_save_u16(pkt, &s->s3->tmp.peer_sigalgs,
1771 &s->s3->tmp.peer_sigalgslen);
1775 /* Set preferred digest for each key type */
1777 int tls1_process_sigalgs(SSL *s)
1780 uint32_t *pvalid = s->s3->tmp.valid_flags;
1783 if (!tls1_set_shared_sigalgs(s))
1786 for (i = 0; i < SSL_PKEY_NUM; i++)
1789 for (i = 0; i < c->shared_sigalgslen; i++) {
1790 const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i];
1791 int idx = sigptr->sig_idx;
1793 /* Ignore PKCS1 based sig algs in TLSv1.3 */
1794 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
1796 /* If not disabled indicate we can explicitly sign */
1797 if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
1798 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
1803 int SSL_get_sigalgs(SSL *s, int idx,
1804 int *psign, int *phash, int *psignhash,
1805 unsigned char *rsig, unsigned char *rhash)
1807 uint16_t *psig = s->s3->tmp.peer_sigalgs;
1808 size_t numsigalgs = s->s3->tmp.peer_sigalgslen;
1809 if (psig == NULL || numsigalgs > INT_MAX)
1812 const SIGALG_LOOKUP *lu;
1814 if (idx >= (int)numsigalgs)
1818 *rhash = (unsigned char)((*psig >> 8) & 0xff);
1820 *rsig = (unsigned char)(*psig & 0xff);
1821 lu = tls1_lookup_sigalg(*psig);
1823 *psign = lu != NULL ? lu->sig : NID_undef;
1825 *phash = lu != NULL ? lu->hash : NID_undef;
1826 if (psignhash != NULL)
1827 *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
1829 return (int)numsigalgs;
1832 int SSL_get_shared_sigalgs(SSL *s, int idx,
1833 int *psign, int *phash, int *psignhash,
1834 unsigned char *rsig, unsigned char *rhash)
1836 const SIGALG_LOOKUP *shsigalgs;
1837 if (s->cert->shared_sigalgs == NULL
1839 || idx >= (int)s->cert->shared_sigalgslen
1840 || s->cert->shared_sigalgslen > INT_MAX)
1842 shsigalgs = s->cert->shared_sigalgs[idx];
1844 *phash = shsigalgs->hash;
1846 *psign = shsigalgs->sig;
1847 if (psignhash != NULL)
1848 *psignhash = shsigalgs->sigandhash;
1850 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
1852 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
1853 return (int)s->cert->shared_sigalgslen;
1856 /* Maximum possible number of unique entries in sigalgs array */
1857 #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
1861 /* TLSEXT_SIGALG_XXX values */
1862 uint16_t sigalgs[TLS_MAX_SIGALGCNT];
1865 static void get_sigorhash(int *psig, int *phash, const char *str)
1867 if (strcmp(str, "RSA") == 0) {
1868 *psig = EVP_PKEY_RSA;
1869 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
1870 *psig = EVP_PKEY_RSA_PSS;
1871 } else if (strcmp(str, "DSA") == 0) {
1872 *psig = EVP_PKEY_DSA;
1873 } else if (strcmp(str, "ECDSA") == 0) {
1874 *psig = EVP_PKEY_EC;
1876 *phash = OBJ_sn2nid(str);
1877 if (*phash == NID_undef)
1878 *phash = OBJ_ln2nid(str);
1881 /* Maximum length of a signature algorithm string component */
1882 #define TLS_MAX_SIGSTRING_LEN 40
1884 static int sig_cb(const char *elem, int len, void *arg)
1886 sig_cb_st *sarg = arg;
1888 const SIGALG_LOOKUP *s;
1889 char etmp[TLS_MAX_SIGSTRING_LEN], *p;
1890 int sig_alg = NID_undef, hash_alg = NID_undef;
1893 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
1895 if (len > (int)(sizeof(etmp) - 1))
1897 memcpy(etmp, elem, len);
1899 p = strchr(etmp, '+');
1901 * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
1902 * if there's no '+' in the provided name, look for the new-style combined
1903 * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
1904 * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
1905 * rsa_pss_rsae_* that differ only by public key OID; in such cases
1906 * we will pick the _rsae_ variant, by virtue of them appearing earlier
1910 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1912 if (s->name != NULL && strcmp(etmp, s->name) == 0) {
1913 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
1917 if (i == OSSL_NELEM(sigalg_lookup_tbl))
1924 get_sigorhash(&sig_alg, &hash_alg, etmp);
1925 get_sigorhash(&sig_alg, &hash_alg, p);
1926 if (sig_alg == NID_undef || hash_alg == NID_undef)
1928 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1930 if (s->hash == hash_alg && s->sig == sig_alg) {
1931 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
1935 if (i == OSSL_NELEM(sigalg_lookup_tbl))
1939 /* Reject duplicates */
1940 for (i = 0; i < sarg->sigalgcnt - 1; i++) {
1941 if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) {
1950 * Set supported signature algorithms based on a colon separated list of the
1951 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
1953 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
1957 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
1961 return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
1964 int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
1969 if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) {
1970 SSLerr(SSL_F_TLS1_SET_RAW_SIGALGS, ERR_R_MALLOC_FAILURE);
1973 memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));
1976 OPENSSL_free(c->client_sigalgs);
1977 c->client_sigalgs = sigalgs;
1978 c->client_sigalgslen = salglen;
1980 OPENSSL_free(c->conf_sigalgs);
1981 c->conf_sigalgs = sigalgs;
1982 c->conf_sigalgslen = salglen;
1988 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
1990 uint16_t *sigalgs, *sptr;
1995 if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) {
1996 SSLerr(SSL_F_TLS1_SET_SIGALGS, ERR_R_MALLOC_FAILURE);
1999 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2001 const SIGALG_LOOKUP *curr;
2002 int md_id = *psig_nids++;
2003 int sig_id = *psig_nids++;
2005 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
2007 if (curr->hash == md_id && curr->sig == sig_id) {
2008 *sptr++ = curr->sigalg;
2013 if (j == OSSL_NELEM(sigalg_lookup_tbl))
2018 OPENSSL_free(c->client_sigalgs);
2019 c->client_sigalgs = sigalgs;
2020 c->client_sigalgslen = salglen / 2;
2022 OPENSSL_free(c->conf_sigalgs);
2023 c->conf_sigalgs = sigalgs;
2024 c->conf_sigalgslen = salglen / 2;
2030 OPENSSL_free(sigalgs);
2034 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
2038 if (default_nid == -1)
2040 sig_nid = X509_get_signature_nid(x);
2042 return sig_nid == default_nid ? 1 : 0;
2043 for (i = 0; i < c->shared_sigalgslen; i++)
2044 if (sig_nid == c->shared_sigalgs[i]->sigandhash)
2049 /* Check to see if a certificate issuer name matches list of CA names */
2050 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2054 nm = X509_get_issuer_name(x);
2055 for (i = 0; i < sk_X509_NAME_num(names); i++) {
2056 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2063 * Check certificate chain is consistent with TLS extensions and is usable by
2064 * server. This servers two purposes: it allows users to check chains before
2065 * passing them to the server and it allows the server to check chains before
2066 * attempting to use them.
2069 /* Flags which need to be set for a certificate when strict mode not set */
2071 #define CERT_PKEY_VALID_FLAGS \
2072 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2073 /* Strict mode flags */
2074 #define CERT_PKEY_STRICT_FLAGS \
2075 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2076 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2078 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2083 int check_flags = 0, strict_mode;
2084 CERT_PKEY *cpk = NULL;
2087 unsigned int suiteb_flags = tls1_suiteb(s);
2088 /* idx == -1 means checking server chains */
2090 /* idx == -2 means checking client certificate chains */
2093 idx = (int)(cpk - c->pkeys);
2095 cpk = c->pkeys + idx;
2096 pvalid = s->s3->tmp.valid_flags + idx;
2098 pk = cpk->privatekey;
2100 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2101 /* If no cert or key, forget it */
2110 if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
2113 pvalid = s->s3->tmp.valid_flags + idx;
2115 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
2116 check_flags = CERT_PKEY_STRICT_FLAGS;
2118 check_flags = CERT_PKEY_VALID_FLAGS;
2125 check_flags |= CERT_PKEY_SUITEB;
2126 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
2127 if (ok == X509_V_OK)
2128 rv |= CERT_PKEY_SUITEB;
2129 else if (!check_flags)
2134 * Check all signature algorithms are consistent with signature
2135 * algorithms extension if TLS 1.2 or later and strict mode.
2137 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
2140 if (s->s3->tmp.peer_cert_sigalgs != NULL
2141 || s->s3->tmp.peer_sigalgs != NULL) {
2143 /* If no sigalgs extension use defaults from RFC5246 */
2147 rsign = EVP_PKEY_RSA;
2148 default_nid = NID_sha1WithRSAEncryption;
2151 case SSL_PKEY_DSA_SIGN:
2152 rsign = EVP_PKEY_DSA;
2153 default_nid = NID_dsaWithSHA1;
2157 rsign = EVP_PKEY_EC;
2158 default_nid = NID_ecdsa_with_SHA1;
2161 case SSL_PKEY_GOST01:
2162 rsign = NID_id_GostR3410_2001;
2163 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
2166 case SSL_PKEY_GOST12_256:
2167 rsign = NID_id_GostR3410_2012_256;
2168 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
2171 case SSL_PKEY_GOST12_512:
2172 rsign = NID_id_GostR3410_2012_512;
2173 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
2182 * If peer sent no signature algorithms extension and we have set
2183 * preferred signature algorithms check we support sha1.
2185 if (default_nid > 0 && c->conf_sigalgs) {
2187 const uint16_t *p = c->conf_sigalgs;
2188 for (j = 0; j < c->conf_sigalgslen; j++, p++) {
2189 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
2191 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
2194 if (j == c->conf_sigalgslen) {
2201 /* Check signature algorithm of each cert in chain */
2202 if (!tls1_check_sig_alg(c, x, default_nid)) {
2206 rv |= CERT_PKEY_EE_SIGNATURE;
2207 rv |= CERT_PKEY_CA_SIGNATURE;
2208 for (i = 0; i < sk_X509_num(chain); i++) {
2209 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
2211 rv &= ~CERT_PKEY_CA_SIGNATURE;
2218 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
2219 else if (check_flags)
2220 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
2222 /* Check cert parameters are consistent */
2223 if (tls1_check_cert_param(s, x, 1))
2224 rv |= CERT_PKEY_EE_PARAM;
2225 else if (!check_flags)
2228 rv |= CERT_PKEY_CA_PARAM;
2229 /* In strict mode check rest of chain too */
2230 else if (strict_mode) {
2231 rv |= CERT_PKEY_CA_PARAM;
2232 for (i = 0; i < sk_X509_num(chain); i++) {
2233 X509 *ca = sk_X509_value(chain, i);
2234 if (!tls1_check_cert_param(s, ca, 0)) {
2236 rv &= ~CERT_PKEY_CA_PARAM;
2243 if (!s->server && strict_mode) {
2244 STACK_OF(X509_NAME) *ca_dn;
2246 switch (EVP_PKEY_id(pk)) {
2248 check_type = TLS_CT_RSA_SIGN;
2251 check_type = TLS_CT_DSS_SIGN;
2254 check_type = TLS_CT_ECDSA_SIGN;
2258 const uint8_t *ctypes = s->s3->tmp.ctype;
2261 for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) {
2262 if (*ctypes == check_type) {
2263 rv |= CERT_PKEY_CERT_TYPE;
2267 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
2270 rv |= CERT_PKEY_CERT_TYPE;
2273 ca_dn = s->s3->tmp.peer_ca_names;
2275 if (!sk_X509_NAME_num(ca_dn))
2276 rv |= CERT_PKEY_ISSUER_NAME;
2278 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2279 if (ssl_check_ca_name(ca_dn, x))
2280 rv |= CERT_PKEY_ISSUER_NAME;
2282 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2283 for (i = 0; i < sk_X509_num(chain); i++) {
2284 X509 *xtmp = sk_X509_value(chain, i);
2285 if (ssl_check_ca_name(ca_dn, xtmp)) {
2286 rv |= CERT_PKEY_ISSUER_NAME;
2291 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
2294 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
2296 if (!check_flags || (rv & check_flags) == check_flags)
2297 rv |= CERT_PKEY_VALID;
2301 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2302 rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
2304 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
2307 * When checking a CERT_PKEY structure all flags are irrelevant if the
2311 if (rv & CERT_PKEY_VALID) {
2314 /* Preserve sign and explicit sign flag, clear rest */
2315 *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
2322 /* Set validity of certificates in an SSL structure */
2323 void tls1_set_cert_validity(SSL *s)
2325 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
2326 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
2327 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
2328 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
2329 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
2330 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
2331 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
2332 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
2333 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448);
2336 /* User level utility function to check a chain is suitable */
2337 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
2339 return tls1_check_chain(s, x, pk, chain, -1);
2342 #ifndef OPENSSL_NO_DH
2343 DH *ssl_get_auto_dh(SSL *s)
2345 int dh_secbits = 80;
2346 if (s->cert->dh_tmp_auto == 2)
2347 return DH_get_1024_160();
2348 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
2349 if (s->s3->tmp.new_cipher->strength_bits == 256)
2354 if (s->s3->tmp.cert == NULL)
2356 dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey);
2359 if (dh_secbits >= 128) {
2365 if (g == NULL || !BN_set_word(g, 2)) {
2370 if (dh_secbits >= 192)
2371 p = BN_get_rfc3526_prime_8192(NULL);
2373 p = BN_get_rfc3526_prime_3072(NULL);
2374 if (p == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
2382 if (dh_secbits >= 112)
2383 return DH_get_2048_224();
2384 return DH_get_1024_160();
2388 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2391 EVP_PKEY *pkey = X509_get0_pubkey(x);
2394 * If no parameters this will return -1 and fail using the default
2395 * security callback for any non-zero security level. This will
2396 * reject keys which omit parameters but this only affects DSA and
2397 * omission of parameters is never (?) done in practice.
2399 secbits = EVP_PKEY_security_bits(pkey);
2402 return ssl_security(s, op, secbits, 0, x);
2404 return ssl_ctx_security(ctx, op, secbits, 0, x);
2407 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2409 /* Lookup signature algorithm digest */
2410 int secbits, nid, pknid;
2411 /* Don't check signature if self signed */
2412 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
2414 if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
2416 /* If digest NID not defined use signature NID */
2417 if (nid == NID_undef)
2420 return ssl_security(s, op, secbits, nid, x);
2422 return ssl_ctx_security(ctx, op, secbits, nid, x);
2425 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
2428 vfy = SSL_SECOP_PEER;
2430 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
2431 return SSL_R_EE_KEY_TOO_SMALL;
2433 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
2434 return SSL_R_CA_KEY_TOO_SMALL;
2436 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
2437 return SSL_R_CA_MD_TOO_WEAK;
2442 * Check security of a chain, if |sk| includes the end entity certificate then
2443 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
2444 * one to the peer. Return values: 1 if ok otherwise error code to use
2447 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
2449 int rv, start_idx, i;
2451 x = sk_X509_value(sk, 0);
2456 rv = ssl_security_cert(s, NULL, x, vfy, 1);
2460 for (i = start_idx; i < sk_X509_num(sk); i++) {
2461 x = sk_X509_value(sk, i);
2462 rv = ssl_security_cert(s, NULL, x, vfy, 0);
2470 * For TLS 1.2 servers check if we have a certificate which can be used
2471 * with the signature algorithm "lu" and return index of certificate.
2474 static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
2476 int sig_idx = lu->sig_idx;
2477 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
2479 /* If not recognised or not supported by cipher mask it is not suitable */
2481 || (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) == 0
2482 || (clu->nid == EVP_PKEY_RSA_PSS
2483 && (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0))
2486 return s->s3->tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
2490 * Returns true if |s| has a usable certificate configured for use
2491 * with signature scheme |sig|.
2492 * "Usable" includes a check for presence as well as applying
2493 * the signature_algorithm_cert restrictions sent by the peer (if any).
2494 * Returns false if no usable certificate is found.
2496 static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx)
2498 const SIGALG_LOOKUP *lu;
2502 /* TLS 1.2 callers can override lu->sig_idx, but not TLS 1.3 callers. */
2505 if (!ssl_has_cert(s, idx))
2507 if (s->s3->tmp.peer_cert_sigalgs != NULL) {
2508 for (i = 0; i < s->s3->tmp.peer_cert_sigalgslen; i++) {
2509 lu = tls1_lookup_sigalg(s->s3->tmp.peer_cert_sigalgs[i]);
2511 || !X509_get_signature_info(s->cert->pkeys[idx].x509, &mdnid,
2512 &pknid, NULL, NULL))
2515 * TODO this does not differentiate between the
2516 * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
2517 * have a chain here that lets us look at the key OID in the
2518 * signing certificate.
2520 if (mdnid == lu->hash && pknid == lu->sig)
2529 * Choose an appropriate signature algorithm based on available certificates
2530 * Sets chosen certificate and signature algorithm.
2532 * For servers if we fail to find a required certificate it is a fatal error,
2533 * an appropriate error code is set and a TLS alert is sent.
2535 * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
2536 * a fatal error: we will either try another certificate or not present one
2537 * to the server. In this case no error is set.
2539 int tls_choose_sigalg(SSL *s, int fatalerrs)
2541 const SIGALG_LOOKUP *lu = NULL;
2544 s->s3->tmp.cert = NULL;
2545 s->s3->tmp.sigalg = NULL;
2547 if (SSL_IS_TLS13(s)) {
2549 #ifndef OPENSSL_NO_EC
2553 /* Look for a certificate matching shared sigalgs */
2554 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2555 lu = s->cert->shared_sigalgs[i];
2558 /* Skip SHA1, SHA224, DSA and RSA if not PSS */
2559 if (lu->hash == NID_sha1
2560 || lu->hash == NID_sha224
2561 || lu->sig == EVP_PKEY_DSA
2562 || lu->sig == EVP_PKEY_RSA)
2564 /* Check that we have a cert, and signature_algorithms_cert */
2565 if (!tls1_lookup_md(lu, NULL) || !has_usable_cert(s, lu, -1))
2567 if (lu->sig == EVP_PKEY_EC) {
2568 #ifndef OPENSSL_NO_EC
2570 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2572 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2574 if (lu->curve != NID_undef && curve != lu->curve)
2579 } else if (lu->sig == EVP_PKEY_RSA_PSS) {
2580 /* validate that key is large enough for the signature algorithm */
2583 pkey = s->cert->pkeys[lu->sig_idx].privatekey;
2584 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu))
2589 if (i == s->cert->shared_sigalgslen) {
2592 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG,
2593 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2597 /* If ciphersuite doesn't require a cert nothing to do */
2598 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aCERT))
2600 if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
2603 if (SSL_USE_SIGALGS(s)) {
2605 if (s->s3->tmp.peer_sigalgs != NULL) {
2606 #ifndef OPENSSL_NO_EC
2609 /* For Suite B need to match signature algorithm to curve */
2610 if (tls1_suiteb(s)) {
2611 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2612 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2619 * Find highest preference signature algorithm matching
2622 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2623 lu = s->cert->shared_sigalgs[i];
2626 if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
2629 int cc_idx = s->cert->key - s->cert->pkeys;
2631 sig_idx = lu->sig_idx;
2632 if (cc_idx != sig_idx)
2635 /* Check that we have a cert, and sig_algs_cert */
2636 if (!has_usable_cert(s, lu, sig_idx))
2638 if (lu->sig == EVP_PKEY_RSA_PSS) {
2639 /* validate that key is large enough for the signature algorithm */
2640 EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;
2642 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu))
2645 #ifndef OPENSSL_NO_EC
2646 if (curve == -1 || lu->curve == curve)
2650 if (i == s->cert->shared_sigalgslen) {
2653 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2654 SSL_F_TLS_CHOOSE_SIGALG,
2655 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2660 * If we have no sigalg use defaults
2662 const uint16_t *sent_sigs;
2663 size_t sent_sigslen;
2665 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2668 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2669 ERR_R_INTERNAL_ERROR);
2673 /* Check signature matches a type we sent */
2674 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
2675 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
2676 if (lu->sigalg == *sent_sigs
2677 && has_usable_cert(s, lu, lu->sig_idx))
2680 if (i == sent_sigslen) {
2683 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2684 SSL_F_TLS_CHOOSE_SIGALG,
2685 SSL_R_WRONG_SIGNATURE_TYPE);
2690 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2693 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2694 ERR_R_INTERNAL_ERROR);
2700 sig_idx = lu->sig_idx;
2701 s->s3->tmp.cert = &s->cert->pkeys[sig_idx];
2702 s->cert->key = s->s3->tmp.cert;
2703 s->s3->tmp.sigalg = lu;
2707 int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
2709 if (mode != TLSEXT_max_fragment_length_DISABLED
2710 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
2711 SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
2712 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
2716 ctx->ext.max_fragment_len_mode = mode;
2720 int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
2722 if (mode != TLSEXT_max_fragment_length_DISABLED
2723 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
2724 SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
2725 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
2729 ssl->ext.max_fragment_len_mode = mode;
2733 uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
2735 return session->ext.max_fragment_len_mode;