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