/src/openssl36/ssl/t1_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 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 <ctype.h> |
13 | | #include <openssl/objects.h> |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/hmac.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/ocsp.h> |
18 | | #include <openssl/conf.h> |
19 | | #include <openssl/x509v3.h> |
20 | | #include <openssl/dh.h> |
21 | | #include <openssl/bn.h> |
22 | | #include <openssl/provider.h> |
23 | | #include <openssl/param_build.h> |
24 | | #include "internal/nelem.h" |
25 | | #include "internal/sizes.h" |
26 | | #include "internal/tlsgroups.h" |
27 | | #include "internal/ssl_unwrap.h" |
28 | | #include "ssl_local.h" |
29 | | #include "quic/quic_local.h" |
30 | | #include <openssl/ct.h> |
31 | | |
32 | | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pkey); |
33 | | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, const SIGALG_LOOKUP *lu); |
34 | | |
35 | | SSL3_ENC_METHOD const TLSv1_enc_data = { |
36 | | tls1_setup_key_block, |
37 | | tls1_generate_master_secret, |
38 | | tls1_change_cipher_state, |
39 | | tls1_final_finish_mac, |
40 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
41 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
42 | | tls1_alert_code, |
43 | | tls1_export_keying_material, |
44 | | 0, |
45 | | ssl3_set_handshake_header, |
46 | | tls_close_construct_packet, |
47 | | ssl3_handshake_write |
48 | | }; |
49 | | |
50 | | SSL3_ENC_METHOD const TLSv1_1_enc_data = { |
51 | | tls1_setup_key_block, |
52 | | tls1_generate_master_secret, |
53 | | tls1_change_cipher_state, |
54 | | tls1_final_finish_mac, |
55 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
56 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
57 | | tls1_alert_code, |
58 | | tls1_export_keying_material, |
59 | | 0, |
60 | | ssl3_set_handshake_header, |
61 | | tls_close_construct_packet, |
62 | | ssl3_handshake_write |
63 | | }; |
64 | | |
65 | | SSL3_ENC_METHOD const TLSv1_2_enc_data = { |
66 | | tls1_setup_key_block, |
67 | | tls1_generate_master_secret, |
68 | | tls1_change_cipher_state, |
69 | | tls1_final_finish_mac, |
70 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
71 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
72 | | tls1_alert_code, |
73 | | tls1_export_keying_material, |
74 | | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF |
75 | | | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
76 | | ssl3_set_handshake_header, |
77 | | tls_close_construct_packet, |
78 | | ssl3_handshake_write |
79 | | }; |
80 | | |
81 | | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
82 | | tls13_setup_key_block, |
83 | | tls13_generate_master_secret, |
84 | | tls13_change_cipher_state, |
85 | | tls13_final_finish_mac, |
86 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
87 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
88 | | tls13_alert_code, |
89 | | tls13_export_keying_material, |
90 | | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
91 | | ssl3_set_handshake_header, |
92 | | tls_close_construct_packet, |
93 | | ssl3_handshake_write |
94 | | }; |
95 | | |
96 | | OSSL_TIME tls1_default_timeout(void) |
97 | 119k | { |
98 | | /* |
99 | | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for |
100 | | * http, the cache would over fill |
101 | | */ |
102 | 119k | return ossl_seconds2time(60 * 60 * 2); |
103 | 119k | } |
104 | | |
105 | | int tls1_new(SSL *s) |
106 | 119k | { |
107 | 119k | if (!ssl3_new(s)) |
108 | 0 | return 0; |
109 | 119k | if (!s->method->ssl_clear(s)) |
110 | 0 | return 0; |
111 | | |
112 | 119k | return 1; |
113 | 119k | } |
114 | | |
115 | | void tls1_free(SSL *s) |
116 | 64.4k | { |
117 | 64.4k | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
118 | | |
119 | 64.4k | if (sc == NULL) |
120 | 0 | return; |
121 | | |
122 | 64.4k | OPENSSL_free(sc->ext.session_ticket); |
123 | 64.4k | ssl3_free(s); |
124 | 64.4k | } |
125 | | |
126 | | int tls1_clear(SSL *s) |
127 | 257k | { |
128 | 257k | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
129 | | |
130 | 257k | if (sc == NULL) |
131 | 0 | return 0; |
132 | | |
133 | 257k | if (!ssl3_clear(s)) |
134 | 0 | return 0; |
135 | | |
136 | 257k | if (s->method->version == TLS_ANY_VERSION) |
137 | 257k | sc->version = TLS_MAX_VERSION_INTERNAL; |
138 | 0 | else |
139 | 0 | sc->version = s->method->version; |
140 | | |
141 | 257k | return 1; |
142 | 257k | } |
143 | | |
144 | | /* Legacy NID to group_id mapping. Only works for groups we know about */ |
145 | | static const struct { |
146 | | int nid; |
147 | | uint16_t group_id; |
148 | | } nid_to_group[] = { |
149 | | { NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1 }, |
150 | | { NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1 }, |
151 | | { NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2 }, |
152 | | { NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1 }, |
153 | | { NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2 }, |
154 | | { NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1 }, |
155 | | { NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1 }, |
156 | | { NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1 }, |
157 | | { NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1 }, |
158 | | { NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1 }, |
159 | | { NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1 }, |
160 | | { NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1 }, |
161 | | { NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1 }, |
162 | | { NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1 }, |
163 | | { NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1 }, |
164 | | { NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1 }, |
165 | | { NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2 }, |
166 | | { NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1 }, |
167 | | { NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1 }, |
168 | | { NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1 }, |
169 | | { NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1 }, |
170 | | { NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1 }, |
171 | | { NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1 }, |
172 | | { NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1 }, |
173 | | { NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1 }, |
174 | | { NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1 }, |
175 | | { NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1 }, |
176 | | { NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1 }, |
177 | | { EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519 }, |
178 | | { EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448 }, |
179 | | { NID_brainpoolP256r1tls13, OSSL_TLS_GROUP_ID_brainpoolP256r1_tls13 }, |
180 | | { NID_brainpoolP384r1tls13, OSSL_TLS_GROUP_ID_brainpoolP384r1_tls13 }, |
181 | | { NID_brainpoolP512r1tls13, OSSL_TLS_GROUP_ID_brainpoolP512r1_tls13 }, |
182 | | { NID_id_tc26_gost_3410_2012_256_paramSetA, OSSL_TLS_GROUP_ID_gc256A }, |
183 | | { NID_id_tc26_gost_3410_2012_256_paramSetB, OSSL_TLS_GROUP_ID_gc256B }, |
184 | | { NID_id_tc26_gost_3410_2012_256_paramSetC, OSSL_TLS_GROUP_ID_gc256C }, |
185 | | { NID_id_tc26_gost_3410_2012_256_paramSetD, OSSL_TLS_GROUP_ID_gc256D }, |
186 | | { NID_id_tc26_gost_3410_2012_512_paramSetA, OSSL_TLS_GROUP_ID_gc512A }, |
187 | | { NID_id_tc26_gost_3410_2012_512_paramSetB, OSSL_TLS_GROUP_ID_gc512B }, |
188 | | { NID_id_tc26_gost_3410_2012_512_paramSetC, OSSL_TLS_GROUP_ID_gc512C }, |
189 | | { NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048 }, |
190 | | { NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072 }, |
191 | | { NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096 }, |
192 | | { NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144 }, |
193 | | { NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192 } |
194 | | }; |
195 | | |
196 | | static const unsigned char ecformats_default[] = { |
197 | | TLSEXT_ECPOINTFORMAT_uncompressed |
198 | | }; |
199 | | |
200 | | static const unsigned char ecformats_all[] = { |
201 | | TLSEXT_ECPOINTFORMAT_uncompressed, |
202 | | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, |
203 | | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 |
204 | | }; |
205 | | |
206 | | /* Group list string of the built-in pseudo group DEFAULT */ |
207 | | #define DEFAULT_GROUP_NAME "DEFAULT" |
208 | | #define TLS_DEFAULT_GROUP_LIST \ |
209 | | "?*X25519MLKEM768 / ?*X25519:?secp256r1 / ?X448:?secp384r1:?secp521r1 / ?ffdhe2048:?ffdhe3072" |
210 | | |
211 | | static const uint16_t suiteb_curves[] = { |
212 | | OSSL_TLS_GROUP_ID_secp256r1, |
213 | | OSSL_TLS_GROUP_ID_secp384r1, |
214 | | }; |
215 | | |
216 | | /* Group list string of the built-in pseudo group DEFAULT_SUITE_B */ |
217 | | #define SUITE_B_GROUP_NAME "DEFAULT_SUITE_B" |
218 | | #define SUITE_B_GROUP_LIST "secp256r1:secp384r1", |
219 | | |
220 | | struct provider_ctx_data_st { |
221 | | SSL_CTX *ctx; |
222 | | OSSL_PROVIDER *provider; |
223 | | }; |
224 | | |
225 | 1.08M | #define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10 |
226 | | static OSSL_CALLBACK add_provider_groups; |
227 | | static int add_provider_groups(const OSSL_PARAM params[], void *data) |
228 | 5.35M | { |
229 | 5.35M | struct provider_ctx_data_st *pgd = data; |
230 | 5.35M | SSL_CTX *ctx = pgd->ctx; |
231 | 5.35M | const OSSL_PARAM *p; |
232 | 5.35M | TLS_GROUP_INFO *ginf = NULL; |
233 | 5.35M | EVP_KEYMGMT *keymgmt; |
234 | 5.35M | unsigned int gid; |
235 | 5.35M | unsigned int is_kem = 0; |
236 | 5.35M | int ret = 0; |
237 | | |
238 | 5.35M | if (ctx->group_list_max_len == ctx->group_list_len) { |
239 | 544k | TLS_GROUP_INFO *tmp = NULL; |
240 | | |
241 | 544k | if (ctx->group_list_max_len == 0) |
242 | 90.6k | tmp = OPENSSL_malloc_array(TLS_GROUP_LIST_MALLOC_BLOCK_SIZE, |
243 | 544k | sizeof(TLS_GROUP_INFO)); |
244 | 453k | else |
245 | 453k | tmp = OPENSSL_realloc_array(ctx->group_list, |
246 | 544k | ctx->group_list_max_len |
247 | 544k | + TLS_GROUP_LIST_MALLOC_BLOCK_SIZE, |
248 | 544k | sizeof(TLS_GROUP_INFO)); |
249 | 544k | if (tmp == NULL) |
250 | 0 | return 0; |
251 | 544k | ctx->group_list = tmp; |
252 | 544k | memset(tmp + ctx->group_list_max_len, |
253 | 544k | 0, |
254 | 544k | sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE); |
255 | 544k | ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE; |
256 | 544k | } |
257 | | |
258 | 5.35M | ginf = &ctx->group_list[ctx->group_list_len]; |
259 | | |
260 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME); |
261 | 5.35M | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
262 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
263 | 0 | goto err; |
264 | 0 | } |
265 | 5.35M | ginf->tlsname = OPENSSL_strdup(p->data); |
266 | 5.35M | if (ginf->tlsname == NULL) |
267 | 0 | goto err; |
268 | | |
269 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL); |
270 | 5.35M | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
271 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
272 | 0 | goto err; |
273 | 0 | } |
274 | 5.35M | ginf->realname = OPENSSL_strdup(p->data); |
275 | 5.35M | if (ginf->realname == NULL) |
276 | 0 | goto err; |
277 | | |
278 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID); |
279 | 5.35M | if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) { |
280 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
281 | 0 | goto err; |
282 | 0 | } |
283 | 5.35M | ginf->group_id = (uint16_t)gid; |
284 | | |
285 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG); |
286 | 5.35M | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
287 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
288 | 0 | goto err; |
289 | 0 | } |
290 | 5.35M | ginf->algorithm = OPENSSL_strdup(p->data); |
291 | 5.35M | if (ginf->algorithm == NULL) |
292 | 0 | goto err; |
293 | | |
294 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS); |
295 | 5.35M | if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) { |
296 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
297 | 0 | goto err; |
298 | 0 | } |
299 | | |
300 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM); |
301 | 5.35M | if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) { |
302 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
303 | 0 | goto err; |
304 | 0 | } |
305 | 5.35M | ginf->is_kem = 1 & is_kem; |
306 | | |
307 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS); |
308 | 5.35M | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) { |
309 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
310 | 0 | goto err; |
311 | 0 | } |
312 | | |
313 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS); |
314 | 5.35M | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) { |
315 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
316 | 0 | goto err; |
317 | 0 | } |
318 | | |
319 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS); |
320 | 5.35M | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) { |
321 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
322 | 0 | goto err; |
323 | 0 | } |
324 | | |
325 | 5.35M | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS); |
326 | 5.35M | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) { |
327 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
328 | 0 | goto err; |
329 | 0 | } |
330 | | /* |
331 | | * Now check that the algorithm is actually usable for our property query |
332 | | * string. Regardless of the result we still return success because we have |
333 | | * successfully processed this group, even though we may decide not to use |
334 | | * it. |
335 | | */ |
336 | 5.35M | ret = 1; |
337 | 5.35M | ERR_set_mark(); |
338 | 5.35M | keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq); |
339 | 5.35M | if (keymgmt != NULL) { |
340 | | /* We have successfully fetched the algorithm, we can use the group. */ |
341 | 5.35M | ctx->group_list_len++; |
342 | 5.35M | ginf = NULL; |
343 | 5.35M | EVP_KEYMGMT_free(keymgmt); |
344 | 5.35M | } |
345 | 5.35M | ERR_pop_to_mark(); |
346 | 5.35M | err: |
347 | 5.35M | if (ginf != NULL) { |
348 | 0 | OPENSSL_free(ginf->tlsname); |
349 | 0 | OPENSSL_free(ginf->realname); |
350 | 0 | OPENSSL_free(ginf->algorithm); |
351 | 0 | ginf->algorithm = ginf->tlsname = ginf->realname = NULL; |
352 | 0 | } |
353 | 5.35M | return ret; |
354 | 5.35M | } |
355 | | |
356 | | static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx) |
357 | 327k | { |
358 | 327k | struct provider_ctx_data_st pgd; |
359 | | |
360 | 327k | pgd.ctx = vctx; |
361 | 327k | pgd.provider = provider; |
362 | 327k | return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP", |
363 | 327k | add_provider_groups, &pgd); |
364 | 327k | } |
365 | | |
366 | | int ssl_load_groups(SSL_CTX *ctx) |
367 | 90.6k | { |
368 | 90.6k | if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx)) |
369 | 0 | return 0; |
370 | | |
371 | 90.6k | return SSL_CTX_set1_groups_list(ctx, TLS_DEFAULT_GROUP_LIST); |
372 | 90.6k | } |
373 | | |
374 | | static const char *inferred_keytype(const TLS_SIGALG_INFO *sinf) |
375 | 544k | { |
376 | 544k | return (sinf->keytype != NULL |
377 | 544k | ? sinf->keytype |
378 | 544k | : (sinf->sig_name != NULL |
379 | 544k | ? sinf->sig_name |
380 | 544k | : sinf->sigalg_name)); |
381 | 544k | } |
382 | | |
383 | 181k | #define TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE 10 |
384 | | static OSSL_CALLBACK add_provider_sigalgs; |
385 | | static int add_provider_sigalgs(const OSSL_PARAM params[], void *data) |
386 | 272k | { |
387 | 272k | struct provider_ctx_data_st *pgd = data; |
388 | 272k | SSL_CTX *ctx = pgd->ctx; |
389 | 272k | OSSL_PROVIDER *provider = pgd->provider; |
390 | 272k | const OSSL_PARAM *p; |
391 | 272k | TLS_SIGALG_INFO *sinf = NULL; |
392 | 272k | EVP_KEYMGMT *keymgmt; |
393 | 272k | const char *keytype; |
394 | 272k | unsigned int code_point = 0; |
395 | 272k | int ret = 0; |
396 | | |
397 | 272k | if (ctx->sigalg_list_max_len == ctx->sigalg_list_len) { |
398 | 90.6k | TLS_SIGALG_INFO *tmp = NULL; |
399 | | |
400 | 90.6k | if (ctx->sigalg_list_max_len == 0) |
401 | 90.6k | tmp = OPENSSL_malloc_array(TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE, |
402 | 90.6k | sizeof(TLS_SIGALG_INFO)); |
403 | 0 | else |
404 | 0 | tmp = OPENSSL_realloc_array(ctx->sigalg_list, |
405 | 90.6k | ctx->sigalg_list_max_len |
406 | 90.6k | + TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE, |
407 | 90.6k | sizeof(TLS_SIGALG_INFO)); |
408 | 90.6k | if (tmp == NULL) |
409 | 0 | return 0; |
410 | 90.6k | ctx->sigalg_list = tmp; |
411 | 90.6k | memset(tmp + ctx->sigalg_list_max_len, 0, |
412 | 90.6k | sizeof(TLS_SIGALG_INFO) * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE); |
413 | 90.6k | ctx->sigalg_list_max_len += TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE; |
414 | 90.6k | } |
415 | | |
416 | 272k | sinf = &ctx->sigalg_list[ctx->sigalg_list_len]; |
417 | | |
418 | | /* First, mandatory parameters */ |
419 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_NAME); |
420 | 272k | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
421 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
422 | 0 | goto err; |
423 | 0 | } |
424 | 272k | OPENSSL_free(sinf->sigalg_name); |
425 | 272k | sinf->sigalg_name = OPENSSL_strdup(p->data); |
426 | 272k | if (sinf->sigalg_name == NULL) |
427 | 0 | goto err; |
428 | | |
429 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME); |
430 | 272k | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
431 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
432 | 0 | goto err; |
433 | 0 | } |
434 | 272k | OPENSSL_free(sinf->name); |
435 | 272k | sinf->name = OPENSSL_strdup(p->data); |
436 | 272k | if (sinf->name == NULL) |
437 | 0 | goto err; |
438 | | |
439 | 272k | p = OSSL_PARAM_locate_const(params, |
440 | 272k | OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT); |
441 | 272k | if (p == NULL |
442 | 272k | || !OSSL_PARAM_get_uint(p, &code_point) |
443 | 272k | || code_point > UINT16_MAX) { |
444 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
445 | 0 | goto err; |
446 | 0 | } |
447 | 272k | sinf->code_point = (uint16_t)code_point; |
448 | | |
449 | 272k | p = OSSL_PARAM_locate_const(params, |
450 | 272k | OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS); |
451 | 272k | if (p == NULL || !OSSL_PARAM_get_uint(p, &sinf->secbits)) { |
452 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
453 | 0 | goto err; |
454 | 0 | } |
455 | | |
456 | | /* Now, optional parameters */ |
457 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_OID); |
458 | 272k | if (p == NULL) { |
459 | 0 | sinf->sigalg_oid = NULL; |
460 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
461 | 0 | goto err; |
462 | 272k | } else { |
463 | 272k | OPENSSL_free(sinf->sigalg_oid); |
464 | 272k | sinf->sigalg_oid = OPENSSL_strdup(p->data); |
465 | 272k | if (sinf->sigalg_oid == NULL) |
466 | 0 | goto err; |
467 | 272k | } |
468 | | |
469 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME); |
470 | 272k | if (p == NULL) { |
471 | 272k | sinf->sig_name = NULL; |
472 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
473 | 0 | goto err; |
474 | 0 | } else { |
475 | 0 | OPENSSL_free(sinf->sig_name); |
476 | 0 | sinf->sig_name = OPENSSL_strdup(p->data); |
477 | 0 | if (sinf->sig_name == NULL) |
478 | 0 | goto err; |
479 | 0 | } |
480 | | |
481 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_OID); |
482 | 272k | if (p == NULL) { |
483 | 272k | sinf->sig_oid = NULL; |
484 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
485 | 0 | goto err; |
486 | 0 | } else { |
487 | 0 | OPENSSL_free(sinf->sig_oid); |
488 | 0 | sinf->sig_oid = OPENSSL_strdup(p->data); |
489 | 0 | if (sinf->sig_oid == NULL) |
490 | 0 | goto err; |
491 | 0 | } |
492 | | |
493 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME); |
494 | 272k | if (p == NULL) { |
495 | 272k | sinf->hash_name = NULL; |
496 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
497 | 0 | goto err; |
498 | 0 | } else { |
499 | 0 | OPENSSL_free(sinf->hash_name); |
500 | 0 | sinf->hash_name = OPENSSL_strdup(p->data); |
501 | 0 | if (sinf->hash_name == NULL) |
502 | 0 | goto err; |
503 | 0 | } |
504 | | |
505 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_OID); |
506 | 272k | if (p == NULL) { |
507 | 272k | sinf->hash_oid = NULL; |
508 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
509 | 0 | goto err; |
510 | 0 | } else { |
511 | 0 | OPENSSL_free(sinf->hash_oid); |
512 | 0 | sinf->hash_oid = OPENSSL_strdup(p->data); |
513 | 0 | if (sinf->hash_oid == NULL) |
514 | 0 | goto err; |
515 | 0 | } |
516 | | |
517 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE); |
518 | 272k | if (p == NULL) { |
519 | 272k | sinf->keytype = NULL; |
520 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
521 | 0 | goto err; |
522 | 0 | } else { |
523 | 0 | OPENSSL_free(sinf->keytype); |
524 | 0 | sinf->keytype = OPENSSL_strdup(p->data); |
525 | 0 | if (sinf->keytype == NULL) |
526 | 0 | goto err; |
527 | 0 | } |
528 | | |
529 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID); |
530 | 272k | if (p == NULL) { |
531 | 272k | sinf->keytype_oid = NULL; |
532 | 272k | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
533 | 0 | goto err; |
534 | 0 | } else { |
535 | 0 | OPENSSL_free(sinf->keytype_oid); |
536 | 0 | sinf->keytype_oid = OPENSSL_strdup(p->data); |
537 | 0 | if (sinf->keytype_oid == NULL) |
538 | 0 | goto err; |
539 | 0 | } |
540 | | |
541 | | /* Optional, not documented prior to 3.5 */ |
542 | 272k | sinf->mindtls = sinf->maxdtls = -1; |
543 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_DTLS); |
544 | 272k | if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->mindtls)) { |
545 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
546 | 0 | goto err; |
547 | 0 | } |
548 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_DTLS); |
549 | 272k | if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->maxdtls)) { |
550 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
551 | 0 | goto err; |
552 | 0 | } |
553 | | /* DTLS version numbers grow downward */ |
554 | 272k | if ((sinf->maxdtls != 0) && (sinf->maxdtls != -1) && ((sinf->maxdtls > sinf->mindtls))) { |
555 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
556 | 0 | goto err; |
557 | 0 | } |
558 | | /* No provider sigalgs are supported in DTLS, reset after checking. */ |
559 | 272k | sinf->mindtls = sinf->maxdtls = -1; |
560 | | |
561 | | /* The remaining parameters below are mandatory again */ |
562 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS); |
563 | 272k | if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->mintls)) { |
564 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
565 | 0 | goto err; |
566 | 0 | } |
567 | 272k | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS); |
568 | 272k | if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->maxtls)) { |
569 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
570 | 0 | goto err; |
571 | 0 | } |
572 | 272k | if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && ((sinf->maxtls < sinf->mintls))) { |
573 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
574 | 0 | goto err; |
575 | 0 | } |
576 | 272k | if ((sinf->mintls != 0) && (sinf->mintls != -1) && ((sinf->mintls > TLS1_3_VERSION))) |
577 | 0 | sinf->mintls = sinf->maxtls = -1; |
578 | 272k | if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && ((sinf->maxtls < TLS1_3_VERSION))) |
579 | 0 | sinf->mintls = sinf->maxtls = -1; |
580 | | |
581 | | /* Ignore unusable sigalgs */ |
582 | 272k | if (sinf->mintls == -1 && sinf->mindtls == -1) { |
583 | 0 | ret = 1; |
584 | 0 | goto err; |
585 | 0 | } |
586 | | |
587 | | /* |
588 | | * Now check that the algorithm is actually usable for our property query |
589 | | * string. Regardless of the result we still return success because we have |
590 | | * successfully processed this signature, even though we may decide not to |
591 | | * use it. |
592 | | */ |
593 | 272k | ret = 1; |
594 | 272k | ERR_set_mark(); |
595 | 272k | keytype = inferred_keytype(sinf); |
596 | 272k | keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, keytype, ctx->propq); |
597 | 272k | if (keymgmt != NULL) { |
598 | | /* |
599 | | * We have successfully fetched the algorithm - however if the provider |
600 | | * doesn't match this one then we ignore it. |
601 | | * |
602 | | * Note: We're cheating a little here. Technically if the same algorithm |
603 | | * is available from more than one provider then it is undefined which |
604 | | * implementation you will get back. Theoretically this could be |
605 | | * different every time...we assume here that you'll always get the |
606 | | * same one back if you repeat the exact same fetch. Is this a reasonable |
607 | | * assumption to make (in which case perhaps we should document this |
608 | | * behaviour)? |
609 | | */ |
610 | 272k | if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) { |
611 | | /* |
612 | | * We have a match - so we could use this signature; |
613 | | * Check proper object registration first, though. |
614 | | * Don't care about return value as this may have been |
615 | | * done within providers or previous calls to |
616 | | * add_provider_sigalgs. |
617 | | */ |
618 | 272k | OBJ_create(sinf->sigalg_oid, sinf->sigalg_name, NULL); |
619 | | /* sanity check: Without successful registration don't use alg */ |
620 | 272k | if ((OBJ_txt2nid(sinf->sigalg_name) == NID_undef) || (OBJ_nid2obj(OBJ_txt2nid(sinf->sigalg_name)) == NULL)) { |
621 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
622 | 0 | goto err; |
623 | 0 | } |
624 | 272k | if (sinf->sig_name != NULL) |
625 | 0 | OBJ_create(sinf->sig_oid, sinf->sig_name, NULL); |
626 | 272k | if (sinf->keytype != NULL) |
627 | 0 | OBJ_create(sinf->keytype_oid, sinf->keytype, NULL); |
628 | 272k | if (sinf->hash_name != NULL) |
629 | 0 | OBJ_create(sinf->hash_oid, sinf->hash_name, NULL); |
630 | 272k | OBJ_add_sigid(OBJ_txt2nid(sinf->sigalg_name), |
631 | 272k | (sinf->hash_name != NULL |
632 | 272k | ? OBJ_txt2nid(sinf->hash_name) |
633 | 272k | : NID_undef), |
634 | 272k | OBJ_txt2nid(keytype)); |
635 | 272k | ctx->sigalg_list_len++; |
636 | 272k | sinf = NULL; |
637 | 272k | } |
638 | 272k | EVP_KEYMGMT_free(keymgmt); |
639 | 272k | } |
640 | 272k | ERR_pop_to_mark(); |
641 | 272k | err: |
642 | 272k | if (sinf != NULL) { |
643 | 0 | OPENSSL_free(sinf->name); |
644 | 0 | sinf->name = NULL; |
645 | 0 | OPENSSL_free(sinf->sigalg_name); |
646 | 0 | sinf->sigalg_name = NULL; |
647 | 0 | OPENSSL_free(sinf->sigalg_oid); |
648 | 0 | sinf->sigalg_oid = NULL; |
649 | 0 | OPENSSL_free(sinf->sig_name); |
650 | 0 | sinf->sig_name = NULL; |
651 | 0 | OPENSSL_free(sinf->sig_oid); |
652 | 0 | sinf->sig_oid = NULL; |
653 | 0 | OPENSSL_free(sinf->hash_name); |
654 | 0 | sinf->hash_name = NULL; |
655 | 0 | OPENSSL_free(sinf->hash_oid); |
656 | 0 | sinf->hash_oid = NULL; |
657 | 0 | OPENSSL_free(sinf->keytype); |
658 | 0 | sinf->keytype = NULL; |
659 | 0 | OPENSSL_free(sinf->keytype_oid); |
660 | 0 | sinf->keytype_oid = NULL; |
661 | 0 | } |
662 | 272k | return ret; |
663 | 272k | } |
664 | | |
665 | | static int discover_provider_sigalgs(OSSL_PROVIDER *provider, void *vctx) |
666 | 304k | { |
667 | 304k | struct provider_ctx_data_st pgd; |
668 | | |
669 | 304k | pgd.ctx = vctx; |
670 | 304k | pgd.provider = provider; |
671 | 304k | OSSL_PROVIDER_get_capabilities(provider, "TLS-SIGALG", |
672 | 304k | add_provider_sigalgs, &pgd); |
673 | | /* |
674 | | * Always OK, even if provider doesn't support the capability: |
675 | | * Reconsider testing retval when legacy sigalgs are also loaded this way. |
676 | | */ |
677 | 304k | return 1; |
678 | 304k | } |
679 | | |
680 | | int ssl_load_sigalgs(SSL_CTX *ctx) |
681 | 152k | { |
682 | 152k | size_t i; |
683 | 152k | SSL_CERT_LOOKUP lu; |
684 | | |
685 | 152k | if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_sigalgs, ctx)) |
686 | 0 | return 0; |
687 | | |
688 | | /* now populate ctx->ssl_cert_info */ |
689 | 152k | if (ctx->sigalg_list_len > 0) { |
690 | 90.6k | OPENSSL_free(ctx->ssl_cert_info); |
691 | 90.6k | ctx->ssl_cert_info = OPENSSL_calloc(ctx->sigalg_list_len, sizeof(lu)); |
692 | 90.6k | if (ctx->ssl_cert_info == NULL) |
693 | 0 | return 0; |
694 | 362k | for (i = 0; i < ctx->sigalg_list_len; i++) { |
695 | 272k | const char *keytype = inferred_keytype(&ctx->sigalg_list[i]); |
696 | 272k | ctx->ssl_cert_info[i].pkey_nid = OBJ_txt2nid(keytype); |
697 | 272k | ctx->ssl_cert_info[i].amask = SSL_aANY; |
698 | 272k | } |
699 | 90.6k | } |
700 | | |
701 | | /* |
702 | | * For now, leave it at this: legacy sigalgs stay in their own |
703 | | * data structures until "legacy cleanup" occurs. |
704 | | */ |
705 | | |
706 | 152k | return 1; |
707 | 152k | } |
708 | | |
709 | | static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name) |
710 | 725k | { |
711 | 725k | size_t i; |
712 | | |
713 | 4.08M | for (i = 0; i < ctx->group_list_len; i++) { |
714 | 4.08M | if (OPENSSL_strcasecmp(ctx->group_list[i].tlsname, name) == 0 |
715 | 3.35M | || OPENSSL_strcasecmp(ctx->group_list[i].realname, name) == 0) |
716 | 725k | return ctx->group_list[i].group_id; |
717 | 4.08M | } |
718 | | |
719 | 0 | return 0; |
720 | 725k | } |
721 | | |
722 | | const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id) |
723 | 3.20M | { |
724 | 3.20M | size_t i; |
725 | | |
726 | 69.8M | for (i = 0; i < ctx->group_list_len; i++) { |
727 | 69.8M | if (ctx->group_list[i].group_id == group_id) |
728 | 3.20M | return &ctx->group_list[i]; |
729 | 69.8M | } |
730 | | |
731 | 0 | return NULL; |
732 | 3.20M | } |
733 | | |
734 | | const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id) |
735 | 0 | { |
736 | 0 | const TLS_GROUP_INFO *tls_group_info = tls1_group_id_lookup(ctx, group_id); |
737 | |
|
738 | 0 | if (tls_group_info == NULL) |
739 | 0 | return NULL; |
740 | | |
741 | 0 | return tls_group_info->tlsname; |
742 | 0 | } |
743 | | |
744 | | int tls1_group_id2nid(uint16_t group_id, int include_unknown) |
745 | 1.40M | { |
746 | 1.40M | size_t i; |
747 | | |
748 | 1.40M | if (group_id == 0) |
749 | 0 | return NID_undef; |
750 | | |
751 | | /* |
752 | | * Return well known Group NIDs - for backwards compatibility. This won't |
753 | | * work for groups we don't know about. |
754 | | */ |
755 | 44.8M | for (i = 0; i < OSSL_NELEM(nid_to_group); i++) { |
756 | 44.7M | if (nid_to_group[i].group_id == group_id) |
757 | 1.30M | return nid_to_group[i].nid; |
758 | 44.7M | } |
759 | 100k | if (!include_unknown) |
760 | 100k | return NID_undef; |
761 | 0 | return TLSEXT_nid_unknown | (int)group_id; |
762 | 100k | } |
763 | | |
764 | | uint16_t tls1_nid2group_id(int nid) |
765 | 28.1k | { |
766 | 28.1k | size_t i; |
767 | | |
768 | | /* |
769 | | * Return well known Group ids - for backwards compatibility. This won't |
770 | | * work for groups we don't know about. |
771 | | */ |
772 | 647k | for (i = 0; i < OSSL_NELEM(nid_to_group); i++) { |
773 | 647k | if (nid_to_group[i].nid == nid) |
774 | 28.1k | return nid_to_group[i].group_id; |
775 | 647k | } |
776 | | |
777 | 6 | return 0; |
778 | 28.1k | } |
779 | | |
780 | | /* |
781 | | * Set *pgroups to the supported groups list and *pgroupslen to |
782 | | * the number of groups supported. |
783 | | */ |
784 | | void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
785 | | size_t *pgroupslen) |
786 | 460k | { |
787 | 460k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
788 | | |
789 | | /* For Suite B mode only include P-256, P-384 */ |
790 | 460k | switch (tls1_suiteb(s)) { |
791 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
792 | 0 | *pgroups = suiteb_curves; |
793 | 0 | *pgroupslen = OSSL_NELEM(suiteb_curves); |
794 | 0 | break; |
795 | | |
796 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
797 | 0 | *pgroups = suiteb_curves; |
798 | 0 | *pgroupslen = 1; |
799 | 0 | break; |
800 | | |
801 | 0 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
802 | 0 | *pgroups = suiteb_curves + 1; |
803 | 0 | *pgroupslen = 1; |
804 | 0 | break; |
805 | | |
806 | 460k | default: |
807 | 460k | if (s->ext.supportedgroups == NULL) { |
808 | 231k | *pgroups = sctx->ext.supportedgroups; |
809 | 231k | *pgroupslen = sctx->ext.supportedgroups_len; |
810 | 231k | } else { |
811 | 228k | *pgroups = s->ext.supportedgroups; |
812 | 228k | *pgroupslen = s->ext.supportedgroups_len; |
813 | 228k | } |
814 | 460k | break; |
815 | 460k | } |
816 | 460k | } |
817 | | |
818 | | /* |
819 | | * Some comments for the function below: |
820 | | * s->ext.supportedgroups == NULL means legacy syntax (no [*,/,-]) from built-in group array. |
821 | | * In this case, we need to send exactly one key share, which MUST be the first (leftmost) |
822 | | * eligible group from the legacy list. Therefore, we provide the entire list of supported |
823 | | * groups in this case. |
824 | | * |
825 | | * A 'flag' to indicate legacy syntax is created by setting the number of key shares to 1, |
826 | | * but the groupID to 0. |
827 | | * The 'flag' is checked right at the beginning in tls_construct_ctos_key_share and either |
828 | | * the "list of requested key share groups" is used, or the "list of supported groups" in |
829 | | * combination with setting add_only_one = 1 is applied. |
830 | | */ |
831 | | void tls1_get_requested_keyshare_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
832 | | size_t *pgroupslen) |
833 | 49.5k | { |
834 | 49.5k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
835 | | |
836 | 49.5k | if (s->ext.supportedgroups == NULL) { |
837 | 0 | *pgroups = sctx->ext.supportedgroups; |
838 | 0 | *pgroupslen = sctx->ext.supportedgroups_len; |
839 | 49.5k | } else { |
840 | 49.5k | *pgroups = s->ext.keyshares; |
841 | 49.5k | *pgroupslen = s->ext.keyshares_len; |
842 | 49.5k | } |
843 | 49.5k | } |
844 | | |
845 | | void tls1_get_group_tuples(SSL_CONNECTION *s, const size_t **ptuples, |
846 | | size_t *ptupleslen) |
847 | 2.28k | { |
848 | 2.28k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
849 | | |
850 | 2.28k | if (s->ext.supportedgroups == NULL) { |
851 | 0 | *ptuples = sctx->ext.tuples; |
852 | 0 | *ptupleslen = sctx->ext.tuples_len; |
853 | 2.28k | } else { |
854 | 2.28k | *ptuples = s->ext.tuples; |
855 | 2.28k | *ptupleslen = s->ext.tuples_len; |
856 | 2.28k | } |
857 | 2.28k | } |
858 | | |
859 | | int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id, |
860 | | int minversion, int maxversion, |
861 | | int isec, int *okfortls13) |
862 | 1.48M | { |
863 | 1.48M | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
864 | 1.48M | group_id); |
865 | 1.48M | int ret; |
866 | 1.48M | int group_minversion, group_maxversion; |
867 | | |
868 | 1.48M | if (okfortls13 != NULL) |
869 | 978k | *okfortls13 = 0; |
870 | | |
871 | 1.48M | if (ginfo == NULL) |
872 | 0 | return 0; |
873 | | |
874 | 1.48M | group_minversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->mindtls : ginfo->mintls; |
875 | 1.48M | group_maxversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->maxdtls : ginfo->maxtls; |
876 | | |
877 | 1.48M | if (group_minversion < 0 || group_maxversion < 0) |
878 | 131k | return 0; |
879 | 1.35M | if (group_maxversion == 0) |
880 | 1.35M | ret = 1; |
881 | 0 | else |
882 | 0 | ret = (ssl_version_cmp(s, minversion, group_maxversion) <= 0); |
883 | 1.35M | if (group_minversion > 0) |
884 | 1.35M | ret &= (ssl_version_cmp(s, maxversion, group_minversion) >= 0); |
885 | | |
886 | 1.35M | if (!SSL_CONNECTION_IS_DTLS(s)) { |
887 | 1.17M | if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION) |
888 | 737k | *okfortls13 = (group_maxversion == 0) |
889 | 0 | || (group_maxversion >= TLS1_3_VERSION); |
890 | 1.17M | } |
891 | 1.35M | ret &= !isec |
892 | 324k | || strcmp(ginfo->algorithm, "EC") == 0 |
893 | 323k | || strcmp(ginfo->algorithm, "X25519") == 0 |
894 | 100k | || strcmp(ginfo->algorithm, "X448") == 0; |
895 | | |
896 | 1.35M | return ret; |
897 | 1.48M | } |
898 | | |
899 | | /* See if group is allowed by security callback */ |
900 | | int tls_group_allowed(SSL_CONNECTION *s, uint16_t group, int op) |
901 | 1.40M | { |
902 | 1.40M | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
903 | 1.40M | group); |
904 | 1.40M | unsigned char gtmp[2]; |
905 | | |
906 | 1.40M | if (ginfo == NULL) |
907 | 0 | return 0; |
908 | | |
909 | 1.40M | gtmp[0] = group >> 8; |
910 | 1.40M | gtmp[1] = group & 0xff; |
911 | 1.40M | return ssl_security(s, op, ginfo->secbits, |
912 | 1.40M | tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp); |
913 | 1.40M | } |
914 | | |
915 | | /* Return 1 if "id" is in "list" */ |
916 | | static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen) |
917 | 102k | { |
918 | 102k | size_t i; |
919 | 687k | for (i = 0; i < listlen; i++) |
920 | 631k | if (list[i] == id) |
921 | 46.6k | return 1; |
922 | 55.5k | return 0; |
923 | 102k | } |
924 | | |
925 | | typedef struct { |
926 | | TLS_GROUP_INFO *grp; |
927 | | size_t ix; |
928 | | } TLS_GROUP_IX; |
929 | | |
930 | | DEFINE_STACK_OF(TLS_GROUP_IX) |
931 | | |
932 | | static void free_wrapper(TLS_GROUP_IX *a) |
933 | 0 | { |
934 | 0 | OPENSSL_free(a); |
935 | 0 | } |
936 | | |
937 | | static int tls_group_ix_cmp(const TLS_GROUP_IX *const *a, |
938 | | const TLS_GROUP_IX *const *b) |
939 | 0 | { |
940 | 0 | int idcmpab = (*a)->grp->group_id < (*b)->grp->group_id; |
941 | 0 | int idcmpba = (*b)->grp->group_id < (*a)->grp->group_id; |
942 | 0 | int ixcmpab = (*a)->ix < (*b)->ix; |
943 | 0 | int ixcmpba = (*b)->ix < (*a)->ix; |
944 | | |
945 | | /* Ascending by group id */ |
946 | 0 | if (idcmpab != idcmpba) |
947 | 0 | return (idcmpba - idcmpab); |
948 | | /* Ascending by original appearance index */ |
949 | 0 | return ixcmpba - ixcmpab; |
950 | 0 | } |
951 | | |
952 | | int tls1_get0_implemented_groups(int min_proto_version, int max_proto_version, |
953 | | TLS_GROUP_INFO *grps, size_t num, long all, |
954 | | STACK_OF(OPENSSL_CSTRING) *out) |
955 | 0 | { |
956 | 0 | STACK_OF(TLS_GROUP_IX) *collect = NULL; |
957 | 0 | TLS_GROUP_IX *gix; |
958 | 0 | uint16_t id = 0; |
959 | 0 | int ret = 0; |
960 | 0 | int ix; |
961 | |
|
962 | 0 | if (grps == NULL || out == NULL || num > INT_MAX) |
963 | 0 | return 0; |
964 | 0 | if ((collect = sk_TLS_GROUP_IX_new(tls_group_ix_cmp)) == NULL) |
965 | 0 | return 0; |
966 | 0 | for (ix = 0; ix < (int)num; ++ix, ++grps) { |
967 | 0 | if (grps->mintls > 0 && max_proto_version > 0 |
968 | 0 | && grps->mintls > max_proto_version) |
969 | 0 | continue; |
970 | 0 | if (grps->maxtls > 0 && min_proto_version > 0 |
971 | 0 | && grps->maxtls < min_proto_version) |
972 | 0 | continue; |
973 | | |
974 | 0 | if ((gix = OPENSSL_malloc(sizeof(*gix))) == NULL) |
975 | 0 | goto end; |
976 | 0 | gix->grp = grps; |
977 | 0 | gix->ix = ix; |
978 | 0 | if (sk_TLS_GROUP_IX_push(collect, gix) <= 0) { |
979 | 0 | OPENSSL_free(gix); |
980 | 0 | goto end; |
981 | 0 | } |
982 | 0 | } |
983 | | |
984 | 0 | sk_TLS_GROUP_IX_sort(collect); |
985 | 0 | num = sk_TLS_GROUP_IX_num(collect); |
986 | 0 | for (ix = 0; ix < (int)num; ++ix) { |
987 | 0 | gix = sk_TLS_GROUP_IX_value(collect, ix); |
988 | 0 | if (!all && gix->grp->group_id == id) |
989 | 0 | continue; |
990 | 0 | id = gix->grp->group_id; |
991 | 0 | if (sk_OPENSSL_CSTRING_push(out, gix->grp->tlsname) <= 0) |
992 | 0 | goto end; |
993 | 0 | } |
994 | 0 | ret = 1; |
995 | |
|
996 | 0 | end: |
997 | 0 | sk_TLS_GROUP_IX_pop_free(collect, free_wrapper); |
998 | 0 | return ret; |
999 | 0 | } |
1000 | | |
1001 | | /*- |
1002 | | * For nmatch >= 0, return the id of the |nmatch|th shared group or 0 |
1003 | | * if there is no match. |
1004 | | * For nmatch == -1, return number of matches |
1005 | | * For nmatch == -2, return the id of the group to use for |
1006 | | * a tmp key, or 0 if there is no match. |
1007 | | */ |
1008 | | uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch) |
1009 | 44.4k | { |
1010 | 44.4k | const uint16_t *pref, *supp; |
1011 | 44.4k | size_t num_pref, num_supp, i; |
1012 | 44.4k | int k; |
1013 | 44.4k | SSL_CTX *ctx = SSL_CONNECTION_GET_CTX(s); |
1014 | | |
1015 | | /* Can't do anything on client side */ |
1016 | 44.4k | if (s->server == 0) |
1017 | 0 | return 0; |
1018 | 44.4k | if (nmatch == -2) { |
1019 | 10.9k | if (tls1_suiteb(s)) { |
1020 | | /* |
1021 | | * For Suite B ciphersuite determines curve: we already know |
1022 | | * these are acceptable due to previous checks. |
1023 | | */ |
1024 | 0 | unsigned long cid = s->s3.tmp.new_cipher->id; |
1025 | |
|
1026 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
1027 | 0 | return OSSL_TLS_GROUP_ID_secp256r1; |
1028 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
1029 | 0 | return OSSL_TLS_GROUP_ID_secp384r1; |
1030 | | /* Should never happen */ |
1031 | 0 | return 0; |
1032 | 0 | } |
1033 | | /* If not Suite B just return first preference shared curve */ |
1034 | 10.9k | nmatch = 0; |
1035 | 10.9k | } |
1036 | | /* |
1037 | | * If server preference set, our groups are the preference order |
1038 | | * otherwise peer decides. |
1039 | | */ |
1040 | 44.4k | if (s->options & SSL_OP_SERVER_PREFERENCE) { |
1041 | 0 | tls1_get_supported_groups(s, &pref, &num_pref); |
1042 | 0 | tls1_get_peer_groups(s, &supp, &num_supp); |
1043 | 44.4k | } else { |
1044 | 44.4k | tls1_get_peer_groups(s, &pref, &num_pref); |
1045 | 44.4k | tls1_get_supported_groups(s, &supp, &num_supp); |
1046 | 44.4k | } |
1047 | | |
1048 | 94.2k | for (k = 0, i = 0; i < num_pref; i++) { |
1049 | 72.1k | uint16_t id = pref[i]; |
1050 | 72.1k | const TLS_GROUP_INFO *inf; |
1051 | 72.1k | int minversion, maxversion; |
1052 | | |
1053 | 72.1k | if (!tls1_in_list(id, supp, num_supp) |
1054 | 28.4k | || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED)) |
1055 | 43.6k | continue; |
1056 | 28.4k | inf = tls1_group_id_lookup(ctx, id); |
1057 | 28.4k | if (!ossl_assert(inf != NULL)) |
1058 | 0 | return 0; |
1059 | | |
1060 | 28.4k | minversion = SSL_CONNECTION_IS_DTLS(s) |
1061 | 28.4k | ? inf->mindtls |
1062 | 28.4k | : inf->mintls; |
1063 | 28.4k | maxversion = SSL_CONNECTION_IS_DTLS(s) |
1064 | 28.4k | ? inf->maxdtls |
1065 | 28.4k | : inf->maxtls; |
1066 | 28.4k | if (maxversion == -1) |
1067 | 3.78k | continue; |
1068 | 24.6k | if ((minversion != 0 && ssl_version_cmp(s, s->version, minversion) < 0) |
1069 | 22.2k | || (maxversion != 0 |
1070 | 0 | && ssl_version_cmp(s, s->version, maxversion) > 0)) |
1071 | 2.43k | continue; |
1072 | | |
1073 | 22.2k | if (nmatch == k) |
1074 | 22.2k | return id; |
1075 | 0 | k++; |
1076 | 0 | } |
1077 | 22.1k | if (nmatch == -1) |
1078 | 0 | return k; |
1079 | | /* Out of range (nmatch > k). */ |
1080 | 22.1k | return 0; |
1081 | 22.1k | } |
1082 | | |
1083 | | int tls1_set_groups(uint16_t **grpext, size_t *grpextlen, |
1084 | | uint16_t **ksext, size_t *ksextlen, |
1085 | | size_t **tplext, size_t *tplextlen, |
1086 | | int *groups, size_t ngroups) |
1087 | 0 | { |
1088 | 0 | uint16_t *glist = NULL, *kslist = NULL; |
1089 | 0 | size_t *tpllist = NULL; |
1090 | 0 | size_t i; |
1091 | | /* |
1092 | | * Bitmap of groups included to detect duplicates: two variables are added |
1093 | | * to detect duplicates as some values are more than 32. |
1094 | | */ |
1095 | 0 | unsigned long *dup_list = NULL; |
1096 | 0 | unsigned long dup_list_egrp = 0; |
1097 | 0 | unsigned long dup_list_dhgrp = 0; |
1098 | |
|
1099 | 0 | if (ngroups == 0) { |
1100 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
1101 | 0 | return 0; |
1102 | 0 | } |
1103 | 0 | if ((glist = OPENSSL_malloc_array(ngroups, sizeof(*glist))) == NULL) |
1104 | 0 | goto err; |
1105 | 0 | if ((kslist = OPENSSL_malloc_array(1, sizeof(*kslist))) == NULL) |
1106 | 0 | goto err; |
1107 | 0 | if ((tpllist = OPENSSL_malloc_array(1, sizeof(*tpllist))) == NULL) |
1108 | 0 | goto err; |
1109 | 0 | for (i = 0; i < ngroups; i++) { |
1110 | 0 | unsigned long idmask; |
1111 | 0 | uint16_t id; |
1112 | 0 | id = tls1_nid2group_id(groups[i]); |
1113 | 0 | if ((id & 0x00FF) >= (sizeof(unsigned long) * 8)) |
1114 | 0 | goto err; |
1115 | 0 | idmask = 1L << (id & 0x00FF); |
1116 | 0 | dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp; |
1117 | 0 | if (!id || ((*dup_list) & idmask)) |
1118 | 0 | goto err; |
1119 | 0 | *dup_list |= idmask; |
1120 | 0 | glist[i] = id; |
1121 | 0 | } |
1122 | 0 | OPENSSL_free(*grpext); |
1123 | 0 | OPENSSL_free(*ksext); |
1124 | 0 | OPENSSL_free(*tplext); |
1125 | 0 | *grpext = glist; |
1126 | 0 | *grpextlen = ngroups; |
1127 | | /* |
1128 | | * No * prefix was used, let tls_construct_ctos_key_share choose a key |
1129 | | * share. This has the advantage that it will filter unsupported groups |
1130 | | * before choosing one, which this function does not do. See also the |
1131 | | * comment for tls1_get_requested_keyshare_groups. |
1132 | | */ |
1133 | 0 | kslist[0] = 0; |
1134 | 0 | *ksext = kslist; |
1135 | 0 | *ksextlen = 1; |
1136 | 0 | tpllist[0] = ngroups; |
1137 | 0 | *tplext = tpllist; |
1138 | 0 | *tplextlen = 1; |
1139 | 0 | return 1; |
1140 | 0 | err: |
1141 | 0 | OPENSSL_free(glist); |
1142 | 0 | OPENSSL_free(kslist); |
1143 | 0 | OPENSSL_free(tpllist); |
1144 | 0 | return 0; |
1145 | 0 | } |
1146 | | |
1147 | | /* |
1148 | | * Definition of DEFAULT[_XYZ] pseudo group names. |
1149 | | * A pseudo group name is actually a full list of groups, including prefixes |
1150 | | * and or tuple delimiters. It can be hierarchically defined (for potential future use). |
1151 | | * IMPORTANT REMARK: For ease of use, in the built-in lists of groups, unknown groups or |
1152 | | * groups not backed by a provider will always silently be ignored, even without '?' prefix |
1153 | | */ |
1154 | | typedef struct { |
1155 | | const char *list_name; /* The name of this pseudo group */ |
1156 | | const char *group_string; /* The group string of this pseudo group */ |
1157 | | } default_group_string_st; /* (can include '?', '*'. '-', '/' as needed) */ |
1158 | | |
1159 | | /* Built-in pseudo group-names must start with a (D or d) */ |
1160 | | static const char *DEFAULT_GROUPNAME_FIRST_CHARACTER = "D"; |
1161 | | |
1162 | | /* The list of all built-in pseudo-group-name structures */ |
1163 | | static const default_group_string_st default_group_strings[] = { |
1164 | | { DEFAULT_GROUP_NAME, TLS_DEFAULT_GROUP_LIST }, |
1165 | | { SUITE_B_GROUP_NAME, SUITE_B_GROUP_LIST } |
1166 | | }; |
1167 | | |
1168 | | /* |
1169 | | * Some GOST names are not resolved by tls1_group_name2id, |
1170 | | * hence we'll check for those manually |
1171 | | */ |
1172 | | typedef struct { |
1173 | | const char *group_name; |
1174 | | uint16_t groupID; |
1175 | | } name2id_st; |
1176 | | static const name2id_st name2id_arr[] = { |
1177 | | { "GC256A", OSSL_TLS_GROUP_ID_gc256A }, |
1178 | | { "GC256B", OSSL_TLS_GROUP_ID_gc256B }, |
1179 | | { "GC256C", OSSL_TLS_GROUP_ID_gc256C }, |
1180 | | { "GC256D", OSSL_TLS_GROUP_ID_gc256D }, |
1181 | | { "GC512A", OSSL_TLS_GROUP_ID_gc512A }, |
1182 | | { "GC512B", OSSL_TLS_GROUP_ID_gc512B }, |
1183 | | { "GC512C", OSSL_TLS_GROUP_ID_gc512C }, |
1184 | | }; |
1185 | | |
1186 | | /* |
1187 | | * Group list management: |
1188 | | * We establish three lists along with their related size counters: |
1189 | | * 1) List of (unique) groups |
1190 | | * 2) List of number of groups per group-priority-tuple |
1191 | | * 3) List of (unique) key share groups |
1192 | | */ |
1193 | 272k | #define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (64 Bytes chunks ~= cache line) */ |
1194 | | #define GROUP_NAME_BUFFER_LENGTH 64 /* Max length of a group name */ |
1195 | | |
1196 | | /* |
1197 | | * Preparation of the prefix used to indicate the desire to send a key share, |
1198 | | * the characters used as separators between groups or tuples of groups, the |
1199 | | * character to indicate that an unknown group should be ignored, and the |
1200 | | * character to indicate that a group should be deleted from a list |
1201 | | */ |
1202 | | #ifndef TUPLE_DELIMITER_CHARACTER |
1203 | | /* The prefix characters to indicate group tuple boundaries */ |
1204 | 90.6k | #define TUPLE_DELIMITER_CHARACTER '/' |
1205 | | #endif |
1206 | | #ifndef GROUP_DELIMITER_CHARACTER |
1207 | | /* The prefix characters to indicate group tuple boundaries */ |
1208 | 362k | #define GROUP_DELIMITER_CHARACTER ':' |
1209 | | #endif |
1210 | | #ifndef IGNORE_UNKNOWN_GROUP_CHARACTER |
1211 | | /* The prefix character to ignore unknown groups */ |
1212 | 725k | #define IGNORE_UNKNOWN_GROUP_CHARACTER '?' |
1213 | | #endif |
1214 | | #ifndef KEY_SHARE_INDICATOR_CHARACTER |
1215 | | /* The prefix character to trigger a key share addition */ |
1216 | 181k | #define KEY_SHARE_INDICATOR_CHARACTER '*' |
1217 | | #endif |
1218 | | #ifndef REMOVE_GROUP_INDICATOR_CHARACTER |
1219 | | /* The prefix character to trigger a key share removal */ |
1220 | 0 | #define REMOVE_GROUP_INDICATOR_CHARACTER '-' |
1221 | | #endif |
1222 | | static const char prefixes[] = { TUPLE_DELIMITER_CHARACTER, |
1223 | | GROUP_DELIMITER_CHARACTER, |
1224 | | IGNORE_UNKNOWN_GROUP_CHARACTER, |
1225 | | KEY_SHARE_INDICATOR_CHARACTER, |
1226 | | REMOVE_GROUP_INDICATOR_CHARACTER, |
1227 | | '\0' }; |
1228 | | |
1229 | | /* |
1230 | | * High-level description of how group strings are analyzed: |
1231 | | * A first call back function (tuple_cb) is used to process group tuples, and a |
1232 | | * second callback function (gid_cb) is used to process the groups inside a tuple. |
1233 | | * Those callback functions are (indirectly) called by CONF_parse_list with |
1234 | | * different separators (nominally ':' or '/'), a variable based on gid_cb_st |
1235 | | * is used to keep track of the parsing results between the various calls |
1236 | | */ |
1237 | | |
1238 | | typedef struct { |
1239 | | SSL_CTX *ctx; |
1240 | | /* Variables to hold the three lists (groups, requested keyshares, tuple structure) */ |
1241 | | size_t gidmax; /* The memory allocation chunk size for the group IDs */ |
1242 | | size_t gidcnt; /* Number of groups */ |
1243 | | uint16_t *gid_arr; /* The IDs of the supported groups (flat list) */ |
1244 | | size_t tplmax; /* The memory allocation chunk size for the tuple counters */ |
1245 | | size_t tplcnt; /* Number of tuples */ |
1246 | | size_t *tuplcnt_arr; /* The number of groups inside a tuple */ |
1247 | | size_t ksidmax; /* The memory allocation chunk size */ |
1248 | | size_t ksidcnt; /* Number of key shares */ |
1249 | | uint16_t *ksid_arr; /* The IDs of the key share groups (flat list) */ |
1250 | | /* Variable to keep state between execution of callback or helper functions */ |
1251 | | size_t tuple_mode; /* Keeps track whether tuple_cb called from 'the top' or from gid_cb */ |
1252 | | int ignore_unknown_default; /* Flag such that unknown groups for DEFAULT[_XYZ] are ignored */ |
1253 | | } gid_cb_st; |
1254 | | |
1255 | | /* Forward declaration of tuple callback function */ |
1256 | | static int tuple_cb(const char *tuple, int len, void *arg); |
1257 | | |
1258 | | /* |
1259 | | * Extract and process the individual groups (and their prefixes if present) |
1260 | | * present in a tuple. Note: The argument 'elem' is a NON-\0-terminated string |
1261 | | * and must be appended by a \0 if used as \0-terminated string |
1262 | | */ |
1263 | | static int gid_cb(const char *elem, int len, void *arg) |
1264 | 725k | { |
1265 | 725k | gid_cb_st *garg = arg; |
1266 | 725k | size_t i, j, k; |
1267 | 725k | uint16_t gid = 0; |
1268 | 725k | int found_group = 0; |
1269 | 725k | char etmp[GROUP_NAME_BUFFER_LENGTH]; |
1270 | 725k | int retval = 1; /* We assume success */ |
1271 | 725k | char *current_prefix; |
1272 | 725k | int ignore_unknown = 0; |
1273 | 725k | int add_keyshare = 0; |
1274 | 725k | int remove_group = 0; |
1275 | 725k | size_t restored_prefix_index = 0; |
1276 | 725k | char *restored_default_group_string; |
1277 | 725k | int continue_while_loop = 1; |
1278 | | |
1279 | | /* Sanity checks */ |
1280 | 725k | if (garg == NULL || elem == NULL || len <= 0) { |
1281 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
1282 | 0 | return 0; |
1283 | 0 | } |
1284 | | |
1285 | | /* Check the possible prefixes (remark: Leading and trailing spaces already cleared) */ |
1286 | 1.63M | while (continue_while_loop && len > 0 |
1287 | 1.63M | && ((current_prefix = strchr(prefixes, elem[0])) != NULL |
1288 | 906k | || OPENSSL_strncasecmp(current_prefix = (char *)DEFAULT_GROUPNAME_FIRST_CHARACTER, elem, 1) == 0)) { |
1289 | | |
1290 | 906k | switch (*current_prefix) { |
1291 | 0 | case TUPLE_DELIMITER_CHARACTER: |
1292 | | /* tuple delimiter not allowed here -> syntax error */ |
1293 | 0 | return -1; |
1294 | 0 | break; |
1295 | 0 | case GROUP_DELIMITER_CHARACTER: |
1296 | 0 | return -1; /* Not a valid prefix for a single group name-> syntax error */ |
1297 | 0 | break; |
1298 | 181k | case KEY_SHARE_INDICATOR_CHARACTER: |
1299 | 181k | if (add_keyshare) |
1300 | 0 | return -1; /* Only single key share prefix allowed -> syntax error */ |
1301 | 181k | add_keyshare = 1; |
1302 | 181k | ++elem; |
1303 | 181k | --len; |
1304 | 181k | break; |
1305 | 0 | case REMOVE_GROUP_INDICATOR_CHARACTER: |
1306 | 0 | if (remove_group) |
1307 | 0 | return -1; /* Only single remove group prefix allowed -> syntax error */ |
1308 | 0 | remove_group = 1; |
1309 | 0 | ++elem; |
1310 | 0 | --len; |
1311 | 0 | break; |
1312 | 725k | case IGNORE_UNKNOWN_GROUP_CHARACTER: |
1313 | 725k | if (ignore_unknown) |
1314 | 0 | return -1; /* Only single ? allowed -> syntax error */ |
1315 | 725k | ignore_unknown = 1; |
1316 | 725k | ++elem; |
1317 | 725k | --len; |
1318 | 725k | break; |
1319 | 0 | default: |
1320 | | /* |
1321 | | * Check whether a DEFAULT[_XYZ] 'pseudo group' (= a built-in |
1322 | | * list of groups) should be added |
1323 | | */ |
1324 | 0 | for (i = 0; i < OSSL_NELEM(default_group_strings); i++) { |
1325 | 0 | if ((size_t)len == (strlen(default_group_strings[i].list_name)) |
1326 | 0 | && OPENSSL_strncasecmp(default_group_strings[i].list_name, elem, len) == 0) { |
1327 | | /* |
1328 | | * We're asked to insert an entire list of groups from a |
1329 | | * DEFAULT[_XYZ] 'pseudo group' which we do by |
1330 | | * recursively calling this function (indirectly via |
1331 | | * CONF_parse_list and tuple_cb); essentially, we treat a DEFAULT |
1332 | | * group string like a tuple which is appended to the current tuple |
1333 | | * rather then starting a new tuple. Variable tuple_mode is the flag which |
1334 | | * controls append tuple vs start new tuple. |
1335 | | */ |
1336 | |
|
1337 | 0 | if (ignore_unknown || remove_group) |
1338 | 0 | return -1; /* removal or ignore not allowed here -> syntax error */ |
1339 | | |
1340 | | /* |
1341 | | * First, we restore any keyshare prefix in a new zero-terminated string |
1342 | | * (if not already present) |
1343 | | */ |
1344 | 0 | restored_default_group_string = OPENSSL_malloc(1 /* max prefix length */ + strlen(default_group_strings[i].group_string) + 1 /* \0 */); |
1345 | 0 | if (restored_default_group_string == NULL) |
1346 | 0 | return 0; |
1347 | 0 | if (add_keyshare |
1348 | | /* Remark: we tolerate a duplicated keyshare indicator here */ |
1349 | 0 | && default_group_strings[i].group_string[0] |
1350 | 0 | != KEY_SHARE_INDICATOR_CHARACTER) |
1351 | 0 | restored_default_group_string[restored_prefix_index++] = KEY_SHARE_INDICATOR_CHARACTER; |
1352 | |
|
1353 | 0 | memcpy(restored_default_group_string + restored_prefix_index, |
1354 | 0 | default_group_strings[i].group_string, |
1355 | 0 | strlen(default_group_strings[i].group_string)); |
1356 | 0 | restored_default_group_string[strlen(default_group_strings[i].group_string) + restored_prefix_index] = '\0'; |
1357 | | /* We execute the recursive call */ |
1358 | 0 | garg->ignore_unknown_default = 1; /* We ignore unknown groups for DEFAULT_XYZ */ |
1359 | | /* we enforce group mode (= append tuple) for DEFAULT_XYZ group lists */ |
1360 | 0 | garg->tuple_mode = 0; |
1361 | | /* We use the tuple_cb callback to process the pseudo group tuple */ |
1362 | 0 | retval = CONF_parse_list(restored_default_group_string, |
1363 | 0 | TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, garg); |
1364 | 0 | garg->tuple_mode = 1; /* next call to tuple_cb will again start new tuple */ |
1365 | 0 | garg->ignore_unknown_default = 0; /* reset to original value */ |
1366 | | /* We don't need the \0-terminated string anymore */ |
1367 | 0 | OPENSSL_free(restored_default_group_string); |
1368 | |
|
1369 | 0 | return retval; |
1370 | 0 | } |
1371 | 0 | } |
1372 | | /* |
1373 | | * If we reached this point, a group name started with a 'd' or 'D', but no request |
1374 | | * for a DEFAULT[_XYZ] 'pseudo group' was detected, hence processing of the group |
1375 | | * name can continue as usual (= the while loop checking prefixes can end) |
1376 | | */ |
1377 | 0 | continue_while_loop = 0; |
1378 | 0 | break; |
1379 | 906k | } |
1380 | 906k | } |
1381 | | |
1382 | 725k | if (len == 0) |
1383 | 0 | return -1; /* Seems we have prefxes without a group name -> syntax error */ |
1384 | | |
1385 | 725k | if (garg->ignore_unknown_default == 1) /* Always ignore unknown groups for DEFAULT[_XYZ] */ |
1386 | 0 | ignore_unknown = 1; |
1387 | | |
1388 | | /* Memory management in case more groups are present compared to initial allocation */ |
1389 | 725k | if (garg->gidcnt == garg->gidmax) { |
1390 | 0 | uint16_t *tmp = OPENSSL_realloc_array(garg->gid_arr, |
1391 | 0 | garg->gidmax + GROUPLIST_INCREMENT, |
1392 | 0 | sizeof(*garg->gid_arr)); |
1393 | |
|
1394 | 0 | if (tmp == NULL) |
1395 | 0 | return 0; |
1396 | | |
1397 | 0 | garg->gidmax += GROUPLIST_INCREMENT; |
1398 | 0 | garg->gid_arr = tmp; |
1399 | 0 | } |
1400 | | /* Memory management for key share groups */ |
1401 | 725k | if (garg->ksidcnt == garg->ksidmax) { |
1402 | 0 | uint16_t *tmp = OPENSSL_realloc_array(garg->ksid_arr, |
1403 | 0 | garg->ksidmax + GROUPLIST_INCREMENT, |
1404 | 0 | sizeof(*garg->ksid_arr)); |
1405 | |
|
1406 | 0 | if (tmp == NULL) |
1407 | 0 | return 0; |
1408 | 0 | garg->ksidmax += GROUPLIST_INCREMENT; |
1409 | 0 | garg->ksid_arr = tmp; |
1410 | 0 | } |
1411 | | |
1412 | 725k | if (len > (int)(sizeof(etmp) - 1)) |
1413 | 0 | return -1; /* group name to long -> syntax error */ |
1414 | | |
1415 | | /* |
1416 | | * Prepare addition or removal of a single group by converting |
1417 | | * a group name into its groupID equivalent |
1418 | | */ |
1419 | | |
1420 | | /* Create a \0-terminated string and get the gid for this group if possible */ |
1421 | 725k | memcpy(etmp, elem, len); |
1422 | 725k | etmp[len] = 0; |
1423 | | |
1424 | | /* Get the groupID */ |
1425 | 725k | gid = tls1_group_name2id(garg->ctx, etmp); |
1426 | | /* |
1427 | | * Handle the case where no valid groupID was returned |
1428 | | * e.g. for an unknown group, which we'd ignore (only) if relevant prefix was set |
1429 | | */ |
1430 | 725k | if (gid == 0) { |
1431 | | /* Is it one of the GOST groups ? */ |
1432 | 0 | for (i = 0; i < OSSL_NELEM(name2id_arr); i++) { |
1433 | 0 | if (OPENSSL_strcasecmp(etmp, name2id_arr[i].group_name) == 0) { |
1434 | 0 | gid = name2id_arr[i].groupID; |
1435 | 0 | break; |
1436 | 0 | } |
1437 | 0 | } |
1438 | 0 | if (gid == 0) { /* still not found */ |
1439 | | /* Unknown group - ignore if ignore_unknown; trigger error otherwise */ |
1440 | 0 | retval = ignore_unknown; |
1441 | 0 | goto done; |
1442 | 0 | } |
1443 | 0 | } |
1444 | | |
1445 | | /* Make sure that at least one provider is supporting this groupID */ |
1446 | 725k | found_group = 0; |
1447 | 4.08M | for (j = 0; j < garg->ctx->group_list_len; j++) |
1448 | 4.08M | if (garg->ctx->group_list[j].group_id == gid) { |
1449 | 725k | found_group = 1; |
1450 | 725k | break; |
1451 | 725k | } |
1452 | | |
1453 | | /* |
1454 | | * No provider supports this group - ignore if |
1455 | | * ignore_unknown; trigger error otherwise |
1456 | | */ |
1457 | 725k | if (found_group == 0) { |
1458 | 0 | retval = ignore_unknown; |
1459 | 0 | goto done; |
1460 | 0 | } |
1461 | | /* Remove group (and keyshare) from anywhere in the list if present, ignore if not present */ |
1462 | 725k | if (remove_group) { |
1463 | | /* Is the current group specified anywhere in the entire list so far? */ |
1464 | 0 | found_group = 0; |
1465 | 0 | for (i = 0; i < garg->gidcnt; i++) |
1466 | 0 | if (garg->gid_arr[i] == gid) { |
1467 | 0 | found_group = 1; |
1468 | 0 | break; |
1469 | 0 | } |
1470 | | /* The group to remove is at position i in the list of (zero indexed) groups */ |
1471 | 0 | if (found_group) { |
1472 | | /* We remove that group from its position (which is at i)... */ |
1473 | 0 | for (j = i; j < (garg->gidcnt - 1); j++) |
1474 | 0 | garg->gid_arr[j] = garg->gid_arr[j + 1]; /* ...shift remaining groups left ... */ |
1475 | 0 | garg->gidcnt--; /* ..and update the book keeping for the number of groups */ |
1476 | | |
1477 | | /* |
1478 | | * We also must update the number of groups either in a previous tuple (which we |
1479 | | * must identify and check whether it becomes empty due to the deletion) or in |
1480 | | * the current tuple, pending where the deleted group resides |
1481 | | */ |
1482 | 0 | k = 0; |
1483 | 0 | for (j = 0; j < garg->tplcnt; j++) { |
1484 | 0 | k += garg->tuplcnt_arr[j]; |
1485 | | /* Remark: i is zero-indexed, k is one-indexed */ |
1486 | 0 | if (k > i) { /* remove from one of the previous tuples */ |
1487 | 0 | garg->tuplcnt_arr[j]--; |
1488 | 0 | break; /* We took care not to have group duplicates, hence we can stop here */ |
1489 | 0 | } |
1490 | 0 | } |
1491 | 0 | if (k <= i) /* remove from current tuple */ |
1492 | 0 | garg->tuplcnt_arr[j]--; |
1493 | | |
1494 | | /* We also remove the group from the list of keyshares (if present) */ |
1495 | 0 | found_group = 0; |
1496 | 0 | for (i = 0; i < garg->ksidcnt; i++) |
1497 | 0 | if (garg->ksid_arr[i] == gid) { |
1498 | 0 | found_group = 1; |
1499 | 0 | break; |
1500 | 0 | } |
1501 | 0 | if (found_group) { |
1502 | | /* Found, hence we remove that keyshare from its position (which is at i)... */ |
1503 | 0 | for (j = i; j < (garg->ksidcnt - 1); j++) |
1504 | 0 | garg->ksid_arr[j] = garg->ksid_arr[j + 1]; /* shift remaining key shares */ |
1505 | | /* ... and update the book keeping */ |
1506 | 0 | garg->ksidcnt--; |
1507 | 0 | } |
1508 | 0 | } |
1509 | 725k | } else { /* Processing addition of a single new group */ |
1510 | | |
1511 | | /* Check for duplicates */ |
1512 | 3.26M | for (i = 0; i < garg->gidcnt; i++) |
1513 | 2.53M | if (garg->gid_arr[i] == gid) { |
1514 | | /* Duplicate group anywhere in the list of groups - ignore */ |
1515 | 0 | goto done; |
1516 | 0 | } |
1517 | | |
1518 | | /* Add the current group to the 'flat' list of groups */ |
1519 | 725k | garg->gid_arr[garg->gidcnt++] = gid; |
1520 | | /* and update the book keeping for the number of groups in current tuple */ |
1521 | 725k | garg->tuplcnt_arr[garg->tplcnt]++; |
1522 | | |
1523 | | /* We memorize if needed that we want to add a key share for the current group */ |
1524 | 725k | if (add_keyshare) |
1525 | 181k | garg->ksid_arr[garg->ksidcnt++] = gid; |
1526 | 725k | } |
1527 | | |
1528 | 725k | done: |
1529 | 725k | return retval; |
1530 | 725k | } |
1531 | | |
1532 | | /* Extract and process a tuple of groups */ |
1533 | | static int tuple_cb(const char *tuple, int len, void *arg) |
1534 | 362k | { |
1535 | 362k | gid_cb_st *garg = arg; |
1536 | 362k | int retval = 1; /* We assume success */ |
1537 | 362k | char *restored_tuple_string; |
1538 | | |
1539 | | /* Sanity checks */ |
1540 | 362k | if (garg == NULL || tuple == NULL || len <= 0) { |
1541 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
1542 | 0 | return 0; |
1543 | 0 | } |
1544 | | |
1545 | | /* Memory management for tuples */ |
1546 | 362k | if (garg->tplcnt == garg->tplmax) { |
1547 | 0 | size_t *tmp = OPENSSL_realloc_array(garg->tuplcnt_arr, |
1548 | 0 | garg->tplmax + GROUPLIST_INCREMENT, |
1549 | 0 | sizeof(*garg->tuplcnt_arr)); |
1550 | |
|
1551 | 0 | if (tmp == NULL) |
1552 | 0 | return 0; |
1553 | 0 | garg->tplmax += GROUPLIST_INCREMENT; |
1554 | 0 | garg->tuplcnt_arr = tmp; |
1555 | 0 | } |
1556 | | |
1557 | | /* Convert to \0-terminated string */ |
1558 | 362k | restored_tuple_string = OPENSSL_malloc(len + 1 /* \0 */); |
1559 | 362k | if (restored_tuple_string == NULL) |
1560 | 0 | return 0; |
1561 | 362k | memcpy(restored_tuple_string, tuple, len); |
1562 | 362k | restored_tuple_string[len] = '\0'; |
1563 | | |
1564 | | /* Analyze group list of this tuple */ |
1565 | 362k | retval = CONF_parse_list(restored_tuple_string, GROUP_DELIMITER_CHARACTER, 1, gid_cb, arg); |
1566 | | |
1567 | | /* We don't need the \o-terminated string anymore */ |
1568 | 362k | OPENSSL_free(restored_tuple_string); |
1569 | | |
1570 | 362k | if (garg->tuplcnt_arr[garg->tplcnt] > 0) { /* Some valid groups are present in current tuple... */ |
1571 | 362k | if (garg->tuple_mode) { |
1572 | | /* We 'close' the tuple */ |
1573 | 362k | garg->tplcnt++; |
1574 | 362k | garg->tuplcnt_arr[garg->tplcnt] = 0; /* Next tuple is initialized to be empty */ |
1575 | 362k | garg->tuple_mode = 1; /* next call will start a tuple (unless overridden in gid_cb) */ |
1576 | 362k | } |
1577 | 362k | } |
1578 | | |
1579 | 362k | return retval; |
1580 | 362k | } |
1581 | | |
1582 | | /* |
1583 | | * Set groups and prepare generation of keyshares based on a string of groupnames, |
1584 | | * names separated by the group or the tuple delimiter, with per-group prefixes to |
1585 | | * (1) add a key share for this group, (2) ignore the group if unknown to the current |
1586 | | * context, (3) delete a previous occurrence of the group in the current tuple. |
1587 | | * |
1588 | | * The list parsing is done in two hierarchical steps: The top-level step extracts the |
1589 | | * string of a tuple using tuple_cb, while the next lower step uses gid_cb to |
1590 | | * parse and process the groups inside a tuple |
1591 | | */ |
1592 | | int tls1_set_groups_list(SSL_CTX *ctx, |
1593 | | uint16_t **grpext, size_t *grpextlen, |
1594 | | uint16_t **ksext, size_t *ksextlen, |
1595 | | size_t **tplext, size_t *tplextlen, |
1596 | | const char *str) |
1597 | 90.6k | { |
1598 | 90.6k | size_t i = 0, j; |
1599 | 90.6k | int ret = 0, parse_ret = 0; |
1600 | 90.6k | gid_cb_st gcb; |
1601 | | |
1602 | | /* Sanity check */ |
1603 | 90.6k | if (ctx == NULL) { |
1604 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
1605 | 0 | return 0; |
1606 | 0 | } |
1607 | | |
1608 | 90.6k | memset(&gcb, 0, sizeof(gcb)); |
1609 | 90.6k | gcb.tuple_mode = 1; /* We prepare to collect the first tuple */ |
1610 | 90.6k | gcb.ignore_unknown_default = 0; |
1611 | 90.6k | gcb.gidmax = GROUPLIST_INCREMENT; |
1612 | 90.6k | gcb.tplmax = GROUPLIST_INCREMENT; |
1613 | 90.6k | gcb.ksidmax = GROUPLIST_INCREMENT; |
1614 | 90.6k | gcb.ctx = ctx; |
1615 | | |
1616 | | /* Prepare initial chunks of memory for groups, tuples and keyshares groupIDs */ |
1617 | 90.6k | gcb.gid_arr = OPENSSL_malloc_array(gcb.gidmax, sizeof(*gcb.gid_arr)); |
1618 | 90.6k | if (gcb.gid_arr == NULL) |
1619 | 0 | goto end; |
1620 | 90.6k | gcb.tuplcnt_arr = OPENSSL_malloc_array(gcb.tplmax, sizeof(*gcb.tuplcnt_arr)); |
1621 | 90.6k | if (gcb.tuplcnt_arr == NULL) |
1622 | 0 | goto end; |
1623 | 90.6k | gcb.tuplcnt_arr[0] = 0; |
1624 | 90.6k | gcb.ksid_arr = OPENSSL_malloc_array(gcb.ksidmax, sizeof(*gcb.ksid_arr)); |
1625 | 90.6k | if (gcb.ksid_arr == NULL) |
1626 | 0 | goto end; |
1627 | | |
1628 | 90.6k | while (str[0] != '\0' && isspace((unsigned char)*str)) |
1629 | 0 | str++; |
1630 | 90.6k | if (str[0] == '\0') |
1631 | 0 | goto empty_list; |
1632 | | |
1633 | | /* |
1634 | | * Start the (potentially recursive) tuple processing by calling CONF_parse_list |
1635 | | * with the TUPLE_DELIMITER_CHARACTER (which will call tuple_cb after cleaning spaces) |
1636 | | */ |
1637 | 90.6k | parse_ret = CONF_parse_list(str, TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, &gcb); |
1638 | | |
1639 | 90.6k | if (parse_ret == 0) |
1640 | 0 | goto end; |
1641 | 90.6k | if (parse_ret == -1) { |
1642 | 0 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
1643 | 0 | "Syntax error in '%s'", str); |
1644 | 0 | goto end; |
1645 | 0 | } |
1646 | | |
1647 | | /* |
1648 | | * We check whether a tuple was completely emptied by using "-" prefix |
1649 | | * excessively, in which case we remove the tuple |
1650 | | */ |
1651 | 453k | for (i = j = 0; j < gcb.tplcnt; j++) { |
1652 | 362k | if (gcb.tuplcnt_arr[j] == 0) |
1653 | 0 | continue; |
1654 | | /* If there's a gap, move to first unfilled slot */ |
1655 | 362k | if (j == i) |
1656 | 362k | ++i; |
1657 | 0 | else |
1658 | 0 | gcb.tuplcnt_arr[i++] = gcb.tuplcnt_arr[j]; |
1659 | 362k | } |
1660 | 90.6k | gcb.tplcnt = i; |
1661 | | |
1662 | 90.6k | if (gcb.ksidcnt > OPENSSL_CLIENT_MAX_KEY_SHARES) { |
1663 | 0 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
1664 | 0 | "To many keyshares requested in '%s' (max = %d)", |
1665 | 0 | str, OPENSSL_CLIENT_MAX_KEY_SHARES); |
1666 | 0 | goto end; |
1667 | 0 | } |
1668 | | |
1669 | | /* |
1670 | | * For backward compatibility we let the rest of the code know that a key share |
1671 | | * for the first valid group should be added if no "*" prefix was used anywhere |
1672 | | */ |
1673 | 90.6k | if (gcb.gidcnt > 0 && gcb.ksidcnt == 0) { |
1674 | | /* |
1675 | | * No key share group prefix character was used, hence we indicate that a single |
1676 | | * key share should be sent and flag that it should come from the supported_groups list |
1677 | | */ |
1678 | 0 | gcb.ksidcnt = 1; |
1679 | 0 | gcb.ksid_arr[0] = 0; |
1680 | 0 | } |
1681 | | |
1682 | 90.6k | empty_list: |
1683 | | /* |
1684 | | * A call to tls1_set_groups_list with any of the args (other than ctx) set |
1685 | | * to NULL only does a syntax check, hence we're done here and report success |
1686 | | */ |
1687 | 90.6k | if (grpext == NULL || ksext == NULL || tplext == NULL || grpextlen == NULL || ksextlen == NULL || tplextlen == NULL) { |
1688 | 0 | ret = 1; |
1689 | 0 | goto end; |
1690 | 0 | } |
1691 | | |
1692 | | /* |
1693 | | * tuple_cb and gid_cb combo ensures there are no duplicates or unknown groups so we |
1694 | | * can just go ahead and set the results (after disposing the existing) |
1695 | | */ |
1696 | 90.6k | OPENSSL_free(*grpext); |
1697 | 90.6k | *grpext = gcb.gid_arr; |
1698 | 90.6k | *grpextlen = gcb.gidcnt; |
1699 | 90.6k | OPENSSL_free(*ksext); |
1700 | 90.6k | *ksext = gcb.ksid_arr; |
1701 | 90.6k | *ksextlen = gcb.ksidcnt; |
1702 | 90.6k | OPENSSL_free(*tplext); |
1703 | 90.6k | *tplext = gcb.tuplcnt_arr; |
1704 | 90.6k | *tplextlen = gcb.tplcnt; |
1705 | | |
1706 | 90.6k | return 1; |
1707 | | |
1708 | 0 | end: |
1709 | 0 | OPENSSL_free(gcb.gid_arr); |
1710 | 0 | OPENSSL_free(gcb.tuplcnt_arr); |
1711 | 0 | OPENSSL_free(gcb.ksid_arr); |
1712 | 0 | return ret; |
1713 | 90.6k | } |
1714 | | |
1715 | | /* Check a group id matches preferences */ |
1716 | | int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id, |
1717 | | int check_own_groups) |
1718 | 36.6k | { |
1719 | 36.6k | const uint16_t *groups; |
1720 | 36.6k | size_t groups_len; |
1721 | | |
1722 | 36.6k | if (group_id == 0) |
1723 | 16 | return 0; |
1724 | | |
1725 | | /* Check for Suite B compliance */ |
1726 | 36.5k | if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) { |
1727 | 0 | unsigned long cid = s->s3.tmp.new_cipher->id; |
1728 | |
|
1729 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { |
1730 | 0 | if (group_id != OSSL_TLS_GROUP_ID_secp256r1) |
1731 | 0 | return 0; |
1732 | 0 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { |
1733 | 0 | if (group_id != OSSL_TLS_GROUP_ID_secp384r1) |
1734 | 0 | return 0; |
1735 | 0 | } else { |
1736 | | /* Should never happen */ |
1737 | 0 | return 0; |
1738 | 0 | } |
1739 | 0 | } |
1740 | | |
1741 | 36.5k | if (check_own_groups) { |
1742 | | /* Check group is one of our preferences */ |
1743 | 8.91k | tls1_get_supported_groups(s, &groups, &groups_len); |
1744 | 8.91k | if (!tls1_in_list(group_id, groups, groups_len)) |
1745 | 147 | return 0; |
1746 | 8.91k | } |
1747 | | |
1748 | 36.4k | if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK)) |
1749 | 0 | return 0; |
1750 | | |
1751 | | /* For clients, nothing more to check */ |
1752 | 36.4k | if (!s->server) |
1753 | 8.77k | return 1; |
1754 | | |
1755 | | /* Check group is one of peers preferences */ |
1756 | 27.6k | tls1_get_peer_groups(s, &groups, &groups_len); |
1757 | | |
1758 | | /* |
1759 | | * RFC 4492 does not require the supported elliptic curves extension |
1760 | | * so if it is not sent we can just choose any curve. |
1761 | | * It is invalid to send an empty list in the supported groups |
1762 | | * extension, so groups_len == 0 always means no extension. |
1763 | | */ |
1764 | 27.6k | if (groups_len == 0) |
1765 | 13.6k | return 1; |
1766 | 14.0k | return tls1_in_list(group_id, groups, groups_len); |
1767 | 27.6k | } |
1768 | | |
1769 | | void tls1_get_formatlist(SSL_CONNECTION *s, const unsigned char **pformats, |
1770 | | size_t *num_formats) |
1771 | 42.7k | { |
1772 | | /* |
1773 | | * If we have a custom point format list use it otherwise use default |
1774 | | */ |
1775 | 42.7k | if (s->ext.ecpointformats) { |
1776 | 0 | *pformats = s->ext.ecpointformats; |
1777 | 0 | *num_formats = s->ext.ecpointformats_len; |
1778 | 42.7k | } else if ((s->options & SSL_OP_LEGACY_EC_POINT_FORMATS) != 0) { |
1779 | 0 | *pformats = ecformats_all; |
1780 | | /* For Suite B we don't support char2 fields */ |
1781 | 0 | if (tls1_suiteb(s)) |
1782 | 0 | *num_formats = sizeof(ecformats_all) - 1; |
1783 | 0 | else |
1784 | 0 | *num_formats = sizeof(ecformats_all); |
1785 | 42.7k | } else { |
1786 | 42.7k | *pformats = ecformats_default; |
1787 | 42.7k | *num_formats = sizeof(ecformats_default); |
1788 | 42.7k | } |
1789 | 42.7k | } |
1790 | | |
1791 | | /* Check a key is compatible with compression extension */ |
1792 | | static int tls1_check_pkey_comp(SSL_CONNECTION *s, EVP_PKEY *pkey) |
1793 | 29.1k | { |
1794 | 29.1k | unsigned char comp_id; |
1795 | 29.1k | size_t i; |
1796 | 29.1k | int point_conv; |
1797 | | |
1798 | | /* If not an EC key nothing to check */ |
1799 | 29.1k | if (!EVP_PKEY_is_a(pkey, "EC")) |
1800 | 0 | return 1; |
1801 | | |
1802 | | /* Get required compression id */ |
1803 | 29.1k | point_conv = EVP_PKEY_get_ec_point_conv_form(pkey); |
1804 | 29.1k | if (point_conv == 0) |
1805 | 0 | return 0; |
1806 | 29.1k | if (point_conv == POINT_CONVERSION_UNCOMPRESSED) { |
1807 | 29.0k | comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
1808 | 29.0k | } else if (SSL_CONNECTION_IS_TLS13(s)) { |
1809 | | /* |
1810 | | * ec_point_formats extension is not used in TLSv1.3 so we ignore |
1811 | | * this check. |
1812 | | */ |
1813 | 0 | return 1; |
1814 | 104 | } else { |
1815 | 104 | int field_type = EVP_PKEY_get_field_type(pkey); |
1816 | | |
1817 | 104 | if (field_type == NID_X9_62_prime_field) |
1818 | 98 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
1819 | 6 | else if (field_type == NID_X9_62_characteristic_two_field) |
1820 | 0 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
1821 | 6 | else |
1822 | 6 | return 0; |
1823 | 104 | } |
1824 | | /* |
1825 | | * If point formats extension present check it, otherwise everything is |
1826 | | * supported (see RFC4492). |
1827 | | */ |
1828 | 29.1k | if (s->ext.peer_ecpointformats == NULL) |
1829 | 23.1k | return 1; |
1830 | | |
1831 | 10.4k | for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { |
1832 | 9.47k | if (s->ext.peer_ecpointformats[i] == comp_id) |
1833 | 5.04k | return 1; |
1834 | 9.47k | } |
1835 | 987 | return 0; |
1836 | 6.03k | } |
1837 | | |
1838 | | /* Return group id of a key */ |
1839 | | static uint16_t tls1_get_group_id(EVP_PKEY *pkey) |
1840 | 28.1k | { |
1841 | 28.1k | int curve_nid = ssl_get_EC_curve_nid(pkey); |
1842 | | |
1843 | 28.1k | if (curve_nid == NID_undef) |
1844 | 0 | return 0; |
1845 | 28.1k | return tls1_nid2group_id(curve_nid); |
1846 | 28.1k | } |
1847 | | |
1848 | | /* |
1849 | | * Check cert parameters compatible with extensions: currently just checks EC |
1850 | | * certificates have compatible curves and compression. |
1851 | | */ |
1852 | | static int tls1_check_cert_param(SSL_CONNECTION *s, X509 *x, int check_ee_md) |
1853 | 85.8k | { |
1854 | 85.8k | uint16_t group_id; |
1855 | 85.8k | EVP_PKEY *pkey; |
1856 | 85.8k | pkey = X509_get0_pubkey(x); |
1857 | 85.8k | if (pkey == NULL) |
1858 | 0 | return 0; |
1859 | | /* If not EC nothing to do */ |
1860 | 85.8k | if (!EVP_PKEY_is_a(pkey, "EC")) |
1861 | 57.2k | return 1; |
1862 | | /* Check compression */ |
1863 | 28.6k | if (!tls1_check_pkey_comp(s, pkey)) |
1864 | 959 | return 0; |
1865 | 27.6k | group_id = tls1_get_group_id(pkey); |
1866 | | /* |
1867 | | * For a server we allow the certificate to not be in our list of supported |
1868 | | * groups. |
1869 | | */ |
1870 | 27.6k | if (!tls1_check_group_id(s, group_id, !s->server)) |
1871 | 7.29k | return 0; |
1872 | | /* |
1873 | | * Special case for suite B. We *MUST* sign using SHA256+P-256 or |
1874 | | * SHA384+P-384. |
1875 | | */ |
1876 | 20.3k | if (check_ee_md && tls1_suiteb(s)) { |
1877 | 0 | int check_md; |
1878 | 0 | size_t i; |
1879 | | |
1880 | | /* Check to see we have necessary signing algorithm */ |
1881 | 0 | if (group_id == OSSL_TLS_GROUP_ID_secp256r1) |
1882 | 0 | check_md = NID_ecdsa_with_SHA256; |
1883 | 0 | else if (group_id == OSSL_TLS_GROUP_ID_secp384r1) |
1884 | 0 | check_md = NID_ecdsa_with_SHA384; |
1885 | 0 | else |
1886 | 0 | return 0; /* Should never happen */ |
1887 | 0 | for (i = 0; i < s->shared_sigalgslen; i++) { |
1888 | 0 | if (check_md == s->shared_sigalgs[i]->sigandhash) |
1889 | 0 | return 1; |
1890 | 0 | } |
1891 | 0 | return 0; |
1892 | 0 | } |
1893 | 20.3k | return 1; |
1894 | 20.3k | } |
1895 | | |
1896 | | /* |
1897 | | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
1898 | | * @s: SSL connection |
1899 | | * @cid: Cipher ID we're considering using |
1900 | | * |
1901 | | * Checks that the kECDHE cipher suite we're considering using |
1902 | | * is compatible with the client extensions. |
1903 | | * |
1904 | | * Returns 0 when the cipher can't be used or 1 when it can. |
1905 | | */ |
1906 | | int tls1_check_ec_tmp_key(SSL_CONNECTION *s, unsigned long cid) |
1907 | 36.5k | { |
1908 | | /* If not Suite B just need a shared group */ |
1909 | 36.5k | if (!tls1_suiteb(s)) |
1910 | 36.5k | return tls1_shared_group(s, 0) != 0; |
1911 | | /* |
1912 | | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other |
1913 | | * curves permitted. |
1914 | | */ |
1915 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
1916 | 0 | return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp256r1, 1); |
1917 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
1918 | 0 | return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp384r1, 1); |
1919 | | |
1920 | 0 | return 0; |
1921 | 0 | } |
1922 | | |
1923 | | /* Default sigalg schemes */ |
1924 | | static const uint16_t tls12_sigalgs[] = { |
1925 | | TLSEXT_SIGALG_mldsa65, |
1926 | | TLSEXT_SIGALG_mldsa87, |
1927 | | TLSEXT_SIGALG_mldsa44, |
1928 | | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1929 | | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
1930 | | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
1931 | | TLSEXT_SIGALG_ed25519, |
1932 | | TLSEXT_SIGALG_ed448, |
1933 | | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
1934 | | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, |
1935 | | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, |
1936 | | |
1937 | | TLSEXT_SIGALG_rsa_pss_pss_sha256, |
1938 | | TLSEXT_SIGALG_rsa_pss_pss_sha384, |
1939 | | TLSEXT_SIGALG_rsa_pss_pss_sha512, |
1940 | | TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
1941 | | TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
1942 | | TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
1943 | | |
1944 | | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
1945 | | TLSEXT_SIGALG_rsa_pkcs1_sha384, |
1946 | | TLSEXT_SIGALG_rsa_pkcs1_sha512, |
1947 | | |
1948 | | TLSEXT_SIGALG_ecdsa_sha224, |
1949 | | TLSEXT_SIGALG_ecdsa_sha1, |
1950 | | |
1951 | | TLSEXT_SIGALG_rsa_pkcs1_sha224, |
1952 | | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
1953 | | |
1954 | | TLSEXT_SIGALG_dsa_sha224, |
1955 | | TLSEXT_SIGALG_dsa_sha1, |
1956 | | |
1957 | | TLSEXT_SIGALG_dsa_sha256, |
1958 | | TLSEXT_SIGALG_dsa_sha384, |
1959 | | TLSEXT_SIGALG_dsa_sha512, |
1960 | | |
1961 | | #ifndef OPENSSL_NO_GOST |
1962 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
1963 | | TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
1964 | | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
1965 | | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
1966 | | TLSEXT_SIGALG_gostr34102001_gostr3411, |
1967 | | #endif |
1968 | | }; |
1969 | | |
1970 | | static const uint16_t suiteb_sigalgs[] = { |
1971 | | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1972 | | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 |
1973 | | }; |
1974 | | |
1975 | | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
1976 | | { TLSEXT_SIGALG_ecdsa_secp256r1_sha256_name, |
1977 | | "ECDSA+SHA256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1978 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
1979 | | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1, 0, |
1980 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
1981 | | { TLSEXT_SIGALG_ecdsa_secp384r1_sha384_name, |
1982 | | "ECDSA+SHA384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
1983 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
1984 | | NID_ecdsa_with_SHA384, NID_secp384r1, 1, 0, |
1985 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
1986 | | { TLSEXT_SIGALG_ecdsa_secp521r1_sha512_name, |
1987 | | "ECDSA+SHA512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
1988 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
1989 | | NID_ecdsa_with_SHA512, NID_secp521r1, 1, 0, |
1990 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
1991 | | |
1992 | | { TLSEXT_SIGALG_ed25519_name, |
1993 | | NULL, TLSEXT_SIGALG_ed25519, |
1994 | | NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519, |
1995 | | NID_undef, NID_undef, 1, 0, |
1996 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
1997 | | { TLSEXT_SIGALG_ed448_name, |
1998 | | NULL, TLSEXT_SIGALG_ed448, |
1999 | | NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448, |
2000 | | NID_undef, NID_undef, 1, 0, |
2001 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2002 | | |
2003 | | { TLSEXT_SIGALG_ecdsa_sha224_name, |
2004 | | "ECDSA+SHA224", TLSEXT_SIGALG_ecdsa_sha224, |
2005 | | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2006 | | NID_ecdsa_with_SHA224, NID_undef, 1, 0, |
2007 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2008 | | { TLSEXT_SIGALG_ecdsa_sha1_name, |
2009 | | "ECDSA+SHA1", TLSEXT_SIGALG_ecdsa_sha1, |
2010 | | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2011 | | NID_ecdsa_with_SHA1, NID_undef, 1, 0, |
2012 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2013 | | |
2014 | | { TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_name, |
2015 | | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_alias, |
2016 | | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
2017 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2018 | | NID_ecdsa_with_SHA256, NID_brainpoolP256r1, 1, 0, |
2019 | | TLS1_3_VERSION, 0, -1, -1 }, |
2020 | | { TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_name, |
2021 | | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_alias, |
2022 | | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, |
2023 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2024 | | NID_ecdsa_with_SHA384, NID_brainpoolP384r1, 1, 0, |
2025 | | TLS1_3_VERSION, 0, -1, -1 }, |
2026 | | { TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_name, |
2027 | | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_alias, |
2028 | | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, |
2029 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2030 | | NID_ecdsa_with_SHA512, NID_brainpoolP512r1, 1, 0, |
2031 | | TLS1_3_VERSION, 0, -1, -1 }, |
2032 | | |
2033 | | { TLSEXT_SIGALG_rsa_pss_rsae_sha256_name, |
2034 | | "PSS+SHA256", TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
2035 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
2036 | | NID_undef, NID_undef, 1, 0, |
2037 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2038 | | { TLSEXT_SIGALG_rsa_pss_rsae_sha384_name, |
2039 | | "PSS+SHA384", TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
2040 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
2041 | | NID_undef, NID_undef, 1, 0, |
2042 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2043 | | { TLSEXT_SIGALG_rsa_pss_rsae_sha512_name, |
2044 | | "PSS+SHA512", TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
2045 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
2046 | | NID_undef, NID_undef, 1, 0, |
2047 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2048 | | |
2049 | | { TLSEXT_SIGALG_rsa_pss_pss_sha256_name, |
2050 | | NULL, TLSEXT_SIGALG_rsa_pss_pss_sha256, |
2051 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
2052 | | NID_undef, NID_undef, 1, 0, |
2053 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2054 | | { TLSEXT_SIGALG_rsa_pss_pss_sha384_name, |
2055 | | NULL, TLSEXT_SIGALG_rsa_pss_pss_sha384, |
2056 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
2057 | | NID_undef, NID_undef, 1, 0, |
2058 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2059 | | { TLSEXT_SIGALG_rsa_pss_pss_sha512_name, |
2060 | | NULL, TLSEXT_SIGALG_rsa_pss_pss_sha512, |
2061 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
2062 | | NID_undef, NID_undef, 1, 0, |
2063 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2064 | | |
2065 | | { TLSEXT_SIGALG_rsa_pkcs1_sha256_name, |
2066 | | "RSA+SHA256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
2067 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2068 | | NID_sha256WithRSAEncryption, NID_undef, 1, 0, |
2069 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2070 | | { TLSEXT_SIGALG_rsa_pkcs1_sha384_name, |
2071 | | "RSA+SHA384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
2072 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2073 | | NID_sha384WithRSAEncryption, NID_undef, 1, 0, |
2074 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2075 | | { TLSEXT_SIGALG_rsa_pkcs1_sha512_name, |
2076 | | "RSA+SHA512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
2077 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2078 | | NID_sha512WithRSAEncryption, NID_undef, 1, 0, |
2079 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0 }, |
2080 | | |
2081 | | { TLSEXT_SIGALG_rsa_pkcs1_sha224_name, |
2082 | | "RSA+SHA224", TLSEXT_SIGALG_rsa_pkcs1_sha224, |
2083 | | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2084 | | NID_sha224WithRSAEncryption, NID_undef, 1, 0, |
2085 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2086 | | { TLSEXT_SIGALG_rsa_pkcs1_sha1_name, |
2087 | | "RSA+SHA1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
2088 | | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2089 | | NID_sha1WithRSAEncryption, NID_undef, 1, 0, |
2090 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2091 | | |
2092 | | { TLSEXT_SIGALG_dsa_sha256_name, |
2093 | | "DSA+SHA256", TLSEXT_SIGALG_dsa_sha256, |
2094 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2095 | | NID_dsa_with_SHA256, NID_undef, 1, 0, |
2096 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2097 | | { TLSEXT_SIGALG_dsa_sha384_name, |
2098 | | "DSA+SHA384", TLSEXT_SIGALG_dsa_sha384, |
2099 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2100 | | NID_undef, NID_undef, 1, 0, |
2101 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2102 | | { TLSEXT_SIGALG_dsa_sha512_name, |
2103 | | "DSA+SHA512", TLSEXT_SIGALG_dsa_sha512, |
2104 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2105 | | NID_undef, NID_undef, 1, 0, |
2106 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2107 | | { TLSEXT_SIGALG_dsa_sha224_name, |
2108 | | "DSA+SHA224", TLSEXT_SIGALG_dsa_sha224, |
2109 | | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2110 | | NID_undef, NID_undef, 1, 0, |
2111 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2112 | | { TLSEXT_SIGALG_dsa_sha1_name, |
2113 | | "DSA+SHA1", TLSEXT_SIGALG_dsa_sha1, |
2114 | | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2115 | | NID_dsaWithSHA1, NID_undef, 1, 0, |
2116 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2117 | | |
2118 | | #ifndef OPENSSL_NO_GOST |
2119 | | { TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ |
2120 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic_name, |
2121 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
2122 | | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
2123 | | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
2124 | | NID_undef, NID_undef, 1, 0, |
2125 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2126 | | { TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ |
2127 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic_name, |
2128 | | TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
2129 | | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
2130 | | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
2131 | | NID_undef, NID_undef, 1, 0, |
2132 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2133 | | |
2134 | | { TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256_name, |
2135 | | NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
2136 | | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
2137 | | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
2138 | | NID_undef, NID_undef, 1, 0, |
2139 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2140 | | { TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512_name, |
2141 | | NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
2142 | | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
2143 | | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
2144 | | NID_undef, NID_undef, 1, 0, |
2145 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2146 | | { TLSEXT_SIGALG_gostr34102001_gostr3411_name, |
2147 | | NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
2148 | | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
2149 | | NID_id_GostR3410_2001, SSL_PKEY_GOST01, |
2150 | | NID_undef, NID_undef, 1, 0, |
2151 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION }, |
2152 | | #endif |
2153 | | }; |
2154 | | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
2155 | | static const SIGALG_LOOKUP legacy_rsa_sigalg = { |
2156 | | "rsa_pkcs1_md5_sha1", NULL, 0, |
2157 | | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, |
2158 | | EVP_PKEY_RSA, SSL_PKEY_RSA, |
2159 | | NID_undef, NID_undef, 1, 0, |
2160 | | TLS1_VERSION, TLS1_2_VERSION, DTLS1_VERSION, DTLS1_2_VERSION |
2161 | | }; |
2162 | | |
2163 | | /* |
2164 | | * Default signature algorithm values used if signature algorithms not present. |
2165 | | * From RFC5246. Note: order must match certificate index order. |
2166 | | */ |
2167 | | static const uint16_t tls_default_sigalg[] = { |
2168 | | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ |
2169 | | 0, /* SSL_PKEY_RSA_PSS_SIGN */ |
2170 | | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
2171 | | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ |
2172 | | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ |
2173 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */ |
2174 | | TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */ |
2175 | | 0, /* SSL_PKEY_ED25519 */ |
2176 | | 0, /* SSL_PKEY_ED448 */ |
2177 | | }; |
2178 | | |
2179 | | int ssl_setup_sigalgs(SSL_CTX *ctx) |
2180 | 90.6k | { |
2181 | 90.6k | size_t i, cache_idx, sigalgs_len, enabled; |
2182 | 90.6k | const SIGALG_LOOKUP *lu; |
2183 | 90.6k | SIGALG_LOOKUP *cache = NULL; |
2184 | 90.6k | uint16_t *tls12_sigalgs_list = NULL; |
2185 | 90.6k | EVP_PKEY *tmpkey = EVP_PKEY_new(); |
2186 | 90.6k | int istls; |
2187 | 90.6k | int ret = 0; |
2188 | | |
2189 | 90.6k | if (ctx == NULL) |
2190 | 0 | goto err; |
2191 | | |
2192 | 90.6k | istls = !SSL_CTX_IS_DTLS(ctx); |
2193 | | |
2194 | 90.6k | sigalgs_len = OSSL_NELEM(sigalg_lookup_tbl) + ctx->sigalg_list_len; |
2195 | | |
2196 | 90.6k | cache = OPENSSL_calloc(sigalgs_len, sizeof(const SIGALG_LOOKUP)); |
2197 | 90.6k | if (cache == NULL || tmpkey == NULL) |
2198 | 0 | goto err; |
2199 | | |
2200 | 90.6k | tls12_sigalgs_list = OPENSSL_calloc(sigalgs_len, sizeof(uint16_t)); |
2201 | 90.6k | if (tls12_sigalgs_list == NULL) |
2202 | 0 | goto err; |
2203 | | |
2204 | 90.6k | ERR_set_mark(); |
2205 | | /* First fill cache and tls12_sigalgs list from legacy algorithm list */ |
2206 | 90.6k | for (i = 0, lu = sigalg_lookup_tbl; |
2207 | 2.90M | i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { |
2208 | 2.81M | EVP_PKEY_CTX *pctx; |
2209 | | |
2210 | 2.81M | cache[i] = *lu; |
2211 | | |
2212 | | /* |
2213 | | * Check hash is available. |
2214 | | * This test is not perfect. A provider could have support |
2215 | | * for a signature scheme, but not a particular hash. However the hash |
2216 | | * could be available from some other loaded provider. In that case it |
2217 | | * could be that the signature is available, and the hash is available |
2218 | | * independently - but not as a combination. We ignore this for now. |
2219 | | */ |
2220 | 2.81M | if (lu->hash != NID_undef |
2221 | 2.63M | && ctx->ssl_digest_methods[lu->hash_idx] == NULL) { |
2222 | 453k | cache[i].available = 0; |
2223 | 453k | continue; |
2224 | 453k | } |
2225 | | |
2226 | 2.35M | if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { |
2227 | 0 | cache[i].available = 0; |
2228 | 0 | continue; |
2229 | 0 | } |
2230 | 2.35M | pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq); |
2231 | | /* If unable to create pctx we assume the sig algorithm is unavailable */ |
2232 | 2.35M | if (pctx == NULL) |
2233 | 0 | cache[i].available = 0; |
2234 | 2.35M | EVP_PKEY_CTX_free(pctx); |
2235 | 2.35M | } |
2236 | | |
2237 | | /* Now complete cache and tls12_sigalgs list with provider sig information */ |
2238 | 90.6k | cache_idx = OSSL_NELEM(sigalg_lookup_tbl); |
2239 | 362k | for (i = 0; i < ctx->sigalg_list_len; i++) { |
2240 | 272k | TLS_SIGALG_INFO si = ctx->sigalg_list[i]; |
2241 | 272k | cache[cache_idx].name = si.name; |
2242 | 272k | cache[cache_idx].name12 = si.sigalg_name; |
2243 | 272k | cache[cache_idx].sigalg = si.code_point; |
2244 | 272k | tls12_sigalgs_list[cache_idx] = si.code_point; |
2245 | 272k | cache[cache_idx].hash = si.hash_name ? OBJ_txt2nid(si.hash_name) : NID_undef; |
2246 | 272k | cache[cache_idx].hash_idx = ssl_get_md_idx(cache[cache_idx].hash); |
2247 | 272k | cache[cache_idx].sig = OBJ_txt2nid(si.sigalg_name); |
2248 | 272k | cache[cache_idx].sig_idx = (int)(i + SSL_PKEY_NUM); |
2249 | 272k | cache[cache_idx].sigandhash = OBJ_txt2nid(si.sigalg_name); |
2250 | 272k | cache[cache_idx].curve = NID_undef; |
2251 | 272k | cache[cache_idx].mintls = TLS1_3_VERSION; |
2252 | 272k | cache[cache_idx].maxtls = TLS1_3_VERSION; |
2253 | 272k | cache[cache_idx].mindtls = -1; |
2254 | 272k | cache[cache_idx].maxdtls = -1; |
2255 | | /* Compatibility with TLS 1.3 is checked on load */ |
2256 | 272k | cache[cache_idx].available = istls; |
2257 | 272k | cache[cache_idx].advertise = 0; |
2258 | 272k | cache_idx++; |
2259 | 272k | } |
2260 | 90.6k | ERR_pop_to_mark(); |
2261 | | |
2262 | 90.6k | enabled = 0; |
2263 | 3.17M | for (i = 0; i < OSSL_NELEM(tls12_sigalgs); ++i) { |
2264 | 3.08M | SIGALG_LOOKUP *ent = cache; |
2265 | 3.08M | size_t j; |
2266 | | |
2267 | 53.9M | for (j = 0; j < sigalgs_len; ent++, j++) { |
2268 | 53.9M | if (ent->sigalg != tls12_sigalgs[i]) |
2269 | 50.8M | continue; |
2270 | | /* Dedup by marking cache entry as default enabled. */ |
2271 | 3.08M | if (ent->available && !ent->advertise) { |
2272 | 2.55M | ent->advertise = 1; |
2273 | 2.55M | tls12_sigalgs_list[enabled++] = tls12_sigalgs[i]; |
2274 | 2.55M | } |
2275 | 3.08M | break; |
2276 | 53.9M | } |
2277 | 3.08M | } |
2278 | | |
2279 | | /* Append any provider sigalgs not yet handled */ |
2280 | 362k | for (i = OSSL_NELEM(sigalg_lookup_tbl); i < sigalgs_len; ++i) { |
2281 | 272k | SIGALG_LOOKUP *ent = &cache[i]; |
2282 | | |
2283 | 272k | if (ent->available && !ent->advertise) |
2284 | 0 | tls12_sigalgs_list[enabled++] = ent->sigalg; |
2285 | 272k | } |
2286 | | |
2287 | 90.6k | ctx->sigalg_lookup_cache = cache; |
2288 | 90.6k | ctx->sigalg_lookup_cache_len = sigalgs_len; |
2289 | 90.6k | ctx->tls12_sigalgs = tls12_sigalgs_list; |
2290 | 90.6k | ctx->tls12_sigalgs_len = enabled; |
2291 | 90.6k | cache = NULL; |
2292 | 90.6k | tls12_sigalgs_list = NULL; |
2293 | | |
2294 | 90.6k | ret = 1; |
2295 | 90.6k | err: |
2296 | 90.6k | OPENSSL_free(cache); |
2297 | 90.6k | OPENSSL_free(tls12_sigalgs_list); |
2298 | 90.6k | EVP_PKEY_free(tmpkey); |
2299 | 90.6k | return ret; |
2300 | 90.6k | } |
2301 | | |
2302 | 0 | #define SIGLEN_BUF_INCREMENT 100 |
2303 | | |
2304 | | char *SSL_get1_builtin_sigalgs(OSSL_LIB_CTX *libctx) |
2305 | 0 | { |
2306 | 0 | size_t i, maxretlen = SIGLEN_BUF_INCREMENT; |
2307 | 0 | const SIGALG_LOOKUP *lu; |
2308 | 0 | EVP_PKEY *tmpkey = EVP_PKEY_new(); |
2309 | 0 | char *retval = OPENSSL_malloc(maxretlen); |
2310 | |
|
2311 | 0 | if (retval == NULL) |
2312 | 0 | return NULL; |
2313 | | |
2314 | | /* ensure retval string is NUL terminated */ |
2315 | 0 | retval[0] = (char)0; |
2316 | |
|
2317 | 0 | for (i = 0, lu = sigalg_lookup_tbl; |
2318 | 0 | i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { |
2319 | 0 | EVP_PKEY_CTX *pctx; |
2320 | 0 | int enabled = 1; |
2321 | |
|
2322 | 0 | ERR_set_mark(); |
2323 | | /* Check hash is available in some provider. */ |
2324 | 0 | if (lu->hash != NID_undef) { |
2325 | 0 | EVP_MD *hash = EVP_MD_fetch(libctx, OBJ_nid2ln(lu->hash), NULL); |
2326 | | |
2327 | | /* If unable to create we assume the hash algorithm is unavailable */ |
2328 | 0 | if (hash == NULL) { |
2329 | 0 | enabled = 0; |
2330 | 0 | ERR_pop_to_mark(); |
2331 | 0 | continue; |
2332 | 0 | } |
2333 | 0 | EVP_MD_free(hash); |
2334 | 0 | } |
2335 | | |
2336 | 0 | if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { |
2337 | 0 | enabled = 0; |
2338 | 0 | ERR_pop_to_mark(); |
2339 | 0 | continue; |
2340 | 0 | } |
2341 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(libctx, tmpkey, NULL); |
2342 | | /* If unable to create pctx we assume the sig algorithm is unavailable */ |
2343 | 0 | if (pctx == NULL) |
2344 | 0 | enabled = 0; |
2345 | 0 | ERR_pop_to_mark(); |
2346 | 0 | EVP_PKEY_CTX_free(pctx); |
2347 | |
|
2348 | 0 | if (enabled) { |
2349 | 0 | const char *sa = lu->name; |
2350 | |
|
2351 | 0 | if (sa != NULL) { |
2352 | 0 | if (strlen(sa) + strlen(retval) + 1 >= maxretlen) { |
2353 | 0 | char *tmp; |
2354 | |
|
2355 | 0 | maxretlen += SIGLEN_BUF_INCREMENT; |
2356 | 0 | tmp = OPENSSL_realloc(retval, maxretlen); |
2357 | 0 | if (tmp == NULL) { |
2358 | 0 | OPENSSL_free(retval); |
2359 | 0 | return NULL; |
2360 | 0 | } |
2361 | 0 | retval = tmp; |
2362 | 0 | } |
2363 | 0 | if (strlen(retval) > 0) |
2364 | 0 | OPENSSL_strlcat(retval, ":", maxretlen); |
2365 | 0 | OPENSSL_strlcat(retval, sa, maxretlen); |
2366 | 0 | } else { |
2367 | | /* lu->name must not be NULL */ |
2368 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
2369 | 0 | } |
2370 | 0 | } |
2371 | 0 | } |
2372 | | |
2373 | 0 | EVP_PKEY_free(tmpkey); |
2374 | 0 | return retval; |
2375 | 0 | } |
2376 | | |
2377 | | /* Lookup TLS signature algorithm */ |
2378 | | static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL_CTX *ctx, |
2379 | | uint16_t sigalg) |
2380 | 14.5M | { |
2381 | 14.5M | size_t i; |
2382 | 14.5M | const SIGALG_LOOKUP *lu = ctx->sigalg_lookup_cache; |
2383 | | |
2384 | 232M | for (i = 0; i < ctx->sigalg_lookup_cache_len; lu++, i++) { |
2385 | 232M | if (lu->sigalg == sigalg) { |
2386 | 14.2M | if (!lu->available) |
2387 | 1.13M | return NULL; |
2388 | 13.0M | return lu; |
2389 | 14.2M | } |
2390 | 232M | } |
2391 | 377k | return NULL; |
2392 | 14.5M | } |
2393 | | |
2394 | | /* Lookup hash: return 0 if invalid or not enabled */ |
2395 | | int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd) |
2396 | 4.10M | { |
2397 | 4.10M | const EVP_MD *md; |
2398 | | |
2399 | 4.10M | if (lu == NULL) |
2400 | 0 | return 0; |
2401 | | /* lu->hash == NID_undef means no associated digest */ |
2402 | 4.10M | if (lu->hash == NID_undef) { |
2403 | 402k | md = NULL; |
2404 | 3.70M | } else { |
2405 | 3.70M | md = ssl_md(ctx, lu->hash_idx); |
2406 | 3.70M | if (md == NULL) |
2407 | 0 | return 0; |
2408 | 3.70M | } |
2409 | 4.10M | if (pmd) |
2410 | 4.01M | *pmd = md; |
2411 | 4.10M | return 1; |
2412 | 4.10M | } |
2413 | | |
2414 | | /* |
2415 | | * Check if key is large enough to generate RSA-PSS signature. |
2416 | | * |
2417 | | * The key must greater than or equal to 2 * hash length + 2. |
2418 | | * SHA512 has a hash length of 64 bytes, which is incompatible |
2419 | | * with a 128 byte (1024 bit) key. |
2420 | | */ |
2421 | 1.32k | #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_get_size(md) + 2) |
2422 | | static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey, |
2423 | | const SIGALG_LOOKUP *lu) |
2424 | 1.32k | { |
2425 | 1.32k | const EVP_MD *md; |
2426 | | |
2427 | 1.32k | if (pkey == NULL) |
2428 | 0 | return 0; |
2429 | 1.32k | if (!tls1_lookup_md(ctx, lu, &md) || md == NULL) |
2430 | 0 | return 0; |
2431 | 1.32k | if (EVP_MD_get_size(md) <= 0) |
2432 | 0 | return 0; |
2433 | 1.32k | if (EVP_PKEY_get_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md)) |
2434 | 0 | return 0; |
2435 | 1.32k | return 1; |
2436 | 1.32k | } |
2437 | | |
2438 | | /* |
2439 | | * Returns a signature algorithm when the peer did not send a list of supported |
2440 | | * signature algorithms. The signature algorithm is fixed for the certificate |
2441 | | * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the |
2442 | | * certificate type from |s| will be used. |
2443 | | * Returns the signature algorithm to use, or NULL on error. |
2444 | | */ |
2445 | | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s, |
2446 | | int idx) |
2447 | 276k | { |
2448 | 276k | if (idx == -1) { |
2449 | 20.4k | if (s->server) { |
2450 | 20.4k | size_t i; |
2451 | | |
2452 | | /* Work out index corresponding to ciphersuite */ |
2453 | 29.4k | for (i = 0; i < s->ssl_pkey_num; i++) { |
2454 | 29.4k | const SSL_CERT_LOOKUP *clu |
2455 | 29.4k | = ssl_cert_lookup_by_idx(i, SSL_CONNECTION_GET_CTX(s)); |
2456 | | |
2457 | 29.4k | if (clu == NULL) |
2458 | 0 | continue; |
2459 | 29.4k | if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) { |
2460 | 20.4k | idx = (int)i; |
2461 | 20.4k | break; |
2462 | 20.4k | } |
2463 | 29.4k | } |
2464 | | |
2465 | | /* |
2466 | | * Some GOST ciphersuites allow more than one signature algorithms |
2467 | | * */ |
2468 | 20.4k | if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) { |
2469 | 0 | int real_idx; |
2470 | |
|
2471 | 0 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01; |
2472 | 0 | real_idx--) { |
2473 | 0 | if (s->cert->pkeys[real_idx].privatekey != NULL) { |
2474 | 0 | idx = real_idx; |
2475 | 0 | break; |
2476 | 0 | } |
2477 | 0 | } |
2478 | 0 | } |
2479 | | /* |
2480 | | * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used |
2481 | | * with new (aGOST12-only) ciphersuites, we should find out which one is available really. |
2482 | | */ |
2483 | 20.4k | else if (idx == SSL_PKEY_GOST12_256) { |
2484 | 0 | int real_idx; |
2485 | |
|
2486 | 0 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256; |
2487 | 0 | real_idx--) { |
2488 | 0 | if (s->cert->pkeys[real_idx].privatekey != NULL) { |
2489 | 0 | idx = real_idx; |
2490 | 0 | break; |
2491 | 0 | } |
2492 | 0 | } |
2493 | 0 | } |
2494 | 20.4k | } else { |
2495 | 0 | idx = (int)(s->cert->key - s->cert->pkeys); |
2496 | 0 | } |
2497 | 20.4k | } |
2498 | 276k | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
2499 | 38.9k | return NULL; |
2500 | | |
2501 | 237k | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { |
2502 | 227k | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
2503 | 227k | tls_default_sigalg[idx]); |
2504 | | |
2505 | 227k | if (lu == NULL) |
2506 | 143k | return NULL; |
2507 | 84.0k | if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, NULL)) |
2508 | 0 | return NULL; |
2509 | 84.0k | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
2510 | 0 | return NULL; |
2511 | 84.0k | return lu; |
2512 | 84.0k | } |
2513 | 9.94k | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg)) |
2514 | 0 | return NULL; |
2515 | 9.94k | return &legacy_rsa_sigalg; |
2516 | 9.94k | } |
2517 | | /* Set peer sigalg based key type */ |
2518 | | int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey) |
2519 | 1.94k | { |
2520 | 1.94k | size_t idx; |
2521 | 1.94k | const SIGALG_LOOKUP *lu; |
2522 | | |
2523 | 1.94k | if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) |
2524 | 0 | return 0; |
2525 | 1.94k | lu = tls1_get_legacy_sigalg(s, (int)idx); |
2526 | 1.94k | if (lu == NULL) |
2527 | 9 | return 0; |
2528 | 1.93k | s->s3.tmp.peer_sigalg = lu; |
2529 | 1.93k | return 1; |
2530 | 1.94k | } |
2531 | | |
2532 | | size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent, const uint16_t **psigs) |
2533 | 538k | { |
2534 | | /* |
2535 | | * If Suite B mode use Suite B sigalgs only, ignore any other |
2536 | | * preferences. |
2537 | | */ |
2538 | 538k | switch (tls1_suiteb(s)) { |
2539 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
2540 | 0 | *psigs = suiteb_sigalgs; |
2541 | 0 | return OSSL_NELEM(suiteb_sigalgs); |
2542 | | |
2543 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
2544 | 0 | *psigs = suiteb_sigalgs; |
2545 | 0 | return 1; |
2546 | | |
2547 | 0 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
2548 | 0 | *psigs = suiteb_sigalgs + 1; |
2549 | 0 | return 1; |
2550 | 538k | } |
2551 | | /* |
2552 | | * We use client_sigalgs (if not NULL) if we're a server |
2553 | | * and sending a certificate request or if we're a client and |
2554 | | * determining which shared algorithm to use. |
2555 | | */ |
2556 | 538k | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { |
2557 | 0 | *psigs = s->cert->client_sigalgs; |
2558 | 0 | return s->cert->client_sigalgslen; |
2559 | 538k | } else if (s->cert->conf_sigalgs) { |
2560 | 0 | *psigs = s->cert->conf_sigalgs; |
2561 | 0 | return s->cert->conf_sigalgslen; |
2562 | 538k | } else { |
2563 | 538k | *psigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; |
2564 | 538k | return SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; |
2565 | 538k | } |
2566 | 538k | } |
2567 | | |
2568 | | /* |
2569 | | * Called by servers only. Checks that we have a sig alg that supports the |
2570 | | * specified EC curve. |
2571 | | */ |
2572 | | int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve) |
2573 | 0 | { |
2574 | 0 | const uint16_t *sigs; |
2575 | 0 | size_t siglen, i; |
2576 | |
|
2577 | 0 | if (s->cert->conf_sigalgs) { |
2578 | 0 | sigs = s->cert->conf_sigalgs; |
2579 | 0 | siglen = s->cert->conf_sigalgslen; |
2580 | 0 | } else { |
2581 | 0 | sigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; |
2582 | 0 | siglen = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; |
2583 | 0 | } |
2584 | |
|
2585 | 0 | for (i = 0; i < siglen; i++) { |
2586 | 0 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sigs[i]); |
2587 | |
|
2588 | 0 | if (lu == NULL) |
2589 | 0 | continue; |
2590 | 0 | if (lu->sig == EVP_PKEY_EC |
2591 | 0 | && lu->curve != NID_undef |
2592 | 0 | && curve == lu->curve) |
2593 | 0 | return 1; |
2594 | 0 | } |
2595 | | |
2596 | 0 | return 0; |
2597 | 0 | } |
2598 | | |
2599 | | /* |
2600 | | * Return the number of security bits for the signature algorithm, or 0 on |
2601 | | * error. |
2602 | | */ |
2603 | | static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu) |
2604 | 2.97M | { |
2605 | 2.97M | const EVP_MD *md = NULL; |
2606 | 2.97M | int secbits = 0; |
2607 | | |
2608 | 2.97M | if (!tls1_lookup_md(ctx, lu, &md)) |
2609 | 0 | return 0; |
2610 | 2.97M | if (md != NULL) { |
2611 | 2.63M | int md_type = EVP_MD_get_type(md); |
2612 | | |
2613 | | /* Security bits: half digest bits */ |
2614 | 2.63M | secbits = EVP_MD_get_size(md) * 4; |
2615 | 2.63M | if (secbits <= 0) |
2616 | 0 | return 0; |
2617 | | /* |
2618 | | * SHA1 and MD5 are known to be broken. Reduce security bits so that |
2619 | | * they're no longer accepted at security level 1. The real values don't |
2620 | | * really matter as long as they're lower than 80, which is our |
2621 | | * security level 1. |
2622 | | * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for |
2623 | | * SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2 |
2624 | | * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf |
2625 | | * puts a chosen-prefix attack for MD5 at 2^39. |
2626 | | */ |
2627 | 2.63M | if (md_type == NID_sha1) |
2628 | 247k | secbits = 64; |
2629 | 2.38M | else if (md_type == NID_md5_sha1) |
2630 | 7.63k | secbits = 67; |
2631 | 2.37M | else if (md_type == NID_md5) |
2632 | 0 | secbits = 39; |
2633 | 2.63M | } else { |
2634 | | /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */ |
2635 | 339k | if (lu->sigalg == TLSEXT_SIGALG_ed25519) |
2636 | 89.1k | secbits = 128; |
2637 | 250k | else if (lu->sigalg == TLSEXT_SIGALG_ed448) |
2638 | 101k | secbits = 224; |
2639 | 339k | } |
2640 | | /* |
2641 | | * For provider-based sigalgs we have secbits information available |
2642 | | * in the (provider-loaded) sigalg_list structure |
2643 | | */ |
2644 | 2.97M | if ((secbits == 0) && (lu->sig_idx >= SSL_PKEY_NUM) |
2645 | 148k | && ((lu->sig_idx - SSL_PKEY_NUM) < (int)ctx->sigalg_list_len)) { |
2646 | 148k | secbits = ctx->sigalg_list[lu->sig_idx - SSL_PKEY_NUM].secbits; |
2647 | 148k | } |
2648 | 2.97M | return secbits; |
2649 | 2.97M | } |
2650 | | |
2651 | | static int tls_sigalg_compat(SSL_CONNECTION *sc, const SIGALG_LOOKUP *lu) |
2652 | 1.85M | { |
2653 | 1.85M | int minversion, maxversion; |
2654 | 1.85M | int minproto, maxproto; |
2655 | | |
2656 | 1.85M | if (!lu->available) |
2657 | 0 | return 0; |
2658 | | |
2659 | 1.85M | if (SSL_CONNECTION_IS_DTLS(sc)) { |
2660 | 386k | if (sc->ssl.method->version == DTLS_ANY_VERSION) { |
2661 | 378k | minproto = sc->min_proto_version; |
2662 | 378k | maxproto = sc->max_proto_version; |
2663 | 378k | } else { |
2664 | 8.17k | maxproto = minproto = sc->version; |
2665 | 8.17k | } |
2666 | 386k | minversion = lu->mindtls; |
2667 | 386k | maxversion = lu->maxdtls; |
2668 | 1.47M | } else { |
2669 | 1.47M | if (sc->ssl.method->version == TLS_ANY_VERSION) { |
2670 | 1.43M | minproto = sc->min_proto_version; |
2671 | 1.43M | maxproto = sc->max_proto_version; |
2672 | 1.43M | } else { |
2673 | 36.8k | maxproto = minproto = sc->version; |
2674 | 36.8k | } |
2675 | 1.47M | minversion = lu->mintls; |
2676 | 1.47M | maxversion = lu->maxtls; |
2677 | 1.47M | } |
2678 | 1.85M | if (minversion == -1 || maxversion == -1 |
2679 | 1.81M | || (minversion != 0 && maxproto != 0 |
2680 | 44.6k | && ssl_version_cmp(sc, minversion, maxproto) > 0) |
2681 | 1.81M | || (maxversion != 0 && minproto != 0 |
2682 | 364k | && ssl_version_cmp(sc, maxversion, minproto) < 0) |
2683 | 1.54M | || !tls12_sigalg_allowed(sc, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
2684 | 311k | return 0; |
2685 | 1.54M | return 1; |
2686 | 1.85M | } |
2687 | | |
2688 | | /* |
2689 | | * Check signature algorithm is consistent with sent supported signature |
2690 | | * algorithms and if so set relevant digest and signature scheme in |
2691 | | * s. |
2692 | | */ |
2693 | | int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) |
2694 | 11.7k | { |
2695 | 11.7k | const uint16_t *sent_sigs; |
2696 | 11.7k | const EVP_MD *md = NULL; |
2697 | 11.7k | char sigalgstr[2]; |
2698 | 11.7k | size_t sent_sigslen, i, cidx; |
2699 | 11.7k | int pkeyid = -1; |
2700 | 11.7k | const SIGALG_LOOKUP *lu; |
2701 | 11.7k | int secbits = 0; |
2702 | | |
2703 | 11.7k | pkeyid = EVP_PKEY_get_id(pkey); |
2704 | | |
2705 | 11.7k | if (SSL_CONNECTION_IS_TLS13(s)) { |
2706 | | /* Disallow DSA for TLS 1.3 */ |
2707 | 9.44k | if (pkeyid == EVP_PKEY_DSA) { |
2708 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2709 | 0 | return 0; |
2710 | 0 | } |
2711 | | /* Only allow PSS for TLS 1.3 */ |
2712 | 9.44k | if (pkeyid == EVP_PKEY_RSA) |
2713 | 9.42k | pkeyid = EVP_PKEY_RSA_PSS; |
2714 | 9.44k | } |
2715 | | |
2716 | | /* Is this code point available and compatible with the protocol */ |
2717 | 11.7k | lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sig); |
2718 | 11.7k | if (lu == NULL || !tls_sigalg_compat(s, lu)) { |
2719 | 107 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2720 | 107 | return 0; |
2721 | 107 | } |
2722 | | |
2723 | | /* If we don't know the pkey nid yet go and find it */ |
2724 | 11.6k | if (pkeyid == EVP_PKEY_KEYMGMT) { |
2725 | 0 | const SSL_CERT_LOOKUP *scl = ssl_cert_lookup_by_pkey(pkey, NULL, SSL_CONNECTION_GET_CTX(s)); |
2726 | |
|
2727 | 0 | if (scl == NULL) { |
2728 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2729 | 0 | return 0; |
2730 | 0 | } |
2731 | 0 | pkeyid = scl->pkey_nid; |
2732 | 0 | } |
2733 | | |
2734 | | /* Should never happen */ |
2735 | 11.6k | if (pkeyid == -1) { |
2736 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2737 | 0 | return -1; |
2738 | 0 | } |
2739 | | |
2740 | | /* |
2741 | | * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type |
2742 | | * is consistent with signature: RSA keys can be used for RSA-PSS |
2743 | | */ |
2744 | 11.6k | if ((SSL_CONNECTION_IS_TLS13(s) |
2745 | 9.40k | && (lu->hash == NID_sha1 || lu->hash == NID_sha224)) |
2746 | 11.6k | || (pkeyid != lu->sig |
2747 | 322 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
2748 | 38 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2749 | 38 | return 0; |
2750 | 38 | } |
2751 | | /* Check the sigalg is consistent with the key OID */ |
2752 | 11.5k | if (!ssl_cert_lookup_by_nid( |
2753 | 11.5k | (pkeyid == EVP_PKEY_RSA_PSS) ? EVP_PKEY_get_id(pkey) : pkeyid, |
2754 | 11.5k | &cidx, SSL_CONNECTION_GET_CTX(s)) |
2755 | 11.5k | || lu->sig_idx != (int)cidx) { |
2756 | 8 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2757 | 8 | return 0; |
2758 | 8 | } |
2759 | | |
2760 | 11.5k | if (pkeyid == EVP_PKEY_EC) { |
2761 | | |
2762 | | /* Check point compression is permitted */ |
2763 | 224 | if (!tls1_check_pkey_comp(s, pkey)) { |
2764 | 12 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2765 | 12 | SSL_R_ILLEGAL_POINT_COMPRESSION); |
2766 | 12 | return 0; |
2767 | 12 | } |
2768 | | |
2769 | | /* For TLS 1.3 or Suite B check curve matches signature algorithm */ |
2770 | 212 | if (SSL_CONNECTION_IS_TLS13(s) || tls1_suiteb(s)) { |
2771 | 0 | int curve = ssl_get_EC_curve_nid(pkey); |
2772 | |
|
2773 | 0 | if (lu->curve != NID_undef && curve != lu->curve) { |
2774 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
2775 | 0 | return 0; |
2776 | 0 | } |
2777 | 0 | } |
2778 | 212 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
2779 | | /* Check curve matches extensions */ |
2780 | 212 | if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { |
2781 | 5 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
2782 | 5 | return 0; |
2783 | 5 | } |
2784 | 207 | if (tls1_suiteb(s)) { |
2785 | | /* Check sigalg matches a permissible Suite B value */ |
2786 | 0 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 |
2787 | 0 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { |
2788 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2789 | 0 | SSL_R_WRONG_SIGNATURE_TYPE); |
2790 | 0 | return 0; |
2791 | 0 | } |
2792 | 0 | } |
2793 | 207 | } |
2794 | 11.3k | } else if (tls1_suiteb(s)) { |
2795 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
2796 | 0 | return 0; |
2797 | 0 | } |
2798 | | |
2799 | | /* Check signature matches a type we sent */ |
2800 | 11.5k | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
2801 | 189k | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
2802 | 189k | if (sig == *sent_sigs) |
2803 | 11.5k | break; |
2804 | 189k | } |
2805 | | /* Allow fallback to SHA1 if not strict mode */ |
2806 | 11.5k | if (i == sent_sigslen && (lu->hash != NID_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
2807 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
2808 | 0 | return 0; |
2809 | 0 | } |
2810 | 11.5k | if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, &md)) { |
2811 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST); |
2812 | 0 | return 0; |
2813 | 0 | } |
2814 | | /* |
2815 | | * Make sure security callback allows algorithm. For historical |
2816 | | * reasons we have to pass the sigalg as a two byte char array. |
2817 | | */ |
2818 | 11.5k | sigalgstr[0] = (sig >> 8) & 0xff; |
2819 | 11.5k | sigalgstr[1] = sig & 0xff; |
2820 | 11.5k | secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
2821 | 11.5k | if (secbits == 0 || !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits, md != NULL ? EVP_MD_get_type(md) : NID_undef, (void *)sigalgstr)) { |
2822 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
2823 | 0 | return 0; |
2824 | 0 | } |
2825 | | /* Store the sigalg the peer uses */ |
2826 | 11.5k | s->s3.tmp.peer_sigalg = lu; |
2827 | 11.5k | return 1; |
2828 | 11.5k | } |
2829 | | |
2830 | | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
2831 | 0 | { |
2832 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
2833 | |
|
2834 | 0 | if (sc == NULL) |
2835 | 0 | return 0; |
2836 | | |
2837 | 0 | if (sc->s3.tmp.peer_sigalg == NULL) |
2838 | 0 | return 0; |
2839 | 0 | *pnid = sc->s3.tmp.peer_sigalg->sig; |
2840 | 0 | return 1; |
2841 | 0 | } |
2842 | | |
2843 | | int SSL_get_signature_type_nid(const SSL *s, int *pnid) |
2844 | 0 | { |
2845 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
2846 | |
|
2847 | 0 | if (sc == NULL) |
2848 | 0 | return 0; |
2849 | | |
2850 | 0 | if (sc->s3.tmp.sigalg == NULL) |
2851 | 0 | return 0; |
2852 | 0 | *pnid = sc->s3.tmp.sigalg->sig; |
2853 | 0 | return 1; |
2854 | 0 | } |
2855 | | |
2856 | | /* |
2857 | | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
2858 | | * supported, doesn't appear in supported signature algorithms, isn't supported |
2859 | | * by the enabled protocol versions or by the security level. |
2860 | | * |
2861 | | * This function should only be used for checking which ciphers are supported |
2862 | | * by the client. |
2863 | | * |
2864 | | * Call ssl_cipher_disabled() to check that it's enabled or not. |
2865 | | */ |
2866 | | int ssl_set_client_disabled(SSL_CONNECTION *s) |
2867 | 353k | { |
2868 | 353k | s->s3.tmp.mask_a = 0; |
2869 | 353k | s->s3.tmp.mask_k = 0; |
2870 | 353k | ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
2871 | 353k | if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver, |
2872 | 353k | &s->s3.tmp.max_ver, NULL) |
2873 | 353k | != 0) |
2874 | 0 | return 0; |
2875 | 353k | #ifndef OPENSSL_NO_PSK |
2876 | | /* with PSK there must be client callback set */ |
2877 | 353k | if (!s->psk_client_callback) { |
2878 | 353k | s->s3.tmp.mask_a |= SSL_aPSK; |
2879 | 353k | s->s3.tmp.mask_k |= SSL_PSK; |
2880 | 353k | } |
2881 | 353k | #endif /* OPENSSL_NO_PSK */ |
2882 | 353k | #ifndef OPENSSL_NO_SRP |
2883 | 353k | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
2884 | 353k | s->s3.tmp.mask_a |= SSL_aSRP; |
2885 | 353k | s->s3.tmp.mask_k |= SSL_kSRP; |
2886 | 353k | } |
2887 | 353k | #endif |
2888 | 353k | return 1; |
2889 | 353k | } |
2890 | | |
2891 | | /* |
2892 | | * ssl_cipher_disabled - check that a cipher is disabled or not |
2893 | | * @s: SSL connection that you want to use the cipher on |
2894 | | * @c: cipher to check |
2895 | | * @op: Security check that you want to do |
2896 | | * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 |
2897 | | * |
2898 | | * Returns 1 when it's disabled, 0 when enabled. |
2899 | | */ |
2900 | | int ssl_cipher_disabled(const SSL_CONNECTION *s, const SSL_CIPHER *c, |
2901 | | int op, int ecdhe) |
2902 | 31.8M | { |
2903 | 31.8M | int minversion = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls; |
2904 | 31.8M | int maxversion = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls; |
2905 | | |
2906 | 31.8M | if (c->algorithm_mkey & s->s3.tmp.mask_k |
2907 | 18.5M | || c->algorithm_auth & s->s3.tmp.mask_a) |
2908 | 13.2M | return 1; |
2909 | 18.5M | if (s->s3.tmp.max_ver == 0) |
2910 | 0 | return 1; |
2911 | | |
2912 | 18.5M | if (SSL_IS_QUIC_INT_HANDSHAKE(s)) |
2913 | | /* For QUIC, only allow these ciphersuites. */ |
2914 | 479k | switch (SSL_CIPHER_get_id(c)) { |
2915 | 152k | case TLS1_3_CK_AES_128_GCM_SHA256: |
2916 | 325k | case TLS1_3_CK_AES_256_GCM_SHA384: |
2917 | 479k | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: |
2918 | 479k | break; |
2919 | 34 | default: |
2920 | 34 | return 1; |
2921 | 479k | } |
2922 | | |
2923 | | /* |
2924 | | * For historical reasons we will allow ECHDE to be selected by a server |
2925 | | * in SSLv3 if we are a client |
2926 | | */ |
2927 | 18.5M | if (minversion == TLS1_VERSION |
2928 | 1.24M | && ecdhe |
2929 | 5.47k | && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) |
2930 | 5.46k | minversion = SSL3_VERSION; |
2931 | | |
2932 | 18.5M | if (ssl_version_cmp(s, minversion, s->s3.tmp.max_ver) > 0 |
2933 | 18.0M | || ssl_version_cmp(s, maxversion, s->s3.tmp.min_ver) < 0) |
2934 | 453k | return 1; |
2935 | | |
2936 | 18.0M | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
2937 | 18.5M | } |
2938 | | |
2939 | | int tls_use_ticket(SSL_CONNECTION *s) |
2940 | 165k | { |
2941 | 165k | if ((s->options & SSL_OP_NO_TICKET)) |
2942 | 0 | return 0; |
2943 | 165k | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); |
2944 | 165k | } |
2945 | | |
2946 | | int tls1_set_server_sigalgs(SSL_CONNECTION *s) |
2947 | 29.6k | { |
2948 | 29.6k | size_t i; |
2949 | | |
2950 | | /* Clear any shared signature algorithms */ |
2951 | 29.6k | OPENSSL_free(s->shared_sigalgs); |
2952 | 29.6k | s->shared_sigalgs = NULL; |
2953 | 29.6k | s->shared_sigalgslen = 0; |
2954 | | |
2955 | | /* Clear certificate validity flags */ |
2956 | 29.6k | if (s->s3.tmp.valid_flags) |
2957 | 113 | memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); |
2958 | 29.5k | else |
2959 | 29.5k | s->s3.tmp.valid_flags = OPENSSL_calloc(s->ssl_pkey_num, sizeof(uint32_t)); |
2960 | 29.6k | if (s->s3.tmp.valid_flags == NULL) { |
2961 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2962 | 0 | return 0; |
2963 | 0 | } |
2964 | | /* |
2965 | | * If peer sent no signature algorithms check to see if we support |
2966 | | * the default algorithm for each certificate type |
2967 | | */ |
2968 | 29.6k | if (s->s3.tmp.peer_cert_sigalgs == NULL |
2969 | 28.9k | && s->s3.tmp.peer_sigalgs == NULL) { |
2970 | 21.9k | const uint16_t *sent_sigs; |
2971 | 21.9k | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
2972 | | |
2973 | 258k | for (i = 0; i < s->ssl_pkey_num; i++) { |
2974 | 236k | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, (int)i); |
2975 | 236k | size_t j; |
2976 | | |
2977 | 236k | if (lu == NULL) |
2978 | 170k | continue; |
2979 | | /* Check default matches a type we sent */ |
2980 | 1.48M | for (j = 0; j < sent_sigslen; j++) { |
2981 | 1.47M | if (lu->sigalg == sent_sigs[j]) { |
2982 | 60.4k | s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN; |
2983 | 60.4k | break; |
2984 | 60.4k | } |
2985 | 1.47M | } |
2986 | 65.8k | } |
2987 | 21.9k | return 1; |
2988 | 21.9k | } |
2989 | | |
2990 | 7.68k | if (!tls1_process_sigalgs(s)) { |
2991 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2992 | 0 | return 0; |
2993 | 0 | } |
2994 | 7.68k | if (s->shared_sigalgs != NULL) |
2995 | 7.57k | return 1; |
2996 | | |
2997 | | /* Fatal error if no shared signature algorithms */ |
2998 | 7.68k | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2999 | 108 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
3000 | 108 | return 0; |
3001 | 7.68k | } |
3002 | | |
3003 | | /*- |
3004 | | * Gets the ticket information supplied by the client if any. |
3005 | | * |
3006 | | * hello: The parsed ClientHello data |
3007 | | * ret: (output) on return, if a ticket was decrypted, then this is set to |
3008 | | * point to the resulting session. |
3009 | | */ |
3010 | | SSL_TICKET_STATUS tls_get_ticket_from_client(SSL_CONNECTION *s, |
3011 | | CLIENTHELLO_MSG *hello, |
3012 | | SSL_SESSION **ret) |
3013 | 30.3k | { |
3014 | 30.3k | size_t size; |
3015 | 30.3k | RAW_EXTENSION *ticketext; |
3016 | | |
3017 | 30.3k | *ret = NULL; |
3018 | 30.3k | s->ext.ticket_expected = 0; |
3019 | | |
3020 | | /* |
3021 | | * If tickets disabled or not supported by the protocol version |
3022 | | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful |
3023 | | * resumption. |
3024 | | */ |
3025 | 30.3k | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
3026 | 0 | return SSL_TICKET_NONE; |
3027 | | |
3028 | 30.3k | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
3029 | 30.3k | if (!ticketext->present) |
3030 | 23.9k | return SSL_TICKET_NONE; |
3031 | | |
3032 | 6.47k | size = PACKET_remaining(&ticketext->data); |
3033 | | |
3034 | 6.47k | return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
3035 | 6.47k | hello->session_id, hello->session_id_len, ret); |
3036 | 30.3k | } |
3037 | | |
3038 | | /*- |
3039 | | * tls_decrypt_ticket attempts to decrypt a session ticket. |
3040 | | * |
3041 | | * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are |
3042 | | * expecting a pre-shared key ciphersuite, in which case we have no use for |
3043 | | * session tickets and one will never be decrypted, nor will |
3044 | | * s->ext.ticket_expected be set to 1. |
3045 | | * |
3046 | | * Side effects: |
3047 | | * Sets s->ext.ticket_expected to 1 if the server will have to issue |
3048 | | * a new session ticket to the client because the client indicated support |
3049 | | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
3050 | | * a session ticket or we couldn't use the one it gave us, or if |
3051 | | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
3052 | | * Otherwise, s->ext.ticket_expected is set to 0. |
3053 | | * |
3054 | | * etick: points to the body of the session ticket extension. |
3055 | | * eticklen: the length of the session tickets extension. |
3056 | | * sess_id: points at the session ID. |
3057 | | * sesslen: the length of the session ID. |
3058 | | * psess: (output) on return, if a ticket was decrypted, then this is set to |
3059 | | * point to the resulting session. |
3060 | | */ |
3061 | | SSL_TICKET_STATUS tls_decrypt_ticket(SSL_CONNECTION *s, |
3062 | | const unsigned char *etick, |
3063 | | size_t eticklen, |
3064 | | const unsigned char *sess_id, |
3065 | | size_t sesslen, SSL_SESSION **psess) |
3066 | 7.43k | { |
3067 | 7.43k | SSL_SESSION *sess = NULL; |
3068 | 7.43k | unsigned char *sdec; |
3069 | 7.43k | const unsigned char *p; |
3070 | 7.43k | int slen, ivlen, renew_ticket = 0, declen; |
3071 | 7.43k | SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER; |
3072 | 7.43k | size_t mlen; |
3073 | 7.43k | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
3074 | 7.43k | SSL_HMAC *hctx = NULL; |
3075 | 7.43k | EVP_CIPHER_CTX *ctx = NULL; |
3076 | 7.43k | SSL_CTX *tctx = s->session_ctx; |
3077 | 7.43k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3078 | | |
3079 | 7.43k | if (eticklen == 0) { |
3080 | | /* |
3081 | | * The client will accept a ticket but doesn't currently have |
3082 | | * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3 |
3083 | | */ |
3084 | 4.04k | ret = SSL_TICKET_EMPTY; |
3085 | 4.04k | goto end; |
3086 | 4.04k | } |
3087 | 3.38k | if (!SSL_CONNECTION_IS_TLS13(s) && s->ext.session_secret_cb) { |
3088 | | /* |
3089 | | * Indicate that the ticket couldn't be decrypted rather than |
3090 | | * generating the session from ticket now, trigger |
3091 | | * abbreviated handshake based on external mechanism to |
3092 | | * calculate the master secret later. |
3093 | | */ |
3094 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3095 | 0 | goto end; |
3096 | 0 | } |
3097 | | |
3098 | | /* Need at least keyname + iv */ |
3099 | 3.38k | if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { |
3100 | 1.01k | ret = SSL_TICKET_NO_DECRYPT; |
3101 | 1.01k | goto end; |
3102 | 1.01k | } |
3103 | | |
3104 | | /* Initialize session ticket encryption and HMAC contexts */ |
3105 | 2.36k | hctx = ssl_hmac_new(tctx); |
3106 | 2.36k | if (hctx == NULL) { |
3107 | 0 | ret = SSL_TICKET_FATAL_ERR_MALLOC; |
3108 | 0 | goto end; |
3109 | 0 | } |
3110 | 2.36k | ctx = EVP_CIPHER_CTX_new(); |
3111 | 2.36k | if (ctx == NULL) { |
3112 | 0 | ret = SSL_TICKET_FATAL_ERR_MALLOC; |
3113 | 0 | goto end; |
3114 | 0 | } |
3115 | 2.36k | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
3116 | 2.36k | if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) |
3117 | | #else |
3118 | | if (tctx->ext.ticket_key_evp_cb != NULL) |
3119 | | #endif |
3120 | 0 | { |
3121 | 0 | unsigned char *nctick = (unsigned char *)etick; |
3122 | 0 | int rv = 0; |
3123 | |
|
3124 | 0 | if (tctx->ext.ticket_key_evp_cb != NULL) |
3125 | 0 | rv = tctx->ext.ticket_key_evp_cb(SSL_CONNECTION_GET_USER_SSL(s), |
3126 | 0 | nctick, |
3127 | 0 | nctick + TLSEXT_KEYNAME_LENGTH, |
3128 | 0 | ctx, |
3129 | 0 | ssl_hmac_get0_EVP_MAC_CTX(hctx), |
3130 | 0 | 0); |
3131 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
3132 | 0 | else if (tctx->ext.ticket_key_cb != NULL) |
3133 | | /* if 0 is returned, write an empty ticket */ |
3134 | 0 | rv = tctx->ext.ticket_key_cb(SSL_CONNECTION_GET_USER_SSL(s), nctick, |
3135 | 0 | nctick + TLSEXT_KEYNAME_LENGTH, |
3136 | 0 | ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0); |
3137 | 0 | #endif |
3138 | 0 | if (rv < 0) { |
3139 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3140 | 0 | goto end; |
3141 | 0 | } |
3142 | 0 | if (rv == 0) { |
3143 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3144 | 0 | goto end; |
3145 | 0 | } |
3146 | 0 | if (rv == 2) |
3147 | 0 | renew_ticket = 1; |
3148 | 2.36k | } else { |
3149 | 2.36k | EVP_CIPHER *aes256cbc = NULL; |
3150 | | |
3151 | | /* Check key name matches */ |
3152 | 2.36k | if (memcmp(etick, tctx->ext.tick_key_name, |
3153 | 2.36k | TLSEXT_KEYNAME_LENGTH) |
3154 | 2.36k | != 0) { |
3155 | 980 | ret = SSL_TICKET_NO_DECRYPT; |
3156 | 980 | goto end; |
3157 | 980 | } |
3158 | | |
3159 | 1.38k | aes256cbc = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", |
3160 | 1.38k | sctx->propq); |
3161 | 1.38k | if (aes256cbc == NULL |
3162 | 1.38k | || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, |
3163 | 1.38k | sizeof(tctx->ext.secure->tick_hmac_key), |
3164 | 1.38k | "SHA256") |
3165 | 1.38k | <= 0 |
3166 | 1.38k | || EVP_DecryptInit_ex(ctx, aes256cbc, NULL, |
3167 | 1.38k | tctx->ext.secure->tick_aes_key, |
3168 | 1.38k | etick + TLSEXT_KEYNAME_LENGTH) |
3169 | 1.38k | <= 0) { |
3170 | 0 | EVP_CIPHER_free(aes256cbc); |
3171 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3172 | 0 | goto end; |
3173 | 0 | } |
3174 | 1.38k | EVP_CIPHER_free(aes256cbc); |
3175 | 1.38k | if (SSL_CONNECTION_IS_TLS13(s)) |
3176 | 626 | renew_ticket = 1; |
3177 | 1.38k | } |
3178 | | /* |
3179 | | * Attempt to process session ticket, first conduct sanity and integrity |
3180 | | * checks on ticket. |
3181 | | */ |
3182 | 1.38k | mlen = ssl_hmac_size(hctx); |
3183 | 1.38k | if (mlen == 0) { |
3184 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3185 | 0 | goto end; |
3186 | 0 | } |
3187 | | |
3188 | 1.38k | ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); |
3189 | 1.38k | if (ivlen < 0) { |
3190 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3191 | 0 | goto end; |
3192 | 0 | } |
3193 | | |
3194 | | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
3195 | 1.38k | if (eticklen <= TLSEXT_KEYNAME_LENGTH + ivlen + mlen) { |
3196 | 159 | ret = SSL_TICKET_NO_DECRYPT; |
3197 | 159 | goto end; |
3198 | 159 | } |
3199 | 1.23k | eticklen -= mlen; |
3200 | | /* Check HMAC of encrypted ticket */ |
3201 | 1.23k | if (ssl_hmac_update(hctx, etick, eticklen) <= 0 |
3202 | 1.23k | || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) { |
3203 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3204 | 0 | goto end; |
3205 | 0 | } |
3206 | | |
3207 | 1.23k | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
3208 | 193 | ret = SSL_TICKET_NO_DECRYPT; |
3209 | 193 | goto end; |
3210 | 193 | } |
3211 | | /* Attempt to decrypt session data */ |
3212 | | /* Move p after IV to start of encrypted ticket, update length */ |
3213 | 1.03k | p = etick + TLSEXT_KEYNAME_LENGTH + ivlen; |
3214 | 1.03k | eticklen -= TLSEXT_KEYNAME_LENGTH + ivlen; |
3215 | 1.03k | sdec = OPENSSL_malloc(eticklen); |
3216 | 1.03k | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, (int)eticklen) <= 0) { |
3217 | 0 | OPENSSL_free(sdec); |
3218 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3219 | 0 | goto end; |
3220 | 0 | } |
3221 | 1.03k | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
3222 | 66 | OPENSSL_free(sdec); |
3223 | 66 | ret = SSL_TICKET_NO_DECRYPT; |
3224 | 66 | goto end; |
3225 | 66 | } |
3226 | 971 | slen += declen; |
3227 | 971 | p = sdec; |
3228 | | |
3229 | 971 | sess = d2i_SSL_SESSION_ex(NULL, &p, slen, sctx->libctx, sctx->propq); |
3230 | 971 | slen -= (int)(p - sdec); |
3231 | 971 | OPENSSL_free(sdec); |
3232 | 971 | if (sess) { |
3233 | | /* Some additional consistency checks */ |
3234 | 816 | if (slen != 0) { |
3235 | 12 | SSL_SESSION_free(sess); |
3236 | 12 | sess = NULL; |
3237 | 12 | ret = SSL_TICKET_NO_DECRYPT; |
3238 | 12 | goto end; |
3239 | 12 | } |
3240 | | /* |
3241 | | * The session ID, if non-empty, is used by some clients to detect |
3242 | | * that the ticket has been accepted. So we copy it to the session |
3243 | | * structure. If it is empty set length to zero as required by |
3244 | | * standard. |
3245 | | */ |
3246 | 804 | if (sesslen) { |
3247 | 289 | memcpy(sess->session_id, sess_id, sesslen); |
3248 | 289 | sess->session_id_length = sesslen; |
3249 | 289 | } |
3250 | 804 | if (renew_ticket) |
3251 | 493 | ret = SSL_TICKET_SUCCESS_RENEW; |
3252 | 311 | else |
3253 | 311 | ret = SSL_TICKET_SUCCESS; |
3254 | 804 | goto end; |
3255 | 816 | } |
3256 | 155 | ERR_clear_error(); |
3257 | | /* |
3258 | | * For session parse failure, indicate that we need to send a new ticket. |
3259 | | */ |
3260 | 155 | ret = SSL_TICKET_NO_DECRYPT; |
3261 | | |
3262 | 7.43k | end: |
3263 | 7.43k | EVP_CIPHER_CTX_free(ctx); |
3264 | 7.43k | ssl_hmac_free(hctx); |
3265 | | |
3266 | | /* |
3267 | | * If set, the decrypt_ticket_cb() is called unless a fatal error was |
3268 | | * detected above. The callback is responsible for checking |ret| before it |
3269 | | * performs any action |
3270 | | */ |
3271 | 7.43k | if (s->session_ctx->decrypt_ticket_cb != NULL |
3272 | 0 | && (ret == SSL_TICKET_EMPTY |
3273 | 0 | || ret == SSL_TICKET_NO_DECRYPT |
3274 | 0 | || ret == SSL_TICKET_SUCCESS |
3275 | 0 | || ret == SSL_TICKET_SUCCESS_RENEW)) { |
3276 | 0 | size_t keyname_len = eticklen; |
3277 | 0 | int retcb; |
3278 | |
|
3279 | 0 | if (keyname_len > TLSEXT_KEYNAME_LENGTH) |
3280 | 0 | keyname_len = TLSEXT_KEYNAME_LENGTH; |
3281 | 0 | retcb = s->session_ctx->decrypt_ticket_cb(SSL_CONNECTION_GET_SSL(s), |
3282 | 0 | sess, etick, keyname_len, |
3283 | 0 | ret, |
3284 | 0 | s->session_ctx->ticket_cb_data); |
3285 | 0 | switch (retcb) { |
3286 | 0 | case SSL_TICKET_RETURN_ABORT: |
3287 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3288 | 0 | break; |
3289 | | |
3290 | 0 | case SSL_TICKET_RETURN_IGNORE: |
3291 | 0 | ret = SSL_TICKET_NONE; |
3292 | 0 | SSL_SESSION_free(sess); |
3293 | 0 | sess = NULL; |
3294 | 0 | break; |
3295 | | |
3296 | 0 | case SSL_TICKET_RETURN_IGNORE_RENEW: |
3297 | 0 | if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT) |
3298 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3299 | | /* else the value of |ret| will already do the right thing */ |
3300 | 0 | SSL_SESSION_free(sess); |
3301 | 0 | sess = NULL; |
3302 | 0 | break; |
3303 | | |
3304 | 0 | case SSL_TICKET_RETURN_USE: |
3305 | 0 | case SSL_TICKET_RETURN_USE_RENEW: |
3306 | 0 | if (ret != SSL_TICKET_SUCCESS |
3307 | 0 | && ret != SSL_TICKET_SUCCESS_RENEW) |
3308 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3309 | 0 | else if (retcb == SSL_TICKET_RETURN_USE) |
3310 | 0 | ret = SSL_TICKET_SUCCESS; |
3311 | 0 | else |
3312 | 0 | ret = SSL_TICKET_SUCCESS_RENEW; |
3313 | 0 | break; |
3314 | | |
3315 | 0 | default: |
3316 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3317 | 0 | } |
3318 | 0 | } |
3319 | | |
3320 | 7.43k | if (s->ext.session_secret_cb == NULL || SSL_CONNECTION_IS_TLS13(s)) { |
3321 | 7.43k | switch (ret) { |
3322 | 2.58k | case SSL_TICKET_NO_DECRYPT: |
3323 | 3.07k | case SSL_TICKET_SUCCESS_RENEW: |
3324 | 7.11k | case SSL_TICKET_EMPTY: |
3325 | 7.11k | s->ext.ticket_expected = 1; |
3326 | 7.43k | } |
3327 | 7.43k | } |
3328 | | |
3329 | 7.43k | *psess = sess; |
3330 | | |
3331 | 7.43k | return ret; |
3332 | 7.43k | } |
3333 | | |
3334 | | /* Check to see if a signature algorithm is allowed */ |
3335 | | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, |
3336 | | const SIGALG_LOOKUP *lu) |
3337 | 4.64M | { |
3338 | 4.64M | unsigned char sigalgstr[2]; |
3339 | 4.64M | int secbits; |
3340 | | |
3341 | 4.64M | if (lu == NULL || !lu->available) |
3342 | 0 | return 0; |
3343 | | /* DSA is not allowed in TLS 1.3 */ |
3344 | 4.64M | if (SSL_CONNECTION_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) |
3345 | 9.95k | return 0; |
3346 | | /* |
3347 | | * At some point we should fully axe DSA/etc. in ClientHello as per TLS 1.3 |
3348 | | * spec |
3349 | | */ |
3350 | 4.63M | if (!s->server && !SSL_CONNECTION_IS_DTLS(s) |
3351 | 3.50M | && s->s3.tmp.min_ver >= TLS1_3_VERSION |
3352 | 1.85M | && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX |
3353 | 1.19M | || lu->hash_idx == SSL_MD_MD5_IDX |
3354 | 1.19M | || lu->hash_idx == SSL_MD_SHA224_IDX)) |
3355 | 693k | return 0; |
3356 | | |
3357 | | /* See if public key algorithm allowed */ |
3358 | 3.93M | if (ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), lu->sig_idx)) |
3359 | 0 | return 0; |
3360 | | |
3361 | 3.93M | if (lu->sig == NID_id_GostR3410_2012_256 |
3362 | 3.93M | || lu->sig == NID_id_GostR3410_2012_512 |
3363 | 3.93M | || lu->sig == NID_id_GostR3410_2001) { |
3364 | | /* We never allow GOST sig algs on the server with TLSv1.3 */ |
3365 | 0 | if (s->server && SSL_CONNECTION_IS_TLS13(s)) |
3366 | 0 | return 0; |
3367 | 0 | if (!s->server |
3368 | 0 | && SSL_CONNECTION_GET_SSL(s)->method->version == TLS_ANY_VERSION |
3369 | 0 | && s->s3.tmp.max_ver >= TLS1_3_VERSION) { |
3370 | 0 | int i, num; |
3371 | 0 | STACK_OF(SSL_CIPHER) *sk; |
3372 | | |
3373 | | /* |
3374 | | * We're a client that could negotiate TLSv1.3. We only allow GOST |
3375 | | * sig algs if we could negotiate TLSv1.2 or below and we have GOST |
3376 | | * ciphersuites enabled. |
3377 | | */ |
3378 | |
|
3379 | 0 | if (s->s3.tmp.min_ver >= TLS1_3_VERSION) |
3380 | 0 | return 0; |
3381 | | |
3382 | 0 | sk = SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)); |
3383 | 0 | num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0; |
3384 | 0 | for (i = 0; i < num; i++) { |
3385 | 0 | const SSL_CIPHER *c; |
3386 | |
|
3387 | 0 | c = sk_SSL_CIPHER_value(sk, i); |
3388 | | /* Skip disabled ciphers */ |
3389 | 0 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) |
3390 | 0 | continue; |
3391 | | |
3392 | 0 | if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0) |
3393 | 0 | break; |
3394 | 0 | } |
3395 | 0 | if (i == num) |
3396 | 0 | return 0; |
3397 | 0 | } |
3398 | 0 | } |
3399 | | |
3400 | | /* Finally see if security callback allows it */ |
3401 | 3.93M | secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
3402 | 3.93M | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
3403 | 3.93M | sigalgstr[1] = lu->sigalg & 0xff; |
3404 | 3.93M | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
3405 | 3.93M | } |
3406 | | |
3407 | | /* |
3408 | | * Get a mask of disabled public key algorithms based on supported signature |
3409 | | * algorithms. For example if no signature algorithm supports RSA then RSA is |
3410 | | * disabled. |
3411 | | */ |
3412 | | |
3413 | | void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op) |
3414 | 353k | { |
3415 | 353k | const uint16_t *sigalgs; |
3416 | 353k | size_t i, sigalgslen; |
3417 | 353k | uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA; |
3418 | | /* |
3419 | | * Go through all signature algorithms seeing if we support any |
3420 | | * in disabled_mask. |
3421 | | */ |
3422 | 353k | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
3423 | 10.7M | for (i = 0; i < sigalgslen; i++, sigalgs++) { |
3424 | 10.3M | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *sigalgs); |
3425 | 10.3M | const SSL_CERT_LOOKUP *clu; |
3426 | | |
3427 | 10.3M | if (lu == NULL) |
3428 | 790k | continue; |
3429 | | |
3430 | 9.58M | clu = ssl_cert_lookup_by_idx(lu->sig_idx, |
3431 | 9.58M | SSL_CONNECTION_GET_CTX(s)); |
3432 | 9.58M | if (clu == NULL) |
3433 | 0 | continue; |
3434 | | |
3435 | | /* If algorithm is disabled see if we can enable it */ |
3436 | 9.58M | if ((clu->amask & disabled_mask) != 0 |
3437 | 1.46M | && tls12_sigalg_allowed(s, op, lu)) |
3438 | 959k | disabled_mask &= ~clu->amask; |
3439 | 9.58M | } |
3440 | 353k | *pmask_a |= disabled_mask; |
3441 | 353k | } |
3442 | | |
3443 | | int tls12_copy_sigalgs(SSL_CONNECTION *s, WPACKET *pkt, |
3444 | | const uint16_t *psig, size_t psiglen) |
3445 | 116k | { |
3446 | 116k | size_t i; |
3447 | 116k | int rv = 0; |
3448 | | |
3449 | 3.54M | for (i = 0; i < psiglen; i++, psig++) { |
3450 | 3.42M | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *psig); |
3451 | | |
3452 | 3.42M | if (lu == NULL || !tls_sigalg_compat(s, lu)) |
3453 | 762k | continue; |
3454 | 2.66M | if (!WPACKET_put_bytes_u16(pkt, *psig)) |
3455 | 0 | return 0; |
3456 | | /* |
3457 | | * If TLS 1.3 must have at least one valid TLS 1.3 message |
3458 | | * signing algorithm: i.e. neither RSA nor SHA1/SHA224 |
3459 | | */ |
3460 | 2.66M | if (rv == 0 && (!SSL_CONNECTION_IS_TLS13(s) || (lu->sig != EVP_PKEY_RSA && lu->hash != NID_sha1 && lu->hash != NID_sha224))) |
3461 | 116k | rv = 1; |
3462 | 2.66M | } |
3463 | 116k | if (rv == 0) |
3464 | 116k | ERR_raise(ERR_LIB_SSL, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
3465 | 116k | return rv; |
3466 | 116k | } |
3467 | | |
3468 | | /* Given preference and allowed sigalgs set shared sigalgs */ |
3469 | | static size_t tls12_shared_sigalgs(SSL_CONNECTION *s, |
3470 | | const SIGALG_LOOKUP **shsig, |
3471 | | const uint16_t *pref, size_t preflen, |
3472 | | const uint16_t *allow, size_t allowlen) |
3473 | 18.5k | { |
3474 | 18.5k | const uint16_t *ptmp, *atmp; |
3475 | 18.5k | size_t i, j, nmatch = 0; |
3476 | 457k | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
3477 | 439k | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *ptmp); |
3478 | | |
3479 | | /* Skip disabled hashes or signature algorithms */ |
3480 | 439k | if (lu == NULL |
3481 | 193k | || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
3482 | 255k | continue; |
3483 | 2.87M | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
3484 | 2.87M | if (*ptmp == *atmp) { |
3485 | 184k | nmatch++; |
3486 | 184k | if (shsig) |
3487 | 92.0k | *shsig++ = lu; |
3488 | 184k | break; |
3489 | 184k | } |
3490 | 2.87M | } |
3491 | 184k | } |
3492 | 18.5k | return nmatch; |
3493 | 18.5k | } |
3494 | | |
3495 | | /* Set shared signature algorithms for SSL structures */ |
3496 | | static int tls1_set_shared_sigalgs(SSL_CONNECTION *s) |
3497 | 9.36k | { |
3498 | 9.36k | const uint16_t *pref, *allow, *conf; |
3499 | 9.36k | size_t preflen, allowlen, conflen; |
3500 | 9.36k | size_t nmatch; |
3501 | 9.36k | const SIGALG_LOOKUP **salgs = NULL; |
3502 | 9.36k | CERT *c = s->cert; |
3503 | 9.36k | unsigned int is_suiteb = tls1_suiteb(s); |
3504 | | |
3505 | 9.36k | OPENSSL_free(s->shared_sigalgs); |
3506 | 9.36k | s->shared_sigalgs = NULL; |
3507 | 9.36k | s->shared_sigalgslen = 0; |
3508 | | /* If client use client signature algorithms if not NULL */ |
3509 | 9.36k | if (!s->server && c->client_sigalgs && !is_suiteb) { |
3510 | 0 | conf = c->client_sigalgs; |
3511 | 0 | conflen = c->client_sigalgslen; |
3512 | 9.36k | } else if (c->conf_sigalgs && !is_suiteb) { |
3513 | 0 | conf = c->conf_sigalgs; |
3514 | 0 | conflen = c->conf_sigalgslen; |
3515 | 0 | } else |
3516 | 9.36k | conflen = tls12_get_psigalgs(s, 0, &conf); |
3517 | 9.36k | if (s->options & SSL_OP_SERVER_PREFERENCE || is_suiteb) { |
3518 | 0 | pref = conf; |
3519 | 0 | preflen = conflen; |
3520 | 0 | allow = s->s3.tmp.peer_sigalgs; |
3521 | 0 | allowlen = s->s3.tmp.peer_sigalgslen; |
3522 | 9.36k | } else { |
3523 | 9.36k | allow = conf; |
3524 | 9.36k | allowlen = conflen; |
3525 | 9.36k | pref = s->s3.tmp.peer_sigalgs; |
3526 | 9.36k | preflen = s->s3.tmp.peer_sigalgslen; |
3527 | 9.36k | } |
3528 | 9.36k | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); |
3529 | 9.36k | if (nmatch) { |
3530 | 9.15k | if ((salgs = OPENSSL_malloc_array(nmatch, sizeof(*salgs))) == NULL) |
3531 | 0 | return 0; |
3532 | 9.15k | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
3533 | 9.15k | } else { |
3534 | 219 | salgs = NULL; |
3535 | 219 | } |
3536 | 9.36k | s->shared_sigalgs = salgs; |
3537 | 9.36k | s->shared_sigalgslen = nmatch; |
3538 | 9.36k | return 1; |
3539 | 9.36k | } |
3540 | | |
3541 | | int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen) |
3542 | 30.7k | { |
3543 | 30.7k | unsigned int stmp; |
3544 | 30.7k | size_t size, i; |
3545 | 30.7k | uint16_t *buf; |
3546 | | |
3547 | 30.7k | size = PACKET_remaining(pkt); |
3548 | | |
3549 | | /* Invalid data length */ |
3550 | 30.7k | if (size == 0 || (size & 1) != 0) |
3551 | 68 | return 0; |
3552 | | |
3553 | 30.6k | size >>= 1; |
3554 | | |
3555 | 30.6k | if ((buf = OPENSSL_malloc_array(size, sizeof(*buf))) == NULL) |
3556 | 0 | return 0; |
3557 | 334k | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
3558 | 303k | buf[i] = stmp; |
3559 | | |
3560 | 30.6k | if (i != size) { |
3561 | 0 | OPENSSL_free(buf); |
3562 | 0 | return 0; |
3563 | 0 | } |
3564 | | |
3565 | 30.6k | OPENSSL_free(*pdest); |
3566 | 30.6k | *pdest = buf; |
3567 | 30.6k | *pdestlen = size; |
3568 | | |
3569 | 30.6k | return 1; |
3570 | 30.6k | } |
3571 | | |
3572 | | int tls1_save_sigalgs(SSL_CONNECTION *s, PACKET *pkt, int cert) |
3573 | 11.3k | { |
3574 | | /* Extension ignored for inappropriate versions */ |
3575 | 11.3k | if (!SSL_USE_SIGALGS(s)) |
3576 | 273 | return 1; |
3577 | | /* Should never happen */ |
3578 | 11.0k | if (s->cert == NULL) |
3579 | 0 | return 0; |
3580 | | |
3581 | 11.0k | if (cert) |
3582 | 950 | return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs, |
3583 | 950 | &s->s3.tmp.peer_cert_sigalgslen); |
3584 | 10.1k | else |
3585 | 10.1k | return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs, |
3586 | 10.1k | &s->s3.tmp.peer_sigalgslen); |
3587 | 11.0k | } |
3588 | | |
3589 | | /* Set preferred digest for each key type */ |
3590 | | |
3591 | | int tls1_process_sigalgs(SSL_CONNECTION *s) |
3592 | 9.36k | { |
3593 | 9.36k | size_t i; |
3594 | 9.36k | uint32_t *pvalid = s->s3.tmp.valid_flags; |
3595 | | |
3596 | 9.36k | if (!tls1_set_shared_sigalgs(s)) |
3597 | 0 | return 0; |
3598 | | |
3599 | 108k | for (i = 0; i < s->ssl_pkey_num; i++) |
3600 | 99.0k | pvalid[i] = 0; |
3601 | | |
3602 | 101k | for (i = 0; i < s->shared_sigalgslen; i++) { |
3603 | 92.0k | const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i]; |
3604 | 92.0k | int idx = sigptr->sig_idx; |
3605 | | |
3606 | | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
3607 | 92.0k | if (SSL_CONNECTION_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
3608 | 2.76k | continue; |
3609 | | /* If not disabled indicate we can explicitly sign */ |
3610 | 89.2k | if (pvalid[idx] == 0 |
3611 | 17.2k | && !ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), idx)) |
3612 | 17.2k | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
3613 | 89.2k | } |
3614 | 9.36k | return 1; |
3615 | 9.36k | } |
3616 | | |
3617 | | int SSL_get_sigalgs(SSL *s, int idx, |
3618 | | int *psign, int *phash, int *psignhash, |
3619 | | unsigned char *rsig, unsigned char *rhash) |
3620 | 0 | { |
3621 | 0 | uint16_t *psig; |
3622 | 0 | size_t numsigalgs; |
3623 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3624 | |
|
3625 | 0 | if (sc == NULL) |
3626 | 0 | return 0; |
3627 | | |
3628 | 0 | psig = sc->s3.tmp.peer_sigalgs; |
3629 | 0 | numsigalgs = sc->s3.tmp.peer_sigalgslen; |
3630 | |
|
3631 | 0 | if (psig == NULL || numsigalgs > INT_MAX) |
3632 | 0 | return 0; |
3633 | 0 | if (idx >= 0) { |
3634 | 0 | const SIGALG_LOOKUP *lu; |
3635 | |
|
3636 | 0 | if (idx >= (int)numsigalgs) |
3637 | 0 | return 0; |
3638 | 0 | psig += idx; |
3639 | 0 | if (rhash != NULL) |
3640 | 0 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
3641 | 0 | if (rsig != NULL) |
3642 | 0 | *rsig = (unsigned char)(*psig & 0xff); |
3643 | 0 | lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(sc), *psig); |
3644 | 0 | if (psign != NULL) |
3645 | 0 | *psign = lu != NULL ? lu->sig : NID_undef; |
3646 | 0 | if (phash != NULL) |
3647 | 0 | *phash = lu != NULL ? lu->hash : NID_undef; |
3648 | 0 | if (psignhash != NULL) |
3649 | 0 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; |
3650 | 0 | } |
3651 | 0 | return (int)numsigalgs; |
3652 | 0 | } |
3653 | | |
3654 | | int SSL_get_shared_sigalgs(SSL *s, int idx, |
3655 | | int *psign, int *phash, int *psignhash, |
3656 | | unsigned char *rsig, unsigned char *rhash) |
3657 | 0 | { |
3658 | 0 | const SIGALG_LOOKUP *shsigalgs; |
3659 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3660 | |
|
3661 | 0 | if (sc == NULL) |
3662 | 0 | return 0; |
3663 | | |
3664 | 0 | if (sc->shared_sigalgs == NULL |
3665 | 0 | || idx < 0 |
3666 | 0 | || idx >= (int)sc->shared_sigalgslen |
3667 | 0 | || sc->shared_sigalgslen > INT_MAX) |
3668 | 0 | return 0; |
3669 | 0 | shsigalgs = sc->shared_sigalgs[idx]; |
3670 | 0 | if (phash != NULL) |
3671 | 0 | *phash = shsigalgs->hash; |
3672 | 0 | if (psign != NULL) |
3673 | 0 | *psign = shsigalgs->sig; |
3674 | 0 | if (psignhash != NULL) |
3675 | 0 | *psignhash = shsigalgs->sigandhash; |
3676 | 0 | if (rsig != NULL) |
3677 | 0 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); |
3678 | 0 | if (rhash != NULL) |
3679 | 0 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); |
3680 | 0 | return (int)sc->shared_sigalgslen; |
3681 | 0 | } |
3682 | | |
3683 | | /* Maximum possible number of unique entries in sigalgs array */ |
3684 | 0 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) |
3685 | | |
3686 | | typedef struct { |
3687 | | size_t sigalgcnt; |
3688 | | /* TLSEXT_SIGALG_XXX values */ |
3689 | | uint16_t sigalgs[TLS_MAX_SIGALGCNT]; |
3690 | | SSL_CTX *ctx; |
3691 | | } sig_cb_st; |
3692 | | |
3693 | | static void get_sigorhash(int *psig, int *phash, const char *str) |
3694 | 0 | { |
3695 | 0 | if (OPENSSL_strcasecmp(str, "RSA") == 0) { |
3696 | 0 | *psig = EVP_PKEY_RSA; |
3697 | 0 | } else if (OPENSSL_strcasecmp(str, "RSA-PSS") == 0 |
3698 | 0 | || OPENSSL_strcasecmp(str, "PSS") == 0) { |
3699 | 0 | *psig = EVP_PKEY_RSA_PSS; |
3700 | 0 | } else if (OPENSSL_strcasecmp(str, "DSA") == 0) { |
3701 | 0 | *psig = EVP_PKEY_DSA; |
3702 | 0 | } else if (OPENSSL_strcasecmp(str, "ECDSA") == 0) { |
3703 | 0 | *psig = EVP_PKEY_EC; |
3704 | 0 | } else { |
3705 | 0 | *phash = OBJ_sn2nid(str); |
3706 | 0 | if (*phash == NID_undef) |
3707 | 0 | *phash = OBJ_ln2nid(str); |
3708 | 0 | } |
3709 | 0 | } |
3710 | | /* Maximum length of a signature algorithm string component */ |
3711 | | #define TLS_MAX_SIGSTRING_LEN 40 |
3712 | | |
3713 | | static int sig_cb(const char *elem, int len, void *arg) |
3714 | 0 | { |
3715 | 0 | sig_cb_st *sarg = arg; |
3716 | 0 | size_t i = 0; |
3717 | 0 | const SIGALG_LOOKUP *s; |
3718 | 0 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
3719 | 0 | const char *iana, *alias; |
3720 | 0 | int sig_alg = NID_undef, hash_alg = NID_undef; |
3721 | 0 | int ignore_unknown = 0; |
3722 | |
|
3723 | 0 | if (elem == NULL) |
3724 | 0 | return 0; |
3725 | 0 | if (elem[0] == '?') { |
3726 | 0 | ignore_unknown = 1; |
3727 | 0 | ++elem; |
3728 | 0 | --len; |
3729 | 0 | } |
3730 | 0 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
3731 | 0 | return 0; |
3732 | 0 | if (len > (int)(sizeof(etmp) - 1)) |
3733 | 0 | return 0; |
3734 | 0 | memcpy(etmp, elem, len); |
3735 | 0 | etmp[len] = 0; |
3736 | 0 | p = strchr(etmp, '+'); |
3737 | | /* |
3738 | | * We only allow SignatureSchemes listed in the sigalg_lookup_tbl; |
3739 | | * if there's no '+' in the provided name, look for the new-style combined |
3740 | | * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP. |
3741 | | * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and |
3742 | | * rsa_pss_rsae_* that differ only by public key OID; in such cases |
3743 | | * we will pick the _rsae_ variant, by virtue of them appearing earlier |
3744 | | * in the table. |
3745 | | */ |
3746 | 0 | if (p == NULL) { |
3747 | 0 | if (sarg->ctx != NULL) { |
3748 | 0 | for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { |
3749 | 0 | iana = sarg->ctx->sigalg_lookup_cache[i].name; |
3750 | 0 | alias = sarg->ctx->sigalg_lookup_cache[i].name12; |
3751 | 0 | if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0) |
3752 | 0 | || OPENSSL_strcasecmp(etmp, iana) == 0) { |
3753 | | /* Ignore known, but unavailable sigalgs. */ |
3754 | 0 | if (!sarg->ctx->sigalg_lookup_cache[i].available) |
3755 | 0 | return 1; |
3756 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = sarg->ctx->sigalg_lookup_cache[i].sigalg; |
3757 | 0 | goto found; |
3758 | 0 | } |
3759 | 0 | } |
3760 | 0 | } else { |
3761 | | /* Syntax checks use the built-in sigalgs */ |
3762 | 0 | for (i = 0, s = sigalg_lookup_tbl; |
3763 | 0 | i < OSSL_NELEM(sigalg_lookup_tbl); i++, s++) { |
3764 | 0 | iana = s->name; |
3765 | 0 | alias = s->name12; |
3766 | 0 | if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0) |
3767 | 0 | || OPENSSL_strcasecmp(etmp, iana) == 0) { |
3768 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
3769 | 0 | goto found; |
3770 | 0 | } |
3771 | 0 | } |
3772 | 0 | } |
3773 | 0 | } else { |
3774 | 0 | *p = 0; |
3775 | 0 | p++; |
3776 | 0 | if (*p == 0) |
3777 | 0 | return 0; |
3778 | 0 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
3779 | 0 | get_sigorhash(&sig_alg, &hash_alg, p); |
3780 | 0 | if (sig_alg != NID_undef && hash_alg != NID_undef) { |
3781 | 0 | if (sarg->ctx != NULL) { |
3782 | 0 | for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { |
3783 | 0 | s = &sarg->ctx->sigalg_lookup_cache[i]; |
3784 | 0 | if (s->hash == hash_alg && s->sig == sig_alg) { |
3785 | | /* Ignore known, but unavailable sigalgs. */ |
3786 | 0 | if (!sarg->ctx->sigalg_lookup_cache[i].available) |
3787 | 0 | return 1; |
3788 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
3789 | 0 | goto found; |
3790 | 0 | } |
3791 | 0 | } |
3792 | 0 | } else { |
3793 | 0 | for (i = 0; i < OSSL_NELEM(sigalg_lookup_tbl); i++) { |
3794 | 0 | s = &sigalg_lookup_tbl[i]; |
3795 | 0 | if (s->hash == hash_alg && s->sig == sig_alg) { |
3796 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
3797 | 0 | goto found; |
3798 | 0 | } |
3799 | 0 | } |
3800 | 0 | } |
3801 | 0 | } |
3802 | 0 | } |
3803 | | /* Ignore unknown algorithms if ignore_unknown */ |
3804 | 0 | return ignore_unknown; |
3805 | | |
3806 | 0 | found: |
3807 | | /* Ignore duplicates */ |
3808 | 0 | for (i = 0; i < sarg->sigalgcnt - 1; i++) { |
3809 | 0 | if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { |
3810 | 0 | sarg->sigalgcnt--; |
3811 | 0 | return 1; |
3812 | 0 | } |
3813 | 0 | } |
3814 | 0 | return 1; |
3815 | 0 | } |
3816 | | |
3817 | | /* |
3818 | | * Set supported signature algorithms based on a colon separated list of the |
3819 | | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
3820 | | */ |
3821 | | int tls1_set_sigalgs_list(SSL_CTX *ctx, CERT *c, const char *str, int client) |
3822 | 0 | { |
3823 | 0 | sig_cb_st sig; |
3824 | 0 | sig.sigalgcnt = 0; |
3825 | |
|
3826 | 0 | if (ctx != NULL) |
3827 | 0 | sig.ctx = ctx; |
3828 | 0 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) |
3829 | 0 | return 0; |
3830 | 0 | if (sig.sigalgcnt == 0) { |
3831 | 0 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
3832 | 0 | "No valid signature algorithms in '%s'", str); |
3833 | 0 | return 0; |
3834 | 0 | } |
3835 | 0 | if (c == NULL) |
3836 | 0 | return 1; |
3837 | 0 | return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
3838 | 0 | } |
3839 | | |
3840 | | int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen, |
3841 | | int client) |
3842 | 0 | { |
3843 | 0 | uint16_t *sigalgs; |
3844 | |
|
3845 | 0 | if ((sigalgs = OPENSSL_malloc_array(salglen, sizeof(*sigalgs))) == NULL) |
3846 | 0 | return 0; |
3847 | 0 | memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs)); |
3848 | |
|
3849 | 0 | if (client) { |
3850 | 0 | OPENSSL_free(c->client_sigalgs); |
3851 | 0 | c->client_sigalgs = sigalgs; |
3852 | 0 | c->client_sigalgslen = salglen; |
3853 | 0 | } else { |
3854 | 0 | OPENSSL_free(c->conf_sigalgs); |
3855 | 0 | c->conf_sigalgs = sigalgs; |
3856 | 0 | c->conf_sigalgslen = salglen; |
3857 | 0 | } |
3858 | |
|
3859 | 0 | return 1; |
3860 | 0 | } |
3861 | | |
3862 | | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
3863 | 0 | { |
3864 | 0 | uint16_t *sigalgs, *sptr; |
3865 | 0 | size_t i; |
3866 | |
|
3867 | 0 | if (salglen & 1) |
3868 | 0 | return 0; |
3869 | 0 | if ((sigalgs = OPENSSL_malloc_array(salglen / 2, sizeof(*sigalgs))) == NULL) |
3870 | 0 | return 0; |
3871 | 0 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
3872 | 0 | size_t j; |
3873 | 0 | const SIGALG_LOOKUP *curr; |
3874 | 0 | int md_id = *psig_nids++; |
3875 | 0 | int sig_id = *psig_nids++; |
3876 | |
|
3877 | 0 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); |
3878 | 0 | j++, curr++) { |
3879 | 0 | if (curr->hash == md_id && curr->sig == sig_id) { |
3880 | 0 | *sptr++ = curr->sigalg; |
3881 | 0 | break; |
3882 | 0 | } |
3883 | 0 | } |
3884 | |
|
3885 | 0 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
3886 | 0 | goto err; |
3887 | 0 | } |
3888 | | |
3889 | 0 | if (client) { |
3890 | 0 | OPENSSL_free(c->client_sigalgs); |
3891 | 0 | c->client_sigalgs = sigalgs; |
3892 | 0 | c->client_sigalgslen = salglen / 2; |
3893 | 0 | } else { |
3894 | 0 | OPENSSL_free(c->conf_sigalgs); |
3895 | 0 | c->conf_sigalgs = sigalgs; |
3896 | 0 | c->conf_sigalgslen = salglen / 2; |
3897 | 0 | } |
3898 | |
|
3899 | 0 | return 1; |
3900 | | |
3901 | 0 | err: |
3902 | 0 | OPENSSL_free(sigalgs); |
3903 | 0 | return 0; |
3904 | 0 | } |
3905 | | |
3906 | | static int tls1_check_sig_alg(SSL_CONNECTION *s, X509 *x, int default_nid) |
3907 | 0 | { |
3908 | 0 | int sig_nid, use_pc_sigalgs = 0; |
3909 | 0 | size_t i; |
3910 | 0 | const SIGALG_LOOKUP *sigalg; |
3911 | 0 | size_t sigalgslen; |
3912 | | |
3913 | | /*- |
3914 | | * RFC 8446, section 4.2.3: |
3915 | | * |
3916 | | * The signatures on certificates that are self-signed or certificates |
3917 | | * that are trust anchors are not validated, since they begin a |
3918 | | * certification path (see [RFC5280], Section 3.2). A certificate that |
3919 | | * begins a certification path MAY use a signature algorithm that is not |
3920 | | * advertised as being supported in the "signature_algorithms" |
3921 | | * extension. |
3922 | | */ |
3923 | 0 | if (default_nid == -1 || X509_self_signed(x, 0)) |
3924 | 0 | return 1; |
3925 | 0 | sig_nid = X509_get_signature_nid(x); |
3926 | 0 | if (default_nid) |
3927 | 0 | return sig_nid == default_nid ? 1 : 0; |
3928 | | |
3929 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) { |
3930 | | /* |
3931 | | * If we're in TLSv1.3 then we only get here if we're checking the |
3932 | | * chain. If the peer has specified peer_cert_sigalgs then we use them |
3933 | | * otherwise we default to normal sigalgs. |
3934 | | */ |
3935 | 0 | sigalgslen = s->s3.tmp.peer_cert_sigalgslen; |
3936 | 0 | use_pc_sigalgs = 1; |
3937 | 0 | } else { |
3938 | 0 | sigalgslen = s->shared_sigalgslen; |
3939 | 0 | } |
3940 | 0 | for (i = 0; i < sigalgslen; i++) { |
3941 | 0 | int mdnid, pknid; |
3942 | |
|
3943 | 0 | sigalg = use_pc_sigalgs |
3944 | 0 | ? tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
3945 | 0 | s->s3.tmp.peer_cert_sigalgs[i]) |
3946 | 0 | : s->shared_sigalgs[i]; |
3947 | 0 | if (sigalg == NULL) |
3948 | 0 | continue; |
3949 | 0 | if (sig_nid == sigalg->sigandhash) |
3950 | 0 | return 1; |
3951 | 0 | if (sigalg->sig != EVP_PKEY_RSA_PSS) |
3952 | 0 | continue; |
3953 | | /* |
3954 | | * Accept RSA PKCS#1 signatures in certificates when the signature |
3955 | | * algorithms include RSA-PSS with a matching digest algorithm. |
3956 | | * |
3957 | | * When a TLS 1.3 peer inadvertently omits the legacy RSA PKCS#1 code |
3958 | | * points, and we're doing strict checking of the certificate chain (in |
3959 | | * a cert_cb via SSL_check_chain()) we may then reject RSA signed |
3960 | | * certificates in the chain, but the TLS requirement on PSS should not |
3961 | | * extend to certificates. Though the peer can in fact list the legacy |
3962 | | * sigalgs for just this purpose, it is not likely that a better chain |
3963 | | * signed with RSA-PSS is available. |
3964 | | */ |
3965 | 0 | if (!OBJ_find_sigid_algs(sig_nid, &mdnid, &pknid)) |
3966 | 0 | continue; |
3967 | 0 | if (pknid == EVP_PKEY_RSA && mdnid == sigalg->hash) |
3968 | 0 | return 1; |
3969 | 0 | } |
3970 | 0 | return 0; |
3971 | 0 | } |
3972 | | |
3973 | | /* Check to see if a certificate issuer name matches list of CA names */ |
3974 | | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) |
3975 | 0 | { |
3976 | 0 | const X509_NAME *nm; |
3977 | 0 | int i; |
3978 | 0 | nm = X509_get_issuer_name(x); |
3979 | 0 | for (i = 0; i < sk_X509_NAME_num(names); i++) { |
3980 | 0 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
3981 | 0 | return 1; |
3982 | 0 | } |
3983 | 0 | return 0; |
3984 | 0 | } |
3985 | | |
3986 | | /* |
3987 | | * Check certificate chain is consistent with TLS extensions and is usable by |
3988 | | * server. This servers two purposes: it allows users to check chains before |
3989 | | * passing them to the server and it allows the server to check chains before |
3990 | | * attempting to use them. |
3991 | | */ |
3992 | | |
3993 | | /* Flags which need to be set for a certificate when strict mode not set */ |
3994 | | |
3995 | | #define CERT_PKEY_VALID_FLAGS \ |
3996 | 0 | (CERT_PKEY_EE_SIGNATURE | CERT_PKEY_EE_PARAM) |
3997 | | /* Strict mode flags */ |
3998 | | #define CERT_PKEY_STRICT_FLAGS \ |
3999 | 0 | (CERT_PKEY_VALID_FLAGS | CERT_PKEY_CA_SIGNATURE | CERT_PKEY_CA_PARAM \ |
4000 | 0 | | CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE) |
4001 | | |
4002 | | int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk, |
4003 | | STACK_OF(X509) *chain, int idx) |
4004 | 236k | { |
4005 | 236k | int i; |
4006 | 236k | int rv = 0; |
4007 | 236k | int check_flags = 0, strict_mode; |
4008 | 236k | CERT_PKEY *cpk = NULL; |
4009 | 236k | CERT *c = s->cert; |
4010 | 236k | uint32_t *pvalid; |
4011 | 236k | unsigned int suiteb_flags = tls1_suiteb(s); |
4012 | | |
4013 | | /* |
4014 | | * Meaning of idx: |
4015 | | * idx == -1 means SSL_check_chain() invocation |
4016 | | * idx == -2 means checking client certificate chains |
4017 | | * idx >= 0 means checking SSL_PKEY index |
4018 | | * |
4019 | | * For RPK, where there may be no cert, we ignore -1 |
4020 | | */ |
4021 | 236k | if (idx != -1) { |
4022 | 236k | if (idx == -2) { |
4023 | 0 | cpk = c->key; |
4024 | 0 | idx = (int)(cpk - c->pkeys); |
4025 | 0 | } else |
4026 | 236k | cpk = c->pkeys + idx; |
4027 | 236k | pvalid = s->s3.tmp.valid_flags + idx; |
4028 | 236k | x = cpk->x509; |
4029 | 236k | pk = cpk->privatekey; |
4030 | 236k | chain = cpk->chain; |
4031 | 236k | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
4032 | 236k | if (tls12_rpk_and_privkey(s, idx)) { |
4033 | 0 | if (EVP_PKEY_is_a(pk, "EC") && !tls1_check_pkey_comp(s, pk)) |
4034 | 0 | return 0; |
4035 | 0 | *pvalid = rv = CERT_PKEY_RPK; |
4036 | 0 | return rv; |
4037 | 0 | } |
4038 | | /* If no cert or key, forget it */ |
4039 | 236k | if (x == NULL || pk == NULL) |
4040 | 157k | goto end; |
4041 | 236k | } else { |
4042 | 0 | size_t certidx; |
4043 | |
|
4044 | 0 | if (x == NULL || pk == NULL) |
4045 | 0 | return 0; |
4046 | | |
4047 | 0 | if (ssl_cert_lookup_by_pkey(pk, &certidx, |
4048 | 0 | SSL_CONNECTION_GET_CTX(s)) |
4049 | 0 | == NULL) |
4050 | 0 | return 0; |
4051 | 0 | idx = (int)certidx; |
4052 | 0 | pvalid = s->s3.tmp.valid_flags + idx; |
4053 | |
|
4054 | 0 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
4055 | 0 | check_flags = CERT_PKEY_STRICT_FLAGS; |
4056 | 0 | else |
4057 | 0 | check_flags = CERT_PKEY_VALID_FLAGS; |
4058 | 0 | strict_mode = 1; |
4059 | 0 | } |
4060 | | |
4061 | 78.8k | if (suiteb_flags) { |
4062 | 0 | int ok; |
4063 | 0 | if (check_flags) |
4064 | 0 | check_flags |= CERT_PKEY_SUITEB; |
4065 | 0 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); |
4066 | 0 | if (ok == X509_V_OK) |
4067 | 0 | rv |= CERT_PKEY_SUITEB; |
4068 | 0 | else if (!check_flags) |
4069 | 0 | goto end; |
4070 | 0 | } |
4071 | | |
4072 | | /* |
4073 | | * Check all signature algorithms are consistent with signature |
4074 | | * algorithms extension if TLS 1.2 or later and strict mode. |
4075 | | */ |
4076 | 78.8k | if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION |
4077 | 32.3k | && strict_mode) { |
4078 | 0 | int default_nid; |
4079 | 0 | int rsign = 0; |
4080 | |
|
4081 | 0 | if (s->s3.tmp.peer_cert_sigalgs != NULL |
4082 | 0 | || s->s3.tmp.peer_sigalgs != NULL) { |
4083 | 0 | default_nid = 0; |
4084 | | /* If no sigalgs extension use defaults from RFC5246 */ |
4085 | 0 | } else { |
4086 | 0 | switch (idx) { |
4087 | 0 | case SSL_PKEY_RSA: |
4088 | 0 | rsign = EVP_PKEY_RSA; |
4089 | 0 | default_nid = NID_sha1WithRSAEncryption; |
4090 | 0 | break; |
4091 | | |
4092 | 0 | case SSL_PKEY_DSA_SIGN: |
4093 | 0 | rsign = EVP_PKEY_DSA; |
4094 | 0 | default_nid = NID_dsaWithSHA1; |
4095 | 0 | break; |
4096 | | |
4097 | 0 | case SSL_PKEY_ECC: |
4098 | 0 | rsign = EVP_PKEY_EC; |
4099 | 0 | default_nid = NID_ecdsa_with_SHA1; |
4100 | 0 | break; |
4101 | | |
4102 | 0 | case SSL_PKEY_GOST01: |
4103 | 0 | rsign = NID_id_GostR3410_2001; |
4104 | 0 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
4105 | 0 | break; |
4106 | | |
4107 | 0 | case SSL_PKEY_GOST12_256: |
4108 | 0 | rsign = NID_id_GostR3410_2012_256; |
4109 | 0 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
4110 | 0 | break; |
4111 | | |
4112 | 0 | case SSL_PKEY_GOST12_512: |
4113 | 0 | rsign = NID_id_GostR3410_2012_512; |
4114 | 0 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
4115 | 0 | break; |
4116 | | |
4117 | 0 | default: |
4118 | 0 | default_nid = -1; |
4119 | 0 | break; |
4120 | 0 | } |
4121 | 0 | } |
4122 | | /* |
4123 | | * If peer sent no signature algorithms extension and we have set |
4124 | | * preferred signature algorithms check we support sha1. |
4125 | | */ |
4126 | 0 | if (default_nid > 0 && c->conf_sigalgs) { |
4127 | 0 | size_t j; |
4128 | 0 | const uint16_t *p = c->conf_sigalgs; |
4129 | 0 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
4130 | 0 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *p); |
4131 | |
|
4132 | 0 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) |
4133 | 0 | break; |
4134 | 0 | } |
4135 | 0 | if (j == c->conf_sigalgslen) { |
4136 | 0 | if (check_flags) |
4137 | 0 | goto skip_sigs; |
4138 | 0 | else |
4139 | 0 | goto end; |
4140 | 0 | } |
4141 | 0 | } |
4142 | | /* Check signature algorithm of each cert in chain */ |
4143 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4144 | | /* |
4145 | | * We only get here if the application has called SSL_check_chain(), |
4146 | | * so check_flags is always set. |
4147 | | */ |
4148 | 0 | if (find_sig_alg(s, x, pk) != NULL) |
4149 | 0 | rv |= CERT_PKEY_EE_SIGNATURE; |
4150 | 0 | } else if (!tls1_check_sig_alg(s, x, default_nid)) { |
4151 | 0 | if (!check_flags) |
4152 | 0 | goto end; |
4153 | 0 | } else |
4154 | 0 | rv |= CERT_PKEY_EE_SIGNATURE; |
4155 | 0 | rv |= CERT_PKEY_CA_SIGNATURE; |
4156 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
4157 | 0 | if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) { |
4158 | 0 | if (check_flags) { |
4159 | 0 | rv &= ~CERT_PKEY_CA_SIGNATURE; |
4160 | 0 | break; |
4161 | 0 | } else |
4162 | 0 | goto end; |
4163 | 0 | } |
4164 | 0 | } |
4165 | 0 | } |
4166 | | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
4167 | 78.8k | else if (check_flags) |
4168 | 0 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; |
4169 | 78.8k | skip_sigs: |
4170 | | /* Check cert parameters are consistent */ |
4171 | 78.8k | if (tls1_check_cert_param(s, x, 1)) |
4172 | 71.1k | rv |= CERT_PKEY_EE_PARAM; |
4173 | 7.63k | else if (!check_flags) |
4174 | 7.63k | goto end; |
4175 | 71.1k | if (!s->server) |
4176 | 0 | rv |= CERT_PKEY_CA_PARAM; |
4177 | | /* In strict mode check rest of chain too */ |
4178 | 71.1k | else if (strict_mode) { |
4179 | 0 | rv |= CERT_PKEY_CA_PARAM; |
4180 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
4181 | 0 | X509 *ca = sk_X509_value(chain, i); |
4182 | 0 | if (!tls1_check_cert_param(s, ca, 0)) { |
4183 | 0 | if (check_flags) { |
4184 | 0 | rv &= ~CERT_PKEY_CA_PARAM; |
4185 | 0 | break; |
4186 | 0 | } else |
4187 | 0 | goto end; |
4188 | 0 | } |
4189 | 0 | } |
4190 | 0 | } |
4191 | 71.1k | if (!s->server && strict_mode) { |
4192 | 0 | STACK_OF(X509_NAME) *ca_dn; |
4193 | 0 | int check_type = 0; |
4194 | |
|
4195 | 0 | if (EVP_PKEY_is_a(pk, "RSA")) |
4196 | 0 | check_type = TLS_CT_RSA_SIGN; |
4197 | 0 | else if (EVP_PKEY_is_a(pk, "DSA")) |
4198 | 0 | check_type = TLS_CT_DSS_SIGN; |
4199 | 0 | else if (EVP_PKEY_is_a(pk, "EC")) |
4200 | 0 | check_type = TLS_CT_ECDSA_SIGN; |
4201 | |
|
4202 | 0 | if (check_type) { |
4203 | 0 | const uint8_t *ctypes = s->s3.tmp.ctype; |
4204 | 0 | size_t j; |
4205 | |
|
4206 | 0 | for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) { |
4207 | 0 | if (*ctypes == check_type) { |
4208 | 0 | rv |= CERT_PKEY_CERT_TYPE; |
4209 | 0 | break; |
4210 | 0 | } |
4211 | 0 | } |
4212 | 0 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
4213 | 0 | goto end; |
4214 | 0 | } else { |
4215 | 0 | rv |= CERT_PKEY_CERT_TYPE; |
4216 | 0 | } |
4217 | | |
4218 | 0 | ca_dn = s->s3.tmp.peer_ca_names; |
4219 | |
|
4220 | 0 | if (ca_dn == NULL |
4221 | 0 | || sk_X509_NAME_num(ca_dn) == 0 |
4222 | 0 | || ssl_check_ca_name(ca_dn, x)) |
4223 | 0 | rv |= CERT_PKEY_ISSUER_NAME; |
4224 | 0 | else |
4225 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
4226 | 0 | X509 *xtmp = sk_X509_value(chain, i); |
4227 | |
|
4228 | 0 | if (ssl_check_ca_name(ca_dn, xtmp)) { |
4229 | 0 | rv |= CERT_PKEY_ISSUER_NAME; |
4230 | 0 | break; |
4231 | 0 | } |
4232 | 0 | } |
4233 | |
|
4234 | 0 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
4235 | 0 | goto end; |
4236 | 0 | } else |
4237 | 71.1k | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; |
4238 | | |
4239 | 71.1k | if (!check_flags || (rv & check_flags) == check_flags) |
4240 | 71.1k | rv |= CERT_PKEY_VALID; |
4241 | | |
4242 | 236k | end: |
4243 | | |
4244 | 236k | if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION) |
4245 | 97.0k | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); |
4246 | 139k | else |
4247 | 139k | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
4248 | | |
4249 | | /* |
4250 | | * When checking a CERT_PKEY structure all flags are irrelevant if the |
4251 | | * chain is invalid. |
4252 | | */ |
4253 | 236k | if (!check_flags) { |
4254 | 236k | if (rv & CERT_PKEY_VALID) { |
4255 | 71.1k | *pvalid = rv; |
4256 | 165k | } else { |
4257 | | /* Preserve sign and explicit sign flag, clear rest */ |
4258 | 165k | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
4259 | 165k | return 0; |
4260 | 165k | } |
4261 | 236k | } |
4262 | 71.1k | return rv; |
4263 | 236k | } |
4264 | | |
4265 | | /* Set validity of certificates in an SSL structure */ |
4266 | | void tls1_set_cert_validity(SSL_CONNECTION *s) |
4267 | 28.6k | { |
4268 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
4269 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN); |
4270 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
4271 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
4272 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
4273 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); |
4274 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); |
4275 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); |
4276 | 28.6k | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448); |
4277 | 28.6k | } |
4278 | | |
4279 | | /* User level utility function to check a chain is suitable */ |
4280 | | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
4281 | 0 | { |
4282 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4283 | |
|
4284 | 0 | if (sc == NULL) |
4285 | 0 | return 0; |
4286 | | |
4287 | 0 | return tls1_check_chain(sc, x, pk, chain, -1); |
4288 | 0 | } |
4289 | | |
4290 | | EVP_PKEY *ssl_get_auto_dh(SSL_CONNECTION *s) |
4291 | 0 | { |
4292 | 0 | EVP_PKEY *dhp = NULL; |
4293 | 0 | BIGNUM *p; |
4294 | 0 | int dh_secbits = 80, sec_level_bits; |
4295 | 0 | EVP_PKEY_CTX *pctx = NULL; |
4296 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
4297 | 0 | OSSL_PARAM *params = NULL; |
4298 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4299 | |
|
4300 | 0 | if (s->cert->dh_tmp_auto != 2) { |
4301 | 0 | if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
4302 | 0 | if (s->s3.tmp.new_cipher->strength_bits == 256) |
4303 | 0 | dh_secbits = 128; |
4304 | 0 | else |
4305 | 0 | dh_secbits = 80; |
4306 | 0 | } else { |
4307 | 0 | if (s->s3.tmp.cert == NULL) |
4308 | 0 | return NULL; |
4309 | 0 | dh_secbits = EVP_PKEY_get_security_bits(s->s3.tmp.cert->privatekey); |
4310 | 0 | } |
4311 | 0 | } |
4312 | | |
4313 | | /* Do not pick a prime that is too weak for the current security level */ |
4314 | 0 | sec_level_bits = ssl_get_security_level_bits(SSL_CONNECTION_GET_SSL(s), |
4315 | 0 | NULL, NULL); |
4316 | 0 | if (dh_secbits < sec_level_bits) |
4317 | 0 | dh_secbits = sec_level_bits; |
4318 | |
|
4319 | 0 | if (dh_secbits >= 192) |
4320 | 0 | p = BN_get_rfc3526_prime_8192(NULL); |
4321 | 0 | else if (dh_secbits >= 152) |
4322 | 0 | p = BN_get_rfc3526_prime_4096(NULL); |
4323 | 0 | else if (dh_secbits >= 128) |
4324 | 0 | p = BN_get_rfc3526_prime_3072(NULL); |
4325 | 0 | else if (dh_secbits >= 112) |
4326 | 0 | p = BN_get_rfc3526_prime_2048(NULL); |
4327 | 0 | else |
4328 | 0 | p = BN_get_rfc2409_prime_1024(NULL); |
4329 | 0 | if (p == NULL) |
4330 | 0 | goto err; |
4331 | | |
4332 | 0 | pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq); |
4333 | 0 | if (pctx == NULL |
4334 | 0 | || EVP_PKEY_fromdata_init(pctx) != 1) |
4335 | 0 | goto err; |
4336 | | |
4337 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
4338 | 0 | if (tmpl == NULL |
4339 | 0 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
4340 | 0 | || !OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2)) |
4341 | 0 | goto err; |
4342 | | |
4343 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
4344 | 0 | if (params == NULL |
4345 | 0 | || EVP_PKEY_fromdata(pctx, &dhp, EVP_PKEY_KEY_PARAMETERS, params) != 1) |
4346 | 0 | goto err; |
4347 | | |
4348 | 0 | err: |
4349 | 0 | OSSL_PARAM_free(params); |
4350 | 0 | OSSL_PARAM_BLD_free(tmpl); |
4351 | 0 | EVP_PKEY_CTX_free(pctx); |
4352 | 0 | BN_free(p); |
4353 | 0 | return dhp; |
4354 | 0 | } |
4355 | | |
4356 | | static int ssl_security_cert_key(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
4357 | | int op) |
4358 | 164k | { |
4359 | 164k | int secbits = -1; |
4360 | 164k | EVP_PKEY *pkey = X509_get0_pubkey(x); |
4361 | | |
4362 | 164k | if (pkey) { |
4363 | | /* |
4364 | | * If no parameters this will return -1 and fail using the default |
4365 | | * security callback for any non-zero security level. This will |
4366 | | * reject keys which omit parameters but this only affects DSA and |
4367 | | * omission of parameters is never (?) done in practice. |
4368 | | */ |
4369 | 164k | secbits = EVP_PKEY_get_security_bits(pkey); |
4370 | 164k | } |
4371 | 164k | if (s != NULL) |
4372 | 24.5k | return ssl_security(s, op, secbits, 0, x); |
4373 | 140k | else |
4374 | 140k | return ssl_ctx_security(ctx, op, secbits, 0, x); |
4375 | 164k | } |
4376 | | |
4377 | | static int ssl_security_cert_sig(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
4378 | | int op) |
4379 | 164k | { |
4380 | | /* Lookup signature algorithm digest */ |
4381 | 164k | int secbits, nid, pknid; |
4382 | | |
4383 | | /* Don't check signature if self signed */ |
4384 | 164k | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) |
4385 | 164k | return 1; |
4386 | 0 | if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) |
4387 | 0 | secbits = -1; |
4388 | | /* If digest NID not defined use signature NID */ |
4389 | 0 | if (nid == NID_undef) |
4390 | 0 | nid = pknid; |
4391 | 0 | if (s != NULL) |
4392 | 0 | return ssl_security(s, op, secbits, nid, x); |
4393 | 0 | else |
4394 | 0 | return ssl_ctx_security(ctx, op, secbits, nid, x); |
4395 | 0 | } |
4396 | | |
4397 | | int ssl_security_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, int vfy, |
4398 | | int is_ee) |
4399 | 181k | { |
4400 | 181k | if (vfy) |
4401 | 0 | vfy = SSL_SECOP_PEER; |
4402 | 181k | if (is_ee) { |
4403 | 181k | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) |
4404 | 0 | return SSL_R_EE_KEY_TOO_SMALL; |
4405 | 181k | } else { |
4406 | 0 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) |
4407 | 0 | return SSL_R_CA_KEY_TOO_SMALL; |
4408 | 0 | } |
4409 | 181k | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) |
4410 | 0 | return SSL_R_CA_MD_TOO_WEAK; |
4411 | 181k | return 1; |
4412 | 181k | } |
4413 | | |
4414 | | /* |
4415 | | * Check security of a chain, if |sk| includes the end entity certificate then |
4416 | | * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending |
4417 | | * one to the peer. Return values: 1 if ok otherwise error code to use |
4418 | | */ |
4419 | | |
4420 | | int ssl_security_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk, |
4421 | | X509 *x, int vfy) |
4422 | 26.7k | { |
4423 | 26.7k | int rv, start_idx, i; |
4424 | | |
4425 | 26.7k | if (x == NULL) { |
4426 | 26.7k | x = sk_X509_value(sk, 0); |
4427 | 26.7k | if (x == NULL) |
4428 | 0 | return ERR_R_INTERNAL_ERROR; |
4429 | 26.7k | start_idx = 1; |
4430 | 26.7k | } else |
4431 | 0 | start_idx = 0; |
4432 | | |
4433 | 26.7k | rv = ssl_security_cert(s, NULL, x, vfy, 1); |
4434 | 26.7k | if (rv != 1) |
4435 | 0 | return rv; |
4436 | | |
4437 | 26.7k | for (i = start_idx; i < sk_X509_num(sk); i++) { |
4438 | 0 | x = sk_X509_value(sk, i); |
4439 | 0 | rv = ssl_security_cert(s, NULL, x, vfy, 0); |
4440 | 0 | if (rv != 1) |
4441 | 0 | return rv; |
4442 | 0 | } |
4443 | 26.7k | return 1; |
4444 | 26.7k | } |
4445 | | |
4446 | | /* |
4447 | | * For TLS 1.2 servers check if we have a certificate which can be used |
4448 | | * with the signature algorithm "lu" and return index of certificate. |
4449 | | */ |
4450 | | |
4451 | | static int tls12_get_cert_sigalg_idx(const SSL_CONNECTION *s, |
4452 | | const SIGALG_LOOKUP *lu) |
4453 | 29.3k | { |
4454 | 29.3k | int sig_idx = lu->sig_idx; |
4455 | 29.3k | const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx, |
4456 | 29.3k | SSL_CONNECTION_GET_CTX(s)); |
4457 | | |
4458 | | /* If not recognised or not supported by cipher mask it is not suitable */ |
4459 | 29.3k | if (clu == NULL |
4460 | 29.3k | || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0 |
4461 | 18.4k | || (clu->pkey_nid == EVP_PKEY_RSA_PSS |
4462 | 1.22k | && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0)) |
4463 | 11.5k | return -1; |
4464 | | |
4465 | | /* If doing RPK, the CERT_PKEY won't be "valid" */ |
4466 | 17.7k | if (tls12_rpk_and_privkey(s, sig_idx)) |
4467 | 0 | return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_RPK ? sig_idx : -1; |
4468 | | |
4469 | 17.7k | return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1; |
4470 | 17.7k | } |
4471 | | |
4472 | | /* |
4473 | | * Checks the given cert against signature_algorithm_cert restrictions sent by |
4474 | | * the peer (if any) as well as whether the hash from the sigalg is usable with |
4475 | | * the key. |
4476 | | * Returns true if the cert is usable and false otherwise. |
4477 | | */ |
4478 | | static int check_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, |
4479 | | X509 *x, EVP_PKEY *pkey) |
4480 | 47.4k | { |
4481 | 47.4k | const SIGALG_LOOKUP *lu; |
4482 | 47.4k | int mdnid, pknid, supported; |
4483 | 47.4k | size_t i; |
4484 | 47.4k | const char *mdname = NULL; |
4485 | 47.4k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4486 | | |
4487 | | /* |
4488 | | * If the given EVP_PKEY cannot support signing with this digest, |
4489 | | * the answer is simply 'no'. |
4490 | | */ |
4491 | 47.4k | if (sig->hash != NID_undef) |
4492 | 47.4k | mdname = OBJ_nid2sn(sig->hash); |
4493 | 47.4k | supported = EVP_PKEY_digestsign_supports_digest(pkey, sctx->libctx, |
4494 | 47.4k | mdname, |
4495 | 47.4k | sctx->propq); |
4496 | 47.4k | if (supported <= 0) |
4497 | 0 | return 0; |
4498 | | |
4499 | | /* |
4500 | | * The TLS 1.3 signature_algorithms_cert extension places restrictions |
4501 | | * on the sigalg with which the certificate was signed (by its issuer). |
4502 | | */ |
4503 | 47.4k | if (s->s3.tmp.peer_cert_sigalgs != NULL) { |
4504 | 21.7k | if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL)) |
4505 | 0 | return 0; |
4506 | 121k | for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) { |
4507 | 100k | lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
4508 | 100k | s->s3.tmp.peer_cert_sigalgs[i]); |
4509 | 100k | if (lu == NULL) |
4510 | 72.3k | continue; |
4511 | | |
4512 | | /* |
4513 | | * This does not differentiate between the |
4514 | | * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not |
4515 | | * have a chain here that lets us look at the key OID in the |
4516 | | * signing certificate. |
4517 | | */ |
4518 | 27.8k | if (mdnid == lu->hash && pknid == lu->sig) |
4519 | 52 | return 1; |
4520 | 27.8k | } |
4521 | 21.7k | return 0; |
4522 | 21.7k | } |
4523 | | |
4524 | | /* |
4525 | | * Without signat_algorithms_cert, any certificate for which we have |
4526 | | * a viable public key is permitted. |
4527 | | */ |
4528 | 25.6k | return 1; |
4529 | 47.4k | } |
4530 | | |
4531 | | /* |
4532 | | * Returns true if |s| has a usable certificate configured for use |
4533 | | * with signature scheme |sig|. |
4534 | | * "Usable" includes a check for presence as well as applying |
4535 | | * the signature_algorithm_cert restrictions sent by the peer (if any). |
4536 | | * Returns false if no usable certificate is found. |
4537 | | */ |
4538 | | static int has_usable_cert(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, int idx) |
4539 | 47.7k | { |
4540 | | /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */ |
4541 | 47.7k | if (idx == -1) |
4542 | 7.21k | idx = sig->sig_idx; |
4543 | 47.7k | if (!ssl_has_cert(s, idx)) |
4544 | 354 | return 0; |
4545 | | |
4546 | 47.4k | return check_cert_usable(s, sig, s->cert->pkeys[idx].x509, |
4547 | 47.4k | s->cert->pkeys[idx].privatekey); |
4548 | 47.7k | } |
4549 | | |
4550 | | /* |
4551 | | * Returns true if the supplied cert |x| and key |pkey| is usable with the |
4552 | | * specified signature scheme |sig|, or false otherwise. |
4553 | | */ |
4554 | | static int is_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, X509 *x, |
4555 | | EVP_PKEY *pkey) |
4556 | 0 | { |
4557 | 0 | size_t idx; |
4558 | |
|
4559 | 0 | if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) |
4560 | 0 | return 0; |
4561 | | |
4562 | | /* Check the key is consistent with the sig alg */ |
4563 | 0 | if ((int)idx != sig->sig_idx) |
4564 | 0 | return 0; |
4565 | | |
4566 | 0 | return check_cert_usable(s, sig, x, pkey); |
4567 | 0 | } |
4568 | | |
4569 | | /* |
4570 | | * Find a signature scheme that works with the supplied certificate |x| and key |
4571 | | * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our |
4572 | | * available certs/keys to find one that works. |
4573 | | */ |
4574 | | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, |
4575 | | EVP_PKEY *pkey) |
4576 | 2.06k | { |
4577 | 2.06k | const SIGALG_LOOKUP *lu = NULL; |
4578 | 2.06k | size_t i; |
4579 | 2.06k | int curve = -1; |
4580 | 2.06k | EVP_PKEY *tmppkey; |
4581 | 2.06k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4582 | | |
4583 | | /* Look for a shared sigalgs matching possible certificates */ |
4584 | 5.86k | for (i = 0; i < s->shared_sigalgslen; i++) { |
4585 | | /* Skip SHA1, SHA224, DSA and RSA if not PSS */ |
4586 | 5.77k | lu = s->shared_sigalgs[i]; |
4587 | 5.77k | if (lu->hash == NID_sha1 |
4588 | 4.88k | || lu->hash == NID_sha224 |
4589 | 4.65k | || lu->sig == EVP_PKEY_DSA |
4590 | 4.65k | || lu->sig == EVP_PKEY_RSA |
4591 | 3.70k | || !tls_sigalg_compat(s, lu)) |
4592 | 2.06k | continue; |
4593 | | |
4594 | | /* Check that we have a cert, and signature_algorithms_cert */ |
4595 | 3.70k | if (!tls1_lookup_md(sctx, lu, NULL)) |
4596 | 0 | continue; |
4597 | 3.70k | if ((pkey == NULL && !has_usable_cert(s, lu, -1)) |
4598 | 3.54k | || (pkey != NULL && !is_cert_usable(s, lu, x, pkey))) |
4599 | 160 | continue; |
4600 | | |
4601 | 3.54k | tmppkey = (pkey != NULL) ? pkey |
4602 | 3.54k | : s->cert->pkeys[lu->sig_idx].privatekey; |
4603 | | |
4604 | 3.54k | if (lu->sig == EVP_PKEY_EC) { |
4605 | 3.03k | if (curve == -1) |
4606 | 1.67k | curve = ssl_get_EC_curve_nid(tmppkey); |
4607 | 3.03k | if (lu->curve != NID_undef && curve != lu->curve) |
4608 | 1.57k | continue; |
4609 | 3.03k | } else if (lu->sig == EVP_PKEY_RSA_PSS) { |
4610 | | /* validate that key is large enough for the signature algorithm */ |
4611 | 504 | if (!rsa_pss_check_min_key_size(sctx, tmppkey, lu)) |
4612 | 0 | continue; |
4613 | 504 | } |
4614 | 1.96k | break; |
4615 | 3.54k | } |
4616 | | |
4617 | 2.06k | if (i == s->shared_sigalgslen) |
4618 | 95 | return NULL; |
4619 | | |
4620 | 1.96k | return lu; |
4621 | 2.06k | } |
4622 | | |
4623 | | /* |
4624 | | * Choose an appropriate signature algorithm based on available certificates |
4625 | | * Sets chosen certificate and signature algorithm. |
4626 | | * |
4627 | | * For servers if we fail to find a required certificate it is a fatal error, |
4628 | | * an appropriate error code is set and a TLS alert is sent. |
4629 | | * |
4630 | | * For clients fatalerrs is set to 0. If a certificate is not suitable it is not |
4631 | | * a fatal error: we will either try another certificate or not present one |
4632 | | * to the server. In this case no error is set. |
4633 | | */ |
4634 | | int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs) |
4635 | 16.7k | { |
4636 | 16.7k | const SIGALG_LOOKUP *lu = NULL; |
4637 | 16.7k | int sig_idx = -1; |
4638 | | |
4639 | 16.7k | s->s3.tmp.cert = NULL; |
4640 | 16.7k | s->s3.tmp.sigalg = NULL; |
4641 | | |
4642 | 16.7k | if (SSL_CONNECTION_IS_TLS13(s)) { |
4643 | 2.06k | lu = find_sig_alg(s, NULL, NULL); |
4644 | 2.06k | if (lu == NULL) { |
4645 | 95 | if (!fatalerrs) |
4646 | 0 | return 1; |
4647 | 95 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4648 | 95 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4649 | 95 | return 0; |
4650 | 95 | } |
4651 | 14.6k | } else { |
4652 | | /* If ciphersuite doesn't require a cert nothing to do */ |
4653 | 14.6k | if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT)) |
4654 | 1.04k | return 1; |
4655 | 13.6k | if (!s->server && !ssl_has_cert(s, (int)(s->cert->key - s->cert->pkeys))) |
4656 | 28 | return 1; |
4657 | | |
4658 | 13.6k | if (SSL_USE_SIGALGS(s)) { |
4659 | 10.8k | size_t i; |
4660 | 10.8k | if (s->s3.tmp.peer_sigalgs != NULL) { |
4661 | 2.30k | int curve = -1; |
4662 | 2.30k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4663 | | |
4664 | | /* For Suite B need to match signature algorithm to curve */ |
4665 | 2.30k | if (tls1_suiteb(s)) |
4666 | 0 | curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC] |
4667 | 0 | .privatekey); |
4668 | | |
4669 | | /* |
4670 | | * Find highest preference signature algorithm matching |
4671 | | * cert type |
4672 | | */ |
4673 | 18.1k | for (i = 0; i < s->shared_sigalgslen; i++) { |
4674 | | /* Check the sigalg version bounds */ |
4675 | 17.6k | lu = s->shared_sigalgs[i]; |
4676 | 17.6k | if (!tls_sigalg_compat(s, lu)) |
4677 | 94 | continue; |
4678 | 17.5k | if (s->server) { |
4679 | 17.5k | if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1) |
4680 | 7.82k | continue; |
4681 | 17.5k | } else { |
4682 | 0 | int cc_idx = (int)(s->cert->key - s->cert->pkeys); |
4683 | |
|
4684 | 0 | sig_idx = lu->sig_idx; |
4685 | 0 | if (cc_idx != sig_idx) |
4686 | 0 | continue; |
4687 | 0 | } |
4688 | | /* Check that we have a cert, and sig_algs_cert */ |
4689 | 9.70k | if (!has_usable_cert(s, lu, sig_idx)) |
4690 | 7.98k | continue; |
4691 | 1.72k | if (lu->sig == EVP_PKEY_RSA_PSS) { |
4692 | | /* validate that key is large enough for the signature algorithm */ |
4693 | 552 | EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey; |
4694 | | |
4695 | 552 | if (!rsa_pss_check_min_key_size(sctx, pkey, lu)) |
4696 | 0 | continue; |
4697 | 552 | } |
4698 | 1.72k | if (curve == -1 || lu->curve == curve) |
4699 | 1.72k | break; |
4700 | 1.72k | } |
4701 | 2.30k | #ifndef OPENSSL_NO_GOST |
4702 | | /* |
4703 | | * Some Windows-based implementations do not send GOST algorithms indication |
4704 | | * in supported_algorithms extension, so when we have GOST-based ciphersuite, |
4705 | | * we have to assume GOST support. |
4706 | | */ |
4707 | 2.30k | if (i == s->shared_sigalgslen |
4708 | 571 | && (s->s3.tmp.new_cipher->algorithm_auth |
4709 | 571 | & (SSL_aGOST01 | SSL_aGOST12)) |
4710 | 571 | != 0) { |
4711 | 0 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
4712 | 0 | if (!fatalerrs) |
4713 | 0 | return 1; |
4714 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4715 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4716 | 0 | return 0; |
4717 | 0 | } else { |
4718 | 0 | i = 0; |
4719 | 0 | sig_idx = lu->sig_idx; |
4720 | 0 | } |
4721 | 0 | } |
4722 | 2.30k | #endif |
4723 | 2.30k | if (i == s->shared_sigalgslen) { |
4724 | 571 | if (!fatalerrs) |
4725 | 0 | return 1; |
4726 | 571 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4727 | 571 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4728 | 571 | return 0; |
4729 | 571 | } |
4730 | 8.58k | } else { |
4731 | | /* |
4732 | | * If we have no sigalg use defaults |
4733 | | */ |
4734 | 8.58k | const uint16_t *sent_sigs; |
4735 | 8.58k | size_t sent_sigslen; |
4736 | | |
4737 | 8.58k | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
4738 | 0 | if (!fatalerrs) |
4739 | 0 | return 1; |
4740 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4741 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4742 | 0 | return 0; |
4743 | 0 | } |
4744 | | |
4745 | | /* Check signature matches a type we sent */ |
4746 | 8.58k | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
4747 | 190k | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
4748 | 190k | if (lu->sigalg == *sent_sigs |
4749 | 8.58k | && has_usable_cert(s, lu, lu->sig_idx)) |
4750 | 8.58k | break; |
4751 | 190k | } |
4752 | 8.58k | if (i == sent_sigslen) { |
4753 | 0 | if (!fatalerrs) |
4754 | 0 | return 1; |
4755 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4756 | 0 | SSL_R_WRONG_SIGNATURE_TYPE); |
4757 | 0 | return 0; |
4758 | 0 | } |
4759 | 8.58k | } |
4760 | 10.8k | } else { |
4761 | 2.71k | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
4762 | 0 | if (!fatalerrs) |
4763 | 0 | return 1; |
4764 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
4765 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4766 | 0 | return 0; |
4767 | 0 | } |
4768 | 2.71k | } |
4769 | 13.6k | } |
4770 | 14.9k | if (sig_idx == -1) |
4771 | 13.2k | sig_idx = lu->sig_idx; |
4772 | 14.9k | s->s3.tmp.cert = &s->cert->pkeys[sig_idx]; |
4773 | 14.9k | s->cert->key = s->s3.tmp.cert; |
4774 | 14.9k | s->s3.tmp.sigalg = lu; |
4775 | 14.9k | return 1; |
4776 | 16.7k | } |
4777 | | |
4778 | | int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode) |
4779 | 0 | { |
4780 | 0 | if (mode != TLSEXT_max_fragment_length_DISABLED |
4781 | 0 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { |
4782 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
4783 | 0 | return 0; |
4784 | 0 | } |
4785 | | |
4786 | 0 | ctx->ext.max_fragment_len_mode = mode; |
4787 | 0 | return 1; |
4788 | 0 | } |
4789 | | |
4790 | | int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode) |
4791 | 0 | { |
4792 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
4793 | |
|
4794 | 0 | if (sc == NULL |
4795 | 0 | || (IS_QUIC(ssl) && mode != TLSEXT_max_fragment_length_DISABLED)) |
4796 | 0 | return 0; |
4797 | | |
4798 | 0 | if (mode != TLSEXT_max_fragment_length_DISABLED |
4799 | 0 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { |
4800 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
4801 | 0 | return 0; |
4802 | 0 | } |
4803 | | |
4804 | 0 | sc->ext.max_fragment_len_mode = mode; |
4805 | 0 | return 1; |
4806 | 0 | } |
4807 | | |
4808 | | uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session) |
4809 | 0 | { |
4810 | 0 | if (session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED) |
4811 | 0 | return TLSEXT_max_fragment_length_DISABLED; |
4812 | 0 | return session->ext.max_fragment_len_mode; |
4813 | 0 | } |
4814 | | |
4815 | | /* |
4816 | | * Helper functions for HMAC access with legacy support included. |
4817 | | */ |
4818 | | SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx) |
4819 | 2.40k | { |
4820 | 2.40k | SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret)); |
4821 | 2.40k | EVP_MAC *mac = NULL; |
4822 | | |
4823 | 2.40k | if (ret == NULL) |
4824 | 0 | return NULL; |
4825 | 2.40k | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4826 | 2.40k | if (ctx->ext.ticket_key_evp_cb == NULL |
4827 | 2.40k | && ctx->ext.ticket_key_cb != NULL) { |
4828 | 0 | if (!ssl_hmac_old_new(ret)) |
4829 | 0 | goto err; |
4830 | 0 | return ret; |
4831 | 0 | } |
4832 | 2.40k | #endif |
4833 | 2.40k | mac = EVP_MAC_fetch(ctx->libctx, "HMAC", ctx->propq); |
4834 | 2.40k | if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL) |
4835 | 0 | goto err; |
4836 | 2.40k | EVP_MAC_free(mac); |
4837 | 2.40k | return ret; |
4838 | 0 | err: |
4839 | 0 | EVP_MAC_CTX_free(ret->ctx); |
4840 | 0 | EVP_MAC_free(mac); |
4841 | 0 | OPENSSL_free(ret); |
4842 | 0 | return NULL; |
4843 | 2.40k | } |
4844 | | |
4845 | | void ssl_hmac_free(SSL_HMAC *ctx) |
4846 | 7.46k | { |
4847 | 7.46k | if (ctx != NULL) { |
4848 | 2.40k | EVP_MAC_CTX_free(ctx->ctx); |
4849 | 2.40k | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4850 | 2.40k | ssl_hmac_old_free(ctx); |
4851 | 2.40k | #endif |
4852 | 2.40k | OPENSSL_free(ctx); |
4853 | 2.40k | } |
4854 | 7.46k | } |
4855 | | |
4856 | | EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx) |
4857 | 0 | { |
4858 | 0 | return ctx->ctx; |
4859 | 0 | } |
4860 | | |
4861 | | int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md) |
4862 | 1.42k | { |
4863 | 1.42k | OSSL_PARAM params[2], *p = params; |
4864 | | |
4865 | 1.42k | if (ctx->ctx != NULL) { |
4866 | 1.42k | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0); |
4867 | 1.42k | *p = OSSL_PARAM_construct_end(); |
4868 | 1.42k | if (EVP_MAC_init(ctx->ctx, key, len, params)) |
4869 | 1.42k | return 1; |
4870 | 1.42k | } |
4871 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4872 | 0 | if (ctx->old_ctx != NULL) |
4873 | 0 | return ssl_hmac_old_init(ctx, key, len, md); |
4874 | 0 | #endif |
4875 | 0 | return 0; |
4876 | 0 | } |
4877 | | |
4878 | | int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len) |
4879 | 1.26k | { |
4880 | 1.26k | if (ctx->ctx != NULL) |
4881 | 1.26k | return EVP_MAC_update(ctx->ctx, data, len); |
4882 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4883 | 0 | if (ctx->old_ctx != NULL) |
4884 | 0 | return ssl_hmac_old_update(ctx, data, len); |
4885 | 0 | #endif |
4886 | 0 | return 0; |
4887 | 0 | } |
4888 | | |
4889 | | int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len, |
4890 | | size_t max_size) |
4891 | 1.26k | { |
4892 | 1.26k | if (ctx->ctx != NULL) |
4893 | 1.26k | return EVP_MAC_final(ctx->ctx, md, len, max_size); |
4894 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4895 | 0 | if (ctx->old_ctx != NULL) |
4896 | 0 | return ssl_hmac_old_final(ctx, md, len); |
4897 | 0 | #endif |
4898 | 0 | return 0; |
4899 | 0 | } |
4900 | | |
4901 | | size_t ssl_hmac_size(const SSL_HMAC *ctx) |
4902 | 1.38k | { |
4903 | 1.38k | if (ctx->ctx != NULL) |
4904 | 1.38k | return EVP_MAC_CTX_get_mac_size(ctx->ctx); |
4905 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4906 | 0 | if (ctx->old_ctx != NULL) |
4907 | 0 | return ssl_hmac_old_size(ctx); |
4908 | 0 | #endif |
4909 | 0 | return 0; |
4910 | 0 | } |
4911 | | |
4912 | | int ssl_get_EC_curve_nid(const EVP_PKEY *pkey) |
4913 | 31.5k | { |
4914 | 31.5k | char gname[OSSL_MAX_NAME_SIZE]; |
4915 | | |
4916 | 31.5k | if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) > 0) |
4917 | 31.5k | return OBJ_txt2nid(gname); |
4918 | | |
4919 | 0 | return NID_undef; |
4920 | 31.5k | } |
4921 | | |
4922 | | __owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey, |
4923 | | const unsigned char *enckey, |
4924 | | size_t enckeylen) |
4925 | 28.6k | { |
4926 | 28.6k | if (EVP_PKEY_is_a(pkey, "DH")) { |
4927 | 161 | int bits = EVP_PKEY_get_bits(pkey); |
4928 | | |
4929 | 161 | if (bits <= 0 || enckeylen != (size_t)bits / 8) |
4930 | | /* the encoded key must be padded to the length of the p */ |
4931 | 12 | return 0; |
4932 | 28.4k | } else if (EVP_PKEY_is_a(pkey, "EC")) { |
4933 | 199 | if (enckeylen < 3 /* point format and at least 1 byte for x and y */ |
4934 | 193 | || enckey[0] != 0x04) |
4935 | 50 | return 0; |
4936 | 199 | } |
4937 | | |
4938 | 28.5k | return EVP_PKEY_set1_encoded_public_key(pkey, enckey, enckeylen); |
4939 | 28.6k | } |