/src/openssl/ssl/t1_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <stdlib.h> |
12 | | #include <ctype.h> |
13 | | #include <openssl/objects.h> |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/hmac.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/ocsp.h> |
18 | | #include <openssl/conf.h> |
19 | | #include <openssl/x509v3.h> |
20 | | #include <openssl/dh.h> |
21 | | #include <openssl/bn.h> |
22 | | #include <openssl/provider.h> |
23 | | #include <openssl/param_build.h> |
24 | | #include "internal/nelem.h" |
25 | | #include "internal/sizes.h" |
26 | | #include "internal/tlsgroups.h" |
27 | | #include "internal/ssl_unwrap.h" |
28 | | #include "ssl_local.h" |
29 | | #include "quic/quic_local.h" |
30 | | #include <openssl/ct.h> |
31 | | |
32 | | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pkey); |
33 | | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, const SIGALG_LOOKUP *lu); |
34 | | |
35 | | SSL3_ENC_METHOD const TLSv1_enc_data = { |
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_setup_key_block, |
52 | | tls1_generate_master_secret, |
53 | | tls1_change_cipher_state, |
54 | | tls1_final_finish_mac, |
55 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
56 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
57 | | tls1_alert_code, |
58 | | tls1_export_keying_material, |
59 | | 0, |
60 | | ssl3_set_handshake_header, |
61 | | tls_close_construct_packet, |
62 | | ssl3_handshake_write |
63 | | }; |
64 | | |
65 | | SSL3_ENC_METHOD const TLSv1_2_enc_data = { |
66 | | tls1_setup_key_block, |
67 | | tls1_generate_master_secret, |
68 | | tls1_change_cipher_state, |
69 | | tls1_final_finish_mac, |
70 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
71 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
72 | | tls1_alert_code, |
73 | | tls1_export_keying_material, |
74 | | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF |
75 | | | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
76 | | ssl3_set_handshake_header, |
77 | | tls_close_construct_packet, |
78 | | ssl3_handshake_write |
79 | | }; |
80 | | |
81 | | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
82 | | tls13_setup_key_block, |
83 | | tls13_generate_master_secret, |
84 | | tls13_change_cipher_state, |
85 | | tls13_final_finish_mac, |
86 | | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
87 | | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
88 | | tls13_alert_code, |
89 | | tls13_export_keying_material, |
90 | | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
91 | | ssl3_set_handshake_header, |
92 | | tls_close_construct_packet, |
93 | | ssl3_handshake_write |
94 | | }; |
95 | | |
96 | | OSSL_TIME tls1_default_timeout(void) |
97 | 0 | { |
98 | | /* |
99 | | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for |
100 | | * http, the cache would over fill |
101 | | */ |
102 | 0 | return ossl_seconds2time(60 * 60 * 2); |
103 | 0 | } |
104 | | |
105 | | int tls1_new(SSL *s) |
106 | 0 | { |
107 | 0 | if (!ssl3_new(s)) |
108 | 0 | return 0; |
109 | 0 | if (!s->method->ssl_clear(s)) |
110 | 0 | return 0; |
111 | | |
112 | 0 | return 1; |
113 | 0 | } |
114 | | |
115 | | void tls1_free(SSL *s) |
116 | 0 | { |
117 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
118 | |
|
119 | 0 | if (sc == NULL) |
120 | 0 | return; |
121 | | |
122 | 0 | OPENSSL_free(sc->ext.session_ticket); |
123 | 0 | ssl3_free(s); |
124 | 0 | } |
125 | | |
126 | | int tls1_clear(SSL *s) |
127 | 0 | { |
128 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
129 | |
|
130 | 0 | if (sc == NULL) |
131 | 0 | return 0; |
132 | | |
133 | 0 | if (!ssl3_clear(s)) |
134 | 0 | return 0; |
135 | | |
136 | 0 | if (s->method->version == TLS_ANY_VERSION) |
137 | 0 | sc->version = TLS_MAX_VERSION_INTERNAL; |
138 | 0 | else |
139 | 0 | sc->version = s->method->version; |
140 | |
|
141 | 0 | return 1; |
142 | 0 | } |
143 | | |
144 | | /* Legacy NID to group_id mapping. Only works for groups we know about */ |
145 | | static const struct { |
146 | | int nid; |
147 | | uint16_t group_id; |
148 | | } nid_to_group[] = { |
149 | | {NID_sect163k1, OSSL_TLS_GROUP_ID_sect163k1}, |
150 | | {NID_sect163r1, OSSL_TLS_GROUP_ID_sect163r1}, |
151 | | {NID_sect163r2, OSSL_TLS_GROUP_ID_sect163r2}, |
152 | | {NID_sect193r1, OSSL_TLS_GROUP_ID_sect193r1}, |
153 | | {NID_sect193r2, OSSL_TLS_GROUP_ID_sect193r2}, |
154 | | {NID_sect233k1, OSSL_TLS_GROUP_ID_sect233k1}, |
155 | | {NID_sect233r1, OSSL_TLS_GROUP_ID_sect233r1}, |
156 | | {NID_sect239k1, OSSL_TLS_GROUP_ID_sect239k1}, |
157 | | {NID_sect283k1, OSSL_TLS_GROUP_ID_sect283k1}, |
158 | | {NID_sect283r1, OSSL_TLS_GROUP_ID_sect283r1}, |
159 | | {NID_sect409k1, OSSL_TLS_GROUP_ID_sect409k1}, |
160 | | {NID_sect409r1, OSSL_TLS_GROUP_ID_sect409r1}, |
161 | | {NID_sect571k1, OSSL_TLS_GROUP_ID_sect571k1}, |
162 | | {NID_sect571r1, OSSL_TLS_GROUP_ID_sect571r1}, |
163 | | {NID_secp160k1, OSSL_TLS_GROUP_ID_secp160k1}, |
164 | | {NID_secp160r1, OSSL_TLS_GROUP_ID_secp160r1}, |
165 | | {NID_secp160r2, OSSL_TLS_GROUP_ID_secp160r2}, |
166 | | {NID_secp192k1, OSSL_TLS_GROUP_ID_secp192k1}, |
167 | | {NID_X9_62_prime192v1, OSSL_TLS_GROUP_ID_secp192r1}, |
168 | | {NID_secp224k1, OSSL_TLS_GROUP_ID_secp224k1}, |
169 | | {NID_secp224r1, OSSL_TLS_GROUP_ID_secp224r1}, |
170 | | {NID_secp256k1, OSSL_TLS_GROUP_ID_secp256k1}, |
171 | | {NID_X9_62_prime256v1, OSSL_TLS_GROUP_ID_secp256r1}, |
172 | | {NID_secp384r1, OSSL_TLS_GROUP_ID_secp384r1}, |
173 | | {NID_secp521r1, OSSL_TLS_GROUP_ID_secp521r1}, |
174 | | {NID_brainpoolP256r1, OSSL_TLS_GROUP_ID_brainpoolP256r1}, |
175 | | {NID_brainpoolP384r1, OSSL_TLS_GROUP_ID_brainpoolP384r1}, |
176 | | {NID_brainpoolP512r1, OSSL_TLS_GROUP_ID_brainpoolP512r1}, |
177 | | {EVP_PKEY_X25519, OSSL_TLS_GROUP_ID_x25519}, |
178 | | {EVP_PKEY_X448, OSSL_TLS_GROUP_ID_x448}, |
179 | | {NID_brainpoolP256r1tls13, OSSL_TLS_GROUP_ID_brainpoolP256r1_tls13}, |
180 | | {NID_brainpoolP384r1tls13, OSSL_TLS_GROUP_ID_brainpoolP384r1_tls13}, |
181 | | {NID_brainpoolP512r1tls13, OSSL_TLS_GROUP_ID_brainpoolP512r1_tls13}, |
182 | | {NID_id_tc26_gost_3410_2012_256_paramSetA, OSSL_TLS_GROUP_ID_gc256A}, |
183 | | {NID_id_tc26_gost_3410_2012_256_paramSetB, OSSL_TLS_GROUP_ID_gc256B}, |
184 | | {NID_id_tc26_gost_3410_2012_256_paramSetC, OSSL_TLS_GROUP_ID_gc256C}, |
185 | | {NID_id_tc26_gost_3410_2012_256_paramSetD, OSSL_TLS_GROUP_ID_gc256D}, |
186 | | {NID_id_tc26_gost_3410_2012_512_paramSetA, OSSL_TLS_GROUP_ID_gc512A}, |
187 | | {NID_id_tc26_gost_3410_2012_512_paramSetB, OSSL_TLS_GROUP_ID_gc512B}, |
188 | | {NID_id_tc26_gost_3410_2012_512_paramSetC, OSSL_TLS_GROUP_ID_gc512C}, |
189 | | {NID_ffdhe2048, OSSL_TLS_GROUP_ID_ffdhe2048}, |
190 | | {NID_ffdhe3072, OSSL_TLS_GROUP_ID_ffdhe3072}, |
191 | | {NID_ffdhe4096, OSSL_TLS_GROUP_ID_ffdhe4096}, |
192 | | {NID_ffdhe6144, OSSL_TLS_GROUP_ID_ffdhe6144}, |
193 | | {NID_ffdhe8192, OSSL_TLS_GROUP_ID_ffdhe8192} |
194 | | }; |
195 | | |
196 | | static const unsigned char ecformats_default[] = { |
197 | | TLSEXT_ECPOINTFORMAT_uncompressed |
198 | | }; |
199 | | |
200 | | static const unsigned char ecformats_all[] = { |
201 | | TLSEXT_ECPOINTFORMAT_uncompressed, |
202 | | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, |
203 | | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 |
204 | | }; |
205 | | |
206 | | /* Group list string of the built-in pseudo group DEFAULT */ |
207 | | #define DEFAULT_GROUP_NAME "DEFAULT" |
208 | | #define TLS_DEFAULT_GROUP_LIST \ |
209 | | "?*X25519MLKEM768 / ?*X25519:?secp256r1 / ?X448:?secp384r1:?secp521r1 / ?ffdhe2048:?ffdhe3072" |
210 | | |
211 | | static const uint16_t suiteb_curves[] = { |
212 | | OSSL_TLS_GROUP_ID_secp256r1, |
213 | | OSSL_TLS_GROUP_ID_secp384r1, |
214 | | }; |
215 | | |
216 | | /* Group list string of the built-in pseudo group DEFAULT_SUITE_B */ |
217 | | #define SUITE_B_GROUP_NAME "DEFAULT_SUITE_B" |
218 | | #define SUITE_B_GROUP_LIST "secp256r1:secp384r1", |
219 | | |
220 | | struct provider_ctx_data_st { |
221 | | SSL_CTX *ctx; |
222 | | OSSL_PROVIDER *provider; |
223 | | }; |
224 | | |
225 | 0 | #define TLS_GROUP_LIST_MALLOC_BLOCK_SIZE 10 |
226 | | static OSSL_CALLBACK add_provider_groups; |
227 | | static int add_provider_groups(const OSSL_PARAM params[], void *data) |
228 | 0 | { |
229 | 0 | struct provider_ctx_data_st *pgd = data; |
230 | 0 | SSL_CTX *ctx = pgd->ctx; |
231 | 0 | const OSSL_PARAM *p; |
232 | 0 | TLS_GROUP_INFO *ginf = NULL; |
233 | 0 | EVP_KEYMGMT *keymgmt; |
234 | 0 | unsigned int gid; |
235 | 0 | unsigned int is_kem = 0; |
236 | 0 | int ret = 0; |
237 | |
|
238 | 0 | if (ctx->group_list_max_len == ctx->group_list_len) { |
239 | 0 | TLS_GROUP_INFO *tmp = NULL; |
240 | |
|
241 | 0 | if (ctx->group_list_max_len == 0) |
242 | 0 | tmp = OPENSSL_malloc(sizeof(TLS_GROUP_INFO) |
243 | 0 | * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE); |
244 | 0 | else |
245 | 0 | tmp = OPENSSL_realloc(ctx->group_list, |
246 | 0 | (ctx->group_list_max_len |
247 | 0 | + TLS_GROUP_LIST_MALLOC_BLOCK_SIZE) |
248 | 0 | * sizeof(TLS_GROUP_INFO)); |
249 | 0 | if (tmp == NULL) |
250 | 0 | return 0; |
251 | 0 | ctx->group_list = tmp; |
252 | 0 | memset(tmp + ctx->group_list_max_len, |
253 | 0 | 0, |
254 | 0 | sizeof(TLS_GROUP_INFO) * TLS_GROUP_LIST_MALLOC_BLOCK_SIZE); |
255 | 0 | ctx->group_list_max_len += TLS_GROUP_LIST_MALLOC_BLOCK_SIZE; |
256 | 0 | } |
257 | | |
258 | 0 | ginf = &ctx->group_list[ctx->group_list_len]; |
259 | |
|
260 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME); |
261 | 0 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
262 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
263 | 0 | goto err; |
264 | 0 | } |
265 | 0 | ginf->tlsname = OPENSSL_strdup(p->data); |
266 | 0 | if (ginf->tlsname == NULL) |
267 | 0 | goto err; |
268 | | |
269 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL); |
270 | 0 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
271 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
272 | 0 | goto err; |
273 | 0 | } |
274 | 0 | ginf->realname = OPENSSL_strdup(p->data); |
275 | 0 | if (ginf->realname == NULL) |
276 | 0 | goto err; |
277 | | |
278 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ID); |
279 | 0 | if (p == NULL || !OSSL_PARAM_get_uint(p, &gid) || gid > UINT16_MAX) { |
280 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
281 | 0 | goto err; |
282 | 0 | } |
283 | 0 | ginf->group_id = (uint16_t)gid; |
284 | |
|
285 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_ALG); |
286 | 0 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
287 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
288 | 0 | goto err; |
289 | 0 | } |
290 | 0 | ginf->algorithm = OPENSSL_strdup(p->data); |
291 | 0 | if (ginf->algorithm == NULL) |
292 | 0 | goto err; |
293 | | |
294 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS); |
295 | 0 | if (p == NULL || !OSSL_PARAM_get_uint(p, &ginf->secbits)) { |
296 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
297 | 0 | goto err; |
298 | 0 | } |
299 | | |
300 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_IS_KEM); |
301 | 0 | if (p != NULL && (!OSSL_PARAM_get_uint(p, &is_kem) || is_kem > 1)) { |
302 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
303 | 0 | goto err; |
304 | 0 | } |
305 | 0 | ginf->is_kem = 1 & is_kem; |
306 | |
|
307 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_TLS); |
308 | 0 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mintls)) { |
309 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
310 | 0 | goto err; |
311 | 0 | } |
312 | | |
313 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_TLS); |
314 | 0 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxtls)) { |
315 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
316 | 0 | goto err; |
317 | 0 | } |
318 | | |
319 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS); |
320 | 0 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->mindtls)) { |
321 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
322 | 0 | goto err; |
323 | 0 | } |
324 | | |
325 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS); |
326 | 0 | if (p == NULL || !OSSL_PARAM_get_int(p, &ginf->maxdtls)) { |
327 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
328 | 0 | goto err; |
329 | 0 | } |
330 | | /* |
331 | | * Now check that the algorithm is actually usable for our property query |
332 | | * string. Regardless of the result we still return success because we have |
333 | | * successfully processed this group, even though we may decide not to use |
334 | | * it. |
335 | | */ |
336 | 0 | ret = 1; |
337 | 0 | ERR_set_mark(); |
338 | 0 | keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, ginf->algorithm, ctx->propq); |
339 | 0 | if (keymgmt != NULL) { |
340 | | /* We have successfully fetched the algorithm, we can use the group. */ |
341 | 0 | ctx->group_list_len++; |
342 | 0 | ginf = NULL; |
343 | 0 | EVP_KEYMGMT_free(keymgmt); |
344 | 0 | } |
345 | 0 | ERR_pop_to_mark(); |
346 | 0 | err: |
347 | 0 | if (ginf != NULL) { |
348 | 0 | OPENSSL_free(ginf->tlsname); |
349 | 0 | OPENSSL_free(ginf->realname); |
350 | 0 | OPENSSL_free(ginf->algorithm); |
351 | 0 | ginf->algorithm = ginf->tlsname = ginf->realname = NULL; |
352 | 0 | } |
353 | 0 | return ret; |
354 | 0 | } |
355 | | |
356 | | static int discover_provider_groups(OSSL_PROVIDER *provider, void *vctx) |
357 | 0 | { |
358 | 0 | struct provider_ctx_data_st pgd; |
359 | |
|
360 | 0 | pgd.ctx = vctx; |
361 | 0 | pgd.provider = provider; |
362 | 0 | return OSSL_PROVIDER_get_capabilities(provider, "TLS-GROUP", |
363 | 0 | add_provider_groups, &pgd); |
364 | 0 | } |
365 | | |
366 | | int ssl_load_groups(SSL_CTX *ctx) |
367 | 0 | { |
368 | 0 | if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_groups, ctx)) |
369 | 0 | return 0; |
370 | | |
371 | 0 | return SSL_CTX_set1_groups_list(ctx, TLS_DEFAULT_GROUP_LIST); |
372 | 0 | } |
373 | | |
374 | 0 | #define TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE 10 |
375 | | static OSSL_CALLBACK add_provider_sigalgs; |
376 | | static int add_provider_sigalgs(const OSSL_PARAM params[], void *data) |
377 | 0 | { |
378 | 0 | struct provider_ctx_data_st *pgd = data; |
379 | 0 | SSL_CTX *ctx = pgd->ctx; |
380 | 0 | OSSL_PROVIDER *provider = pgd->provider; |
381 | 0 | const OSSL_PARAM *p; |
382 | 0 | TLS_SIGALG_INFO *sinf = NULL; |
383 | 0 | EVP_KEYMGMT *keymgmt; |
384 | 0 | const char *keytype; |
385 | 0 | unsigned int code_point = 0; |
386 | 0 | int ret = 0; |
387 | |
|
388 | 0 | if (ctx->sigalg_list_max_len == ctx->sigalg_list_len) { |
389 | 0 | TLS_SIGALG_INFO *tmp = NULL; |
390 | |
|
391 | 0 | if (ctx->sigalg_list_max_len == 0) |
392 | 0 | tmp = OPENSSL_malloc(sizeof(TLS_SIGALG_INFO) |
393 | 0 | * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE); |
394 | 0 | else |
395 | 0 | tmp = OPENSSL_realloc(ctx->sigalg_list, |
396 | 0 | (ctx->sigalg_list_max_len |
397 | 0 | + TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE) |
398 | 0 | * sizeof(TLS_SIGALG_INFO)); |
399 | 0 | if (tmp == NULL) |
400 | 0 | return 0; |
401 | 0 | ctx->sigalg_list = tmp; |
402 | 0 | memset(tmp + ctx->sigalg_list_max_len, 0, |
403 | 0 | sizeof(TLS_SIGALG_INFO) * TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE); |
404 | 0 | ctx->sigalg_list_max_len += TLS_SIGALG_LIST_MALLOC_BLOCK_SIZE; |
405 | 0 | } |
406 | | |
407 | 0 | sinf = &ctx->sigalg_list[ctx->sigalg_list_len]; |
408 | | |
409 | | /* First, mandatory parameters */ |
410 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_NAME); |
411 | 0 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
412 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
413 | 0 | goto err; |
414 | 0 | } |
415 | 0 | OPENSSL_free(sinf->sigalg_name); |
416 | 0 | sinf->sigalg_name = OPENSSL_strdup(p->data); |
417 | 0 | if (sinf->sigalg_name == NULL) |
418 | 0 | goto err; |
419 | | |
420 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME); |
421 | 0 | if (p == NULL || p->data_type != OSSL_PARAM_UTF8_STRING) { |
422 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
423 | 0 | goto err; |
424 | 0 | } |
425 | 0 | OPENSSL_free(sinf->name); |
426 | 0 | sinf->name = OPENSSL_strdup(p->data); |
427 | 0 | if (sinf->name == NULL) |
428 | 0 | goto err; |
429 | | |
430 | 0 | p = OSSL_PARAM_locate_const(params, |
431 | 0 | OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT); |
432 | 0 | if (p == NULL |
433 | 0 | || !OSSL_PARAM_get_uint(p, &code_point) |
434 | 0 | || code_point > UINT16_MAX) { |
435 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
436 | 0 | goto err; |
437 | 0 | } |
438 | 0 | sinf->code_point = (uint16_t)code_point; |
439 | |
|
440 | 0 | p = OSSL_PARAM_locate_const(params, |
441 | 0 | OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS); |
442 | 0 | if (p == NULL || !OSSL_PARAM_get_uint(p, &sinf->secbits)) { |
443 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
444 | 0 | goto err; |
445 | 0 | } |
446 | | |
447 | | /* Now, optional parameters */ |
448 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_OID); |
449 | 0 | if (p == NULL) { |
450 | 0 | sinf->sigalg_oid = NULL; |
451 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
452 | 0 | goto err; |
453 | 0 | } else { |
454 | 0 | OPENSSL_free(sinf->sigalg_oid); |
455 | 0 | sinf->sigalg_oid = OPENSSL_strdup(p->data); |
456 | 0 | if (sinf->sigalg_oid == NULL) |
457 | 0 | goto err; |
458 | 0 | } |
459 | | |
460 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_NAME); |
461 | 0 | if (p == NULL) { |
462 | 0 | sinf->sig_name = NULL; |
463 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
464 | 0 | goto err; |
465 | 0 | } else { |
466 | 0 | OPENSSL_free(sinf->sig_name); |
467 | 0 | sinf->sig_name = OPENSSL_strdup(p->data); |
468 | 0 | if (sinf->sig_name == NULL) |
469 | 0 | goto err; |
470 | 0 | } |
471 | | |
472 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_SIG_OID); |
473 | 0 | if (p == NULL) { |
474 | 0 | sinf->sig_oid = NULL; |
475 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
476 | 0 | goto err; |
477 | 0 | } else { |
478 | 0 | OPENSSL_free(sinf->sig_oid); |
479 | 0 | sinf->sig_oid = OPENSSL_strdup(p->data); |
480 | 0 | if (sinf->sig_oid == NULL) |
481 | 0 | goto err; |
482 | 0 | } |
483 | | |
484 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME); |
485 | 0 | if (p == NULL) { |
486 | 0 | sinf->hash_name = NULL; |
487 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
488 | 0 | goto err; |
489 | 0 | } else { |
490 | 0 | OPENSSL_free(sinf->hash_name); |
491 | 0 | sinf->hash_name = OPENSSL_strdup(p->data); |
492 | 0 | if (sinf->hash_name == NULL) |
493 | 0 | goto err; |
494 | 0 | } |
495 | | |
496 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_HASH_OID); |
497 | 0 | if (p == NULL) { |
498 | 0 | sinf->hash_oid = NULL; |
499 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
500 | 0 | goto err; |
501 | 0 | } else { |
502 | 0 | OPENSSL_free(sinf->hash_oid); |
503 | 0 | sinf->hash_oid = OPENSSL_strdup(p->data); |
504 | 0 | if (sinf->hash_oid == NULL) |
505 | 0 | goto err; |
506 | 0 | } |
507 | | |
508 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE); |
509 | 0 | if (p == NULL) { |
510 | 0 | sinf->keytype = NULL; |
511 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
512 | 0 | goto err; |
513 | 0 | } else { |
514 | 0 | OPENSSL_free(sinf->keytype); |
515 | 0 | sinf->keytype = OPENSSL_strdup(p->data); |
516 | 0 | if (sinf->keytype == NULL) |
517 | 0 | goto err; |
518 | 0 | } |
519 | | |
520 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_KEYTYPE_OID); |
521 | 0 | if (p == NULL) { |
522 | 0 | sinf->keytype_oid = NULL; |
523 | 0 | } else if (p->data_type != OSSL_PARAM_UTF8_STRING) { |
524 | 0 | goto err; |
525 | 0 | } else { |
526 | 0 | OPENSSL_free(sinf->keytype_oid); |
527 | 0 | sinf->keytype_oid = OPENSSL_strdup(p->data); |
528 | 0 | if (sinf->keytype_oid == NULL) |
529 | 0 | goto err; |
530 | 0 | } |
531 | | |
532 | | /* Optional, not documented prior to 3.5 */ |
533 | 0 | sinf->mindtls = sinf->maxdtls = -1; |
534 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_DTLS); |
535 | 0 | if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->mindtls)) { |
536 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
537 | 0 | goto err; |
538 | 0 | } |
539 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_DTLS); |
540 | 0 | if (p != NULL && !OSSL_PARAM_get_int(p, &sinf->maxdtls)) { |
541 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
542 | 0 | goto err; |
543 | 0 | } |
544 | | /* DTLS version numbers grow downward */ |
545 | 0 | if ((sinf->maxdtls != 0) && (sinf->maxdtls != -1) && |
546 | 0 | ((sinf->maxdtls > sinf->mindtls))) { |
547 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
548 | 0 | goto err; |
549 | 0 | } |
550 | | /* No provider sigalgs are supported in DTLS, reset after checking. */ |
551 | 0 | sinf->mindtls = sinf->maxdtls = -1; |
552 | | |
553 | | /* The remaining parameters below are mandatory again */ |
554 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS); |
555 | 0 | if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->mintls)) { |
556 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
557 | 0 | goto err; |
558 | 0 | } |
559 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS); |
560 | 0 | if (p == NULL || !OSSL_PARAM_get_int(p, &sinf->maxtls)) { |
561 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
562 | 0 | goto err; |
563 | 0 | } |
564 | 0 | if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && |
565 | 0 | ((sinf->maxtls < sinf->mintls))) { |
566 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
567 | 0 | goto err; |
568 | 0 | } |
569 | 0 | if ((sinf->mintls != 0) && (sinf->mintls != -1) && |
570 | 0 | ((sinf->mintls > TLS1_3_VERSION))) |
571 | 0 | sinf->mintls = sinf->maxtls = -1; |
572 | 0 | if ((sinf->maxtls != 0) && (sinf->maxtls != -1) && |
573 | 0 | ((sinf->maxtls < TLS1_3_VERSION))) |
574 | 0 | sinf->mintls = sinf->maxtls = -1; |
575 | | |
576 | | /* Ignore unusable sigalgs */ |
577 | 0 | if (sinf->mintls == -1 && sinf->mindtls == -1) { |
578 | 0 | ret = 1; |
579 | 0 | goto err; |
580 | 0 | } |
581 | | |
582 | | /* |
583 | | * Now check that the algorithm is actually usable for our property query |
584 | | * string. Regardless of the result we still return success because we have |
585 | | * successfully processed this signature, even though we may decide not to |
586 | | * use it. |
587 | | */ |
588 | 0 | ret = 1; |
589 | 0 | ERR_set_mark(); |
590 | 0 | keytype = (sinf->keytype != NULL |
591 | 0 | ? sinf->keytype |
592 | 0 | : (sinf->sig_name != NULL |
593 | 0 | ? sinf->sig_name |
594 | 0 | : sinf->sigalg_name)); |
595 | 0 | keymgmt = EVP_KEYMGMT_fetch(ctx->libctx, keytype, ctx->propq); |
596 | 0 | if (keymgmt != NULL) { |
597 | | /* |
598 | | * We have successfully fetched the algorithm - however if the provider |
599 | | * doesn't match this one then we ignore it. |
600 | | * |
601 | | * Note: We're cheating a little here. Technically if the same algorithm |
602 | | * is available from more than one provider then it is undefined which |
603 | | * implementation you will get back. Theoretically this could be |
604 | | * different every time...we assume here that you'll always get the |
605 | | * same one back if you repeat the exact same fetch. Is this a reasonable |
606 | | * assumption to make (in which case perhaps we should document this |
607 | | * behaviour)? |
608 | | */ |
609 | 0 | if (EVP_KEYMGMT_get0_provider(keymgmt) == provider) { |
610 | | /* |
611 | | * We have a match - so we could use this signature; |
612 | | * Check proper object registration first, though. |
613 | | * Don't care about return value as this may have been |
614 | | * done within providers or previous calls to |
615 | | * add_provider_sigalgs. |
616 | | */ |
617 | 0 | OBJ_create(sinf->sigalg_oid, sinf->sigalg_name, NULL); |
618 | | /* sanity check: Without successful registration don't use alg */ |
619 | 0 | if ((OBJ_txt2nid(sinf->sigalg_name) == NID_undef) || |
620 | 0 | (OBJ_nid2obj(OBJ_txt2nid(sinf->sigalg_name)) == NULL)) { |
621 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT); |
622 | 0 | goto err; |
623 | 0 | } |
624 | 0 | if (sinf->sig_name != NULL) |
625 | 0 | OBJ_create(sinf->sig_oid, sinf->sig_name, NULL); |
626 | 0 | if (sinf->keytype != NULL) |
627 | 0 | OBJ_create(sinf->keytype_oid, sinf->keytype, NULL); |
628 | 0 | if (sinf->hash_name != NULL) |
629 | 0 | OBJ_create(sinf->hash_oid, sinf->hash_name, NULL); |
630 | 0 | OBJ_add_sigid(OBJ_txt2nid(sinf->sigalg_name), |
631 | 0 | (sinf->hash_name != NULL |
632 | 0 | ? OBJ_txt2nid(sinf->hash_name) |
633 | 0 | : NID_undef), |
634 | 0 | OBJ_txt2nid(keytype)); |
635 | 0 | ctx->sigalg_list_len++; |
636 | 0 | sinf = NULL; |
637 | 0 | } |
638 | 0 | EVP_KEYMGMT_free(keymgmt); |
639 | 0 | } |
640 | 0 | ERR_pop_to_mark(); |
641 | 0 | err: |
642 | 0 | if (sinf != NULL) { |
643 | 0 | OPENSSL_free(sinf->name); |
644 | 0 | sinf->name = NULL; |
645 | 0 | OPENSSL_free(sinf->sigalg_name); |
646 | 0 | sinf->sigalg_name = NULL; |
647 | 0 | OPENSSL_free(sinf->sigalg_oid); |
648 | 0 | sinf->sigalg_oid = NULL; |
649 | 0 | OPENSSL_free(sinf->sig_name); |
650 | 0 | sinf->sig_name = NULL; |
651 | 0 | OPENSSL_free(sinf->sig_oid); |
652 | 0 | sinf->sig_oid = NULL; |
653 | 0 | OPENSSL_free(sinf->hash_name); |
654 | 0 | sinf->hash_name = NULL; |
655 | 0 | OPENSSL_free(sinf->hash_oid); |
656 | 0 | sinf->hash_oid = NULL; |
657 | 0 | OPENSSL_free(sinf->keytype); |
658 | 0 | sinf->keytype = NULL; |
659 | 0 | OPENSSL_free(sinf->keytype_oid); |
660 | 0 | sinf->keytype_oid = NULL; |
661 | 0 | } |
662 | 0 | return ret; |
663 | 0 | } |
664 | | |
665 | | static int discover_provider_sigalgs(OSSL_PROVIDER *provider, void *vctx) |
666 | 0 | { |
667 | 0 | struct provider_ctx_data_st pgd; |
668 | |
|
669 | 0 | pgd.ctx = vctx; |
670 | 0 | pgd.provider = provider; |
671 | 0 | OSSL_PROVIDER_get_capabilities(provider, "TLS-SIGALG", |
672 | 0 | add_provider_sigalgs, &pgd); |
673 | | /* |
674 | | * Always OK, even if provider doesn't support the capability: |
675 | | * Reconsider testing retval when legacy sigalgs are also loaded this way. |
676 | | */ |
677 | 0 | return 1; |
678 | 0 | } |
679 | | |
680 | | int ssl_load_sigalgs(SSL_CTX *ctx) |
681 | 0 | { |
682 | 0 | size_t i; |
683 | 0 | SSL_CERT_LOOKUP lu; |
684 | |
|
685 | 0 | if (!OSSL_PROVIDER_do_all(ctx->libctx, discover_provider_sigalgs, ctx)) |
686 | 0 | return 0; |
687 | | |
688 | | /* now populate ctx->ssl_cert_info */ |
689 | 0 | if (ctx->sigalg_list_len > 0) { |
690 | 0 | OPENSSL_free(ctx->ssl_cert_info); |
691 | 0 | ctx->ssl_cert_info = OPENSSL_zalloc(sizeof(lu) * ctx->sigalg_list_len); |
692 | 0 | if (ctx->ssl_cert_info == NULL) |
693 | 0 | return 0; |
694 | 0 | for(i = 0; i < ctx->sigalg_list_len; i++) { |
695 | 0 | ctx->ssl_cert_info[i].nid = OBJ_txt2nid(ctx->sigalg_list[i].sigalg_name); |
696 | 0 | ctx->ssl_cert_info[i].amask = SSL_aANY; |
697 | 0 | } |
698 | 0 | } |
699 | | |
700 | | /* |
701 | | * For now, leave it at this: legacy sigalgs stay in their own |
702 | | * data structures until "legacy cleanup" occurs. |
703 | | */ |
704 | | |
705 | 0 | return 1; |
706 | 0 | } |
707 | | |
708 | | static uint16_t tls1_group_name2id(SSL_CTX *ctx, const char *name) |
709 | 0 | { |
710 | 0 | size_t i; |
711 | |
|
712 | 0 | for (i = 0; i < ctx->group_list_len; i++) { |
713 | 0 | if (OPENSSL_strcasecmp(ctx->group_list[i].tlsname, name) == 0 |
714 | 0 | || OPENSSL_strcasecmp(ctx->group_list[i].realname, name) == 0) |
715 | 0 | return ctx->group_list[i].group_id; |
716 | 0 | } |
717 | | |
718 | 0 | return 0; |
719 | 0 | } |
720 | | |
721 | | const TLS_GROUP_INFO *tls1_group_id_lookup(SSL_CTX *ctx, uint16_t group_id) |
722 | 0 | { |
723 | 0 | size_t i; |
724 | |
|
725 | 0 | for (i = 0; i < ctx->group_list_len; i++) { |
726 | 0 | if (ctx->group_list[i].group_id == group_id) |
727 | 0 | return &ctx->group_list[i]; |
728 | 0 | } |
729 | | |
730 | 0 | return NULL; |
731 | 0 | } |
732 | | |
733 | | const char *tls1_group_id2name(SSL_CTX *ctx, uint16_t group_id) |
734 | 0 | { |
735 | 0 | const TLS_GROUP_INFO *tls_group_info = tls1_group_id_lookup(ctx, group_id); |
736 | |
|
737 | 0 | if (tls_group_info == NULL) |
738 | 0 | return NULL; |
739 | | |
740 | 0 | return tls_group_info->tlsname; |
741 | 0 | } |
742 | | |
743 | | int tls1_group_id2nid(uint16_t group_id, int include_unknown) |
744 | 0 | { |
745 | 0 | size_t i; |
746 | |
|
747 | 0 | if (group_id == 0) |
748 | 0 | return NID_undef; |
749 | | |
750 | | /* |
751 | | * Return well known Group NIDs - for backwards compatibility. This won't |
752 | | * work for groups we don't know about. |
753 | | */ |
754 | 0 | for (i = 0; i < OSSL_NELEM(nid_to_group); i++) |
755 | 0 | { |
756 | 0 | if (nid_to_group[i].group_id == group_id) |
757 | 0 | return nid_to_group[i].nid; |
758 | 0 | } |
759 | 0 | if (!include_unknown) |
760 | 0 | return NID_undef; |
761 | 0 | return TLSEXT_nid_unknown | (int)group_id; |
762 | 0 | } |
763 | | |
764 | | uint16_t tls1_nid2group_id(int nid) |
765 | 0 | { |
766 | 0 | size_t i; |
767 | | |
768 | | /* |
769 | | * Return well known Group ids - for backwards compatibility. This won't |
770 | | * work for groups we don't know about. |
771 | | */ |
772 | 0 | for (i = 0; i < OSSL_NELEM(nid_to_group); i++) |
773 | 0 | { |
774 | 0 | if (nid_to_group[i].nid == nid) |
775 | 0 | return nid_to_group[i].group_id; |
776 | 0 | } |
777 | | |
778 | 0 | return 0; |
779 | 0 | } |
780 | | |
781 | | /* |
782 | | * Set *pgroups to the supported groups list and *pgroupslen to |
783 | | * the number of groups supported. |
784 | | */ |
785 | | void tls1_get_supported_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
786 | | size_t *pgroupslen) |
787 | 0 | { |
788 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
789 | | |
790 | | /* For Suite B mode only include P-256, P-384 */ |
791 | 0 | switch (tls1_suiteb(s)) { |
792 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
793 | 0 | *pgroups = suiteb_curves; |
794 | 0 | *pgroupslen = OSSL_NELEM(suiteb_curves); |
795 | 0 | break; |
796 | | |
797 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
798 | 0 | *pgroups = suiteb_curves; |
799 | 0 | *pgroupslen = 1; |
800 | 0 | break; |
801 | | |
802 | 0 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
803 | 0 | *pgroups = suiteb_curves + 1; |
804 | 0 | *pgroupslen = 1; |
805 | 0 | break; |
806 | | |
807 | 0 | default: |
808 | 0 | if (s->ext.supportedgroups == NULL) { |
809 | 0 | *pgroups = sctx->ext.supportedgroups; |
810 | 0 | *pgroupslen = sctx->ext.supportedgroups_len; |
811 | 0 | } else { |
812 | 0 | *pgroups = s->ext.supportedgroups; |
813 | 0 | *pgroupslen = s->ext.supportedgroups_len; |
814 | 0 | } |
815 | 0 | break; |
816 | 0 | } |
817 | 0 | } |
818 | | |
819 | | /* |
820 | | * Some comments for the function below: |
821 | | * s->ext.supportedgroups == NULL means legacy syntax (no [*,/,-]) from built-in group array. |
822 | | * In this case, we need to send exactly one key share, which MUST be the first (leftmost) |
823 | | * eligible group from the legacy list. Therefore, we provide the entire list of supported |
824 | | * groups in this case. |
825 | | * |
826 | | * A 'flag' to indicate legacy syntax is created by setting the number of key shares to 1, |
827 | | * but the groupID to 0. |
828 | | * The 'flag' is checked right at the beginning in tls_construct_ctos_key_share and either |
829 | | * the "list of requested key share groups" is used, or the "list of supported groups" in |
830 | | * combination with setting add_only_one = 1 is applied. |
831 | | */ |
832 | | void tls1_get_requested_keyshare_groups(SSL_CONNECTION *s, const uint16_t **pgroups, |
833 | | size_t *pgroupslen) |
834 | 0 | { |
835 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
836 | |
|
837 | 0 | if (s->ext.supportedgroups == NULL) { |
838 | 0 | *pgroups = sctx->ext.supportedgroups; |
839 | 0 | *pgroupslen = sctx->ext.supportedgroups_len; |
840 | 0 | } else { |
841 | 0 | *pgroups = s->ext.keyshares; |
842 | 0 | *pgroupslen = s->ext.keyshares_len; |
843 | 0 | } |
844 | 0 | } |
845 | | |
846 | | void tls1_get_group_tuples(SSL_CONNECTION *s, const size_t **ptuples, |
847 | | size_t *ptupleslen) |
848 | 0 | { |
849 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
850 | |
|
851 | 0 | if (s->ext.supportedgroups == NULL) { |
852 | 0 | *ptuples = sctx->ext.tuples; |
853 | 0 | *ptupleslen = sctx->ext.tuples_len; |
854 | 0 | } else { |
855 | 0 | *ptuples = s->ext.tuples; |
856 | 0 | *ptupleslen = s->ext.tuples_len; |
857 | 0 | } |
858 | 0 | } |
859 | | |
860 | | int tls_valid_group(SSL_CONNECTION *s, uint16_t group_id, |
861 | | int minversion, int maxversion, |
862 | | int isec, int *okfortls13) |
863 | 0 | { |
864 | 0 | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
865 | 0 | group_id); |
866 | 0 | int ret; |
867 | 0 | int group_minversion, group_maxversion; |
868 | |
|
869 | 0 | if (okfortls13 != NULL) |
870 | 0 | *okfortls13 = 0; |
871 | |
|
872 | 0 | if (ginfo == NULL) |
873 | 0 | return 0; |
874 | | |
875 | 0 | group_minversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->mindtls : ginfo->mintls; |
876 | 0 | group_maxversion = SSL_CONNECTION_IS_DTLS(s) ? ginfo->maxdtls : ginfo->maxtls; |
877 | |
|
878 | 0 | if (group_minversion < 0 || group_maxversion < 0) |
879 | 0 | return 0; |
880 | 0 | if (group_maxversion == 0) |
881 | 0 | ret = 1; |
882 | 0 | else |
883 | 0 | ret = (ssl_version_cmp(s, minversion, group_maxversion) <= 0); |
884 | 0 | if (group_minversion > 0) |
885 | 0 | ret &= (ssl_version_cmp(s, maxversion, group_minversion) >= 0); |
886 | |
|
887 | 0 | if (!SSL_CONNECTION_IS_DTLS(s)) { |
888 | 0 | if (ret && okfortls13 != NULL && maxversion == TLS1_3_VERSION) |
889 | 0 | *okfortls13 = (group_maxversion == 0) |
890 | 0 | || (group_maxversion >= TLS1_3_VERSION); |
891 | 0 | } |
892 | 0 | ret &= !isec |
893 | 0 | || strcmp(ginfo->algorithm, "EC") == 0 |
894 | 0 | || strcmp(ginfo->algorithm, "X25519") == 0 |
895 | 0 | || strcmp(ginfo->algorithm, "X448") == 0; |
896 | |
|
897 | 0 | return ret; |
898 | 0 | } |
899 | | |
900 | | /* See if group is allowed by security callback */ |
901 | | int tls_group_allowed(SSL_CONNECTION *s, uint16_t group, int op) |
902 | 0 | { |
903 | 0 | const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
904 | 0 | group); |
905 | 0 | unsigned char gtmp[2]; |
906 | |
|
907 | 0 | if (ginfo == NULL) |
908 | 0 | return 0; |
909 | | |
910 | 0 | gtmp[0] = group >> 8; |
911 | 0 | gtmp[1] = group & 0xff; |
912 | 0 | return ssl_security(s, op, ginfo->secbits, |
913 | 0 | tls1_group_id2nid(ginfo->group_id, 0), (void *)gtmp); |
914 | 0 | } |
915 | | |
916 | | /* Return 1 if "id" is in "list" */ |
917 | | static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen) |
918 | 0 | { |
919 | 0 | size_t i; |
920 | 0 | for (i = 0; i < listlen; i++) |
921 | 0 | if (list[i] == id) |
922 | 0 | return 1; |
923 | 0 | return 0; |
924 | 0 | } |
925 | | |
926 | | typedef struct { |
927 | | TLS_GROUP_INFO *grp; |
928 | | size_t ix; |
929 | | } TLS_GROUP_IX; |
930 | | |
931 | | DEFINE_STACK_OF(TLS_GROUP_IX) |
932 | | |
933 | | static void free_wrapper(TLS_GROUP_IX *a) |
934 | 0 | { |
935 | 0 | OPENSSL_free(a); |
936 | 0 | } |
937 | | |
938 | | static int tls_group_ix_cmp(const TLS_GROUP_IX *const *a, |
939 | | const TLS_GROUP_IX *const *b) |
940 | 0 | { |
941 | 0 | int idcmpab = (*a)->grp->group_id < (*b)->grp->group_id; |
942 | 0 | int idcmpba = (*b)->grp->group_id < (*a)->grp->group_id; |
943 | 0 | int ixcmpab = (*a)->ix < (*b)->ix; |
944 | 0 | int ixcmpba = (*b)->ix < (*a)->ix; |
945 | | |
946 | | /* Ascending by group id */ |
947 | 0 | if (idcmpab != idcmpba) |
948 | 0 | return (idcmpba - idcmpab); |
949 | | /* Ascending by original appearance index */ |
950 | 0 | return ixcmpba - ixcmpab; |
951 | 0 | } |
952 | | |
953 | | int tls1_get0_implemented_groups(int min_proto_version, int max_proto_version, |
954 | | TLS_GROUP_INFO *grps, size_t num, long all, |
955 | | STACK_OF(OPENSSL_CSTRING) *out) |
956 | 0 | { |
957 | 0 | STACK_OF(TLS_GROUP_IX) *collect = NULL; |
958 | 0 | TLS_GROUP_IX *gix; |
959 | 0 | uint16_t id = 0; |
960 | 0 | int ret = 0; |
961 | 0 | int ix; |
962 | |
|
963 | 0 | if (grps == NULL || out == NULL || num > INT_MAX) |
964 | 0 | return 0; |
965 | 0 | if ((collect = sk_TLS_GROUP_IX_new(tls_group_ix_cmp)) == NULL) |
966 | 0 | return 0; |
967 | 0 | for (ix = 0; ix < (int)num; ++ix, ++grps) { |
968 | 0 | if (grps->mintls > 0 && max_proto_version > 0 |
969 | 0 | && grps->mintls > max_proto_version) |
970 | 0 | continue; |
971 | 0 | if (grps->maxtls > 0 && min_proto_version > 0 |
972 | 0 | && grps->maxtls < min_proto_version) |
973 | 0 | continue; |
974 | | |
975 | 0 | if ((gix = OPENSSL_malloc(sizeof(*gix))) == NULL) |
976 | 0 | goto end; |
977 | 0 | gix->grp = grps; |
978 | 0 | gix->ix = ix; |
979 | 0 | if (sk_TLS_GROUP_IX_push(collect, gix) <= 0) { |
980 | 0 | OPENSSL_free(gix); |
981 | 0 | goto end; |
982 | 0 | } |
983 | 0 | } |
984 | | |
985 | 0 | sk_TLS_GROUP_IX_sort(collect); |
986 | 0 | num = sk_TLS_GROUP_IX_num(collect); |
987 | 0 | for (ix = 0; ix < (int)num; ++ix) { |
988 | 0 | gix = sk_TLS_GROUP_IX_value(collect, ix); |
989 | 0 | if (!all && gix->grp->group_id == id) |
990 | 0 | continue; |
991 | 0 | id = gix->grp->group_id; |
992 | 0 | if (sk_OPENSSL_CSTRING_push(out, gix->grp->tlsname) <= 0) |
993 | 0 | goto end; |
994 | 0 | } |
995 | 0 | ret = 1; |
996 | |
|
997 | 0 | end: |
998 | 0 | sk_TLS_GROUP_IX_pop_free(collect, free_wrapper); |
999 | 0 | return ret; |
1000 | 0 | } |
1001 | | |
1002 | | /*- |
1003 | | * For nmatch >= 0, return the id of the |nmatch|th shared group or 0 |
1004 | | * if there is no match. |
1005 | | * For nmatch == -1, return number of matches |
1006 | | * For nmatch == -2, return the id of the group to use for |
1007 | | * a tmp key, or 0 if there is no match. |
1008 | | */ |
1009 | | uint16_t tls1_shared_group(SSL_CONNECTION *s, int nmatch) |
1010 | 0 | { |
1011 | 0 | const uint16_t *pref, *supp; |
1012 | 0 | size_t num_pref, num_supp, i; |
1013 | 0 | int k; |
1014 | 0 | SSL_CTX *ctx = SSL_CONNECTION_GET_CTX(s); |
1015 | | |
1016 | | /* Can't do anything on client side */ |
1017 | 0 | if (s->server == 0) |
1018 | 0 | return 0; |
1019 | 0 | if (nmatch == -2) { |
1020 | 0 | if (tls1_suiteb(s)) { |
1021 | | /* |
1022 | | * For Suite B ciphersuite determines curve: we already know |
1023 | | * these are acceptable due to previous checks. |
1024 | | */ |
1025 | 0 | unsigned long cid = s->s3.tmp.new_cipher->id; |
1026 | |
|
1027 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
1028 | 0 | return OSSL_TLS_GROUP_ID_secp256r1; |
1029 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
1030 | 0 | return OSSL_TLS_GROUP_ID_secp384r1; |
1031 | | /* Should never happen */ |
1032 | 0 | return 0; |
1033 | 0 | } |
1034 | | /* If not Suite B just return first preference shared curve */ |
1035 | 0 | nmatch = 0; |
1036 | 0 | } |
1037 | | /* |
1038 | | * If server preference set, our groups are the preference order |
1039 | | * otherwise peer decides. |
1040 | | */ |
1041 | 0 | if (s->options & SSL_OP_SERVER_PREFERENCE) { |
1042 | 0 | tls1_get_supported_groups(s, &pref, &num_pref); |
1043 | 0 | tls1_get_peer_groups(s, &supp, &num_supp); |
1044 | 0 | } else { |
1045 | 0 | tls1_get_peer_groups(s, &pref, &num_pref); |
1046 | 0 | tls1_get_supported_groups(s, &supp, &num_supp); |
1047 | 0 | } |
1048 | |
|
1049 | 0 | for (k = 0, i = 0; i < num_pref; i++) { |
1050 | 0 | uint16_t id = pref[i]; |
1051 | 0 | const TLS_GROUP_INFO *inf; |
1052 | 0 | int minversion, maxversion; |
1053 | |
|
1054 | 0 | if (!tls1_in_list(id, supp, num_supp) |
1055 | 0 | || !tls_group_allowed(s, id, SSL_SECOP_CURVE_SHARED)) |
1056 | 0 | continue; |
1057 | 0 | inf = tls1_group_id_lookup(ctx, id); |
1058 | 0 | if (!ossl_assert(inf != NULL)) |
1059 | 0 | return 0; |
1060 | | |
1061 | 0 | minversion = SSL_CONNECTION_IS_DTLS(s) |
1062 | 0 | ? inf->mindtls : inf->mintls; |
1063 | 0 | maxversion = SSL_CONNECTION_IS_DTLS(s) |
1064 | 0 | ? inf->maxdtls : inf->maxtls; |
1065 | 0 | if (maxversion == -1) |
1066 | 0 | continue; |
1067 | 0 | if ((minversion != 0 && ssl_version_cmp(s, s->version, minversion) < 0) |
1068 | 0 | || (maxversion != 0 |
1069 | 0 | && ssl_version_cmp(s, s->version, maxversion) > 0)) |
1070 | 0 | continue; |
1071 | | |
1072 | 0 | if (nmatch == k) |
1073 | 0 | return id; |
1074 | 0 | k++; |
1075 | 0 | } |
1076 | 0 | if (nmatch == -1) |
1077 | 0 | return k; |
1078 | | /* Out of range (nmatch > k). */ |
1079 | 0 | return 0; |
1080 | 0 | } |
1081 | | |
1082 | | int tls1_set_groups(uint16_t **grpext, size_t *grpextlen, |
1083 | | uint16_t **ksext, size_t *ksextlen, |
1084 | | size_t **tplext, size_t *tplextlen, |
1085 | | int *groups, size_t ngroups) |
1086 | 0 | { |
1087 | 0 | uint16_t *glist = NULL, *kslist = NULL; |
1088 | 0 | size_t *tpllist = NULL; |
1089 | 0 | size_t i; |
1090 | | /* |
1091 | | * Bitmap of groups included to detect duplicates: two variables are added |
1092 | | * to detect duplicates as some values are more than 32. |
1093 | | */ |
1094 | 0 | unsigned long *dup_list = NULL; |
1095 | 0 | unsigned long dup_list_egrp = 0; |
1096 | 0 | unsigned long dup_list_dhgrp = 0; |
1097 | |
|
1098 | 0 | if (ngroups == 0) { |
1099 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH); |
1100 | 0 | return 0; |
1101 | 0 | } |
1102 | 0 | if ((glist = OPENSSL_malloc(ngroups * sizeof(*glist))) == NULL) |
1103 | 0 | goto err; |
1104 | 0 | if ((kslist = OPENSSL_malloc(1 * sizeof(*kslist))) == NULL) |
1105 | 0 | goto err; |
1106 | 0 | if ((tpllist = OPENSSL_malloc(1 * sizeof(*tpllist))) == NULL) |
1107 | 0 | goto err; |
1108 | 0 | for (i = 0; i < ngroups; i++) { |
1109 | 0 | unsigned long idmask; |
1110 | 0 | uint16_t id; |
1111 | 0 | id = tls1_nid2group_id(groups[i]); |
1112 | 0 | if ((id & 0x00FF) >= (sizeof(unsigned long) * 8)) |
1113 | 0 | goto err; |
1114 | 0 | idmask = 1L << (id & 0x00FF); |
1115 | 0 | dup_list = (id < 0x100) ? &dup_list_egrp : &dup_list_dhgrp; |
1116 | 0 | if (!id || ((*dup_list) & idmask)) |
1117 | 0 | goto err; |
1118 | 0 | *dup_list |= idmask; |
1119 | 0 | glist[i] = id; |
1120 | 0 | } |
1121 | 0 | OPENSSL_free(*grpext); |
1122 | 0 | OPENSSL_free(*ksext); |
1123 | 0 | OPENSSL_free(*tplext); |
1124 | 0 | *grpext = glist; |
1125 | 0 | *grpextlen = ngroups; |
1126 | 0 | kslist[0] = glist[0]; |
1127 | 0 | *ksext = kslist; |
1128 | 0 | *ksextlen = 1; |
1129 | 0 | tpllist[0] = ngroups; |
1130 | 0 | *tplext = tpllist; |
1131 | 0 | *tplextlen = 1; |
1132 | 0 | return 1; |
1133 | 0 | err: |
1134 | 0 | OPENSSL_free(glist); |
1135 | 0 | OPENSSL_free(kslist); |
1136 | 0 | OPENSSL_free(tpllist); |
1137 | 0 | return 0; |
1138 | 0 | } |
1139 | | |
1140 | | /* |
1141 | | * Definition of DEFAULT[_XYZ] pseudo group names. |
1142 | | * A pseudo group name is actually a full list of groups, including prefixes |
1143 | | * and or tuple delimiters. It can be hierarchically defined (for potential future use). |
1144 | | * IMPORTANT REMARK: For ease of use, in the built-in lists of groups, unknown groups or |
1145 | | * groups not backed by a provider will always silently be ignored, even without '?' prefix |
1146 | | */ |
1147 | | typedef struct { |
1148 | | const char *list_name; /* The name of this pseudo group */ |
1149 | | const char *group_string; /* The group string of this pseudo group */ |
1150 | | } default_group_string_st; /* (can include '?', '*'. '-', '/' as needed) */ |
1151 | | |
1152 | | /* Built-in pseudo group-names must start with a (D or d) */ |
1153 | | static const char *DEFAULT_GROUPNAME_FIRST_CHARACTER = "D"; |
1154 | | |
1155 | | /* The list of all built-in pseudo-group-name structures */ |
1156 | | static const default_group_string_st default_group_strings[] = { |
1157 | | {DEFAULT_GROUP_NAME, TLS_DEFAULT_GROUP_LIST}, |
1158 | | {SUITE_B_GROUP_NAME, SUITE_B_GROUP_LIST} |
1159 | | }; |
1160 | | |
1161 | | /* |
1162 | | * Some GOST names are not resolved by tls1_group_name2id, |
1163 | | * hence we'll check for those manually |
1164 | | */ |
1165 | | typedef struct { |
1166 | | const char *group_name; |
1167 | | uint16_t groupID; |
1168 | | } name2id_st; |
1169 | | static const name2id_st name2id_arr[] = { |
1170 | | {"GC256A", OSSL_TLS_GROUP_ID_gc256A }, |
1171 | | {"GC256B", OSSL_TLS_GROUP_ID_gc256B }, |
1172 | | {"GC256C", OSSL_TLS_GROUP_ID_gc256C }, |
1173 | | {"GC256D", OSSL_TLS_GROUP_ID_gc256D }, |
1174 | | {"GC512A", OSSL_TLS_GROUP_ID_gc512A }, |
1175 | | {"GC512B", OSSL_TLS_GROUP_ID_gc512B }, |
1176 | | {"GC512C", OSSL_TLS_GROUP_ID_gc512C }, |
1177 | | }; |
1178 | | |
1179 | | /* |
1180 | | * Group list management: |
1181 | | * We establish three lists along with their related size counters: |
1182 | | * 1) List of (unique) groups |
1183 | | * 2) List of number of groups per group-priority-tuple |
1184 | | * 3) List of (unique) key share groups |
1185 | | */ |
1186 | 0 | #define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (64 Bytes chunks ~= cache line) */ |
1187 | | #define GROUP_NAME_BUFFER_LENGTH 64 /* Max length of a group name */ |
1188 | | |
1189 | | /* |
1190 | | * Preparation of the prefix used to indicate the desire to send a key share, |
1191 | | * the characters used as separators between groups or tuples of groups, the |
1192 | | * character to indicate that an unknown group should be ignored, and the |
1193 | | * character to indicate that a group should be deleted from a list |
1194 | | */ |
1195 | | #ifndef TUPLE_DELIMITER_CHARACTER |
1196 | | /* The prefix characters to indicate group tuple boundaries */ |
1197 | 0 | # define TUPLE_DELIMITER_CHARACTER '/' |
1198 | | #endif |
1199 | | #ifndef GROUP_DELIMITER_CHARACTER |
1200 | | /* The prefix characters to indicate group tuple boundaries */ |
1201 | 0 | # define GROUP_DELIMITER_CHARACTER ':' |
1202 | | #endif |
1203 | | #ifndef IGNORE_UNKNOWN_GROUP_CHARACTER |
1204 | | /* The prefix character to ignore unknown groups */ |
1205 | 0 | # define IGNORE_UNKNOWN_GROUP_CHARACTER '?' |
1206 | | #endif |
1207 | | #ifndef KEY_SHARE_INDICATOR_CHARACTER |
1208 | | /* The prefix character to trigger a key share addition */ |
1209 | 0 | # define KEY_SHARE_INDICATOR_CHARACTER '*' |
1210 | | #endif |
1211 | | #ifndef REMOVE_GROUP_INDICATOR_CHARACTER |
1212 | | /* The prefix character to trigger a key share removal */ |
1213 | 0 | # define REMOVE_GROUP_INDICATOR_CHARACTER '-' |
1214 | | #endif |
1215 | | static const char prefixes[] = {TUPLE_DELIMITER_CHARACTER, |
1216 | | GROUP_DELIMITER_CHARACTER, |
1217 | | IGNORE_UNKNOWN_GROUP_CHARACTER, |
1218 | | KEY_SHARE_INDICATOR_CHARACTER, |
1219 | | REMOVE_GROUP_INDICATOR_CHARACTER, |
1220 | | '\0'}; |
1221 | | |
1222 | | /* |
1223 | | * High-level description of how group strings are analyzed: |
1224 | | * A first call back function (tuple_cb) is used to process group tuples, and a |
1225 | | * second callback function (gid_cb) is used to process the groups inside a tuple. |
1226 | | * Those callback functions are (indirectly) called by CONF_parse_list with |
1227 | | * different separators (nominally ':' or '/'), a variable based on gid_cb_st |
1228 | | * is used to keep track of the parsing results between the various calls |
1229 | | */ |
1230 | | |
1231 | | typedef struct { |
1232 | | SSL_CTX *ctx; |
1233 | | /* Variables to hold the three lists (groups, requested keyshares, tuple structure) */ |
1234 | | size_t gidmax; /* The memory allocation chunk size for the group IDs */ |
1235 | | size_t gidcnt; /* Number of groups */ |
1236 | | uint16_t *gid_arr; /* The IDs of the supported groups (flat list) */ |
1237 | | size_t tplmax; /* The memory allocation chunk size for the tuple counters */ |
1238 | | size_t tplcnt; /* Number of tuples */ |
1239 | | size_t *tuplcnt_arr; /* The number of groups inside a tuple */ |
1240 | | size_t ksidmax; /* The memory allocation chunk size */ |
1241 | | size_t ksidcnt; /* Number of key shares */ |
1242 | | uint16_t *ksid_arr; /* The IDs of the key share groups (flat list) */ |
1243 | | /* Variable to keep state between execution of callback or helper functions */ |
1244 | | size_t tuple_mode; /* Keeps track whether tuple_cb called from 'the top' or from gid_cb */ |
1245 | | int ignore_unknown_default; /* Flag such that unknown groups for DEFAULT[_XYZ] are ignored */ |
1246 | | } gid_cb_st; |
1247 | | |
1248 | | /* Forward declaration of tuple callback function */ |
1249 | | static int tuple_cb(const char *tuple, int len, void *arg); |
1250 | | |
1251 | | /* |
1252 | | * Extract and process the individual groups (and their prefixes if present) |
1253 | | * present in a tuple. Note: The argument 'elem' is a NON-\0-terminated string |
1254 | | * and must be appended by a \0 if used as \0-terminated string |
1255 | | */ |
1256 | | static int gid_cb(const char *elem, int len, void *arg) |
1257 | 0 | { |
1258 | 0 | gid_cb_st *garg = arg; |
1259 | 0 | size_t i, j, k; |
1260 | 0 | uint16_t gid = 0; |
1261 | 0 | int found_group = 0; |
1262 | 0 | char etmp[GROUP_NAME_BUFFER_LENGTH]; |
1263 | 0 | int retval = 1; /* We assume success */ |
1264 | 0 | char *current_prefix; |
1265 | 0 | int ignore_unknown = 0; |
1266 | 0 | int add_keyshare = 0; |
1267 | 0 | int remove_group = 0; |
1268 | 0 | size_t restored_prefix_index = 0; |
1269 | 0 | char *restored_default_group_string; |
1270 | 0 | int continue_while_loop = 1; |
1271 | | |
1272 | | /* Sanity checks */ |
1273 | 0 | if (garg == NULL || elem == NULL || len <= 0) { |
1274 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
1275 | 0 | return 0; |
1276 | 0 | } |
1277 | | |
1278 | | /* Check the possible prefixes (remark: Leading and trailing spaces already cleared) */ |
1279 | 0 | while (continue_while_loop && len > 0 |
1280 | 0 | && ((current_prefix = strchr(prefixes, elem[0])) != NULL |
1281 | 0 | || OPENSSL_strncasecmp(current_prefix = (char *)DEFAULT_GROUPNAME_FIRST_CHARACTER, elem, 1) == 0)) { |
1282 | |
|
1283 | 0 | switch (*current_prefix) { |
1284 | 0 | case TUPLE_DELIMITER_CHARACTER: |
1285 | | /* tuple delimiter not allowed here -> syntax error */ |
1286 | 0 | return -1; |
1287 | 0 | break; |
1288 | 0 | case GROUP_DELIMITER_CHARACTER: |
1289 | 0 | return -1; /* Not a valid prefix for a single group name-> syntax error */ |
1290 | 0 | break; |
1291 | 0 | case KEY_SHARE_INDICATOR_CHARACTER: |
1292 | 0 | if (add_keyshare) |
1293 | 0 | return -1; /* Only single key share prefix allowed -> syntax error */ |
1294 | 0 | add_keyshare = 1; |
1295 | 0 | ++elem; |
1296 | 0 | --len; |
1297 | 0 | break; |
1298 | 0 | case REMOVE_GROUP_INDICATOR_CHARACTER: |
1299 | 0 | if (remove_group) |
1300 | 0 | return -1; /* Only single remove group prefix allowed -> syntax error */ |
1301 | 0 | remove_group = 1; |
1302 | 0 | ++elem; |
1303 | 0 | --len; |
1304 | 0 | break; |
1305 | 0 | case IGNORE_UNKNOWN_GROUP_CHARACTER: |
1306 | 0 | if (ignore_unknown) |
1307 | 0 | return -1; /* Only single ? allowed -> syntax error */ |
1308 | 0 | ignore_unknown = 1; |
1309 | 0 | ++elem; |
1310 | 0 | --len; |
1311 | 0 | break; |
1312 | 0 | default: |
1313 | | /* |
1314 | | * Check whether a DEFAULT[_XYZ] 'pseudo group' (= a built-in |
1315 | | * list of groups) should be added |
1316 | | */ |
1317 | 0 | for (i = 0; i < OSSL_NELEM(default_group_strings); i++) { |
1318 | 0 | if ((size_t)len == (strlen(default_group_strings[i].list_name)) |
1319 | 0 | && OPENSSL_strncasecmp(default_group_strings[i].list_name, elem, len) == 0) { |
1320 | | /* |
1321 | | * We're asked to insert an entire list of groups from a |
1322 | | * DEFAULT[_XYZ] 'pseudo group' which we do by |
1323 | | * recursively calling this function (indirectly via |
1324 | | * CONF_parse_list and tuple_cb); essentially, we treat a DEFAULT |
1325 | | * group string like a tuple which is appended to the current tuple |
1326 | | * rather then starting a new tuple. Variable tuple_mode is the flag which |
1327 | | * controls append tuple vs start new tuple. |
1328 | | */ |
1329 | |
|
1330 | 0 | if (ignore_unknown || remove_group) |
1331 | 0 | return -1; /* removal or ignore not allowed here -> syntax error */ |
1332 | | |
1333 | | /* |
1334 | | * First, we restore any keyshare prefix in a new zero-terminated string |
1335 | | * (if not already present) |
1336 | | */ |
1337 | 0 | restored_default_group_string = OPENSSL_malloc((1 /* max prefix length */ + |
1338 | 0 | strlen(default_group_strings[i].group_string) + |
1339 | 0 | 1 /* \0 */) * sizeof(char)); |
1340 | 0 | if (restored_default_group_string == NULL) |
1341 | 0 | return 0; |
1342 | 0 | if (add_keyshare |
1343 | | /* Remark: we tolerate a duplicated keyshare indicator here */ |
1344 | 0 | && default_group_strings[i].group_string[0] |
1345 | 0 | != KEY_SHARE_INDICATOR_CHARACTER) |
1346 | 0 | restored_default_group_string[restored_prefix_index++] = |
1347 | 0 | KEY_SHARE_INDICATOR_CHARACTER; |
1348 | |
|
1349 | 0 | memcpy(restored_default_group_string + restored_prefix_index, |
1350 | 0 | default_group_strings[i].group_string, |
1351 | 0 | strlen(default_group_strings[i].group_string)); |
1352 | 0 | restored_default_group_string[strlen(default_group_strings[i].group_string) + |
1353 | 0 | restored_prefix_index] = '\0'; |
1354 | | /* We execute the recursive call */ |
1355 | 0 | garg->ignore_unknown_default = 1; /* We ignore unknown groups for DEFAULT_XYZ */ |
1356 | | /* we enforce group mode (= append tuple) for DEFAULT_XYZ group lists */ |
1357 | 0 | garg->tuple_mode = 0; |
1358 | | /* We use the tuple_cb callback to process the pseudo group tuple */ |
1359 | 0 | retval = CONF_parse_list(restored_default_group_string, |
1360 | 0 | TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, garg); |
1361 | 0 | garg->tuple_mode = 1; /* next call to tuple_cb will again start new tuple */ |
1362 | 0 | garg->ignore_unknown_default = 0; /* reset to original value */ |
1363 | | /* We don't need the \0-terminated string anymore */ |
1364 | 0 | OPENSSL_free(restored_default_group_string); |
1365 | |
|
1366 | 0 | return retval; |
1367 | 0 | } |
1368 | 0 | } |
1369 | | /* |
1370 | | * If we reached this point, a group name started with a 'd' or 'D', but no request |
1371 | | * for a DEFAULT[_XYZ] 'pseudo group' was detected, hence processing of the group |
1372 | | * name can continue as usual (= the while loop checking prefixes can end) |
1373 | | */ |
1374 | 0 | continue_while_loop = 0; |
1375 | 0 | break; |
1376 | 0 | } |
1377 | 0 | } |
1378 | | |
1379 | 0 | if (len == 0) |
1380 | 0 | return -1; /* Seems we have prefxes without a group name -> syntax error */ |
1381 | | |
1382 | 0 | if (garg->ignore_unknown_default == 1) /* Always ignore unknown groups for DEFAULT[_XYZ] */ |
1383 | 0 | ignore_unknown = 1; |
1384 | | |
1385 | | /* Memory management in case more groups are present compared to initial allocation */ |
1386 | 0 | if (garg->gidcnt == garg->gidmax) { |
1387 | 0 | uint16_t *tmp = |
1388 | 0 | OPENSSL_realloc(garg->gid_arr, |
1389 | 0 | (garg->gidmax + GROUPLIST_INCREMENT) * sizeof(*garg->gid_arr)); |
1390 | |
|
1391 | 0 | if (tmp == NULL) |
1392 | 0 | return 0; |
1393 | | |
1394 | 0 | garg->gidmax += GROUPLIST_INCREMENT; |
1395 | 0 | garg->gid_arr = tmp; |
1396 | 0 | } |
1397 | | /* Memory management for key share groups */ |
1398 | 0 | if (garg->ksidcnt == garg->ksidmax) { |
1399 | 0 | uint16_t *tmp = |
1400 | 0 | OPENSSL_realloc(garg->ksid_arr, |
1401 | 0 | (garg->ksidmax + GROUPLIST_INCREMENT) * sizeof(*garg->ksid_arr)); |
1402 | |
|
1403 | 0 | if (tmp == NULL) |
1404 | 0 | return 0; |
1405 | 0 | garg->ksidmax += GROUPLIST_INCREMENT; |
1406 | 0 | garg->ksid_arr = tmp; |
1407 | 0 | } |
1408 | | |
1409 | 0 | if (len > (int)(sizeof(etmp) - 1)) |
1410 | 0 | return -1; /* group name to long -> syntax error */ |
1411 | | |
1412 | | /* |
1413 | | * Prepare addition or removal of a single group by converting |
1414 | | * a group name into its groupID equivalent |
1415 | | */ |
1416 | | |
1417 | | /* Create a \0-terminated string and get the gid for this group if possible */ |
1418 | 0 | memcpy(etmp, elem, len); |
1419 | 0 | etmp[len] = 0; |
1420 | | |
1421 | | /* Get the groupID */ |
1422 | 0 | gid = tls1_group_name2id(garg->ctx, etmp); |
1423 | | /* |
1424 | | * Handle the case where no valid groupID was returned |
1425 | | * e.g. for an unknown group, which we'd ignore (only) if relevant prefix was set |
1426 | | */ |
1427 | 0 | if (gid == 0) { |
1428 | | /* Is it one of the GOST groups ? */ |
1429 | 0 | for (i = 0; i < OSSL_NELEM(name2id_arr); i++) { |
1430 | 0 | if (OPENSSL_strcasecmp(etmp, name2id_arr[i].group_name) == 0) { |
1431 | 0 | gid = name2id_arr[i].groupID; |
1432 | 0 | break; |
1433 | 0 | } |
1434 | 0 | } |
1435 | 0 | if (gid == 0) { /* still not found */ |
1436 | | /* Unknown group - ignore if ignore_unknown; trigger error otherwise */ |
1437 | 0 | retval = ignore_unknown; |
1438 | 0 | goto done; |
1439 | 0 | } |
1440 | 0 | } |
1441 | | |
1442 | | /* Make sure that at least one provider is supporting this groupID */ |
1443 | 0 | found_group = 0; |
1444 | 0 | for (j = 0; j < garg->ctx->group_list_len; j++) |
1445 | 0 | if (garg->ctx->group_list[j].group_id == gid) { |
1446 | 0 | found_group = 1; |
1447 | 0 | break; |
1448 | 0 | } |
1449 | | |
1450 | | /* |
1451 | | * No provider supports this group - ignore if |
1452 | | * ignore_unknown; trigger error otherwise |
1453 | | */ |
1454 | 0 | if (found_group == 0) { |
1455 | 0 | retval = ignore_unknown; |
1456 | 0 | goto done; |
1457 | 0 | } |
1458 | | /* Remove group (and keyshare) from anywhere in the list if present, ignore if not present */ |
1459 | 0 | if (remove_group) { |
1460 | | /* Is the current group specified anywhere in the entire list so far? */ |
1461 | 0 | found_group = 0; |
1462 | 0 | for (i = 0; i < garg->gidcnt; i++) |
1463 | 0 | if (garg->gid_arr[i] == gid) { |
1464 | 0 | found_group = 1; |
1465 | 0 | break; |
1466 | 0 | } |
1467 | | /* The group to remove is at position i in the list of (zero indexed) groups */ |
1468 | 0 | if (found_group) { |
1469 | | /* We remove that group from its position (which is at i)... */ |
1470 | 0 | for (j = i; j < (garg->gidcnt - 1); j++) |
1471 | 0 | garg->gid_arr[j] = garg->gid_arr[j + 1]; /* ...shift remaining groups left ... */ |
1472 | 0 | garg->gidcnt--; /* ..and update the book keeping for the number of groups */ |
1473 | | |
1474 | | /* |
1475 | | * We also must update the number of groups either in a previous tuple (which we |
1476 | | * must identify and check whether it becomes empty due to the deletion) or in |
1477 | | * the current tuple, pending where the deleted group resides |
1478 | | */ |
1479 | 0 | k = 0; |
1480 | 0 | for (j = 0; j < garg->tplcnt; j++) { |
1481 | 0 | k += garg->tuplcnt_arr[j]; |
1482 | | /* Remark: i is zero-indexed, k is one-indexed */ |
1483 | 0 | if (k > i) { /* remove from one of the previous tuples */ |
1484 | 0 | garg->tuplcnt_arr[j]--; |
1485 | 0 | break; /* We took care not to have group duplicates, hence we can stop here */ |
1486 | 0 | } |
1487 | 0 | } |
1488 | 0 | if (k <= i) /* remove from current tuple */ |
1489 | 0 | garg->tuplcnt_arr[j]--; |
1490 | | |
1491 | | /* We also remove the group from the list of keyshares (if present) */ |
1492 | 0 | found_group = 0; |
1493 | 0 | for (i = 0; i < garg->ksidcnt; i++) |
1494 | 0 | if (garg->ksid_arr[i] == gid) { |
1495 | 0 | found_group = 1; |
1496 | 0 | break; |
1497 | 0 | } |
1498 | 0 | if (found_group) { |
1499 | | /* Found, hence we remove that keyshare from its position (which is at i)... */ |
1500 | 0 | for (j = i; j < (garg->ksidcnt - 1); j++) |
1501 | 0 | garg->ksid_arr[j] = garg->ksid_arr[j + 1]; /* shift remaining key shares */ |
1502 | | /* ... and update the book keeping */ |
1503 | 0 | garg->ksidcnt--; |
1504 | 0 | } |
1505 | 0 | } |
1506 | 0 | } else { /* Processing addition of a single new group */ |
1507 | | |
1508 | | /* Check for duplicates */ |
1509 | 0 | for (i = 0; i < garg->gidcnt; i++) |
1510 | 0 | if (garg->gid_arr[i] == gid) { |
1511 | | /* Duplicate group anywhere in the list of groups - ignore */ |
1512 | 0 | goto done; |
1513 | 0 | } |
1514 | | |
1515 | | /* Add the current group to the 'flat' list of groups */ |
1516 | 0 | garg->gid_arr[garg->gidcnt++] = gid; |
1517 | | /* and update the book keeping for the number of groups in current tuple */ |
1518 | 0 | garg->tuplcnt_arr[garg->tplcnt]++; |
1519 | | |
1520 | | /* We memorize if needed that we want to add a key share for the current group */ |
1521 | 0 | if (add_keyshare) |
1522 | 0 | garg->ksid_arr[garg->ksidcnt++] = gid; |
1523 | 0 | } |
1524 | | |
1525 | 0 | done: |
1526 | 0 | return retval; |
1527 | 0 | } |
1528 | | |
1529 | | /* Extract and process a tuple of groups */ |
1530 | | static int tuple_cb(const char *tuple, int len, void *arg) |
1531 | 0 | { |
1532 | 0 | gid_cb_st *garg = arg; |
1533 | 0 | int retval = 1; /* We assume success */ |
1534 | 0 | char *restored_tuple_string; |
1535 | | |
1536 | | /* Sanity checks */ |
1537 | 0 | if (garg == NULL || tuple == NULL || len <= 0) { |
1538 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_CONFIG_VALUE); |
1539 | 0 | return 0; |
1540 | 0 | } |
1541 | | |
1542 | | /* Memory management for tuples */ |
1543 | 0 | if (garg->tplcnt == garg->tplmax) { |
1544 | 0 | size_t *tmp = |
1545 | 0 | OPENSSL_realloc(garg->tuplcnt_arr, |
1546 | 0 | (garg->tplmax + GROUPLIST_INCREMENT) * sizeof(*garg->tuplcnt_arr)); |
1547 | |
|
1548 | 0 | if (tmp == NULL) |
1549 | 0 | return 0; |
1550 | 0 | garg->tplmax += GROUPLIST_INCREMENT; |
1551 | 0 | garg->tuplcnt_arr = tmp; |
1552 | 0 | } |
1553 | | |
1554 | | /* Convert to \0-terminated string */ |
1555 | 0 | restored_tuple_string = OPENSSL_malloc((len + 1 /* \0 */) * sizeof(char)); |
1556 | 0 | if (restored_tuple_string == NULL) |
1557 | 0 | return 0; |
1558 | 0 | memcpy(restored_tuple_string, tuple, len); |
1559 | 0 | restored_tuple_string[len] = '\0'; |
1560 | | |
1561 | | /* Analyze group list of this tuple */ |
1562 | 0 | retval = CONF_parse_list(restored_tuple_string, GROUP_DELIMITER_CHARACTER, 1, gid_cb, arg); |
1563 | | |
1564 | | /* We don't need the \o-terminated string anymore */ |
1565 | 0 | OPENSSL_free(restored_tuple_string); |
1566 | |
|
1567 | 0 | if (garg->tuplcnt_arr[garg->tplcnt] > 0) { /* Some valid groups are present in current tuple... */ |
1568 | 0 | if (garg->tuple_mode) { |
1569 | | /* We 'close' the tuple */ |
1570 | 0 | garg->tplcnt++; |
1571 | 0 | garg->tuplcnt_arr[garg->tplcnt] = 0; /* Next tuple is initialized to be empty */ |
1572 | 0 | garg->tuple_mode = 1; /* next call will start a tuple (unless overridden in gid_cb) */ |
1573 | 0 | } |
1574 | 0 | } |
1575 | |
|
1576 | 0 | return retval; |
1577 | 0 | } |
1578 | | |
1579 | | /* |
1580 | | * Set groups and prepare generation of keyshares based on a string of groupnames, |
1581 | | * names separated by the group or the tuple delimiter, with per-group prefixes to |
1582 | | * (1) add a key share for this group, (2) ignore the group if unkown to the current |
1583 | | * context, (3) delete a previous occurrence of the group in the current tuple. |
1584 | | * |
1585 | | * The list parsing is done in two hierachical steps: The top-level step extracts the |
1586 | | * string of a tuple using tuple_cb, while the next lower step uses gid_cb to |
1587 | | * parse and process the groups inside a tuple |
1588 | | */ |
1589 | | int tls1_set_groups_list(SSL_CTX *ctx, |
1590 | | uint16_t **grpext, size_t *grpextlen, |
1591 | | uint16_t **ksext, size_t *ksextlen, |
1592 | | size_t **tplext, size_t *tplextlen, |
1593 | | const char *str) |
1594 | 0 | { |
1595 | 0 | size_t i = 0, j; |
1596 | 0 | int ret = 0, parse_ret = 0; |
1597 | 0 | gid_cb_st gcb; |
1598 | | |
1599 | | /* Sanity check */ |
1600 | 0 | if (ctx == NULL) { |
1601 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER); |
1602 | 0 | return 0; |
1603 | 0 | } |
1604 | | |
1605 | 0 | memset(&gcb, 0, sizeof(gcb)); |
1606 | 0 | gcb.tuple_mode = 1; /* We prepare to collect the first tuple */ |
1607 | 0 | gcb.ignore_unknown_default = 0; |
1608 | 0 | gcb.gidmax = GROUPLIST_INCREMENT; |
1609 | 0 | gcb.tplmax = GROUPLIST_INCREMENT; |
1610 | 0 | gcb.ksidmax = GROUPLIST_INCREMENT; |
1611 | 0 | gcb.ctx = ctx; |
1612 | | |
1613 | | /* Prepare initial chunks of memory for groups, tuples and keyshares groupIDs */ |
1614 | 0 | gcb.gid_arr = OPENSSL_malloc(gcb.gidmax * sizeof(*gcb.gid_arr)); |
1615 | 0 | if (gcb.gid_arr == NULL) |
1616 | 0 | goto end; |
1617 | 0 | gcb.tuplcnt_arr = OPENSSL_malloc(gcb.tplmax * sizeof(*gcb.tuplcnt_arr)); |
1618 | 0 | if (gcb.tuplcnt_arr == NULL) |
1619 | 0 | goto end; |
1620 | 0 | gcb.tuplcnt_arr[0] = 0; |
1621 | 0 | gcb.ksid_arr = OPENSSL_malloc(gcb.ksidmax * sizeof(*gcb.ksid_arr)); |
1622 | 0 | if (gcb.ksid_arr == NULL) |
1623 | 0 | goto end; |
1624 | | |
1625 | 0 | while (str[0] != '\0' && isspace((unsigned char)*str)) |
1626 | 0 | str++; |
1627 | 0 | if (str[0] == '\0') |
1628 | 0 | goto empty_list; |
1629 | | |
1630 | | /* |
1631 | | * Start the (potentially recursive) tuple processing by calling CONF_parse_list |
1632 | | * with the TUPLE_DELIMITER_CHARACTER (which will call tuple_cb after cleaning spaces) |
1633 | | */ |
1634 | 0 | parse_ret = CONF_parse_list(str, TUPLE_DELIMITER_CHARACTER, 1, tuple_cb, &gcb); |
1635 | |
|
1636 | 0 | if (parse_ret == 0) |
1637 | 0 | goto end; |
1638 | 0 | if (parse_ret == -1) { |
1639 | 0 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
1640 | 0 | "Syntax error in '%s'", str); |
1641 | 0 | goto end; |
1642 | 0 | } |
1643 | | |
1644 | | /* |
1645 | | * We check whether a tuple was completly emptied by using "-" prefix |
1646 | | * excessively, in which case we remove the tuple |
1647 | | */ |
1648 | 0 | for (i = j = 0; j < gcb.tplcnt; j++) { |
1649 | 0 | if (gcb.tuplcnt_arr[j] == 0) |
1650 | 0 | continue; |
1651 | | /* If there's a gap, move to first unfilled slot */ |
1652 | 0 | if (j == i) |
1653 | 0 | ++i; |
1654 | 0 | else |
1655 | 0 | gcb.tuplcnt_arr[i++] = gcb.tuplcnt_arr[j]; |
1656 | 0 | } |
1657 | 0 | gcb.tplcnt = i; |
1658 | |
|
1659 | 0 | if (gcb.ksidcnt > OPENSSL_CLIENT_MAX_KEY_SHARES) { |
1660 | 0 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
1661 | 0 | "To many keyshares requested in '%s' (max = %d)", |
1662 | 0 | str, OPENSSL_CLIENT_MAX_KEY_SHARES); |
1663 | 0 | goto end; |
1664 | 0 | } |
1665 | | |
1666 | | /* |
1667 | | * For backward compatibility we let the rest of the code know that a key share |
1668 | | * for the first valid group should be added if no "*" prefix was used anywhere |
1669 | | */ |
1670 | 0 | if (gcb.gidcnt > 0 && gcb.ksidcnt == 0) { |
1671 | | /* |
1672 | | * No key share group prefix character was used, hence we indicate that a single |
1673 | | * key share should be sent and flag that it should come from the supported_groups list |
1674 | | */ |
1675 | 0 | gcb.ksidcnt = 1; |
1676 | 0 | gcb.ksid_arr[0] = 0; |
1677 | 0 | } |
1678 | |
|
1679 | 0 | empty_list: |
1680 | | /* |
1681 | | * A call to tls1_set_groups_list with any of the args (other than ctx) set |
1682 | | * to NULL only does a syntax check, hence we're done here and report success |
1683 | | */ |
1684 | 0 | if (grpext == NULL || ksext == NULL || tplext == NULL || |
1685 | 0 | grpextlen == NULL || ksextlen == NULL || tplextlen == NULL) { |
1686 | 0 | ret = 1; |
1687 | 0 | goto end; |
1688 | 0 | } |
1689 | | |
1690 | | /* |
1691 | | * tuple_cb and gid_cb combo ensures there are no duplicates or unknown groups so we |
1692 | | * can just go ahead and set the results (after diposing the existing) |
1693 | | */ |
1694 | 0 | OPENSSL_free(*grpext); |
1695 | 0 | *grpext = gcb.gid_arr; |
1696 | 0 | *grpextlen = gcb.gidcnt; |
1697 | 0 | OPENSSL_free(*ksext); |
1698 | 0 | *ksext = gcb.ksid_arr; |
1699 | 0 | *ksextlen = gcb.ksidcnt; |
1700 | 0 | OPENSSL_free(*tplext); |
1701 | 0 | *tplext = gcb.tuplcnt_arr; |
1702 | 0 | *tplextlen = gcb.tplcnt; |
1703 | |
|
1704 | 0 | return 1; |
1705 | | |
1706 | 0 | end: |
1707 | 0 | OPENSSL_free(gcb.gid_arr); |
1708 | 0 | OPENSSL_free(gcb.tuplcnt_arr); |
1709 | 0 | OPENSSL_free(gcb.ksid_arr); |
1710 | 0 | return ret; |
1711 | 0 | } |
1712 | | |
1713 | | /* Check a group id matches preferences */ |
1714 | | int tls1_check_group_id(SSL_CONNECTION *s, uint16_t group_id, |
1715 | | int check_own_groups) |
1716 | 0 | { |
1717 | 0 | const uint16_t *groups; |
1718 | 0 | size_t groups_len; |
1719 | |
|
1720 | 0 | if (group_id == 0) |
1721 | 0 | return 0; |
1722 | | |
1723 | | /* Check for Suite B compliance */ |
1724 | 0 | if (tls1_suiteb(s) && s->s3.tmp.new_cipher != NULL) { |
1725 | 0 | unsigned long cid = s->s3.tmp.new_cipher->id; |
1726 | |
|
1727 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { |
1728 | 0 | if (group_id != OSSL_TLS_GROUP_ID_secp256r1) |
1729 | 0 | return 0; |
1730 | 0 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { |
1731 | 0 | if (group_id != OSSL_TLS_GROUP_ID_secp384r1) |
1732 | 0 | return 0; |
1733 | 0 | } else { |
1734 | | /* Should never happen */ |
1735 | 0 | return 0; |
1736 | 0 | } |
1737 | 0 | } |
1738 | | |
1739 | 0 | if (check_own_groups) { |
1740 | | /* Check group is one of our preferences */ |
1741 | 0 | tls1_get_supported_groups(s, &groups, &groups_len); |
1742 | 0 | if (!tls1_in_list(group_id, groups, groups_len)) |
1743 | 0 | return 0; |
1744 | 0 | } |
1745 | | |
1746 | 0 | if (!tls_group_allowed(s, group_id, SSL_SECOP_CURVE_CHECK)) |
1747 | 0 | return 0; |
1748 | | |
1749 | | /* For clients, nothing more to check */ |
1750 | 0 | if (!s->server) |
1751 | 0 | return 1; |
1752 | | |
1753 | | /* Check group is one of peers preferences */ |
1754 | 0 | tls1_get_peer_groups(s, &groups, &groups_len); |
1755 | | |
1756 | | /* |
1757 | | * RFC 4492 does not require the supported elliptic curves extension |
1758 | | * so if it is not sent we can just choose any curve. |
1759 | | * It is invalid to send an empty list in the supported groups |
1760 | | * extension, so groups_len == 0 always means no extension. |
1761 | | */ |
1762 | 0 | if (groups_len == 0) |
1763 | 0 | return 1; |
1764 | 0 | return tls1_in_list(group_id, groups, groups_len); |
1765 | 0 | } |
1766 | | |
1767 | | void tls1_get_formatlist(SSL_CONNECTION *s, const unsigned char **pformats, |
1768 | | size_t *num_formats) |
1769 | 0 | { |
1770 | | /* |
1771 | | * If we have a custom point format list use it otherwise use default |
1772 | | */ |
1773 | 0 | if (s->ext.ecpointformats) { |
1774 | 0 | *pformats = s->ext.ecpointformats; |
1775 | 0 | *num_formats = s->ext.ecpointformats_len; |
1776 | 0 | } else if ((s->options & SSL_OP_LEGACY_EC_POINT_FORMATS) != 0) { |
1777 | 0 | *pformats = ecformats_all; |
1778 | | /* For Suite B we don't support char2 fields */ |
1779 | 0 | if (tls1_suiteb(s)) |
1780 | 0 | *num_formats = sizeof(ecformats_all) - 1; |
1781 | 0 | else |
1782 | 0 | *num_formats = sizeof(ecformats_all); |
1783 | 0 | } else { |
1784 | 0 | *pformats = ecformats_default; |
1785 | 0 | *num_formats = sizeof(ecformats_default); |
1786 | 0 | } |
1787 | 0 | } |
1788 | | |
1789 | | /* Check a key is compatible with compression extension */ |
1790 | | static int tls1_check_pkey_comp(SSL_CONNECTION *s, EVP_PKEY *pkey) |
1791 | 0 | { |
1792 | 0 | unsigned char comp_id; |
1793 | 0 | size_t i; |
1794 | 0 | int point_conv; |
1795 | | |
1796 | | /* If not an EC key nothing to check */ |
1797 | 0 | if (!EVP_PKEY_is_a(pkey, "EC")) |
1798 | 0 | return 1; |
1799 | | |
1800 | | |
1801 | | /* Get required compression id */ |
1802 | 0 | point_conv = EVP_PKEY_get_ec_point_conv_form(pkey); |
1803 | 0 | if (point_conv == 0) |
1804 | 0 | return 0; |
1805 | 0 | if (point_conv == POINT_CONVERSION_UNCOMPRESSED) { |
1806 | 0 | comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
1807 | 0 | } else if (SSL_CONNECTION_IS_TLS13(s)) { |
1808 | | /* |
1809 | | * ec_point_formats extension is not used in TLSv1.3 so we ignore |
1810 | | * this check. |
1811 | | */ |
1812 | 0 | return 1; |
1813 | 0 | } else { |
1814 | 0 | int field_type = EVP_PKEY_get_field_type(pkey); |
1815 | |
|
1816 | 0 | if (field_type == NID_X9_62_prime_field) |
1817 | 0 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
1818 | 0 | else if (field_type == NID_X9_62_characteristic_two_field) |
1819 | 0 | comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
1820 | 0 | else |
1821 | 0 | return 0; |
1822 | 0 | } |
1823 | | /* |
1824 | | * If point formats extension present check it, otherwise everything is |
1825 | | * supported (see RFC4492). |
1826 | | */ |
1827 | 0 | if (s->ext.peer_ecpointformats == NULL) |
1828 | 0 | return 1; |
1829 | | |
1830 | 0 | for (i = 0; i < s->ext.peer_ecpointformats_len; i++) { |
1831 | 0 | if (s->ext.peer_ecpointformats[i] == comp_id) |
1832 | 0 | return 1; |
1833 | 0 | } |
1834 | 0 | return 0; |
1835 | 0 | } |
1836 | | |
1837 | | /* Return group id of a key */ |
1838 | | static uint16_t tls1_get_group_id(EVP_PKEY *pkey) |
1839 | 0 | { |
1840 | 0 | int curve_nid = ssl_get_EC_curve_nid(pkey); |
1841 | |
|
1842 | 0 | if (curve_nid == NID_undef) |
1843 | 0 | return 0; |
1844 | 0 | return tls1_nid2group_id(curve_nid); |
1845 | 0 | } |
1846 | | |
1847 | | /* |
1848 | | * Check cert parameters compatible with extensions: currently just checks EC |
1849 | | * certificates have compatible curves and compression. |
1850 | | */ |
1851 | | static int tls1_check_cert_param(SSL_CONNECTION *s, X509 *x, int check_ee_md) |
1852 | 0 | { |
1853 | 0 | uint16_t group_id; |
1854 | 0 | EVP_PKEY *pkey; |
1855 | 0 | pkey = X509_get0_pubkey(x); |
1856 | 0 | if (pkey == NULL) |
1857 | 0 | return 0; |
1858 | | /* If not EC nothing to do */ |
1859 | 0 | if (!EVP_PKEY_is_a(pkey, "EC")) |
1860 | 0 | return 1; |
1861 | | /* Check compression */ |
1862 | 0 | if (!tls1_check_pkey_comp(s, pkey)) |
1863 | 0 | return 0; |
1864 | 0 | group_id = tls1_get_group_id(pkey); |
1865 | | /* |
1866 | | * For a server we allow the certificate to not be in our list of supported |
1867 | | * groups. |
1868 | | */ |
1869 | 0 | if (!tls1_check_group_id(s, group_id, !s->server)) |
1870 | 0 | return 0; |
1871 | | /* |
1872 | | * Special case for suite B. We *MUST* sign using SHA256+P-256 or |
1873 | | * SHA384+P-384. |
1874 | | */ |
1875 | 0 | if (check_ee_md && tls1_suiteb(s)) { |
1876 | 0 | int check_md; |
1877 | 0 | size_t i; |
1878 | | |
1879 | | /* Check to see we have necessary signing algorithm */ |
1880 | 0 | if (group_id == OSSL_TLS_GROUP_ID_secp256r1) |
1881 | 0 | check_md = NID_ecdsa_with_SHA256; |
1882 | 0 | else if (group_id == OSSL_TLS_GROUP_ID_secp384r1) |
1883 | 0 | check_md = NID_ecdsa_with_SHA384; |
1884 | 0 | else |
1885 | 0 | return 0; /* Should never happen */ |
1886 | 0 | for (i = 0; i < s->shared_sigalgslen; i++) { |
1887 | 0 | if (check_md == s->shared_sigalgs[i]->sigandhash) |
1888 | 0 | return 1; |
1889 | 0 | } |
1890 | 0 | return 0; |
1891 | 0 | } |
1892 | 0 | return 1; |
1893 | 0 | } |
1894 | | |
1895 | | /* |
1896 | | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
1897 | | * @s: SSL connection |
1898 | | * @cid: Cipher ID we're considering using |
1899 | | * |
1900 | | * Checks that the kECDHE cipher suite we're considering using |
1901 | | * is compatible with the client extensions. |
1902 | | * |
1903 | | * Returns 0 when the cipher can't be used or 1 when it can. |
1904 | | */ |
1905 | | int tls1_check_ec_tmp_key(SSL_CONNECTION *s, unsigned long cid) |
1906 | 0 | { |
1907 | | /* If not Suite B just need a shared group */ |
1908 | 0 | if (!tls1_suiteb(s)) |
1909 | 0 | return tls1_shared_group(s, 0) != 0; |
1910 | | /* |
1911 | | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other |
1912 | | * curves permitted. |
1913 | | */ |
1914 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
1915 | 0 | return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp256r1, 1); |
1916 | 0 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) |
1917 | 0 | return tls1_check_group_id(s, OSSL_TLS_GROUP_ID_secp384r1, 1); |
1918 | | |
1919 | 0 | return 0; |
1920 | 0 | } |
1921 | | |
1922 | | /* Default sigalg schemes */ |
1923 | | static const uint16_t tls12_sigalgs[] = { |
1924 | | TLSEXT_SIGALG_mldsa65, |
1925 | | TLSEXT_SIGALG_mldsa87, |
1926 | | TLSEXT_SIGALG_mldsa44, |
1927 | | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1928 | | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
1929 | | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
1930 | | TLSEXT_SIGALG_ed25519, |
1931 | | TLSEXT_SIGALG_ed448, |
1932 | | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
1933 | | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, |
1934 | | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, |
1935 | | |
1936 | | TLSEXT_SIGALG_rsa_pss_pss_sha256, |
1937 | | TLSEXT_SIGALG_rsa_pss_pss_sha384, |
1938 | | TLSEXT_SIGALG_rsa_pss_pss_sha512, |
1939 | | TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
1940 | | TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
1941 | | TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
1942 | | |
1943 | | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
1944 | | TLSEXT_SIGALG_rsa_pkcs1_sha384, |
1945 | | TLSEXT_SIGALG_rsa_pkcs1_sha512, |
1946 | | |
1947 | | TLSEXT_SIGALG_ecdsa_sha224, |
1948 | | TLSEXT_SIGALG_ecdsa_sha1, |
1949 | | |
1950 | | TLSEXT_SIGALG_rsa_pkcs1_sha224, |
1951 | | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
1952 | | |
1953 | | TLSEXT_SIGALG_dsa_sha224, |
1954 | | TLSEXT_SIGALG_dsa_sha1, |
1955 | | |
1956 | | TLSEXT_SIGALG_dsa_sha256, |
1957 | | TLSEXT_SIGALG_dsa_sha384, |
1958 | | TLSEXT_SIGALG_dsa_sha512, |
1959 | | |
1960 | | #ifndef OPENSSL_NO_GOST |
1961 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
1962 | | TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
1963 | | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
1964 | | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
1965 | | TLSEXT_SIGALG_gostr34102001_gostr3411, |
1966 | | #endif |
1967 | | }; |
1968 | | |
1969 | | |
1970 | | static const uint16_t suiteb_sigalgs[] = { |
1971 | | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1972 | | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 |
1973 | | }; |
1974 | | |
1975 | | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
1976 | | {TLSEXT_SIGALG_ecdsa_secp256r1_sha256_name, |
1977 | | "ECDSA+SHA256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
1978 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
1979 | | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1, 1, 0, |
1980 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
1981 | | {TLSEXT_SIGALG_ecdsa_secp384r1_sha384_name, |
1982 | | "ECDSA+SHA384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
1983 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
1984 | | NID_ecdsa_with_SHA384, NID_secp384r1, 1, 0, |
1985 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
1986 | | {TLSEXT_SIGALG_ecdsa_secp521r1_sha512_name, |
1987 | | "ECDSA+SHA512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
1988 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
1989 | | NID_ecdsa_with_SHA512, NID_secp521r1, 1, 0, |
1990 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
1991 | | |
1992 | | {TLSEXT_SIGALG_ed25519_name, |
1993 | | NULL, TLSEXT_SIGALG_ed25519, |
1994 | | NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519, |
1995 | | NID_undef, NID_undef, 1, 0, |
1996 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
1997 | | {TLSEXT_SIGALG_ed448_name, |
1998 | | NULL, TLSEXT_SIGALG_ed448, |
1999 | | NID_undef, -1, EVP_PKEY_ED448, SSL_PKEY_ED448, |
2000 | | NID_undef, NID_undef, 1, 0, |
2001 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2002 | | |
2003 | | {TLSEXT_SIGALG_ecdsa_sha224_name, |
2004 | | "ECDSA+SHA224", TLSEXT_SIGALG_ecdsa_sha224, |
2005 | | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2006 | | NID_ecdsa_with_SHA224, NID_undef, 1, 0, |
2007 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2008 | | {TLSEXT_SIGALG_ecdsa_sha1_name, |
2009 | | "ECDSA+SHA1", TLSEXT_SIGALG_ecdsa_sha1, |
2010 | | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2011 | | NID_ecdsa_with_SHA1, NID_undef, 1, 0, |
2012 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2013 | | |
2014 | | {TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_name, |
2015 | | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256_alias, |
2016 | | TLSEXT_SIGALG_ecdsa_brainpoolP256r1_sha256, |
2017 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2018 | | NID_ecdsa_with_SHA256, NID_brainpoolP256r1, 1, 0, |
2019 | | TLS1_3_VERSION, 0, -1, -1}, |
2020 | | {TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_name, |
2021 | | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384_alias, |
2022 | | TLSEXT_SIGALG_ecdsa_brainpoolP384r1_sha384, |
2023 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2024 | | NID_ecdsa_with_SHA384, NID_brainpoolP384r1, 1, 0, |
2025 | | TLS1_3_VERSION, 0, -1, -1}, |
2026 | | {TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_name, |
2027 | | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512_alias, |
2028 | | TLSEXT_SIGALG_ecdsa_brainpoolP512r1_sha512, |
2029 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
2030 | | NID_ecdsa_with_SHA512, NID_brainpoolP512r1, 1, 0, |
2031 | | TLS1_3_VERSION, 0, -1, -1}, |
2032 | | |
2033 | | {TLSEXT_SIGALG_rsa_pss_rsae_sha256_name, |
2034 | | "PSS+SHA256", TLSEXT_SIGALG_rsa_pss_rsae_sha256, |
2035 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
2036 | | NID_undef, NID_undef, 1, 0, |
2037 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2038 | | {TLSEXT_SIGALG_rsa_pss_rsae_sha384_name, |
2039 | | "PSS+SHA384", TLSEXT_SIGALG_rsa_pss_rsae_sha384, |
2040 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
2041 | | NID_undef, NID_undef, 1, 0, |
2042 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2043 | | {TLSEXT_SIGALG_rsa_pss_rsae_sha512_name, |
2044 | | "PSS+SHA512", TLSEXT_SIGALG_rsa_pss_rsae_sha512, |
2045 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA, |
2046 | | NID_undef, NID_undef, 1, 0, |
2047 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2048 | | |
2049 | | {TLSEXT_SIGALG_rsa_pss_pss_sha256_name, |
2050 | | NULL, TLSEXT_SIGALG_rsa_pss_pss_sha256, |
2051 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
2052 | | NID_undef, NID_undef, 1, 0, |
2053 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2054 | | {TLSEXT_SIGALG_rsa_pss_pss_sha384_name, |
2055 | | NULL, TLSEXT_SIGALG_rsa_pss_pss_sha384, |
2056 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
2057 | | NID_undef, NID_undef, 1, 0, |
2058 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2059 | | {TLSEXT_SIGALG_rsa_pss_pss_sha512_name, |
2060 | | NULL, TLSEXT_SIGALG_rsa_pss_pss_sha512, |
2061 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
2062 | | NID_undef, NID_undef, 1, 0, |
2063 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2064 | | |
2065 | | {TLSEXT_SIGALG_rsa_pkcs1_sha256_name, |
2066 | | "RSA+SHA256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
2067 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2068 | | NID_sha256WithRSAEncryption, NID_undef, 1, 0, |
2069 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2070 | | {TLSEXT_SIGALG_rsa_pkcs1_sha384_name, |
2071 | | "RSA+SHA384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
2072 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2073 | | NID_sha384WithRSAEncryption, NID_undef, 1, 0, |
2074 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2075 | | {TLSEXT_SIGALG_rsa_pkcs1_sha512_name, |
2076 | | "RSA+SHA512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
2077 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2078 | | NID_sha512WithRSAEncryption, NID_undef, 1, 0, |
2079 | | TLS1_2_VERSION, 0, DTLS1_2_VERSION, 0}, |
2080 | | |
2081 | | {TLSEXT_SIGALG_rsa_pkcs1_sha224_name, |
2082 | | "RSA+SHA224", TLSEXT_SIGALG_rsa_pkcs1_sha224, |
2083 | | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2084 | | NID_sha224WithRSAEncryption, NID_undef, 1, 0, |
2085 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2086 | | {TLSEXT_SIGALG_rsa_pkcs1_sha1_name, |
2087 | | "RSA+SHA1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
2088 | | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
2089 | | NID_sha1WithRSAEncryption, NID_undef, 1, 0, |
2090 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2091 | | |
2092 | | {TLSEXT_SIGALG_dsa_sha256_name, |
2093 | | "DSA+SHA256", TLSEXT_SIGALG_dsa_sha256, |
2094 | | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2095 | | NID_dsa_with_SHA256, NID_undef, 1, 0, |
2096 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2097 | | {TLSEXT_SIGALG_dsa_sha384_name, |
2098 | | "DSA+SHA384", TLSEXT_SIGALG_dsa_sha384, |
2099 | | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2100 | | NID_undef, NID_undef, 1, 0, |
2101 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2102 | | {TLSEXT_SIGALG_dsa_sha512_name, |
2103 | | "DSA+SHA512", TLSEXT_SIGALG_dsa_sha512, |
2104 | | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2105 | | NID_undef, NID_undef, 1, 0, |
2106 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2107 | | {TLSEXT_SIGALG_dsa_sha224_name, |
2108 | | "DSA+SHA224", TLSEXT_SIGALG_dsa_sha224, |
2109 | | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2110 | | NID_undef, NID_undef, 1, 0, |
2111 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2112 | | {TLSEXT_SIGALG_dsa_sha1_name, |
2113 | | "DSA+SHA1", TLSEXT_SIGALG_dsa_sha1, |
2114 | | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
2115 | | NID_dsaWithSHA1, NID_undef, 1, 0, |
2116 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2117 | | |
2118 | | #ifndef OPENSSL_NO_GOST |
2119 | | {TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ |
2120 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic_name, |
2121 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic, |
2122 | | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
2123 | | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
2124 | | NID_undef, NID_undef, 1, 0, |
2125 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2126 | | {TLSEXT_SIGALG_gostr34102012_256_intrinsic_alias, /* RFC9189 */ |
2127 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic_name, |
2128 | | TLSEXT_SIGALG_gostr34102012_512_intrinsic, |
2129 | | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
2130 | | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
2131 | | NID_undef, NID_undef, 1, 0, |
2132 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2133 | | |
2134 | | {TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256_name, |
2135 | | NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
2136 | | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
2137 | | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, |
2138 | | NID_undef, NID_undef, 1, 0, |
2139 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2140 | | {TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512_name, |
2141 | | NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
2142 | | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
2143 | | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, |
2144 | | NID_undef, NID_undef, 1, 0, |
2145 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2146 | | {TLSEXT_SIGALG_gostr34102001_gostr3411_name, |
2147 | | NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
2148 | | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
2149 | | NID_id_GostR3410_2001, SSL_PKEY_GOST01, |
2150 | | NID_undef, NID_undef, 1, 0, |
2151 | | TLS1_2_VERSION, TLS1_2_VERSION, DTLS1_2_VERSION, DTLS1_2_VERSION}, |
2152 | | #endif |
2153 | | }; |
2154 | | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
2155 | | static const SIGALG_LOOKUP legacy_rsa_sigalg = { |
2156 | | "rsa_pkcs1_md5_sha1", NULL, 0, |
2157 | | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, |
2158 | | EVP_PKEY_RSA, SSL_PKEY_RSA, |
2159 | | NID_undef, NID_undef, 1, 0, |
2160 | | TLS1_VERSION, TLS1_2_VERSION, DTLS1_VERSION, DTLS1_2_VERSION |
2161 | | }; |
2162 | | |
2163 | | /* |
2164 | | * Default signature algorithm values used if signature algorithms not present. |
2165 | | * From RFC5246. Note: order must match certificate index order. |
2166 | | */ |
2167 | | static const uint16_t tls_default_sigalg[] = { |
2168 | | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ |
2169 | | 0, /* SSL_PKEY_RSA_PSS_SIGN */ |
2170 | | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ |
2171 | | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ |
2172 | | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ |
2173 | | TLSEXT_SIGALG_gostr34102012_256_intrinsic, /* SSL_PKEY_GOST12_256 */ |
2174 | | TLSEXT_SIGALG_gostr34102012_512_intrinsic, /* SSL_PKEY_GOST12_512 */ |
2175 | | 0, /* SSL_PKEY_ED25519 */ |
2176 | | 0, /* SSL_PKEY_ED448 */ |
2177 | | }; |
2178 | | |
2179 | | int ssl_setup_sigalgs(SSL_CTX *ctx) |
2180 | 0 | { |
2181 | 0 | size_t i, cache_idx, sigalgs_len, enabled; |
2182 | 0 | const SIGALG_LOOKUP *lu; |
2183 | 0 | SIGALG_LOOKUP *cache = NULL; |
2184 | 0 | uint16_t *tls12_sigalgs_list = NULL; |
2185 | 0 | EVP_PKEY *tmpkey = EVP_PKEY_new(); |
2186 | 0 | int istls; |
2187 | 0 | int ret = 0; |
2188 | |
|
2189 | 0 | if (ctx == NULL) |
2190 | 0 | goto err; |
2191 | | |
2192 | 0 | istls = !SSL_CTX_IS_DTLS(ctx); |
2193 | |
|
2194 | 0 | sigalgs_len = OSSL_NELEM(sigalg_lookup_tbl) + ctx->sigalg_list_len; |
2195 | |
|
2196 | 0 | cache = OPENSSL_zalloc(sizeof(const SIGALG_LOOKUP) * sigalgs_len); |
2197 | 0 | if (cache == NULL || tmpkey == NULL) |
2198 | 0 | goto err; |
2199 | | |
2200 | 0 | tls12_sigalgs_list = OPENSSL_zalloc(sizeof(uint16_t) * sigalgs_len); |
2201 | 0 | if (tls12_sigalgs_list == NULL) |
2202 | 0 | goto err; |
2203 | | |
2204 | 0 | ERR_set_mark(); |
2205 | | /* First fill cache and tls12_sigalgs list from legacy algorithm list */ |
2206 | 0 | for (i = 0, lu = sigalg_lookup_tbl; |
2207 | 0 | i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { |
2208 | 0 | EVP_PKEY_CTX *pctx; |
2209 | |
|
2210 | 0 | cache[i] = *lu; |
2211 | | |
2212 | | /* |
2213 | | * Check hash is available. |
2214 | | * This test is not perfect. A provider could have support |
2215 | | * for a signature scheme, but not a particular hash. However the hash |
2216 | | * could be available from some other loaded provider. In that case it |
2217 | | * could be that the signature is available, and the hash is available |
2218 | | * independently - but not as a combination. We ignore this for now. |
2219 | | */ |
2220 | 0 | if (lu->hash != NID_undef |
2221 | 0 | && ctx->ssl_digest_methods[lu->hash_idx] == NULL) { |
2222 | 0 | cache[i].available = 0; |
2223 | 0 | continue; |
2224 | 0 | } |
2225 | | |
2226 | 0 | if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { |
2227 | 0 | cache[i].available = 0; |
2228 | 0 | continue; |
2229 | 0 | } |
2230 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, tmpkey, ctx->propq); |
2231 | | /* If unable to create pctx we assume the sig algorithm is unavailable */ |
2232 | 0 | if (pctx == NULL) |
2233 | 0 | cache[i].available = 0; |
2234 | 0 | EVP_PKEY_CTX_free(pctx); |
2235 | 0 | } |
2236 | | |
2237 | | /* Now complete cache and tls12_sigalgs list with provider sig information */ |
2238 | 0 | cache_idx = OSSL_NELEM(sigalg_lookup_tbl); |
2239 | 0 | for (i = 0; i < ctx->sigalg_list_len; i++) { |
2240 | 0 | TLS_SIGALG_INFO si = ctx->sigalg_list[i]; |
2241 | 0 | cache[cache_idx].name = si.name; |
2242 | 0 | cache[cache_idx].name12 = si.sigalg_name; |
2243 | 0 | cache[cache_idx].sigalg = si.code_point; |
2244 | 0 | tls12_sigalgs_list[cache_idx] = si.code_point; |
2245 | 0 | cache[cache_idx].hash = si.hash_name?OBJ_txt2nid(si.hash_name):NID_undef; |
2246 | 0 | cache[cache_idx].hash_idx = ssl_get_md_idx(cache[cache_idx].hash); |
2247 | 0 | cache[cache_idx].sig = OBJ_txt2nid(si.sigalg_name); |
2248 | 0 | cache[cache_idx].sig_idx = (int)(i + SSL_PKEY_NUM); |
2249 | 0 | cache[cache_idx].sigandhash = OBJ_txt2nid(si.sigalg_name); |
2250 | 0 | cache[cache_idx].curve = NID_undef; |
2251 | 0 | cache[cache_idx].mintls = TLS1_3_VERSION; |
2252 | 0 | cache[cache_idx].maxtls = TLS1_3_VERSION; |
2253 | 0 | cache[cache_idx].mindtls = -1; |
2254 | 0 | cache[cache_idx].maxdtls = -1; |
2255 | | /* Compatibility with TLS 1.3 is checked on load */ |
2256 | 0 | cache[cache_idx].available = istls; |
2257 | 0 | cache[cache_idx].advertise = 0; |
2258 | 0 | cache_idx++; |
2259 | 0 | } |
2260 | 0 | ERR_pop_to_mark(); |
2261 | |
|
2262 | 0 | enabled = 0; |
2263 | 0 | for (i = 0; i < OSSL_NELEM(tls12_sigalgs); ++i) { |
2264 | 0 | SIGALG_LOOKUP *ent = cache; |
2265 | 0 | size_t j; |
2266 | |
|
2267 | 0 | for (j = 0; j < sigalgs_len; ent++, j++) { |
2268 | 0 | if (ent->sigalg != tls12_sigalgs[i]) |
2269 | 0 | continue; |
2270 | | /* Dedup by marking cache entry as default enabled. */ |
2271 | 0 | if (ent->available && !ent->advertise) { |
2272 | 0 | ent->advertise = 1; |
2273 | 0 | tls12_sigalgs_list[enabled++] = tls12_sigalgs[i]; |
2274 | 0 | } |
2275 | 0 | break; |
2276 | 0 | } |
2277 | 0 | } |
2278 | | |
2279 | | /* Append any provider sigalgs not yet handled */ |
2280 | 0 | for (i = OSSL_NELEM(sigalg_lookup_tbl); i < sigalgs_len; ++i) { |
2281 | 0 | SIGALG_LOOKUP *ent = &cache[i]; |
2282 | |
|
2283 | 0 | if (ent->available && !ent->advertise) |
2284 | 0 | tls12_sigalgs_list[enabled++] = ent->sigalg; |
2285 | 0 | } |
2286 | |
|
2287 | 0 | ctx->sigalg_lookup_cache = cache; |
2288 | 0 | ctx->sigalg_lookup_cache_len = sigalgs_len; |
2289 | 0 | ctx->tls12_sigalgs = tls12_sigalgs_list; |
2290 | 0 | ctx->tls12_sigalgs_len = enabled; |
2291 | 0 | cache = NULL; |
2292 | 0 | tls12_sigalgs_list = NULL; |
2293 | |
|
2294 | 0 | ret = 1; |
2295 | 0 | err: |
2296 | 0 | OPENSSL_free(cache); |
2297 | 0 | OPENSSL_free(tls12_sigalgs_list); |
2298 | 0 | EVP_PKEY_free(tmpkey); |
2299 | 0 | return ret; |
2300 | 0 | } |
2301 | | |
2302 | 0 | #define SIGLEN_BUF_INCREMENT 100 |
2303 | | |
2304 | | char *SSL_get1_builtin_sigalgs(OSSL_LIB_CTX *libctx) |
2305 | 0 | { |
2306 | 0 | size_t i, maxretlen = SIGLEN_BUF_INCREMENT; |
2307 | 0 | const SIGALG_LOOKUP *lu; |
2308 | 0 | EVP_PKEY *tmpkey = EVP_PKEY_new(); |
2309 | 0 | char *retval = OPENSSL_malloc(maxretlen); |
2310 | |
|
2311 | 0 | if (retval == NULL) |
2312 | 0 | return NULL; |
2313 | | |
2314 | | /* ensure retval string is NUL terminated */ |
2315 | 0 | retval[0] = (char)0; |
2316 | |
|
2317 | 0 | for (i = 0, lu = sigalg_lookup_tbl; |
2318 | 0 | i < OSSL_NELEM(sigalg_lookup_tbl); lu++, i++) { |
2319 | 0 | EVP_PKEY_CTX *pctx; |
2320 | 0 | int enabled = 1; |
2321 | |
|
2322 | 0 | ERR_set_mark(); |
2323 | | /* Check hash is available in some provider. */ |
2324 | 0 | if (lu->hash != NID_undef) { |
2325 | 0 | EVP_MD *hash = EVP_MD_fetch(libctx, OBJ_nid2ln(lu->hash), NULL); |
2326 | | |
2327 | | /* If unable to create we assume the hash algorithm is unavailable */ |
2328 | 0 | if (hash == NULL) { |
2329 | 0 | enabled = 0; |
2330 | 0 | ERR_pop_to_mark(); |
2331 | 0 | continue; |
2332 | 0 | } |
2333 | 0 | EVP_MD_free(hash); |
2334 | 0 | } |
2335 | | |
2336 | 0 | if (!EVP_PKEY_set_type(tmpkey, lu->sig)) { |
2337 | 0 | enabled = 0; |
2338 | 0 | ERR_pop_to_mark(); |
2339 | 0 | continue; |
2340 | 0 | } |
2341 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(libctx, tmpkey, NULL); |
2342 | | /* If unable to create pctx we assume the sig algorithm is unavailable */ |
2343 | 0 | if (pctx == NULL) |
2344 | 0 | enabled = 0; |
2345 | 0 | ERR_pop_to_mark(); |
2346 | 0 | EVP_PKEY_CTX_free(pctx); |
2347 | |
|
2348 | 0 | if (enabled) { |
2349 | 0 | const char *sa = lu->name; |
2350 | |
|
2351 | 0 | if (sa != NULL) { |
2352 | 0 | if (strlen(sa) + strlen(retval) + 1 >= maxretlen) { |
2353 | 0 | char *tmp; |
2354 | |
|
2355 | 0 | maxretlen += SIGLEN_BUF_INCREMENT; |
2356 | 0 | tmp = OPENSSL_realloc(retval, maxretlen); |
2357 | 0 | if (tmp == NULL) { |
2358 | 0 | OPENSSL_free(retval); |
2359 | 0 | return NULL; |
2360 | 0 | } |
2361 | 0 | retval = tmp; |
2362 | 0 | } |
2363 | 0 | if (strlen(retval) > 0) |
2364 | 0 | OPENSSL_strlcat(retval, ":", maxretlen); |
2365 | 0 | OPENSSL_strlcat(retval, sa, maxretlen); |
2366 | 0 | } else { |
2367 | | /* lu->name must not be NULL */ |
2368 | 0 | ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR); |
2369 | 0 | } |
2370 | 0 | } |
2371 | 0 | } |
2372 | | |
2373 | 0 | EVP_PKEY_free(tmpkey); |
2374 | 0 | return retval; |
2375 | 0 | } |
2376 | | |
2377 | | /* Lookup TLS signature algorithm */ |
2378 | | static const SIGALG_LOOKUP *tls1_lookup_sigalg(const SSL_CTX *ctx, |
2379 | | uint16_t sigalg) |
2380 | 0 | { |
2381 | 0 | size_t i; |
2382 | 0 | const SIGALG_LOOKUP *lu = ctx->sigalg_lookup_cache; |
2383 | |
|
2384 | 0 | for (i = 0; i < ctx->sigalg_lookup_cache_len; lu++, i++) { |
2385 | 0 | if (lu->sigalg == sigalg) { |
2386 | 0 | if (!lu->available) |
2387 | 0 | return NULL; |
2388 | 0 | return lu; |
2389 | 0 | } |
2390 | 0 | } |
2391 | 0 | return NULL; |
2392 | 0 | } |
2393 | | |
2394 | | /* Lookup hash: return 0 if invalid or not enabled */ |
2395 | | int tls1_lookup_md(SSL_CTX *ctx, const SIGALG_LOOKUP *lu, const EVP_MD **pmd) |
2396 | 0 | { |
2397 | 0 | const EVP_MD *md; |
2398 | |
|
2399 | 0 | if (lu == NULL) |
2400 | 0 | return 0; |
2401 | | /* lu->hash == NID_undef means no associated digest */ |
2402 | 0 | if (lu->hash == NID_undef) { |
2403 | 0 | md = NULL; |
2404 | 0 | } else { |
2405 | 0 | md = ssl_md(ctx, lu->hash_idx); |
2406 | 0 | if (md == NULL) |
2407 | 0 | return 0; |
2408 | 0 | } |
2409 | 0 | if (pmd) |
2410 | 0 | *pmd = md; |
2411 | 0 | return 1; |
2412 | 0 | } |
2413 | | |
2414 | | /* |
2415 | | * Check if key is large enough to generate RSA-PSS signature. |
2416 | | * |
2417 | | * The key must greater than or equal to 2 * hash length + 2. |
2418 | | * SHA512 has a hash length of 64 bytes, which is incompatible |
2419 | | * with a 128 byte (1024 bit) key. |
2420 | | */ |
2421 | 0 | #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_get_size(md) + 2) |
2422 | | static int rsa_pss_check_min_key_size(SSL_CTX *ctx, const EVP_PKEY *pkey, |
2423 | | const SIGALG_LOOKUP *lu) |
2424 | 0 | { |
2425 | 0 | const EVP_MD *md; |
2426 | |
|
2427 | 0 | if (pkey == NULL) |
2428 | 0 | return 0; |
2429 | 0 | if (!tls1_lookup_md(ctx, lu, &md) || md == NULL) |
2430 | 0 | return 0; |
2431 | 0 | if (EVP_MD_get_size(md) <= 0) |
2432 | 0 | return 0; |
2433 | 0 | if (EVP_PKEY_get_size(pkey) < RSA_PSS_MINIMUM_KEY_SIZE(md)) |
2434 | 0 | return 0; |
2435 | 0 | return 1; |
2436 | 0 | } |
2437 | | |
2438 | | /* |
2439 | | * Returns a signature algorithm when the peer did not send a list of supported |
2440 | | * signature algorithms. The signature algorithm is fixed for the certificate |
2441 | | * type. |idx| is a certificate type index (SSL_PKEY_*). When |idx| is -1 the |
2442 | | * certificate type from |s| will be used. |
2443 | | * Returns the signature algorithm to use, or NULL on error. |
2444 | | */ |
2445 | | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL_CONNECTION *s, |
2446 | | int idx) |
2447 | 0 | { |
2448 | 0 | if (idx == -1) { |
2449 | 0 | if (s->server) { |
2450 | 0 | size_t i; |
2451 | | |
2452 | | /* Work out index corresponding to ciphersuite */ |
2453 | 0 | for (i = 0; i < s->ssl_pkey_num; i++) { |
2454 | 0 | const SSL_CERT_LOOKUP *clu |
2455 | 0 | = ssl_cert_lookup_by_idx(i, SSL_CONNECTION_GET_CTX(s)); |
2456 | |
|
2457 | 0 | if (clu == NULL) |
2458 | 0 | continue; |
2459 | 0 | if (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) { |
2460 | 0 | idx = (int)i; |
2461 | 0 | break; |
2462 | 0 | } |
2463 | 0 | } |
2464 | | |
2465 | | /* |
2466 | | * Some GOST ciphersuites allow more than one signature algorithms |
2467 | | * */ |
2468 | 0 | if (idx == SSL_PKEY_GOST01 && s->s3.tmp.new_cipher->algorithm_auth != SSL_aGOST01) { |
2469 | 0 | int real_idx; |
2470 | |
|
2471 | 0 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST01; |
2472 | 0 | real_idx--) { |
2473 | 0 | if (s->cert->pkeys[real_idx].privatekey != NULL) { |
2474 | 0 | idx = real_idx; |
2475 | 0 | break; |
2476 | 0 | } |
2477 | 0 | } |
2478 | 0 | } |
2479 | | /* |
2480 | | * As both SSL_PKEY_GOST12_512 and SSL_PKEY_GOST12_256 indices can be used |
2481 | | * with new (aGOST12-only) ciphersuites, we should find out which one is available really. |
2482 | | */ |
2483 | 0 | else if (idx == SSL_PKEY_GOST12_256) { |
2484 | 0 | int real_idx; |
2485 | |
|
2486 | 0 | for (real_idx = SSL_PKEY_GOST12_512; real_idx >= SSL_PKEY_GOST12_256; |
2487 | 0 | real_idx--) { |
2488 | 0 | if (s->cert->pkeys[real_idx].privatekey != NULL) { |
2489 | 0 | idx = real_idx; |
2490 | 0 | break; |
2491 | 0 | } |
2492 | 0 | } |
2493 | 0 | } |
2494 | 0 | } else { |
2495 | 0 | idx = (int)(s->cert->key - s->cert->pkeys); |
2496 | 0 | } |
2497 | 0 | } |
2498 | 0 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) |
2499 | 0 | return NULL; |
2500 | | |
2501 | 0 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { |
2502 | 0 | const SIGALG_LOOKUP *lu = |
2503 | 0 | tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
2504 | 0 | tls_default_sigalg[idx]); |
2505 | |
|
2506 | 0 | if (lu == NULL) |
2507 | 0 | return NULL; |
2508 | 0 | if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, NULL)) |
2509 | 0 | return NULL; |
2510 | 0 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
2511 | 0 | return NULL; |
2512 | 0 | return lu; |
2513 | 0 | } |
2514 | 0 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, &legacy_rsa_sigalg)) |
2515 | 0 | return NULL; |
2516 | 0 | return &legacy_rsa_sigalg; |
2517 | 0 | } |
2518 | | /* Set peer sigalg based key type */ |
2519 | | int tls1_set_peer_legacy_sigalg(SSL_CONNECTION *s, const EVP_PKEY *pkey) |
2520 | 0 | { |
2521 | 0 | size_t idx; |
2522 | 0 | const SIGALG_LOOKUP *lu; |
2523 | |
|
2524 | 0 | if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) |
2525 | 0 | return 0; |
2526 | 0 | lu = tls1_get_legacy_sigalg(s, (int)idx); |
2527 | 0 | if (lu == NULL) |
2528 | 0 | return 0; |
2529 | 0 | s->s3.tmp.peer_sigalg = lu; |
2530 | 0 | return 1; |
2531 | 0 | } |
2532 | | |
2533 | | size_t tls12_get_psigalgs(SSL_CONNECTION *s, int sent, const uint16_t **psigs) |
2534 | 0 | { |
2535 | | /* |
2536 | | * If Suite B mode use Suite B sigalgs only, ignore any other |
2537 | | * preferences. |
2538 | | */ |
2539 | 0 | switch (tls1_suiteb(s)) { |
2540 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS: |
2541 | 0 | *psigs = suiteb_sigalgs; |
2542 | 0 | return OSSL_NELEM(suiteb_sigalgs); |
2543 | | |
2544 | 0 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: |
2545 | 0 | *psigs = suiteb_sigalgs; |
2546 | 0 | return 1; |
2547 | | |
2548 | 0 | case SSL_CERT_FLAG_SUITEB_192_LOS: |
2549 | 0 | *psigs = suiteb_sigalgs + 1; |
2550 | 0 | return 1; |
2551 | 0 | } |
2552 | | /* |
2553 | | * We use client_sigalgs (if not NULL) if we're a server |
2554 | | * and sending a certificate request or if we're a client and |
2555 | | * determining which shared algorithm to use. |
2556 | | */ |
2557 | 0 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { |
2558 | 0 | *psigs = s->cert->client_sigalgs; |
2559 | 0 | return s->cert->client_sigalgslen; |
2560 | 0 | } else if (s->cert->conf_sigalgs) { |
2561 | 0 | *psigs = s->cert->conf_sigalgs; |
2562 | 0 | return s->cert->conf_sigalgslen; |
2563 | 0 | } else { |
2564 | 0 | *psigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; |
2565 | 0 | return SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; |
2566 | 0 | } |
2567 | 0 | } |
2568 | | |
2569 | | /* |
2570 | | * Called by servers only. Checks that we have a sig alg that supports the |
2571 | | * specified EC curve. |
2572 | | */ |
2573 | | int tls_check_sigalg_curve(const SSL_CONNECTION *s, int curve) |
2574 | 0 | { |
2575 | 0 | const uint16_t *sigs; |
2576 | 0 | size_t siglen, i; |
2577 | |
|
2578 | 0 | if (s->cert->conf_sigalgs) { |
2579 | 0 | sigs = s->cert->conf_sigalgs; |
2580 | 0 | siglen = s->cert->conf_sigalgslen; |
2581 | 0 | } else { |
2582 | 0 | sigs = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs; |
2583 | 0 | siglen = SSL_CONNECTION_GET_CTX(s)->tls12_sigalgs_len; |
2584 | 0 | } |
2585 | |
|
2586 | 0 | for (i = 0; i < siglen; i++) { |
2587 | 0 | const SIGALG_LOOKUP *lu = |
2588 | 0 | tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sigs[i]); |
2589 | |
|
2590 | 0 | if (lu == NULL) |
2591 | 0 | continue; |
2592 | 0 | if (lu->sig == EVP_PKEY_EC |
2593 | 0 | && lu->curve != NID_undef |
2594 | 0 | && curve == lu->curve) |
2595 | 0 | return 1; |
2596 | 0 | } |
2597 | | |
2598 | 0 | return 0; |
2599 | 0 | } |
2600 | | |
2601 | | /* |
2602 | | * Return the number of security bits for the signature algorithm, or 0 on |
2603 | | * error. |
2604 | | */ |
2605 | | static int sigalg_security_bits(SSL_CTX *ctx, const SIGALG_LOOKUP *lu) |
2606 | 0 | { |
2607 | 0 | const EVP_MD *md = NULL; |
2608 | 0 | int secbits = 0; |
2609 | |
|
2610 | 0 | if (!tls1_lookup_md(ctx, lu, &md)) |
2611 | 0 | return 0; |
2612 | 0 | if (md != NULL) |
2613 | 0 | { |
2614 | 0 | int md_type = EVP_MD_get_type(md); |
2615 | | |
2616 | | /* Security bits: half digest bits */ |
2617 | 0 | secbits = EVP_MD_get_size(md) * 4; |
2618 | 0 | if (secbits <= 0) |
2619 | 0 | return 0; |
2620 | | /* |
2621 | | * SHA1 and MD5 are known to be broken. Reduce security bits so that |
2622 | | * they're no longer accepted at security level 1. The real values don't |
2623 | | * really matter as long as they're lower than 80, which is our |
2624 | | * security level 1. |
2625 | | * https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for |
2626 | | * SHA1 at 2^63.4 and MD5+SHA1 at 2^67.2 |
2627 | | * https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf |
2628 | | * puts a chosen-prefix attack for MD5 at 2^39. |
2629 | | */ |
2630 | 0 | if (md_type == NID_sha1) |
2631 | 0 | secbits = 64; |
2632 | 0 | else if (md_type == NID_md5_sha1) |
2633 | 0 | secbits = 67; |
2634 | 0 | else if (md_type == NID_md5) |
2635 | 0 | secbits = 39; |
2636 | 0 | } else { |
2637 | | /* Values from https://tools.ietf.org/html/rfc8032#section-8.5 */ |
2638 | 0 | if (lu->sigalg == TLSEXT_SIGALG_ed25519) |
2639 | 0 | secbits = 128; |
2640 | 0 | else if (lu->sigalg == TLSEXT_SIGALG_ed448) |
2641 | 0 | secbits = 224; |
2642 | 0 | } |
2643 | | /* |
2644 | | * For provider-based sigalgs we have secbits information available |
2645 | | * in the (provider-loaded) sigalg_list structure |
2646 | | */ |
2647 | 0 | if ((secbits == 0) && (lu->sig_idx >= SSL_PKEY_NUM) |
2648 | 0 | && ((lu->sig_idx - SSL_PKEY_NUM) < (int)ctx->sigalg_list_len)) { |
2649 | 0 | secbits = ctx->sigalg_list[lu->sig_idx - SSL_PKEY_NUM].secbits; |
2650 | 0 | } |
2651 | 0 | return secbits; |
2652 | 0 | } |
2653 | | |
2654 | | static int tls_sigalg_compat(SSL_CONNECTION *sc, const SIGALG_LOOKUP *lu) |
2655 | 0 | { |
2656 | 0 | int minversion, maxversion; |
2657 | 0 | int minproto, maxproto; |
2658 | |
|
2659 | 0 | if (!lu->available) |
2660 | 0 | return 0; |
2661 | | |
2662 | 0 | if (SSL_CONNECTION_IS_DTLS(sc)) { |
2663 | 0 | if (sc->ssl.method->version == DTLS_ANY_VERSION) { |
2664 | 0 | minproto = sc->min_proto_version; |
2665 | 0 | maxproto = sc->max_proto_version; |
2666 | 0 | } else { |
2667 | 0 | maxproto = minproto = sc->version; |
2668 | 0 | } |
2669 | 0 | minversion = lu->mindtls; |
2670 | 0 | maxversion = lu->maxdtls; |
2671 | 0 | } else { |
2672 | 0 | if (sc->ssl.method->version == TLS_ANY_VERSION) { |
2673 | 0 | minproto = sc->min_proto_version; |
2674 | 0 | maxproto = sc->max_proto_version; |
2675 | 0 | } else { |
2676 | 0 | maxproto = minproto = sc->version; |
2677 | 0 | } |
2678 | 0 | minversion = lu->mintls; |
2679 | 0 | maxversion = lu->maxtls; |
2680 | 0 | } |
2681 | 0 | if (minversion == -1 || maxversion == -1 |
2682 | 0 | || (minversion != 0 && maxproto != 0 |
2683 | 0 | && ssl_version_cmp(sc, minversion, maxproto) > 0) |
2684 | 0 | || (maxversion != 0 && minproto != 0 |
2685 | 0 | && ssl_version_cmp(sc, maxversion, minproto) < 0) |
2686 | 0 | || !tls12_sigalg_allowed(sc, SSL_SECOP_SIGALG_SUPPORTED, lu)) |
2687 | 0 | return 0; |
2688 | 0 | return 1; |
2689 | 0 | } |
2690 | | |
2691 | | /* |
2692 | | * Check signature algorithm is consistent with sent supported signature |
2693 | | * algorithms and if so set relevant digest and signature scheme in |
2694 | | * s. |
2695 | | */ |
2696 | | int tls12_check_peer_sigalg(SSL_CONNECTION *s, uint16_t sig, EVP_PKEY *pkey) |
2697 | 0 | { |
2698 | 0 | const uint16_t *sent_sigs; |
2699 | 0 | const EVP_MD *md = NULL; |
2700 | 0 | char sigalgstr[2]; |
2701 | 0 | size_t sent_sigslen, i, cidx; |
2702 | 0 | int pkeyid = -1; |
2703 | 0 | const SIGALG_LOOKUP *lu; |
2704 | 0 | int secbits = 0; |
2705 | |
|
2706 | 0 | pkeyid = EVP_PKEY_get_id(pkey); |
2707 | |
|
2708 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
2709 | | /* Disallow DSA for TLS 1.3 */ |
2710 | 0 | if (pkeyid == EVP_PKEY_DSA) { |
2711 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2712 | 0 | return 0; |
2713 | 0 | } |
2714 | | /* Only allow PSS for TLS 1.3 */ |
2715 | 0 | if (pkeyid == EVP_PKEY_RSA) |
2716 | 0 | pkeyid = EVP_PKEY_RSA_PSS; |
2717 | 0 | } |
2718 | | |
2719 | | /* Is this code point available and compatible with the protocol */ |
2720 | 0 | lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), sig); |
2721 | 0 | if (lu == NULL || !tls_sigalg_compat(s, lu)) { |
2722 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2723 | 0 | return 0; |
2724 | 0 | } |
2725 | | |
2726 | | /* if this sigalg is loaded, set so far unknown pkeyid to its sig NID */ |
2727 | 0 | if (pkeyid == EVP_PKEY_KEYMGMT) |
2728 | 0 | pkeyid = lu->sig; |
2729 | | |
2730 | | /* Should never happen */ |
2731 | 0 | if (pkeyid == -1) { |
2732 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2733 | 0 | return -1; |
2734 | 0 | } |
2735 | | |
2736 | | /* |
2737 | | * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type |
2738 | | * is consistent with signature: RSA keys can be used for RSA-PSS |
2739 | | */ |
2740 | 0 | if ((SSL_CONNECTION_IS_TLS13(s) |
2741 | 0 | && (lu->hash == NID_sha1 || lu->hash == NID_sha224)) |
2742 | 0 | || (pkeyid != lu->sig |
2743 | 0 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
2744 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2745 | 0 | return 0; |
2746 | 0 | } |
2747 | | /* Check the sigalg is consistent with the key OID */ |
2748 | 0 | if (!ssl_cert_lookup_by_nid( |
2749 | 0 | (pkeyid == EVP_PKEY_RSA_PSS) ? EVP_PKEY_get_id(pkey) : pkeyid, |
2750 | 0 | &cidx, SSL_CONNECTION_GET_CTX(s)) |
2751 | 0 | || lu->sig_idx != (int)cidx) { |
2752 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_SIGNATURE_TYPE); |
2753 | 0 | return 0; |
2754 | 0 | } |
2755 | | |
2756 | 0 | if (pkeyid == EVP_PKEY_EC) { |
2757 | | |
2758 | | /* Check point compression is permitted */ |
2759 | 0 | if (!tls1_check_pkey_comp(s, pkey)) { |
2760 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
2761 | 0 | SSL_R_ILLEGAL_POINT_COMPRESSION); |
2762 | 0 | return 0; |
2763 | 0 | } |
2764 | | |
2765 | | /* For TLS 1.3 or Suite B check curve matches signature algorithm */ |
2766 | 0 | if (SSL_CONNECTION_IS_TLS13(s) || tls1_suiteb(s)) { |
2767 | 0 | int curve = ssl_get_EC_curve_nid(pkey); |
2768 | |
|
2769 | 0 | if (lu->curve != NID_undef && curve != lu->curve) { |
2770 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
2771 | 0 | return 0; |
2772 | 0 | } |
2773 | 0 | } |
2774 | 0 | if (!SSL_CONNECTION_IS_TLS13(s)) { |
2775 | | /* Check curve matches extensions */ |
2776 | 0 | if (!tls1_check_group_id(s, tls1_get_group_id(pkey), 1)) { |
2777 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
2778 | 0 | return 0; |
2779 | 0 | } |
2780 | 0 | if (tls1_suiteb(s)) { |
2781 | | /* Check sigalg matches a permissible Suite B value */ |
2782 | 0 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 |
2783 | 0 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { |
2784 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2785 | 0 | SSL_R_WRONG_SIGNATURE_TYPE); |
2786 | 0 | return 0; |
2787 | 0 | } |
2788 | 0 | } |
2789 | 0 | } |
2790 | 0 | } else if (tls1_suiteb(s)) { |
2791 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
2792 | 0 | return 0; |
2793 | 0 | } |
2794 | | |
2795 | | /* Check signature matches a type we sent */ |
2796 | 0 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
2797 | 0 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
2798 | 0 | if (sig == *sent_sigs) |
2799 | 0 | break; |
2800 | 0 | } |
2801 | | /* Allow fallback to SHA1 if not strict mode */ |
2802 | 0 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
2803 | 0 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { |
2804 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
2805 | 0 | return 0; |
2806 | 0 | } |
2807 | 0 | if (!tls1_lookup_md(SSL_CONNECTION_GET_CTX(s), lu, &md)) { |
2808 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_UNKNOWN_DIGEST); |
2809 | 0 | return 0; |
2810 | 0 | } |
2811 | | /* |
2812 | | * Make sure security callback allows algorithm. For historical |
2813 | | * reasons we have to pass the sigalg as a two byte char array. |
2814 | | */ |
2815 | 0 | sigalgstr[0] = (sig >> 8) & 0xff; |
2816 | 0 | sigalgstr[1] = sig & 0xff; |
2817 | 0 | secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
2818 | 0 | if (secbits == 0 || |
2819 | 0 | !ssl_security(s, SSL_SECOP_SIGALG_CHECK, secbits, |
2820 | 0 | md != NULL ? EVP_MD_get_type(md) : NID_undef, |
2821 | 0 | (void *)sigalgstr)) { |
2822 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_WRONG_SIGNATURE_TYPE); |
2823 | 0 | return 0; |
2824 | 0 | } |
2825 | | /* Store the sigalg the peer uses */ |
2826 | 0 | s->s3.tmp.peer_sigalg = lu; |
2827 | 0 | return 1; |
2828 | 0 | } |
2829 | | |
2830 | | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
2831 | 0 | { |
2832 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
2833 | |
|
2834 | 0 | if (sc == NULL) |
2835 | 0 | return 0; |
2836 | | |
2837 | 0 | if (sc->s3.tmp.peer_sigalg == NULL) |
2838 | 0 | return 0; |
2839 | 0 | *pnid = sc->s3.tmp.peer_sigalg->sig; |
2840 | 0 | return 1; |
2841 | 0 | } |
2842 | | |
2843 | | int SSL_get_signature_type_nid(const SSL *s, int *pnid) |
2844 | 0 | { |
2845 | 0 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
2846 | |
|
2847 | 0 | if (sc == NULL) |
2848 | 0 | return 0; |
2849 | | |
2850 | 0 | if (sc->s3.tmp.sigalg == NULL) |
2851 | 0 | return 0; |
2852 | 0 | *pnid = sc->s3.tmp.sigalg->sig; |
2853 | 0 | return 1; |
2854 | 0 | } |
2855 | | |
2856 | | /* |
2857 | | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
2858 | | * supported, doesn't appear in supported signature algorithms, isn't supported |
2859 | | * by the enabled protocol versions or by the security level. |
2860 | | * |
2861 | | * This function should only be used for checking which ciphers are supported |
2862 | | * by the client. |
2863 | | * |
2864 | | * Call ssl_cipher_disabled() to check that it's enabled or not. |
2865 | | */ |
2866 | | int ssl_set_client_disabled(SSL_CONNECTION *s) |
2867 | 0 | { |
2868 | 0 | s->s3.tmp.mask_a = 0; |
2869 | 0 | s->s3.tmp.mask_k = 0; |
2870 | 0 | ssl_set_sig_mask(&s->s3.tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
2871 | 0 | if (ssl_get_min_max_version(s, &s->s3.tmp.min_ver, |
2872 | 0 | &s->s3.tmp.max_ver, NULL) != 0) |
2873 | 0 | return 0; |
2874 | 0 | #ifndef OPENSSL_NO_PSK |
2875 | | /* with PSK there must be client callback set */ |
2876 | 0 | if (!s->psk_client_callback) { |
2877 | 0 | s->s3.tmp.mask_a |= SSL_aPSK; |
2878 | 0 | s->s3.tmp.mask_k |= SSL_PSK; |
2879 | 0 | } |
2880 | 0 | #endif /* OPENSSL_NO_PSK */ |
2881 | 0 | #ifndef OPENSSL_NO_SRP |
2882 | 0 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
2883 | 0 | s->s3.tmp.mask_a |= SSL_aSRP; |
2884 | 0 | s->s3.tmp.mask_k |= SSL_kSRP; |
2885 | 0 | } |
2886 | 0 | #endif |
2887 | 0 | return 1; |
2888 | 0 | } |
2889 | | |
2890 | | /* |
2891 | | * ssl_cipher_disabled - check that a cipher is disabled or not |
2892 | | * @s: SSL connection that you want to use the cipher on |
2893 | | * @c: cipher to check |
2894 | | * @op: Security check that you want to do |
2895 | | * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 |
2896 | | * |
2897 | | * Returns 1 when it's disabled, 0 when enabled. |
2898 | | */ |
2899 | | int ssl_cipher_disabled(const SSL_CONNECTION *s, const SSL_CIPHER *c, |
2900 | | int op, int ecdhe) |
2901 | 0 | { |
2902 | 0 | int minversion = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls; |
2903 | 0 | int maxversion = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls; |
2904 | |
|
2905 | 0 | if (c->algorithm_mkey & s->s3.tmp.mask_k |
2906 | 0 | || c->algorithm_auth & s->s3.tmp.mask_a) |
2907 | 0 | return 1; |
2908 | 0 | if (s->s3.tmp.max_ver == 0) |
2909 | 0 | return 1; |
2910 | | |
2911 | 0 | if (SSL_IS_QUIC_INT_HANDSHAKE(s)) |
2912 | | /* For QUIC, only allow these ciphersuites. */ |
2913 | 0 | switch (SSL_CIPHER_get_id(c)) { |
2914 | 0 | case TLS1_3_CK_AES_128_GCM_SHA256: |
2915 | 0 | case TLS1_3_CK_AES_256_GCM_SHA384: |
2916 | 0 | case TLS1_3_CK_CHACHA20_POLY1305_SHA256: |
2917 | 0 | break; |
2918 | 0 | default: |
2919 | 0 | return 1; |
2920 | 0 | } |
2921 | | |
2922 | | /* |
2923 | | * For historical reasons we will allow ECHDE to be selected by a server |
2924 | | * in SSLv3 if we are a client |
2925 | | */ |
2926 | 0 | if (minversion == TLS1_VERSION |
2927 | 0 | && ecdhe |
2928 | 0 | && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) |
2929 | 0 | minversion = SSL3_VERSION; |
2930 | |
|
2931 | 0 | if (ssl_version_cmp(s, minversion, s->s3.tmp.max_ver) > 0 |
2932 | 0 | || ssl_version_cmp(s, maxversion, s->s3.tmp.min_ver) < 0) |
2933 | 0 | return 1; |
2934 | | |
2935 | 0 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
2936 | 0 | } |
2937 | | |
2938 | | int tls_use_ticket(SSL_CONNECTION *s) |
2939 | 0 | { |
2940 | 0 | if ((s->options & SSL_OP_NO_TICKET)) |
2941 | 0 | return 0; |
2942 | 0 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); |
2943 | 0 | } |
2944 | | |
2945 | | int tls1_set_server_sigalgs(SSL_CONNECTION *s) |
2946 | 0 | { |
2947 | 0 | size_t i; |
2948 | | |
2949 | | /* Clear any shared signature algorithms */ |
2950 | 0 | OPENSSL_free(s->shared_sigalgs); |
2951 | 0 | s->shared_sigalgs = NULL; |
2952 | 0 | s->shared_sigalgslen = 0; |
2953 | | |
2954 | | /* Clear certificate validity flags */ |
2955 | 0 | if (s->s3.tmp.valid_flags) |
2956 | 0 | memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); |
2957 | 0 | else |
2958 | 0 | s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t)); |
2959 | 0 | if (s->s3.tmp.valid_flags == NULL) |
2960 | 0 | return 0; |
2961 | | /* |
2962 | | * If peer sent no signature algorithms check to see if we support |
2963 | | * the default algorithm for each certificate type |
2964 | | */ |
2965 | 0 | if (s->s3.tmp.peer_cert_sigalgs == NULL |
2966 | 0 | && s->s3.tmp.peer_sigalgs == NULL) { |
2967 | 0 | const uint16_t *sent_sigs; |
2968 | 0 | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
2969 | |
|
2970 | 0 | for (i = 0; i < s->ssl_pkey_num; i++) { |
2971 | 0 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, (int)i); |
2972 | 0 | size_t j; |
2973 | |
|
2974 | 0 | if (lu == NULL) |
2975 | 0 | continue; |
2976 | | /* Check default matches a type we sent */ |
2977 | 0 | for (j = 0; j < sent_sigslen; j++) { |
2978 | 0 | if (lu->sigalg == sent_sigs[j]) { |
2979 | 0 | s->s3.tmp.valid_flags[i] = CERT_PKEY_SIGN; |
2980 | 0 | break; |
2981 | 0 | } |
2982 | 0 | } |
2983 | 0 | } |
2984 | 0 | return 1; |
2985 | 0 | } |
2986 | | |
2987 | 0 | if (!tls1_process_sigalgs(s)) { |
2988 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2989 | 0 | return 0; |
2990 | 0 | } |
2991 | 0 | if (s->shared_sigalgs != NULL) |
2992 | 0 | return 1; |
2993 | | |
2994 | | /* Fatal error if no shared signature algorithms */ |
2995 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
2996 | 0 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
2997 | 0 | return 0; |
2998 | 0 | } |
2999 | | |
3000 | | /*- |
3001 | | * Gets the ticket information supplied by the client if any. |
3002 | | * |
3003 | | * hello: The parsed ClientHello data |
3004 | | * ret: (output) on return, if a ticket was decrypted, then this is set to |
3005 | | * point to the resulting session. |
3006 | | */ |
3007 | | SSL_TICKET_STATUS tls_get_ticket_from_client(SSL_CONNECTION *s, |
3008 | | CLIENTHELLO_MSG *hello, |
3009 | | SSL_SESSION **ret) |
3010 | 0 | { |
3011 | 0 | size_t size; |
3012 | 0 | RAW_EXTENSION *ticketext; |
3013 | |
|
3014 | 0 | *ret = NULL; |
3015 | 0 | s->ext.ticket_expected = 0; |
3016 | | |
3017 | | /* |
3018 | | * If tickets disabled or not supported by the protocol version |
3019 | | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful |
3020 | | * resumption. |
3021 | | */ |
3022 | 0 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
3023 | 0 | return SSL_TICKET_NONE; |
3024 | | |
3025 | 0 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
3026 | 0 | if (!ticketext->present) |
3027 | 0 | return SSL_TICKET_NONE; |
3028 | | |
3029 | 0 | size = PACKET_remaining(&ticketext->data); |
3030 | |
|
3031 | 0 | return tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, |
3032 | 0 | hello->session_id, hello->session_id_len, ret); |
3033 | 0 | } |
3034 | | |
3035 | | /*- |
3036 | | * tls_decrypt_ticket attempts to decrypt a session ticket. |
3037 | | * |
3038 | | * If s->tls_session_secret_cb is set and we're not doing TLSv1.3 then we are |
3039 | | * expecting a pre-shared key ciphersuite, in which case we have no use for |
3040 | | * session tickets and one will never be decrypted, nor will |
3041 | | * s->ext.ticket_expected be set to 1. |
3042 | | * |
3043 | | * Side effects: |
3044 | | * Sets s->ext.ticket_expected to 1 if the server will have to issue |
3045 | | * a new session ticket to the client because the client indicated support |
3046 | | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
3047 | | * a session ticket or we couldn't use the one it gave us, or if |
3048 | | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
3049 | | * Otherwise, s->ext.ticket_expected is set to 0. |
3050 | | * |
3051 | | * etick: points to the body of the session ticket extension. |
3052 | | * eticklen: the length of the session tickets extension. |
3053 | | * sess_id: points at the session ID. |
3054 | | * sesslen: the length of the session ID. |
3055 | | * psess: (output) on return, if a ticket was decrypted, then this is set to |
3056 | | * point to the resulting session. |
3057 | | */ |
3058 | | SSL_TICKET_STATUS tls_decrypt_ticket(SSL_CONNECTION *s, |
3059 | | const unsigned char *etick, |
3060 | | size_t eticklen, |
3061 | | const unsigned char *sess_id, |
3062 | | size_t sesslen, SSL_SESSION **psess) |
3063 | 0 | { |
3064 | 0 | SSL_SESSION *sess = NULL; |
3065 | 0 | unsigned char *sdec; |
3066 | 0 | const unsigned char *p; |
3067 | 0 | int slen, ivlen, renew_ticket = 0, declen; |
3068 | 0 | SSL_TICKET_STATUS ret = SSL_TICKET_FATAL_ERR_OTHER; |
3069 | 0 | size_t mlen; |
3070 | 0 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
3071 | 0 | SSL_HMAC *hctx = NULL; |
3072 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
3073 | 0 | SSL_CTX *tctx = s->session_ctx; |
3074 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
3075 | |
|
3076 | 0 | if (eticklen == 0) { |
3077 | | /* |
3078 | | * The client will accept a ticket but doesn't currently have |
3079 | | * one (TLSv1.2 and below), or treated as a fatal error in TLSv1.3 |
3080 | | */ |
3081 | 0 | ret = SSL_TICKET_EMPTY; |
3082 | 0 | goto end; |
3083 | 0 | } |
3084 | 0 | if (!SSL_CONNECTION_IS_TLS13(s) && s->ext.session_secret_cb) { |
3085 | | /* |
3086 | | * Indicate that the ticket couldn't be decrypted rather than |
3087 | | * generating the session from ticket now, trigger |
3088 | | * abbreviated handshake based on external mechanism to |
3089 | | * calculate the master secret later. |
3090 | | */ |
3091 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3092 | 0 | goto end; |
3093 | 0 | } |
3094 | | |
3095 | | /* Need at least keyname + iv */ |
3096 | 0 | if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) { |
3097 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3098 | 0 | goto end; |
3099 | 0 | } |
3100 | | |
3101 | | /* Initialize session ticket encryption and HMAC contexts */ |
3102 | 0 | hctx = ssl_hmac_new(tctx); |
3103 | 0 | if (hctx == NULL) { |
3104 | 0 | ret = SSL_TICKET_FATAL_ERR_MALLOC; |
3105 | 0 | goto end; |
3106 | 0 | } |
3107 | 0 | ctx = EVP_CIPHER_CTX_new(); |
3108 | 0 | if (ctx == NULL) { |
3109 | 0 | ret = SSL_TICKET_FATAL_ERR_MALLOC; |
3110 | 0 | goto end; |
3111 | 0 | } |
3112 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
3113 | 0 | if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) |
3114 | | #else |
3115 | | if (tctx->ext.ticket_key_evp_cb != NULL) |
3116 | | #endif |
3117 | 0 | { |
3118 | 0 | unsigned char *nctick = (unsigned char *)etick; |
3119 | 0 | int rv = 0; |
3120 | |
|
3121 | 0 | if (tctx->ext.ticket_key_evp_cb != NULL) |
3122 | 0 | rv = tctx->ext.ticket_key_evp_cb(SSL_CONNECTION_GET_USER_SSL(s), |
3123 | 0 | nctick, |
3124 | 0 | nctick + TLSEXT_KEYNAME_LENGTH, |
3125 | 0 | ctx, |
3126 | 0 | ssl_hmac_get0_EVP_MAC_CTX(hctx), |
3127 | 0 | 0); |
3128 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
3129 | 0 | else if (tctx->ext.ticket_key_cb != NULL) |
3130 | | /* if 0 is returned, write an empty ticket */ |
3131 | 0 | rv = tctx->ext.ticket_key_cb(SSL_CONNECTION_GET_USER_SSL(s), nctick, |
3132 | 0 | nctick + TLSEXT_KEYNAME_LENGTH, |
3133 | 0 | ctx, ssl_hmac_get0_HMAC_CTX(hctx), 0); |
3134 | 0 | #endif |
3135 | 0 | if (rv < 0) { |
3136 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3137 | 0 | goto end; |
3138 | 0 | } |
3139 | 0 | if (rv == 0) { |
3140 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3141 | 0 | goto end; |
3142 | 0 | } |
3143 | 0 | if (rv == 2) |
3144 | 0 | renew_ticket = 1; |
3145 | 0 | } else { |
3146 | 0 | EVP_CIPHER *aes256cbc = NULL; |
3147 | | |
3148 | | /* Check key name matches */ |
3149 | 0 | if (memcmp(etick, tctx->ext.tick_key_name, |
3150 | 0 | TLSEXT_KEYNAME_LENGTH) != 0) { |
3151 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3152 | 0 | goto end; |
3153 | 0 | } |
3154 | | |
3155 | 0 | aes256cbc = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", |
3156 | 0 | sctx->propq); |
3157 | 0 | if (aes256cbc == NULL |
3158 | 0 | || ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, |
3159 | 0 | sizeof(tctx->ext.secure->tick_hmac_key), |
3160 | 0 | "SHA256") <= 0 |
3161 | 0 | || EVP_DecryptInit_ex(ctx, aes256cbc, NULL, |
3162 | 0 | tctx->ext.secure->tick_aes_key, |
3163 | 0 | etick + TLSEXT_KEYNAME_LENGTH) <= 0) { |
3164 | 0 | EVP_CIPHER_free(aes256cbc); |
3165 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3166 | 0 | goto end; |
3167 | 0 | } |
3168 | 0 | EVP_CIPHER_free(aes256cbc); |
3169 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) |
3170 | 0 | renew_ticket = 1; |
3171 | 0 | } |
3172 | | /* |
3173 | | * Attempt to process session ticket, first conduct sanity and integrity |
3174 | | * checks on ticket. |
3175 | | */ |
3176 | 0 | mlen = ssl_hmac_size(hctx); |
3177 | 0 | if (mlen == 0) { |
3178 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3179 | 0 | goto end; |
3180 | 0 | } |
3181 | | |
3182 | 0 | ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); |
3183 | 0 | if (ivlen < 0) { |
3184 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3185 | 0 | goto end; |
3186 | 0 | } |
3187 | | |
3188 | | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
3189 | 0 | if (eticklen <= TLSEXT_KEYNAME_LENGTH + ivlen + mlen) { |
3190 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3191 | 0 | goto end; |
3192 | 0 | } |
3193 | 0 | eticklen -= mlen; |
3194 | | /* Check HMAC of encrypted ticket */ |
3195 | 0 | if (ssl_hmac_update(hctx, etick, eticklen) <= 0 |
3196 | 0 | || ssl_hmac_final(hctx, tick_hmac, NULL, sizeof(tick_hmac)) <= 0) { |
3197 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3198 | 0 | goto end; |
3199 | 0 | } |
3200 | | |
3201 | 0 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
3202 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3203 | 0 | goto end; |
3204 | 0 | } |
3205 | | /* Attempt to decrypt session data */ |
3206 | | /* Move p after IV to start of encrypted ticket, update length */ |
3207 | 0 | p = etick + TLSEXT_KEYNAME_LENGTH + ivlen; |
3208 | 0 | eticklen -= TLSEXT_KEYNAME_LENGTH + ivlen; |
3209 | 0 | sdec = OPENSSL_malloc(eticklen); |
3210 | 0 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
3211 | 0 | (int)eticklen) <= 0) { |
3212 | 0 | OPENSSL_free(sdec); |
3213 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3214 | 0 | goto end; |
3215 | 0 | } |
3216 | 0 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
3217 | 0 | OPENSSL_free(sdec); |
3218 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3219 | 0 | goto end; |
3220 | 0 | } |
3221 | 0 | slen += declen; |
3222 | 0 | p = sdec; |
3223 | |
|
3224 | 0 | sess = d2i_SSL_SESSION_ex(NULL, &p, slen, sctx->libctx, sctx->propq); |
3225 | 0 | slen -= (int)(p - sdec); |
3226 | 0 | OPENSSL_free(sdec); |
3227 | 0 | if (sess) { |
3228 | | /* Some additional consistency checks */ |
3229 | 0 | if (slen != 0) { |
3230 | 0 | SSL_SESSION_free(sess); |
3231 | 0 | sess = NULL; |
3232 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3233 | 0 | goto end; |
3234 | 0 | } |
3235 | | /* |
3236 | | * The session ID, if non-empty, is used by some clients to detect |
3237 | | * that the ticket has been accepted. So we copy it to the session |
3238 | | * structure. If it is empty set length to zero as required by |
3239 | | * standard. |
3240 | | */ |
3241 | 0 | if (sesslen) { |
3242 | 0 | memcpy(sess->session_id, sess_id, sesslen); |
3243 | 0 | sess->session_id_length = sesslen; |
3244 | 0 | } |
3245 | 0 | if (renew_ticket) |
3246 | 0 | ret = SSL_TICKET_SUCCESS_RENEW; |
3247 | 0 | else |
3248 | 0 | ret = SSL_TICKET_SUCCESS; |
3249 | 0 | goto end; |
3250 | 0 | } |
3251 | 0 | ERR_clear_error(); |
3252 | | /* |
3253 | | * For session parse failure, indicate that we need to send a new ticket. |
3254 | | */ |
3255 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3256 | |
|
3257 | 0 | end: |
3258 | 0 | EVP_CIPHER_CTX_free(ctx); |
3259 | 0 | ssl_hmac_free(hctx); |
3260 | | |
3261 | | /* |
3262 | | * If set, the decrypt_ticket_cb() is called unless a fatal error was |
3263 | | * detected above. The callback is responsible for checking |ret| before it |
3264 | | * performs any action |
3265 | | */ |
3266 | 0 | if (s->session_ctx->decrypt_ticket_cb != NULL |
3267 | 0 | && (ret == SSL_TICKET_EMPTY |
3268 | 0 | || ret == SSL_TICKET_NO_DECRYPT |
3269 | 0 | || ret == SSL_TICKET_SUCCESS |
3270 | 0 | || ret == SSL_TICKET_SUCCESS_RENEW)) { |
3271 | 0 | size_t keyname_len = eticklen; |
3272 | 0 | int retcb; |
3273 | |
|
3274 | 0 | if (keyname_len > TLSEXT_KEYNAME_LENGTH) |
3275 | 0 | keyname_len = TLSEXT_KEYNAME_LENGTH; |
3276 | 0 | retcb = s->session_ctx->decrypt_ticket_cb(SSL_CONNECTION_GET_SSL(s), |
3277 | 0 | sess, etick, keyname_len, |
3278 | 0 | ret, |
3279 | 0 | s->session_ctx->ticket_cb_data); |
3280 | 0 | switch (retcb) { |
3281 | 0 | case SSL_TICKET_RETURN_ABORT: |
3282 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3283 | 0 | break; |
3284 | | |
3285 | 0 | case SSL_TICKET_RETURN_IGNORE: |
3286 | 0 | ret = SSL_TICKET_NONE; |
3287 | 0 | SSL_SESSION_free(sess); |
3288 | 0 | sess = NULL; |
3289 | 0 | break; |
3290 | | |
3291 | 0 | case SSL_TICKET_RETURN_IGNORE_RENEW: |
3292 | 0 | if (ret != SSL_TICKET_EMPTY && ret != SSL_TICKET_NO_DECRYPT) |
3293 | 0 | ret = SSL_TICKET_NO_DECRYPT; |
3294 | | /* else the value of |ret| will already do the right thing */ |
3295 | 0 | SSL_SESSION_free(sess); |
3296 | 0 | sess = NULL; |
3297 | 0 | break; |
3298 | | |
3299 | 0 | case SSL_TICKET_RETURN_USE: |
3300 | 0 | case SSL_TICKET_RETURN_USE_RENEW: |
3301 | 0 | if (ret != SSL_TICKET_SUCCESS |
3302 | 0 | && ret != SSL_TICKET_SUCCESS_RENEW) |
3303 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3304 | 0 | else if (retcb == SSL_TICKET_RETURN_USE) |
3305 | 0 | ret = SSL_TICKET_SUCCESS; |
3306 | 0 | else |
3307 | 0 | ret = SSL_TICKET_SUCCESS_RENEW; |
3308 | 0 | break; |
3309 | | |
3310 | 0 | default: |
3311 | 0 | ret = SSL_TICKET_FATAL_ERR_OTHER; |
3312 | 0 | } |
3313 | 0 | } |
3314 | | |
3315 | 0 | if (s->ext.session_secret_cb == NULL || SSL_CONNECTION_IS_TLS13(s)) { |
3316 | 0 | switch (ret) { |
3317 | 0 | case SSL_TICKET_NO_DECRYPT: |
3318 | 0 | case SSL_TICKET_SUCCESS_RENEW: |
3319 | 0 | case SSL_TICKET_EMPTY: |
3320 | 0 | s->ext.ticket_expected = 1; |
3321 | 0 | } |
3322 | 0 | } |
3323 | |
|
3324 | 0 | *psess = sess; |
3325 | |
|
3326 | 0 | return ret; |
3327 | 0 | } |
3328 | | |
3329 | | /* Check to see if a signature algorithm is allowed */ |
3330 | | static int tls12_sigalg_allowed(const SSL_CONNECTION *s, int op, |
3331 | | const SIGALG_LOOKUP *lu) |
3332 | 0 | { |
3333 | 0 | unsigned char sigalgstr[2]; |
3334 | 0 | int secbits; |
3335 | |
|
3336 | 0 | if (lu == NULL || !lu->available) |
3337 | 0 | return 0; |
3338 | | /* DSA is not allowed in TLS 1.3 */ |
3339 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) |
3340 | 0 | return 0; |
3341 | | /* |
3342 | | * At some point we should fully axe DSA/etc. in ClientHello as per TLS 1.3 |
3343 | | * spec |
3344 | | */ |
3345 | 0 | if (!s->server && !SSL_CONNECTION_IS_DTLS(s) |
3346 | 0 | && s->s3.tmp.min_ver >= TLS1_3_VERSION |
3347 | 0 | && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX |
3348 | 0 | || lu->hash_idx == SSL_MD_MD5_IDX |
3349 | 0 | || lu->hash_idx == SSL_MD_SHA224_IDX)) |
3350 | 0 | return 0; |
3351 | | |
3352 | | /* See if public key algorithm allowed */ |
3353 | 0 | if (ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), lu->sig_idx)) |
3354 | 0 | return 0; |
3355 | | |
3356 | 0 | if (lu->sig == NID_id_GostR3410_2012_256 |
3357 | 0 | || lu->sig == NID_id_GostR3410_2012_512 |
3358 | 0 | || lu->sig == NID_id_GostR3410_2001) { |
3359 | | /* We never allow GOST sig algs on the server with TLSv1.3 */ |
3360 | 0 | if (s->server && SSL_CONNECTION_IS_TLS13(s)) |
3361 | 0 | return 0; |
3362 | 0 | if (!s->server |
3363 | 0 | && SSL_CONNECTION_GET_SSL(s)->method->version == TLS_ANY_VERSION |
3364 | 0 | && s->s3.tmp.max_ver >= TLS1_3_VERSION) { |
3365 | 0 | int i, num; |
3366 | 0 | STACK_OF(SSL_CIPHER) *sk; |
3367 | | |
3368 | | /* |
3369 | | * We're a client that could negotiate TLSv1.3. We only allow GOST |
3370 | | * sig algs if we could negotiate TLSv1.2 or below and we have GOST |
3371 | | * ciphersuites enabled. |
3372 | | */ |
3373 | |
|
3374 | 0 | if (s->s3.tmp.min_ver >= TLS1_3_VERSION) |
3375 | 0 | return 0; |
3376 | | |
3377 | 0 | sk = SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)); |
3378 | 0 | num = sk != NULL ? sk_SSL_CIPHER_num(sk) : 0; |
3379 | 0 | for (i = 0; i < num; i++) { |
3380 | 0 | const SSL_CIPHER *c; |
3381 | |
|
3382 | 0 | c = sk_SSL_CIPHER_value(sk, i); |
3383 | | /* Skip disabled ciphers */ |
3384 | 0 | if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) |
3385 | 0 | continue; |
3386 | | |
3387 | 0 | if ((c->algorithm_mkey & (SSL_kGOST | SSL_kGOST18)) != 0) |
3388 | 0 | break; |
3389 | 0 | } |
3390 | 0 | if (i == num) |
3391 | 0 | return 0; |
3392 | 0 | } |
3393 | 0 | } |
3394 | | |
3395 | | /* Finally see if security callback allows it */ |
3396 | 0 | secbits = sigalg_security_bits(SSL_CONNECTION_GET_CTX(s), lu); |
3397 | 0 | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
3398 | 0 | sigalgstr[1] = lu->sigalg & 0xff; |
3399 | 0 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
3400 | 0 | } |
3401 | | |
3402 | | /* |
3403 | | * Get a mask of disabled public key algorithms based on supported signature |
3404 | | * algorithms. For example if no signature algorithm supports RSA then RSA is |
3405 | | * disabled. |
3406 | | */ |
3407 | | |
3408 | | void ssl_set_sig_mask(uint32_t *pmask_a, SSL_CONNECTION *s, int op) |
3409 | 0 | { |
3410 | 0 | const uint16_t *sigalgs; |
3411 | 0 | size_t i, sigalgslen; |
3412 | 0 | uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA; |
3413 | | /* |
3414 | | * Go through all signature algorithms seeing if we support any |
3415 | | * in disabled_mask. |
3416 | | */ |
3417 | 0 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
3418 | 0 | for (i = 0; i < sigalgslen; i++, sigalgs++) { |
3419 | 0 | const SIGALG_LOOKUP *lu = |
3420 | 0 | tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *sigalgs); |
3421 | 0 | const SSL_CERT_LOOKUP *clu; |
3422 | |
|
3423 | 0 | if (lu == NULL) |
3424 | 0 | continue; |
3425 | | |
3426 | 0 | clu = ssl_cert_lookup_by_idx(lu->sig_idx, |
3427 | 0 | SSL_CONNECTION_GET_CTX(s)); |
3428 | 0 | if (clu == NULL) |
3429 | 0 | continue; |
3430 | | |
3431 | | /* If algorithm is disabled see if we can enable it */ |
3432 | 0 | if ((clu->amask & disabled_mask) != 0 |
3433 | 0 | && tls12_sigalg_allowed(s, op, lu)) |
3434 | 0 | disabled_mask &= ~clu->amask; |
3435 | 0 | } |
3436 | 0 | *pmask_a |= disabled_mask; |
3437 | 0 | } |
3438 | | |
3439 | | int tls12_copy_sigalgs(SSL_CONNECTION *s, WPACKET *pkt, |
3440 | | const uint16_t *psig, size_t psiglen) |
3441 | 0 | { |
3442 | 0 | size_t i; |
3443 | 0 | int rv = 0; |
3444 | |
|
3445 | 0 | for (i = 0; i < psiglen; i++, psig++) { |
3446 | 0 | const SIGALG_LOOKUP *lu = |
3447 | 0 | tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *psig); |
3448 | |
|
3449 | 0 | if (lu == NULL || !tls_sigalg_compat(s, lu)) |
3450 | 0 | continue; |
3451 | 0 | if (!WPACKET_put_bytes_u16(pkt, *psig)) |
3452 | 0 | return 0; |
3453 | | /* |
3454 | | * If TLS 1.3 must have at least one valid TLS 1.3 message |
3455 | | * signing algorithm: i.e. neither RSA nor SHA1/SHA224 |
3456 | | */ |
3457 | 0 | if (rv == 0 && (!SSL_CONNECTION_IS_TLS13(s) |
3458 | 0 | || (lu->sig != EVP_PKEY_RSA |
3459 | 0 | && lu->hash != NID_sha1 |
3460 | 0 | && lu->hash != NID_sha224))) |
3461 | 0 | rv = 1; |
3462 | 0 | } |
3463 | 0 | if (rv == 0) |
3464 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
3465 | 0 | return rv; |
3466 | 0 | } |
3467 | | |
3468 | | /* Given preference and allowed sigalgs set shared sigalgs */ |
3469 | | static size_t tls12_shared_sigalgs(SSL_CONNECTION *s, |
3470 | | const SIGALG_LOOKUP **shsig, |
3471 | | const uint16_t *pref, size_t preflen, |
3472 | | const uint16_t *allow, size_t allowlen) |
3473 | 0 | { |
3474 | 0 | const uint16_t *ptmp, *atmp; |
3475 | 0 | size_t i, j, nmatch = 0; |
3476 | 0 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
3477 | 0 | const SIGALG_LOOKUP *lu = |
3478 | 0 | tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *ptmp); |
3479 | | |
3480 | | /* Skip disabled hashes or signature algorithms */ |
3481 | 0 | if (lu == NULL |
3482 | 0 | || !tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
3483 | 0 | continue; |
3484 | 0 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
3485 | 0 | if (*ptmp == *atmp) { |
3486 | 0 | nmatch++; |
3487 | 0 | if (shsig) |
3488 | 0 | *shsig++ = lu; |
3489 | 0 | break; |
3490 | 0 | } |
3491 | 0 | } |
3492 | 0 | } |
3493 | 0 | return nmatch; |
3494 | 0 | } |
3495 | | |
3496 | | /* Set shared signature algorithms for SSL structures */ |
3497 | | static int tls1_set_shared_sigalgs(SSL_CONNECTION *s) |
3498 | 0 | { |
3499 | 0 | const uint16_t *pref, *allow, *conf; |
3500 | 0 | size_t preflen, allowlen, conflen; |
3501 | 0 | size_t nmatch; |
3502 | 0 | const SIGALG_LOOKUP **salgs = NULL; |
3503 | 0 | CERT *c = s->cert; |
3504 | 0 | unsigned int is_suiteb = tls1_suiteb(s); |
3505 | |
|
3506 | 0 | OPENSSL_free(s->shared_sigalgs); |
3507 | 0 | s->shared_sigalgs = NULL; |
3508 | 0 | s->shared_sigalgslen = 0; |
3509 | | /* If client use client signature algorithms if not NULL */ |
3510 | 0 | if (!s->server && c->client_sigalgs && !is_suiteb) { |
3511 | 0 | conf = c->client_sigalgs; |
3512 | 0 | conflen = c->client_sigalgslen; |
3513 | 0 | } else if (c->conf_sigalgs && !is_suiteb) { |
3514 | 0 | conf = c->conf_sigalgs; |
3515 | 0 | conflen = c->conf_sigalgslen; |
3516 | 0 | } else |
3517 | 0 | conflen = tls12_get_psigalgs(s, 0, &conf); |
3518 | 0 | if (s->options & SSL_OP_SERVER_PREFERENCE || is_suiteb) { |
3519 | 0 | pref = conf; |
3520 | 0 | preflen = conflen; |
3521 | 0 | allow = s->s3.tmp.peer_sigalgs; |
3522 | 0 | allowlen = s->s3.tmp.peer_sigalgslen; |
3523 | 0 | } else { |
3524 | 0 | allow = conf; |
3525 | 0 | allowlen = conflen; |
3526 | 0 | pref = s->s3.tmp.peer_sigalgs; |
3527 | 0 | preflen = s->s3.tmp.peer_sigalgslen; |
3528 | 0 | } |
3529 | 0 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); |
3530 | 0 | if (nmatch) { |
3531 | 0 | if ((salgs = OPENSSL_malloc(nmatch * sizeof(*salgs))) == NULL) |
3532 | 0 | return 0; |
3533 | 0 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); |
3534 | 0 | } else { |
3535 | 0 | salgs = NULL; |
3536 | 0 | } |
3537 | 0 | s->shared_sigalgs = salgs; |
3538 | 0 | s->shared_sigalgslen = nmatch; |
3539 | 0 | return 1; |
3540 | 0 | } |
3541 | | |
3542 | | int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen) |
3543 | 0 | { |
3544 | 0 | unsigned int stmp; |
3545 | 0 | size_t size, i; |
3546 | 0 | uint16_t *buf; |
3547 | |
|
3548 | 0 | size = PACKET_remaining(pkt); |
3549 | | |
3550 | | /* Invalid data length */ |
3551 | 0 | if (size == 0 || (size & 1) != 0) |
3552 | 0 | return 0; |
3553 | | |
3554 | 0 | size >>= 1; |
3555 | |
|
3556 | 0 | if ((buf = OPENSSL_malloc(size * sizeof(*buf))) == NULL) |
3557 | 0 | return 0; |
3558 | 0 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
3559 | 0 | buf[i] = stmp; |
3560 | |
|
3561 | 0 | if (i != size) { |
3562 | 0 | OPENSSL_free(buf); |
3563 | 0 | return 0; |
3564 | 0 | } |
3565 | | |
3566 | 0 | OPENSSL_free(*pdest); |
3567 | 0 | *pdest = buf; |
3568 | 0 | *pdestlen = size; |
3569 | |
|
3570 | 0 | return 1; |
3571 | 0 | } |
3572 | | |
3573 | | int tls1_save_sigalgs(SSL_CONNECTION *s, PACKET *pkt, int cert) |
3574 | 0 | { |
3575 | | /* Extension ignored for inappropriate versions */ |
3576 | 0 | if (!SSL_USE_SIGALGS(s)) |
3577 | 0 | return 1; |
3578 | | /* Should never happen */ |
3579 | 0 | if (s->cert == NULL) |
3580 | 0 | return 0; |
3581 | | |
3582 | 0 | if (cert) |
3583 | 0 | return tls1_save_u16(pkt, &s->s3.tmp.peer_cert_sigalgs, |
3584 | 0 | &s->s3.tmp.peer_cert_sigalgslen); |
3585 | 0 | else |
3586 | 0 | return tls1_save_u16(pkt, &s->s3.tmp.peer_sigalgs, |
3587 | 0 | &s->s3.tmp.peer_sigalgslen); |
3588 | |
|
3589 | 0 | } |
3590 | | |
3591 | | /* Set preferred digest for each key type */ |
3592 | | |
3593 | | int tls1_process_sigalgs(SSL_CONNECTION *s) |
3594 | 0 | { |
3595 | 0 | size_t i; |
3596 | 0 | uint32_t *pvalid = s->s3.tmp.valid_flags; |
3597 | |
|
3598 | 0 | if (!tls1_set_shared_sigalgs(s)) |
3599 | 0 | return 0; |
3600 | | |
3601 | 0 | for (i = 0; i < s->ssl_pkey_num; i++) |
3602 | 0 | pvalid[i] = 0; |
3603 | |
|
3604 | 0 | for (i = 0; i < s->shared_sigalgslen; i++) { |
3605 | 0 | const SIGALG_LOOKUP *sigptr = s->shared_sigalgs[i]; |
3606 | 0 | int idx = sigptr->sig_idx; |
3607 | | |
3608 | | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
3609 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
3610 | 0 | continue; |
3611 | | /* If not disabled indicate we can explicitly sign */ |
3612 | 0 | if (pvalid[idx] == 0 |
3613 | 0 | && !ssl_cert_is_disabled(SSL_CONNECTION_GET_CTX(s), idx)) |
3614 | 0 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
3615 | 0 | } |
3616 | 0 | return 1; |
3617 | 0 | } |
3618 | | |
3619 | | int SSL_get_sigalgs(SSL *s, int idx, |
3620 | | int *psign, int *phash, int *psignhash, |
3621 | | unsigned char *rsig, unsigned char *rhash) |
3622 | 0 | { |
3623 | 0 | uint16_t *psig; |
3624 | 0 | size_t numsigalgs; |
3625 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3626 | |
|
3627 | 0 | if (sc == NULL) |
3628 | 0 | return 0; |
3629 | | |
3630 | 0 | psig = sc->s3.tmp.peer_sigalgs; |
3631 | 0 | numsigalgs = sc->s3.tmp.peer_sigalgslen; |
3632 | |
|
3633 | 0 | if (psig == NULL || numsigalgs > INT_MAX) |
3634 | 0 | return 0; |
3635 | 0 | if (idx >= 0) { |
3636 | 0 | const SIGALG_LOOKUP *lu; |
3637 | |
|
3638 | 0 | if (idx >= (int)numsigalgs) |
3639 | 0 | return 0; |
3640 | 0 | psig += idx; |
3641 | 0 | if (rhash != NULL) |
3642 | 0 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
3643 | 0 | if (rsig != NULL) |
3644 | 0 | *rsig = (unsigned char)(*psig & 0xff); |
3645 | 0 | lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(sc), *psig); |
3646 | 0 | if (psign != NULL) |
3647 | 0 | *psign = lu != NULL ? lu->sig : NID_undef; |
3648 | 0 | if (phash != NULL) |
3649 | 0 | *phash = lu != NULL ? lu->hash : NID_undef; |
3650 | 0 | if (psignhash != NULL) |
3651 | 0 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; |
3652 | 0 | } |
3653 | 0 | return (int)numsigalgs; |
3654 | 0 | } |
3655 | | |
3656 | | int SSL_get_shared_sigalgs(SSL *s, int idx, |
3657 | | int *psign, int *phash, int *psignhash, |
3658 | | unsigned char *rsig, unsigned char *rhash) |
3659 | 0 | { |
3660 | 0 | const SIGALG_LOOKUP *shsigalgs; |
3661 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
3662 | |
|
3663 | 0 | if (sc == NULL) |
3664 | 0 | return 0; |
3665 | | |
3666 | 0 | if (sc->shared_sigalgs == NULL |
3667 | 0 | || idx < 0 |
3668 | 0 | || idx >= (int)sc->shared_sigalgslen |
3669 | 0 | || sc->shared_sigalgslen > INT_MAX) |
3670 | 0 | return 0; |
3671 | 0 | shsigalgs = sc->shared_sigalgs[idx]; |
3672 | 0 | if (phash != NULL) |
3673 | 0 | *phash = shsigalgs->hash; |
3674 | 0 | if (psign != NULL) |
3675 | 0 | *psign = shsigalgs->sig; |
3676 | 0 | if (psignhash != NULL) |
3677 | 0 | *psignhash = shsigalgs->sigandhash; |
3678 | 0 | if (rsig != NULL) |
3679 | 0 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); |
3680 | 0 | if (rhash != NULL) |
3681 | 0 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); |
3682 | 0 | return (int)sc->shared_sigalgslen; |
3683 | 0 | } |
3684 | | |
3685 | | /* Maximum possible number of unique entries in sigalgs array */ |
3686 | 0 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) |
3687 | | |
3688 | | typedef struct { |
3689 | | size_t sigalgcnt; |
3690 | | /* TLSEXT_SIGALG_XXX values */ |
3691 | | uint16_t sigalgs[TLS_MAX_SIGALGCNT]; |
3692 | | SSL_CTX *ctx; |
3693 | | } sig_cb_st; |
3694 | | |
3695 | | static void get_sigorhash(int *psig, int *phash, const char *str) |
3696 | 0 | { |
3697 | 0 | if (OPENSSL_strcasecmp(str, "RSA") == 0) { |
3698 | 0 | *psig = EVP_PKEY_RSA; |
3699 | 0 | } else if (OPENSSL_strcasecmp(str, "RSA-PSS") == 0 |
3700 | 0 | || OPENSSL_strcasecmp(str, "PSS") == 0) { |
3701 | 0 | *psig = EVP_PKEY_RSA_PSS; |
3702 | 0 | } else if (OPENSSL_strcasecmp(str, "DSA") == 0) { |
3703 | 0 | *psig = EVP_PKEY_DSA; |
3704 | 0 | } else if (OPENSSL_strcasecmp(str, "ECDSA") == 0) { |
3705 | 0 | *psig = EVP_PKEY_EC; |
3706 | 0 | } else { |
3707 | 0 | *phash = OBJ_sn2nid(str); |
3708 | 0 | if (*phash == NID_undef) |
3709 | 0 | *phash = OBJ_ln2nid(str); |
3710 | 0 | } |
3711 | 0 | } |
3712 | | /* Maximum length of a signature algorithm string component */ |
3713 | | #define TLS_MAX_SIGSTRING_LEN 40 |
3714 | | |
3715 | | static int sig_cb(const char *elem, int len, void *arg) |
3716 | 0 | { |
3717 | 0 | sig_cb_st *sarg = arg; |
3718 | 0 | size_t i = 0; |
3719 | 0 | const SIGALG_LOOKUP *s; |
3720 | 0 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
3721 | 0 | const char *iana, *alias; |
3722 | 0 | int sig_alg = NID_undef, hash_alg = NID_undef; |
3723 | 0 | int ignore_unknown = 0; |
3724 | |
|
3725 | 0 | if (elem == NULL) |
3726 | 0 | return 0; |
3727 | 0 | if (elem[0] == '?') { |
3728 | 0 | ignore_unknown = 1; |
3729 | 0 | ++elem; |
3730 | 0 | --len; |
3731 | 0 | } |
3732 | 0 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
3733 | 0 | return 0; |
3734 | 0 | if (len > (int)(sizeof(etmp) - 1)) |
3735 | 0 | return 0; |
3736 | 0 | memcpy(etmp, elem, len); |
3737 | 0 | etmp[len] = 0; |
3738 | 0 | p = strchr(etmp, '+'); |
3739 | | /* |
3740 | | * We only allow SignatureSchemes listed in the sigalg_lookup_tbl; |
3741 | | * if there's no '+' in the provided name, look for the new-style combined |
3742 | | * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP. |
3743 | | * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and |
3744 | | * rsa_pss_rsae_* that differ only by public key OID; in such cases |
3745 | | * we will pick the _rsae_ variant, by virtue of them appearing earlier |
3746 | | * in the table. |
3747 | | */ |
3748 | 0 | if (p == NULL) { |
3749 | 0 | if (sarg->ctx != NULL) { |
3750 | 0 | for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { |
3751 | 0 | iana = sarg->ctx->sigalg_lookup_cache[i].name; |
3752 | 0 | alias = sarg->ctx->sigalg_lookup_cache[i].name12; |
3753 | 0 | if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0) |
3754 | 0 | || OPENSSL_strcasecmp(etmp, iana) == 0) { |
3755 | | /* Ignore known, but unavailable sigalgs. */ |
3756 | 0 | if (!sarg->ctx->sigalg_lookup_cache[i].available) |
3757 | 0 | return 1; |
3758 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = |
3759 | 0 | sarg->ctx->sigalg_lookup_cache[i].sigalg; |
3760 | 0 | goto found; |
3761 | 0 | } |
3762 | 0 | } |
3763 | 0 | } else { |
3764 | | /* Syntax checks use the built-in sigalgs */ |
3765 | 0 | for (i = 0, s = sigalg_lookup_tbl; |
3766 | 0 | i < OSSL_NELEM(sigalg_lookup_tbl); i++, s++) { |
3767 | 0 | iana = s->name; |
3768 | 0 | alias = s->name12; |
3769 | 0 | if ((alias != NULL && OPENSSL_strcasecmp(etmp, alias) == 0) |
3770 | 0 | || OPENSSL_strcasecmp(etmp, iana) == 0) { |
3771 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
3772 | 0 | goto found; |
3773 | 0 | } |
3774 | 0 | } |
3775 | 0 | } |
3776 | 0 | } else { |
3777 | 0 | *p = 0; |
3778 | 0 | p++; |
3779 | 0 | if (*p == 0) |
3780 | 0 | return 0; |
3781 | 0 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
3782 | 0 | get_sigorhash(&sig_alg, &hash_alg, p); |
3783 | 0 | if (sig_alg != NID_undef && hash_alg != NID_undef) { |
3784 | 0 | if (sarg->ctx != NULL) { |
3785 | 0 | for (i = 0; i < sarg->ctx->sigalg_lookup_cache_len; i++) { |
3786 | 0 | s = &sarg->ctx->sigalg_lookup_cache[i]; |
3787 | 0 | if (s->hash == hash_alg && s->sig == sig_alg) { |
3788 | | /* Ignore known, but unavailable sigalgs. */ |
3789 | 0 | if (!sarg->ctx->sigalg_lookup_cache[i].available) |
3790 | 0 | return 1; |
3791 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
3792 | 0 | goto found; |
3793 | 0 | } |
3794 | 0 | } |
3795 | 0 | } else { |
3796 | 0 | for (i = 0; i < OSSL_NELEM(sigalg_lookup_tbl); i++) { |
3797 | 0 | s = &sigalg_lookup_tbl[i]; |
3798 | 0 | if (s->hash == hash_alg && s->sig == sig_alg) { |
3799 | 0 | sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg; |
3800 | 0 | goto found; |
3801 | 0 | } |
3802 | 0 | } |
3803 | 0 | } |
3804 | 0 | } |
3805 | 0 | } |
3806 | | /* Ignore unknown algorithms if ignore_unknown */ |
3807 | 0 | return ignore_unknown; |
3808 | | |
3809 | 0 | found: |
3810 | | /* Ignore duplicates */ |
3811 | 0 | for (i = 0; i < sarg->sigalgcnt - 1; i++) { |
3812 | 0 | if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt - 1]) { |
3813 | 0 | sarg->sigalgcnt--; |
3814 | 0 | return 1; |
3815 | 0 | } |
3816 | 0 | } |
3817 | 0 | return 1; |
3818 | 0 | } |
3819 | | |
3820 | | /* |
3821 | | * Set supported signature algorithms based on a colon separated list of the |
3822 | | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
3823 | | */ |
3824 | | int tls1_set_sigalgs_list(SSL_CTX *ctx, CERT *c, const char *str, int client) |
3825 | 0 | { |
3826 | 0 | sig_cb_st sig; |
3827 | 0 | sig.sigalgcnt = 0; |
3828 | |
|
3829 | 0 | if (ctx != NULL) |
3830 | 0 | sig.ctx = ctx; |
3831 | 0 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) |
3832 | 0 | return 0; |
3833 | 0 | if (sig.sigalgcnt == 0) { |
3834 | 0 | ERR_raise_data(ERR_LIB_SSL, ERR_R_PASSED_INVALID_ARGUMENT, |
3835 | 0 | "No valid signature algorithms in '%s'", str); |
3836 | 0 | return 0; |
3837 | 0 | } |
3838 | 0 | if (c == NULL) |
3839 | 0 | return 1; |
3840 | 0 | return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); |
3841 | 0 | } |
3842 | | |
3843 | | int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen, |
3844 | | int client) |
3845 | 0 | { |
3846 | 0 | uint16_t *sigalgs; |
3847 | |
|
3848 | 0 | if ((sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs))) == NULL) |
3849 | 0 | return 0; |
3850 | 0 | memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs)); |
3851 | |
|
3852 | 0 | if (client) { |
3853 | 0 | OPENSSL_free(c->client_sigalgs); |
3854 | 0 | c->client_sigalgs = sigalgs; |
3855 | 0 | c->client_sigalgslen = salglen; |
3856 | 0 | } else { |
3857 | 0 | OPENSSL_free(c->conf_sigalgs); |
3858 | 0 | c->conf_sigalgs = sigalgs; |
3859 | 0 | c->conf_sigalgslen = salglen; |
3860 | 0 | } |
3861 | |
|
3862 | 0 | return 1; |
3863 | 0 | } |
3864 | | |
3865 | | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
3866 | 0 | { |
3867 | 0 | uint16_t *sigalgs, *sptr; |
3868 | 0 | size_t i; |
3869 | |
|
3870 | 0 | if (salglen & 1) |
3871 | 0 | return 0; |
3872 | 0 | if ((sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs))) == NULL) |
3873 | 0 | return 0; |
3874 | 0 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
3875 | 0 | size_t j; |
3876 | 0 | const SIGALG_LOOKUP *curr; |
3877 | 0 | int md_id = *psig_nids++; |
3878 | 0 | int sig_id = *psig_nids++; |
3879 | |
|
3880 | 0 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); |
3881 | 0 | j++, curr++) { |
3882 | 0 | if (curr->hash == md_id && curr->sig == sig_id) { |
3883 | 0 | *sptr++ = curr->sigalg; |
3884 | 0 | break; |
3885 | 0 | } |
3886 | 0 | } |
3887 | |
|
3888 | 0 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
3889 | 0 | goto err; |
3890 | 0 | } |
3891 | | |
3892 | 0 | if (client) { |
3893 | 0 | OPENSSL_free(c->client_sigalgs); |
3894 | 0 | c->client_sigalgs = sigalgs; |
3895 | 0 | c->client_sigalgslen = salglen / 2; |
3896 | 0 | } else { |
3897 | 0 | OPENSSL_free(c->conf_sigalgs); |
3898 | 0 | c->conf_sigalgs = sigalgs; |
3899 | 0 | c->conf_sigalgslen = salglen / 2; |
3900 | 0 | } |
3901 | |
|
3902 | 0 | return 1; |
3903 | | |
3904 | 0 | err: |
3905 | 0 | OPENSSL_free(sigalgs); |
3906 | 0 | return 0; |
3907 | 0 | } |
3908 | | |
3909 | | static int tls1_check_sig_alg(SSL_CONNECTION *s, X509 *x, int default_nid) |
3910 | 0 | { |
3911 | 0 | int sig_nid, use_pc_sigalgs = 0; |
3912 | 0 | size_t i; |
3913 | 0 | const SIGALG_LOOKUP *sigalg; |
3914 | 0 | size_t sigalgslen; |
3915 | | |
3916 | | /*- |
3917 | | * RFC 8446, section 4.2.3: |
3918 | | * |
3919 | | * The signatures on certificates that are self-signed or certificates |
3920 | | * that are trust anchors are not validated, since they begin a |
3921 | | * certification path (see [RFC5280], Section 3.2). A certificate that |
3922 | | * begins a certification path MAY use a signature algorithm that is not |
3923 | | * advertised as being supported in the "signature_algorithms" |
3924 | | * extension. |
3925 | | */ |
3926 | 0 | if (default_nid == -1 || X509_self_signed(x, 0)) |
3927 | 0 | return 1; |
3928 | 0 | sig_nid = X509_get_signature_nid(x); |
3929 | 0 | if (default_nid) |
3930 | 0 | return sig_nid == default_nid ? 1 : 0; |
3931 | | |
3932 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.peer_cert_sigalgs != NULL) { |
3933 | | /* |
3934 | | * If we're in TLSv1.3 then we only get here if we're checking the |
3935 | | * chain. If the peer has specified peer_cert_sigalgs then we use them |
3936 | | * otherwise we default to normal sigalgs. |
3937 | | */ |
3938 | 0 | sigalgslen = s->s3.tmp.peer_cert_sigalgslen; |
3939 | 0 | use_pc_sigalgs = 1; |
3940 | 0 | } else { |
3941 | 0 | sigalgslen = s->shared_sigalgslen; |
3942 | 0 | } |
3943 | 0 | for (i = 0; i < sigalgslen; i++) { |
3944 | 0 | int mdnid, pknid; |
3945 | |
|
3946 | 0 | sigalg = use_pc_sigalgs |
3947 | 0 | ? tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
3948 | 0 | s->s3.tmp.peer_cert_sigalgs[i]) |
3949 | 0 | : s->shared_sigalgs[i]; |
3950 | 0 | if (sigalg == NULL) |
3951 | 0 | continue; |
3952 | 0 | if (sig_nid == sigalg->sigandhash) |
3953 | 0 | return 1; |
3954 | 0 | if (sigalg->sig != EVP_PKEY_RSA_PSS) |
3955 | 0 | continue; |
3956 | | /* |
3957 | | * Accept RSA PKCS#1 signatures in certificates when the signature |
3958 | | * algorithms include RSA-PSS with a matching digest algorithm. |
3959 | | * |
3960 | | * When a TLS 1.3 peer inadvertently omits the legacy RSA PKCS#1 code |
3961 | | * points, and we're doing strict checking of the certificate chain (in |
3962 | | * a cert_cb via SSL_check_chain()) we may then reject RSA signed |
3963 | | * certificates in the chain, but the TLS requirement on PSS should not |
3964 | | * extend to certificates. Though the peer can in fact list the legacy |
3965 | | * sigalgs for just this purpose, it is not likely that a better chain |
3966 | | * signed with RSA-PSS is available. |
3967 | | */ |
3968 | 0 | if (!OBJ_find_sigid_algs(sig_nid, &mdnid, &pknid)) |
3969 | 0 | continue; |
3970 | 0 | if (pknid == EVP_PKEY_RSA && mdnid == sigalg->hash) |
3971 | 0 | return 1; |
3972 | 0 | } |
3973 | 0 | return 0; |
3974 | 0 | } |
3975 | | |
3976 | | /* Check to see if a certificate issuer name matches list of CA names */ |
3977 | | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) |
3978 | 0 | { |
3979 | 0 | const X509_NAME *nm; |
3980 | 0 | int i; |
3981 | 0 | nm = X509_get_issuer_name(x); |
3982 | 0 | for (i = 0; i < sk_X509_NAME_num(names); i++) { |
3983 | 0 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
3984 | 0 | return 1; |
3985 | 0 | } |
3986 | 0 | return 0; |
3987 | 0 | } |
3988 | | |
3989 | | /* |
3990 | | * Check certificate chain is consistent with TLS extensions and is usable by |
3991 | | * server. This servers two purposes: it allows users to check chains before |
3992 | | * passing them to the server and it allows the server to check chains before |
3993 | | * attempting to use them. |
3994 | | */ |
3995 | | |
3996 | | /* Flags which need to be set for a certificate when strict mode not set */ |
3997 | | |
3998 | | #define CERT_PKEY_VALID_FLAGS \ |
3999 | 0 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
4000 | | /* Strict mode flags */ |
4001 | | #define CERT_PKEY_STRICT_FLAGS \ |
4002 | 0 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
4003 | 0 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) |
4004 | | |
4005 | | int tls1_check_chain(SSL_CONNECTION *s, X509 *x, EVP_PKEY *pk, |
4006 | | STACK_OF(X509) *chain, int idx) |
4007 | 0 | { |
4008 | 0 | int i; |
4009 | 0 | int rv = 0; |
4010 | 0 | int check_flags = 0, strict_mode; |
4011 | 0 | CERT_PKEY *cpk = NULL; |
4012 | 0 | CERT *c = s->cert; |
4013 | 0 | uint32_t *pvalid; |
4014 | 0 | unsigned int suiteb_flags = tls1_suiteb(s); |
4015 | | |
4016 | | /* |
4017 | | * Meaning of idx: |
4018 | | * idx == -1 means SSL_check_chain() invocation |
4019 | | * idx == -2 means checking client certificate chains |
4020 | | * idx >= 0 means checking SSL_PKEY index |
4021 | | * |
4022 | | * For RPK, where there may be no cert, we ignore -1 |
4023 | | */ |
4024 | 0 | if (idx != -1) { |
4025 | 0 | if (idx == -2) { |
4026 | 0 | cpk = c->key; |
4027 | 0 | idx = (int)(cpk - c->pkeys); |
4028 | 0 | } else |
4029 | 0 | cpk = c->pkeys + idx; |
4030 | 0 | pvalid = s->s3.tmp.valid_flags + idx; |
4031 | 0 | x = cpk->x509; |
4032 | 0 | pk = cpk->privatekey; |
4033 | 0 | chain = cpk->chain; |
4034 | 0 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
4035 | 0 | if (tls12_rpk_and_privkey(s, idx)) { |
4036 | 0 | if (EVP_PKEY_is_a(pk, "EC") && !tls1_check_pkey_comp(s, pk)) |
4037 | 0 | return 0; |
4038 | 0 | *pvalid = rv = CERT_PKEY_RPK; |
4039 | 0 | return rv; |
4040 | 0 | } |
4041 | | /* If no cert or key, forget it */ |
4042 | 0 | if (x == NULL || pk == NULL) |
4043 | 0 | goto end; |
4044 | 0 | } else { |
4045 | 0 | size_t certidx; |
4046 | |
|
4047 | 0 | if (x == NULL || pk == NULL) |
4048 | 0 | return 0; |
4049 | | |
4050 | 0 | if (ssl_cert_lookup_by_pkey(pk, &certidx, |
4051 | 0 | SSL_CONNECTION_GET_CTX(s)) == NULL) |
4052 | 0 | return 0; |
4053 | 0 | idx = (int)certidx; |
4054 | 0 | pvalid = s->s3.tmp.valid_flags + idx; |
4055 | |
|
4056 | 0 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
4057 | 0 | check_flags = CERT_PKEY_STRICT_FLAGS; |
4058 | 0 | else |
4059 | 0 | check_flags = CERT_PKEY_VALID_FLAGS; |
4060 | 0 | strict_mode = 1; |
4061 | 0 | } |
4062 | | |
4063 | 0 | if (suiteb_flags) { |
4064 | 0 | int ok; |
4065 | 0 | if (check_flags) |
4066 | 0 | check_flags |= CERT_PKEY_SUITEB; |
4067 | 0 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); |
4068 | 0 | if (ok == X509_V_OK) |
4069 | 0 | rv |= CERT_PKEY_SUITEB; |
4070 | 0 | else if (!check_flags) |
4071 | 0 | goto end; |
4072 | 0 | } |
4073 | | |
4074 | | /* |
4075 | | * Check all signature algorithms are consistent with signature |
4076 | | * algorithms extension if TLS 1.2 or later and strict mode. |
4077 | | */ |
4078 | 0 | if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION |
4079 | 0 | && strict_mode) { |
4080 | 0 | int default_nid; |
4081 | 0 | int rsign = 0; |
4082 | |
|
4083 | 0 | if (s->s3.tmp.peer_cert_sigalgs != NULL |
4084 | 0 | || s->s3.tmp.peer_sigalgs != NULL) { |
4085 | 0 | default_nid = 0; |
4086 | | /* If no sigalgs extension use defaults from RFC5246 */ |
4087 | 0 | } else { |
4088 | 0 | switch (idx) { |
4089 | 0 | case SSL_PKEY_RSA: |
4090 | 0 | rsign = EVP_PKEY_RSA; |
4091 | 0 | default_nid = NID_sha1WithRSAEncryption; |
4092 | 0 | break; |
4093 | | |
4094 | 0 | case SSL_PKEY_DSA_SIGN: |
4095 | 0 | rsign = EVP_PKEY_DSA; |
4096 | 0 | default_nid = NID_dsaWithSHA1; |
4097 | 0 | break; |
4098 | | |
4099 | 0 | case SSL_PKEY_ECC: |
4100 | 0 | rsign = EVP_PKEY_EC; |
4101 | 0 | default_nid = NID_ecdsa_with_SHA1; |
4102 | 0 | break; |
4103 | | |
4104 | 0 | case SSL_PKEY_GOST01: |
4105 | 0 | rsign = NID_id_GostR3410_2001; |
4106 | 0 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
4107 | 0 | break; |
4108 | | |
4109 | 0 | case SSL_PKEY_GOST12_256: |
4110 | 0 | rsign = NID_id_GostR3410_2012_256; |
4111 | 0 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
4112 | 0 | break; |
4113 | | |
4114 | 0 | case SSL_PKEY_GOST12_512: |
4115 | 0 | rsign = NID_id_GostR3410_2012_512; |
4116 | 0 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
4117 | 0 | break; |
4118 | | |
4119 | 0 | default: |
4120 | 0 | default_nid = -1; |
4121 | 0 | break; |
4122 | 0 | } |
4123 | 0 | } |
4124 | | /* |
4125 | | * If peer sent no signature algorithms extension and we have set |
4126 | | * preferred signature algorithms check we support sha1. |
4127 | | */ |
4128 | 0 | if (default_nid > 0 && c->conf_sigalgs) { |
4129 | 0 | size_t j; |
4130 | 0 | const uint16_t *p = c->conf_sigalgs; |
4131 | 0 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
4132 | 0 | const SIGALG_LOOKUP *lu = |
4133 | 0 | tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), *p); |
4134 | |
|
4135 | 0 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) |
4136 | 0 | break; |
4137 | 0 | } |
4138 | 0 | if (j == c->conf_sigalgslen) { |
4139 | 0 | if (check_flags) |
4140 | 0 | goto skip_sigs; |
4141 | 0 | else |
4142 | 0 | goto end; |
4143 | 0 | } |
4144 | 0 | } |
4145 | | /* Check signature algorithm of each cert in chain */ |
4146 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4147 | | /* |
4148 | | * We only get here if the application has called SSL_check_chain(), |
4149 | | * so check_flags is always set. |
4150 | | */ |
4151 | 0 | if (find_sig_alg(s, x, pk) != NULL) |
4152 | 0 | rv |= CERT_PKEY_EE_SIGNATURE; |
4153 | 0 | } else if (!tls1_check_sig_alg(s, x, default_nid)) { |
4154 | 0 | if (!check_flags) |
4155 | 0 | goto end; |
4156 | 0 | } else |
4157 | 0 | rv |= CERT_PKEY_EE_SIGNATURE; |
4158 | 0 | rv |= CERT_PKEY_CA_SIGNATURE; |
4159 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
4160 | 0 | if (!tls1_check_sig_alg(s, sk_X509_value(chain, i), default_nid)) { |
4161 | 0 | if (check_flags) { |
4162 | 0 | rv &= ~CERT_PKEY_CA_SIGNATURE; |
4163 | 0 | break; |
4164 | 0 | } else |
4165 | 0 | goto end; |
4166 | 0 | } |
4167 | 0 | } |
4168 | 0 | } |
4169 | | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
4170 | 0 | else if (check_flags) |
4171 | 0 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; |
4172 | 0 | skip_sigs: |
4173 | | /* Check cert parameters are consistent */ |
4174 | 0 | if (tls1_check_cert_param(s, x, 1)) |
4175 | 0 | rv |= CERT_PKEY_EE_PARAM; |
4176 | 0 | else if (!check_flags) |
4177 | 0 | goto end; |
4178 | 0 | if (!s->server) |
4179 | 0 | rv |= CERT_PKEY_CA_PARAM; |
4180 | | /* In strict mode check rest of chain too */ |
4181 | 0 | else if (strict_mode) { |
4182 | 0 | rv |= CERT_PKEY_CA_PARAM; |
4183 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
4184 | 0 | X509 *ca = sk_X509_value(chain, i); |
4185 | 0 | if (!tls1_check_cert_param(s, ca, 0)) { |
4186 | 0 | if (check_flags) { |
4187 | 0 | rv &= ~CERT_PKEY_CA_PARAM; |
4188 | 0 | break; |
4189 | 0 | } else |
4190 | 0 | goto end; |
4191 | 0 | } |
4192 | 0 | } |
4193 | 0 | } |
4194 | 0 | if (!s->server && strict_mode) { |
4195 | 0 | STACK_OF(X509_NAME) *ca_dn; |
4196 | 0 | int check_type = 0; |
4197 | |
|
4198 | 0 | if (EVP_PKEY_is_a(pk, "RSA")) |
4199 | 0 | check_type = TLS_CT_RSA_SIGN; |
4200 | 0 | else if (EVP_PKEY_is_a(pk, "DSA")) |
4201 | 0 | check_type = TLS_CT_DSS_SIGN; |
4202 | 0 | else if (EVP_PKEY_is_a(pk, "EC")) |
4203 | 0 | check_type = TLS_CT_ECDSA_SIGN; |
4204 | |
|
4205 | 0 | if (check_type) { |
4206 | 0 | const uint8_t *ctypes = s->s3.tmp.ctype; |
4207 | 0 | size_t j; |
4208 | |
|
4209 | 0 | for (j = 0; j < s->s3.tmp.ctype_len; j++, ctypes++) { |
4210 | 0 | if (*ctypes == check_type) { |
4211 | 0 | rv |= CERT_PKEY_CERT_TYPE; |
4212 | 0 | break; |
4213 | 0 | } |
4214 | 0 | } |
4215 | 0 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
4216 | 0 | goto end; |
4217 | 0 | } else { |
4218 | 0 | rv |= CERT_PKEY_CERT_TYPE; |
4219 | 0 | } |
4220 | | |
4221 | 0 | ca_dn = s->s3.tmp.peer_ca_names; |
4222 | |
|
4223 | 0 | if (ca_dn == NULL |
4224 | 0 | || sk_X509_NAME_num(ca_dn) == 0 |
4225 | 0 | || ssl_check_ca_name(ca_dn, x)) |
4226 | 0 | rv |= CERT_PKEY_ISSUER_NAME; |
4227 | 0 | else |
4228 | 0 | for (i = 0; i < sk_X509_num(chain); i++) { |
4229 | 0 | X509 *xtmp = sk_X509_value(chain, i); |
4230 | |
|
4231 | 0 | if (ssl_check_ca_name(ca_dn, xtmp)) { |
4232 | 0 | rv |= CERT_PKEY_ISSUER_NAME; |
4233 | 0 | break; |
4234 | 0 | } |
4235 | 0 | } |
4236 | |
|
4237 | 0 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
4238 | 0 | goto end; |
4239 | 0 | } else |
4240 | 0 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; |
4241 | | |
4242 | 0 | if (!check_flags || (rv & check_flags) == check_flags) |
4243 | 0 | rv |= CERT_PKEY_VALID; |
4244 | |
|
4245 | 0 | end: |
4246 | |
|
4247 | 0 | if (TLS1_get_version(SSL_CONNECTION_GET_SSL(s)) >= TLS1_2_VERSION) |
4248 | 0 | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); |
4249 | 0 | else |
4250 | 0 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
4251 | | |
4252 | | /* |
4253 | | * When checking a CERT_PKEY structure all flags are irrelevant if the |
4254 | | * chain is invalid. |
4255 | | */ |
4256 | 0 | if (!check_flags) { |
4257 | 0 | if (rv & CERT_PKEY_VALID) { |
4258 | 0 | *pvalid = rv; |
4259 | 0 | } else { |
4260 | | /* Preserve sign and explicit sign flag, clear rest */ |
4261 | 0 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
4262 | 0 | return 0; |
4263 | 0 | } |
4264 | 0 | } |
4265 | 0 | return rv; |
4266 | 0 | } |
4267 | | |
4268 | | /* Set validity of certificates in an SSL structure */ |
4269 | | void tls1_set_cert_validity(SSL_CONNECTION *s) |
4270 | 0 | { |
4271 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
4272 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN); |
4273 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
4274 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
4275 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
4276 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); |
4277 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); |
4278 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); |
4279 | 0 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED448); |
4280 | 0 | } |
4281 | | |
4282 | | /* User level utility function to check a chain is suitable */ |
4283 | | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
4284 | 0 | { |
4285 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
4286 | |
|
4287 | 0 | if (sc == NULL) |
4288 | 0 | return 0; |
4289 | | |
4290 | 0 | return tls1_check_chain(sc, x, pk, chain, -1); |
4291 | 0 | } |
4292 | | |
4293 | | EVP_PKEY *ssl_get_auto_dh(SSL_CONNECTION *s) |
4294 | 0 | { |
4295 | 0 | EVP_PKEY *dhp = NULL; |
4296 | 0 | BIGNUM *p; |
4297 | 0 | int dh_secbits = 80, sec_level_bits; |
4298 | 0 | EVP_PKEY_CTX *pctx = NULL; |
4299 | 0 | OSSL_PARAM_BLD *tmpl = NULL; |
4300 | 0 | OSSL_PARAM *params = NULL; |
4301 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4302 | |
|
4303 | 0 | if (s->cert->dh_tmp_auto != 2) { |
4304 | 0 | if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
4305 | 0 | if (s->s3.tmp.new_cipher->strength_bits == 256) |
4306 | 0 | dh_secbits = 128; |
4307 | 0 | else |
4308 | 0 | dh_secbits = 80; |
4309 | 0 | } else { |
4310 | 0 | if (s->s3.tmp.cert == NULL) |
4311 | 0 | return NULL; |
4312 | 0 | dh_secbits = EVP_PKEY_get_security_bits(s->s3.tmp.cert->privatekey); |
4313 | 0 | } |
4314 | 0 | } |
4315 | | |
4316 | | /* Do not pick a prime that is too weak for the current security level */ |
4317 | 0 | sec_level_bits = ssl_get_security_level_bits(SSL_CONNECTION_GET_SSL(s), |
4318 | 0 | NULL, NULL); |
4319 | 0 | if (dh_secbits < sec_level_bits) |
4320 | 0 | dh_secbits = sec_level_bits; |
4321 | |
|
4322 | 0 | if (dh_secbits >= 192) |
4323 | 0 | p = BN_get_rfc3526_prime_8192(NULL); |
4324 | 0 | else if (dh_secbits >= 152) |
4325 | 0 | p = BN_get_rfc3526_prime_4096(NULL); |
4326 | 0 | else if (dh_secbits >= 128) |
4327 | 0 | p = BN_get_rfc3526_prime_3072(NULL); |
4328 | 0 | else if (dh_secbits >= 112) |
4329 | 0 | p = BN_get_rfc3526_prime_2048(NULL); |
4330 | 0 | else |
4331 | 0 | p = BN_get_rfc2409_prime_1024(NULL); |
4332 | 0 | if (p == NULL) |
4333 | 0 | goto err; |
4334 | | |
4335 | 0 | pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq); |
4336 | 0 | if (pctx == NULL |
4337 | 0 | || EVP_PKEY_fromdata_init(pctx) != 1) |
4338 | 0 | goto err; |
4339 | | |
4340 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
4341 | 0 | if (tmpl == NULL |
4342 | 0 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
4343 | 0 | || !OSSL_PARAM_BLD_push_uint(tmpl, OSSL_PKEY_PARAM_FFC_G, 2)) |
4344 | 0 | goto err; |
4345 | | |
4346 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
4347 | 0 | if (params == NULL |
4348 | 0 | || EVP_PKEY_fromdata(pctx, &dhp, EVP_PKEY_KEY_PARAMETERS, params) != 1) |
4349 | 0 | goto err; |
4350 | | |
4351 | 0 | err: |
4352 | 0 | OSSL_PARAM_free(params); |
4353 | 0 | OSSL_PARAM_BLD_free(tmpl); |
4354 | 0 | EVP_PKEY_CTX_free(pctx); |
4355 | 0 | BN_free(p); |
4356 | 0 | return dhp; |
4357 | 0 | } |
4358 | | |
4359 | | static int ssl_security_cert_key(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
4360 | | int op) |
4361 | 0 | { |
4362 | 0 | int secbits = -1; |
4363 | 0 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
4364 | |
|
4365 | 0 | if (pkey) { |
4366 | | /* |
4367 | | * If no parameters this will return -1 and fail using the default |
4368 | | * security callback for any non-zero security level. This will |
4369 | | * reject keys which omit parameters but this only affects DSA and |
4370 | | * omission of parameters is never (?) done in practice. |
4371 | | */ |
4372 | 0 | secbits = EVP_PKEY_get_security_bits(pkey); |
4373 | 0 | } |
4374 | 0 | if (s != NULL) |
4375 | 0 | return ssl_security(s, op, secbits, 0, x); |
4376 | 0 | else |
4377 | 0 | return ssl_ctx_security(ctx, op, secbits, 0, x); |
4378 | 0 | } |
4379 | | |
4380 | | static int ssl_security_cert_sig(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, |
4381 | | int op) |
4382 | 0 | { |
4383 | | /* Lookup signature algorithm digest */ |
4384 | 0 | int secbits, nid, pknid; |
4385 | | |
4386 | | /* Don't check signature if self signed */ |
4387 | 0 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) |
4388 | 0 | return 1; |
4389 | 0 | if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) |
4390 | 0 | secbits = -1; |
4391 | | /* If digest NID not defined use signature NID */ |
4392 | 0 | if (nid == NID_undef) |
4393 | 0 | nid = pknid; |
4394 | 0 | if (s != NULL) |
4395 | 0 | return ssl_security(s, op, secbits, nid, x); |
4396 | 0 | else |
4397 | 0 | return ssl_ctx_security(ctx, op, secbits, nid, x); |
4398 | 0 | } |
4399 | | |
4400 | | int ssl_security_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x, int vfy, |
4401 | | int is_ee) |
4402 | 0 | { |
4403 | 0 | if (vfy) |
4404 | 0 | vfy = SSL_SECOP_PEER; |
4405 | 0 | if (is_ee) { |
4406 | 0 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) |
4407 | 0 | return SSL_R_EE_KEY_TOO_SMALL; |
4408 | 0 | } else { |
4409 | 0 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) |
4410 | 0 | return SSL_R_CA_KEY_TOO_SMALL; |
4411 | 0 | } |
4412 | 0 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) |
4413 | 0 | return SSL_R_CA_MD_TOO_WEAK; |
4414 | 0 | return 1; |
4415 | 0 | } |
4416 | | |
4417 | | /* |
4418 | | * Check security of a chain, if |sk| includes the end entity certificate then |
4419 | | * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending |
4420 | | * one to the peer. Return values: 1 if ok otherwise error code to use |
4421 | | */ |
4422 | | |
4423 | | int ssl_security_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk, |
4424 | | X509 *x, int vfy) |
4425 | 0 | { |
4426 | 0 | int rv, start_idx, i; |
4427 | |
|
4428 | 0 | if (x == NULL) { |
4429 | 0 | x = sk_X509_value(sk, 0); |
4430 | 0 | if (x == NULL) |
4431 | 0 | return ERR_R_INTERNAL_ERROR; |
4432 | 0 | start_idx = 1; |
4433 | 0 | } else |
4434 | 0 | start_idx = 0; |
4435 | | |
4436 | 0 | rv = ssl_security_cert(s, NULL, x, vfy, 1); |
4437 | 0 | if (rv != 1) |
4438 | 0 | return rv; |
4439 | | |
4440 | 0 | for (i = start_idx; i < sk_X509_num(sk); i++) { |
4441 | 0 | x = sk_X509_value(sk, i); |
4442 | 0 | rv = ssl_security_cert(s, NULL, x, vfy, 0); |
4443 | 0 | if (rv != 1) |
4444 | 0 | return rv; |
4445 | 0 | } |
4446 | 0 | return 1; |
4447 | 0 | } |
4448 | | |
4449 | | /* |
4450 | | * For TLS 1.2 servers check if we have a certificate which can be used |
4451 | | * with the signature algorithm "lu" and return index of certificate. |
4452 | | */ |
4453 | | |
4454 | | static int tls12_get_cert_sigalg_idx(const SSL_CONNECTION *s, |
4455 | | const SIGALG_LOOKUP *lu) |
4456 | 0 | { |
4457 | 0 | int sig_idx = lu->sig_idx; |
4458 | 0 | const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx, |
4459 | 0 | SSL_CONNECTION_GET_CTX(s)); |
4460 | | |
4461 | | /* If not recognised or not supported by cipher mask it is not suitable */ |
4462 | 0 | if (clu == NULL |
4463 | 0 | || (clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0 |
4464 | 0 | || (clu->nid == EVP_PKEY_RSA_PSS |
4465 | 0 | && (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kRSA) != 0)) |
4466 | 0 | return -1; |
4467 | | |
4468 | | /* If doing RPK, the CERT_PKEY won't be "valid" */ |
4469 | 0 | if (tls12_rpk_and_privkey(s, sig_idx)) |
4470 | 0 | return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_RPK ? sig_idx : -1; |
4471 | | |
4472 | 0 | return s->s3.tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1; |
4473 | 0 | } |
4474 | | |
4475 | | /* |
4476 | | * Checks the given cert against signature_algorithm_cert restrictions sent by |
4477 | | * the peer (if any) as well as whether the hash from the sigalg is usable with |
4478 | | * the key. |
4479 | | * Returns true if the cert is usable and false otherwise. |
4480 | | */ |
4481 | | static int check_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, |
4482 | | X509 *x, EVP_PKEY *pkey) |
4483 | 0 | { |
4484 | 0 | const SIGALG_LOOKUP *lu; |
4485 | 0 | int mdnid, pknid, supported; |
4486 | 0 | size_t i; |
4487 | 0 | const char *mdname = NULL; |
4488 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4489 | | |
4490 | | /* |
4491 | | * If the given EVP_PKEY cannot support signing with this digest, |
4492 | | * the answer is simply 'no'. |
4493 | | */ |
4494 | 0 | if (sig->hash != NID_undef) |
4495 | 0 | mdname = OBJ_nid2sn(sig->hash); |
4496 | 0 | supported = EVP_PKEY_digestsign_supports_digest(pkey, sctx->libctx, |
4497 | 0 | mdname, |
4498 | 0 | sctx->propq); |
4499 | 0 | if (supported <= 0) |
4500 | 0 | return 0; |
4501 | | |
4502 | | /* |
4503 | | * The TLS 1.3 signature_algorithms_cert extension places restrictions |
4504 | | * on the sigalg with which the certificate was signed (by its issuer). |
4505 | | */ |
4506 | 0 | if (s->s3.tmp.peer_cert_sigalgs != NULL) { |
4507 | 0 | if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL)) |
4508 | 0 | return 0; |
4509 | 0 | for (i = 0; i < s->s3.tmp.peer_cert_sigalgslen; i++) { |
4510 | 0 | lu = tls1_lookup_sigalg(SSL_CONNECTION_GET_CTX(s), |
4511 | 0 | s->s3.tmp.peer_cert_sigalgs[i]); |
4512 | 0 | if (lu == NULL) |
4513 | 0 | continue; |
4514 | | |
4515 | | /* |
4516 | | * This does not differentiate between the |
4517 | | * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not |
4518 | | * have a chain here that lets us look at the key OID in the |
4519 | | * signing certificate. |
4520 | | */ |
4521 | 0 | if (mdnid == lu->hash && pknid == lu->sig) |
4522 | 0 | return 1; |
4523 | 0 | } |
4524 | 0 | return 0; |
4525 | 0 | } |
4526 | | |
4527 | | /* |
4528 | | * Without signat_algorithms_cert, any certificate for which we have |
4529 | | * a viable public key is permitted. |
4530 | | */ |
4531 | 0 | return 1; |
4532 | 0 | } |
4533 | | |
4534 | | /* |
4535 | | * Returns true if |s| has a usable certificate configured for use |
4536 | | * with signature scheme |sig|. |
4537 | | * "Usable" includes a check for presence as well as applying |
4538 | | * the signature_algorithm_cert restrictions sent by the peer (if any). |
4539 | | * Returns false if no usable certificate is found. |
4540 | | */ |
4541 | | static int has_usable_cert(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, int idx) |
4542 | 0 | { |
4543 | | /* TLS 1.2 callers can override sig->sig_idx, but not TLS 1.3 callers. */ |
4544 | 0 | if (idx == -1) |
4545 | 0 | idx = sig->sig_idx; |
4546 | 0 | if (!ssl_has_cert(s, idx)) |
4547 | 0 | return 0; |
4548 | | |
4549 | 0 | return check_cert_usable(s, sig, s->cert->pkeys[idx].x509, |
4550 | 0 | s->cert->pkeys[idx].privatekey); |
4551 | 0 | } |
4552 | | |
4553 | | /* |
4554 | | * Returns true if the supplied cert |x| and key |pkey| is usable with the |
4555 | | * specified signature scheme |sig|, or false otherwise. |
4556 | | */ |
4557 | | static int is_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig, X509 *x, |
4558 | | EVP_PKEY *pkey) |
4559 | 0 | { |
4560 | 0 | size_t idx; |
4561 | |
|
4562 | 0 | if (ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)) == NULL) |
4563 | 0 | return 0; |
4564 | | |
4565 | | /* Check the key is consistent with the sig alg */ |
4566 | 0 | if ((int)idx != sig->sig_idx) |
4567 | 0 | return 0; |
4568 | | |
4569 | 0 | return check_cert_usable(s, sig, x, pkey); |
4570 | 0 | } |
4571 | | |
4572 | | /* |
4573 | | * Find a signature scheme that works with the supplied certificate |x| and key |
4574 | | * |pkey|. |x| and |pkey| may be NULL in which case we additionally look at our |
4575 | | * available certs/keys to find one that works. |
4576 | | */ |
4577 | | static const SIGALG_LOOKUP *find_sig_alg(SSL_CONNECTION *s, X509 *x, |
4578 | | EVP_PKEY *pkey) |
4579 | 0 | { |
4580 | 0 | const SIGALG_LOOKUP *lu = NULL; |
4581 | 0 | size_t i; |
4582 | 0 | int curve = -1; |
4583 | 0 | EVP_PKEY *tmppkey; |
4584 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4585 | | |
4586 | | /* Look for a shared sigalgs matching possible certificates */ |
4587 | 0 | for (i = 0; i < s->shared_sigalgslen; i++) { |
4588 | | /* Skip SHA1, SHA224, DSA and RSA if not PSS */ |
4589 | 0 | lu = s->shared_sigalgs[i]; |
4590 | 0 | if (lu->hash == NID_sha1 |
4591 | 0 | || lu->hash == NID_sha224 |
4592 | 0 | || lu->sig == EVP_PKEY_DSA |
4593 | 0 | || lu->sig == EVP_PKEY_RSA |
4594 | 0 | || !tls_sigalg_compat(s, lu)) |
4595 | 0 | continue; |
4596 | | |
4597 | | /* Check that we have a cert, and signature_algorithms_cert */ |
4598 | 0 | if (!tls1_lookup_md(sctx, lu, NULL)) |
4599 | 0 | continue; |
4600 | 0 | if ((pkey == NULL && !has_usable_cert(s, lu, -1)) |
4601 | 0 | || (pkey != NULL && !is_cert_usable(s, lu, x, pkey))) |
4602 | 0 | continue; |
4603 | | |
4604 | 0 | tmppkey = (pkey != NULL) ? pkey |
4605 | 0 | : s->cert->pkeys[lu->sig_idx].privatekey; |
4606 | |
|
4607 | 0 | if (lu->sig == EVP_PKEY_EC) { |
4608 | 0 | if (curve == -1) |
4609 | 0 | curve = ssl_get_EC_curve_nid(tmppkey); |
4610 | 0 | if (lu->curve != NID_undef && curve != lu->curve) |
4611 | 0 | continue; |
4612 | 0 | } else if (lu->sig == EVP_PKEY_RSA_PSS) { |
4613 | | /* validate that key is large enough for the signature algorithm */ |
4614 | 0 | if (!rsa_pss_check_min_key_size(sctx, tmppkey, lu)) |
4615 | 0 | continue; |
4616 | 0 | } |
4617 | 0 | break; |
4618 | 0 | } |
4619 | |
|
4620 | 0 | if (i == s->shared_sigalgslen) |
4621 | 0 | return NULL; |
4622 | | |
4623 | 0 | return lu; |
4624 | 0 | } |
4625 | | |
4626 | | /* |
4627 | | * Choose an appropriate signature algorithm based on available certificates |
4628 | | * Sets chosen certificate and signature algorithm. |
4629 | | * |
4630 | | * For servers if we fail to find a required certificate it is a fatal error, |
4631 | | * an appropriate error code is set and a TLS alert is sent. |
4632 | | * |
4633 | | * For clients fatalerrs is set to 0. If a certificate is not suitable it is not |
4634 | | * a fatal error: we will either try another certificate or not present one |
4635 | | * to the server. In this case no error is set. |
4636 | | */ |
4637 | | int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs) |
4638 | 0 | { |
4639 | 0 | const SIGALG_LOOKUP *lu = NULL; |
4640 | 0 | int sig_idx = -1; |
4641 | |
|
4642 | 0 | s->s3.tmp.cert = NULL; |
4643 | 0 | s->s3.tmp.sigalg = NULL; |
4644 | |
|
4645 | 0 | if (SSL_CONNECTION_IS_TLS13(s)) { |
4646 | 0 | lu = find_sig_alg(s, NULL, NULL); |
4647 | 0 | if (lu == NULL) { |
4648 | 0 | if (!fatalerrs) |
4649 | 0 | return 1; |
4650 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4651 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4652 | 0 | return 0; |
4653 | 0 | } |
4654 | 0 | } else { |
4655 | | /* If ciphersuite doesn't require a cert nothing to do */ |
4656 | 0 | if (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aCERT)) |
4657 | 0 | return 1; |
4658 | 0 | if (!s->server && !ssl_has_cert(s, (int)(s->cert->key - s->cert->pkeys))) |
4659 | 0 | return 1; |
4660 | | |
4661 | 0 | if (SSL_USE_SIGALGS(s)) { |
4662 | 0 | size_t i; |
4663 | 0 | if (s->s3.tmp.peer_sigalgs != NULL) { |
4664 | 0 | int curve = -1; |
4665 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
4666 | | |
4667 | | /* For Suite B need to match signature algorithm to curve */ |
4668 | 0 | if (tls1_suiteb(s)) |
4669 | 0 | curve = ssl_get_EC_curve_nid(s->cert->pkeys[SSL_PKEY_ECC] |
4670 | 0 | .privatekey); |
4671 | | |
4672 | | /* |
4673 | | * Find highest preference signature algorithm matching |
4674 | | * cert type |
4675 | | */ |
4676 | 0 | for (i = 0; i < s->shared_sigalgslen; i++) { |
4677 | | /* Check the sigalg version bounds */ |
4678 | 0 | lu = s->shared_sigalgs[i]; |
4679 | 0 | if (!tls_sigalg_compat(s, lu)) |
4680 | 0 | continue; |
4681 | 0 | if (s->server) { |
4682 | 0 | if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1) |
4683 | 0 | continue; |
4684 | 0 | } else { |
4685 | 0 | int cc_idx = (int)(s->cert->key - s->cert->pkeys); |
4686 | |
|
4687 | 0 | sig_idx = lu->sig_idx; |
4688 | 0 | if (cc_idx != sig_idx) |
4689 | 0 | continue; |
4690 | 0 | } |
4691 | | /* Check that we have a cert, and sig_algs_cert */ |
4692 | 0 | if (!has_usable_cert(s, lu, sig_idx)) |
4693 | 0 | continue; |
4694 | 0 | if (lu->sig == EVP_PKEY_RSA_PSS) { |
4695 | | /* validate that key is large enough for the signature algorithm */ |
4696 | 0 | EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey; |
4697 | |
|
4698 | 0 | if (!rsa_pss_check_min_key_size(sctx, pkey, lu)) |
4699 | 0 | continue; |
4700 | 0 | } |
4701 | 0 | if (curve == -1 || lu->curve == curve) |
4702 | 0 | break; |
4703 | 0 | } |
4704 | 0 | #ifndef OPENSSL_NO_GOST |
4705 | | /* |
4706 | | * Some Windows-based implementations do not send GOST algorithms indication |
4707 | | * in supported_algorithms extension, so when we have GOST-based ciphersuite, |
4708 | | * we have to assume GOST support. |
4709 | | */ |
4710 | 0 | if (i == s->shared_sigalgslen |
4711 | 0 | && (s->s3.tmp.new_cipher->algorithm_auth |
4712 | 0 | & (SSL_aGOST01 | SSL_aGOST12)) != 0) { |
4713 | 0 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
4714 | 0 | if (!fatalerrs) |
4715 | 0 | return 1; |
4716 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4717 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4718 | 0 | return 0; |
4719 | 0 | } else { |
4720 | 0 | i = 0; |
4721 | 0 | sig_idx = lu->sig_idx; |
4722 | 0 | } |
4723 | 0 | } |
4724 | 0 | #endif |
4725 | 0 | if (i == s->shared_sigalgslen) { |
4726 | 0 | if (!fatalerrs) |
4727 | 0 | return 1; |
4728 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4729 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4730 | 0 | return 0; |
4731 | 0 | } |
4732 | 0 | } else { |
4733 | | /* |
4734 | | * If we have no sigalg use defaults |
4735 | | */ |
4736 | 0 | const uint16_t *sent_sigs; |
4737 | 0 | size_t sent_sigslen; |
4738 | |
|
4739 | 0 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
4740 | 0 | if (!fatalerrs) |
4741 | 0 | return 1; |
4742 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4743 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4744 | 0 | return 0; |
4745 | 0 | } |
4746 | | |
4747 | | /* Check signature matches a type we sent */ |
4748 | 0 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
4749 | 0 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
4750 | 0 | if (lu->sigalg == *sent_sigs |
4751 | 0 | && has_usable_cert(s, lu, lu->sig_idx)) |
4752 | 0 | break; |
4753 | 0 | } |
4754 | 0 | if (i == sent_sigslen) { |
4755 | 0 | if (!fatalerrs) |
4756 | 0 | return 1; |
4757 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
4758 | 0 | SSL_R_WRONG_SIGNATURE_TYPE); |
4759 | 0 | return 0; |
4760 | 0 | } |
4761 | 0 | } |
4762 | 0 | } else { |
4763 | 0 | if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) { |
4764 | 0 | if (!fatalerrs) |
4765 | 0 | return 1; |
4766 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
4767 | 0 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); |
4768 | 0 | return 0; |
4769 | 0 | } |
4770 | 0 | } |
4771 | 0 | } |
4772 | 0 | if (sig_idx == -1) |
4773 | 0 | sig_idx = lu->sig_idx; |
4774 | 0 | s->s3.tmp.cert = &s->cert->pkeys[sig_idx]; |
4775 | 0 | s->cert->key = s->s3.tmp.cert; |
4776 | 0 | s->s3.tmp.sigalg = lu; |
4777 | 0 | return 1; |
4778 | 0 | } |
4779 | | |
4780 | | int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode) |
4781 | 0 | { |
4782 | 0 | if (mode != TLSEXT_max_fragment_length_DISABLED |
4783 | 0 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { |
4784 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
4785 | 0 | return 0; |
4786 | 0 | } |
4787 | | |
4788 | 0 | ctx->ext.max_fragment_len_mode = mode; |
4789 | 0 | return 1; |
4790 | 0 | } |
4791 | | |
4792 | | int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode) |
4793 | 0 | { |
4794 | 0 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl); |
4795 | |
|
4796 | 0 | if (sc == NULL |
4797 | 0 | || (IS_QUIC(ssl) && mode != TLSEXT_max_fragment_length_DISABLED)) |
4798 | 0 | return 0; |
4799 | | |
4800 | 0 | if (mode != TLSEXT_max_fragment_length_DISABLED |
4801 | 0 | && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) { |
4802 | 0 | ERR_raise(ERR_LIB_SSL, SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
4803 | 0 | return 0; |
4804 | 0 | } |
4805 | | |
4806 | 0 | sc->ext.max_fragment_len_mode = mode; |
4807 | 0 | return 1; |
4808 | 0 | } |
4809 | | |
4810 | | uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session) |
4811 | 0 | { |
4812 | 0 | if (session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED) |
4813 | 0 | return TLSEXT_max_fragment_length_DISABLED; |
4814 | 0 | return session->ext.max_fragment_len_mode; |
4815 | 0 | } |
4816 | | |
4817 | | /* |
4818 | | * Helper functions for HMAC access with legacy support included. |
4819 | | */ |
4820 | | SSL_HMAC *ssl_hmac_new(const SSL_CTX *ctx) |
4821 | 0 | { |
4822 | 0 | SSL_HMAC *ret = OPENSSL_zalloc(sizeof(*ret)); |
4823 | 0 | EVP_MAC *mac = NULL; |
4824 | |
|
4825 | 0 | if (ret == NULL) |
4826 | 0 | return NULL; |
4827 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4828 | 0 | if (ctx->ext.ticket_key_evp_cb == NULL |
4829 | 0 | && ctx->ext.ticket_key_cb != NULL) { |
4830 | 0 | if (!ssl_hmac_old_new(ret)) |
4831 | 0 | goto err; |
4832 | 0 | return ret; |
4833 | 0 | } |
4834 | 0 | #endif |
4835 | 0 | mac = EVP_MAC_fetch(ctx->libctx, "HMAC", ctx->propq); |
4836 | 0 | if (mac == NULL || (ret->ctx = EVP_MAC_CTX_new(mac)) == NULL) |
4837 | 0 | goto err; |
4838 | 0 | EVP_MAC_free(mac); |
4839 | 0 | return ret; |
4840 | 0 | err: |
4841 | 0 | EVP_MAC_CTX_free(ret->ctx); |
4842 | 0 | EVP_MAC_free(mac); |
4843 | 0 | OPENSSL_free(ret); |
4844 | 0 | return NULL; |
4845 | 0 | } |
4846 | | |
4847 | | void ssl_hmac_free(SSL_HMAC *ctx) |
4848 | 0 | { |
4849 | 0 | if (ctx != NULL) { |
4850 | 0 | EVP_MAC_CTX_free(ctx->ctx); |
4851 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4852 | 0 | ssl_hmac_old_free(ctx); |
4853 | 0 | #endif |
4854 | 0 | OPENSSL_free(ctx); |
4855 | 0 | } |
4856 | 0 | } |
4857 | | |
4858 | | EVP_MAC_CTX *ssl_hmac_get0_EVP_MAC_CTX(SSL_HMAC *ctx) |
4859 | 0 | { |
4860 | 0 | return ctx->ctx; |
4861 | 0 | } |
4862 | | |
4863 | | int ssl_hmac_init(SSL_HMAC *ctx, void *key, size_t len, char *md) |
4864 | 0 | { |
4865 | 0 | OSSL_PARAM params[2], *p = params; |
4866 | |
|
4867 | 0 | if (ctx->ctx != NULL) { |
4868 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, md, 0); |
4869 | 0 | *p = OSSL_PARAM_construct_end(); |
4870 | 0 | if (EVP_MAC_init(ctx->ctx, key, len, params)) |
4871 | 0 | return 1; |
4872 | 0 | } |
4873 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4874 | 0 | if (ctx->old_ctx != NULL) |
4875 | 0 | return ssl_hmac_old_init(ctx, key, len, md); |
4876 | 0 | #endif |
4877 | 0 | return 0; |
4878 | 0 | } |
4879 | | |
4880 | | int ssl_hmac_update(SSL_HMAC *ctx, const unsigned char *data, size_t len) |
4881 | 0 | { |
4882 | 0 | if (ctx->ctx != NULL) |
4883 | 0 | return EVP_MAC_update(ctx->ctx, data, len); |
4884 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4885 | 0 | if (ctx->old_ctx != NULL) |
4886 | 0 | return ssl_hmac_old_update(ctx, data, len); |
4887 | 0 | #endif |
4888 | 0 | return 0; |
4889 | 0 | } |
4890 | | |
4891 | | int ssl_hmac_final(SSL_HMAC *ctx, unsigned char *md, size_t *len, |
4892 | | size_t max_size) |
4893 | 0 | { |
4894 | 0 | if (ctx->ctx != NULL) |
4895 | 0 | return EVP_MAC_final(ctx->ctx, md, len, max_size); |
4896 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4897 | 0 | if (ctx->old_ctx != NULL) |
4898 | 0 | return ssl_hmac_old_final(ctx, md, len); |
4899 | 0 | #endif |
4900 | 0 | return 0; |
4901 | 0 | } |
4902 | | |
4903 | | size_t ssl_hmac_size(const SSL_HMAC *ctx) |
4904 | 0 | { |
4905 | 0 | if (ctx->ctx != NULL) |
4906 | 0 | return EVP_MAC_CTX_get_mac_size(ctx->ctx); |
4907 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
4908 | 0 | if (ctx->old_ctx != NULL) |
4909 | 0 | return ssl_hmac_old_size(ctx); |
4910 | 0 | #endif |
4911 | 0 | return 0; |
4912 | 0 | } |
4913 | | |
4914 | | int ssl_get_EC_curve_nid(const EVP_PKEY *pkey) |
4915 | 0 | { |
4916 | 0 | char gname[OSSL_MAX_NAME_SIZE]; |
4917 | |
|
4918 | 0 | if (EVP_PKEY_get_group_name(pkey, gname, sizeof(gname), NULL) > 0) |
4919 | 0 | return OBJ_txt2nid(gname); |
4920 | | |
4921 | 0 | return NID_undef; |
4922 | 0 | } |
4923 | | |
4924 | | __owur int tls13_set_encoded_pub_key(EVP_PKEY *pkey, |
4925 | | const unsigned char *enckey, |
4926 | | size_t enckeylen) |
4927 | 0 | { |
4928 | 0 | if (EVP_PKEY_is_a(pkey, "DH")) { |
4929 | 0 | int bits = EVP_PKEY_get_bits(pkey); |
4930 | |
|
4931 | 0 | if (bits <= 0 || enckeylen != (size_t)bits / 8) |
4932 | | /* the encoded key must be padded to the length of the p */ |
4933 | 0 | return 0; |
4934 | 0 | } else if (EVP_PKEY_is_a(pkey, "EC")) { |
4935 | 0 | if (enckeylen < 3 /* point format and at least 1 byte for x and y */ |
4936 | 0 | || enckey[0] != 0x04) |
4937 | 0 | return 0; |
4938 | 0 | } |
4939 | | |
4940 | 0 | return EVP_PKEY_set1_encoded_public_key(pkey, enckey, enckeylen); |
4941 | 0 | } |