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