]>
Commit | Line | Data |
---|---|---|
846e33c7 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
f1fd4544 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
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> | |
67c8e7f4 | 15 | #include <openssl/ocsp.h> |
5951e840 MC |
16 | #include <openssl/conf.h> |
17 | #include <openssl/x509v3.h> | |
3c27208f RS |
18 | #include <openssl/dh.h> |
19 | #include <openssl/bn.h> | |
58964a49 | 20 | #include "ssl_locl.h" |
3c27208f | 21 | #include <openssl/ct.h> |
58964a49 | 22 | |
0f113f3e MC |
23 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
24 | tls1_enc, | |
25 | tls1_mac, | |
26 | tls1_setup_key_block, | |
27 | tls1_generate_master_secret, | |
28 | tls1_change_cipher_state, | |
29 | tls1_final_finish_mac, | |
0f113f3e MC |
30 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
31 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
32 | tls1_alert_code, | |
33 | tls1_export_keying_material, | |
34 | 0, | |
a29fa98c | 35 | ssl3_set_handshake_header, |
2c7b4dbc | 36 | tls_close_construct_packet, |
0f113f3e MC |
37 | ssl3_handshake_write |
38 | }; | |
39 | ||
40 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { | |
41 | tls1_enc, | |
42 | tls1_mac, | |
43 | tls1_setup_key_block, | |
44 | tls1_generate_master_secret, | |
45 | tls1_change_cipher_state, | |
46 | tls1_final_finish_mac, | |
0f113f3e MC |
47 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
48 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
49 | tls1_alert_code, | |
50 | tls1_export_keying_material, | |
51 | SSL_ENC_FLAG_EXPLICIT_IV, | |
a29fa98c | 52 | ssl3_set_handshake_header, |
2c7b4dbc | 53 | tls_close_construct_packet, |
0f113f3e MC |
54 | ssl3_handshake_write |
55 | }; | |
56 | ||
57 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { | |
58 | tls1_enc, | |
59 | tls1_mac, | |
60 | tls1_setup_key_block, | |
61 | tls1_generate_master_secret, | |
62 | tls1_change_cipher_state, | |
63 | tls1_final_finish_mac, | |
0f113f3e MC |
64 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
65 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
66 | tls1_alert_code, | |
67 | tls1_export_keying_material, | |
68 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF | |
69 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, | |
a29fa98c | 70 | ssl3_set_handshake_header, |
2c7b4dbc | 71 | tls_close_construct_packet, |
0f113f3e MC |
72 | ssl3_handshake_write |
73 | }; | |
58964a49 | 74 | |
582a17d6 | 75 | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
bebc0c7d | 76 | tls13_enc, |
582a17d6 | 77 | tls1_mac, |
92760c21 MC |
78 | tls13_setup_key_block, |
79 | tls13_generate_master_secret, | |
80 | tls13_change_cipher_state, | |
81 | tls13_final_finish_mac, | |
582a17d6 MC |
82 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
83 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
04904312 | 84 | tls13_alert_code, |
582a17d6 | 85 | tls1_export_keying_material, |
bebc0c7d | 86 | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF, |
582a17d6 MC |
87 | ssl3_set_handshake_header, |
88 | tls_close_construct_packet, | |
89 | ssl3_handshake_write | |
90 | }; | |
91 | ||
f3b656b2 | 92 | long tls1_default_timeout(void) |
0f113f3e MC |
93 | { |
94 | /* | |
95 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for | |
96 | * http, the cache would over fill | |
97 | */ | |
98 | return (60 * 60 * 2); | |
99 | } | |
58964a49 | 100 | |
6b691a5c | 101 | int tls1_new(SSL *s) |
0f113f3e MC |
102 | { |
103 | if (!ssl3_new(s)) | |
b77f3ed1 MC |
104 | return 0; |
105 | if (!s->method->ssl_clear(s)) | |
106 | return 0; | |
107 | ||
108 | return 1; | |
0f113f3e | 109 | } |
58964a49 | 110 | |
6b691a5c | 111 | void tls1_free(SSL *s) |
0f113f3e | 112 | { |
aff8c126 | 113 | OPENSSL_free(s->ext.session_ticket); |
0f113f3e MC |
114 | ssl3_free(s); |
115 | } | |
58964a49 | 116 | |
b77f3ed1 | 117 | int tls1_clear(SSL *s) |
0f113f3e | 118 | { |
b77f3ed1 MC |
119 | if (!ssl3_clear(s)) |
120 | return 0; | |
121 | ||
4fa52141 VD |
122 | if (s->method->version == TLS_ANY_VERSION) |
123 | s->version = TLS_MAX_VERSION; | |
124 | else | |
125 | s->version = s->method->version; | |
b77f3ed1 MC |
126 | |
127 | return 1; | |
0f113f3e | 128 | } |
58964a49 | 129 | |
525de5d3 | 130 | #ifndef OPENSSL_NO_EC |
eda3766b | 131 | |
0f113f3e MC |
132 | typedef struct { |
133 | int nid; /* Curve NID */ | |
134 | int secbits; /* Bits of security (from SP800-57) */ | |
135 | unsigned int flags; /* Flags: currently just field type */ | |
136 | } tls_curve_info; | |
137 | ||
2dc1aeed DSH |
138 | /* |
139 | * Table of curve information. | |
ddb4c047 | 140 | * Do not delete entries or reorder this array! It is used as a lookup |
2dc1aeed DSH |
141 | * table: the index of each entry is one less than the TLS curve id. |
142 | */ | |
0f113f3e MC |
143 | static const tls_curve_info nid_list[] = { |
144 | {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */ | |
145 | {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */ | |
146 | {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */ | |
147 | {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */ | |
148 | {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */ | |
149 | {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */ | |
150 | {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */ | |
151 | {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */ | |
152 | {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */ | |
153 | {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */ | |
154 | {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */ | |
155 | {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */ | |
156 | {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */ | |
157 | {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */ | |
158 | {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */ | |
159 | {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */ | |
160 | {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */ | |
161 | {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */ | |
162 | {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */ | |
163 | {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */ | |
164 | {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */ | |
165 | {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */ | |
166 | {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */ | |
167 | {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */ | |
168 | {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */ | |
169 | {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */ | |
170 | {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */ | |
171 | {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */ | |
ec24630a | 172 | {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */ |
0f113f3e MC |
173 | }; |
174 | ||
175 | static const unsigned char ecformats_default[] = { | |
176 | TLSEXT_ECPOINTFORMAT_uncompressed, | |
177 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, | |
178 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 | |
179 | }; | |
180 | ||
fe6ef247 KR |
181 | /* The default curves */ |
182 | static const unsigned char eccurves_default[] = { | |
1db3107a | 183 | 0, 29, /* X25519 (29) */ |
de57d237 | 184 | 0, 23, /* secp256r1 (23) */ |
0f113f3e | 185 | 0, 25, /* secp521r1 (25) */ |
0f113f3e | 186 | 0, 24, /* secp384r1 (24) */ |
de57d237 EK |
187 | }; |
188 | ||
0f113f3e MC |
189 | static const unsigned char suiteb_curves[] = { |
190 | 0, TLSEXT_curve_P_256, | |
191 | 0, TLSEXT_curve_P_384 | |
192 | }; | |
2ea80354 | 193 | |
ec24630a | 194 | int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags) |
0f113f3e | 195 | { |
ec24630a | 196 | const tls_curve_info *cinfo; |
0f113f3e | 197 | /* ECC curves from RFC 4492 and RFC 7027 */ |
b6eb9827 | 198 | if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list))) |
0f113f3e | 199 | return 0; |
ec24630a DSH |
200 | cinfo = nid_list + curve_id - 1; |
201 | if (pflags) | |
202 | *pflags = cinfo->flags; | |
203 | return cinfo->nid; | |
0f113f3e | 204 | } |
525de5d3 DSH |
205 | |
206 | int tls1_ec_nid2curve_id(int nid) | |
0f113f3e | 207 | { |
2fa2d15a DSH |
208 | size_t i; |
209 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { | |
210 | if (nid_list[i].nid == nid) | |
348240c6 | 211 | return (int)(i + 1); |
0f113f3e | 212 | } |
2fa2d15a | 213 | return 0; |
0f113f3e MC |
214 | } |
215 | ||
740580c2 EK |
216 | /* |
217 | * Get curves list, if "sess" is set return client curves otherwise | |
218 | * preferred list. | |
219 | * Sets |num_curves| to the number of curves in the list, i.e., | |
220 | * the length of |pcurves| is 2 * num_curves. | |
221 | * Returns 1 on success and 0 if the client curves list has invalid format. | |
222 | * The latter indicates an internal error: we should not be accepting such | |
223 | * lists in the first place. | |
224 | * TODO(emilia): we should really be storing the curves list in explicitly | |
225 | * parsed form instead. (However, this would affect binary compatibility | |
226 | * so cannot happen in the 1.0.x series.) | |
fd2b65ce | 227 | */ |
6b473aca MC |
228 | int tls1_get_curvelist(SSL *s, int sess, const unsigned char **pcurves, |
229 | size_t *num_curves) | |
0f113f3e MC |
230 | { |
231 | size_t pcurveslen = 0; | |
3e373518 | 232 | |
0f113f3e | 233 | if (sess) { |
aff8c126 RS |
234 | *pcurves = s->session->ext.supportedgroups; |
235 | pcurveslen = s->session->ext.supportedgroups_len; | |
0f113f3e MC |
236 | } else { |
237 | /* For Suite B mode only include P-256, P-384 */ | |
238 | switch (tls1_suiteb(s)) { | |
239 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
240 | *pcurves = suiteb_curves; | |
241 | pcurveslen = sizeof(suiteb_curves); | |
242 | break; | |
243 | ||
244 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
245 | *pcurves = suiteb_curves; | |
246 | pcurveslen = 2; | |
247 | break; | |
248 | ||
249 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
250 | *pcurves = suiteb_curves + 2; | |
251 | pcurveslen = 2; | |
252 | break; | |
253 | default: | |
aff8c126 RS |
254 | *pcurves = s->ext.supportedgroups; |
255 | pcurveslen = s->ext.supportedgroups_len; | |
0f113f3e MC |
256 | } |
257 | if (!*pcurves) { | |
fe6ef247 KR |
258 | *pcurves = eccurves_default; |
259 | pcurveslen = sizeof(eccurves_default); | |
0f113f3e MC |
260 | } |
261 | } | |
262 | ||
263 | /* We do not allow odd length arrays to enter the system. */ | |
264 | if (pcurveslen & 1) { | |
265 | SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR); | |
266 | *num_curves = 0; | |
267 | return 0; | |
0f113f3e | 268 | } |
3e373518 RS |
269 | *num_curves = pcurveslen / 2; |
270 | return 1; | |
0f113f3e | 271 | } |
b362ccab DSH |
272 | |
273 | /* See if curve is allowed by security callback */ | |
6b473aca | 274 | int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) |
0f113f3e MC |
275 | { |
276 | const tls_curve_info *cinfo; | |
277 | if (curve[0]) | |
278 | return 1; | |
b6eb9827 | 279 | if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) |
0f113f3e MC |
280 | return 0; |
281 | cinfo = &nid_list[curve[1] - 1]; | |
282 | # ifdef OPENSSL_NO_EC2M | |
283 | if (cinfo->flags & TLS_CURVE_CHAR2) | |
284 | return 0; | |
285 | # endif | |
286 | return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); | |
287 | } | |
b362ccab | 288 | |
d18b716d DSH |
289 | /* Check a curve is one of our preferences */ |
290 | int tls1_check_curve(SSL *s, const unsigned char *p, size_t len) | |
0f113f3e MC |
291 | { |
292 | const unsigned char *curves; | |
293 | size_t num_curves, i; | |
294 | unsigned int suiteb_flags = tls1_suiteb(s); | |
295 | if (len != 3 || p[0] != NAMED_CURVE_TYPE) | |
296 | return 0; | |
297 | /* Check curve matches Suite B preferences */ | |
298 | if (suiteb_flags) { | |
299 | unsigned long cid = s->s3->tmp.new_cipher->id; | |
300 | if (p[1]) | |
301 | return 0; | |
302 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { | |
303 | if (p[2] != TLSEXT_curve_P_256) | |
304 | return 0; | |
305 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { | |
306 | if (p[2] != TLSEXT_curve_P_384) | |
307 | return 0; | |
308 | } else /* Should never happen */ | |
309 | return 0; | |
310 | } | |
311 | if (!tls1_get_curvelist(s, 0, &curves, &num_curves)) | |
312 | return 0; | |
313 | for (i = 0; i < num_curves; i++, curves += 2) { | |
314 | if (p[1] == curves[0] && p[2] == curves[1]) | |
315 | return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK); | |
316 | } | |
317 | return 0; | |
318 | } | |
d0595f17 | 319 | |
1d97c843 | 320 | /*- |
de4d764e | 321 | * For nmatch >= 0, return the NID of the |nmatch|th shared group or NID_undef |
6977e8ee KR |
322 | * if there is no match. |
323 | * For nmatch == -1, return number of matches | |
de4d764e | 324 | * For nmatch == -2, return the NID of the group to use for |
376e2ca3 | 325 | * an EC tmp key, or NID_undef if there is no match. |
d0595f17 | 326 | */ |
de4d764e | 327 | int tls1_shared_group(SSL *s, int nmatch) |
0f113f3e MC |
328 | { |
329 | const unsigned char *pref, *supp; | |
330 | size_t num_pref, num_supp, i, j; | |
331 | int k; | |
3e373518 | 332 | |
0f113f3e MC |
333 | /* Can't do anything on client side */ |
334 | if (s->server == 0) | |
335 | return -1; | |
336 | if (nmatch == -2) { | |
337 | if (tls1_suiteb(s)) { | |
338 | /* | |
339 | * For Suite B ciphersuite determines curve: we already know | |
340 | * these are acceptable due to previous checks. | |
341 | */ | |
342 | unsigned long cid = s->s3->tmp.new_cipher->id; | |
3e373518 | 343 | |
0f113f3e MC |
344 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) |
345 | return NID_X9_62_prime256v1; /* P-256 */ | |
346 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
347 | return NID_secp384r1; /* P-384 */ | |
348 | /* Should never happen */ | |
349 | return NID_undef; | |
350 | } | |
351 | /* If not Suite B just return first preference shared curve */ | |
352 | nmatch = 0; | |
353 | } | |
354 | /* | |
355 | * Avoid truncation. tls1_get_curvelist takes an int | |
356 | * but s->options is a long... | |
357 | */ | |
3e373518 RS |
358 | if (!tls1_get_curvelist(s, |
359 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, | |
360 | &supp, &num_supp)) | |
0f113f3e MC |
361 | /* In practice, NID_undef == 0 but let's be precise. */ |
362 | return nmatch == -1 ? 0 : NID_undef; | |
3e373518 RS |
363 | if (!tls1_get_curvelist(s, |
364 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0, | |
365 | &pref, &num_pref)) | |
0f113f3e | 366 | return nmatch == -1 ? 0 : NID_undef; |
3c06513f | 367 | |
3e373518 | 368 | for (k = 0, i = 0; i < num_pref; i++, pref += 2) { |
0f113f3e | 369 | const unsigned char *tsupp = supp; |
3e373518 | 370 | |
0f113f3e MC |
371 | for (j = 0; j < num_supp; j++, tsupp += 2) { |
372 | if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) { | |
373 | if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED)) | |
374 | continue; | |
375 | if (nmatch == k) { | |
376 | int id = (pref[0] << 8) | pref[1]; | |
3e373518 | 377 | |
ec24630a | 378 | return tls1_ec_curve_id2nid(id, NULL); |
0f113f3e MC |
379 | } |
380 | k++; | |
381 | } | |
382 | } | |
383 | } | |
384 | if (nmatch == -1) | |
385 | return k; | |
386 | /* Out of range (nmatch > k). */ | |
387 | return NID_undef; | |
388 | } | |
d0595f17 | 389 | |
de4d764e MC |
390 | int tls1_set_groups(unsigned char **pext, size_t *pextlen, |
391 | int *groups, size_t ngroups) | |
0f113f3e | 392 | { |
de4d764e | 393 | unsigned char *glist, *p; |
0f113f3e MC |
394 | size_t i; |
395 | /* | |
de4d764e | 396 | * Bitmap of groups included to detect duplicates: only works while group |
0f113f3e MC |
397 | * ids < 32 |
398 | */ | |
399 | unsigned long dup_list = 0; | |
de4d764e MC |
400 | glist = OPENSSL_malloc(ngroups * 2); |
401 | if (glist == NULL) | |
0f113f3e | 402 | return 0; |
de4d764e | 403 | for (i = 0, p = glist; i < ngroups; i++) { |
0f113f3e MC |
404 | unsigned long idmask; |
405 | int id; | |
de4d764e MC |
406 | /* TODO(TLS1.3): Convert for DH groups */ |
407 | id = tls1_ec_nid2curve_id(groups[i]); | |
0f113f3e MC |
408 | idmask = 1L << id; |
409 | if (!id || (dup_list & idmask)) { | |
de4d764e | 410 | OPENSSL_free(glist); |
0f113f3e MC |
411 | return 0; |
412 | } | |
413 | dup_list |= idmask; | |
414 | s2n(id, p); | |
415 | } | |
b548a1f1 | 416 | OPENSSL_free(*pext); |
de4d764e MC |
417 | *pext = glist; |
418 | *pextlen = ngroups * 2; | |
0f113f3e MC |
419 | return 1; |
420 | } | |
421 | ||
422 | # define MAX_CURVELIST 28 | |
423 | ||
424 | typedef struct { | |
425 | size_t nidcnt; | |
426 | int nid_arr[MAX_CURVELIST]; | |
427 | } nid_cb_st; | |
d0595f17 DSH |
428 | |
429 | static int nid_cb(const char *elem, int len, void *arg) | |
0f113f3e MC |
430 | { |
431 | nid_cb_st *narg = arg; | |
432 | size_t i; | |
433 | int nid; | |
434 | char etmp[20]; | |
2747d73c KR |
435 | if (elem == NULL) |
436 | return 0; | |
0f113f3e MC |
437 | if (narg->nidcnt == MAX_CURVELIST) |
438 | return 0; | |
439 | if (len > (int)(sizeof(etmp) - 1)) | |
440 | return 0; | |
441 | memcpy(etmp, elem, len); | |
442 | etmp[len] = 0; | |
443 | nid = EC_curve_nist2nid(etmp); | |
444 | if (nid == NID_undef) | |
445 | nid = OBJ_sn2nid(etmp); | |
446 | if (nid == NID_undef) | |
447 | nid = OBJ_ln2nid(etmp); | |
448 | if (nid == NID_undef) | |
449 | return 0; | |
450 | for (i = 0; i < narg->nidcnt; i++) | |
451 | if (narg->nid_arr[i] == nid) | |
452 | return 0; | |
453 | narg->nid_arr[narg->nidcnt++] = nid; | |
454 | return 1; | |
455 | } | |
456 | ||
de4d764e MC |
457 | /* Set groups based on a colon separate list */ |
458 | int tls1_set_groups_list(unsigned char **pext, size_t *pextlen, const char *str) | |
0f113f3e MC |
459 | { |
460 | nid_cb_st ncb; | |
461 | ncb.nidcnt = 0; | |
462 | if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) | |
463 | return 0; | |
464 | if (pext == NULL) | |
465 | return 1; | |
de4d764e | 466 | return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt); |
0f113f3e MC |
467 | } |
468 | ||
fd2b65ce DSH |
469 | /* For an EC key set TLS id and required compression based on parameters */ |
470 | static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id, | |
0f113f3e MC |
471 | EC_KEY *ec) |
472 | { | |
2235b7f2 | 473 | int id; |
0f113f3e | 474 | const EC_GROUP *grp; |
0f113f3e MC |
475 | if (!ec) |
476 | return 0; | |
477 | /* Determine if it is a prime field */ | |
478 | grp = EC_KEY_get0_group(ec); | |
479 | if (!grp) | |
480 | return 0; | |
0f113f3e MC |
481 | /* Determine curve ID */ |
482 | id = EC_GROUP_get_curve_name(grp); | |
483 | id = tls1_ec_nid2curve_id(id); | |
2235b7f2 DSH |
484 | /* If no id return error: we don't support arbitrary explicit curves */ |
485 | if (id == 0) | |
486 | return 0; | |
487 | curve_id[0] = 0; | |
488 | curve_id[1] = (unsigned char)id; | |
0f113f3e MC |
489 | if (comp_id) { |
490 | if (EC_KEY_get0_public_key(ec) == NULL) | |
491 | return 0; | |
2235b7f2 DSH |
492 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { |
493 | *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; | |
494 | } else { | |
495 | if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME) | |
0f113f3e MC |
496 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
497 | else | |
498 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | |
2235b7f2 | 499 | } |
0f113f3e MC |
500 | } |
501 | return 1; | |
502 | } | |
503 | ||
fd2b65ce DSH |
504 | /* Check an EC key is compatible with extensions */ |
505 | static int tls1_check_ec_key(SSL *s, | |
0f113f3e MC |
506 | unsigned char *curve_id, unsigned char *comp_id) |
507 | { | |
508 | const unsigned char *pformats, *pcurves; | |
509 | size_t num_formats, num_curves, i; | |
510 | int j; | |
511 | /* | |
512 | * If point formats extension present check it, otherwise everything is | |
513 | * supported (see RFC4492). | |
514 | */ | |
aff8c126 RS |
515 | if (comp_id && s->session->ext.ecpointformats) { |
516 | pformats = s->session->ext.ecpointformats; | |
517 | num_formats = s->session->ext.ecpointformats_len; | |
0f113f3e MC |
518 | for (i = 0; i < num_formats; i++, pformats++) { |
519 | if (*comp_id == *pformats) | |
520 | break; | |
521 | } | |
522 | if (i == num_formats) | |
523 | return 0; | |
524 | } | |
525 | if (!curve_id) | |
526 | return 1; | |
527 | /* Check curve is consistent with client and server preferences */ | |
528 | for (j = 0; j <= 1; j++) { | |
529 | if (!tls1_get_curvelist(s, j, &pcurves, &num_curves)) | |
530 | return 0; | |
b79d2410 MC |
531 | if (j == 1 && num_curves == 0) { |
532 | /* | |
533 | * If we've not received any curves then skip this check. | |
534 | * RFC 4492 does not require the supported elliptic curves extension | |
535 | * so if it is not sent we can just choose any curve. | |
536 | * It is invalid to send an empty list in the elliptic curves | |
537 | * extension, so num_curves == 0 always means no extension. | |
538 | */ | |
539 | break; | |
540 | } | |
0f113f3e MC |
541 | for (i = 0; i < num_curves; i++, pcurves += 2) { |
542 | if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1]) | |
543 | break; | |
544 | } | |
545 | if (i == num_curves) | |
546 | return 0; | |
547 | /* For clients can only check sent curve list */ | |
548 | if (!s->server) | |
549 | break; | |
550 | } | |
551 | return 1; | |
552 | } | |
d61ff83b | 553 | |
7da160b0 MC |
554 | void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
555 | size_t *num_formats) | |
0f113f3e MC |
556 | { |
557 | /* | |
558 | * If we have a custom point format list use it otherwise use default | |
559 | */ | |
aff8c126 RS |
560 | if (s->ext.ecpointformats) { |
561 | *pformats = s->ext.ecpointformats; | |
562 | *num_formats = s->ext.ecpointformats_len; | |
0f113f3e MC |
563 | } else { |
564 | *pformats = ecformats_default; | |
565 | /* For Suite B we don't support char2 fields */ | |
566 | if (tls1_suiteb(s)) | |
567 | *num_formats = sizeof(ecformats_default) - 1; | |
568 | else | |
569 | *num_formats = sizeof(ecformats_default); | |
570 | } | |
571 | } | |
572 | ||
573 | /* | |
574 | * Check cert parameters compatible with extensions: currently just checks EC | |
575 | * certificates have compatible curves and compression. | |
d61ff83b | 576 | */ |
9195ddcd | 577 | static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md) |
0f113f3e MC |
578 | { |
579 | unsigned char comp_id, curve_id[2]; | |
580 | EVP_PKEY *pkey; | |
581 | int rv; | |
8382fd3a | 582 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
583 | if (!pkey) |
584 | return 0; | |
585 | /* If not EC nothing to do */ | |
3aeb9348 | 586 | if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) |
0f113f3e | 587 | return 1; |
3aeb9348 | 588 | rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)); |
0f113f3e MC |
589 | if (!rv) |
590 | return 0; | |
591 | /* | |
592 | * Can't check curve_id for client certs as we don't have a supported | |
593 | * curves extension. | |
594 | */ | |
595 | rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id); | |
596 | if (!rv) | |
597 | return 0; | |
598 | /* | |
599 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or | |
9195ddcd | 600 | * SHA384+P-384. |
0f113f3e | 601 | */ |
9195ddcd | 602 | if (check_ee_md && tls1_suiteb(s)) { |
0f113f3e MC |
603 | int check_md; |
604 | size_t i; | |
605 | CERT *c = s->cert; | |
606 | if (curve_id[0]) | |
607 | return 0; | |
608 | /* Check to see we have necessary signing algorithm */ | |
609 | if (curve_id[1] == TLSEXT_curve_P_256) | |
610 | check_md = NID_ecdsa_with_SHA256; | |
611 | else if (curve_id[1] == TLSEXT_curve_P_384) | |
612 | check_md = NID_ecdsa_with_SHA384; | |
613 | else | |
614 | return 0; /* Should never happen */ | |
615 | for (i = 0; i < c->shared_sigalgslen; i++) | |
4d43ee28 | 616 | if (check_md == c->shared_sigalgs[i]->sigandhash) |
0f113f3e MC |
617 | break; |
618 | if (i == c->shared_sigalgslen) | |
619 | return 0; | |
0f113f3e MC |
620 | } |
621 | return rv; | |
622 | } | |
623 | ||
6977e8ee | 624 | /* |
8483a003 | 625 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
6977e8ee KR |
626 | * @s: SSL connection |
627 | * @cid: Cipher ID we're considering using | |
628 | * | |
629 | * Checks that the kECDHE cipher suite we're considering using | |
630 | * is compatible with the client extensions. | |
631 | * | |
632 | * Returns 0 when the cipher can't be used or 1 when it can. | |
633 | */ | |
2ea80354 | 634 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
0f113f3e | 635 | { |
0f113f3e MC |
636 | /* |
637 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other | |
638 | * curves permitted. | |
639 | */ | |
640 | if (tls1_suiteb(s)) { | |
6977e8ee | 641 | unsigned char curve_id[2]; |
0f113f3e MC |
642 | /* Curve to check determined by ciphersuite */ |
643 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) | |
644 | curve_id[1] = TLSEXT_curve_P_256; | |
645 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
646 | curve_id[1] = TLSEXT_curve_P_384; | |
647 | else | |
648 | return 0; | |
649 | curve_id[0] = 0; | |
650 | /* Check this curve is acceptable */ | |
651 | if (!tls1_check_ec_key(s, curve_id, NULL)) | |
652 | return 0; | |
fe6ef247 | 653 | return 1; |
0f113f3e | 654 | } |
fe6ef247 | 655 | /* Need a shared curve */ |
de4d764e | 656 | if (tls1_shared_group(s, 0)) |
fe6ef247 | 657 | return 1; |
6977e8ee | 658 | return 0; |
0f113f3e | 659 | } |
d0595f17 | 660 | |
14536c8c DSH |
661 | #else |
662 | ||
663 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) | |
0f113f3e MC |
664 | { |
665 | return 1; | |
666 | } | |
14536c8c | 667 | |
0f113f3e | 668 | #endif /* OPENSSL_NO_EC */ |
f1fd4544 | 669 | |
703bcee0 | 670 | /* Default sigalg schemes */ |
98c792d1 | 671 | static const uint16_t tls12_sigalgs[] = { |
703bcee0 MC |
672 | #ifndef OPENSSL_NO_EC |
673 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, | |
674 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, | |
675 | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, | |
3d234c9e | 676 | TLSEXT_SIGALG_ed25519, |
e481f9b9 | 677 | #endif |
0f113f3e | 678 | |
536199ec MC |
679 | TLSEXT_SIGALG_rsa_pss_sha256, |
680 | TLSEXT_SIGALG_rsa_pss_sha384, | |
681 | TLSEXT_SIGALG_rsa_pss_sha512, | |
682 | ||
703bcee0 MC |
683 | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
684 | TLSEXT_SIGALG_rsa_pkcs1_sha384, | |
685 | TLSEXT_SIGALG_rsa_pkcs1_sha512, | |
0f113f3e | 686 | |
5eeb6c6e | 687 | #ifndef OPENSSL_NO_EC |
d8311fc9 | 688 | TLSEXT_SIGALG_ecdsa_sha224, |
42ab2230 | 689 | TLSEXT_SIGALG_ecdsa_sha1, |
5eeb6c6e | 690 | #endif |
d8311fc9 | 691 | TLSEXT_SIGALG_rsa_pkcs1_sha224, |
42ab2230 | 692 | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
5eeb6c6e | 693 | #ifndef OPENSSL_NO_DSA |
d8311fc9 | 694 | TLSEXT_SIGALG_dsa_sha224, |
42ab2230 MC |
695 | TLSEXT_SIGALG_dsa_sha1, |
696 | ||
703bcee0 MC |
697 | TLSEXT_SIGALG_dsa_sha256, |
698 | TLSEXT_SIGALG_dsa_sha384, | |
699 | TLSEXT_SIGALG_dsa_sha512 | |
5eeb6c6e | 700 | #endif |
fc101f88 | 701 | }; |
0f113f3e | 702 | |
e481f9b9 | 703 | #ifndef OPENSSL_NO_EC |
98c792d1 | 704 | static const uint16_t suiteb_sigalgs[] = { |
703bcee0 MC |
705 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
706 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 | |
2ea80354 | 707 | }; |
e481f9b9 | 708 | #endif |
aff8c126 | 709 | |
7a531ee4 | 710 | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
5eeb6c6e | 711 | #ifndef OPENSSL_NO_EC |
edbfba1a | 712 | {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
17ae384e DSH |
713 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
714 | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1}, | |
edbfba1a | 715 | {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
17ae384e DSH |
716 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
717 | NID_ecdsa_with_SHA384, NID_secp384r1}, | |
edbfba1a | 718 | {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
17ae384e DSH |
719 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
720 | NID_ecdsa_with_SHA512, NID_secp521r1}, | |
b04d4e38 DSH |
721 | {"ed25519", TLSEXT_SIGALG_ed25519, |
722 | NID_undef, -1, NID_ED25519, SSL_PKEY_ED25519, | |
723 | NID_undef, NID_undef}, | |
d8311fc9 MC |
724 | {NULL, TLSEXT_SIGALG_ecdsa_sha224, |
725 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, | |
726 | NID_ecdsa_with_SHA224, NID_undef}, | |
edbfba1a | 727 | {NULL, TLSEXT_SIGALG_ecdsa_sha1, |
17ae384e DSH |
728 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
729 | NID_ecdsa_with_SHA1, NID_undef}, | |
5eeb6c6e | 730 | #endif |
edbfba1a | 731 | {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256, |
17ae384e DSH |
732 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
733 | NID_undef, NID_undef}, | |
edbfba1a | 734 | {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384, |
17ae384e DSH |
735 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
736 | NID_undef, NID_undef}, | |
edbfba1a | 737 | {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512, |
17ae384e DSH |
738 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
739 | NID_undef, NID_undef}, | |
edbfba1a | 740 | {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
d0ff28f8 | 741 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 742 | NID_sha256WithRSAEncryption, NID_undef}, |
edbfba1a | 743 | {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
d0ff28f8 | 744 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 745 | NID_sha384WithRSAEncryption, NID_undef}, |
edbfba1a | 746 | {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
d0ff28f8 | 747 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 748 | NID_sha512WithRSAEncryption, NID_undef}, |
d8311fc9 MC |
749 | {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224, |
750 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, | |
751 | NID_sha224WithRSAEncryption, NID_undef}, | |
edbfba1a | 752 | {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
d0ff28f8 | 753 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 754 | NID_sha1WithRSAEncryption, NID_undef}, |
5eeb6c6e | 755 | #ifndef OPENSSL_NO_DSA |
edbfba1a | 756 | {NULL, TLSEXT_SIGALG_dsa_sha256, |
17ae384e DSH |
757 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
758 | NID_dsa_with_SHA256, NID_undef}, | |
edbfba1a | 759 | {NULL, TLSEXT_SIGALG_dsa_sha384, |
17ae384e DSH |
760 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
761 | NID_undef, NID_undef}, | |
edbfba1a | 762 | {NULL, TLSEXT_SIGALG_dsa_sha512, |
17ae384e DSH |
763 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
764 | NID_undef, NID_undef}, | |
d8311fc9 MC |
765 | {NULL, TLSEXT_SIGALG_dsa_sha224, |
766 | NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, | |
767 | NID_undef, NID_undef}, | |
edbfba1a | 768 | {NULL, TLSEXT_SIGALG_dsa_sha1, |
17ae384e DSH |
769 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
770 | NID_dsaWithSHA1, NID_undef}, | |
5eeb6c6e MC |
771 | #endif |
772 | #ifndef OPENSSL_NO_GOST | |
edbfba1a | 773 | {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
17ae384e DSH |
774 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
775 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, | |
776 | NID_undef, NID_undef}, | |
edbfba1a | 777 | {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
17ae384e DSH |
778 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
779 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, | |
780 | NID_undef, NID_undef}, | |
edbfba1a | 781 | {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
17ae384e DSH |
782 | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
783 | NID_id_GostR3410_2001, SSL_PKEY_GOST01, | |
784 | NID_undef, NID_undef} | |
5eeb6c6e | 785 | #endif |
703bcee0 | 786 | }; |
0972bc5c DSH |
787 | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
788 | static const SIGALG_LOOKUP legacy_rsa_sigalg = { | |
789 | "rsa_pkcs1_md5_sha1", 0, | |
790 | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, | |
791 | EVP_PKEY_RSA, SSL_PKEY_RSA, | |
792 | NID_undef, NID_undef | |
793 | }; | |
794 | ||
795 | /* | |
796 | * Default signature algorithm values used if signature algorithms not present. | |
797 | * From RFC5246. Note: order must match certificate index order. | |
798 | */ | |
799 | static const uint16_t tls_default_sigalg[] = { | |
800 | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ | |
801 | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ | |
802 | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ | |
803 | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ | |
804 | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */ | |
07afa3d8 DSH |
805 | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */ |
806 | 0 /* SSL_PKEY_ED25519 */ | |
0972bc5c | 807 | }; |
703bcee0 | 808 | |
4d43ee28 DSH |
809 | /* Lookup TLS signature algorithm */ |
810 | static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg) | |
703bcee0 MC |
811 | { |
812 | size_t i; | |
4d43ee28 | 813 | const SIGALG_LOOKUP *s; |
703bcee0 | 814 | |
4d43ee28 DSH |
815 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
816 | i++, s++) { | |
817 | if (s->sigalg == sigalg) | |
818 | return s; | |
703bcee0 | 819 | } |
4d43ee28 DSH |
820 | return NULL; |
821 | } | |
168067b6 DSH |
822 | /* Lookup hash: return 0 if invalid or not enabled */ |
823 | int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd) | |
824 | { | |
825 | const EVP_MD *md; | |
826 | if (lu == NULL) | |
827 | return 0; | |
828 | /* lu->hash == NID_undef means no associated digest */ | |
829 | if (lu->hash == NID_undef) { | |
830 | md = NULL; | |
831 | } else { | |
832 | md = ssl_md(lu->hash_idx); | |
833 | if (md == NULL) | |
834 | return 0; | |
835 | } | |
836 | if (pmd) | |
837 | *pmd = md; | |
838 | return 1; | |
839 | } | |
840 | ||
0972bc5c DSH |
841 | /* |
842 | * Return a signature algorithm for TLS < 1.2 where the signature type | |
843 | * is fixed by the certificate type. | |
844 | */ | |
845 | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx) | |
846 | { | |
847 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) | |
848 | return NULL; | |
849 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { | |
850 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]); | |
851 | ||
168067b6 | 852 | if (!tls1_lookup_md(lu, NULL)) |
0972bc5c | 853 | return NULL; |
0972bc5c DSH |
854 | return lu; |
855 | } | |
856 | return &legacy_rsa_sigalg; | |
857 | } | |
858 | /* Set peer sigalg based key type */ | |
859 | int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey) | |
860 | { | |
861 | int idx = ssl_cert_type(NULL, pkey); | |
862 | ||
863 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, idx); | |
864 | if (lu == NULL) | |
865 | return 0; | |
866 | s->s3->tmp.peer_sigalg = lu; | |
867 | return 1; | |
868 | } | |
703bcee0 | 869 | |
98c792d1 | 870 | size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs) |
0f113f3e MC |
871 | { |
872 | /* | |
873 | * If Suite B mode use Suite B sigalgs only, ignore any other | |
874 | * preferences. | |
875 | */ | |
e481f9b9 | 876 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
877 | switch (tls1_suiteb(s)) { |
878 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
879 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 880 | return OSSL_NELEM(suiteb_sigalgs); |
0f113f3e MC |
881 | |
882 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
883 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 884 | return 1; |
0f113f3e MC |
885 | |
886 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
7a531ee4 MC |
887 | *psigs = suiteb_sigalgs + 1; |
888 | return 1; | |
0f113f3e | 889 | } |
e481f9b9 | 890 | #endif |
a9669ddc DSH |
891 | /* |
892 | * We use client_sigalgs (if not NULL) if we're a server | |
893 | * and sending a certificate request or if we're a client and | |
894 | * determining which shared algorithm to use. | |
895 | */ | |
896 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { | |
0f113f3e MC |
897 | *psigs = s->cert->client_sigalgs; |
898 | return s->cert->client_sigalgslen; | |
899 | } else if (s->cert->conf_sigalgs) { | |
900 | *psigs = s->cert->conf_sigalgs; | |
901 | return s->cert->conf_sigalgslen; | |
902 | } else { | |
903 | *psigs = tls12_sigalgs; | |
703bcee0 | 904 | return OSSL_NELEM(tls12_sigalgs); |
0f113f3e MC |
905 | } |
906 | } | |
907 | ||
908 | /* | |
909 | * Check signature algorithm is consistent with sent supported signature | |
b2eb6998 DSH |
910 | * algorithms and if so set relevant digest and signature scheme in |
911 | * s. | |
ec4a50b3 | 912 | */ |
f742cda8 | 913 | int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey) |
0f113f3e | 914 | { |
98c792d1 | 915 | const uint16_t *sent_sigs; |
5554facb | 916 | const EVP_MD *md = NULL; |
703bcee0 | 917 | char sigalgstr[2]; |
0f113f3e | 918 | size_t sent_sigslen, i; |
536199ec | 919 | int pkeyid = EVP_PKEY_id(pkey); |
f742cda8 | 920 | const SIGALG_LOOKUP *lu; |
4d43ee28 | 921 | |
0f113f3e | 922 | /* Should never happen */ |
536199ec | 923 | if (pkeyid == -1) |
0f113f3e | 924 | return -1; |
5a8916d9 DSH |
925 | if (SSL_IS_TLS13(s)) { |
926 | /* Disallow DSA for TLS 1.3 */ | |
927 | if (pkeyid == EVP_PKEY_DSA) { | |
928 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
929 | return 0; | |
930 | } | |
931 | /* Only allow PSS for TLS 1.3 */ | |
932 | if (pkeyid == EVP_PKEY_RSA) | |
933 | pkeyid = EVP_PKEY_RSA_PSS; | |
934 | } | |
f742cda8 DSH |
935 | lu = tls1_lookup_sigalg(sig); |
936 | /* | |
d8311fc9 MC |
937 | * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type |
938 | * is consistent with signature: RSA keys can be used for RSA-PSS | |
f742cda8 | 939 | */ |
d8311fc9 MC |
940 | if (lu == NULL |
941 | || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224)) | |
095a982b | 942 | || (pkeyid != lu->sig |
f742cda8 | 943 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
0f113f3e MC |
944 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
945 | return 0; | |
946 | } | |
e481f9b9 | 947 | #ifndef OPENSSL_NO_EC |
fe3066ee | 948 | if (pkeyid == EVP_PKEY_EC) { |
8f88cb53 | 949 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
f1adb006 | 950 | int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
44b6318f | 951 | |
8f88cb53 | 952 | if (SSL_IS_TLS13(s)) { |
e892e325 DSH |
953 | if (EC_KEY_get_conv_form(ec) != POINT_CONVERSION_UNCOMPRESSED) { |
954 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
955 | SSL_R_ILLEGAL_POINT_COMPRESSION); | |
956 | return 0; | |
957 | } | |
8f88cb53 | 958 | /* For TLS 1.3 check curve matches signature algorithm */ |
a34a9df0 | 959 | if (lu->curve != NID_undef && curve != lu->curve) { |
8f88cb53 DSH |
960 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
961 | return 0; | |
962 | } | |
963 | } else { | |
964 | unsigned char curve_id[2], comp_id; | |
44b6318f | 965 | |
8f88cb53 DSH |
966 | /* Check compression and curve matches extensions */ |
967 | if (!tls1_set_ec_id(curve_id, &comp_id, ec)) | |
0f113f3e | 968 | return 0; |
8f88cb53 DSH |
969 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { |
970 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); | |
971 | return 0; | |
972 | } | |
8f88cb53 | 973 | if (tls1_suiteb(s)) { |
f1adb006 DSH |
974 | /* Check sigalg matches a permissible Suite B value */ |
975 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 | |
976 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { | |
977 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
978 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0f113f3e | 979 | return 0; |
f1adb006 DSH |
980 | } |
981 | /* | |
982 | * Suite B also requires P-256+SHA256 and P-384+SHA384: | |
983 | * this matches the TLS 1.3 requirements so we can just | |
984 | * check the curve is the expected TLS 1.3 value. | |
985 | * If this fails an inappropriate digest is being used. | |
986 | */ | |
987 | if (curve != lu->curve) { | |
988 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
989 | SSL_R_ILLEGAL_SUITEB_DIGEST); | |
0f113f3e MC |
990 | return 0; |
991 | } | |
8f88cb53 | 992 | } |
0f113f3e | 993 | } |
8f88cb53 | 994 | } else if (tls1_suiteb(s)) { |
0f113f3e | 995 | return 0; |
8f88cb53 | 996 | } |
e481f9b9 | 997 | #endif |
0f113f3e MC |
998 | |
999 | /* Check signature matches a type we sent */ | |
a9669ddc | 1000 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
536199ec | 1001 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
703bcee0 | 1002 | if (sig == *sent_sigs) |
0f113f3e MC |
1003 | break; |
1004 | } | |
1005 | /* Allow fallback to SHA1 if not strict mode */ | |
f742cda8 DSH |
1006 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
1007 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
0f113f3e MC |
1008 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
1009 | return 0; | |
1010 | } | |
168067b6 DSH |
1011 | if (!tls1_lookup_md(lu, &md)) { |
1012 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); | |
1013 | return 0; | |
0f113f3e | 1014 | } |
168067b6 DSH |
1015 | if (md != NULL) { |
1016 | /* | |
1017 | * Make sure security callback allows algorithm. For historical | |
1018 | * reasons we have to pass the sigalg as a two byte char array. | |
1019 | */ | |
1020 | sigalgstr[0] = (sig >> 8) & 0xff; | |
1021 | sigalgstr[1] = sig & 0xff; | |
1022 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, | |
1023 | EVP_MD_size(md) * 4, EVP_MD_type(md), | |
1024 | (void *)sigalgstr)) { | |
1025 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
1026 | return 0; | |
1027 | } | |
0f113f3e | 1028 | } |
6cbebb55 | 1029 | /* Store the sigalg the peer uses */ |
f742cda8 | 1030 | s->s3->tmp.peer_sigalg = lu; |
0f113f3e MC |
1031 | return 1; |
1032 | } | |
2ea80354 | 1033 | |
42ef7aea DSH |
1034 | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
1035 | { | |
f742cda8 | 1036 | if (s->s3->tmp.peer_sigalg == NULL) |
42ef7aea | 1037 | return 0; |
f742cda8 | 1038 | *pnid = s->s3->tmp.peer_sigalg->sig; |
42ef7aea DSH |
1039 | return 1; |
1040 | } | |
1041 | ||
0f113f3e | 1042 | /* |
3eb2aff4 KR |
1043 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
1044 | * supported, doesn't appear in supported signature algorithms, isn't supported | |
1045 | * by the enabled protocol versions or by the security level. | |
1046 | * | |
1047 | * This function should only be used for checking which ciphers are supported | |
1048 | * by the client. | |
1049 | * | |
1050 | * Call ssl_cipher_disabled() to check that it's enabled or not. | |
b7bfe69b DSH |
1051 | */ |
1052 | void ssl_set_client_disabled(SSL *s) | |
0f113f3e | 1053 | { |
4d69f9e6 DSH |
1054 | s->s3->tmp.mask_a = 0; |
1055 | s->s3->tmp.mask_k = 0; | |
4d69f9e6 | 1056 | ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
38a73150 | 1057 | ssl_get_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver); |
a230b26e | 1058 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
1059 | /* with PSK there must be client callback set */ |
1060 | if (!s->psk_client_callback) { | |
4d69f9e6 | 1061 | s->s3->tmp.mask_a |= SSL_aPSK; |
fe5eef3a | 1062 | s->s3->tmp.mask_k |= SSL_PSK; |
0f113f3e | 1063 | } |
a230b26e | 1064 | #endif /* OPENSSL_NO_PSK */ |
e481f9b9 | 1065 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 1066 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
4d69f9e6 DSH |
1067 | s->s3->tmp.mask_a |= SSL_aSRP; |
1068 | s->s3->tmp.mask_k |= SSL_kSRP; | |
0f113f3e | 1069 | } |
e481f9b9 | 1070 | #endif |
0f113f3e | 1071 | } |
fc101f88 | 1072 | |
3eb2aff4 KR |
1073 | /* |
1074 | * ssl_cipher_disabled - check that a cipher is disabled or not | |
1075 | * @s: SSL connection that you want to use the cipher on | |
1076 | * @c: cipher to check | |
1077 | * @op: Security check that you want to do | |
8af91fd9 | 1078 | * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3 |
3eb2aff4 KR |
1079 | * |
1080 | * Returns 1 when it's disabled, 0 when enabled. | |
1081 | */ | |
8af91fd9 | 1082 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe) |
0f113f3e | 1083 | { |
3eb2aff4 | 1084 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
4d69f9e6 | 1085 | || c->algorithm_auth & s->s3->tmp.mask_a) |
0f113f3e | 1086 | return 1; |
3eb2aff4 KR |
1087 | if (s->s3->tmp.max_ver == 0) |
1088 | return 1; | |
8af91fd9 MC |
1089 | if (!SSL_IS_DTLS(s)) { |
1090 | int min_tls = c->min_tls; | |
1091 | ||
1092 | /* | |
1093 | * For historical reasons we will allow ECHDE to be selected by a server | |
1094 | * in SSLv3 if we are a client | |
1095 | */ | |
1096 | if (min_tls == TLS1_VERSION && ecdhe | |
1097 | && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0) | |
1098 | min_tls = SSL3_VERSION; | |
1099 | ||
1100 | if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver)) | |
1101 | return 1; | |
1102 | } | |
3eb2aff4 | 1103 | if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver) |
a230b26e | 1104 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) |
3eb2aff4 KR |
1105 | return 1; |
1106 | ||
0f113f3e MC |
1107 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
1108 | } | |
b362ccab | 1109 | |
7da160b0 | 1110 | int tls_use_ticket(SSL *s) |
0f113f3e | 1111 | { |
08191294 | 1112 | if ((s->options & SSL_OP_NO_TICKET)) |
0f113f3e MC |
1113 | return 0; |
1114 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); | |
1115 | } | |
ed3883d2 | 1116 | |
e469af8d | 1117 | int tls1_set_server_sigalgs(SSL *s) |
0f113f3e MC |
1118 | { |
1119 | int al; | |
1120 | size_t i; | |
8483a003 F |
1121 | |
1122 | /* Clear any shared signature algorithms */ | |
b548a1f1 RS |
1123 | OPENSSL_free(s->cert->shared_sigalgs); |
1124 | s->cert->shared_sigalgs = NULL; | |
1125 | s->cert->shared_sigalgslen = 0; | |
9195ddcd DSH |
1126 | /* Clear certificate validity flags */ |
1127 | for (i = 0; i < SSL_PKEY_NUM; i++) | |
6383d316 | 1128 | s->s3->tmp.valid_flags[i] = 0; |
a8bb912d DSH |
1129 | /* |
1130 | * If peer sent no signature algorithms check to see if we support | |
1131 | * the default algorithm for each certificate type | |
1132 | */ | |
1133 | if (s->s3->tmp.peer_sigalgs == NULL) { | |
1134 | const uint16_t *sent_sigs; | |
1135 | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
0f113f3e | 1136 | |
a8bb912d DSH |
1137 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
1138 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); | |
1139 | size_t j; | |
1140 | ||
1141 | if (lu == NULL) | |
1142 | continue; | |
1143 | /* Check default matches a type we sent */ | |
1144 | for (j = 0; j < sent_sigslen; j++) { | |
1145 | if (lu->sigalg == sent_sigs[j]) { | |
1146 | s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN; | |
1147 | break; | |
1148 | } | |
1149 | } | |
1150 | } | |
9195ddcd | 1151 | return 1; |
a8bb912d | 1152 | } |
9195ddcd DSH |
1153 | |
1154 | if (!tls1_process_sigalgs(s)) { | |
1155 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); | |
1156 | al = SSL_AD_INTERNAL_ERROR; | |
1157 | goto err; | |
d376e57d | 1158 | } |
9195ddcd DSH |
1159 | if (s->cert->shared_sigalgs != NULL) |
1160 | return 1; | |
fb34a0f4 | 1161 | /* Fatal error if no shared signature algorithms */ |
9195ddcd | 1162 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
fb34a0f4 | 1163 | al = SSL_AD_HANDSHAKE_FAILURE; |
0f113f3e MC |
1164 | err: |
1165 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1166 | return 0; | |
1167 | } | |
e469af8d | 1168 | |
1d97c843 | 1169 | /*- |
1ab3836b | 1170 | * Gets the ticket information supplied by the client if any. |
e7f0d921 | 1171 | * |
1ab3836b | 1172 | * hello: The parsed ClientHello data |
c519e89f BM |
1173 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
1174 | * point to the resulting session. | |
1175 | * | |
1176 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key | |
1177 | * ciphersuite, in which case we have no use for session tickets and one will | |
aff8c126 | 1178 | * never be decrypted, nor will s->ext.ticket_expected be set to 1. |
c519e89f BM |
1179 | * |
1180 | * Returns: | |
1181 | * -1: fatal error, either from parsing or decrypting the ticket. | |
1182 | * 0: no ticket was found (or was ignored, based on settings). | |
1183 | * 1: a zero length extension was found, indicating that the client supports | |
1184 | * session tickets but doesn't currently have one to offer. | |
1185 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but | |
1186 | * couldn't be decrypted because of a non-fatal error. | |
1187 | * 3: a ticket was successfully decrypted and *ret was set. | |
1188 | * | |
1189 | * Side effects: | |
aff8c126 | 1190 | * Sets s->ext.ticket_expected to 1 if the server will have to issue |
c519e89f BM |
1191 | * a new session ticket to the client because the client indicated support |
1192 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | |
1193 | * a session ticket or we couldn't use the one it gave us, or if | |
aff8c126 RS |
1194 | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
1195 | * Otherwise, s->ext.ticket_expected is set to 0. | |
6434abbf | 1196 | */ |
ddf6ec00 MC |
1197 | TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
1198 | SSL_SESSION **ret) | |
0f113f3e | 1199 | { |
1ab3836b | 1200 | int retv; |
1ab3836b MC |
1201 | size_t size; |
1202 | RAW_EXTENSION *ticketext; | |
e7f0d921 | 1203 | |
0f113f3e | 1204 | *ret = NULL; |
aff8c126 | 1205 | s->ext.ticket_expected = 0; |
0f113f3e MC |
1206 | |
1207 | /* | |
9362c93e MC |
1208 | * If tickets disabled or not supported by the protocol version |
1209 | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful | |
0f113f3e MC |
1210 | * resumption. |
1211 | */ | |
1ab3836b | 1212 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
ddf6ec00 | 1213 | return TICKET_NONE; |
9ceb2426 | 1214 | |
70af3d8e MC |
1215 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
1216 | if (!ticketext->present) | |
ddf6ec00 | 1217 | return TICKET_NONE; |
1ab3836b MC |
1218 | |
1219 | size = PACKET_remaining(&ticketext->data); | |
1220 | if (size == 0) { | |
1221 | /* | |
1222 | * The client will accept a ticket but doesn't currently have | |
1223 | * one. | |
1224 | */ | |
aff8c126 | 1225 | s->ext.ticket_expected = 1; |
ddf6ec00 | 1226 | return TICKET_EMPTY; |
9ceb2426 | 1227 | } |
aff8c126 | 1228 | if (s->ext.session_secret_cb) { |
1ab3836b MC |
1229 | /* |
1230 | * Indicate that the ticket couldn't be decrypted rather than | |
1231 | * generating the session from ticket now, trigger | |
1232 | * abbreviated handshake based on external mechanism to | |
1233 | * calculate the master secret later. | |
1234 | */ | |
ddf6ec00 | 1235 | return TICKET_NO_DECRYPT; |
1ab3836b | 1236 | } |
70af3d8e MC |
1237 | |
1238 | retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, | |
1239 | hello->session_id, hello->session_id_len, ret); | |
1ab3836b | 1240 | switch (retv) { |
61c32649 | 1241 | case TICKET_NO_DECRYPT: |
aff8c126 | 1242 | s->ext.ticket_expected = 1; |
ddf6ec00 | 1243 | return TICKET_NO_DECRYPT; |
9ceb2426 | 1244 | |
61c32649 | 1245 | case TICKET_SUCCESS: |
ddf6ec00 | 1246 | return TICKET_SUCCESS; |
9ceb2426 | 1247 | |
61c32649 | 1248 | case TICKET_SUCCESS_RENEW: |
aff8c126 | 1249 | s->ext.ticket_expected = 1; |
ddf6ec00 | 1250 | return TICKET_SUCCESS; |
e7f0d921 | 1251 | |
61c32649 | 1252 | default: |
ddf6ec00 | 1253 | return TICKET_FATAL_ERR_OTHER; |
0f113f3e | 1254 | } |
1ab3836b MC |
1255 | } |
1256 | ||
1d97c843 TH |
1257 | /*- |
1258 | * tls_decrypt_ticket attempts to decrypt a session ticket. | |
c519e89f BM |
1259 | * |
1260 | * etick: points to the body of the session ticket extension. | |
8483a003 | 1261 | * eticklen: the length of the session tickets extension. |
c519e89f BM |
1262 | * sess_id: points at the session ID. |
1263 | * sesslen: the length of the session ID. | |
1264 | * psess: (output) on return, if a ticket was decrypted, then this is set to | |
1265 | * point to the resulting session. | |
c519e89f | 1266 | */ |
ddf6ec00 MC |
1267 | TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
1268 | size_t eticklen, const unsigned char *sess_id, | |
1269 | size_t sesslen, SSL_SESSION **psess) | |
0f113f3e MC |
1270 | { |
1271 | SSL_SESSION *sess; | |
1272 | unsigned char *sdec; | |
1273 | const unsigned char *p; | |
ddf6ec00 MC |
1274 | int slen, renew_ticket = 0, declen; |
1275 | TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER; | |
348240c6 | 1276 | size_t mlen; |
0f113f3e | 1277 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
bf7c6817 | 1278 | HMAC_CTX *hctx = NULL; |
846ec07d | 1279 | EVP_CIPHER_CTX *ctx; |
222da979 | 1280 | SSL_CTX *tctx = s->session_ctx; |
e97763c9 | 1281 | |
0f113f3e | 1282 | /* Initialize session ticket encryption and HMAC contexts */ |
bf7c6817 RL |
1283 | hctx = HMAC_CTX_new(); |
1284 | if (hctx == NULL) | |
1053a6e2 | 1285 | return TICKET_FATAL_ERR_MALLOC; |
846ec07d | 1286 | ctx = EVP_CIPHER_CTX_new(); |
35b1a433 | 1287 | if (ctx == NULL) { |
1053a6e2 | 1288 | ret = TICKET_FATAL_ERR_MALLOC; |
35b1a433 MC |
1289 | goto err; |
1290 | } | |
aff8c126 | 1291 | if (tctx->ext.ticket_key_cb) { |
0f113f3e | 1292 | unsigned char *nctick = (unsigned char *)etick; |
aff8c126 | 1293 | int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16, |
846ec07d | 1294 | ctx, hctx, 0); |
0f113f3e | 1295 | if (rv < 0) |
35b1a433 MC |
1296 | goto err; |
1297 | if (rv == 0) { | |
1053a6e2 | 1298 | ret = TICKET_NO_DECRYPT; |
35b1a433 MC |
1299 | goto err; |
1300 | } | |
0f113f3e MC |
1301 | if (rv == 2) |
1302 | renew_ticket = 1; | |
1303 | } else { | |
1304 | /* Check key name matches */ | |
aff8c126 RS |
1305 | if (memcmp(etick, tctx->ext.tick_key_name, |
1306 | sizeof(tctx->ext.tick_key_name)) != 0) { | |
1053a6e2 | 1307 | ret = TICKET_NO_DECRYPT; |
35b1a433 MC |
1308 | goto err; |
1309 | } | |
aff8c126 RS |
1310 | if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key, |
1311 | sizeof(tctx->ext.tick_hmac_key), | |
5f3d93e4 | 1312 | EVP_sha256(), NULL) <= 0 |
a230b26e | 1313 | || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, |
aff8c126 | 1314 | tctx->ext.tick_aes_key, |
1053a6e2 MC |
1315 | etick |
1316 | + sizeof(tctx->ext.tick_key_name)) <= 0) { | |
5f3d93e4 | 1317 | goto err; |
a230b26e | 1318 | } |
0f113f3e MC |
1319 | } |
1320 | /* | |
1321 | * Attempt to process session ticket, first conduct sanity and integrity | |
1322 | * checks on ticket. | |
1323 | */ | |
bf7c6817 | 1324 | mlen = HMAC_size(hctx); |
348240c6 | 1325 | if (mlen == 0) { |
5f3d93e4 | 1326 | goto err; |
0f113f3e | 1327 | } |
e97763c9 DSH |
1328 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
1329 | if (eticklen <= | |
348240c6 | 1330 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
1053a6e2 | 1331 | ret = TICKET_NO_DECRYPT; |
e97763c9 DSH |
1332 | goto err; |
1333 | } | |
0f113f3e MC |
1334 | eticklen -= mlen; |
1335 | /* Check HMAC of encrypted ticket */ | |
bf7c6817 | 1336 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
a230b26e | 1337 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { |
5f3d93e4 MC |
1338 | goto err; |
1339 | } | |
bf7c6817 | 1340 | HMAC_CTX_free(hctx); |
0f113f3e | 1341 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
846ec07d | 1342 | EVP_CIPHER_CTX_free(ctx); |
1053a6e2 | 1343 | return TICKET_NO_DECRYPT; |
0f113f3e MC |
1344 | } |
1345 | /* Attempt to decrypt session data */ | |
1346 | /* Move p after IV to start of encrypted ticket, update length */ | |
57b0d651 BE |
1347 | p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
1348 | eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); | |
0f113f3e | 1349 | sdec = OPENSSL_malloc(eticklen); |
348240c6 MC |
1350 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
1351 | (int)eticklen) <= 0) { | |
846ec07d | 1352 | EVP_CIPHER_CTX_free(ctx); |
d1247df2 | 1353 | OPENSSL_free(sdec); |
1053a6e2 | 1354 | return TICKET_FATAL_ERR_OTHER; |
0f113f3e | 1355 | } |
348240c6 | 1356 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
846ec07d | 1357 | EVP_CIPHER_CTX_free(ctx); |
0f113f3e | 1358 | OPENSSL_free(sdec); |
1053a6e2 | 1359 | return TICKET_NO_DECRYPT; |
0f113f3e | 1360 | } |
348240c6 | 1361 | slen += declen; |
846ec07d RL |
1362 | EVP_CIPHER_CTX_free(ctx); |
1363 | ctx = NULL; | |
0f113f3e MC |
1364 | p = sdec; |
1365 | ||
1366 | sess = d2i_SSL_SESSION(NULL, &p, slen); | |
d3bc9805 | 1367 | slen -= p - sdec; |
0f113f3e MC |
1368 | OPENSSL_free(sdec); |
1369 | if (sess) { | |
79020b27 | 1370 | /* Some additional consistency checks */ |
d3bc9805 | 1371 | if (slen != 0 || sess->session_id_length != 0) { |
79020b27 | 1372 | SSL_SESSION_free(sess); |
0b1f2664 | 1373 | return TICKET_NO_DECRYPT; |
79020b27 | 1374 | } |
0f113f3e MC |
1375 | /* |
1376 | * The session ID, if non-empty, is used by some clients to detect | |
1377 | * that the ticket has been accepted. So we copy it to the session | |
1378 | * structure. If it is empty set length to zero as required by | |
1379 | * standard. | |
1380 | */ | |
1381 | if (sesslen) | |
1382 | memcpy(sess->session_id, sess_id, sesslen); | |
1383 | sess->session_id_length = sesslen; | |
1384 | *psess = sess; | |
1385 | if (renew_ticket) | |
1053a6e2 | 1386 | return TICKET_SUCCESS_RENEW; |
0f113f3e | 1387 | else |
1053a6e2 | 1388 | return TICKET_SUCCESS; |
0f113f3e MC |
1389 | } |
1390 | ERR_clear_error(); | |
1391 | /* | |
1392 | * For session parse failure, indicate that we need to send a new ticket. | |
1393 | */ | |
1053a6e2 | 1394 | return TICKET_NO_DECRYPT; |
a230b26e | 1395 | err: |
846ec07d | 1396 | EVP_CIPHER_CTX_free(ctx); |
bf7c6817 | 1397 | HMAC_CTX_free(hctx); |
35b1a433 | 1398 | return ret; |
0f113f3e | 1399 | } |
6434abbf | 1400 | |
536199ec | 1401 | static int tls12_get_pkey_idx(int sig_nid) |
0f113f3e | 1402 | { |
536199ec | 1403 | switch (sig_nid) { |
e481f9b9 | 1404 | #ifndef OPENSSL_NO_RSA |
536199ec | 1405 | case EVP_PKEY_RSA: |
d0ff28f8 | 1406 | return SSL_PKEY_RSA; |
b2eb6998 DSH |
1407 | /* |
1408 | * For now return RSA key for PSS. When we support PSS only keys | |
1409 | * this will need to be updated. | |
1410 | */ | |
1411 | case EVP_PKEY_RSA_PSS: | |
d0ff28f8 | 1412 | return SSL_PKEY_RSA; |
e481f9b9 MC |
1413 | #endif |
1414 | #ifndef OPENSSL_NO_DSA | |
536199ec | 1415 | case EVP_PKEY_DSA: |
0f113f3e | 1416 | return SSL_PKEY_DSA_SIGN; |
e481f9b9 MC |
1417 | #endif |
1418 | #ifndef OPENSSL_NO_EC | |
536199ec | 1419 | case EVP_PKEY_EC: |
0f113f3e | 1420 | return SSL_PKEY_ECC; |
07afa3d8 DSH |
1421 | case NID_ED25519: |
1422 | return SSL_PKEY_ED25519; | |
e481f9b9 | 1423 | #endif |
a230b26e | 1424 | #ifndef OPENSSL_NO_GOST |
536199ec | 1425 | case NID_id_GostR3410_2001: |
e44380a9 DB |
1426 | return SSL_PKEY_GOST01; |
1427 | ||
536199ec | 1428 | case NID_id_GostR3410_2012_256: |
e44380a9 DB |
1429 | return SSL_PKEY_GOST12_256; |
1430 | ||
536199ec | 1431 | case NID_id_GostR3410_2012_512: |
e44380a9 | 1432 | return SSL_PKEY_GOST12_512; |
a230b26e | 1433 | #endif |
0f113f3e MC |
1434 | } |
1435 | return -1; | |
1436 | } | |
4453cd8c | 1437 | |
b362ccab | 1438 | /* Check to see if a signature algorithm is allowed */ |
b0e9ab95 | 1439 | static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu) |
0f113f3e | 1440 | { |
703bcee0 | 1441 | unsigned char sigalgstr[2]; |
44b6318f | 1442 | int secbits; |
703bcee0 | 1443 | |
44b6318f | 1444 | /* See if sigalgs is recognised and if hash is enabled */ |
168067b6 | 1445 | if (!tls1_lookup_md(lu, NULL)) |
0f113f3e | 1446 | return 0; |
224b4e37 DSH |
1447 | /* DSA is not allowed in TLS 1.3 */ |
1448 | if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) | |
1449 | return 0; | |
0f113f3e | 1450 | /* See if public key algorithm allowed */ |
44b6318f | 1451 | if (tls12_get_pkey_idx(lu->sig) == -1) |
0f113f3e | 1452 | return 0; |
168067b6 DSH |
1453 | if (lu->hash == NID_undef) |
1454 | return 1; | |
44b6318f DSH |
1455 | /* Security bits: half digest bits */ |
1456 | secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4; | |
0f113f3e | 1457 | /* Finally see if security callback allows it */ |
b0e9ab95 DSH |
1458 | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
1459 | sigalgstr[1] = lu->sigalg & 0xff; | |
44b6318f | 1460 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
0f113f3e MC |
1461 | } |
1462 | ||
1463 | /* | |
1464 | * Get a mask of disabled public key algorithms based on supported signature | |
1465 | * algorithms. For example if no signature algorithm supports RSA then RSA is | |
1466 | * disabled. | |
b362ccab DSH |
1467 | */ |
1468 | ||
90d9e49a | 1469 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) |
0f113f3e | 1470 | { |
98c792d1 | 1471 | const uint16_t *sigalgs; |
0f113f3e MC |
1472 | size_t i, sigalgslen; |
1473 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; | |
1474 | /* | |
1475 | * Now go through all signature algorithms seeing if we support any for | |
1476 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep | |
1477 | * down calls to security callback only check if we have to. | |
1478 | */ | |
a9669ddc | 1479 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
703bcee0 | 1480 | for (i = 0; i < sigalgslen; i ++, sigalgs++) { |
b0e9ab95 DSH |
1481 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs); |
1482 | ||
1483 | if (lu == NULL) | |
1484 | continue; | |
1485 | switch (lu->sig) { | |
e481f9b9 | 1486 | #ifndef OPENSSL_NO_RSA |
b2eb6998 DSH |
1487 | /* Any RSA-PSS signature algorithms also mean we allow RSA */ |
1488 | case EVP_PKEY_RSA_PSS: | |
536199ec | 1489 | case EVP_PKEY_RSA: |
b0e9ab95 | 1490 | if (!have_rsa && tls12_sigalg_allowed(s, op, lu)) |
0f113f3e MC |
1491 | have_rsa = 1; |
1492 | break; | |
e481f9b9 MC |
1493 | #endif |
1494 | #ifndef OPENSSL_NO_DSA | |
536199ec | 1495 | case EVP_PKEY_DSA: |
b0e9ab95 | 1496 | if (!have_dsa && tls12_sigalg_allowed(s, op, lu)) |
0f113f3e MC |
1497 | have_dsa = 1; |
1498 | break; | |
e481f9b9 MC |
1499 | #endif |
1500 | #ifndef OPENSSL_NO_EC | |
b2021556 | 1501 | case NID_ED25519: |
536199ec | 1502 | case EVP_PKEY_EC: |
b0e9ab95 | 1503 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, lu)) |
0f113f3e MC |
1504 | have_ecdsa = 1; |
1505 | break; | |
e481f9b9 | 1506 | #endif |
0f113f3e MC |
1507 | } |
1508 | } | |
1509 | if (!have_rsa) | |
1510 | *pmask_a |= SSL_aRSA; | |
1511 | if (!have_dsa) | |
1512 | *pmask_a |= SSL_aDSS; | |
1513 | if (!have_ecdsa) | |
1514 | *pmask_a |= SSL_aECDSA; | |
1515 | } | |
b362ccab | 1516 | |
ae2f7b37 | 1517 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
98c792d1 | 1518 | const uint16_t *psig, size_t psiglen) |
2c7b4dbc MC |
1519 | { |
1520 | size_t i; | |
b0e9ab95 | 1521 | int rv = 0; |
c0f9e23c | 1522 | |
703bcee0 | 1523 | for (i = 0; i < psiglen; i++, psig++) { |
b0e9ab95 DSH |
1524 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig); |
1525 | ||
1526 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) | |
1527 | continue; | |
1528 | if (!WPACKET_put_bytes_u16(pkt, *psig)) | |
1529 | return 0; | |
1530 | /* | |
1531 | * If TLS 1.3 must have at least one valid TLS 1.3 message | |
d8311fc9 | 1532 | * signing algorithm: i.e. neither RSA nor SHA1/SHA224 |
b0e9ab95 DSH |
1533 | */ |
1534 | if (rv == 0 && (!SSL_IS_TLS13(s) | |
d8311fc9 MC |
1535 | || (lu->sig != EVP_PKEY_RSA |
1536 | && lu->hash != NID_sha1 | |
1537 | && lu->hash != NID_sha224))) | |
b0e9ab95 | 1538 | rv = 1; |
2c7b4dbc | 1539 | } |
5528d68f DSH |
1540 | if (rv == 0) |
1541 | SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
b0e9ab95 | 1542 | return rv; |
2c7b4dbc MC |
1543 | } |
1544 | ||
4453cd8c | 1545 | /* Given preference and allowed sigalgs set shared sigalgs */ |
4d43ee28 | 1546 | static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig, |
98c792d1 DSH |
1547 | const uint16_t *pref, size_t preflen, |
1548 | const uint16_t *allow, size_t allowlen) | |
0f113f3e | 1549 | { |
98c792d1 | 1550 | const uint16_t *ptmp, *atmp; |
0f113f3e | 1551 | size_t i, j, nmatch = 0; |
703bcee0 | 1552 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
b0e9ab95 DSH |
1553 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp); |
1554 | ||
0f113f3e | 1555 | /* Skip disabled hashes or signature algorithms */ |
b0e9ab95 | 1556 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
0f113f3e | 1557 | continue; |
703bcee0 MC |
1558 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
1559 | if (*ptmp == *atmp) { | |
0f113f3e | 1560 | nmatch++; |
b0e9ab95 DSH |
1561 | if (shsig) |
1562 | *shsig++ = lu; | |
0f113f3e MC |
1563 | break; |
1564 | } | |
1565 | } | |
1566 | } | |
1567 | return nmatch; | |
1568 | } | |
4453cd8c DSH |
1569 | |
1570 | /* Set shared signature algorithms for SSL structures */ | |
1571 | static int tls1_set_shared_sigalgs(SSL *s) | |
0f113f3e | 1572 | { |
98c792d1 | 1573 | const uint16_t *pref, *allow, *conf; |
0f113f3e MC |
1574 | size_t preflen, allowlen, conflen; |
1575 | size_t nmatch; | |
4d43ee28 | 1576 | const SIGALG_LOOKUP **salgs = NULL; |
0f113f3e MC |
1577 | CERT *c = s->cert; |
1578 | unsigned int is_suiteb = tls1_suiteb(s); | |
b548a1f1 RS |
1579 | |
1580 | OPENSSL_free(c->shared_sigalgs); | |
1581 | c->shared_sigalgs = NULL; | |
1582 | c->shared_sigalgslen = 0; | |
0f113f3e MC |
1583 | /* If client use client signature algorithms if not NULL */ |
1584 | if (!s->server && c->client_sigalgs && !is_suiteb) { | |
1585 | conf = c->client_sigalgs; | |
1586 | conflen = c->client_sigalgslen; | |
1587 | } else if (c->conf_sigalgs && !is_suiteb) { | |
1588 | conf = c->conf_sigalgs; | |
1589 | conflen = c->conf_sigalgslen; | |
1590 | } else | |
a9669ddc | 1591 | conflen = tls12_get_psigalgs(s, 0, &conf); |
0f113f3e MC |
1592 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
1593 | pref = conf; | |
1594 | preflen = conflen; | |
76106e60 DSH |
1595 | allow = s->s3->tmp.peer_sigalgs; |
1596 | allowlen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
1597 | } else { |
1598 | allow = conf; | |
1599 | allowlen = conflen; | |
76106e60 DSH |
1600 | pref = s->s3->tmp.peer_sigalgs; |
1601 | preflen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
1602 | } |
1603 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); | |
34e3edbf | 1604 | if (nmatch) { |
4d43ee28 | 1605 | salgs = OPENSSL_malloc(nmatch * sizeof(*salgs)); |
a71edf3b | 1606 | if (salgs == NULL) |
34e3edbf DSH |
1607 | return 0; |
1608 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); | |
1609 | } else { | |
1610 | salgs = NULL; | |
1611 | } | |
0f113f3e MC |
1612 | c->shared_sigalgs = salgs; |
1613 | c->shared_sigalgslen = nmatch; | |
1614 | return 1; | |
1615 | } | |
4453cd8c | 1616 | |
6b7be581 DSH |
1617 | /* Set preferred digest for each key type */ |
1618 | ||
703bcee0 | 1619 | int tls1_save_sigalgs(SSL *s, PACKET *pkt) |
0f113f3e MC |
1620 | { |
1621 | CERT *c = s->cert; | |
98c792d1 | 1622 | unsigned int stmp; |
703bcee0 MC |
1623 | size_t size, i; |
1624 | ||
0f113f3e MC |
1625 | /* Extension ignored for inappropriate versions */ |
1626 | if (!SSL_USE_SIGALGS(s)) | |
1627 | return 1; | |
1628 | /* Should never happen */ | |
1629 | if (!c) | |
1630 | return 0; | |
1631 | ||
703bcee0 MC |
1632 | size = PACKET_remaining(pkt); |
1633 | ||
1634 | /* Invalid data length */ | |
8f12296e | 1635 | if (size == 0 || (size & 1) != 0) |
703bcee0 MC |
1636 | return 0; |
1637 | ||
1638 | size >>= 1; | |
1639 | ||
76106e60 | 1640 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
536199ec MC |
1641 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(size |
1642 | * sizeof(*s->s3->tmp.peer_sigalgs)); | |
76106e60 | 1643 | if (s->s3->tmp.peer_sigalgs == NULL) |
0f113f3e | 1644 | return 0; |
703bcee0 | 1645 | s->s3->tmp.peer_sigalgslen = size; |
98c792d1 DSH |
1646 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
1647 | s->s3->tmp.peer_sigalgs[i] = stmp; | |
703bcee0 MC |
1648 | |
1649 | if (i != size) | |
1650 | return 0; | |
1651 | ||
0f113f3e MC |
1652 | return 1; |
1653 | } | |
6b7be581 | 1654 | |
c800c27a | 1655 | int tls1_process_sigalgs(SSL *s) |
0f113f3e | 1656 | { |
0f113f3e | 1657 | size_t i; |
f7d53487 | 1658 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
0f113f3e | 1659 | CERT *c = s->cert; |
4d43ee28 | 1660 | |
0f113f3e MC |
1661 | if (!tls1_set_shared_sigalgs(s)) |
1662 | return 0; | |
1663 | ||
9195ddcd DSH |
1664 | for (i = 0; i < SSL_PKEY_NUM; i++) |
1665 | pvalid[i] = 0; | |
1666 | ||
4d43ee28 DSH |
1667 | for (i = 0; i < c->shared_sigalgslen; i++) { |
1668 | const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i]; | |
9195ddcd | 1669 | int idx = sigptr->sig_idx; |
4d43ee28 | 1670 | |
523fb323 | 1671 | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
4d43ee28 | 1672 | if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
523fb323 | 1673 | continue; |
9195ddcd DSH |
1674 | /* If not disabled indicate we can explicitly sign */ |
1675 | if (pvalid[idx] == 0 && tls12_get_pkey_idx(sigptr->sig) != -1) | |
a8bb912d | 1676 | pvalid[sigptr->sig_idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
0f113f3e MC |
1677 | } |
1678 | return 1; | |
1679 | } | |
4817504d | 1680 | |
e7f8ff43 | 1681 | int SSL_get_sigalgs(SSL *s, int idx, |
0f113f3e MC |
1682 | int *psign, int *phash, int *psignhash, |
1683 | unsigned char *rsig, unsigned char *rhash) | |
1684 | { | |
98c792d1 | 1685 | uint16_t *psig = s->s3->tmp.peer_sigalgs; |
703bcee0 | 1686 | size_t numsigalgs = s->s3->tmp.peer_sigalgslen; |
348240c6 | 1687 | if (psig == NULL || numsigalgs > INT_MAX) |
0f113f3e MC |
1688 | return 0; |
1689 | if (idx >= 0) { | |
4d43ee28 DSH |
1690 | const SIGALG_LOOKUP *lu; |
1691 | ||
703bcee0 | 1692 | if (idx >= (int)numsigalgs) |
0f113f3e MC |
1693 | return 0; |
1694 | psig += idx; | |
4d43ee28 | 1695 | if (rhash != NULL) |
536199ec | 1696 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
4d43ee28 | 1697 | if (rsig != NULL) |
536199ec | 1698 | *rsig = (unsigned char)(*psig & 0xff); |
4d43ee28 DSH |
1699 | lu = tls1_lookup_sigalg(*psig); |
1700 | if (psign != NULL) | |
1701 | *psign = lu != NULL ? lu->sig : NID_undef; | |
1702 | if (phash != NULL) | |
1703 | *phash = lu != NULL ? lu->hash : NID_undef; | |
1704 | if (psignhash != NULL) | |
1705 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; | |
0f113f3e | 1706 | } |
348240c6 | 1707 | return (int)numsigalgs; |
0f113f3e | 1708 | } |
4453cd8c DSH |
1709 | |
1710 | int SSL_get_shared_sigalgs(SSL *s, int idx, | |
0f113f3e MC |
1711 | int *psign, int *phash, int *psignhash, |
1712 | unsigned char *rsig, unsigned char *rhash) | |
1713 | { | |
4d43ee28 DSH |
1714 | const SIGALG_LOOKUP *shsigalgs; |
1715 | if (s->cert->shared_sigalgs == NULL | |
6d047e06 | 1716 | || idx < 0 |
4d43ee28 DSH |
1717 | || idx >= (int)s->cert->shared_sigalgslen |
1718 | || s->cert->shared_sigalgslen > INT_MAX) | |
0f113f3e | 1719 | return 0; |
4d43ee28 DSH |
1720 | shsigalgs = s->cert->shared_sigalgs[idx]; |
1721 | if (phash != NULL) | |
1722 | *phash = shsigalgs->hash; | |
1723 | if (psign != NULL) | |
1724 | *psign = shsigalgs->sig; | |
1725 | if (psignhash != NULL) | |
1726 | *psignhash = shsigalgs->sigandhash; | |
1727 | if (rsig != NULL) | |
1728 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); | |
1729 | if (rhash != NULL) | |
1730 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); | |
348240c6 | 1731 | return (int)s->cert->shared_sigalgslen; |
0f113f3e MC |
1732 | } |
1733 | ||
787ebcaf DSH |
1734 | /* Maximum possible number of unique entries in sigalgs array */ |
1735 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) | |
0f229cce | 1736 | |
0f113f3e MC |
1737 | typedef struct { |
1738 | size_t sigalgcnt; | |
787ebcaf | 1739 | int sigalgs[TLS_MAX_SIGALGCNT]; |
0f113f3e | 1740 | } sig_cb_st; |
0f229cce | 1741 | |
431f458d DSH |
1742 | static void get_sigorhash(int *psig, int *phash, const char *str) |
1743 | { | |
1744 | if (strcmp(str, "RSA") == 0) { | |
1745 | *psig = EVP_PKEY_RSA; | |
b2eb6998 DSH |
1746 | } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { |
1747 | *psig = EVP_PKEY_RSA_PSS; | |
431f458d DSH |
1748 | } else if (strcmp(str, "DSA") == 0) { |
1749 | *psig = EVP_PKEY_DSA; | |
1750 | } else if (strcmp(str, "ECDSA") == 0) { | |
1751 | *psig = EVP_PKEY_EC; | |
1752 | } else { | |
1753 | *phash = OBJ_sn2nid(str); | |
1754 | if (*phash == NID_undef) | |
1755 | *phash = OBJ_ln2nid(str); | |
1756 | } | |
1757 | } | |
787ebcaf DSH |
1758 | /* Maximum length of a signature algorithm string component */ |
1759 | #define TLS_MAX_SIGSTRING_LEN 40 | |
431f458d | 1760 | |
0f229cce | 1761 | static int sig_cb(const char *elem, int len, void *arg) |
0f113f3e MC |
1762 | { |
1763 | sig_cb_st *sarg = arg; | |
1764 | size_t i; | |
787ebcaf | 1765 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
431f458d | 1766 | int sig_alg = NID_undef, hash_alg = NID_undef; |
2747d73c KR |
1767 | if (elem == NULL) |
1768 | return 0; | |
787ebcaf | 1769 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
0f113f3e MC |
1770 | return 0; |
1771 | if (len > (int)(sizeof(etmp) - 1)) | |
1772 | return 0; | |
1773 | memcpy(etmp, elem, len); | |
1774 | etmp[len] = 0; | |
1775 | p = strchr(etmp, '+'); | |
8a43a42a DSH |
1776 | /* See if we have a match for TLS 1.3 names */ |
1777 | if (p == NULL) { | |
1778 | const SIGALG_LOOKUP *s; | |
1779 | ||
1780 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); | |
1781 | i++, s++) { | |
1782 | if (s->name != NULL && strcmp(etmp, s->name) == 0) { | |
1783 | sig_alg = s->sig; | |
1784 | hash_alg = s->hash; | |
1785 | break; | |
1786 | } | |
1787 | } | |
1788 | } else { | |
1789 | *p = 0; | |
1790 | p++; | |
1791 | if (*p == 0) | |
1792 | return 0; | |
1793 | get_sigorhash(&sig_alg, &hash_alg, etmp); | |
1794 | get_sigorhash(&sig_alg, &hash_alg, p); | |
1795 | } | |
0f113f3e | 1796 | |
168067b6 | 1797 | if (sig_alg == NID_undef || (p != NULL && hash_alg == NID_undef)) |
0f113f3e MC |
1798 | return 0; |
1799 | ||
1800 | for (i = 0; i < sarg->sigalgcnt; i += 2) { | |
1801 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) | |
1802 | return 0; | |
1803 | } | |
1804 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; | |
1805 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; | |
1806 | return 1; | |
1807 | } | |
1808 | ||
1809 | /* | |
9d22666e | 1810 | * Set supported signature algorithms based on a colon separated list of the |
0f113f3e MC |
1811 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
1812 | */ | |
3dbc46df | 1813 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
0f113f3e MC |
1814 | { |
1815 | sig_cb_st sig; | |
1816 | sig.sigalgcnt = 0; | |
1817 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) | |
1818 | return 0; | |
1819 | if (c == NULL) | |
1820 | return 1; | |
1821 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); | |
1822 | } | |
1823 | ||
a230b26e | 1824 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
0f113f3e | 1825 | { |
98c792d1 | 1826 | uint16_t *sigalgs, *sptr; |
0f113f3e | 1827 | size_t i; |
63c1df09 | 1828 | |
0f113f3e MC |
1829 | if (salglen & 1) |
1830 | return 0; | |
7a531ee4 | 1831 | sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs)); |
0f113f3e MC |
1832 | if (sigalgs == NULL) |
1833 | return 0; | |
1834 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { | |
63c1df09 | 1835 | size_t j; |
7a531ee4 | 1836 | const SIGALG_LOOKUP *curr; |
63c1df09 MC |
1837 | int md_id = *psig_nids++; |
1838 | int sig_id = *psig_nids++; | |
1839 | ||
1840 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); | |
1841 | j++, curr++) { | |
fe3066ee | 1842 | if (curr->hash == md_id && curr->sig == sig_id) { |
63c1df09 MC |
1843 | *sptr++ = curr->sigalg; |
1844 | break; | |
1845 | } | |
1846 | } | |
0f113f3e | 1847 | |
63c1df09 | 1848 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
0f113f3e | 1849 | goto err; |
0f113f3e MC |
1850 | } |
1851 | ||
1852 | if (client) { | |
b548a1f1 | 1853 | OPENSSL_free(c->client_sigalgs); |
0f113f3e | 1854 | c->client_sigalgs = sigalgs; |
7a531ee4 | 1855 | c->client_sigalgslen = salglen / 2; |
0f113f3e | 1856 | } else { |
b548a1f1 | 1857 | OPENSSL_free(c->conf_sigalgs); |
0f113f3e | 1858 | c->conf_sigalgs = sigalgs; |
7a531ee4 | 1859 | c->conf_sigalgslen = salglen / 2; |
0f113f3e MC |
1860 | } |
1861 | ||
1862 | return 1; | |
1863 | ||
1864 | err: | |
1865 | OPENSSL_free(sigalgs); | |
1866 | return 0; | |
1867 | } | |
4453cd8c | 1868 | |
d61ff83b | 1869 | static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) |
0f113f3e MC |
1870 | { |
1871 | int sig_nid; | |
1872 | size_t i; | |
1873 | if (default_nid == -1) | |
1874 | return 1; | |
1875 | sig_nid = X509_get_signature_nid(x); | |
1876 | if (default_nid) | |
1877 | return sig_nid == default_nid ? 1 : 0; | |
1878 | for (i = 0; i < c->shared_sigalgslen; i++) | |
4d43ee28 | 1879 | if (sig_nid == c->shared_sigalgs[i]->sigandhash) |
0f113f3e MC |
1880 | return 1; |
1881 | return 0; | |
1882 | } | |
1883 | ||
6dbb6219 DSH |
1884 | /* Check to see if a certificate issuer name matches list of CA names */ |
1885 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) | |
0f113f3e MC |
1886 | { |
1887 | X509_NAME *nm; | |
1888 | int i; | |
1889 | nm = X509_get_issuer_name(x); | |
1890 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
1891 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) | |
1892 | return 1; | |
1893 | } | |
1894 | return 0; | |
1895 | } | |
1896 | ||
1897 | /* | |
1898 | * Check certificate chain is consistent with TLS extensions and is usable by | |
1899 | * server. This servers two purposes: it allows users to check chains before | |
1900 | * passing them to the server and it allows the server to check chains before | |
1901 | * attempting to use them. | |
d61ff83b | 1902 | */ |
6dbb6219 | 1903 | |
69687aa8 | 1904 | /* Flags which need to be set for a certificate when strict mode not set */ |
6dbb6219 | 1905 | |
e481f9b9 | 1906 | #define CERT_PKEY_VALID_FLAGS \ |
0f113f3e | 1907 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
6dbb6219 | 1908 | /* Strict mode flags */ |
e481f9b9 | 1909 | #define CERT_PKEY_STRICT_FLAGS \ |
0f113f3e MC |
1910 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
1911 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) | |
6dbb6219 | 1912 | |
d61ff83b | 1913 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
0f113f3e MC |
1914 | int idx) |
1915 | { | |
1916 | int i; | |
1917 | int rv = 0; | |
1918 | int check_flags = 0, strict_mode; | |
1919 | CERT_PKEY *cpk = NULL; | |
1920 | CERT *c = s->cert; | |
f7d53487 | 1921 | uint32_t *pvalid; |
0f113f3e MC |
1922 | unsigned int suiteb_flags = tls1_suiteb(s); |
1923 | /* idx == -1 means checking server chains */ | |
1924 | if (idx != -1) { | |
1925 | /* idx == -2 means checking client certificate chains */ | |
1926 | if (idx == -2) { | |
1927 | cpk = c->key; | |
348240c6 | 1928 | idx = (int)(cpk - c->pkeys); |
0f113f3e MC |
1929 | } else |
1930 | cpk = c->pkeys + idx; | |
6383d316 | 1931 | pvalid = s->s3->tmp.valid_flags + idx; |
0f113f3e MC |
1932 | x = cpk->x509; |
1933 | pk = cpk->privatekey; | |
1934 | chain = cpk->chain; | |
1935 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; | |
1936 | /* If no cert or key, forget it */ | |
1937 | if (!x || !pk) | |
1938 | goto end; | |
0f113f3e MC |
1939 | } else { |
1940 | if (!x || !pk) | |
d813f9eb | 1941 | return 0; |
0f113f3e MC |
1942 | idx = ssl_cert_type(x, pk); |
1943 | if (idx == -1) | |
d813f9eb | 1944 | return 0; |
6383d316 DSH |
1945 | pvalid = s->s3->tmp.valid_flags + idx; |
1946 | ||
0f113f3e MC |
1947 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
1948 | check_flags = CERT_PKEY_STRICT_FLAGS; | |
1949 | else | |
1950 | check_flags = CERT_PKEY_VALID_FLAGS; | |
1951 | strict_mode = 1; | |
1952 | } | |
1953 | ||
1954 | if (suiteb_flags) { | |
1955 | int ok; | |
1956 | if (check_flags) | |
1957 | check_flags |= CERT_PKEY_SUITEB; | |
1958 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); | |
1959 | if (ok == X509_V_OK) | |
1960 | rv |= CERT_PKEY_SUITEB; | |
1961 | else if (!check_flags) | |
1962 | goto end; | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Check all signature algorithms are consistent with signature | |
1967 | * algorithms extension if TLS 1.2 or later and strict mode. | |
1968 | */ | |
1969 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { | |
1970 | int default_nid; | |
536199ec | 1971 | int rsign = 0; |
76106e60 | 1972 | if (s->s3->tmp.peer_sigalgs) |
0f113f3e MC |
1973 | default_nid = 0; |
1974 | /* If no sigalgs extension use defaults from RFC5246 */ | |
1975 | else { | |
1976 | switch (idx) { | |
d0ff28f8 | 1977 | case SSL_PKEY_RSA: |
536199ec | 1978 | rsign = EVP_PKEY_RSA; |
0f113f3e MC |
1979 | default_nid = NID_sha1WithRSAEncryption; |
1980 | break; | |
1981 | ||
1982 | case SSL_PKEY_DSA_SIGN: | |
536199ec | 1983 | rsign = EVP_PKEY_DSA; |
0f113f3e MC |
1984 | default_nid = NID_dsaWithSHA1; |
1985 | break; | |
1986 | ||
1987 | case SSL_PKEY_ECC: | |
536199ec | 1988 | rsign = EVP_PKEY_EC; |
0f113f3e MC |
1989 | default_nid = NID_ecdsa_with_SHA1; |
1990 | break; | |
1991 | ||
e44380a9 | 1992 | case SSL_PKEY_GOST01: |
536199ec | 1993 | rsign = NID_id_GostR3410_2001; |
e44380a9 DB |
1994 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
1995 | break; | |
1996 | ||
1997 | case SSL_PKEY_GOST12_256: | |
536199ec | 1998 | rsign = NID_id_GostR3410_2012_256; |
e44380a9 DB |
1999 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
2000 | break; | |
2001 | ||
2002 | case SSL_PKEY_GOST12_512: | |
536199ec | 2003 | rsign = NID_id_GostR3410_2012_512; |
e44380a9 DB |
2004 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
2005 | break; | |
2006 | ||
0f113f3e MC |
2007 | default: |
2008 | default_nid = -1; | |
2009 | break; | |
2010 | } | |
2011 | } | |
2012 | /* | |
2013 | * If peer sent no signature algorithms extension and we have set | |
2014 | * preferred signature algorithms check we support sha1. | |
2015 | */ | |
2016 | if (default_nid > 0 && c->conf_sigalgs) { | |
2017 | size_t j; | |
98c792d1 | 2018 | const uint16_t *p = c->conf_sigalgs; |
703bcee0 | 2019 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
44b6318f DSH |
2020 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p); |
2021 | ||
2022 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) | |
0f113f3e MC |
2023 | break; |
2024 | } | |
2025 | if (j == c->conf_sigalgslen) { | |
2026 | if (check_flags) | |
2027 | goto skip_sigs; | |
2028 | else | |
2029 | goto end; | |
2030 | } | |
2031 | } | |
2032 | /* Check signature algorithm of each cert in chain */ | |
2033 | if (!tls1_check_sig_alg(c, x, default_nid)) { | |
2034 | if (!check_flags) | |
2035 | goto end; | |
2036 | } else | |
2037 | rv |= CERT_PKEY_EE_SIGNATURE; | |
2038 | rv |= CERT_PKEY_CA_SIGNATURE; | |
2039 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2040 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { | |
2041 | if (check_flags) { | |
2042 | rv &= ~CERT_PKEY_CA_SIGNATURE; | |
2043 | break; | |
2044 | } else | |
2045 | goto end; | |
2046 | } | |
2047 | } | |
2048 | } | |
2049 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ | |
2050 | else if (check_flags) | |
2051 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; | |
2052 | skip_sigs: | |
2053 | /* Check cert parameters are consistent */ | |
9195ddcd | 2054 | if (tls1_check_cert_param(s, x, 1)) |
0f113f3e MC |
2055 | rv |= CERT_PKEY_EE_PARAM; |
2056 | else if (!check_flags) | |
2057 | goto end; | |
2058 | if (!s->server) | |
2059 | rv |= CERT_PKEY_CA_PARAM; | |
2060 | /* In strict mode check rest of chain too */ | |
2061 | else if (strict_mode) { | |
2062 | rv |= CERT_PKEY_CA_PARAM; | |
2063 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2064 | X509 *ca = sk_X509_value(chain, i); | |
2065 | if (!tls1_check_cert_param(s, ca, 0)) { | |
2066 | if (check_flags) { | |
2067 | rv &= ~CERT_PKEY_CA_PARAM; | |
2068 | break; | |
2069 | } else | |
2070 | goto end; | |
2071 | } | |
2072 | } | |
2073 | } | |
2074 | if (!s->server && strict_mode) { | |
2075 | STACK_OF(X509_NAME) *ca_dn; | |
2076 | int check_type = 0; | |
3aeb9348 | 2077 | switch (EVP_PKEY_id(pk)) { |
0f113f3e MC |
2078 | case EVP_PKEY_RSA: |
2079 | check_type = TLS_CT_RSA_SIGN; | |
2080 | break; | |
2081 | case EVP_PKEY_DSA: | |
2082 | check_type = TLS_CT_DSS_SIGN; | |
2083 | break; | |
2084 | case EVP_PKEY_EC: | |
2085 | check_type = TLS_CT_ECDSA_SIGN; | |
2086 | break; | |
0f113f3e MC |
2087 | } |
2088 | if (check_type) { | |
75c13e78 DSH |
2089 | const uint8_t *ctypes = s->s3->tmp.ctype; |
2090 | size_t j; | |
2091 | ||
2092 | for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) { | |
2093 | if (*ctypes == check_type) { | |
0f113f3e MC |
2094 | rv |= CERT_PKEY_CERT_TYPE; |
2095 | break; | |
2096 | } | |
2097 | } | |
2098 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) | |
2099 | goto end; | |
75c13e78 | 2100 | } else { |
0f113f3e | 2101 | rv |= CERT_PKEY_CERT_TYPE; |
75c13e78 | 2102 | } |
0f113f3e | 2103 | |
fa7c2637 | 2104 | ca_dn = s->s3->tmp.peer_ca_names; |
0f113f3e MC |
2105 | |
2106 | if (!sk_X509_NAME_num(ca_dn)) | |
2107 | rv |= CERT_PKEY_ISSUER_NAME; | |
2108 | ||
2109 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
2110 | if (ssl_check_ca_name(ca_dn, x)) | |
2111 | rv |= CERT_PKEY_ISSUER_NAME; | |
2112 | } | |
2113 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
2114 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2115 | X509 *xtmp = sk_X509_value(chain, i); | |
2116 | if (ssl_check_ca_name(ca_dn, xtmp)) { | |
2117 | rv |= CERT_PKEY_ISSUER_NAME; | |
2118 | break; | |
2119 | } | |
2120 | } | |
2121 | } | |
2122 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) | |
2123 | goto end; | |
2124 | } else | |
2125 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; | |
2126 | ||
2127 | if (!check_flags || (rv & check_flags) == check_flags) | |
2128 | rv |= CERT_PKEY_VALID; | |
2129 | ||
2130 | end: | |
2131 | ||
a8bb912d DSH |
2132 | if (TLS1_get_version(s) >= TLS1_2_VERSION) |
2133 | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); | |
2134 | else | |
0f113f3e MC |
2135 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
2136 | ||
2137 | /* | |
2138 | * When checking a CERT_PKEY structure all flags are irrelevant if the | |
2139 | * chain is invalid. | |
2140 | */ | |
2141 | if (!check_flags) { | |
a8bb912d | 2142 | if (rv & CERT_PKEY_VALID) { |
6383d316 | 2143 | *pvalid = rv; |
a8bb912d DSH |
2144 | } else { |
2145 | /* Preserve sign and explicit sign flag, clear rest */ | |
2146 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; | |
0f113f3e MC |
2147 | return 0; |
2148 | } | |
2149 | } | |
2150 | return rv; | |
2151 | } | |
d61ff83b DSH |
2152 | |
2153 | /* Set validity of certificates in an SSL structure */ | |
2154 | void tls1_set_cert_validity(SSL *s) | |
0f113f3e | 2155 | { |
d0ff28f8 | 2156 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
17dd65e6 | 2157 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
17dd65e6 | 2158 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
e44380a9 DB |
2159 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
2160 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); | |
2161 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); | |
3d234c9e | 2162 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519); |
0f113f3e MC |
2163 | } |
2164 | ||
69687aa8 | 2165 | /* User level utility function to check a chain is suitable */ |
18d71588 | 2166 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
0f113f3e MC |
2167 | { |
2168 | return tls1_check_chain(s, x, pk, chain, -1); | |
2169 | } | |
d61ff83b | 2170 | |
09599b52 DSH |
2171 | #ifndef OPENSSL_NO_DH |
2172 | DH *ssl_get_auto_dh(SSL *s) | |
0f113f3e MC |
2173 | { |
2174 | int dh_secbits = 80; | |
2175 | if (s->cert->dh_tmp_auto == 2) | |
2176 | return DH_get_1024_160(); | |
adc5506a | 2177 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
0f113f3e MC |
2178 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
2179 | dh_secbits = 128; | |
2180 | else | |
2181 | dh_secbits = 80; | |
2182 | } else { | |
a497cf25 | 2183 | if (s->s3->tmp.cert == NULL) |
f365a3e2 | 2184 | return NULL; |
a497cf25 | 2185 | dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey); |
0f113f3e MC |
2186 | } |
2187 | ||
2188 | if (dh_secbits >= 128) { | |
2189 | DH *dhp = DH_new(); | |
0aeddcfa | 2190 | BIGNUM *p, *g; |
a71edf3b | 2191 | if (dhp == NULL) |
0f113f3e | 2192 | return NULL; |
0aeddcfa MC |
2193 | g = BN_new(); |
2194 | if (g != NULL) | |
2195 | BN_set_word(g, 2); | |
0f113f3e | 2196 | if (dh_secbits >= 192) |
9021a5df | 2197 | p = BN_get_rfc3526_prime_8192(NULL); |
0f113f3e | 2198 | else |
9021a5df | 2199 | p = BN_get_rfc3526_prime_3072(NULL); |
0aeddcfa | 2200 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
0f113f3e | 2201 | DH_free(dhp); |
0aeddcfa MC |
2202 | BN_free(p); |
2203 | BN_free(g); | |
0f113f3e MC |
2204 | return NULL; |
2205 | } | |
2206 | return dhp; | |
2207 | } | |
2208 | if (dh_secbits >= 112) | |
2209 | return DH_get_2048_224(); | |
2210 | return DH_get_1024_160(); | |
2211 | } | |
09599b52 | 2212 | #endif |
b362ccab DSH |
2213 | |
2214 | static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e | 2215 | { |
72245f34 | 2216 | int secbits = -1; |
8382fd3a | 2217 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
0f113f3e | 2218 | if (pkey) { |
72245f34 DSH |
2219 | /* |
2220 | * If no parameters this will return -1 and fail using the default | |
2221 | * security callback for any non-zero security level. This will | |
2222 | * reject keys which omit parameters but this only affects DSA and | |
2223 | * omission of parameters is never (?) done in practice. | |
2224 | */ | |
0f113f3e | 2225 | secbits = EVP_PKEY_security_bits(pkey); |
72245f34 | 2226 | } |
0f113f3e MC |
2227 | if (s) |
2228 | return ssl_security(s, op, secbits, 0, x); | |
2229 | else | |
2230 | return ssl_ctx_security(ctx, op, secbits, 0, x); | |
2231 | } | |
b362ccab DSH |
2232 | |
2233 | static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e MC |
2234 | { |
2235 | /* Lookup signature algorithm digest */ | |
65e89736 | 2236 | int secbits, nid, pknid; |
221c7b55 DSH |
2237 | /* Don't check signature if self signed */ |
2238 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) | |
2239 | return 1; | |
65e89736 DSH |
2240 | if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL)) |
2241 | secbits = -1; | |
2242 | /* If digest NID not defined use signature NID */ | |
2243 | if (nid == NID_undef) | |
2244 | nid = pknid; | |
0f113f3e | 2245 | if (s) |
65e89736 | 2246 | return ssl_security(s, op, secbits, nid, x); |
0f113f3e | 2247 | else |
65e89736 | 2248 | return ssl_ctx_security(ctx, op, secbits, nid, x); |
0f113f3e | 2249 | } |
b362ccab DSH |
2250 | |
2251 | int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) | |
0f113f3e MC |
2252 | { |
2253 | if (vfy) | |
2254 | vfy = SSL_SECOP_PEER; | |
2255 | if (is_ee) { | |
2256 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) | |
2257 | return SSL_R_EE_KEY_TOO_SMALL; | |
2258 | } else { | |
2259 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) | |
2260 | return SSL_R_CA_KEY_TOO_SMALL; | |
2261 | } | |
2262 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) | |
2263 | return SSL_R_CA_MD_TOO_WEAK; | |
2264 | return 1; | |
2265 | } | |
2266 | ||
2267 | /* | |
69687aa8 F |
2268 | * Check security of a chain, if |sk| includes the end entity certificate then |
2269 | * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending | |
0f113f3e | 2270 | * one to the peer. Return values: 1 if ok otherwise error code to use |
b362ccab DSH |
2271 | */ |
2272 | ||
2273 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) | |
0f113f3e MC |
2274 | { |
2275 | int rv, start_idx, i; | |
2276 | if (x == NULL) { | |
2277 | x = sk_X509_value(sk, 0); | |
2278 | start_idx = 1; | |
2279 | } else | |
2280 | start_idx = 0; | |
2281 | ||
2282 | rv = ssl_security_cert(s, NULL, x, vfy, 1); | |
2283 | if (rv != 1) | |
2284 | return rv; | |
2285 | ||
2286 | for (i = start_idx; i < sk_X509_num(sk); i++) { | |
2287 | x = sk_X509_value(sk, i); | |
2288 | rv = ssl_security_cert(s, NULL, x, vfy, 0); | |
2289 | if (rv != 1) | |
2290 | return rv; | |
2291 | } | |
2292 | return 1; | |
2293 | } | |
93a77f9e DSH |
2294 | |
2295 | /* | |
2296 | * Choose an appropriate signature algorithm based on available certificates | |
717a265a DSH |
2297 | * Sets chosen certificate and signature algorithm. |
2298 | * | |
2299 | * For servers if we fail to find a required certificate it is a fatal error | |
2300 | * and an appropriate error code is set and the TLS alert set in *al. | |
2301 | * | |
2302 | * For clients al is set to NULL. If a certificate is not suitable it is not | |
2303 | * a fatal error: we will either try another certificate or not present one | |
2304 | * to the server. In this case no error is set. | |
93a77f9e | 2305 | */ |
4a419f60 | 2306 | int tls_choose_sigalg(SSL *s, int *al) |
93a77f9e | 2307 | { |
7b3a4d61 | 2308 | int idx = -1; |
0972bc5c DSH |
2309 | const SIGALG_LOOKUP *lu = NULL; |
2310 | ||
717a265a DSH |
2311 | s->s3->tmp.cert = NULL; |
2312 | s->s3->tmp.sigalg = NULL; | |
2313 | ||
93a77f9e DSH |
2314 | if (SSL_IS_TLS13(s)) { |
2315 | size_t i; | |
21f198ec | 2316 | #ifndef OPENSSL_NO_EC |
e892e325 | 2317 | int curve = -1, skip_ec = 0; |
21f198ec | 2318 | #endif |
93a77f9e | 2319 | |
69687aa8 | 2320 | /* Look for a certificate matching shared sigalgs */ |
93a77f9e | 2321 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { |
0972bc5c | 2322 | lu = s->cert->shared_sigalgs[i]; |
93a77f9e | 2323 | |
d8311fc9 MC |
2324 | /* Skip SHA1, SHA224, DSA and RSA if not PSS */ |
2325 | if (lu->hash == NID_sha1 | |
2326 | || lu->hash == NID_sha224 | |
2327 | || lu->sig == EVP_PKEY_DSA | |
095a982b | 2328 | || lu->sig == EVP_PKEY_RSA) |
93a77f9e | 2329 | continue; |
168067b6 | 2330 | if (!tls1_lookup_md(lu, NULL)) |
93a77f9e DSH |
2331 | continue; |
2332 | idx = lu->sig_idx; | |
0972bc5c | 2333 | if (!ssl_has_cert(s, idx)) |
93a77f9e | 2334 | continue; |
93a77f9e | 2335 | if (lu->sig == EVP_PKEY_EC) { |
21f198ec | 2336 | #ifndef OPENSSL_NO_EC |
93a77f9e | 2337 | if (curve == -1) { |
0972bc5c | 2338 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey); |
93a77f9e DSH |
2339 | |
2340 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); | |
e892e325 DSH |
2341 | if (EC_KEY_get_conv_form(ec) |
2342 | != POINT_CONVERSION_UNCOMPRESSED) | |
2343 | skip_ec = 1; | |
93a77f9e | 2344 | } |
e892e325 | 2345 | if (skip_ec || (lu->curve != NID_undef && curve != lu->curve)) |
93a77f9e | 2346 | continue; |
21f198ec RL |
2347 | #else |
2348 | continue; | |
2349 | #endif | |
93a77f9e | 2350 | } |
0972bc5c DSH |
2351 | break; |
2352 | } | |
2353 | if (i == s->cert->shared_sigalgslen) { | |
717a265a DSH |
2354 | if (al == NULL) |
2355 | return 1; | |
0972bc5c DSH |
2356 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2357 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, | |
2358 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
2359 | return 0; | |
2360 | } | |
2361 | } else { | |
717a265a DSH |
2362 | if (s->server) { |
2363 | /* Find index corresponding to ciphersuite */ | |
2364 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); | |
2365 | /* If no certificate for ciphersuite return */ | |
2366 | if (idx == -1) | |
2367 | return 1; | |
2368 | if (idx == SSL_PKEY_GOST_EC) { | |
69687aa8 | 2369 | /* Work out which GOST certificate is available */ |
717a265a DSH |
2370 | if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) { |
2371 | idx = SSL_PKEY_GOST12_512; | |
2372 | } else if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) { | |
2373 | idx = SSL_PKEY_GOST12_256; | |
2374 | } else if (ssl_has_cert(s, SSL_PKEY_GOST01)) { | |
2375 | idx = SSL_PKEY_GOST01; | |
2376 | } else { | |
2377 | if (al == NULL) | |
2378 | return 1; | |
2379 | *al = SSL_AD_INTERNAL_ERROR; | |
2380 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2381 | return 0; | |
2382 | } | |
2383 | } else if (!ssl_has_cert(s, idx)) { | |
b2021556 DSH |
2384 | /* Allow Ed25519 if no EC certificate */ |
2385 | if (idx == SSL_PKEY_ECC && ssl_has_cert(s, SSL_PKEY_ED25519)) { | |
2386 | idx = SSL_PKEY_ED25519; | |
2387 | } else { | |
2388 | if (al == NULL) | |
2389 | return 1; | |
2390 | *al = SSL_AD_INTERNAL_ERROR; | |
2391 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2392 | return 0; | |
2393 | } | |
0972bc5c | 2394 | } |
717a265a DSH |
2395 | } else { |
2396 | /* Find index for client certificate */ | |
2397 | idx = s->cert->key - s->cert->pkeys; | |
2398 | if (!ssl_has_cert(s, idx)) | |
2399 | return 1; | |
0972bc5c DSH |
2400 | } |
2401 | ||
2402 | if (SSL_USE_SIGALGS(s)) { | |
2403 | if (s->s3->tmp.peer_sigalgs != NULL) { | |
2404 | size_t i; | |
599b586d DSH |
2405 | #ifndef OPENSSL_NO_EC |
2406 | int curve; | |
2407 | ||
2408 | /* For Suite B need to match signature algorithm to curve */ | |
2409 | if (tls1_suiteb(s)) { | |
2410 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey); | |
2411 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); | |
2412 | } else { | |
2413 | curve = -1; | |
2414 | } | |
2415 | #endif | |
0972bc5c DSH |
2416 | |
2417 | /* | |
2418 | * Find highest preference signature algorithm matching | |
2419 | * cert type | |
2420 | */ | |
2421 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { | |
2422 | lu = s->cert->shared_sigalgs[i]; | |
599b586d | 2423 | #ifdef OPENSSL_NO_EC |
0972bc5c DSH |
2424 | if (lu->sig_idx == idx) |
2425 | break; | |
599b586d DSH |
2426 | #else |
2427 | if (lu->sig_idx == idx | |
2428 | && (curve == -1 || lu->curve == curve)) | |
2429 | break; | |
b2021556 DSH |
2430 | if (idx == SSL_PKEY_ECC && lu->sig == NID_ED25519) { |
2431 | idx = SSL_PKEY_ED25519; | |
2432 | break; | |
2433 | } | |
599b586d | 2434 | #endif |
0972bc5c DSH |
2435 | if (idx == SSL_PKEY_RSA && lu->sig == EVP_PKEY_RSA_PSS) |
2436 | break; | |
2437 | } | |
2438 | if (i == s->cert->shared_sigalgslen) { | |
717a265a DSH |
2439 | if (al == NULL) |
2440 | return 1; | |
0972bc5c DSH |
2441 | *al = SSL_AD_INTERNAL_ERROR; |
2442 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2443 | return 0; | |
2444 | } | |
2445 | } else { | |
2446 | /* | |
2447 | * If we have no sigalg use defaults | |
2448 | */ | |
2449 | const uint16_t *sent_sigs; | |
2450 | size_t sent_sigslen, i; | |
2451 | ||
2452 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { | |
717a265a DSH |
2453 | if (al == NULL) |
2454 | return 1; | |
0972bc5c DSH |
2455 | *al = SSL_AD_INTERNAL_ERROR; |
2456 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2457 | return 0; | |
2458 | } | |
2459 | ||
2460 | /* Check signature matches a type we sent */ | |
2461 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
2462 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { | |
2463 | if (lu->sigalg == *sent_sigs) | |
2464 | break; | |
2465 | } | |
2466 | if (i == sent_sigslen) { | |
717a265a DSH |
2467 | if (al == NULL) |
2468 | return 1; | |
0972bc5c | 2469 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
fb34a0f4 | 2470 | *al = SSL_AD_ILLEGAL_PARAMETER; |
0972bc5c DSH |
2471 | return 0; |
2472 | } | |
2473 | } | |
2474 | } else { | |
2475 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { | |
717a265a DSH |
2476 | if (al == NULL) |
2477 | return 1; | |
0972bc5c DSH |
2478 | *al = SSL_AD_INTERNAL_ERROR; |
2479 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2480 | return 0; | |
2481 | } | |
2482 | } | |
93a77f9e | 2483 | } |
7b3a4d61 | 2484 | if (idx == -1) { |
717a265a DSH |
2485 | if (al != NULL) { |
2486 | *al = SSL_AD_INTERNAL_ERROR; | |
2487 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2488 | } | |
7b3a4d61 DSH |
2489 | return 0; |
2490 | } | |
a497cf25 | 2491 | s->s3->tmp.cert = &s->cert->pkeys[idx]; |
59088e43 | 2492 | s->cert->key = s->s3->tmp.cert; |
0972bc5c | 2493 | s->s3->tmp.sigalg = lu; |
93a77f9e DSH |
2494 | return 1; |
2495 | } |