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