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