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