2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/hmac.h>
15 #include <openssl/ocsp.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/dh.h>
19 #include <openssl/bn.h>
20 #include "internal/nelem.h"
22 #include <openssl/ct.h>
24 SSL3_ENC_METHOD
const TLSv1_enc_data
= {
28 tls1_generate_master_secret
,
29 tls1_change_cipher_state
,
30 tls1_final_finish_mac
,
31 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
32 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
34 tls1_export_keying_material
,
36 ssl3_set_handshake_header
,
37 tls_close_construct_packet
,
41 SSL3_ENC_METHOD
const TLSv1_1_enc_data
= {
45 tls1_generate_master_secret
,
46 tls1_change_cipher_state
,
47 tls1_final_finish_mac
,
48 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
49 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
51 tls1_export_keying_material
,
52 SSL_ENC_FLAG_EXPLICIT_IV
,
53 ssl3_set_handshake_header
,
54 tls_close_construct_packet
,
58 SSL3_ENC_METHOD
const TLSv1_2_enc_data
= {
62 tls1_generate_master_secret
,
63 tls1_change_cipher_state
,
64 tls1_final_finish_mac
,
65 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
66 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
68 tls1_export_keying_material
,
69 SSL_ENC_FLAG_EXPLICIT_IV
| SSL_ENC_FLAG_SIGALGS
| SSL_ENC_FLAG_SHA256_PRF
70 | SSL_ENC_FLAG_TLS1_2_CIPHERS
,
71 ssl3_set_handshake_header
,
72 tls_close_construct_packet
,
76 SSL3_ENC_METHOD
const TLSv1_3_enc_data
= {
79 tls13_setup_key_block
,
80 tls13_generate_master_secret
,
81 tls13_change_cipher_state
,
82 tls13_final_finish_mac
,
83 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
84 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
86 tls13_export_keying_material
,
87 SSL_ENC_FLAG_SIGALGS
| SSL_ENC_FLAG_SHA256_PRF
,
88 ssl3_set_handshake_header
,
89 tls_close_construct_packet
,
93 long tls1_default_timeout(void)
96 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
97 * http, the cache would over fill
106 if (!s
->method
->ssl_clear(s
))
112 void tls1_free(SSL
*s
)
114 OPENSSL_free(s
->ext
.session_ticket
);
118 int tls1_clear(SSL
*s
)
123 if (s
->method
->version
== TLS_ANY_VERSION
)
124 s
->version
= TLS_MAX_VERSION
;
126 s
->version
= s
->method
->version
;
131 #ifndef OPENSSL_NO_EC
134 * Table of curve information.
135 * Do not delete entries or reorder this array! It is used as a lookup
136 * table: the index of each entry is one less than the TLS curve id.
138 static const TLS_GROUP_INFO nid_list
[] = {
139 {NID_sect163k1
, 80, TLS_CURVE_CHAR2
}, /* sect163k1 (1) */
140 {NID_sect163r1
, 80, TLS_CURVE_CHAR2
}, /* sect163r1 (2) */
141 {NID_sect163r2
, 80, TLS_CURVE_CHAR2
}, /* sect163r2 (3) */
142 {NID_sect193r1
, 80, TLS_CURVE_CHAR2
}, /* sect193r1 (4) */
143 {NID_sect193r2
, 80, TLS_CURVE_CHAR2
}, /* sect193r2 (5) */
144 {NID_sect233k1
, 112, TLS_CURVE_CHAR2
}, /* sect233k1 (6) */
145 {NID_sect233r1
, 112, TLS_CURVE_CHAR2
}, /* sect233r1 (7) */
146 {NID_sect239k1
, 112, TLS_CURVE_CHAR2
}, /* sect239k1 (8) */
147 {NID_sect283k1
, 128, TLS_CURVE_CHAR2
}, /* sect283k1 (9) */
148 {NID_sect283r1
, 128, TLS_CURVE_CHAR2
}, /* sect283r1 (10) */
149 {NID_sect409k1
, 192, TLS_CURVE_CHAR2
}, /* sect409k1 (11) */
150 {NID_sect409r1
, 192, TLS_CURVE_CHAR2
}, /* sect409r1 (12) */
151 {NID_sect571k1
, 256, TLS_CURVE_CHAR2
}, /* sect571k1 (13) */
152 {NID_sect571r1
, 256, TLS_CURVE_CHAR2
}, /* sect571r1 (14) */
153 {NID_secp160k1
, 80, TLS_CURVE_PRIME
}, /* secp160k1 (15) */
154 {NID_secp160r1
, 80, TLS_CURVE_PRIME
}, /* secp160r1 (16) */
155 {NID_secp160r2
, 80, TLS_CURVE_PRIME
}, /* secp160r2 (17) */
156 {NID_secp192k1
, 80, TLS_CURVE_PRIME
}, /* secp192k1 (18) */
157 {NID_X9_62_prime192v1
, 80, TLS_CURVE_PRIME
}, /* secp192r1 (19) */
158 {NID_secp224k1
, 112, TLS_CURVE_PRIME
}, /* secp224k1 (20) */
159 {NID_secp224r1
, 112, TLS_CURVE_PRIME
}, /* secp224r1 (21) */
160 {NID_secp256k1
, 128, TLS_CURVE_PRIME
}, /* secp256k1 (22) */
161 {NID_X9_62_prime256v1
, 128, TLS_CURVE_PRIME
}, /* secp256r1 (23) */
162 {NID_secp384r1
, 192, TLS_CURVE_PRIME
}, /* secp384r1 (24) */
163 {NID_secp521r1
, 256, TLS_CURVE_PRIME
}, /* secp521r1 (25) */
164 {NID_brainpoolP256r1
, 128, TLS_CURVE_PRIME
}, /* brainpoolP256r1 (26) */
165 {NID_brainpoolP384r1
, 192, TLS_CURVE_PRIME
}, /* brainpoolP384r1 (27) */
166 {NID_brainpoolP512r1
, 256, TLS_CURVE_PRIME
}, /* brainpool512r1 (28) */
167 {EVP_PKEY_X25519
, 128, TLS_CURVE_CUSTOM
}, /* X25519 (29) */
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
)
202 return (uint16_t)(i
+ 1);
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_pss_sha256
,
628 TLSEXT_SIGALG_rsa_pss_pss_sha384
,
629 TLSEXT_SIGALG_rsa_pss_pss_sha512
,
630 TLSEXT_SIGALG_rsa_pss_rsae_sha256
,
631 TLSEXT_SIGALG_rsa_pss_rsae_sha384
,
632 TLSEXT_SIGALG_rsa_pss_rsae_sha512
,
634 TLSEXT_SIGALG_rsa_pkcs1_sha256
,
635 TLSEXT_SIGALG_rsa_pkcs1_sha384
,
636 TLSEXT_SIGALG_rsa_pkcs1_sha512
,
638 #ifndef OPENSSL_NO_EC
639 TLSEXT_SIGALG_ecdsa_sha224
,
640 TLSEXT_SIGALG_ecdsa_sha1
,
642 TLSEXT_SIGALG_rsa_pkcs1_sha224
,
643 TLSEXT_SIGALG_rsa_pkcs1_sha1
,
644 #ifndef OPENSSL_NO_DSA
645 TLSEXT_SIGALG_dsa_sha224
,
646 TLSEXT_SIGALG_dsa_sha1
,
648 TLSEXT_SIGALG_dsa_sha256
,
649 TLSEXT_SIGALG_dsa_sha384
,
650 TLSEXT_SIGALG_dsa_sha512
654 #ifndef OPENSSL_NO_EC
655 static const uint16_t suiteb_sigalgs
[] = {
656 TLSEXT_SIGALG_ecdsa_secp256r1_sha256
,
657 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
661 static const SIGALG_LOOKUP sigalg_lookup_tbl
[] = {
662 #ifndef OPENSSL_NO_EC
663 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256
,
664 NID_sha256
, SSL_MD_SHA256_IDX
, EVP_PKEY_EC
, SSL_PKEY_ECC
,
665 NID_ecdsa_with_SHA256
, NID_X9_62_prime256v1
},
666 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384
,
667 NID_sha384
, SSL_MD_SHA384_IDX
, EVP_PKEY_EC
, SSL_PKEY_ECC
,
668 NID_ecdsa_with_SHA384
, NID_secp384r1
},
669 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512
,
670 NID_sha512
, SSL_MD_SHA512_IDX
, EVP_PKEY_EC
, SSL_PKEY_ECC
,
671 NID_ecdsa_with_SHA512
, NID_secp521r1
},
672 {"ed25519", TLSEXT_SIGALG_ed25519
,
673 NID_undef
, -1, EVP_PKEY_ED25519
, SSL_PKEY_ED25519
,
674 NID_undef
, NID_undef
},
675 {NULL
, TLSEXT_SIGALG_ecdsa_sha224
,
676 NID_sha224
, SSL_MD_SHA224_IDX
, EVP_PKEY_EC
, SSL_PKEY_ECC
,
677 NID_ecdsa_with_SHA224
, NID_undef
},
678 {NULL
, TLSEXT_SIGALG_ecdsa_sha1
,
679 NID_sha1
, SSL_MD_SHA1_IDX
, EVP_PKEY_EC
, SSL_PKEY_ECC
,
680 NID_ecdsa_with_SHA1
, NID_undef
},
682 {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256
,
683 NID_sha256
, SSL_MD_SHA256_IDX
, EVP_PKEY_RSA_PSS
, SSL_PKEY_RSA
,
684 NID_undef
, NID_undef
},
685 {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384
,
686 NID_sha384
, SSL_MD_SHA384_IDX
, EVP_PKEY_RSA_PSS
, SSL_PKEY_RSA
,
687 NID_undef
, NID_undef
},
688 {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512
,
689 NID_sha512
, SSL_MD_SHA512_IDX
, EVP_PKEY_RSA_PSS
, SSL_PKEY_RSA
,
690 NID_undef
, NID_undef
},
691 {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256
,
692 NID_sha256
, SSL_MD_SHA256_IDX
, EVP_PKEY_RSA_PSS
, SSL_PKEY_RSA_PSS_SIGN
,
693 NID_undef
, NID_undef
},
694 {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384
,
695 NID_sha384
, SSL_MD_SHA384_IDX
, EVP_PKEY_RSA_PSS
, SSL_PKEY_RSA_PSS_SIGN
,
696 NID_undef
, NID_undef
},
697 {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512
,
698 NID_sha512
, SSL_MD_SHA512_IDX
, EVP_PKEY_RSA_PSS
, SSL_PKEY_RSA_PSS_SIGN
,
699 NID_undef
, NID_undef
},
700 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256
,
701 NID_sha256
, SSL_MD_SHA256_IDX
, EVP_PKEY_RSA
, SSL_PKEY_RSA
,
702 NID_sha256WithRSAEncryption
, NID_undef
},
703 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384
,
704 NID_sha384
, SSL_MD_SHA384_IDX
, EVP_PKEY_RSA
, SSL_PKEY_RSA
,
705 NID_sha384WithRSAEncryption
, NID_undef
},
706 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512
,
707 NID_sha512
, SSL_MD_SHA512_IDX
, EVP_PKEY_RSA
, SSL_PKEY_RSA
,
708 NID_sha512WithRSAEncryption
, NID_undef
},
709 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224
,
710 NID_sha224
, SSL_MD_SHA224_IDX
, EVP_PKEY_RSA
, SSL_PKEY_RSA
,
711 NID_sha224WithRSAEncryption
, NID_undef
},
712 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1
,
713 NID_sha1
, SSL_MD_SHA1_IDX
, EVP_PKEY_RSA
, SSL_PKEY_RSA
,
714 NID_sha1WithRSAEncryption
, NID_undef
},
715 #ifndef OPENSSL_NO_DSA
716 {NULL
, TLSEXT_SIGALG_dsa_sha256
,
717 NID_sha256
, SSL_MD_SHA256_IDX
, EVP_PKEY_DSA
, SSL_PKEY_DSA_SIGN
,
718 NID_dsa_with_SHA256
, NID_undef
},
719 {NULL
, TLSEXT_SIGALG_dsa_sha384
,
720 NID_sha384
, SSL_MD_SHA384_IDX
, EVP_PKEY_DSA
, SSL_PKEY_DSA_SIGN
,
721 NID_undef
, NID_undef
},
722 {NULL
, TLSEXT_SIGALG_dsa_sha512
,
723 NID_sha512
, SSL_MD_SHA512_IDX
, EVP_PKEY_DSA
, SSL_PKEY_DSA_SIGN
,
724 NID_undef
, NID_undef
},
725 {NULL
, TLSEXT_SIGALG_dsa_sha224
,
726 NID_sha224
, SSL_MD_SHA224_IDX
, EVP_PKEY_DSA
, SSL_PKEY_DSA_SIGN
,
727 NID_undef
, NID_undef
},
728 {NULL
, TLSEXT_SIGALG_dsa_sha1
,
729 NID_sha1
, SSL_MD_SHA1_IDX
, EVP_PKEY_DSA
, SSL_PKEY_DSA_SIGN
,
730 NID_dsaWithSHA1
, NID_undef
},
732 #ifndef OPENSSL_NO_GOST
733 {NULL
, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256
,
734 NID_id_GostR3411_2012_256
, SSL_MD_GOST12_256_IDX
,
735 NID_id_GostR3410_2012_256
, SSL_PKEY_GOST12_256
,
736 NID_undef
, NID_undef
},
737 {NULL
, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512
,
738 NID_id_GostR3411_2012_512
, SSL_MD_GOST12_512_IDX
,
739 NID_id_GostR3410_2012_512
, SSL_PKEY_GOST12_512
,
740 NID_undef
, NID_undef
},
741 {NULL
, TLSEXT_SIGALG_gostr34102001_gostr3411
,
742 NID_id_GostR3411_94
, SSL_MD_GOST94_IDX
,
743 NID_id_GostR3410_2001
, SSL_PKEY_GOST01
,
744 NID_undef
, NID_undef
}
747 /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
748 static const SIGALG_LOOKUP legacy_rsa_sigalg
= {
749 "rsa_pkcs1_md5_sha1", 0,
750 NID_md5_sha1
, SSL_MD_MD5_SHA1_IDX
,
751 EVP_PKEY_RSA
, SSL_PKEY_RSA
,
756 * Default signature algorithm values used if signature algorithms not present.
757 * From RFC5246. Note: order must match certificate index order.
759 static const uint16_t tls_default_sigalg
[] = {
760 TLSEXT_SIGALG_rsa_pkcs1_sha1
, /* SSL_PKEY_RSA */
761 0, /* SSL_PKEY_RSA_PSS_SIGN */
762 TLSEXT_SIGALG_dsa_sha1
, /* SSL_PKEY_DSA_SIGN */
763 TLSEXT_SIGALG_ecdsa_sha1
, /* SSL_PKEY_ECC */
764 TLSEXT_SIGALG_gostr34102001_gostr3411
, /* SSL_PKEY_GOST01 */
765 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256
, /* SSL_PKEY_GOST12_256 */
766 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512
, /* SSL_PKEY_GOST12_512 */
767 0 /* SSL_PKEY_ED25519 */
770 /* Lookup TLS signature algorithm */
771 static const SIGALG_LOOKUP
*tls1_lookup_sigalg(uint16_t sigalg
)
774 const SIGALG_LOOKUP
*s
;
776 for (i
= 0, s
= sigalg_lookup_tbl
; i
< OSSL_NELEM(sigalg_lookup_tbl
);
778 if (s
->sigalg
== sigalg
)
783 /* Lookup hash: return 0 if invalid or not enabled */
784 int tls1_lookup_md(const SIGALG_LOOKUP
*lu
, const EVP_MD
**pmd
)
789 /* lu->hash == NID_undef means no associated digest */
790 if (lu
->hash
== NID_undef
) {
793 md
= ssl_md(lu
->hash_idx
);
803 * Check if key is large enough to generate RSA-PSS signature.
805 * The key must greater than or equal to 2 * hash length + 2.
806 * SHA512 has a hash length of 64 bytes, which is incompatible
807 * with a 128 byte (1024 bit) key.
809 #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2)
810 static int rsa_pss_check_min_key_size(const RSA
*rsa
, const SIGALG_LOOKUP
*lu
)
816 if (!tls1_lookup_md(lu
, &md
) || md
== NULL
)
818 if (RSA_size(rsa
) < RSA_PSS_MINIMUM_KEY_SIZE(md
))
824 * Return a signature algorithm for TLS < 1.2 where the signature type
825 * is fixed by the certificate type.
827 static const SIGALG_LOOKUP
*tls1_get_legacy_sigalg(const SSL
*s
, int idx
)
833 /* Work out index corresponding to ciphersuite */
834 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
835 const SSL_CERT_LOOKUP
*clu
= ssl_cert_lookup_by_idx(i
);
837 if (clu
->amask
& s
->s3
->tmp
.new_cipher
->algorithm_auth
) {
843 idx
= s
->cert
->key
- s
->cert
->pkeys
;
846 if (idx
< 0 || idx
>= (int)OSSL_NELEM(tls_default_sigalg
))
848 if (SSL_USE_SIGALGS(s
) || idx
!= SSL_PKEY_RSA
) {
849 const SIGALG_LOOKUP
*lu
= tls1_lookup_sigalg(tls_default_sigalg
[idx
]);
851 if (!tls1_lookup_md(lu
, NULL
))
855 return &legacy_rsa_sigalg
;
857 /* Set peer sigalg based key type */
858 int tls1_set_peer_legacy_sigalg(SSL
*s
, const EVP_PKEY
*pkey
)
861 const SIGALG_LOOKUP
*lu
;
863 if (ssl_cert_lookup_by_pkey(pkey
, &idx
) == NULL
)
865 lu
= tls1_get_legacy_sigalg(s
, idx
);
868 s
->s3
->tmp
.peer_sigalg
= lu
;
872 size_t tls12_get_psigalgs(SSL
*s
, int sent
, const uint16_t **psigs
)
875 * If Suite B mode use Suite B sigalgs only, ignore any other
878 #ifndef OPENSSL_NO_EC
879 switch (tls1_suiteb(s
)) {
880 case SSL_CERT_FLAG_SUITEB_128_LOS
:
881 *psigs
= suiteb_sigalgs
;
882 return OSSL_NELEM(suiteb_sigalgs
);
884 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY
:
885 *psigs
= suiteb_sigalgs
;
888 case SSL_CERT_FLAG_SUITEB_192_LOS
:
889 *psigs
= suiteb_sigalgs
+ 1;
894 * We use client_sigalgs (if not NULL) if we're a server
895 * and sending a certificate request or if we're a client and
896 * determining which shared algorithm to use.
898 if ((s
->server
== sent
) && s
->cert
->client_sigalgs
!= NULL
) {
899 *psigs
= s
->cert
->client_sigalgs
;
900 return s
->cert
->client_sigalgslen
;
901 } else if (s
->cert
->conf_sigalgs
) {
902 *psigs
= s
->cert
->conf_sigalgs
;
903 return s
->cert
->conf_sigalgslen
;
905 *psigs
= tls12_sigalgs
;
906 return OSSL_NELEM(tls12_sigalgs
);
911 * Check signature algorithm is consistent with sent supported signature
912 * algorithms and if so set relevant digest and signature scheme in
915 int tls12_check_peer_sigalg(SSL
*s
, uint16_t sig
, EVP_PKEY
*pkey
)
917 const uint16_t *sent_sigs
;
918 const EVP_MD
*md
= NULL
;
920 size_t sent_sigslen
, i
;
921 int pkeyid
= EVP_PKEY_id(pkey
);
922 const SIGALG_LOOKUP
*lu
;
924 /* Should never happen */
927 if (SSL_IS_TLS13(s
)) {
928 /* Disallow DSA for TLS 1.3 */
929 if (pkeyid
== EVP_PKEY_DSA
) {
930 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS12_CHECK_PEER_SIGALG
,
931 SSL_R_WRONG_SIGNATURE_TYPE
);
934 /* Only allow PSS for TLS 1.3 */
935 if (pkeyid
== EVP_PKEY_RSA
)
936 pkeyid
= EVP_PKEY_RSA_PSS
;
938 lu
= tls1_lookup_sigalg(sig
);
940 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
941 * is consistent with signature: RSA keys can be used for RSA-PSS
944 || (SSL_IS_TLS13(s
) && (lu
->hash
== NID_sha1
|| lu
->hash
== NID_sha224
))
945 || (pkeyid
!= lu
->sig
946 && (lu
->sig
!= EVP_PKEY_RSA_PSS
|| pkeyid
!= EVP_PKEY_RSA
))) {
947 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_TLS12_CHECK_PEER_SIGALG
,
948 SSL_R_WRONG_SIGNATURE_TYPE
);
951 #ifndef OPENSSL_NO_EC
952 if (pkeyid
== EVP_PKEY_EC
) {
954 /* Check point compression is permitted */
955 if (!tls1_check_pkey_comp(s
, pkey
)) {
956 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
957 SSL_F_TLS12_CHECK_PEER_SIGALG
,
958 SSL_R_ILLEGAL_POINT_COMPRESSION
);
962 /* For TLS 1.3 or Suite B check curve matches signature algorithm */
963 if (SSL_IS_TLS13(s
) || tls1_suiteb(s
)) {
964 EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
965 int curve
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
967 if (lu
->curve
!= NID_undef
&& curve
!= lu
->curve
) {
968 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
969 SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_WRONG_CURVE
);
973 if (!SSL_IS_TLS13(s
)) {
974 /* Check curve matches extensions */
975 if (!tls1_check_group_id(s
, tls1_get_group_id(pkey
))) {
976 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
977 SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_WRONG_CURVE
);
980 if (tls1_suiteb(s
)) {
981 /* Check sigalg matches a permissible Suite B value */
982 if (sig
!= TLSEXT_SIGALG_ecdsa_secp256r1_sha256
983 && sig
!= TLSEXT_SIGALG_ecdsa_secp384r1_sha384
) {
984 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
985 SSL_F_TLS12_CHECK_PEER_SIGALG
,
986 SSL_R_WRONG_SIGNATURE_TYPE
);
991 } else if (tls1_suiteb(s
)) {
992 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS12_CHECK_PEER_SIGALG
,
993 SSL_R_WRONG_SIGNATURE_TYPE
);
998 /* Check signature matches a type we sent */
999 sent_sigslen
= tls12_get_psigalgs(s
, 1, &sent_sigs
);
1000 for (i
= 0; i
< sent_sigslen
; i
++, sent_sigs
++) {
1001 if (sig
== *sent_sigs
)
1004 /* Allow fallback to SHA1 if not strict mode */
1005 if (i
== sent_sigslen
&& (lu
->hash
!= NID_sha1
1006 || s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
)) {
1007 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS12_CHECK_PEER_SIGALG
,
1008 SSL_R_WRONG_SIGNATURE_TYPE
);
1011 if (!tls1_lookup_md(lu
, &md
)) {
1012 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS12_CHECK_PEER_SIGALG
,
1013 SSL_R_UNKNOWN_DIGEST
);
1018 * Make sure security callback allows algorithm. For historical
1019 * reasons we have to pass the sigalg as a two byte char array.
1021 sigalgstr
[0] = (sig
>> 8) & 0xff;
1022 sigalgstr
[1] = sig
& 0xff;
1023 if (!ssl_security(s
, SSL_SECOP_SIGALG_CHECK
,
1024 EVP_MD_size(md
) * 4, EVP_MD_type(md
),
1025 (void *)sigalgstr
)) {
1026 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS12_CHECK_PEER_SIGALG
,
1027 SSL_R_WRONG_SIGNATURE_TYPE
);
1031 /* Store the sigalg the peer uses */
1032 s
->s3
->tmp
.peer_sigalg
= lu
;
1036 int SSL_get_peer_signature_type_nid(const SSL
*s
, int *pnid
)
1038 if (s
->s3
->tmp
.peer_sigalg
== NULL
)
1040 *pnid
= s
->s3
->tmp
.peer_sigalg
->sig
;
1045 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1046 * supported, doesn't appear in supported signature algorithms, isn't supported
1047 * by the enabled protocol versions or by the security level.
1049 * This function should only be used for checking which ciphers are supported
1052 * Call ssl_cipher_disabled() to check that it's enabled or not.
1054 void ssl_set_client_disabled(SSL
*s
)
1056 s
->s3
->tmp
.mask_a
= 0;
1057 s
->s3
->tmp
.mask_k
= 0;
1058 ssl_set_sig_mask(&s
->s3
->tmp
.mask_a
, s
, SSL_SECOP_SIGALG_MASK
);
1059 ssl_get_min_max_version(s
, &s
->s3
->tmp
.min_ver
, &s
->s3
->tmp
.max_ver
);
1060 #ifndef OPENSSL_NO_PSK
1061 /* with PSK there must be client callback set */
1062 if (!s
->psk_client_callback
) {
1063 s
->s3
->tmp
.mask_a
|= SSL_aPSK
;
1064 s
->s3
->tmp
.mask_k
|= SSL_PSK
;
1066 #endif /* OPENSSL_NO_PSK */
1067 #ifndef OPENSSL_NO_SRP
1068 if (!(s
->srp_ctx
.srp_Mask
& SSL_kSRP
)) {
1069 s
->s3
->tmp
.mask_a
|= SSL_aSRP
;
1070 s
->s3
->tmp
.mask_k
|= SSL_kSRP
;
1076 * ssl_cipher_disabled - check that a cipher is disabled or not
1077 * @s: SSL connection that you want to use the cipher on
1078 * @c: cipher to check
1079 * @op: Security check that you want to do
1080 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
1082 * Returns 1 when it's disabled, 0 when enabled.
1084 int ssl_cipher_disabled(SSL
*s
, const SSL_CIPHER
*c
, int op
, int ecdhe
)
1086 if (c
->algorithm_mkey
& s
->s3
->tmp
.mask_k
1087 || c
->algorithm_auth
& s
->s3
->tmp
.mask_a
)
1089 if (s
->s3
->tmp
.max_ver
== 0)
1091 if (!SSL_IS_DTLS(s
)) {
1092 int min_tls
= c
->min_tls
;
1095 * For historical reasons we will allow ECHDE to be selected by a server
1096 * in SSLv3 if we are a client
1098 if (min_tls
== TLS1_VERSION
&& ecdhe
1099 && (c
->algorithm_mkey
& (SSL_kECDHE
| SSL_kECDHEPSK
)) != 0)
1100 min_tls
= SSL3_VERSION
;
1102 if ((min_tls
> s
->s3
->tmp
.max_ver
) || (c
->max_tls
< s
->s3
->tmp
.min_ver
))
1105 if (SSL_IS_DTLS(s
) && (DTLS_VERSION_GT(c
->min_dtls
, s
->s3
->tmp
.max_ver
)
1106 || DTLS_VERSION_LT(c
->max_dtls
, s
->s3
->tmp
.min_ver
)))
1109 return !ssl_security(s
, op
, c
->strength_bits
, 0, (void *)c
);
1112 int tls_use_ticket(SSL
*s
)
1114 if ((s
->options
& SSL_OP_NO_TICKET
))
1116 return ssl_security(s
, SSL_SECOP_TICKET
, 0, 0, NULL
);
1119 int tls1_set_server_sigalgs(SSL
*s
)
1123 /* Clear any shared signature algorithms */
1124 OPENSSL_free(s
->cert
->shared_sigalgs
);
1125 s
->cert
->shared_sigalgs
= NULL
;
1126 s
->cert
->shared_sigalgslen
= 0;
1127 /* Clear certificate validity flags */
1128 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
1129 s
->s3
->tmp
.valid_flags
[i
] = 0;
1131 * If peer sent no signature algorithms check to see if we support
1132 * the default algorithm for each certificate type
1134 if (s
->s3
->tmp
.peer_cert_sigalgs
== NULL
1135 && s
->s3
->tmp
.peer_sigalgs
== NULL
) {
1136 const uint16_t *sent_sigs
;
1137 size_t sent_sigslen
= tls12_get_psigalgs(s
, 1, &sent_sigs
);
1139 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
1140 const SIGALG_LOOKUP
*lu
= tls1_get_legacy_sigalg(s
, i
);
1145 /* Check default matches a type we sent */
1146 for (j
= 0; j
< sent_sigslen
; j
++) {
1147 if (lu
->sigalg
== sent_sigs
[j
]) {
1148 s
->s3
->tmp
.valid_flags
[i
] = CERT_PKEY_SIGN
;
1156 if (!tls1_process_sigalgs(s
)) {
1157 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
1158 SSL_F_TLS1_SET_SERVER_SIGALGS
, ERR_R_INTERNAL_ERROR
);
1161 if (s
->cert
->shared_sigalgs
!= NULL
)
1164 /* Fatal error if no shared signature algorithms */
1165 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS1_SET_SERVER_SIGALGS
,
1166 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS
);
1171 * Gets the ticket information supplied by the client if any.
1173 * hello: The parsed ClientHello data
1174 * ret: (output) on return, if a ticket was decrypted, then this is set to
1175 * point to the resulting session.
1177 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
1178 * ciphersuite, in which case we have no use for session tickets and one will
1179 * never be decrypted, nor will s->ext.ticket_expected be set to 1.
1182 * -1: fatal error, either from parsing or decrypting the ticket.
1183 * 0: no ticket was found (or was ignored, based on settings).
1184 * 1: a zero length extension was found, indicating that the client supports
1185 * session tickets but doesn't currently have one to offer.
1186 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
1187 * couldn't be decrypted because of a non-fatal error.
1188 * 3: a ticket was successfully decrypted and *ret was set.
1191 * Sets s->ext.ticket_expected to 1 if the server will have to issue
1192 * a new session ticket to the client because the client indicated support
1193 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1194 * a session ticket or we couldn't use the one it gave us, or if
1195 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1196 * Otherwise, s->ext.ticket_expected is set to 0.
1198 TICKET_RETURN
tls_get_ticket_from_client(SSL
*s
, CLIENTHELLO_MSG
*hello
,
1203 RAW_EXTENSION
*ticketext
;
1206 s
->ext
.ticket_expected
= 0;
1209 * If tickets disabled or not supported by the protocol version
1210 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
1213 if (s
->version
<= SSL3_VERSION
|| !tls_use_ticket(s
))
1216 ticketext
= &hello
->pre_proc_exts
[TLSEXT_IDX_session_ticket
];
1217 if (!ticketext
->present
)
1220 size
= PACKET_remaining(&ticketext
->data
);
1223 * The client will accept a ticket but doesn't currently have
1226 s
->ext
.ticket_expected
= 1;
1227 return TICKET_EMPTY
;
1229 if (s
->ext
.session_secret_cb
) {
1231 * Indicate that the ticket couldn't be decrypted rather than
1232 * generating the session from ticket now, trigger
1233 * abbreviated handshake based on external mechanism to
1234 * calculate the master secret later.
1236 return TICKET_NO_DECRYPT
;
1239 retv
= tls_decrypt_ticket(s
, PACKET_data(&ticketext
->data
), size
,
1240 hello
->session_id
, hello
->session_id_len
, ret
);
1242 case TICKET_NO_DECRYPT
:
1243 s
->ext
.ticket_expected
= 1;
1244 return TICKET_NO_DECRYPT
;
1246 case TICKET_SUCCESS
:
1247 return TICKET_SUCCESS
;
1249 case TICKET_SUCCESS_RENEW
:
1250 s
->ext
.ticket_expected
= 1;
1251 return TICKET_SUCCESS
;
1254 return TICKET_FATAL_ERR_OTHER
;
1259 * tls_decrypt_ticket attempts to decrypt a session ticket.
1261 * etick: points to the body of the session ticket extension.
1262 * eticklen: the length of the session tickets extension.
1263 * sess_id: points at the session ID.
1264 * sesslen: the length of the session ID.
1265 * psess: (output) on return, if a ticket was decrypted, then this is set to
1266 * point to the resulting session.
1268 TICKET_RETURN
tls_decrypt_ticket(SSL
*s
, const unsigned char *etick
,
1269 size_t eticklen
, const unsigned char *sess_id
,
1270 size_t sesslen
, SSL_SESSION
**psess
)
1273 unsigned char *sdec
;
1274 const unsigned char *p
;
1275 int slen
, renew_ticket
= 0, declen
;
1276 TICKET_RETURN ret
= TICKET_FATAL_ERR_OTHER
;
1278 unsigned char tick_hmac
[EVP_MAX_MD_SIZE
];
1279 HMAC_CTX
*hctx
= NULL
;
1280 EVP_CIPHER_CTX
*ctx
;
1281 SSL_CTX
*tctx
= s
->session_ctx
;
1283 /* Initialize session ticket encryption and HMAC contexts */
1284 hctx
= HMAC_CTX_new();
1286 return TICKET_FATAL_ERR_MALLOC
;
1287 ctx
= EVP_CIPHER_CTX_new();
1289 ret
= TICKET_FATAL_ERR_MALLOC
;
1292 if (tctx
->ext
.ticket_key_cb
) {
1293 unsigned char *nctick
= (unsigned char *)etick
;
1294 int rv
= tctx
->ext
.ticket_key_cb(s
, nctick
, nctick
+ 16,
1299 ret
= TICKET_NO_DECRYPT
;
1305 /* Check key name matches */
1306 if (memcmp(etick
, tctx
->ext
.tick_key_name
,
1307 sizeof(tctx
->ext
.tick_key_name
)) != 0) {
1308 ret
= TICKET_NO_DECRYPT
;
1311 if (HMAC_Init_ex(hctx
, tctx
->ext
.tick_hmac_key
,
1312 sizeof(tctx
->ext
.tick_hmac_key
),
1313 EVP_sha256(), NULL
) <= 0
1314 || EVP_DecryptInit_ex(ctx
, EVP_aes_256_cbc(), NULL
,
1315 tctx
->ext
.tick_aes_key
,
1317 + sizeof(tctx
->ext
.tick_key_name
)) <= 0) {
1322 * Attempt to process session ticket, first conduct sanity and integrity
1325 mlen
= HMAC_size(hctx
);
1329 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1331 TLSEXT_KEYNAME_LENGTH
+ EVP_CIPHER_CTX_iv_length(ctx
) + mlen
) {
1332 ret
= TICKET_NO_DECRYPT
;
1336 /* Check HMAC of encrypted ticket */
1337 if (HMAC_Update(hctx
, etick
, eticklen
) <= 0
1338 || HMAC_Final(hctx
, tick_hmac
, NULL
) <= 0) {
1341 HMAC_CTX_free(hctx
);
1342 if (CRYPTO_memcmp(tick_hmac
, etick
+ eticklen
, mlen
)) {
1343 EVP_CIPHER_CTX_free(ctx
);
1344 return TICKET_NO_DECRYPT
;
1346 /* Attempt to decrypt session data */
1347 /* Move p after IV to start of encrypted ticket, update length */
1348 p
= etick
+ TLSEXT_KEYNAME_LENGTH
+ EVP_CIPHER_CTX_iv_length(ctx
);
1349 eticklen
-= TLSEXT_KEYNAME_LENGTH
+ EVP_CIPHER_CTX_iv_length(ctx
);
1350 sdec
= OPENSSL_malloc(eticklen
);
1351 if (sdec
== NULL
|| EVP_DecryptUpdate(ctx
, sdec
, &slen
, p
,
1352 (int)eticklen
) <= 0) {
1353 EVP_CIPHER_CTX_free(ctx
);
1355 return TICKET_FATAL_ERR_OTHER
;
1357 if (EVP_DecryptFinal(ctx
, sdec
+ slen
, &declen
) <= 0) {
1358 EVP_CIPHER_CTX_free(ctx
);
1360 return TICKET_NO_DECRYPT
;
1363 EVP_CIPHER_CTX_free(ctx
);
1367 sess
= d2i_SSL_SESSION(NULL
, &p
, slen
);
1371 /* Some additional consistency checks */
1372 if (slen
!= 0 || sess
->session_id_length
!= 0) {
1373 SSL_SESSION_free(sess
);
1374 return TICKET_NO_DECRYPT
;
1377 * The session ID, if non-empty, is used by some clients to detect
1378 * that the ticket has been accepted. So we copy it to the session
1379 * structure. If it is empty set length to zero as required by
1383 memcpy(sess
->session_id
, sess_id
, sesslen
);
1384 sess
->session_id_length
= sesslen
;
1387 return TICKET_SUCCESS_RENEW
;
1389 return TICKET_SUCCESS
;
1393 * For session parse failure, indicate that we need to send a new ticket.
1395 return TICKET_NO_DECRYPT
;
1397 EVP_CIPHER_CTX_free(ctx
);
1398 HMAC_CTX_free(hctx
);
1402 /* Check to see if a signature algorithm is allowed */
1403 static int tls12_sigalg_allowed(SSL
*s
, int op
, const SIGALG_LOOKUP
*lu
)
1405 unsigned char sigalgstr
[2];
1408 /* See if sigalgs is recognised and if hash is enabled */
1409 if (!tls1_lookup_md(lu
, NULL
))
1411 /* DSA is not allowed in TLS 1.3 */
1412 if (SSL_IS_TLS13(s
) && lu
->sig
== EVP_PKEY_DSA
)
1414 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
1415 if (!s
->server
&& !SSL_IS_DTLS(s
) && s
->s3
->tmp
.min_ver
>= TLS1_3_VERSION
1416 && (lu
->sig
== EVP_PKEY_DSA
|| lu
->hash_idx
== SSL_MD_SHA1_IDX
1417 || lu
->hash_idx
== SSL_MD_MD5_IDX
1418 || lu
->hash_idx
== SSL_MD_SHA224_IDX
))
1420 /* See if public key algorithm allowed */
1421 if (ssl_cert_is_disabled(lu
->sig_idx
))
1423 if (lu
->hash
== NID_undef
)
1425 /* Security bits: half digest bits */
1426 secbits
= EVP_MD_size(ssl_md(lu
->hash_idx
)) * 4;
1427 /* Finally see if security callback allows it */
1428 sigalgstr
[0] = (lu
->sigalg
>> 8) & 0xff;
1429 sigalgstr
[1] = lu
->sigalg
& 0xff;
1430 return ssl_security(s
, op
, secbits
, lu
->hash
, (void *)sigalgstr
);
1434 * Get a mask of disabled public key algorithms based on supported signature
1435 * algorithms. For example if no signature algorithm supports RSA then RSA is
1439 void ssl_set_sig_mask(uint32_t *pmask_a
, SSL
*s
, int op
)
1441 const uint16_t *sigalgs
;
1442 size_t i
, sigalgslen
;
1443 uint32_t disabled_mask
= SSL_aRSA
| SSL_aDSS
| SSL_aECDSA
;
1445 * Go through all signature algorithms seeing if we support any
1448 sigalgslen
= tls12_get_psigalgs(s
, 1, &sigalgs
);
1449 for (i
= 0; i
< sigalgslen
; i
++, sigalgs
++) {
1450 const SIGALG_LOOKUP
*lu
= tls1_lookup_sigalg(*sigalgs
);
1451 const SSL_CERT_LOOKUP
*clu
;
1456 clu
= ssl_cert_lookup_by_idx(lu
->sig_idx
);
1460 /* If algorithm is disabled see if we can enable it */
1461 if ((clu
->amask
& disabled_mask
) != 0
1462 && tls12_sigalg_allowed(s
, op
, lu
))
1463 disabled_mask
&= ~clu
->amask
;
1465 *pmask_a
|= disabled_mask
;
1468 int tls12_copy_sigalgs(SSL
*s
, WPACKET
*pkt
,
1469 const uint16_t *psig
, size_t psiglen
)
1474 for (i
= 0; i
< psiglen
; i
++, psig
++) {
1475 const SIGALG_LOOKUP
*lu
= tls1_lookup_sigalg(*psig
);
1477 if (!tls12_sigalg_allowed(s
, SSL_SECOP_SIGALG_SUPPORTED
, lu
))
1479 if (!WPACKET_put_bytes_u16(pkt
, *psig
))
1482 * If TLS 1.3 must have at least one valid TLS 1.3 message
1483 * signing algorithm: i.e. neither RSA nor SHA1/SHA224
1485 if (rv
== 0 && (!SSL_IS_TLS13(s
)
1486 || (lu
->sig
!= EVP_PKEY_RSA
1487 && lu
->hash
!= NID_sha1
1488 && lu
->hash
!= NID_sha224
)))
1492 SSLerr(SSL_F_TLS12_COPY_SIGALGS
, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM
);
1496 /* Given preference and allowed sigalgs set shared sigalgs */
1497 static size_t tls12_shared_sigalgs(SSL
*s
, const SIGALG_LOOKUP
**shsig
,
1498 const uint16_t *pref
, size_t preflen
,
1499 const uint16_t *allow
, size_t allowlen
)
1501 const uint16_t *ptmp
, *atmp
;
1502 size_t i
, j
, nmatch
= 0;
1503 for (i
= 0, ptmp
= pref
; i
< preflen
; i
++, ptmp
++) {
1504 const SIGALG_LOOKUP
*lu
= tls1_lookup_sigalg(*ptmp
);
1506 /* Skip disabled hashes or signature algorithms */
1507 if (!tls12_sigalg_allowed(s
, SSL_SECOP_SIGALG_SHARED
, lu
))
1509 for (j
= 0, atmp
= allow
; j
< allowlen
; j
++, atmp
++) {
1510 if (*ptmp
== *atmp
) {
1521 /* Set shared signature algorithms for SSL structures */
1522 static int tls1_set_shared_sigalgs(SSL
*s
)
1524 const uint16_t *pref
, *allow
, *conf
;
1525 size_t preflen
, allowlen
, conflen
;
1527 const SIGALG_LOOKUP
**salgs
= NULL
;
1529 unsigned int is_suiteb
= tls1_suiteb(s
);
1531 OPENSSL_free(c
->shared_sigalgs
);
1532 c
->shared_sigalgs
= NULL
;
1533 c
->shared_sigalgslen
= 0;
1534 /* If client use client signature algorithms if not NULL */
1535 if (!s
->server
&& c
->client_sigalgs
&& !is_suiteb
) {
1536 conf
= c
->client_sigalgs
;
1537 conflen
= c
->client_sigalgslen
;
1538 } else if (c
->conf_sigalgs
&& !is_suiteb
) {
1539 conf
= c
->conf_sigalgs
;
1540 conflen
= c
->conf_sigalgslen
;
1542 conflen
= tls12_get_psigalgs(s
, 0, &conf
);
1543 if (s
->options
& SSL_OP_CIPHER_SERVER_PREFERENCE
|| is_suiteb
) {
1546 allow
= s
->s3
->tmp
.peer_sigalgs
;
1547 allowlen
= s
->s3
->tmp
.peer_sigalgslen
;
1551 pref
= s
->s3
->tmp
.peer_sigalgs
;
1552 preflen
= s
->s3
->tmp
.peer_sigalgslen
;
1554 nmatch
= tls12_shared_sigalgs(s
, NULL
, pref
, preflen
, allow
, allowlen
);
1556 salgs
= OPENSSL_malloc(nmatch
* sizeof(*salgs
));
1559 nmatch
= tls12_shared_sigalgs(s
, salgs
, pref
, preflen
, allow
, allowlen
);
1563 c
->shared_sigalgs
= salgs
;
1564 c
->shared_sigalgslen
= nmatch
;
1568 int tls1_save_u16(PACKET
*pkt
, uint16_t **pdest
, size_t *pdestlen
)
1574 size
= PACKET_remaining(pkt
);
1576 /* Invalid data length */
1577 if (size
== 0 || (size
& 1) != 0)
1582 buf
= OPENSSL_malloc(size
* sizeof(*buf
));
1585 for (i
= 0; i
< size
&& PACKET_get_net_2(pkt
, &stmp
); i
++)
1593 OPENSSL_free(*pdest
);
1600 int tls1_save_sigalgs(SSL
*s
, PACKET
*pkt
, int cert
)
1602 /* Extension ignored for inappropriate versions */
1603 if (!SSL_USE_SIGALGS(s
))
1605 /* Should never happen */
1606 if (s
->cert
== NULL
)
1610 return tls1_save_u16(pkt
, &s
->s3
->tmp
.peer_cert_sigalgs
,
1611 &s
->s3
->tmp
.peer_cert_sigalgslen
);
1613 return tls1_save_u16(pkt
, &s
->s3
->tmp
.peer_sigalgs
,
1614 &s
->s3
->tmp
.peer_sigalgslen
);
1618 /* Set preferred digest for each key type */
1620 int tls1_process_sigalgs(SSL
*s
)
1623 uint32_t *pvalid
= s
->s3
->tmp
.valid_flags
;
1626 if (!tls1_set_shared_sigalgs(s
))
1629 for (i
= 0; i
< SSL_PKEY_NUM
; i
++)
1632 for (i
= 0; i
< c
->shared_sigalgslen
; i
++) {
1633 const SIGALG_LOOKUP
*sigptr
= c
->shared_sigalgs
[i
];
1634 int idx
= sigptr
->sig_idx
;
1636 /* Ignore PKCS1 based sig algs in TLSv1.3 */
1637 if (SSL_IS_TLS13(s
) && sigptr
->sig
== EVP_PKEY_RSA
)
1639 /* If not disabled indicate we can explicitly sign */
1640 if (pvalid
[idx
] == 0 && !ssl_cert_is_disabled(idx
))
1641 pvalid
[idx
] = CERT_PKEY_EXPLICIT_SIGN
| CERT_PKEY_SIGN
;
1646 int SSL_get_sigalgs(SSL
*s
, int idx
,
1647 int *psign
, int *phash
, int *psignhash
,
1648 unsigned char *rsig
, unsigned char *rhash
)
1650 uint16_t *psig
= s
->s3
->tmp
.peer_sigalgs
;
1651 size_t numsigalgs
= s
->s3
->tmp
.peer_sigalgslen
;
1652 if (psig
== NULL
|| numsigalgs
> INT_MAX
)
1655 const SIGALG_LOOKUP
*lu
;
1657 if (idx
>= (int)numsigalgs
)
1661 *rhash
= (unsigned char)((*psig
>> 8) & 0xff);
1663 *rsig
= (unsigned char)(*psig
& 0xff);
1664 lu
= tls1_lookup_sigalg(*psig
);
1666 *psign
= lu
!= NULL
? lu
->sig
: NID_undef
;
1668 *phash
= lu
!= NULL
? lu
->hash
: NID_undef
;
1669 if (psignhash
!= NULL
)
1670 *psignhash
= lu
!= NULL
? lu
->sigandhash
: NID_undef
;
1672 return (int)numsigalgs
;
1675 int SSL_get_shared_sigalgs(SSL
*s
, int idx
,
1676 int *psign
, int *phash
, int *psignhash
,
1677 unsigned char *rsig
, unsigned char *rhash
)
1679 const SIGALG_LOOKUP
*shsigalgs
;
1680 if (s
->cert
->shared_sigalgs
== NULL
1682 || idx
>= (int)s
->cert
->shared_sigalgslen
1683 || s
->cert
->shared_sigalgslen
> INT_MAX
)
1685 shsigalgs
= s
->cert
->shared_sigalgs
[idx
];
1687 *phash
= shsigalgs
->hash
;
1689 *psign
= shsigalgs
->sig
;
1690 if (psignhash
!= NULL
)
1691 *psignhash
= shsigalgs
->sigandhash
;
1693 *rsig
= (unsigned char)(shsigalgs
->sigalg
& 0xff);
1695 *rhash
= (unsigned char)((shsigalgs
->sigalg
>> 8) & 0xff);
1696 return (int)s
->cert
->shared_sigalgslen
;
1699 /* Maximum possible number of unique entries in sigalgs array */
1700 #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
1704 /* TLSEXT_SIGALG_XXX values */
1705 uint16_t sigalgs
[TLS_MAX_SIGALGCNT
];
1708 static void get_sigorhash(int *psig
, int *phash
, const char *str
)
1710 if (strcmp(str
, "RSA") == 0) {
1711 *psig
= EVP_PKEY_RSA
;
1712 } else if (strcmp(str
, "RSA-PSS") == 0 || strcmp(str
, "PSS") == 0) {
1713 *psig
= EVP_PKEY_RSA_PSS
;
1714 } else if (strcmp(str
, "DSA") == 0) {
1715 *psig
= EVP_PKEY_DSA
;
1716 } else if (strcmp(str
, "ECDSA") == 0) {
1717 *psig
= EVP_PKEY_EC
;
1719 *phash
= OBJ_sn2nid(str
);
1720 if (*phash
== NID_undef
)
1721 *phash
= OBJ_ln2nid(str
);
1724 /* Maximum length of a signature algorithm string component */
1725 #define TLS_MAX_SIGSTRING_LEN 40
1727 static int sig_cb(const char *elem
, int len
, void *arg
)
1729 sig_cb_st
*sarg
= arg
;
1731 const SIGALG_LOOKUP
*s
;
1732 char etmp
[TLS_MAX_SIGSTRING_LEN
], *p
;
1733 int sig_alg
= NID_undef
, hash_alg
= NID_undef
;
1736 if (sarg
->sigalgcnt
== TLS_MAX_SIGALGCNT
)
1738 if (len
> (int)(sizeof(etmp
) - 1))
1740 memcpy(etmp
, elem
, len
);
1742 p
= strchr(etmp
, '+');
1744 * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
1745 * if there's no '+' in the provided name, look for the new-style combined
1746 * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
1747 * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
1748 * rsa_pss_rsae_* that differ only by public key OID; in such cases
1749 * we will pick the _rsae_ variant, by virtue of them appearing earlier
1753 for (i
= 0, s
= sigalg_lookup_tbl
; i
< OSSL_NELEM(sigalg_lookup_tbl
);
1755 if (s
->name
!= NULL
&& strcmp(etmp
, s
->name
) == 0) {
1756 sarg
->sigalgs
[sarg
->sigalgcnt
++] = s
->sigalg
;
1760 if (i
== OSSL_NELEM(sigalg_lookup_tbl
))
1767 get_sigorhash(&sig_alg
, &hash_alg
, etmp
);
1768 get_sigorhash(&sig_alg
, &hash_alg
, p
);
1769 if (sig_alg
== NID_undef
|| hash_alg
== NID_undef
)
1771 for (i
= 0, s
= sigalg_lookup_tbl
; i
< OSSL_NELEM(sigalg_lookup_tbl
);
1773 if (s
->hash
== hash_alg
&& s
->sig
== sig_alg
) {
1774 sarg
->sigalgs
[sarg
->sigalgcnt
++] = s
->sigalg
;
1778 if (i
== OSSL_NELEM(sigalg_lookup_tbl
))
1782 /* Reject duplicates */
1783 for (i
= 0; i
< sarg
->sigalgcnt
- 1; i
++) {
1784 if (sarg
->sigalgs
[i
] == sarg
->sigalgs
[sarg
->sigalgcnt
]) {
1793 * Set supported signature algorithms based on a colon separated list of the
1794 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
1796 int tls1_set_sigalgs_list(CERT
*c
, const char *str
, int client
)
1800 if (!CONF_parse_list(str
, ':', 1, sig_cb
, &sig
))
1804 return tls1_set_raw_sigalgs(c
, sig
.sigalgs
, sig
.sigalgcnt
, client
);
1807 int tls1_set_raw_sigalgs(CERT
*c
, const uint16_t *psigs
, size_t salglen
,
1812 sigalgs
= OPENSSL_malloc(salglen
* sizeof(*sigalgs
));
1813 if (sigalgs
== NULL
)
1815 memcpy(sigalgs
, psigs
, salglen
* sizeof(*sigalgs
));
1818 OPENSSL_free(c
->client_sigalgs
);
1819 c
->client_sigalgs
= sigalgs
;
1820 c
->client_sigalgslen
= salglen
;
1822 OPENSSL_free(c
->conf_sigalgs
);
1823 c
->conf_sigalgs
= sigalgs
;
1824 c
->conf_sigalgslen
= salglen
;
1830 int tls1_set_sigalgs(CERT
*c
, const int *psig_nids
, size_t salglen
, int client
)
1832 uint16_t *sigalgs
, *sptr
;
1837 sigalgs
= OPENSSL_malloc((salglen
/ 2) * sizeof(*sigalgs
));
1838 if (sigalgs
== NULL
)
1840 for (i
= 0, sptr
= sigalgs
; i
< salglen
; i
+= 2) {
1842 const SIGALG_LOOKUP
*curr
;
1843 int md_id
= *psig_nids
++;
1844 int sig_id
= *psig_nids
++;
1846 for (j
= 0, curr
= sigalg_lookup_tbl
; j
< OSSL_NELEM(sigalg_lookup_tbl
);
1848 if (curr
->hash
== md_id
&& curr
->sig
== sig_id
) {
1849 *sptr
++ = curr
->sigalg
;
1854 if (j
== OSSL_NELEM(sigalg_lookup_tbl
))
1859 OPENSSL_free(c
->client_sigalgs
);
1860 c
->client_sigalgs
= sigalgs
;
1861 c
->client_sigalgslen
= salglen
/ 2;
1863 OPENSSL_free(c
->conf_sigalgs
);
1864 c
->conf_sigalgs
= sigalgs
;
1865 c
->conf_sigalgslen
= salglen
/ 2;
1871 OPENSSL_free(sigalgs
);
1875 static int tls1_check_sig_alg(CERT
*c
, X509
*x
, int default_nid
)
1879 if (default_nid
== -1)
1881 sig_nid
= X509_get_signature_nid(x
);
1883 return sig_nid
== default_nid
? 1 : 0;
1884 for (i
= 0; i
< c
->shared_sigalgslen
; i
++)
1885 if (sig_nid
== c
->shared_sigalgs
[i
]->sigandhash
)
1890 /* Check to see if a certificate issuer name matches list of CA names */
1891 static int ssl_check_ca_name(STACK_OF(X509_NAME
) *names
, X509
*x
)
1895 nm
= X509_get_issuer_name(x
);
1896 for (i
= 0; i
< sk_X509_NAME_num(names
); i
++) {
1897 if (!X509_NAME_cmp(nm
, sk_X509_NAME_value(names
, i
)))
1904 * Check certificate chain is consistent with TLS extensions and is usable by
1905 * server. This servers two purposes: it allows users to check chains before
1906 * passing them to the server and it allows the server to check chains before
1907 * attempting to use them.
1910 /* Flags which need to be set for a certificate when strict mode not set */
1912 #define CERT_PKEY_VALID_FLAGS \
1913 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
1914 /* Strict mode flags */
1915 #define CERT_PKEY_STRICT_FLAGS \
1916 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
1917 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
1919 int tls1_check_chain(SSL
*s
, X509
*x
, EVP_PKEY
*pk
, STACK_OF(X509
) *chain
,
1924 int check_flags
= 0, strict_mode
;
1925 CERT_PKEY
*cpk
= NULL
;
1928 unsigned int suiteb_flags
= tls1_suiteb(s
);
1929 /* idx == -1 means checking server chains */
1931 /* idx == -2 means checking client certificate chains */
1934 idx
= (int)(cpk
- c
->pkeys
);
1936 cpk
= c
->pkeys
+ idx
;
1937 pvalid
= s
->s3
->tmp
.valid_flags
+ idx
;
1939 pk
= cpk
->privatekey
;
1941 strict_mode
= c
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
;
1942 /* If no cert or key, forget it */
1951 if (ssl_cert_lookup_by_pkey(pk
, &certidx
) == NULL
)
1954 pvalid
= s
->s3
->tmp
.valid_flags
+ idx
;
1956 if (c
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
)
1957 check_flags
= CERT_PKEY_STRICT_FLAGS
;
1959 check_flags
= CERT_PKEY_VALID_FLAGS
;
1966 check_flags
|= CERT_PKEY_SUITEB
;
1967 ok
= X509_chain_check_suiteb(NULL
, x
, chain
, suiteb_flags
);
1968 if (ok
== X509_V_OK
)
1969 rv
|= CERT_PKEY_SUITEB
;
1970 else if (!check_flags
)
1975 * Check all signature algorithms are consistent with signature
1976 * algorithms extension if TLS 1.2 or later and strict mode.
1978 if (TLS1_get_version(s
) >= TLS1_2_VERSION
&& strict_mode
) {
1981 if (s
->s3
->tmp
.peer_cert_sigalgs
!= NULL
1982 || s
->s3
->tmp
.peer_sigalgs
!= NULL
) {
1984 /* If no sigalgs extension use defaults from RFC5246 */
1988 rsign
= EVP_PKEY_RSA
;
1989 default_nid
= NID_sha1WithRSAEncryption
;
1992 case SSL_PKEY_DSA_SIGN
:
1993 rsign
= EVP_PKEY_DSA
;
1994 default_nid
= NID_dsaWithSHA1
;
1998 rsign
= EVP_PKEY_EC
;
1999 default_nid
= NID_ecdsa_with_SHA1
;
2002 case SSL_PKEY_GOST01
:
2003 rsign
= NID_id_GostR3410_2001
;
2004 default_nid
= NID_id_GostR3411_94_with_GostR3410_2001
;
2007 case SSL_PKEY_GOST12_256
:
2008 rsign
= NID_id_GostR3410_2012_256
;
2009 default_nid
= NID_id_tc26_signwithdigest_gost3410_2012_256
;
2012 case SSL_PKEY_GOST12_512
:
2013 rsign
= NID_id_GostR3410_2012_512
;
2014 default_nid
= NID_id_tc26_signwithdigest_gost3410_2012_512
;
2023 * If peer sent no signature algorithms extension and we have set
2024 * preferred signature algorithms check we support sha1.
2026 if (default_nid
> 0 && c
->conf_sigalgs
) {
2028 const uint16_t *p
= c
->conf_sigalgs
;
2029 for (j
= 0; j
< c
->conf_sigalgslen
; j
++, p
++) {
2030 const SIGALG_LOOKUP
*lu
= tls1_lookup_sigalg(*p
);
2032 if (lu
!= NULL
&& lu
->hash
== NID_sha1
&& lu
->sig
== rsign
)
2035 if (j
== c
->conf_sigalgslen
) {
2042 /* Check signature algorithm of each cert in chain */
2043 if (!tls1_check_sig_alg(c
, x
, default_nid
)) {
2047 rv
|= CERT_PKEY_EE_SIGNATURE
;
2048 rv
|= CERT_PKEY_CA_SIGNATURE
;
2049 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
2050 if (!tls1_check_sig_alg(c
, sk_X509_value(chain
, i
), default_nid
)) {
2052 rv
&= ~CERT_PKEY_CA_SIGNATURE
;
2059 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
2060 else if (check_flags
)
2061 rv
|= CERT_PKEY_EE_SIGNATURE
| CERT_PKEY_CA_SIGNATURE
;
2063 /* Check cert parameters are consistent */
2064 if (tls1_check_cert_param(s
, x
, 1))
2065 rv
|= CERT_PKEY_EE_PARAM
;
2066 else if (!check_flags
)
2069 rv
|= CERT_PKEY_CA_PARAM
;
2070 /* In strict mode check rest of chain too */
2071 else if (strict_mode
) {
2072 rv
|= CERT_PKEY_CA_PARAM
;
2073 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
2074 X509
*ca
= sk_X509_value(chain
, i
);
2075 if (!tls1_check_cert_param(s
, ca
, 0)) {
2077 rv
&= ~CERT_PKEY_CA_PARAM
;
2084 if (!s
->server
&& strict_mode
) {
2085 STACK_OF(X509_NAME
) *ca_dn
;
2087 switch (EVP_PKEY_id(pk
)) {
2089 check_type
= TLS_CT_RSA_SIGN
;
2092 check_type
= TLS_CT_DSS_SIGN
;
2095 check_type
= TLS_CT_ECDSA_SIGN
;
2099 const uint8_t *ctypes
= s
->s3
->tmp
.ctype
;
2102 for (j
= 0; j
< s
->s3
->tmp
.ctype_len
; j
++, ctypes
++) {
2103 if (*ctypes
== check_type
) {
2104 rv
|= CERT_PKEY_CERT_TYPE
;
2108 if (!(rv
& CERT_PKEY_CERT_TYPE
) && !check_flags
)
2111 rv
|= CERT_PKEY_CERT_TYPE
;
2114 ca_dn
= s
->s3
->tmp
.peer_ca_names
;
2116 if (!sk_X509_NAME_num(ca_dn
))
2117 rv
|= CERT_PKEY_ISSUER_NAME
;
2119 if (!(rv
& CERT_PKEY_ISSUER_NAME
)) {
2120 if (ssl_check_ca_name(ca_dn
, x
))
2121 rv
|= CERT_PKEY_ISSUER_NAME
;
2123 if (!(rv
& CERT_PKEY_ISSUER_NAME
)) {
2124 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
2125 X509
*xtmp
= sk_X509_value(chain
, i
);
2126 if (ssl_check_ca_name(ca_dn
, xtmp
)) {
2127 rv
|= CERT_PKEY_ISSUER_NAME
;
2132 if (!check_flags
&& !(rv
& CERT_PKEY_ISSUER_NAME
))
2135 rv
|= CERT_PKEY_ISSUER_NAME
| CERT_PKEY_CERT_TYPE
;
2137 if (!check_flags
|| (rv
& check_flags
) == check_flags
)
2138 rv
|= CERT_PKEY_VALID
;
2142 if (TLS1_get_version(s
) >= TLS1_2_VERSION
)
2143 rv
|= *pvalid
& (CERT_PKEY_EXPLICIT_SIGN
| CERT_PKEY_SIGN
);
2145 rv
|= CERT_PKEY_SIGN
| CERT_PKEY_EXPLICIT_SIGN
;
2148 * When checking a CERT_PKEY structure all flags are irrelevant if the
2152 if (rv
& CERT_PKEY_VALID
) {
2155 /* Preserve sign and explicit sign flag, clear rest */
2156 *pvalid
&= CERT_PKEY_EXPLICIT_SIGN
| CERT_PKEY_SIGN
;
2163 /* Set validity of certificates in an SSL structure */
2164 void tls1_set_cert_validity(SSL
*s
)
2166 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_RSA
);
2167 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_RSA_PSS_SIGN
);
2168 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_DSA_SIGN
);
2169 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_ECC
);
2170 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_GOST01
);
2171 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_GOST12_256
);
2172 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_GOST12_512
);
2173 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_ED25519
);
2176 /* User level utility function to check a chain is suitable */
2177 int SSL_check_chain(SSL
*s
, X509
*x
, EVP_PKEY
*pk
, STACK_OF(X509
) *chain
)
2179 return tls1_check_chain(s
, x
, pk
, chain
, -1);
2182 #ifndef OPENSSL_NO_DH
2183 DH
*ssl_get_auto_dh(SSL
*s
)
2185 int dh_secbits
= 80;
2186 if (s
->cert
->dh_tmp_auto
== 2)
2187 return DH_get_1024_160();
2188 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aNULL
| SSL_aPSK
)) {
2189 if (s
->s3
->tmp
.new_cipher
->strength_bits
== 256)
2194 if (s
->s3
->tmp
.cert
== NULL
)
2196 dh_secbits
= EVP_PKEY_security_bits(s
->s3
->tmp
.cert
->privatekey
);
2199 if (dh_secbits
>= 128) {
2207 if (dh_secbits
>= 192)
2208 p
= BN_get_rfc3526_prime_8192(NULL
);
2210 p
= BN_get_rfc3526_prime_3072(NULL
);
2211 if (p
== NULL
|| g
== NULL
|| !DH_set0_pqg(dhp
, p
, NULL
, g
)) {
2219 if (dh_secbits
>= 112)
2220 return DH_get_2048_224();
2221 return DH_get_1024_160();
2225 static int ssl_security_cert_key(SSL
*s
, SSL_CTX
*ctx
, X509
*x
, int op
)
2228 EVP_PKEY
*pkey
= X509_get0_pubkey(x
);
2231 * If no parameters this will return -1 and fail using the default
2232 * security callback for any non-zero security level. This will
2233 * reject keys which omit parameters but this only affects DSA and
2234 * omission of parameters is never (?) done in practice.
2236 secbits
= EVP_PKEY_security_bits(pkey
);
2239 return ssl_security(s
, op
, secbits
, 0, x
);
2241 return ssl_ctx_security(ctx
, op
, secbits
, 0, x
);
2244 static int ssl_security_cert_sig(SSL
*s
, SSL_CTX
*ctx
, X509
*x
, int op
)
2246 /* Lookup signature algorithm digest */
2247 int secbits
, nid
, pknid
;
2248 /* Don't check signature if self signed */
2249 if ((X509_get_extension_flags(x
) & EXFLAG_SS
) != 0)
2251 if (!X509_get_signature_info(x
, &nid
, &pknid
, &secbits
, NULL
))
2253 /* If digest NID not defined use signature NID */
2254 if (nid
== NID_undef
)
2257 return ssl_security(s
, op
, secbits
, nid
, x
);
2259 return ssl_ctx_security(ctx
, op
, secbits
, nid
, x
);
2262 int ssl_security_cert(SSL
*s
, SSL_CTX
*ctx
, X509
*x
, int vfy
, int is_ee
)
2265 vfy
= SSL_SECOP_PEER
;
2267 if (!ssl_security_cert_key(s
, ctx
, x
, SSL_SECOP_EE_KEY
| vfy
))
2268 return SSL_R_EE_KEY_TOO_SMALL
;
2270 if (!ssl_security_cert_key(s
, ctx
, x
, SSL_SECOP_CA_KEY
| vfy
))
2271 return SSL_R_CA_KEY_TOO_SMALL
;
2273 if (!ssl_security_cert_sig(s
, ctx
, x
, SSL_SECOP_CA_MD
| vfy
))
2274 return SSL_R_CA_MD_TOO_WEAK
;
2279 * Check security of a chain, if |sk| includes the end entity certificate then
2280 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
2281 * one to the peer. Return values: 1 if ok otherwise error code to use
2284 int ssl_security_cert_chain(SSL
*s
, STACK_OF(X509
) *sk
, X509
*x
, int vfy
)
2286 int rv
, start_idx
, i
;
2288 x
= sk_X509_value(sk
, 0);
2293 rv
= ssl_security_cert(s
, NULL
, x
, vfy
, 1);
2297 for (i
= start_idx
; i
< sk_X509_num(sk
); i
++) {
2298 x
= sk_X509_value(sk
, i
);
2299 rv
= ssl_security_cert(s
, NULL
, x
, vfy
, 0);
2307 * For TLS 1.2 servers check if we have a certificate which can be used
2308 * with the signature algorithm "lu" and return index of certificate.
2311 static int tls12_get_cert_sigalg_idx(const SSL
*s
, const SIGALG_LOOKUP
*lu
)
2313 int sig_idx
= lu
->sig_idx
;
2314 const SSL_CERT_LOOKUP
*clu
= ssl_cert_lookup_by_idx(sig_idx
);
2316 /* If not recognised or not supported by cipher mask it is not suitable */
2317 if (clu
== NULL
|| !(clu
->amask
& s
->s3
->tmp
.new_cipher
->algorithm_auth
))
2320 return s
->s3
->tmp
.valid_flags
[sig_idx
] & CERT_PKEY_VALID
? sig_idx
: -1;
2324 * Returns true if |s| has a usable certificate configured for use
2325 * with signature scheme |sig|.
2326 * "Usable" includes a check for presence as well as applying
2327 * the signature_algorithm_cert restrictions sent by the peer (if any).
2328 * Returns false if no usable certificate is found.
2330 static int has_usable_cert(SSL
*s
, const SIGALG_LOOKUP
*sig
, int idx
)
2332 const SIGALG_LOOKUP
*lu
;
2336 /* TLS 1.2 callers can override lu->sig_idx, but not TLS 1.3 callers. */
2339 if (!ssl_has_cert(s
, idx
))
2341 if (s
->s3
->tmp
.peer_cert_sigalgs
!= NULL
) {
2342 for (i
= 0; i
< s
->s3
->tmp
.peer_cert_sigalgslen
; i
++) {
2343 lu
= tls1_lookup_sigalg(s
->s3
->tmp
.peer_cert_sigalgs
[i
]);
2345 || !X509_get_signature_info(s
->cert
->pkeys
[idx
].x509
, &mdnid
,
2346 &pknid
, NULL
, NULL
))
2349 * TODO this does not differentiate between the
2350 * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
2351 * have a chain here that lets us look at the key OID in the
2352 * signing certificate.
2354 if (mdnid
== lu
->hash
&& pknid
== lu
->sig
)
2363 * Choose an appropriate signature algorithm based on available certificates
2364 * Sets chosen certificate and signature algorithm.
2366 * For servers if we fail to find a required certificate it is a fatal error,
2367 * an appropriate error code is set and a TLS alert is sent.
2369 * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
2370 * a fatal error: we will either try another certificate or not present one
2371 * to the server. In this case no error is set.
2373 int tls_choose_sigalg(SSL
*s
, int fatalerrs
)
2375 const SIGALG_LOOKUP
*lu
= NULL
;
2378 s
->s3
->tmp
.cert
= NULL
;
2379 s
->s3
->tmp
.sigalg
= NULL
;
2381 if (SSL_IS_TLS13(s
)) {
2383 #ifndef OPENSSL_NO_EC
2384 int curve
= -1, skip_ec
= 0;
2387 /* Look for a certificate matching shared sigalgs */
2388 for (i
= 0; i
< s
->cert
->shared_sigalgslen
; i
++) {
2389 lu
= s
->cert
->shared_sigalgs
[i
];
2392 /* Skip SHA1, SHA224, DSA and RSA if not PSS */
2393 if (lu
->hash
== NID_sha1
2394 || lu
->hash
== NID_sha224
2395 || lu
->sig
== EVP_PKEY_DSA
2396 || lu
->sig
== EVP_PKEY_RSA
)
2398 /* Check that we have a cert, and signature_algorithms_cert */
2399 if (!tls1_lookup_md(lu
, NULL
) || !has_usable_cert(s
, lu
, -1))
2401 if (lu
->sig
== EVP_PKEY_EC
) {
2402 #ifndef OPENSSL_NO_EC
2404 EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(s
->cert
->pkeys
[SSL_PKEY_ECC
].privatekey
);
2406 curve
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
2407 if (EC_KEY_get_conv_form(ec
)
2408 != POINT_CONVERSION_UNCOMPRESSED
)
2411 if (skip_ec
|| (lu
->curve
!= NID_undef
&& curve
!= lu
->curve
))
2416 } else if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2417 /* validate that key is large enough for the signature algorithm */
2420 pkey
= s
->cert
->pkeys
[lu
->sig_idx
].privatekey
;
2421 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey
), lu
))
2426 if (i
== s
->cert
->shared_sigalgslen
) {
2429 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_F_TLS_CHOOSE_SIGALG
,
2430 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM
);
2434 /* If ciphersuite doesn't require a cert nothing to do */
2435 if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aCERT
))
2437 if (!s
->server
&& !ssl_has_cert(s
, s
->cert
->key
- s
->cert
->pkeys
))
2440 if (SSL_USE_SIGALGS(s
)) {
2442 if (s
->s3
->tmp
.peer_sigalgs
!= NULL
) {
2443 #ifndef OPENSSL_NO_EC
2446 /* For Suite B need to match signature algorithm to curve */
2447 if (tls1_suiteb(s
)) {
2448 EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(s
->cert
->pkeys
[SSL_PKEY_ECC
].privatekey
);
2449 curve
= EC_GROUP_get_curve_name(EC_KEY_get0_group(ec
));
2456 * Find highest preference signature algorithm matching
2459 for (i
= 0; i
< s
->cert
->shared_sigalgslen
; i
++) {
2460 lu
= s
->cert
->shared_sigalgs
[i
];
2463 if ((sig_idx
= tls12_get_cert_sigalg_idx(s
, lu
)) == -1)
2466 int cc_idx
= s
->cert
->key
- s
->cert
->pkeys
;
2468 sig_idx
= lu
->sig_idx
;
2469 if (cc_idx
!= sig_idx
)
2472 /* Check that we have a cert, and sig_algs_cert */
2473 if (!has_usable_cert(s
, lu
, sig_idx
))
2475 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
2476 /* validate that key is large enough for the signature algorithm */
2477 EVP_PKEY
*pkey
= s
->cert
->pkeys
[sig_idx
].privatekey
;
2479 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey
), lu
))
2482 #ifndef OPENSSL_NO_EC
2483 if (curve
== -1 || lu
->curve
== curve
)
2487 if (i
== s
->cert
->shared_sigalgslen
) {
2490 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CHOOSE_SIGALG
,
2491 ERR_R_INTERNAL_ERROR
);
2496 * If we have no sigalg use defaults
2498 const uint16_t *sent_sigs
;
2499 size_t sent_sigslen
;
2501 if ((lu
= tls1_get_legacy_sigalg(s
, -1)) == NULL
) {
2504 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CHOOSE_SIGALG
,
2505 ERR_R_INTERNAL_ERROR
);
2509 /* Check signature matches a type we sent */
2510 sent_sigslen
= tls12_get_psigalgs(s
, 1, &sent_sigs
);
2511 for (i
= 0; i
< sent_sigslen
; i
++, sent_sigs
++) {
2512 if (lu
->sigalg
== *sent_sigs
2513 && has_usable_cert(s
, lu
, lu
->sig_idx
))
2516 if (i
== sent_sigslen
) {
2519 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2520 SSL_F_TLS_CHOOSE_SIGALG
,
2521 SSL_R_WRONG_SIGNATURE_TYPE
);
2526 if ((lu
= tls1_get_legacy_sigalg(s
, -1)) == NULL
) {
2529 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS_CHOOSE_SIGALG
,
2530 ERR_R_INTERNAL_ERROR
);
2536 sig_idx
= lu
->sig_idx
;
2537 s
->s3
->tmp
.cert
= &s
->cert
->pkeys
[sig_idx
];
2538 s
->cert
->key
= s
->s3
->tmp
.cert
;
2539 s
->s3
->tmp
.sigalg
= lu
;
2543 int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX
*ctx
, uint8_t mode
)
2545 if (mode
!= TLSEXT_max_fragment_length_DISABLED
2546 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode
)) {
2547 SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH
,
2548 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH
);
2552 ctx
->ext
.max_fragment_len_mode
= mode
;
2556 int SSL_set_tlsext_max_fragment_length(SSL
*ssl
, uint8_t mode
)
2558 if (mode
!= TLSEXT_max_fragment_length_DISABLED
2559 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode
)) {
2560 SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH
,
2561 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH
);
2565 ssl
->ext
.max_fragment_len_mode
= mode
;
2569 uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION
*session
)
2571 return session
->ext
.max_fragment_len_mode
;