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