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