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