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