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