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