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