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