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