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