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