]>
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); |
2daceb03 | 26 | static int ssl_check_clienthello_tlsext_early(SSL *s); |
aa474d1f | 27 | static int ssl_check_serverhello_tlsext(SSL *s); |
6434abbf | 28 | |
0f113f3e MC |
29 | SSL3_ENC_METHOD const TLSv1_enc_data = { |
30 | tls1_enc, | |
31 | tls1_mac, | |
32 | tls1_setup_key_block, | |
33 | tls1_generate_master_secret, | |
34 | tls1_change_cipher_state, | |
35 | tls1_final_finish_mac, | |
0f113f3e MC |
36 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
37 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
38 | tls1_alert_code, | |
39 | tls1_export_keying_material, | |
40 | 0, | |
a29fa98c | 41 | ssl3_set_handshake_header, |
2c7b4dbc | 42 | tls_close_construct_packet, |
0f113f3e MC |
43 | ssl3_handshake_write |
44 | }; | |
45 | ||
46 | SSL3_ENC_METHOD const TLSv1_1_enc_data = { | |
47 | tls1_enc, | |
48 | tls1_mac, | |
49 | tls1_setup_key_block, | |
50 | tls1_generate_master_secret, | |
51 | tls1_change_cipher_state, | |
52 | tls1_final_finish_mac, | |
0f113f3e MC |
53 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
54 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
55 | tls1_alert_code, | |
56 | tls1_export_keying_material, | |
57 | SSL_ENC_FLAG_EXPLICIT_IV, | |
a29fa98c | 58 | ssl3_set_handshake_header, |
2c7b4dbc | 59 | tls_close_construct_packet, |
0f113f3e MC |
60 | ssl3_handshake_write |
61 | }; | |
62 | ||
63 | SSL3_ENC_METHOD const TLSv1_2_enc_data = { | |
64 | tls1_enc, | |
65 | tls1_mac, | |
66 | tls1_setup_key_block, | |
67 | tls1_generate_master_secret, | |
68 | tls1_change_cipher_state, | |
69 | tls1_final_finish_mac, | |
0f113f3e MC |
70 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
71 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
72 | tls1_alert_code, | |
73 | tls1_export_keying_material, | |
74 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF | |
75 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, | |
a29fa98c | 76 | ssl3_set_handshake_header, |
2c7b4dbc | 77 | tls_close_construct_packet, |
0f113f3e MC |
78 | ssl3_handshake_write |
79 | }; | |
58964a49 | 80 | |
582a17d6 MC |
81 | SSL3_ENC_METHOD const TLSv1_3_enc_data = { |
82 | tls1_enc, | |
83 | tls1_mac, | |
92760c21 MC |
84 | tls13_setup_key_block, |
85 | tls13_generate_master_secret, | |
86 | tls13_change_cipher_state, | |
87 | tls13_final_finish_mac, | |
582a17d6 MC |
88 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
89 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | |
90 | tls1_alert_code, | |
91 | tls1_export_keying_material, | |
92 | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF | |
93 | | SSL_ENC_FLAG_TLS1_2_CIPHERS, | |
582a17d6 MC |
94 | ssl3_set_handshake_header, |
95 | tls_close_construct_packet, | |
96 | ssl3_handshake_write | |
97 | }; | |
98 | ||
f3b656b2 | 99 | long tls1_default_timeout(void) |
0f113f3e MC |
100 | { |
101 | /* | |
102 | * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for | |
103 | * http, the cache would over fill | |
104 | */ | |
105 | return (60 * 60 * 2); | |
106 | } | |
58964a49 | 107 | |
6b691a5c | 108 | int tls1_new(SSL *s) |
0f113f3e MC |
109 | { |
110 | if (!ssl3_new(s)) | |
111 | return (0); | |
112 | s->method->ssl_clear(s); | |
113 | return (1); | |
114 | } | |
58964a49 | 115 | |
6b691a5c | 116 | void tls1_free(SSL *s) |
0f113f3e | 117 | { |
b548a1f1 | 118 | OPENSSL_free(s->tlsext_session_ticket); |
0f113f3e MC |
119 | ssl3_free(s); |
120 | } | |
58964a49 | 121 | |
6b691a5c | 122 | void tls1_clear(SSL *s) |
0f113f3e MC |
123 | { |
124 | ssl3_clear(s); | |
4fa52141 VD |
125 | if (s->method->version == TLS_ANY_VERSION) |
126 | s->version = TLS_MAX_VERSION; | |
127 | else | |
128 | s->version = s->method->version; | |
0f113f3e | 129 | } |
58964a49 | 130 | |
525de5d3 | 131 | #ifndef OPENSSL_NO_EC |
eda3766b | 132 | |
0f113f3e MC |
133 | typedef struct { |
134 | int nid; /* Curve NID */ | |
135 | int secbits; /* Bits of security (from SP800-57) */ | |
136 | unsigned int flags; /* Flags: currently just field type */ | |
137 | } tls_curve_info; | |
138 | ||
2dc1aeed DSH |
139 | /* |
140 | * Table of curve information. | |
ddb4c047 | 141 | * Do not delete entries or reorder this array! It is used as a lookup |
2dc1aeed DSH |
142 | * table: the index of each entry is one less than the TLS curve id. |
143 | */ | |
0f113f3e MC |
144 | static const tls_curve_info nid_list[] = { |
145 | {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */ | |
146 | {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */ | |
147 | {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */ | |
148 | {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */ | |
149 | {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */ | |
150 | {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */ | |
151 | {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */ | |
152 | {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */ | |
153 | {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */ | |
154 | {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */ | |
155 | {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */ | |
156 | {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */ | |
157 | {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */ | |
158 | {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */ | |
159 | {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */ | |
160 | {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */ | |
161 | {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */ | |
162 | {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */ | |
163 | {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */ | |
164 | {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */ | |
165 | {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */ | |
166 | {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */ | |
167 | {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */ | |
168 | {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */ | |
169 | {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */ | |
170 | {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */ | |
171 | {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */ | |
172 | {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */ | |
ec24630a | 173 | {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */ |
0f113f3e MC |
174 | }; |
175 | ||
176 | static const unsigned char ecformats_default[] = { | |
177 | TLSEXT_ECPOINTFORMAT_uncompressed, | |
178 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, | |
179 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 | |
180 | }; | |
181 | ||
fe6ef247 KR |
182 | /* The default curves */ |
183 | static const unsigned char eccurves_default[] = { | |
1db3107a | 184 | 0, 29, /* X25519 (29) */ |
de57d237 | 185 | 0, 23, /* secp256r1 (23) */ |
0f113f3e | 186 | 0, 25, /* secp521r1 (25) */ |
0f113f3e | 187 | 0, 24, /* secp384r1 (24) */ |
de57d237 EK |
188 | }; |
189 | ||
190 | static const unsigned char eccurves_all[] = { | |
1db3107a | 191 | 0, 29, /* X25519 (29) */ |
de57d237 | 192 | 0, 23, /* secp256r1 (23) */ |
de57d237 | 193 | 0, 25, /* secp521r1 (25) */ |
de57d237 | 194 | 0, 24, /* secp384r1 (24) */ |
0f113f3e | 195 | 0, 26, /* brainpoolP256r1 (26) */ |
ddb4c047 RS |
196 | 0, 27, /* brainpoolP384r1 (27) */ |
197 | 0, 28, /* brainpool512r1 (28) */ | |
198 | ||
199 | /* | |
200 | * Remaining curves disabled by default but still permitted if set | |
201 | * via an explicit callback or parameters. | |
202 | */ | |
0f113f3e | 203 | 0, 22, /* secp256k1 (22) */ |
de57d237 EK |
204 | 0, 14, /* sect571r1 (14) */ |
205 | 0, 13, /* sect571k1 (13) */ | |
206 | 0, 11, /* sect409k1 (11) */ | |
207 | 0, 12, /* sect409r1 (12) */ | |
208 | 0, 9, /* sect283k1 (9) */ | |
209 | 0, 10, /* sect283r1 (10) */ | |
de57d237 EK |
210 | 0, 20, /* secp224k1 (20) */ |
211 | 0, 21, /* secp224r1 (21) */ | |
212 | 0, 18, /* secp192k1 (18) */ | |
213 | 0, 19, /* secp192r1 (19) */ | |
214 | 0, 15, /* secp160k1 (15) */ | |
215 | 0, 16, /* secp160r1 (16) */ | |
216 | 0, 17, /* secp160r2 (17) */ | |
0f113f3e MC |
217 | 0, 8, /* sect239k1 (8) */ |
218 | 0, 6, /* sect233k1 (6) */ | |
219 | 0, 7, /* sect233r1 (7) */ | |
0f113f3e MC |
220 | 0, 4, /* sect193r1 (4) */ |
221 | 0, 5, /* sect193r2 (5) */ | |
0f113f3e MC |
222 | 0, 1, /* sect163k1 (1) */ |
223 | 0, 2, /* sect163r1 (2) */ | |
224 | 0, 3, /* sect163r2 (3) */ | |
0f113f3e MC |
225 | }; |
226 | ||
227 | static const unsigned char suiteb_curves[] = { | |
228 | 0, TLSEXT_curve_P_256, | |
229 | 0, TLSEXT_curve_P_384 | |
230 | }; | |
2ea80354 | 231 | |
ec24630a | 232 | int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags) |
0f113f3e | 233 | { |
ec24630a | 234 | const tls_curve_info *cinfo; |
0f113f3e | 235 | /* ECC curves from RFC 4492 and RFC 7027 */ |
b6eb9827 | 236 | if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list))) |
0f113f3e | 237 | return 0; |
ec24630a DSH |
238 | cinfo = nid_list + curve_id - 1; |
239 | if (pflags) | |
240 | *pflags = cinfo->flags; | |
241 | return cinfo->nid; | |
0f113f3e | 242 | } |
525de5d3 DSH |
243 | |
244 | int tls1_ec_nid2curve_id(int nid) | |
0f113f3e | 245 | { |
2fa2d15a DSH |
246 | size_t i; |
247 | for (i = 0; i < OSSL_NELEM(nid_list); i++) { | |
248 | if (nid_list[i].nid == nid) | |
348240c6 | 249 | return (int)(i + 1); |
0f113f3e | 250 | } |
2fa2d15a | 251 | return 0; |
0f113f3e MC |
252 | } |
253 | ||
740580c2 EK |
254 | /* |
255 | * Get curves list, if "sess" is set return client curves otherwise | |
256 | * preferred list. | |
257 | * Sets |num_curves| to the number of curves in the list, i.e., | |
258 | * the length of |pcurves| is 2 * num_curves. | |
259 | * Returns 1 on success and 0 if the client curves list has invalid format. | |
260 | * The latter indicates an internal error: we should not be accepting such | |
261 | * lists in the first place. | |
262 | * TODO(emilia): we should really be storing the curves list in explicitly | |
263 | * parsed form instead. (However, this would affect binary compatibility | |
264 | * so cannot happen in the 1.0.x series.) | |
fd2b65ce | 265 | */ |
740580c2 | 266 | static int tls1_get_curvelist(SSL *s, int sess, |
a230b26e | 267 | const unsigned char **pcurves, size_t *num_curves) |
0f113f3e MC |
268 | { |
269 | size_t pcurveslen = 0; | |
270 | if (sess) { | |
de4d764e MC |
271 | *pcurves = s->session->tlsext_supportedgroupslist; |
272 | pcurveslen = s->session->tlsext_supportedgroupslist_length; | |
0f113f3e MC |
273 | } else { |
274 | /* For Suite B mode only include P-256, P-384 */ | |
275 | switch (tls1_suiteb(s)) { | |
276 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
277 | *pcurves = suiteb_curves; | |
278 | pcurveslen = sizeof(suiteb_curves); | |
279 | break; | |
280 | ||
281 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
282 | *pcurves = suiteb_curves; | |
283 | pcurveslen = 2; | |
284 | break; | |
285 | ||
286 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
287 | *pcurves = suiteb_curves + 2; | |
288 | pcurveslen = 2; | |
289 | break; | |
290 | default: | |
de4d764e MC |
291 | *pcurves = s->tlsext_supportedgroupslist; |
292 | pcurveslen = s->tlsext_supportedgroupslist_length; | |
0f113f3e MC |
293 | } |
294 | if (!*pcurves) { | |
fe6ef247 KR |
295 | *pcurves = eccurves_default; |
296 | pcurveslen = sizeof(eccurves_default); | |
0f113f3e MC |
297 | } |
298 | } | |
299 | ||
300 | /* We do not allow odd length arrays to enter the system. */ | |
301 | if (pcurveslen & 1) { | |
302 | SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR); | |
303 | *num_curves = 0; | |
304 | return 0; | |
305 | } else { | |
306 | *num_curves = pcurveslen / 2; | |
307 | return 1; | |
308 | } | |
309 | } | |
b362ccab DSH |
310 | |
311 | /* See if curve is allowed by security callback */ | |
312 | static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) | |
0f113f3e MC |
313 | { |
314 | const tls_curve_info *cinfo; | |
315 | if (curve[0]) | |
316 | return 1; | |
b6eb9827 | 317 | if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) |
0f113f3e MC |
318 | return 0; |
319 | cinfo = &nid_list[curve[1] - 1]; | |
320 | # ifdef OPENSSL_NO_EC2M | |
321 | if (cinfo->flags & TLS_CURVE_CHAR2) | |
322 | return 0; | |
323 | # endif | |
324 | return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); | |
325 | } | |
b362ccab | 326 | |
d18b716d DSH |
327 | /* Check a curve is one of our preferences */ |
328 | int tls1_check_curve(SSL *s, const unsigned char *p, size_t len) | |
0f113f3e MC |
329 | { |
330 | const unsigned char *curves; | |
331 | size_t num_curves, i; | |
332 | unsigned int suiteb_flags = tls1_suiteb(s); | |
333 | if (len != 3 || p[0] != NAMED_CURVE_TYPE) | |
334 | return 0; | |
335 | /* Check curve matches Suite B preferences */ | |
336 | if (suiteb_flags) { | |
337 | unsigned long cid = s->s3->tmp.new_cipher->id; | |
338 | if (p[1]) | |
339 | return 0; | |
340 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) { | |
341 | if (p[2] != TLSEXT_curve_P_256) | |
342 | return 0; | |
343 | } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) { | |
344 | if (p[2] != TLSEXT_curve_P_384) | |
345 | return 0; | |
346 | } else /* Should never happen */ | |
347 | return 0; | |
348 | } | |
349 | if (!tls1_get_curvelist(s, 0, &curves, &num_curves)) | |
350 | return 0; | |
351 | for (i = 0; i < num_curves; i++, curves += 2) { | |
352 | if (p[1] == curves[0] && p[2] == curves[1]) | |
353 | return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK); | |
354 | } | |
355 | return 0; | |
356 | } | |
d0595f17 | 357 | |
1d97c843 | 358 | /*- |
de4d764e | 359 | * For nmatch >= 0, return the NID of the |nmatch|th shared group or NID_undef |
6977e8ee KR |
360 | * if there is no match. |
361 | * For nmatch == -1, return number of matches | |
de4d764e | 362 | * For nmatch == -2, return the NID of the group to use for |
376e2ca3 | 363 | * an EC tmp key, or NID_undef if there is no match. |
d0595f17 | 364 | */ |
de4d764e | 365 | int tls1_shared_group(SSL *s, int nmatch) |
0f113f3e MC |
366 | { |
367 | const unsigned char *pref, *supp; | |
368 | size_t num_pref, num_supp, i, j; | |
369 | int k; | |
370 | /* Can't do anything on client side */ | |
371 | if (s->server == 0) | |
372 | return -1; | |
373 | if (nmatch == -2) { | |
374 | if (tls1_suiteb(s)) { | |
375 | /* | |
376 | * For Suite B ciphersuite determines curve: we already know | |
377 | * these are acceptable due to previous checks. | |
378 | */ | |
379 | unsigned long cid = s->s3->tmp.new_cipher->id; | |
380 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) | |
381 | return NID_X9_62_prime256v1; /* P-256 */ | |
382 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
383 | return NID_secp384r1; /* P-384 */ | |
384 | /* Should never happen */ | |
385 | return NID_undef; | |
386 | } | |
387 | /* If not Suite B just return first preference shared curve */ | |
388 | nmatch = 0; | |
389 | } | |
390 | /* | |
391 | * Avoid truncation. tls1_get_curvelist takes an int | |
392 | * but s->options is a long... | |
393 | */ | |
394 | if (!tls1_get_curvelist | |
395 | (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp, | |
396 | &num_supp)) | |
397 | /* In practice, NID_undef == 0 but let's be precise. */ | |
398 | return nmatch == -1 ? 0 : NID_undef; | |
399 | if (!tls1_get_curvelist | |
a230b26e | 400 | (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, &num_pref)) |
0f113f3e | 401 | return nmatch == -1 ? 0 : NID_undef; |
3c06513f KR |
402 | |
403 | /* | |
404 | * If the client didn't send the elliptic_curves extension all of them | |
405 | * are allowed. | |
406 | */ | |
407 | if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) { | |
408 | supp = eccurves_all; | |
409 | num_supp = sizeof(eccurves_all) / 2; | |
410 | } else if (num_pref == 0 && | |
a230b26e | 411 | (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) { |
3c06513f KR |
412 | pref = eccurves_all; |
413 | num_pref = sizeof(eccurves_all) / 2; | |
414 | } | |
415 | ||
0f113f3e MC |
416 | k = 0; |
417 | for (i = 0; i < num_pref; i++, pref += 2) { | |
418 | const unsigned char *tsupp = supp; | |
419 | for (j = 0; j < num_supp; j++, tsupp += 2) { | |
420 | if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) { | |
421 | if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED)) | |
422 | continue; | |
423 | if (nmatch == k) { | |
424 | int id = (pref[0] << 8) | pref[1]; | |
ec24630a | 425 | return tls1_ec_curve_id2nid(id, NULL); |
0f113f3e MC |
426 | } |
427 | k++; | |
428 | } | |
429 | } | |
430 | } | |
431 | if (nmatch == -1) | |
432 | return k; | |
433 | /* Out of range (nmatch > k). */ | |
434 | return NID_undef; | |
435 | } | |
d0595f17 | 436 | |
de4d764e MC |
437 | int tls1_set_groups(unsigned char **pext, size_t *pextlen, |
438 | int *groups, size_t ngroups) | |
0f113f3e | 439 | { |
de4d764e | 440 | unsigned char *glist, *p; |
0f113f3e MC |
441 | size_t i; |
442 | /* | |
de4d764e | 443 | * Bitmap of groups included to detect duplicates: only works while group |
0f113f3e MC |
444 | * ids < 32 |
445 | */ | |
446 | unsigned long dup_list = 0; | |
de4d764e MC |
447 | glist = OPENSSL_malloc(ngroups * 2); |
448 | if (glist == NULL) | |
0f113f3e | 449 | return 0; |
de4d764e | 450 | for (i = 0, p = glist; i < ngroups; i++) { |
0f113f3e MC |
451 | unsigned long idmask; |
452 | int id; | |
de4d764e MC |
453 | /* TODO(TLS1.3): Convert for DH groups */ |
454 | id = tls1_ec_nid2curve_id(groups[i]); | |
0f113f3e MC |
455 | idmask = 1L << id; |
456 | if (!id || (dup_list & idmask)) { | |
de4d764e | 457 | OPENSSL_free(glist); |
0f113f3e MC |
458 | return 0; |
459 | } | |
460 | dup_list |= idmask; | |
461 | s2n(id, p); | |
462 | } | |
b548a1f1 | 463 | OPENSSL_free(*pext); |
de4d764e MC |
464 | *pext = glist; |
465 | *pextlen = ngroups * 2; | |
0f113f3e MC |
466 | return 1; |
467 | } | |
468 | ||
469 | # define MAX_CURVELIST 28 | |
470 | ||
471 | typedef struct { | |
472 | size_t nidcnt; | |
473 | int nid_arr[MAX_CURVELIST]; | |
474 | } nid_cb_st; | |
d0595f17 DSH |
475 | |
476 | static int nid_cb(const char *elem, int len, void *arg) | |
0f113f3e MC |
477 | { |
478 | nid_cb_st *narg = arg; | |
479 | size_t i; | |
480 | int nid; | |
481 | char etmp[20]; | |
2747d73c KR |
482 | if (elem == NULL) |
483 | return 0; | |
0f113f3e MC |
484 | if (narg->nidcnt == MAX_CURVELIST) |
485 | return 0; | |
486 | if (len > (int)(sizeof(etmp) - 1)) | |
487 | return 0; | |
488 | memcpy(etmp, elem, len); | |
489 | etmp[len] = 0; | |
490 | nid = EC_curve_nist2nid(etmp); | |
491 | if (nid == NID_undef) | |
492 | nid = OBJ_sn2nid(etmp); | |
493 | if (nid == NID_undef) | |
494 | nid = OBJ_ln2nid(etmp); | |
495 | if (nid == NID_undef) | |
496 | return 0; | |
497 | for (i = 0; i < narg->nidcnt; i++) | |
498 | if (narg->nid_arr[i] == nid) | |
499 | return 0; | |
500 | narg->nid_arr[narg->nidcnt++] = nid; | |
501 | return 1; | |
502 | } | |
503 | ||
de4d764e MC |
504 | /* Set groups based on a colon separate list */ |
505 | int tls1_set_groups_list(unsigned char **pext, size_t *pextlen, const char *str) | |
0f113f3e MC |
506 | { |
507 | nid_cb_st ncb; | |
508 | ncb.nidcnt = 0; | |
509 | if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb)) | |
510 | return 0; | |
511 | if (pext == NULL) | |
512 | return 1; | |
de4d764e | 513 | return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt); |
0f113f3e MC |
514 | } |
515 | ||
fd2b65ce DSH |
516 | /* For an EC key set TLS id and required compression based on parameters */ |
517 | static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id, | |
0f113f3e MC |
518 | EC_KEY *ec) |
519 | { | |
2235b7f2 | 520 | int id; |
0f113f3e | 521 | const EC_GROUP *grp; |
0f113f3e MC |
522 | if (!ec) |
523 | return 0; | |
524 | /* Determine if it is a prime field */ | |
525 | grp = EC_KEY_get0_group(ec); | |
526 | if (!grp) | |
527 | return 0; | |
0f113f3e MC |
528 | /* Determine curve ID */ |
529 | id = EC_GROUP_get_curve_name(grp); | |
530 | id = tls1_ec_nid2curve_id(id); | |
2235b7f2 DSH |
531 | /* If no id return error: we don't support arbitrary explicit curves */ |
532 | if (id == 0) | |
533 | return 0; | |
534 | curve_id[0] = 0; | |
535 | curve_id[1] = (unsigned char)id; | |
0f113f3e MC |
536 | if (comp_id) { |
537 | if (EC_KEY_get0_public_key(ec) == NULL) | |
538 | return 0; | |
2235b7f2 DSH |
539 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) { |
540 | *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; | |
541 | } else { | |
542 | if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME) | |
0f113f3e MC |
543 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
544 | else | |
545 | *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | |
2235b7f2 | 546 | } |
0f113f3e MC |
547 | } |
548 | return 1; | |
549 | } | |
550 | ||
fd2b65ce DSH |
551 | /* Check an EC key is compatible with extensions */ |
552 | static int tls1_check_ec_key(SSL *s, | |
0f113f3e MC |
553 | unsigned char *curve_id, unsigned char *comp_id) |
554 | { | |
555 | const unsigned char *pformats, *pcurves; | |
556 | size_t num_formats, num_curves, i; | |
557 | int j; | |
558 | /* | |
559 | * If point formats extension present check it, otherwise everything is | |
560 | * supported (see RFC4492). | |
561 | */ | |
562 | if (comp_id && s->session->tlsext_ecpointformatlist) { | |
563 | pformats = s->session->tlsext_ecpointformatlist; | |
564 | num_formats = s->session->tlsext_ecpointformatlist_length; | |
565 | for (i = 0; i < num_formats; i++, pformats++) { | |
566 | if (*comp_id == *pformats) | |
567 | break; | |
568 | } | |
569 | if (i == num_formats) | |
570 | return 0; | |
571 | } | |
572 | if (!curve_id) | |
573 | return 1; | |
574 | /* Check curve is consistent with client and server preferences */ | |
575 | for (j = 0; j <= 1; j++) { | |
576 | if (!tls1_get_curvelist(s, j, &pcurves, &num_curves)) | |
577 | return 0; | |
b79d2410 MC |
578 | if (j == 1 && num_curves == 0) { |
579 | /* | |
580 | * If we've not received any curves then skip this check. | |
581 | * RFC 4492 does not require the supported elliptic curves extension | |
582 | * so if it is not sent we can just choose any curve. | |
583 | * It is invalid to send an empty list in the elliptic curves | |
584 | * extension, so num_curves == 0 always means no extension. | |
585 | */ | |
586 | break; | |
587 | } | |
0f113f3e MC |
588 | for (i = 0; i < num_curves; i++, pcurves += 2) { |
589 | if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1]) | |
590 | break; | |
591 | } | |
592 | if (i == num_curves) | |
593 | return 0; | |
594 | /* For clients can only check sent curve list */ | |
595 | if (!s->server) | |
596 | break; | |
597 | } | |
598 | return 1; | |
599 | } | |
d61ff83b | 600 | |
5087afa1 | 601 | static void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
0f113f3e MC |
602 | size_t *num_formats) |
603 | { | |
604 | /* | |
605 | * If we have a custom point format list use it otherwise use default | |
606 | */ | |
607 | if (s->tlsext_ecpointformatlist) { | |
608 | *pformats = s->tlsext_ecpointformatlist; | |
609 | *num_formats = s->tlsext_ecpointformatlist_length; | |
610 | } else { | |
611 | *pformats = ecformats_default; | |
612 | /* For Suite B we don't support char2 fields */ | |
613 | if (tls1_suiteb(s)) | |
614 | *num_formats = sizeof(ecformats_default) - 1; | |
615 | else | |
616 | *num_formats = sizeof(ecformats_default); | |
617 | } | |
618 | } | |
619 | ||
620 | /* | |
621 | * Check cert parameters compatible with extensions: currently just checks EC | |
622 | * certificates have compatible curves and compression. | |
d61ff83b | 623 | */ |
2ea80354 | 624 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) |
0f113f3e MC |
625 | { |
626 | unsigned char comp_id, curve_id[2]; | |
627 | EVP_PKEY *pkey; | |
628 | int rv; | |
8382fd3a | 629 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
630 | if (!pkey) |
631 | return 0; | |
632 | /* If not EC nothing to do */ | |
3aeb9348 | 633 | if (EVP_PKEY_id(pkey) != EVP_PKEY_EC) |
0f113f3e | 634 | return 1; |
3aeb9348 | 635 | rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)); |
0f113f3e MC |
636 | if (!rv) |
637 | return 0; | |
638 | /* | |
639 | * Can't check curve_id for client certs as we don't have a supported | |
640 | * curves extension. | |
641 | */ | |
642 | rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id); | |
643 | if (!rv) | |
644 | return 0; | |
645 | /* | |
646 | * Special case for suite B. We *MUST* sign using SHA256+P-256 or | |
647 | * SHA384+P-384, adjust digest if necessary. | |
648 | */ | |
649 | if (set_ee_md && tls1_suiteb(s)) { | |
650 | int check_md; | |
651 | size_t i; | |
652 | CERT *c = s->cert; | |
653 | if (curve_id[0]) | |
654 | return 0; | |
655 | /* Check to see we have necessary signing algorithm */ | |
656 | if (curve_id[1] == TLSEXT_curve_P_256) | |
657 | check_md = NID_ecdsa_with_SHA256; | |
658 | else if (curve_id[1] == TLSEXT_curve_P_384) | |
659 | check_md = NID_ecdsa_with_SHA384; | |
660 | else | |
661 | return 0; /* Should never happen */ | |
662 | for (i = 0; i < c->shared_sigalgslen; i++) | |
663 | if (check_md == c->shared_sigalgs[i].signandhash_nid) | |
664 | break; | |
665 | if (i == c->shared_sigalgslen) | |
666 | return 0; | |
667 | if (set_ee_md == 2) { | |
668 | if (check_md == NID_ecdsa_with_SHA256) | |
d376e57d | 669 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256(); |
0f113f3e | 670 | else |
d376e57d | 671 | s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384(); |
0f113f3e MC |
672 | } |
673 | } | |
674 | return rv; | |
675 | } | |
676 | ||
10bf4fc2 | 677 | # ifndef OPENSSL_NO_EC |
6977e8ee | 678 | /* |
8483a003 | 679 | * tls1_check_ec_tmp_key - Check EC temporary key compatibility |
6977e8ee KR |
680 | * @s: SSL connection |
681 | * @cid: Cipher ID we're considering using | |
682 | * | |
683 | * Checks that the kECDHE cipher suite we're considering using | |
684 | * is compatible with the client extensions. | |
685 | * | |
686 | * Returns 0 when the cipher can't be used or 1 when it can. | |
687 | */ | |
2ea80354 | 688 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
0f113f3e | 689 | { |
0f113f3e MC |
690 | /* |
691 | * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other | |
692 | * curves permitted. | |
693 | */ | |
694 | if (tls1_suiteb(s)) { | |
6977e8ee | 695 | unsigned char curve_id[2]; |
0f113f3e MC |
696 | /* Curve to check determined by ciphersuite */ |
697 | if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) | |
698 | curve_id[1] = TLSEXT_curve_P_256; | |
699 | else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) | |
700 | curve_id[1] = TLSEXT_curve_P_384; | |
701 | else | |
702 | return 0; | |
703 | curve_id[0] = 0; | |
704 | /* Check this curve is acceptable */ | |
705 | if (!tls1_check_ec_key(s, curve_id, NULL)) | |
706 | return 0; | |
fe6ef247 | 707 | return 1; |
0f113f3e | 708 | } |
fe6ef247 | 709 | /* Need a shared curve */ |
de4d764e | 710 | if (tls1_shared_group(s, 0)) |
fe6ef247 | 711 | return 1; |
6977e8ee | 712 | return 0; |
0f113f3e | 713 | } |
10bf4fc2 | 714 | # endif /* OPENSSL_NO_EC */ |
d0595f17 | 715 | |
14536c8c DSH |
716 | #else |
717 | ||
718 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) | |
0f113f3e MC |
719 | { |
720 | return 1; | |
721 | } | |
14536c8c | 722 | |
0f113f3e | 723 | #endif /* OPENSSL_NO_EC */ |
f1fd4544 | 724 | |
0f113f3e MC |
725 | /* |
726 | * List of supported signature algorithms and hashes. Should make this | |
fc101f88 DSH |
727 | * customisable at some point, for now include everything we support. |
728 | */ | |
729 | ||
e481f9b9 | 730 | #ifdef OPENSSL_NO_RSA |
a230b26e | 731 | # define tlsext_sigalg_rsa(md) /* */ |
e481f9b9 MC |
732 | #else |
733 | # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, | |
734 | #endif | |
0f113f3e | 735 | |
e481f9b9 | 736 | #ifdef OPENSSL_NO_DSA |
a230b26e | 737 | # define tlsext_sigalg_dsa(md) /* */ |
e481f9b9 MC |
738 | #else |
739 | # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa, | |
740 | #endif | |
0f113f3e | 741 | |
e481f9b9 | 742 | #ifdef OPENSSL_NO_EC |
a230b26e | 743 | # define tlsext_sigalg_ecdsa(md)/* */ |
e481f9b9 MC |
744 | #else |
745 | # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, | |
746 | #endif | |
0f113f3e | 747 | |
e481f9b9 | 748 | #define tlsext_sigalg(md) \ |
0f113f3e MC |
749 | tlsext_sigalg_rsa(md) \ |
750 | tlsext_sigalg_dsa(md) \ | |
751 | tlsext_sigalg_ecdsa(md) | |
fc101f88 | 752 | |
d97ed219 | 753 | static const unsigned char tls12_sigalgs[] = { |
0f113f3e MC |
754 | tlsext_sigalg(TLSEXT_hash_sha512) |
755 | tlsext_sigalg(TLSEXT_hash_sha384) | |
0f113f3e MC |
756 | tlsext_sigalg(TLSEXT_hash_sha256) |
757 | tlsext_sigalg(TLSEXT_hash_sha224) | |
0f113f3e | 758 | tlsext_sigalg(TLSEXT_hash_sha1) |
e44380a9 DB |
759 | #ifndef OPENSSL_NO_GOST |
760 | TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001, | |
a230b26e EK |
761 | TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256, |
762 | TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512 | |
e44380a9 | 763 | #endif |
fc101f88 | 764 | }; |
0f113f3e | 765 | |
e481f9b9 | 766 | #ifndef OPENSSL_NO_EC |
d97ed219 | 767 | static const unsigned char suiteb_sigalgs[] = { |
0f113f3e MC |
768 | tlsext_sigalg_ecdsa(TLSEXT_hash_sha256) |
769 | tlsext_sigalg_ecdsa(TLSEXT_hash_sha384) | |
2ea80354 | 770 | }; |
e481f9b9 | 771 | #endif |
b7bfe69b | 772 | size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs) |
0f113f3e MC |
773 | { |
774 | /* | |
775 | * If Suite B mode use Suite B sigalgs only, ignore any other | |
776 | * preferences. | |
777 | */ | |
e481f9b9 | 778 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
779 | switch (tls1_suiteb(s)) { |
780 | case SSL_CERT_FLAG_SUITEB_128_LOS: | |
781 | *psigs = suiteb_sigalgs; | |
782 | return sizeof(suiteb_sigalgs); | |
783 | ||
784 | case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY: | |
785 | *psigs = suiteb_sigalgs; | |
786 | return 2; | |
787 | ||
788 | case SSL_CERT_FLAG_SUITEB_192_LOS: | |
789 | *psigs = suiteb_sigalgs + 2; | |
790 | return 2; | |
791 | } | |
e481f9b9 | 792 | #endif |
0f113f3e MC |
793 | /* If server use client authentication sigalgs if not NULL */ |
794 | if (s->server && s->cert->client_sigalgs) { | |
795 | *psigs = s->cert->client_sigalgs; | |
796 | return s->cert->client_sigalgslen; | |
797 | } else if (s->cert->conf_sigalgs) { | |
798 | *psigs = s->cert->conf_sigalgs; | |
799 | return s->cert->conf_sigalgslen; | |
800 | } else { | |
801 | *psigs = tls12_sigalgs; | |
802 | return sizeof(tls12_sigalgs); | |
803 | } | |
804 | } | |
805 | ||
806 | /* | |
807 | * Check signature algorithm is consistent with sent supported signature | |
ec4a50b3 DSH |
808 | * algorithms and if so return relevant digest. |
809 | */ | |
810 | int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s, | |
0f113f3e MC |
811 | const unsigned char *sig, EVP_PKEY *pkey) |
812 | { | |
813 | const unsigned char *sent_sigs; | |
814 | size_t sent_sigslen, i; | |
815 | int sigalg = tls12_get_sigid(pkey); | |
816 | /* Should never happen */ | |
817 | if (sigalg == -1) | |
818 | return -1; | |
819 | /* Check key type is consistent with signature */ | |
820 | if (sigalg != (int)sig[1]) { | |
821 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
822 | return 0; | |
823 | } | |
e481f9b9 | 824 | #ifndef OPENSSL_NO_EC |
3aeb9348 | 825 | if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { |
0f113f3e MC |
826 | unsigned char curve_id[2], comp_id; |
827 | /* Check compression and curve matches extensions */ | |
3aeb9348 | 828 | if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey))) |
0f113f3e MC |
829 | return 0; |
830 | if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) { | |
831 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE); | |
832 | return 0; | |
833 | } | |
834 | /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */ | |
835 | if (tls1_suiteb(s)) { | |
836 | if (curve_id[0]) | |
837 | return 0; | |
838 | if (curve_id[1] == TLSEXT_curve_P_256) { | |
839 | if (sig[0] != TLSEXT_hash_sha256) { | |
840 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
841 | SSL_R_ILLEGAL_SUITEB_DIGEST); | |
842 | return 0; | |
843 | } | |
844 | } else if (curve_id[1] == TLSEXT_curve_P_384) { | |
845 | if (sig[0] != TLSEXT_hash_sha384) { | |
846 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, | |
847 | SSL_R_ILLEGAL_SUITEB_DIGEST); | |
848 | return 0; | |
849 | } | |
850 | } else | |
851 | return 0; | |
852 | } | |
853 | } else if (tls1_suiteb(s)) | |
854 | return 0; | |
e481f9b9 | 855 | #endif |
0f113f3e MC |
856 | |
857 | /* Check signature matches a type we sent */ | |
858 | sent_sigslen = tls12_get_psigalgs(s, &sent_sigs); | |
859 | for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { | |
860 | if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1]) | |
861 | break; | |
862 | } | |
863 | /* Allow fallback to SHA1 if not strict mode */ | |
864 | if (i == sent_sigslen | |
865 | && (sig[0] != TLSEXT_hash_sha1 | |
866 | || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
867 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); | |
868 | return 0; | |
869 | } | |
870 | *pmd = tls12_get_hash(sig[0]); | |
871 | if (*pmd == NULL) { | |
872 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST); | |
873 | return 0; | |
874 | } | |
875 | /* Make sure security callback allows algorithm */ | |
876 | if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK, | |
a230b26e | 877 | EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) { |
0f113f3e MC |
878 | SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE); |
879 | return 0; | |
880 | } | |
881 | /* | |
882 | * Store the digest used so applications can retrieve it if they wish. | |
883 | */ | |
d376e57d | 884 | s->s3->tmp.peer_md = *pmd; |
0f113f3e MC |
885 | return 1; |
886 | } | |
2ea80354 | 887 | |
0f113f3e | 888 | /* |
3eb2aff4 KR |
889 | * Set a mask of disabled algorithms: an algorithm is disabled if it isn't |
890 | * supported, doesn't appear in supported signature algorithms, isn't supported | |
891 | * by the enabled protocol versions or by the security level. | |
892 | * | |
893 | * This function should only be used for checking which ciphers are supported | |
894 | * by the client. | |
895 | * | |
896 | * Call ssl_cipher_disabled() to check that it's enabled or not. | |
b7bfe69b DSH |
897 | */ |
898 | void ssl_set_client_disabled(SSL *s) | |
0f113f3e | 899 | { |
4d69f9e6 DSH |
900 | s->s3->tmp.mask_a = 0; |
901 | s->s3->tmp.mask_k = 0; | |
4d69f9e6 | 902 | ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK); |
3eb2aff4 | 903 | ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver); |
a230b26e | 904 | #ifndef OPENSSL_NO_PSK |
0f113f3e MC |
905 | /* with PSK there must be client callback set */ |
906 | if (!s->psk_client_callback) { | |
4d69f9e6 | 907 | s->s3->tmp.mask_a |= SSL_aPSK; |
fe5eef3a | 908 | s->s3->tmp.mask_k |= SSL_PSK; |
0f113f3e | 909 | } |
a230b26e | 910 | #endif /* OPENSSL_NO_PSK */ |
e481f9b9 | 911 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 912 | if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) { |
4d69f9e6 DSH |
913 | s->s3->tmp.mask_a |= SSL_aSRP; |
914 | s->s3->tmp.mask_k |= SSL_kSRP; | |
0f113f3e | 915 | } |
e481f9b9 | 916 | #endif |
0f113f3e | 917 | } |
fc101f88 | 918 | |
3eb2aff4 KR |
919 | /* |
920 | * ssl_cipher_disabled - check that a cipher is disabled or not | |
921 | * @s: SSL connection that you want to use the cipher on | |
922 | * @c: cipher to check | |
923 | * @op: Security check that you want to do | |
924 | * | |
925 | * Returns 1 when it's disabled, 0 when enabled. | |
926 | */ | |
b362ccab | 927 | int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op) |
0f113f3e | 928 | { |
3eb2aff4 | 929 | if (c->algorithm_mkey & s->s3->tmp.mask_k |
4d69f9e6 | 930 | || c->algorithm_auth & s->s3->tmp.mask_a) |
0f113f3e | 931 | return 1; |
3eb2aff4 KR |
932 | if (s->s3->tmp.max_ver == 0) |
933 | return 1; | |
934 | if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver) | |
a230b26e | 935 | || (c->max_tls < s->s3->tmp.min_ver))) |
3eb2aff4 KR |
936 | return 1; |
937 | if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver) | |
a230b26e | 938 | || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver))) |
3eb2aff4 KR |
939 | return 1; |
940 | ||
0f113f3e MC |
941 | return !ssl_security(s, op, c->strength_bits, 0, (void *)c); |
942 | } | |
b362ccab DSH |
943 | |
944 | static int tls_use_ticket(SSL *s) | |
0f113f3e | 945 | { |
f5ca0b04 | 946 | if ((s->options & SSL_OP_NO_TICKET) || SSL_IS_TLS13(s)) |
0f113f3e MC |
947 | return 0; |
948 | return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL); | |
949 | } | |
ed3883d2 | 950 | |
a230b26e EK |
951 | static int compare_uint(const void *p1, const void *p2) |
952 | { | |
aa474d1f EK |
953 | unsigned int u1 = *((const unsigned int *)p1); |
954 | unsigned int u2 = *((const unsigned int *)p2); | |
955 | if (u1 < u2) | |
956 | return -1; | |
957 | else if (u1 > u2) | |
958 | return 1; | |
959 | else | |
960 | return 0; | |
961 | } | |
962 | ||
963 | /* | |
964 | * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be | |
965 | * more than one extension of the same type in a ClientHello or ServerHello. | |
966 | * This function does an initial scan over the extensions block to filter those | |
967 | * out. It returns 1 if all extensions are unique, and 0 if the extensions | |
968 | * contain duplicates, could not be successfully parsed, or an internal error | |
969 | * occurred. | |
970 | */ | |
a230b26e EK |
971 | static int tls1_check_duplicate_extensions(const PACKET *packet) |
972 | { | |
aa474d1f EK |
973 | PACKET extensions = *packet; |
974 | size_t num_extensions = 0, i = 0; | |
975 | unsigned int *extension_types = NULL; | |
976 | int ret = 0; | |
977 | ||
978 | /* First pass: count the extensions. */ | |
979 | while (PACKET_remaining(&extensions) > 0) { | |
980 | unsigned int type; | |
981 | PACKET extension; | |
982 | if (!PACKET_get_net_2(&extensions, &type) || | |
983 | !PACKET_get_length_prefixed_2(&extensions, &extension)) { | |
984 | goto done; | |
985 | } | |
986 | num_extensions++; | |
987 | } | |
988 | ||
989 | if (num_extensions <= 1) | |
990 | return 1; | |
991 | ||
992 | extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions); | |
993 | if (extension_types == NULL) { | |
994 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE); | |
995 | goto done; | |
996 | } | |
997 | ||
998 | /* Second pass: gather the extension types. */ | |
999 | extensions = *packet; | |
1000 | for (i = 0; i < num_extensions; i++) { | |
1001 | PACKET extension; | |
1002 | if (!PACKET_get_net_2(&extensions, &extension_types[i]) || | |
1003 | !PACKET_get_length_prefixed_2(&extensions, &extension)) { | |
1004 | /* This should not happen. */ | |
1005 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR); | |
1006 | goto done; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | if (PACKET_remaining(&extensions) != 0) { | |
1011 | SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR); | |
1012 | goto done; | |
1013 | } | |
1014 | /* Sort the extensions and make sure there are no duplicates. */ | |
1015 | qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint); | |
1016 | for (i = 1; i < num_extensions; i++) { | |
1017 | if (extension_types[i - 1] == extension_types[i]) | |
1018 | goto done; | |
1019 | } | |
1020 | ret = 1; | |
1021 | done: | |
1022 | OPENSSL_free(extension_types); | |
1023 | return ret; | |
1024 | } | |
1025 | ||
ae2f7b37 | 1026 | int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al) |
0f113f3e | 1027 | { |
e481f9b9 | 1028 | #ifndef OPENSSL_NO_EC |
d2c27a28 MC |
1029 | const unsigned char *pcurves = NULL; |
1030 | size_t num_curves = 0; | |
0f113f3e | 1031 | int using_ecc = 0; |
c805f618 | 1032 | int min_version, max_version, reason; |
d2c27a28 MC |
1033 | |
1034 | /* See if we support any ECC ciphersuites */ | |
c805f618 | 1035 | if ((s->version >= TLS1_VERSION && s->version <= TLS1_3_VERSION) |
d2c27a28 | 1036 | || SSL_IS_DTLS(s)) { |
0f113f3e MC |
1037 | int i; |
1038 | unsigned long alg_k, alg_a; | |
1039 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); | |
1040 | ||
1041 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { | |
4a640fb6 | 1042 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
0f113f3e MC |
1043 | |
1044 | alg_k = c->algorithm_mkey; | |
1045 | alg_a = c->algorithm_auth; | |
ce0c1f2b | 1046 | if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) |
c805f618 MC |
1047 | || (alg_a & SSL_aECDSA) |
1048 | || c->min_tls >= TLS1_3_VERSION) { | |
0f113f3e MC |
1049 | using_ecc = 1; |
1050 | break; | |
1051 | } | |
1052 | } | |
d2c27a28 MC |
1053 | } |
1054 | #else | |
94ed2c67 | 1055 | if (SSL_IS_TLS13(s)) { |
d2c27a28 MC |
1056 | /* Shouldn't happen! */ |
1057 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1058 | return 0; | |
0f113f3e | 1059 | } |
e481f9b9 | 1060 | #endif |
ed3883d2 | 1061 | |
0f113f3e MC |
1062 | /* Add RI if renegotiating */ |
1063 | if (s->renegotiate) { | |
08029dfa | 1064 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
2f97192c MC |
1065 | || !WPACKET_start_sub_packet_u16(pkt) |
1066 | || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished, | |
1067 | s->s3->previous_client_finished_len) | |
1068 | || !WPACKET_close(pkt)) { | |
0f113f3e | 1069 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
2c7b4dbc | 1070 | return 0; |
5a3d8eeb | 1071 | } |
0f113f3e MC |
1072 | } |
1073 | /* Only add RI for SSLv3 */ | |
1074 | if (s->client_version == SSL3_VERSION) | |
1075 | goto done; | |
1076 | ||
1077 | if (s->tlsext_hostname != NULL) { | |
1078 | /* Add TLS extension servername to the Client Hello message */ | |
08029dfa | 1079 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) |
2c7b4dbc | 1080 | /* Sub-packet for server_name extension */ |
de451856 | 1081 | || !WPACKET_start_sub_packet_u16(pkt) |
2c7b4dbc | 1082 | /* Sub-packet for servername list (always 1 hostname)*/ |
de451856 | 1083 | || !WPACKET_start_sub_packet_u16(pkt) |
08029dfa | 1084 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name) |
b2b3024e MC |
1085 | || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname, |
1086 | strlen(s->tlsext_hostname)) | |
0217dd19 MC |
1087 | || !WPACKET_close(pkt) |
1088 | || !WPACKET_close(pkt)) { | |
2c7b4dbc MC |
1089 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1090 | return 0; | |
1091 | } | |
0f113f3e | 1092 | } |
e481f9b9 | 1093 | #ifndef OPENSSL_NO_SRP |
0f113f3e | 1094 | /* Add SRP username if there is one */ |
2c7b4dbc | 1095 | if (s->srp_ctx.login != NULL) { |
08029dfa | 1096 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp) |
2c7b4dbc | 1097 | /* Sub-packet for SRP extension */ |
de451856 MC |
1098 | || !WPACKET_start_sub_packet_u16(pkt) |
1099 | || !WPACKET_start_sub_packet_u8(pkt) | |
2c7b4dbc | 1100 | /* login must not be zero...internal error if so */ |
de451856 | 1101 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) |
0217dd19 | 1102 | || !WPACKET_memcpy(pkt, s->srp_ctx.login, |
2c7b4dbc | 1103 | strlen(s->srp_ctx.login)) |
0217dd19 MC |
1104 | || !WPACKET_close(pkt) |
1105 | || !WPACKET_close(pkt)) { | |
0f113f3e | 1106 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
2c7b4dbc | 1107 | return 0; |
0f113f3e | 1108 | } |
0f113f3e | 1109 | } |
e481f9b9 | 1110 | #endif |
0f113f3e | 1111 | |
e481f9b9 | 1112 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1113 | if (using_ecc) { |
1114 | /* | |
1115 | * Add TLS extension ECPointFormats to the ClientHello message | |
1116 | */ | |
d2c27a28 MC |
1117 | const unsigned char *pformats, *pcurvestmp; |
1118 | size_t num_formats; | |
0f113f3e | 1119 | size_t i; |
0f113f3e MC |
1120 | |
1121 | tls1_get_formatlist(s, &pformats, &num_formats); | |
1122 | ||
08029dfa | 1123 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
2c7b4dbc | 1124 | /* Sub-packet for formats extension */ |
de451856 | 1125 | || !WPACKET_start_sub_packet_u16(pkt) |
b2b3024e | 1126 | || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats) |
0217dd19 | 1127 | || !WPACKET_close(pkt)) { |
0f113f3e | 1128 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
2c7b4dbc | 1129 | return 0; |
0f113f3e | 1130 | } |
4817504d | 1131 | |
0f113f3e | 1132 | /* |
de4d764e | 1133 | * Add TLS extension supported_groups to the ClientHello message |
0f113f3e | 1134 | */ |
de4d764e MC |
1135 | /* TODO(TLS1.3): Add support for DHE groups */ |
1136 | pcurves = s->tlsext_supportedgroupslist; | |
2c7b4dbc | 1137 | if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) { |
0f113f3e | 1138 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
2c7b4dbc | 1139 | return 0; |
0f113f3e | 1140 | } |
d2c27a28 | 1141 | pcurvestmp = pcurves; |
ee2ffc27 | 1142 | |
de4d764e MC |
1143 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) |
1144 | /* Sub-packet for supported_groups extension */ | |
de451856 MC |
1145 | || !WPACKET_start_sub_packet_u16(pkt) |
1146 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
2c7b4dbc MC |
1147 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1148 | return 0; | |
1149 | } | |
0f113f3e | 1150 | /* Copy curve ID if supported */ |
d2c27a28 | 1151 | for (i = 0; i < num_curves; i++, pcurvestmp += 2) { |
0f113f3e | 1152 | if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) { |
d2c27a28 MC |
1153 | if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0]) |
1154 | || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) { | |
2c7b4dbc MC |
1155 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, |
1156 | ERR_R_INTERNAL_ERROR); | |
1157 | return 0; | |
1158 | } | |
0f113f3e MC |
1159 | } |
1160 | } | |
0217dd19 | 1161 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
2c7b4dbc MC |
1162 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1163 | return 0; | |
1164 | } | |
0f113f3e | 1165 | } |
a230b26e | 1166 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
1167 | |
1168 | if (tls_use_ticket(s)) { | |
348240c6 | 1169 | size_t ticklen; |
0f113f3e MC |
1170 | if (!s->new_session && s->session && s->session->tlsext_tick) |
1171 | ticklen = s->session->tlsext_ticklen; | |
1172 | else if (s->session && s->tlsext_session_ticket && | |
1173 | s->tlsext_session_ticket->data) { | |
1174 | ticklen = s->tlsext_session_ticket->length; | |
1175 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); | |
2c7b4dbc MC |
1176 | if (s->session->tlsext_tick == NULL) { |
1177 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1178 | return 0; | |
1179 | } | |
0f113f3e MC |
1180 | memcpy(s->session->tlsext_tick, |
1181 | s->tlsext_session_ticket->data, ticklen); | |
1182 | s->session->tlsext_ticklen = ticklen; | |
1183 | } else | |
1184 | ticklen = 0; | |
1185 | if (ticklen == 0 && s->tlsext_session_ticket && | |
1186 | s->tlsext_session_ticket->data == NULL) | |
1187 | goto skip_ext; | |
2c7b4dbc | 1188 | |
08029dfa | 1189 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
b2b3024e MC |
1190 | || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick, |
1191 | ticklen)) { | |
2c7b4dbc MC |
1192 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1193 | return 0; | |
0f113f3e MC |
1194 | } |
1195 | } | |
1196 | skip_ext: | |
1197 | ||
f7aa3185 | 1198 | if (SSL_CLIENT_USE_SIGALGS(s)) { |
0f113f3e MC |
1199 | size_t salglen; |
1200 | const unsigned char *salg; | |
2c7b4dbc | 1201 | |
0f113f3e | 1202 | salglen = tls12_get_psigalgs(s, &salg); |
2c7b4dbc | 1203 | |
08029dfa | 1204 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) |
2c7b4dbc | 1205 | /* Sub-packet for sig-algs extension */ |
de451856 | 1206 | || !WPACKET_start_sub_packet_u16(pkt) |
2c7b4dbc | 1207 | /* Sub-packet for the actual list */ |
de451856 | 1208 | || !WPACKET_start_sub_packet_u16(pkt) |
0217dd19 MC |
1209 | || !tls12_copy_sigalgs(s, pkt, salg, salglen) |
1210 | || !WPACKET_close(pkt) | |
1211 | || !WPACKET_close(pkt)) { | |
2c7b4dbc MC |
1212 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1213 | return 0; | |
1214 | } | |
0f113f3e | 1215 | } |
3e41ac35 | 1216 | #ifndef OPENSSL_NO_OCSP |
0f113f3e MC |
1217 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { |
1218 | int i; | |
0f113f3e | 1219 | |
08029dfa | 1220 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
2c7b4dbc | 1221 | /* Sub-packet for status request extension */ |
de451856 | 1222 | || !WPACKET_start_sub_packet_u16(pkt) |
08029dfa | 1223 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp) |
2c7b4dbc | 1224 | /* Sub-packet for the ids */ |
de451856 | 1225 | || !WPACKET_start_sub_packet_u16(pkt)) { |
2c7b4dbc MC |
1226 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1227 | return 0; | |
1228 | } | |
0f113f3e | 1229 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { |
2c7b4dbc MC |
1230 | unsigned char *idbytes; |
1231 | int idlen; | |
1232 | OCSP_RESPID *id; | |
2c7b4dbc | 1233 | |
0f113f3e | 1234 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); |
2c7b4dbc MC |
1235 | idlen = i2d_OCSP_RESPID(id, NULL); |
1236 | if (idlen <= 0 | |
1237 | /* Sub-packet for an individual id */ | |
dd8710dc | 1238 | || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) |
b2b3024e | 1239 | || i2d_OCSP_RESPID(id, &idbytes) != idlen) { |
2c7b4dbc MC |
1240 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1241 | return 0; | |
1242 | } | |
1243 | } | |
0217dd19 | 1244 | if (!WPACKET_close(pkt) |
de451856 | 1245 | || !WPACKET_start_sub_packet_u16(pkt)) { |
2c7b4dbc MC |
1246 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1247 | return 0; | |
860c3dd1 | 1248 | } |
0f113f3e | 1249 | if (s->tlsext_ocsp_exts) { |
2c7b4dbc MC |
1250 | unsigned char *extbytes; |
1251 | int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); | |
0f113f3e | 1252 | |
2c7b4dbc MC |
1253 | if (extlen < 0) { |
1254 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1255 | return 0; | |
1256 | } | |
0217dd19 | 1257 | if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes) |
2c7b4dbc MC |
1258 | || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes) |
1259 | != extlen) { | |
1260 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1261 | return 0; | |
1262 | } | |
1263 | } | |
0217dd19 | 1264 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
2c7b4dbc MC |
1265 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1266 | return 0; | |
0f113f3e | 1267 | } |
0f113f3e | 1268 | } |
3e41ac35 | 1269 | #endif |
0f113f3e | 1270 | |
e481f9b9 | 1271 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1272 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { |
1273 | /* | |
8483a003 | 1274 | * The client advertises an empty extension to indicate its support |
0f113f3e MC |
1275 | * for Next Protocol Negotiation |
1276 | */ | |
08029dfa MC |
1277 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
1278 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
2c7b4dbc MC |
1279 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1280 | return 0; | |
1281 | } | |
0f113f3e | 1282 | } |
e481f9b9 | 1283 | #endif |
0f113f3e | 1284 | |
817cd0d5 TS |
1285 | /* |
1286 | * finish_md_len is non-zero during a renegotiation, so | |
1287 | * this avoids sending ALPN during the renegotiation | |
1288 | * (see longer comment below) | |
1289 | */ | |
0f113f3e | 1290 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) { |
08029dfa MC |
1291 | if (!WPACKET_put_bytes_u16(pkt, |
1292 | TLSEXT_TYPE_application_layer_protocol_negotiation) | |
2c7b4dbc | 1293 | /* Sub-packet ALPN extension */ |
de451856 | 1294 | || !WPACKET_start_sub_packet_u16(pkt) |
b2b3024e MC |
1295 | || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list, |
1296 | s->alpn_client_proto_list_len) | |
0217dd19 | 1297 | || !WPACKET_close(pkt)) { |
2c7b4dbc MC |
1298 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1299 | return 0; | |
1300 | } | |
817cd0d5 | 1301 | s->s3->alpn_sent = 1; |
0f113f3e | 1302 | } |
e481f9b9 | 1303 | #ifndef OPENSSL_NO_SRTP |
0f113f3e | 1304 | if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) { |
7facdbd6 | 1305 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s); |
2c7b4dbc MC |
1306 | SRTP_PROTECTION_PROFILE *prof; |
1307 | int i, ct; | |
2c7b4dbc | 1308 | |
08029dfa | 1309 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
2c7b4dbc | 1310 | /* Sub-packet for SRTP extension */ |
de451856 | 1311 | || !WPACKET_start_sub_packet_u16(pkt) |
2c7b4dbc | 1312 | /* Sub-packet for the protection profile list */ |
de451856 | 1313 | || !WPACKET_start_sub_packet_u16(pkt)) { |
69f68237 | 1314 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
2c7b4dbc | 1315 | return 0; |
69f68237 | 1316 | } |
2c7b4dbc MC |
1317 | ct = sk_SRTP_PROTECTION_PROFILE_num(clnt); |
1318 | for (i = 0; i < ct; i++) { | |
1319 | prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); | |
08029dfa | 1320 | if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { |
2c7b4dbc MC |
1321 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1322 | return 0; | |
1323 | } | |
1324 | } | |
7facdbd6 MC |
1325 | if (!WPACKET_close(pkt) |
1326 | /* Add an empty use_mki value */ | |
1327 | || !WPACKET_put_bytes_u8(pkt, 0) | |
1328 | || !WPACKET_close(pkt)) { | |
0f113f3e | 1329 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
2c7b4dbc | 1330 | return 0; |
0f113f3e | 1331 | } |
0f113f3e | 1332 | } |
e481f9b9 | 1333 | #endif |
0f113f3e MC |
1334 | custom_ext_init(&s->cert->cli_ext); |
1335 | /* Add custom TLS Extensions to ClientHello */ | |
2c7b4dbc MC |
1336 | if (!custom_ext_add(s, 0, pkt, al)) { |
1337 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1338 | return 0; | |
1339 | } | |
1340 | ||
cde6145b DW |
1341 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) { |
1342 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) | |
08029dfa | 1343 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
cde6145b DW |
1344 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1345 | return 0; | |
1346 | } | |
2c7b4dbc MC |
1347 | } |
1348 | ||
ed29e82a RP |
1349 | #ifndef OPENSSL_NO_CT |
1350 | if (s->ct_validation_callback != NULL) { | |
08029dfa MC |
1351 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp) |
1352 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
2c7b4dbc MC |
1353 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1354 | return 0; | |
1355 | } | |
ed29e82a | 1356 | } |
e481f9b9 | 1357 | #endif |
2c7b4dbc | 1358 | |
08029dfa MC |
1359 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
1360 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
2c7b4dbc MC |
1361 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1362 | return 0; | |
1363 | } | |
0f113f3e | 1364 | |
c805f618 MC |
1365 | reason = ssl_get_client_min_max_version(s, &min_version, &max_version); |
1366 | if (reason != 0) { | |
1367 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, reason); | |
1368 | return 0; | |
1369 | } | |
1370 | ||
d2c27a28 | 1371 | /* TLS1.3 specific extensions */ |
c805f618 MC |
1372 | if (!SSL_IS_DTLS(s) && max_version >= TLS1_3_VERSION) { |
1373 | int currv; | |
d2c27a28 MC |
1374 | size_t i, sharessent = 0; |
1375 | ||
1376 | /* TODO(TLS1.3): Should we add this extension for versions < TLS1.3? */ | |
1377 | /* supported_versions extension */ | |
b5b253b1 MC |
1378 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) |
1379 | || !WPACKET_start_sub_packet_u16(pkt) | |
1380 | || !WPACKET_start_sub_packet_u8(pkt)) { | |
1381 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1382 | return 0; | |
1383 | } | |
c805f618 | 1384 | |
f2342b7a MC |
1385 | /* |
1386 | * TODO(TLS1.3): There is some discussion on the TLS list as to wheter | |
1387 | * we should include versions <TLS1.2. For the moment we do. To be | |
1388 | * reviewed later. | |
1389 | */ | |
b5b253b1 MC |
1390 | for (currv = max_version; currv >= min_version; currv--) { |
1391 | /* TODO(TLS1.3): Remove this first if clause prior to release!! */ | |
1392 | if (currv == TLS1_3_VERSION) { | |
1393 | if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) { | |
1394 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | |
1395 | ERR_R_INTERNAL_ERROR); | |
1396 | return 0; | |
1397 | } | |
1398 | } else if (!WPACKET_put_bytes_u16(pkt, currv)) { | |
1399 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1400 | return 0; | |
1401 | } | |
1402 | } | |
1403 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
1404 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1405 | return 0; | |
1406 | } | |
d2c27a28 MC |
1407 | |
1408 | ||
1409 | /* key_share extension */ | |
1410 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) | |
1411 | /* Extension data sub-packet */ | |
1412 | || !WPACKET_start_sub_packet_u16(pkt) | |
1413 | /* KeyShare list sub-packet */ | |
1414 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
1415 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1416 | return 0; | |
1417 | } | |
1418 | ||
1419 | /* | |
1420 | * TODO(TLS1.3): Make the number of key_shares sent configurable. For | |
1421 | * now, just send one | |
1422 | */ | |
1423 | for (i = 0; i < num_curves && sharessent < 1; i++, pcurves += 2) { | |
94ed2c67 MC |
1424 | unsigned char *encodedPoint = NULL; |
1425 | unsigned int curve_id = 0; | |
1426 | EVP_PKEY *key_share_key = NULL; | |
1427 | size_t encodedlen; | |
1428 | ||
1429 | if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) | |
1430 | continue; | |
1431 | ||
1432 | if (s->s3->tmp.pkey != NULL) { | |
1433 | /* Shouldn't happen! */ | |
1434 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | |
1435 | ERR_R_INTERNAL_ERROR); | |
1436 | return 0; | |
1437 | } | |
d2c27a28 | 1438 | |
94ed2c67 MC |
1439 | /* Generate a key for this key_share */ |
1440 | curve_id = (pcurves[0] << 8) | pcurves[1]; | |
1441 | key_share_key = ssl_generate_pkey_curve(curve_id); | |
1442 | if (key_share_key == NULL) { | |
1443 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_EVP_LIB); | |
1444 | return 0; | |
1445 | } | |
d2c27a28 | 1446 | |
94ed2c67 MC |
1447 | /* Encode the public key. */ |
1448 | encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key, | |
1449 | &encodedPoint); | |
1450 | if (encodedlen == 0) { | |
1451 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_EC_LIB); | |
1452 | EVP_PKEY_free(key_share_key); | |
1453 | return 0; | |
1454 | } | |
d2c27a28 | 1455 | |
94ed2c67 MC |
1456 | /* Create KeyShareEntry */ |
1457 | if (!WPACKET_put_bytes_u16(pkt, curve_id) | |
1458 | || !WPACKET_sub_memcpy_u16(pkt, encodedPoint, encodedlen)) { | |
1459 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | |
1460 | ERR_R_INTERNAL_ERROR); | |
1461 | EVP_PKEY_free(key_share_key); | |
d2c27a28 | 1462 | OPENSSL_free(encodedPoint); |
94ed2c67 | 1463 | return 0; |
d2c27a28 | 1464 | } |
94ed2c67 MC |
1465 | |
1466 | /* | |
1467 | * TODO(TLS1.3): When changing to send more than one key_share we're | |
1468 | * going to need to be able to save more than one EVP_PKEY. For now | |
1469 | * we reuse the existing tmp.pkey | |
1470 | */ | |
1471 | s->s3->group_id = curve_id; | |
1472 | s->s3->tmp.pkey = key_share_key; | |
1473 | sharessent++; | |
1474 | OPENSSL_free(encodedPoint); | |
d2c27a28 MC |
1475 | } |
1476 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
1477 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1478 | return 0; | |
1479 | } | |
b5b253b1 MC |
1480 | } |
1481 | ||
0f113f3e MC |
1482 | /* |
1483 | * Add padding to workaround bugs in F5 terminators. See | |
1484 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this | |
1485 | * code works out the length of all existing extensions it MUST always | |
1486 | * appear last. | |
1487 | */ | |
1488 | if (s->options & SSL_OP_TLSEXT_PADDING) { | |
2c7b4dbc MC |
1489 | unsigned char *padbytes; |
1490 | size_t hlen; | |
1491 | ||
ae2f7b37 | 1492 | if (!WPACKET_get_total_written(pkt, &hlen)) { |
2c7b4dbc MC |
1493 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1494 | return 0; | |
1495 | } | |
a3680c8f | 1496 | |
0f113f3e MC |
1497 | if (hlen > 0xff && hlen < 0x200) { |
1498 | hlen = 0x200 - hlen; | |
1499 | if (hlen >= 4) | |
1500 | hlen -= 4; | |
1501 | else | |
1502 | hlen = 0; | |
1503 | ||
08029dfa | 1504 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding) |
b2b3024e | 1505 | || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) { |
2c7b4dbc MC |
1506 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
1507 | return 0; | |
1508 | } | |
1509 | memset(padbytes, 0, hlen); | |
0f113f3e MC |
1510 | } |
1511 | } | |
5a3d8eeb | 1512 | |
0f113f3e | 1513 | done: |
2c7b4dbc | 1514 | return 1; |
0f113f3e | 1515 | } |
333f926d | 1516 | |
94ed2c67 MC |
1517 | /* |
1518 | * Add the key_share extension. | |
1519 | * | |
1520 | * Returns 1 on success or 0 on failure. | |
1521 | */ | |
1522 | static int add_client_key_share_ext(SSL *s, WPACKET *pkt, int *al) | |
1523 | { | |
1524 | unsigned char *encodedPoint; | |
1525 | size_t encoded_pt_len = 0; | |
1526 | EVP_PKEY *ckey = s->s3->peer_tmp, *skey = NULL; | |
1527 | ||
1528 | if (ckey == NULL) { | |
1529 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); | |
1530 | return 0; | |
1531 | } | |
1532 | ||
1533 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) | |
1534 | || !WPACKET_start_sub_packet_u16(pkt) | |
1535 | || !WPACKET_put_bytes_u16(pkt, s->s3->group_id)) { | |
1536 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); | |
1537 | return 0; | |
1538 | } | |
1539 | ||
1540 | skey = ssl_generate_pkey(ckey); | |
884a790e MC |
1541 | if (skey == NULL) { |
1542 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_MALLOC_FAILURE); | |
1543 | return 0; | |
1544 | } | |
94ed2c67 MC |
1545 | |
1546 | /* Generate encoding of server key */ | |
1547 | encoded_pt_len = EVP_PKEY_get1_tls_encodedpoint(skey, &encodedPoint); | |
1548 | if (encoded_pt_len == 0) { | |
1549 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_EC_LIB); | |
1550 | EVP_PKEY_free(skey); | |
1551 | return 0; | |
1552 | } | |
1553 | ||
1554 | if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len) | |
1555 | || !WPACKET_close(pkt)) { | |
1556 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); | |
1557 | EVP_PKEY_free(skey); | |
1558 | OPENSSL_free(encodedPoint); | |
1559 | return 0; | |
1560 | } | |
1561 | OPENSSL_free(encodedPoint); | |
1562 | ||
1563 | /* This causes the crypto state to be updated based on the derived keys */ | |
1564 | s->s3->tmp.pkey = skey; | |
1565 | if (ssl_derive(s, skey, ckey, 1) == 0) { | |
1566 | *al = SSL_AD_INTERNAL_ERROR; | |
1567 | SSLerr(SSL_F_ADD_CLIENT_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); | |
1568 | return 0; | |
1569 | } | |
1570 | ||
1571 | return 1; | |
1572 | } | |
1573 | ||
8157d44b | 1574 | int ssl_add_serverhello_tlsext(SSL *s, WPACKET *pkt, int *al) |
0f113f3e | 1575 | { |
e481f9b9 | 1576 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 1577 | int next_proto_neg_seen; |
e481f9b9 MC |
1578 | #endif |
1579 | #ifndef OPENSSL_NO_EC | |
0f113f3e MC |
1580 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
1581 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
ce0c1f2b | 1582 | int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
0f113f3e | 1583 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); |
e481f9b9 | 1584 | #endif |
0f113f3e | 1585 | |
8157d44b MC |
1586 | if (!WPACKET_start_sub_packet_u16(pkt) |
1587 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) { | |
1588 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1589 | return 0; | |
1590 | } | |
333f926d | 1591 | |
8157d44b MC |
1592 | if (s->s3->send_connection_binding && |
1593 | !ssl_add_serverhello_renegotiate_ext(s, pkt)) { | |
1594 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1595 | return 0; | |
0f113f3e MC |
1596 | } |
1597 | ||
1598 | /* Only add RI for SSLv3 */ | |
1599 | if (s->version == SSL3_VERSION) | |
1600 | goto done; | |
1601 | ||
1602 | if (!s->hit && s->servername_done == 1 | |
8157d44b MC |
1603 | && s->session->tlsext_hostname != NULL) { |
1604 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) | |
1605 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
1606 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1607 | return 0; | |
1608 | } | |
0f113f3e | 1609 | } |
e481f9b9 | 1610 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
1611 | if (using_ecc) { |
1612 | const unsigned char *plist; | |
1613 | size_t plistlen; | |
1614 | /* | |
1615 | * Add TLS extension ECPointFormats to the ServerHello message | |
1616 | */ | |
0f113f3e MC |
1617 | tls1_get_formatlist(s, &plist, &plistlen); |
1618 | ||
8157d44b MC |
1619 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
1620 | || !WPACKET_start_sub_packet_u16(pkt) | |
1621 | || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen) | |
1622 | || !WPACKET_close(pkt)) { | |
0f113f3e | 1623 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
8157d44b | 1624 | return 0; |
0f113f3e | 1625 | } |
0f113f3e MC |
1626 | } |
1627 | /* | |
1628 | * Currently the server should not respond with a SupportedCurves | |
1629 | * extension | |
1630 | */ | |
a230b26e | 1631 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
1632 | |
1633 | if (s->tlsext_ticket_expected && tls_use_ticket(s)) { | |
8157d44b MC |
1634 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
1635 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
1636 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1637 | return 0; | |
1638 | } | |
5c753de6 | 1639 | } else { |
a230b26e EK |
1640 | /* |
1641 | * if we don't add the above TLSEXT, we can't add a session ticket | |
1642 | * later | |
1643 | */ | |
5c753de6 | 1644 | s->tlsext_ticket_expected = 0; |
0f113f3e MC |
1645 | } |
1646 | ||
1647 | if (s->tlsext_status_expected) { | |
8157d44b MC |
1648 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
1649 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
1650 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1651 | return 0; | |
1652 | } | |
0f113f3e | 1653 | } |
e481f9b9 | 1654 | #ifndef OPENSSL_NO_SRTP |
0f113f3e | 1655 | if (SSL_IS_DTLS(s) && s->srtp_profile) { |
8157d44b MC |
1656 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
1657 | || !WPACKET_start_sub_packet_u16(pkt) | |
1658 | || !WPACKET_put_bytes_u16(pkt, 2) | |
1659 | || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id) | |
1660 | || !WPACKET_put_bytes_u8(pkt, 0) | |
1661 | || !WPACKET_close(pkt)) { | |
0f113f3e | 1662 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
8157d44b | 1663 | return 0; |
0f113f3e | 1664 | } |
0f113f3e | 1665 | } |
e481f9b9 | 1666 | #endif |
0f113f3e MC |
1667 | |
1668 | if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80 | |
1669 | || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81) | |
1670 | && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) { | |
1671 | const unsigned char cryptopro_ext[36] = { | |
1672 | 0xfd, 0xe8, /* 65000 */ | |
1673 | 0x00, 0x20, /* 32 bytes length */ | |
1674 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, | |
1675 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, | |
1676 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, | |
1677 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 | |
1678 | }; | |
8157d44b MC |
1679 | if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) { |
1680 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1681 | return 0; | |
1682 | } | |
0f113f3e | 1683 | } |
0f113f3e | 1684 | |
e481f9b9 | 1685 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
1686 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
1687 | s->s3->next_proto_neg_seen = 0; | |
1688 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { | |
1689 | const unsigned char *npa; | |
1690 | unsigned int npalen; | |
1691 | int r; | |
1692 | ||
1693 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, | |
1694 | s-> | |
1695 | ctx->next_protos_advertised_cb_arg); | |
1696 | if (r == SSL_TLSEXT_ERR_OK) { | |
8157d44b MC |
1697 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
1698 | || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) { | |
1699 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1700 | return 0; | |
1701 | } | |
0f113f3e MC |
1702 | s->s3->next_proto_neg_seen = 1; |
1703 | } | |
1704 | } | |
e481f9b9 | 1705 | #endif |
bcec3358 | 1706 | |
94ed2c67 MC |
1707 | if (SSL_IS_TLS13(s) && !s->hit && !add_client_key_share_ext(s, pkt, al)) |
1708 | return 0; | |
bcec3358 | 1709 | |
8157d44b MC |
1710 | if (!custom_ext_add(s, 1, pkt, al)) { |
1711 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1712 | return 0; | |
1713 | } | |
1714 | ||
0f113f3e MC |
1715 | if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) { |
1716 | /* | |
1717 | * Don't use encrypt_then_mac if AEAD or RC4 might want to disable | |
1718 | * for other cases too. | |
1719 | */ | |
1720 | if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD | |
e44380a9 DB |
1721 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4 |
1722 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT | |
1723 | || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12) | |
0f113f3e MC |
1724 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
1725 | else { | |
8157d44b MC |
1726 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) |
1727 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
1728 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1729 | return 0; | |
1730 | } | |
0f113f3e MC |
1731 | } |
1732 | } | |
e7f0d921 | 1733 | if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) { |
8157d44b MC |
1734 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
1735 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
1736 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1737 | return 0; | |
1738 | } | |
ddc06b35 | 1739 | } |
0f113f3e | 1740 | |
817cd0d5 | 1741 | if (s->s3->alpn_selected != NULL) { |
8157d44b MC |
1742 | if (!WPACKET_put_bytes_u16(pkt, |
1743 | TLSEXT_TYPE_application_layer_protocol_negotiation) | |
1744 | || !WPACKET_start_sub_packet_u16(pkt) | |
1745 | || !WPACKET_start_sub_packet_u16(pkt) | |
1746 | || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected, | |
1747 | s->s3->alpn_selected_len) | |
1748 | || !WPACKET_close(pkt) | |
1749 | || !WPACKET_close(pkt)) { | |
1750 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1751 | return 0; | |
1752 | } | |
0f113f3e MC |
1753 | } |
1754 | ||
1755 | done: | |
8157d44b MC |
1756 | if (!WPACKET_close(pkt)) { |
1757 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
1758 | return 0; | |
1759 | } | |
1760 | return 1; | |
0f113f3e | 1761 | } |
a398f821 | 1762 | |
0f113f3e | 1763 | /* |
817cd0d5 | 1764 | * Save the ALPN extension in a ClientHello. |
06217867 EK |
1765 | * pkt: the contents of the ALPN extension, not including type and length. |
1766 | * al: a pointer to the alert value to send in the event of a failure. | |
1767 | * returns: 1 on success, 0 on error. | |
0f113f3e | 1768 | */ |
9ceb2426 | 1769 | static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al) |
0f113f3e | 1770 | { |
06217867 | 1771 | PACKET protocol_list, save_protocol_list, protocol; |
0f113f3e | 1772 | |
06217867 | 1773 | *al = SSL_AD_DECODE_ERROR; |
0f113f3e | 1774 | |
06217867 EK |
1775 | if (!PACKET_as_length_prefixed_2(pkt, &protocol_list) |
1776 | || PACKET_remaining(&protocol_list) < 2) { | |
1777 | return 0; | |
1778 | } | |
0f113f3e | 1779 | |
06217867 | 1780 | save_protocol_list = protocol_list; |
9ceb2426 | 1781 | do { |
06217867 EK |
1782 | /* Protocol names can't be empty. */ |
1783 | if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol) | |
1784 | || PACKET_remaining(&protocol) == 0) { | |
1785 | return 0; | |
1786 | } | |
1787 | } while (PACKET_remaining(&protocol_list) != 0); | |
1788 | ||
817cd0d5 | 1789 | if (!PACKET_memdup(&save_protocol_list, |
a230b26e | 1790 | &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) { |
817cd0d5 TS |
1791 | *al = TLS1_AD_INTERNAL_ERROR; |
1792 | return 0; | |
1793 | } | |
1794 | ||
1795 | return 1; | |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * Process the ALPN extension in a ClientHello. | |
817cd0d5 | 1800 | * al: a pointer to the alert value to send in the event of a failure. |
70c22888 | 1801 | * returns 1 on success, 0 on error. |
817cd0d5 | 1802 | */ |
70c22888 | 1803 | static int tls1_alpn_handle_client_hello_late(SSL *s, int *al) |
817cd0d5 TS |
1804 | { |
1805 | const unsigned char *selected = NULL; | |
1806 | unsigned char selected_len = 0; | |
1807 | ||
1808 | if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) { | |
1809 | int r = s->ctx->alpn_select_cb(s, &selected, &selected_len, | |
1810 | s->s3->alpn_proposed, | |
348240c6 | 1811 | (unsigned int)s->s3->alpn_proposed_len, |
817cd0d5 | 1812 | s->ctx->alpn_select_cb_arg); |
0f113f3e | 1813 | |
817cd0d5 TS |
1814 | if (r == SSL_TLSEXT_ERR_OK) { |
1815 | OPENSSL_free(s->s3->alpn_selected); | |
1816 | s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len); | |
1817 | if (s->s3->alpn_selected == NULL) { | |
1818 | *al = SSL_AD_INTERNAL_ERROR; | |
817cd0d5 TS |
1819 | return 0; |
1820 | } | |
1821 | s->s3->alpn_selected_len = selected_len; | |
0351baae TS |
1822 | #ifndef OPENSSL_NO_NEXTPROTONEG |
1823 | /* ALPN takes precedence over NPN. */ | |
1824 | s->s3->next_proto_neg_seen = 0; | |
1825 | #endif | |
817cd0d5 TS |
1826 | } else { |
1827 | *al = SSL_AD_NO_APPLICATION_PROTOCOL; | |
06217867 | 1828 | return 0; |
0f113f3e | 1829 | } |
0f113f3e | 1830 | } |
0f113f3e | 1831 | |
06217867 | 1832 | return 1; |
0f113f3e | 1833 | } |
6f017a8f | 1834 | |
e481f9b9 | 1835 | #ifndef OPENSSL_NO_EC |
1d97c843 TH |
1836 | /*- |
1837 | * ssl_check_for_safari attempts to fingerprint Safari using OS X | |
3d33f3bb | 1838 | * SecureTransport using the TLS extension block in |hello|. |
dece3209 RS |
1839 | * Safari, since 10.6, sends exactly these extensions, in this order: |
1840 | * SNI, | |
1841 | * elliptic_curves | |
1842 | * ec_point_formats | |
1843 | * | |
1844 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, | |
1845 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. | |
1846 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from | |
1847 | * 10.8..10.8.3 (which don't work). | |
1848 | */ | |
902aca09 | 1849 | static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello) |
0f113f3e | 1850 | { |
06217867 EK |
1851 | unsigned int type; |
1852 | PACKET sni, tmppkt; | |
1853 | size_t ext_len; | |
9ceb2426 | 1854 | |
0f113f3e MC |
1855 | static const unsigned char kSafariExtensionsBlock[] = { |
1856 | 0x00, 0x0a, /* elliptic_curves extension */ | |
1857 | 0x00, 0x08, /* 8 bytes */ | |
1858 | 0x00, 0x06, /* 6 bytes of curve ids */ | |
1859 | 0x00, 0x17, /* P-256 */ | |
1860 | 0x00, 0x18, /* P-384 */ | |
1861 | 0x00, 0x19, /* P-521 */ | |
1862 | ||
1863 | 0x00, 0x0b, /* ec_point_formats */ | |
1864 | 0x00, 0x02, /* 2 bytes */ | |
1865 | 0x01, /* 1 point format */ | |
1866 | 0x00, /* uncompressed */ | |
06217867 | 1867 | /* The following is only present in TLS 1.2 */ |
0f113f3e MC |
1868 | 0x00, 0x0d, /* signature_algorithms */ |
1869 | 0x00, 0x0c, /* 12 bytes */ | |
1870 | 0x00, 0x0a, /* 10 bytes */ | |
1871 | 0x05, 0x01, /* SHA-384/RSA */ | |
1872 | 0x04, 0x01, /* SHA-256/RSA */ | |
1873 | 0x02, 0x01, /* SHA-1/RSA */ | |
1874 | 0x04, 0x03, /* SHA-256/ECDSA */ | |
1875 | 0x02, 0x03, /* SHA-1/ECDSA */ | |
1876 | }; | |
1877 | ||
06217867 EK |
1878 | /* Length of the common prefix (first two extensions). */ |
1879 | static const size_t kSafariCommonExtensionsLength = 18; | |
1880 | ||
1ab3836b | 1881 | tmppkt = hello->extensions; |
68a16628 MC |
1882 | |
1883 | if (!PACKET_forward(&tmppkt, 2) | |
06217867 EK |
1884 | || !PACKET_get_net_2(&tmppkt, &type) |
1885 | || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) { | |
0f113f3e | 1886 | return; |
06217867 | 1887 | } |
0f113f3e MC |
1888 | |
1889 | if (type != TLSEXT_TYPE_server_name) | |
1890 | return; | |
1891 | ||
06217867 EK |
1892 | ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ? |
1893 | sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength; | |
0f113f3e | 1894 | |
06217867 EK |
1895 | s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock, |
1896 | ext_len); | |
dece3209 | 1897 | } |
a230b26e | 1898 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e | 1899 | |
ef7daaf9 MC |
1900 | |
1901 | /* | |
1902 | * Process the supported_groups extension if present. Returns success if the | |
1903 | * extension is absent, or if it has been successfully processed. | |
1904 | * | |
94ed2c67 | 1905 | * Returns 1 on success or 0 on failure |
ef7daaf9 MC |
1906 | */ |
1907 | static int tls_process_supported_groups(SSL *s, CLIENTHELLO_MSG *hello) | |
1908 | { | |
1909 | #ifndef OPENSSL_NO_EC | |
1910 | PACKET supported_groups_list; | |
1911 | RAW_EXTENSION *suppgroups = tls_get_extension_by_type(hello->pre_proc_exts, | |
1912 | hello->num_extensions, | |
1913 | TLSEXT_TYPE_supported_groups); | |
1914 | ||
1915 | if (suppgroups == NULL) | |
1916 | return 1; | |
1917 | ||
1918 | /* Each group is 2 bytes and we must have at least 1. */ | |
1919 | if (!PACKET_as_length_prefixed_2(&suppgroups->data, | |
1920 | &supported_groups_list) | |
1921 | || PACKET_remaining(&supported_groups_list) == 0 | |
1922 | || (PACKET_remaining(&supported_groups_list) % 2) != 0) { | |
1923 | return 0; | |
1924 | } | |
1925 | ||
1926 | if (!s->hit | |
1927 | && !PACKET_memdup(&supported_groups_list, | |
1928 | &s->session->tlsext_supportedgroupslist, | |
1929 | &s->session->tlsext_supportedgroupslist_length)) { | |
1930 | return 0; | |
1931 | } | |
1932 | #endif | |
1933 | return 1; | |
1934 | } | |
1935 | ||
1936 | /* | |
1937 | * Checks a list of |groups| to determine if the |group_id| is in it. If it is | |
1938 | * and |checkallow| is 1 then additionally check if the group is allowed to be | |
94ed2c67 MC |
1939 | * used. Returns 1 if the group is in the list (and allowed if |checkallow| is |
1940 | * 1) or 0 otherwise. | |
ef7daaf9 MC |
1941 | */ |
1942 | static int check_in_list(SSL *s, unsigned int group_id, | |
1943 | const unsigned char *groups, size_t num_groups, | |
1944 | int checkallow) | |
1945 | { | |
1946 | size_t i; | |
1947 | ||
1948 | if (groups == NULL || num_groups == 0) | |
1949 | return 0; | |
1950 | ||
1951 | for (i = 0; i < num_groups; i++, groups += 2) { | |
1952 | unsigned int share_id = (groups[0] << 8) | (groups[1]); | |
94ed2c67 | 1953 | |
ef7daaf9 MC |
1954 | if (group_id == share_id |
1955 | && (!checkallow || tls_curve_allowed(s, groups, | |
1956 | SSL_SECOP_CURVE_CHECK))) { | |
1957 | break; | |
1958 | } | |
1959 | } | |
1960 | ||
94ed2c67 MC |
1961 | /* If i == num_groups then not in the list */ |
1962 | return i < num_groups; | |
1963 | } | |
1964 | ||
1965 | /* | |
1966 | * Process a key_share extension received in the ClientHello. |pkt| contains | |
1967 | * the raw PACKET data for the extension. Returns 1 on success or 0 on failure. | |
1968 | * If a failure occurs then |*al| is set to an appropriate alert value. | |
1969 | */ | |
1970 | static int process_key_share_ext(SSL *s, PACKET *pkt, int *al) | |
1971 | { | |
1972 | unsigned int group_id; | |
1973 | PACKET key_share_list, encoded_pt; | |
9a519880 MC |
1974 | const unsigned char *clntcurves, *srvrcurves; |
1975 | size_t clnt_num_curves, srvr_num_curves; | |
94ed2c67 MC |
1976 | int group_nid, found = 0; |
1977 | unsigned int curve_flags; | |
1978 | ||
1979 | /* Sanity check */ | |
1980 | if (s->s3->peer_tmp != NULL) { | |
1981 | *al = SSL_AD_INTERNAL_ERROR; | |
1982 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, ERR_R_INTERNAL_ERROR); | |
ef7daaf9 MC |
1983 | return 0; |
1984 | } | |
1985 | ||
94ed2c67 MC |
1986 | if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) { |
1987 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
1988 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, | |
1989 | SSL_R_LENGTH_MISMATCH); | |
1990 | return 0; | |
1991 | } | |
1992 | ||
9a519880 MC |
1993 | /* Get our list of supported curves */ |
1994 | if (!tls1_get_curvelist(s, 0, &srvrcurves, &srvr_num_curves)) { | |
1995 | *al = SSL_AD_INTERNAL_ERROR; | |
1996 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, | |
1997 | ERR_R_INTERNAL_ERROR); | |
1998 | return 0; | |
1999 | } | |
2000 | ||
2001 | /* Get the clients list of supported curves */ | |
2002 | if (!tls1_get_curvelist(s, 1, &clntcurves, &clnt_num_curves)) { | |
2003 | *al = SSL_AD_INTERNAL_ERROR; | |
2004 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, | |
2005 | ERR_R_INTERNAL_ERROR); | |
2006 | return 0; | |
2007 | } | |
2008 | ||
94ed2c67 MC |
2009 | while (PACKET_remaining(&key_share_list) > 0) { |
2010 | if (!PACKET_get_net_2(&key_share_list, &group_id) | |
2011 | || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt) | |
2012 | || PACKET_remaining(&encoded_pt) == 0) { | |
2013 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2014 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, | |
2015 | SSL_R_LENGTH_MISMATCH); | |
2016 | return 0; | |
2017 | } | |
2018 | ||
2019 | /* | |
2020 | * If we already found a suitable key_share we loop through the | |
2021 | * rest to verify the structure, but don't process them. | |
2022 | */ | |
2023 | if (found) | |
2024 | continue; | |
2025 | ||
2026 | /* Check if this share is in supported_groups sent from client */ | |
9a519880 | 2027 | if (!check_in_list(s, group_id, clntcurves, clnt_num_curves, 0)) { |
94ed2c67 MC |
2028 | *al = SSL_AD_HANDSHAKE_FAILURE; |
2029 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, | |
2030 | SSL_R_BAD_KEY_SHARE); | |
2031 | return 0; | |
2032 | } | |
2033 | ||
2034 | /* Check if this share is for a group we can use */ | |
9a519880 | 2035 | if (!check_in_list(s, group_id, srvrcurves, srvr_num_curves, 1)) { |
94ed2c67 MC |
2036 | /* Share not suitable */ |
2037 | continue; | |
2038 | } | |
2039 | ||
2040 | group_nid = tls1_ec_curve_id2nid(group_id, &curve_flags); | |
2041 | ||
2042 | if (group_nid == 0) { | |
2043 | *al = SSL_AD_INTERNAL_ERROR; | |
2044 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, | |
2045 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); | |
2046 | return 0; | |
2047 | } | |
2048 | ||
2049 | if ((curve_flags & TLS_CURVE_TYPE) == TLS_CURVE_CUSTOM) { | |
2050 | /* Can happen for some curves, e.g. X25519 */ | |
2051 | EVP_PKEY *key = EVP_PKEY_new(); | |
2052 | ||
2053 | if (key == NULL || !EVP_PKEY_set_type(key, group_nid)) { | |
2054 | *al = SSL_AD_INTERNAL_ERROR; | |
2055 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, ERR_R_EVP_LIB); | |
2056 | EVP_PKEY_free(key); | |
2057 | return 0; | |
2058 | } | |
2059 | s->s3->peer_tmp = key; | |
2060 | } else { | |
2061 | /* Set up EVP_PKEY with named curve as parameters */ | |
2062 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); | |
2063 | if (pctx == NULL | |
2064 | || EVP_PKEY_paramgen_init(pctx) <= 0 | |
2065 | || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, | |
2066 | group_nid) <= 0 | |
2067 | || EVP_PKEY_paramgen(pctx, &s->s3->peer_tmp) <= 0) { | |
2068 | *al = SSL_AD_INTERNAL_ERROR; | |
2069 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, ERR_R_EVP_LIB); | |
2070 | EVP_PKEY_CTX_free(pctx); | |
2071 | return 0; | |
2072 | } | |
2073 | EVP_PKEY_CTX_free(pctx); | |
2074 | pctx = NULL; | |
2075 | } | |
2076 | s->s3->group_id = group_id; | |
2077 | ||
2078 | if (!EVP_PKEY_set1_tls_encodedpoint(s->s3->peer_tmp, | |
2079 | PACKET_data(&encoded_pt), | |
2080 | PACKET_remaining(&encoded_pt))) { | |
2081 | *al = SSL_AD_DECODE_ERROR; | |
2082 | SSLerr(SSL_F_PROCESS_KEY_SHARE_EXT, SSL_R_BAD_ECPOINT); | |
2083 | return 0; | |
2084 | } | |
2085 | ||
2086 | found = 1; | |
2087 | } | |
2088 | ||
ef7daaf9 MC |
2089 | return 1; |
2090 | } | |
2091 | ||
06217867 | 2092 | /* |
64386324 MC |
2093 | * Loop through all remaining ClientHello extensions that we collected earlier |
2094 | * and haven't already processed. For each one parse it and update the SSL | |
2095 | * object as required. | |
06217867 EK |
2096 | * |
2097 | * Behaviour upon resumption is extension-specific. If the extension has no | |
2098 | * effect during resumption, it is parsed (to verify its format) but otherwise | |
2099 | * ignored. | |
2100 | * | |
64386324 | 2101 | * Returns 1 on success and 0 on failure. |
06217867 EK |
2102 | * Upon failure, sets |al| to the appropriate alert. |
2103 | */ | |
1ab3836b | 2104 | static int ssl_scan_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello, int *al) |
0f113f3e | 2105 | { |
1ab3836b | 2106 | size_t loop; |
0f113f3e MC |
2107 | int renegotiate_seen = 0; |
2108 | ||
06217867 | 2109 | *al = SSL_AD_DECODE_ERROR; |
0f113f3e MC |
2110 | s->servername_done = 0; |
2111 | s->tlsext_status_type = -1; | |
e481f9b9 | 2112 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 2113 | s->s3->next_proto_neg_seen = 0; |
e481f9b9 | 2114 | #endif |
0f113f3e | 2115 | |
b548a1f1 RS |
2116 | OPENSSL_free(s->s3->alpn_selected); |
2117 | s->s3->alpn_selected = NULL; | |
0351baae TS |
2118 | s->s3->alpn_selected_len = 0; |
2119 | OPENSSL_free(s->s3->alpn_proposed); | |
2120 | s->s3->alpn_proposed = NULL; | |
2121 | s->s3->alpn_proposed_len = 0; | |
0f113f3e | 2122 | |
e481f9b9 | 2123 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2124 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) |
1ab3836b | 2125 | ssl_check_for_safari(s, hello); |
a230b26e | 2126 | #endif /* !OPENSSL_NO_EC */ |
0f113f3e MC |
2127 | |
2128 | /* Clear any signature algorithms extension received */ | |
76106e60 DSH |
2129 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
2130 | s->s3->tmp.peer_sigalgs = NULL; | |
0f113f3e | 2131 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
0f113f3e | 2132 | |
e481f9b9 | 2133 | #ifndef OPENSSL_NO_SRP |
b548a1f1 RS |
2134 | OPENSSL_free(s->srp_ctx.login); |
2135 | s->srp_ctx.login = NULL; | |
e481f9b9 | 2136 | #endif |
0f113f3e MC |
2137 | |
2138 | s->srtp_profile = NULL; | |
2139 | ||
ef7daaf9 MC |
2140 | /* |
2141 | * We process the supported_groups extension first so that is done before | |
2142 | * we get to key_share which needs to use the information in it. | |
2143 | */ | |
2144 | if (!tls_process_supported_groups(s, hello)) { | |
2145 | *al = TLS1_AD_INTERNAL_ERROR; | |
2146 | return 0; | |
2147 | } | |
2148 | ||
06217867 EK |
2149 | /* |
2150 | * We parse all extensions to ensure the ClientHello is well-formed but, | |
2151 | * unless an extension specifies otherwise, we ignore extensions upon | |
2152 | * resumption. | |
2153 | */ | |
1ab3836b | 2154 | for (loop = 0; loop < hello->num_extensions; loop++) { |
b1b4b543 MC |
2155 | RAW_EXTENSION *currext = &hello->pre_proc_exts[loop]; |
2156 | ||
0f113f3e | 2157 | if (s->tlsext_debug_cb) |
b1b4b543 MC |
2158 | s->tlsext_debug_cb(s, 0, currext->type, |
2159 | PACKET_data(&currext->data), | |
2160 | PACKET_remaining(&currext->data), | |
06217867 | 2161 | s->tlsext_debug_arg); |
9ceb2426 | 2162 | |
b1b4b543 | 2163 | if (currext->type == TLSEXT_TYPE_renegotiate) { |
1ab3836b | 2164 | if (!ssl_parse_clienthello_renegotiate_ext(s, |
b1b4b543 | 2165 | &currext->data, al)) |
0f113f3e MC |
2166 | return 0; |
2167 | renegotiate_seen = 1; | |
2168 | } else if (s->version == SSL3_VERSION) { | |
2169 | } | |
1d97c843 TH |
2170 | /*- |
2171 | * The servername extension is treated as follows: | |
2172 | * | |
2173 | * - Only the hostname type is supported with a maximum length of 255. | |
2174 | * - The servername is rejected if too long or if it contains zeros, | |
2175 | * in which case an fatal alert is generated. | |
2176 | * - The servername field is maintained together with the session cache. | |
2177 | * - When a session is resumed, the servername call back invoked in order | |
0f113f3e MC |
2178 | * to allow the application to position itself to the right context. |
2179 | * - The servername is acknowledged if it is new for a session or when | |
2180 | * it is identical to a previously used for the same session. | |
1d97c843 TH |
2181 | * Applications can control the behaviour. They can at any time |
2182 | * set a 'desirable' servername for a new SSL object. This can be the | |
2183 | * case for example with HTTPS when a Host: header field is received and | |
2184 | * a renegotiation is requested. In this case, a possible servername | |
2185 | * presented in the new client hello is only acknowledged if it matches | |
0f113f3e | 2186 | * the value of the Host: field. |
1d97c843 | 2187 | * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
0f113f3e MC |
2188 | * if they provide for changing an explicit servername context for the |
2189 | * session, i.e. when the session has been established with a servername | |
2190 | * extension. | |
2191 | * - On session reconnect, the servername extension may be absent. | |
1d97c843 | 2192 | * |
0f113f3e | 2193 | */ |
ed3883d2 | 2194 | |
b1b4b543 | 2195 | else if (currext->type == TLSEXT_TYPE_server_name) { |
9ceb2426 | 2196 | unsigned int servname_type; |
06217867 EK |
2197 | PACKET sni, hostname; |
2198 | ||
b1b4b543 | 2199 | if (!PACKET_as_length_prefixed_2(&currext->data, &sni) |
06217867 EK |
2200 | /* ServerNameList must be at least 1 byte long. */ |
2201 | || PACKET_remaining(&sni) == 0) { | |
2202 | return 0; | |
0f113f3e | 2203 | } |
6f017a8f | 2204 | |
06217867 EK |
2205 | /* |
2206 | * Although the server_name extension was intended to be | |
2207 | * extensible to new name types, RFC 4366 defined the | |
8483a003 | 2208 | * syntax inextensibility and OpenSSL 1.0.x parses it as |
06217867 EK |
2209 | * such. |
2210 | * RFC 6066 corrected the mistake but adding new name types | |
2211 | * is nevertheless no longer feasible, so act as if no other | |
2212 | * SNI types can exist, to simplify parsing. | |
2213 | * | |
2214 | * Also note that the RFC permits only one SNI value per type, | |
2215 | * i.e., we can only have a single hostname. | |
2216 | */ | |
2217 | if (!PACKET_get_1(&sni, &servname_type) | |
2218 | || servname_type != TLSEXT_NAMETYPE_host_name | |
2219 | || !PACKET_as_length_prefixed_2(&sni, &hostname)) { | |
2220 | return 0; | |
2221 | } | |
2222 | ||
2223 | if (!s->hit) { | |
2224 | if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) { | |
2225 | *al = TLS1_AD_UNRECOGNIZED_NAME; | |
2226 | return 0; | |
2227 | } | |
2228 | ||
2229 | if (PACKET_contains_zero_byte(&hostname)) { | |
2230 | *al = TLS1_AD_UNRECOGNIZED_NAME; | |
2231 | return 0; | |
2232 | } | |
2233 | ||
2234 | if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) { | |
2235 | *al = TLS1_AD_INTERNAL_ERROR; | |
2236 | return 0; | |
2237 | } | |
2238 | ||
2239 | s->servername_done = 1; | |
2240 | } else { | |
2241 | /* | |
2242 | * TODO(openssl-team): if the SNI doesn't match, we MUST | |
2243 | * fall back to a full handshake. | |
2244 | */ | |
2245 | s->servername_done = s->session->tlsext_hostname | |
2246 | && PACKET_equal(&hostname, s->session->tlsext_hostname, | |
2247 | strlen(s->session->tlsext_hostname)); | |
2248 | } | |
0f113f3e | 2249 | } |
e481f9b9 | 2250 | #ifndef OPENSSL_NO_SRP |
b1b4b543 | 2251 | else if (currext->type == TLSEXT_TYPE_srp) { |
06217867 EK |
2252 | PACKET srp_I; |
2253 | ||
b1b4b543 | 2254 | if (!PACKET_as_length_prefixed_1(&currext->data, &srp_I)) |
06217867 EK |
2255 | return 0; |
2256 | ||
2257 | if (PACKET_contains_zero_byte(&srp_I)) | |
2258 | return 0; | |
2259 | ||
2260 | /* | |
2261 | * TODO(openssl-team): currently, we re-authenticate the user | |
2262 | * upon resumption. Instead, we MUST ignore the login. | |
2263 | */ | |
2264 | if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) { | |
2265 | *al = TLS1_AD_INTERNAL_ERROR; | |
2266 | return 0; | |
2267 | } | |
0f113f3e | 2268 | } |
e481f9b9 | 2269 | #endif |
0f113f3e | 2270 | |
e481f9b9 | 2271 | #ifndef OPENSSL_NO_EC |
b1b4b543 | 2272 | else if (currext->type == TLSEXT_TYPE_ec_point_formats) { |
06217867 | 2273 | PACKET ec_point_format_list; |
0f113f3e | 2274 | |
b1b4b543 | 2275 | if (!PACKET_as_length_prefixed_1(&currext->data, |
1ab3836b | 2276 | &ec_point_format_list) |
06217867 EK |
2277 | || PACKET_remaining(&ec_point_format_list) == 0) { |
2278 | return 0; | |
2279 | } | |
9ceb2426 | 2280 | |
0f113f3e | 2281 | if (!s->hit) { |
06217867 EK |
2282 | if (!PACKET_memdup(&ec_point_format_list, |
2283 | &s->session->tlsext_ecpointformatlist, | |
a230b26e EK |
2284 | &s-> |
2285 | session->tlsext_ecpointformatlist_length)) { | |
0f113f3e MC |
2286 | *al = TLS1_AD_INTERNAL_ERROR; |
2287 | return 0; | |
2288 | } | |
0f113f3e | 2289 | } |
0f113f3e | 2290 | } |
a230b26e | 2291 | #endif /* OPENSSL_NO_EC */ |
9362c93e MC |
2292 | else if (currext->type == TLSEXT_TYPE_session_ticket |
2293 | && !SSL_IS_TLS13(s)) { | |
06217867 | 2294 | if (s->tls_session_ticket_ext_cb && |
1ab3836b | 2295 | !s->tls_session_ticket_ext_cb(s, |
b1b4b543 MC |
2296 | PACKET_data(&currext->data), |
2297 | PACKET_remaining(&currext->data), | |
1ab3836b | 2298 | s->tls_session_ticket_ext_cb_arg)) { |
0f113f3e MC |
2299 | *al = TLS1_AD_INTERNAL_ERROR; |
2300 | return 0; | |
2301 | } | |
b1b4b543 | 2302 | } else if (currext->type == TLSEXT_TYPE_signature_algorithms) { |
06217867 EK |
2303 | PACKET supported_sig_algs; |
2304 | ||
b1b4b543 | 2305 | if (!PACKET_as_length_prefixed_2(&currext->data, |
1ab3836b | 2306 | &supported_sig_algs) |
06217867 EK |
2307 | || (PACKET_remaining(&supported_sig_algs) % 2) != 0 |
2308 | || PACKET_remaining(&supported_sig_algs) == 0) { | |
2309 | return 0; | |
2310 | } | |
2311 | ||
a230b26e | 2312 | if (!s->hit) { |
06217867 EK |
2313 | if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs), |
2314 | PACKET_remaining(&supported_sig_algs))) { | |
2315 | return 0; | |
2316 | } | |
9ceb2426 | 2317 | } |
b1b4b543 MC |
2318 | } else if (currext->type == TLSEXT_TYPE_status_request) { |
2319 | if (!PACKET_get_1(&currext->data, | |
06217867 EK |
2320 | (unsigned int *)&s->tlsext_status_type)) { |
2321 | return 0; | |
2322 | } | |
3e41ac35 | 2323 | #ifndef OPENSSL_NO_OCSP |
0f113f3e | 2324 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { |
3e41ac35 | 2325 | const unsigned char *ext_data; |
06217867 | 2326 | PACKET responder_id_list, exts; |
a230b26e | 2327 | if (!PACKET_get_length_prefixed_2 |
b1b4b543 | 2328 | (&currext->data, &responder_id_list)) |
06217867 EK |
2329 | return 0; |
2330 | ||
e408c09b MC |
2331 | /* |
2332 | * We remove any OCSP_RESPIDs from a previous handshake | |
2333 | * to prevent unbounded memory growth - CVE-2016-6304 | |
2334 | */ | |
2335 | sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, | |
2336 | OCSP_RESPID_free); | |
2337 | if (PACKET_remaining(&responder_id_list) > 0) { | |
2338 | s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null(); | |
2339 | if (s->tlsext_ocsp_ids == NULL) { | |
2340 | *al = SSL_AD_INTERNAL_ERROR; | |
2341 | return 0; | |
2342 | } | |
2343 | } else { | |
2344 | s->tlsext_ocsp_ids = NULL; | |
2345 | } | |
2346 | ||
06217867 | 2347 | while (PACKET_remaining(&responder_id_list) > 0) { |
0f113f3e | 2348 | OCSP_RESPID *id; |
06217867 EK |
2349 | PACKET responder_id; |
2350 | const unsigned char *id_data; | |
9ceb2426 | 2351 | |
06217867 EK |
2352 | if (!PACKET_get_length_prefixed_2(&responder_id_list, |
2353 | &responder_id) | |
2354 | || PACKET_remaining(&responder_id) == 0) { | |
2355 | return 0; | |
9ceb2426 | 2356 | } |
06217867 | 2357 | |
06217867 | 2358 | id_data = PACKET_data(&responder_id); |
348240c6 | 2359 | /* TODO(size_t): Convert d2i_* to size_t */ |
06217867 | 2360 | id = d2i_OCSP_RESPID(NULL, &id_data, |
348240c6 | 2361 | (int)PACKET_remaining(&responder_id)); |
06217867 EK |
2362 | if (id == NULL) |
2363 | return 0; | |
2364 | ||
2365 | if (id_data != PACKET_end(&responder_id)) { | |
0f113f3e | 2366 | OCSP_RESPID_free(id); |
0f113f3e MC |
2367 | return 0; |
2368 | } | |
06217867 | 2369 | |
0f113f3e MC |
2370 | if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) { |
2371 | OCSP_RESPID_free(id); | |
2372 | *al = SSL_AD_INTERNAL_ERROR; | |
2373 | return 0; | |
2374 | } | |
2375 | } | |
4817504d | 2376 | |
0f113f3e | 2377 | /* Read in request_extensions */ |
1ab3836b | 2378 | if (!PACKET_as_length_prefixed_2( |
b1b4b543 | 2379 | &currext->data, &exts)) |
06217867 EK |
2380 | return 0; |
2381 | ||
2382 | if (PACKET_remaining(&exts) > 0) { | |
2383 | ext_data = PACKET_data(&exts); | |
222561fe RS |
2384 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, |
2385 | X509_EXTENSION_free); | |
0f113f3e | 2386 | s->tlsext_ocsp_exts = |
06217867 | 2387 | d2i_X509_EXTENSIONS(NULL, &ext_data, |
348240c6 | 2388 | (int)PACKET_remaining(&exts)); |
06217867 EK |
2389 | if (s->tlsext_ocsp_exts == NULL |
2390 | || ext_data != PACKET_end(&exts)) { | |
2391 | return 0; | |
2392 | } | |
0f113f3e | 2393 | } |
3e41ac35 MC |
2394 | } else |
2395 | #endif | |
2396 | { | |
2397 | /* | |
2398 | * We don't know what to do with any other type so ignore it. | |
2399 | */ | |
0f113f3e | 2400 | s->tlsext_status_type = -1; |
06217867 | 2401 | } |
0f113f3e | 2402 | } |
e481f9b9 | 2403 | #ifndef OPENSSL_NO_NEXTPROTONEG |
b1b4b543 | 2404 | else if (currext->type == TLSEXT_TYPE_next_proto_neg |
1ab3836b | 2405 | && s->s3->tmp.finish_md_len == 0) { |
50e735f9 MC |
2406 | /*- |
2407 | * We shouldn't accept this extension on a | |
2408 | * renegotiation. | |
2409 | * | |
2410 | * s->new_session will be set on renegotiation, but we | |
2411 | * probably shouldn't rely that it couldn't be set on | |
8483a003 | 2412 | * the initial renegotiation too in certain cases (when |
50e735f9 MC |
2413 | * there's some other reason to disallow resuming an |
2414 | * earlier session -- the current code won't be doing | |
2415 | * anything like that, but this might change). | |
2416 | * | |
2417 | * A valid sign that there's been a previous handshake | |
2418 | * in this connection is if s->s3->tmp.finish_md_len > | |
2419 | * 0. (We are talking about a check that will happen | |
2420 | * in the Hello protocol round, well before a new | |
2421 | * Finished message could have been computed.) | |
2422 | */ | |
0f113f3e MC |
2423 | s->s3->next_proto_neg_seen = 1; |
2424 | } | |
e481f9b9 | 2425 | #endif |
0f113f3e | 2426 | |
b1b4b543 | 2427 | else if (currext->type |
1ab3836b MC |
2428 | == TLSEXT_TYPE_application_layer_protocol_negotiation |
2429 | && s->s3->tmp.finish_md_len == 0) { | |
2430 | if (!tls1_alpn_handle_client_hello(s, | |
b1b4b543 | 2431 | &currext->data, al)) |
0f113f3e | 2432 | return 0; |
0f113f3e | 2433 | } |
5e3ff62c | 2434 | |
0f113f3e | 2435 | /* session ticket processed earlier */ |
e481f9b9 | 2436 | #ifndef OPENSSL_NO_SRTP |
0f113f3e | 2437 | else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s) |
b1b4b543 | 2438 | && currext->type == TLSEXT_TYPE_use_srtp) { |
1ab3836b | 2439 | if (ssl_parse_clienthello_use_srtp_ext(s, |
b1b4b543 | 2440 | &currext->data, al)) |
0f113f3e MC |
2441 | return 0; |
2442 | } | |
e481f9b9 | 2443 | #endif |
b1b4b543 | 2444 | else if (currext->type == TLSEXT_TYPE_encrypt_then_mac |
b1834ad7 | 2445 | && !(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) { |
0f113f3e | 2446 | s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; |
b1834ad7 | 2447 | } else if (currext->type == TLSEXT_TYPE_key_share |
94ed2c67 MC |
2448 | && SSL_IS_TLS13(s) && !s->hit |
2449 | && !process_key_share_ext(s, &currext->data, al)) { | |
2450 | return 0; | |
b1834ad7 | 2451 | } |
e7f0d921 DSH |
2452 | /* |
2453 | * Note: extended master secret extension handled in | |
1ab3836b | 2454 | * tls_check_client_ems_support() |
e7f0d921 DSH |
2455 | */ |
2456 | ||
0f113f3e MC |
2457 | /* |
2458 | * If this ClientHello extension was unhandled and this is a | |
2459 | * nonresumed connection, check whether the extension is a custom | |
2460 | * TLS Extension (has a custom_srv_ext_record), and if so call the | |
2461 | * callback and record the extension number so that an appropriate | |
2462 | * ServerHello may be later returned. | |
2463 | */ | |
2464 | else if (!s->hit) { | |
b1b4b543 MC |
2465 | if (custom_ext_parse(s, 1, currext->type, |
2466 | PACKET_data(&currext->data), | |
2467 | PACKET_remaining(&currext->data), al) <= 0) | |
0f113f3e MC |
2468 | return 0; |
2469 | } | |
0f113f3e | 2470 | } |
6f017a8f | 2471 | |
0f113f3e MC |
2472 | /* Need RI if renegotiating */ |
2473 | ||
2474 | if (!renegotiate_seen && s->renegotiate && | |
2475 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | |
2476 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2477 | SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT, | |
2478 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | |
2479 | return 0; | |
2480 | } | |
2481 | ||
06217867 EK |
2482 | /* |
2483 | * This function currently has no state to clean up, so it returns directly. | |
2484 | * If parsing fails at any point, the function returns early. | |
2485 | * The SSL object may be left with partial data from extensions, but it must | |
2486 | * then no longer be used, and clearing it up will free the leftovers. | |
2487 | */ | |
0f113f3e MC |
2488 | return 1; |
2489 | } | |
2490 | ||
1ab3836b | 2491 | int ssl_parse_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello) |
0f113f3e MC |
2492 | { |
2493 | int al = -1; | |
2494 | custom_ext_init(&s->cert->srv_ext); | |
1ab3836b | 2495 | if (ssl_scan_clienthello_tlsext(s, hello, &al) <= 0) { |
0f113f3e MC |
2496 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
2497 | return 0; | |
2498 | } | |
0f113f3e MC |
2499 | if (ssl_check_clienthello_tlsext_early(s) <= 0) { |
2500 | SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT); | |
2501 | return 0; | |
2502 | } | |
2503 | return 1; | |
2504 | } | |
2505 | ||
e481f9b9 | 2506 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
2507 | /* |
2508 | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No | |
2509 | * elements of zero length are allowed and the set of elements must exactly | |
2510 | * fill the length of the block. | |
2511 | */ | |
50932c4a | 2512 | static char ssl_next_proto_validate(PACKET *pkt) |
0f113f3e | 2513 | { |
b04f9479 | 2514 | PACKET tmp_protocol; |
0f113f3e | 2515 | |
50932c4a | 2516 | while (PACKET_remaining(pkt)) { |
b04f9479 | 2517 | if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol) |
a230b26e | 2518 | || PACKET_remaining(&tmp_protocol) == 0) |
0f113f3e | 2519 | return 0; |
0f113f3e MC |
2520 | } |
2521 | ||
50932c4a | 2522 | return 1; |
0f113f3e | 2523 | } |
e481f9b9 | 2524 | #endif |
0f113f3e | 2525 | |
50932c4a | 2526 | static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al) |
0f113f3e | 2527 | { |
50932c4a | 2528 | unsigned int length, type, size; |
0f113f3e MC |
2529 | int tlsext_servername = 0; |
2530 | int renegotiate_seen = 0; | |
2531 | ||
e481f9b9 | 2532 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e | 2533 | s->s3->next_proto_neg_seen = 0; |
e481f9b9 | 2534 | #endif |
0f113f3e MC |
2535 | s->tlsext_ticket_expected = 0; |
2536 | ||
b548a1f1 RS |
2537 | OPENSSL_free(s->s3->alpn_selected); |
2538 | s->s3->alpn_selected = NULL; | |
0f113f3e | 2539 | |
0f113f3e | 2540 | s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC; |
0f113f3e | 2541 | |
e7f0d921 DSH |
2542 | s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; |
2543 | ||
50932c4a | 2544 | if (!PACKET_get_net_2(pkt, &length)) |
0f113f3e MC |
2545 | goto ri_check; |
2546 | ||
50932c4a | 2547 | if (PACKET_remaining(pkt) != length) { |
0f113f3e MC |
2548 | *al = SSL_AD_DECODE_ERROR; |
2549 | return 0; | |
2550 | } | |
2551 | ||
aa474d1f EK |
2552 | if (!tls1_check_duplicate_extensions(pkt)) { |
2553 | *al = SSL_AD_DECODE_ERROR; | |
2554 | return 0; | |
2555 | } | |
2556 | ||
50932c4a | 2557 | while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) { |
b6981744 | 2558 | const unsigned char *data; |
50932c4a | 2559 | PACKET spkt; |
0f113f3e | 2560 | |
50932c4a | 2561 | if (!PACKET_get_sub_packet(pkt, &spkt, size) |
a230b26e | 2562 | || !PACKET_peek_bytes(&spkt, &data, size)) |
0f113f3e MC |
2563 | goto ri_check; |
2564 | ||
2565 | if (s->tlsext_debug_cb) | |
2566 | s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg); | |
2567 | ||
2568 | if (type == TLSEXT_TYPE_renegotiate) { | |
50932c4a | 2569 | if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al)) |
0f113f3e MC |
2570 | return 0; |
2571 | renegotiate_seen = 1; | |
2572 | } else if (s->version == SSL3_VERSION) { | |
2573 | } else if (type == TLSEXT_TYPE_server_name) { | |
2574 | if (s->tlsext_hostname == NULL || size > 0) { | |
2575 | *al = TLS1_AD_UNRECOGNIZED_NAME; | |
2576 | return 0; | |
2577 | } | |
2578 | tlsext_servername = 1; | |
2579 | } | |
e481f9b9 | 2580 | #ifndef OPENSSL_NO_EC |
0f113f3e | 2581 | else if (type == TLSEXT_TYPE_ec_point_formats) { |
50932c4a MC |
2582 | unsigned int ecpointformatlist_length; |
2583 | if (!PACKET_get_1(&spkt, &ecpointformatlist_length) | |
a230b26e | 2584 | || ecpointformatlist_length != size - 1) { |
0f113f3e MC |
2585 | *al = TLS1_AD_DECODE_ERROR; |
2586 | return 0; | |
2587 | } | |
2588 | if (!s->hit) { | |
2589 | s->session->tlsext_ecpointformatlist_length = 0; | |
b548a1f1 | 2590 | OPENSSL_free(s->session->tlsext_ecpointformatlist); |
0f113f3e MC |
2591 | if ((s->session->tlsext_ecpointformatlist = |
2592 | OPENSSL_malloc(ecpointformatlist_length)) == NULL) { | |
2593 | *al = TLS1_AD_INTERNAL_ERROR; | |
2594 | return 0; | |
2595 | } | |
2596 | s->session->tlsext_ecpointformatlist_length = | |
2597 | ecpointformatlist_length; | |
50932c4a MC |
2598 | if (!PACKET_copy_bytes(&spkt, |
2599 | s->session->tlsext_ecpointformatlist, | |
2600 | ecpointformatlist_length)) { | |
2601 | *al = TLS1_AD_DECODE_ERROR; | |
2602 | return 0; | |
2603 | } | |
2604 | ||
0f113f3e | 2605 | } |
0f113f3e | 2606 | } |
a230b26e | 2607 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
2608 | |
2609 | else if (type == TLSEXT_TYPE_session_ticket) { | |
2610 | if (s->tls_session_ticket_ext_cb && | |
2611 | !s->tls_session_ticket_ext_cb(s, data, size, | |
2612 | s->tls_session_ticket_ext_cb_arg)) | |
2613 | { | |
2614 | *al = TLS1_AD_INTERNAL_ERROR; | |
2615 | return 0; | |
2616 | } | |
2617 | if (!tls_use_ticket(s) || (size > 0)) { | |
2618 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | |
2619 | return 0; | |
2620 | } | |
2621 | s->tlsext_ticket_expected = 1; | |
a230b26e | 2622 | } else if (type == TLSEXT_TYPE_status_request) { |
0f113f3e MC |
2623 | /* |
2624 | * MUST be empty and only sent if we've requested a status | |
2625 | * request message. | |
2626 | */ | |
2627 | if ((s->tlsext_status_type == -1) || (size > 0)) { | |
2628 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | |
2629 | return 0; | |
2630 | } | |
2631 | /* Set flag to expect CertificateStatus message */ | |
2632 | s->tlsext_status_expected = 1; | |
2633 | } | |
ed29e82a RP |
2634 | #ifndef OPENSSL_NO_CT |
2635 | /* | |
2636 | * Only take it if we asked for it - i.e if there is no CT validation | |
2637 | * callback set, then a custom extension MAY be processing it, so we | |
2638 | * need to let control continue to flow to that. | |
2639 | */ | |
2640 | else if (type == TLSEXT_TYPE_signed_certificate_timestamp && | |
2641 | s->ct_validation_callback != NULL) { | |
2642 | /* Simply copy it off for later processing */ | |
2643 | if (s->tlsext_scts != NULL) { | |
2644 | OPENSSL_free(s->tlsext_scts); | |
2645 | s->tlsext_scts = NULL; | |
2646 | } | |
2647 | s->tlsext_scts_len = size; | |
2648 | if (size > 0) { | |
2649 | s->tlsext_scts = OPENSSL_malloc(size); | |
2650 | if (s->tlsext_scts == NULL) { | |
2651 | *al = TLS1_AD_INTERNAL_ERROR; | |
2652 | return 0; | |
2653 | } | |
2654 | memcpy(s->tlsext_scts, data, size); | |
2655 | } | |
2656 | } | |
2657 | #endif | |
e481f9b9 | 2658 | #ifndef OPENSSL_NO_NEXTPROTONEG |
0f113f3e MC |
2659 | else if (type == TLSEXT_TYPE_next_proto_neg && |
2660 | s->s3->tmp.finish_md_len == 0) { | |
2661 | unsigned char *selected; | |
2662 | unsigned char selected_len; | |
0f113f3e MC |
2663 | /* We must have requested it. */ |
2664 | if (s->ctx->next_proto_select_cb == NULL) { | |
2665 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | |
2666 | return 0; | |
2667 | } | |
2668 | /* The data must be valid */ | |
50932c4a | 2669 | if (!ssl_next_proto_validate(&spkt)) { |
0f113f3e MC |
2670 | *al = TLS1_AD_DECODE_ERROR; |
2671 | return 0; | |
2672 | } | |
a230b26e EK |
2673 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, |
2674 | size, | |
2675 | s-> | |
2676 | ctx->next_proto_select_cb_arg) != | |
0f113f3e MC |
2677 | SSL_TLSEXT_ERR_OK) { |
2678 | *al = TLS1_AD_INTERNAL_ERROR; | |
2679 | return 0; | |
2680 | } | |
c31dbed7 MC |
2681 | /* |
2682 | * Could be non-NULL if server has sent multiple NPN extensions in | |
2683 | * a single Serverhello | |
2684 | */ | |
2685 | OPENSSL_free(s->next_proto_negotiated); | |
0f113f3e | 2686 | s->next_proto_negotiated = OPENSSL_malloc(selected_len); |
a71edf3b | 2687 | if (s->next_proto_negotiated == NULL) { |
0f113f3e MC |
2688 | *al = TLS1_AD_INTERNAL_ERROR; |
2689 | return 0; | |
2690 | } | |
2691 | memcpy(s->next_proto_negotiated, selected, selected_len); | |
2692 | s->next_proto_negotiated_len = selected_len; | |
2693 | s->s3->next_proto_neg_seen = 1; | |
2694 | } | |
e481f9b9 | 2695 | #endif |
0f113f3e MC |
2696 | |
2697 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) { | |
2698 | unsigned len; | |
0f113f3e | 2699 | /* We must have requested it. */ |
817cd0d5 | 2700 | if (!s->s3->alpn_sent) { |
0f113f3e MC |
2701 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
2702 | return 0; | |
2703 | } | |
50e735f9 MC |
2704 | /*- |
2705 | * The extension data consists of: | |
2706 | * uint16 list_length | |
2707 | * uint8 proto_length; | |
2708 | * uint8 proto[proto_length]; | |
2709 | */ | |
50932c4a | 2710 | if (!PACKET_get_net_2(&spkt, &len) |
a230b26e EK |
2711 | || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len) |
2712 | || PACKET_remaining(&spkt) != len) { | |
0f113f3e MC |
2713 | *al = TLS1_AD_DECODE_ERROR; |
2714 | return 0; | |
2715 | } | |
b548a1f1 | 2716 | OPENSSL_free(s->s3->alpn_selected); |
0f113f3e | 2717 | s->s3->alpn_selected = OPENSSL_malloc(len); |
a71edf3b | 2718 | if (s->s3->alpn_selected == NULL) { |
0f113f3e MC |
2719 | *al = TLS1_AD_INTERNAL_ERROR; |
2720 | return 0; | |
2721 | } | |
50932c4a MC |
2722 | if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) { |
2723 | *al = TLS1_AD_DECODE_ERROR; | |
2724 | return 0; | |
2725 | } | |
0f113f3e MC |
2726 | s->s3->alpn_selected_len = len; |
2727 | } | |
e481f9b9 | 2728 | #ifndef OPENSSL_NO_SRTP |
0f113f3e | 2729 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { |
50932c4a | 2730 | if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al)) |
0f113f3e MC |
2731 | return 0; |
2732 | } | |
e481f9b9 | 2733 | #endif |
0f113f3e MC |
2734 | else if (type == TLSEXT_TYPE_encrypt_then_mac) { |
2735 | /* Ignore if inappropriate ciphersuite */ | |
cde6145b DW |
2736 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) && |
2737 | s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD | |
0f113f3e MC |
2738 | && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4) |
2739 | s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC; | |
0f1e51ea | 2740 | } else if (type == TLSEXT_TYPE_extended_master_secret && |
94ed2c67 | 2741 | (SSL_IS_DTLS(s) || !SSL_IS_TLS13(s))) { |
e7f0d921 | 2742 | s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
ddc06b35 DSH |
2743 | if (!s->hit) |
2744 | s->session->flags |= SSL_SESS_FLAG_EXTMS; | |
d7c42d71 | 2745 | } else if (type == TLSEXT_TYPE_key_share |
94ed2c67 | 2746 | && SSL_IS_TLS13(s)) { |
d7c42d71 MC |
2747 | unsigned int group_id; |
2748 | PACKET encoded_pt; | |
2749 | EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL; | |
2750 | ||
2751 | /* Sanity check */ | |
2752 | if (ckey == NULL) { | |
2753 | *al = SSL_AD_INTERNAL_ERROR; | |
2754 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
2755 | return 0; | |
2756 | } | |
2757 | ||
2758 | if (!PACKET_get_net_2(&spkt, &group_id)) { | |
2759 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2760 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, | |
2761 | SSL_R_LENGTH_MISMATCH); | |
2762 | return 0; | |
2763 | } | |
2764 | ||
2765 | if (group_id != s->s3->group_id) { | |
2766 | /* | |
2767 | * This isn't for the group that we sent in the original | |
2768 | * key_share! | |
2769 | */ | |
2770 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2771 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, | |
2772 | SSL_R_BAD_KEY_SHARE); | |
2773 | return 0; | |
2774 | } | |
2775 | ||
323f212a MC |
2776 | if (!PACKET_as_length_prefixed_2(&spkt, &encoded_pt) |
2777 | || PACKET_remaining(&encoded_pt) == 0) { | |
d7c42d71 MC |
2778 | *al = SSL_AD_DECODE_ERROR; |
2779 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, | |
2780 | SSL_R_LENGTH_MISMATCH); | |
2781 | return 0; | |
2782 | } | |
2783 | ||
94ed2c67 | 2784 | skey = ssl_generate_pkey(ckey); |
884a790e MC |
2785 | if (skey == NULL) { |
2786 | *al = SSL_AD_INTERNAL_ERROR; | |
2787 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, ERR_R_MALLOC_FAILURE); | |
2788 | return 0; | |
2789 | } | |
d7c42d71 MC |
2790 | if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt), |
2791 | PACKET_remaining(&encoded_pt))) { | |
2792 | *al = SSL_AD_DECODE_ERROR; | |
2793 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_BAD_ECPOINT); | |
2794 | return 0; | |
2795 | } | |
2796 | ||
0f1e51ea MC |
2797 | if (ssl_derive(s, ckey, skey, 1) == 0) { |
2798 | *al = SSL_AD_INTERNAL_ERROR; | |
2799 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | |
2800 | EVP_PKEY_free(skey); | |
2801 | return 0; | |
2802 | } | |
d7c42d71 | 2803 | EVP_PKEY_free(skey); |
0f113f3e MC |
2804 | /* |
2805 | * If this extension type was not otherwise handled, but matches a | |
2806 | * custom_cli_ext_record, then send it to the c callback | |
2807 | */ | |
d7c42d71 | 2808 | } else if (custom_ext_parse(s, 0, type, data, size, al) <= 0) |
0f113f3e | 2809 | return 0; |
0f113f3e MC |
2810 | } |
2811 | ||
50932c4a | 2812 | if (PACKET_remaining(pkt) != 0) { |
0f113f3e MC |
2813 | *al = SSL_AD_DECODE_ERROR; |
2814 | return 0; | |
2815 | } | |
2816 | ||
2817 | if (!s->hit && tlsext_servername == 1) { | |
2818 | if (s->tlsext_hostname) { | |
2819 | if (s->session->tlsext_hostname == NULL) { | |
a230b26e EK |
2820 | s->session->tlsext_hostname = |
2821 | OPENSSL_strdup(s->tlsext_hostname); | |
0f113f3e MC |
2822 | if (!s->session->tlsext_hostname) { |
2823 | *al = SSL_AD_UNRECOGNIZED_NAME; | |
2824 | return 0; | |
2825 | } | |
2826 | } else { | |
2827 | *al = SSL_AD_DECODE_ERROR; | |
2828 | return 0; | |
2829 | } | |
2830 | } | |
2831 | } | |
2832 | ||
0f113f3e MC |
2833 | ri_check: |
2834 | ||
2835 | /* | |
2836 | * Determine if we need to see RI. Strictly speaking if we want to avoid | |
2837 | * an attack we should *always* see RI even on initial server hello | |
2838 | * because the client doesn't see any renegotiation during an attack. | |
2839 | * However this would mean we could not connect to any server which | |
e7653f3b | 2840 | * doesn't support RI so for the immediate future tolerate RI absence |
0f113f3e MC |
2841 | */ |
2842 | if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) | |
2843 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | |
2844 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2845 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, | |
2846 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | |
2847 | return 0; | |
2848 | } | |
2849 | ||
e7f0d921 DSH |
2850 | if (s->hit) { |
2851 | /* | |
2852 | * Check extended master secret extension is consistent with | |
2853 | * original session. | |
2854 | */ | |
2855 | if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) != | |
2856 | !(s->session->flags & SSL_SESS_FLAG_EXTMS)) { | |
2857 | *al = SSL_AD_HANDSHAKE_FAILURE; | |
2858 | SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS); | |
2859 | return 0; | |
a230b26e | 2860 | } |
e7f0d921 DSH |
2861 | } |
2862 | ||
0f113f3e MC |
2863 | return 1; |
2864 | } | |
b2172f4f | 2865 | |
36ca4ba6 | 2866 | int ssl_prepare_clienthello_tlsext(SSL *s) |
0f113f3e | 2867 | { |
817cd0d5 | 2868 | s->s3->alpn_sent = 0; |
0f113f3e MC |
2869 | return 1; |
2870 | } | |
36ca4ba6 BM |
2871 | |
2872 | int ssl_prepare_serverhello_tlsext(SSL *s) | |
0f113f3e MC |
2873 | { |
2874 | return 1; | |
2875 | } | |
36ca4ba6 | 2876 | |
2daceb03 | 2877 | static int ssl_check_clienthello_tlsext_early(SSL *s) |
0f113f3e MC |
2878 | { |
2879 | int ret = SSL_TLSEXT_ERR_NOACK; | |
2880 | int al = SSL_AD_UNRECOGNIZED_NAME; | |
2881 | ||
e481f9b9 | 2882 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
2883 | /* |
2884 | * The handling of the ECPointFormats extension is done elsewhere, namely | |
2885 | * in ssl3_choose_cipher in s3_lib.c. | |
2886 | */ | |
2887 | /* | |
2888 | * The handling of the EllipticCurves extension is done elsewhere, namely | |
2889 | * in ssl3_choose_cipher in s3_lib.c. | |
2890 | */ | |
e481f9b9 | 2891 | #endif |
0f113f3e MC |
2892 | |
2893 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | |
2894 | ret = | |
2895 | s->ctx->tlsext_servername_callback(s, &al, | |
2896 | s->ctx->tlsext_servername_arg); | |
2897 | else if (s->initial_ctx != NULL | |
2898 | && s->initial_ctx->tlsext_servername_callback != 0) | |
2899 | ret = | |
2900 | s->initial_ctx->tlsext_servername_callback(s, &al, | |
2901 | s-> | |
2902 | initial_ctx->tlsext_servername_arg); | |
2903 | ||
0f113f3e MC |
2904 | switch (ret) { |
2905 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
2906 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2907 | return -1; | |
2908 | ||
2909 | case SSL_TLSEXT_ERR_ALERT_WARNING: | |
2910 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | |
2911 | return 1; | |
2912 | ||
2913 | case SSL_TLSEXT_ERR_NOACK: | |
2914 | s->servername_done = 0; | |
2915 | default: | |
2916 | return 1; | |
2917 | } | |
2918 | } | |
a230b26e | 2919 | |
d376e57d | 2920 | /* Initialise digests to default values */ |
a0f63828 | 2921 | void ssl_set_default_md(SSL *s) |
d376e57d DSH |
2922 | { |
2923 | const EVP_MD **pmd = s->s3->tmp.md; | |
2924 | #ifndef OPENSSL_NO_DSA | |
152fbc28 | 2925 | pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
d376e57d DSH |
2926 | #endif |
2927 | #ifndef OPENSSL_NO_RSA | |
d18d31a1 | 2928 | if (SSL_USE_SIGALGS(s)) |
152fbc28 | 2929 | pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX); |
d18d31a1 | 2930 | else |
152fbc28 | 2931 | pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX); |
d18d31a1 | 2932 | pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN]; |
d376e57d DSH |
2933 | #endif |
2934 | #ifndef OPENSSL_NO_EC | |
152fbc28 | 2935 | pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX); |
d376e57d | 2936 | #endif |
e44380a9 | 2937 | #ifndef OPENSSL_NO_GOST |
152fbc28 DSH |
2938 | pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX); |
2939 | pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX); | |
2940 | pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX); | |
e44380a9 | 2941 | #endif |
d376e57d | 2942 | } |
f1fd4544 | 2943 | |
e469af8d | 2944 | int tls1_set_server_sigalgs(SSL *s) |
0f113f3e MC |
2945 | { |
2946 | int al; | |
2947 | size_t i; | |
8483a003 F |
2948 | |
2949 | /* Clear any shared signature algorithms */ | |
b548a1f1 RS |
2950 | OPENSSL_free(s->cert->shared_sigalgs); |
2951 | s->cert->shared_sigalgs = NULL; | |
2952 | s->cert->shared_sigalgslen = 0; | |
0f113f3e MC |
2953 | /* Clear certificate digests and validity flags */ |
2954 | for (i = 0; i < SSL_PKEY_NUM; i++) { | |
d376e57d | 2955 | s->s3->tmp.md[i] = NULL; |
6383d316 | 2956 | s->s3->tmp.valid_flags[i] = 0; |
0f113f3e MC |
2957 | } |
2958 | ||
2959 | /* If sigalgs received process it. */ | |
76106e60 | 2960 | if (s->s3->tmp.peer_sigalgs) { |
0f113f3e MC |
2961 | if (!tls1_process_sigalgs(s)) { |
2962 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE); | |
2963 | al = SSL_AD_INTERNAL_ERROR; | |
2964 | goto err; | |
2965 | } | |
2966 | /* Fatal error is no shared signature algorithms */ | |
2967 | if (!s->cert->shared_sigalgs) { | |
2968 | SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, | |
f430ba31 | 2969 | SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS); |
0f113f3e MC |
2970 | al = SSL_AD_ILLEGAL_PARAMETER; |
2971 | goto err; | |
2972 | } | |
d376e57d DSH |
2973 | } else { |
2974 | ssl_set_default_md(s); | |
2975 | } | |
0f113f3e MC |
2976 | return 1; |
2977 | err: | |
2978 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
2979 | return 0; | |
2980 | } | |
e469af8d | 2981 | |
70c22888 EK |
2982 | /* |
2983 | * Upon success, returns 1. | |
2984 | * Upon failure, returns 0 and sets |al| to the appropriate fatal alert. | |
2985 | */ | |
2986 | int ssl_check_clienthello_tlsext_late(SSL *s, int *al) | |
0f113f3e | 2987 | { |
70c22888 | 2988 | s->tlsext_status_expected = 0; |
0f113f3e MC |
2989 | |
2990 | /* | |
2991 | * If status request then ask callback what to do. Note: this must be | |
2992 | * called after servername callbacks in case the certificate has changed, | |
2993 | * and must be called after the cipher has been chosen because this may | |
2994 | * influence which certificate is sent | |
2995 | */ | |
2996 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) { | |
70c22888 | 2997 | int ret; |
0f113f3e MC |
2998 | CERT_PKEY *certpkey; |
2999 | certpkey = ssl_get_server_send_pkey(s); | |
3000 | /* If no certificate can't return certificate status */ | |
70c22888 EK |
3001 | if (certpkey != NULL) { |
3002 | /* | |
3003 | * Set current certificate to one we will use so SSL_get_certificate | |
3004 | * et al can pick it up. | |
3005 | */ | |
3006 | s->cert->key = certpkey; | |
3007 | ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | |
3008 | switch (ret) { | |
3009 | /* We don't want to send a status request response */ | |
3010 | case SSL_TLSEXT_ERR_NOACK: | |
0f113f3e | 3011 | s->tlsext_status_expected = 0; |
70c22888 EK |
3012 | break; |
3013 | /* status request response should be sent */ | |
3014 | case SSL_TLSEXT_ERR_OK: | |
3015 | if (s->tlsext_ocsp_resp) | |
3016 | s->tlsext_status_expected = 1; | |
3017 | break; | |
3018 | /* something bad happened */ | |
3019 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
3020 | default: | |
3021 | *al = SSL_AD_INTERNAL_ERROR; | |
3022 | return 0; | |
3023 | } | |
0f113f3e | 3024 | } |
817cd0d5 TS |
3025 | } |
3026 | ||
70c22888 EK |
3027 | if (!tls1_alpn_handle_client_hello_late(s, al)) { |
3028 | return 0; | |
0f113f3e | 3029 | } |
70c22888 EK |
3030 | |
3031 | return 1; | |
0f113f3e | 3032 | } |
2daceb03 | 3033 | |
36ca4ba6 | 3034 | int ssl_check_serverhello_tlsext(SSL *s) |
0f113f3e MC |
3035 | { |
3036 | int ret = SSL_TLSEXT_ERR_NOACK; | |
3037 | int al = SSL_AD_UNRECOGNIZED_NAME; | |
3038 | ||
e481f9b9 | 3039 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
3040 | /* |
3041 | * If we are client and using an elliptic curve cryptography cipher | |
3042 | * suite, then if server returns an EC point formats lists extension it | |
3043 | * must contain uncompressed. | |
3044 | */ | |
3045 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | |
3046 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | |
3047 | if ((s->tlsext_ecpointformatlist != NULL) | |
3048 | && (s->tlsext_ecpointformatlist_length > 0) | |
3049 | && (s->session->tlsext_ecpointformatlist != NULL) | |
3050 | && (s->session->tlsext_ecpointformatlist_length > 0) | |
ce0c1f2b | 3051 | && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) { |
0f113f3e MC |
3052 | /* we are using an ECC cipher */ |
3053 | size_t i; | |
3054 | unsigned char *list; | |
3055 | int found_uncompressed = 0; | |
3056 | list = s->session->tlsext_ecpointformatlist; | |
3057 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) { | |
3058 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { | |
3059 | found_uncompressed = 1; | |
3060 | break; | |
3061 | } | |
3062 | } | |
3063 | if (!found_uncompressed) { | |
3064 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT, | |
3065 | SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); | |
3066 | return -1; | |
3067 | } | |
3068 | } | |
3069 | ret = SSL_TLSEXT_ERR_OK; | |
a230b26e | 3070 | #endif /* OPENSSL_NO_EC */ |
0f113f3e MC |
3071 | |
3072 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | |
3073 | ret = | |
3074 | s->ctx->tlsext_servername_callback(s, &al, | |
3075 | s->ctx->tlsext_servername_arg); | |
3076 | else if (s->initial_ctx != NULL | |
3077 | && s->initial_ctx->tlsext_servername_callback != 0) | |
3078 | ret = | |
3079 | s->initial_ctx->tlsext_servername_callback(s, &al, | |
3080 | s-> | |
3081 | initial_ctx->tlsext_servername_arg); | |
3082 | ||
b1931d43 MC |
3083 | /* |
3084 | * Ensure we get sensible values passed to tlsext_status_cb in the event | |
3085 | * that we don't receive a status message | |
3086 | */ | |
bb1aaab4 MC |
3087 | OPENSSL_free(s->tlsext_ocsp_resp); |
3088 | s->tlsext_ocsp_resp = NULL; | |
8b0e934a | 3089 | s->tlsext_ocsp_resplen = 0; |
0f113f3e MC |
3090 | |
3091 | switch (ret) { | |
3092 | case SSL_TLSEXT_ERR_ALERT_FATAL: | |
3093 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | |
3094 | return -1; | |
3095 | ||
3096 | case SSL_TLSEXT_ERR_ALERT_WARNING: | |
3097 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | |
3098 | return 1; | |
3099 | ||
3100 | case SSL_TLSEXT_ERR_NOACK: | |
3101 | s->servername_done = 0; | |
3102 | default: | |
3103 | return 1; | |
3104 | } | |
3105 | } | |
761772d7 | 3106 | |
50932c4a | 3107 | int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt) |
0f113f3e MC |
3108 | { |
3109 | int al = -1; | |
3110 | if (s->version < SSL3_VERSION) | |
3111 | return 1; | |
50932c4a | 3112 | if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) { |
0f113f3e MC |
3113 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
3114 | return 0; | |
3115 | } | |
3116 | ||
3117 | if (ssl_check_serverhello_tlsext(s) <= 0) { | |
3118 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT); | |
3119 | return 0; | |
3120 | } | |
3121 | return 1; | |
09e4e4b9 DSH |
3122 | } |
3123 | ||
64386324 MC |
3124 | /* |
3125 | * Given a list of extensions that we collected earlier, find one of a given | |
3126 | * type and return it. | |
3127 | * | |
3128 | * |exts| is the set of extensions previously collected. | |
3129 | * |numexts| is the number of extensions that we have. | |
3130 | * |type| the type of the extension that we are looking for. | |
3131 | * | |
3132 | * Returns a pointer to the found RAW_EXTENSION data, or NULL if not found. | |
3133 | */ | |
cd998837 MC |
3134 | RAW_EXTENSION *tls_get_extension_by_type(RAW_EXTENSION *exts, size_t numexts, |
3135 | unsigned int type) | |
1ab3836b MC |
3136 | { |
3137 | size_t loop; | |
3138 | ||
3139 | for (loop = 0; loop < numexts; loop++) { | |
3140 | if (exts[loop].type == type) | |
3141 | return &exts[loop]; | |
3142 | } | |
3143 | ||
3144 | return NULL; | |
3145 | } | |
3146 | ||
1d97c843 | 3147 | /*- |
1ab3836b | 3148 | * Gets the ticket information supplied by the client if any. |
e7f0d921 | 3149 | * |
1ab3836b | 3150 | * hello: The parsed ClientHello data |
c519e89f BM |
3151 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
3152 | * point to the resulting session. | |
3153 | * | |
3154 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key | |
3155 | * ciphersuite, in which case we have no use for session tickets and one will | |
3156 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. | |
3157 | * | |
3158 | * Returns: | |
3159 | * -1: fatal error, either from parsing or decrypting the ticket. | |
3160 | * 0: no ticket was found (or was ignored, based on settings). | |
3161 | * 1: a zero length extension was found, indicating that the client supports | |
3162 | * session tickets but doesn't currently have one to offer. | |
3163 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but | |
3164 | * couldn't be decrypted because of a non-fatal error. | |
3165 | * 3: a ticket was successfully decrypted and *ret was set. | |
3166 | * | |
3167 | * Side effects: | |
3168 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue | |
3169 | * a new session ticket to the client because the client indicated support | |
3170 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | |
3171 | * a session ticket or we couldn't use the one it gave us, or if | |
3172 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. | |
3173 | * Otherwise, s->tlsext_ticket_expected is set to 0. | |
6434abbf | 3174 | */ |
1ab3836b MC |
3175 | int tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello, |
3176 | SSL_SESSION **ret) | |
0f113f3e | 3177 | { |
1ab3836b MC |
3178 | int retv; |
3179 | const unsigned char *etick; | |
3180 | size_t size; | |
3181 | RAW_EXTENSION *ticketext; | |
e7f0d921 | 3182 | |
0f113f3e MC |
3183 | *ret = NULL; |
3184 | s->tlsext_ticket_expected = 0; | |
3185 | ||
3186 | /* | |
9362c93e MC |
3187 | * If tickets disabled or not supported by the protocol version |
3188 | * (e.g. TLSv1.3) behave as if no ticket present to permit stateful | |
0f113f3e MC |
3189 | * resumption. |
3190 | */ | |
1ab3836b | 3191 | if (s->version <= SSL3_VERSION || !tls_use_ticket(s)) |
0f113f3e | 3192 | return 0; |
9ceb2426 | 3193 | |
cd998837 MC |
3194 | ticketext = tls_get_extension_by_type(hello->pre_proc_exts, |
3195 | hello->num_extensions, | |
3196 | TLSEXT_TYPE_session_ticket); | |
1ab3836b MC |
3197 | if (ticketext == NULL) |
3198 | return 0; | |
3199 | ||
3200 | size = PACKET_remaining(&ticketext->data); | |
3201 | if (size == 0) { | |
3202 | /* | |
3203 | * The client will accept a ticket but doesn't currently have | |
3204 | * one. | |
3205 | */ | |
3206 | s->tlsext_ticket_expected = 1; | |
3207 | return 1; | |
9ceb2426 | 3208 | } |
1ab3836b MC |
3209 | if (s->tls_session_secret_cb) { |
3210 | /* | |
3211 | * Indicate that the ticket couldn't be decrypted rather than | |
3212 | * generating the session from ticket now, trigger | |
3213 | * abbreviated handshake based on external mechanism to | |
3214 | * calculate the master secret later. | |
3215 | */ | |
3216 | return 2; | |
3217 | } | |
3218 | if (!PACKET_get_bytes(&ticketext->data, &etick, size)) { | |
3219 | /* Shouldn't ever happen */ | |
3220 | return -1; | |
3221 | } | |
3222 | retv = tls_decrypt_ticket(s, etick, size, hello->session_id, | |
3223 | hello->session_id_len, ret); | |
3224 | switch (retv) { | |
3225 | case 2: /* ticket couldn't be decrypted */ | |
3226 | s->tlsext_ticket_expected = 1; | |
3227 | return 2; | |
9ceb2426 | 3228 | |
1ab3836b MC |
3229 | case 3: /* ticket was decrypted */ |
3230 | return 3; | |
9ceb2426 | 3231 | |
1ab3836b MC |
3232 | case 4: /* ticket decrypted but need to renew */ |
3233 | s->tlsext_ticket_expected = 1; | |
3234 | return 3; | |
e7f0d921 | 3235 | |
1ab3836b MC |
3236 | default: /* fatal error */ |
3237 | return -1; | |
0f113f3e | 3238 | } |
1ab3836b MC |
3239 | } |
3240 | ||
3241 | /* | |
de7d61d5 | 3242 | * Sets the extended master secret flag if the extension is present in the |
0f1e51ea | 3243 | * ClientHello and we can support it |
de7d61d5 MC |
3244 | * Returns: |
3245 | * 1 on success | |
3246 | * 0 on error | |
1ab3836b | 3247 | */ |
902aca09 | 3248 | int tls_check_client_ems_support(SSL *s, const CLIENTHELLO_MSG *hello) |
1ab3836b MC |
3249 | { |
3250 | RAW_EXTENSION *emsext; | |
3251 | ||
3252 | s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS; | |
3253 | ||
0f1e51ea MC |
3254 | if (!SSL_IS_DTLS(s) && (s->version < TLS1_VERSION |
3255 | || s->version > TLS1_2_VERSION)) | |
1ab3836b MC |
3256 | return 1; |
3257 | ||
cd998837 MC |
3258 | emsext = tls_get_extension_by_type(hello->pre_proc_exts, |
3259 | hello->num_extensions, | |
3260 | TLSEXT_TYPE_extended_master_secret); | |
1ab3836b MC |
3261 | |
3262 | /* | |
3263 | * No extensions is a success - we have successfully discovered that the | |
3264 | * client doesn't support EMS. | |
3265 | */ | |
3266 | if (emsext == NULL) | |
3267 | return 1; | |
3268 | ||
3269 | /* The extensions must always be empty */ | |
3270 | if (PACKET_remaining(&emsext->data) != 0) | |
3271 | return 0; | |
3272 | ||
3273 | s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS; | |
3274 | ||
3275 | return 1; | |
0f113f3e | 3276 | } |
6434abbf | 3277 | |
1d97c843 TH |
3278 | /*- |
3279 | * tls_decrypt_ticket attempts to decrypt a session ticket. | |
c519e89f BM |
3280 | * |
3281 | * etick: points to the body of the session ticket extension. | |
8483a003 | 3282 | * eticklen: the length of the session tickets extension. |
c519e89f BM |
3283 | * sess_id: points at the session ID. |
3284 | * sesslen: the length of the session ID. | |
3285 | * psess: (output) on return, if a ticket was decrypted, then this is set to | |
3286 | * point to the resulting session. | |
3287 | * | |
3288 | * Returns: | |
bf7c6817 | 3289 | * -2: fatal error, malloc failure. |
c519e89f BM |
3290 | * -1: fatal error, either from parsing or decrypting the ticket. |
3291 | * 2: the ticket couldn't be decrypted. | |
3292 | * 3: a ticket was successfully decrypted and *psess was set. | |
3293 | * 4: same as 3, but the ticket needs to be renewed. | |
3294 | */ | |
0f113f3e | 3295 | static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, |
d736bc1a | 3296 | size_t eticklen, const unsigned char *sess_id, |
ec60ccc1 | 3297 | size_t sesslen, SSL_SESSION **psess) |
0f113f3e MC |
3298 | { |
3299 | SSL_SESSION *sess; | |
3300 | unsigned char *sdec; | |
3301 | const unsigned char *p; | |
348240c6 MC |
3302 | int slen, renew_ticket = 0, ret = -1, declen; |
3303 | size_t mlen; | |
0f113f3e | 3304 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
bf7c6817 | 3305 | HMAC_CTX *hctx = NULL; |
846ec07d | 3306 | EVP_CIPHER_CTX *ctx; |
0f113f3e | 3307 | SSL_CTX *tctx = s->initial_ctx; |
e97763c9 | 3308 | |
0f113f3e | 3309 | /* Initialize session ticket encryption and HMAC contexts */ |
bf7c6817 RL |
3310 | hctx = HMAC_CTX_new(); |
3311 | if (hctx == NULL) | |
3312 | return -2; | |
846ec07d | 3313 | ctx = EVP_CIPHER_CTX_new(); |
35b1a433 MC |
3314 | if (ctx == NULL) { |
3315 | ret = -2; | |
3316 | goto err; | |
3317 | } | |
0f113f3e MC |
3318 | if (tctx->tlsext_ticket_key_cb) { |
3319 | unsigned char *nctick = (unsigned char *)etick; | |
3320 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, | |
846ec07d | 3321 | ctx, hctx, 0); |
0f113f3e | 3322 | if (rv < 0) |
35b1a433 MC |
3323 | goto err; |
3324 | if (rv == 0) { | |
3325 | ret = 2; | |
3326 | goto err; | |
3327 | } | |
0f113f3e MC |
3328 | if (rv == 2) |
3329 | renew_ticket = 1; | |
3330 | } else { | |
3331 | /* Check key name matches */ | |
4e2e1ec9 TS |
3332 | if (memcmp(etick, tctx->tlsext_tick_key_name, |
3333 | sizeof(tctx->tlsext_tick_key_name)) != 0) { | |
35b1a433 MC |
3334 | ret = 2; |
3335 | goto err; | |
3336 | } | |
4e2e1ec9 TS |
3337 | if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, |
3338 | sizeof(tctx->tlsext_tick_hmac_key), | |
5f3d93e4 | 3339 | EVP_sha256(), NULL) <= 0 |
a230b26e EK |
3340 | || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, |
3341 | tctx->tlsext_tick_aes_key, | |
3342 | etick + sizeof(tctx->tlsext_tick_key_name)) <= | |
3343 | 0) { | |
5f3d93e4 | 3344 | goto err; |
a230b26e | 3345 | } |
0f113f3e MC |
3346 | } |
3347 | /* | |
3348 | * Attempt to process session ticket, first conduct sanity and integrity | |
3349 | * checks on ticket. | |
3350 | */ | |
bf7c6817 | 3351 | mlen = HMAC_size(hctx); |
348240c6 | 3352 | if (mlen == 0) { |
5f3d93e4 | 3353 | goto err; |
0f113f3e | 3354 | } |
e97763c9 DSH |
3355 | /* Sanity check ticket length: must exceed keyname + IV + HMAC */ |
3356 | if (eticklen <= | |
348240c6 | 3357 | TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) { |
e97763c9 DSH |
3358 | ret = 2; |
3359 | goto err; | |
3360 | } | |
0f113f3e MC |
3361 | eticklen -= mlen; |
3362 | /* Check HMAC of encrypted ticket */ | |
bf7c6817 | 3363 | if (HMAC_Update(hctx, etick, eticklen) <= 0 |
a230b26e | 3364 | || HMAC_Final(hctx, tick_hmac, NULL) <= 0) { |
5f3d93e4 MC |
3365 | goto err; |
3366 | } | |
bf7c6817 | 3367 | HMAC_CTX_free(hctx); |
0f113f3e | 3368 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) { |
846ec07d | 3369 | EVP_CIPHER_CTX_free(ctx); |
0f113f3e MC |
3370 | return 2; |
3371 | } | |
3372 | /* Attempt to decrypt session data */ | |
3373 | /* Move p after IV to start of encrypted ticket, update length */ | |
846ec07d RL |
3374 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx); |
3375 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx); | |
0f113f3e | 3376 | sdec = OPENSSL_malloc(eticklen); |
348240c6 MC |
3377 | if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, |
3378 | (int)eticklen) <= 0) { | |
846ec07d | 3379 | EVP_CIPHER_CTX_free(ctx); |
d1247df2 | 3380 | OPENSSL_free(sdec); |
0f113f3e MC |
3381 | return -1; |
3382 | } | |
348240c6 | 3383 | if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) { |
846ec07d | 3384 | EVP_CIPHER_CTX_free(ctx); |
0f113f3e MC |
3385 | OPENSSL_free(sdec); |
3386 | return 2; | |
3387 | } | |
348240c6 | 3388 | slen += declen; |
846ec07d RL |
3389 | EVP_CIPHER_CTX_free(ctx); |
3390 | ctx = NULL; | |
0f113f3e MC |
3391 | p = sdec; |
3392 | ||
3393 | sess = d2i_SSL_SESSION(NULL, &p, slen); | |
3394 | OPENSSL_free(sdec); | |
3395 | if (sess) { | |
3396 | /* | |
3397 | * The session ID, if non-empty, is used by some clients to detect | |
3398 | * that the ticket has been accepted. So we copy it to the session | |
3399 | * structure. If it is empty set length to zero as required by | |
3400 | * standard. | |
3401 | */ | |
3402 | if (sesslen) | |
3403 | memcpy(sess->session_id, sess_id, sesslen); | |
3404 | sess->session_id_length = sesslen; | |
3405 | *psess = sess; | |
3406 | if (renew_ticket) | |
3407 | return 4; | |
3408 | else | |
3409 | return 3; | |
3410 | } | |
3411 | ERR_clear_error(); | |
3412 | /* | |
3413 | * For session parse failure, indicate that we need to send a new ticket. | |
3414 | */ | |
3415 | return 2; | |
a230b26e | 3416 | err: |
846ec07d | 3417 | EVP_CIPHER_CTX_free(ctx); |
bf7c6817 | 3418 | HMAC_CTX_free(hctx); |
35b1a433 | 3419 | return ret; |
0f113f3e | 3420 | } |
6434abbf | 3421 | |
6b7be581 DSH |
3422 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
3423 | ||
0f113f3e MC |
3424 | typedef struct { |
3425 | int nid; | |
3426 | int id; | |
3427 | } tls12_lookup; | |
6b7be581 | 3428 | |
d97ed219 | 3429 | static const tls12_lookup tls12_md[] = { |
0f113f3e MC |
3430 | {NID_md5, TLSEXT_hash_md5}, |
3431 | {NID_sha1, TLSEXT_hash_sha1}, | |
3432 | {NID_sha224, TLSEXT_hash_sha224}, | |
3433 | {NID_sha256, TLSEXT_hash_sha256}, | |
3434 | {NID_sha384, TLSEXT_hash_sha384}, | |
e44380a9 DB |
3435 | {NID_sha512, TLSEXT_hash_sha512}, |
3436 | {NID_id_GostR3411_94, TLSEXT_hash_gostr3411}, | |
3437 | {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256}, | |
3438 | {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512}, | |
6b7be581 DSH |
3439 | }; |
3440 | ||
d97ed219 | 3441 | static const tls12_lookup tls12_sig[] = { |
0f113f3e MC |
3442 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
3443 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, | |
e44380a9 DB |
3444 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}, |
3445 | {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001}, | |
3446 | {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256}, | |
3447 | {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512} | |
6b7be581 DSH |
3448 | }; |
3449 | ||
d97ed219 | 3450 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) |
0f113f3e MC |
3451 | { |
3452 | size_t i; | |
3453 | for (i = 0; i < tlen; i++) { | |
3454 | if (table[i].nid == nid) | |
3455 | return table[i].id; | |
3456 | } | |
3457 | return -1; | |
3458 | } | |
e7f8ff43 | 3459 | |
d97ed219 | 3460 | static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) |
0f113f3e MC |
3461 | { |
3462 | size_t i; | |
3463 | for (i = 0; i < tlen; i++) { | |
3464 | if ((table[i].id) == id) | |
3465 | return table[i].nid; | |
3466 | } | |
3467 | return NID_undef; | |
3468 | } | |
3469 | ||
6400f338 MC |
3470 | int tls12_get_sigandhash(WPACKET *pkt, const EVP_PKEY *pk, const EVP_MD *md) |
3471 | { | |
3472 | int sig_id, md_id; | |
418a18a2 MC |
3473 | |
3474 | if (md == NULL) | |
6400f338 MC |
3475 | return 0; |
3476 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md)); | |
3477 | if (md_id == -1) | |
3478 | return 0; | |
3479 | sig_id = tls12_get_sigid(pk); | |
3480 | if (sig_id == -1) | |
3481 | return 0; | |
08029dfa | 3482 | if (!WPACKET_put_bytes_u8(pkt, md_id) || !WPACKET_put_bytes_u8(pkt, sig_id)) |
6400f338 MC |
3483 | return 0; |
3484 | ||
3485 | return 1; | |
3486 | } | |
3487 | ||
a2f9200f | 3488 | int tls12_get_sigid(const EVP_PKEY *pk) |
0f113f3e | 3489 | { |
3aeb9348 | 3490 | return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig)); |
0f113f3e MC |
3491 | } |
3492 | ||
3493 | typedef struct { | |
3494 | int nid; | |
3495 | int secbits; | |
7afd2312 | 3496 | int md_idx; |
e44380a9 | 3497 | unsigned char tlsext_hash; |
0f113f3e | 3498 | } tls12_hash_info; |
b362ccab DSH |
3499 | |
3500 | static const tls12_hash_info tls12_md_info[] = { | |
7afd2312 DSH |
3501 | {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5}, |
3502 | {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1}, | |
3503 | {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224}, | |
3504 | {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256}, | |
3505 | {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384}, | |
3506 | {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512}, | |
a230b26e EK |
3507 | {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411}, |
3508 | {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, | |
3509 | TLSEXT_hash_gostr34112012_256}, | |
3510 | {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, | |
3511 | TLSEXT_hash_gostr34112012_512}, | |
b362ccab | 3512 | }; |
a2f9200f | 3513 | |
b362ccab | 3514 | static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg) |
0f113f3e | 3515 | { |
e44380a9 | 3516 | unsigned int i; |
0f113f3e MC |
3517 | if (hash_alg == 0) |
3518 | return NULL; | |
e44380a9 | 3519 | |
a230b26e | 3520 | for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) { |
e44380a9 DB |
3521 | if (tls12_md_info[i].tlsext_hash == hash_alg) |
3522 | return tls12_md_info + i; | |
3523 | } | |
3524 | ||
3525 | return NULL; | |
0f113f3e | 3526 | } |
a2f9200f | 3527 | |
b362ccab | 3528 | const EVP_MD *tls12_get_hash(unsigned char hash_alg) |
0f113f3e MC |
3529 | { |
3530 | const tls12_hash_info *inf; | |
3531 | if (hash_alg == TLSEXT_hash_md5 && FIPS_mode()) | |
3532 | return NULL; | |
3533 | inf = tls12_get_hash_info(hash_alg); | |
7afd2312 | 3534 | if (!inf) |
0f113f3e | 3535 | return NULL; |
7afd2312 | 3536 | return ssl_md(inf->md_idx); |
0f113f3e | 3537 | } |
a2f9200f | 3538 | |
4453cd8c | 3539 | static int tls12_get_pkey_idx(unsigned char sig_alg) |
0f113f3e MC |
3540 | { |
3541 | switch (sig_alg) { | |
e481f9b9 | 3542 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3543 | case TLSEXT_signature_rsa: |
3544 | return SSL_PKEY_RSA_SIGN; | |
e481f9b9 MC |
3545 | #endif |
3546 | #ifndef OPENSSL_NO_DSA | |
0f113f3e MC |
3547 | case TLSEXT_signature_dsa: |
3548 | return SSL_PKEY_DSA_SIGN; | |
e481f9b9 MC |
3549 | #endif |
3550 | #ifndef OPENSSL_NO_EC | |
0f113f3e MC |
3551 | case TLSEXT_signature_ecdsa: |
3552 | return SSL_PKEY_ECC; | |
e481f9b9 | 3553 | #endif |
a230b26e | 3554 | #ifndef OPENSSL_NO_GOST |
e44380a9 DB |
3555 | case TLSEXT_signature_gostr34102001: |
3556 | return SSL_PKEY_GOST01; | |
3557 | ||
3558 | case TLSEXT_signature_gostr34102012_256: | |
3559 | return SSL_PKEY_GOST12_256; | |
3560 | ||
3561 | case TLSEXT_signature_gostr34102012_512: | |
3562 | return SSL_PKEY_GOST12_512; | |
a230b26e | 3563 | #endif |
0f113f3e MC |
3564 | } |
3565 | return -1; | |
3566 | } | |
4453cd8c DSH |
3567 | |
3568 | /* Convert TLS 1.2 signature algorithm extension values into NIDs */ | |
3569 | static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, | |
0f113f3e MC |
3570 | int *psignhash_nid, const unsigned char *data) |
3571 | { | |
330dcb09 | 3572 | int sign_nid = NID_undef, hash_nid = NID_undef; |
0f113f3e MC |
3573 | if (!phash_nid && !psign_nid && !psignhash_nid) |
3574 | return; | |
3575 | if (phash_nid || psignhash_nid) { | |
b6eb9827 | 3576 | hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md)); |
0f113f3e MC |
3577 | if (phash_nid) |
3578 | *phash_nid = hash_nid; | |
3579 | } | |
3580 | if (psign_nid || psignhash_nid) { | |
b6eb9827 | 3581 | sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig)); |
0f113f3e MC |
3582 | if (psign_nid) |
3583 | *psign_nid = sign_nid; | |
3584 | } | |
3585 | if (psignhash_nid) { | |
330dcb09 | 3586 | if (sign_nid == NID_undef || hash_nid == NID_undef |
a230b26e | 3587 | || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0) |
0f113f3e MC |
3588 | *psignhash_nid = NID_undef; |
3589 | } | |
3590 | } | |
3591 | ||
b362ccab DSH |
3592 | /* Check to see if a signature algorithm is allowed */ |
3593 | static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp) | |
0f113f3e MC |
3594 | { |
3595 | /* See if we have an entry in the hash table and it is enabled */ | |
3596 | const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]); | |
7afd2312 | 3597 | if (hinf == NULL || ssl_md(hinf->md_idx) == NULL) |
0f113f3e MC |
3598 | return 0; |
3599 | /* See if public key algorithm allowed */ | |
3600 | if (tls12_get_pkey_idx(ptmp[1]) == -1) | |
3601 | return 0; | |
3602 | /* Finally see if security callback allows it */ | |
3603 | return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp); | |
3604 | } | |
3605 | ||
3606 | /* | |
3607 | * Get a mask of disabled public key algorithms based on supported signature | |
3608 | * algorithms. For example if no signature algorithm supports RSA then RSA is | |
3609 | * disabled. | |
b362ccab DSH |
3610 | */ |
3611 | ||
90d9e49a | 3612 | void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op) |
0f113f3e MC |
3613 | { |
3614 | const unsigned char *sigalgs; | |
3615 | size_t i, sigalgslen; | |
3616 | int have_rsa = 0, have_dsa = 0, have_ecdsa = 0; | |
3617 | /* | |
3618 | * Now go through all signature algorithms seeing if we support any for | |
3619 | * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep | |
3620 | * down calls to security callback only check if we have to. | |
3621 | */ | |
3622 | sigalgslen = tls12_get_psigalgs(s, &sigalgs); | |
3623 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { | |
3624 | switch (sigalgs[1]) { | |
e481f9b9 | 3625 | #ifndef OPENSSL_NO_RSA |
0f113f3e MC |
3626 | case TLSEXT_signature_rsa: |
3627 | if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs)) | |
3628 | have_rsa = 1; | |
3629 | break; | |
e481f9b9 MC |
3630 | #endif |
3631 | #ifndef OPENSSL_NO_DSA | |
0f113f3e MC |
3632 | case TLSEXT_signature_dsa: |
3633 | if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs)) | |
3634 | have_dsa = 1; | |
3635 | break; | |
e481f9b9 MC |
3636 | #endif |
3637 | #ifndef OPENSSL_NO_EC | |
0f113f3e MC |
3638 | case TLSEXT_signature_ecdsa: |
3639 | if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs)) | |
3640 | have_ecdsa = 1; | |
3641 | break; | |
e481f9b9 | 3642 | #endif |
0f113f3e MC |
3643 | } |
3644 | } | |
3645 | if (!have_rsa) | |
3646 | *pmask_a |= SSL_aRSA; | |
3647 | if (!have_dsa) | |
3648 | *pmask_a |= SSL_aDSS; | |
3649 | if (!have_ecdsa) | |
3650 | *pmask_a |= SSL_aECDSA; | |
3651 | } | |
b362ccab | 3652 | |
ae2f7b37 | 3653 | int tls12_copy_sigalgs(SSL *s, WPACKET *pkt, |
2c7b4dbc MC |
3654 | const unsigned char *psig, size_t psiglen) |
3655 | { | |
3656 | size_t i; | |
c0f9e23c | 3657 | |
2c7b4dbc MC |
3658 | for (i = 0; i < psiglen; i += 2, psig += 2) { |
3659 | if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) { | |
08029dfa MC |
3660 | if (!WPACKET_put_bytes_u8(pkt, psig[0]) |
3661 | || !WPACKET_put_bytes_u8(pkt, psig[1])) | |
2c7b4dbc MC |
3662 | return 0; |
3663 | } | |
3664 | } | |
3665 | return 1; | |
3666 | } | |
3667 | ||
4453cd8c | 3668 | /* Given preference and allowed sigalgs set shared sigalgs */ |
348240c6 MC |
3669 | static size_t tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig, |
3670 | const unsigned char *pref, size_t preflen, | |
3671 | const unsigned char *allow, size_t allowlen) | |
0f113f3e MC |
3672 | { |
3673 | const unsigned char *ptmp, *atmp; | |
3674 | size_t i, j, nmatch = 0; | |
3675 | for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) { | |
3676 | /* Skip disabled hashes or signature algorithms */ | |
3677 | if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp)) | |
3678 | continue; | |
3679 | for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) { | |
3680 | if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) { | |
3681 | nmatch++; | |
3682 | if (shsig) { | |
3683 | shsig->rhash = ptmp[0]; | |
3684 | shsig->rsign = ptmp[1]; | |
3685 | tls1_lookup_sigalg(&shsig->hash_nid, | |
3686 | &shsig->sign_nid, | |
3687 | &shsig->signandhash_nid, ptmp); | |
3688 | shsig++; | |
3689 | } | |
3690 | break; | |
3691 | } | |
3692 | } | |
3693 | } | |
3694 | return nmatch; | |
3695 | } | |
4453cd8c DSH |
3696 | |
3697 | /* Set shared signature algorithms for SSL structures */ | |
3698 | static int tls1_set_shared_sigalgs(SSL *s) | |
0f113f3e MC |
3699 | { |
3700 | const unsigned char *pref, *allow, *conf; | |
3701 | size_t preflen, allowlen, conflen; | |
3702 | size_t nmatch; | |
3703 | TLS_SIGALGS *salgs = NULL; | |
3704 | CERT *c = s->cert; | |
3705 | unsigned int is_suiteb = tls1_suiteb(s); | |
b548a1f1 RS |
3706 | |
3707 | OPENSSL_free(c->shared_sigalgs); | |
3708 | c->shared_sigalgs = NULL; | |
3709 | c->shared_sigalgslen = 0; | |
0f113f3e MC |
3710 | /* If client use client signature algorithms if not NULL */ |
3711 | if (!s->server && c->client_sigalgs && !is_suiteb) { | |
3712 | conf = c->client_sigalgs; | |
3713 | conflen = c->client_sigalgslen; | |
3714 | } else if (c->conf_sigalgs && !is_suiteb) { | |
3715 | conf = c->conf_sigalgs; | |
3716 | conflen = c->conf_sigalgslen; | |
3717 | } else | |
3718 | conflen = tls12_get_psigalgs(s, &conf); | |
3719 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) { | |
3720 | pref = conf; | |
3721 | preflen = conflen; | |
76106e60 DSH |
3722 | allow = s->s3->tmp.peer_sigalgs; |
3723 | allowlen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
3724 | } else { |
3725 | allow = conf; | |
3726 | allowlen = conflen; | |
76106e60 DSH |
3727 | pref = s->s3->tmp.peer_sigalgs; |
3728 | preflen = s->s3->tmp.peer_sigalgslen; | |
0f113f3e MC |
3729 | } |
3730 | nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen); | |
34e3edbf DSH |
3731 | if (nmatch) { |
3732 | salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS)); | |
a71edf3b | 3733 | if (salgs == NULL) |
34e3edbf DSH |
3734 | return 0; |
3735 | nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen); | |
3736 | } else { | |
3737 | salgs = NULL; | |
3738 | } | |
0f113f3e MC |
3739 | c->shared_sigalgs = salgs; |
3740 | c->shared_sigalgslen = nmatch; | |
3741 | return 1; | |
3742 | } | |
4453cd8c | 3743 | |
6b7be581 DSH |
3744 | /* Set preferred digest for each key type */ |
3745 | ||
d736bc1a | 3746 | int tls1_save_sigalgs(SSL *s, const unsigned char *data, size_t dsize) |
0f113f3e MC |
3747 | { |
3748 | CERT *c = s->cert; | |
3749 | /* Extension ignored for inappropriate versions */ | |
3750 | if (!SSL_USE_SIGALGS(s)) | |
3751 | return 1; | |
3752 | /* Should never happen */ | |
3753 | if (!c) | |
3754 | return 0; | |
3755 | ||
76106e60 DSH |
3756 | OPENSSL_free(s->s3->tmp.peer_sigalgs); |
3757 | s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize); | |
3758 | if (s->s3->tmp.peer_sigalgs == NULL) | |
0f113f3e | 3759 | return 0; |
76106e60 DSH |
3760 | s->s3->tmp.peer_sigalgslen = dsize; |
3761 | memcpy(s->s3->tmp.peer_sigalgs, data, dsize); | |
0f113f3e MC |
3762 | return 1; |
3763 | } | |
6b7be581 | 3764 | |
c800c27a | 3765 | int tls1_process_sigalgs(SSL *s) |
0f113f3e MC |
3766 | { |
3767 | int idx; | |
3768 | size_t i; | |
3769 | const EVP_MD *md; | |
d376e57d | 3770 | const EVP_MD **pmd = s->s3->tmp.md; |
f7d53487 | 3771 | uint32_t *pvalid = s->s3->tmp.valid_flags; |
0f113f3e MC |
3772 | CERT *c = s->cert; |
3773 | TLS_SIGALGS *sigptr; | |
3774 | if (!tls1_set_shared_sigalgs(s)) | |
3775 | return 0; | |
3776 | ||
0f113f3e MC |
3777 | for (i = 0, sigptr = c->shared_sigalgs; |
3778 | i < c->shared_sigalgslen; i++, sigptr++) { | |
3779 | idx = tls12_get_pkey_idx(sigptr->rsign); | |
d376e57d | 3780 | if (idx > 0 && pmd[idx] == NULL) { |
0f113f3e | 3781 | md = tls12_get_hash(sigptr->rhash); |
d376e57d | 3782 | pmd[idx] = md; |
6383d316 | 3783 | pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN; |
0f113f3e | 3784 | if (idx == SSL_PKEY_RSA_SIGN) { |
6383d316 | 3785 | pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN; |
d376e57d | 3786 | pmd[SSL_PKEY_RSA_ENC] = md; |
0f113f3e MC |
3787 | } |
3788 | } | |
6b7be581 | 3789 | |
0f113f3e MC |
3790 | } |
3791 | /* | |
3792 | * In strict mode leave unset digests as NULL to indicate we can't use | |
3793 | * the certificate for signing. | |
3794 | */ | |
3795 | if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) { | |
3796 | /* | |
3797 | * Set any remaining keys to default values. NOTE: if alg is not | |
3798 | * supported it stays as NULL. | |
3799 | */ | |
e481f9b9 | 3800 | #ifndef OPENSSL_NO_DSA |
d376e57d DSH |
3801 | if (pmd[SSL_PKEY_DSA_SIGN] == NULL) |
3802 | pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1(); | |
e481f9b9 MC |
3803 | #endif |
3804 | #ifndef OPENSSL_NO_RSA | |
d376e57d DSH |
3805 | if (pmd[SSL_PKEY_RSA_SIGN] == NULL) { |
3806 | pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1(); | |
3807 | pmd[SSL_PKEY_RSA_ENC] = EVP_sha1(); | |
0f113f3e | 3808 | } |
e481f9b9 MC |
3809 | #endif |
3810 | #ifndef OPENSSL_NO_EC | |
d376e57d DSH |
3811 | if (pmd[SSL_PKEY_ECC] == NULL) |
3812 | pmd[SSL_PKEY_ECC] = EVP_sha1(); | |
e481f9b9 | 3813 | #endif |
a230b26e | 3814 | #ifndef OPENSSL_NO_GOST |
e44380a9 DB |
3815 | if (pmd[SSL_PKEY_GOST01] == NULL) |
3816 | pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94); | |
3817 | if (pmd[SSL_PKEY_GOST12_256] == NULL) | |
a230b26e EK |
3818 | pmd[SSL_PKEY_GOST12_256] = |
3819 | EVP_get_digestbynid(NID_id_GostR3411_2012_256); | |
e44380a9 | 3820 | if (pmd[SSL_PKEY_GOST12_512] == NULL) |
a230b26e EK |
3821 | pmd[SSL_PKEY_GOST12_512] = |
3822 | EVP_get_digestbynid(NID_id_GostR3411_2012_512); | |
3823 | #endif | |
0f113f3e MC |
3824 | } |
3825 | return 1; | |
3826 | } | |
4817504d | 3827 | |
e7f8ff43 | 3828 | int SSL_get_sigalgs(SSL *s, int idx, |
0f113f3e MC |
3829 | int *psign, int *phash, int *psignhash, |
3830 | unsigned char *rsig, unsigned char *rhash) | |
3831 | { | |
76106e60 | 3832 | const unsigned char *psig = s->s3->tmp.peer_sigalgs; |
348240c6 MC |
3833 | size_t numsigalgs = s->s3->tmp.peer_sigalgslen / 2; |
3834 | if (psig == NULL || numsigalgs > INT_MAX) | |
0f113f3e MC |
3835 | return 0; |
3836 | if (idx >= 0) { | |
3837 | idx <<= 1; | |
76106e60 | 3838 | if (idx >= (int)s->s3->tmp.peer_sigalgslen) |
0f113f3e MC |
3839 | return 0; |
3840 | psig += idx; | |
3841 | if (rhash) | |
3842 | *rhash = psig[0]; | |
3843 | if (rsig) | |
3844 | *rsig = psig[1]; | |
3845 | tls1_lookup_sigalg(phash, psign, psignhash, psig); | |
3846 | } | |
348240c6 | 3847 | return (int)numsigalgs; |
0f113f3e | 3848 | } |
4453cd8c DSH |
3849 | |
3850 | int SSL_get_shared_sigalgs(SSL *s, int idx, | |
0f113f3e MC |
3851 | int *psign, int *phash, int *psignhash, |
3852 | unsigned char *rsig, unsigned char *rhash) | |
3853 | { | |
3854 | TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs; | |
348240c6 MC |
3855 | if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen |
3856 | || s->cert->shared_sigalgslen > INT_MAX) | |
0f113f3e MC |
3857 | return 0; |
3858 | shsigalgs += idx; | |
3859 | if (phash) | |
3860 | *phash = shsigalgs->hash_nid; | |
3861 | if (psign) | |
3862 | *psign = shsigalgs->sign_nid; | |
3863 | if (psignhash) | |
3864 | *psignhash = shsigalgs->signandhash_nid; | |
3865 | if (rsig) | |
3866 | *rsig = shsigalgs->rsign; | |
3867 | if (rhash) | |
3868 | *rhash = shsigalgs->rhash; | |
348240c6 | 3869 | return (int)s->cert->shared_sigalgslen; |
0f113f3e MC |
3870 | } |
3871 | ||
e481f9b9 | 3872 | #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2) |
0f229cce | 3873 | |
0f113f3e MC |
3874 | typedef struct { |
3875 | size_t sigalgcnt; | |
3876 | int sigalgs[MAX_SIGALGLEN]; | |
3877 | } sig_cb_st; | |
0f229cce | 3878 | |
431f458d DSH |
3879 | static void get_sigorhash(int *psig, int *phash, const char *str) |
3880 | { | |
3881 | if (strcmp(str, "RSA") == 0) { | |
3882 | *psig = EVP_PKEY_RSA; | |
3883 | } else if (strcmp(str, "DSA") == 0) { | |
3884 | *psig = EVP_PKEY_DSA; | |
3885 | } else if (strcmp(str, "ECDSA") == 0) { | |
3886 | *psig = EVP_PKEY_EC; | |
3887 | } else { | |
3888 | *phash = OBJ_sn2nid(str); | |
3889 | if (*phash == NID_undef) | |
3890 | *phash = OBJ_ln2nid(str); | |
3891 | } | |
3892 | } | |
3893 | ||
0f229cce | 3894 | static int sig_cb(const char *elem, int len, void *arg) |
0f113f3e MC |
3895 | { |
3896 | sig_cb_st *sarg = arg; | |
3897 | size_t i; | |
3898 | char etmp[20], *p; | |
431f458d | 3899 | int sig_alg = NID_undef, hash_alg = NID_undef; |
2747d73c KR |
3900 | if (elem == NULL) |
3901 | return 0; | |
0f113f3e MC |
3902 | if (sarg->sigalgcnt == MAX_SIGALGLEN) |
3903 | return 0; | |
3904 | if (len > (int)(sizeof(etmp) - 1)) | |
3905 | return 0; | |
3906 | memcpy(etmp, elem, len); | |
3907 | etmp[len] = 0; | |
3908 | p = strchr(etmp, '+'); | |
3909 | if (!p) | |
3910 | return 0; | |
3911 | *p = 0; | |
3912 | p++; | |
3913 | if (!*p) | |
3914 | return 0; | |
3915 | ||
431f458d DSH |
3916 | get_sigorhash(&sig_alg, &hash_alg, etmp); |
3917 | get_sigorhash(&sig_alg, &hash_alg, p); | |
0f113f3e | 3918 | |
431f458d | 3919 | if (sig_alg == NID_undef || hash_alg == NID_undef) |
0f113f3e MC |
3920 | return 0; |
3921 | ||
3922 | for (i = 0; i < sarg->sigalgcnt; i += 2) { | |
3923 | if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg) | |
3924 | return 0; | |
3925 | } | |
3926 | sarg->sigalgs[sarg->sigalgcnt++] = hash_alg; | |
3927 | sarg->sigalgs[sarg->sigalgcnt++] = sig_alg; | |
3928 | return 1; | |
3929 | } | |
3930 | ||
3931 | /* | |
9d22666e | 3932 | * Set supported signature algorithms based on a colon separated list of the |
0f113f3e MC |
3933 | * form sig+hash e.g. RSA+SHA512:DSA+SHA512 |
3934 | */ | |
3dbc46df | 3935 | int tls1_set_sigalgs_list(CERT *c, const char *str, int client) |
0f113f3e MC |
3936 | { |
3937 | sig_cb_st sig; | |
3938 | sig.sigalgcnt = 0; | |
3939 | if (!CONF_parse_list(str, ':', 1, sig_cb, &sig)) | |
3940 | return 0; | |
3941 | if (c == NULL) | |
3942 | return 1; | |
3943 | return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client); | |
3944 | } | |
3945 | ||
a230b26e | 3946 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
0f113f3e MC |
3947 | { |
3948 | unsigned char *sigalgs, *sptr; | |
3949 | int rhash, rsign; | |
3950 | size_t i; | |
3951 | if (salglen & 1) | |
3952 | return 0; | |
3953 | sigalgs = OPENSSL_malloc(salglen); | |
3954 | if (sigalgs == NULL) | |
3955 | return 0; | |
3956 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { | |
b6eb9827 DSH |
3957 | rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md)); |
3958 | rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig)); | |
0f113f3e MC |
3959 | |
3960 | if (rhash == -1 || rsign == -1) | |
3961 | goto err; | |
3962 | *sptr++ = rhash; | |
3963 | *sptr++ = rsign; | |
3964 | } | |
3965 | ||
3966 | if (client) { | |
b548a1f1 | 3967 | OPENSSL_free(c->client_sigalgs); |
0f113f3e MC |
3968 | c->client_sigalgs = sigalgs; |
3969 | c->client_sigalgslen = salglen; | |
3970 | } else { | |
b548a1f1 | 3971 | OPENSSL_free(c->conf_sigalgs); |
0f113f3e MC |
3972 | c->conf_sigalgs = sigalgs; |
3973 | c->conf_sigalgslen = salglen; | |
3974 | } | |
3975 | ||
3976 | return 1; | |
3977 | ||
3978 | err: | |
3979 | OPENSSL_free(sigalgs); | |
3980 | return 0; | |
3981 | } | |
4453cd8c | 3982 | |
d61ff83b | 3983 | static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) |
0f113f3e MC |
3984 | { |
3985 | int sig_nid; | |
3986 | size_t i; | |
3987 | if (default_nid == -1) | |
3988 | return 1; | |
3989 | sig_nid = X509_get_signature_nid(x); | |
3990 | if (default_nid) | |
3991 | return sig_nid == default_nid ? 1 : 0; | |
3992 | for (i = 0; i < c->shared_sigalgslen; i++) | |
3993 | if (sig_nid == c->shared_sigalgs[i].signandhash_nid) | |
3994 | return 1; | |
3995 | return 0; | |
3996 | } | |
3997 | ||
6dbb6219 DSH |
3998 | /* Check to see if a certificate issuer name matches list of CA names */ |
3999 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) | |
0f113f3e MC |
4000 | { |
4001 | X509_NAME *nm; | |
4002 | int i; | |
4003 | nm = X509_get_issuer_name(x); | |
4004 | for (i = 0; i < sk_X509_NAME_num(names); i++) { | |
4005 | if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) | |
4006 | return 1; | |
4007 | } | |
4008 | return 0; | |
4009 | } | |
4010 | ||
4011 | /* | |
4012 | * Check certificate chain is consistent with TLS extensions and is usable by | |
4013 | * server. This servers two purposes: it allows users to check chains before | |
4014 | * passing them to the server and it allows the server to check chains before | |
4015 | * attempting to use them. | |
d61ff83b | 4016 | */ |
6dbb6219 DSH |
4017 | |
4018 | /* Flags which need to be set for a certificate when stict mode not set */ | |
4019 | ||
e481f9b9 | 4020 | #define CERT_PKEY_VALID_FLAGS \ |
0f113f3e | 4021 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
6dbb6219 | 4022 | /* Strict mode flags */ |
e481f9b9 | 4023 | #define CERT_PKEY_STRICT_FLAGS \ |
0f113f3e MC |
4024 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
4025 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) | |
6dbb6219 | 4026 | |
d61ff83b | 4027 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
0f113f3e MC |
4028 | int idx) |
4029 | { | |
4030 | int i; | |
4031 | int rv = 0; | |
4032 | int check_flags = 0, strict_mode; | |
4033 | CERT_PKEY *cpk = NULL; | |
4034 | CERT *c = s->cert; | |
f7d53487 | 4035 | uint32_t *pvalid; |
0f113f3e MC |
4036 | unsigned int suiteb_flags = tls1_suiteb(s); |
4037 | /* idx == -1 means checking server chains */ | |
4038 | if (idx != -1) { | |
4039 | /* idx == -2 means checking client certificate chains */ | |
4040 | if (idx == -2) { | |
4041 | cpk = c->key; | |
348240c6 | 4042 | idx = (int)(cpk - c->pkeys); |
0f113f3e MC |
4043 | } else |
4044 | cpk = c->pkeys + idx; | |
6383d316 | 4045 | pvalid = s->s3->tmp.valid_flags + idx; |
0f113f3e MC |
4046 | x = cpk->x509; |
4047 | pk = cpk->privatekey; | |
4048 | chain = cpk->chain; | |
4049 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; | |
4050 | /* If no cert or key, forget it */ | |
4051 | if (!x || !pk) | |
4052 | goto end; | |
0f113f3e MC |
4053 | } else { |
4054 | if (!x || !pk) | |
d813f9eb | 4055 | return 0; |
0f113f3e MC |
4056 | idx = ssl_cert_type(x, pk); |
4057 | if (idx == -1) | |
d813f9eb | 4058 | return 0; |
6383d316 DSH |
4059 | pvalid = s->s3->tmp.valid_flags + idx; |
4060 | ||
0f113f3e MC |
4061 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
4062 | check_flags = CERT_PKEY_STRICT_FLAGS; | |
4063 | else | |
4064 | check_flags = CERT_PKEY_VALID_FLAGS; | |
4065 | strict_mode = 1; | |
4066 | } | |
4067 | ||
4068 | if (suiteb_flags) { | |
4069 | int ok; | |
4070 | if (check_flags) | |
4071 | check_flags |= CERT_PKEY_SUITEB; | |
4072 | ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags); | |
4073 | if (ok == X509_V_OK) | |
4074 | rv |= CERT_PKEY_SUITEB; | |
4075 | else if (!check_flags) | |
4076 | goto end; | |
4077 | } | |
4078 | ||
4079 | /* | |
4080 | * Check all signature algorithms are consistent with signature | |
4081 | * algorithms extension if TLS 1.2 or later and strict mode. | |
4082 | */ | |
4083 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) { | |
4084 | int default_nid; | |
4085 | unsigned char rsign = 0; | |
76106e60 | 4086 | if (s->s3->tmp.peer_sigalgs) |
0f113f3e MC |
4087 | default_nid = 0; |
4088 | /* If no sigalgs extension use defaults from RFC5246 */ | |
4089 | else { | |
4090 | switch (idx) { | |
4091 | case SSL_PKEY_RSA_ENC: | |
4092 | case SSL_PKEY_RSA_SIGN: | |
0f113f3e MC |
4093 | rsign = TLSEXT_signature_rsa; |
4094 | default_nid = NID_sha1WithRSAEncryption; | |
4095 | break; | |
4096 | ||
4097 | case SSL_PKEY_DSA_SIGN: | |
0f113f3e MC |
4098 | rsign = TLSEXT_signature_dsa; |
4099 | default_nid = NID_dsaWithSHA1; | |
4100 | break; | |
4101 | ||
4102 | case SSL_PKEY_ECC: | |
4103 | rsign = TLSEXT_signature_ecdsa; | |
4104 | default_nid = NID_ecdsa_with_SHA1; | |
4105 | break; | |
4106 | ||
e44380a9 DB |
4107 | case SSL_PKEY_GOST01: |
4108 | rsign = TLSEXT_signature_gostr34102001; | |
4109 | default_nid = NID_id_GostR3411_94_with_GostR3410_2001; | |
4110 | break; | |
4111 | ||
4112 | case SSL_PKEY_GOST12_256: | |
4113 | rsign = TLSEXT_signature_gostr34102012_256; | |
4114 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256; | |
4115 | break; | |
4116 | ||
4117 | case SSL_PKEY_GOST12_512: | |
4118 | rsign = TLSEXT_signature_gostr34102012_512; | |
4119 | default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512; | |
4120 | break; | |
4121 | ||
0f113f3e MC |
4122 | default: |
4123 | default_nid = -1; | |
4124 | break; | |
4125 | } | |
4126 | } | |
4127 | /* | |
4128 | * If peer sent no signature algorithms extension and we have set | |
4129 | * preferred signature algorithms check we support sha1. | |
4130 | */ | |
4131 | if (default_nid > 0 && c->conf_sigalgs) { | |
4132 | size_t j; | |
4133 | const unsigned char *p = c->conf_sigalgs; | |
4134 | for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) { | |
4135 | if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign) | |
4136 | break; | |
4137 | } | |
4138 | if (j == c->conf_sigalgslen) { | |
4139 | if (check_flags) | |
4140 | goto skip_sigs; | |
4141 | else | |
4142 | goto end; | |
4143 | } | |
4144 | } | |
4145 | /* Check signature algorithm of each cert in chain */ | |
4146 | if (!tls1_check_sig_alg(c, x, default_nid)) { | |
4147 | if (!check_flags) | |
4148 | goto end; | |
4149 | } else | |
4150 | rv |= CERT_PKEY_EE_SIGNATURE; | |
4151 | rv |= CERT_PKEY_CA_SIGNATURE; | |
4152 | for (i = 0; i < sk_X509_num(chain); i++) { | |
4153 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) { | |
4154 | if (check_flags) { | |
4155 | rv &= ~CERT_PKEY_CA_SIGNATURE; | |
4156 | break; | |
4157 | } else | |
4158 | goto end; | |
4159 | } | |
4160 | } | |
4161 | } | |
4162 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ | |
4163 | else if (check_flags) | |
4164 | rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE; | |
4165 | skip_sigs: | |
4166 | /* Check cert parameters are consistent */ | |
4167 | if (tls1_check_cert_param(s, x, check_flags ? 1 : 2)) | |
4168 | rv |= CERT_PKEY_EE_PARAM; | |
4169 | else if (!check_flags) | |
4170 | goto end; | |
4171 | if (!s->server) | |
4172 | rv |= CERT_PKEY_CA_PARAM; | |
4173 | /* In strict mode check rest of chain too */ | |
4174 | else if (strict_mode) { | |
4175 | rv |= CERT_PKEY_CA_PARAM; | |
4176 | for (i = 0; i < sk_X509_num(chain); i++) { | |
4177 | X509 *ca = sk_X509_value(chain, i); | |
4178 | if (!tls1_check_cert_param(s, ca, 0)) { | |
4179 | if (check_flags) { | |
4180 | rv &= ~CERT_PKEY_CA_PARAM; | |
4181 | break; | |
4182 | } else | |
4183 | goto end; | |
4184 | } | |
4185 | } | |
4186 | } | |
4187 | if (!s->server && strict_mode) { | |
4188 | STACK_OF(X509_NAME) *ca_dn; | |
4189 | int check_type = 0; | |
3aeb9348 | 4190 | switch (EVP_PKEY_id(pk)) { |
0f113f3e MC |
4191 | case EVP_PKEY_RSA: |
4192 | check_type = TLS_CT_RSA_SIGN; | |
4193 | break; | |
4194 | case EVP_PKEY_DSA: | |
4195 | check_type = TLS_CT_DSS_SIGN; | |
4196 | break; | |
4197 | case EVP_PKEY_EC: | |
4198 | check_type = TLS_CT_ECDSA_SIGN; | |
4199 | break; | |
0f113f3e MC |
4200 | } |
4201 | if (check_type) { | |
4202 | const unsigned char *ctypes; | |
4203 | int ctypelen; | |
4204 | if (c->ctypes) { | |
4205 | ctypes = c->ctypes; | |
4206 | ctypelen = (int)c->ctype_num; | |
4207 | } else { | |
4208 | ctypes = (unsigned char *)s->s3->tmp.ctype; | |
4209 | ctypelen = s->s3->tmp.ctype_num; | |
4210 | } | |
4211 | for (i = 0; i < ctypelen; i++) { | |
4212 | if (ctypes[i] == check_type) { | |
4213 | rv |= CERT_PKEY_CERT_TYPE; | |
4214 | break; | |
4215 | } | |
4216 | } | |
4217 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) | |
4218 | goto end; | |
4219 | } else | |
4220 | rv |= CERT_PKEY_CERT_TYPE; | |
4221 | ||
4222 | ca_dn = s->s3->tmp.ca_names; | |
4223 | ||
4224 | if (!sk_X509_NAME_num(ca_dn)) | |
4225 | rv |= CERT_PKEY_ISSUER_NAME; | |
4226 | ||
4227 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
4228 | if (ssl_check_ca_name(ca_dn, x)) | |
4229 | rv |= CERT_PKEY_ISSUER_NAME; | |
4230 | } | |
4231 | if (!(rv & CERT_PKEY_ISSUER_NAME)) { | |
4232 | for (i = 0; i < sk_X509_num(chain); i++) { | |
4233 | X509 *xtmp = sk_X509_value(chain, i); | |
4234 | if (ssl_check_ca_name(ca_dn, xtmp)) { | |
4235 | rv |= CERT_PKEY_ISSUER_NAME; | |
4236 | break; | |
4237 | } | |
4238 | } | |
4239 | } | |
4240 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) | |
4241 | goto end; | |
4242 | } else | |
4243 | rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE; | |
4244 | ||
4245 | if (!check_flags || (rv & check_flags) == check_flags) | |
4246 | rv |= CERT_PKEY_VALID; | |
4247 | ||
4248 | end: | |
4249 | ||
4250 | if (TLS1_get_version(s) >= TLS1_2_VERSION) { | |
6383d316 | 4251 | if (*pvalid & CERT_PKEY_EXPLICIT_SIGN) |
0f113f3e | 4252 | rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN; |
d376e57d | 4253 | else if (s->s3->tmp.md[idx] != NULL) |
0f113f3e MC |
4254 | rv |= CERT_PKEY_SIGN; |
4255 | } else | |
4256 | rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN; | |
4257 | ||
4258 | /* | |
4259 | * When checking a CERT_PKEY structure all flags are irrelevant if the | |
4260 | * chain is invalid. | |
4261 | */ | |
4262 | if (!check_flags) { | |
4263 | if (rv & CERT_PKEY_VALID) | |
6383d316 | 4264 | *pvalid = rv; |
0f113f3e MC |
4265 | else { |
4266 | /* Preserve explicit sign flag, clear rest */ | |
6383d316 | 4267 | *pvalid &= CERT_PKEY_EXPLICIT_SIGN; |
0f113f3e MC |
4268 | return 0; |
4269 | } | |
4270 | } | |
4271 | return rv; | |
4272 | } | |
d61ff83b DSH |
4273 | |
4274 | /* Set validity of certificates in an SSL structure */ | |
4275 | void tls1_set_cert_validity(SSL *s) | |
0f113f3e | 4276 | { |
17dd65e6 MC |
4277 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC); |
4278 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN); | |
4279 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN); | |
17dd65e6 | 4280 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
e44380a9 DB |
4281 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01); |
4282 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256); | |
4283 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512); | |
0f113f3e MC |
4284 | } |
4285 | ||
18d71588 DSH |
4286 | /* User level utiity function to check a chain is suitable */ |
4287 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) | |
0f113f3e MC |
4288 | { |
4289 | return tls1_check_chain(s, x, pk, chain, -1); | |
4290 | } | |
d61ff83b | 4291 | |
09599b52 DSH |
4292 | #ifndef OPENSSL_NO_DH |
4293 | DH *ssl_get_auto_dh(SSL *s) | |
0f113f3e MC |
4294 | { |
4295 | int dh_secbits = 80; | |
4296 | if (s->cert->dh_tmp_auto == 2) | |
4297 | return DH_get_1024_160(); | |
adc5506a | 4298 | if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { |
0f113f3e MC |
4299 | if (s->s3->tmp.new_cipher->strength_bits == 256) |
4300 | dh_secbits = 128; | |
4301 | else | |
4302 | dh_secbits = 80; | |
4303 | } else { | |
4304 | CERT_PKEY *cpk = ssl_get_server_send_pkey(s); | |
4305 | dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); | |
4306 | } | |
4307 | ||
4308 | if (dh_secbits >= 128) { | |
4309 | DH *dhp = DH_new(); | |
0aeddcfa | 4310 | BIGNUM *p, *g; |
a71edf3b | 4311 | if (dhp == NULL) |
0f113f3e | 4312 | return NULL; |
0aeddcfa MC |
4313 | g = BN_new(); |
4314 | if (g != NULL) | |
4315 | BN_set_word(g, 2); | |
0f113f3e | 4316 | if (dh_secbits >= 192) |
9021a5df | 4317 | p = BN_get_rfc3526_prime_8192(NULL); |
0f113f3e | 4318 | else |
9021a5df | 4319 | p = BN_get_rfc3526_prime_3072(NULL); |
0aeddcfa | 4320 | if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) { |
0f113f3e | 4321 | DH_free(dhp); |
0aeddcfa MC |
4322 | BN_free(p); |
4323 | BN_free(g); | |
0f113f3e MC |
4324 | return NULL; |
4325 | } | |
4326 | return dhp; | |
4327 | } | |
4328 | if (dh_secbits >= 112) | |
4329 | return DH_get_2048_224(); | |
4330 | return DH_get_1024_160(); | |
4331 | } | |
09599b52 | 4332 | #endif |
b362ccab DSH |
4333 | |
4334 | static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e | 4335 | { |
72245f34 | 4336 | int secbits = -1; |
8382fd3a | 4337 | EVP_PKEY *pkey = X509_get0_pubkey(x); |
0f113f3e | 4338 | if (pkey) { |
72245f34 DSH |
4339 | /* |
4340 | * If no parameters this will return -1 and fail using the default | |
4341 | * security callback for any non-zero security level. This will | |
4342 | * reject keys which omit parameters but this only affects DSA and | |
4343 | * omission of parameters is never (?) done in practice. | |
4344 | */ | |
0f113f3e | 4345 | secbits = EVP_PKEY_security_bits(pkey); |
72245f34 | 4346 | } |
0f113f3e MC |
4347 | if (s) |
4348 | return ssl_security(s, op, secbits, 0, x); | |
4349 | else | |
4350 | return ssl_ctx_security(ctx, op, secbits, 0, x); | |
4351 | } | |
b362ccab DSH |
4352 | |
4353 | static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op) | |
0f113f3e MC |
4354 | { |
4355 | /* Lookup signature algorithm digest */ | |
4356 | int secbits = -1, md_nid = NID_undef, sig_nid; | |
221c7b55 DSH |
4357 | /* Don't check signature if self signed */ |
4358 | if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0) | |
4359 | return 1; | |
0f113f3e MC |
4360 | sig_nid = X509_get_signature_nid(x); |
4361 | if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) { | |
4362 | const EVP_MD *md; | |
4363 | if (md_nid && (md = EVP_get_digestbynid(md_nid))) | |
4364 | secbits = EVP_MD_size(md) * 4; | |
4365 | } | |
4366 | if (s) | |
4367 | return ssl_security(s, op, secbits, md_nid, x); | |
4368 | else | |
4369 | return ssl_ctx_security(ctx, op, secbits, md_nid, x); | |
4370 | } | |
b362ccab DSH |
4371 | |
4372 | int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee) | |
0f113f3e MC |
4373 | { |
4374 | if (vfy) | |
4375 | vfy = SSL_SECOP_PEER; | |
4376 | if (is_ee) { | |
4377 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy)) | |
4378 | return SSL_R_EE_KEY_TOO_SMALL; | |
4379 | } else { | |
4380 | if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy)) | |
4381 | return SSL_R_CA_KEY_TOO_SMALL; | |
4382 | } | |
4383 | if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy)) | |
4384 | return SSL_R_CA_MD_TOO_WEAK; | |
4385 | return 1; | |
4386 | } | |
4387 | ||
4388 | /* | |
4389 | * Check security of a chain, if sk includes the end entity certificate then | |
4390 | * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending | |
4391 | * one to the peer. Return values: 1 if ok otherwise error code to use | |
b362ccab DSH |
4392 | */ |
4393 | ||
4394 | int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy) | |
0f113f3e MC |
4395 | { |
4396 | int rv, start_idx, i; | |
4397 | if (x == NULL) { | |
4398 | x = sk_X509_value(sk, 0); | |
4399 | start_idx = 1; | |
4400 | } else | |
4401 | start_idx = 0; | |
4402 | ||
4403 | rv = ssl_security_cert(s, NULL, x, vfy, 1); | |
4404 | if (rv != 1) | |
4405 | return rv; | |
4406 | ||
4407 | for (i = start_idx; i < sk_X509_num(sk); i++) { | |
4408 | x = sk_X509_value(sk, i); | |
4409 | rv = ssl_security_cert(s, NULL, x, vfy, 0); | |
4410 | if (rv != 1) | |
4411 | return rv; | |
4412 | } | |
4413 | return 1; | |
4414 | } |