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