2 * Copyright 1995-2017 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) */
170 static const unsigned char ecformats_default[] = {
171 TLSEXT_ECPOINTFORMAT_uncompressed,
172 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
173 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
176 /* The default curves */
177 static const uint16_t eccurves_default[] = {
178 29, /* X25519 (29) */
179 23, /* secp256r1 (23) */
180 25, /* secp521r1 (25) */
181 24, /* secp384r1 (24) */
184 static const uint16_t suiteb_curves[] = {
189 const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)
191 /* ECC curves from RFC 4492 and RFC 7027 */
192 if (group_id < 1 || group_id > OSSL_NELEM(nid_list))
194 return &nid_list[group_id - 1];
197 static uint16_t tls1_nid2group_id(int nid)
200 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
201 if (nid_list[i].nid == nid)
208 * Set *pgroups to the supported groups list and *pgroupslen to
209 * the number of groups supported.
211 void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
215 /* For Suite B mode only include P-256, P-384 */
216 switch (tls1_suiteb(s)) {
217 case SSL_CERT_FLAG_SUITEB_128_LOS:
218 *pgroups = suiteb_curves;
219 *pgroupslen = OSSL_NELEM(suiteb_curves);
222 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
223 *pgroups = suiteb_curves;
227 case SSL_CERT_FLAG_SUITEB_192_LOS:
228 *pgroups = suiteb_curves + 1;
233 if (s->ext.supportedgroups == NULL) {
234 *pgroups = eccurves_default;
235 *pgroupslen = OSSL_NELEM(eccurves_default);
237 *pgroups = s->ext.supportedgroups;
238 *pgroupslen = s->ext.supportedgroups_len;
244 /* See if curve is allowed by security callback */
245 int tls_curve_allowed(SSL *s, uint16_t curve, int op)
247 const TLS_GROUP_INFO *cinfo = tls1_group_id_lookup(curve);
248 unsigned char ctmp[2];
252 # ifdef OPENSSL_NO_EC2M
253 if (cinfo->flags & TLS_CURVE_CHAR2)
256 ctmp[0] = curve >> 8;
257 ctmp[1] = curve & 0xff;
258 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)ctmp);
261 /* Return 1 if "id" is in "list" */
262 static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
265 for (i = 0; i < listlen; i++)
272 * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
273 * if there is no match.
274 * For nmatch == -1, return number of matches
275 * For nmatch == -2, return the id of the group to use for
276 * a tmp key, or 0 if there is no match.
278 uint16_t tls1_shared_group(SSL *s, int nmatch)
280 const uint16_t *pref, *supp;
281 size_t num_pref, num_supp, i;
284 /* Can't do anything on client side */
288 if (tls1_suiteb(s)) {
290 * For Suite B ciphersuite determines curve: we already know
291 * these are acceptable due to previous checks.
293 unsigned long cid = s->s3->tmp.new_cipher->id;
295 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
296 return TLSEXT_curve_P_256;
297 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
298 return TLSEXT_curve_P_384;
299 /* Should never happen */
302 /* If not Suite B just return first preference shared curve */
306 * If server preference set, our groups are the preference order
307 * otherwise peer decides.
309 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
310 tls1_get_supported_groups(s, &pref, &num_pref);
311 tls1_get_peer_groups(s, &supp, &num_supp);
313 tls1_get_peer_groups(s, &pref, &num_pref);
314 tls1_get_supported_groups(s, &supp, &num_supp);
317 for (k = 0, i = 0; i < num_pref; i++) {
318 uint16_t id = pref[i];
320 if (!tls1_in_list(id, supp, num_supp)
321 || !tls_curve_allowed(s, id, SSL_SECOP_CURVE_SHARED))
329 /* Out of range (nmatch > k). */
333 int tls1_set_groups(uint16_t **pext, size_t *pextlen,
334 int *groups, size_t ngroups)
339 * Bitmap of groups included to detect duplicates: only works while group
342 unsigned long dup_list = 0;
343 glist = OPENSSL_malloc(ngroups * sizeof(*glist));
346 for (i = 0; i < ngroups; i++) {
347 unsigned long idmask;
349 /* TODO(TLS1.3): Convert for DH groups */
350 id = tls1_nid2group_id(groups[i]);
352 if (!id || (dup_list & idmask)) {
365 # define MAX_CURVELIST 28
369 int nid_arr[MAX_CURVELIST];
372 static int nid_cb(const char *elem, int len, void *arg)
374 nid_cb_st *narg = arg;
380 if (narg->nidcnt == MAX_CURVELIST)
382 if (len > (int)(sizeof(etmp) - 1))
384 memcpy(etmp, elem, len);
386 nid = EC_curve_nist2nid(etmp);
387 if (nid == NID_undef)
388 nid = OBJ_sn2nid(etmp);
389 if (nid == NID_undef)
390 nid = OBJ_ln2nid(etmp);
391 if (nid == NID_undef)
393 for (i = 0; i < narg->nidcnt; i++)
394 if (narg->nid_arr[i] == nid)
396 narg->nid_arr[narg->nidcnt++] = nid;
400 /* Set groups based on a colon separate list */
401 int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str)
405 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
409 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
411 /* Return group id of a key */
412 static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
414 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
419 grp = EC_KEY_get0_group(ec);
420 return tls1_nid2group_id(EC_GROUP_get_curve_name(grp));
423 /* Check a key is compatible with compression extension */
424 static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
428 unsigned char comp_id;
431 /* If not an EC key nothing to check */
432 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
434 ec = EVP_PKEY_get0_EC_KEY(pkey);
435 grp = EC_KEY_get0_group(ec);
437 /* Get required compression id */
438 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
439 comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
440 } else if (SSL_IS_TLS13(s)) {
441 /* Compression not allowed in TLS 1.3 */
444 int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp));
446 if (field_type == NID_X9_62_prime_field)
447 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
448 else if (field_type == NID_X9_62_characteristic_two_field)
449 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
454 * If point formats extension present check it, otherwise everything is
455 * supported (see RFC4492).
457 if (s->session->ext.ecpointformats == NULL)
460 for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
461 if (s->session->ext.ecpointformats[i] == comp_id)
467 /* Check a group id matches preferences */
468 int tls1_check_group_id(SSL *s, uint16_t group_id)
470 const uint16_t *groups;
476 /* Check for Suite B compliance */
477 if (tls1_suiteb(s) && s->s3->tmp.new_cipher != NULL) {
478 unsigned long cid = s->s3->tmp.new_cipher->id;
480 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
481 if (group_id != TLSEXT_curve_P_256)
483 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
484 if (group_id != TLSEXT_curve_P_384)
487 /* Should never happen */
492 /* Check group is one of our preferences */
493 tls1_get_supported_groups(s, &groups, &groups_len);
494 if (!tls1_in_list(group_id, groups, groups_len))
497 if (!tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
500 /* For clients, nothing more to check */
504 /* Check group is one of peers preferences */
505 tls1_get_peer_groups(s, &groups, &groups_len);
508 * RFC 4492 does not require the supported elliptic curves extension
509 * so if it is not sent we can just choose any curve.
510 * It is invalid to send an empty list in the supported groups
511 * extension, so groups_len == 0 always means no extension.
515 return tls1_in_list(group_id, groups, groups_len);
518 void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
522 * If we have a custom point format list use it otherwise use default
524 if (s->ext.ecpointformats) {
525 *pformats = s->ext.ecpointformats;
526 *num_formats = s->ext.ecpointformats_len;
528 *pformats = ecformats_default;
529 /* For Suite B we don't support char2 fields */
531 *num_formats = sizeof(ecformats_default) - 1;
533 *num_formats = sizeof(ecformats_default);
538 * Check cert parameters compatible with extensions: currently just checks EC
539 * certificates have compatible curves and compression.
541 static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
545 pkey = X509_get0_pubkey(x);
548 /* If not EC nothing to do */
549 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
551 /* Check compression */
552 if (!tls1_check_pkey_comp(s, pkey))
554 group_id = tls1_get_group_id(pkey);
555 if (!tls1_check_group_id(s, group_id))
558 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
561 if (check_ee_md && tls1_suiteb(s)) {
566 /* Check to see we have necessary signing algorithm */
567 if (group_id == TLSEXT_curve_P_256)
568 check_md = NID_ecdsa_with_SHA256;
569 else if (group_id == TLSEXT_curve_P_384)
570 check_md = NID_ecdsa_with_SHA384;
572 return 0; /* Should never happen */
573 for (i = 0; i < c->shared_sigalgslen; i++) {
574 if (check_md == c->shared_sigalgs[i]->sigandhash)
583 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
585 * @cid: Cipher ID we're considering using
587 * Checks that the kECDHE cipher suite we're considering using
588 * is compatible with the client extensions.
590 * Returns 0 when the cipher can't be used or 1 when it can.
592 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
594 /* If not Suite B just need a shared group */
596 return tls1_shared_group(s, 0) != 0;
598 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
601 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
602 return tls1_check_group_id(s, TLSEXT_curve_P_256);
603 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
604 return tls1_check_group_id(s, TLSEXT_curve_P_384);
611 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
616 #endif /* OPENSSL_NO_EC */
618 /* Default sigalg schemes */
619 static const uint16_t tls12_sigalgs[] = {
620 #ifndef OPENSSL_NO_EC
621 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
622 TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
623 TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
624 TLSEXT_SIGALG_ed25519,
627 TLSEXT_SIGALG_rsa_pss_sha256,
628 TLSEXT_SIGALG_rsa_pss_sha384,
629 TLSEXT_SIGALG_rsa_pss_sha512,
631 TLSEXT_SIGALG_rsa_pkcs1_sha256,
632 TLSEXT_SIGALG_rsa_pkcs1_sha384,
633 TLSEXT_SIGALG_rsa_pkcs1_sha512,
635 #ifndef OPENSSL_NO_EC
636 TLSEXT_SIGALG_ecdsa_sha224,
637 TLSEXT_SIGALG_ecdsa_sha1,
639 TLSEXT_SIGALG_rsa_pkcs1_sha224,
640 TLSEXT_SIGALG_rsa_pkcs1_sha1,
641 #ifndef OPENSSL_NO_DSA
642 TLSEXT_SIGALG_dsa_sha224,
643 TLSEXT_SIGALG_dsa_sha1,
645 TLSEXT_SIGALG_dsa_sha256,
646 TLSEXT_SIGALG_dsa_sha384,
647 TLSEXT_SIGALG_dsa_sha512
651 #ifndef OPENSSL_NO_EC
652 static const uint16_t suiteb_sigalgs[] = {
653 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
654 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
658 static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
659 #ifndef OPENSSL_NO_EC
660 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
661 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
662 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
663 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
664 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
665 NID_ecdsa_with_SHA384, NID_secp384r1},
666 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
667 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
668 NID_ecdsa_with_SHA512, NID_secp521r1},
669 {"ed25519", TLSEXT_SIGALG_ed25519,
670 NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
671 NID_undef, NID_undef},
672 {NULL, TLSEXT_SIGALG_ecdsa_sha224,
673 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
674 NID_ecdsa_with_SHA224, NID_undef},
675 {NULL, TLSEXT_SIGALG_ecdsa_sha1,
676 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
677 NID_ecdsa_with_SHA1, NID_undef},
679 {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256,
680 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
681 NID_undef, NID_undef},
682 {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384,
683 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
684 NID_undef, NID_undef},
685 {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512,
686 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
687 NID_undef, NID_undef},
688 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
689 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
690 NID_sha256WithRSAEncryption, NID_undef},
691 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
692 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
693 NID_sha384WithRSAEncryption, NID_undef},
694 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
695 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
696 NID_sha512WithRSAEncryption, NID_undef},
697 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
698 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
699 NID_sha224WithRSAEncryption, NID_undef},
700 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
701 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
702 NID_sha1WithRSAEncryption, NID_undef},
703 #ifndef OPENSSL_NO_DSA
704 {NULL, TLSEXT_SIGALG_dsa_sha256,
705 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
706 NID_dsa_with_SHA256, NID_undef},
707 {NULL, TLSEXT_SIGALG_dsa_sha384,
708 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
709 NID_undef, NID_undef},
710 {NULL, TLSEXT_SIGALG_dsa_sha512,
711 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
712 NID_undef, NID_undef},
713 {NULL, TLSEXT_SIGALG_dsa_sha224,
714 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
715 NID_undef, NID_undef},
716 {NULL, TLSEXT_SIGALG_dsa_sha1,
717 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
718 NID_dsaWithSHA1, NID_undef},
720 #ifndef OPENSSL_NO_GOST
721 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
722 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
723 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
724 NID_undef, NID_undef},
725 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
726 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
727 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
728 NID_undef, NID_undef},
729 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
730 NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
731 NID_id_GostR3410_2001, SSL_PKEY_GOST01,
732 NID_undef, NID_undef}
735 /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
736 static const SIGALG_LOOKUP legacy_rsa_sigalg = {
737 "rsa_pkcs1_md5_sha1", 0,
738 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
739 EVP_PKEY_RSA, SSL_PKEY_RSA,
744 * Default signature algorithm values used if signature algorithms not present.
745 * From RFC5246. Note: order must match certificate index order.
747 static const uint16_t tls_default_sigalg[] = {
748 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
749 0, /* SSL_PKEY_RSA_PSS_SIGN */
750 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
751 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
752 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
753 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
754 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */
755 0 /* SSL_PKEY_ED25519 */
758 /* Lookup TLS signature algorithm */
759 static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
762 const SIGALG_LOOKUP *s;
764 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
766 if (s->sigalg == sigalg)
771 /* Lookup hash: return 0 if invalid or not enabled */
772 int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
777 /* lu->hash == NID_undef means no associated digest */
778 if (lu->hash == NID_undef) {
781 md = ssl_md(lu->hash_idx);
791 * Return a signature algorithm for TLS < 1.2 where the signature type
792 * is fixed by the certificate type.
794 static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
800 /* Work out index corresponding to ciphersuite */
801 for (i = 0; i < SSL_PKEY_NUM; i++) {
802 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
804 if (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) {
810 idx = s->cert->key - s->cert->pkeys;
813 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
815 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
816 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
818 if (!tls1_lookup_md(lu, NULL))
822 return &legacy_rsa_sigalg;
824 /* Set peer sigalg based key type */
825 int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
828 const SIGALG_LOOKUP *lu;
830 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
832 lu = tls1_get_legacy_sigalg(s, idx);
835 s->s3->tmp.peer_sigalg = lu;
839 size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
842 * If Suite B mode use Suite B sigalgs only, ignore any other
845 #ifndef OPENSSL_NO_EC
846 switch (tls1_suiteb(s)) {
847 case SSL_CERT_FLAG_SUITEB_128_LOS:
848 *psigs = suiteb_sigalgs;
849 return OSSL_NELEM(suiteb_sigalgs);
851 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
852 *psigs = suiteb_sigalgs;
855 case SSL_CERT_FLAG_SUITEB_192_LOS:
856 *psigs = suiteb_sigalgs + 1;
861 * We use client_sigalgs (if not NULL) if we're a server
862 * and sending a certificate request or if we're a client and
863 * determining which shared algorithm to use.
865 if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
866 *psigs = s->cert->client_sigalgs;
867 return s->cert->client_sigalgslen;
868 } else if (s->cert->conf_sigalgs) {
869 *psigs = s->cert->conf_sigalgs;
870 return s->cert->conf_sigalgslen;
872 *psigs = tls12_sigalgs;
873 return OSSL_NELEM(tls12_sigalgs);
878 * Check signature algorithm is consistent with sent supported signature
879 * algorithms and if so set relevant digest and signature scheme in
882 int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
884 const uint16_t *sent_sigs;
885 const EVP_MD *md = NULL;
887 size_t sent_sigslen, i;
888 int pkeyid = EVP_PKEY_id(pkey);
889 const SIGALG_LOOKUP *lu;
891 /* Should never happen */
894 if (SSL_IS_TLS13(s)) {
895 /* Disallow DSA for TLS 1.3 */
896 if (pkeyid == EVP_PKEY_DSA) {
897 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
900 /* Only allow PSS for TLS 1.3 */
901 if (pkeyid == EVP_PKEY_RSA)
902 pkeyid = EVP_PKEY_RSA_PSS;
904 lu = tls1_lookup_sigalg(sig);
906 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
907 * is consistent with signature: RSA keys can be used for RSA-PSS
910 || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
911 || (pkeyid != lu->sig
912 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
913 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
916 #ifndef OPENSSL_NO_EC
917 if (pkeyid == EVP_PKEY_EC) {
919 /* Check point compression is permitted */
920 if (!tls1_check_pkey_comp(s, pkey)) {
921 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
922 SSL_R_ILLEGAL_POINT_COMPRESSION);
926 /* For TLS 1.3 or Suite B check curve matches signature algorithm */
927 if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
928 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
929 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
931 if (lu->curve != NID_undef && curve != lu->curve) {
932 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
936 if (!SSL_IS_TLS13(s)) {
937 /* Check curve matches extensions */
938 if (!tls1_check_group_id(s, tls1_get_group_id(pkey))) {
939 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
942 if (tls1_suiteb(s)) {
943 /* Check sigalg matches a permissible Suite B value */
944 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
945 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
946 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
947 SSL_R_WRONG_SIGNATURE_TYPE);
952 } else if (tls1_suiteb(s)) {
957 /* Check signature matches a type we sent */
958 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
959 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
960 if (sig == *sent_sigs)
963 /* Allow fallback to SHA1 if not strict mode */
964 if (i == sent_sigslen && (lu->hash != NID_sha1
965 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
966 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
969 if (!tls1_lookup_md(lu, &md)) {
970 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
975 * Make sure security callback allows algorithm. For historical
976 * reasons we have to pass the sigalg as a two byte char array.
978 sigalgstr[0] = (sig >> 8) & 0xff;
979 sigalgstr[1] = sig & 0xff;
980 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
981 EVP_MD_size(md) * 4, EVP_MD_type(md),
982 (void *)sigalgstr)) {
983 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
987 /* Store the sigalg the peer uses */
988 s->s3->tmp.peer_sigalg = lu;
992 int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
994 if (s->s3->tmp.peer_sigalg == NULL)
996 *pnid = s->s3->tmp.peer_sigalg->sig;
1001 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1002 * supported, doesn't appear in supported signature algorithms, isn't supported
1003 * by the enabled protocol versions or by the security level.
1005 * This function should only be used for checking which ciphers are supported
1008 * Call ssl_cipher_disabled() to check that it's enabled or not.
1010 void ssl_set_client_disabled(SSL *s)
1012 s->s3->tmp.mask_a = 0;
1013 s->s3->tmp.mask_k = 0;
1014 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1015 ssl_get_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
1016 #ifndef OPENSSL_NO_PSK
1017 /* with PSK there must be client callback set */
1018 if (!s->psk_client_callback) {
1019 s->s3->tmp.mask_a |= SSL_aPSK;
1020 s->s3->tmp.mask_k |= SSL_PSK;
1022 #endif /* OPENSSL_NO_PSK */
1023 #ifndef OPENSSL_NO_SRP
1024 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1025 s->s3->tmp.mask_a |= SSL_aSRP;
1026 s->s3->tmp.mask_k |= SSL_kSRP;
1032 * ssl_cipher_disabled - check that a cipher is disabled or not
1033 * @s: SSL connection that you want to use the cipher on
1034 * @c: cipher to check
1035 * @op: Security check that you want to do
1036 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
1038 * Returns 1 when it's disabled, 0 when enabled.
1040 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
1042 if (c->algorithm_mkey & s->s3->tmp.mask_k
1043 || c->algorithm_auth & s->s3->tmp.mask_a)
1045 if (s->s3->tmp.max_ver == 0)
1047 if (!SSL_IS_DTLS(s)) {
1048 int min_tls = c->min_tls;
1051 * For historical reasons we will allow ECHDE to be selected by a server
1052 * in SSLv3 if we are a client
1054 if (min_tls == TLS1_VERSION && ecdhe
1055 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
1056 min_tls = SSL3_VERSION;
1058 if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver))
1061 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
1062 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
1065 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1068 int tls_use_ticket(SSL *s)
1070 if ((s->options & SSL_OP_NO_TICKET))
1072 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1075 int tls1_set_server_sigalgs(SSL *s)
1080 /* Clear any shared signature algorithms */
1081 OPENSSL_free(s->cert->shared_sigalgs);
1082 s->cert->shared_sigalgs = NULL;
1083 s->cert->shared_sigalgslen = 0;
1084 /* Clear certificate validity flags */
1085 for (i = 0; i < SSL_PKEY_NUM; i++)
1086 s->s3->tmp.valid_flags[i] = 0;
1088 * If peer sent no signature algorithms check to see if we support
1089 * the default algorithm for each certificate type
1091 if (s->s3->tmp.peer_sigalgs == NULL) {
1092 const uint16_t *sent_sigs;
1093 size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1095 for (i = 0; i < SSL_PKEY_NUM; i++) {
1096 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
1101 /* Check default matches a type we sent */
1102 for (j = 0; j < sent_sigslen; j++) {
1103 if (lu->sigalg == sent_sigs[j]) {
1104 s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN;
1112 if (!tls1_process_sigalgs(s)) {
1113 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
1114 al = SSL_AD_INTERNAL_ERROR;
1117 if (s->cert->shared_sigalgs != NULL)
1119 /* Fatal error if no shared signature algorithms */
1120 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
1121 al = SSL_AD_HANDSHAKE_FAILURE;
1123 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1128 * Gets the ticket information supplied by the client if any.
1130 * hello: The parsed ClientHello data
1131 * ret: (output) on return, if a ticket was decrypted, then this is set to
1132 * point to the resulting session.
1134 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
1135 * ciphersuite, in which case we have no use for session tickets and one will
1136 * never be decrypted, nor will s->ext.ticket_expected be set to 1.
1139 * -1: fatal error, either from parsing or decrypting the ticket.
1140 * 0: no ticket was found (or was ignored, based on settings).
1141 * 1: a zero length extension was found, indicating that the client supports
1142 * session tickets but doesn't currently have one to offer.
1143 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
1144 * couldn't be decrypted because of a non-fatal error.
1145 * 3: a ticket was successfully decrypted and *ret was set.
1148 * Sets s->ext.ticket_expected to 1 if the server will have to issue
1149 * a new session ticket to the client because the client indicated support
1150 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1151 * a session ticket or we couldn't use the one it gave us, or if
1152 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1153 * Otherwise, s->ext.ticket_expected is set to 0.
1155 TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
1160 RAW_EXTENSION *ticketext;
1163 s->ext.ticket_expected = 0;
1166 * If tickets disabled or not supported by the protocol version
1167 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
1170 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
1173 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
1174 if (!ticketext->present)
1177 size = PACKET_remaining(&ticketext->data);
1180 * The client will accept a ticket but doesn't currently have
1183 s->ext.ticket_expected = 1;
1184 return TICKET_EMPTY;
1186 if (s->ext.session_secret_cb) {
1188 * Indicate that the ticket couldn't be decrypted rather than
1189 * generating the session from ticket now, trigger
1190 * abbreviated handshake based on external mechanism to
1191 * calculate the master secret later.
1193 return TICKET_NO_DECRYPT;
1196 retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
1197 hello->session_id, hello->session_id_len, ret);
1199 case TICKET_NO_DECRYPT:
1200 s->ext.ticket_expected = 1;
1201 return TICKET_NO_DECRYPT;
1203 case TICKET_SUCCESS:
1204 return TICKET_SUCCESS;
1206 case TICKET_SUCCESS_RENEW:
1207 s->ext.ticket_expected = 1;
1208 return TICKET_SUCCESS;
1211 return TICKET_FATAL_ERR_OTHER;
1216 * tls_decrypt_ticket attempts to decrypt a session ticket.
1218 * etick: points to the body of the session ticket extension.
1219 * eticklen: the length of the session tickets extension.
1220 * sess_id: points at the session ID.
1221 * sesslen: the length of the session ID.
1222 * psess: (output) on return, if a ticket was decrypted, then this is set to
1223 * point to the resulting session.
1225 TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1226 size_t eticklen, const unsigned char *sess_id,
1227 size_t sesslen, SSL_SESSION **psess)
1230 unsigned char *sdec;
1231 const unsigned char *p;
1232 int slen, renew_ticket = 0, declen;
1233 TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER;
1235 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1236 HMAC_CTX *hctx = NULL;
1237 EVP_CIPHER_CTX *ctx;
1238 SSL_CTX *tctx = s->session_ctx;
1240 /* Initialize session ticket encryption and HMAC contexts */
1241 hctx = HMAC_CTX_new();
1243 return TICKET_FATAL_ERR_MALLOC;
1244 ctx = EVP_CIPHER_CTX_new();
1246 ret = TICKET_FATAL_ERR_MALLOC;
1249 if (tctx->ext.ticket_key_cb) {
1250 unsigned char *nctick = (unsigned char *)etick;
1251 int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16,
1256 ret = TICKET_NO_DECRYPT;
1262 /* Check key name matches */
1263 if (memcmp(etick, tctx->ext.tick_key_name,
1264 sizeof(tctx->ext.tick_key_name)) != 0) {
1265 ret = TICKET_NO_DECRYPT;
1268 if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
1269 sizeof(tctx->ext.tick_hmac_key),
1270 EVP_sha256(), NULL) <= 0
1271 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
1272 tctx->ext.tick_aes_key,
1274 + sizeof(tctx->ext.tick_key_name)) <= 0) {
1279 * Attempt to process session ticket, first conduct sanity and integrity
1282 mlen = HMAC_size(hctx);
1286 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1288 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
1289 ret = TICKET_NO_DECRYPT;
1293 /* Check HMAC of encrypted ticket */
1294 if (HMAC_Update(hctx, etick, eticklen) <= 0
1295 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
1298 HMAC_CTX_free(hctx);
1299 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
1300 EVP_CIPHER_CTX_free(ctx);
1301 return TICKET_NO_DECRYPT;
1303 /* Attempt to decrypt session data */
1304 /* Move p after IV to start of encrypted ticket, update length */
1305 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1306 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1307 sdec = OPENSSL_malloc(eticklen);
1308 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
1309 (int)eticklen) <= 0) {
1310 EVP_CIPHER_CTX_free(ctx);
1312 return TICKET_FATAL_ERR_OTHER;
1314 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
1315 EVP_CIPHER_CTX_free(ctx);
1317 return TICKET_NO_DECRYPT;
1320 EVP_CIPHER_CTX_free(ctx);
1324 sess = d2i_SSL_SESSION(NULL, &p, slen);
1328 /* Some additional consistency checks */
1329 if (slen != 0 || sess->session_id_length != 0) {
1330 SSL_SESSION_free(sess);
1331 return TICKET_NO_DECRYPT;
1334 * The session ID, if non-empty, is used by some clients to detect
1335 * that the ticket has been accepted. So we copy it to the session
1336 * structure. If it is empty set length to zero as required by
1340 memcpy(sess->session_id, sess_id, sesslen);
1341 sess->session_id_length = sesslen;
1344 return TICKET_SUCCESS_RENEW;
1346 return TICKET_SUCCESS;
1350 * For session parse failure, indicate that we need to send a new ticket.
1352 return TICKET_NO_DECRYPT;
1354 EVP_CIPHER_CTX_free(ctx);
1355 HMAC_CTX_free(hctx);
1359 /* Check to see if a signature algorithm is allowed */
1360 static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu)
1362 unsigned char sigalgstr[2];
1365 /* See if sigalgs is recognised and if hash is enabled */
1366 if (!tls1_lookup_md(lu, NULL))
1368 /* DSA is not allowed in TLS 1.3 */
1369 if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
1371 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
1372 if (!s->server && !SSL_IS_DTLS(s) && s->s3->tmp.min_ver >= TLS1_3_VERSION
1373 && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
1374 || lu->hash_idx == SSL_MD_MD5_IDX
1375 || lu->hash_idx == SSL_MD_SHA224_IDX))
1377 /* See if public key algorithm allowed */
1378 if (ssl_cert_is_disabled(lu->sig_idx))
1380 if (lu->hash == NID_undef)
1382 /* Security bits: half digest bits */
1383 secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4;
1384 /* Finally see if security callback allows it */
1385 sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
1386 sigalgstr[1] = lu->sigalg & 0xff;
1387 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
1391 * Get a mask of disabled public key algorithms based on supported signature
1392 * algorithms. For example if no signature algorithm supports RSA then RSA is
1396 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
1398 const uint16_t *sigalgs;
1399 size_t i, sigalgslen;
1400 uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
1402 * Go through all signature algorithms seeing if we support any
1405 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
1406 for (i = 0; i < sigalgslen; i++, sigalgs++) {
1407 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs);
1408 const SSL_CERT_LOOKUP *clu;
1413 clu = ssl_cert_lookup_by_idx(lu->sig_idx);
1417 /* If algorithm is disabled see if we can enable it */
1418 if ((clu->amask & disabled_mask) != 0
1419 && tls12_sigalg_allowed(s, op, lu))
1420 disabled_mask &= ~clu->amask;
1422 *pmask_a |= disabled_mask;
1425 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
1426 const uint16_t *psig, size_t psiglen)
1431 for (i = 0; i < psiglen; i++, psig++) {
1432 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig);
1434 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
1436 if (!WPACKET_put_bytes_u16(pkt, *psig))
1439 * If TLS 1.3 must have at least one valid TLS 1.3 message
1440 * signing algorithm: i.e. neither RSA nor SHA1/SHA224
1442 if (rv == 0 && (!SSL_IS_TLS13(s)
1443 || (lu->sig != EVP_PKEY_RSA
1444 && lu->hash != NID_sha1
1445 && lu->hash != NID_sha224)))
1449 SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
1453 /* Given preference and allowed sigalgs set shared sigalgs */
1454 static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
1455 const uint16_t *pref, size_t preflen,
1456 const uint16_t *allow, size_t allowlen)
1458 const uint16_t *ptmp, *atmp;
1459 size_t i, j, nmatch = 0;
1460 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
1461 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp);
1463 /* Skip disabled hashes or signature algorithms */
1464 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
1466 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
1467 if (*ptmp == *atmp) {
1478 /* Set shared signature algorithms for SSL structures */
1479 static int tls1_set_shared_sigalgs(SSL *s)
1481 const uint16_t *pref, *allow, *conf;
1482 size_t preflen, allowlen, conflen;
1484 const SIGALG_LOOKUP **salgs = NULL;
1486 unsigned int is_suiteb = tls1_suiteb(s);
1488 OPENSSL_free(c->shared_sigalgs);
1489 c->shared_sigalgs = NULL;
1490 c->shared_sigalgslen = 0;
1491 /* If client use client signature algorithms if not NULL */
1492 if (!s->server && c->client_sigalgs && !is_suiteb) {
1493 conf = c->client_sigalgs;
1494 conflen = c->client_sigalgslen;
1495 } else if (c->conf_sigalgs && !is_suiteb) {
1496 conf = c->conf_sigalgs;
1497 conflen = c->conf_sigalgslen;
1499 conflen = tls12_get_psigalgs(s, 0, &conf);
1500 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
1503 allow = s->s3->tmp.peer_sigalgs;
1504 allowlen = s->s3->tmp.peer_sigalgslen;
1508 pref = s->s3->tmp.peer_sigalgs;
1509 preflen = s->s3->tmp.peer_sigalgslen;
1511 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
1513 salgs = OPENSSL_malloc(nmatch * sizeof(*salgs));
1516 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
1520 c->shared_sigalgs = salgs;
1521 c->shared_sigalgslen = nmatch;
1525 int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
1531 size = PACKET_remaining(pkt);
1533 /* Invalid data length */
1534 if (size == 0 || (size & 1) != 0)
1539 buf = OPENSSL_malloc(size * sizeof(*buf));
1542 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
1550 OPENSSL_free(*pdest);
1557 int tls1_save_sigalgs(SSL *s, PACKET *pkt)
1559 /* Extension ignored for inappropriate versions */
1560 if (!SSL_USE_SIGALGS(s))
1562 /* Should never happen */
1563 if (s->cert == NULL)
1566 return tls1_save_u16(pkt, &s->s3->tmp.peer_sigalgs,
1567 &s->s3->tmp.peer_sigalgslen);
1572 /* Set preferred digest for each key type */
1574 int tls1_process_sigalgs(SSL *s)
1577 uint32_t *pvalid = s->s3->tmp.valid_flags;
1580 if (!tls1_set_shared_sigalgs(s))
1583 for (i = 0; i < SSL_PKEY_NUM; i++)
1586 for (i = 0; i < c->shared_sigalgslen; i++) {
1587 const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i];
1588 int idx = sigptr->sig_idx;
1590 /* Ignore PKCS1 based sig algs in TLSv1.3 */
1591 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
1593 /* If not disabled indicate we can explicitly sign */
1594 if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
1595 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
1600 int SSL_get_sigalgs(SSL *s, int idx,
1601 int *psign, int *phash, int *psignhash,
1602 unsigned char *rsig, unsigned char *rhash)
1604 uint16_t *psig = s->s3->tmp.peer_sigalgs;
1605 size_t numsigalgs = s->s3->tmp.peer_sigalgslen;
1606 if (psig == NULL || numsigalgs > INT_MAX)
1609 const SIGALG_LOOKUP *lu;
1611 if (idx >= (int)numsigalgs)
1615 *rhash = (unsigned char)((*psig >> 8) & 0xff);
1617 *rsig = (unsigned char)(*psig & 0xff);
1618 lu = tls1_lookup_sigalg(*psig);
1620 *psign = lu != NULL ? lu->sig : NID_undef;
1622 *phash = lu != NULL ? lu->hash : NID_undef;
1623 if (psignhash != NULL)
1624 *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
1626 return (int)numsigalgs;
1629 int SSL_get_shared_sigalgs(SSL *s, int idx,
1630 int *psign, int *phash, int *psignhash,
1631 unsigned char *rsig, unsigned char *rhash)
1633 const SIGALG_LOOKUP *shsigalgs;
1634 if (s->cert->shared_sigalgs == NULL
1636 || idx >= (int)s->cert->shared_sigalgslen
1637 || s->cert->shared_sigalgslen > INT_MAX)
1639 shsigalgs = s->cert->shared_sigalgs[idx];
1641 *phash = shsigalgs->hash;
1643 *psign = shsigalgs->sig;
1644 if (psignhash != NULL)
1645 *psignhash = shsigalgs->sigandhash;
1647 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
1649 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
1650 return (int)s->cert->shared_sigalgslen;
1653 /* Maximum possible number of unique entries in sigalgs array */
1654 #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
1658 int sigalgs[TLS_MAX_SIGALGCNT];
1661 static void get_sigorhash(int *psig, int *phash, const char *str)
1663 if (strcmp(str, "RSA") == 0) {
1664 *psig = EVP_PKEY_RSA;
1665 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
1666 *psig = EVP_PKEY_RSA_PSS;
1667 } else if (strcmp(str, "DSA") == 0) {
1668 *psig = EVP_PKEY_DSA;
1669 } else if (strcmp(str, "ECDSA") == 0) {
1670 *psig = EVP_PKEY_EC;
1672 *phash = OBJ_sn2nid(str);
1673 if (*phash == NID_undef)
1674 *phash = OBJ_ln2nid(str);
1677 /* Maximum length of a signature algorithm string component */
1678 #define TLS_MAX_SIGSTRING_LEN 40
1680 static int sig_cb(const char *elem, int len, void *arg)
1682 sig_cb_st *sarg = arg;
1684 char etmp[TLS_MAX_SIGSTRING_LEN], *p;
1685 int sig_alg = NID_undef, hash_alg = NID_undef;
1688 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
1690 if (len > (int)(sizeof(etmp) - 1))
1692 memcpy(etmp, elem, len);
1694 p = strchr(etmp, '+');
1695 /* See if we have a match for TLS 1.3 names */
1697 const SIGALG_LOOKUP *s;
1699 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1701 if (s->name != NULL && strcmp(etmp, s->name) == 0) {
1712 get_sigorhash(&sig_alg, &hash_alg, etmp);
1713 get_sigorhash(&sig_alg, &hash_alg, p);
1716 if (sig_alg == NID_undef || (p != NULL && hash_alg == NID_undef))
1719 for (i = 0; i < sarg->sigalgcnt; i += 2) {
1720 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
1723 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
1724 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
1729 * Set supported signature algorithms based on a colon separated list of the
1730 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
1732 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
1736 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
1740 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
1743 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
1745 uint16_t *sigalgs, *sptr;
1750 sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs));
1751 if (sigalgs == NULL)
1753 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
1755 const SIGALG_LOOKUP *curr;
1756 int md_id = *psig_nids++;
1757 int sig_id = *psig_nids++;
1759 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
1761 if (curr->hash == md_id && curr->sig == sig_id) {
1762 *sptr++ = curr->sigalg;
1767 if (j == OSSL_NELEM(sigalg_lookup_tbl))
1772 OPENSSL_free(c->client_sigalgs);
1773 c->client_sigalgs = sigalgs;
1774 c->client_sigalgslen = salglen / 2;
1776 OPENSSL_free(c->conf_sigalgs);
1777 c->conf_sigalgs = sigalgs;
1778 c->conf_sigalgslen = salglen / 2;
1784 OPENSSL_free(sigalgs);
1788 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
1792 if (default_nid == -1)
1794 sig_nid = X509_get_signature_nid(x);
1796 return sig_nid == default_nid ? 1 : 0;
1797 for (i = 0; i < c->shared_sigalgslen; i++)
1798 if (sig_nid == c->shared_sigalgs[i]->sigandhash)
1803 /* Check to see if a certificate issuer name matches list of CA names */
1804 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
1808 nm = X509_get_issuer_name(x);
1809 for (i = 0; i < sk_X509_NAME_num(names); i++) {
1810 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
1817 * Check certificate chain is consistent with TLS extensions and is usable by
1818 * server. This servers two purposes: it allows users to check chains before
1819 * passing them to the server and it allows the server to check chains before
1820 * attempting to use them.
1823 /* Flags which need to be set for a certificate when strict mode not set */
1825 #define CERT_PKEY_VALID_FLAGS \
1826 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
1827 /* Strict mode flags */
1828 #define CERT_PKEY_STRICT_FLAGS \
1829 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
1830 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
1832 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
1837 int check_flags = 0, strict_mode;
1838 CERT_PKEY *cpk = NULL;
1841 unsigned int suiteb_flags = tls1_suiteb(s);
1842 /* idx == -1 means checking server chains */
1844 /* idx == -2 means checking client certificate chains */
1847 idx = (int)(cpk - c->pkeys);
1849 cpk = c->pkeys + idx;
1850 pvalid = s->s3->tmp.valid_flags + idx;
1852 pk = cpk->privatekey;
1854 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
1855 /* If no cert or key, forget it */
1864 if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
1867 pvalid = s->s3->tmp.valid_flags + idx;
1869 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
1870 check_flags = CERT_PKEY_STRICT_FLAGS;
1872 check_flags = CERT_PKEY_VALID_FLAGS;
1879 check_flags |= CERT_PKEY_SUITEB;
1880 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
1881 if (ok == X509_V_OK)
1882 rv |= CERT_PKEY_SUITEB;
1883 else if (!check_flags)
1888 * Check all signature algorithms are consistent with signature
1889 * algorithms extension if TLS 1.2 or later and strict mode.
1891 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
1894 if (s->s3->tmp.peer_sigalgs)
1896 /* If no sigalgs extension use defaults from RFC5246 */
1900 rsign = EVP_PKEY_RSA;
1901 default_nid = NID_sha1WithRSAEncryption;
1904 case SSL_PKEY_DSA_SIGN:
1905 rsign = EVP_PKEY_DSA;
1906 default_nid = NID_dsaWithSHA1;
1910 rsign = EVP_PKEY_EC;
1911 default_nid = NID_ecdsa_with_SHA1;
1914 case SSL_PKEY_GOST01:
1915 rsign = NID_id_GostR3410_2001;
1916 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
1919 case SSL_PKEY_GOST12_256:
1920 rsign = NID_id_GostR3410_2012_256;
1921 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
1924 case SSL_PKEY_GOST12_512:
1925 rsign = NID_id_GostR3410_2012_512;
1926 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
1935 * If peer sent no signature algorithms extension and we have set
1936 * preferred signature algorithms check we support sha1.
1938 if (default_nid > 0 && c->conf_sigalgs) {
1940 const uint16_t *p = c->conf_sigalgs;
1941 for (j = 0; j < c->conf_sigalgslen; j++, p++) {
1942 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
1944 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
1947 if (j == c->conf_sigalgslen) {
1954 /* Check signature algorithm of each cert in chain */
1955 if (!tls1_check_sig_alg(c, x, default_nid)) {
1959 rv |= CERT_PKEY_EE_SIGNATURE;
1960 rv |= CERT_PKEY_CA_SIGNATURE;
1961 for (i = 0; i < sk_X509_num(chain); i++) {
1962 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
1964 rv &= ~CERT_PKEY_CA_SIGNATURE;
1971 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
1972 else if (check_flags)
1973 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
1975 /* Check cert parameters are consistent */
1976 if (tls1_check_cert_param(s, x, 1))
1977 rv |= CERT_PKEY_EE_PARAM;
1978 else if (!check_flags)
1981 rv |= CERT_PKEY_CA_PARAM;
1982 /* In strict mode check rest of chain too */
1983 else if (strict_mode) {
1984 rv |= CERT_PKEY_CA_PARAM;
1985 for (i = 0; i < sk_X509_num(chain); i++) {
1986 X509 *ca = sk_X509_value(chain, i);
1987 if (!tls1_check_cert_param(s, ca, 0)) {
1989 rv &= ~CERT_PKEY_CA_PARAM;
1996 if (!s->server && strict_mode) {
1997 STACK_OF(X509_NAME) *ca_dn;
1999 switch (EVP_PKEY_id(pk)) {
2001 check_type = TLS_CT_RSA_SIGN;
2004 check_type = TLS_CT_DSS_SIGN;
2007 check_type = TLS_CT_ECDSA_SIGN;
2011 const uint8_t *ctypes = s->s3->tmp.ctype;
2014 for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) {
2015 if (*ctypes == check_type) {
2016 rv |= CERT_PKEY_CERT_TYPE;
2020 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
2023 rv |= CERT_PKEY_CERT_TYPE;
2026 ca_dn = s->s3->tmp.peer_ca_names;
2028 if (!sk_X509_NAME_num(ca_dn))
2029 rv |= CERT_PKEY_ISSUER_NAME;
2031 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2032 if (ssl_check_ca_name(ca_dn, x))
2033 rv |= CERT_PKEY_ISSUER_NAME;
2035 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2036 for (i = 0; i < sk_X509_num(chain); i++) {
2037 X509 *xtmp = sk_X509_value(chain, i);
2038 if (ssl_check_ca_name(ca_dn, xtmp)) {
2039 rv |= CERT_PKEY_ISSUER_NAME;
2044 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
2047 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
2049 if (!check_flags || (rv & check_flags) == check_flags)
2050 rv |= CERT_PKEY_VALID;
2054 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2055 rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
2057 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
2060 * When checking a CERT_PKEY structure all flags are irrelevant if the
2064 if (rv & CERT_PKEY_VALID) {
2067 /* Preserve sign and explicit sign flag, clear rest */
2068 *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
2075 /* Set validity of certificates in an SSL structure */
2076 void tls1_set_cert_validity(SSL *s)
2078 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
2079 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
2080 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
2081 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
2082 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
2083 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
2084 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
2085 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
2088 /* User level utility function to check a chain is suitable */
2089 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
2091 return tls1_check_chain(s, x, pk, chain, -1);
2094 #ifndef OPENSSL_NO_DH
2095 DH *ssl_get_auto_dh(SSL *s)
2097 int dh_secbits = 80;
2098 if (s->cert->dh_tmp_auto == 2)
2099 return DH_get_1024_160();
2100 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
2101 if (s->s3->tmp.new_cipher->strength_bits == 256)
2106 if (s->s3->tmp.cert == NULL)
2108 dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey);
2111 if (dh_secbits >= 128) {
2119 if (dh_secbits >= 192)
2120 p = BN_get_rfc3526_prime_8192(NULL);
2122 p = BN_get_rfc3526_prime_3072(NULL);
2123 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
2131 if (dh_secbits >= 112)
2132 return DH_get_2048_224();
2133 return DH_get_1024_160();
2137 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2140 EVP_PKEY *pkey = X509_get0_pubkey(x);
2143 * If no parameters this will return -1 and fail using the default
2144 * security callback for any non-zero security level. This will
2145 * reject keys which omit parameters but this only affects DSA and
2146 * omission of parameters is never (?) done in practice.
2148 secbits = EVP_PKEY_security_bits(pkey);
2151 return ssl_security(s, op, secbits, 0, x);
2153 return ssl_ctx_security(ctx, op, secbits, 0, x);
2156 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2158 /* Lookup signature algorithm digest */
2159 int secbits, nid, pknid;
2160 /* Don't check signature if self signed */
2161 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
2163 if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
2165 /* If digest NID not defined use signature NID */
2166 if (nid == NID_undef)
2169 return ssl_security(s, op, secbits, nid, x);
2171 return ssl_ctx_security(ctx, op, secbits, nid, x);
2174 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
2177 vfy = SSL_SECOP_PEER;
2179 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
2180 return SSL_R_EE_KEY_TOO_SMALL;
2182 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
2183 return SSL_R_CA_KEY_TOO_SMALL;
2185 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
2186 return SSL_R_CA_MD_TOO_WEAK;
2191 * Check security of a chain, if |sk| includes the end entity certificate then
2192 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
2193 * one to the peer. Return values: 1 if ok otherwise error code to use
2196 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
2198 int rv, start_idx, i;
2200 x = sk_X509_value(sk, 0);
2205 rv = ssl_security_cert(s, NULL, x, vfy, 1);
2209 for (i = start_idx; i < sk_X509_num(sk); i++) {
2210 x = sk_X509_value(sk, i);
2211 rv = ssl_security_cert(s, NULL, x, vfy, 0);
2219 * For TLS 1.2 servers check if we have a certificate which can be used
2220 * with the signature algorithm "lu" and return index of certificate.
2223 static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
2225 int sig_idx = lu->sig_idx;
2226 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
2228 /* If not recognised or not supported by cipher mask it is not suitable */
2229 if (clu == NULL || !(clu->amask & s->s3->tmp.new_cipher->algorithm_auth))
2232 /* If PSS and we have no PSS cert use RSA */
2233 if (sig_idx == SSL_PKEY_RSA_PSS_SIGN && !ssl_has_cert(s, sig_idx))
2234 sig_idx = SSL_PKEY_RSA;
2236 return s->s3->tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
2240 * Choose an appropriate signature algorithm based on available certificates
2241 * Sets chosen certificate and signature algorithm.
2243 * For servers if we fail to find a required certificate it is a fatal error
2244 * and an appropriate error code is set and the TLS alert set in *al.
2246 * For clients al is set to NULL. If a certificate is not suitable it is not
2247 * a fatal error: we will either try another certificate or not present one
2248 * to the server. In this case no error is set.
2250 int tls_choose_sigalg(SSL *s, int *al)
2252 const SIGALG_LOOKUP *lu = NULL;
2255 s->s3->tmp.cert = NULL;
2256 s->s3->tmp.sigalg = NULL;
2258 if (SSL_IS_TLS13(s)) {
2260 #ifndef OPENSSL_NO_EC
2261 int curve = -1, skip_ec = 0;
2264 /* Look for a certificate matching shared sigalgs */
2265 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2266 lu = s->cert->shared_sigalgs[i];
2268 /* Skip SHA1, SHA224, DSA and RSA if not PSS */
2269 if (lu->hash == NID_sha1
2270 || lu->hash == NID_sha224
2271 || lu->sig == EVP_PKEY_DSA
2272 || lu->sig == EVP_PKEY_RSA)
2274 if (!tls1_lookup_md(lu, NULL))
2276 if (!ssl_has_cert(s, lu->sig_idx)) {
2277 if (lu->sig_idx != SSL_PKEY_RSA_PSS_SIGN
2278 || !ssl_has_cert(s, SSL_PKEY_RSA))
2280 sig_idx = SSL_PKEY_RSA;
2282 if (lu->sig == EVP_PKEY_EC) {
2283 #ifndef OPENSSL_NO_EC
2285 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2287 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2288 if (EC_KEY_get_conv_form(ec)
2289 != POINT_CONVERSION_UNCOMPRESSED)
2292 if (skip_ec || (lu->curve != NID_undef && curve != lu->curve))
2300 if (i == s->cert->shared_sigalgslen) {
2303 *al = SSL_AD_HANDSHAKE_FAILURE;
2304 SSLerr(SSL_F_TLS_CHOOSE_SIGALG,
2305 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2309 /* If ciphersuite doesn't require a cert nothing to do */
2310 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aCERT))
2312 if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
2315 if (SSL_USE_SIGALGS(s)) {
2316 if (s->s3->tmp.peer_sigalgs != NULL) {
2318 #ifndef OPENSSL_NO_EC
2321 /* For Suite B need to match signature algorithm to curve */
2322 if (tls1_suiteb(s)) {
2323 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2324 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2331 * Find highest preference signature algorithm matching
2334 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2335 lu = s->cert->shared_sigalgs[i];
2338 if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
2341 int cc_idx = s->cert->key - s->cert->pkeys;
2343 sig_idx = lu->sig_idx;
2344 if (cc_idx != sig_idx) {
2345 if (sig_idx != SSL_PKEY_RSA_PSS_SIGN
2346 || cc_idx != SSL_PKEY_RSA)
2348 sig_idx = SSL_PKEY_RSA;
2351 #ifndef OPENSSL_NO_EC
2352 if (curve == -1 || lu->curve == curve)
2356 if (i == s->cert->shared_sigalgslen) {
2359 *al = SSL_AD_INTERNAL_ERROR;
2360 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2365 * If we have no sigalg use defaults
2367 const uint16_t *sent_sigs;
2368 size_t sent_sigslen, i;
2370 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2373 *al = SSL_AD_INTERNAL_ERROR;
2374 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2378 /* Check signature matches a type we sent */
2379 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
2380 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
2381 if (lu->sigalg == *sent_sigs)
2384 if (i == sent_sigslen) {
2387 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
2388 *al = SSL_AD_ILLEGAL_PARAMETER;
2393 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2396 *al = SSL_AD_INTERNAL_ERROR;
2397 SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR);
2403 sig_idx = lu->sig_idx;
2404 s->s3->tmp.cert = &s->cert->pkeys[sig_idx];
2405 s->cert->key = s->s3->tmp.cert;
2406 s->s3->tmp.sigalg = lu;
2410 int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
2412 if (mode != TLSEXT_max_fragment_length_DISABLED
2413 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
2414 SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
2415 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
2419 ctx->ext.max_fragment_len_mode = mode;
2423 int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
2425 if (mode != TLSEXT_max_fragment_length_DISABLED
2426 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
2427 SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
2428 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
2432 ssl->ext.max_fragment_len_mode = mode;
2436 uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
2438 return session->ext.max_fragment_len_mode;