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