diff options
Diffstat (limited to 'src/lib/libssl/t1_lib.c')
-rw-r--r-- | src/lib/libssl/t1_lib.c | 2486 |
1 files changed, 0 insertions, 2486 deletions
diff --git a/src/lib/libssl/t1_lib.c b/src/lib/libssl/t1_lib.c deleted file mode 100644 index 9b7f54682b..0000000000 --- a/src/lib/libssl/t1_lib.c +++ /dev/null | |||
@@ -1,2486 +0,0 @@ | |||
1 | /* $OpenBSD: t1_lib.c,v 1.75 2015/03/02 13:43:09 jsing Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
18 | * If this package is used in a product, Eric Young should be given attribution | ||
19 | * as the author of the parts of the library used. | ||
20 | * This can be in the form of a textual message at program startup or | ||
21 | * in documentation (online or textual) provided with the package. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
38 | * the apps directory (application code) you must include an acknowledgement: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
53 | * The licence and distribution terms for any publically available version or | ||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | /* ==================================================================== | ||
59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. | ||
60 | * | ||
61 | * Redistribution and use in source and binary forms, with or without | ||
62 | * modification, are permitted provided that the following conditions | ||
63 | * are met: | ||
64 | * | ||
65 | * 1. Redistributions of source code must retain the above copyright | ||
66 | * notice, this list of conditions and the following disclaimer. | ||
67 | * | ||
68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
69 | * notice, this list of conditions and the following disclaimer in | ||
70 | * the documentation and/or other materials provided with the | ||
71 | * distribution. | ||
72 | * | ||
73 | * 3. All advertising materials mentioning features or use of this | ||
74 | * software must display the following acknowledgment: | ||
75 | * "This product includes software developed by the OpenSSL Project | ||
76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
77 | * | ||
78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
79 | * endorse or promote products derived from this software without | ||
80 | * prior written permission. For written permission, please contact | ||
81 | * openssl-core@openssl.org. | ||
82 | * | ||
83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
84 | * nor may "OpenSSL" appear in their names without prior written | ||
85 | * permission of the OpenSSL Project. | ||
86 | * | ||
87 | * 6. Redistributions of any form whatsoever must retain the following | ||
88 | * acknowledgment: | ||
89 | * "This product includes software developed by the OpenSSL Project | ||
90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
91 | * | ||
92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
104 | * ==================================================================== | ||
105 | * | ||
106 | * This product includes cryptographic software written by Eric Young | ||
107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
108 | * Hudson (tjh@cryptsoft.com). | ||
109 | * | ||
110 | */ | ||
111 | |||
112 | #include <stdio.h> | ||
113 | |||
114 | #include <openssl/evp.h> | ||
115 | #include <openssl/hmac.h> | ||
116 | #include <openssl/objects.h> | ||
117 | #include <openssl/ocsp.h> | ||
118 | |||
119 | #include "ssl_locl.h" | ||
120 | |||
121 | static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen, | ||
122 | const unsigned char *sess_id, int sesslen, | ||
123 | SSL_SESSION **psess); | ||
124 | |||
125 | SSL3_ENC_METHOD TLSv1_enc_data = { | ||
126 | .enc = tls1_enc, | ||
127 | .mac = tls1_mac, | ||
128 | .setup_key_block = tls1_setup_key_block, | ||
129 | .generate_master_secret = tls1_generate_master_secret, | ||
130 | .change_cipher_state = tls1_change_cipher_state, | ||
131 | .final_finish_mac = tls1_final_finish_mac, | ||
132 | .finish_mac_length = TLS1_FINISH_MAC_LENGTH, | ||
133 | .cert_verify_mac = tls1_cert_verify_mac, | ||
134 | .client_finished_label = TLS_MD_CLIENT_FINISH_CONST, | ||
135 | .client_finished_label_len = TLS_MD_CLIENT_FINISH_CONST_SIZE, | ||
136 | .server_finished_label = TLS_MD_SERVER_FINISH_CONST, | ||
137 | .server_finished_label_len = TLS_MD_SERVER_FINISH_CONST_SIZE, | ||
138 | .alert_value = tls1_alert_code, | ||
139 | .export_keying_material = tls1_export_keying_material, | ||
140 | .enc_flags = 0, | ||
141 | }; | ||
142 | |||
143 | SSL3_ENC_METHOD TLSv1_1_enc_data = { | ||
144 | .enc = tls1_enc, | ||
145 | .mac = tls1_mac, | ||
146 | .setup_key_block = tls1_setup_key_block, | ||
147 | .generate_master_secret = tls1_generate_master_secret, | ||
148 | .change_cipher_state = tls1_change_cipher_state, | ||
149 | .final_finish_mac = tls1_final_finish_mac, | ||
150 | .finish_mac_length = TLS1_FINISH_MAC_LENGTH, | ||
151 | .cert_verify_mac = tls1_cert_verify_mac, | ||
152 | .client_finished_label = TLS_MD_CLIENT_FINISH_CONST, | ||
153 | .client_finished_label_len = TLS_MD_CLIENT_FINISH_CONST_SIZE, | ||
154 | .server_finished_label = TLS_MD_SERVER_FINISH_CONST, | ||
155 | .server_finished_label_len = TLS_MD_SERVER_FINISH_CONST_SIZE, | ||
156 | .alert_value = tls1_alert_code, | ||
157 | .export_keying_material = tls1_export_keying_material, | ||
158 | .enc_flags = SSL_ENC_FLAG_EXPLICIT_IV, | ||
159 | }; | ||
160 | |||
161 | SSL3_ENC_METHOD TLSv1_2_enc_data = { | ||
162 | .enc = tls1_enc, | ||
163 | .mac = tls1_mac, | ||
164 | .setup_key_block = tls1_setup_key_block, | ||
165 | .generate_master_secret = tls1_generate_master_secret, | ||
166 | .change_cipher_state = tls1_change_cipher_state, | ||
167 | .final_finish_mac = tls1_final_finish_mac, | ||
168 | .finish_mac_length = TLS1_FINISH_MAC_LENGTH, | ||
169 | .cert_verify_mac = tls1_cert_verify_mac, | ||
170 | .client_finished_label = TLS_MD_CLIENT_FINISH_CONST, | ||
171 | .client_finished_label_len = TLS_MD_CLIENT_FINISH_CONST_SIZE, | ||
172 | .server_finished_label = TLS_MD_SERVER_FINISH_CONST, | ||
173 | .server_finished_label_len = TLS_MD_SERVER_FINISH_CONST_SIZE, | ||
174 | .alert_value = tls1_alert_code, | ||
175 | .export_keying_material = tls1_export_keying_material, | ||
176 | .enc_flags = SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS| | ||
177 | SSL_ENC_FLAG_SHA256_PRF|SSL_ENC_FLAG_TLS1_2_CIPHERS, | ||
178 | }; | ||
179 | |||
180 | long | ||
181 | tls1_default_timeout(void) | ||
182 | { | ||
183 | /* 2 hours, the 24 hours mentioned in the TLSv1 spec | ||
184 | * is way too long for http, the cache would over fill */ | ||
185 | return (60 * 60 * 2); | ||
186 | } | ||
187 | |||
188 | int | ||
189 | tls1_new(SSL *s) | ||
190 | { | ||
191 | if (!ssl3_new(s)) | ||
192 | return (0); | ||
193 | s->method->ssl_clear(s); | ||
194 | return (1); | ||
195 | } | ||
196 | |||
197 | void | ||
198 | tls1_free(SSL *s) | ||
199 | { | ||
200 | free(s->tlsext_session_ticket); | ||
201 | ssl3_free(s); | ||
202 | } | ||
203 | |||
204 | void | ||
205 | tls1_clear(SSL *s) | ||
206 | { | ||
207 | ssl3_clear(s); | ||
208 | s->version = s->method->version; | ||
209 | } | ||
210 | |||
211 | |||
212 | static int nid_list[] = { | ||
213 | NID_sect163k1, /* sect163k1 (1) */ | ||
214 | NID_sect163r1, /* sect163r1 (2) */ | ||
215 | NID_sect163r2, /* sect163r2 (3) */ | ||
216 | NID_sect193r1, /* sect193r1 (4) */ | ||
217 | NID_sect193r2, /* sect193r2 (5) */ | ||
218 | NID_sect233k1, /* sect233k1 (6) */ | ||
219 | NID_sect233r1, /* sect233r1 (7) */ | ||
220 | NID_sect239k1, /* sect239k1 (8) */ | ||
221 | NID_sect283k1, /* sect283k1 (9) */ | ||
222 | NID_sect283r1, /* sect283r1 (10) */ | ||
223 | NID_sect409k1, /* sect409k1 (11) */ | ||
224 | NID_sect409r1, /* sect409r1 (12) */ | ||
225 | NID_sect571k1, /* sect571k1 (13) */ | ||
226 | NID_sect571r1, /* sect571r1 (14) */ | ||
227 | NID_secp160k1, /* secp160k1 (15) */ | ||
228 | NID_secp160r1, /* secp160r1 (16) */ | ||
229 | NID_secp160r2, /* secp160r2 (17) */ | ||
230 | NID_secp192k1, /* secp192k1 (18) */ | ||
231 | NID_X9_62_prime192v1, /* secp192r1 (19) */ | ||
232 | NID_secp224k1, /* secp224k1 (20) */ | ||
233 | NID_secp224r1, /* secp224r1 (21) */ | ||
234 | NID_secp256k1, /* secp256k1 (22) */ | ||
235 | NID_X9_62_prime256v1, /* secp256r1 (23) */ | ||
236 | NID_secp384r1, /* secp384r1 (24) */ | ||
237 | NID_secp521r1, /* secp521r1 (25) */ | ||
238 | NID_brainpoolP256r1, /* brainpoolP256r1 (26) */ | ||
239 | NID_brainpoolP384r1, /* brainpoolP384r1 (27) */ | ||
240 | NID_brainpoolP512r1 /* brainpoolP512r1 (28) */ | ||
241 | }; | ||
242 | |||
243 | static const uint8_t ecformats_default[] = { | ||
244 | TLSEXT_ECPOINTFORMAT_uncompressed, | ||
245 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime, | ||
246 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2 | ||
247 | }; | ||
248 | |||
249 | static const uint16_t eccurves_default[] = { | ||
250 | 14, /* sect571r1 (14) */ | ||
251 | 13, /* sect571k1 (13) */ | ||
252 | 25, /* secp521r1 (25) */ | ||
253 | 28, /* brainpool512r1 (28) */ | ||
254 | 11, /* sect409k1 (11) */ | ||
255 | 12, /* sect409r1 (12) */ | ||
256 | 27, /* brainpoolP384r1 (27) */ | ||
257 | 24, /* secp384r1 (24) */ | ||
258 | 9, /* sect283k1 (9) */ | ||
259 | 10, /* sect283r1 (10) */ | ||
260 | 26, /* brainpoolP256r1 (26) */ | ||
261 | 22, /* secp256k1 (22) */ | ||
262 | 23, /* secp256r1 (23) */ | ||
263 | 8, /* sect239k1 (8) */ | ||
264 | 6, /* sect233k1 (6) */ | ||
265 | 7, /* sect233r1 (7) */ | ||
266 | 20, /* secp224k1 (20) */ | ||
267 | 21, /* secp224r1 (21) */ | ||
268 | 4, /* sect193r1 (4) */ | ||
269 | 5, /* sect193r2 (5) */ | ||
270 | 18, /* secp192k1 (18) */ | ||
271 | 19, /* secp192r1 (19) */ | ||
272 | 1, /* sect163k1 (1) */ | ||
273 | 2, /* sect163r1 (2) */ | ||
274 | 3, /* sect163r2 (3) */ | ||
275 | 15, /* secp160k1 (15) */ | ||
276 | 16, /* secp160r1 (16) */ | ||
277 | 17, /* secp160r2 (17) */ | ||
278 | }; | ||
279 | |||
280 | int | ||
281 | tls1_ec_curve_id2nid(uint16_t curve_id) | ||
282 | { | ||
283 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ | ||
284 | if ((curve_id < 1) || | ||
285 | ((unsigned int)curve_id > sizeof(nid_list) / sizeof(nid_list[0]))) | ||
286 | return 0; | ||
287 | return nid_list[curve_id - 1]; | ||
288 | } | ||
289 | |||
290 | uint16_t | ||
291 | tls1_ec_nid2curve_id(int nid) | ||
292 | { | ||
293 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ | ||
294 | switch (nid) { | ||
295 | case NID_sect163k1: /* sect163k1 (1) */ | ||
296 | return 1; | ||
297 | case NID_sect163r1: /* sect163r1 (2) */ | ||
298 | return 2; | ||
299 | case NID_sect163r2: /* sect163r2 (3) */ | ||
300 | return 3; | ||
301 | case NID_sect193r1: /* sect193r1 (4) */ | ||
302 | return 4; | ||
303 | case NID_sect193r2: /* sect193r2 (5) */ | ||
304 | return 5; | ||
305 | case NID_sect233k1: /* sect233k1 (6) */ | ||
306 | return 6; | ||
307 | case NID_sect233r1: /* sect233r1 (7) */ | ||
308 | return 7; | ||
309 | case NID_sect239k1: /* sect239k1 (8) */ | ||
310 | return 8; | ||
311 | case NID_sect283k1: /* sect283k1 (9) */ | ||
312 | return 9; | ||
313 | case NID_sect283r1: /* sect283r1 (10) */ | ||
314 | return 10; | ||
315 | case NID_sect409k1: /* sect409k1 (11) */ | ||
316 | return 11; | ||
317 | case NID_sect409r1: /* sect409r1 (12) */ | ||
318 | return 12; | ||
319 | case NID_sect571k1: /* sect571k1 (13) */ | ||
320 | return 13; | ||
321 | case NID_sect571r1: /* sect571r1 (14) */ | ||
322 | return 14; | ||
323 | case NID_secp160k1: /* secp160k1 (15) */ | ||
324 | return 15; | ||
325 | case NID_secp160r1: /* secp160r1 (16) */ | ||
326 | return 16; | ||
327 | case NID_secp160r2: /* secp160r2 (17) */ | ||
328 | return 17; | ||
329 | case NID_secp192k1: /* secp192k1 (18) */ | ||
330 | return 18; | ||
331 | case NID_X9_62_prime192v1: /* secp192r1 (19) */ | ||
332 | return 19; | ||
333 | case NID_secp224k1: /* secp224k1 (20) */ | ||
334 | return 20; | ||
335 | case NID_secp224r1: /* secp224r1 (21) */ | ||
336 | return 21; | ||
337 | case NID_secp256k1: /* secp256k1 (22) */ | ||
338 | return 22; | ||
339 | case NID_X9_62_prime256v1: /* secp256r1 (23) */ | ||
340 | return 23; | ||
341 | case NID_secp384r1: /* secp384r1 (24) */ | ||
342 | return 24; | ||
343 | case NID_secp521r1: /* secp521r1 (25) */ | ||
344 | return 25; | ||
345 | case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */ | ||
346 | return 26; | ||
347 | case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */ | ||
348 | return 27; | ||
349 | case NID_brainpoolP512r1: /* brainpoolP512r1 (28) */ | ||
350 | return 28; | ||
351 | default: | ||
352 | return 0; | ||
353 | } | ||
354 | } | ||
355 | |||
356 | /* | ||
357 | * Return the appropriate format list. If client_formats is non-zero, return | ||
358 | * the client/session formats. Otherwise return the custom format list if one | ||
359 | * exists, or the default formats if a custom list has not been specified. | ||
360 | */ | ||
361 | static void | ||
362 | tls1_get_formatlist(SSL *s, int client_formats, const uint8_t **pformats, | ||
363 | size_t *pformatslen) | ||
364 | { | ||
365 | if (client_formats != 0) { | ||
366 | *pformats = s->session->tlsext_ecpointformatlist; | ||
367 | *pformatslen = s->session->tlsext_ecpointformatlist_length; | ||
368 | return; | ||
369 | } | ||
370 | |||
371 | *pformats = s->tlsext_ecpointformatlist; | ||
372 | *pformatslen = s->tlsext_ecpointformatlist_length; | ||
373 | if (*pformats == NULL) { | ||
374 | *pformats = ecformats_default; | ||
375 | *pformatslen = sizeof(ecformats_default); | ||
376 | } | ||
377 | } | ||
378 | |||
379 | /* | ||
380 | * Return the appropriate curve list. If client_curves is non-zero, return | ||
381 | * the client/session curves. Otherwise return the custom curve list if one | ||
382 | * exists, or the default curves if a custom list has not been specified. | ||
383 | */ | ||
384 | static void | ||
385 | tls1_get_curvelist(SSL *s, int client_curves, const uint16_t **pcurves, | ||
386 | size_t *pcurveslen) | ||
387 | { | ||
388 | if (client_curves != 0) { | ||
389 | *pcurves = s->session->tlsext_ellipticcurvelist; | ||
390 | *pcurveslen = s->session->tlsext_ellipticcurvelist_length; | ||
391 | return; | ||
392 | } | ||
393 | |||
394 | *pcurves = s->tlsext_ellipticcurvelist; | ||
395 | *pcurveslen = s->tlsext_ellipticcurvelist_length; | ||
396 | if (*pcurves == NULL) { | ||
397 | *pcurves = eccurves_default; | ||
398 | *pcurveslen = sizeof(eccurves_default) / 2; | ||
399 | } | ||
400 | } | ||
401 | |||
402 | /* Check that a curve is one of our preferences. */ | ||
403 | int | ||
404 | tls1_check_curve(SSL *s, const unsigned char *p, size_t len) | ||
405 | { | ||
406 | const uint16_t *curves; | ||
407 | size_t curveslen, i; | ||
408 | uint16_t cid; | ||
409 | |||
410 | /* Only named curves are supported. */ | ||
411 | if (len != 3 || p[0] != NAMED_CURVE_TYPE) | ||
412 | return (0); | ||
413 | |||
414 | cid = (p[1] << 8) | p[2]; | ||
415 | |||
416 | tls1_get_curvelist(s, 0, &curves, &curveslen); | ||
417 | |||
418 | for (i = 0; i < curveslen; i++) { | ||
419 | if (curves[i] == cid) | ||
420 | return (1); | ||
421 | } | ||
422 | return (0); | ||
423 | } | ||
424 | |||
425 | int | ||
426 | tls1_get_shared_curve(SSL *s) | ||
427 | { | ||
428 | size_t preflen, supplen, i, j; | ||
429 | const uint16_t *pref, *supp; | ||
430 | unsigned long server_pref; | ||
431 | |||
432 | /* Cannot do anything on the client side. */ | ||
433 | if (s->server == 0) | ||
434 | return (NID_undef); | ||
435 | |||
436 | /* Return first preference shared curve. */ | ||
437 | server_pref = (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE); | ||
438 | tls1_get_curvelist(s, (server_pref == 0), &pref, &preflen); | ||
439 | tls1_get_curvelist(s, (server_pref != 0), &supp, &supplen); | ||
440 | |||
441 | for (i = 0; i < preflen; i++) { | ||
442 | for (j = 0; j < supplen; j++) { | ||
443 | if (pref[i] == supp[j]) | ||
444 | return (tls1_ec_curve_id2nid(pref[i])); | ||
445 | } | ||
446 | } | ||
447 | return (NID_undef); | ||
448 | } | ||
449 | |||
450 | /* For an EC key set TLS ID and required compression based on parameters. */ | ||
451 | static int | ||
452 | tls1_set_ec_id(uint16_t *curve_id, uint8_t *comp_id, EC_KEY *ec) | ||
453 | { | ||
454 | const EC_GROUP *grp; | ||
455 | const EC_METHOD *meth; | ||
456 | int is_prime = 0; | ||
457 | int nid, id; | ||
458 | |||
459 | if (ec == NULL) | ||
460 | return (0); | ||
461 | |||
462 | /* Determine if it is a prime field. */ | ||
463 | if ((grp = EC_KEY_get0_group(ec)) == NULL) | ||
464 | return (0); | ||
465 | if ((meth = EC_GROUP_method_of(grp)) == NULL) | ||
466 | return (0); | ||
467 | if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field) | ||
468 | is_prime = 1; | ||
469 | |||
470 | /* Determine curve ID. */ | ||
471 | nid = EC_GROUP_get_curve_name(grp); | ||
472 | id = tls1_ec_nid2curve_id(nid); | ||
473 | |||
474 | /* If we have an ID set it, otherwise set arbitrary explicit curve. */ | ||
475 | if (id != 0) | ||
476 | *curve_id = id; | ||
477 | else | ||
478 | *curve_id = is_prime ? 0xff01 : 0xff02; | ||
479 | |||
480 | /* Specify the compression identifier. */ | ||
481 | if (comp_id != NULL) { | ||
482 | if (EC_KEY_get0_public_key(ec) == NULL) | ||
483 | return (0); | ||
484 | |||
485 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { | ||
486 | *comp_id = is_prime ? | ||
487 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime : | ||
488 | TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | ||
489 | } else { | ||
490 | *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; | ||
491 | } | ||
492 | } | ||
493 | return (1); | ||
494 | } | ||
495 | |||
496 | /* Check that an EC key is compatible with extensions. */ | ||
497 | static int | ||
498 | tls1_check_ec_key(SSL *s, const uint16_t *curve_id, const uint8_t *comp_id) | ||
499 | { | ||
500 | size_t curveslen, formatslen, i; | ||
501 | const uint16_t *curves; | ||
502 | const uint8_t *formats; | ||
503 | |||
504 | /* | ||
505 | * Check point formats extension if present, otherwise everything | ||
506 | * is supported (see RFC4492). | ||
507 | */ | ||
508 | tls1_get_formatlist(s, 1, &formats, &formatslen); | ||
509 | if (comp_id != NULL && formats != NULL) { | ||
510 | for (i = 0; i < formatslen; i++) { | ||
511 | if (formats[i] == *comp_id) | ||
512 | break; | ||
513 | } | ||
514 | if (i == formatslen) | ||
515 | return (0); | ||
516 | } | ||
517 | |||
518 | /* | ||
519 | * Check curve list if present, otherwise everything is supported. | ||
520 | */ | ||
521 | tls1_get_curvelist(s, 1, &curves, &curveslen); | ||
522 | if (curve_id != NULL && curves != NULL) { | ||
523 | for (i = 0; i < curveslen; i++) { | ||
524 | if (curves[i] == *curve_id) | ||
525 | break; | ||
526 | } | ||
527 | if (i == curveslen) | ||
528 | return (0); | ||
529 | } | ||
530 | |||
531 | return (1); | ||
532 | } | ||
533 | |||
534 | /* Check EC server key is compatible with client extensions. */ | ||
535 | int | ||
536 | tls1_check_ec_server_key(SSL *s) | ||
537 | { | ||
538 | CERT_PKEY *cpk = s->cert->pkeys + SSL_PKEY_ECC; | ||
539 | uint16_t curve_id; | ||
540 | uint8_t comp_id; | ||
541 | EVP_PKEY *pkey; | ||
542 | int rv; | ||
543 | |||
544 | if (cpk->x509 == NULL || cpk->privatekey == NULL) | ||
545 | return (0); | ||
546 | if ((pkey = X509_get_pubkey(cpk->x509)) == NULL) | ||
547 | return (0); | ||
548 | rv = tls1_set_ec_id(&curve_id, &comp_id, pkey->pkey.ec); | ||
549 | EVP_PKEY_free(pkey); | ||
550 | if (rv != 1) | ||
551 | return (0); | ||
552 | |||
553 | return tls1_check_ec_key(s, &curve_id, &comp_id); | ||
554 | } | ||
555 | |||
556 | /* Check EC temporary key is compatible with client extensions. */ | ||
557 | int | ||
558 | tls1_check_ec_tmp_key(SSL *s) | ||
559 | { | ||
560 | EC_KEY *ec = s->cert->ecdh_tmp; | ||
561 | uint16_t curve_id; | ||
562 | |||
563 | if (s->cert->ecdh_tmp_auto != 0) { | ||
564 | /* Need a shared curve. */ | ||
565 | if (tls1_get_shared_curve(s) != NID_undef) | ||
566 | return (1); | ||
567 | return (0); | ||
568 | } | ||
569 | |||
570 | if (ec == NULL) { | ||
571 | if (s->cert->ecdh_tmp_cb != NULL) | ||
572 | return (1); | ||
573 | return (0); | ||
574 | } | ||
575 | if (tls1_set_ec_id(&curve_id, NULL, ec) != 1) | ||
576 | return (0); | ||
577 | |||
578 | return tls1_check_ec_key(s, &curve_id, NULL); | ||
579 | } | ||
580 | |||
581 | /* | ||
582 | * List of supported signature algorithms and hashes. Should make this | ||
583 | * customisable at some point, for now include everything we support. | ||
584 | */ | ||
585 | |||
586 | static unsigned char tls12_sigalgs[] = { | ||
587 | TLSEXT_hash_sha512, TLSEXT_signature_rsa, | ||
588 | TLSEXT_hash_sha512, TLSEXT_signature_dsa, | ||
589 | TLSEXT_hash_sha512, TLSEXT_signature_ecdsa, | ||
590 | #ifndef OPENSSL_NO_GOST | ||
591 | TLSEXT_hash_streebog_512, TLSEXT_signature_gostr12_512, | ||
592 | #endif | ||
593 | |||
594 | TLSEXT_hash_sha384, TLSEXT_signature_rsa, | ||
595 | TLSEXT_hash_sha384, TLSEXT_signature_dsa, | ||
596 | TLSEXT_hash_sha384, TLSEXT_signature_ecdsa, | ||
597 | |||
598 | TLSEXT_hash_sha256, TLSEXT_signature_rsa, | ||
599 | TLSEXT_hash_sha256, TLSEXT_signature_dsa, | ||
600 | TLSEXT_hash_sha256, TLSEXT_signature_ecdsa, | ||
601 | |||
602 | #ifndef OPENSSL_NO_GOST | ||
603 | TLSEXT_hash_streebog_256, TLSEXT_signature_gostr12_256, | ||
604 | TLSEXT_hash_gost94, TLSEXT_signature_gostr01, | ||
605 | #endif | ||
606 | |||
607 | TLSEXT_hash_sha224, TLSEXT_signature_rsa, | ||
608 | TLSEXT_hash_sha224, TLSEXT_signature_dsa, | ||
609 | TLSEXT_hash_sha224, TLSEXT_signature_ecdsa, | ||
610 | |||
611 | TLSEXT_hash_sha1, TLSEXT_signature_rsa, | ||
612 | TLSEXT_hash_sha1, TLSEXT_signature_dsa, | ||
613 | TLSEXT_hash_sha1, TLSEXT_signature_ecdsa, | ||
614 | }; | ||
615 | |||
616 | int | ||
617 | tls12_get_req_sig_algs(SSL *s, unsigned char *p) | ||
618 | { | ||
619 | size_t slen = sizeof(tls12_sigalgs); | ||
620 | |||
621 | if (p) | ||
622 | memcpy(p, tls12_sigalgs, slen); | ||
623 | return (int)slen; | ||
624 | } | ||
625 | |||
626 | unsigned char * | ||
627 | ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) | ||
628 | { | ||
629 | int extdatalen = 0; | ||
630 | unsigned char *ret = p; | ||
631 | int using_ecc = 0; | ||
632 | |||
633 | /* See if we support any ECC ciphersuites. */ | ||
634 | if (s->version != DTLS1_VERSION && s->version >= TLS1_VERSION) { | ||
635 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); | ||
636 | unsigned long alg_k, alg_a; | ||
637 | int i; | ||
638 | |||
639 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { | ||
640 | SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); | ||
641 | |||
642 | alg_k = c->algorithm_mkey; | ||
643 | alg_a = c->algorithm_auth; | ||
644 | |||
645 | if ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe) || | ||
646 | (alg_a & SSL_aECDSA))) { | ||
647 | using_ecc = 1; | ||
648 | break; | ||
649 | } | ||
650 | } | ||
651 | } | ||
652 | |||
653 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ | ||
654 | if (s->client_version == SSL3_VERSION && | ||
655 | !s->s3->send_connection_binding) | ||
656 | return p; | ||
657 | |||
658 | ret += 2; | ||
659 | |||
660 | if (ret >= limit) | ||
661 | return NULL; /* this really never occurs, but ... */ | ||
662 | |||
663 | if (s->tlsext_hostname != NULL) { | ||
664 | /* Add TLS extension servername to the Client Hello message */ | ||
665 | size_t size_str, lenmax; | ||
666 | |||
667 | /* check for enough space. | ||
668 | 4 for the servername type and extension length | ||
669 | 2 for servernamelist length | ||
670 | 1 for the hostname type | ||
671 | 2 for hostname length | ||
672 | + hostname length | ||
673 | */ | ||
674 | |||
675 | if ((size_t)(limit - ret) < 9) | ||
676 | return NULL; | ||
677 | |||
678 | lenmax = limit - ret - 9; | ||
679 | if ((size_str = strlen(s->tlsext_hostname)) > lenmax) | ||
680 | return NULL; | ||
681 | |||
682 | /* extension type and length */ | ||
683 | s2n(TLSEXT_TYPE_server_name, ret); | ||
684 | |||
685 | s2n(size_str + 5, ret); | ||
686 | |||
687 | /* length of servername list */ | ||
688 | s2n(size_str + 3, ret); | ||
689 | |||
690 | /* hostname type, length and hostname */ | ||
691 | *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name; | ||
692 | s2n(size_str, ret); | ||
693 | memcpy(ret, s->tlsext_hostname, size_str); | ||
694 | ret += size_str; | ||
695 | } | ||
696 | |||
697 | /* Add RI if renegotiating */ | ||
698 | if (s->renegotiate) { | ||
699 | int el; | ||
700 | |||
701 | if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) { | ||
702 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | ||
703 | ERR_R_INTERNAL_ERROR); | ||
704 | return NULL; | ||
705 | } | ||
706 | |||
707 | if ((size_t)(limit - ret) < 4 + el) | ||
708 | return NULL; | ||
709 | |||
710 | s2n(TLSEXT_TYPE_renegotiate, ret); | ||
711 | s2n(el, ret); | ||
712 | |||
713 | if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) { | ||
714 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | ||
715 | ERR_R_INTERNAL_ERROR); | ||
716 | return NULL; | ||
717 | } | ||
718 | |||
719 | ret += el; | ||
720 | } | ||
721 | |||
722 | if (using_ecc) { | ||
723 | size_t curveslen, formatslen, lenmax; | ||
724 | const uint16_t *curves; | ||
725 | const uint8_t *formats; | ||
726 | int i; | ||
727 | |||
728 | /* | ||
729 | * Add TLS extension ECPointFormats to the ClientHello message. | ||
730 | */ | ||
731 | tls1_get_formatlist(s, 0, &formats, &formatslen); | ||
732 | |||
733 | if ((size_t)(limit - ret) < 5) | ||
734 | return NULL; | ||
735 | |||
736 | lenmax = limit - ret - 5; | ||
737 | if (formatslen > lenmax) | ||
738 | return NULL; | ||
739 | if (formatslen > 255) { | ||
740 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | ||
741 | ERR_R_INTERNAL_ERROR); | ||
742 | return NULL; | ||
743 | } | ||
744 | |||
745 | s2n(TLSEXT_TYPE_ec_point_formats, ret); | ||
746 | s2n(formatslen + 1, ret); | ||
747 | *(ret++) = (unsigned char)formatslen; | ||
748 | memcpy(ret, formats, formatslen); | ||
749 | ret += formatslen; | ||
750 | |||
751 | /* | ||
752 | * Add TLS extension EllipticCurves to the ClientHello message. | ||
753 | */ | ||
754 | tls1_get_curvelist(s, 0, &curves, &curveslen); | ||
755 | |||
756 | if ((size_t)(limit - ret) < 6) | ||
757 | return NULL; | ||
758 | |||
759 | lenmax = limit - ret - 6; | ||
760 | if (curveslen > lenmax) | ||
761 | return NULL; | ||
762 | if (curveslen > 65532) { | ||
763 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | ||
764 | ERR_R_INTERNAL_ERROR); | ||
765 | return NULL; | ||
766 | } | ||
767 | |||
768 | s2n(TLSEXT_TYPE_elliptic_curves, ret); | ||
769 | s2n((curveslen * 2) + 2, ret); | ||
770 | |||
771 | /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for | ||
772 | * elliptic_curve_list, but the examples use two bytes. | ||
773 | * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html | ||
774 | * resolves this to two bytes. | ||
775 | */ | ||
776 | s2n(curveslen * 2, ret); | ||
777 | for (i = 0; i < curveslen; i++) | ||
778 | s2n(curves[i], ret); | ||
779 | } | ||
780 | |||
781 | if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) { | ||
782 | int ticklen; | ||
783 | if (!s->new_session && s->session && s->session->tlsext_tick) | ||
784 | ticklen = s->session->tlsext_ticklen; | ||
785 | else if (s->session && s->tlsext_session_ticket && | ||
786 | s->tlsext_session_ticket->data) { | ||
787 | ticklen = s->tlsext_session_ticket->length; | ||
788 | s->session->tlsext_tick = malloc(ticklen); | ||
789 | if (!s->session->tlsext_tick) | ||
790 | return NULL; | ||
791 | memcpy(s->session->tlsext_tick, | ||
792 | s->tlsext_session_ticket->data, ticklen); | ||
793 | s->session->tlsext_ticklen = ticklen; | ||
794 | } else | ||
795 | ticklen = 0; | ||
796 | if (ticklen == 0 && s->tlsext_session_ticket && | ||
797 | s->tlsext_session_ticket->data == NULL) | ||
798 | goto skip_ext; | ||
799 | /* Check for enough room 2 for extension type, 2 for len | ||
800 | * rest for ticket | ||
801 | */ | ||
802 | if ((size_t)(limit - ret) < 4 + ticklen) | ||
803 | return NULL; | ||
804 | s2n(TLSEXT_TYPE_session_ticket, ret); | ||
805 | |||
806 | s2n(ticklen, ret); | ||
807 | if (ticklen) { | ||
808 | memcpy(ret, s->session->tlsext_tick, ticklen); | ||
809 | ret += ticklen; | ||
810 | } | ||
811 | } | ||
812 | skip_ext: | ||
813 | |||
814 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) { | ||
815 | if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6) | ||
816 | return NULL; | ||
817 | |||
818 | s2n(TLSEXT_TYPE_signature_algorithms, ret); | ||
819 | s2n(sizeof(tls12_sigalgs) + 2, ret); | ||
820 | s2n(sizeof(tls12_sigalgs), ret); | ||
821 | memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs)); | ||
822 | ret += sizeof(tls12_sigalgs); | ||
823 | } | ||
824 | |||
825 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp && | ||
826 | s->version != DTLS1_VERSION) { | ||
827 | int i; | ||
828 | long extlen, idlen, itmp; | ||
829 | OCSP_RESPID *id; | ||
830 | |||
831 | idlen = 0; | ||
832 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { | ||
833 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); | ||
834 | itmp = i2d_OCSP_RESPID(id, NULL); | ||
835 | if (itmp <= 0) | ||
836 | return NULL; | ||
837 | idlen += itmp + 2; | ||
838 | } | ||
839 | |||
840 | if (s->tlsext_ocsp_exts) { | ||
841 | extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); | ||
842 | if (extlen < 0) | ||
843 | return NULL; | ||
844 | } else | ||
845 | extlen = 0; | ||
846 | |||
847 | if ((size_t)(limit - ret) < 7 + extlen + idlen) | ||
848 | return NULL; | ||
849 | s2n(TLSEXT_TYPE_status_request, ret); | ||
850 | if (extlen + idlen > 0xFFF0) | ||
851 | return NULL; | ||
852 | s2n(extlen + idlen + 5, ret); | ||
853 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; | ||
854 | s2n(idlen, ret); | ||
855 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { | ||
856 | /* save position of id len */ | ||
857 | unsigned char *q = ret; | ||
858 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); | ||
859 | /* skip over id len */ | ||
860 | ret += 2; | ||
861 | itmp = i2d_OCSP_RESPID(id, &ret); | ||
862 | /* write id len */ | ||
863 | s2n(itmp, q); | ||
864 | } | ||
865 | s2n(extlen, ret); | ||
866 | if (extlen > 0) | ||
867 | i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret); | ||
868 | } | ||
869 | |||
870 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { | ||
871 | /* The client advertises an emtpy extension to indicate its | ||
872 | * support for Next Protocol Negotiation */ | ||
873 | if ((size_t)(limit - ret) < 4) | ||
874 | return NULL; | ||
875 | s2n(TLSEXT_TYPE_next_proto_neg, ret); | ||
876 | s2n(0, ret); | ||
877 | } | ||
878 | |||
879 | if (s->alpn_client_proto_list != NULL && | ||
880 | s->s3->tmp.finish_md_len == 0) { | ||
881 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) | ||
882 | return (NULL); | ||
883 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); | ||
884 | s2n(2 + s->alpn_client_proto_list_len, ret); | ||
885 | s2n(s->alpn_client_proto_list_len, ret); | ||
886 | memcpy(ret, s->alpn_client_proto_list, | ||
887 | s->alpn_client_proto_list_len); | ||
888 | ret += s->alpn_client_proto_list_len; | ||
889 | } | ||
890 | |||
891 | #ifndef OPENSSL_NO_SRTP | ||
892 | if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) { | ||
893 | int el; | ||
894 | |||
895 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); | ||
896 | |||
897 | if ((size_t)(limit - ret) < 4 + el) | ||
898 | return NULL; | ||
899 | |||
900 | s2n(TLSEXT_TYPE_use_srtp, ret); | ||
901 | s2n(el, ret); | ||
902 | |||
903 | if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) { | ||
904 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, | ||
905 | ERR_R_INTERNAL_ERROR); | ||
906 | return NULL; | ||
907 | } | ||
908 | ret += el; | ||
909 | } | ||
910 | #endif | ||
911 | |||
912 | /* | ||
913 | * Add padding to workaround bugs in F5 terminators. | ||
914 | * See https://tools.ietf.org/html/draft-agl-tls-padding-03 | ||
915 | * | ||
916 | * Note that this seems to trigger issues with IronPort SMTP | ||
917 | * appliances. | ||
918 | * | ||
919 | * NB: because this code works out the length of all existing | ||
920 | * extensions it MUST always appear last. | ||
921 | */ | ||
922 | if (s->options & SSL_OP_TLSEXT_PADDING) { | ||
923 | int hlen = ret - (unsigned char *)s->init_buf->data; | ||
924 | |||
925 | /* | ||
926 | * The code in s23_clnt.c to build ClientHello messages | ||
927 | * includes the 5-byte record header in the buffer, while the | ||
928 | * code in s3_clnt.c does not. | ||
929 | */ | ||
930 | if (s->state == SSL23_ST_CW_CLNT_HELLO_A) | ||
931 | hlen -= 5; | ||
932 | if (hlen > 0xff && hlen < 0x200) { | ||
933 | hlen = 0x200 - hlen; | ||
934 | if (hlen >= 4) | ||
935 | hlen -= 4; | ||
936 | else | ||
937 | hlen = 0; | ||
938 | |||
939 | s2n(TLSEXT_TYPE_padding, ret); | ||
940 | s2n(hlen, ret); | ||
941 | memset(ret, 0, hlen); | ||
942 | ret += hlen; | ||
943 | } | ||
944 | } | ||
945 | |||
946 | if ((extdatalen = ret - p - 2) == 0) | ||
947 | return p; | ||
948 | |||
949 | s2n(extdatalen, p); | ||
950 | return ret; | ||
951 | } | ||
952 | |||
953 | unsigned char * | ||
954 | ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) | ||
955 | { | ||
956 | int using_ecc, extdatalen = 0; | ||
957 | unsigned long alg_a, alg_k; | ||
958 | unsigned char *ret = p; | ||
959 | int next_proto_neg_seen; | ||
960 | |||
961 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; | ||
962 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
963 | using_ecc = (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe) || | ||
964 | alg_a & SSL_aECDSA) && | ||
965 | s->session->tlsext_ecpointformatlist != NULL; | ||
966 | |||
967 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ | ||
968 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) | ||
969 | return p; | ||
970 | |||
971 | ret += 2; | ||
972 | if (ret >= limit) | ||
973 | return NULL; /* this really never occurs, but ... */ | ||
974 | |||
975 | if (!s->hit && s->servername_done == 1 && | ||
976 | s->session->tlsext_hostname != NULL) { | ||
977 | if ((size_t)(limit - ret) < 4) | ||
978 | return NULL; | ||
979 | |||
980 | s2n(TLSEXT_TYPE_server_name, ret); | ||
981 | s2n(0, ret); | ||
982 | } | ||
983 | |||
984 | if (s->s3->send_connection_binding) { | ||
985 | int el; | ||
986 | |||
987 | if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) { | ||
988 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, | ||
989 | ERR_R_INTERNAL_ERROR); | ||
990 | return NULL; | ||
991 | } | ||
992 | |||
993 | if ((size_t)(limit - ret) < 4 + el) | ||
994 | return NULL; | ||
995 | |||
996 | s2n(TLSEXT_TYPE_renegotiate, ret); | ||
997 | s2n(el, ret); | ||
998 | |||
999 | if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) { | ||
1000 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, | ||
1001 | ERR_R_INTERNAL_ERROR); | ||
1002 | return NULL; | ||
1003 | } | ||
1004 | |||
1005 | ret += el; | ||
1006 | } | ||
1007 | |||
1008 | if (using_ecc && s->version != DTLS1_VERSION) { | ||
1009 | const unsigned char *formats; | ||
1010 | size_t formatslen, lenmax; | ||
1011 | |||
1012 | /* | ||
1013 | * Add TLS extension ECPointFormats to the ServerHello message. | ||
1014 | */ | ||
1015 | tls1_get_formatlist(s, 0, &formats, &formatslen); | ||
1016 | |||
1017 | if ((size_t)(limit - ret) < 5) | ||
1018 | return NULL; | ||
1019 | |||
1020 | lenmax = limit - ret - 5; | ||
1021 | if (formatslen > lenmax) | ||
1022 | return NULL; | ||
1023 | if (formatslen > 255) { | ||
1024 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, | ||
1025 | ERR_R_INTERNAL_ERROR); | ||
1026 | return NULL; | ||
1027 | } | ||
1028 | |||
1029 | s2n(TLSEXT_TYPE_ec_point_formats, ret); | ||
1030 | s2n(formatslen + 1, ret); | ||
1031 | *(ret++) = (unsigned char)formatslen; | ||
1032 | memcpy(ret, formats, formatslen); | ||
1033 | ret += formatslen; | ||
1034 | } | ||
1035 | |||
1036 | /* | ||
1037 | * Currently the server should not respond with a SupportedCurves | ||
1038 | * extension. | ||
1039 | */ | ||
1040 | |||
1041 | if (s->tlsext_ticket_expected && | ||
1042 | !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { | ||
1043 | if ((size_t)(limit - ret) < 4) | ||
1044 | return NULL; | ||
1045 | |||
1046 | s2n(TLSEXT_TYPE_session_ticket, ret); | ||
1047 | s2n(0, ret); | ||
1048 | } | ||
1049 | |||
1050 | if (s->tlsext_status_expected) { | ||
1051 | if ((size_t)(limit - ret) < 4) | ||
1052 | return NULL; | ||
1053 | |||
1054 | s2n(TLSEXT_TYPE_status_request, ret); | ||
1055 | s2n(0, ret); | ||
1056 | } | ||
1057 | |||
1058 | #ifndef OPENSSL_NO_SRTP | ||
1059 | if (SSL_IS_DTLS(s) && s->srtp_profile) { | ||
1060 | int el; | ||
1061 | |||
1062 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); | ||
1063 | |||
1064 | if ((size_t)(limit - ret) < 4 + el) | ||
1065 | return NULL; | ||
1066 | |||
1067 | s2n(TLSEXT_TYPE_use_srtp, ret); | ||
1068 | s2n(el, ret); | ||
1069 | |||
1070 | if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) { | ||
1071 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, | ||
1072 | ERR_R_INTERNAL_ERROR); | ||
1073 | return NULL; | ||
1074 | } | ||
1075 | ret += el; | ||
1076 | } | ||
1077 | #endif | ||
1078 | |||
1079 | if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80 || | ||
1080 | (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81) && | ||
1081 | (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) { | ||
1082 | static const unsigned char cryptopro_ext[36] = { | ||
1083 | 0xfd, 0xe8, /*65000*/ | ||
1084 | 0x00, 0x20, /*32 bytes length*/ | ||
1085 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, | ||
1086 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, | ||
1087 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, | ||
1088 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 | ||
1089 | }; | ||
1090 | if ((size_t)(limit - ret) < sizeof(cryptopro_ext)) | ||
1091 | return NULL; | ||
1092 | memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext)); | ||
1093 | ret += sizeof(cryptopro_ext); | ||
1094 | } | ||
1095 | |||
1096 | next_proto_neg_seen = s->s3->next_proto_neg_seen; | ||
1097 | s->s3->next_proto_neg_seen = 0; | ||
1098 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { | ||
1099 | const unsigned char *npa; | ||
1100 | unsigned int npalen; | ||
1101 | int r; | ||
1102 | |||
1103 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, | ||
1104 | s->ctx->next_protos_advertised_cb_arg); | ||
1105 | if (r == SSL_TLSEXT_ERR_OK) { | ||
1106 | if ((size_t)(limit - ret) < 4 + npalen) | ||
1107 | return NULL; | ||
1108 | s2n(TLSEXT_TYPE_next_proto_neg, ret); | ||
1109 | s2n(npalen, ret); | ||
1110 | memcpy(ret, npa, npalen); | ||
1111 | ret += npalen; | ||
1112 | s->s3->next_proto_neg_seen = 1; | ||
1113 | } | ||
1114 | } | ||
1115 | |||
1116 | if (s->s3->alpn_selected != NULL) { | ||
1117 | const unsigned char *selected = s->s3->alpn_selected; | ||
1118 | unsigned int len = s->s3->alpn_selected_len; | ||
1119 | |||
1120 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) | ||
1121 | return (NULL); | ||
1122 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); | ||
1123 | s2n(3 + len, ret); | ||
1124 | s2n(1 + len, ret); | ||
1125 | *ret++ = len; | ||
1126 | memcpy(ret, selected, len); | ||
1127 | ret += len; | ||
1128 | } | ||
1129 | |||
1130 | if ((extdatalen = ret - p - 2) == 0) | ||
1131 | return p; | ||
1132 | |||
1133 | s2n(extdatalen, p); | ||
1134 | return ret; | ||
1135 | } | ||
1136 | |||
1137 | /* | ||
1138 | * tls1_alpn_handle_client_hello is called to process the ALPN extension in a | ||
1139 | * ClientHello. | ||
1140 | * data: the contents of the extension, not including the type and length. | ||
1141 | * data_len: the number of bytes in data. | ||
1142 | * al: a pointer to the alert value to send in the event of a non-zero | ||
1143 | * return. | ||
1144 | * returns: 1 on success. | ||
1145 | */ | ||
1146 | static int | ||
1147 | tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data, | ||
1148 | unsigned int data_len, int *al) | ||
1149 | { | ||
1150 | const unsigned char *selected; | ||
1151 | unsigned char selected_len; | ||
1152 | unsigned int proto_len; | ||
1153 | unsigned int i; | ||
1154 | int r; | ||
1155 | |||
1156 | if (s->ctx->alpn_select_cb == NULL) | ||
1157 | return (1); | ||
1158 | |||
1159 | if (data_len < 2) | ||
1160 | goto parse_error; | ||
1161 | |||
1162 | /* | ||
1163 | * data should contain a uint16 length followed by a series of 8-bit, | ||
1164 | * length-prefixed strings. | ||
1165 | */ | ||
1166 | i = ((unsigned int)data[0]) << 8 | ((unsigned int)data[1]); | ||
1167 | data_len -= 2; | ||
1168 | data += 2; | ||
1169 | if (data_len != i) | ||
1170 | goto parse_error; | ||
1171 | |||
1172 | if (data_len < 2) | ||
1173 | goto parse_error; | ||
1174 | |||
1175 | for (i = 0; i < data_len; ) { | ||
1176 | proto_len = data[i]; | ||
1177 | i++; | ||
1178 | |||
1179 | if (proto_len == 0) | ||
1180 | goto parse_error; | ||
1181 | |||
1182 | if (i + proto_len < i || i + proto_len > data_len) | ||
1183 | goto parse_error; | ||
1184 | |||
1185 | i += proto_len; | ||
1186 | } | ||
1187 | |||
1188 | r = s->ctx->alpn_select_cb(s, &selected, &selected_len, | ||
1189 | data, data_len, s->ctx->alpn_select_cb_arg); | ||
1190 | if (r == SSL_TLSEXT_ERR_OK) { | ||
1191 | free(s->s3->alpn_selected); | ||
1192 | if ((s->s3->alpn_selected = malloc(selected_len)) == NULL) { | ||
1193 | *al = SSL_AD_INTERNAL_ERROR; | ||
1194 | return (-1); | ||
1195 | } | ||
1196 | memcpy(s->s3->alpn_selected, selected, selected_len); | ||
1197 | s->s3->alpn_selected_len = selected_len; | ||
1198 | } | ||
1199 | |||
1200 | return (1); | ||
1201 | |||
1202 | parse_error: | ||
1203 | *al = SSL_AD_DECODE_ERROR; | ||
1204 | return (0); | ||
1205 | } | ||
1206 | |||
1207 | /* ssl_check_for_safari attempts to fingerprint Safari using OS X | ||
1208 | * SecureTransport using the TLS extension block in |d|, of length |n|. | ||
1209 | * Safari, since 10.6, sends exactly these extensions, in this order: | ||
1210 | * SNI, | ||
1211 | * elliptic_curves | ||
1212 | * ec_point_formats | ||
1213 | * | ||
1214 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, | ||
1215 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. | ||
1216 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from | ||
1217 | * 10.8..10.8.3 (which don't work). | ||
1218 | */ | ||
1219 | static void | ||
1220 | ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, | ||
1221 | int n) | ||
1222 | { | ||
1223 | unsigned short type, size; | ||
1224 | static const unsigned char kSafariExtensionsBlock[] = { | ||
1225 | 0x00, 0x0a, /* elliptic_curves extension */ | ||
1226 | 0x00, 0x08, /* 8 bytes */ | ||
1227 | 0x00, 0x06, /* 6 bytes of curve ids */ | ||
1228 | 0x00, 0x17, /* P-256 */ | ||
1229 | 0x00, 0x18, /* P-384 */ | ||
1230 | 0x00, 0x19, /* P-521 */ | ||
1231 | |||
1232 | 0x00, 0x0b, /* ec_point_formats */ | ||
1233 | 0x00, 0x02, /* 2 bytes */ | ||
1234 | 0x01, /* 1 point format */ | ||
1235 | 0x00, /* uncompressed */ | ||
1236 | }; | ||
1237 | |||
1238 | /* The following is only present in TLS 1.2 */ | ||
1239 | static const unsigned char kSafariTLS12ExtensionsBlock[] = { | ||
1240 | 0x00, 0x0d, /* signature_algorithms */ | ||
1241 | 0x00, 0x0c, /* 12 bytes */ | ||
1242 | 0x00, 0x0a, /* 10 bytes */ | ||
1243 | 0x05, 0x01, /* SHA-384/RSA */ | ||
1244 | 0x04, 0x01, /* SHA-256/RSA */ | ||
1245 | 0x02, 0x01, /* SHA-1/RSA */ | ||
1246 | 0x04, 0x03, /* SHA-256/ECDSA */ | ||
1247 | 0x02, 0x03, /* SHA-1/ECDSA */ | ||
1248 | }; | ||
1249 | |||
1250 | if (data >= (d + n - 2)) | ||
1251 | return; | ||
1252 | data += 2; | ||
1253 | |||
1254 | if (data > (d + n - 4)) | ||
1255 | return; | ||
1256 | n2s(data, type); | ||
1257 | n2s(data, size); | ||
1258 | |||
1259 | if (type != TLSEXT_TYPE_server_name) | ||
1260 | return; | ||
1261 | |||
1262 | if (data + size > d + n) | ||
1263 | return; | ||
1264 | data += size; | ||
1265 | |||
1266 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) { | ||
1267 | const size_t len1 = sizeof(kSafariExtensionsBlock); | ||
1268 | const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock); | ||
1269 | |||
1270 | if (data + len1 + len2 != d + n) | ||
1271 | return; | ||
1272 | if (memcmp(data, kSafariExtensionsBlock, len1) != 0) | ||
1273 | return; | ||
1274 | if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0) | ||
1275 | return; | ||
1276 | } else { | ||
1277 | const size_t len = sizeof(kSafariExtensionsBlock); | ||
1278 | |||
1279 | if (data + len != d + n) | ||
1280 | return; | ||
1281 | if (memcmp(data, kSafariExtensionsBlock, len) != 0) | ||
1282 | return; | ||
1283 | } | ||
1284 | |||
1285 | s->s3->is_probably_safari = 1; | ||
1286 | } | ||
1287 | |||
1288 | int | ||
1289 | ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, | ||
1290 | int n, int *al) | ||
1291 | { | ||
1292 | unsigned short type; | ||
1293 | unsigned short size; | ||
1294 | unsigned short len; | ||
1295 | unsigned char *data = *p; | ||
1296 | int renegotiate_seen = 0; | ||
1297 | int sigalg_seen = 0; | ||
1298 | |||
1299 | s->servername_done = 0; | ||
1300 | s->tlsext_status_type = -1; | ||
1301 | s->s3->next_proto_neg_seen = 0; | ||
1302 | free(s->s3->alpn_selected); | ||
1303 | s->s3->alpn_selected = NULL; | ||
1304 | |||
1305 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) | ||
1306 | ssl_check_for_safari(s, data, d, n); | ||
1307 | |||
1308 | if (data >= (d + n - 2)) | ||
1309 | goto ri_check; | ||
1310 | n2s(data, len); | ||
1311 | |||
1312 | if (data > (d + n - len)) | ||
1313 | goto ri_check; | ||
1314 | |||
1315 | while (data <= (d + n - 4)) { | ||
1316 | n2s(data, type); | ||
1317 | n2s(data, size); | ||
1318 | |||
1319 | if (data + size > (d + n)) | ||
1320 | goto ri_check; | ||
1321 | if (s->tlsext_debug_cb) | ||
1322 | s->tlsext_debug_cb(s, 0, type, data, size, | ||
1323 | s->tlsext_debug_arg); | ||
1324 | /* The servername extension is treated as follows: | ||
1325 | |||
1326 | - Only the hostname type is supported with a maximum length of 255. | ||
1327 | - The servername is rejected if too long or if it contains zeros, | ||
1328 | in which case an fatal alert is generated. | ||
1329 | - The servername field is maintained together with the session cache. | ||
1330 | - When a session is resumed, the servername call back invoked in order | ||
1331 | to allow the application to position itself to the right context. | ||
1332 | - The servername is acknowledged if it is new for a session or when | ||
1333 | it is identical to a previously used for the same session. | ||
1334 | Applications can control the behaviour. They can at any time | ||
1335 | set a 'desirable' servername for a new SSL object. This can be the | ||
1336 | case for example with HTTPS when a Host: header field is received and | ||
1337 | a renegotiation is requested. In this case, a possible servername | ||
1338 | presented in the new client hello is only acknowledged if it matches | ||
1339 | the value of the Host: field. | ||
1340 | - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | ||
1341 | if they provide for changing an explicit servername context for the session, | ||
1342 | i.e. when the session has been established with a servername extension. | ||
1343 | - On session reconnect, the servername extension may be absent. | ||
1344 | |||
1345 | */ | ||
1346 | |||
1347 | if (type == TLSEXT_TYPE_server_name) { | ||
1348 | unsigned char *sdata; | ||
1349 | int servname_type; | ||
1350 | int dsize; | ||
1351 | |||
1352 | if (size < 2) { | ||
1353 | *al = SSL_AD_DECODE_ERROR; | ||
1354 | return 0; | ||
1355 | } | ||
1356 | n2s(data, dsize); | ||
1357 | |||
1358 | size -= 2; | ||
1359 | if (dsize > size) { | ||
1360 | *al = SSL_AD_DECODE_ERROR; | ||
1361 | return 0; | ||
1362 | } | ||
1363 | |||
1364 | sdata = data; | ||
1365 | while (dsize > 3) { | ||
1366 | servname_type = *(sdata++); | ||
1367 | |||
1368 | n2s(sdata, len); | ||
1369 | dsize -= 3; | ||
1370 | |||
1371 | if (len > dsize) { | ||
1372 | *al = SSL_AD_DECODE_ERROR; | ||
1373 | return 0; | ||
1374 | } | ||
1375 | if (s->servername_done == 0) | ||
1376 | switch (servname_type) { | ||
1377 | case TLSEXT_NAMETYPE_host_name: | ||
1378 | if (!s->hit) { | ||
1379 | if (s->session->tlsext_hostname) { | ||
1380 | *al = SSL_AD_DECODE_ERROR; | ||
1381 | return 0; | ||
1382 | } | ||
1383 | if (len > TLSEXT_MAXLEN_host_name) { | ||
1384 | *al = TLS1_AD_UNRECOGNIZED_NAME; | ||
1385 | return 0; | ||
1386 | } | ||
1387 | if ((s->session->tlsext_hostname = | ||
1388 | malloc(len + 1)) == NULL) { | ||
1389 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1390 | return 0; | ||
1391 | } | ||
1392 | memcpy(s->session->tlsext_hostname, sdata, len); | ||
1393 | s->session->tlsext_hostname[len] = '\0'; | ||
1394 | if (strlen(s->session->tlsext_hostname) != len) { | ||
1395 | free(s->session->tlsext_hostname); | ||
1396 | s->session->tlsext_hostname = NULL; | ||
1397 | *al = TLS1_AD_UNRECOGNIZED_NAME; | ||
1398 | return 0; | ||
1399 | } | ||
1400 | s->servername_done = 1; | ||
1401 | |||
1402 | |||
1403 | } else { | ||
1404 | s->servername_done = s->session->tlsext_hostname && | ||
1405 | strlen(s->session->tlsext_hostname) == len && | ||
1406 | strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0; | ||
1407 | } | ||
1408 | break; | ||
1409 | |||
1410 | default: | ||
1411 | break; | ||
1412 | } | ||
1413 | |||
1414 | dsize -= len; | ||
1415 | } | ||
1416 | if (dsize != 0) { | ||
1417 | *al = SSL_AD_DECODE_ERROR; | ||
1418 | return 0; | ||
1419 | } | ||
1420 | |||
1421 | } | ||
1422 | |||
1423 | else if (type == TLSEXT_TYPE_ec_point_formats && | ||
1424 | s->version != DTLS1_VERSION) { | ||
1425 | unsigned char *sdata = data; | ||
1426 | size_t formatslen; | ||
1427 | uint8_t *formats; | ||
1428 | |||
1429 | if (size < 1) { | ||
1430 | *al = TLS1_AD_DECODE_ERROR; | ||
1431 | return 0; | ||
1432 | } | ||
1433 | formatslen = *(sdata++); | ||
1434 | if (formatslen != size - 1) { | ||
1435 | *al = TLS1_AD_DECODE_ERROR; | ||
1436 | return 0; | ||
1437 | } | ||
1438 | |||
1439 | if (!s->hit) { | ||
1440 | free(s->session->tlsext_ecpointformatlist); | ||
1441 | s->session->tlsext_ecpointformatlist = NULL; | ||
1442 | s->session->tlsext_ecpointformatlist_length = 0; | ||
1443 | |||
1444 | if ((formats = reallocarray(NULL, formatslen, | ||
1445 | sizeof(uint8_t))) == NULL) { | ||
1446 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1447 | return 0; | ||
1448 | } | ||
1449 | memcpy(formats, sdata, formatslen); | ||
1450 | s->session->tlsext_ecpointformatlist = formats; | ||
1451 | s->session->tlsext_ecpointformatlist_length = | ||
1452 | formatslen; | ||
1453 | } | ||
1454 | } else if (type == TLSEXT_TYPE_elliptic_curves && | ||
1455 | s->version != DTLS1_VERSION) { | ||
1456 | unsigned char *sdata = data; | ||
1457 | size_t curveslen, i; | ||
1458 | uint16_t *curves; | ||
1459 | |||
1460 | if (size < 2) { | ||
1461 | *al = TLS1_AD_DECODE_ERROR; | ||
1462 | return 0; | ||
1463 | } | ||
1464 | n2s(sdata, curveslen); | ||
1465 | if (curveslen != size - 2 || curveslen % 2 != 0) { | ||
1466 | *al = TLS1_AD_DECODE_ERROR; | ||
1467 | return 0; | ||
1468 | } | ||
1469 | curveslen /= 2; | ||
1470 | |||
1471 | if (!s->hit) { | ||
1472 | if (s->session->tlsext_ellipticcurvelist) { | ||
1473 | *al = TLS1_AD_DECODE_ERROR; | ||
1474 | return 0; | ||
1475 | } | ||
1476 | s->session->tlsext_ellipticcurvelist_length = 0; | ||
1477 | if ((curves = reallocarray(NULL, curveslen, | ||
1478 | sizeof(uint16_t))) == NULL) { | ||
1479 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1480 | return 0; | ||
1481 | } | ||
1482 | for (i = 0; i < curveslen; i++) | ||
1483 | n2s(sdata, curves[i]); | ||
1484 | s->session->tlsext_ellipticcurvelist = curves; | ||
1485 | s->session->tlsext_ellipticcurvelist_length = curveslen; | ||
1486 | } | ||
1487 | } | ||
1488 | else if (type == TLSEXT_TYPE_session_ticket) { | ||
1489 | if (s->tls_session_ticket_ext_cb && | ||
1490 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) { | ||
1491 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1492 | return 0; | ||
1493 | } | ||
1494 | } else if (type == TLSEXT_TYPE_renegotiate) { | ||
1495 | if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al)) | ||
1496 | return 0; | ||
1497 | renegotiate_seen = 1; | ||
1498 | } else if (type == TLSEXT_TYPE_signature_algorithms) { | ||
1499 | int dsize; | ||
1500 | if (sigalg_seen || size < 2) { | ||
1501 | *al = SSL_AD_DECODE_ERROR; | ||
1502 | return 0; | ||
1503 | } | ||
1504 | sigalg_seen = 1; | ||
1505 | n2s(data, dsize); | ||
1506 | size -= 2; | ||
1507 | if (dsize != size || dsize & 1) { | ||
1508 | *al = SSL_AD_DECODE_ERROR; | ||
1509 | return 0; | ||
1510 | } | ||
1511 | if (!tls1_process_sigalgs(s, data, dsize)) { | ||
1512 | *al = SSL_AD_DECODE_ERROR; | ||
1513 | return 0; | ||
1514 | } | ||
1515 | } else if (type == TLSEXT_TYPE_status_request && | ||
1516 | s->version != DTLS1_VERSION) { | ||
1517 | |||
1518 | if (size < 5) { | ||
1519 | *al = SSL_AD_DECODE_ERROR; | ||
1520 | return 0; | ||
1521 | } | ||
1522 | |||
1523 | s->tlsext_status_type = *data++; | ||
1524 | size--; | ||
1525 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { | ||
1526 | const unsigned char *sdata; | ||
1527 | int dsize; | ||
1528 | /* Read in responder_id_list */ | ||
1529 | n2s(data, dsize); | ||
1530 | size -= 2; | ||
1531 | if (dsize > size ) { | ||
1532 | *al = SSL_AD_DECODE_ERROR; | ||
1533 | return 0; | ||
1534 | } | ||
1535 | while (dsize > 0) { | ||
1536 | OCSP_RESPID *id; | ||
1537 | int idsize; | ||
1538 | if (dsize < 4) { | ||
1539 | *al = SSL_AD_DECODE_ERROR; | ||
1540 | return 0; | ||
1541 | } | ||
1542 | n2s(data, idsize); | ||
1543 | dsize -= 2 + idsize; | ||
1544 | size -= 2 + idsize; | ||
1545 | if (dsize < 0) { | ||
1546 | *al = SSL_AD_DECODE_ERROR; | ||
1547 | return 0; | ||
1548 | } | ||
1549 | sdata = data; | ||
1550 | data += idsize; | ||
1551 | id = d2i_OCSP_RESPID(NULL, | ||
1552 | &sdata, idsize); | ||
1553 | if (!id) { | ||
1554 | *al = SSL_AD_DECODE_ERROR; | ||
1555 | return 0; | ||
1556 | } | ||
1557 | if (data != sdata) { | ||
1558 | OCSP_RESPID_free(id); | ||
1559 | *al = SSL_AD_DECODE_ERROR; | ||
1560 | return 0; | ||
1561 | } | ||
1562 | if (!s->tlsext_ocsp_ids && | ||
1563 | !(s->tlsext_ocsp_ids = | ||
1564 | sk_OCSP_RESPID_new_null())) { | ||
1565 | OCSP_RESPID_free(id); | ||
1566 | *al = SSL_AD_INTERNAL_ERROR; | ||
1567 | return 0; | ||
1568 | } | ||
1569 | if (!sk_OCSP_RESPID_push( | ||
1570 | s->tlsext_ocsp_ids, id)) { | ||
1571 | OCSP_RESPID_free(id); | ||
1572 | *al = SSL_AD_INTERNAL_ERROR; | ||
1573 | return 0; | ||
1574 | } | ||
1575 | } | ||
1576 | |||
1577 | /* Read in request_extensions */ | ||
1578 | if (size < 2) { | ||
1579 | *al = SSL_AD_DECODE_ERROR; | ||
1580 | return 0; | ||
1581 | } | ||
1582 | n2s(data, dsize); | ||
1583 | size -= 2; | ||
1584 | if (dsize != size) { | ||
1585 | *al = SSL_AD_DECODE_ERROR; | ||
1586 | return 0; | ||
1587 | } | ||
1588 | sdata = data; | ||
1589 | if (dsize > 0) { | ||
1590 | if (s->tlsext_ocsp_exts) { | ||
1591 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, | ||
1592 | X509_EXTENSION_free); | ||
1593 | } | ||
1594 | |||
1595 | s->tlsext_ocsp_exts = | ||
1596 | d2i_X509_EXTENSIONS(NULL, | ||
1597 | &sdata, dsize); | ||
1598 | if (!s->tlsext_ocsp_exts || | ||
1599 | (data + dsize != sdata)) { | ||
1600 | *al = SSL_AD_DECODE_ERROR; | ||
1601 | return 0; | ||
1602 | } | ||
1603 | } | ||
1604 | } else { | ||
1605 | /* We don't know what to do with any other type | ||
1606 | * so ignore it. | ||
1607 | */ | ||
1608 | s->tlsext_status_type = -1; | ||
1609 | } | ||
1610 | } | ||
1611 | else if (type == TLSEXT_TYPE_next_proto_neg && | ||
1612 | s->s3->tmp.finish_md_len == 0 && | ||
1613 | s->s3->alpn_selected == NULL) { | ||
1614 | /* We shouldn't accept this extension on a | ||
1615 | * renegotiation. | ||
1616 | * | ||
1617 | * s->new_session will be set on renegotiation, but we | ||
1618 | * probably shouldn't rely that it couldn't be set on | ||
1619 | * the initial renegotation too in certain cases (when | ||
1620 | * there's some other reason to disallow resuming an | ||
1621 | * earlier session -- the current code won't be doing | ||
1622 | * anything like that, but this might change). | ||
1623 | |||
1624 | * A valid sign that there's been a previous handshake | ||
1625 | * in this connection is if s->s3->tmp.finish_md_len > | ||
1626 | * 0. (We are talking about a check that will happen | ||
1627 | * in the Hello protocol round, well before a new | ||
1628 | * Finished message could have been computed.) */ | ||
1629 | s->s3->next_proto_neg_seen = 1; | ||
1630 | } | ||
1631 | else if (type == | ||
1632 | TLSEXT_TYPE_application_layer_protocol_negotiation && | ||
1633 | s->ctx->alpn_select_cb != NULL && | ||
1634 | s->s3->tmp.finish_md_len == 0) { | ||
1635 | if (tls1_alpn_handle_client_hello(s, data, | ||
1636 | size, al) != 1) | ||
1637 | return (0); | ||
1638 | /* ALPN takes precedence over NPN. */ | ||
1639 | s->s3->next_proto_neg_seen = 0; | ||
1640 | } | ||
1641 | |||
1642 | /* session ticket processed earlier */ | ||
1643 | #ifndef OPENSSL_NO_SRTP | ||
1644 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { | ||
1645 | if (ssl_parse_clienthello_use_srtp_ext(s, data, size, al)) | ||
1646 | return 0; | ||
1647 | } | ||
1648 | #endif | ||
1649 | |||
1650 | data += size; | ||
1651 | } | ||
1652 | |||
1653 | *p = data; | ||
1654 | |||
1655 | ri_check: | ||
1656 | |||
1657 | /* Need RI if renegotiating */ | ||
1658 | |||
1659 | if (!renegotiate_seen && s->renegotiate) { | ||
1660 | *al = SSL_AD_HANDSHAKE_FAILURE; | ||
1661 | SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, | ||
1662 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | ||
1663 | return 0; | ||
1664 | } | ||
1665 | |||
1666 | return 1; | ||
1667 | } | ||
1668 | |||
1669 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No | ||
1670 | * elements of zero length are allowed and the set of elements must exactly fill | ||
1671 | * the length of the block. */ | ||
1672 | static char | ||
1673 | ssl_next_proto_validate(unsigned char *d, unsigned len) | ||
1674 | { | ||
1675 | unsigned int off = 0; | ||
1676 | |||
1677 | while (off < len) { | ||
1678 | if (d[off] == 0) | ||
1679 | return 0; | ||
1680 | off += d[off]; | ||
1681 | off++; | ||
1682 | } | ||
1683 | |||
1684 | return off == len; | ||
1685 | } | ||
1686 | |||
1687 | int | ||
1688 | ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, | ||
1689 | int n, int *al) | ||
1690 | { | ||
1691 | unsigned short length; | ||
1692 | unsigned short type; | ||
1693 | unsigned short size; | ||
1694 | unsigned char *data = *p; | ||
1695 | int tlsext_servername = 0; | ||
1696 | int renegotiate_seen = 0; | ||
1697 | |||
1698 | s->s3->next_proto_neg_seen = 0; | ||
1699 | free(s->s3->alpn_selected); | ||
1700 | s->s3->alpn_selected = NULL; | ||
1701 | |||
1702 | if (data >= (d + n - 2)) | ||
1703 | goto ri_check; | ||
1704 | |||
1705 | n2s(data, length); | ||
1706 | if (data + length != d + n) { | ||
1707 | *al = SSL_AD_DECODE_ERROR; | ||
1708 | return 0; | ||
1709 | } | ||
1710 | |||
1711 | while (data <= (d + n - 4)) { | ||
1712 | n2s(data, type); | ||
1713 | n2s(data, size); | ||
1714 | |||
1715 | if (data + size > (d + n)) | ||
1716 | goto ri_check; | ||
1717 | |||
1718 | if (s->tlsext_debug_cb) | ||
1719 | s->tlsext_debug_cb(s, 1, type, data, size, | ||
1720 | s->tlsext_debug_arg); | ||
1721 | |||
1722 | if (type == TLSEXT_TYPE_server_name) { | ||
1723 | if (s->tlsext_hostname == NULL || size > 0) { | ||
1724 | *al = TLS1_AD_UNRECOGNIZED_NAME; | ||
1725 | return 0; | ||
1726 | } | ||
1727 | tlsext_servername = 1; | ||
1728 | |||
1729 | } | ||
1730 | else if (type == TLSEXT_TYPE_ec_point_formats && | ||
1731 | s->version != DTLS1_VERSION) { | ||
1732 | unsigned char *sdata = data; | ||
1733 | size_t formatslen; | ||
1734 | uint8_t *formats; | ||
1735 | |||
1736 | if (size < 1) { | ||
1737 | *al = TLS1_AD_DECODE_ERROR; | ||
1738 | return 0; | ||
1739 | } | ||
1740 | formatslen = *(sdata++); | ||
1741 | if (formatslen != size - 1) { | ||
1742 | *al = TLS1_AD_DECODE_ERROR; | ||
1743 | return 0; | ||
1744 | } | ||
1745 | |||
1746 | if (!s->hit) { | ||
1747 | free(s->session->tlsext_ecpointformatlist); | ||
1748 | s->session->tlsext_ecpointformatlist = NULL; | ||
1749 | s->session->tlsext_ecpointformatlist_length = 0; | ||
1750 | |||
1751 | if ((formats = reallocarray(NULL, formatslen, | ||
1752 | sizeof(uint8_t))) == NULL) { | ||
1753 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1754 | return 0; | ||
1755 | } | ||
1756 | memcpy(formats, sdata, formatslen); | ||
1757 | s->session->tlsext_ecpointformatlist = formats; | ||
1758 | s->session->tlsext_ecpointformatlist_length = | ||
1759 | formatslen; | ||
1760 | } | ||
1761 | } | ||
1762 | else if (type == TLSEXT_TYPE_session_ticket) { | ||
1763 | if (s->tls_session_ticket_ext_cb && | ||
1764 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) { | ||
1765 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1766 | return 0; | ||
1767 | } | ||
1768 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || (size > 0)) { | ||
1769 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
1770 | return 0; | ||
1771 | } | ||
1772 | s->tlsext_ticket_expected = 1; | ||
1773 | } | ||
1774 | else if (type == TLSEXT_TYPE_status_request && | ||
1775 | s->version != DTLS1_VERSION) { | ||
1776 | /* MUST be empty and only sent if we've requested | ||
1777 | * a status request message. | ||
1778 | */ | ||
1779 | if ((s->tlsext_status_type == -1) || (size > 0)) { | ||
1780 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
1781 | return 0; | ||
1782 | } | ||
1783 | /* Set flag to expect CertificateStatus message */ | ||
1784 | s->tlsext_status_expected = 1; | ||
1785 | } | ||
1786 | else if (type == TLSEXT_TYPE_next_proto_neg && | ||
1787 | s->s3->tmp.finish_md_len == 0) { | ||
1788 | unsigned char *selected; | ||
1789 | unsigned char selected_len; | ||
1790 | |||
1791 | /* We must have requested it. */ | ||
1792 | if (s->ctx->next_proto_select_cb == NULL) { | ||
1793 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
1794 | return 0; | ||
1795 | } | ||
1796 | /* The data must be valid */ | ||
1797 | if (!ssl_next_proto_validate(data, size)) { | ||
1798 | *al = TLS1_AD_DECODE_ERROR; | ||
1799 | return 0; | ||
1800 | } | ||
1801 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { | ||
1802 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1803 | return 0; | ||
1804 | } | ||
1805 | s->next_proto_negotiated = malloc(selected_len); | ||
1806 | if (!s->next_proto_negotiated) { | ||
1807 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1808 | return 0; | ||
1809 | } | ||
1810 | memcpy(s->next_proto_negotiated, selected, selected_len); | ||
1811 | s->next_proto_negotiated_len = selected_len; | ||
1812 | s->s3->next_proto_neg_seen = 1; | ||
1813 | } | ||
1814 | else if (type == | ||
1815 | TLSEXT_TYPE_application_layer_protocol_negotiation) { | ||
1816 | unsigned int len; | ||
1817 | |||
1818 | /* We must have requested it. */ | ||
1819 | if (s->alpn_client_proto_list == NULL) { | ||
1820 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
1821 | return 0; | ||
1822 | } | ||
1823 | if (size < 4) { | ||
1824 | *al = TLS1_AD_DECODE_ERROR; | ||
1825 | return (0); | ||
1826 | } | ||
1827 | |||
1828 | /* The extension data consists of: | ||
1829 | * uint16 list_length | ||
1830 | * uint8 proto_length; | ||
1831 | * uint8 proto[proto_length]; */ | ||
1832 | len = ((unsigned int)data[0]) << 8 | | ||
1833 | ((unsigned int)data[1]); | ||
1834 | if (len != (unsigned int)size - 2) { | ||
1835 | *al = TLS1_AD_DECODE_ERROR; | ||
1836 | return (0); | ||
1837 | } | ||
1838 | len = data[2]; | ||
1839 | if (len != (unsigned int)size - 3) { | ||
1840 | *al = TLS1_AD_DECODE_ERROR; | ||
1841 | return (0); | ||
1842 | } | ||
1843 | free(s->s3->alpn_selected); | ||
1844 | s->s3->alpn_selected = malloc(len); | ||
1845 | if (s->s3->alpn_selected == NULL) { | ||
1846 | *al = TLS1_AD_INTERNAL_ERROR; | ||
1847 | return (0); | ||
1848 | } | ||
1849 | memcpy(s->s3->alpn_selected, data + 3, len); | ||
1850 | s->s3->alpn_selected_len = len; | ||
1851 | |||
1852 | } else if (type == TLSEXT_TYPE_renegotiate) { | ||
1853 | if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al)) | ||
1854 | return 0; | ||
1855 | renegotiate_seen = 1; | ||
1856 | } | ||
1857 | #ifndef OPENSSL_NO_SRTP | ||
1858 | else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) { | ||
1859 | if (ssl_parse_serverhello_use_srtp_ext(s, data, | ||
1860 | size, al)) | ||
1861 | return 0; | ||
1862 | } | ||
1863 | #endif | ||
1864 | |||
1865 | data += size; | ||
1866 | |||
1867 | } | ||
1868 | |||
1869 | if (data != d + n) { | ||
1870 | *al = SSL_AD_DECODE_ERROR; | ||
1871 | return 0; | ||
1872 | } | ||
1873 | |||
1874 | if (!s->hit && tlsext_servername == 1) { | ||
1875 | if (s->tlsext_hostname) { | ||
1876 | if (s->session->tlsext_hostname == NULL) { | ||
1877 | s->session->tlsext_hostname = | ||
1878 | strdup(s->tlsext_hostname); | ||
1879 | |||
1880 | if (!s->session->tlsext_hostname) { | ||
1881 | *al = SSL_AD_UNRECOGNIZED_NAME; | ||
1882 | return 0; | ||
1883 | } | ||
1884 | } else { | ||
1885 | *al = SSL_AD_DECODE_ERROR; | ||
1886 | return 0; | ||
1887 | } | ||
1888 | } | ||
1889 | } | ||
1890 | |||
1891 | *p = data; | ||
1892 | |||
1893 | ri_check: | ||
1894 | |||
1895 | /* Determine if we need to see RI. Strictly speaking if we want to | ||
1896 | * avoid an attack we should *always* see RI even on initial server | ||
1897 | * hello because the client doesn't see any renegotiation during an | ||
1898 | * attack. However this would mean we could not connect to any server | ||
1899 | * which doesn't support RI so for the immediate future tolerate RI | ||
1900 | * absence on initial connect only. | ||
1901 | */ | ||
1902 | if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)) { | ||
1903 | *al = SSL_AD_HANDSHAKE_FAILURE; | ||
1904 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, | ||
1905 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | ||
1906 | return 0; | ||
1907 | } | ||
1908 | |||
1909 | return 1; | ||
1910 | } | ||
1911 | |||
1912 | int | ||
1913 | ssl_prepare_clienthello_tlsext(SSL *s) | ||
1914 | { | ||
1915 | return 1; | ||
1916 | } | ||
1917 | |||
1918 | int | ||
1919 | ssl_prepare_serverhello_tlsext(SSL *s) | ||
1920 | { | ||
1921 | return 1; | ||
1922 | } | ||
1923 | |||
1924 | int | ||
1925 | ssl_check_clienthello_tlsext_early(SSL *s) | ||
1926 | { | ||
1927 | int ret = SSL_TLSEXT_ERR_NOACK; | ||
1928 | int al = SSL_AD_UNRECOGNIZED_NAME; | ||
1929 | |||
1930 | /* The handling of the ECPointFormats extension is done elsewhere, namely in | ||
1931 | * ssl3_choose_cipher in s3_lib.c. | ||
1932 | */ | ||
1933 | /* The handling of the EllipticCurves extension is done elsewhere, namely in | ||
1934 | * ssl3_choose_cipher in s3_lib.c. | ||
1935 | */ | ||
1936 | |||
1937 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | ||
1938 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); | ||
1939 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) | ||
1940 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); | ||
1941 | |||
1942 | switch (ret) { | ||
1943 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
1944 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
1945 | return -1; | ||
1946 | case SSL_TLSEXT_ERR_ALERT_WARNING: | ||
1947 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | ||
1948 | return 1; | ||
1949 | case SSL_TLSEXT_ERR_NOACK: | ||
1950 | s->servername_done = 0; | ||
1951 | default: | ||
1952 | return 1; | ||
1953 | } | ||
1954 | } | ||
1955 | |||
1956 | int | ||
1957 | ssl_check_clienthello_tlsext_late(SSL *s) | ||
1958 | { | ||
1959 | int ret = SSL_TLSEXT_ERR_OK; | ||
1960 | int al = 0; /* XXX gcc3 */ | ||
1961 | |||
1962 | /* If status request then ask callback what to do. | ||
1963 | * Note: this must be called after servername callbacks in case | ||
1964 | * the certificate has changed, and must be called after the cipher | ||
1965 | * has been chosen because this may influence which certificate is sent | ||
1966 | */ | ||
1967 | if ((s->tlsext_status_type != -1) && | ||
1968 | s->ctx && s->ctx->tlsext_status_cb) { | ||
1969 | int r; | ||
1970 | CERT_PKEY *certpkey; | ||
1971 | certpkey = ssl_get_server_send_pkey(s); | ||
1972 | /* If no certificate can't return certificate status */ | ||
1973 | if (certpkey == NULL) { | ||
1974 | s->tlsext_status_expected = 0; | ||
1975 | return 1; | ||
1976 | } | ||
1977 | /* Set current certificate to one we will use so | ||
1978 | * SSL_get_certificate et al can pick it up. | ||
1979 | */ | ||
1980 | s->cert->key = certpkey; | ||
1981 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | ||
1982 | switch (r) { | ||
1983 | /* We don't want to send a status request response */ | ||
1984 | case SSL_TLSEXT_ERR_NOACK: | ||
1985 | s->tlsext_status_expected = 0; | ||
1986 | break; | ||
1987 | /* status request response should be sent */ | ||
1988 | case SSL_TLSEXT_ERR_OK: | ||
1989 | if (s->tlsext_ocsp_resp) | ||
1990 | s->tlsext_status_expected = 1; | ||
1991 | else | ||
1992 | s->tlsext_status_expected = 0; | ||
1993 | break; | ||
1994 | /* something bad happened */ | ||
1995 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
1996 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
1997 | al = SSL_AD_INTERNAL_ERROR; | ||
1998 | goto err; | ||
1999 | } | ||
2000 | } else | ||
2001 | s->tlsext_status_expected = 0; | ||
2002 | |||
2003 | err: | ||
2004 | switch (ret) { | ||
2005 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
2006 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2007 | return -1; | ||
2008 | case SSL_TLSEXT_ERR_ALERT_WARNING: | ||
2009 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | ||
2010 | return 1; | ||
2011 | default: | ||
2012 | return 1; | ||
2013 | } | ||
2014 | } | ||
2015 | |||
2016 | int | ||
2017 | ssl_check_serverhello_tlsext(SSL *s) | ||
2018 | { | ||
2019 | int ret = SSL_TLSEXT_ERR_NOACK; | ||
2020 | int al = SSL_AD_UNRECOGNIZED_NAME; | ||
2021 | |||
2022 | /* If we are client and using an elliptic curve cryptography cipher | ||
2023 | * suite, then if server returns an EC point formats lists extension | ||
2024 | * it must contain uncompressed. | ||
2025 | */ | ||
2026 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
2027 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | ||
2028 | if ((s->tlsext_ecpointformatlist != NULL) && | ||
2029 | (s->tlsext_ecpointformatlist_length > 0) && | ||
2030 | (s->session->tlsext_ecpointformatlist != NULL) && | ||
2031 | (s->session->tlsext_ecpointformatlist_length > 0) && | ||
2032 | ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA))) { | ||
2033 | /* we are using an ECC cipher */ | ||
2034 | size_t i; | ||
2035 | unsigned char *list; | ||
2036 | int found_uncompressed = 0; | ||
2037 | list = s->session->tlsext_ecpointformatlist; | ||
2038 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) { | ||
2039 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { | ||
2040 | found_uncompressed = 1; | ||
2041 | break; | ||
2042 | } | ||
2043 | } | ||
2044 | if (!found_uncompressed) { | ||
2045 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); | ||
2046 | return -1; | ||
2047 | } | ||
2048 | } | ||
2049 | ret = SSL_TLSEXT_ERR_OK; | ||
2050 | |||
2051 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | ||
2052 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); | ||
2053 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) | ||
2054 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); | ||
2055 | |||
2056 | /* If we've requested certificate status and we wont get one | ||
2057 | * tell the callback | ||
2058 | */ | ||
2059 | if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected) && | ||
2060 | s->ctx && s->ctx->tlsext_status_cb) { | ||
2061 | int r; | ||
2062 | /* Set resp to NULL, resplen to -1 so callback knows | ||
2063 | * there is no response. | ||
2064 | */ | ||
2065 | free(s->tlsext_ocsp_resp); | ||
2066 | s->tlsext_ocsp_resp = NULL; | ||
2067 | s->tlsext_ocsp_resplen = -1; | ||
2068 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | ||
2069 | if (r == 0) { | ||
2070 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; | ||
2071 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
2072 | } | ||
2073 | if (r < 0) { | ||
2074 | al = SSL_AD_INTERNAL_ERROR; | ||
2075 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
2076 | } | ||
2077 | } | ||
2078 | |||
2079 | switch (ret) { | ||
2080 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
2081 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
2082 | |||
2083 | return -1; | ||
2084 | case SSL_TLSEXT_ERR_ALERT_WARNING: | ||
2085 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | ||
2086 | |||
2087 | return 1; | ||
2088 | case SSL_TLSEXT_ERR_NOACK: | ||
2089 | s->servername_done = 0; | ||
2090 | default: | ||
2091 | return 1; | ||
2092 | } | ||
2093 | } | ||
2094 | |||
2095 | /* Since the server cache lookup is done early on in the processing of the | ||
2096 | * ClientHello, and other operations depend on the result, we need to handle | ||
2097 | * any TLS session ticket extension at the same time. | ||
2098 | * | ||
2099 | * session_id: points at the session ID in the ClientHello. This code will | ||
2100 | * read past the end of this in order to parse out the session ticket | ||
2101 | * extension, if any. | ||
2102 | * len: the length of the session ID. | ||
2103 | * limit: a pointer to the first byte after the ClientHello. | ||
2104 | * ret: (output) on return, if a ticket was decrypted, then this is set to | ||
2105 | * point to the resulting session. | ||
2106 | * | ||
2107 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key | ||
2108 | * ciphersuite, in which case we have no use for session tickets and one will | ||
2109 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. | ||
2110 | * | ||
2111 | * Returns: | ||
2112 | * -1: fatal error, either from parsing or decrypting the ticket. | ||
2113 | * 0: no ticket was found (or was ignored, based on settings). | ||
2114 | * 1: a zero length extension was found, indicating that the client supports | ||
2115 | * session tickets but doesn't currently have one to offer. | ||
2116 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but | ||
2117 | * couldn't be decrypted because of a non-fatal error. | ||
2118 | * 3: a ticket was successfully decrypted and *ret was set. | ||
2119 | * | ||
2120 | * Side effects: | ||
2121 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue | ||
2122 | * a new session ticket to the client because the client indicated support | ||
2123 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | ||
2124 | * a session ticket or we couldn't use the one it gave us, or if | ||
2125 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. | ||
2126 | * Otherwise, s->tlsext_ticket_expected is set to 0. | ||
2127 | */ | ||
2128 | int | ||
2129 | tls1_process_ticket(SSL *s, unsigned char *session_id, int len, | ||
2130 | const unsigned char *limit, SSL_SESSION **ret) | ||
2131 | { | ||
2132 | /* Point after session ID in client hello */ | ||
2133 | const unsigned char *p = session_id + len; | ||
2134 | unsigned short i; | ||
2135 | |||
2136 | *ret = NULL; | ||
2137 | s->tlsext_ticket_expected = 0; | ||
2138 | |||
2139 | /* If tickets disabled behave as if no ticket present | ||
2140 | * to permit stateful resumption. | ||
2141 | */ | ||
2142 | if (SSL_get_options(s) & SSL_OP_NO_TICKET) | ||
2143 | return 0; | ||
2144 | if ((s->version <= SSL3_VERSION) || !limit) | ||
2145 | return 0; | ||
2146 | if (p >= limit) | ||
2147 | return -1; | ||
2148 | /* Skip past DTLS cookie */ | ||
2149 | if (SSL_IS_DTLS(s)) { | ||
2150 | i = *(p++); | ||
2151 | p += i; | ||
2152 | if (p >= limit) | ||
2153 | return -1; | ||
2154 | } | ||
2155 | /* Skip past cipher list */ | ||
2156 | n2s(p, i); | ||
2157 | p += i; | ||
2158 | if (p >= limit) | ||
2159 | return -1; | ||
2160 | /* Skip past compression algorithm list */ | ||
2161 | i = *(p++); | ||
2162 | p += i; | ||
2163 | if (p > limit) | ||
2164 | return -1; | ||
2165 | /* Now at start of extensions */ | ||
2166 | if ((p + 2) >= limit) | ||
2167 | return 0; | ||
2168 | n2s(p, i); | ||
2169 | while ((p + 4) <= limit) { | ||
2170 | unsigned short type, size; | ||
2171 | n2s(p, type); | ||
2172 | n2s(p, size); | ||
2173 | if (p + size > limit) | ||
2174 | return 0; | ||
2175 | if (type == TLSEXT_TYPE_session_ticket) { | ||
2176 | int r; | ||
2177 | if (size == 0) { | ||
2178 | /* The client will accept a ticket but doesn't | ||
2179 | * currently have one. */ | ||
2180 | s->tlsext_ticket_expected = 1; | ||
2181 | return 1; | ||
2182 | } | ||
2183 | if (s->tls_session_secret_cb) { | ||
2184 | /* Indicate that the ticket couldn't be | ||
2185 | * decrypted rather than generating the session | ||
2186 | * from ticket now, trigger abbreviated | ||
2187 | * handshake based on external mechanism to | ||
2188 | * calculate the master secret later. */ | ||
2189 | return 2; | ||
2190 | } | ||
2191 | r = tls_decrypt_ticket(s, p, size, session_id, len, ret); | ||
2192 | switch (r) { | ||
2193 | case 2: /* ticket couldn't be decrypted */ | ||
2194 | s->tlsext_ticket_expected = 1; | ||
2195 | return 2; | ||
2196 | case 3: /* ticket was decrypted */ | ||
2197 | return r; | ||
2198 | case 4: /* ticket decrypted but need to renew */ | ||
2199 | s->tlsext_ticket_expected = 1; | ||
2200 | return 3; | ||
2201 | default: /* fatal error */ | ||
2202 | return -1; | ||
2203 | } | ||
2204 | } | ||
2205 | p += size; | ||
2206 | } | ||
2207 | return 0; | ||
2208 | } | ||
2209 | |||
2210 | /* tls_decrypt_ticket attempts to decrypt a session ticket. | ||
2211 | * | ||
2212 | * etick: points to the body of the session ticket extension. | ||
2213 | * eticklen: the length of the session tickets extenion. | ||
2214 | * sess_id: points at the session ID. | ||
2215 | * sesslen: the length of the session ID. | ||
2216 | * psess: (output) on return, if a ticket was decrypted, then this is set to | ||
2217 | * point to the resulting session. | ||
2218 | * | ||
2219 | * Returns: | ||
2220 | * -1: fatal error, either from parsing or decrypting the ticket. | ||
2221 | * 2: the ticket couldn't be decrypted. | ||
2222 | * 3: a ticket was successfully decrypted and *psess was set. | ||
2223 | * 4: same as 3, but the ticket needs to be renewed. | ||
2224 | */ | ||
2225 | static int | ||
2226 | tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen, | ||
2227 | const unsigned char *sess_id, int sesslen, SSL_SESSION **psess) | ||
2228 | { | ||
2229 | SSL_SESSION *sess; | ||
2230 | unsigned char *sdec; | ||
2231 | const unsigned char *p; | ||
2232 | int slen, mlen, renew_ticket = 0; | ||
2233 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; | ||
2234 | HMAC_CTX hctx; | ||
2235 | EVP_CIPHER_CTX ctx; | ||
2236 | SSL_CTX *tctx = s->initial_ctx; | ||
2237 | /* Need at least keyname + iv + some encrypted data */ | ||
2238 | if (eticklen < 48) | ||
2239 | return 2; | ||
2240 | /* Initialize session ticket encryption and HMAC contexts */ | ||
2241 | HMAC_CTX_init(&hctx); | ||
2242 | EVP_CIPHER_CTX_init(&ctx); | ||
2243 | if (tctx->tlsext_ticket_key_cb) { | ||
2244 | unsigned char *nctick = (unsigned char *)etick; | ||
2245 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, | ||
2246 | &ctx, &hctx, 0); | ||
2247 | if (rv < 0) { | ||
2248 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2249 | return -1; | ||
2250 | } | ||
2251 | if (rv == 0) { | ||
2252 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2253 | return 2; | ||
2254 | } | ||
2255 | if (rv == 2) | ||
2256 | renew_ticket = 1; | ||
2257 | } else { | ||
2258 | /* Check key name matches */ | ||
2259 | if (timingsafe_memcmp(etick, tctx->tlsext_tick_key_name, 16)) | ||
2260 | return 2; | ||
2261 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | ||
2262 | tlsext_tick_md(), NULL); | ||
2263 | EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | ||
2264 | tctx->tlsext_tick_aes_key, etick + 16); | ||
2265 | } | ||
2266 | /* Attempt to process session ticket, first conduct sanity and | ||
2267 | * integrity checks on ticket. | ||
2268 | */ | ||
2269 | mlen = HMAC_size(&hctx); | ||
2270 | if (mlen < 0) { | ||
2271 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2272 | return -1; | ||
2273 | } | ||
2274 | eticklen -= mlen; | ||
2275 | /* Check HMAC of encrypted ticket */ | ||
2276 | HMAC_Update(&hctx, etick, eticklen); | ||
2277 | HMAC_Final(&hctx, tick_hmac, NULL); | ||
2278 | HMAC_CTX_cleanup(&hctx); | ||
2279 | if (timingsafe_memcmp(tick_hmac, etick + eticklen, mlen)) { | ||
2280 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2281 | return 2; | ||
2282 | } | ||
2283 | /* Attempt to decrypt session data */ | ||
2284 | /* Move p after IV to start of encrypted ticket, update length */ | ||
2285 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); | ||
2286 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); | ||
2287 | sdec = malloc(eticklen); | ||
2288 | if (!sdec) { | ||
2289 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2290 | return -1; | ||
2291 | } | ||
2292 | EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen); | ||
2293 | if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) { | ||
2294 | free(sdec); | ||
2295 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2296 | return 2; | ||
2297 | } | ||
2298 | slen += mlen; | ||
2299 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
2300 | p = sdec; | ||
2301 | |||
2302 | sess = d2i_SSL_SESSION(NULL, &p, slen); | ||
2303 | free(sdec); | ||
2304 | if (sess) { | ||
2305 | /* The session ID, if non-empty, is used by some clients to | ||
2306 | * detect that the ticket has been accepted. So we copy it to | ||
2307 | * the session structure. If it is empty set length to zero | ||
2308 | * as required by standard. | ||
2309 | */ | ||
2310 | if (sesslen) | ||
2311 | memcpy(sess->session_id, sess_id, sesslen); | ||
2312 | sess->session_id_length = sesslen; | ||
2313 | *psess = sess; | ||
2314 | if (renew_ticket) | ||
2315 | return 4; | ||
2316 | else | ||
2317 | return 3; | ||
2318 | } | ||
2319 | ERR_clear_error(); | ||
2320 | /* For session parse failure, indicate that we need to send a new | ||
2321 | * ticket. */ | ||
2322 | return 2; | ||
2323 | } | ||
2324 | |||
2325 | /* Tables to translate from NIDs to TLS v1.2 ids */ | ||
2326 | |||
2327 | typedef struct { | ||
2328 | int nid; | ||
2329 | int id; | ||
2330 | } tls12_lookup; | ||
2331 | |||
2332 | static tls12_lookup tls12_md[] = { | ||
2333 | {NID_md5, TLSEXT_hash_md5}, | ||
2334 | {NID_sha1, TLSEXT_hash_sha1}, | ||
2335 | {NID_sha224, TLSEXT_hash_sha224}, | ||
2336 | {NID_sha256, TLSEXT_hash_sha256}, | ||
2337 | {NID_sha384, TLSEXT_hash_sha384}, | ||
2338 | {NID_sha512, TLSEXT_hash_sha512}, | ||
2339 | {NID_id_GostR3411_94, TLSEXT_hash_gost94}, | ||
2340 | {NID_id_tc26_gost3411_2012_256, TLSEXT_hash_streebog_256}, | ||
2341 | {NID_id_tc26_gost3411_2012_512, TLSEXT_hash_streebog_512} | ||
2342 | }; | ||
2343 | |||
2344 | static tls12_lookup tls12_sig[] = { | ||
2345 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, | ||
2346 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, | ||
2347 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}, | ||
2348 | {EVP_PKEY_GOSTR01, TLSEXT_signature_gostr01}, | ||
2349 | }; | ||
2350 | |||
2351 | static int | ||
2352 | tls12_find_id(int nid, tls12_lookup *table, size_t tlen) | ||
2353 | { | ||
2354 | size_t i; | ||
2355 | for (i = 0; i < tlen; i++) { | ||
2356 | if (table[i].nid == nid) | ||
2357 | return table[i].id; | ||
2358 | } | ||
2359 | return -1; | ||
2360 | } | ||
2361 | |||
2362 | int | ||
2363 | tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md) | ||
2364 | { | ||
2365 | int sig_id, md_id; | ||
2366 | if (!md) | ||
2367 | return 0; | ||
2368 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, | ||
2369 | sizeof(tls12_md) / sizeof(tls12_lookup)); | ||
2370 | if (md_id == -1) | ||
2371 | return 0; | ||
2372 | sig_id = tls12_get_sigid(pk); | ||
2373 | if (sig_id == -1) | ||
2374 | return 0; | ||
2375 | p[0] = (unsigned char)md_id; | ||
2376 | p[1] = (unsigned char)sig_id; | ||
2377 | return 1; | ||
2378 | } | ||
2379 | |||
2380 | int | ||
2381 | tls12_get_sigid(const EVP_PKEY *pk) | ||
2382 | { | ||
2383 | return tls12_find_id(pk->type, tls12_sig, | ||
2384 | sizeof(tls12_sig) / sizeof(tls12_lookup)); | ||
2385 | } | ||
2386 | |||
2387 | const EVP_MD * | ||
2388 | tls12_get_hash(unsigned char hash_alg) | ||
2389 | { | ||
2390 | switch (hash_alg) { | ||
2391 | case TLSEXT_hash_sha1: | ||
2392 | return EVP_sha1(); | ||
2393 | case TLSEXT_hash_sha224: | ||
2394 | return EVP_sha224(); | ||
2395 | case TLSEXT_hash_sha256: | ||
2396 | return EVP_sha256(); | ||
2397 | case TLSEXT_hash_sha384: | ||
2398 | return EVP_sha384(); | ||
2399 | case TLSEXT_hash_sha512: | ||
2400 | return EVP_sha512(); | ||
2401 | #ifndef OPENSSL_NO_GOST | ||
2402 | case TLSEXT_hash_gost94: | ||
2403 | return EVP_gostr341194(); | ||
2404 | case TLSEXT_hash_streebog_256: | ||
2405 | return EVP_streebog256(); | ||
2406 | case TLSEXT_hash_streebog_512: | ||
2407 | return EVP_streebog512(); | ||
2408 | #endif | ||
2409 | default: | ||
2410 | return NULL; | ||
2411 | } | ||
2412 | } | ||
2413 | |||
2414 | /* Set preferred digest for each key type */ | ||
2415 | |||
2416 | int | ||
2417 | tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize) | ||
2418 | { | ||
2419 | int i, idx; | ||
2420 | const EVP_MD *md; | ||
2421 | CERT *c = s->cert; | ||
2422 | |||
2423 | /* Extension ignored for inappropriate versions */ | ||
2424 | if (!SSL_USE_SIGALGS(s)) | ||
2425 | return 1; | ||
2426 | |||
2427 | /* Should never happen */ | ||
2428 | if (!c) | ||
2429 | return 0; | ||
2430 | |||
2431 | c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL; | ||
2432 | c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL; | ||
2433 | c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL; | ||
2434 | c->pkeys[SSL_PKEY_ECC].digest = NULL; | ||
2435 | c->pkeys[SSL_PKEY_GOST01].digest = NULL; | ||
2436 | |||
2437 | for (i = 0; i < dsize; i += 2) { | ||
2438 | unsigned char hash_alg = data[i], sig_alg = data[i + 1]; | ||
2439 | |||
2440 | switch (sig_alg) { | ||
2441 | case TLSEXT_signature_rsa: | ||
2442 | idx = SSL_PKEY_RSA_SIGN; | ||
2443 | break; | ||
2444 | case TLSEXT_signature_dsa: | ||
2445 | idx = SSL_PKEY_DSA_SIGN; | ||
2446 | break; | ||
2447 | case TLSEXT_signature_ecdsa: | ||
2448 | idx = SSL_PKEY_ECC; | ||
2449 | break; | ||
2450 | case TLSEXT_signature_gostr01: | ||
2451 | case TLSEXT_signature_gostr12_256: | ||
2452 | case TLSEXT_signature_gostr12_512: | ||
2453 | idx = SSL_PKEY_GOST01; | ||
2454 | break; | ||
2455 | default: | ||
2456 | continue; | ||
2457 | } | ||
2458 | |||
2459 | if (c->pkeys[idx].digest == NULL) { | ||
2460 | md = tls12_get_hash(hash_alg); | ||
2461 | if (md) { | ||
2462 | c->pkeys[idx].digest = md; | ||
2463 | if (idx == SSL_PKEY_RSA_SIGN) | ||
2464 | c->pkeys[SSL_PKEY_RSA_ENC].digest = md; | ||
2465 | } | ||
2466 | } | ||
2467 | |||
2468 | } | ||
2469 | |||
2470 | /* Set any remaining keys to default values. NOTE: if alg is not | ||
2471 | * supported it stays as NULL. | ||
2472 | */ | ||
2473 | if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest) | ||
2474 | c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1(); | ||
2475 | if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) { | ||
2476 | c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); | ||
2477 | c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); | ||
2478 | } | ||
2479 | if (!c->pkeys[SSL_PKEY_ECC].digest) | ||
2480 | c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); | ||
2481 | #ifndef OPENSSL_NO_GOST | ||
2482 | if (!c->pkeys[SSL_PKEY_GOST01].digest) | ||
2483 | c->pkeys[SSL_PKEY_GOST01].digest = EVP_gostr341194(); | ||
2484 | #endif | ||
2485 | return 1; | ||
2486 | } | ||