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