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