2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 /* We need access to the deprecated low level HMAC APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
15 #include <openssl/objects.h>
16 #include <openssl/evp.h>
17 #include <openssl/hmac.h>
18 #include <openssl/core_names.h>
19 #include <openssl/ocsp.h>
20 #include <openssl/conf.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/dh.h>
23 #include <openssl/bn.h>
24 #include "internal/nelem.h"
25 #include "ssl_local.h"
26 #include <openssl/ct.h>
28 static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey);
29 static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu);
31 SSL3_ENC_METHOD const TLSv1_enc_data = {
35 tls1_generate_master_secret,
36 tls1_change_cipher_state,
37 tls1_final_finish_mac,
38 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
39 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
41 tls1_export_keying_material,
43 ssl3_set_handshake_header,
44 tls_close_construct_packet,
48 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
52 tls1_generate_master_secret,
53 tls1_change_cipher_state,
54 tls1_final_finish_mac,
55 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
56 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
58 tls1_export_keying_material,
59 SSL_ENC_FLAG_EXPLICIT_IV,
60 ssl3_set_handshake_header,
61 tls_close_construct_packet,
65 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
69 tls1_generate_master_secret,
70 tls1_change_cipher_state,
71 tls1_final_finish_mac,
72 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
73 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
75 tls1_export_keying_material,
76 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
77 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
78 ssl3_set_handshake_header,
79 tls_close_construct_packet,
83 SSL3_ENC_METHOD const TLSv1_3_enc_data = {
86 tls13_setup_key_block,
87 tls13_generate_master_secret,
88 tls13_change_cipher_state,
89 tls13_final_finish_mac,
90 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
91 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
93 tls13_export_keying_material,
94 SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
95 ssl3_set_handshake_header,
96 tls_close_construct_packet,
100 long tls1_default_timeout(void)
103 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
104 * http, the cache would over fill
106 return (60 * 60 * 2);
113 if (!s->method->ssl_clear(s))
119 void tls1_free(SSL *s)
121 OPENSSL_free(s->ext.session_ticket);
125 int tls1_clear(SSL *s)
130 if (s->method->version == TLS_ANY_VERSION)
131 s->version = TLS_MAX_VERSION_INTERNAL;
133 s->version = s->method->version;
139 * Table of group information.
141 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
142 static const TLS_GROUP_INFO nid_list[] = {
143 # ifndef OPENSSL_NO_EC
144 {NID_sect163k1, 80, TLS_GROUP_CURVE_CHAR2, 0x0001}, /* sect163k1 (1) */
145 {NID_sect163r1, 80, TLS_GROUP_CURVE_CHAR2, 0x0002}, /* sect163r1 (2) */
146 {NID_sect163r2, 80, TLS_GROUP_CURVE_CHAR2, 0x0003}, /* sect163r2 (3) */
147 {NID_sect193r1, 80, TLS_GROUP_CURVE_CHAR2, 0x0004}, /* sect193r1 (4) */
148 {NID_sect193r2, 80, TLS_GROUP_CURVE_CHAR2, 0x0005}, /* sect193r2 (5) */
149 {NID_sect233k1, 112, TLS_GROUP_CURVE_CHAR2, 0x0006}, /* sect233k1 (6) */
150 {NID_sect233r1, 112, TLS_GROUP_CURVE_CHAR2, 0x0007}, /* sect233r1 (7) */
151 {NID_sect239k1, 112, TLS_GROUP_CURVE_CHAR2, 0x0008}, /* sect239k1 (8) */
152 {NID_sect283k1, 128, TLS_GROUP_CURVE_CHAR2, 0x0009}, /* sect283k1 (9) */
153 {NID_sect283r1, 128, TLS_GROUP_CURVE_CHAR2, 0x000A}, /* sect283r1 (10) */
154 {NID_sect409k1, 192, TLS_GROUP_CURVE_CHAR2, 0x000B}, /* sect409k1 (11) */
155 {NID_sect409r1, 192, TLS_GROUP_CURVE_CHAR2, 0x000C}, /* sect409r1 (12) */
156 {NID_sect571k1, 256, TLS_GROUP_CURVE_CHAR2, 0x000D}, /* sect571k1 (13) */
157 {NID_sect571r1, 256, TLS_GROUP_CURVE_CHAR2, 0x000E}, /* sect571r1 (14) */
158 {NID_secp160k1, 80, TLS_GROUP_CURVE_PRIME, 0x000F}, /* secp160k1 (15) */
159 {NID_secp160r1, 80, TLS_GROUP_CURVE_PRIME, 0x0010}, /* secp160r1 (16) */
160 {NID_secp160r2, 80, TLS_GROUP_CURVE_PRIME, 0x0011}, /* secp160r2 (17) */
161 {NID_secp192k1, 80, TLS_GROUP_CURVE_PRIME, 0x0012}, /* secp192k1 (18) */
162 {NID_X9_62_prime192v1, 80, TLS_GROUP_CURVE_PRIME, 0x0013}, /* secp192r1 (19) */
163 {NID_secp224k1, 112, TLS_GROUP_CURVE_PRIME, 0x0014}, /* secp224k1 (20) */
164 {NID_secp224r1, 112, TLS_GROUP_CURVE_PRIME, 0x0015}, /* secp224r1 (21) */
165 {NID_secp256k1, 128, TLS_GROUP_CURVE_PRIME, 0x0016}, /* secp256k1 (22) */
166 {NID_X9_62_prime256v1, 128, TLS_GROUP_CURVE_PRIME, 0x0017}, /* secp256r1 (23) */
167 {NID_secp384r1, 192, TLS_GROUP_CURVE_PRIME, 0x0018}, /* secp384r1 (24) */
168 {NID_secp521r1, 256, TLS_GROUP_CURVE_PRIME, 0x0019}, /* secp521r1 (25) */
169 {NID_brainpoolP256r1, 128, TLS_GROUP_CURVE_PRIME, 0x001A}, /* brainpoolP256r1 (26) */
170 {NID_brainpoolP384r1, 192, TLS_GROUP_CURVE_PRIME, 0x001B}, /* brainpoolP384r1 (27) */
171 {NID_brainpoolP512r1, 256, TLS_GROUP_CURVE_PRIME, 0x001C}, /* brainpool512r1 (28) */
172 {EVP_PKEY_X25519, 128, TLS_GROUP_CURVE_CUSTOM, 0x001D}, /* X25519 (29) */
173 {EVP_PKEY_X448, 224, TLS_GROUP_CURVE_CUSTOM, 0x001E}, /* X448 (30) */
174 # endif /* OPENSSL_NO_EC */
175 # ifndef OPENSSL_NO_DH
176 /* Security bit values for FFDHE groups are updated as per RFC 7919 */
177 {NID_ffdhe2048, 103, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0100}, /* ffdhe2048 (0x0100) */
178 {NID_ffdhe3072, 125, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0101}, /* ffdhe3072 (0x0101) */
179 {NID_ffdhe4096, 150, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0102}, /* ffdhe4096 (0x0102) */
180 {NID_ffdhe6144, 175, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0103}, /* ffdhe6144 (0x0103) */
181 {NID_ffdhe8192, 192, TLS_GROUP_FFDHE_FOR_TLS1_3, 0x0104}, /* ffdhe8192 (0x0104) */
182 # endif /* OPENSSL_NO_DH */
186 #ifndef OPENSSL_NO_EC
187 static const unsigned char ecformats_default[] = {
188 TLSEXT_ECPOINTFORMAT_uncompressed,
189 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
190 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
192 #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
194 /* The default curves */
195 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
196 static const uint16_t supported_groups_default[] = {
197 # ifndef OPENSSL_NO_EC
198 29, /* X25519 (29) */
199 23, /* secp256r1 (23) */
201 25, /* secp521r1 (25) */
202 24, /* secp384r1 (24) */
204 # ifndef OPENSSL_NO_DH
205 0x100, /* ffdhe2048 (0x100) */
206 0x101, /* ffdhe3072 (0x101) */
207 0x102, /* ffdhe4096 (0x102) */
208 0x103, /* ffdhe6144 (0x103) */
209 0x104, /* ffdhe8192 (0x104) */
212 #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
214 #ifndef OPENSSL_NO_EC
215 static const uint16_t suiteb_curves[] = {
221 const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)
223 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
226 /* ECC curves from RFC 4492 and RFC 7027 FFDHE group from RFC 8446 */
227 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
228 if (nid_list[i].group_id == group_id)
231 #endif /* !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC) */
235 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)
236 int tls1_group_id2nid(uint16_t group_id)
238 const TLS_GROUP_INFO *ginf = tls1_group_id_lookup(group_id);
240 return ginf == NULL ? NID_undef : ginf->nid;
243 static uint16_t tls1_nid2group_id(int nid)
247 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
248 if (nid_list[i].nid == nid)
249 return nid_list[i].group_id;
253 #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
256 * Set *pgroups to the supported groups list and *pgroupslen to
257 * the number of groups supported.
259 void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
262 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
263 /* For Suite B mode only include P-256, P-384 */
264 switch (tls1_suiteb(s)) {
265 # ifndef OPENSSL_NO_EC
266 case SSL_CERT_FLAG_SUITEB_128_LOS:
267 *pgroups = suiteb_curves;
268 *pgroupslen = OSSL_NELEM(suiteb_curves);
271 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
272 *pgroups = suiteb_curves;
276 case SSL_CERT_FLAG_SUITEB_192_LOS:
277 *pgroups = suiteb_curves + 1;
283 if (s->ext.supportedgroups == NULL) {
284 *pgroups = supported_groups_default;
285 *pgroupslen = OSSL_NELEM(supported_groups_default);
287 *pgroups = s->ext.supportedgroups;
288 *pgroupslen = s->ext.supportedgroups_len;
295 #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
298 int tls_valid_group(SSL *s, uint16_t group_id, int version)
300 const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(group_id);
302 if (version < TLS1_3_VERSION) {
303 if ((ginfo->flags & TLS_GROUP_ONLY_FOR_TLS1_3) != 0)
309 /* See if group is allowed by security callback */
310 int tls_group_allowed(SSL *s, uint16_t group, int op)
312 const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(group);
313 unsigned char gtmp[2];
317 #ifdef OPENSSL_NO_EC2M
318 if (ginfo->flags & TLS_GROUP_CURVE_CHAR2)
322 if (ginfo->flags & TLS_GROUP_FFDHE)
325 gtmp[0] = group >> 8;
326 gtmp[1] = group & 0xff;
327 return ssl_security(s, op, ginfo->secbits, ginfo->nid, (void *)gtmp);
330 /* Return 1 if "id" is in "list" */
331 static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
334 for (i = 0; i < listlen; i++)
341 * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
342 * if there is no match.
343 * For nmatch == -1, return number of matches
344 * For nmatch == -2, return the id of the group to use for
345 * a tmp key, or 0 if there is no match.
347 uint16_t tls1_shared_group(SSL *s, int nmatch)
349 const uint16_t *pref, *supp;
350 size_t num_pref, num_supp, i;
353 /* Can't do anything on client side */
357 if (tls1_suiteb(s)) {
359 * For Suite B ciphersuite determines curve: we already know
360 * these are acceptable due to previous checks.
362 unsigned long cid = s->s3.tmp.new_cipher->id;
364 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
365 return TLSEXT_curve_P_256;
366 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
367 return TLSEXT_curve_P_384;
368 /* Should never happen */
371 /* If not Suite B just return first preference shared curve */
375 * If server preference set, our groups are the preference order
376 * otherwise peer decides.
378 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
379 tls1_get_supported_groups(s, &pref, &num_pref);
380 tls1_get_peer_groups(s, &supp, &num_supp);
382 tls1_get_peer_groups(s, &pref, &num_pref);
383 tls1_get_supported_groups(s, &supp, &num_supp);
386 for (k = 0, i = 0; i < num_pref; i++) {
387 uint16_t id = pref[i];
389 if (!tls1_in_list(id, supp, num_supp)
390 || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED))
398 /* Out of range (nmatch > k). */
402 int tls1_set_groups(uint16_t **pext, size_t *pextlen,
403 int *groups, size_t ngroups)
405 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
409 * Bitmap of groups included to detect duplicates: two variables are added
410 * to detect duplicates as some values are more than 32.
412 unsigned long *dup_list = NULL;
413 unsigned long dup_list_egrp = 0;
414 unsigned long dup_list_dhgrp = 0;
417 SSLerr(SSL_F_TLS1_SET_GROUPS, SSL_R_BAD_LENGTH);
420 if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) {
421 SSLerr(SSL_F_TLS1_SET_GROUPS, ERR_R_MALLOC_FAILURE);
424 for (i = 0; i < ngroups; i++) {
425 unsigned long idmask;
427 id = tls1_nid2group_id(groups[i]);
428 if ((id & 0x00FF) >= (sizeof(unsigned long) * 8))
430 idmask = 1L << (id & 0x00FF);
431 dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp;
432 if (!id || ((*dup_list) & idmask))
446 #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
449 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
450 # define MAX_GROUPLIST OSSL_NELEM(nid_list)
454 int nid_arr[MAX_GROUPLIST];
457 static int nid_cb(const char *elem, int len, void *arg)
459 nid_cb_st *narg = arg;
465 if (narg->nidcnt == MAX_GROUPLIST)
467 if (len > (int)(sizeof(etmp) - 1))
469 memcpy(etmp, elem, len);
471 # ifndef OPENSSL_NO_EC
472 nid = EC_curve_nist2nid(etmp);
474 if (nid == NID_undef)
475 nid = OBJ_sn2nid(etmp);
476 if (nid == NID_undef)
477 nid = OBJ_ln2nid(etmp);
478 if (nid == NID_undef)
480 for (i = 0; i < narg->nidcnt; i++)
481 if (narg->nid_arr[i] == nid)
483 narg->nid_arr[narg->nidcnt++] = nid;
486 #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
488 /* Set groups based on a colon separate list */
489 int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str)
491 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
494 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
498 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
504 /* Check a group id matches preferences */
505 int tls1_check_group_id(SSL *s, uint16_t group_id, int check_own_groups)
507 const uint16_t *groups;
513 /* Check for Suite B compliance */
514 if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) {
515 unsigned long cid = s->s3.tmp.new_cipher->id;
517 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
518 if (group_id != TLSEXT_curve_P_256)
520 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
521 if (group_id != TLSEXT_curve_P_384)
524 /* Should never happen */
529 if (check_own_groups) {
530 /* Check group is one of our preferences */
531 tls1_get_supported_groups(s, &groups, &groups_len);
532 if (!tls1_in_list(group_id, groups, groups_len))
536 if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
539 /* For clients, nothing more to check */
543 /* Check group is one of peers preferences */
544 tls1_get_peer_groups(s, &groups, &groups_len);
547 * RFC 4492 does not require the supported elliptic curves extension
548 * so if it is not sent we can just choose any curve.
549 * It is invalid to send an empty list in the supported groups
550 * extension, so groups_len == 0 always means no extension.
554 return tls1_in_list(group_id, groups, groups_len);
557 #ifndef OPENSSL_NO_EC
558 void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
562 * If we have a custom point format list use it otherwise use default
564 if (s->ext.ecpointformats) {
565 *pformats = s->ext.ecpointformats;
566 *num_formats = s->ext.ecpointformats_len;
568 *pformats = ecformats_default;
569 /* For Suite B we don't support char2 fields */
571 *num_formats = sizeof(ecformats_default) - 1;
573 *num_formats = sizeof(ecformats_default);
577 /* Check a key is compatible with compression extension */
578 static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
582 unsigned char comp_id;
585 /* If not an EC key nothing to check */
586 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
588 ec = EVP_PKEY_get0_EC_KEY(pkey);
589 grp = EC_KEY_get0_group(ec);
591 /* Get required compression id */
592 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
593 comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
594 } else if (SSL_IS_TLS13(s)) {
596 * ec_point_formats extension is not used in TLSv1.3 so we ignore
601 int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp));
603 if (field_type == NID_X9_62_prime_field)
604 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
605 else if (field_type == NID_X9_62_characteristic_two_field)
606 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
611 * If point formats extension present check it, otherwise everything is
612 * supported (see RFC4492).
614 if (s->ext.peer_ecpointformats == NULL)
617 for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
618 if (s->ext.peer_ecpointformats[i] == comp_id)
624 /* Return group id of a key */
625 static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
627 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
632 grp = EC_KEY_get0_group(ec);
633 return tls1_nid2group_id(EC_GROUP_get_curve_name(grp));
637 * Check cert parameters compatible with extensions: currently just checks EC
638 * certificates have compatible curves and compression.
640 static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
644 pkey = X509_get0_pubkey(x);
647 /* If not EC nothing to do */
648 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
650 /* Check compression */
651 if (!tls1_check_pkey_comp(s, pkey))
653 group_id = tls1_get_group_id(pkey);
655 * For a server we allow the certificate to not be in our list of supported
658 if (!tls1_check_group_id(s, group_id, !s->server))
661 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
664 if (check_ee_md && tls1_suiteb(s)) {
668 /* Check to see we have necessary signing algorithm */
669 if (group_id == TLSEXT_curve_P_256)
670 check_md = NID_ecdsa_with_SHA256;
671 else if (group_id == TLSEXT_curve_P_384)
672 check_md = NID_ecdsa_with_SHA384;
674 return 0; /* Should never happen */
675 for (i = 0; i < s->shared_sigalgslen; i++) {
676 if (check_md == s->shared_sigalgs[i]->sigandhash)
685 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
687 * @cid: Cipher ID we're considering using
689 * Checks that the kECDHE cipher suite we're considering using
690 * is compatible with the client extensions.
692 * Returns 0 when the cipher can't be used or 1 when it can.
694 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
696 /* If not Suite B just need a shared group */
698 return tls1_shared_group(s, 0) != 0;
700 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
703 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
704 return tls1_check_group_id(s, TLSEXT_curve_P_256, 1);
705 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
706 return tls1_check_group_id(s, TLSEXT_curve_P_384, 1);
713 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
718 #endif /* OPENSSL_NO_EC */
720 /* Default sigalg schemes */
721 static const uint16_t tls12_sigalgs[] = {
722 #ifndef OPENSSL_NO_EC
723 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
724 TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
725 TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
726 TLSEXT_SIGALG_ed25519,
730 TLSEXT_SIGALG_rsa_pss_pss_sha256,
731 TLSEXT_SIGALG_rsa_pss_pss_sha384,
732 TLSEXT_SIGALG_rsa_pss_pss_sha512,
733 TLSEXT_SIGALG_rsa_pss_rsae_sha256,
734 TLSEXT_SIGALG_rsa_pss_rsae_sha384,
735 TLSEXT_SIGALG_rsa_pss_rsae_sha512,
737 TLSEXT_SIGALG_rsa_pkcs1_sha256,
738 TLSEXT_SIGALG_rsa_pkcs1_sha384,
739 TLSEXT_SIGALG_rsa_pkcs1_sha512,
741 #ifndef OPENSSL_NO_EC
742 TLSEXT_SIGALG_ecdsa_sha224,
743 TLSEXT_SIGALG_ecdsa_sha1,
745 TLSEXT_SIGALG_rsa_pkcs1_sha224,
746 TLSEXT_SIGALG_rsa_pkcs1_sha1,
747 #ifndef OPENSSL_NO_DSA
748 TLSEXT_SIGALG_dsa_sha224,
749 TLSEXT_SIGALG_dsa_sha1,
751 TLSEXT_SIGALG_dsa_sha256,
752 TLSEXT_SIGALG_dsa_sha384,
753 TLSEXT_SIGALG_dsa_sha512,
755 #ifndef OPENSSL_NO_GOST
756 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
757 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
758 TLSEXT_SIGALG_gostr34102001_gostr3411,
762 #ifndef OPENSSL_NO_EC
763 static const uint16_t suiteb_sigalgs[] = {
764 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
765 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
769 static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
770 #ifndef OPENSSL_NO_EC
771 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
772 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
773 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
774 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
775 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
776 NID_ecdsa_with_SHA384, NID_secp384r1},
777 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
778 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
779 NID_ecdsa_with_SHA512, NID_secp521r1},
780 {"ed25519", TLSEXT_SIGALG_ed25519,
781 NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
782 NID_undef, NID_undef},
783 {"ed448", TLSEXT_SIGALG_ed448,
784 NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448,
785 NID_undef, NID_undef},
786 {NULL, TLSEXT_SIGALG_ecdsa_sha224,
787 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
788 NID_ecdsa_with_SHA224, NID_undef},
789 {NULL, TLSEXT_SIGALG_ecdsa_sha1,
790 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
791 NID_ecdsa_with_SHA1, NID_undef},
793 {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,
794 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
795 NID_undef, NID_undef},
796 {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,
797 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
798 NID_undef, NID_undef},
799 {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,
800 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
801 NID_undef, NID_undef},
802 {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256,
803 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
804 NID_undef, NID_undef},
805 {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384,
806 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
807 NID_undef, NID_undef},
808 {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512,
809 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
810 NID_undef, NID_undef},
811 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
812 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
813 NID_sha256WithRSAEncryption, NID_undef},
814 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
815 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
816 NID_sha384WithRSAEncryption, NID_undef},
817 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
818 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
819 NID_sha512WithRSAEncryption, NID_undef},
820 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
821 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
822 NID_sha224WithRSAEncryption, NID_undef},
823 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
824 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
825 NID_sha1WithRSAEncryption, NID_undef},
826 #ifndef OPENSSL_NO_DSA
827 {NULL, TLSEXT_SIGALG_dsa_sha256,
828 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
829 NID_dsa_with_SHA256, NID_undef},
830 {NULL, TLSEXT_SIGALG_dsa_sha384,
831 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
832 NID_undef, NID_undef},
833 {NULL, TLSEXT_SIGALG_dsa_sha512,
834 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
835 NID_undef, NID_undef},
836 {NULL, TLSEXT_SIGALG_dsa_sha224,
837 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
838 NID_undef, NID_undef},
839 {NULL, TLSEXT_SIGALG_dsa_sha1,
840 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
841 NID_dsaWithSHA1, NID_undef},
843 #ifndef OPENSSL_NO_GOST
844 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
845 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
846 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
847 NID_undef, NID_undef},
848 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
849 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
850 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
851 NID_undef, NID_undef},
852 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
853 NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
854 NID_id_GostR3410_2001, SSL_PKEY_GOST01,
855 NID_undef, NID_undef}
858 /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
859 static const SIGALG_LOOKUP legacy_rsa_sigalg = {
860 "rsa_pkcs1_md5_sha1", 0,
861 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
862 EVP_PKEY_RSA, SSL_PKEY_RSA,
867 * Default signature algorithm values used if signature algorithms not present.
868 * From RFC5246. Note: order must match certificate index order.
870 static const uint16_t tls_default_sigalg[] = {
871 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
872 0, /* SSL_PKEY_RSA_PSS_SIGN */
873 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
874 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
875 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
876 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
877 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */
878 0, /* SSL_PKEY_ED25519 */
879 0, /* SSL_PKEY_ED448 */
882 /* Lookup TLS signature algorithm */
883 static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
886 const SIGALG_LOOKUP *s;
888 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
890 if (s->sigalg == sigalg)
895 /* Lookup hash: return 0 if invalid or not enabled */
896 int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
901 /* lu->hash == NID_undef means no associated digest */
902 if (lu->hash == NID_undef) {
905 md = ssl_md(ctx, lu->hash_idx);
915 * Check if key is large enough to generate RSA-PSS signature.
917 * The key must greater than or equal to 2 * hash length + 2.
918 * SHA512 has a hash length of 64 bytes, which is incompatible
919 * with a 128 byte (1024 bit) key.
921 #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2)
922 static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey,
923 const SIGALG_LOOKUP *lu)
929 if (!tls1_lookup_md(ctx, lu, &md) || md == NULL)
931 if (EVP_PKEY_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md))
937 * Returns a signature algorithm when the peer did not send a list of supported
938 * signature algorithms. The signature algorithm is fixed for the certificate
939 * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the
940 * certificate type from |s| will be used.
941 * Returns the signature algorithm to use, or NULL on error.
943 static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
949 /* Work out index corresponding to ciphersuite */
950 for (i = 0; i < SSL_PKEY_NUM; i++) {
951 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
953 if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) {
960 * Some GOST ciphersuites allow more than one signature algorithms
962 if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) {
965 for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01;
967 if (s->cert->pkeys[real_idx].privatekey != NULL) {
974 idx = s->cert->key - s->cert->pkeys;
977 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
979 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
980 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
982 if (!tls1_lookup_md(s->ctx, lu, NULL))
984 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
988 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg))
990 return &legacy_rsa_sigalg;
992 /* Set peer sigalg based key type */
993 int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
996 const SIGALG_LOOKUP *lu;
998 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
1000 lu = tls1_get_legacy_sigalg(s, idx);
1003 s->s3.tmp.peer_sigalg = lu;
1007 size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
1010 * If Suite B mode use Suite B sigalgs only, ignore any other
1013 #ifndef OPENSSL_NO_EC
1014 switch (tls1_suiteb(s)) {
1015 case SSL_CERT_FLAG_SUITEB_128_LOS:
1016 *psigs = suiteb_sigalgs;
1017 return OSSL_NELEM(suiteb_sigalgs);
1019 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1020 *psigs = suiteb_sigalgs;
1023 case SSL_CERT_FLAG_SUITEB_192_LOS:
1024 *psigs = suiteb_sigalgs + 1;
1029 * We use client_sigalgs (if not NULL) if we're a server
1030 * and sending a certificate request or if we're a client and
1031 * determining which shared algorithm to use.
1033 if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
1034 *psigs = s->cert->client_sigalgs;
1035 return s->cert->client_sigalgslen;
1036 } else if (s->cert->conf_sigalgs) {
1037 *psigs = s->cert->conf_sigalgs;
1038 return s->cert->conf_sigalgslen;
1040 *psigs = tls12_sigalgs;
1041 return OSSL_NELEM(tls12_sigalgs);
1045 #ifndef OPENSSL_NO_EC
1047 * Called by servers only. Checks that we have a sig alg that supports the
1048 * specified EC curve.
1050 int tls_check_sigalg_curve(const SSL *s, int curve)
1052 const uint16_t *sigs;
1055 if (s->cert->conf_sigalgs) {
1056 sigs = s->cert->conf_sigalgs;
1057 siglen = s->cert->conf_sigalgslen;
1059 sigs = tls12_sigalgs;
1060 siglen = OSSL_NELEM(tls12_sigalgs);
1063 for (i = 0; i < siglen; i++) {
1064 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(sigs[i]);
1068 if (lu->sig == EVP_PKEY_EC
1069 && lu->curve != NID_undef
1070 && curve == lu->curve)
1079 * Return the number of security bits for the signature algorithm, or 0 on
1082 static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu)
1084 const EVP_MD *md = NULL;
1087 if (!tls1_lookup_md(ctx, lu, &md))
1091 /* Security bits: half digest bits */
1092 secbits = EVP_MD_size(md) * 4;
1094 /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */
1095 if (lu->sigalg == TLSEXT_SIGALG_ed25519)
1097 else if (lu->sigalg == TLSEXT_SIGALG_ed448)
1104 * Check signature algorithm is consistent with sent supported signature
1105 * algorithms and if so set relevant digest and signature scheme in
1108 int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
1110 const uint16_t *sent_sigs;
1111 const EVP_MD *md = NULL;
1113 size_t sent_sigslen, i, cidx;
1114 int pkeyid = EVP_PKEY_id(pkey);
1115 const SIGALG_LOOKUP *lu;
1118 /* Should never happen */
1121 if (SSL_IS_TLS13(s)) {
1122 /* Disallow DSA for TLS 1.3 */
1123 if (pkeyid == EVP_PKEY_DSA) {
1124 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
1125 SSL_R_WRONG_SIGNATURE_TYPE);
1128 /* Only allow PSS for TLS 1.3 */
1129 if (pkeyid == EVP_PKEY_RSA)
1130 pkeyid = EVP_PKEY_RSA_PSS;
1132 lu = tls1_lookup_sigalg(sig);
1134 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
1135 * is consistent with signature: RSA keys can be used for RSA-PSS
1138 || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
1139 || (pkeyid != lu->sig
1140 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
1141 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
1142 SSL_R_WRONG_SIGNATURE_TYPE);
1145 /* Check the sigalg is consistent with the key OID */
1146 if (!ssl_cert_lookup_by_nid(EVP_PKEY_id(pkey), &cidx)
1147 || lu->sig_idx != (int)cidx) {
1148 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
1149 SSL_R_WRONG_SIGNATURE_TYPE);
1153 #ifndef OPENSSL_NO_EC
1154 if (pkeyid == EVP_PKEY_EC) {
1156 /* Check point compression is permitted */
1157 if (!tls1_check_pkey_comp(s, pkey)) {
1158 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1159 SSL_F_TLS12_CHECK_PEER_SIGALG,
1160 SSL_R_ILLEGAL_POINT_COMPRESSION);
1164 /* For TLS 1.3 or Suite B check curve matches signature algorithm */
1165 if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
1166 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1167 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
1169 if (lu->curve != NID_undef && curve != lu->curve) {
1170 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1171 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1175 if (!SSL_IS_TLS13(s)) {
1176 /* Check curve matches extensions */
1177 if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) {
1178 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1179 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1182 if (tls1_suiteb(s)) {
1183 /* Check sigalg matches a permissible Suite B value */
1184 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
1185 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
1186 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1187 SSL_F_TLS12_CHECK_PEER_SIGALG,
1188 SSL_R_WRONG_SIGNATURE_TYPE);
1193 } else if (tls1_suiteb(s)) {
1194 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1195 SSL_R_WRONG_SIGNATURE_TYPE);
1200 /* Check signature matches a type we sent */
1201 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1202 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
1203 if (sig == *sent_sigs)
1206 /* Allow fallback to SHA1 if not strict mode */
1207 if (i == sent_sigslen && (lu->hash != NID_sha1
1208 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1209 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1210 SSL_R_WRONG_SIGNATURE_TYPE);
1213 if (!tls1_lookup_md(s->ctx, lu, &md)) {
1214 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1215 SSL_R_UNKNOWN_DIGEST);
1219 * Make sure security callback allows algorithm. For historical
1220 * reasons we have to pass the sigalg as a two byte char array.
1222 sigalgstr[0] = (sig >> 8) & 0xff;
1223 sigalgstr[1] = sig & 0xff;
1224 secbits = sigalg_security_bits(s->ctx, lu);
1226 !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits,
1227 md != NULL ? EVP_MD_type(md) : NID_undef,
1228 (void *)sigalgstr)) {
1229 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1230 SSL_R_WRONG_SIGNATURE_TYPE);
1233 /* Store the sigalg the peer uses */
1234 s->s3.tmp.peer_sigalg = lu;
1238 int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
1240 if (s->s3.tmp.peer_sigalg == NULL)
1242 *pnid = s->s3.tmp.peer_sigalg->sig;
1246 int SSL_get_signature_type_nid(const SSL *s, int *pnid)
1248 if (s->s3.tmp.sigalg == NULL)
1250 *pnid = s->s3.tmp.sigalg->sig;
1255 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1256 * supported, doesn't appear in supported signature algorithms, isn't supported
1257 * by the enabled protocol versions or by the security level.
1259 * This function should only be used for checking which ciphers are supported
1262 * Call ssl_cipher_disabled() to check that it's enabled or not.
1264 int ssl_set_client_disabled(SSL *s)
1266 s->s3.tmp.mask_a = 0;
1267 s->s3.tmp.mask_k = 0;
1268 ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1269 if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver,
1270 &s->s3.tmp.max_ver, NULL) != 0)
1272 #ifndef OPENSSL_NO_PSK
1273 /* with PSK there must be client callback set */
1274 if (!s->psk_client_callback) {
1275 s->s3.tmp.mask_a |= SSL_aPSK;
1276 s->s3.tmp.mask_k |= SSL_PSK;
1278 #endif /* OPENSSL_NO_PSK */
1279 #ifndef OPENSSL_NO_SRP
1280 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1281 s->s3.tmp.mask_a |= SSL_aSRP;
1282 s->s3.tmp.mask_k |= SSL_kSRP;
1289 * ssl_cipher_disabled - check that a cipher is disabled or not
1290 * @s: SSL connection that you want to use the cipher on
1291 * @c: cipher to check
1292 * @op: Security check that you want to do
1293 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
1295 * Returns 1 when it's disabled, 0 when enabled.
1297 int ssl_cipher_disabled(const SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
1299 if (c->algorithm_mkey & s->s3.tmp.mask_k
1300 || c->algorithm_auth & s->s3.tmp.mask_a)
1302 if (s->s3.tmp.max_ver == 0)
1304 if (!SSL_IS_DTLS(s)) {
1305 int min_tls = c->min_tls;
1308 * For historical reasons we will allow ECHDE to be selected by a server
1309 * in SSLv3 if we are a client
1311 if (min_tls == TLS1_VERSION && ecdhe
1312 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
1313 min_tls = SSL3_VERSION;
1315 if ((min_tls > s->s3.tmp.max_ver) || (c->max_tls < s->s3.tmp.min_ver))
1318 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3.tmp.max_ver)
1319 || DTLS_VERSION_LT(c->max_dtls, s->s3.tmp.min_ver)))
1322 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1325 int tls_use_ticket(SSL *s)
1327 if ((s->options & SSL_OP_NO_TICKET))
1329 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1332 int tls1_set_server_sigalgs(SSL *s)
1336 /* Clear any shared signature algorithms */
1337 OPENSSL_free(s->shared_sigalgs);
1338 s->shared_sigalgs = NULL;
1339 s->shared_sigalgslen = 0;
1340 /* Clear certificate validity flags */
1341 for (i = 0; i < SSL_PKEY_NUM; i++)
1342 s->s3.tmp.valid_flags[i] = 0;
1344 * If peer sent no signature algorithms check to see if we support
1345 * the default algorithm for each certificate type
1347 if (s->s3.tmp.peer_cert_sigalgs == NULL
1348 && s->s3.tmp.peer_sigalgs == NULL) {
1349 const uint16_t *sent_sigs;
1350 size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1352 for (i = 0; i < SSL_PKEY_NUM; i++) {
1353 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
1358 /* Check default matches a type we sent */
1359 for (j = 0; j < sent_sigslen; j++) {
1360 if (lu->sigalg == sent_sigs[j]) {
1361 s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN;
1369 if (!tls1_process_sigalgs(s)) {
1370 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1371 SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR);
1374 if (s->shared_sigalgs != NULL)
1377 /* Fatal error if no shared signature algorithms */
1378 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS,
1379 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
1384 * Gets the ticket information supplied by the client if any.
1386 * hello: The parsed ClientHello data
1387 * ret: (output) on return, if a ticket was decrypted, then this is set to
1388 * point to the resulting session.
1390 SSL_TICKET_STATUS tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
1394 RAW_EXTENSION *ticketext;
1397 s->ext.ticket_expected = 0;
1400 * If tickets disabled or not supported by the protocol version
1401 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
1404 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
1405 return SSL_TICKET_NONE;
1407 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
1408 if (!ticketext->present)
1409 return SSL_TICKET_NONE;
1411 size = PACKET_remaining(&ticketext->data);
1413 return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
1414 hello->session_id, hello->session_id_len, ret);
1418 * tls_decrypt_ticket attempts to decrypt a session ticket.
1420 * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are
1421 * expecting a pre-shared key ciphersuite, in which case we have no use for
1422 * session tickets and one will never be decrypted, nor will
1423 * s->ext.ticket_expected be set to 1.
1426 * Sets s->ext.ticket_expected to 1 if the server will have to issue
1427 * a new session ticket to the client because the client indicated support
1428 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1429 * a session ticket or we couldn't use the one it gave us, or if
1430 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1431 * Otherwise, s->ext.ticket_expected is set to 0.
1433 * etick: points to the body of the session ticket extension.
1434 * eticklen: the length of the session tickets extension.
1435 * sess_id: points at the session ID.
1436 * sesslen: the length of the session ID.
1437 * psess: (output) on return, if a ticket was decrypted, then this is set to
1438 * point to the resulting session.
1440 SSL_TICKET_STATUS tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1441 size_t eticklen, const unsigned char *sess_id,
1442 size_t sesslen, SSL_SESSION **psess)
1444 SSL_SESSION *sess = NULL;
1445 unsigned char *sdec;
1446 const unsigned char *p;
1447 int slen, renew_ticket = 0, declen;
1448 SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER;
1450 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1451 SSL_HMAC *hctx = NULL;
1452 EVP_CIPHER_CTX *ctx = NULL;
1453 SSL_CTX *tctx = s->session_ctx;
1455 if (eticklen == 0) {
1457 * The client will accept a ticket but doesn't currently have
1458 * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3
1460 ret = SSL_TICKET_EMPTY;
1463 if (!SSL_IS_TLS13(s) && s->ext.session_secret_cb) {
1465 * Indicate that the ticket couldn't be decrypted rather than
1466 * generating the session from ticket now, trigger
1467 * abbreviated handshake based on external mechanism to
1468 * calculate the master secret later.
1470 ret = SSL_TICKET_NO_DECRYPT;
1474 /* Need at least keyname + iv */
1475 if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) {
1476 ret = SSL_TICKET_NO_DECRYPT;
1480 /* Initialize session ticket encryption and HMAC contexts */
1481 hctx = ssl_hmac_new(tctx);
1483 ret = SSL_TICKET_FATAL_ERR_MALLOC;
1486 ctx = EVP_CIPHER_CTX_new();
1488 ret = SSL_TICKET_FATAL_ERR_MALLOC;
1491 #ifndef OPENSSL_NO_DEPRECATED_3_0
1492 if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
1494 if (tctx->ext.ticket_key_evp_cb != NULL)
1497 unsigned char *nctick = (unsigned char *)etick;
1500 if (tctx->ext.ticket_key_evp_cb != NULL)
1501 rv = tctx->ext.ticket_key_evp_cb(s, nctick,
1502 nctick + TLSEXT_KEYNAME_LENGTH,
1504 ssl_hmac_get0_EVP_MAC_CTX(hctx),
1506 #ifndef OPENSSL_NO_DEPRECATED_3_0
1507 else if (tctx->ext.ticket_key_cb != NULL)
1508 /* if 0 is returned, write an empty ticket */
1509 rv = tctx->ext.ticket_key_cb(s, nctick,
1510 nctick + TLSEXT_KEYNAME_LENGTH,
1511 ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0);
1514 ret = SSL_TICKET_FATAL_ERR_OTHER;
1518 ret = SSL_TICKET_NO_DECRYPT;
1524 /* Check key name matches */
1525 if (memcmp(etick, tctx->ext.tick_key_name,
1526 TLSEXT_KEYNAME_LENGTH) != 0) {
1527 ret = SSL_TICKET_NO_DECRYPT;
1530 if (ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
1531 sizeof(tctx->ext.secure->tick_hmac_key),
1533 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
1534 tctx->ext.secure->tick_aes_key,
1535 etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
1536 ret = SSL_TICKET_FATAL_ERR_OTHER;
1539 if (SSL_IS_TLS13(s))
1543 * Attempt to process session ticket, first conduct sanity and integrity
1546 mlen = ssl_hmac_size(hctx);
1548 ret = SSL_TICKET_FATAL_ERR_OTHER;
1552 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1554 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
1555 ret = SSL_TICKET_NO_DECRYPT;
1559 /* Check HMAC of encrypted ticket */
1560 if (ssl_hmac_update(hctx, etick, eticklen) <= 0
1561 || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) {
1562 ret = SSL_TICKET_FATAL_ERR_OTHER;
1566 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
1567 ret = SSL_TICKET_NO_DECRYPT;
1570 /* Attempt to decrypt session data */
1571 /* Move p after IV to start of encrypted ticket, update length */
1572 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1573 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1574 sdec = OPENSSL_malloc(eticklen);
1575 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
1576 (int)eticklen) <= 0) {
1578 ret = SSL_TICKET_FATAL_ERR_OTHER;
1581 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
1583 ret = SSL_TICKET_NO_DECRYPT;
1589 sess = d2i_SSL_SESSION(NULL, &p, slen);
1593 /* Some additional consistency checks */
1595 SSL_SESSION_free(sess);
1597 ret = SSL_TICKET_NO_DECRYPT;
1601 * The session ID, if non-empty, is used by some clients to detect
1602 * that the ticket has been accepted. So we copy it to the session
1603 * structure. If it is empty set length to zero as required by
1607 memcpy(sess->session_id, sess_id, sesslen);
1608 sess->session_id_length = sesslen;
1611 ret = SSL_TICKET_SUCCESS_RENEW;
1613 ret = SSL_TICKET_SUCCESS;
1618 * For session parse failure, indicate that we need to send a new ticket.
1620 ret = SSL_TICKET_NO_DECRYPT;
1623 EVP_CIPHER_CTX_free(ctx);
1624 ssl_hmac_free(hctx);
1627 * If set, the decrypt_ticket_cb() is called unless a fatal error was
1628 * detected above. The callback is responsible for checking |ret| before it
1629 * performs any action
1631 if (s->session_ctx->decrypt_ticket_cb != NULL
1632 && (ret == SSL_TICKET_EMPTY
1633 || ret == SSL_TICKET_NO_DECRYPT
1634 || ret == SSL_TICKET_SUCCESS
1635 || ret == SSL_TICKET_SUCCESS_RENEW)) {
1636 size_t keyname_len = eticklen;
1639 if (keyname_len > TLSEXT_KEYNAME_LENGTH)
1640 keyname_len = TLSEXT_KEYNAME_LENGTH;
1641 retcb = s->session_ctx->decrypt_ticket_cb(s, sess, etick, keyname_len,
1643 s->session_ctx->ticket_cb_data);
1645 case SSL_TICKET_RETURN_ABORT:
1646 ret = SSL_TICKET_FATAL_ERR_OTHER;
1649 case SSL_TICKET_RETURN_IGNORE:
1650 ret = SSL_TICKET_NONE;
1651 SSL_SESSION_free(sess);
1655 case SSL_TICKET_RETURN_IGNORE_RENEW:
1656 if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT)
1657 ret = SSL_TICKET_NO_DECRYPT;
1658 /* else the value of |ret| will already do the right thing */
1659 SSL_SESSION_free(sess);
1663 case SSL_TICKET_RETURN_USE:
1664 case SSL_TICKET_RETURN_USE_RENEW:
1665 if (ret != SSL_TICKET_SUCCESS
1666 && ret != SSL_TICKET_SUCCESS_RENEW)
1667 ret = SSL_TICKET_FATAL_ERR_OTHER;
1668 else if (retcb == SSL_TICKET_RETURN_USE)
1669 ret = SSL_TICKET_SUCCESS;
1671 ret = SSL_TICKET_SUCCESS_RENEW;
1675 ret = SSL_TICKET_FATAL_ERR_OTHER;
1679 if (s->ext.session_secret_cb == NULL || SSL_IS_TLS13(s)) {
1681 case SSL_TICKET_NO_DECRYPT:
1682 case SSL_TICKET_SUCCESS_RENEW:
1683 case SSL_TICKET_EMPTY:
1684 s->ext.ticket_expected = 1;
1693 /* Check to see if a signature algorithm is allowed */
1694 static int tls12_sigalg_allowed(const SSL *s, int op, const SIGALG_LOOKUP *lu)
1696 unsigned char sigalgstr[2];
1699 /* See if sigalgs is recognised and if hash is enabled */
1700 if (!tls1_lookup_md(s->ctx, lu, NULL))
1702 /* DSA is not allowed in TLS 1.3 */
1703 if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
1705 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
1706 if (!s->server && !SSL_IS_DTLS(s) && s->s3.tmp.min_ver >= TLS1_3_VERSION
1707 && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
1708 || lu->hash_idx == SSL_MD_MD5_IDX
1709 || lu->hash_idx == SSL_MD_SHA224_IDX))
1712 /* See if public key algorithm allowed */
1713 if (ssl_cert_is_disabled(lu->sig_idx))
1716 if (lu->sig == NID_id_GostR3410_2012_256
1717 || lu->sig == NID_id_GostR3410_2012_512
1718 || lu->sig == NID_id_GostR3410_2001) {
1719 /* We never allow GOST sig algs on the server with TLSv1.3 */
1720 if (s->server && SSL_IS_TLS13(s))
1723 && s->method->version == TLS_ANY_VERSION
1724 && s->s3.tmp.max_ver >= TLS1_3_VERSION) {
1726 STACK_OF(SSL_CIPHER) *sk;
1729 * We're a client that could negotiate TLSv1.3. We only allow GOST
1730 * sig algs if we could negotiate TLSv1.2 or below and we have GOST
1731 * ciphersuites enabled.
1734 if (s->s3.tmp.min_ver >= TLS1_3_VERSION)
1737 sk = SSL_get_ciphers(s);
1738 num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0;
1739 for (i = 0; i < num; i++) {
1740 const SSL_CIPHER *c;
1742 c = sk_SSL_CIPHER_value(sk, i);
1743 /* Skip disabled ciphers */
1744 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
1747 if ((c->algorithm_mkey & SSL_kGOST) != 0)
1755 /* Finally see if security callback allows it */
1756 secbits = sigalg_security_bits(s->ctx, lu);
1757 sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
1758 sigalgstr[1] = lu->sigalg & 0xff;
1759 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
1763 * Get a mask of disabled public key algorithms based on supported signature
1764 * algorithms. For example if no signature algorithm supports RSA then RSA is
1768 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
1770 const uint16_t *sigalgs;
1771 size_t i, sigalgslen;
1772 uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
1774 * Go through all signature algorithms seeing if we support any
1777 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
1778 for (i = 0; i < sigalgslen; i++, sigalgs++) {
1779 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs);
1780 const SSL_CERT_LOOKUP *clu;
1785 clu = ssl_cert_lookup_by_idx(lu->sig_idx);
1789 /* If algorithm is disabled see if we can enable it */
1790 if ((clu->amask & disabled_mask) != 0
1791 && tls12_sigalg_allowed(s, op, lu))
1792 disabled_mask &= ~clu->amask;
1794 *pmask_a |= disabled_mask;
1797 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
1798 const uint16_t *psig, size_t psiglen)
1803 for (i = 0; i < psiglen; i++, psig++) {
1804 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig);
1806 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
1808 if (!WPACKET_put_bytes_u16(pkt, *psig))
1811 * If TLS 1.3 must have at least one valid TLS 1.3 message
1812 * signing algorithm: i.e. neither RSA nor SHA1/SHA224
1814 if (rv == 0 && (!SSL_IS_TLS13(s)
1815 || (lu->sig != EVP_PKEY_RSA
1816 && lu->hash != NID_sha1
1817 && lu->hash != NID_sha224)))
1821 SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
1825 /* Given preference and allowed sigalgs set shared sigalgs */
1826 static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
1827 const uint16_t *pref, size_t preflen,
1828 const uint16_t *allow, size_t allowlen)
1830 const uint16_t *ptmp, *atmp;
1831 size_t i, j, nmatch = 0;
1832 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
1833 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp);
1835 /* Skip disabled hashes or signature algorithms */
1836 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
1838 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
1839 if (*ptmp == *atmp) {
1850 /* Set shared signature algorithms for SSL structures */
1851 static int tls1_set_shared_sigalgs(SSL *s)
1853 const uint16_t *pref, *allow, *conf;
1854 size_t preflen, allowlen, conflen;
1856 const SIGALG_LOOKUP **salgs = NULL;
1858 unsigned int is_suiteb = tls1_suiteb(s);
1860 OPENSSL_free(s->shared_sigalgs);
1861 s->shared_sigalgs = NULL;
1862 s->shared_sigalgslen = 0;
1863 /* If client use client signature algorithms if not NULL */
1864 if (!s->server && c->client_sigalgs && !is_suiteb) {
1865 conf = c->client_sigalgs;
1866 conflen = c->client_sigalgslen;
1867 } else if (c->conf_sigalgs && !is_suiteb) {
1868 conf = c->conf_sigalgs;
1869 conflen = c->conf_sigalgslen;
1871 conflen = tls12_get_psigalgs(s, 0, &conf);
1872 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
1875 allow = s->s3.tmp.peer_sigalgs;
1876 allowlen = s->s3.tmp.peer_sigalgslen;
1880 pref = s->s3.tmp.peer_sigalgs;
1881 preflen = s->s3.tmp.peer_sigalgslen;
1883 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
1885 if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) {
1886 SSLerr(SSL_F_TLS1_SET_SHARED_SIGALGS, ERR_R_MALLOC_FAILURE);
1889 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
1893 s->shared_sigalgs = salgs;
1894 s->shared_sigalgslen = nmatch;
1898 int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
1904 size = PACKET_remaining(pkt);
1906 /* Invalid data length */
1907 if (size == 0 || (size & 1) != 0)
1912 if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) {
1913 SSLerr(SSL_F_TLS1_SAVE_U16, ERR_R_MALLOC_FAILURE);
1916 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
1924 OPENSSL_free(*pdest);
1931 int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert)
1933 /* Extension ignored for inappropriate versions */
1934 if (!SSL_USE_SIGALGS(s))
1936 /* Should never happen */
1937 if (s->cert == NULL)
1941 return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs,
1942 &s->s3.tmp.peer_cert_sigalgslen);
1944 return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs,
1945 &s->s3.tmp.peer_sigalgslen);
1949 /* Set preferred digest for each key type */
1951 int tls1_process_sigalgs(SSL *s)
1954 uint32_t *pvalid = s->s3.tmp.valid_flags;
1956 if (!tls1_set_shared_sigalgs(s))
1959 for (i = 0; i < SSL_PKEY_NUM; i++)
1962 for (i = 0; i < s->shared_sigalgslen; i++) {
1963 const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i];
1964 int idx = sigptr->sig_idx;
1966 /* Ignore PKCS1 based sig algs in TLSv1.3 */
1967 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
1969 /* If not disabled indicate we can explicitly sign */
1970 if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
1971 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
1976 int SSL_get_sigalgs(SSL *s, int idx,
1977 int *psign, int *phash, int *psignhash,
1978 unsigned char *rsig, unsigned char *rhash)
1980 uint16_t *psig = s->s3.tmp.peer_sigalgs;
1981 size_t numsigalgs = s->s3.tmp.peer_sigalgslen;
1982 if (psig == NULL || numsigalgs > INT_MAX)
1985 const SIGALG_LOOKUP *lu;
1987 if (idx >= (int)numsigalgs)
1991 *rhash = (unsigned char)((*psig >> 8) & 0xff);
1993 *rsig = (unsigned char)(*psig & 0xff);
1994 lu = tls1_lookup_sigalg(*psig);
1996 *psign = lu != NULL ? lu->sig : NID_undef;
1998 *phash = lu != NULL ? lu->hash : NID_undef;
1999 if (psignhash != NULL)
2000 *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
2002 return (int)numsigalgs;
2005 int SSL_get_shared_sigalgs(SSL *s, int idx,
2006 int *psign, int *phash, int *psignhash,
2007 unsigned char *rsig, unsigned char *rhash)
2009 const SIGALG_LOOKUP *shsigalgs;
2010 if (s->shared_sigalgs == NULL
2012 || idx >= (int)s->shared_sigalgslen
2013 || s->shared_sigalgslen > INT_MAX)
2015 shsigalgs = s->shared_sigalgs[idx];
2017 *phash = shsigalgs->hash;
2019 *psign = shsigalgs->sig;
2020 if (psignhash != NULL)
2021 *psignhash = shsigalgs->sigandhash;
2023 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
2025 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
2026 return (int)s->shared_sigalgslen;
2029 /* Maximum possible number of unique entries in sigalgs array */
2030 #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
2034 /* TLSEXT_SIGALG_XXX values */
2035 uint16_t sigalgs[TLS_MAX_SIGALGCNT];
2038 static void get_sigorhash(int *psig, int *phash, const char *str)
2040 if (strcmp(str, "RSA") == 0) {
2041 *psig = EVP_PKEY_RSA;
2042 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
2043 *psig = EVP_PKEY_RSA_PSS;
2044 } else if (strcmp(str, "DSA") == 0) {
2045 *psig = EVP_PKEY_DSA;
2046 } else if (strcmp(str, "ECDSA") == 0) {
2047 *psig = EVP_PKEY_EC;
2049 *phash = OBJ_sn2nid(str);
2050 if (*phash == NID_undef)
2051 *phash = OBJ_ln2nid(str);
2054 /* Maximum length of a signature algorithm string component */
2055 #define TLS_MAX_SIGSTRING_LEN 40
2057 static int sig_cb(const char *elem, int len, void *arg)
2059 sig_cb_st *sarg = arg;
2061 const SIGALG_LOOKUP *s;
2062 char etmp[TLS_MAX_SIGSTRING_LEN], *p;
2063 int sig_alg = NID_undef, hash_alg = NID_undef;
2066 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
2068 if (len > (int)(sizeof(etmp) - 1))
2070 memcpy(etmp, elem, len);
2072 p = strchr(etmp, '+');
2074 * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
2075 * if there's no '+' in the provided name, look for the new-style combined
2076 * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
2077 * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
2078 * rsa_pss_rsae_* that differ only by public key OID; in such cases
2079 * we will pick the _rsae_ variant, by virtue of them appearing earlier
2083 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
2085 if (s->name != NULL && strcmp(etmp, s->name) == 0) {
2086 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
2090 if (i == OSSL_NELEM(sigalg_lookup_tbl))
2097 get_sigorhash(&sig_alg, &hash_alg, etmp);
2098 get_sigorhash(&sig_alg, &hash_alg, p);
2099 if (sig_alg == NID_undef || hash_alg == NID_undef)
2101 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
2103 if (s->hash == hash_alg && s->sig == sig_alg) {
2104 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
2108 if (i == OSSL_NELEM(sigalg_lookup_tbl))
2112 /* Reject duplicates */
2113 for (i = 0; i < sarg->sigalgcnt - 1; i++) {
2114 if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) {
2123 * Set supported signature algorithms based on a colon separated list of the
2124 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
2126 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
2130 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
2134 return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
2137 int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
2142 if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) {
2143 SSLerr(SSL_F_TLS1_SET_RAW_SIGALGS, ERR_R_MALLOC_FAILURE);
2146 memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));
2149 OPENSSL_free(c->client_sigalgs);
2150 c->client_sigalgs = sigalgs;
2151 c->client_sigalgslen = salglen;
2153 OPENSSL_free(c->conf_sigalgs);
2154 c->conf_sigalgs = sigalgs;
2155 c->conf_sigalgslen = salglen;
2161 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
2163 uint16_t *sigalgs, *sptr;
2168 if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) {
2169 SSLerr(SSL_F_TLS1_SET_SIGALGS, ERR_R_MALLOC_FAILURE);
2172 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
2174 const SIGALG_LOOKUP *curr;
2175 int md_id = *psig_nids++;
2176 int sig_id = *psig_nids++;
2178 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
2180 if (curr->hash == md_id && curr->sig == sig_id) {
2181 *sptr++ = curr->sigalg;
2186 if (j == OSSL_NELEM(sigalg_lookup_tbl))
2191 OPENSSL_free(c->client_sigalgs);
2192 c->client_sigalgs = sigalgs;
2193 c->client_sigalgslen = salglen / 2;
2195 OPENSSL_free(c->conf_sigalgs);
2196 c->conf_sigalgs = sigalgs;
2197 c->conf_sigalgslen = salglen / 2;
2203 OPENSSL_free(sigalgs);
2207 static int tls1_check_sig_alg(SSL *s, X509 *x, int default_nid)
2209 int sig_nid, use_pc_sigalgs = 0;
2211 const SIGALG_LOOKUP *sigalg;
2213 if (default_nid == -1)
2215 sig_nid = X509_get_signature_nid(x);
2217 return sig_nid == default_nid ? 1 : 0;
2219 if (SSL_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) {
2221 * If we're in TLSv1.3 then we only get here if we're checking the
2222 * chain. If the peer has specified peer_cert_sigalgs then we use them
2223 * otherwise we default to normal sigalgs.
2225 sigalgslen = s->s3.tmp.peer_cert_sigalgslen;
2228 sigalgslen = s->shared_sigalgslen;
2230 for (i = 0; i < sigalgslen; i++) {
2231 sigalg = use_pc_sigalgs
2232 ? tls1_lookup_sigalg(s->s3.tmp.peer_cert_sigalgs[i])
2233 : s->shared_sigalgs[i];
2234 if (sig_nid == sigalg->sigandhash)
2240 /* Check to see if a certificate issuer name matches list of CA names */
2241 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
2245 nm = X509_get_issuer_name(x);
2246 for (i = 0; i < sk_X509_NAME_num(names); i++) {
2247 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
2254 * Check certificate chain is consistent with TLS extensions and is usable by
2255 * server. This servers two purposes: it allows users to check chains before
2256 * passing them to the server and it allows the server to check chains before
2257 * attempting to use them.
2260 /* Flags which need to be set for a certificate when strict mode not set */
2262 #define CERT_PKEY_VALID_FLAGS \
2263 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
2264 /* Strict mode flags */
2265 #define CERT_PKEY_STRICT_FLAGS \
2266 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
2267 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
2269 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
2274 int check_flags = 0, strict_mode;
2275 CERT_PKEY *cpk = NULL;
2278 unsigned int suiteb_flags = tls1_suiteb(s);
2279 /* idx == -1 means checking server chains */
2281 /* idx == -2 means checking client certificate chains */
2284 idx = (int)(cpk - c->pkeys);
2286 cpk = c->pkeys + idx;
2287 pvalid = s->s3.tmp.valid_flags + idx;
2289 pk = cpk->privatekey;
2291 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
2292 /* If no cert or key, forget it */
2301 if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
2304 pvalid = s->s3.tmp.valid_flags + idx;
2306 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
2307 check_flags = CERT_PKEY_STRICT_FLAGS;
2309 check_flags = CERT_PKEY_VALID_FLAGS;
2316 check_flags |= CERT_PKEY_SUITEB;
2317 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
2318 if (ok == X509_V_OK)
2319 rv |= CERT_PKEY_SUITEB;
2320 else if (!check_flags)
2325 * Check all signature algorithms are consistent with signature
2326 * algorithms extension if TLS 1.2 or later and strict mode.
2328 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
2331 if (s->s3.tmp.peer_cert_sigalgs != NULL
2332 || s->s3.tmp.peer_sigalgs != NULL) {
2334 /* If no sigalgs extension use defaults from RFC5246 */
2338 rsign = EVP_PKEY_RSA;
2339 default_nid = NID_sha1WithRSAEncryption;
2342 case SSL_PKEY_DSA_SIGN:
2343 rsign = EVP_PKEY_DSA;
2344 default_nid = NID_dsaWithSHA1;
2348 rsign = EVP_PKEY_EC;
2349 default_nid = NID_ecdsa_with_SHA1;
2352 case SSL_PKEY_GOST01:
2353 rsign = NID_id_GostR3410_2001;
2354 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
2357 case SSL_PKEY_GOST12_256:
2358 rsign = NID_id_GostR3410_2012_256;
2359 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
2362 case SSL_PKEY_GOST12_512:
2363 rsign = NID_id_GostR3410_2012_512;
2364 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
2373 * If peer sent no signature algorithms extension and we have set
2374 * preferred signature algorithms check we support sha1.
2376 if (default_nid > 0 && c->conf_sigalgs) {
2378 const uint16_t *p = c->conf_sigalgs;
2379 for (j = 0; j < c->conf_sigalgslen; j++, p++) {
2380 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
2382 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
2385 if (j == c->conf_sigalgslen) {
2392 /* Check signature algorithm of each cert in chain */
2393 if (SSL_IS_TLS13(s)) {
2395 * We only get here if the application has called SSL_check_chain(),
2396 * so check_flags is always set.
2398 if (find_sig_alg(s, x, pk) != NULL)
2399 rv |= CERT_PKEY_EE_SIGNATURE;
2400 } else if (!tls1_check_sig_alg(s, x, default_nid)) {
2404 rv |= CERT_PKEY_EE_SIGNATURE;
2405 rv |= CERT_PKEY_CA_SIGNATURE;
2406 for (i = 0; i < sk_X509_num(chain); i++) {
2407 if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) {
2409 rv &= ~CERT_PKEY_CA_SIGNATURE;
2416 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
2417 else if (check_flags)
2418 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
2420 /* Check cert parameters are consistent */
2421 if (tls1_check_cert_param(s, x, 1))
2422 rv |= CERT_PKEY_EE_PARAM;
2423 else if (!check_flags)
2426 rv |= CERT_PKEY_CA_PARAM;
2427 /* In strict mode check rest of chain too */
2428 else if (strict_mode) {
2429 rv |= CERT_PKEY_CA_PARAM;
2430 for (i = 0; i < sk_X509_num(chain); i++) {
2431 X509 *ca = sk_X509_value(chain, i);
2432 if (!tls1_check_cert_param(s, ca, 0)) {
2434 rv &= ~CERT_PKEY_CA_PARAM;
2441 if (!s->server && strict_mode) {
2442 STACK_OF(X509_NAME) *ca_dn;
2444 switch (EVP_PKEY_id(pk)) {
2446 check_type = TLS_CT_RSA_SIGN;
2449 check_type = TLS_CT_DSS_SIGN;
2452 check_type = TLS_CT_ECDSA_SIGN;
2456 const uint8_t *ctypes = s->s3.tmp.ctype;
2459 for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) {
2460 if (*ctypes == check_type) {
2461 rv |= CERT_PKEY_CERT_TYPE;
2465 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
2468 rv |= CERT_PKEY_CERT_TYPE;
2471 ca_dn = s->s3.tmp.peer_ca_names;
2473 if (!sk_X509_NAME_num(ca_dn))
2474 rv |= CERT_PKEY_ISSUER_NAME;
2476 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2477 if (ssl_check_ca_name(ca_dn, x))
2478 rv |= CERT_PKEY_ISSUER_NAME;
2480 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2481 for (i = 0; i < sk_X509_num(chain); i++) {
2482 X509 *xtmp = sk_X509_value(chain, i);
2483 if (ssl_check_ca_name(ca_dn, xtmp)) {
2484 rv |= CERT_PKEY_ISSUER_NAME;
2489 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
2492 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
2494 if (!check_flags || (rv & check_flags) == check_flags)
2495 rv |= CERT_PKEY_VALID;
2499 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2500 rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
2502 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
2505 * When checking a CERT_PKEY structure all flags are irrelevant if the
2509 if (rv & CERT_PKEY_VALID) {
2512 /* Preserve sign and explicit sign flag, clear rest */
2513 *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
2520 /* Set validity of certificates in an SSL structure */
2521 void tls1_set_cert_validity(SSL *s)
2523 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
2524 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
2525 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
2526 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
2527 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
2528 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
2529 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
2530 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
2531 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448);
2534 /* User level utility function to check a chain is suitable */
2535 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
2537 return tls1_check_chain(s, x, pk, chain, -1);
2540 #ifndef OPENSSL_NO_DH
2541 DH *ssl_get_auto_dh(SSL *s)
2543 int dh_secbits = 80;
2544 if (s->cert->dh_tmp_auto == 2)
2545 return DH_get_1024_160();
2546 if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
2547 if (s->s3.tmp.new_cipher->strength_bits == 256)
2552 if (s->s3.tmp.cert == NULL)
2554 dh_secbits = EVP_PKEY_security_bits(s->s3.tmp.cert->privatekey);
2557 if (dh_secbits >= 128) {
2563 if (g == NULL || !BN_set_word(g, 2)) {
2568 if (dh_secbits >= 192)
2569 p = BN_get_rfc3526_prime_8192(NULL);
2571 p = BN_get_rfc3526_prime_3072(NULL);
2572 if (p == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
2580 if (dh_secbits >= 112)
2581 return DH_get_2048_224();
2582 return DH_get_1024_160();
2586 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2589 EVP_PKEY *pkey = X509_get0_pubkey(x);
2592 * If no parameters this will return -1 and fail using the default
2593 * security callback for any non-zero security level. This will
2594 * reject keys which omit parameters but this only affects DSA and
2595 * omission of parameters is never (?) done in practice.
2597 secbits = EVP_PKEY_security_bits(pkey);
2600 return ssl_security(s, op, secbits, 0, x);
2602 return ssl_ctx_security(ctx, op, secbits, 0, x);
2605 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2607 /* Lookup signature algorithm digest */
2608 int secbits, nid, pknid;
2609 /* Don't check signature if self signed */
2610 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
2612 if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
2614 /* If digest NID not defined use signature NID */
2615 if (nid == NID_undef)
2618 return ssl_security(s, op, secbits, nid, x);
2620 return ssl_ctx_security(ctx, op, secbits, nid, x);
2623 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
2626 vfy = SSL_SECOP_PEER;
2628 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
2629 return SSL_R_EE_KEY_TOO_SMALL;
2631 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
2632 return SSL_R_CA_KEY_TOO_SMALL;
2634 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
2635 return SSL_R_CA_MD_TOO_WEAK;
2640 * Check security of a chain, if |sk| includes the end entity certificate then
2641 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
2642 * one to the peer. Return values: 1 if ok otherwise error code to use
2645 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
2647 int rv, start_idx, i;
2649 x = sk_X509_value(sk, 0);
2654 rv = ssl_security_cert(s, NULL, x, vfy, 1);
2658 for (i = start_idx; i < sk_X509_num(sk); i++) {
2659 x = sk_X509_value(sk, i);
2660 rv = ssl_security_cert(s, NULL, x, vfy, 0);
2668 * For TLS 1.2 servers check if we have a certificate which can be used
2669 * with the signature algorithm "lu" and return index of certificate.
2672 static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
2674 int sig_idx = lu->sig_idx;
2675 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
2677 /* If not recognised or not supported by cipher mask it is not suitable */
2679 || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0
2680 || (clu->nid == EVP_PKEY_RSA_PSS
2681 && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0))
2684 return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
2688 * Checks the given cert against signature_algorithm_cert restrictions sent by
2689 * the peer (if any) as well as whether the hash from the sigalg is usable with
2691 * Returns true if the cert is usable and false otherwise.
2693 static int check_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x,
2696 const SIGALG_LOOKUP *lu;
2697 int mdnid, pknid, supported;
2701 * If the given EVP_PKEY cannot supporting signing with this sigalg,
2702 * the answer is simply 'no'.
2705 supported = EVP_PKEY_supports_digest_nid(pkey, sig->hash);
2711 * The TLS 1.3 signature_algorithms_cert extension places restrictions
2712 * on the sigalg with which the certificate was signed (by its issuer).
2714 if (s->s3.tmp.peer_cert_sigalgs != NULL) {
2715 if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL))
2717 for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) {
2718 lu = tls1_lookup_sigalg(s->s3.tmp.peer_cert_sigalgs[i]);
2723 * TODO this does not differentiate between the
2724 * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
2725 * have a chain here that lets us look at the key OID in the
2726 * signing certificate.
2728 if (mdnid == lu->hash && pknid == lu->sig)
2735 * Without signat_algorithms_cert, any certificate for which we have
2736 * a viable public key is permitted.
2742 * Returns true if |s| has a usable certificate configured for use
2743 * with signature scheme |sig|.
2744 * "Usable" includes a check for presence as well as applying
2745 * the signature_algorithm_cert restrictions sent by the peer (if any).
2746 * Returns false if no usable certificate is found.
2748 static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx)
2750 /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */
2753 if (!ssl_has_cert(s, idx))
2756 return check_cert_usable(s, sig, s->cert->pkeys[idx].x509,
2757 s->cert->pkeys[idx].privatekey);
2761 * Returns true if the supplied cert |x| and key |pkey| is usable with the
2762 * specified signature scheme |sig|, or false otherwise.
2764 static int is_cert_usable(SSL *s, const SIGALG_LOOKUP *sig, X509 *x,
2769 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
2772 /* Check the key is consistent with the sig alg */
2773 if ((int)idx != sig->sig_idx)
2776 return check_cert_usable(s, sig, x, pkey);
2780 * Find a signature scheme that works with the supplied certificate |x| and key
2781 * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our
2782 * available certs/keys to find one that works.
2784 static const SIGALG_LOOKUP *find_sig_alg(SSL *s, X509 *x, EVP_PKEY *pkey)
2786 const SIGALG_LOOKUP *lu = NULL;
2788 #ifndef OPENSSL_NO_EC
2793 /* Look for a shared sigalgs matching possible certificates */
2794 for (i = 0; i < s->shared_sigalgslen; i++) {
2795 lu = s->shared_sigalgs[i];
2797 /* Skip SHA1, SHA224, DSA and RSA if not PSS */
2798 if (lu->hash == NID_sha1
2799 || lu->hash == NID_sha224
2800 || lu->sig == EVP_PKEY_DSA
2801 || lu->sig == EVP_PKEY_RSA)
2803 /* Check that we have a cert, and signature_algorithms_cert */
2804 if (!tls1_lookup_md(s->ctx, lu, NULL))
2806 if ((pkey == NULL && !has_usable_cert(s, lu, -1))
2807 || (pkey != NULL && !is_cert_usable(s, lu, x, pkey)))
2810 tmppkey = (pkey != NULL) ? pkey
2811 : s->cert->pkeys[lu->sig_idx].privatekey;
2813 if (lu->sig == EVP_PKEY_EC) {
2814 #ifndef OPENSSL_NO_EC
2816 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(tmppkey);
2817 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2819 if (lu->curve != NID_undef && curve != lu->curve)
2824 } else if (lu->sig == EVP_PKEY_RSA_PSS) {
2825 /* validate that key is large enough for the signature algorithm */
2826 if (!rsa_pss_check_min_key_size(s->ctx, tmppkey, lu))
2832 if (i == s->shared_sigalgslen)
2839 * Choose an appropriate signature algorithm based on available certificates
2840 * Sets chosen certificate and signature algorithm.
2842 * For servers if we fail to find a required certificate it is a fatal error,
2843 * an appropriate error code is set and a TLS alert is sent.
2845 * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
2846 * a fatal error: we will either try another certificate or not present one
2847 * to the server. In this case no error is set.
2849 int tls_choose_sigalg(SSL *s, int fatalerrs)
2851 const SIGALG_LOOKUP *lu = NULL;
2854 s->s3.tmp.cert = NULL;
2855 s->s3.tmp.sigalg = NULL;
2857 if (SSL_IS_TLS13(s)) {
2858 lu = find_sig_alg(s, NULL, NULL);
2862 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG,
2863 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2867 /* If ciphersuite doesn't require a cert nothing to do */
2868 if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT))
2870 if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
2873 if (SSL_USE_SIGALGS(s)) {
2875 if (s->s3.tmp.peer_sigalgs != NULL) {
2876 #ifndef OPENSSL_NO_EC
2879 /* For Suite B need to match signature algorithm to curve */
2880 if (tls1_suiteb(s)) {
2881 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2882 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2889 * Find highest preference signature algorithm matching
2892 for (i = 0; i < s->shared_sigalgslen; i++) {
2893 lu = s->shared_sigalgs[i];
2896 if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
2899 int cc_idx = s->cert->key - s->cert->pkeys;
2901 sig_idx = lu->sig_idx;
2902 if (cc_idx != sig_idx)
2905 /* Check that we have a cert, and sig_algs_cert */
2906 if (!has_usable_cert(s, lu, sig_idx))
2908 if (lu->sig == EVP_PKEY_RSA_PSS) {
2909 /* validate that key is large enough for the signature algorithm */
2910 EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;
2912 if (!rsa_pss_check_min_key_size(s->ctx, pkey, lu))
2915 #ifndef OPENSSL_NO_EC
2916 if (curve == -1 || lu->curve == curve)
2920 #ifndef OPENSSL_NO_GOST
2922 * Some Windows-based implementations do not send GOST algorithms indication
2923 * in supported_algorithms extension, so when we have GOST-based ciphersuite,
2924 * we have to assume GOST support.
2926 if (i == s->shared_sigalgslen && s->s3.tmp.new_cipher->algorithm_auth & (SSL_aGOST01 | SSL_aGOST12)) {
2927 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2930 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2931 SSL_F_TLS_CHOOSE_SIGALG,
2932 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2936 sig_idx = lu->sig_idx;
2940 if (i == s->shared_sigalgslen) {
2943 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2944 SSL_F_TLS_CHOOSE_SIGALG,
2945 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2950 * If we have no sigalg use defaults
2952 const uint16_t *sent_sigs;
2953 size_t sent_sigslen;
2955 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2958 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2959 ERR_R_INTERNAL_ERROR);
2963 /* Check signature matches a type we sent */
2964 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
2965 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
2966 if (lu->sigalg == *sent_sigs
2967 && has_usable_cert(s, lu, lu->sig_idx))
2970 if (i == sent_sigslen) {
2973 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2974 SSL_F_TLS_CHOOSE_SIGALG,
2975 SSL_R_WRONG_SIGNATURE_TYPE);
2980 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2983 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2984 ERR_R_INTERNAL_ERROR);
2990 sig_idx = lu->sig_idx;
2991 s->s3.tmp.cert = &s->cert->pkeys[sig_idx];
2992 s->cert->key = s->s3.tmp.cert;
2993 s->s3.tmp.sigalg = lu;
2997 int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
2999 if (mode != TLSEXT_max_fragment_length_DISABLED
3000 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
3001 SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
3002 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
3006 ctx->ext.max_fragment_len_mode = mode;
3010 int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
3012 if (mode != TLSEXT_max_fragment_length_DISABLED
3013 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
3014 SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
3015 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
3019 ssl->ext.max_fragment_len_mode = mode;
3023 uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
3025 return session->ext.max_fragment_len_mode;
3029 * Helper functions for HMAC access with legacy support included.
3031 SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx)
3033 SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret));
3034 EVP_MAC *mac = NULL;
3038 #ifndef OPENSSL_NO_DEPRECATED_3_0
3039 if (ctx->ext.ticket_key_evp_cb == NULL
3040 && ctx->ext.ticket_key_cb != NULL) {
3041 ret->old_ctx = HMAC_CTX_new();
3042 if (ret->old_ctx == NULL)
3047 mac = EVP_MAC_fetch(ctx->libctx, "HMAC", NULL);
3048 if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL)
3053 EVP_MAC_CTX_free(ret->ctx);
3059 void ssl_hmac_free(SSL_HMAC *ctx)
3062 EVP_MAC_CTX_free(ctx->ctx);
3063 #ifndef OPENSSL_NO_DEPRECATED_3_0
3064 HMAC_CTX_free(ctx->old_ctx);
3070 #ifndef OPENSSL_NO_DEPRECATED_3_0
3071 HMAC_CTX *ssl_hmac_get0_HMAC_CTX(SSL_HMAC *ctx)
3073 return ctx->old_ctx;
3077 EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx)
3082 int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md)
3084 OSSL_PARAM params[3], *p = params;
3086 if (ctx->ctx != NULL) {
3087 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0);
3088 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key, len);
3089 *p = OSSL_PARAM_construct_end();
3090 if (EVP_MAC_CTX_set_params(ctx->ctx, params) && EVP_MAC_init(ctx->ctx))
3093 #ifndef OPENSSL_NO_DEPRECATED_3_0
3094 if (ctx->old_ctx != NULL)
3095 return HMAC_Init_ex(ctx->old_ctx, key, len,
3096 EVP_get_digestbyname(md), NULL);
3101 int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len)
3103 if (ctx->ctx != NULL)
3104 return EVP_MAC_update(ctx->ctx, data, len);
3105 #ifndef OPENSSL_NO_DEPRECATED_3_0
3106 if (ctx->old_ctx != NULL)
3107 return HMAC_Update(ctx->old_ctx, data, len);
3112 int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len,
3115 if (ctx->ctx != NULL)
3116 return EVP_MAC_final(ctx->ctx, md, len, max_size);
3117 #ifndef OPENSSL_NO_DEPRECATED_3_0
3118 if (ctx->old_ctx != NULL) {
3121 if (HMAC_Final(ctx->old_ctx, md, &l) > 0) {
3131 size_t ssl_hmac_size(const SSL_HMAC *ctx)
3133 if (ctx->ctx != NULL)
3134 return EVP_MAC_size(ctx->ctx);
3135 #ifndef OPENSSL_NO_DEPRECATED_3_0
3136 if (ctx->old_ctx != NULL)
3137 return HMAC_size(ctx->old_ctx);