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