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