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