]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
fecb3aae | 2 | * Copyright 1995-2022 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> | |
a76ce286 | 15 | #include <openssl/core_names.h> |
67c8e7f4 | 16 | #include <openssl/ocsp.h> |
5951e840 MC |
17 | #include <openssl/conf.h> |
18 | #include <openssl/x509v3.h> | |
3c27208f RS |
19 | #include <openssl/dh.h> |
20 | #include <openssl/bn.h> | |
9d2d857f | 21 | #include <openssl/provider.h> |
091f6074 | 22 | #include <openssl/param_build.h> |
677963e5 | 23 | #include "internal/nelem.h" |
d8975dec | 24 | #include "internal/sizes.h" |
48e971dd | 25 | #include "internal/tlsgroups.h" |
706457b7 | 26 | #include "ssl_local.h" |
3c27208f | 27 | #include <openssl/ct.h> |
58964a49 | 28 | |
38b051a1 TM |
29 | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pkey); |
30 | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, const SIGALG_LOOKUP *lu); | |
5235ef44 | 31 | |
0f113f3e MC |
32 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
33 | tls1_enc, | |
aedbb71b | 34 | tls1_mac_old, |
0f113f3e MC |
35 | tls1_setup_key_block, |
36 | tls1_generate_master_secret, | |
37 | tls1_change_cipher_state, | |
38 | tls1_final_finish_mac, | |
0f113f3e MC |
39 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
40 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
41 | tls1_alert_code, | |
42 | tls1_export_keying_material, | |
43 | 0, | |
a29fa98c | 44 | ssl3_set_handshake_header, |
2c7b4dbc | 45 | tls_close_construct_packet, |
0f113f3e MC |
46 | ssl3_handshake_write |
47 | }; | |
48 | ||
49 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { | |
50 | tls1_enc, | |
aedbb71b | 51 | tls1_mac_old, |
0f113f3e MC |
52 | tls1_setup_key_block, |
53 | tls1_generate_master_secret, | |
54 | tls1_change_cipher_state, | |
55 | tls1_final_finish_mac, | |
0f113f3e MC |
56 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
57 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
58 | tls1_alert_code, | |
59 | tls1_export_keying_material, | |
60 | SSL_ENC_FLAG_EXPLICIT_IV, | |
a29fa98c | 61 | ssl3_set_handshake_header, |
2c7b4dbc | 62 | tls_close_construct_packet, |
0f113f3e MC |
63 | ssl3_handshake_write |
64 | }; | |
65 | ||
66 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { | |
67 | tls1_enc, | |
aedbb71b | 68 | tls1_mac_old, |
0f113f3e MC |
69 | tls1_setup_key_block, |
70 | tls1_generate_master_secret, | |
71 | tls1_change_cipher_state, | |
72 | tls1_final_finish_mac, | |
0f113f3e MC |
73 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
74 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
75 | tls1_alert_code, | |
76 | tls1_export_keying_material, | |
77 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF | |
78 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, | |
a29fa98c | 79 | ssl3_set_handshake_header, |
2c7b4dbc | 80 | tls_close_construct_packet, |
0f113f3e MC |
81 | ssl3_handshake_write |
82 | }; | |
58964a49 | 83 | |
582a17d6 | 84 | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
bebc0c7d | 85 | tls13_enc, |
aedbb71b | 86 | tls1_mac_old, |
92760c21 MC |
87 | tls13_setup_key_block, |
88 | tls13_generate_master_secret, | |
89 | tls13_change_cipher_state, | |
90 | tls13_final_finish_mac, | |
582a17d6 MC |
91 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
92 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
04904312 | 93 | tls13_alert_code, |
0ca8d1ec | 94 | tls13_export_keying_material, |
bebc0c7d | 95 | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
582a17d6 MC |
96 | ssl3_set_handshake_header, |
97 | tls_close_construct_packet, | |
98 | ssl3_handshake_write | |
99 | }; | |
100 | ||
f0131dc0 | 101 | OSSL_TIME tls1_default_timeout(void) |
0f113f3e MC |
102 | { |
103 | /* | |
104 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for | |
105 | * http, the cache would over fill | |
106 | */ | |
f0131dc0 | 107 | return ossl_seconds2time(60 * 60 * 2); |
0f113f3e | 108 | } |
58964a49 | 109 | |
6b691a5c | 110 | int tls1_new(SSL *s) |
0f113f3e MC |
111 | { |
112 | if (!ssl3_new(s)) | |
b77f3ed1 MC |
113 | return 0; |
114 | if (!s->method->ssl_clear(s)) | |
115 | return 0; | |
116 | ||
117 | return 1; | |
0f113f3e | 118 | } |
58964a49 | 119 | |
6b691a5c | 120 | void tls1_free(SSL *s) |
0f113f3e | 121 | { |
38b051a1 TM |
122 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
123 | ||
124 | if (sc == NULL) | |
125 | return; | |
126 | ||
127 | OPENSSL_free(sc->ext.session_ticket); | |
0f113f3e MC |
128 | ssl3_free(s); |
129 | } | |
58964a49 | 130 | |
b77f3ed1 | 131 | int tls1_clear(SSL *s) |
0f113f3e | 132 | { |
38b051a1 TM |
133 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
134 | ||
135 | if (sc == NULL) | |
136 | return 0; | |
137 | ||
b77f3ed1 MC |
138 | if (!ssl3_clear(s)) |
139 | return 0; | |
140 | ||
4fa52141 | 141 | if (s->method->version == TLS_ANY_VERSION) |
38b051a1 | 142 | sc->version = TLS_MAX_VERSION_INTERNAL; |
4fa52141 | 143 | else |
38b051a1 | 144 | sc->version = s->method->version; |
b77f3ed1 MC |
145 | |
146 | return 1; | |
0f113f3e | 147 | } |
58964a49 | 148 | |
9d2d857f MC |
149 | /* Legacy NID to group_id mapping. Only works for groups we know about */ |
150 | static struct { | |
151 | int nid; | |
152 | uint16_t group_id; | |
153 | } nid_to_group[] = { | |
48e971dd MC |
154 | {NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1}, |
155 | {NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1}, | |
156 | {NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2}, | |
157 | {NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1}, | |
158 | {NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2}, | |
159 | {NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1}, | |
160 | {NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1}, | |
161 | {NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1}, | |
162 | {NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1}, | |
163 | {NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1}, | |
164 | {NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1}, | |
165 | {NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1}, | |
166 | {NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1}, | |
167 | {NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1}, | |
168 | {NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1}, | |
169 | {NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1}, | |
170 | {NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2}, | |
171 | {NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1}, | |
172 | {NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1}, | |
173 | {NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1}, | |
174 | {NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1}, | |
175 | {NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1}, | |
176 | {NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1}, | |
177 | {NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1}, | |
178 | {NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1}, | |
179 | {NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1}, | |
180 | {NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1}, | |
181 | {NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1}, | |
182 | {EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519}, | |
183 | {EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448}, | |
c9ee6e36 MC |
184 | {NID_brainpoolP256r1tls13, OSSL_TLS_GROUP_ID_brainpoolP256r1_tls13}, |
185 | {NID_brainpoolP384r1tls13, OSSL_TLS_GROUP_ID_brainpoolP384r1_tls13}, | |
186 | {NID_brainpoolP512r1tls13, OSSL_TLS_GROUP_ID_brainpoolP512r1_tls13}, | |
0a10825a BE |
187 | {NID_id_tc26_gost_3410_2012_256_paramSetA, OSSL_TLS_GROUP_ID_gc256A}, |
188 | {NID_id_tc26_gost_3410_2012_256_paramSetB, OSSL_TLS_GROUP_ID_gc256B}, | |
189 | {NID_id_tc26_gost_3410_2012_256_paramSetC, OSSL_TLS_GROUP_ID_gc256C}, | |
190 | {NID_id_tc26_gost_3410_2012_256_paramSetD, OSSL_TLS_GROUP_ID_gc256D}, | |
191 | {NID_id_tc26_gost_3410_2012_512_paramSetA, OSSL_TLS_GROUP_ID_gc512A}, | |
192 | {NID_id_tc26_gost_3410_2012_512_paramSetB, OSSL_TLS_GROUP_ID_gc512B}, | |
193 | {NID_id_tc26_gost_3410_2012_512_paramSetC, OSSL_TLS_GROUP_ID_gc512C}, | |
48e971dd MC |
194 | {NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048}, |
195 | {NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072}, | |
196 | {NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096}, | |
197 | {NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144}, | |
198 | {NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192} | |
0f113f3e MC |
199 | }; |
200 | ||
201 | static const unsigned char ecformats_default[] = { | |
202 | TLSEXT_ECPOINTFORMAT_uncompressed, | |
203 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, | |
204 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 | |
205 | }; | |
206 | ||
fe6ef247 | 207 | /* The default curves */ |
9aaecbfc | 208 | static const uint16_t supported_groups_default[] = { |
0a10825a BE |
209 | OSSL_TLS_GROUP_ID_x25519, /* X25519 (29) */ |
210 | OSSL_TLS_GROUP_ID_secp256r1, /* secp256r1 (23) */ | |
211 | OSSL_TLS_GROUP_ID_x448, /* X448 (30) */ | |
212 | OSSL_TLS_GROUP_ID_secp521r1, /* secp521r1 (25) */ | |
213 | OSSL_TLS_GROUP_ID_secp384r1, /* secp384r1 (24) */ | |
214 | OSSL_TLS_GROUP_ID_gc256A, /* GC256A (34) */ | |
215 | OSSL_TLS_GROUP_ID_gc256B, /* GC256B (35) */ | |
216 | OSSL_TLS_GROUP_ID_gc256C, /* GC256C (36) */ | |
217 | OSSL_TLS_GROUP_ID_gc256D, /* GC256D (37) */ | |
218 | OSSL_TLS_GROUP_ID_gc512A, /* GC512A (38) */ | |
219 | OSSL_TLS_GROUP_ID_gc512B, /* GC512B (39) */ | |
220 | OSSL_TLS_GROUP_ID_gc512C, /* GC512C (40) */ | |
221 | OSSL_TLS_GROUP_ID_ffdhe2048, /* ffdhe2048 (0x100) */ | |
222 | OSSL_TLS_GROUP_ID_ffdhe3072, /* ffdhe3072 (0x101) */ | |
223 | OSSL_TLS_GROUP_ID_ffdhe4096, /* ffdhe4096 (0x102) */ | |
224 | OSSL_TLS_GROUP_ID_ffdhe6144, /* ffdhe6144 (0x103) */ | |
225 | OSSL_TLS_GROUP_ID_ffdhe8192, /* ffdhe8192 (0x104) */ | |
de57d237 EK |
226 | }; |
227 | ||
9e84a42d | 228 | static const uint16_t suiteb_curves[] = { |
0a10825a BE |
229 | OSSL_TLS_GROUP_ID_secp256r1, |
230 | OSSL_TLS_GROUP_ID_secp384r1, | |
0f113f3e | 231 | }; |
2ea80354 | 232 | |
9d2d857f MC |
233 | struct provider_group_data_st { |
234 | SSL_CTX *ctx; | |
235 | OSSL_PROVIDER *provider; | |
236 | }; | |
237 | ||
238 | #define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10 | |
239 | static OSSL_CALLBACK add_provider_groups; | |
240 | static int add_provider_groups(const OSSL_PARAM params[], void *data) | |
241 | { | |
242 | struct provider_group_data_st *pgd = data; | |
243 | SSL_CTX *ctx = pgd->ctx; | |
244 | OSSL_PROVIDER *provider = pgd->provider; | |
245 | const OSSL_PARAM *p; | |
246 | TLS_GROUP_INFO *ginf = NULL; | |
247 | EVP_KEYMGMT *keymgmt; | |
248 | unsigned int gid; | |
c1a74f59 | 249 | unsigned int is_kem = 0; |
9d2d857f MC |
250 | int ret = 0; |
251 | ||
252 | if (ctx->group_list_max_len == ctx->group_list_len) { | |
253 | TLS_GROUP_INFO *tmp = NULL; | |
254 | ||
255 | if (ctx->group_list_max_len == 0) | |
256 | tmp = OPENSSL_malloc(sizeof(TLS_GROUP_INFO) | |
257 | * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE); | |
258 | else | |
259 | tmp = OPENSSL_realloc(ctx->group_list, | |
260 | (ctx->group_list_max_len | |
261 | + TLS_GROUP_LIST_MALLOC_BLOCK_SIZE) | |
262 | * sizeof(TLS_GROUP_INFO)); | |
e077455e | 263 | if (tmp == NULL) |
9d2d857f | 264 | return 0; |
9d2d857f MC |
265 | ctx->group_list = tmp; |
266 | memset(tmp + ctx->group_list_max_len, | |
267 | 0, | |
268 | sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE); | |
269 | ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE; | |
270 | } | |
271 | ||
272 | ginf = &ctx->group_list[ctx->group_list_len]; | |
273 | ||
274 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME); | |
275 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | |
6849b73c | 276 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
277 | goto err; |
278 | } | |
279 | ginf->tlsname = OPENSSL_strdup(p->data); | |
e077455e | 280 | if (ginf->tlsname == NULL) |
9d2d857f | 281 | goto err; |
9d2d857f MC |
282 | |
283 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL); | |
284 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | |
6849b73c | 285 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
286 | goto err; |
287 | } | |
288 | ginf->realname = OPENSSL_strdup(p->data); | |
e077455e | 289 | if (ginf->realname == NULL) |
9d2d857f | 290 | goto err; |
9d2d857f MC |
291 | |
292 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID); | |
293 | if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) { | |
6849b73c | 294 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
295 | goto err; |
296 | } | |
297 | ginf->group_id = (uint16_t)gid; | |
298 | ||
299 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG); | |
300 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { | |
6849b73c | 301 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
302 | goto err; |
303 | } | |
304 | ginf->algorithm = OPENSSL_strdup(p->data); | |
e077455e | 305 | if (ginf->algorithm == NULL) |
9d2d857f | 306 | goto err; |
9d2d857f MC |
307 | |
308 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS); | |
309 | if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) { | |
6849b73c | 310 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
311 | goto err; |
312 | } | |
313 | ||
c1a74f59 NT |
314 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM); |
315 | if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) { | |
6849b73c | 316 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
c1a74f59 NT |
317 | goto err; |
318 | } | |
319 | ginf->is_kem = 1 & is_kem; | |
320 | ||
9d2d857f MC |
321 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS); |
322 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) { | |
6849b73c | 323 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
324 | goto err; |
325 | } | |
326 | ||
327 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS); | |
328 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) { | |
6849b73c | 329 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
90a74d8c | 330 | goto err; |
9d2d857f MC |
331 | } |
332 | ||
333 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS); | |
334 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) { | |
6849b73c | 335 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
336 | goto err; |
337 | } | |
338 | ||
339 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS); | |
340 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) { | |
6849b73c | 341 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
9d2d857f MC |
342 | goto err; |
343 | } | |
344 | /* | |
345 | * Now check that the algorithm is actually usable for our property query | |
346 | * string. Regardless of the result we still return success because we have | |
347 | * successfully processed this group, even though we may decide not to use | |
348 | * it. | |
349 | */ | |
350 | ret = 1; | |
ce8822b7 | 351 | ERR_set_mark(); |
9d2d857f MC |
352 | keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq); |
353 | if (keymgmt != NULL) { | |
354 | /* | |
355 | * We have successfully fetched the algorithm - however if the provider | |
356 | * doesn't match this one then we ignore it. | |
357 | * | |
358 | * Note: We're cheating a little here. Technically if the same algorithm | |
359 | * is available from more than one provider then it is undefined which | |
360 | * implementation you will get back. Theoretically this could be | |
361 | * different every time...we assume here that you'll always get the | |
362 | * same one back if you repeat the exact same fetch. Is this a reasonable | |
363 | * assumption to make (in which case perhaps we should document this | |
364 | * behaviour)? | |
365 | */ | |
ed576acd | 366 | if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) { |
9d2d857f MC |
367 | /* We have a match - so we will use this group */ |
368 | ctx->group_list_len++; | |
369 | ginf = NULL; | |
370 | } | |
371 | EVP_KEYMGMT_free(keymgmt); | |
372 | } | |
ce8822b7 | 373 | ERR_pop_to_mark(); |
9d2d857f MC |
374 | err: |
375 | if (ginf != NULL) { | |
376 | OPENSSL_free(ginf->tlsname); | |
377 | OPENSSL_free(ginf->realname); | |
378 | OPENSSL_free(ginf->algorithm); | |
a7863f99 | 379 | ginf->algorithm = ginf->tlsname = ginf->realname = NULL; |
9d2d857f MC |
380 | } |
381 | return ret; | |
382 | } | |
383 | ||
384 | static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx) | |
385 | { | |
386 | struct provider_group_data_st pgd; | |
387 | ||
388 | pgd.ctx = vctx; | |
389 | pgd.provider = provider; | |
390 | return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP", | |
391 | add_provider_groups, &pgd); | |
392 | } | |
393 | ||
394 | int ssl_load_groups(SSL_CTX *ctx) | |
395 | { | |
ddf8f1ce | 396 | size_t i, j, num_deflt_grps = 0; |
8b1db5d3 | 397 | uint16_t tmp_supp_groups[OSSL_NELEM(supported_groups_default)]; |
ddf8f1ce MC |
398 | |
399 | if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx)) | |
400 | return 0; | |
401 | ||
8b1db5d3 | 402 | for (i = 0; i < OSSL_NELEM(supported_groups_default); i++) { |
ddf8f1ce | 403 | for (j = 0; j < ctx->group_list_len; j++) { |
8b1db5d3 | 404 | if (ctx->group_list[j].group_id == supported_groups_default[i]) { |
ddf8f1ce | 405 | tmp_supp_groups[num_deflt_grps++] = ctx->group_list[j].group_id; |
8b1db5d3 MC |
406 | break; |
407 | } | |
ddf8f1ce MC |
408 | } |
409 | } | |
410 | ||
411 | if (num_deflt_grps == 0) | |
412 | return 1; | |
413 | ||
414 | ctx->ext.supported_groups_default | |
415 | = OPENSSL_malloc(sizeof(uint16_t) * num_deflt_grps); | |
416 | ||
e077455e | 417 | if (ctx->ext.supported_groups_default == NULL) |
ddf8f1ce | 418 | return 0; |
ddf8f1ce | 419 | |
8b1db5d3 MC |
420 | memcpy(ctx->ext.supported_groups_default, |
421 | tmp_supp_groups, | |
422 | num_deflt_grps * sizeof(tmp_supp_groups[0])); | |
ddf8f1ce MC |
423 | ctx->ext.supported_groups_default_len = num_deflt_grps; |
424 | ||
425 | return 1; | |
9d2d857f MC |
426 | } |
427 | ||
260009d8 MC |
428 | static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name) |
429 | { | |
430 | size_t i; | |
260009d8 MC |
431 | |
432 | for (i = 0; i < ctx->group_list_len; i++) { | |
433 | if (strcmp(ctx->group_list[i].tlsname, name) == 0 | |
306b8e7e | 434 | || strcmp(ctx->group_list[i].realname, name) == 0) |
260009d8 MC |
435 | return ctx->group_list[i].group_id; |
436 | } | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
9d2d857f | 441 | const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id) |
0f113f3e | 442 | { |
9aaecbfc | 443 | size_t i; |
444 | ||
9d2d857f MC |
445 | for (i = 0; i < ctx->group_list_len; i++) { |
446 | if (ctx->group_list[i].group_id == group_id) | |
447 | return &ctx->group_list[i]; | |
9aaecbfc | 448 | } |
9d2d857f | 449 | |
9aaecbfc | 450 | return NULL; |
0f113f3e | 451 | } |
525de5d3 | 452 | |
260009d8 | 453 | int tls1_group_id2nid(uint16_t group_id, int include_unknown) |
84d4b9e3 | 454 | { |
9d2d857f | 455 | size_t i; |
84d4b9e3 | 456 | |
260009d8 MC |
457 | if (group_id == 0) |
458 | return NID_undef; | |
459 | ||
9d2d857f MC |
460 | /* |
461 | * Return well known Group NIDs - for backwards compatibility. This won't | |
462 | * work for groups we don't know about. | |
463 | */ | |
464 | for (i = 0; i < OSSL_NELEM(nid_to_group); i++) | |
465 | { | |
466 | if (nid_to_group[i].group_id == group_id) | |
467 | return nid_to_group[i].nid; | |
468 | } | |
260009d8 MC |
469 | if (!include_unknown) |
470 | return NID_undef; | |
471 | return TLSEXT_nid_unknown | (int)group_id; | |
84d4b9e3 | 472 | } |
473 | ||
becbacd7 | 474 | uint16_t tls1_nid2group_id(int nid) |
0f113f3e | 475 | { |
2fa2d15a | 476 | size_t i; |
9aaecbfc | 477 | |
9d2d857f MC |
478 | /* |
479 | * Return well known Group ids - for backwards compatibility. This won't | |
480 | * work for groups we don't know about. | |
481 | */ | |
482 | for (i = 0; i < OSSL_NELEM(nid_to_group); i++) | |
483 | { | |
484 | if (nid_to_group[i].nid == nid) | |
485 | return nid_to_group[i].group_id; | |
0f113f3e | 486 | } |
9d2d857f | 487 | |
2fa2d15a | 488 | return 0; |
0f113f3e MC |
489 | } |
490 | ||
740580c2 | 491 | /* |
ff6d20a6 DSH |
492 | * Set *pgroups to the supported groups list and *pgroupslen to |
493 | * the number of groups supported. | |
fd2b65ce | 494 | */ |
38b051a1 | 495 | void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
ff6d20a6 | 496 | size_t *pgroupslen) |
0f113f3e | 497 | { |
38b051a1 TM |
498 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
499 | ||
34e5292c DSH |
500 | /* For Suite B mode only include P-256, P-384 */ |
501 | switch (tls1_suiteb(s)) { | |
502 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
ff6d20a6 DSH |
503 | *pgroups = suiteb_curves; |
504 | *pgroupslen = OSSL_NELEM(suiteb_curves); | |
34e5292c DSH |
505 | break; |
506 | ||
507 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
ff6d20a6 DSH |
508 | *pgroups = suiteb_curves; |
509 | *pgroupslen = 1; | |
34e5292c DSH |
510 | break; |
511 | ||
512 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
ff6d20a6 DSH |
513 | *pgroups = suiteb_curves + 1; |
514 | *pgroupslen = 1; | |
34e5292c DSH |
515 | break; |
516 | ||
517 | default: | |
518 | if (s->ext.supportedgroups == NULL) { | |
38b051a1 TM |
519 | *pgroups = sctx->ext.supported_groups_default; |
520 | *pgroupslen = sctx->ext.supported_groups_default_len; | |
34e5292c | 521 | } else { |
ff6d20a6 DSH |
522 | *pgroups = s->ext.supportedgroups; |
523 | *pgroupslen = s->ext.supportedgroups_len; | |
0f113f3e | 524 | } |
34e5292c | 525 | break; |
0f113f3e | 526 | } |
0f113f3e | 527 | } |
b362ccab | 528 | |
38b051a1 TM |
529 | int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id, |
530 | int minversion, int maxversion, | |
8b1db5d3 | 531 | int isec, int *okfortls13) |
9aaecbfc | 532 | { |
38b051a1 TM |
533 | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
534 | group_id); | |
9d2d857f | 535 | int ret; |
9aaecbfc | 536 | |
8b1db5d3 | 537 | if (okfortls13 != NULL) |
89e14ca7 | 538 | *okfortls13 = 0; |
8b1db5d3 | 539 | |
9d2d857f MC |
540 | if (ginfo == NULL) |
541 | return 0; | |
542 | ||
38b051a1 | 543 | if (SSL_CONNECTION_IS_DTLS(s)) { |
9d2d857f | 544 | if (ginfo->mindtls < 0 || ginfo->maxdtls < 0) |
9aaecbfc | 545 | return 0; |
9d2d857f MC |
546 | if (ginfo->maxdtls == 0) |
547 | ret = 1; | |
548 | else | |
549 | ret = DTLS_VERSION_LE(minversion, ginfo->maxdtls); | |
550 | if (ginfo->mindtls > 0) | |
551 | ret &= DTLS_VERSION_GE(maxversion, ginfo->mindtls); | |
552 | } else { | |
553 | if (ginfo->mintls < 0 || ginfo->maxtls < 0) | |
554 | return 0; | |
555 | if (ginfo->maxtls == 0) | |
556 | ret = 1; | |
557 | else | |
558 | ret = (minversion <= ginfo->maxtls); | |
559 | if (ginfo->mintls > 0) | |
560 | ret &= (maxversion >= ginfo->mintls); | |
8b1db5d3 MC |
561 | if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION) |
562 | *okfortls13 = (ginfo->maxtls == 0) | |
563 | || (ginfo->maxtls >= TLS1_3_VERSION); | |
9aaecbfc | 564 | } |
8b1db5d3 MC |
565 | ret &= !isec |
566 | || strcmp(ginfo->algorithm, "EC") == 0 | |
567 | || strcmp(ginfo->algorithm, "X25519") == 0 | |
568 | || strcmp(ginfo->algorithm, "X448") == 0; | |
9d2d857f MC |
569 | |
570 | return ret; | |
9aaecbfc | 571 | } |
572 | ||
dbc6268f | 573 | /* See if group is allowed by security callback */ |
38b051a1 | 574 | int tls_group_allowed(SSL_CONNECTION *s, uint16_t group, int op) |
0f113f3e | 575 | { |
38b051a1 TM |
576 | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
577 | group); | |
dbc6268f | 578 | unsigned char gtmp[2]; |
5ce5f787 | 579 | |
dbc6268f | 580 | if (ginfo == NULL) |
0f113f3e | 581 | return 0; |
9d2d857f | 582 | |
dbc6268f MC |
583 | gtmp[0] = group >> 8; |
584 | gtmp[1] = group & 0xff; | |
9d2d857f | 585 | return ssl_security(s, op, ginfo->secbits, |
260009d8 | 586 | tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp); |
0f113f3e | 587 | } |
b362ccab | 588 | |
b50951d3 DSH |
589 | /* Return 1 if "id" is in "list" */ |
590 | static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen) | |
591 | { | |
592 | size_t i; | |
593 | for (i = 0; i < listlen; i++) | |
594 | if (list[i] == id) | |
595 | return 1; | |
596 | return 0; | |
597 | } | |
598 | ||
1d97c843 | 599 | /*- |
8841154a | 600 | * For nmatch >= 0, return the id of the |nmatch|th shared group or 0 |
6977e8ee KR |
601 | * if there is no match. |
602 | * For nmatch == -1, return number of matches | |
8841154a | 603 | * For nmatch == -2, return the id of the group to use for |
b50951d3 | 604 | * a tmp key, or 0 if there is no match. |
d0595f17 | 605 | */ |
38b051a1 | 606 | uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch) |
0f113f3e | 607 | { |
9e84a42d | 608 | const uint16_t *pref, *supp; |
b50951d3 | 609 | size_t num_pref, num_supp, i; |
0f113f3e | 610 | int k; |
3e373518 | 611 | |
0f113f3e MC |
612 | /* Can't do anything on client side */ |
613 | if (s->server == 0) | |
8841154a | 614 | return 0; |
0f113f3e MC |
615 | if (nmatch == -2) { |
616 | if (tls1_suiteb(s)) { | |
617 | /* | |
618 | * For Suite B ciphersuite determines curve: we already know | |
619 | * these are acceptable due to previous checks. | |
620 | */ | |
555cbb32 | 621 | unsigned long cid = s->s3.tmp.new_cipher->id; |
3e373518 | 622 | |
0f113f3e | 623 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
0a10825a | 624 | return OSSL_TLS_GROUP_ID_secp256r1; |
0f113f3e | 625 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
0a10825a | 626 | return OSSL_TLS_GROUP_ID_secp384r1; |
0f113f3e | 627 | /* Should never happen */ |
8841154a | 628 | return 0; |
0f113f3e MC |
629 | } |
630 | /* If not Suite B just return first preference shared curve */ | |
631 | nmatch = 0; | |
632 | } | |
633 | /* | |
ff6d20a6 DSH |
634 | * If server preference set, our groups are the preference order |
635 | * otherwise peer decides. | |
0f113f3e | 636 | */ |
ff6d20a6 DSH |
637 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
638 | tls1_get_supported_groups(s, &pref, &num_pref); | |
639 | tls1_get_peer_groups(s, &supp, &num_supp); | |
640 | } else { | |
641 | tls1_get_peer_groups(s, &pref, &num_pref); | |
642 | tls1_get_supported_groups(s, &supp, &num_supp); | |
643 | } | |
3c06513f | 644 | |
9e84a42d DSH |
645 | for (k = 0, i = 0; i < num_pref; i++) { |
646 | uint16_t id = pref[i]; | |
3e373518 | 647 | |
16f0e91c | 648 | if (!tls1_in_list(id, supp, num_supp) |
0a10825a BE |
649 | || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED)) |
650 | continue; | |
b50951d3 DSH |
651 | if (nmatch == k) |
652 | return id; | |
653 | k++; | |
0f113f3e MC |
654 | } |
655 | if (nmatch == -1) | |
656 | return k; | |
657 | /* Out of range (nmatch > k). */ | |
8841154a | 658 | return 0; |
0f113f3e | 659 | } |
d0595f17 | 660 | |
9e84a42d | 661 | int tls1_set_groups(uint16_t **pext, size_t *pextlen, |
de4d764e | 662 | int *groups, size_t ngroups) |
0f113f3e | 663 | { |
9e84a42d | 664 | uint16_t *glist; |
0f113f3e MC |
665 | size_t i; |
666 | /* | |
9aaecbfc | 667 | * Bitmap of groups included to detect duplicates: two variables are added |
668 | * to detect duplicates as some values are more than 32. | |
0f113f3e | 669 | */ |
9aaecbfc | 670 | unsigned long *dup_list = NULL; |
671 | unsigned long dup_list_egrp = 0; | |
672 | unsigned long dup_list_dhgrp = 0; | |
cdb10bae | 673 | |
680bd131 | 674 | if (ngroups == 0) { |
6849b73c | 675 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
680bd131 MC |
676 | return 0; |
677 | } | |
e077455e | 678 | if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) |
0f113f3e | 679 | return 0; |
9e84a42d | 680 | for (i = 0; i < ngroups; i++) { |
0f113f3e | 681 | unsigned long idmask; |
9e84a42d | 682 | uint16_t id; |
4a1b4280 | 683 | id = tls1_nid2group_id(groups[i]); |
9aaecbfc | 684 | if ((id & 0x00FF) >= (sizeof(unsigned long) * 8)) |
685 | goto err; | |
686 | idmask = 1L << (id & 0x00FF); | |
687 | dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp; | |
688 | if (!id || ((*dup_list) & idmask)) | |
689 | goto err; | |
690 | *dup_list |= idmask; | |
9e84a42d | 691 | glist[i] = id; |
0f113f3e | 692 | } |
b548a1f1 | 693 | OPENSSL_free(*pext); |
de4d764e | 694 | *pext = glist; |
9e84a42d | 695 | *pextlen = ngroups; |
0f113f3e | 696 | return 1; |
9aaecbfc | 697 | err: |
698 | OPENSSL_free(glist); | |
699 | return 0; | |
0f113f3e MC |
700 | } |
701 | ||
57e7401f | 702 | # define GROUPLIST_INCREMENT 40 |
0a8e6c1f | 703 | # define GROUP_NAME_BUFFER_LENGTH 64 |
0f113f3e | 704 | typedef struct { |
260009d8 MC |
705 | SSL_CTX *ctx; |
706 | size_t gidcnt; | |
57e7401f MC |
707 | size_t gidmax; |
708 | uint16_t *gid_arr; | |
260009d8 | 709 | } gid_cb_st; |
d0595f17 | 710 | |
260009d8 | 711 | static int gid_cb(const char *elem, int len, void *arg) |
0f113f3e | 712 | { |
260009d8 | 713 | gid_cb_st *garg = arg; |
0f113f3e | 714 | size_t i; |
260009d8 | 715 | uint16_t gid = 0; |
0a8e6c1f | 716 | char etmp[GROUP_NAME_BUFFER_LENGTH]; |
260009d8 | 717 | |
2747d73c KR |
718 | if (elem == NULL) |
719 | return 0; | |
57e7401f MC |
720 | if (garg->gidcnt == garg->gidmax) { |
721 | uint16_t *tmp = | |
722 | OPENSSL_realloc(garg->gid_arr, garg->gidmax + GROUPLIST_INCREMENT); | |
723 | if (tmp == NULL) | |
724 | return 0; | |
725 | garg->gidmax += GROUPLIST_INCREMENT; | |
726 | garg->gid_arr = tmp; | |
727 | } | |
0f113f3e MC |
728 | if (len > (int)(sizeof(etmp) - 1)) |
729 | return 0; | |
730 | memcpy(etmp, elem, len); | |
731 | etmp[len] = 0; | |
260009d8 MC |
732 | |
733 | gid = tls1_group_name2id(garg->ctx, etmp); | |
ce8822b7 DB |
734 | if (gid == 0) { |
735 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, | |
736 | "group '%s' cannot be set", etmp); | |
0f113f3e | 737 | return 0; |
ce8822b7 | 738 | } |
260009d8 MC |
739 | for (i = 0; i < garg->gidcnt; i++) |
740 | if (garg->gid_arr[i] == gid) | |
0f113f3e | 741 | return 0; |
260009d8 | 742 | garg->gid_arr[garg->gidcnt++] = gid; |
0f113f3e MC |
743 | return 1; |
744 | } | |
745 | ||
260009d8 MC |
746 | /* Set groups based on a colon separated list */ |
747 | int tls1_set_groups_list(SSL_CTX *ctx, uint16_t **pext, size_t *pextlen, | |
748 | const char *str) | |
0f113f3e | 749 | { |
260009d8 MC |
750 | gid_cb_st gcb; |
751 | uint16_t *tmparr; | |
57e7401f | 752 | int ret = 0; |
260009d8 MC |
753 | |
754 | gcb.gidcnt = 0; | |
57e7401f MC |
755 | gcb.gidmax = GROUPLIST_INCREMENT; |
756 | gcb.gid_arr = OPENSSL_malloc(gcb.gidmax * sizeof(*gcb.gid_arr)); | |
757 | if (gcb.gid_arr == NULL) | |
758 | return 0; | |
260009d8 MC |
759 | gcb.ctx = ctx; |
760 | if (!CONF_parse_list(str, ':', 1, gid_cb, &gcb)) | |
57e7401f MC |
761 | goto end; |
762 | if (pext == NULL) { | |
763 | ret = 1; | |
764 | goto end; | |
765 | } | |
260009d8 MC |
766 | |
767 | /* | |
768 | * gid_cb ensurse there are no duplicates so we can just go ahead and set | |
769 | * the result | |
770 | */ | |
771 | tmparr = OPENSSL_memdup(gcb.gid_arr, gcb.gidcnt * sizeof(*tmparr)); | |
772 | if (tmparr == NULL) | |
57e7401f | 773 | goto end; |
260009d8 MC |
774 | *pext = tmparr; |
775 | *pextlen = gcb.gidcnt; | |
57e7401f MC |
776 | ret = 1; |
777 | end: | |
778 | OPENSSL_free(gcb.gid_arr); | |
779 | return ret; | |
0f113f3e | 780 | } |
b50951d3 | 781 | |
4a1b4280 | 782 | /* Check a group id matches preferences */ |
38b051a1 TM |
783 | int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id, |
784 | int check_own_groups) | |
4a1b4280 DSH |
785 | { |
786 | const uint16_t *groups; | |
b50951d3 | 787 | size_t groups_len; |
4a1b4280 DSH |
788 | |
789 | if (group_id == 0) | |
790 | return 0; | |
791 | ||
6447e818 | 792 | /* Check for Suite B compliance */ |
555cbb32 TS |
793 | if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) { |
794 | unsigned long cid = s->s3.tmp.new_cipher->id; | |
6447e818 DSH |
795 | |
796 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { | |
0a10825a | 797 | if (group_id != OSSL_TLS_GROUP_ID_secp256r1) |
6447e818 DSH |
798 | return 0; |
799 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { | |
0a10825a | 800 | if (group_id != OSSL_TLS_GROUP_ID_secp384r1) |
6447e818 DSH |
801 | return 0; |
802 | } else { | |
803 | /* Should never happen */ | |
804 | return 0; | |
805 | } | |
806 | } | |
b50951d3 | 807 | |
dcf8b01f MC |
808 | if (check_own_groups) { |
809 | /* Check group is one of our preferences */ | |
810 | tls1_get_supported_groups(s, &groups, &groups_len); | |
811 | if (!tls1_in_list(group_id, groups, groups_len)) | |
812 | return 0; | |
813 | } | |
4a1b4280 | 814 | |
dbc6268f | 815 | if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK)) |
6447e818 DSH |
816 | return 0; |
817 | ||
4a1b4280 DSH |
818 | /* For clients, nothing more to check */ |
819 | if (!s->server) | |
820 | return 1; | |
821 | ||
822 | /* Check group is one of peers preferences */ | |
ff6d20a6 | 823 | tls1_get_peer_groups(s, &groups, &groups_len); |
4a1b4280 DSH |
824 | |
825 | /* | |
826 | * RFC 4492 does not require the supported elliptic curves extension | |
827 | * so if it is not sent we can just choose any curve. | |
828 | * It is invalid to send an empty list in the supported groups | |
829 | * extension, so groups_len == 0 always means no extension. | |
830 | */ | |
831 | if (groups_len == 0) | |
832 | return 1; | |
b50951d3 | 833 | return tls1_in_list(group_id, groups, groups_len); |
0f113f3e | 834 | } |
d61ff83b | 835 | |
38b051a1 | 836 | void tls1_get_formatlist(SSL_CONNECTION *s, const unsigned char **pformats, |
7da160b0 | 837 | size_t *num_formats) |
0f113f3e MC |
838 | { |
839 | /* | |
840 | * If we have a custom point format list use it otherwise use default | |
841 | */ | |
aff8c126 RS |
842 | if (s->ext.ecpointformats) { |
843 | *pformats = s->ext.ecpointformats; | |
844 | *num_formats = s->ext.ecpointformats_len; | |
0f113f3e MC |
845 | } else { |
846 | *pformats = ecformats_default; | |
847 | /* For Suite B we don't support char2 fields */ | |
848 | if (tls1_suiteb(s)) | |
849 | *num_formats = sizeof(ecformats_default) - 1; | |
850 | else | |
851 | *num_formats = sizeof(ecformats_default); | |
852 | } | |
853 | } | |
854 | ||
dbc6268f | 855 | /* Check a key is compatible with compression extension */ |
38b051a1 | 856 | static int tls1_check_pkey_comp(SSL_CONNECTION *s, EVP_PKEY *pkey) |
dbc6268f | 857 | { |
dbc6268f MC |
858 | unsigned char comp_id; |
859 | size_t i; | |
3d34bedf | 860 | int point_conv; |
dbc6268f MC |
861 | |
862 | /* If not an EC key nothing to check */ | |
c2041da8 | 863 | if (!EVP_PKEY_is_a(pkey, "EC")) |
dbc6268f | 864 | return 1; |
5b5eea4b | 865 | |
dbc6268f MC |
866 | |
867 | /* Get required compression id */ | |
3d34bedf MC |
868 | point_conv = EVP_PKEY_get_ec_point_conv_form(pkey); |
869 | if (point_conv == 0) | |
870 | return 0; | |
871 | if (point_conv == POINT_CONVERSION_UNCOMPRESSED) { | |
872 | comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; | |
38b051a1 | 873 | } else if (SSL_CONNECTION_IS_TLS13(s)) { |
5b5eea4b SL |
874 | /* |
875 | * ec_point_formats extension is not used in TLSv1.3 so we ignore | |
876 | * this check. | |
877 | */ | |
878 | return 1; | |
dbc6268f | 879 | } else { |
3d34bedf | 880 | int field_type = EVP_PKEY_get_field_type(pkey); |
dbc6268f | 881 | |
3d34bedf | 882 | if (field_type == NID_X9_62_prime_field) |
dbc6268f | 883 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
3d34bedf | 884 | else if (field_type == NID_X9_62_characteristic_two_field) |
dbc6268f MC |
885 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
886 | else | |
887 | return 0; | |
888 | } | |
889 | /* | |
890 | * If point formats extension present check it, otherwise everything is | |
891 | * supported (see RFC4492). | |
892 | */ | |
cd0fb43c | 893 | if (s->ext.peer_ecpointformats == NULL) |
dbc6268f MC |
894 | return 1; |
895 | ||
cd0fb43c MC |
896 | for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { |
897 | if (s->ext.peer_ecpointformats[i] == comp_id) | |
dbc6268f MC |
898 | return 1; |
899 | } | |
900 | return 0; | |
901 | } | |
902 | ||
903 | /* Return group id of a key */ | |
904 | static uint16_t tls1_get_group_id(EVP_PKEY *pkey) | |
905 | { | |
d8975dec | 906 | int curve_nid = ssl_get_EC_curve_nid(pkey); |
dbc6268f | 907 | |
c2041da8 | 908 | if (curve_nid == NID_undef) |
dbc6268f | 909 | return 0; |
c2041da8 | 910 | return tls1_nid2group_id(curve_nid); |
dbc6268f MC |
911 | } |
912 | ||
0f113f3e MC |
913 | /* |
914 | * Check cert parameters compatible with extensions: currently just checks EC | |
915 | * certificates have compatible curves and compression. | |
d61ff83b | 916 | */ |
38b051a1 | 917 | static int tls1_check_cert_param(SSL_CONNECTION *s, X509 *x, int check_ee_md) |
0f113f3e | 918 | { |
4a1b4280 | 919 | uint16_t group_id; |
0f113f3e | 920 | EVP_PKEY *pkey; |
8382fd3a | 921 | pkey = X509_get0_pubkey(x); |
4a1b4280 | 922 | if (pkey == NULL) |
0f113f3e MC |
923 | return 0; |
924 | /* If not EC nothing to do */ | |
c2041da8 | 925 | if (!EVP_PKEY_is_a(pkey, "EC")) |
0f113f3e | 926 | return 1; |
4a1b4280 DSH |
927 | /* Check compression */ |
928 | if (!tls1_check_pkey_comp(s, pkey)) | |
0f113f3e | 929 | return 0; |
4a1b4280 | 930 | group_id = tls1_get_group_id(pkey); |
dcf8b01f MC |
931 | /* |
932 | * For a server we allow the certificate to not be in our list of supported | |
933 | * groups. | |
934 | */ | |
935 | if (!tls1_check_group_id(s, group_id, !s->server)) | |
0f113f3e MC |
936 | return 0; |
937 | /* | |
938 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or | |
9195ddcd | 939 | * SHA384+P-384. |
0f113f3e | 940 | */ |
9195ddcd | 941 | if (check_ee_md && tls1_suiteb(s)) { |
0f113f3e MC |
942 | int check_md; |
943 | size_t i; | |
9e84a42d | 944 | |
0f113f3e | 945 | /* Check to see we have necessary signing algorithm */ |
0a10825a | 946 | if (group_id == OSSL_TLS_GROUP_ID_secp256r1) |
0f113f3e | 947 | check_md = NID_ecdsa_with_SHA256; |
0a10825a | 948 | else if (group_id == OSSL_TLS_GROUP_ID_secp384r1) |
0f113f3e MC |
949 | check_md = NID_ecdsa_with_SHA384; |
950 | else | |
951 | return 0; /* Should never happen */ | |
29948ac8 BK |
952 | for (i = 0; i < s->shared_sigalgslen; i++) { |
953 | if (check_md == s->shared_sigalgs[i]->sigandhash) | |
1e331727 | 954 | return 1; |
4a1b4280 DSH |
955 | } |
956 | return 0; | |
0f113f3e | 957 | } |
4a1b4280 | 958 | return 1; |
0f113f3e MC |
959 | } |
960 | ||
6977e8ee | 961 | /* |
8483a003 | 962 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
6977e8ee KR |
963 | * @s: SSL connection |
964 | * @cid: Cipher ID we're considering using | |
965 | * | |
966 | * Checks that the kECDHE cipher suite we're considering using | |
967 | * is compatible with the client extensions. | |
968 | * | |
969 | * Returns 0 when the cipher can't be used or 1 when it can. | |
970 | */ | |
38b051a1 | 971 | int tls1_check_ec_tmp_key(SSL_CONNECTION *s, unsigned long cid) |
0f113f3e | 972 | { |
4a1b4280 DSH |
973 | /* If not Suite B just need a shared group */ |
974 | if (!tls1_suiteb(s)) | |
975 | return tls1_shared_group(s, 0) != 0; | |
0f113f3e MC |
976 | /* |
977 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other | |
978 | * curves permitted. | |
979 | */ | |
4a1b4280 | 980 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
0a10825a | 981 | return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp256r1, 1); |
4a1b4280 | 982 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
0a10825a | 983 | return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp384r1, 1); |
4a1b4280 DSH |
984 | |
985 | return 0; | |
0f113f3e | 986 | } |
d0595f17 | 987 | |
703bcee0 | 988 | /* Default sigalg schemes */ |
98c792d1 | 989 | static const uint16_t tls12_sigalgs[] = { |
703bcee0 MC |
990 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
991 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, | |
992 | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, | |
3d234c9e | 993 | TLSEXT_SIGALG_ed25519, |
0e1d6ecf | 994 | TLSEXT_SIGALG_ed448, |
0a10825a BE |
995 | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
996 | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, | |
997 | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, | |
0f113f3e | 998 | |
f55e99f7 BK |
999 | TLSEXT_SIGALG_rsa_pss_pss_sha256, |
1000 | TLSEXT_SIGALG_rsa_pss_pss_sha384, | |
1001 | TLSEXT_SIGALG_rsa_pss_pss_sha512, | |
1002 | TLSEXT_SIGALG_rsa_pss_rsae_sha256, | |
1003 | TLSEXT_SIGALG_rsa_pss_rsae_sha384, | |
1004 | TLSEXT_SIGALG_rsa_pss_rsae_sha512, | |
536199ec | 1005 | |
703bcee0 MC |
1006 | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
1007 | TLSEXT_SIGALG_rsa_pkcs1_sha384, | |
1008 | TLSEXT_SIGALG_rsa_pkcs1_sha512, | |
0f113f3e | 1009 | |
d8311fc9 | 1010 | TLSEXT_SIGALG_ecdsa_sha224, |
42ab2230 | 1011 | TLSEXT_SIGALG_ecdsa_sha1, |
462f4f4b | 1012 | |
d8311fc9 | 1013 | TLSEXT_SIGALG_rsa_pkcs1_sha224, |
42ab2230 | 1014 | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
e376242d | 1015 | |
d8311fc9 | 1016 | TLSEXT_SIGALG_dsa_sha224, |
42ab2230 MC |
1017 | TLSEXT_SIGALG_dsa_sha1, |
1018 | ||
703bcee0 MC |
1019 | TLSEXT_SIGALG_dsa_sha256, |
1020 | TLSEXT_SIGALG_dsa_sha384, | |
41f10305 | 1021 | TLSEXT_SIGALG_dsa_sha512, |
e376242d | 1022 | |
41f10305 | 1023 | #ifndef OPENSSL_NO_GOST |
6f892296 NM |
1024 | TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
1025 | TLSEXT_SIGALG_gostr34102012_512_intrinsic, | |
41f10305 DB |
1026 | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
1027 | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, | |
1028 | TLSEXT_SIGALG_gostr34102001_gostr3411, | |
5eeb6c6e | 1029 | #endif |
fc101f88 | 1030 | }; |
0f113f3e | 1031 | |
462f4f4b | 1032 | |
98c792d1 | 1033 | static const uint16_t suiteb_sigalgs[] = { |
703bcee0 MC |
1034 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1035 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 | |
2ea80354 | 1036 | }; |
aff8c126 | 1037 | |
7a531ee4 | 1038 | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
edbfba1a | 1039 | {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
17ae384e | 1040 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
263ff2c9 | 1041 | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1}, |
edbfba1a | 1042 | {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
17ae384e | 1043 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
263ff2c9 | 1044 | NID_ecdsa_with_SHA384, NID_secp384r1, 1}, |
edbfba1a | 1045 | {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
17ae384e | 1046 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
263ff2c9 | 1047 | NID_ecdsa_with_SHA512, NID_secp521r1, 1}, |
b04d4e38 | 1048 | {"ed25519", TLSEXT_SIGALG_ed25519, |
d2916a5b | 1049 | NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519, |
263ff2c9 | 1050 | NID_undef, NID_undef, 1}, |
0e1d6ecf MC |
1051 | {"ed448", TLSEXT_SIGALG_ed448, |
1052 | NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448, | |
263ff2c9 | 1053 | NID_undef, NID_undef, 1}, |
d8311fc9 MC |
1054 | {NULL, TLSEXT_SIGALG_ecdsa_sha224, |
1055 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, | |
263ff2c9 | 1056 | NID_ecdsa_with_SHA224, NID_undef, 1}, |
edbfba1a | 1057 | {NULL, TLSEXT_SIGALG_ecdsa_sha1, |
17ae384e | 1058 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
263ff2c9 | 1059 | NID_ecdsa_with_SHA1, NID_undef, 1}, |
0a10825a BE |
1060 | {"ecdsa_brainpoolP256r1_sha256", TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
1061 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, | |
1062 | NID_ecdsa_with_SHA256, NID_brainpoolP256r1, 1}, | |
1063 | {"ecdsa_brainpoolP384r1_sha384", TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, | |
1064 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, | |
1065 | NID_ecdsa_with_SHA384, NID_brainpoolP384r1, 1}, | |
1066 | {"ecdsa_brainpoolP512r1_sha512", TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, | |
1067 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, | |
1068 | NID_ecdsa_with_SHA512, NID_brainpoolP512r1, 1}, | |
f55e99f7 BK |
1069 | {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
1070 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, | |
263ff2c9 | 1071 | NID_undef, NID_undef, 1}, |
f55e99f7 BK |
1072 | {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
1073 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, | |
263ff2c9 | 1074 | NID_undef, NID_undef, 1}, |
f55e99f7 BK |
1075 | {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
1076 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, | |
263ff2c9 | 1077 | NID_undef, NID_undef, 1}, |
f55e99f7 | 1078 | {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256, |
17ae384e | 1079 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
263ff2c9 | 1080 | NID_undef, NID_undef, 1}, |
f55e99f7 | 1081 | {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384, |
17ae384e | 1082 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
263ff2c9 | 1083 | NID_undef, NID_undef, 1}, |
f55e99f7 | 1084 | {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512, |
17ae384e | 1085 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
263ff2c9 | 1086 | NID_undef, NID_undef, 1}, |
edbfba1a | 1087 | {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
d0ff28f8 | 1088 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
263ff2c9 | 1089 | NID_sha256WithRSAEncryption, NID_undef, 1}, |
edbfba1a | 1090 | {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
d0ff28f8 | 1091 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
263ff2c9 | 1092 | NID_sha384WithRSAEncryption, NID_undef, 1}, |
edbfba1a | 1093 | {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
d0ff28f8 | 1094 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
263ff2c9 | 1095 | NID_sha512WithRSAEncryption, NID_undef, 1}, |
d8311fc9 MC |
1096 | {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224, |
1097 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, | |
263ff2c9 | 1098 | NID_sha224WithRSAEncryption, NID_undef, 1}, |
edbfba1a | 1099 | {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
d0ff28f8 | 1100 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
263ff2c9 | 1101 | NID_sha1WithRSAEncryption, NID_undef, 1}, |
edbfba1a | 1102 | {NULL, TLSEXT_SIGALG_dsa_sha256, |
17ae384e | 1103 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
263ff2c9 | 1104 | NID_dsa_with_SHA256, NID_undef, 1}, |
edbfba1a | 1105 | {NULL, TLSEXT_SIGALG_dsa_sha384, |
17ae384e | 1106 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
263ff2c9 | 1107 | NID_undef, NID_undef, 1}, |
edbfba1a | 1108 | {NULL, TLSEXT_SIGALG_dsa_sha512, |
17ae384e | 1109 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
263ff2c9 | 1110 | NID_undef, NID_undef, 1}, |
d8311fc9 MC |
1111 | {NULL, TLSEXT_SIGALG_dsa_sha224, |
1112 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, | |
263ff2c9 | 1113 | NID_undef, NID_undef, 1}, |
edbfba1a | 1114 | {NULL, TLSEXT_SIGALG_dsa_sha1, |
17ae384e | 1115 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
263ff2c9 | 1116 | NID_dsaWithSHA1, NID_undef, 1}, |
5eeb6c6e | 1117 | #ifndef OPENSSL_NO_GOST |
6f892296 NM |
1118 | {NULL, TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
1119 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, | |
1120 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, | |
263ff2c9 | 1121 | NID_undef, NID_undef, 1}, |
6f892296 NM |
1122 | {NULL, TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
1123 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, | |
1124 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, | |
263ff2c9 | 1125 | NID_undef, NID_undef, 1}, |
edbfba1a | 1126 | {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
17ae384e DSH |
1127 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
1128 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, | |
263ff2c9 | 1129 | NID_undef, NID_undef, 1}, |
edbfba1a | 1130 | {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
17ae384e DSH |
1131 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
1132 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, | |
263ff2c9 | 1133 | NID_undef, NID_undef, 1}, |
edbfba1a | 1134 | {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
17ae384e DSH |
1135 | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
1136 | NID_id_GostR3410_2001, SSL_PKEY_GOST01, | |
263ff2c9 | 1137 | NID_undef, NID_undef, 1} |
5eeb6c6e | 1138 | #endif |
703bcee0 | 1139 | }; |
0972bc5c DSH |
1140 | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
1141 | static const SIGALG_LOOKUP legacy_rsa_sigalg = { | |
1142 | "rsa_pkcs1_md5_sha1", 0, | |
1143 | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, | |
1144 | EVP_PKEY_RSA, SSL_PKEY_RSA, | |
263ff2c9 | 1145 | NID_undef, NID_undef, 1 |
0972bc5c DSH |
1146 | }; |
1147 | ||
1148 | /* | |
1149 | * Default signature algorithm values used if signature algorithms not present. | |
1150 | * From RFC5246. Note: order must match certificate index order. | |
1151 | */ | |
1152 | static const uint16_t tls_default_sigalg[] = { | |
1153 | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ | |
045d078a | 1154 | 0, /* SSL_PKEY_RSA_PSS_SIGN */ |
0972bc5c DSH |
1155 | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
1156 | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ | |
1157 | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ | |
6f892296 NM |
1158 | TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */ |
1159 | TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */ | |
0e1d6ecf MC |
1160 | 0, /* SSL_PKEY_ED25519 */ |
1161 | 0, /* SSL_PKEY_ED448 */ | |
0972bc5c | 1162 | }; |
703bcee0 | 1163 | |
263ff2c9 MC |
1164 | int ssl_setup_sig_algs(SSL_CTX *ctx) |
1165 | { | |
1166 | size_t i; | |
1167 | const SIGALG_LOOKUP *lu; | |
1168 | SIGALG_LOOKUP *cache | |
1169 | = OPENSSL_malloc(sizeof(*lu) * OSSL_NELEM(sigalg_lookup_tbl)); | |
1170 | EVP_PKEY *tmpkey = EVP_PKEY_new(); | |
1171 | int ret = 0; | |
1172 | ||
1173 | if (cache == NULL || tmpkey == NULL) | |
1174 | goto err; | |
1175 | ||
1176 | ERR_set_mark(); | |
1177 | for (i = 0, lu = sigalg_lookup_tbl; | |
1178 | i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { | |
1179 | EVP_PKEY_CTX *pctx; | |
1180 | ||
1181 | cache[i] = *lu; | |
1182 | ||
1183 | /* | |
1184 | * Check hash is available. | |
57e7401f | 1185 | * This test is not perfect. A provider could have support |
263ff2c9 MC |
1186 | * for a signature scheme, but not a particular hash. However the hash |
1187 | * could be available from some other loaded provider. In that case it | |
1188 | * could be that the signature is available, and the hash is available | |
1189 | * independently - but not as a combination. We ignore this for now. | |
1190 | */ | |
1191 | if (lu->hash != NID_undef | |
1192 | && ctx->ssl_digest_methods[lu->hash_idx] == NULL) { | |
1193 | cache[i].enabled = 0; | |
1194 | continue; | |
1195 | } | |
1196 | ||
1197 | if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { | |
1198 | cache[i].enabled = 0; | |
1199 | continue; | |
1200 | } | |
1201 | pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq); | |
1202 | /* If unable to create pctx we assume the sig algorithm is unavailable */ | |
1203 | if (pctx == NULL) | |
1204 | cache[i].enabled = 0; | |
1205 | EVP_PKEY_CTX_free(pctx); | |
1206 | } | |
1207 | ERR_pop_to_mark(); | |
1208 | ctx->sigalg_lookup_cache = cache; | |
1209 | cache = NULL; | |
1210 | ||
1211 | ret = 1; | |
1212 | err: | |
1213 | OPENSSL_free(cache); | |
1214 | EVP_PKEY_free(tmpkey); | |
1215 | return ret; | |
1216 | } | |
1217 | ||
4d43ee28 | 1218 | /* Lookup TLS signature algorithm */ |
38b051a1 TM |
1219 | static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL_CONNECTION *s, |
1220 | uint16_t sigalg) | |
703bcee0 MC |
1221 | { |
1222 | size_t i; | |
263ff2c9 | 1223 | const SIGALG_LOOKUP *lu; |
703bcee0 | 1224 | |
38b051a1 | 1225 | for (i = 0, lu = SSL_CONNECTION_GET_CTX(s)->sigalg_lookup_cache; |
263ff2c9 MC |
1226 | /* cache should have the same number of elements as sigalg_lookup_tbl */ |
1227 | i < OSSL_NELEM(sigalg_lookup_tbl); | |
1228 | lu++, i++) { | |
54e3efff MC |
1229 | if (lu->sigalg == sigalg) { |
1230 | if (!lu->enabled) | |
1231 | return NULL; | |
263ff2c9 | 1232 | return lu; |
54e3efff | 1233 | } |
703bcee0 | 1234 | } |
4d43ee28 DSH |
1235 | return NULL; |
1236 | } | |
168067b6 | 1237 | /* Lookup hash: return 0 if invalid or not enabled */ |
c8f6c28a | 1238 | int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd) |
168067b6 DSH |
1239 | { |
1240 | const EVP_MD *md; | |
38b051a1 | 1241 | |
168067b6 DSH |
1242 | if (lu == NULL) |
1243 | return 0; | |
1244 | /* lu->hash == NID_undef means no associated digest */ | |
1245 | if (lu->hash == NID_undef) { | |
1246 | md = NULL; | |
1247 | } else { | |
c8f6c28a | 1248 | md = ssl_md(ctx, lu->hash_idx); |
168067b6 DSH |
1249 | if (md == NULL) |
1250 | return 0; | |
1251 | } | |
1252 | if (pmd) | |
1253 | *pmd = md; | |
1254 | return 1; | |
1255 | } | |
1256 | ||
0fe3db25 NR |
1257 | /* |
1258 | * Check if key is large enough to generate RSA-PSS signature. | |
1259 | * | |
1260 | * The key must greater than or equal to 2 * hash length + 2. | |
1261 | * SHA512 has a hash length of 64 bytes, which is incompatible | |
1262 | * with a 128 byte (1024 bit) key. | |
1263 | */ | |
ed576acd | 1264 | #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_get_size(md) + 2) |
c5f87134 | 1265 | static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey, |
c8f6c28a | 1266 | const SIGALG_LOOKUP *lu) |
0fe3db25 NR |
1267 | { |
1268 | const EVP_MD *md; | |
1269 | ||
c5f87134 | 1270 | if (pkey == NULL) |
0fe3db25 | 1271 | return 0; |
c8f6c28a | 1272 | if (!tls1_lookup_md(ctx, lu, &md) || md == NULL) |
0fe3db25 | 1273 | return 0; |
ed576acd | 1274 | if (EVP_PKEY_get_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md)) |
0fe3db25 NR |
1275 | return 0; |
1276 | return 1; | |
1277 | } | |
1278 | ||
0972bc5c | 1279 | /* |
b0031e5d KR |
1280 | * Returns a signature algorithm when the peer did not send a list of supported |
1281 | * signature algorithms. The signature algorithm is fixed for the certificate | |
1282 | * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the | |
1283 | * certificate type from |s| will be used. | |
1284 | * Returns the signature algorithm to use, or NULL on error. | |
0972bc5c | 1285 | */ |
38b051a1 TM |
1286 | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s, |
1287 | int idx) | |
0972bc5c | 1288 | { |
7f6b466b DSH |
1289 | if (idx == -1) { |
1290 | if (s->server) { | |
1291 | size_t i; | |
1292 | ||
1293 | /* Work out index corresponding to ciphersuite */ | |
1294 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
1295 | const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i); | |
1296 | ||
ed5b26ce P |
1297 | if (clu == NULL) |
1298 | continue; | |
555cbb32 | 1299 | if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) { |
7f6b466b DSH |
1300 | idx = i; |
1301 | break; | |
1302 | } | |
1303 | } | |
1f65c045 DB |
1304 | |
1305 | /* | |
1306 | * Some GOST ciphersuites allow more than one signature algorithms | |
1307 | * */ | |
555cbb32 | 1308 | if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) { |
1f65c045 DB |
1309 | int real_idx; |
1310 | ||
1311 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01; | |
1312 | real_idx--) { | |
1313 | if (s->cert->pkeys[real_idx].privatekey != NULL) { | |
1314 | idx = real_idx; | |
1315 | break; | |
1316 | } | |
1317 | } | |
1318 | } | |
5a5530a2 DB |
1319 | /* |
1320 | * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used | |
1321 | * with new (aGOST12-only) ciphersuites, we should find out which one is available really. | |
1322 | */ | |
1323 | else if (idx == SSL_PKEY_GOST12_256) { | |
1324 | int real_idx; | |
1325 | ||
1326 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256; | |
1327 | real_idx--) { | |
1328 | if (s->cert->pkeys[real_idx].privatekey != NULL) { | |
1329 | idx = real_idx; | |
1330 | break; | |
1331 | } | |
1332 | } | |
1333 | } | |
7f6b466b DSH |
1334 | } else { |
1335 | idx = s->cert->key - s->cert->pkeys; | |
1336 | } | |
1337 | } | |
0972bc5c DSH |
1338 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
1339 | return NULL; | |
1340 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { | |
263ff2c9 | 1341 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, tls_default_sigalg[idx]); |
0972bc5c | 1342 | |
54e3efff MC |
1343 | if (lu == NULL) |
1344 | return NULL; | |
38b051a1 | 1345 | if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, NULL)) |
0972bc5c | 1346 | return NULL; |
b0031e5d KR |
1347 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
1348 | return NULL; | |
0972bc5c DSH |
1349 | return lu; |
1350 | } | |
b0031e5d KR |
1351 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg)) |
1352 | return NULL; | |
0972bc5c DSH |
1353 | return &legacy_rsa_sigalg; |
1354 | } | |
1355 | /* Set peer sigalg based key type */ | |
38b051a1 | 1356 | int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey) |
0972bc5c | 1357 | { |
52fd27f9 DSH |
1358 | size_t idx; |
1359 | const SIGALG_LOOKUP *lu; | |
0972bc5c | 1360 | |
52fd27f9 DSH |
1361 | if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) |
1362 | return 0; | |
1363 | lu = tls1_get_legacy_sigalg(s, idx); | |
0972bc5c DSH |
1364 | if (lu == NULL) |
1365 | return 0; | |
555cbb32 | 1366 | s->s3.tmp.peer_sigalg = lu; |
0972bc5c DSH |
1367 | return 1; |
1368 | } | |
703bcee0 | 1369 | |
38b051a1 | 1370 | size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent, const uint16_t **psigs) |
0f113f3e MC |
1371 | { |
1372 | /* | |
1373 | * If Suite B mode use Suite B sigalgs only, ignore any other | |
1374 | * preferences. | |
1375 | */ | |
0f113f3e MC |
1376 | switch (tls1_suiteb(s)) { |
1377 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
1378 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 1379 | return OSSL_NELEM(suiteb_sigalgs); |
0f113f3e MC |
1380 | |
1381 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
1382 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 1383 | return 1; |
0f113f3e MC |
1384 | |
1385 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
7a531ee4 MC |
1386 | *psigs = suiteb_sigalgs + 1; |
1387 | return 1; | |
0f113f3e | 1388 | } |
a9669ddc DSH |
1389 | /* |
1390 | * We use client_sigalgs (if not NULL) if we're a server | |
1391 | * and sending a certificate request or if we're a client and | |
1392 | * determining which shared algorithm to use. | |
1393 | */ | |
1394 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { | |
0f113f3e MC |
1395 | *psigs = s->cert->client_sigalgs; |
1396 | return s->cert->client_sigalgslen; | |
1397 | } else if (s->cert->conf_sigalgs) { | |
1398 | *psigs = s->cert->conf_sigalgs; | |
1399 | return s->cert->conf_sigalgslen; | |
1400 | } else { | |
1401 | *psigs = tls12_sigalgs; | |
703bcee0 | 1402 | return OSSL_NELEM(tls12_sigalgs); |
0f113f3e MC |
1403 | } |
1404 | } | |
1405 | ||
de4dc598 MC |
1406 | /* |
1407 | * Called by servers only. Checks that we have a sig alg that supports the | |
1408 | * specified EC curve. | |
1409 | */ | |
38b051a1 | 1410 | int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve) |
de4dc598 MC |
1411 | { |
1412 | const uint16_t *sigs; | |
1413 | size_t siglen, i; | |
1414 | ||
1415 | if (s->cert->conf_sigalgs) { | |
1416 | sigs = s->cert->conf_sigalgs; | |
1417 | siglen = s->cert->conf_sigalgslen; | |
1418 | } else { | |
1419 | sigs = tls12_sigalgs; | |
1420 | siglen = OSSL_NELEM(tls12_sigalgs); | |
1421 | } | |
1422 | ||
1423 | for (i = 0; i < siglen; i++) { | |
263ff2c9 | 1424 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, sigs[i]); |
de4dc598 MC |
1425 | |
1426 | if (lu == NULL) | |
1427 | continue; | |
1428 | if (lu->sig == EVP_PKEY_EC | |
1429 | && lu->curve != NID_undef | |
1430 | && curve == lu->curve) | |
1431 | return 1; | |
1432 | } | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
620c97b6 KR |
1437 | /* |
1438 | * Return the number of security bits for the signature algorithm, or 0 on | |
1439 | * error. | |
1440 | */ | |
1441 | static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu) | |
1442 | { | |
1443 | const EVP_MD *md = NULL; | |
1444 | int secbits = 0; | |
1445 | ||
1446 | if (!tls1_lookup_md(ctx, lu, &md)) | |
1447 | return 0; | |
1448 | if (md != NULL) | |
1449 | { | |
ed576acd | 1450 | int md_type = EVP_MD_get_type(md); |
aba03ae5 | 1451 | |
620c97b6 | 1452 | /* Security bits: half digest bits */ |
ed576acd | 1453 | secbits = EVP_MD_get_size(md) * 4; |
aba03ae5 KR |
1454 | /* |
1455 | * SHA1 and MD5 are known to be broken. Reduce security bits so that | |
1456 | * they're no longer accepted at security level 1. The real values don't | |
1457 | * really matter as long as they're lower than 80, which is our | |
1458 | * security level 1. | |
1459 | * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for | |
1460 | * SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2 | |
1461 | * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf | |
1462 | * puts a chosen-prefix attack for MD5 at 2^39. | |
5ea4d764 | 1463 | */ |
aba03ae5 KR |
1464 | if (md_type == NID_sha1) |
1465 | secbits = 64; | |
1466 | else if (md_type == NID_md5_sha1) | |
1467 | secbits = 67; | |
1468 | else if (md_type == NID_md5) | |
1469 | secbits = 39; | |
620c97b6 KR |
1470 | } else { |
1471 | /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */ | |
1472 | if (lu->sigalg == TLSEXT_SIGALG_ed25519) | |
1473 | secbits = 128; | |
1474 | else if (lu->sigalg == TLSEXT_SIGALG_ed448) | |
1475 | secbits = 224; | |
1476 | } | |
1477 | return secbits; | |
1478 | } | |
1479 | ||
0f113f3e MC |
1480 | /* |
1481 | * Check signature algorithm is consistent with sent supported signature | |
b2eb6998 DSH |
1482 | * algorithms and if so set relevant digest and signature scheme in |
1483 | * s. | |
ec4a50b3 | 1484 | */ |
38b051a1 | 1485 | int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) |
0f113f3e | 1486 | { |
98c792d1 | 1487 | const uint16_t *sent_sigs; |
5554facb | 1488 | const EVP_MD *md = NULL; |
703bcee0 | 1489 | char sigalgstr[2]; |
11d2641f | 1490 | size_t sent_sigslen, i, cidx; |
c2041da8 | 1491 | int pkeyid = -1; |
f742cda8 | 1492 | const SIGALG_LOOKUP *lu; |
620c97b6 | 1493 | int secbits = 0; |
4d43ee28 | 1494 | |
ed576acd | 1495 | pkeyid = EVP_PKEY_get_id(pkey); |
0f113f3e | 1496 | /* Should never happen */ |
536199ec | 1497 | if (pkeyid == -1) |
0f113f3e | 1498 | return -1; |
38b051a1 | 1499 | if (SSL_CONNECTION_IS_TLS13(s)) { |
5a8916d9 DSH |
1500 | /* Disallow DSA for TLS 1.3 */ |
1501 | if (pkeyid == EVP_PKEY_DSA) { | |
c48ffbcc | 1502 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
5a8916d9 DSH |
1503 | return 0; |
1504 | } | |
1505 | /* Only allow PSS for TLS 1.3 */ | |
1506 | if (pkeyid == EVP_PKEY_RSA) | |
1507 | pkeyid = EVP_PKEY_RSA_PSS; | |
1508 | } | |
263ff2c9 | 1509 | lu = tls1_lookup_sigalg(s, sig); |
f742cda8 | 1510 | /* |
d8311fc9 MC |
1511 | * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type |
1512 | * is consistent with signature: RSA keys can be used for RSA-PSS | |
f742cda8 | 1513 | */ |
d8311fc9 | 1514 | if (lu == NULL |
38b051a1 TM |
1515 | || (SSL_CONNECTION_IS_TLS13(s) |
1516 | && (lu->hash == NID_sha1 || lu->hash == NID_sha224)) | |
095a982b | 1517 | || (pkeyid != lu->sig |
f742cda8 | 1518 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
c48ffbcc | 1519 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
0f113f3e MC |
1520 | return 0; |
1521 | } | |
11d2641f | 1522 | /* Check the sigalg is consistent with the key OID */ |
ed576acd | 1523 | if (!ssl_cert_lookup_by_nid(EVP_PKEY_get_id(pkey), &cidx) |
11d2641f | 1524 | || lu->sig_idx != (int)cidx) { |
c48ffbcc | 1525 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
11d2641f MC |
1526 | return 0; |
1527 | } | |
1528 | ||
fe3066ee | 1529 | if (pkeyid == EVP_PKEY_EC) { |
44b6318f | 1530 | |
4a1b4280 DSH |
1531 | /* Check point compression is permitted */ |
1532 | if (!tls1_check_pkey_comp(s, pkey)) { | |
f63a17d6 | 1533 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 1534 | SSL_R_ILLEGAL_POINT_COMPRESSION); |
4a1b4280 DSH |
1535 | return 0; |
1536 | } | |
1537 | ||
1538 | /* For TLS 1.3 or Suite B check curve matches signature algorithm */ | |
38b051a1 | 1539 | if (SSL_CONNECTION_IS_TLS13(s) || tls1_suiteb(s)) { |
d8975dec | 1540 | int curve = ssl_get_EC_curve_nid(pkey); |
4a1b4280 | 1541 | |
a34a9df0 | 1542 | if (lu->curve != NID_undef && curve != lu->curve) { |
c48ffbcc | 1543 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
8f88cb53 DSH |
1544 | return 0; |
1545 | } | |
4a1b4280 | 1546 | } |
38b051a1 | 1547 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
4a1b4280 | 1548 | /* Check curve matches extensions */ |
dcf8b01f | 1549 | if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { |
c48ffbcc | 1550 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
8f88cb53 DSH |
1551 | return 0; |
1552 | } | |
8f88cb53 | 1553 | if (tls1_suiteb(s)) { |
f1adb006 DSH |
1554 | /* Check sigalg matches a permissible Suite B value */ |
1555 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 | |
1556 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { | |
f63a17d6 | 1557 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 1558 | SSL_R_WRONG_SIGNATURE_TYPE); |
0f113f3e | 1559 | return 0; |
f1adb006 | 1560 | } |
8f88cb53 | 1561 | } |
0f113f3e | 1562 | } |
8f88cb53 | 1563 | } else if (tls1_suiteb(s)) { |
c48ffbcc | 1564 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
0f113f3e | 1565 | return 0; |
8f88cb53 | 1566 | } |
0f113f3e MC |
1567 | |
1568 | /* Check signature matches a type we sent */ | |
a9669ddc | 1569 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
536199ec | 1570 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
703bcee0 | 1571 | if (sig == *sent_sigs) |
0f113f3e MC |
1572 | break; |
1573 | } | |
1574 | /* Allow fallback to SHA1 if not strict mode */ | |
f742cda8 DSH |
1575 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
1576 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
c48ffbcc | 1577 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
0f113f3e MC |
1578 | return 0; |
1579 | } | |
38b051a1 | 1580 | if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, &md)) { |
c48ffbcc | 1581 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST); |
f63a17d6 | 1582 | return 0; |
0f113f3e | 1583 | } |
620c97b6 KR |
1584 | /* |
1585 | * Make sure security callback allows algorithm. For historical | |
1586 | * reasons we have to pass the sigalg as a two byte char array. | |
1587 | */ | |
1588 | sigalgstr[0] = (sig >> 8) & 0xff; | |
1589 | sigalgstr[1] = sig & 0xff; | |
38b051a1 | 1590 | secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
620c97b6 KR |
1591 | if (secbits == 0 || |
1592 | !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits, | |
ed576acd | 1593 | md != NULL ? EVP_MD_get_type(md) : NID_undef, |
620c97b6 | 1594 | (void *)sigalgstr)) { |
c48ffbcc | 1595 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
620c97b6 | 1596 | return 0; |
0f113f3e | 1597 | } |
6cbebb55 | 1598 | /* Store the sigalg the peer uses */ |
555cbb32 | 1599 | s->s3.tmp.peer_sigalg = lu; |
0f113f3e MC |
1600 | return 1; |
1601 | } | |
2ea80354 | 1602 | |
42ef7aea DSH |
1603 | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
1604 | { | |
38b051a1 TM |
1605 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1606 | ||
1607 | if (sc == NULL) | |
42ef7aea | 1608 | return 0; |
38b051a1 TM |
1609 | |
1610 | if (sc->s3.tmp.peer_sigalg == NULL) | |
1611 | return 0; | |
1612 | *pnid = sc->s3.tmp.peer_sigalg->sig; | |
42ef7aea DSH |
1613 | return 1; |
1614 | } | |
1615 | ||
a51c9f63 VD |
1616 | int SSL_get_signature_type_nid(const SSL *s, int *pnid) |
1617 | { | |
38b051a1 TM |
1618 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
1619 | ||
1620 | if (sc == NULL) | |
1621 | return 0; | |
1622 | ||
1623 | if (sc->s3.tmp.sigalg == NULL) | |
a51c9f63 | 1624 | return 0; |
38b051a1 | 1625 | *pnid = sc->s3.tmp.sigalg->sig; |
a51c9f63 VD |
1626 | return 1; |
1627 | } | |
1628 | ||
0f113f3e | 1629 | /* |
3eb2aff4 KR |
1630 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
1631 | * supported, doesn't appear in supported signature algorithms, isn't supported | |
1632 | * by the enabled protocol versions or by the security level. | |
1633 | * | |
1634 | * This function should only be used for checking which ciphers are supported | |
1635 | * by the client. | |
1636 | * | |
1637 | * Call ssl_cipher_disabled() to check that it's enabled or not. | |
b7bfe69b | 1638 | */ |
38b051a1 | 1639 | int ssl_set_client_disabled(SSL_CONNECTION *s) |
0f113f3e | 1640 | { |
555cbb32 TS |
1641 | s->s3.tmp.mask_a = 0; |
1642 | s->s3.tmp.mask_k = 0; | |
1643 | ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); | |
1644 | if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver, | |
1645 | &s->s3.tmp.max_ver, NULL) != 0) | |
1d0c08b4 | 1646 | return 0; |
a230b26e | 1647 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
1648 | /* with PSK there must be client callback set */ |
1649 | if (!s->psk_client_callback) { | |
555cbb32 TS |
1650 | s->s3.tmp.mask_a |= SSL_aPSK; |
1651 | s->s3.tmp.mask_k |= SSL_PSK; | |
0f113f3e | 1652 | } |
a230b26e | 1653 | #endif /* OPENSSL_NO_PSK */ |
e481f9b9 | 1654 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 1655 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
555cbb32 TS |
1656 | s->s3.tmp.mask_a |= SSL_aSRP; |
1657 | s->s3.tmp.mask_k |= SSL_kSRP; | |
0f113f3e | 1658 | } |
e481f9b9 | 1659 | #endif |
1d0c08b4 | 1660 | return 1; |
0f113f3e | 1661 | } |
fc101f88 | 1662 | |
3eb2aff4 KR |
1663 | /* |
1664 | * ssl_cipher_disabled - check that a cipher is disabled or not | |
1665 | * @s: SSL connection that you want to use the cipher on | |
1666 | * @c: cipher to check | |
1667 | * @op: Security check that you want to do | |
8af91fd9 | 1668 | * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 |
3eb2aff4 KR |
1669 | * |
1670 | * Returns 1 when it's disabled, 0 when enabled. | |
1671 | */ | |
38b051a1 TM |
1672 | int ssl_cipher_disabled(const SSL_CONNECTION *s, const SSL_CIPHER *c, |
1673 | int op, int ecdhe) | |
0f113f3e | 1674 | { |
555cbb32 TS |
1675 | if (c->algorithm_mkey & s->s3.tmp.mask_k |
1676 | || c->algorithm_auth & s->s3.tmp.mask_a) | |
0f113f3e | 1677 | return 1; |
555cbb32 | 1678 | if (s->s3.tmp.max_ver == 0) |
3eb2aff4 | 1679 | return 1; |
38b051a1 | 1680 | if (!SSL_CONNECTION_IS_DTLS(s)) { |
8af91fd9 MC |
1681 | int min_tls = c->min_tls; |
1682 | ||
1683 | /* | |
1684 | * For historical reasons we will allow ECHDE to be selected by a server | |
1685 | * in SSLv3 if we are a client | |
1686 | */ | |
1687 | if (min_tls == TLS1_VERSION && ecdhe | |
1688 | && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) | |
1689 | min_tls = SSL3_VERSION; | |
1690 | ||
555cbb32 | 1691 | if ((min_tls > s->s3.tmp.max_ver) || (c->max_tls < s->s3.tmp.min_ver)) |
8af91fd9 MC |
1692 | return 1; |
1693 | } | |
38b051a1 TM |
1694 | if (SSL_CONNECTION_IS_DTLS(s) |
1695 | && (DTLS_VERSION_GT(c->min_dtls, s->s3.tmp.max_ver) | |
1696 | || DTLS_VERSION_LT(c->max_dtls, s->s3.tmp.min_ver))) | |
3eb2aff4 KR |
1697 | return 1; |
1698 | ||
0f113f3e MC |
1699 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
1700 | } | |
b362ccab | 1701 | |
38b051a1 | 1702 | int tls_use_ticket(SSL_CONNECTION *s) |
0f113f3e | 1703 | { |
08191294 | 1704 | if ((s->options & SSL_OP_NO_TICKET)) |
0f113f3e MC |
1705 | return 0; |
1706 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); | |
1707 | } | |
ed3883d2 | 1708 | |
38b051a1 | 1709 | int tls1_set_server_sigalgs(SSL_CONNECTION *s) |
0f113f3e | 1710 | { |
0f113f3e | 1711 | size_t i; |
8483a003 F |
1712 | |
1713 | /* Clear any shared signature algorithms */ | |
29948ac8 BK |
1714 | OPENSSL_free(s->shared_sigalgs); |
1715 | s->shared_sigalgs = NULL; | |
1716 | s->shared_sigalgslen = 0; | |
9195ddcd DSH |
1717 | /* Clear certificate validity flags */ |
1718 | for (i = 0; i < SSL_PKEY_NUM; i++) | |
555cbb32 | 1719 | s->s3.tmp.valid_flags[i] = 0; |
a8bb912d DSH |
1720 | /* |
1721 | * If peer sent no signature algorithms check to see if we support | |
1722 | * the default algorithm for each certificate type | |
1723 | */ | |
555cbb32 TS |
1724 | if (s->s3.tmp.peer_cert_sigalgs == NULL |
1725 | && s->s3.tmp.peer_sigalgs == NULL) { | |
a8bb912d DSH |
1726 | const uint16_t *sent_sigs; |
1727 | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
0f113f3e | 1728 | |
a8bb912d DSH |
1729 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
1730 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); | |
1731 | size_t j; | |
1732 | ||
1733 | if (lu == NULL) | |
1734 | continue; | |
1735 | /* Check default matches a type we sent */ | |
1736 | for (j = 0; j < sent_sigslen; j++) { | |
1737 | if (lu->sigalg == sent_sigs[j]) { | |
555cbb32 | 1738 | s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN; |
a8bb912d DSH |
1739 | break; |
1740 | } | |
1741 | } | |
1742 | } | |
9195ddcd | 1743 | return 1; |
a8bb912d | 1744 | } |
9195ddcd DSH |
1745 | |
1746 | if (!tls1_process_sigalgs(s)) { | |
c48ffbcc | 1747 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 | 1748 | return 0; |
d376e57d | 1749 | } |
29948ac8 | 1750 | if (s->shared_sigalgs != NULL) |
9195ddcd | 1751 | return 1; |
f63a17d6 | 1752 | |
fb34a0f4 | 1753 | /* Fatal error if no shared signature algorithms */ |
c48ffbcc | 1754 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 1755 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
0f113f3e MC |
1756 | return 0; |
1757 | } | |
e469af8d | 1758 | |
1d97c843 | 1759 | /*- |
1ab3836b | 1760 | * Gets the ticket information supplied by the client if any. |
e7f0d921 | 1761 | * |
1ab3836b | 1762 | * hello: The parsed ClientHello data |
c519e89f BM |
1763 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
1764 | * point to the resulting session. | |
6434abbf | 1765 | */ |
38b051a1 TM |
1766 | SSL_TICKET_STATUS tls_get_ticket_from_client(SSL_CONNECTION *s, |
1767 | CLIENTHELLO_MSG *hello, | |
df0fed9a | 1768 | SSL_SESSION **ret) |
0f113f3e | 1769 | { |
1ab3836b MC |
1770 | size_t size; |
1771 | RAW_EXTENSION *ticketext; | |
e7f0d921 | 1772 | |
0f113f3e | 1773 | *ret = NULL; |
aff8c126 | 1774 | s->ext.ticket_expected = 0; |
0f113f3e MC |
1775 | |
1776 | /* | |
9362c93e MC |
1777 | * If tickets disabled or not supported by the protocol version |
1778 | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful | |
0f113f3e MC |
1779 | * resumption. |
1780 | */ | |
1ab3836b | 1781 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
df0fed9a | 1782 | return SSL_TICKET_NONE; |
9ceb2426 | 1783 | |
70af3d8e MC |
1784 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
1785 | if (!ticketext->present) | |
df0fed9a | 1786 | return SSL_TICKET_NONE; |
1ab3836b MC |
1787 | |
1788 | size = PACKET_remaining(&ticketext->data); | |
70af3d8e | 1789 | |
c0638ade | 1790 | return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
70af3d8e | 1791 | hello->session_id, hello->session_id_len, ret); |
1ab3836b MC |
1792 | } |
1793 | ||
1d97c843 TH |
1794 | /*- |
1795 | * tls_decrypt_ticket attempts to decrypt a session ticket. | |
c519e89f | 1796 | * |
61fb5923 MC |
1797 | * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are |
1798 | * expecting a pre-shared key ciphersuite, in which case we have no use for | |
1799 | * session tickets and one will never be decrypted, nor will | |
1800 | * s->ext.ticket_expected be set to 1. | |
1801 | * | |
1802 | * Side effects: | |
1803 | * Sets s->ext.ticket_expected to 1 if the server will have to issue | |
1804 | * a new session ticket to the client because the client indicated support | |
1805 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | |
1806 | * a session ticket or we couldn't use the one it gave us, or if | |
1807 | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. | |
1808 | * Otherwise, s->ext.ticket_expected is set to 0. | |
1809 | * | |
c519e89f | 1810 | * etick: points to the body of the session ticket extension. |
8483a003 | 1811 | * eticklen: the length of the session tickets extension. |
c519e89f BM |
1812 | * sess_id: points at the session ID. |
1813 | * sesslen: the length of the session ID. | |
1814 | * psess: (output) on return, if a ticket was decrypted, then this is set to | |
1815 | * point to the resulting session. | |
c519e89f | 1816 | */ |
38b051a1 TM |
1817 | SSL_TICKET_STATUS tls_decrypt_ticket(SSL_CONNECTION *s, |
1818 | const unsigned char *etick, | |
1819 | size_t eticklen, | |
1820 | const unsigned char *sess_id, | |
df0fed9a | 1821 | size_t sesslen, SSL_SESSION **psess) |
0f113f3e | 1822 | { |
61fb5923 | 1823 | SSL_SESSION *sess = NULL; |
0f113f3e MC |
1824 | unsigned char *sdec; |
1825 | const unsigned char *p; | |
83ab43da | 1826 | int slen, ivlen, renew_ticket = 0, declen; |
61fb5923 | 1827 | SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER; |
348240c6 | 1828 | size_t mlen; |
0f113f3e | 1829 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
a76ce286 | 1830 | SSL_HMAC *hctx = NULL; |
ee763495 | 1831 | EVP_CIPHER_CTX *ctx = NULL; |
222da979 | 1832 | SSL_CTX *tctx = s->session_ctx; |
e97763c9 | 1833 | |
61fb5923 MC |
1834 | if (eticklen == 0) { |
1835 | /* | |
1836 | * The client will accept a ticket but doesn't currently have | |
1837 | * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3 | |
1838 | */ | |
1839 | ret = SSL_TICKET_EMPTY; | |
1840 | goto end; | |
1841 | } | |
38b051a1 | 1842 | if (!SSL_CONNECTION_IS_TLS13(s) && s->ext.session_secret_cb) { |
61fb5923 MC |
1843 | /* |
1844 | * Indicate that the ticket couldn't be decrypted rather than | |
1845 | * generating the session from ticket now, trigger | |
1846 | * abbreviated handshake based on external mechanism to | |
1847 | * calculate the master secret later. | |
1848 | */ | |
1849 | ret = SSL_TICKET_NO_DECRYPT; | |
1850 | goto end; | |
1851 | } | |
1852 | ||
ee763495 MC |
1853 | /* Need at least keyname + iv */ |
1854 | if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { | |
df0fed9a | 1855 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1856 | goto end; |
ee763495 MC |
1857 | } |
1858 | ||
0f113f3e | 1859 | /* Initialize session ticket encryption and HMAC contexts */ |
a76ce286 | 1860 | hctx = ssl_hmac_new(tctx); |
c0638ade MC |
1861 | if (hctx == NULL) { |
1862 | ret = SSL_TICKET_FATAL_ERR_MALLOC; | |
1863 | goto end; | |
1864 | } | |
846ec07d | 1865 | ctx = EVP_CIPHER_CTX_new(); |
35b1a433 | 1866 | if (ctx == NULL) { |
df0fed9a | 1867 | ret = SSL_TICKET_FATAL_ERR_MALLOC; |
c0638ade | 1868 | goto end; |
35b1a433 | 1869 | } |
a76ce286 P |
1870 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
1871 | if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) | |
1872 | #else | |
1873 | if (tctx->ext.ticket_key_evp_cb != NULL) | |
1874 | #endif | |
1875 | { | |
0f113f3e | 1876 | unsigned char *nctick = (unsigned char *)etick; |
a76ce286 P |
1877 | int rv = 0; |
1878 | ||
1879 | if (tctx->ext.ticket_key_evp_cb != NULL) | |
38b051a1 | 1880 | rv = tctx->ext.ticket_key_evp_cb(SSL_CONNECTION_GET_SSL(s), nctick, |
a76ce286 P |
1881 | nctick + TLSEXT_KEYNAME_LENGTH, |
1882 | ctx, | |
1883 | ssl_hmac_get0_EVP_MAC_CTX(hctx), | |
1884 | 0); | |
1885 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
1886 | else if (tctx->ext.ticket_key_cb != NULL) | |
1887 | /* if 0 is returned, write an empty ticket */ | |
38b051a1 | 1888 | rv = tctx->ext.ticket_key_cb(SSL_CONNECTION_GET_SSL(s), nctick, |
ee763495 | 1889 | nctick + TLSEXT_KEYNAME_LENGTH, |
a76ce286 P |
1890 | ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0); |
1891 | #endif | |
c0638ade MC |
1892 | if (rv < 0) { |
1893 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
1894 | goto end; | |
1895 | } | |
35b1a433 | 1896 | if (rv == 0) { |
df0fed9a | 1897 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1898 | goto end; |
35b1a433 | 1899 | } |
0f113f3e MC |
1900 | if (rv == 2) |
1901 | renew_ticket = 1; | |
1902 | } else { | |
148bfd26 | 1903 | EVP_CIPHER *aes256cbc = NULL; |
38b051a1 | 1904 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
148bfd26 | 1905 | |
0f113f3e | 1906 | /* Check key name matches */ |
aff8c126 | 1907 | if (memcmp(etick, tctx->ext.tick_key_name, |
ee763495 | 1908 | TLSEXT_KEYNAME_LENGTH) != 0) { |
df0fed9a | 1909 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1910 | goto end; |
35b1a433 | 1911 | } |
148bfd26 | 1912 | |
38b051a1 TM |
1913 | aes256cbc = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", |
1914 | sctx->propq); | |
148bfd26 MC |
1915 | if (aes256cbc == NULL |
1916 | || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, | |
1917 | sizeof(tctx->ext.secure->tick_hmac_key), | |
1918 | "SHA256") <= 0 | |
1919 | || EVP_DecryptInit_ex(ctx, aes256cbc, NULL, | |
4bfb96f2 | 1920 | tctx->ext.secure->tick_aes_key, |
ee763495 | 1921 | etick + TLSEXT_KEYNAME_LENGTH) <= 0) { |
148bfd26 | 1922 | EVP_CIPHER_free(aes256cbc); |
c0638ade MC |
1923 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1924 | goto end; | |
a230b26e | 1925 | } |
148bfd26 | 1926 | EVP_CIPHER_free(aes256cbc); |
38b051a1 | 1927 | if (SSL_CONNECTION_IS_TLS13(s)) |
c0638ade | 1928 | renew_ticket = 1; |
0f113f3e MC |
1929 | } |
1930 | /* | |
1931 | * Attempt to process session ticket, first conduct sanity and integrity | |
1932 | * checks on ticket. | |
1933 | */ | |
a76ce286 | 1934 | mlen = ssl_hmac_size(hctx); |
348240c6 | 1935 | if (mlen == 0) { |
c0638ade MC |
1936 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1937 | goto end; | |
0f113f3e | 1938 | } |
c0638ade | 1939 | |
83ab43da DB |
1940 | ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); |
1941 | if (ivlen < 0) { | |
1942 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
1943 | goto end; | |
1944 | } | |
1945 | ||
e97763c9 | 1946 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
83ab43da | 1947 | if (eticklen <= TLSEXT_KEYNAME_LENGTH + ivlen + mlen) { |
df0fed9a | 1948 | ret = SSL_TICKET_NO_DECRYPT; |
c0638ade | 1949 | goto end; |
e97763c9 | 1950 | } |
0f113f3e MC |
1951 | eticklen -= mlen; |
1952 | /* Check HMAC of encrypted ticket */ | |
a76ce286 P |
1953 | if (ssl_hmac_update(hctx, etick, eticklen) <= 0 |
1954 | || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) { | |
c0638ade MC |
1955 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1956 | goto end; | |
5f3d93e4 | 1957 | } |
c0638ade | 1958 | |
0f113f3e | 1959 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
c0638ade MC |
1960 | ret = SSL_TICKET_NO_DECRYPT; |
1961 | goto end; | |
0f113f3e MC |
1962 | } |
1963 | /* Attempt to decrypt session data */ | |
1964 | /* Move p after IV to start of encrypted ticket, update length */ | |
83ab43da DB |
1965 | p = etick + TLSEXT_KEYNAME_LENGTH + ivlen; |
1966 | eticklen -= TLSEXT_KEYNAME_LENGTH + ivlen; | |
0f113f3e | 1967 | sdec = OPENSSL_malloc(eticklen); |
348240c6 MC |
1968 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
1969 | (int)eticklen) <= 0) { | |
d1247df2 | 1970 | OPENSSL_free(sdec); |
c0638ade MC |
1971 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
1972 | goto end; | |
0f113f3e | 1973 | } |
348240c6 | 1974 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
0f113f3e | 1975 | OPENSSL_free(sdec); |
c0638ade MC |
1976 | ret = SSL_TICKET_NO_DECRYPT; |
1977 | goto end; | |
0f113f3e | 1978 | } |
348240c6 | 1979 | slen += declen; |
0f113f3e MC |
1980 | p = sdec; |
1981 | ||
1982 | sess = d2i_SSL_SESSION(NULL, &p, slen); | |
d3bc9805 | 1983 | slen -= p - sdec; |
0f113f3e MC |
1984 | OPENSSL_free(sdec); |
1985 | if (sess) { | |
79020b27 | 1986 | /* Some additional consistency checks */ |
32305f88 | 1987 | if (slen != 0) { |
79020b27 | 1988 | SSL_SESSION_free(sess); |
5f96a95e | 1989 | sess = NULL; |
c0638ade MC |
1990 | ret = SSL_TICKET_NO_DECRYPT; |
1991 | goto end; | |
79020b27 | 1992 | } |
0f113f3e MC |
1993 | /* |
1994 | * The session ID, if non-empty, is used by some clients to detect | |
1995 | * that the ticket has been accepted. So we copy it to the session | |
1996 | * structure. If it is empty set length to zero as required by | |
1997 | * standard. | |
1998 | */ | |
32305f88 | 1999 | if (sesslen) { |
0f113f3e | 2000 | memcpy(sess->session_id, sess_id, sesslen); |
32305f88 MC |
2001 | sess->session_id_length = sesslen; |
2002 | } | |
0f113f3e | 2003 | if (renew_ticket) |
c0638ade | 2004 | ret = SSL_TICKET_SUCCESS_RENEW; |
0f113f3e | 2005 | else |
c0638ade MC |
2006 | ret = SSL_TICKET_SUCCESS; |
2007 | goto end; | |
0f113f3e MC |
2008 | } |
2009 | ERR_clear_error(); | |
2010 | /* | |
2011 | * For session parse failure, indicate that we need to send a new ticket. | |
2012 | */ | |
c0638ade MC |
2013 | ret = SSL_TICKET_NO_DECRYPT; |
2014 | ||
2015 | end: | |
846ec07d | 2016 | EVP_CIPHER_CTX_free(ctx); |
a76ce286 | 2017 | ssl_hmac_free(hctx); |
c0638ade MC |
2018 | |
2019 | /* | |
61fb5923 MC |
2020 | * If set, the decrypt_ticket_cb() is called unless a fatal error was |
2021 | * detected above. The callback is responsible for checking |ret| before it | |
2022 | * performs any action | |
c0638ade | 2023 | */ |
61fb5923 MC |
2024 | if (s->session_ctx->decrypt_ticket_cb != NULL |
2025 | && (ret == SSL_TICKET_EMPTY | |
2026 | || ret == SSL_TICKET_NO_DECRYPT | |
2027 | || ret == SSL_TICKET_SUCCESS | |
2028 | || ret == SSL_TICKET_SUCCESS_RENEW)) { | |
c0638ade | 2029 | size_t keyname_len = eticklen; |
61fb5923 | 2030 | int retcb; |
c0638ade MC |
2031 | |
2032 | if (keyname_len > TLSEXT_KEYNAME_LENGTH) | |
2033 | keyname_len = TLSEXT_KEYNAME_LENGTH; | |
38b051a1 TM |
2034 | retcb = s->session_ctx->decrypt_ticket_cb(SSL_CONNECTION_GET_SSL(s), |
2035 | sess, etick, keyname_len, | |
61fb5923 MC |
2036 | ret, |
2037 | s->session_ctx->ticket_cb_data); | |
2038 | switch (retcb) { | |
2039 | case SSL_TICKET_RETURN_ABORT: | |
2040 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
2041 | break; | |
2042 | ||
2043 | case SSL_TICKET_RETURN_IGNORE: | |
2044 | ret = SSL_TICKET_NONE; | |
2045 | SSL_SESSION_free(sess); | |
2046 | sess = NULL; | |
2047 | break; | |
2048 | ||
2049 | case SSL_TICKET_RETURN_IGNORE_RENEW: | |
2050 | if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT) | |
2051 | ret = SSL_TICKET_NO_DECRYPT; | |
2052 | /* else the value of |ret| will already do the right thing */ | |
2053 | SSL_SESSION_free(sess); | |
2054 | sess = NULL; | |
2055 | break; | |
2056 | ||
2057 | case SSL_TICKET_RETURN_USE: | |
2058 | case SSL_TICKET_RETURN_USE_RENEW: | |
2059 | if (ret != SSL_TICKET_SUCCESS | |
2060 | && ret != SSL_TICKET_SUCCESS_RENEW) | |
2061 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
2062 | else if (retcb == SSL_TICKET_RETURN_USE) | |
2063 | ret = SSL_TICKET_SUCCESS; | |
2064 | else | |
2065 | ret = SSL_TICKET_SUCCESS_RENEW; | |
2066 | break; | |
2067 | ||
2068 | default: | |
2069 | ret = SSL_TICKET_FATAL_ERR_OTHER; | |
2070 | } | |
c0638ade MC |
2071 | } |
2072 | ||
38b051a1 | 2073 | if (s->ext.session_secret_cb == NULL || SSL_CONNECTION_IS_TLS13(s)) { |
309371d6 MC |
2074 | switch (ret) { |
2075 | case SSL_TICKET_NO_DECRYPT: | |
2076 | case SSL_TICKET_SUCCESS_RENEW: | |
2077 | case SSL_TICKET_EMPTY: | |
2078 | s->ext.ticket_expected = 1; | |
2079 | } | |
c0638ade MC |
2080 | } |
2081 | ||
61fb5923 MC |
2082 | *psess = sess; |
2083 | ||
2084 | return ret; | |
0f113f3e | 2085 | } |
6434abbf | 2086 | |
b362ccab | 2087 | /* Check to see if a signature algorithm is allowed */ |
38b051a1 TM |
2088 | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, |
2089 | const SIGALG_LOOKUP *lu) | |
0f113f3e | 2090 | { |
703bcee0 | 2091 | unsigned char sigalgstr[2]; |
44b6318f | 2092 | int secbits; |
703bcee0 | 2093 | |
263ff2c9 | 2094 | if (lu == NULL || !lu->enabled) |
0f113f3e | 2095 | return 0; |
224b4e37 | 2096 | /* DSA is not allowed in TLS 1.3 */ |
38b051a1 | 2097 | if (SSL_CONNECTION_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) |
224b4e37 | 2098 | return 0; |
08cea586 MC |
2099 | /* |
2100 | * At some point we should fully axe DSA/etc. in ClientHello as per TLS 1.3 | |
2101 | * spec | |
2102 | */ | |
38b051a1 TM |
2103 | if (!s->server && !SSL_CONNECTION_IS_DTLS(s) |
2104 | && s->s3.tmp.min_ver >= TLS1_3_VERSION | |
6ffeb269 BK |
2105 | && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX |
2106 | || lu->hash_idx == SSL_MD_MD5_IDX | |
2107 | || lu->hash_idx == SSL_MD_SHA224_IDX)) | |
2108 | return 0; | |
871980a9 | 2109 | |
0f113f3e | 2110 | /* See if public key algorithm allowed */ |
38b051a1 | 2111 | if (ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), lu->sig_idx)) |
0f113f3e | 2112 | return 0; |
871980a9 MC |
2113 | |
2114 | if (lu->sig == NID_id_GostR3410_2012_256 | |
2115 | || lu->sig == NID_id_GostR3410_2012_512 | |
2116 | || lu->sig == NID_id_GostR3410_2001) { | |
2117 | /* We never allow GOST sig algs on the server with TLSv1.3 */ | |
38b051a1 | 2118 | if (s->server && SSL_CONNECTION_IS_TLS13(s)) |
871980a9 MC |
2119 | return 0; |
2120 | if (!s->server | |
38b051a1 | 2121 | && SSL_CONNECTION_GET_SSL(s)->method->version == TLS_ANY_VERSION |
555cbb32 | 2122 | && s->s3.tmp.max_ver >= TLS1_3_VERSION) { |
871980a9 MC |
2123 | int i, num; |
2124 | STACK_OF(SSL_CIPHER) *sk; | |
2125 | ||
2126 | /* | |
2127 | * We're a client that could negotiate TLSv1.3. We only allow GOST | |
2128 | * sig algs if we could negotiate TLSv1.2 or below and we have GOST | |
2129 | * ciphersuites enabled. | |
2130 | */ | |
2131 | ||
555cbb32 | 2132 | if (s->s3.tmp.min_ver >= TLS1_3_VERSION) |
871980a9 MC |
2133 | return 0; |
2134 | ||
38b051a1 | 2135 | sk = SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)); |
871980a9 MC |
2136 | num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0; |
2137 | for (i = 0; i < num; i++) { | |
2138 | const SSL_CIPHER *c; | |
2139 | ||
2140 | c = sk_SSL_CIPHER_value(sk, i); | |
2141 | /* Skip disabled ciphers */ | |
2142 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) | |
2143 | continue; | |
2144 | ||
5a5530a2 | 2145 | if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0) |
871980a9 MC |
2146 | break; |
2147 | } | |
2148 | if (i == num) | |
2149 | return 0; | |
2150 | } | |
2151 | } | |
2152 | ||
0f113f3e | 2153 | /* Finally see if security callback allows it */ |
38b051a1 | 2154 | secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
b0e9ab95 DSH |
2155 | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
2156 | sigalgstr[1] = lu->sigalg & 0xff; | |
44b6318f | 2157 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
0f113f3e MC |
2158 | } |
2159 | ||
2160 | /* | |
2161 | * Get a mask of disabled public key algorithms based on supported signature | |
2162 | * algorithms. For example if no signature algorithm supports RSA then RSA is | |
2163 | * disabled. | |
b362ccab DSH |
2164 | */ |
2165 | ||
38b051a1 | 2166 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op) |
0f113f3e | 2167 | { |
98c792d1 | 2168 | const uint16_t *sigalgs; |
0f113f3e | 2169 | size_t i, sigalgslen; |
13cc2574 | 2170 | uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA; |
0f113f3e | 2171 | /* |
13cc2574 DSH |
2172 | * Go through all signature algorithms seeing if we support any |
2173 | * in disabled_mask. | |
0f113f3e | 2174 | */ |
a9669ddc | 2175 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
b2555168 | 2176 | for (i = 0; i < sigalgslen; i++, sigalgs++) { |
263ff2c9 | 2177 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *sigalgs); |
13cc2574 | 2178 | const SSL_CERT_LOOKUP *clu; |
b0e9ab95 DSH |
2179 | |
2180 | if (lu == NULL) | |
2181 | continue; | |
13cc2574 DSH |
2182 | |
2183 | clu = ssl_cert_lookup_by_idx(lu->sig_idx); | |
dd6b2706 P |
2184 | if (clu == NULL) |
2185 | continue; | |
13cc2574 DSH |
2186 | |
2187 | /* If algorithm is disabled see if we can enable it */ | |
2188 | if ((clu->amask & disabled_mask) != 0 | |
2189 | && tls12_sigalg_allowed(s, op, lu)) | |
2190 | disabled_mask &= ~clu->amask; | |
0f113f3e | 2191 | } |
13cc2574 | 2192 | *pmask_a |= disabled_mask; |
0f113f3e | 2193 | } |
b362ccab | 2194 | |
38b051a1 | 2195 | int tls12_copy_sigalgs(SSL_CONNECTION *s, WPACKET *pkt, |
98c792d1 | 2196 | const uint16_t *psig, size_t psiglen) |
2c7b4dbc MC |
2197 | { |
2198 | size_t i; | |
b0e9ab95 | 2199 | int rv = 0; |
c0f9e23c | 2200 | |
703bcee0 | 2201 | for (i = 0; i < psiglen; i++, psig++) { |
263ff2c9 | 2202 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *psig); |
b0e9ab95 | 2203 | |
54e3efff MC |
2204 | if (lu == NULL |
2205 | || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) | |
b0e9ab95 DSH |
2206 | continue; |
2207 | if (!WPACKET_put_bytes_u16(pkt, *psig)) | |
2208 | return 0; | |
2209 | /* | |
2210 | * If TLS 1.3 must have at least one valid TLS 1.3 message | |
d8311fc9 | 2211 | * signing algorithm: i.e. neither RSA nor SHA1/SHA224 |
b0e9ab95 | 2212 | */ |
38b051a1 | 2213 | if (rv == 0 && (!SSL_CONNECTION_IS_TLS13(s) |
d8311fc9 MC |
2214 | || (lu->sig != EVP_PKEY_RSA |
2215 | && lu->hash != NID_sha1 | |
2216 | && lu->hash != NID_sha224))) | |
b0e9ab95 | 2217 | rv = 1; |
2c7b4dbc | 2218 | } |
5528d68f | 2219 | if (rv == 0) |
6849b73c | 2220 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
b0e9ab95 | 2221 | return rv; |
2c7b4dbc MC |
2222 | } |
2223 | ||
4453cd8c | 2224 | /* Given preference and allowed sigalgs set shared sigalgs */ |
38b051a1 TM |
2225 | static size_t tls12_shared_sigalgs(SSL_CONNECTION *s, |
2226 | const SIGALG_LOOKUP **shsig, | |
98c792d1 DSH |
2227 | const uint16_t *pref, size_t preflen, |
2228 | const uint16_t *allow, size_t allowlen) | |
0f113f3e | 2229 | { |
98c792d1 | 2230 | const uint16_t *ptmp, *atmp; |
0f113f3e | 2231 | size_t i, j, nmatch = 0; |
703bcee0 | 2232 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
263ff2c9 | 2233 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *ptmp); |
b0e9ab95 | 2234 | |
0f113f3e | 2235 | /* Skip disabled hashes or signature algorithms */ |
54e3efff MC |
2236 | if (lu == NULL |
2237 | || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) | |
0f113f3e | 2238 | continue; |
703bcee0 MC |
2239 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
2240 | if (*ptmp == *atmp) { | |
0f113f3e | 2241 | nmatch++; |
b0e9ab95 DSH |
2242 | if (shsig) |
2243 | *shsig++ = lu; | |
0f113f3e MC |
2244 | break; |
2245 | } | |
2246 | } | |
2247 | } | |
2248 | return nmatch; | |
2249 | } | |
4453cd8c DSH |
2250 | |
2251 | /* Set shared signature algorithms for SSL structures */ | |
38b051a1 | 2252 | static int tls1_set_shared_sigalgs(SSL_CONNECTION *s) |
0f113f3e | 2253 | { |
98c792d1 | 2254 | const uint16_t *pref, *allow, *conf; |
0f113f3e MC |
2255 | size_t preflen, allowlen, conflen; |
2256 | size_t nmatch; | |
4d43ee28 | 2257 | const SIGALG_LOOKUP **salgs = NULL; |
0f113f3e MC |
2258 | CERT *c = s->cert; |
2259 | unsigned int is_suiteb = tls1_suiteb(s); | |
b548a1f1 | 2260 | |
29948ac8 BK |
2261 | OPENSSL_free(s->shared_sigalgs); |
2262 | s->shared_sigalgs = NULL; | |
2263 | s->shared_sigalgslen = 0; | |
0f113f3e MC |
2264 | /* If client use client signature algorithms if not NULL */ |
2265 | if (!s->server && c->client_sigalgs && !is_suiteb) { | |
2266 | conf = c->client_sigalgs; | |
2267 | conflen = c->client_sigalgslen; | |
2268 | } else if (c->conf_sigalgs && !is_suiteb) { | |
2269 | conf = c->conf_sigalgs; | |
2270 | conflen = c->conf_sigalgslen; | |
2271 | } else | |
a9669ddc | 2272 | conflen = tls12_get_psigalgs(s, 0, &conf); |
0f113f3e MC |
2273 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
2274 | pref = conf; | |
2275 | preflen = conflen; | |
555cbb32 TS |
2276 | allow = s->s3.tmp.peer_sigalgs; |
2277 | allowlen = s->s3.tmp.peer_sigalgslen; | |
0f113f3e MC |
2278 | } else { |
2279 | allow = conf; | |
2280 | allowlen = conflen; | |
555cbb32 TS |
2281 | pref = s->s3.tmp.peer_sigalgs; |
2282 | preflen = s->s3.tmp.peer_sigalgslen; | |
0f113f3e MC |
2283 | } |
2284 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); | |
34e3edbf | 2285 | if (nmatch) { |
e077455e | 2286 | if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) |
34e3edbf DSH |
2287 | return 0; |
2288 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); | |
2289 | } else { | |
2290 | salgs = NULL; | |
2291 | } | |
29948ac8 BK |
2292 | s->shared_sigalgs = salgs; |
2293 | s->shared_sigalgslen = nmatch; | |
0f113f3e MC |
2294 | return 1; |
2295 | } | |
4453cd8c | 2296 | |
9e84a42d | 2297 | int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen) |
0f113f3e | 2298 | { |
98c792d1 | 2299 | unsigned int stmp; |
703bcee0 | 2300 | size_t size, i; |
9e84a42d | 2301 | uint16_t *buf; |
0f113f3e | 2302 | |
703bcee0 MC |
2303 | size = PACKET_remaining(pkt); |
2304 | ||
2305 | /* Invalid data length */ | |
8f12296e | 2306 | if (size == 0 || (size & 1) != 0) |
703bcee0 MC |
2307 | return 0; |
2308 | ||
2309 | size >>= 1; | |
2310 | ||
e077455e | 2311 | if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) |
0f113f3e | 2312 | return 0; |
98c792d1 | 2313 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
9e84a42d | 2314 | buf[i] = stmp; |
703bcee0 | 2315 | |
9e84a42d DSH |
2316 | if (i != size) { |
2317 | OPENSSL_free(buf); | |
703bcee0 | 2318 | return 0; |
9e84a42d DSH |
2319 | } |
2320 | ||
2321 | OPENSSL_free(*pdest); | |
2322 | *pdest = buf; | |
2323 | *pdestlen = size; | |
703bcee0 | 2324 | |
0f113f3e MC |
2325 | return 1; |
2326 | } | |
6b7be581 | 2327 | |
38b051a1 | 2328 | int tls1_save_sigalgs(SSL_CONNECTION *s, PACKET *pkt, int cert) |
9e84a42d DSH |
2329 | { |
2330 | /* Extension ignored for inappropriate versions */ | |
2331 | if (!SSL_USE_SIGALGS(s)) | |
2332 | return 1; | |
2333 | /* Should never happen */ | |
2334 | if (s->cert == NULL) | |
2335 | return 0; | |
2336 | ||
c589c34e | 2337 | if (cert) |
555cbb32 TS |
2338 | return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs, |
2339 | &s->s3.tmp.peer_cert_sigalgslen); | |
c589c34e | 2340 | else |
555cbb32 TS |
2341 | return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs, |
2342 | &s->s3.tmp.peer_sigalgslen); | |
9e84a42d | 2343 | |
9e84a42d DSH |
2344 | } |
2345 | ||
2346 | /* Set preferred digest for each key type */ | |
2347 | ||
38b051a1 | 2348 | int tls1_process_sigalgs(SSL_CONNECTION *s) |
0f113f3e | 2349 | { |
0f113f3e | 2350 | size_t i; |
555cbb32 | 2351 | uint32_t *pvalid = s->s3.tmp.valid_flags; |
4d43ee28 | 2352 | |
0f113f3e MC |
2353 | if (!tls1_set_shared_sigalgs(s)) |
2354 | return 0; | |
2355 | ||
9195ddcd DSH |
2356 | for (i = 0; i < SSL_PKEY_NUM; i++) |
2357 | pvalid[i] = 0; | |
2358 | ||
29948ac8 BK |
2359 | for (i = 0; i < s->shared_sigalgslen; i++) { |
2360 | const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i]; | |
9195ddcd | 2361 | int idx = sigptr->sig_idx; |
4d43ee28 | 2362 | |
523fb323 | 2363 | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
38b051a1 | 2364 | if (SSL_CONNECTION_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
523fb323 | 2365 | continue; |
9195ddcd | 2366 | /* If not disabled indicate we can explicitly sign */ |
38b051a1 TM |
2367 | if (pvalid[idx] == 0 |
2368 | && !ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), idx)) | |
b8858aec | 2369 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
0f113f3e MC |
2370 | } |
2371 | return 1; | |
2372 | } | |
4817504d | 2373 | |
e7f8ff43 | 2374 | int SSL_get_sigalgs(SSL *s, int idx, |
0f113f3e MC |
2375 | int *psign, int *phash, int *psignhash, |
2376 | unsigned char *rsig, unsigned char *rhash) | |
2377 | { | |
38b051a1 TM |
2378 | uint16_t *psig; |
2379 | size_t numsigalgs; | |
2380 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); | |
2381 | ||
2382 | if (sc == NULL) | |
2383 | return 0; | |
2384 | ||
2385 | psig = sc->s3.tmp.peer_sigalgs; | |
2386 | numsigalgs = sc->s3.tmp.peer_sigalgslen; | |
2387 | ||
348240c6 | 2388 | if (psig == NULL || numsigalgs > INT_MAX) |
0f113f3e MC |
2389 | return 0; |
2390 | if (idx >= 0) { | |
4d43ee28 DSH |
2391 | const SIGALG_LOOKUP *lu; |
2392 | ||
703bcee0 | 2393 | if (idx >= (int)numsigalgs) |
0f113f3e MC |
2394 | return 0; |
2395 | psig += idx; | |
4d43ee28 | 2396 | if (rhash != NULL) |
536199ec | 2397 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
4d43ee28 | 2398 | if (rsig != NULL) |
536199ec | 2399 | *rsig = (unsigned char)(*psig & 0xff); |
38b051a1 | 2400 | lu = tls1_lookup_sigalg(sc, *psig); |
4d43ee28 DSH |
2401 | if (psign != NULL) |
2402 | *psign = lu != NULL ? lu->sig : NID_undef; | |
2403 | if (phash != NULL) | |
2404 | *phash = lu != NULL ? lu->hash : NID_undef; | |
2405 | if (psignhash != NULL) | |
2406 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; | |
0f113f3e | 2407 | } |
348240c6 | 2408 | return (int)numsigalgs; |
0f113f3e | 2409 | } |
4453cd8c DSH |
2410 | |
2411 | int SSL_get_shared_sigalgs(SSL *s, int idx, | |
0f113f3e MC |
2412 | int *psign, int *phash, int *psignhash, |
2413 | unsigned char *rsig, unsigned char *rhash) | |
2414 | { | |
4d43ee28 | 2415 | const SIGALG_LOOKUP *shsigalgs; |
38b051a1 TM |
2416 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2417 | ||
2418 | if (sc == NULL) | |
2419 | return 0; | |
2420 | ||
2421 | if (sc->shared_sigalgs == NULL | |
6d047e06 | 2422 | || idx < 0 |
38b051a1 TM |
2423 | || idx >= (int)sc->shared_sigalgslen |
2424 | || sc->shared_sigalgslen > INT_MAX) | |
0f113f3e | 2425 | return 0; |
38b051a1 | 2426 | shsigalgs = sc->shared_sigalgs[idx]; |
4d43ee28 DSH |
2427 | if (phash != NULL) |
2428 | *phash = shsigalgs->hash; | |
2429 | if (psign != NULL) | |
2430 | *psign = shsigalgs->sig; | |
2431 | if (psignhash != NULL) | |
2432 | *psignhash = shsigalgs->sigandhash; | |
2433 | if (rsig != NULL) | |
2434 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); | |
2435 | if (rhash != NULL) | |
2436 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); | |
38b051a1 | 2437 | return (int)sc->shared_sigalgslen; |
0f113f3e MC |
2438 | } |
2439 | ||
787ebcaf DSH |
2440 | /* Maximum possible number of unique entries in sigalgs array */ |
2441 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) | |
0f229cce | 2442 | |
0f113f3e MC |
2443 | typedef struct { |
2444 | size_t sigalgcnt; | |
fd5e1a8c BK |
2445 | /* TLSEXT_SIGALG_XXX values */ |
2446 | uint16_t sigalgs[TLS_MAX_SIGALGCNT]; | |
0f113f3e | 2447 | } sig_cb_st; |
0f229cce | 2448 | |
431f458d DSH |
2449 | static void get_sigorhash(int *psig, int *phash, const char *str) |
2450 | { | |
2451 | if (strcmp(str, "RSA") == 0) { | |
2452 | *psig = EVP_PKEY_RSA; | |
b2eb6998 DSH |
2453 | } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { |
2454 | *psig = EVP_PKEY_RSA_PSS; | |
431f458d DSH |
2455 | } else if (strcmp(str, "DSA") == 0) { |
2456 | *psig = EVP_PKEY_DSA; | |
2457 | } else if (strcmp(str, "ECDSA") == 0) { | |
2458 | *psig = EVP_PKEY_EC; | |
2459 | } else { | |
2460 | *phash = OBJ_sn2nid(str); | |
2461 | if (*phash == NID_undef) | |
2462 | *phash = OBJ_ln2nid(str); | |
2463 | } | |
2464 | } | |
787ebcaf DSH |
2465 | /* Maximum length of a signature algorithm string component */ |
2466 | #define TLS_MAX_SIGSTRING_LEN 40 | |
431f458d | 2467 | |
0f229cce | 2468 | static int sig_cb(const char *elem, int len, void *arg) |
0f113f3e MC |
2469 | { |
2470 | sig_cb_st *sarg = arg; | |
2471 | size_t i; | |
fd5e1a8c | 2472 | const SIGALG_LOOKUP *s; |
787ebcaf | 2473 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
431f458d | 2474 | int sig_alg = NID_undef, hash_alg = NID_undef; |
2747d73c KR |
2475 | if (elem == NULL) |
2476 | return 0; | |
787ebcaf | 2477 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
0f113f3e MC |
2478 | return 0; |
2479 | if (len > (int)(sizeof(etmp) - 1)) | |
2480 | return 0; | |
2481 | memcpy(etmp, elem, len); | |
2482 | etmp[len] = 0; | |
2483 | p = strchr(etmp, '+'); | |
fd5e1a8c BK |
2484 | /* |
2485 | * We only allow SignatureSchemes listed in the sigalg_lookup_tbl; | |
2486 | * if there's no '+' in the provided name, look for the new-style combined | |
2487 | * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP. | |
2488 | * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and | |
2489 | * rsa_pss_rsae_* that differ only by public key OID; in such cases | |
2490 | * we will pick the _rsae_ variant, by virtue of them appearing earlier | |
2491 | * in the table. | |
2492 | */ | |
8a43a42a | 2493 | if (p == NULL) { |
8a43a42a DSH |
2494 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
2495 | i++, s++) { | |
2496 | if (s->name != NULL && strcmp(etmp, s->name) == 0) { | |
fd5e1a8c | 2497 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
8a43a42a DSH |
2498 | break; |
2499 | } | |
2500 | } | |
fd5e1a8c BK |
2501 | if (i == OSSL_NELEM(sigalg_lookup_tbl)) |
2502 | return 0; | |
8a43a42a DSH |
2503 | } else { |
2504 | *p = 0; | |
2505 | p++; | |
2506 | if (*p == 0) | |
2507 | return 0; | |
2508 | get_sigorhash(&sig_alg, &hash_alg, etmp); | |
2509 | get_sigorhash(&sig_alg, &hash_alg, p); | |
fd5e1a8c BK |
2510 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
2511 | return 0; | |
2512 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); | |
2513 | i++, s++) { | |
2514 | if (s->hash == hash_alg && s->sig == sig_alg) { | |
2515 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; | |
2516 | break; | |
2517 | } | |
2518 | } | |
2519 | if (i == OSSL_NELEM(sigalg_lookup_tbl)) | |
2520 | return 0; | |
8a43a42a | 2521 | } |
0f113f3e | 2522 | |
fd5e1a8c BK |
2523 | /* Reject duplicates */ |
2524 | for (i = 0; i < sarg->sigalgcnt - 1; i++) { | |
c1acef92 | 2525 | if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { |
fd5e1a8c | 2526 | sarg->sigalgcnt--; |
0f113f3e | 2527 | return 0; |
fd5e1a8c | 2528 | } |
0f113f3e | 2529 | } |
0f113f3e MC |
2530 | return 1; |
2531 | } | |
2532 | ||
2533 | /* | |
9d22666e | 2534 | * Set supported signature algorithms based on a colon separated list of the |
0f113f3e MC |
2535 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
2536 | */ | |
3dbc46df | 2537 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
0f113f3e MC |
2538 | { |
2539 | sig_cb_st sig; | |
2540 | sig.sigalgcnt = 0; | |
2541 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) | |
2542 | return 0; | |
2543 | if (c == NULL) | |
2544 | return 1; | |
fd5e1a8c BK |
2545 | return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
2546 | } | |
2547 | ||
2548 | int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen, | |
2549 | int client) | |
2550 | { | |
2551 | uint16_t *sigalgs; | |
2552 | ||
e077455e | 2553 | if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) |
fd5e1a8c BK |
2554 | return 0; |
2555 | memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs)); | |
2556 | ||
2557 | if (client) { | |
2558 | OPENSSL_free(c->client_sigalgs); | |
2559 | c->client_sigalgs = sigalgs; | |
2560 | c->client_sigalgslen = salglen; | |
2561 | } else { | |
2562 | OPENSSL_free(c->conf_sigalgs); | |
2563 | c->conf_sigalgs = sigalgs; | |
2564 | c->conf_sigalgslen = salglen; | |
2565 | } | |
2566 | ||
2567 | return 1; | |
0f113f3e MC |
2568 | } |
2569 | ||
a230b26e | 2570 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
0f113f3e | 2571 | { |
98c792d1 | 2572 | uint16_t *sigalgs, *sptr; |
0f113f3e | 2573 | size_t i; |
63c1df09 | 2574 | |
0f113f3e MC |
2575 | if (salglen & 1) |
2576 | return 0; | |
e077455e | 2577 | if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) |
0f113f3e MC |
2578 | return 0; |
2579 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { | |
63c1df09 | 2580 | size_t j; |
7a531ee4 | 2581 | const SIGALG_LOOKUP *curr; |
63c1df09 MC |
2582 | int md_id = *psig_nids++; |
2583 | int sig_id = *psig_nids++; | |
2584 | ||
2585 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); | |
2586 | j++, curr++) { | |
fe3066ee | 2587 | if (curr->hash == md_id && curr->sig == sig_id) { |
63c1df09 MC |
2588 | *sptr++ = curr->sigalg; |
2589 | break; | |
2590 | } | |
2591 | } | |
0f113f3e | 2592 | |
63c1df09 | 2593 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
0f113f3e | 2594 | goto err; |
0f113f3e MC |
2595 | } |
2596 | ||
2597 | if (client) { | |
b548a1f1 | 2598 | OPENSSL_free(c->client_sigalgs); |
0f113f3e | 2599 | c->client_sigalgs = sigalgs; |
7a531ee4 | 2600 | c->client_sigalgslen = salglen / 2; |
0f113f3e | 2601 | } else { |
b548a1f1 | 2602 | OPENSSL_free(c->conf_sigalgs); |
0f113f3e | 2603 | c->conf_sigalgs = sigalgs; |
7a531ee4 | 2604 | c->conf_sigalgslen = salglen / 2; |
0f113f3e MC |
2605 | } |
2606 | ||
2607 | return 1; | |
2608 | ||
2609 | err: | |
2610 | OPENSSL_free(sigalgs); | |
2611 | return 0; | |
2612 | } | |
4453cd8c | 2613 | |
38b051a1 | 2614 | static int tls1_check_sig_alg(SSL_CONNECTION *s, X509 *x, int default_nid) |
0f113f3e | 2615 | { |
5235ef44 | 2616 | int sig_nid, use_pc_sigalgs = 0; |
0f113f3e | 2617 | size_t i; |
5235ef44 MC |
2618 | const SIGALG_LOOKUP *sigalg; |
2619 | size_t sigalgslen; | |
38b051a1 | 2620 | |
0f113f3e MC |
2621 | if (default_nid == -1) |
2622 | return 1; | |
2623 | sig_nid = X509_get_signature_nid(x); | |
2624 | if (default_nid) | |
2625 | return sig_nid == default_nid ? 1 : 0; | |
5235ef44 | 2626 | |
38b051a1 | 2627 | if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) { |
5235ef44 MC |
2628 | /* |
2629 | * If we're in TLSv1.3 then we only get here if we're checking the | |
2630 | * chain. If the peer has specified peer_cert_sigalgs then we use them | |
2631 | * otherwise we default to normal sigalgs. | |
2632 | */ | |
2633 | sigalgslen = s->s3.tmp.peer_cert_sigalgslen; | |
2634 | use_pc_sigalgs = 1; | |
2635 | } else { | |
2636 | sigalgslen = s->shared_sigalgslen; | |
2637 | } | |
2638 | for (i = 0; i < sigalgslen; i++) { | |
2639 | sigalg = use_pc_sigalgs | |
263ff2c9 | 2640 | ? tls1_lookup_sigalg(s, s->s3.tmp.peer_cert_sigalgs[i]) |
5235ef44 | 2641 | : s->shared_sigalgs[i]; |
a87f3fe0 | 2642 | if (sigalg != NULL && sig_nid == sigalg->sigandhash) |
0f113f3e | 2643 | return 1; |
5235ef44 | 2644 | } |
0f113f3e MC |
2645 | return 0; |
2646 | } | |
2647 | ||
6dbb6219 DSH |
2648 | /* Check to see if a certificate issuer name matches list of CA names */ |
2649 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) | |
0f113f3e | 2650 | { |
8cc86b81 | 2651 | const X509_NAME *nm; |
0f113f3e MC |
2652 | int i; |
2653 | nm = X509_get_issuer_name(x); | |
2654 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
2655 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) | |
2656 | return 1; | |
2657 | } | |
2658 | return 0; | |
2659 | } | |
2660 | ||
2661 | /* | |
2662 | * Check certificate chain is consistent with TLS extensions and is usable by | |
2663 | * server. This servers two purposes: it allows users to check chains before | |
2664 | * passing them to the server and it allows the server to check chains before | |
2665 | * attempting to use them. | |
d61ff83b | 2666 | */ |
6dbb6219 | 2667 | |
69687aa8 | 2668 | /* Flags which need to be set for a certificate when strict mode not set */ |
6dbb6219 | 2669 | |
e481f9b9 | 2670 | #define CERT_PKEY_VALID_FLAGS \ |
0f113f3e | 2671 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
6dbb6219 | 2672 | /* Strict mode flags */ |
e481f9b9 | 2673 | #define CERT_PKEY_STRICT_FLAGS \ |
0f113f3e MC |
2674 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
2675 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) | |
6dbb6219 | 2676 | |
38b051a1 TM |
2677 | int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk, |
2678 | STACK_OF(X509) *chain, int idx) | |
0f113f3e MC |
2679 | { |
2680 | int i; | |
2681 | int rv = 0; | |
2682 | int check_flags = 0, strict_mode; | |
2683 | CERT_PKEY *cpk = NULL; | |
2684 | CERT *c = s->cert; | |
f7d53487 | 2685 | uint32_t *pvalid; |
0f113f3e | 2686 | unsigned int suiteb_flags = tls1_suiteb(s); |
38b051a1 | 2687 | |
0f113f3e MC |
2688 | /* idx == -1 means checking server chains */ |
2689 | if (idx != -1) { | |
2690 | /* idx == -2 means checking client certificate chains */ | |
2691 | if (idx == -2) { | |
2692 | cpk = c->key; | |
348240c6 | 2693 | idx = (int)(cpk - c->pkeys); |
0f113f3e MC |
2694 | } else |
2695 | cpk = c->pkeys + idx; | |
555cbb32 | 2696 | pvalid = s->s3.tmp.valid_flags + idx; |
0f113f3e MC |
2697 | x = cpk->x509; |
2698 | pk = cpk->privatekey; | |
2699 | chain = cpk->chain; | |
2700 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; | |
2701 | /* If no cert or key, forget it */ | |
2702 | if (!x || !pk) | |
2703 | goto end; | |
0f113f3e | 2704 | } else { |
52fd27f9 DSH |
2705 | size_t certidx; |
2706 | ||
0f113f3e | 2707 | if (!x || !pk) |
d813f9eb | 2708 | return 0; |
52fd27f9 DSH |
2709 | |
2710 | if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL) | |
d813f9eb | 2711 | return 0; |
52fd27f9 | 2712 | idx = certidx; |
555cbb32 | 2713 | pvalid = s->s3.tmp.valid_flags + idx; |
6383d316 | 2714 | |
0f113f3e MC |
2715 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
2716 | check_flags = CERT_PKEY_STRICT_FLAGS; | |
2717 | else | |
2718 | check_flags = CERT_PKEY_VALID_FLAGS; | |
2719 | strict_mode = 1; | |
2720 | } | |
2721 | ||
2722 | if (suiteb_flags) { | |
2723 | int ok; | |
2724 | if (check_flags) | |
2725 | check_flags |= CERT_PKEY_SUITEB; | |
2726 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); | |
2727 | if (ok == X509_V_OK) | |
2728 | rv |= CERT_PKEY_SUITEB; | |
2729 | else if (!check_flags) | |
2730 | goto end; | |
2731 | } | |
2732 | ||
2733 | /* | |
2734 | * Check all signature algorithms are consistent with signature | |
2735 | * algorithms extension if TLS 1.2 or later and strict mode. | |
2736 | */ | |
38b051a1 TM |
2737 | if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION |
2738 | && strict_mode) { | |
0f113f3e | 2739 | int default_nid; |
536199ec | 2740 | int rsign = 0; |
38b051a1 | 2741 | |
555cbb32 TS |
2742 | if (s->s3.tmp.peer_cert_sigalgs != NULL |
2743 | || s->s3.tmp.peer_sigalgs != NULL) { | |
0f113f3e MC |
2744 | default_nid = 0; |
2745 | /* If no sigalgs extension use defaults from RFC5246 */ | |
c589c34e | 2746 | } else { |
0f113f3e | 2747 | switch (idx) { |
d0ff28f8 | 2748 | case SSL_PKEY_RSA: |
536199ec | 2749 | rsign = EVP_PKEY_RSA; |
0f113f3e MC |
2750 | default_nid = NID_sha1WithRSAEncryption; |
2751 | break; | |
2752 | ||
2753 | case SSL_PKEY_DSA_SIGN: | |
536199ec | 2754 | rsign = EVP_PKEY_DSA; |
0f113f3e MC |
2755 | default_nid = NID_dsaWithSHA1; |
2756 | break; | |
2757 | ||
2758 | case SSL_PKEY_ECC: | |
536199ec | 2759 | rsign = EVP_PKEY_EC; |
0f113f3e MC |
2760 | default_nid = NID_ecdsa_with_SHA1; |
2761 | break; | |
2762 | ||
e44380a9 | 2763 | case SSL_PKEY_GOST01: |
536199ec | 2764 | rsign = NID_id_GostR3410_2001; |
e44380a9 DB |
2765 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
2766 | break; | |
2767 | ||
2768 | case SSL_PKEY_GOST12_256: | |
536199ec | 2769 | rsign = NID_id_GostR3410_2012_256; |
e44380a9 DB |
2770 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
2771 | break; | |
2772 | ||
2773 | case SSL_PKEY_GOST12_512: | |
536199ec | 2774 | rsign = NID_id_GostR3410_2012_512; |
e44380a9 DB |
2775 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
2776 | break; | |
2777 | ||
0f113f3e MC |
2778 | default: |
2779 | default_nid = -1; | |
2780 | break; | |
2781 | } | |
2782 | } | |
2783 | /* | |
2784 | * If peer sent no signature algorithms extension and we have set | |
2785 | * preferred signature algorithms check we support sha1. | |
2786 | */ | |
2787 | if (default_nid > 0 && c->conf_sigalgs) { | |
2788 | size_t j; | |
98c792d1 | 2789 | const uint16_t *p = c->conf_sigalgs; |
703bcee0 | 2790 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
263ff2c9 | 2791 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(s, *p); |
44b6318f DSH |
2792 | |
2793 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) | |
0f113f3e MC |
2794 | break; |
2795 | } | |
2796 | if (j == c->conf_sigalgslen) { | |
2797 | if (check_flags) | |
2798 | goto skip_sigs; | |
2799 | else | |
2800 | goto end; | |
2801 | } | |
2802 | } | |
2803 | /* Check signature algorithm of each cert in chain */ | |
38b051a1 | 2804 | if (SSL_CONNECTION_IS_TLS13(s)) { |
5235ef44 MC |
2805 | /* |
2806 | * We only get here if the application has called SSL_check_chain(), | |
2807 | * so check_flags is always set. | |
2808 | */ | |
2809 | if (find_sig_alg(s, x, pk) != NULL) | |
2810 | rv |= CERT_PKEY_EE_SIGNATURE; | |
2811 | } else if (!tls1_check_sig_alg(s, x, default_nid)) { | |
0f113f3e MC |
2812 | if (!check_flags) |
2813 | goto end; | |
2814 | } else | |
2815 | rv |= CERT_PKEY_EE_SIGNATURE; | |
2816 | rv |= CERT_PKEY_CA_SIGNATURE; | |
2817 | for (i = 0; i < sk_X509_num(chain); i++) { | |
29948ac8 | 2818 | if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) { |
0f113f3e MC |
2819 | if (check_flags) { |
2820 | rv &= ~CERT_PKEY_CA_SIGNATURE; | |
2821 | break; | |
2822 | } else | |
2823 | goto end; | |
2824 | } | |
2825 | } | |
2826 | } | |
2827 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ | |
2828 | else if (check_flags) | |
2829 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; | |
2830 | skip_sigs: | |
2831 | /* Check cert parameters are consistent */ | |
9195ddcd | 2832 | if (tls1_check_cert_param(s, x, 1)) |
0f113f3e MC |
2833 | rv |= CERT_PKEY_EE_PARAM; |
2834 | else if (!check_flags) | |
2835 | goto end; | |
2836 | if (!s->server) | |
2837 | rv |= CERT_PKEY_CA_PARAM; | |
2838 | /* In strict mode check rest of chain too */ | |
2839 | else if (strict_mode) { | |
2840 | rv |= CERT_PKEY_CA_PARAM; | |
2841 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2842 | X509 *ca = sk_X509_value(chain, i); | |
2843 | if (!tls1_check_cert_param(s, ca, 0)) { | |
2844 | if (check_flags) { | |
2845 | rv &= ~CERT_PKEY_CA_PARAM; | |
2846 | break; | |
2847 | } else | |
2848 | goto end; | |
2849 | } | |
2850 | } | |
2851 | } | |
2852 | if (!s->server && strict_mode) { | |
2853 | STACK_OF(X509_NAME) *ca_dn; | |
2854 | int check_type = 0; | |
c2041da8 RL |
2855 | |
2856 | if (EVP_PKEY_is_a(pk, "RSA")) | |
0f113f3e | 2857 | check_type = TLS_CT_RSA_SIGN; |
c2041da8 | 2858 | else if (EVP_PKEY_is_a(pk, "DSA")) |
0f113f3e | 2859 | check_type = TLS_CT_DSS_SIGN; |
c2041da8 | 2860 | else if (EVP_PKEY_is_a(pk, "EC")) |
0f113f3e | 2861 | check_type = TLS_CT_ECDSA_SIGN; |
c2041da8 | 2862 | |
0f113f3e | 2863 | if (check_type) { |
555cbb32 | 2864 | const uint8_t *ctypes = s->s3.tmp.ctype; |
75c13e78 DSH |
2865 | size_t j; |
2866 | ||
555cbb32 | 2867 | for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) { |
75c13e78 | 2868 | if (*ctypes == check_type) { |
0f113f3e MC |
2869 | rv |= CERT_PKEY_CERT_TYPE; |
2870 | break; | |
2871 | } | |
2872 | } | |
2873 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) | |
2874 | goto end; | |
75c13e78 | 2875 | } else { |
0f113f3e | 2876 | rv |= CERT_PKEY_CERT_TYPE; |
75c13e78 | 2877 | } |
0f113f3e | 2878 | |
555cbb32 | 2879 | ca_dn = s->s3.tmp.peer_ca_names; |
0f113f3e | 2880 | |
89dd8543 TM |
2881 | if (ca_dn == NULL |
2882 | || sk_X509_NAME_num(ca_dn) == 0 | |
2883 | || ssl_check_ca_name(ca_dn, x)) | |
0f113f3e | 2884 | rv |= CERT_PKEY_ISSUER_NAME; |
89dd8543 | 2885 | else |
0f113f3e MC |
2886 | for (i = 0; i < sk_X509_num(chain); i++) { |
2887 | X509 *xtmp = sk_X509_value(chain, i); | |
89dd8543 | 2888 | |
0f113f3e MC |
2889 | if (ssl_check_ca_name(ca_dn, xtmp)) { |
2890 | rv |= CERT_PKEY_ISSUER_NAME; | |
2891 | break; | |
2892 | } | |
2893 | } | |
89dd8543 | 2894 | |
0f113f3e MC |
2895 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
2896 | goto end; | |
2897 | } else | |
2898 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; | |
2899 | ||
2900 | if (!check_flags || (rv & check_flags) == check_flags) | |
2901 | rv |= CERT_PKEY_VALID; | |
2902 | ||
2903 | end: | |
2904 | ||
38b051a1 | 2905 | if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION) |
a8bb912d DSH |
2906 | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); |
2907 | else | |
0f113f3e MC |
2908 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
2909 | ||
2910 | /* | |
2911 | * When checking a CERT_PKEY structure all flags are irrelevant if the | |
2912 | * chain is invalid. | |
2913 | */ | |
2914 | if (!check_flags) { | |
a8bb912d | 2915 | if (rv & CERT_PKEY_VALID) { |
6383d316 | 2916 | *pvalid = rv; |
a8bb912d DSH |
2917 | } else { |
2918 | /* Preserve sign and explicit sign flag, clear rest */ | |
2919 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; | |
0f113f3e MC |
2920 | return 0; |
2921 | } | |
2922 | } | |
2923 | return rv; | |
2924 | } | |
d61ff83b DSH |
2925 | |
2926 | /* Set validity of certificates in an SSL structure */ | |
38b051a1 | 2927 | void tls1_set_cert_validity(SSL_CONNECTION *s) |
0f113f3e | 2928 | { |
d0ff28f8 | 2929 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
045d078a | 2930 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN); |
17dd65e6 | 2931 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
17dd65e6 | 2932 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
e44380a9 DB |
2933 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
2934 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); | |
2935 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); | |
3d234c9e | 2936 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); |
0e1d6ecf | 2937 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448); |
0f113f3e MC |
2938 | } |
2939 | ||
69687aa8 | 2940 | /* User level utility function to check a chain is suitable */ |
18d71588 | 2941 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
0f113f3e | 2942 | { |
38b051a1 TM |
2943 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
2944 | ||
2945 | if (sc == NULL) | |
2946 | return 0; | |
2947 | ||
2948 | return tls1_check_chain(sc, x, pk, chain, -1); | |
0f113f3e | 2949 | } |
d61ff83b | 2950 | |
38b051a1 | 2951 | EVP_PKEY *ssl_get_auto_dh(SSL_CONNECTION *s) |
0f113f3e | 2952 | { |
091f6074 MC |
2953 | EVP_PKEY *dhp = NULL; |
2954 | BIGNUM *p; | |
d7b5c648 | 2955 | int dh_secbits = 80, sec_level_bits; |
091f6074 MC |
2956 | EVP_PKEY_CTX *pctx = NULL; |
2957 | OSSL_PARAM_BLD *tmpl = NULL; | |
2958 | OSSL_PARAM *params = NULL; | |
38b051a1 | 2959 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
091f6074 | 2960 | |
7646610b HK |
2961 | if (s->cert->dh_tmp_auto != 2) { |
2962 | if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { | |
2963 | if (s->s3.tmp.new_cipher->strength_bits == 256) | |
2964 | dh_secbits = 128; | |
2965 | else | |
2966 | dh_secbits = 80; | |
2967 | } else { | |
2968 | if (s->s3.tmp.cert == NULL) | |
2969 | return NULL; | |
ed576acd | 2970 | dh_secbits = EVP_PKEY_get_security_bits(s->s3.tmp.cert->privatekey); |
7646610b | 2971 | } |
0f113f3e MC |
2972 | } |
2973 | ||
d7b5c648 | 2974 | /* Do not pick a prime that is too weak for the current security level */ |
38b051a1 TM |
2975 | sec_level_bits = ssl_get_security_level_bits(SSL_CONNECTION_GET_SSL(s), |
2976 | NULL, NULL); | |
d7b5c648 P |
2977 | if (dh_secbits < sec_level_bits) |
2978 | dh_secbits = sec_level_bits; | |
2979 | ||
7646610b HK |
2980 | if (dh_secbits >= 192) |
2981 | p = BN_get_rfc3526_prime_8192(NULL); | |
2982 | else if (dh_secbits >= 152) | |
2983 | p = BN_get_rfc3526_prime_4096(NULL); | |
2984 | else if (dh_secbits >= 128) | |
2985 | p = BN_get_rfc3526_prime_3072(NULL); | |
2986 | else if (dh_secbits >= 112) | |
2987 | p = BN_get_rfc3526_prime_2048(NULL); | |
2988 | else | |
2989 | p = BN_get_rfc2409_prime_1024(NULL); | |
091f6074 MC |
2990 | if (p == NULL) |
2991 | goto err; | |
2992 | ||
38b051a1 | 2993 | pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq); |
091f6074 | 2994 | if (pctx == NULL |
2db985b7 | 2995 | || EVP_PKEY_fromdata_init(pctx) != 1) |
091f6074 MC |
2996 | goto err; |
2997 | ||
2998 | tmpl = OSSL_PARAM_BLD_new(); | |
2999 | if (tmpl == NULL | |
3000 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) | |
3001 | || !OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2)) | |
3002 | goto err; | |
3003 | ||
3004 | params = OSSL_PARAM_BLD_to_param(tmpl); | |
2db985b7 SL |
3005 | if (params == NULL |
3006 | || EVP_PKEY_fromdata(pctx, &dhp, EVP_PKEY_KEY_PARAMETERS, params) != 1) | |
091f6074 MC |
3007 | goto err; |
3008 | ||
3009 | err: | |
3f883c7c | 3010 | OSSL_PARAM_free(params); |
091f6074 MC |
3011 | OSSL_PARAM_BLD_free(tmpl); |
3012 | EVP_PKEY_CTX_free(pctx); | |
3013 | BN_free(p); | |
7646610b | 3014 | return dhp; |
0f113f3e | 3015 | } |
b362ccab | 3016 | |
38b051a1 TM |
3017 | static int ssl_security_cert_key(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
3018 | int op) | |
0f113f3e | 3019 | { |
72245f34 | 3020 | int secbits = -1; |
8382fd3a | 3021 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
38b051a1 | 3022 | |
0f113f3e | 3023 | if (pkey) { |
72245f34 DSH |
3024 | /* |
3025 | * If no parameters this will return -1 and fail using the default | |
3026 | * security callback for any non-zero security level. This will | |
3027 | * reject keys which omit parameters but this only affects DSA and | |
3028 | * omission of parameters is never (?) done in practice. | |
3029 | */ | |
ed576acd | 3030 | secbits = EVP_PKEY_get_security_bits(pkey); |
72245f34 | 3031 | } |
38b051a1 | 3032 | if (s != NULL) |
0f113f3e MC |
3033 | return ssl_security(s, op, secbits, 0, x); |
3034 | else | |
3035 | return ssl_ctx_security(ctx, op, secbits, 0, x); | |
3036 | } | |
b362ccab | 3037 | |
38b051a1 TM |
3038 | static int ssl_security_cert_sig(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
3039 | int op) | |
0f113f3e MC |
3040 | { |
3041 | /* Lookup signature algorithm digest */ | |
65e89736 | 3042 | int secbits, nid, pknid; |
38b051a1 | 3043 | |
221c7b55 DSH |
3044 | /* Don't check signature if self signed */ |
3045 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) | |
3046 | return 1; | |
65e89736 DSH |
3047 | if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) |
3048 | secbits = -1; | |
3049 | /* If digest NID not defined use signature NID */ | |
3050 | if (nid == NID_undef) | |
3051 | nid = pknid; | |
38b051a1 | 3052 | if (s != NULL) |
65e89736 | 3053 | return ssl_security(s, op, secbits, nid, x); |
0f113f3e | 3054 | else |
65e89736 | 3055 | return ssl_ctx_security(ctx, op, secbits, nid, x); |
0f113f3e | 3056 | } |
b362ccab | 3057 | |
38b051a1 TM |
3058 | int ssl_security_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, int vfy, |
3059 | int is_ee) | |
0f113f3e MC |
3060 | { |
3061 | if (vfy) | |
3062 | vfy = SSL_SECOP_PEER; | |
3063 | if (is_ee) { | |
3064 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) | |
3065 | return SSL_R_EE_KEY_TOO_SMALL; | |
3066 | } else { | |
3067 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) | |
3068 | return SSL_R_CA_KEY_TOO_SMALL; | |
3069 | } | |
3070 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) | |
3071 | return SSL_R_CA_MD_TOO_WEAK; | |
3072 | return 1; | |
3073 | } | |
3074 | ||
3075 | /* | |
69687aa8 F |
3076 | * Check security of a chain, if |sk| includes the end entity certificate then |
3077 | * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending | |
0f113f3e | 3078 | * one to the peer. Return values: 1 if ok otherwise error code to use |
b362ccab DSH |
3079 | */ |
3080 | ||
38b051a1 TM |
3081 | int ssl_security_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk, |
3082 | X509 *x, int vfy) | |
0f113f3e MC |
3083 | { |
3084 | int rv, start_idx, i; | |
38b051a1 | 3085 | |
0f113f3e MC |
3086 | if (x == NULL) { |
3087 | x = sk_X509_value(sk, 0); | |
dc0ef292 BE |
3088 | if (x == NULL) |
3089 | return ERR_R_INTERNAL_ERROR; | |
0f113f3e MC |
3090 | start_idx = 1; |
3091 | } else | |
3092 | start_idx = 0; | |
3093 | ||
3094 | rv = ssl_security_cert(s, NULL, x, vfy, 1); | |
3095 | if (rv != 1) | |
3096 | return rv; | |
3097 | ||
3098 | for (i = start_idx; i < sk_X509_num(sk); i++) { | |
3099 | x = sk_X509_value(sk, i); | |
3100 | rv = ssl_security_cert(s, NULL, x, vfy, 0); | |
3101 | if (rv != 1) | |
3102 | return rv; | |
3103 | } | |
3104 | return 1; | |
3105 | } | |
93a77f9e | 3106 | |
7f6b466b DSH |
3107 | /* |
3108 | * For TLS 1.2 servers check if we have a certificate which can be used | |
b46867d7 | 3109 | * with the signature algorithm "lu" and return index of certificate. |
7f6b466b DSH |
3110 | */ |
3111 | ||
38b051a1 TM |
3112 | static int tls12_get_cert_sigalg_idx(const SSL_CONNECTION *s, |
3113 | const SIGALG_LOOKUP *lu) | |
7f6b466b | 3114 | { |
b46867d7 DSH |
3115 | int sig_idx = lu->sig_idx; |
3116 | const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx); | |
7f6b466b DSH |
3117 | |
3118 | /* If not recognised or not supported by cipher mask it is not suitable */ | |
b8fef8ee | 3119 | if (clu == NULL |
555cbb32 | 3120 | || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0 |
b8fef8ee | 3121 | || (clu->nid == EVP_PKEY_RSA_PSS |
555cbb32 | 3122 | && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0)) |
b46867d7 DSH |
3123 | return -1; |
3124 | ||
555cbb32 | 3125 | return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1; |
7f6b466b DSH |
3126 | } |
3127 | ||
c589c34e | 3128 | /* |
5235ef44 MC |
3129 | * Checks the given cert against signature_algorithm_cert restrictions sent by |
3130 | * the peer (if any) as well as whether the hash from the sigalg is usable with | |
3131 | * the key. | |
3132 | * Returns true if the cert is usable and false otherwise. | |
c589c34e | 3133 | */ |
38b051a1 TM |
3134 | static int check_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, |
3135 | X509 *x, EVP_PKEY *pkey) | |
c589c34e BK |
3136 | { |
3137 | const SIGALG_LOOKUP *lu; | |
ecbb2fca | 3138 | int mdnid, pknid, supported; |
c589c34e | 3139 | size_t i; |
e9fe0f7e | 3140 | const char *mdname = NULL; |
38b051a1 | 3141 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
c589c34e | 3142 | |
b5a27688 | 3143 | /* |
e9fe0f7e | 3144 | * If the given EVP_PKEY cannot support signing with this digest, |
b5a27688 DW |
3145 | * the answer is simply 'no'. |
3146 | */ | |
e9fe0f7e TM |
3147 | if (sig->hash != NID_undef) |
3148 | mdname = OBJ_nid2sn(sig->hash); | |
38b051a1 | 3149 | supported = EVP_PKEY_digestsign_supports_digest(pkey, sctx->libctx, |
e9fe0f7e | 3150 | mdname, |
38b051a1 | 3151 | sctx->propq); |
e9fe0f7e | 3152 | if (supported <= 0) |
b5a27688 DW |
3153 | return 0; |
3154 | ||
3155 | /* | |
3156 | * The TLS 1.3 signature_algorithms_cert extension places restrictions | |
3157 | * on the sigalg with which the certificate was signed (by its issuer). | |
3158 | */ | |
555cbb32 | 3159 | if (s->s3.tmp.peer_cert_sigalgs != NULL) { |
b5a27688 DW |
3160 | if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL)) |
3161 | return 0; | |
555cbb32 | 3162 | for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) { |
263ff2c9 | 3163 | lu = tls1_lookup_sigalg(s, s->s3.tmp.peer_cert_sigalgs[i]); |
b5a27688 | 3164 | if (lu == NULL) |
c589c34e | 3165 | continue; |
ecbb2fca | 3166 | |
5235ef44 | 3167 | /* |
407820c0 | 3168 | * This does not differentiate between the |
b5a27688 DW |
3169 | * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not |
3170 | * have a chain here that lets us look at the key OID in the | |
3171 | * signing certificate. | |
5235ef44 | 3172 | */ |
b5a27688 DW |
3173 | if (mdnid == lu->hash && pknid == lu->sig) |
3174 | return 1; | |
c589c34e BK |
3175 | } |
3176 | return 0; | |
3177 | } | |
b5a27688 | 3178 | |
5235ef44 | 3179 | /* |
b5a27688 DW |
3180 | * Without signat_algorithms_cert, any certificate for which we have |
3181 | * a viable public key is permitted. | |
5235ef44 | 3182 | */ |
ecbb2fca | 3183 | return 1; |
c589c34e BK |
3184 | } |
3185 | ||
5235ef44 MC |
3186 | /* |
3187 | * Returns true if |s| has a usable certificate configured for use | |
3188 | * with signature scheme |sig|. | |
3189 | * "Usable" includes a check for presence as well as applying | |
3190 | * the signature_algorithm_cert restrictions sent by the peer (if any). | |
3191 | * Returns false if no usable certificate is found. | |
3192 | */ | |
38b051a1 | 3193 | static int has_usable_cert(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, int idx) |
5235ef44 MC |
3194 | { |
3195 | /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */ | |
3196 | if (idx == -1) | |
3197 | idx = sig->sig_idx; | |
3198 | if (!ssl_has_cert(s, idx)) | |
3199 | return 0; | |
3200 | ||
3201 | return check_cert_usable(s, sig, s->cert->pkeys[idx].x509, | |
3202 | s->cert->pkeys[idx].privatekey); | |
3203 | } | |
3204 | ||
3205 | /* | |
3206 | * Returns true if the supplied cert |x| and key |pkey| is usable with the | |
3207 | * specified signature scheme |sig|, or false otherwise. | |
3208 | */ | |
38b051a1 | 3209 | static int is_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, X509 *x, |
5235ef44 MC |
3210 | EVP_PKEY *pkey) |
3211 | { | |
3212 | size_t idx; | |
3213 | ||
3214 | if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL) | |
3215 | return 0; | |
3216 | ||
3217 | /* Check the key is consistent with the sig alg */ | |
3218 | if ((int)idx != sig->sig_idx) | |
3219 | return 0; | |
3220 | ||
3221 | return check_cert_usable(s, sig, x, pkey); | |
3222 | } | |
3223 | ||
3224 | /* | |
3225 | * Find a signature scheme that works with the supplied certificate |x| and key | |
3226 | * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our | |
3227 | * available certs/keys to find one that works. | |
3228 | */ | |
38b051a1 TM |
3229 | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, |
3230 | EVP_PKEY *pkey) | |
5235ef44 MC |
3231 | { |
3232 | const SIGALG_LOOKUP *lu = NULL; | |
3233 | size_t i; | |
5235ef44 | 3234 | int curve = -1; |
5235ef44 | 3235 | EVP_PKEY *tmppkey; |
38b051a1 | 3236 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
5235ef44 MC |
3237 | |
3238 | /* Look for a shared sigalgs matching possible certificates */ | |
3239 | for (i = 0; i < s->shared_sigalgslen; i++) { | |
3240 | lu = s->shared_sigalgs[i]; | |
3241 | ||
3242 | /* Skip SHA1, SHA224, DSA and RSA if not PSS */ | |
3243 | if (lu->hash == NID_sha1 | |
3244 | || lu->hash == NID_sha224 | |
3245 | || lu->sig == EVP_PKEY_DSA | |
3246 | || lu->sig == EVP_PKEY_RSA) | |
3247 | continue; | |
3248 | /* Check that we have a cert, and signature_algorithms_cert */ | |
38b051a1 | 3249 | if (!tls1_lookup_md(sctx, lu, NULL)) |
5235ef44 MC |
3250 | continue; |
3251 | if ((pkey == NULL && !has_usable_cert(s, lu, -1)) | |
3252 | || (pkey != NULL && !is_cert_usable(s, lu, x, pkey))) | |
3253 | continue; | |
3254 | ||
3255 | tmppkey = (pkey != NULL) ? pkey | |
3256 | : s->cert->pkeys[lu->sig_idx].privatekey; | |
3257 | ||
3258 | if (lu->sig == EVP_PKEY_EC) { | |
c2041da8 | 3259 | if (curve == -1) |
d8975dec | 3260 | curve = ssl_get_EC_curve_nid(tmppkey); |
5235ef44 MC |
3261 | if (lu->curve != NID_undef && curve != lu->curve) |
3262 | continue; | |
5235ef44 MC |
3263 | } else if (lu->sig == EVP_PKEY_RSA_PSS) { |
3264 | /* validate that key is large enough for the signature algorithm */ | |
38b051a1 | 3265 | if (!rsa_pss_check_min_key_size(sctx, tmppkey, lu)) |
5235ef44 MC |
3266 | continue; |
3267 | } | |
3268 | break; | |
3269 | } | |
3270 | ||
3271 | if (i == s->shared_sigalgslen) | |
3272 | return NULL; | |
3273 | ||
3274 | return lu; | |
3275 | } | |
3276 | ||
93a77f9e DSH |
3277 | /* |
3278 | * Choose an appropriate signature algorithm based on available certificates | |
717a265a DSH |
3279 | * Sets chosen certificate and signature algorithm. |
3280 | * | |
f63a17d6 MC |
3281 | * For servers if we fail to find a required certificate it is a fatal error, |
3282 | * an appropriate error code is set and a TLS alert is sent. | |
717a265a | 3283 | * |
f63a17d6 | 3284 | * For clients fatalerrs is set to 0. If a certificate is not suitable it is not |
717a265a DSH |
3285 | * a fatal error: we will either try another certificate or not present one |
3286 | * to the server. In this case no error is set. | |
93a77f9e | 3287 | */ |
38b051a1 | 3288 | int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs) |
93a77f9e | 3289 | { |
0972bc5c | 3290 | const SIGALG_LOOKUP *lu = NULL; |
b46867d7 | 3291 | int sig_idx = -1; |
0972bc5c | 3292 | |
555cbb32 TS |
3293 | s->s3.tmp.cert = NULL; |
3294 | s->s3.tmp.sigalg = NULL; | |
717a265a | 3295 | |
38b051a1 | 3296 | if (SSL_CONNECTION_IS_TLS13(s)) { |
5235ef44 MC |
3297 | lu = find_sig_alg(s, NULL, NULL); |
3298 | if (lu == NULL) { | |
f63a17d6 | 3299 | if (!fatalerrs) |
717a265a | 3300 | return 1; |
c48ffbcc | 3301 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 3302 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
0972bc5c DSH |
3303 | return 0; |
3304 | } | |
3305 | } else { | |
7f6b466b | 3306 | /* If ciphersuite doesn't require a cert nothing to do */ |
555cbb32 | 3307 | if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT)) |
7f6b466b DSH |
3308 | return 1; |
3309 | if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys)) | |
717a265a | 3310 | return 1; |
0972bc5c DSH |
3311 | |
3312 | if (SSL_USE_SIGALGS(s)) { | |
c589c34e | 3313 | size_t i; |
555cbb32 | 3314 | if (s->s3.tmp.peer_sigalgs != NULL) { |
c2041da8 | 3315 | int curve = -1; |
38b051a1 | 3316 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
599b586d DSH |
3317 | |
3318 | /* For Suite B need to match signature algorithm to curve */ | |
c2041da8 | 3319 | if (tls1_suiteb(s)) |
d8975dec RL |
3320 | curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC] |
3321 | .privatekey); | |
0972bc5c DSH |
3322 | |
3323 | /* | |
3324 | * Find highest preference signature algorithm matching | |
3325 | * cert type | |
3326 | */ | |
29948ac8 BK |
3327 | for (i = 0; i < s->shared_sigalgslen; i++) { |
3328 | lu = s->shared_sigalgs[i]; | |
7f6b466b DSH |
3329 | |
3330 | if (s->server) { | |
b46867d7 | 3331 | if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1) |
7f6b466b | 3332 | continue; |
b46867d7 DSH |
3333 | } else { |
3334 | int cc_idx = s->cert->key - s->cert->pkeys; | |
3335 | ||
3336 | sig_idx = lu->sig_idx; | |
c589c34e BK |
3337 | if (cc_idx != sig_idx) |
3338 | continue; | |
b2021556 | 3339 | } |
c589c34e BK |
3340 | /* Check that we have a cert, and sig_algs_cert */ |
3341 | if (!has_usable_cert(s, lu, sig_idx)) | |
3342 | continue; | |
0fe3db25 NR |
3343 | if (lu->sig == EVP_PKEY_RSA_PSS) { |
3344 | /* validate that key is large enough for the signature algorithm */ | |
bcec0b94 | 3345 | EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey; |
0fe3db25 | 3346 | |
38b051a1 | 3347 | if (!rsa_pss_check_min_key_size(sctx, pkey, lu)) |
0fe3db25 NR |
3348 | continue; |
3349 | } | |
7f6b466b | 3350 | if (curve == -1 || lu->curve == curve) |
0972bc5c DSH |
3351 | break; |
3352 | } | |
db30f432 DB |
3353 | #ifndef OPENSSL_NO_GOST |
3354 | /* | |
3355 | * Some Windows-based implementations do not send GOST algorithms indication | |
3356 | * in supported_algorithms extension, so when we have GOST-based ciphersuite, | |
3357 | * we have to assume GOST support. | |
3358 | */ | |
38b051a1 TM |
3359 | if (i == s->shared_sigalgslen |
3360 | && (s->s3.tmp.new_cipher->algorithm_auth | |
3361 | & (SSL_aGOST01 | SSL_aGOST12)) != 0) { | |
db30f432 DB |
3362 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
3363 | if (!fatalerrs) | |
3364 | return 1; | |
3365 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, | |
db30f432 DB |
3366 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
3367 | return 0; | |
3368 | } else { | |
3369 | i = 0; | |
3370 | sig_idx = lu->sig_idx; | |
3371 | } | |
3372 | } | |
3373 | #endif | |
29948ac8 | 3374 | if (i == s->shared_sigalgslen) { |
f63a17d6 | 3375 | if (!fatalerrs) |
717a265a | 3376 | return 1; |
b8fef8ee | 3377 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
b8fef8ee | 3378 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
0972bc5c DSH |
3379 | return 0; |
3380 | } | |
3381 | } else { | |
3382 | /* | |
3383 | * If we have no sigalg use defaults | |
3384 | */ | |
3385 | const uint16_t *sent_sigs; | |
c589c34e | 3386 | size_t sent_sigslen; |
0972bc5c | 3387 | |
7f6b466b | 3388 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
f63a17d6 | 3389 | if (!fatalerrs) |
717a265a | 3390 | return 1; |
b4f1b7b6 | 3391 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
a70535f8 | 3392 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
0972bc5c DSH |
3393 | return 0; |
3394 | } | |
3395 | ||
3396 | /* Check signature matches a type we sent */ | |
3397 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
3398 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { | |
c589c34e BK |
3399 | if (lu->sigalg == *sent_sigs |
3400 | && has_usable_cert(s, lu, lu->sig_idx)) | |
0972bc5c DSH |
3401 | break; |
3402 | } | |
3403 | if (i == sent_sigslen) { | |
f63a17d6 | 3404 | if (!fatalerrs) |
717a265a | 3405 | return 1; |
b4f1b7b6 | 3406 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
f63a17d6 | 3407 | SSL_R_WRONG_SIGNATURE_TYPE); |
0972bc5c DSH |
3408 | return 0; |
3409 | } | |
3410 | } | |
3411 | } else { | |
7f6b466b | 3412 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
f63a17d6 | 3413 | if (!fatalerrs) |
717a265a | 3414 | return 1; |
c48ffbcc | 3415 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
a70535f8 | 3416 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
0972bc5c DSH |
3417 | return 0; |
3418 | } | |
3419 | } | |
93a77f9e | 3420 | } |
b46867d7 DSH |
3421 | if (sig_idx == -1) |
3422 | sig_idx = lu->sig_idx; | |
555cbb32 TS |
3423 | s->s3.tmp.cert = &s->cert->pkeys[sig_idx]; |
3424 | s->cert->key = s->s3.tmp.cert; | |
3425 | s->s3.tmp.sigalg = lu; | |
93a77f9e DSH |
3426 | return 1; |
3427 | } | |
cf72c757 F |
3428 | |
3429 | int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode) | |
3430 | { | |
3431 | if (mode != TLSEXT_max_fragment_length_DISABLED | |
3432 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { | |
6849b73c | 3433 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
cf72c757 F |
3434 | return 0; |
3435 | } | |
3436 | ||
3437 | ctx->ext.max_fragment_len_mode = mode; | |
3438 | return 1; | |
3439 | } | |
3440 | ||
3441 | int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode) | |
3442 | { | |
38b051a1 TM |
3443 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
3444 | ||
3445 | if (sc == NULL) | |
3446 | return 0; | |
3447 | ||
cf72c757 F |
3448 | if (mode != TLSEXT_max_fragment_length_DISABLED |
3449 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { | |
6849b73c | 3450 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
cf72c757 F |
3451 | return 0; |
3452 | } | |
3453 | ||
38b051a1 | 3454 | sc->ext.max_fragment_len_mode = mode; |
cf72c757 F |
3455 | return 1; |
3456 | } | |
3457 | ||
3458 | uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session) | |
3459 | { | |
3460 | return session->ext.max_fragment_len_mode; | |
3461 | } | |
a76ce286 P |
3462 | |
3463 | /* | |
3464 | * Helper functions for HMAC access with legacy support included. | |
3465 | */ | |
3466 | SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx) | |
3467 | { | |
3468 | SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret)); | |
3469 | EVP_MAC *mac = NULL; | |
3470 | ||
3471 | if (ret == NULL) | |
3472 | return NULL; | |
3473 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3474 | if (ctx->ext.ticket_key_evp_cb == NULL | |
3475 | && ctx->ext.ticket_key_cb != NULL) { | |
301fcb28 | 3476 | if (!ssl_hmac_old_new(ret)) |
a76ce286 P |
3477 | goto err; |
3478 | return ret; | |
3479 | } | |
3480 | #endif | |
7f80980f | 3481 | mac = EVP_MAC_fetch(ctx->libctx, "HMAC", ctx->propq); |
865adf97 | 3482 | if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL) |
a76ce286 P |
3483 | goto err; |
3484 | EVP_MAC_free(mac); | |
3485 | return ret; | |
3486 | err: | |
865adf97 | 3487 | EVP_MAC_CTX_free(ret->ctx); |
a76ce286 P |
3488 | EVP_MAC_free(mac); |
3489 | OPENSSL_free(ret); | |
3490 | return NULL; | |
3491 | } | |
3492 | ||
3493 | void ssl_hmac_free(SSL_HMAC *ctx) | |
3494 | { | |
3495 | if (ctx != NULL) { | |
865adf97 | 3496 | EVP_MAC_CTX_free(ctx->ctx); |
a76ce286 | 3497 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
301fcb28 | 3498 | ssl_hmac_old_free(ctx); |
a76ce286 P |
3499 | #endif |
3500 | OPENSSL_free(ctx); | |
3501 | } | |
3502 | } | |
3503 | ||
a76ce286 P |
3504 | EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx) |
3505 | { | |
3506 | return ctx->ctx; | |
3507 | } | |
3508 | ||
3509 | int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md) | |
3510 | { | |
0edb8194 | 3511 | OSSL_PARAM params[2], *p = params; |
a76ce286 P |
3512 | |
3513 | if (ctx->ctx != NULL) { | |
3514 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0); | |
a76ce286 | 3515 | *p = OSSL_PARAM_construct_end(); |
0edb8194 | 3516 | if (EVP_MAC_init(ctx->ctx, key, len, params)) |
a76ce286 P |
3517 | return 1; |
3518 | } | |
3519 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3520 | if (ctx->old_ctx != NULL) | |
301fcb28 | 3521 | return ssl_hmac_old_init(ctx, key, len, md); |
a76ce286 P |
3522 | #endif |
3523 | return 0; | |
3524 | } | |
3525 | ||
3526 | int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len) | |
3527 | { | |
3528 | if (ctx->ctx != NULL) | |
3529 | return EVP_MAC_update(ctx->ctx, data, len); | |
3530 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
3531 | if (ctx->old_ctx != NULL) | |
301fcb28 | 3532 | return ssl_hmac_old_update(ctx, data, len); |
a76ce286 P |
3533 | #endif |
3534 | return 0; | |
3535 | } | |
3536 | ||
3537 | int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len, | |
3538 | size_t max_size) | |
3539 | { | |
3540 | if (ctx->ctx != NULL) | |
3541 | return EVP_MAC_final(ctx->ctx, md, len, max_size); | |
3542 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
301fcb28 MC |
3543 | if (ctx->old_ctx != NULL) |
3544 | return ssl_hmac_old_final(ctx, md, len); | |
a76ce286 P |
3545 | #endif |
3546 | return 0; | |
3547 | } | |
3548 | ||
3549 | size_t ssl_hmac_size(const SSL_HMAC *ctx) | |
3550 | { | |
3551 | if (ctx->ctx != NULL) | |
90a2576b | 3552 | return EVP_MAC_CTX_get_mac_size(ctx->ctx); |
a76ce286 P |
3553 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
3554 | if (ctx->old_ctx != NULL) | |
301fcb28 | 3555 | return ssl_hmac_old_size(ctx); |
a76ce286 P |
3556 | #endif |
3557 | return 0; | |
3558 | } | |
3559 | ||
d8975dec RL |
3560 | int ssl_get_EC_curve_nid(const EVP_PKEY *pkey) |
3561 | { | |
3562 | char gname[OSSL_MAX_NAME_SIZE]; | |
3563 | ||
3564 | if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) > 0) | |
3565 | return OBJ_txt2nid(gname); | |
3566 | ||
3567 | return NID_undef; | |
3568 | } | |
d5530efa TM |
3569 | |
3570 | __owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey, | |
3571 | const unsigned char *enckey, | |
3572 | size_t enckeylen) | |
3573 | { | |
3574 | if (EVP_PKEY_is_a(pkey, "DH")) { | |
3575 | int bits = EVP_PKEY_get_bits(pkey); | |
3576 | ||
3577 | if (bits <= 0 || enckeylen != (size_t)bits / 8) | |
3578 | /* the encoded key must be padded to the length of the p */ | |
3579 | return 0; | |
3580 | } else if (EVP_PKEY_is_a(pkey, "EC")) { | |
3581 | if (enckeylen < 3 /* point format and at least 1 byte for x and y */ | |
3582 | || enckey[0] != 0x04) | |
3583 | return 0; | |
3584 | } | |
3585 | ||
3586 | return EVP_PKEY_set1_encoded_public_key(pkey, enckey, enckeylen); | |
3587 | } |