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