]>
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 | */ |
9195ddcd | 571 | static int tls1_check_cert_param(SSL *s, X509 *x, int check_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 | |
9195ddcd | 594 | * SHA384+P-384. |
0f113f3e | 595 | */ |
9195ddcd | 596 | if (check_ee_md && tls1_suiteb(s)) { |
0f113f3e MC |
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; | |
0f113f3e MC |
614 | } |
615 | return rv; | |
616 | } | |
617 | ||
10bf4fc2 | 618 | # ifndef OPENSSL_NO_EC |
6977e8ee | 619 | /* |
8483a003 | 620 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
6977e8ee KR |
621 | * @s: SSL connection |
622 | * @cid: Cipher ID we're considering using | |
623 | * | |
624 | * Checks that the kECDHE cipher suite we're considering using | |
625 | * is compatible with the client extensions. | |
626 | * | |
627 | * Returns 0 when the cipher can't be used or 1 when it can. | |
628 | */ | |
2ea80354 | 629 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
0f113f3e | 630 | { |
0f113f3e MC |
631 | /* |
632 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other | |
633 | * curves permitted. | |
634 | */ | |
635 | if (tls1_suiteb(s)) { | |
6977e8ee | 636 | unsigned char curve_id[2]; |
0f113f3e MC |
637 | /* Curve to check determined by ciphersuite */ |
638 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) | |
639 | curve_id[1] = TLSEXT_curve_P_256; | |
640 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
641 | curve_id[1] = TLSEXT_curve_P_384; | |
642 | else | |
643 | return 0; | |
644 | curve_id[0] = 0; | |
645 | /* Check this curve is acceptable */ | |
646 | if (!tls1_check_ec_key(s, curve_id, NULL)) | |
647 | return 0; | |
fe6ef247 | 648 | return 1; |
0f113f3e | 649 | } |
fe6ef247 | 650 | /* Need a shared curve */ |
de4d764e | 651 | if (tls1_shared_group(s, 0)) |
fe6ef247 | 652 | return 1; |
6977e8ee | 653 | return 0; |
0f113f3e | 654 | } |
10bf4fc2 | 655 | # endif /* OPENSSL_NO_EC */ |
d0595f17 | 656 | |
14536c8c DSH |
657 | #else |
658 | ||
659 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) | |
0f113f3e MC |
660 | { |
661 | return 1; | |
662 | } | |
14536c8c | 663 | |
0f113f3e | 664 | #endif /* OPENSSL_NO_EC */ |
f1fd4544 | 665 | |
703bcee0 | 666 | /* Default sigalg schemes */ |
98c792d1 | 667 | static const uint16_t tls12_sigalgs[] = { |
703bcee0 MC |
668 | #ifndef OPENSSL_NO_EC |
669 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, | |
670 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384, | |
671 | TLSEXT_SIGALG_ecdsa_secp521r1_sha512, | |
e481f9b9 | 672 | #endif |
0f113f3e | 673 | |
536199ec MC |
674 | TLSEXT_SIGALG_rsa_pss_sha256, |
675 | TLSEXT_SIGALG_rsa_pss_sha384, | |
676 | TLSEXT_SIGALG_rsa_pss_sha512, | |
677 | ||
703bcee0 MC |
678 | TLSEXT_SIGALG_rsa_pkcs1_sha256, |
679 | TLSEXT_SIGALG_rsa_pkcs1_sha384, | |
680 | TLSEXT_SIGALG_rsa_pkcs1_sha512, | |
0f113f3e | 681 | |
5eeb6c6e | 682 | #ifndef OPENSSL_NO_EC |
42ab2230 | 683 | TLSEXT_SIGALG_ecdsa_sha1, |
5eeb6c6e | 684 | #endif |
42ab2230 | 685 | TLSEXT_SIGALG_rsa_pkcs1_sha1, |
5eeb6c6e | 686 | #ifndef OPENSSL_NO_DSA |
42ab2230 MC |
687 | TLSEXT_SIGALG_dsa_sha1, |
688 | ||
703bcee0 MC |
689 | TLSEXT_SIGALG_dsa_sha256, |
690 | TLSEXT_SIGALG_dsa_sha384, | |
691 | TLSEXT_SIGALG_dsa_sha512 | |
5eeb6c6e | 692 | #endif |
fc101f88 | 693 | }; |
0f113f3e | 694 | |
e481f9b9 | 695 | #ifndef OPENSSL_NO_EC |
98c792d1 | 696 | static const uint16_t suiteb_sigalgs[] = { |
703bcee0 MC |
697 | TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
698 | TLSEXT_SIGALG_ecdsa_secp384r1_sha384 | |
2ea80354 | 699 | }; |
e481f9b9 | 700 | #endif |
aff8c126 | 701 | |
7a531ee4 | 702 | static const SIGALG_LOOKUP sigalg_lookup_tbl[] = { |
5eeb6c6e | 703 | #ifndef OPENSSL_NO_EC |
edbfba1a | 704 | {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256, |
17ae384e DSH |
705 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
706 | NID_ecdsa_with_SHA256, NID_X9_62_prime256v1}, | |
edbfba1a | 707 | {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384, |
17ae384e DSH |
708 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
709 | NID_ecdsa_with_SHA384, NID_secp384r1}, | |
edbfba1a | 710 | {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512, |
17ae384e DSH |
711 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
712 | NID_ecdsa_with_SHA512, NID_secp521r1}, | |
edbfba1a | 713 | {NULL, TLSEXT_SIGALG_ecdsa_sha1, |
17ae384e DSH |
714 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC, |
715 | NID_ecdsa_with_SHA1, NID_undef}, | |
5eeb6c6e | 716 | #endif |
edbfba1a | 717 | {"rsa_pss_sha256", TLSEXT_SIGALG_rsa_pss_sha256, |
17ae384e DSH |
718 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
719 | NID_undef, NID_undef}, | |
edbfba1a | 720 | {"rsa_pss_sha384", TLSEXT_SIGALG_rsa_pss_sha384, |
17ae384e DSH |
721 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
722 | NID_undef, NID_undef}, | |
edbfba1a | 723 | {"rsa_pss_sha512", TLSEXT_SIGALG_rsa_pss_sha512, |
17ae384e DSH |
724 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN, |
725 | NID_undef, NID_undef}, | |
edbfba1a | 726 | {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256, |
d0ff28f8 | 727 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 728 | NID_sha256WithRSAEncryption, NID_undef}, |
edbfba1a | 729 | {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384, |
d0ff28f8 | 730 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 731 | NID_sha384WithRSAEncryption, NID_undef}, |
edbfba1a | 732 | {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512, |
d0ff28f8 | 733 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 734 | NID_sha512WithRSAEncryption, NID_undef}, |
edbfba1a | 735 | {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1, |
d0ff28f8 | 736 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA, |
17ae384e | 737 | NID_sha1WithRSAEncryption, NID_undef}, |
5eeb6c6e | 738 | #ifndef OPENSSL_NO_DSA |
edbfba1a | 739 | {NULL, TLSEXT_SIGALG_dsa_sha256, |
17ae384e DSH |
740 | NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
741 | NID_dsa_with_SHA256, NID_undef}, | |
edbfba1a | 742 | {NULL, TLSEXT_SIGALG_dsa_sha384, |
17ae384e DSH |
743 | NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
744 | NID_undef, NID_undef}, | |
edbfba1a | 745 | {NULL, TLSEXT_SIGALG_dsa_sha512, |
17ae384e DSH |
746 | NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
747 | NID_undef, NID_undef}, | |
edbfba1a | 748 | {NULL, TLSEXT_SIGALG_dsa_sha1, |
17ae384e DSH |
749 | NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN, |
750 | NID_dsaWithSHA1, NID_undef}, | |
5eeb6c6e MC |
751 | #endif |
752 | #ifndef OPENSSL_NO_GOST | |
edbfba1a | 753 | {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, |
17ae384e DSH |
754 | NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX, |
755 | NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256, | |
756 | NID_undef, NID_undef}, | |
edbfba1a | 757 | {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, |
17ae384e DSH |
758 | NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX, |
759 | NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512, | |
760 | NID_undef, NID_undef}, | |
edbfba1a | 761 | {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411, |
17ae384e DSH |
762 | NID_id_GostR3411_94, SSL_MD_GOST94_IDX, |
763 | NID_id_GostR3410_2001, SSL_PKEY_GOST01, | |
764 | NID_undef, NID_undef} | |
5eeb6c6e | 765 | #endif |
703bcee0 | 766 | }; |
0972bc5c DSH |
767 | /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */ |
768 | static const SIGALG_LOOKUP legacy_rsa_sigalg = { | |
769 | "rsa_pkcs1_md5_sha1", 0, | |
770 | NID_md5_sha1, SSL_MD_MD5_SHA1_IDX, | |
771 | EVP_PKEY_RSA, SSL_PKEY_RSA, | |
772 | NID_undef, NID_undef | |
773 | }; | |
774 | ||
775 | /* | |
776 | * Default signature algorithm values used if signature algorithms not present. | |
777 | * From RFC5246. Note: order must match certificate index order. | |
778 | */ | |
779 | static const uint16_t tls_default_sigalg[] = { | |
780 | TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */ | |
781 | TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */ | |
782 | TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */ | |
783 | TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */ | |
784 | TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */ | |
785 | TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512 /* SSL_PKEY_GOST12_512 */ | |
786 | }; | |
703bcee0 | 787 | |
4d43ee28 DSH |
788 | /* Lookup TLS signature algorithm */ |
789 | static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg) | |
703bcee0 MC |
790 | { |
791 | size_t i; | |
4d43ee28 | 792 | const SIGALG_LOOKUP *s; |
703bcee0 | 793 | |
4d43ee28 DSH |
794 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); |
795 | i++, s++) { | |
796 | if (s->sigalg == sigalg) | |
797 | return s; | |
703bcee0 | 798 | } |
4d43ee28 DSH |
799 | return NULL; |
800 | } | |
0972bc5c DSH |
801 | /* |
802 | * Return a signature algorithm for TLS < 1.2 where the signature type | |
803 | * is fixed by the certificate type. | |
804 | */ | |
805 | static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx) | |
806 | { | |
807 | if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg)) | |
808 | return NULL; | |
809 | if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) { | |
810 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]); | |
811 | ||
812 | if (lu == NULL || ssl_md(lu->hash_idx) == NULL) { | |
813 | return NULL; | |
814 | } | |
815 | return lu; | |
816 | } | |
817 | return &legacy_rsa_sigalg; | |
818 | } | |
819 | /* Set peer sigalg based key type */ | |
820 | int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey) | |
821 | { | |
822 | int idx = ssl_cert_type(NULL, pkey); | |
823 | ||
824 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, idx); | |
825 | if (lu == NULL) | |
826 | return 0; | |
827 | s->s3->tmp.peer_sigalg = lu; | |
828 | return 1; | |
829 | } | |
703bcee0 | 830 | |
98c792d1 | 831 | size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs) |
0f113f3e MC |
832 | { |
833 | /* | |
834 | * If Suite B mode use Suite B sigalgs only, ignore any other | |
835 | * preferences. | |
836 | */ | |
e481f9b9 | 837 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
838 | switch (tls1_suiteb(s)) { |
839 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
840 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 841 | return OSSL_NELEM(suiteb_sigalgs); |
0f113f3e MC |
842 | |
843 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
844 | *psigs = suiteb_sigalgs; | |
7a531ee4 | 845 | return 1; |
0f113f3e MC |
846 | |
847 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
7a531ee4 MC |
848 | *psigs = suiteb_sigalgs + 1; |
849 | return 1; | |
0f113f3e | 850 | } |
e481f9b9 | 851 | #endif |
a9669ddc DSH |
852 | /* |
853 | * We use client_sigalgs (if not NULL) if we're a server | |
854 | * and sending a certificate request or if we're a client and | |
855 | * determining which shared algorithm to use. | |
856 | */ | |
857 | if ((s->server == sent) && s->cert->client_sigalgs != NULL) { | |
0f113f3e MC |
858 | *psigs = s->cert->client_sigalgs; |
859 | return s->cert->client_sigalgslen; | |
860 | } else if (s->cert->conf_sigalgs) { | |
861 | *psigs = s->cert->conf_sigalgs; | |
862 | return s->cert->conf_sigalgslen; | |
863 | } else { | |
864 | *psigs = tls12_sigalgs; | |
703bcee0 | 865 | return OSSL_NELEM(tls12_sigalgs); |
0f113f3e MC |
866 | } |
867 | } | |
868 | ||
869 | /* | |
870 | * Check signature algorithm is consistent with sent supported signature | |
b2eb6998 DSH |
871 | * algorithms and if so set relevant digest and signature scheme in |
872 | * s. | |
ec4a50b3 | 873 | */ |
f742cda8 | 874 | int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey) |
0f113f3e | 875 | { |
98c792d1 | 876 | const uint16_t *sent_sigs; |
5554facb | 877 | const EVP_MD *md = NULL; |
703bcee0 | 878 | char sigalgstr[2]; |
0f113f3e | 879 | size_t sent_sigslen, i; |
536199ec | 880 | int pkeyid = EVP_PKEY_id(pkey); |
f742cda8 | 881 | const SIGALG_LOOKUP *lu; |
4d43ee28 | 882 | |
0f113f3e | 883 | /* Should never happen */ |
536199ec | 884 | if (pkeyid == -1) |
0f113f3e | 885 | return -1; |
5a8916d9 DSH |
886 | if (SSL_IS_TLS13(s)) { |
887 | /* Disallow DSA for TLS 1.3 */ | |
888 | if (pkeyid == EVP_PKEY_DSA) { | |
889 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
890 | return 0; | |
891 | } | |
892 | /* Only allow PSS for TLS 1.3 */ | |
893 | if (pkeyid == EVP_PKEY_RSA) | |
894 | pkeyid = EVP_PKEY_RSA_PSS; | |
895 | } | |
f742cda8 DSH |
896 | lu = tls1_lookup_sigalg(sig); |
897 | /* | |
095a982b DSH |
898 | * Check sigalgs is known. Disallow SHA1 with TLS 1.3. Check key type is |
899 | * consistent with signature: RSA keys can be used for RSA-PSS | |
f742cda8 | 900 | */ |
095a982b DSH |
901 | if (lu == NULL || (SSL_IS_TLS13(s) && lu->hash == NID_sha1) |
902 | || (pkeyid != lu->sig | |
f742cda8 | 903 | && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) { |
0f113f3e MC |
904 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
905 | return 0; | |
906 | } | |
e481f9b9 | 907 | #ifndef OPENSSL_NO_EC |
fe3066ee | 908 | if (pkeyid == EVP_PKEY_EC) { |
8f88cb53 | 909 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
f1adb006 | 910 | int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); |
44b6318f | 911 | |
8f88cb53 | 912 | if (SSL_IS_TLS13(s)) { |
e892e325 DSH |
913 | if (EC_KEY_get_conv_form(ec) != POINT_CONVERSION_UNCOMPRESSED) { |
914 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
915 | SSL_R_ILLEGAL_POINT_COMPRESSION); | |
916 | return 0; | |
917 | } | |
8f88cb53 | 918 | /* For TLS 1.3 check curve matches signature algorithm */ |
a34a9df0 | 919 | if (lu->curve != NID_undef && curve != lu->curve) { |
8f88cb53 DSH |
920 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); |
921 | return 0; | |
922 | } | |
923 | } else { | |
924 | unsigned char curve_id[2], comp_id; | |
44b6318f | 925 | |
8f88cb53 DSH |
926 | /* Check compression and curve matches extensions */ |
927 | if (!tls1_set_ec_id(curve_id, &comp_id, ec)) | |
0f113f3e | 928 | return 0; |
8f88cb53 DSH |
929 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { |
930 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); | |
931 | return 0; | |
932 | } | |
8f88cb53 | 933 | if (tls1_suiteb(s)) { |
f1adb006 DSH |
934 | /* Check sigalg matches a permissible Suite B value */ |
935 | if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256 | |
936 | && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) { | |
937 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
938 | SSL_R_WRONG_SIGNATURE_TYPE); | |
0f113f3e | 939 | return 0; |
f1adb006 DSH |
940 | } |
941 | /* | |
942 | * Suite B also requires P-256+SHA256 and P-384+SHA384: | |
943 | * this matches the TLS 1.3 requirements so we can just | |
944 | * check the curve is the expected TLS 1.3 value. | |
945 | * If this fails an inappropriate digest is being used. | |
946 | */ | |
947 | if (curve != lu->curve) { | |
948 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
949 | SSL_R_ILLEGAL_SUITEB_DIGEST); | |
0f113f3e MC |
950 | return 0; |
951 | } | |
8f88cb53 | 952 | } |
0f113f3e | 953 | } |
8f88cb53 | 954 | } else if (tls1_suiteb(s)) { |
0f113f3e | 955 | return 0; |
8f88cb53 | 956 | } |
e481f9b9 | 957 | #endif |
0f113f3e MC |
958 | |
959 | /* Check signature matches a type we sent */ | |
a9669ddc | 960 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); |
536199ec | 961 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { |
703bcee0 | 962 | if (sig == *sent_sigs) |
0f113f3e MC |
963 | break; |
964 | } | |
965 | /* Allow fallback to SHA1 if not strict mode */ | |
f742cda8 DSH |
966 | if (i == sent_sigslen && (lu->hash != NID_sha1 |
967 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
0f113f3e MC |
968 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
969 | return 0; | |
970 | } | |
44b6318f | 971 | md = ssl_md(lu->hash_idx); |
5554facb | 972 | if (md == NULL) { |
0f113f3e MC |
973 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); |
974 | return 0; | |
975 | } | |
703bcee0 MC |
976 | /* |
977 | * Make sure security callback allows algorithm. For historical reasons we | |
978 | * have to pass the sigalg as a two byte char array. | |
979 | */ | |
980 | sigalgstr[0] = (sig >> 8) & 0xff; | |
981 | sigalgstr[1] = sig & 0xff; | |
0f113f3e | 982 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, |
5554facb | 983 | EVP_MD_size(md) * 4, EVP_MD_type(md), |
703bcee0 | 984 | (void *)sigalgstr)) { |
0f113f3e MC |
985 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
986 | return 0; | |
987 | } | |
6cbebb55 | 988 | /* Store the sigalg the peer uses */ |
f742cda8 | 989 | s->s3->tmp.peer_sigalg = lu; |
0f113f3e MC |
990 | return 1; |
991 | } | |
2ea80354 | 992 | |
42ef7aea DSH |
993 | int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid) |
994 | { | |
f742cda8 | 995 | if (s->s3->tmp.peer_sigalg == NULL) |
42ef7aea | 996 | return 0; |
f742cda8 | 997 | *pnid = s->s3->tmp.peer_sigalg->sig; |
42ef7aea DSH |
998 | return 1; |
999 | } | |
1000 | ||
0f113f3e | 1001 | /* |
3eb2aff4 KR |
1002 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
1003 | * supported, doesn't appear in supported signature algorithms, isn't supported | |
1004 | * by the enabled protocol versions or by the security level. | |
1005 | * | |
1006 | * This function should only be used for checking which ciphers are supported | |
1007 | * by the client. | |
1008 | * | |
1009 | * Call ssl_cipher_disabled() to check that it's enabled or not. | |
b7bfe69b DSH |
1010 | */ |
1011 | void ssl_set_client_disabled(SSL *s) | |
0f113f3e | 1012 | { |
4d69f9e6 DSH |
1013 | s->s3->tmp.mask_a = 0; |
1014 | s->s3->tmp.mask_k = 0; | |
4d69f9e6 | 1015 | ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
3eb2aff4 | 1016 | ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver); |
a230b26e | 1017 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
1018 | /* with PSK there must be client callback set */ |
1019 | if (!s->psk_client_callback) { | |
4d69f9e6 | 1020 | s->s3->tmp.mask_a |= SSL_aPSK; |
fe5eef3a | 1021 | s->s3->tmp.mask_k |= SSL_PSK; |
0f113f3e | 1022 | } |
a230b26e | 1023 | #endif /* OPENSSL_NO_PSK */ |
e481f9b9 | 1024 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 1025 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
4d69f9e6 DSH |
1026 | s->s3->tmp.mask_a |= SSL_aSRP; |
1027 | s->s3->tmp.mask_k |= SSL_kSRP; | |
0f113f3e | 1028 | } |
e481f9b9 | 1029 | #endif |
0f113f3e | 1030 | } |
fc101f88 | 1031 | |
3eb2aff4 KR |
1032 | /* |
1033 | * ssl_cipher_disabled - check that a cipher is disabled or not | |
1034 | * @s: SSL connection that you want to use the cipher on | |
1035 | * @c: cipher to check | |
1036 | * @op: Security check that you want to do | |
1037 | * | |
1038 | * Returns 1 when it's disabled, 0 when enabled. | |
1039 | */ | |
b362ccab | 1040 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op) |
0f113f3e | 1041 | { |
3eb2aff4 | 1042 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
4d69f9e6 | 1043 | || c->algorithm_auth & s->s3->tmp.mask_a) |
0f113f3e | 1044 | return 1; |
3eb2aff4 KR |
1045 | if (s->s3->tmp.max_ver == 0) |
1046 | return 1; | |
1047 | if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver) | |
a230b26e | 1048 | || (c->max_tls < s->s3->tmp.min_ver))) |
3eb2aff4 KR |
1049 | return 1; |
1050 | if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver) | |
a230b26e | 1051 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) |
3eb2aff4 KR |
1052 | return 1; |
1053 | ||
0f113f3e MC |
1054 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
1055 | } | |
b362ccab | 1056 | |
7da160b0 | 1057 | int tls_use_ticket(SSL *s) |
0f113f3e | 1058 | { |
08191294 | 1059 | if ((s->options & SSL_OP_NO_TICKET)) |
0f113f3e MC |
1060 | return 0; |
1061 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); | |
1062 | } | |
ed3883d2 | 1063 | |
e469af8d | 1064 | int tls1_set_server_sigalgs(SSL *s) |
0f113f3e MC |
1065 | { |
1066 | int al; | |
1067 | size_t i; | |
8483a003 F |
1068 | |
1069 | /* Clear any shared signature algorithms */ | |
b548a1f1 RS |
1070 | OPENSSL_free(s->cert->shared_sigalgs); |
1071 | s->cert->shared_sigalgs = NULL; | |
1072 | s->cert->shared_sigalgslen = 0; | |
9195ddcd DSH |
1073 | /* Clear certificate validity flags */ |
1074 | for (i = 0; i < SSL_PKEY_NUM; i++) | |
6383d316 | 1075 | s->s3->tmp.valid_flags[i] = 0; |
a8bb912d DSH |
1076 | /* |
1077 | * If peer sent no signature algorithms check to see if we support | |
1078 | * the default algorithm for each certificate type | |
1079 | */ | |
1080 | if (s->s3->tmp.peer_sigalgs == NULL) { | |
1081 | const uint16_t *sent_sigs; | |
1082 | size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
0f113f3e | 1083 | |
a8bb912d DSH |
1084 | for (i = 0; i < SSL_PKEY_NUM; i++) { |
1085 | const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i); | |
1086 | size_t j; | |
1087 | ||
1088 | if (lu == NULL) | |
1089 | continue; | |
1090 | /* Check default matches a type we sent */ | |
1091 | for (j = 0; j < sent_sigslen; j++) { | |
1092 | if (lu->sigalg == sent_sigs[j]) { | |
1093 | s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN; | |
1094 | break; | |
1095 | } | |
1096 | } | |
1097 | } | |
9195ddcd | 1098 | return 1; |
a8bb912d | 1099 | } |
9195ddcd DSH |
1100 | |
1101 | if (!tls1_process_sigalgs(s)) { | |
1102 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); | |
1103 | al = SSL_AD_INTERNAL_ERROR; | |
1104 | goto err; | |
d376e57d | 1105 | } |
9195ddcd DSH |
1106 | if (s->cert->shared_sigalgs != NULL) |
1107 | return 1; | |
1108 | /* Fatal error is no shared signature algorithms */ | |
1109 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); | |
1110 | al = SSL_AD_ILLEGAL_PARAMETER; | |
0f113f3e MC |
1111 | err: |
1112 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
1113 | return 0; | |
1114 | } | |
e469af8d | 1115 | |
1d97c843 | 1116 | /*- |
1ab3836b | 1117 | * Gets the ticket information supplied by the client if any. |
e7f0d921 | 1118 | * |
1ab3836b | 1119 | * hello: The parsed ClientHello data |
c519e89f BM |
1120 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
1121 | * point to the resulting session. | |
1122 | * | |
1123 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key | |
1124 | * ciphersuite, in which case we have no use for session tickets and one will | |
aff8c126 | 1125 | * never be decrypted, nor will s->ext.ticket_expected be set to 1. |
c519e89f BM |
1126 | * |
1127 | * Returns: | |
1128 | * -1: fatal error, either from parsing or decrypting the ticket. | |
1129 | * 0: no ticket was found (or was ignored, based on settings). | |
1130 | * 1: a zero length extension was found, indicating that the client supports | |
1131 | * session tickets but doesn't currently have one to offer. | |
1132 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but | |
1133 | * couldn't be decrypted because of a non-fatal error. | |
1134 | * 3: a ticket was successfully decrypted and *ret was set. | |
1135 | * | |
1136 | * Side effects: | |
aff8c126 | 1137 | * Sets s->ext.ticket_expected to 1 if the server will have to issue |
c519e89f BM |
1138 | * a new session ticket to the client because the client indicated support |
1139 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | |
1140 | * a session ticket or we couldn't use the one it gave us, or if | |
aff8c126 RS |
1141 | * s->ctx->ext.ticket_key_cb asked to renew the client's ticket. |
1142 | * Otherwise, s->ext.ticket_expected is set to 0. | |
6434abbf | 1143 | */ |
ddf6ec00 MC |
1144 | TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
1145 | SSL_SESSION **ret) | |
0f113f3e | 1146 | { |
1ab3836b | 1147 | int retv; |
1ab3836b MC |
1148 | size_t size; |
1149 | RAW_EXTENSION *ticketext; | |
e7f0d921 | 1150 | |
0f113f3e | 1151 | *ret = NULL; |
aff8c126 | 1152 | s->ext.ticket_expected = 0; |
0f113f3e MC |
1153 | |
1154 | /* | |
9362c93e MC |
1155 | * If tickets disabled or not supported by the protocol version |
1156 | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful | |
0f113f3e MC |
1157 | * resumption. |
1158 | */ | |
1ab3836b | 1159 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
ddf6ec00 | 1160 | return TICKET_NONE; |
9ceb2426 | 1161 | |
70af3d8e MC |
1162 | ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket]; |
1163 | if (!ticketext->present) | |
ddf6ec00 | 1164 | return TICKET_NONE; |
1ab3836b MC |
1165 | |
1166 | size = PACKET_remaining(&ticketext->data); | |
1167 | if (size == 0) { | |
1168 | /* | |
1169 | * The client will accept a ticket but doesn't currently have | |
1170 | * one. | |
1171 | */ | |
aff8c126 | 1172 | s->ext.ticket_expected = 1; |
ddf6ec00 | 1173 | return TICKET_EMPTY; |
9ceb2426 | 1174 | } |
aff8c126 | 1175 | if (s->ext.session_secret_cb) { |
1ab3836b MC |
1176 | /* |
1177 | * Indicate that the ticket couldn't be decrypted rather than | |
1178 | * generating the session from ticket now, trigger | |
1179 | * abbreviated handshake based on external mechanism to | |
1180 | * calculate the master secret later. | |
1181 | */ | |
ddf6ec00 | 1182 | return TICKET_NO_DECRYPT; |
1ab3836b | 1183 | } |
70af3d8e MC |
1184 | |
1185 | retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size, | |
1186 | hello->session_id, hello->session_id_len, ret); | |
1ab3836b | 1187 | switch (retv) { |
61c32649 | 1188 | case TICKET_NO_DECRYPT: |
aff8c126 | 1189 | s->ext.ticket_expected = 1; |
ddf6ec00 | 1190 | return TICKET_NO_DECRYPT; |
9ceb2426 | 1191 | |
61c32649 | 1192 | case TICKET_SUCCESS: |
ddf6ec00 | 1193 | return TICKET_SUCCESS; |
9ceb2426 | 1194 | |
61c32649 | 1195 | case TICKET_SUCCESS_RENEW: |
aff8c126 | 1196 | s->ext.ticket_expected = 1; |
ddf6ec00 | 1197 | return TICKET_SUCCESS; |
e7f0d921 | 1198 | |
61c32649 | 1199 | default: |
ddf6ec00 | 1200 | return TICKET_FATAL_ERR_OTHER; |
0f113f3e | 1201 | } |
1ab3836b MC |
1202 | } |
1203 | ||
1d97c843 TH |
1204 | /*- |
1205 | * tls_decrypt_ticket attempts to decrypt a session ticket. | |
c519e89f BM |
1206 | * |
1207 | * etick: points to the body of the session ticket extension. | |
8483a003 | 1208 | * eticklen: the length of the session tickets extension. |
c519e89f BM |
1209 | * sess_id: points at the session ID. |
1210 | * sesslen: the length of the session ID. | |
1211 | * psess: (output) on return, if a ticket was decrypted, then this is set to | |
1212 | * point to the resulting session. | |
c519e89f | 1213 | */ |
ddf6ec00 MC |
1214 | TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
1215 | size_t eticklen, const unsigned char *sess_id, | |
1216 | size_t sesslen, SSL_SESSION **psess) | |
0f113f3e MC |
1217 | { |
1218 | SSL_SESSION *sess; | |
1219 | unsigned char *sdec; | |
1220 | const unsigned char *p; | |
ddf6ec00 MC |
1221 | int slen, renew_ticket = 0, declen; |
1222 | TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER; | |
348240c6 | 1223 | size_t mlen; |
0f113f3e | 1224 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
bf7c6817 | 1225 | HMAC_CTX *hctx = NULL; |
846ec07d | 1226 | EVP_CIPHER_CTX *ctx; |
222da979 | 1227 | SSL_CTX *tctx = s->session_ctx; |
e97763c9 | 1228 | |
0f113f3e | 1229 | /* Initialize session ticket encryption and HMAC contexts */ |
bf7c6817 RL |
1230 | hctx = HMAC_CTX_new(); |
1231 | if (hctx == NULL) | |
1053a6e2 | 1232 | return TICKET_FATAL_ERR_MALLOC; |
846ec07d | 1233 | ctx = EVP_CIPHER_CTX_new(); |
35b1a433 | 1234 | if (ctx == NULL) { |
1053a6e2 | 1235 | ret = TICKET_FATAL_ERR_MALLOC; |
35b1a433 MC |
1236 | goto err; |
1237 | } | |
aff8c126 | 1238 | if (tctx->ext.ticket_key_cb) { |
0f113f3e | 1239 | unsigned char *nctick = (unsigned char *)etick; |
aff8c126 | 1240 | int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16, |
846ec07d | 1241 | ctx, hctx, 0); |
0f113f3e | 1242 | if (rv < 0) |
35b1a433 MC |
1243 | goto err; |
1244 | if (rv == 0) { | |
1053a6e2 | 1245 | ret = TICKET_NO_DECRYPT; |
35b1a433 MC |
1246 | goto err; |
1247 | } | |
0f113f3e MC |
1248 | if (rv == 2) |
1249 | renew_ticket = 1; | |
1250 | } else { | |
1251 | /* Check key name matches */ | |
aff8c126 RS |
1252 | if (memcmp(etick, tctx->ext.tick_key_name, |
1253 | sizeof(tctx->ext.tick_key_name)) != 0) { | |
1053a6e2 | 1254 | ret = TICKET_NO_DECRYPT; |
35b1a433 MC |
1255 | goto err; |
1256 | } | |
aff8c126 RS |
1257 | if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key, |
1258 | sizeof(tctx->ext.tick_hmac_key), | |
5f3d93e4 | 1259 | EVP_sha256(), NULL) <= 0 |
a230b26e | 1260 | || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, |
aff8c126 | 1261 | tctx->ext.tick_aes_key, |
1053a6e2 MC |
1262 | etick |
1263 | + sizeof(tctx->ext.tick_key_name)) <= 0) { | |
5f3d93e4 | 1264 | goto err; |
a230b26e | 1265 | } |
0f113f3e MC |
1266 | } |
1267 | /* | |
1268 | * Attempt to process session ticket, first conduct sanity and integrity | |
1269 | * checks on ticket. | |
1270 | */ | |
bf7c6817 | 1271 | mlen = HMAC_size(hctx); |
348240c6 | 1272 | if (mlen == 0) { |
5f3d93e4 | 1273 | goto err; |
0f113f3e | 1274 | } |
e97763c9 DSH |
1275 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
1276 | if (eticklen <= | |
348240c6 | 1277 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
1053a6e2 | 1278 | ret = TICKET_NO_DECRYPT; |
e97763c9 DSH |
1279 | goto err; |
1280 | } | |
0f113f3e MC |
1281 | eticklen -= mlen; |
1282 | /* Check HMAC of encrypted ticket */ | |
bf7c6817 | 1283 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
a230b26e | 1284 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { |
5f3d93e4 MC |
1285 | goto err; |
1286 | } | |
bf7c6817 | 1287 | HMAC_CTX_free(hctx); |
0f113f3e | 1288 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
846ec07d | 1289 | EVP_CIPHER_CTX_free(ctx); |
1053a6e2 | 1290 | return TICKET_NO_DECRYPT; |
0f113f3e MC |
1291 | } |
1292 | /* Attempt to decrypt session data */ | |
1293 | /* Move p after IV to start of encrypted ticket, update length */ | |
57b0d651 BE |
1294 | p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); |
1295 | eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx); | |
0f113f3e | 1296 | sdec = OPENSSL_malloc(eticklen); |
348240c6 MC |
1297 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
1298 | (int)eticklen) <= 0) { | |
846ec07d | 1299 | EVP_CIPHER_CTX_free(ctx); |
d1247df2 | 1300 | OPENSSL_free(sdec); |
1053a6e2 | 1301 | return TICKET_FATAL_ERR_OTHER; |
0f113f3e | 1302 | } |
348240c6 | 1303 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
846ec07d | 1304 | EVP_CIPHER_CTX_free(ctx); |
0f113f3e | 1305 | OPENSSL_free(sdec); |
1053a6e2 | 1306 | return TICKET_NO_DECRYPT; |
0f113f3e | 1307 | } |
348240c6 | 1308 | slen += declen; |
846ec07d RL |
1309 | EVP_CIPHER_CTX_free(ctx); |
1310 | ctx = NULL; | |
0f113f3e MC |
1311 | p = sdec; |
1312 | ||
1313 | sess = d2i_SSL_SESSION(NULL, &p, slen); | |
1314 | OPENSSL_free(sdec); | |
1315 | if (sess) { | |
79020b27 BE |
1316 | /* Some additional consistency checks */ |
1317 | if (p != sdec + slen || sess->session_id_length != 0) { | |
1318 | SSL_SESSION_free(sess); | |
1319 | return 2; | |
1320 | } | |
0f113f3e MC |
1321 | /* |
1322 | * The session ID, if non-empty, is used by some clients to detect | |
1323 | * that the ticket has been accepted. So we copy it to the session | |
1324 | * structure. If it is empty set length to zero as required by | |
1325 | * standard. | |
1326 | */ | |
1327 | if (sesslen) | |
1328 | memcpy(sess->session_id, sess_id, sesslen); | |
1329 | sess->session_id_length = sesslen; | |
1330 | *psess = sess; | |
1331 | if (renew_ticket) | |
1053a6e2 | 1332 | return TICKET_SUCCESS_RENEW; |
0f113f3e | 1333 | else |
1053a6e2 | 1334 | return TICKET_SUCCESS; |
0f113f3e MC |
1335 | } |
1336 | ERR_clear_error(); | |
1337 | /* | |
1338 | * For session parse failure, indicate that we need to send a new ticket. | |
1339 | */ | |
1053a6e2 | 1340 | return TICKET_NO_DECRYPT; |
a230b26e | 1341 | err: |
846ec07d | 1342 | EVP_CIPHER_CTX_free(ctx); |
bf7c6817 | 1343 | HMAC_CTX_free(hctx); |
35b1a433 | 1344 | return ret; |
0f113f3e | 1345 | } |
6434abbf | 1346 | |
536199ec | 1347 | static int tls12_get_pkey_idx(int sig_nid) |
0f113f3e | 1348 | { |
536199ec | 1349 | switch (sig_nid) { |
e481f9b9 | 1350 | #ifndef OPENSSL_NO_RSA |
536199ec | 1351 | case EVP_PKEY_RSA: |
d0ff28f8 | 1352 | return SSL_PKEY_RSA; |
b2eb6998 DSH |
1353 | /* |
1354 | * For now return RSA key for PSS. When we support PSS only keys | |
1355 | * this will need to be updated. | |
1356 | */ | |
1357 | case EVP_PKEY_RSA_PSS: | |
d0ff28f8 | 1358 | return SSL_PKEY_RSA; |
e481f9b9 MC |
1359 | #endif |
1360 | #ifndef OPENSSL_NO_DSA | |
536199ec | 1361 | case EVP_PKEY_DSA: |
0f113f3e | 1362 | return SSL_PKEY_DSA_SIGN; |
e481f9b9 MC |
1363 | #endif |
1364 | #ifndef OPENSSL_NO_EC | |
536199ec | 1365 | case EVP_PKEY_EC: |
0f113f3e | 1366 | return SSL_PKEY_ECC; |
e481f9b9 | 1367 | #endif |
a230b26e | 1368 | #ifndef OPENSSL_NO_GOST |
536199ec | 1369 | case NID_id_GostR3410_2001: |
e44380a9 DB |
1370 | return SSL_PKEY_GOST01; |
1371 | ||
536199ec | 1372 | case NID_id_GostR3410_2012_256: |
e44380a9 DB |
1373 | return SSL_PKEY_GOST12_256; |
1374 | ||
536199ec | 1375 | case NID_id_GostR3410_2012_512: |
e44380a9 | 1376 | return SSL_PKEY_GOST12_512; |
a230b26e | 1377 | #endif |
0f113f3e MC |
1378 | } |
1379 | return -1; | |
1380 | } | |
4453cd8c | 1381 | |
b362ccab | 1382 | /* Check to see if a signature algorithm is allowed */ |
b0e9ab95 | 1383 | static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu) |
0f113f3e | 1384 | { |
703bcee0 | 1385 | unsigned char sigalgstr[2]; |
44b6318f | 1386 | int secbits; |
703bcee0 | 1387 | |
44b6318f DSH |
1388 | /* See if sigalgs is recognised and if hash is enabled */ |
1389 | if (lu == NULL || ssl_md(lu->hash_idx) == NULL) | |
0f113f3e | 1390 | return 0; |
224b4e37 DSH |
1391 | /* DSA is not allowed in TLS 1.3 */ |
1392 | if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA) | |
1393 | return 0; | |
0f113f3e | 1394 | /* See if public key algorithm allowed */ |
44b6318f | 1395 | if (tls12_get_pkey_idx(lu->sig) == -1) |
0f113f3e | 1396 | return 0; |
44b6318f DSH |
1397 | /* Security bits: half digest bits */ |
1398 | secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4; | |
0f113f3e | 1399 | /* Finally see if security callback allows it */ |
b0e9ab95 DSH |
1400 | sigalgstr[0] = (lu->sigalg >> 8) & 0xff; |
1401 | sigalgstr[1] = lu->sigalg & 0xff; | |
44b6318f | 1402 | return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr); |
0f113f3e MC |
1403 | } |
1404 | ||
1405 | /* | |
1406 | * Get a mask of disabled public key algorithms based on supported signature | |
1407 | * algorithms. For example if no signature algorithm supports RSA then RSA is | |
1408 | * disabled. | |
b362ccab DSH |
1409 | */ |
1410 | ||
90d9e49a | 1411 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) |
0f113f3e | 1412 | { |
98c792d1 | 1413 | const uint16_t *sigalgs; |
0f113f3e MC |
1414 | size_t i, sigalgslen; |
1415 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; | |
1416 | /* | |
1417 | * Now go through all signature algorithms seeing if we support any for | |
1418 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep | |
1419 | * down calls to security callback only check if we have to. | |
1420 | */ | |
a9669ddc | 1421 | sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs); |
703bcee0 | 1422 | for (i = 0; i < sigalgslen; i ++, sigalgs++) { |
b0e9ab95 DSH |
1423 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs); |
1424 | ||
1425 | if (lu == NULL) | |
1426 | continue; | |
1427 | switch (lu->sig) { | |
e481f9b9 | 1428 | #ifndef OPENSSL_NO_RSA |
b2eb6998 DSH |
1429 | /* Any RSA-PSS signature algorithms also mean we allow RSA */ |
1430 | case EVP_PKEY_RSA_PSS: | |
536199ec | 1431 | case EVP_PKEY_RSA: |
b0e9ab95 | 1432 | if (!have_rsa && tls12_sigalg_allowed(s, op, lu)) |
0f113f3e MC |
1433 | have_rsa = 1; |
1434 | break; | |
e481f9b9 MC |
1435 | #endif |
1436 | #ifndef OPENSSL_NO_DSA | |
536199ec | 1437 | case EVP_PKEY_DSA: |
b0e9ab95 | 1438 | if (!have_dsa && tls12_sigalg_allowed(s, op, lu)) |
0f113f3e MC |
1439 | have_dsa = 1; |
1440 | break; | |
e481f9b9 MC |
1441 | #endif |
1442 | #ifndef OPENSSL_NO_EC | |
536199ec | 1443 | case EVP_PKEY_EC: |
b0e9ab95 | 1444 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, lu)) |
0f113f3e MC |
1445 | have_ecdsa = 1; |
1446 | break; | |
e481f9b9 | 1447 | #endif |
0f113f3e MC |
1448 | } |
1449 | } | |
1450 | if (!have_rsa) | |
1451 | *pmask_a |= SSL_aRSA; | |
1452 | if (!have_dsa) | |
1453 | *pmask_a |= SSL_aDSS; | |
1454 | if (!have_ecdsa) | |
1455 | *pmask_a |= SSL_aECDSA; | |
1456 | } | |
b362ccab | 1457 | |
ae2f7b37 | 1458 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
98c792d1 | 1459 | const uint16_t *psig, size_t psiglen) |
2c7b4dbc MC |
1460 | { |
1461 | size_t i; | |
b0e9ab95 | 1462 | int rv = 0; |
c0f9e23c | 1463 | |
703bcee0 | 1464 | for (i = 0; i < psiglen; i++, psig++) { |
b0e9ab95 DSH |
1465 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig); |
1466 | ||
1467 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu)) | |
1468 | continue; | |
1469 | if (!WPACKET_put_bytes_u16(pkt, *psig)) | |
1470 | return 0; | |
1471 | /* | |
1472 | * If TLS 1.3 must have at least one valid TLS 1.3 message | |
1473 | * signing algorithm: i.e. neither RSA nor SHA1 | |
1474 | */ | |
1475 | if (rv == 0 && (!SSL_IS_TLS13(s) | |
1476 | || (lu->sig != EVP_PKEY_RSA && lu->hash != NID_sha1))) | |
1477 | rv = 1; | |
2c7b4dbc | 1478 | } |
b0e9ab95 | 1479 | return rv; |
2c7b4dbc MC |
1480 | } |
1481 | ||
4453cd8c | 1482 | /* Given preference and allowed sigalgs set shared sigalgs */ |
4d43ee28 | 1483 | static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig, |
98c792d1 DSH |
1484 | const uint16_t *pref, size_t preflen, |
1485 | const uint16_t *allow, size_t allowlen) | |
0f113f3e | 1486 | { |
98c792d1 | 1487 | const uint16_t *ptmp, *atmp; |
0f113f3e | 1488 | size_t i, j, nmatch = 0; |
703bcee0 | 1489 | for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) { |
b0e9ab95 DSH |
1490 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp); |
1491 | ||
0f113f3e | 1492 | /* Skip disabled hashes or signature algorithms */ |
b0e9ab95 | 1493 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu)) |
0f113f3e | 1494 | continue; |
703bcee0 MC |
1495 | for (j = 0, atmp = allow; j < allowlen; j++, atmp++) { |
1496 | if (*ptmp == *atmp) { | |
0f113f3e | 1497 | nmatch++; |
b0e9ab95 DSH |
1498 | if (shsig) |
1499 | *shsig++ = lu; | |
0f113f3e MC |
1500 | break; |
1501 | } | |
1502 | } | |
1503 | } | |
1504 | return nmatch; | |
1505 | } | |
4453cd8c DSH |
1506 | |
1507 | /* Set shared signature algorithms for SSL structures */ | |
1508 | static int tls1_set_shared_sigalgs(SSL *s) | |
0f113f3e | 1509 | { |
98c792d1 | 1510 | const uint16_t *pref, *allow, *conf; |
0f113f3e MC |
1511 | size_t preflen, allowlen, conflen; |
1512 | size_t nmatch; | |
4d43ee28 | 1513 | const SIGALG_LOOKUP **salgs = NULL; |
0f113f3e MC |
1514 | CERT *c = s->cert; |
1515 | unsigned int is_suiteb = tls1_suiteb(s); | |
b548a1f1 RS |
1516 | |
1517 | OPENSSL_free(c->shared_sigalgs); | |
1518 | c->shared_sigalgs = NULL; | |
1519 | c->shared_sigalgslen = 0; | |
0f113f3e MC |
1520 | /* If client use client signature algorithms if not NULL */ |
1521 | if (!s->server && c->client_sigalgs && !is_suiteb) { | |
1522 | conf = c->client_sigalgs; | |
1523 | conflen = c->client_sigalgslen; | |
1524 | } else if (c->conf_sigalgs && !is_suiteb) { | |
1525 | conf = c->conf_sigalgs; | |
1526 | conflen = c->conf_sigalgslen; | |
1527 | } else | |
a9669ddc | 1528 | conflen = tls12_get_psigalgs(s, 0, &conf); |
0f113f3e MC |
1529 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { |
1530 | pref = conf; | |
1531 | preflen = conflen; | |
76106e60 DSH |
1532 | allow = s->s3->tmp.peer_sigalgs; |
1533 | allowlen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
1534 | } else { |
1535 | allow = conf; | |
1536 | allowlen = conflen; | |
76106e60 DSH |
1537 | pref = s->s3->tmp.peer_sigalgs; |
1538 | preflen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
1539 | } |
1540 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); | |
34e3edbf | 1541 | if (nmatch) { |
4d43ee28 | 1542 | salgs = OPENSSL_malloc(nmatch * sizeof(*salgs)); |
a71edf3b | 1543 | if (salgs == NULL) |
34e3edbf DSH |
1544 | return 0; |
1545 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); | |
1546 | } else { | |
1547 | salgs = NULL; | |
1548 | } | |
0f113f3e MC |
1549 | c->shared_sigalgs = salgs; |
1550 | c->shared_sigalgslen = nmatch; | |
1551 | return 1; | |
1552 | } | |
4453cd8c | 1553 | |
6b7be581 DSH |
1554 | /* Set preferred digest for each key type */ |
1555 | ||
703bcee0 | 1556 | int tls1_save_sigalgs(SSL *s, PACKET *pkt) |
0f113f3e MC |
1557 | { |
1558 | CERT *c = s->cert; | |
98c792d1 | 1559 | unsigned int stmp; |
703bcee0 MC |
1560 | size_t size, i; |
1561 | ||
0f113f3e MC |
1562 | /* Extension ignored for inappropriate versions */ |
1563 | if (!SSL_USE_SIGALGS(s)) | |
1564 | return 1; | |
1565 | /* Should never happen */ | |
1566 | if (!c) | |
1567 | return 0; | |
1568 | ||
703bcee0 MC |
1569 | size = PACKET_remaining(pkt); |
1570 | ||
1571 | /* Invalid data length */ | |
8f12296e | 1572 | if (size == 0 || (size & 1) != 0) |
703bcee0 MC |
1573 | return 0; |
1574 | ||
1575 | size >>= 1; | |
1576 | ||
76106e60 | 1577 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
536199ec MC |
1578 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(size |
1579 | * sizeof(*s->s3->tmp.peer_sigalgs)); | |
76106e60 | 1580 | if (s->s3->tmp.peer_sigalgs == NULL) |
0f113f3e | 1581 | return 0; |
703bcee0 | 1582 | s->s3->tmp.peer_sigalgslen = size; |
98c792d1 DSH |
1583 | for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++) |
1584 | s->s3->tmp.peer_sigalgs[i] = stmp; | |
703bcee0 MC |
1585 | |
1586 | if (i != size) | |
1587 | return 0; | |
1588 | ||
0f113f3e MC |
1589 | return 1; |
1590 | } | |
6b7be581 | 1591 | |
c800c27a | 1592 | int tls1_process_sigalgs(SSL *s) |
0f113f3e | 1593 | { |
0f113f3e | 1594 | size_t i; |
f7d53487 | 1595 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
0f113f3e | 1596 | CERT *c = s->cert; |
4d43ee28 | 1597 | |
0f113f3e MC |
1598 | if (!tls1_set_shared_sigalgs(s)) |
1599 | return 0; | |
1600 | ||
9195ddcd DSH |
1601 | for (i = 0; i < SSL_PKEY_NUM; i++) |
1602 | pvalid[i] = 0; | |
1603 | ||
4d43ee28 DSH |
1604 | for (i = 0; i < c->shared_sigalgslen; i++) { |
1605 | const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i]; | |
9195ddcd | 1606 | int idx = sigptr->sig_idx; |
4d43ee28 | 1607 | |
523fb323 | 1608 | /* Ignore PKCS1 based sig algs in TLSv1.3 */ |
4d43ee28 | 1609 | if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA) |
523fb323 | 1610 | continue; |
9195ddcd DSH |
1611 | /* If not disabled indicate we can explicitly sign */ |
1612 | if (pvalid[idx] == 0 && tls12_get_pkey_idx(sigptr->sig) != -1) | |
a8bb912d | 1613 | pvalid[sigptr->sig_idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
0f113f3e MC |
1614 | } |
1615 | return 1; | |
1616 | } | |
4817504d | 1617 | |
e7f8ff43 | 1618 | int SSL_get_sigalgs(SSL *s, int idx, |
0f113f3e MC |
1619 | int *psign, int *phash, int *psignhash, |
1620 | unsigned char *rsig, unsigned char *rhash) | |
1621 | { | |
98c792d1 | 1622 | uint16_t *psig = s->s3->tmp.peer_sigalgs; |
703bcee0 | 1623 | size_t numsigalgs = s->s3->tmp.peer_sigalgslen; |
348240c6 | 1624 | if (psig == NULL || numsigalgs > INT_MAX) |
0f113f3e MC |
1625 | return 0; |
1626 | if (idx >= 0) { | |
4d43ee28 DSH |
1627 | const SIGALG_LOOKUP *lu; |
1628 | ||
703bcee0 | 1629 | if (idx >= (int)numsigalgs) |
0f113f3e MC |
1630 | return 0; |
1631 | psig += idx; | |
4d43ee28 | 1632 | if (rhash != NULL) |
536199ec | 1633 | *rhash = (unsigned char)((*psig >> 8) & 0xff); |
4d43ee28 | 1634 | if (rsig != NULL) |
536199ec | 1635 | *rsig = (unsigned char)(*psig & 0xff); |
4d43ee28 DSH |
1636 | lu = tls1_lookup_sigalg(*psig); |
1637 | if (psign != NULL) | |
1638 | *psign = lu != NULL ? lu->sig : NID_undef; | |
1639 | if (phash != NULL) | |
1640 | *phash = lu != NULL ? lu->hash : NID_undef; | |
1641 | if (psignhash != NULL) | |
1642 | *psignhash = lu != NULL ? lu->sigandhash : NID_undef; | |
0f113f3e | 1643 | } |
348240c6 | 1644 | return (int)numsigalgs; |
0f113f3e | 1645 | } |
4453cd8c DSH |
1646 | |
1647 | int SSL_get_shared_sigalgs(SSL *s, int idx, | |
0f113f3e MC |
1648 | int *psign, int *phash, int *psignhash, |
1649 | unsigned char *rsig, unsigned char *rhash) | |
1650 | { | |
4d43ee28 DSH |
1651 | const SIGALG_LOOKUP *shsigalgs; |
1652 | if (s->cert->shared_sigalgs == NULL | |
6d047e06 | 1653 | || idx < 0 |
4d43ee28 DSH |
1654 | || idx >= (int)s->cert->shared_sigalgslen |
1655 | || s->cert->shared_sigalgslen > INT_MAX) | |
0f113f3e | 1656 | return 0; |
4d43ee28 DSH |
1657 | shsigalgs = s->cert->shared_sigalgs[idx]; |
1658 | if (phash != NULL) | |
1659 | *phash = shsigalgs->hash; | |
1660 | if (psign != NULL) | |
1661 | *psign = shsigalgs->sig; | |
1662 | if (psignhash != NULL) | |
1663 | *psignhash = shsigalgs->sigandhash; | |
1664 | if (rsig != NULL) | |
1665 | *rsig = (unsigned char)(shsigalgs->sigalg & 0xff); | |
1666 | if (rhash != NULL) | |
1667 | *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff); | |
348240c6 | 1668 | return (int)s->cert->shared_sigalgslen; |
0f113f3e MC |
1669 | } |
1670 | ||
787ebcaf DSH |
1671 | /* Maximum possible number of unique entries in sigalgs array */ |
1672 | #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2) | |
0f229cce | 1673 | |
0f113f3e MC |
1674 | typedef struct { |
1675 | size_t sigalgcnt; | |
787ebcaf | 1676 | int sigalgs[TLS_MAX_SIGALGCNT]; |
0f113f3e | 1677 | } sig_cb_st; |
0f229cce | 1678 | |
431f458d DSH |
1679 | static void get_sigorhash(int *psig, int *phash, const char *str) |
1680 | { | |
1681 | if (strcmp(str, "RSA") == 0) { | |
1682 | *psig = EVP_PKEY_RSA; | |
b2eb6998 DSH |
1683 | } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) { |
1684 | *psig = EVP_PKEY_RSA_PSS; | |
431f458d DSH |
1685 | } else if (strcmp(str, "DSA") == 0) { |
1686 | *psig = EVP_PKEY_DSA; | |
1687 | } else if (strcmp(str, "ECDSA") == 0) { | |
1688 | *psig = EVP_PKEY_EC; | |
1689 | } else { | |
1690 | *phash = OBJ_sn2nid(str); | |
1691 | if (*phash == NID_undef) | |
1692 | *phash = OBJ_ln2nid(str); | |
1693 | } | |
1694 | } | |
787ebcaf DSH |
1695 | /* Maximum length of a signature algorithm string component */ |
1696 | #define TLS_MAX_SIGSTRING_LEN 40 | |
431f458d | 1697 | |
0f229cce | 1698 | static int sig_cb(const char *elem, int len, void *arg) |
0f113f3e MC |
1699 | { |
1700 | sig_cb_st *sarg = arg; | |
1701 | size_t i; | |
787ebcaf | 1702 | char etmp[TLS_MAX_SIGSTRING_LEN], *p; |
431f458d | 1703 | int sig_alg = NID_undef, hash_alg = NID_undef; |
2747d73c KR |
1704 | if (elem == NULL) |
1705 | return 0; | |
787ebcaf | 1706 | if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT) |
0f113f3e MC |
1707 | return 0; |
1708 | if (len > (int)(sizeof(etmp) - 1)) | |
1709 | return 0; | |
1710 | memcpy(etmp, elem, len); | |
1711 | etmp[len] = 0; | |
1712 | p = strchr(etmp, '+'); | |
8a43a42a DSH |
1713 | /* See if we have a match for TLS 1.3 names */ |
1714 | if (p == NULL) { | |
1715 | const SIGALG_LOOKUP *s; | |
1716 | ||
1717 | for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl); | |
1718 | i++, s++) { | |
1719 | if (s->name != NULL && strcmp(etmp, s->name) == 0) { | |
1720 | sig_alg = s->sig; | |
1721 | hash_alg = s->hash; | |
1722 | break; | |
1723 | } | |
1724 | } | |
1725 | } else { | |
1726 | *p = 0; | |
1727 | p++; | |
1728 | if (*p == 0) | |
1729 | return 0; | |
1730 | get_sigorhash(&sig_alg, &hash_alg, etmp); | |
1731 | get_sigorhash(&sig_alg, &hash_alg, p); | |
1732 | } | |
0f113f3e | 1733 | |
431f458d | 1734 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
0f113f3e MC |
1735 | return 0; |
1736 | ||
1737 | for (i = 0; i < sarg->sigalgcnt; i += 2) { | |
1738 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) | |
1739 | return 0; | |
1740 | } | |
1741 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; | |
1742 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; | |
1743 | return 1; | |
1744 | } | |
1745 | ||
1746 | /* | |
9d22666e | 1747 | * Set supported signature algorithms based on a colon separated list of the |
0f113f3e MC |
1748 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
1749 | */ | |
3dbc46df | 1750 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
0f113f3e MC |
1751 | { |
1752 | sig_cb_st sig; | |
1753 | sig.sigalgcnt = 0; | |
1754 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) | |
1755 | return 0; | |
1756 | if (c == NULL) | |
1757 | return 1; | |
1758 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); | |
1759 | } | |
1760 | ||
a230b26e | 1761 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
0f113f3e | 1762 | { |
98c792d1 | 1763 | uint16_t *sigalgs, *sptr; |
0f113f3e | 1764 | size_t i; |
63c1df09 | 1765 | |
0f113f3e MC |
1766 | if (salglen & 1) |
1767 | return 0; | |
7a531ee4 | 1768 | sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs)); |
0f113f3e MC |
1769 | if (sigalgs == NULL) |
1770 | return 0; | |
1771 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { | |
63c1df09 | 1772 | size_t j; |
7a531ee4 | 1773 | const SIGALG_LOOKUP *curr; |
63c1df09 MC |
1774 | int md_id = *psig_nids++; |
1775 | int sig_id = *psig_nids++; | |
1776 | ||
1777 | for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl); | |
1778 | j++, curr++) { | |
fe3066ee | 1779 | if (curr->hash == md_id && curr->sig == sig_id) { |
63c1df09 MC |
1780 | *sptr++ = curr->sigalg; |
1781 | break; | |
1782 | } | |
1783 | } | |
0f113f3e | 1784 | |
63c1df09 | 1785 | if (j == OSSL_NELEM(sigalg_lookup_tbl)) |
0f113f3e | 1786 | goto err; |
0f113f3e MC |
1787 | } |
1788 | ||
1789 | if (client) { | |
b548a1f1 | 1790 | OPENSSL_free(c->client_sigalgs); |
0f113f3e | 1791 | c->client_sigalgs = sigalgs; |
7a531ee4 | 1792 | c->client_sigalgslen = salglen / 2; |
0f113f3e | 1793 | } else { |
b548a1f1 | 1794 | OPENSSL_free(c->conf_sigalgs); |
0f113f3e | 1795 | c->conf_sigalgs = sigalgs; |
7a531ee4 | 1796 | c->conf_sigalgslen = salglen / 2; |
0f113f3e MC |
1797 | } |
1798 | ||
1799 | return 1; | |
1800 | ||
1801 | err: | |
1802 | OPENSSL_free(sigalgs); | |
1803 | return 0; | |
1804 | } | |
4453cd8c | 1805 | |
d61ff83b | 1806 | static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) |
0f113f3e MC |
1807 | { |
1808 | int sig_nid; | |
1809 | size_t i; | |
1810 | if (default_nid == -1) | |
1811 | return 1; | |
1812 | sig_nid = X509_get_signature_nid(x); | |
1813 | if (default_nid) | |
1814 | return sig_nid == default_nid ? 1 : 0; | |
1815 | for (i = 0; i < c->shared_sigalgslen; i++) | |
4d43ee28 | 1816 | if (sig_nid == c->shared_sigalgs[i]->sigandhash) |
0f113f3e MC |
1817 | return 1; |
1818 | return 0; | |
1819 | } | |
1820 | ||
6dbb6219 DSH |
1821 | /* Check to see if a certificate issuer name matches list of CA names */ |
1822 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) | |
0f113f3e MC |
1823 | { |
1824 | X509_NAME *nm; | |
1825 | int i; | |
1826 | nm = X509_get_issuer_name(x); | |
1827 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
1828 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) | |
1829 | return 1; | |
1830 | } | |
1831 | return 0; | |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * Check certificate chain is consistent with TLS extensions and is usable by | |
1836 | * server. This servers two purposes: it allows users to check chains before | |
1837 | * passing them to the server and it allows the server to check chains before | |
1838 | * attempting to use them. | |
d61ff83b | 1839 | */ |
6dbb6219 DSH |
1840 | |
1841 | /* Flags which need to be set for a certificate when stict mode not set */ | |
1842 | ||
e481f9b9 | 1843 | #define CERT_PKEY_VALID_FLAGS \ |
0f113f3e | 1844 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
6dbb6219 | 1845 | /* Strict mode flags */ |
e481f9b9 | 1846 | #define CERT_PKEY_STRICT_FLAGS \ |
0f113f3e MC |
1847 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
1848 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) | |
6dbb6219 | 1849 | |
d61ff83b | 1850 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
0f113f3e MC |
1851 | int idx) |
1852 | { | |
1853 | int i; | |
1854 | int rv = 0; | |
1855 | int check_flags = 0, strict_mode; | |
1856 | CERT_PKEY *cpk = NULL; | |
1857 | CERT *c = s->cert; | |
f7d53487 | 1858 | uint32_t *pvalid; |
0f113f3e MC |
1859 | unsigned int suiteb_flags = tls1_suiteb(s); |
1860 | /* idx == -1 means checking server chains */ | |
1861 | if (idx != -1) { | |
1862 | /* idx == -2 means checking client certificate chains */ | |
1863 | if (idx == -2) { | |
1864 | cpk = c->key; | |
348240c6 | 1865 | idx = (int)(cpk - c->pkeys); |
0f113f3e MC |
1866 | } else |
1867 | cpk = c->pkeys + idx; | |
6383d316 | 1868 | pvalid = s->s3->tmp.valid_flags + idx; |
0f113f3e MC |
1869 | x = cpk->x509; |
1870 | pk = cpk->privatekey; | |
1871 | chain = cpk->chain; | |
1872 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; | |
1873 | /* If no cert or key, forget it */ | |
1874 | if (!x || !pk) | |
1875 | goto end; | |
0f113f3e MC |
1876 | } else { |
1877 | if (!x || !pk) | |
d813f9eb | 1878 | return 0; |
0f113f3e MC |
1879 | idx = ssl_cert_type(x, pk); |
1880 | if (idx == -1) | |
d813f9eb | 1881 | return 0; |
6383d316 DSH |
1882 | pvalid = s->s3->tmp.valid_flags + idx; |
1883 | ||
0f113f3e MC |
1884 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
1885 | check_flags = CERT_PKEY_STRICT_FLAGS; | |
1886 | else | |
1887 | check_flags = CERT_PKEY_VALID_FLAGS; | |
1888 | strict_mode = 1; | |
1889 | } | |
1890 | ||
1891 | if (suiteb_flags) { | |
1892 | int ok; | |
1893 | if (check_flags) | |
1894 | check_flags |= CERT_PKEY_SUITEB; | |
1895 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); | |
1896 | if (ok == X509_V_OK) | |
1897 | rv |= CERT_PKEY_SUITEB; | |
1898 | else if (!check_flags) | |
1899 | goto end; | |
1900 | } | |
1901 | ||
1902 | /* | |
1903 | * Check all signature algorithms are consistent with signature | |
1904 | * algorithms extension if TLS 1.2 or later and strict mode. | |
1905 | */ | |
1906 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { | |
1907 | int default_nid; | |
536199ec | 1908 | int rsign = 0; |
76106e60 | 1909 | if (s->s3->tmp.peer_sigalgs) |
0f113f3e MC |
1910 | default_nid = 0; |
1911 | /* If no sigalgs extension use defaults from RFC5246 */ | |
1912 | else { | |
1913 | switch (idx) { | |
d0ff28f8 | 1914 | case SSL_PKEY_RSA: |
536199ec | 1915 | rsign = EVP_PKEY_RSA; |
0f113f3e MC |
1916 | default_nid = NID_sha1WithRSAEncryption; |
1917 | break; | |
1918 | ||
1919 | case SSL_PKEY_DSA_SIGN: | |
536199ec | 1920 | rsign = EVP_PKEY_DSA; |
0f113f3e MC |
1921 | default_nid = NID_dsaWithSHA1; |
1922 | break; | |
1923 | ||
1924 | case SSL_PKEY_ECC: | |
536199ec | 1925 | rsign = EVP_PKEY_EC; |
0f113f3e MC |
1926 | default_nid = NID_ecdsa_with_SHA1; |
1927 | break; | |
1928 | ||
e44380a9 | 1929 | case SSL_PKEY_GOST01: |
536199ec | 1930 | rsign = NID_id_GostR3410_2001; |
e44380a9 DB |
1931 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; |
1932 | break; | |
1933 | ||
1934 | case SSL_PKEY_GOST12_256: | |
536199ec | 1935 | rsign = NID_id_GostR3410_2012_256; |
e44380a9 DB |
1936 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; |
1937 | break; | |
1938 | ||
1939 | case SSL_PKEY_GOST12_512: | |
536199ec | 1940 | rsign = NID_id_GostR3410_2012_512; |
e44380a9 DB |
1941 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; |
1942 | break; | |
1943 | ||
0f113f3e MC |
1944 | default: |
1945 | default_nid = -1; | |
1946 | break; | |
1947 | } | |
1948 | } | |
1949 | /* | |
1950 | * If peer sent no signature algorithms extension and we have set | |
1951 | * preferred signature algorithms check we support sha1. | |
1952 | */ | |
1953 | if (default_nid > 0 && c->conf_sigalgs) { | |
1954 | size_t j; | |
98c792d1 | 1955 | const uint16_t *p = c->conf_sigalgs; |
703bcee0 | 1956 | for (j = 0; j < c->conf_sigalgslen; j++, p++) { |
44b6318f DSH |
1957 | const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p); |
1958 | ||
1959 | if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign) | |
0f113f3e MC |
1960 | break; |
1961 | } | |
1962 | if (j == c->conf_sigalgslen) { | |
1963 | if (check_flags) | |
1964 | goto skip_sigs; | |
1965 | else | |
1966 | goto end; | |
1967 | } | |
1968 | } | |
1969 | /* Check signature algorithm of each cert in chain */ | |
1970 | if (!tls1_check_sig_alg(c, x, default_nid)) { | |
1971 | if (!check_flags) | |
1972 | goto end; | |
1973 | } else | |
1974 | rv |= CERT_PKEY_EE_SIGNATURE; | |
1975 | rv |= CERT_PKEY_CA_SIGNATURE; | |
1976 | for (i = 0; i < sk_X509_num(chain); i++) { | |
1977 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { | |
1978 | if (check_flags) { | |
1979 | rv &= ~CERT_PKEY_CA_SIGNATURE; | |
1980 | break; | |
1981 | } else | |
1982 | goto end; | |
1983 | } | |
1984 | } | |
1985 | } | |
1986 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ | |
1987 | else if (check_flags) | |
1988 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; | |
1989 | skip_sigs: | |
1990 | /* Check cert parameters are consistent */ | |
9195ddcd | 1991 | if (tls1_check_cert_param(s, x, 1)) |
0f113f3e MC |
1992 | rv |= CERT_PKEY_EE_PARAM; |
1993 | else if (!check_flags) | |
1994 | goto end; | |
1995 | if (!s->server) | |
1996 | rv |= CERT_PKEY_CA_PARAM; | |
1997 | /* In strict mode check rest of chain too */ | |
1998 | else if (strict_mode) { | |
1999 | rv |= CERT_PKEY_CA_PARAM; | |
2000 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2001 | X509 *ca = sk_X509_value(chain, i); | |
2002 | if (!tls1_check_cert_param(s, ca, 0)) { | |
2003 | if (check_flags) { | |
2004 | rv &= ~CERT_PKEY_CA_PARAM; | |
2005 | break; | |
2006 | } else | |
2007 | goto end; | |
2008 | } | |
2009 | } | |
2010 | } | |
2011 | if (!s->server && strict_mode) { | |
2012 | STACK_OF(X509_NAME) *ca_dn; | |
2013 | int check_type = 0; | |
3aeb9348 | 2014 | switch (EVP_PKEY_id(pk)) { |
0f113f3e MC |
2015 | case EVP_PKEY_RSA: |
2016 | check_type = TLS_CT_RSA_SIGN; | |
2017 | break; | |
2018 | case EVP_PKEY_DSA: | |
2019 | check_type = TLS_CT_DSS_SIGN; | |
2020 | break; | |
2021 | case EVP_PKEY_EC: | |
2022 | check_type = TLS_CT_ECDSA_SIGN; | |
2023 | break; | |
0f113f3e MC |
2024 | } |
2025 | if (check_type) { | |
75c13e78 DSH |
2026 | const uint8_t *ctypes = s->s3->tmp.ctype; |
2027 | size_t j; | |
2028 | ||
2029 | for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) { | |
2030 | if (*ctypes == check_type) { | |
0f113f3e MC |
2031 | rv |= CERT_PKEY_CERT_TYPE; |
2032 | break; | |
2033 | } | |
2034 | } | |
2035 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) | |
2036 | goto end; | |
75c13e78 | 2037 | } else { |
0f113f3e | 2038 | rv |= CERT_PKEY_CERT_TYPE; |
75c13e78 | 2039 | } |
0f113f3e MC |
2040 | |
2041 | ca_dn = s->s3->tmp.ca_names; | |
2042 | ||
2043 | if (!sk_X509_NAME_num(ca_dn)) | |
2044 | rv |= CERT_PKEY_ISSUER_NAME; | |
2045 | ||
2046 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
2047 | if (ssl_check_ca_name(ca_dn, x)) | |
2048 | rv |= CERT_PKEY_ISSUER_NAME; | |
2049 | } | |
2050 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
2051 | for (i = 0; i < sk_X509_num(chain); i++) { | |
2052 | X509 *xtmp = sk_X509_value(chain, i); | |
2053 | if (ssl_check_ca_name(ca_dn, xtmp)) { | |
2054 | rv |= CERT_PKEY_ISSUER_NAME; | |
2055 | break; | |
2056 | } | |
2057 | } | |
2058 | } | |
2059 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) | |
2060 | goto end; | |
2061 | } else | |
2062 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; | |
2063 | ||
2064 | if (!check_flags || (rv & check_flags) == check_flags) | |
2065 | rv |= CERT_PKEY_VALID; | |
2066 | ||
2067 | end: | |
2068 | ||
a8bb912d DSH |
2069 | if (TLS1_get_version(s) >= TLS1_2_VERSION) |
2070 | rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN); | |
2071 | else | |
0f113f3e MC |
2072 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; |
2073 | ||
2074 | /* | |
2075 | * When checking a CERT_PKEY structure all flags are irrelevant if the | |
2076 | * chain is invalid. | |
2077 | */ | |
2078 | if (!check_flags) { | |
a8bb912d | 2079 | if (rv & CERT_PKEY_VALID) { |
6383d316 | 2080 | *pvalid = rv; |
a8bb912d DSH |
2081 | } else { |
2082 | /* Preserve sign and explicit sign flag, clear rest */ | |
2083 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; | |
0f113f3e MC |
2084 | return 0; |
2085 | } | |
2086 | } | |
2087 | return rv; | |
2088 | } | |
d61ff83b DSH |
2089 | |
2090 | /* Set validity of certificates in an SSL structure */ | |
2091 | void tls1_set_cert_validity(SSL *s) | |
0f113f3e | 2092 | { |
d0ff28f8 | 2093 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA); |
17dd65e6 | 2094 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); |
17dd65e6 | 2095 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
e44380a9 DB |
2096 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
2097 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); | |
2098 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); | |
0f113f3e MC |
2099 | } |
2100 | ||
18d71588 DSH |
2101 | /* User level utiity function to check a chain is suitable */ |
2102 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) | |
0f113f3e MC |
2103 | { |
2104 | return tls1_check_chain(s, x, pk, chain, -1); | |
2105 | } | |
d61ff83b | 2106 | |
09599b52 DSH |
2107 | #ifndef OPENSSL_NO_DH |
2108 | DH *ssl_get_auto_dh(SSL *s) | |
0f113f3e MC |
2109 | { |
2110 | int dh_secbits = 80; | |
2111 | if (s->cert->dh_tmp_auto == 2) | |
2112 | return DH_get_1024_160(); | |
adc5506a | 2113 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
0f113f3e MC |
2114 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
2115 | dh_secbits = 128; | |
2116 | else | |
2117 | dh_secbits = 80; | |
2118 | } else { | |
a497cf25 | 2119 | if (s->s3->tmp.cert == NULL) |
f365a3e2 | 2120 | return NULL; |
a497cf25 | 2121 | dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey); |
0f113f3e MC |
2122 | } |
2123 | ||
2124 | if (dh_secbits >= 128) { | |
2125 | DH *dhp = DH_new(); | |
0aeddcfa | 2126 | BIGNUM *p, *g; |
a71edf3b | 2127 | if (dhp == NULL) |
0f113f3e | 2128 | return NULL; |
0aeddcfa MC |
2129 | g = BN_new(); |
2130 | if (g != NULL) | |
2131 | BN_set_word(g, 2); | |
0f113f3e | 2132 | if (dh_secbits >= 192) |
9021a5df | 2133 | p = BN_get_rfc3526_prime_8192(NULL); |
0f113f3e | 2134 | else |
9021a5df | 2135 | p = BN_get_rfc3526_prime_3072(NULL); |
0aeddcfa | 2136 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
0f113f3e | 2137 | DH_free(dhp); |
0aeddcfa MC |
2138 | BN_free(p); |
2139 | BN_free(g); | |
0f113f3e MC |
2140 | return NULL; |
2141 | } | |
2142 | return dhp; | |
2143 | } | |
2144 | if (dh_secbits >= 112) | |
2145 | return DH_get_2048_224(); | |
2146 | return DH_get_1024_160(); | |
2147 | } | |
09599b52 | 2148 | #endif |
b362ccab DSH |
2149 | |
2150 | static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e | 2151 | { |
72245f34 | 2152 | int secbits = -1; |
8382fd3a | 2153 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
0f113f3e | 2154 | if (pkey) { |
72245f34 DSH |
2155 | /* |
2156 | * If no parameters this will return -1 and fail using the default | |
2157 | * security callback for any non-zero security level. This will | |
2158 | * reject keys which omit parameters but this only affects DSA and | |
2159 | * omission of parameters is never (?) done in practice. | |
2160 | */ | |
0f113f3e | 2161 | secbits = EVP_PKEY_security_bits(pkey); |
72245f34 | 2162 | } |
0f113f3e MC |
2163 | if (s) |
2164 | return ssl_security(s, op, secbits, 0, x); | |
2165 | else | |
2166 | return ssl_ctx_security(ctx, op, secbits, 0, x); | |
2167 | } | |
b362ccab DSH |
2168 | |
2169 | static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e MC |
2170 | { |
2171 | /* Lookup signature algorithm digest */ | |
2172 | int secbits = -1, md_nid = NID_undef, sig_nid; | |
221c7b55 DSH |
2173 | /* Don't check signature if self signed */ |
2174 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) | |
2175 | return 1; | |
0f113f3e MC |
2176 | sig_nid = X509_get_signature_nid(x); |
2177 | if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) { | |
2178 | const EVP_MD *md; | |
2179 | if (md_nid && (md = EVP_get_digestbynid(md_nid))) | |
2180 | secbits = EVP_MD_size(md) * 4; | |
2181 | } | |
2182 | if (s) | |
2183 | return ssl_security(s, op, secbits, md_nid, x); | |
2184 | else | |
2185 | return ssl_ctx_security(ctx, op, secbits, md_nid, x); | |
2186 | } | |
b362ccab DSH |
2187 | |
2188 | int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) | |
0f113f3e MC |
2189 | { |
2190 | if (vfy) | |
2191 | vfy = SSL_SECOP_PEER; | |
2192 | if (is_ee) { | |
2193 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) | |
2194 | return SSL_R_EE_KEY_TOO_SMALL; | |
2195 | } else { | |
2196 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) | |
2197 | return SSL_R_CA_KEY_TOO_SMALL; | |
2198 | } | |
2199 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) | |
2200 | return SSL_R_CA_MD_TOO_WEAK; | |
2201 | return 1; | |
2202 | } | |
2203 | ||
2204 | /* | |
2205 | * Check security of a chain, if sk includes the end entity certificate then | |
2206 | * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending | |
2207 | * one to the peer. Return values: 1 if ok otherwise error code to use | |
b362ccab DSH |
2208 | */ |
2209 | ||
2210 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) | |
0f113f3e MC |
2211 | { |
2212 | int rv, start_idx, i; | |
2213 | if (x == NULL) { | |
2214 | x = sk_X509_value(sk, 0); | |
2215 | start_idx = 1; | |
2216 | } else | |
2217 | start_idx = 0; | |
2218 | ||
2219 | rv = ssl_security_cert(s, NULL, x, vfy, 1); | |
2220 | if (rv != 1) | |
2221 | return rv; | |
2222 | ||
2223 | for (i = start_idx; i < sk_X509_num(sk); i++) { | |
2224 | x = sk_X509_value(sk, i); | |
2225 | rv = ssl_security_cert(s, NULL, x, vfy, 0); | |
2226 | if (rv != 1) | |
2227 | return rv; | |
2228 | } | |
2229 | return 1; | |
2230 | } | |
93a77f9e DSH |
2231 | |
2232 | /* | |
2233 | * Choose an appropriate signature algorithm based on available certificates | |
717a265a DSH |
2234 | * Sets chosen certificate and signature algorithm. |
2235 | * | |
2236 | * For servers if we fail to find a required certificate it is a fatal error | |
2237 | * and an appropriate error code is set and the TLS alert set in *al. | |
2238 | * | |
2239 | * For clients al is set to NULL. If a certificate is not suitable it is not | |
2240 | * a fatal error: we will either try another certificate or not present one | |
2241 | * to the server. In this case no error is set. | |
93a77f9e | 2242 | */ |
4a419f60 | 2243 | int tls_choose_sigalg(SSL *s, int *al) |
93a77f9e | 2244 | { |
7b3a4d61 | 2245 | int idx = -1; |
0972bc5c DSH |
2246 | const SIGALG_LOOKUP *lu = NULL; |
2247 | ||
717a265a DSH |
2248 | s->s3->tmp.cert = NULL; |
2249 | s->s3->tmp.sigalg = NULL; | |
2250 | ||
93a77f9e DSH |
2251 | if (SSL_IS_TLS13(s)) { |
2252 | size_t i; | |
21f198ec | 2253 | #ifndef OPENSSL_NO_EC |
e892e325 | 2254 | int curve = -1, skip_ec = 0; |
21f198ec | 2255 | #endif |
93a77f9e DSH |
2256 | |
2257 | /* Look for a certificate matching shared sigaglgs */ | |
2258 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { | |
0972bc5c | 2259 | lu = s->cert->shared_sigalgs[i]; |
93a77f9e | 2260 | |
095a982b DSH |
2261 | /* Skip SHA1, DSA and RSA if not PSS */ |
2262 | if (lu->hash == NID_sha1 || lu->sig == EVP_PKEY_DSA | |
2263 | || lu->sig == EVP_PKEY_RSA) | |
93a77f9e | 2264 | continue; |
0972bc5c | 2265 | if (ssl_md(lu->hash_idx) == NULL) |
93a77f9e DSH |
2266 | continue; |
2267 | idx = lu->sig_idx; | |
0972bc5c | 2268 | if (!ssl_has_cert(s, idx)) |
93a77f9e | 2269 | continue; |
93a77f9e | 2270 | if (lu->sig == EVP_PKEY_EC) { |
21f198ec | 2271 | #ifndef OPENSSL_NO_EC |
93a77f9e | 2272 | if (curve == -1) { |
0972bc5c | 2273 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey); |
93a77f9e DSH |
2274 | |
2275 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); | |
e892e325 DSH |
2276 | if (EC_KEY_get_conv_form(ec) |
2277 | != POINT_CONVERSION_UNCOMPRESSED) | |
2278 | skip_ec = 1; | |
93a77f9e | 2279 | } |
e892e325 | 2280 | if (skip_ec || (lu->curve != NID_undef && curve != lu->curve)) |
93a77f9e | 2281 | continue; |
21f198ec RL |
2282 | #else |
2283 | continue; | |
2284 | #endif | |
93a77f9e | 2285 | } |
0972bc5c DSH |
2286 | break; |
2287 | } | |
2288 | if (i == s->cert->shared_sigalgslen) { | |
717a265a DSH |
2289 | if (al == NULL) |
2290 | return 1; | |
0972bc5c DSH |
2291 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2292 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, | |
2293 | SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM); | |
2294 | return 0; | |
2295 | } | |
2296 | } else { | |
717a265a DSH |
2297 | if (s->server) { |
2298 | /* Find index corresponding to ciphersuite */ | |
2299 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); | |
2300 | /* If no certificate for ciphersuite return */ | |
2301 | if (idx == -1) | |
2302 | return 1; | |
2303 | if (idx == SSL_PKEY_GOST_EC) { | |
2304 | /* Work out which GOST certificate is avaiable */ | |
2305 | if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) { | |
2306 | idx = SSL_PKEY_GOST12_512; | |
2307 | } else if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) { | |
2308 | idx = SSL_PKEY_GOST12_256; | |
2309 | } else if (ssl_has_cert(s, SSL_PKEY_GOST01)) { | |
2310 | idx = SSL_PKEY_GOST01; | |
2311 | } else { | |
2312 | if (al == NULL) | |
2313 | return 1; | |
2314 | *al = SSL_AD_INTERNAL_ERROR; | |
2315 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2316 | return 0; | |
2317 | } | |
2318 | } else if (!ssl_has_cert(s, idx)) { | |
2319 | if (al == NULL) | |
2320 | return 1; | |
0972bc5c DSH |
2321 | *al = SSL_AD_INTERNAL_ERROR; |
2322 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2323 | return 0; | |
2324 | } | |
717a265a DSH |
2325 | } else { |
2326 | /* Find index for client certificate */ | |
2327 | idx = s->cert->key - s->cert->pkeys; | |
2328 | if (!ssl_has_cert(s, idx)) | |
2329 | return 1; | |
0972bc5c DSH |
2330 | } |
2331 | ||
2332 | if (SSL_USE_SIGALGS(s)) { | |
2333 | if (s->s3->tmp.peer_sigalgs != NULL) { | |
2334 | size_t i; | |
599b586d DSH |
2335 | #ifndef OPENSSL_NO_EC |
2336 | int curve; | |
2337 | ||
2338 | /* For Suite B need to match signature algorithm to curve */ | |
2339 | if (tls1_suiteb(s)) { | |
2340 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[idx].privatekey); | |
2341 | curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); | |
2342 | } else { | |
2343 | curve = -1; | |
2344 | } | |
2345 | #endif | |
0972bc5c DSH |
2346 | |
2347 | /* | |
2348 | * Find highest preference signature algorithm matching | |
2349 | * cert type | |
2350 | */ | |
2351 | for (i = 0; i < s->cert->shared_sigalgslen; i++) { | |
2352 | lu = s->cert->shared_sigalgs[i]; | |
599b586d | 2353 | #ifdef OPENSSL_NO_EC |
0972bc5c DSH |
2354 | if (lu->sig_idx == idx) |
2355 | break; | |
599b586d DSH |
2356 | #else |
2357 | if (lu->sig_idx == idx | |
2358 | && (curve == -1 || lu->curve == curve)) | |
2359 | break; | |
2360 | #endif | |
0972bc5c DSH |
2361 | if (idx == SSL_PKEY_RSA && lu->sig == EVP_PKEY_RSA_PSS) |
2362 | break; | |
2363 | } | |
2364 | if (i == s->cert->shared_sigalgslen) { | |
717a265a DSH |
2365 | if (al == NULL) |
2366 | return 1; | |
0972bc5c DSH |
2367 | *al = SSL_AD_INTERNAL_ERROR; |
2368 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2369 | return 0; | |
2370 | } | |
2371 | } else { | |
2372 | /* | |
2373 | * If we have no sigalg use defaults | |
2374 | */ | |
2375 | const uint16_t *sent_sigs; | |
2376 | size_t sent_sigslen, i; | |
2377 | ||
2378 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { | |
717a265a DSH |
2379 | if (al == NULL) |
2380 | return 1; | |
0972bc5c DSH |
2381 | *al = SSL_AD_INTERNAL_ERROR; |
2382 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2383 | return 0; | |
2384 | } | |
2385 | ||
2386 | /* Check signature matches a type we sent */ | |
2387 | sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs); | |
2388 | for (i = 0; i < sent_sigslen; i++, sent_sigs++) { | |
2389 | if (lu->sigalg == *sent_sigs) | |
2390 | break; | |
2391 | } | |
2392 | if (i == sent_sigslen) { | |
717a265a DSH |
2393 | if (al == NULL) |
2394 | return 1; | |
0972bc5c DSH |
2395 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
2396 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2397 | return 0; | |
2398 | } | |
2399 | } | |
2400 | } else { | |
2401 | if ((lu = tls1_get_legacy_sigalg(s, idx)) == NULL) { | |
717a265a DSH |
2402 | if (al == NULL) |
2403 | return 1; | |
0972bc5c DSH |
2404 | *al = SSL_AD_INTERNAL_ERROR; |
2405 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2406 | return 0; | |
2407 | } | |
2408 | } | |
93a77f9e | 2409 | } |
7b3a4d61 | 2410 | if (idx == -1) { |
717a265a DSH |
2411 | if (al != NULL) { |
2412 | *al = SSL_AD_INTERNAL_ERROR; | |
2413 | SSLerr(SSL_F_TLS_CHOOSE_SIGALG, ERR_R_INTERNAL_ERROR); | |
2414 | } | |
7b3a4d61 DSH |
2415 | return 0; |
2416 | } | |
a497cf25 | 2417 | s->s3->tmp.cert = &s->cert->pkeys[idx]; |
59088e43 | 2418 | s->cert->key = s->s3->tmp.cert; |
0972bc5c | 2419 | s->s3->tmp.sigalg = lu; |
93a77f9e DSH |
2420 | return 1; |
2421 | } |