blob: b3cb70b8401635c2866b01ed35d17c64f03134a6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com). */
108
109#include <stdio.h>
110#include <assert.h>
111
David Benjamin03973092014-06-24 23:27:17 -0400112#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113#include <openssl/evp.h>
114#include <openssl/hmac.h>
115#include <openssl/mem.h>
116#include <openssl/obj.h>
117#include <openssl/rand.h>
118
119#include "ssl_locl.h"
120#ifndef OPENSSL_NO_TLSEXT
121static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
124static int ssl_check_clienthello_tlsext_early(SSL *s);
125int ssl_check_serverhello_tlsext(SSL *s);
126#endif
127
128SSL3_ENC_METHOD TLSv1_enc_data={
129 tls1_enc,
130 tls1_mac,
131 tls1_setup_key_block,
132 tls1_generate_master_secret,
133 tls1_change_cipher_state,
134 tls1_final_finish_mac,
135 TLS1_FINISH_MAC_LENGTH,
136 tls1_cert_verify_mac,
137 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
138 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
139 tls1_alert_code,
140 tls1_export_keying_material,
141 0,
142 SSL3_HM_HEADER_LENGTH,
143 ssl3_set_handshake_header,
144 ssl3_handshake_write
145 };
146
147SSL3_ENC_METHOD TLSv1_1_enc_data={
148 tls1_enc,
149 tls1_mac,
150 tls1_setup_key_block,
151 tls1_generate_master_secret,
152 tls1_change_cipher_state,
153 tls1_final_finish_mac,
154 TLS1_FINISH_MAC_LENGTH,
155 tls1_cert_verify_mac,
156 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
158 tls1_alert_code,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
163 ssl3_handshake_write
164 };
165
166SSL3_ENC_METHOD TLSv1_2_enc_data={
167 tls1_enc,
168 tls1_mac,
169 tls1_setup_key_block,
170 tls1_generate_master_secret,
171 tls1_change_cipher_state,
172 tls1_final_finish_mac,
173 TLS1_FINISH_MAC_LENGTH,
174 tls1_cert_verify_mac,
175 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
176 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
177 tls1_alert_code,
178 tls1_export_keying_material,
179 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
180 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
181 SSL3_HM_HEADER_LENGTH,
182 ssl3_set_handshake_header,
183 ssl3_handshake_write
184 };
185
186long tls1_default_timeout(void)
187 {
188 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
189 * is way too long for http, the cache would over fill */
190 return(60*60*2);
191 }
192
193int tls1_new(SSL *s)
194 {
195 if (!ssl3_new(s)) return(0);
196 s->method->ssl_clear(s);
197 return(1);
198 }
199
200void tls1_free(SSL *s)
201 {
202#ifndef OPENSSL_NO_TLSEXT
203 if (s->tlsext_session_ticket)
204 {
205 OPENSSL_free(s->tlsext_session_ticket);
206 }
207#endif /* OPENSSL_NO_TLSEXT */
208 ssl3_free(s);
209 }
210
211void tls1_clear(SSL *s)
212 {
213 ssl3_clear(s);
214 s->version = s->method->version;
215 }
216
Adam Langleydc9b1412014-06-20 12:00:00 -0700217char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx)
218 {
219 size_t len = ctx->client_hello_len;
220 const unsigned char *p = ctx->client_hello;
221 uint16_t *extension_types;
222 unsigned num_extensions;
223
224 /* Skip client version. */
225 if (len < 2)
226 return 0;
227 len -= 2; p += 2;
228
229 /* Skip client nonce. */
230 if (len < 32)
231 return 0;
232 len -= 32; p += 32;
233
234 /* Get length of session id. */
235 if (len < 1)
236 return 0;
237 ctx->session_id_len = *p;
238 p++; len--;
239
240 ctx->session_id = p;
241 if (len < ctx->session_id_len)
242 return 0;
243 p += ctx->session_id_len; len -= ctx->session_id_len;
244
245 /* Skip past DTLS cookie */
246 if (ctx->ssl->version == DTLS1_VERSION || ctx->ssl->version == DTLS1_BAD_VER)
247 {
248 unsigned cookie_len;
249
250 if (len < 1)
251 return 0;
252 cookie_len = *p;
253 p++; len--;
254 if (len < cookie_len)
255 return 0;
256 p += cookie_len; len -= cookie_len;
257 }
258
259 /* Skip cipher suites. */
260 if (len < 2)
261 return 0;
262 n2s(p, ctx->cipher_suites_len);
263 len -= 2;
264
265 if ((ctx->cipher_suites_len & 1) != 0)
266 return 0;
267
268 ctx->cipher_suites = p;
269 if (len < ctx->cipher_suites_len)
270 return 0;
271 p += ctx->cipher_suites_len; len -= ctx->cipher_suites_len;
272
273 /* Skip compression methods. */
274 if (len < 1)
275 return 0;
276 ctx->compression_methods_len = *p;
277 p++; len--;
278
279 ctx->compression_methods = p;
280 if (len < ctx->compression_methods_len)
281 return 0;
282 p += ctx->compression_methods_len; len -= ctx->compression_methods_len;
283
284 /* If the ClientHello ends here then it's valid, but doesn't have any
285 * extensions. (E.g. SSLv3.) */
286 if (len == 0)
287 {
288 ctx->extensions = NULL;
289 ctx->extensions_len = 0;
290 return 1;
291 }
292
293 if (len < 2)
294 return 0;
295 n2s(p, ctx->extensions_len);
296 len -= 2;
297
298 if (ctx->extensions_len == 0 && len == 0)
299 {
300 ctx->extensions = NULL;
301 return 1;
302 }
303
304 ctx->extensions = p;
305 if (len != ctx->extensions_len)
306 return 0;
307
308 /* Verify that the extensions have valid lengths and that there are
309 * no duplicates. Each extension takes, at least, four bytes, so
310 * we can allocate a buffer of extensions_len/4 elements and be sure
311 * that we have enough space for all the extension types. */
312 extension_types =
313 OPENSSL_malloc(sizeof(uint16_t) * ctx->extensions_len/4);
314 if (extension_types == NULL)
315 return 0;
316 num_extensions = 0;
317
318 while (len != 0)
319 {
320 uint16_t extension_type, extension_len;
321 unsigned i;
322
323 if (len < 4)
324 goto err;
325 n2s(p, extension_type);
326 n2s(p, extension_len);
327 len -= 4;
328
329 if (len < extension_len)
330 goto err;
331 p += extension_len; len -= extension_len;
332
333 for (i = 0; i < num_extensions; i++)
334 {
335 if (extension_types[i] == extension_type)
336 {
337 /* Duplicate extension type. */
338 goto err;
339 }
340 }
341 extension_types[num_extensions] = extension_type;
342 num_extensions++;
343 }
344
345 OPENSSL_free(extension_types);
346 return 1;
347
348err:
349 OPENSSL_free(extension_types);
350 return 0;
351 }
352
353char
354SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx,
355 uint16_t extension_type,
356 const unsigned char **out_data,
357 size_t *out_len)
358 {
359 size_t len = ctx->extensions_len;
360 const unsigned char *p = ctx->extensions;
361
362 while (len != 0)
363 {
364 uint16_t ext_type, ext_len;
365
366 if (len < 4)
367 return 0;
368 n2s(p, ext_type);
369 n2s(p, ext_len);
370 len -= 4;
371
372 if (len < ext_len)
373 return 0;
374 if (ext_type == extension_type)
375 {
376 *out_data = p;
377 *out_len = ext_len;
378 return 1;
379 }
380
381 p += ext_len; len -= ext_len;
382 }
383
384 return 0;
385 }
386
Adam Langley95c29f32014-06-20 12:00:00 -0700387#ifndef OPENSSL_NO_EC
388
389static int nid_list[] =
390 {
391 NID_sect163k1, /* sect163k1 (1) */
392 NID_sect163r1, /* sect163r1 (2) */
393 NID_sect163r2, /* sect163r2 (3) */
394 NID_sect193r1, /* sect193r1 (4) */
395 NID_sect193r2, /* sect193r2 (5) */
396 NID_sect233k1, /* sect233k1 (6) */
397 NID_sect233r1, /* sect233r1 (7) */
398 NID_sect239k1, /* sect239k1 (8) */
399 NID_sect283k1, /* sect283k1 (9) */
400 NID_sect283r1, /* sect283r1 (10) */
401 NID_sect409k1, /* sect409k1 (11) */
402 NID_sect409r1, /* sect409r1 (12) */
403 NID_sect571k1, /* sect571k1 (13) */
404 NID_sect571r1, /* sect571r1 (14) */
405 NID_secp160k1, /* secp160k1 (15) */
406 NID_secp160r1, /* secp160r1 (16) */
407 NID_secp160r2, /* secp160r2 (17) */
408 NID_secp192k1, /* secp192k1 (18) */
409 NID_X9_62_prime192v1, /* secp192r1 (19) */
410 NID_secp224k1, /* secp224k1 (20) */
411 NID_secp224r1, /* secp224r1 (21) */
412 NID_secp256k1, /* secp256k1 (22) */
413 NID_X9_62_prime256v1, /* secp256r1 (23) */
414 NID_secp384r1, /* secp384r1 (24) */
415 NID_secp521r1, /* secp521r1 (25) */
416 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
417 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
418 NID_brainpoolP512r1 /* brainpool512r1 (28) */
419 };
420
421
422static const unsigned char ecformats_default[] =
423 {
424 TLSEXT_ECPOINTFORMAT_uncompressed,
Adam Langley95c29f32014-06-20 12:00:00 -0700425 };
426
427static const unsigned char eccurves_default[] =
428 {
Adam Langleyf3a95c42014-06-20 15:50:41 -0700429 0,23, /* secp256r1 (23) */
Adam Langley95c29f32014-06-20 12:00:00 -0700430 0,24, /* secp384r1 (24) */
Adam Langleyf3a95c42014-06-20 15:50:41 -0700431 0,25, /* secp521r1 (25) */
Adam Langley95c29f32014-06-20 12:00:00 -0700432 };
433
434static const unsigned char suiteb_curves[] =
435 {
436 0, TLSEXT_curve_P_256,
437 0, TLSEXT_curve_P_384
438 };
439
440int tls1_ec_curve_id2nid(int curve_id)
441 {
442 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
443 if ((curve_id < 1) || ((unsigned int)curve_id >
444 sizeof(nid_list)/sizeof(nid_list[0])))
445 return 0;
446 return nid_list[curve_id-1];
447 }
448
449int tls1_ec_nid2curve_id(int nid)
450 {
451 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
452 switch (nid)
453 {
454 case NID_sect163k1: /* sect163k1 (1) */
455 return 1;
456 case NID_sect163r1: /* sect163r1 (2) */
457 return 2;
458 case NID_sect163r2: /* sect163r2 (3) */
459 return 3;
460 case NID_sect193r1: /* sect193r1 (4) */
461 return 4;
462 case NID_sect193r2: /* sect193r2 (5) */
463 return 5;
464 case NID_sect233k1: /* sect233k1 (6) */
465 return 6;
466 case NID_sect233r1: /* sect233r1 (7) */
467 return 7;
468 case NID_sect239k1: /* sect239k1 (8) */
469 return 8;
470 case NID_sect283k1: /* sect283k1 (9) */
471 return 9;
472 case NID_sect283r1: /* sect283r1 (10) */
473 return 10;
474 case NID_sect409k1: /* sect409k1 (11) */
475 return 11;
476 case NID_sect409r1: /* sect409r1 (12) */
477 return 12;
478 case NID_sect571k1: /* sect571k1 (13) */
479 return 13;
480 case NID_sect571r1: /* sect571r1 (14) */
481 return 14;
482 case NID_secp160k1: /* secp160k1 (15) */
483 return 15;
484 case NID_secp160r1: /* secp160r1 (16) */
485 return 16;
486 case NID_secp160r2: /* secp160r2 (17) */
487 return 17;
488 case NID_secp192k1: /* secp192k1 (18) */
489 return 18;
490 case NID_X9_62_prime192v1: /* secp192r1 (19) */
491 return 19;
492 case NID_secp224k1: /* secp224k1 (20) */
493 return 20;
494 case NID_secp224r1: /* secp224r1 (21) */
495 return 21;
496 case NID_secp256k1: /* secp256k1 (22) */
497 return 22;
498 case NID_X9_62_prime256v1: /* secp256r1 (23) */
499 return 23;
500 case NID_secp384r1: /* secp384r1 (24) */
501 return 24;
502 case NID_secp521r1: /* secp521r1 (25) */
503 return 25;
504 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */
505 return 26;
506 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */
507 return 27;
508 case NID_brainpoolP512r1: /* brainpool512r1 (28) */
509 return 28;
510 default:
511 return 0;
512 }
513 }
514/* Get curves list, if "sess" is set return client curves otherwise
515 * preferred list
516 */
517static void tls1_get_curvelist(SSL *s, int sess,
518 const unsigned char **pcurves,
519 size_t *pcurveslen)
520 {
521 if (sess)
522 {
523 *pcurves = s->session->tlsext_ellipticcurvelist;
524 *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
525 return;
526 }
527 /* For Suite B mode only include P-256, P-384 */
528 switch (tls1_suiteb(s))
529 {
530 case SSL_CERT_FLAG_SUITEB_128_LOS:
531 *pcurves = suiteb_curves;
532 *pcurveslen = sizeof(suiteb_curves);
533 break;
534
535 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
536 *pcurves = suiteb_curves;
537 *pcurveslen = 2;
538 break;
539
540 case SSL_CERT_FLAG_SUITEB_192_LOS:
541 *pcurves = suiteb_curves + 2;
542 *pcurveslen = 2;
543 break;
544 default:
545 *pcurves = s->tlsext_ellipticcurvelist;
546 *pcurveslen = s->tlsext_ellipticcurvelist_length;
547 }
548 if (!*pcurves)
549 {
550 *pcurves = eccurves_default;
551 *pcurveslen = sizeof(eccurves_default);
552 }
553 }
554/* Check a curve is one of our preferences */
555int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
556 {
557 const unsigned char *curves;
558 size_t curveslen, i;
559 unsigned int suiteb_flags = tls1_suiteb(s);
560 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
561 return 0;
562 /* Check curve matches Suite B preferences */
563 if (suiteb_flags)
564 {
565 unsigned long cid = s->s3->tmp.new_cipher->id;
566 if (p[1])
567 return 0;
568 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
569 {
570 if (p[2] != TLSEXT_curve_P_256)
571 return 0;
572 }
573 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
574 {
575 if (p[2] != TLSEXT_curve_P_384)
576 return 0;
577 }
578 else /* Should never happen */
579 return 0;
580 }
581 tls1_get_curvelist(s, 0, &curves, &curveslen);
582 for (i = 0; i < curveslen; i += 2, curves += 2)
583 {
584 if (p[1] == curves[0] && p[2] == curves[1])
585 return 1;
586 }
587 return 0;
588 }
589
590/* Return nth shared curve. If nmatch == -1 return number of
591 * matches. For nmatch == -2 return the NID of the curve to use for
592 * an EC tmp key.
593 */
594
595int tls1_shared_curve(SSL *s, int nmatch)
596 {
597 const unsigned char *pref, *supp;
598 size_t preflen, supplen, i, j;
599 int k;
600 /* Can't do anything on client side */
601 if (s->server == 0)
602 return -1;
603 if (nmatch == -2)
604 {
605 if (tls1_suiteb(s))
606 {
607 /* For Suite B ciphersuite determines curve: we
608 * already know these are acceptable due to previous
609 * checks.
610 */
611 unsigned long cid = s->s3->tmp.new_cipher->id;
612 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
613 return NID_X9_62_prime256v1; /* P-256 */
614 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
615 return NID_secp384r1; /* P-384 */
616 /* Should never happen */
617 return NID_undef;
618 }
619 /* If not Suite B just return first preference shared curve */
620 nmatch = 0;
621 }
622 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
623 &supp, &supplen);
624 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
625 &pref, &preflen);
626 preflen /= 2;
627 supplen /= 2;
628 k = 0;
629 for (i = 0; i < preflen; i++, pref+=2)
630 {
631 const unsigned char *tsupp = supp;
632 for (j = 0; j < supplen; j++, tsupp+=2)
633 {
634 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
635 {
636 if (nmatch == k)
637 {
638 int id = (pref[0] << 8) | pref[1];
639 return tls1_ec_curve_id2nid(id);
640 }
641 k++;
642 }
643 }
644 }
645 if (nmatch == -1)
646 return k;
647 return 0;
648 }
649
650int tls1_set_curves(unsigned char **pext, size_t *pextlen,
651 int *curves, size_t ncurves)
652 {
653 unsigned char *clist, *p;
654 size_t i;
655 /* Bitmap of curves included to detect duplicates: only works
656 * while curve ids < 32
657 */
658 unsigned long dup_list = 0;
659 clist = OPENSSL_malloc(ncurves * 2);
660 if (!clist)
661 return 0;
662 for (i = 0, p = clist; i < ncurves; i++)
663 {
664 unsigned long idmask;
665 int id;
666 id = tls1_ec_nid2curve_id(curves[i]);
667 idmask = 1L << id;
668 if (!id || (dup_list & idmask))
669 {
670 OPENSSL_free(clist);
671 return 0;
672 }
673 dup_list |= idmask;
674 s2n(id, p);
675 }
676 if (*pext)
677 OPENSSL_free(*pext);
678 *pext = clist;
679 *pextlen = ncurves * 2;
680 return 1;
681 }
682
683/* TODO(fork): remove */
684#if 0
685#define MAX_CURVELIST 28
686
687typedef struct
688 {
689 size_t nidcnt;
690 int nid_arr[MAX_CURVELIST];
691 } nid_cb_st;
692
693static int nid_cb(const char *elem, int len, void *arg)
694 {
695 nid_cb_st *narg = arg;
696 size_t i;
697 int nid;
698 char etmp[20];
699 if (narg->nidcnt == MAX_CURVELIST)
700 return 0;
701 if (len > (int)(sizeof(etmp) - 1))
702 return 0;
703 memcpy(etmp, elem, len);
704 etmp[len] = 0;
705 nid = EC_curve_nist2nid(etmp);
706 if (nid == NID_undef)
707 nid = OBJ_sn2nid(etmp);
708 if (nid == NID_undef)
709 nid = OBJ_ln2nid(etmp);
710 if (nid == NID_undef)
711 return 0;
712 for (i = 0; i < narg->nidcnt; i++)
713 if (narg->nid_arr[i] == nid)
714 return 0;
715 narg->nid_arr[narg->nidcnt++] = nid;
716 return 1;
717 }
718/* Set curves based on a colon separate list */
719int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
720 const char *str)
721 {
722 nid_cb_st ncb;
723 ncb.nidcnt = 0;
724 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
725 return 0;
726 if (pext == NULL)
727 return 1;
728 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
729 }
730#endif
731
732/* For an EC key set TLS id and required compression based on parameters */
733static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
734 EC_KEY *ec)
735 {
736 int is_prime = 1, id;
737 const EC_GROUP *grp;
738 if (!ec)
739 return 0;
740
741 /* TODO(fork): remove. All curves are prime now. */
742 grp = EC_KEY_get0_group(ec);
743 if (!grp)
744 return 0;
745#if 0
746 /* Determine if it is a prime field */
747 meth = EC_GROUP_method_of(grp);
748 if (!meth)
749 return 0;
750 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
751 is_prime = 1;
752 else
753 is_prime = 0;
754#endif
755
756 /* Determine curve ID */
757 id = EC_GROUP_get_curve_name(grp);
758 id = tls1_ec_nid2curve_id(id);
759 /* If we have an ID set it, otherwise set arbitrary explicit curve */
760 if (id)
761 {
762 curve_id[0] = 0;
763 curve_id[1] = (unsigned char)id;
764 }
765 else
766 {
767 curve_id[0] = 0xff;
768 if (is_prime)
769 curve_id[1] = 0x01;
770 else
771 curve_id[1] = 0x02;
772 }
773 if (comp_id)
774 {
775 if (EC_KEY_get0_public_key(ec) == NULL)
776 return 0;
777 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
778 {
779 if (is_prime)
780 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
781 else
782 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
783 }
784 else
785 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
786 }
787 return 1;
788 }
789/* Check an EC key is compatible with extensions */
790static int tls1_check_ec_key(SSL *s,
791 unsigned char *curve_id, unsigned char *comp_id)
792 {
793 const unsigned char *p;
794 size_t plen, i;
795 int j;
796 /* If point formats extension present check it, otherwise everything
797 * is supported (see RFC4492).
798 */
799 if (comp_id && s->session->tlsext_ecpointformatlist)
800 {
801 p = s->session->tlsext_ecpointformatlist;
802 plen = s->session->tlsext_ecpointformatlist_length;
803 for (i = 0; i < plen; i++, p++)
804 {
805 if (*comp_id == *p)
806 break;
807 }
808 if (i == plen)
809 return 0;
810 }
811 if (!curve_id)
812 return 1;
813 /* Check curve is consistent with client and server preferences */
814 for (j = 0; j <= 1; j++)
815 {
816 tls1_get_curvelist(s, j, &p, &plen);
817 for (i = 0; i < plen; i+=2, p+=2)
818 {
819 if (p[0] == curve_id[0] && p[1] == curve_id[1])
820 break;
821 }
822 if (i == plen)
823 return 0;
824 /* For clients can only check sent curve list */
825 if (!s->server)
826 return 1;
827 }
828 return 1;
829 }
830
831static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
832 size_t *pformatslen)
833 {
834 /* If we have a custom point format list use it otherwise
835 * use default */
836 if (s->tlsext_ecpointformatlist)
837 {
838 *pformats = s->tlsext_ecpointformatlist;
839 *pformatslen = s->tlsext_ecpointformatlist_length;
840 }
841 else
842 {
843 *pformats = ecformats_default;
844 /* For Suite B we don't support char2 fields */
845 if (tls1_suiteb(s))
846 *pformatslen = sizeof(ecformats_default) - 1;
847 else
848 *pformatslen = sizeof(ecformats_default);
849 }
850 }
851
852/* Check cert parameters compatible with extensions: currently just checks
853 * EC certificates have compatible curves and compression.
854 */
855static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
856 {
857 unsigned char comp_id, curve_id[2];
858 EVP_PKEY *pkey;
859 int rv;
860 pkey = X509_get_pubkey(x);
861 if (!pkey)
862 return 0;
863 /* If not EC nothing to do */
864 if (pkey->type != EVP_PKEY_EC)
865 {
866 EVP_PKEY_free(pkey);
867 return 1;
868 }
869 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
870 EVP_PKEY_free(pkey);
871 if (!rv)
872 return 0;
873 /* Can't check curve_id for client certs as we don't have a
874 * supported curves extension.
875 */
876 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
877 if (!rv)
878 return 0;
879 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
880 * SHA384+P-384, adjust digest if necessary.
881 */
882 if (set_ee_md && tls1_suiteb(s))
883 {
884 int check_md;
885 size_t i;
886 CERT *c = s->cert;
887 if (curve_id[0])
888 return 0;
889 /* Check to see we have necessary signing algorithm */
890 if (curve_id[1] == TLSEXT_curve_P_256)
891 check_md = NID_ecdsa_with_SHA256;
892 else if (curve_id[1] == TLSEXT_curve_P_384)
893 check_md = NID_ecdsa_with_SHA384;
894 else
895 return 0; /* Should never happen */
896 for (i = 0; i < c->shared_sigalgslen; i++)
897 if (check_md == c->shared_sigalgs[i].signandhash_nid)
898 break;
899 if (i == c->shared_sigalgslen)
900 return 0;
901 if (set_ee_md == 2)
902 {
903 if (check_md == NID_ecdsa_with_SHA256)
904 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
905 else
906 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
907 }
908 }
909 return rv;
910 }
911/* Check EC temporary key is compatible with client extensions */
912int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
913 {
914 unsigned char curve_id[2];
915 EC_KEY *ec = s->cert->ecdh_tmp;
916#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
917 /* Allow any curve: not just those peer supports */
918 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
919 return 1;
920#endif
921 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
922 * no other curves permitted.
923 */
924 if (tls1_suiteb(s))
925 {
926 /* Curve to check determined by ciphersuite */
927 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
928 curve_id[1] = TLSEXT_curve_P_256;
929 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
930 curve_id[1] = TLSEXT_curve_P_384;
931 else
932 return 0;
933 curve_id[0] = 0;
934 /* Check this curve is acceptable */
935 if (!tls1_check_ec_key(s, curve_id, NULL))
936 return 0;
937 /* If auto or setting curve from callback assume OK */
938 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
939 return 1;
940 /* Otherwise check curve is acceptable */
941 else
942 {
943 unsigned char curve_tmp[2];
944 if (!ec)
945 return 0;
946 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
947 return 0;
948 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
949 return 1;
950 return 0;
951 }
952
953 }
954 if (s->cert->ecdh_tmp_auto)
955 {
956 /* Need a shared curve */
957 if (tls1_shared_curve(s, 0))
958 return 1;
959 else return 0;
960 }
961 if (!ec)
962 {
963 if (s->cert->ecdh_tmp_cb)
964 return 1;
965 else
966 return 0;
967 }
968 if (!tls1_set_ec_id(curve_id, NULL, ec))
969 return 0;
970/* Set this to allow use of invalid curves for testing */
971#if 0
972 return 1;
973#else
974 return tls1_check_ec_key(s, curve_id, NULL);
975#endif
976 }
977
978#else
979
980static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
981 {
982 return 1;
983 }
984
985#endif /* OPENSSL_NO_EC */
986
987#ifndef OPENSSL_NO_TLSEXT
988
989/* List of supported signature algorithms and hashes. Should make this
990 * customisable at some point, for now include everything we support.
991 */
992
993#ifdef OPENSSL_NO_RSA
994#define tlsext_sigalg_rsa(md) /* */
995#else
996#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
997#endif
998
999#ifdef OPENSSL_NO_DSA
1000#define tlsext_sigalg_dsa(md) /* */
1001#else
1002#define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
1003#endif
1004
1005#ifdef OPENSSL_NO_ECDSA
1006#define tlsext_sigalg_ecdsa(md) /* */
1007#else
1008#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
1009#endif
1010
1011#define tlsext_sigalg(md) \
1012 tlsext_sigalg_rsa(md) \
1013 tlsext_sigalg_dsa(md) \
1014 tlsext_sigalg_ecdsa(md)
1015
1016static unsigned char tls12_sigalgs[] = {
1017#ifndef OPENSSL_NO_SHA512
1018 tlsext_sigalg(TLSEXT_hash_sha512)
1019 tlsext_sigalg(TLSEXT_hash_sha384)
1020#endif
1021#ifndef OPENSSL_NO_SHA256
1022 tlsext_sigalg(TLSEXT_hash_sha256)
1023 tlsext_sigalg(TLSEXT_hash_sha224)
1024#endif
1025#ifndef OPENSSL_NO_SHA
1026 tlsext_sigalg(TLSEXT_hash_sha1)
1027#endif
1028};
1029#ifndef OPENSSL_NO_ECDSA
1030static unsigned char suiteb_sigalgs[] = {
1031 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
1032 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
1033};
1034#endif
1035size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
1036 {
1037 /* If Suite B mode use Suite B sigalgs only, ignore any other
1038 * preferences.
1039 */
1040#ifndef OPENSSL_NO_EC
1041 switch (tls1_suiteb(s))
1042 {
1043 case SSL_CERT_FLAG_SUITEB_128_LOS:
1044 *psigs = suiteb_sigalgs;
1045 return sizeof(suiteb_sigalgs);
1046
1047 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1048 *psigs = suiteb_sigalgs;
1049 return 2;
1050
1051 case SSL_CERT_FLAG_SUITEB_192_LOS:
1052 *psigs = suiteb_sigalgs + 2;
1053 return 2;
1054 }
1055#endif
1056 /* If server use client authentication sigalgs if not NULL */
1057 if (s->server && s->cert->client_sigalgs)
1058 {
1059 *psigs = s->cert->client_sigalgs;
1060 return s->cert->client_sigalgslen;
1061 }
1062 else if (s->cert->conf_sigalgs)
1063 {
1064 *psigs = s->cert->conf_sigalgs;
1065 return s->cert->conf_sigalgslen;
1066 }
1067 else
1068 {
1069 *psigs = tls12_sigalgs;
1070 return sizeof(tls12_sigalgs);
1071 }
1072 }
1073/* Check signature algorithm is consistent with sent supported signature
1074 * algorithms and if so return relevant digest.
1075 */
1076int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
1077 const unsigned char *sig, EVP_PKEY *pkey)
1078 {
1079 const unsigned char *sent_sigs;
1080 size_t sent_sigslen, i;
1081 int sigalg = tls12_get_sigid(pkey);
1082 /* Should never happen */
1083 if (sigalg == -1)
1084 return -1;
1085 /* Check key type is consistent with signature */
1086 if (sigalg != (int)sig[1])
1087 {
1088 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
1089 return 0;
1090 }
1091#ifndef OPENSSL_NO_EC
1092 if (pkey->type == EVP_PKEY_EC)
1093 {
1094 unsigned char curve_id[2], comp_id;
1095 /* Check compression and curve matches extensions */
1096 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
1097 return 0;
1098 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
1099 {
1100 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE);
1101 return 0;
1102 }
1103 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
1104 if (tls1_suiteb(s))
1105 {
1106 if (curve_id[0])
1107 return 0;
1108 if (curve_id[1] == TLSEXT_curve_P_256)
1109 {
1110 if (sig[0] != TLSEXT_hash_sha256)
1111 {
1112 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_ILLEGAL_SUITEB_DIGEST);
1113 return 0;
1114 }
1115 }
1116 else if (curve_id[1] == TLSEXT_curve_P_384)
1117 {
1118 if (sig[0] != TLSEXT_hash_sha384)
1119 {
1120 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_ILLEGAL_SUITEB_DIGEST);
1121 return 0;
1122 }
1123 }
1124 else
1125 return 0;
1126 }
1127 }
1128 else if (tls1_suiteb(s))
1129 return 0;
1130#endif
1131
1132 /* Check signature matches a type we sent */
1133 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1134 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
1135 {
1136 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1137 break;
1138 }
1139 /* Allow fallback to SHA1 if not strict mode */
1140 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
1141 {
1142 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE);
1143 return 0;
1144 }
1145 *pmd = tls12_get_hash(sig[0]);
1146 if (*pmd == NULL)
1147 {
1148 OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST);
1149 return 0;
1150 }
1151 /* Store the digest used so applications can retrieve it if they
1152 * wish.
1153 */
1154 if (s->session && s->session->sess_cert)
1155 s->session->sess_cert->peer_key->digest = *pmd;
1156 return 1;
1157 }
1158/* Get a mask of disabled algorithms: an algorithm is disabled
1159 * if it isn't supported or doesn't appear in supported signature
1160 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1161 * session and not global settings.
1162 *
1163 */
1164void ssl_set_client_disabled(SSL *s)
1165 {
1166 CERT *c = s->cert;
1167 const unsigned char *sigalgs;
1168 size_t i, sigalgslen;
1169 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1170 c->mask_a = 0;
1171 c->mask_k = 0;
1172 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1173 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1174 c->mask_ssl = SSL_TLSV1_2;
1175 else
1176 c->mask_ssl = 0;
1177 /* Now go through all signature algorithms seeing if we support
1178 * any for RSA, DSA, ECDSA. Do this for all versions not just
1179 * TLS 1.2.
1180 */
1181 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
1182 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
1183 {
1184 switch(sigalgs[1])
1185 {
1186#ifndef OPENSSL_NO_RSA
1187 case TLSEXT_signature_rsa:
1188 have_rsa = 1;
1189 break;
1190#endif
1191#ifndef OPENSSL_NO_DSA
1192 case TLSEXT_signature_dsa:
1193 have_dsa = 1;
1194 break;
1195#endif
1196#ifndef OPENSSL_NO_ECDSA
1197 case TLSEXT_signature_ecdsa:
1198 have_ecdsa = 1;
1199 break;
1200#endif
1201 }
1202 }
1203 /* Disable auth and static DH if we don't include any appropriate
1204 * signature algorithms.
1205 */
1206 if (!have_rsa)
1207 {
1208 c->mask_a |= SSL_aRSA;
1209 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1210 }
1211 if (!have_dsa)
1212 {
1213 c->mask_a |= SSL_aDSS;
1214 c->mask_k |= SSL_kDHd;
1215 }
1216 if (!have_ecdsa)
1217 {
1218 c->mask_a |= SSL_aECDSA;
1219 c->mask_k |= SSL_kECDHe;
1220 }
1221#ifndef OPENSSL_NO_PSK
1222 /* with PSK there must be client callback set */
1223 if (!s->psk_client_callback)
1224 {
1225 c->mask_a |= SSL_aPSK;
1226 c->mask_k |= SSL_kPSK;
1227 }
1228#endif /* OPENSSL_NO_PSK */
1229 c->valid = 1;
1230 }
1231
1232/* byte_compare is a compare function for qsort(3) that compares bytes. */
1233static int byte_compare(const void *in_a, const void *in_b)
1234 {
1235 unsigned char a = *((const unsigned char*) in_a);
1236 unsigned char b = *((const unsigned char*) in_b);
1237
1238 if (a > b)
1239 return 1;
1240 else if (a < b)
1241 return -1;
1242 return 0;
1243}
1244
Adam Langleyb0c235e2014-06-20 12:00:00 -07001245/* header_len is the length of the ClientHello header written so far, used to
1246 * compute padding. It does not include the record header. Pass 0 if no padding
1247 * is to be done. */
1248unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len)
Adam Langley95c29f32014-06-20 12:00:00 -07001249 {
1250 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001251 unsigned char *ret = buf;
1252 unsigned char *orig = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001253#ifndef OPENSSL_NO_EC
1254 /* See if we support any ECC ciphersuites */
1255 int using_ecc = 0;
1256 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1257 {
1258 int i;
1259 unsigned long alg_k, alg_a;
1260 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1261
1262 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1263 {
1264 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1265
1266 alg_k = c->algorithm_mkey;
1267 alg_a = c->algorithm_auth;
1268 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
1269 || (alg_a & SSL_aECDSA)))
1270 {
1271 using_ecc = 1;
1272 break;
1273 }
1274 }
1275 }
1276#endif
1277
1278 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1279 if (s->client_version == SSL3_VERSION
1280 && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001281 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001282
1283 ret+=2;
1284
1285 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1286
1287 if (s->tlsext_hostname != NULL)
1288 {
1289 /* Add TLS extension servername to the Client Hello message */
1290 unsigned long size_str;
1291 long lenmax;
1292
1293 /* check for enough space.
1294 4 for the servername type and entension length
1295 2 for servernamelist length
1296 1 for the hostname type
1297 2 for hostname length
1298 + hostname length
1299 */
1300
1301 if ((lenmax = limit - ret - 9) < 0
1302 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1303 return NULL;
1304
1305 /* extension type and length */
1306 s2n(TLSEXT_TYPE_server_name,ret);
1307 s2n(size_str+5,ret);
1308
1309 /* length of servername list */
1310 s2n(size_str+3,ret);
1311
1312 /* hostname type, length and hostname */
1313 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1314 s2n(size_str,ret);
1315 memcpy(ret, s->tlsext_hostname, size_str);
1316 ret+=size_str;
1317 }
1318
1319 /* Add RI if renegotiating */
1320 if (s->renegotiate)
1321 {
1322 int el;
1323
1324 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1325 {
1326 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1327 return NULL;
1328 }
1329
Adam Langleyb0c235e2014-06-20 12:00:00 -07001330 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001331
1332 s2n(TLSEXT_TYPE_renegotiate,ret);
1333 s2n(el,ret);
1334
1335 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1336 {
1337 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1338 return NULL;
1339 }
1340
1341 ret += el;
1342 }
1343
Adam Langley95c29f32014-06-20 12:00:00 -07001344 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1345 {
1346 int ticklen;
1347 if (!s->new_session && s->session && s->session->tlsext_tick)
1348 ticklen = s->session->tlsext_ticklen;
1349 else if (s->session && s->tlsext_session_ticket &&
1350 s->tlsext_session_ticket->data)
1351 {
1352 ticklen = s->tlsext_session_ticket->length;
1353 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1354 if (!s->session->tlsext_tick)
1355 return NULL;
1356 memcpy(s->session->tlsext_tick,
1357 s->tlsext_session_ticket->data,
1358 ticklen);
1359 s->session->tlsext_ticklen = ticklen;
1360 }
1361 else
1362 ticklen = 0;
1363 if (ticklen == 0 && s->tlsext_session_ticket &&
1364 s->tlsext_session_ticket->data == NULL)
1365 goto skip_ext;
1366 /* Check for enough room 2 for extension type, 2 for len
1367 * rest for ticket
1368 */
1369 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1370 s2n(TLSEXT_TYPE_session_ticket,ret);
1371 s2n(ticklen,ret);
1372 if (ticklen)
1373 {
1374 memcpy(ret, s->session->tlsext_tick, ticklen);
1375 ret += ticklen;
1376 }
1377 }
1378 skip_ext:
1379
1380 if (SSL_USE_SIGALGS(s))
1381 {
1382 size_t salglen;
1383 const unsigned char *salg;
1384 salglen = tls12_get_psigalgs(s, &salg);
1385 if ((size_t)(limit - ret) < salglen + 6)
1386 return NULL;
1387 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1388 s2n(salglen + 2, ret);
1389 s2n(salglen, ret);
1390 memcpy(ret, salg, salglen);
1391 ret += salglen;
1392 }
1393
1394#ifdef TLSEXT_TYPE_opaque_prf_input
1395 if (s->s3->client_opaque_prf_input != NULL)
1396 {
1397 size_t col = s->s3->client_opaque_prf_input_len;
1398
1399 if ((long)(limit - ret - 6 - col < 0))
1400 return NULL;
1401 if (col > 0xFFFD) /* can't happen */
1402 return NULL;
1403
1404 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1405 s2n(col + 2, ret);
1406 s2n(col, ret);
1407 memcpy(ret, s->s3->client_opaque_prf_input, col);
1408 ret += col;
1409 }
1410#endif
1411
1412 /* TODO(fork): we probably want OCSP stapling, but it currently pulls in a lot of code. */
1413#if 0
1414 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1415 {
1416 int i;
1417 long extlen, idlen, itmp;
1418 OCSP_RESPID *id;
1419
1420 idlen = 0;
1421 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1422 {
1423 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1424 itmp = i2d_OCSP_RESPID(id, NULL);
1425 if (itmp <= 0)
1426 return NULL;
1427 idlen += itmp + 2;
1428 }
1429
1430 if (s->tlsext_ocsp_exts)
1431 {
1432 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1433 if (extlen < 0)
1434 return NULL;
1435 }
1436 else
1437 extlen = 0;
1438
1439 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1440 s2n(TLSEXT_TYPE_status_request, ret);
1441 if (extlen + idlen > 0xFFF0)
1442 return NULL;
1443 s2n(extlen + idlen + 5, ret);
1444 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1445 s2n(idlen, ret);
1446 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1447 {
1448 /* save position of id len */
1449 unsigned char *q = ret;
1450 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1451 /* skip over id len */
1452 ret += 2;
1453 itmp = i2d_OCSP_RESPID(id, &ret);
1454 /* write id len */
1455 s2n(itmp, q);
1456 }
1457 s2n(extlen, ret);
1458 if (extlen > 0)
1459 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1460 }
1461#endif
1462
1463#ifndef OPENSSL_NO_HEARTBEATS
1464 /* Add Heartbeat extension */
Adam Langleyb7bf53c2014-06-20 12:00:00 -07001465 if ((limit - ret - 4 - 1) < 0)
1466 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001467 s2n(TLSEXT_TYPE_heartbeat,ret);
1468 s2n(1,ret);
1469 /* Set mode:
1470 * 1: peer may send requests
1471 * 2: peer not allowed to send requests
1472 */
1473 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1474 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1475 else
1476 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1477#endif
1478
1479#ifndef OPENSSL_NO_NEXTPROTONEG
1480 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1481 {
1482 /* The client advertises an emtpy extension to indicate its
1483 * support for Next Protocol Negotiation */
1484 if (limit - ret - 4 < 0)
1485 return NULL;
1486 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1487 s2n(0,ret);
1488 }
1489#endif
1490
1491 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1492 {
1493 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1494 return NULL;
1495 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1496 s2n(2 + s->alpn_client_proto_list_len,ret);
1497 s2n(s->alpn_client_proto_list_len,ret);
1498 memcpy(ret, s->alpn_client_proto_list,
1499 s->alpn_client_proto_list_len);
1500 ret += s->alpn_client_proto_list_len;
1501 }
1502
Adam Langley1258b6a2014-06-20 12:00:00 -07001503 if (s->tlsext_channel_id_enabled)
1504 {
1505 /* The client advertises an emtpy extension to indicate its
1506 * support for Channel ID. */
1507 if (limit - ret - 4 < 0)
1508 return NULL;
1509 if (s->ctx->tlsext_channel_id_enabled_new)
1510 s2n(TLSEXT_TYPE_channel_id_new,ret);
1511 else
1512 s2n(TLSEXT_TYPE_channel_id,ret);
1513 s2n(0,ret);
1514 }
1515
Adam Langley95c29f32014-06-20 12:00:00 -07001516 if(SSL_get_srtp_profiles(s))
1517 {
1518 int el;
1519
1520 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1521
Adam Langleyb0c235e2014-06-20 12:00:00 -07001522 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001523
1524 s2n(TLSEXT_TYPE_use_srtp,ret);
1525 s2n(el,ret);
1526
1527 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1528 {
1529 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1530 return NULL;
1531 }
1532 ret += el;
1533 }
1534
1535 /* Add TLS extension Server_Authz_DataFormats to the ClientHello */
1536 /* 2 bytes for extension type */
1537 /* 2 bytes for extension length */
1538 /* 1 byte for the list length */
1539 /* 1 byte for the list (we only support audit proofs) */
1540 if (s->ctx->tlsext_authz_server_audit_proof_cb != NULL)
1541 {
1542 const unsigned short ext_len = 2;
1543 const unsigned char list_len = 1;
1544
1545 if (limit < ret + 6)
1546 return NULL;
1547
1548 s2n(TLSEXT_TYPE_server_authz, ret);
1549 /* Extension length: 2 bytes */
1550 s2n(ext_len, ret);
1551 *(ret++) = list_len;
1552 *(ret++) = TLSEXT_AUTHZDATAFORMAT_audit_proof;
1553 }
1554
1555 /* Add custom TLS Extensions to ClientHello */
1556 if (s->ctx->custom_cli_ext_records_count)
1557 {
1558 size_t i;
1559 custom_cli_ext_record* record;
1560
1561 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
1562 {
1563 const unsigned char* out = NULL;
1564 unsigned short outlen = 0;
1565
1566 record = &s->ctx->custom_cli_ext_records[i];
1567 /* NULL callback sends empty extension */
1568 /* -1 from callback omits extension */
1569 if (record->fn1)
1570 {
1571 int cb_retval = 0;
1572 cb_retval = record->fn1(s, record->ext_type,
1573 &out, &outlen,
1574 record->arg);
1575 if (cb_retval == 0)
1576 return NULL; /* error */
1577 if (cb_retval == -1)
1578 continue; /* skip this extension */
1579 }
1580 if (limit < ret + 4 + outlen)
1581 return NULL;
1582 s2n(record->ext_type, ret);
1583 s2n(outlen, ret);
1584 memcpy(ret, out, outlen);
1585 ret += outlen;
1586 }
1587 }
1588
Adam Langleyc3174b72014-06-20 12:00:00 -07001589#ifndef OPENSSL_NO_EC
1590 if (using_ecc)
1591 {
1592 /* Add TLS extension ECPointFormats to the ClientHello message */
1593 long lenmax;
1594 const unsigned char *plist;
1595 size_t plistlen;
1596
1597 tls1_get_formatlist(s, &plist, &plistlen);
1598
1599 if ((lenmax = limit - ret - 5) < 0) return NULL;
1600 if (plistlen > (size_t)lenmax) return NULL;
1601 if (plistlen > 255)
1602 {
1603 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1604 return NULL;
1605 }
1606
1607 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1608 s2n(plistlen + 1,ret);
1609 *(ret++) = (unsigned char)plistlen ;
1610 memcpy(ret, plist, plistlen);
1611 ret+=plistlen;
1612
1613 /* Add TLS extension EllipticCurves to the ClientHello message */
1614 plist = s->tlsext_ellipticcurvelist;
1615 tls1_get_curvelist(s, 0, &plist, &plistlen);
1616
1617 if ((lenmax = limit - ret - 6) < 0) return NULL;
1618 if (plistlen > (size_t)lenmax) return NULL;
1619 if (plistlen > 65532)
1620 {
1621 OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
1622 return NULL;
1623 }
1624
1625 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1626 s2n(plistlen + 2, ret);
1627
1628 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1629 * elliptic_curve_list, but the examples use two bytes.
1630 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1631 * resolves this to two bytes.
1632 */
1633 s2n(plistlen, ret);
1634 memcpy(ret, plist, plistlen);
1635 ret+=plistlen;
1636 }
1637#endif /* OPENSSL_NO_EC */
1638
Adam Langley95c29f32014-06-20 12:00:00 -07001639#ifdef TLSEXT_TYPE_padding
1640 /* Add padding to workaround bugs in F5 terminators.
Adam Langleyb0c235e2014-06-20 12:00:00 -07001641 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
Adam Langley95c29f32014-06-20 12:00:00 -07001642 *
1643 * NB: because this code works out the length of all existing
Adam Langleyb0c235e2014-06-20 12:00:00 -07001644 * extensions it MUST always appear last. */
1645 if (header_len > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001646 {
Adam Langleyb0c235e2014-06-20 12:00:00 -07001647 header_len += ret - orig;
1648 if (header_len > 0xff && header_len < 0x200)
1649 {
1650 size_t padding_len = 0x200 - header_len;
Adam Langleyc3174b72014-06-20 12:00:00 -07001651 /* Extensions take at least four bytes to encode. Always
1652 * include least one byte of data if including the
1653 * extension. WebSphere Application Server 7.0 is
1654 * intolerant to the last extension being zero-length. */
1655 if (padding_len >= 4 + 1)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001656 padding_len -= 4;
1657 else
Adam Langleyc3174b72014-06-20 12:00:00 -07001658 padding_len = 1;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001659 if (limit - ret - 4 - (long)padding_len < 0)
1660 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001661
Adam Langleyb0c235e2014-06-20 12:00:00 -07001662 s2n(TLSEXT_TYPE_padding, ret);
1663 s2n(padding_len, ret);
1664 memset(ret, 0, padding_len);
1665 ret += padding_len;
1666 }
Adam Langley95c29f32014-06-20 12:00:00 -07001667 }
1668#endif
1669
Adam Langleyb0c235e2014-06-20 12:00:00 -07001670 if ((extdatalen = ret-orig-2)== 0)
1671 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001672
Adam Langleyb0c235e2014-06-20 12:00:00 -07001673 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001674 return ret;
1675 }
1676
Adam Langleyb0c235e2014-06-20 12:00:00 -07001677unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit)
Adam Langley95c29f32014-06-20 12:00:00 -07001678 {
1679 int extdatalen=0;
Adam Langleyb0c235e2014-06-20 12:00:00 -07001680 unsigned char *orig = buf;
1681 unsigned char *ret = buf;
Adam Langley95c29f32014-06-20 12:00:00 -07001682#ifndef OPENSSL_NO_NEXTPROTONEG
1683 int next_proto_neg_seen;
1684#endif
1685#ifndef OPENSSL_NO_EC
1686 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1687 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1688 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1689 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1690#endif
1691 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1692 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
Adam Langleyb0c235e2014-06-20 12:00:00 -07001693 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001694
1695 ret+=2;
1696 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1697
1698 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1699 {
1700 if ((long)(limit - ret - 4) < 0) return NULL;
1701
1702 s2n(TLSEXT_TYPE_server_name,ret);
1703 s2n(0,ret);
1704 }
1705
1706 if(s->s3->send_connection_binding)
1707 {
1708 int el;
1709
1710 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1711 {
1712 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1713 return NULL;
1714 }
1715
Adam Langleyb0c235e2014-06-20 12:00:00 -07001716 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001717
1718 s2n(TLSEXT_TYPE_renegotiate,ret);
1719 s2n(el,ret);
1720
1721 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1722 {
1723 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1724 return NULL;
1725 }
1726
1727 ret += el;
1728 }
1729
1730#ifndef OPENSSL_NO_EC
1731 if (using_ecc)
1732 {
1733 const unsigned char *plist;
1734 size_t plistlen;
1735 /* Add TLS extension ECPointFormats to the ServerHello message */
1736 long lenmax;
1737
1738 tls1_get_formatlist(s, &plist, &plistlen);
1739
1740 if ((lenmax = limit - ret - 5) < 0) return NULL;
1741 if (plistlen > (size_t)lenmax) return NULL;
1742 if (plistlen > 255)
1743 {
1744 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1745 return NULL;
1746 }
1747
1748 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1749 s2n(plistlen + 1,ret);
1750 *(ret++) = (unsigned char) plistlen;
1751 memcpy(ret, plist, plistlen);
1752 ret+=plistlen;
1753
1754 }
1755 /* Currently the server should not respond with a SupportedCurves extension */
1756#endif /* OPENSSL_NO_EC */
1757
1758 if (s->tlsext_ticket_expected
1759 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1760 {
1761 if ((long)(limit - ret - 4) < 0) return NULL;
1762 s2n(TLSEXT_TYPE_session_ticket,ret);
1763 s2n(0,ret);
1764 }
1765
1766 if (s->tlsext_status_expected)
1767 {
1768 if ((long)(limit - ret - 4) < 0) return NULL;
1769 s2n(TLSEXT_TYPE_status_request,ret);
1770 s2n(0,ret);
1771 }
1772
1773#ifdef TLSEXT_TYPE_opaque_prf_input
1774 if (s->s3->server_opaque_prf_input != NULL)
1775 {
1776 size_t sol = s->s3->server_opaque_prf_input_len;
1777
1778 if ((long)(limit - ret - 6 - sol) < 0)
1779 return NULL;
1780 if (sol > 0xFFFD) /* can't happen */
1781 return NULL;
1782
1783 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1784 s2n(sol + 2, ret);
1785 s2n(sol, ret);
1786 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1787 ret += sol;
1788 }
1789#endif
1790
1791 if(s->srtp_profile)
1792 {
1793 int el;
1794
1795 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1796
Adam Langleyb0c235e2014-06-20 12:00:00 -07001797 if((limit - ret - 4 - el) < 0) return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001798
1799 s2n(TLSEXT_TYPE_use_srtp,ret);
1800 s2n(el,ret);
1801
1802 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1803 {
1804 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
1805 return NULL;
1806 }
1807 ret+=el;
1808 }
1809
Adam Langley95c29f32014-06-20 12:00:00 -07001810#ifndef OPENSSL_NO_HEARTBEATS
1811 /* Add Heartbeat extension if we've received one */
1812 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1813 {
Adam Langleyb7bf53c2014-06-20 12:00:00 -07001814 if ((limit - ret - 4 - 1) < 0)
1815 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001816 s2n(TLSEXT_TYPE_heartbeat,ret);
1817 s2n(1,ret);
1818 /* Set mode:
1819 * 1: peer may send requests
1820 * 2: peer not allowed to send requests
1821 */
1822 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1823 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1824 else
1825 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1826
1827 }
1828#endif
1829
1830#ifndef OPENSSL_NO_NEXTPROTONEG
1831 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1832 s->s3->next_proto_neg_seen = 0;
1833 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1834 {
1835 const unsigned char *npa;
1836 unsigned int npalen;
1837 int r;
1838
1839 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1840 if (r == SSL_TLSEXT_ERR_OK)
1841 {
1842 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1843 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1844 s2n(npalen,ret);
1845 memcpy(ret, npa, npalen);
1846 ret += npalen;
1847 s->s3->next_proto_neg_seen = 1;
1848 }
1849 }
1850#endif
1851
1852 /* If the client supports authz then see whether we have any to offer
1853 * to it. */
1854 if (s->s3->tlsext_authz_client_types_len)
1855 {
1856 size_t authz_length;
1857 /* By now we already know the new cipher, so we can look ahead
1858 * to see whether the cert we are going to send
1859 * has any authz data attached to it. */
1860 const unsigned char* authz = ssl_get_authz_data(s, &authz_length);
1861 const unsigned char* const orig_authz = authz;
1862 size_t i;
1863 unsigned authz_count = 0;
1864
1865 /* The authz data contains a number of the following structures:
1866 * uint8_t authz_type
1867 * uint16_t length
1868 * uint8_t data[length]
1869 *
1870 * First we walk over it to find the number of authz elements. */
1871 for (i = 0; i < authz_length; i++)
1872 {
1873 unsigned short length;
1874 unsigned char type;
1875
1876 type = *(authz++);
1877 if (memchr(s->s3->tlsext_authz_client_types,
1878 type,
1879 s->s3->tlsext_authz_client_types_len) != NULL)
1880 authz_count++;
1881
1882 n2s(authz, length);
1883 /* n2s increments authz by 2 */
1884 i += 2;
1885 authz += length;
1886 i += length;
1887 }
1888
1889 if (authz_count)
1890 {
1891 /* Add TLS extension server_authz to the ServerHello message
1892 * 2 bytes for extension type
1893 * 2 bytes for extension length
1894 * 1 byte for the list length
1895 * n bytes for the list */
1896 const unsigned short ext_len = 1 + authz_count;
1897
1898 if ((long)(limit - ret - 4 - ext_len) < 0) return NULL;
1899 s2n(TLSEXT_TYPE_server_authz, ret);
1900 s2n(ext_len, ret);
1901 *(ret++) = authz_count;
1902 s->s3->tlsext_authz_promised_to_client = 1;
1903 }
1904
1905 authz = orig_authz;
1906 for (i = 0; i < authz_length; i++)
1907 {
1908 unsigned short length;
1909 unsigned char type;
1910
1911 authz_count++;
1912 type = *(authz++);
1913 if (memchr(s->s3->tlsext_authz_client_types,
1914 type,
1915 s->s3->tlsext_authz_client_types_len) != NULL)
1916 *(ret++) = type;
1917 n2s(authz, length);
1918 /* n2s increments authz by 2 */
1919 i += 2;
1920 authz += length;
1921 i += length;
1922 }
1923 }
1924
1925 /* If custom types were sent in ClientHello, add ServerHello responses */
1926 if (s->s3->tlsext_custom_types_count)
1927 {
1928 size_t i;
1929
1930 for (i = 0; i < s->s3->tlsext_custom_types_count; i++)
1931 {
1932 size_t j;
1933 custom_srv_ext_record *record;
1934
1935 for (j = 0; j < s->ctx->custom_srv_ext_records_count; j++)
1936 {
1937 record = &s->ctx->custom_srv_ext_records[j];
1938 if (s->s3->tlsext_custom_types[i] == record->ext_type)
1939 {
1940 const unsigned char *out = NULL;
1941 unsigned short outlen = 0;
1942 int cb_retval = 0;
1943
1944 /* NULL callback or -1 omits extension */
1945 if (!record->fn2)
1946 break;
1947 cb_retval = record->fn2(s, record->ext_type,
1948 &out, &outlen,
1949 record->arg);
1950 if (cb_retval == 0)
1951 return NULL; /* error */
1952 if (cb_retval == -1)
1953 break; /* skip this extension */
1954 if (limit < ret + 4 + outlen)
1955 return NULL;
1956 s2n(record->ext_type, ret);
1957 s2n(outlen, ret);
1958 memcpy(ret, out, outlen);
1959 ret += outlen;
1960 break;
1961 }
1962 }
1963 }
1964 }
1965
1966 if (s->s3->alpn_selected)
1967 {
David Benjamin03973092014-06-24 23:27:17 -04001968 const uint8_t *selected = s->s3->alpn_selected;
1969 size_t len = s->s3->alpn_selected_len;
Adam Langley95c29f32014-06-20 12:00:00 -07001970
1971 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1972 return NULL;
1973 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1974 s2n(3 + len,ret);
1975 s2n(1 + len,ret);
1976 *ret++ = len;
1977 memcpy(ret, selected, len);
1978 ret += len;
1979 }
1980
Adam Langley1258b6a2014-06-20 12:00:00 -07001981 /* If the client advertised support for Channel ID, and we have it
1982 * enabled, then we want to echo it back. */
1983 if (s->s3->tlsext_channel_id_valid)
1984 {
1985 if (limit - ret - 4 < 0)
1986 return NULL;
1987 if (s->s3->tlsext_channel_id_new)
1988 s2n(TLSEXT_TYPE_channel_id_new,ret);
1989 else
1990 s2n(TLSEXT_TYPE_channel_id,ret);
1991 s2n(0,ret);
1992 }
1993
Adam Langleyb0c235e2014-06-20 12:00:00 -07001994 if ((extdatalen = ret-orig-2) == 0)
1995 return orig;
Adam Langley95c29f32014-06-20 12:00:00 -07001996
Adam Langleyb0c235e2014-06-20 12:00:00 -07001997 s2n(extdatalen, orig);
Adam Langley95c29f32014-06-20 12:00:00 -07001998 return ret;
1999 }
2000
2001#ifndef OPENSSL_NO_EC
2002/* ssl_check_for_safari attempts to fingerprint Safari using OS X
2003 * SecureTransport using the TLS extension block in |d|, of length |n|.
2004 * Safari, since 10.6, sends exactly these extensions, in this order:
2005 * SNI,
2006 * elliptic_curves
2007 * ec_point_formats
2008 *
2009 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
2010 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
2011 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
2012 * 10.8..10.8.3 (which don't work).
2013 */
2014static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
2015 unsigned short type, size;
2016 static const unsigned char kSafariExtensionsBlock[] = {
2017 0x00, 0x0a, /* elliptic_curves extension */
2018 0x00, 0x08, /* 8 bytes */
2019 0x00, 0x06, /* 6 bytes of curve ids */
2020 0x00, 0x17, /* P-256 */
2021 0x00, 0x18, /* P-384 */
2022 0x00, 0x19, /* P-521 */
2023
2024 0x00, 0x0b, /* ec_point_formats */
2025 0x00, 0x02, /* 2 bytes */
2026 0x01, /* 1 point format */
2027 0x00, /* uncompressed */
2028 };
2029
2030 /* The following is only present in TLS 1.2 */
2031 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
2032 0x00, 0x0d, /* signature_algorithms */
2033 0x00, 0x0c, /* 12 bytes */
2034 0x00, 0x0a, /* 10 bytes */
2035 0x05, 0x01, /* SHA-384/RSA */
2036 0x04, 0x01, /* SHA-256/RSA */
2037 0x02, 0x01, /* SHA-1/RSA */
2038 0x04, 0x03, /* SHA-256/ECDSA */
2039 0x02, 0x03, /* SHA-1/ECDSA */
2040 };
2041
2042 if (data >= (d+n-2))
2043 return;
2044 data += 2;
2045
2046 if (data > (d+n-4))
2047 return;
2048 n2s(data,type);
2049 n2s(data,size);
2050
2051 if (type != TLSEXT_TYPE_server_name)
2052 return;
2053
2054 if (data+size > d+n)
2055 return;
2056 data += size;
2057
2058 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
2059 {
2060 const size_t len1 = sizeof(kSafariExtensionsBlock);
2061 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
2062
2063 if (data + len1 + len2 != d+n)
2064 return;
2065 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
2066 return;
2067 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
2068 return;
2069 }
2070 else
2071 {
2072 const size_t len = sizeof(kSafariExtensionsBlock);
2073
2074 if (data + len != d+n)
2075 return;
2076 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
2077 return;
2078 }
2079
2080 s->s3->is_probably_safari = 1;
2081}
2082#endif /* !OPENSSL_NO_EC */
2083
2084/* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
2085 * ClientHello.
2086 * data: the contents of the extension, not including the type and length.
2087 * data_len: the number of bytes in |data|
2088 * al: a pointer to the alert value to send in the event of a non-zero
2089 * return.
2090 *
2091 * returns: 0 on success. */
2092static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
2093 unsigned data_len, int *al)
2094 {
2095 unsigned i;
2096 unsigned proto_len;
2097 const unsigned char *selected;
2098 unsigned char selected_len;
2099 int r;
2100
2101 if (s->ctx->alpn_select_cb == NULL)
2102 return 0;
2103
2104 if (data_len < 2)
2105 goto parse_error;
2106
2107 /* data should contain a uint16 length followed by a series of 8-bit,
2108 * length-prefixed strings. */
2109 i = ((unsigned) data[0]) << 8 |
2110 ((unsigned) data[1]);
2111 data_len -= 2;
2112 data += 2;
2113 if (data_len != i)
2114 goto parse_error;
2115
2116 if (data_len < 2)
2117 goto parse_error;
2118
2119 for (i = 0; i < data_len;)
2120 {
2121 proto_len = data[i];
2122 i++;
2123
2124 if (proto_len == 0)
2125 goto parse_error;
2126
2127 if (i + proto_len < i || i + proto_len > data_len)
2128 goto parse_error;
2129
2130 i += proto_len;
2131 }
2132
2133 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
2134 s->ctx->alpn_select_cb_arg);
2135 if (r == SSL_TLSEXT_ERR_OK) {
2136 if (s->s3->alpn_selected)
2137 OPENSSL_free(s->s3->alpn_selected);
2138 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
2139 if (!s->s3->alpn_selected)
2140 {
2141 *al = SSL_AD_INTERNAL_ERROR;
2142 return -1;
2143 }
2144 memcpy(s->s3->alpn_selected, selected, selected_len);
2145 s->s3->alpn_selected_len = selected_len;
2146 }
2147 return 0;
2148
2149parse_error:
2150 *al = SSL_AD_DECODE_ERROR;
2151 return -1;
2152 }
2153
2154static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2155 {
2156 unsigned short type;
2157 unsigned short size;
2158 unsigned short len;
2159 unsigned char *data = *p;
2160 int renegotiate_seen = 0;
2161 size_t i;
2162
2163 s->servername_done = 0;
2164 s->tlsext_status_type = -1;
2165#ifndef OPENSSL_NO_NEXTPROTONEG
2166 s->s3->next_proto_neg_seen = 0;
2167#endif
2168
2169 /* Clear observed custom extensions */
2170 s->s3->tlsext_custom_types_count = 0;
2171 if (s->s3->tlsext_custom_types != NULL)
2172 {
2173 OPENSSL_free(s->s3->tlsext_custom_types);
2174 s->s3->tlsext_custom_types = NULL;
2175 }
2176
2177 if (s->s3->alpn_selected)
2178 {
2179 OPENSSL_free(s->s3->alpn_selected);
2180 s->s3->alpn_selected = NULL;
2181 }
2182
2183#ifndef OPENSSL_NO_HEARTBEATS
2184 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2185 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2186#endif
2187
2188#ifndef OPENSSL_NO_EC
2189 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
2190 ssl_check_for_safari(s, data, d, n);
2191#endif /* !OPENSSL_NO_EC */
2192
2193 /* Clear any signature algorithms extension received */
2194 if (s->cert->peer_sigalgs)
2195 {
2196 OPENSSL_free(s->cert->peer_sigalgs);
2197 s->cert->peer_sigalgs = NULL;
2198 }
2199 /* Clear any shared sigtnature algorithms */
2200 if (s->cert->shared_sigalgs)
2201 {
2202 OPENSSL_free(s->cert->shared_sigalgs);
2203 s->cert->shared_sigalgs = NULL;
2204 }
2205 /* Clear certificate digests and validity flags */
2206 for (i = 0; i < SSL_PKEY_NUM; i++)
2207 {
2208 s->cert->pkeys[i].digest = NULL;
2209 s->cert->pkeys[i].valid_flags = 0;
2210 }
2211
2212 if (data >= (d+n-2))
2213 goto ri_check;
2214 n2s(data,len);
2215
2216 if (data > (d+n-len))
2217 goto ri_check;
2218
2219 while (data <= (d+n-4))
2220 {
2221 n2s(data,type);
2222 n2s(data,size);
2223
2224 if (data+size > (d+n))
2225 goto ri_check;
2226#if 0
2227 fprintf(stderr,"Received extension type %d size %d\n",type,size);
2228#endif
2229 if (s->tlsext_debug_cb)
2230 s->tlsext_debug_cb(s, 0, type, data, size,
2231 s->tlsext_debug_arg);
2232/* The servername extension is treated as follows:
2233
2234 - Only the hostname type is supported with a maximum length of 255.
2235 - The servername is rejected if too long or if it contains zeros,
2236 in which case an fatal alert is generated.
2237 - The servername field is maintained together with the session cache.
2238 - When a session is resumed, the servername call back invoked in order
2239 to allow the application to position itself to the right context.
2240 - The servername is acknowledged if it is new for a session or when
2241 it is identical to a previously used for the same session.
2242 Applications can control the behaviour. They can at any time
2243 set a 'desirable' servername for a new SSL object. This can be the
2244 case for example with HTTPS when a Host: header field is received and
2245 a renegotiation is requested. In this case, a possible servername
2246 presented in the new client hello is only acknowledged if it matches
2247 the value of the Host: field.
2248 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2249 if they provide for changing an explicit servername context for the session,
2250 i.e. when the session has been established with a servername extension.
2251 - On session reconnect, the servername extension may be absent.
2252
2253*/
2254
2255 if (type == TLSEXT_TYPE_server_name)
2256 {
2257 unsigned char *sdata;
2258 int servname_type;
2259 int dsize;
2260
2261 if (size < 2)
2262 {
2263 *al = SSL_AD_DECODE_ERROR;
2264 return 0;
2265 }
2266 n2s(data,dsize);
2267 size -= 2;
2268 if (dsize > size )
2269 {
2270 *al = SSL_AD_DECODE_ERROR;
2271 return 0;
2272 }
2273
2274 sdata = data;
2275 while (dsize > 3)
2276 {
2277 servname_type = *(sdata++);
2278 n2s(sdata,len);
2279 dsize -= 3;
2280
2281 if (len > dsize)
2282 {
2283 *al = SSL_AD_DECODE_ERROR;
2284 return 0;
2285 }
2286 if (s->servername_done == 0)
2287 switch (servname_type)
2288 {
2289 case TLSEXT_NAMETYPE_host_name:
2290 if (!s->hit)
2291 {
2292 if(s->session->tlsext_hostname)
2293 {
2294 *al = SSL_AD_DECODE_ERROR;
2295 return 0;
2296 }
2297 if (len > TLSEXT_MAXLEN_host_name)
2298 {
2299 *al = TLS1_AD_UNRECOGNIZED_NAME;
2300 return 0;
2301 }
2302 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
2303 {
2304 *al = TLS1_AD_INTERNAL_ERROR;
2305 return 0;
2306 }
2307 memcpy(s->session->tlsext_hostname, sdata, len);
2308 s->session->tlsext_hostname[len]='\0';
2309 if (strlen(s->session->tlsext_hostname) != len) {
2310 OPENSSL_free(s->session->tlsext_hostname);
2311 s->session->tlsext_hostname = NULL;
2312 *al = TLS1_AD_UNRECOGNIZED_NAME;
2313 return 0;
2314 }
2315 s->servername_done = 1;
2316
2317 }
2318 else
2319 s->servername_done = s->session->tlsext_hostname
2320 && strlen(s->session->tlsext_hostname) == len
2321 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2322
2323 break;
2324
2325 default:
2326 break;
2327 }
2328
2329 dsize -= len;
2330 }
2331 if (dsize != 0)
2332 {
2333 *al = SSL_AD_DECODE_ERROR;
2334 return 0;
2335 }
2336
2337 }
2338
2339#ifndef OPENSSL_NO_EC
2340 else if (type == TLSEXT_TYPE_ec_point_formats)
2341 {
2342 unsigned char *sdata = data;
2343 int ecpointformatlist_length = *(sdata++);
2344
2345 if (ecpointformatlist_length != size - 1 ||
2346 ecpointformatlist_length < 1)
2347 {
2348 *al = TLS1_AD_DECODE_ERROR;
2349 return 0;
2350 }
2351 if (!s->hit)
2352 {
2353 if(s->session->tlsext_ecpointformatlist)
2354 {
2355 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2356 s->session->tlsext_ecpointformatlist = NULL;
2357 }
2358 s->session->tlsext_ecpointformatlist_length = 0;
2359 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2360 {
2361 *al = TLS1_AD_INTERNAL_ERROR;
2362 return 0;
2363 }
2364 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2365 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2366 }
2367#if 0
2368 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2369 sdata = s->session->tlsext_ecpointformatlist;
2370 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2371 fprintf(stderr,"%i ",*(sdata++));
2372 fprintf(stderr,"\n");
2373#endif
2374 }
2375 else if (type == TLSEXT_TYPE_elliptic_curves)
2376 {
2377 unsigned char *sdata = data;
2378 int ellipticcurvelist_length = (*(sdata++) << 8);
2379 ellipticcurvelist_length += (*(sdata++));
2380
2381 if (ellipticcurvelist_length != size - 2 ||
2382 ellipticcurvelist_length < 1)
2383 {
2384 *al = TLS1_AD_DECODE_ERROR;
2385 return 0;
2386 }
2387 if (!s->hit)
2388 {
2389 if(s->session->tlsext_ellipticcurvelist)
2390 {
2391 *al = TLS1_AD_DECODE_ERROR;
2392 return 0;
2393 }
2394 s->session->tlsext_ellipticcurvelist_length = 0;
2395 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2396 {
2397 *al = TLS1_AD_INTERNAL_ERROR;
2398 return 0;
2399 }
2400 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2401 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2402 }
2403#if 0
2404 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2405 sdata = s->session->tlsext_ellipticcurvelist;
2406 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2407 fprintf(stderr,"%i ",*(sdata++));
2408 fprintf(stderr,"\n");
2409#endif
2410 }
2411#endif /* OPENSSL_NO_EC */
2412#ifdef TLSEXT_TYPE_opaque_prf_input
2413 else if (type == TLSEXT_TYPE_opaque_prf_input)
2414 {
2415 unsigned char *sdata = data;
2416
2417 if (size < 2)
2418 {
2419 *al = SSL_AD_DECODE_ERROR;
2420 return 0;
2421 }
2422 n2s(sdata, s->s3->client_opaque_prf_input_len);
2423 if (s->s3->client_opaque_prf_input_len != size - 2)
2424 {
2425 *al = SSL_AD_DECODE_ERROR;
2426 return 0;
2427 }
2428
2429 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2430 OPENSSL_free(s->s3->client_opaque_prf_input);
2431 if (s->s3->client_opaque_prf_input_len == 0)
2432 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2433 else
2434 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2435 if (s->s3->client_opaque_prf_input == NULL)
2436 {
2437 *al = TLS1_AD_INTERNAL_ERROR;
2438 return 0;
2439 }
2440 }
2441#endif
2442 else if (type == TLSEXT_TYPE_session_ticket)
2443 {
2444 if (s->tls_session_ticket_ext_cb &&
2445 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2446 {
2447 *al = TLS1_AD_INTERNAL_ERROR;
2448 return 0;
2449 }
2450 }
2451 else if (type == TLSEXT_TYPE_renegotiate)
2452 {
2453 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2454 return 0;
2455 renegotiate_seen = 1;
2456 }
2457 else if (type == TLSEXT_TYPE_signature_algorithms)
2458 {
2459 int dsize;
2460 if (s->cert->peer_sigalgs || size < 2)
2461 {
2462 *al = SSL_AD_DECODE_ERROR;
2463 return 0;
2464 }
2465 n2s(data,dsize);
2466 size -= 2;
2467 if (dsize != size || dsize & 1 || !dsize)
2468 {
2469 *al = SSL_AD_DECODE_ERROR;
2470 return 0;
2471 }
2472 if (!tls1_process_sigalgs(s, data, dsize))
2473 {
2474 *al = SSL_AD_DECODE_ERROR;
2475 return 0;
2476 }
2477 /* If sigalgs received and no shared algorithms fatal
2478 * error.
2479 */
2480 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
2481 {
2482 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2483 *al = SSL_AD_ILLEGAL_PARAMETER;
2484 return 0;
2485 }
2486 }
2487
2488 /* TODO(fork): we probably want OCSP stapling support, but this pulls in a lot of code. */
2489#if 0
2490 else if (type == TLSEXT_TYPE_status_request)
2491 {
2492
2493 if (size < 5)
2494 {
2495 *al = SSL_AD_DECODE_ERROR;
2496 return 0;
2497 }
2498
2499 s->tlsext_status_type = *data++;
2500 size--;
2501 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2502 {
2503 const unsigned char *sdata;
2504 int dsize;
2505 /* Read in responder_id_list */
2506 n2s(data,dsize);
2507 size -= 2;
2508 if (dsize > size )
2509 {
2510 *al = SSL_AD_DECODE_ERROR;
2511 return 0;
2512 }
2513 while (dsize > 0)
2514 {
2515 OCSP_RESPID *id;
2516 int idsize;
2517 if (dsize < 4)
2518 {
2519 *al = SSL_AD_DECODE_ERROR;
2520 return 0;
2521 }
2522 n2s(data, idsize);
2523 dsize -= 2 + idsize;
2524 size -= 2 + idsize;
2525 if (dsize < 0)
2526 {
2527 *al = SSL_AD_DECODE_ERROR;
2528 return 0;
2529 }
2530 sdata = data;
2531 data += idsize;
2532 id = d2i_OCSP_RESPID(NULL,
2533 &sdata, idsize);
2534 if (!id)
2535 {
2536 *al = SSL_AD_DECODE_ERROR;
2537 return 0;
2538 }
2539 if (data != sdata)
2540 {
2541 OCSP_RESPID_free(id);
2542 *al = SSL_AD_DECODE_ERROR;
2543 return 0;
2544 }
2545 if (!s->tlsext_ocsp_ids
2546 && !(s->tlsext_ocsp_ids =
2547 sk_OCSP_RESPID_new_null()))
2548 {
2549 OCSP_RESPID_free(id);
2550 *al = SSL_AD_INTERNAL_ERROR;
2551 return 0;
2552 }
2553 if (!sk_OCSP_RESPID_push(
2554 s->tlsext_ocsp_ids, id))
2555 {
2556 OCSP_RESPID_free(id);
2557 *al = SSL_AD_INTERNAL_ERROR;
2558 return 0;
2559 }
2560 }
2561
2562 /* Read in request_extensions */
2563 if (size < 2)
2564 {
2565 *al = SSL_AD_DECODE_ERROR;
2566 return 0;
2567 }
2568 n2s(data,dsize);
2569 size -= 2;
2570 if (dsize != size)
2571 {
2572 *al = SSL_AD_DECODE_ERROR;
2573 return 0;
2574 }
2575 sdata = data;
2576 if (dsize > 0)
2577 {
2578 if (s->tlsext_ocsp_exts)
2579 {
2580 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2581 X509_EXTENSION_free);
2582 }
2583
2584 s->tlsext_ocsp_exts =
2585 d2i_X509_EXTENSIONS(NULL,
2586 &sdata, dsize);
2587 if (!s->tlsext_ocsp_exts
2588 || (data + dsize != sdata))
2589 {
2590 *al = SSL_AD_DECODE_ERROR;
2591 return 0;
2592 }
2593 }
2594 }
2595 /* We don't know what to do with any other type
2596 * so ignore it.
2597 */
2598 else
2599 s->tlsext_status_type = -1;
2600 }
2601#endif
2602
2603#ifndef OPENSSL_NO_HEARTBEATS
2604 else if (type == TLSEXT_TYPE_heartbeat)
2605 {
2606 switch(data[0])
2607 {
2608 case 0x01: /* Client allows us to send HB requests */
2609 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2610 break;
2611 case 0x02: /* Client doesn't accept HB requests */
2612 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2613 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2614 break;
2615 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2616 return 0;
2617 }
2618 }
2619#endif
2620#ifndef OPENSSL_NO_NEXTPROTONEG
2621 else if (type == TLSEXT_TYPE_next_proto_neg &&
2622 s->s3->tmp.finish_md_len == 0 &&
2623 s->s3->alpn_selected == NULL)
2624 {
2625 /* We shouldn't accept this extension on a
2626 * renegotiation.
2627 *
2628 * s->new_session will be set on renegotiation, but we
2629 * probably shouldn't rely that it couldn't be set on
2630 * the initial renegotation too in certain cases (when
2631 * there's some other reason to disallow resuming an
2632 * earlier session -- the current code won't be doing
2633 * anything like that, but this might change).
2634
2635 * A valid sign that there's been a previous handshake
2636 * in this connection is if s->s3->tmp.finish_md_len >
2637 * 0. (We are talking about a check that will happen
2638 * in the Hello protocol round, well before a new
2639 * Finished message could have been computed.) */
2640 s->s3->next_proto_neg_seen = 1;
2641 }
2642#endif
2643
2644 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2645 s->ctx->alpn_select_cb &&
2646 s->s3->tmp.finish_md_len == 0)
2647 {
2648 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2649 return 0;
2650#ifndef OPENSSL_NO_NEXTPROTONEG
2651 /* ALPN takes precedence over NPN. */
2652 s->s3->next_proto_neg_seen = 0;
2653#endif
2654 }
2655
Adam Langley1258b6a2014-06-20 12:00:00 -07002656 else if (type == TLSEXT_TYPE_channel_id &&
2657 s->tlsext_channel_id_enabled)
2658 s->s3->tlsext_channel_id_valid = 1;
2659
2660 else if (type == TLSEXT_TYPE_channel_id_new &&
2661 s->tlsext_channel_id_enabled)
2662 {
2663 s->s3->tlsext_channel_id_valid = 1;
2664 s->s3->tlsext_channel_id_new = 1;
2665 }
2666
2667
Adam Langley95c29f32014-06-20 12:00:00 -07002668 /* session ticket processed earlier */
2669 else if (type == TLSEXT_TYPE_use_srtp)
2670 {
2671 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2672 al))
2673 return 0;
2674 }
2675
2676 else if (type == TLSEXT_TYPE_server_authz)
2677 {
2678 unsigned char *sdata = data;
2679 unsigned char server_authz_dataformatlist_length;
2680
2681 if (size == 0)
2682 {
2683 *al = TLS1_AD_DECODE_ERROR;
2684 return 0;
2685 }
2686
2687 server_authz_dataformatlist_length = *(sdata++);
2688
2689 if (server_authz_dataformatlist_length != size - 1)
2690 {
2691 *al = TLS1_AD_DECODE_ERROR;
2692 return 0;
2693 }
2694
2695 /* Successful session resumption uses the same authz
2696 * information as the original session so we ignore this
2697 * in the case of a session resumption. */
2698 if (!s->hit)
2699 {
2700 if (s->s3->tlsext_authz_client_types != NULL)
2701 OPENSSL_free(s->s3->tlsext_authz_client_types);
2702 s->s3->tlsext_authz_client_types =
2703 OPENSSL_malloc(server_authz_dataformatlist_length);
2704 if (!s->s3->tlsext_authz_client_types)
2705 {
2706 *al = TLS1_AD_INTERNAL_ERROR;
2707 return 0;
2708 }
2709
2710 s->s3->tlsext_authz_client_types_len =
2711 server_authz_dataformatlist_length;
2712 memcpy(s->s3->tlsext_authz_client_types,
2713 sdata,
2714 server_authz_dataformatlist_length);
2715
2716 /* Sort the types in order to check for duplicates. */
2717 qsort(s->s3->tlsext_authz_client_types,
2718 server_authz_dataformatlist_length,
2719 1 /* element size */,
2720 byte_compare);
2721
2722 for (i = 0; i < server_authz_dataformatlist_length; i++)
2723 {
2724 if (i > 0 &&
2725 s->s3->tlsext_authz_client_types[i] ==
2726 s->s3->tlsext_authz_client_types[i-1])
2727 {
2728 *al = TLS1_AD_DECODE_ERROR;
2729 return 0;
2730 }
2731 }
2732 }
2733 }
2734
2735 /* If this ClientHello extension was unhandled and this is
2736 * a nonresumed connection, check whether the extension is a
2737 * custom TLS Extension (has a custom_srv_ext_record), and if
2738 * so call the callback and record the extension number so that
2739 * an appropriate ServerHello may be later returned.
2740 */
2741 else if (!s->hit && s->ctx->custom_srv_ext_records_count)
2742 {
2743 custom_srv_ext_record *record;
2744
2745 for (i=0; i < s->ctx->custom_srv_ext_records_count; i++)
2746 {
2747 record = &s->ctx->custom_srv_ext_records[i];
2748 if (type == record->ext_type)
2749 {
2750 size_t j;
2751
2752 /* Error on duplicate TLS Extensions */
2753 for (j = 0; j < s->s3->tlsext_custom_types_count; j++)
2754 {
2755 if (type == s->s3->tlsext_custom_types[j])
2756 {
2757 *al = TLS1_AD_DECODE_ERROR;
2758 return 0;
2759 }
2760 }
2761
2762 /* NULL callback still notes the extension */
2763 if (record->fn1 && !record->fn1(s, type, data, size, al, record->arg))
2764 return 0;
2765
2766 /* Add the (non-duplicated) entry */
2767 s->s3->tlsext_custom_types_count++;
2768 s->s3->tlsext_custom_types = OPENSSL_realloc(
2769 s->s3->tlsext_custom_types,
2770 s->s3->tlsext_custom_types_count * 2);
2771 if (s->s3->tlsext_custom_types == NULL)
2772 {
2773 s->s3->tlsext_custom_types = 0;
2774 *al = TLS1_AD_INTERNAL_ERROR;
2775 return 0;
2776 }
2777 s->s3->tlsext_custom_types[
2778 s->s3->tlsext_custom_types_count - 1] = type;
2779 }
2780 }
2781 }
2782
2783 data+=size;
2784 }
2785
2786 *p = data;
2787
2788 ri_check:
2789
2790 /* Need RI if renegotiating */
2791
2792 if (!renegotiate_seen && s->renegotiate &&
2793 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2794 {
2795 *al = SSL_AD_HANDSHAKE_FAILURE;
2796 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2797 return 0;
2798 }
2799 /* If no signature algorithms extension set default values */
2800 if (!s->cert->peer_sigalgs)
2801 ssl_cert_set_default_md(s->cert);
2802
2803 return 1;
2804 }
2805
2806int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2807 {
2808 int al = -1;
2809 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2810 {
2811 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2812 return 0;
2813 }
2814
2815 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2816 {
2817 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_CLIENTHELLO_TLSEXT);
2818 return 0;
2819 }
2820 return 1;
2821}
2822
2823#ifndef OPENSSL_NO_NEXTPROTONEG
2824/* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2825 * elements of zero length are allowed and the set of elements must exactly fill
2826 * the length of the block. */
David Benjamin03973092014-06-24 23:27:17 -04002827static char ssl_next_proto_validate(const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07002828 {
David Benjamin03973092014-06-24 23:27:17 -04002829 CBS copy = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07002830
David Benjamin03973092014-06-24 23:27:17 -04002831 while (CBS_len(&copy) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002832 {
David Benjamin03973092014-06-24 23:27:17 -04002833 CBS proto;
2834 if (!CBS_get_u8_length_prefixed(&copy, &proto) ||
2835 CBS_len(&proto) == 0)
2836 {
Adam Langley95c29f32014-06-20 12:00:00 -07002837 return 0;
David Benjamin03973092014-06-24 23:27:17 -04002838 }
Adam Langley95c29f32014-06-20 12:00:00 -07002839 }
David Benjamin03973092014-06-24 23:27:17 -04002840 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002841 }
2842#endif
2843
David Benjamin03973092014-06-24 23:27:17 -04002844static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert)
Adam Langley95c29f32014-06-20 12:00:00 -07002845 {
Adam Langley95c29f32014-06-20 12:00:00 -07002846 int tlsext_servername = 0;
2847 int renegotiate_seen = 0;
David Benjamin03973092014-06-24 23:27:17 -04002848 CBS extensions;
Adam Langley95c29f32014-06-20 12:00:00 -07002849
2850#ifndef OPENSSL_NO_NEXTPROTONEG
2851 s->s3->next_proto_neg_seen = 0;
2852#endif
2853
2854 if (s->s3->alpn_selected)
2855 {
2856 OPENSSL_free(s->s3->alpn_selected);
2857 s->s3->alpn_selected = NULL;
2858 }
2859
2860#ifndef OPENSSL_NO_HEARTBEATS
2861 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
David Benjamin03973092014-06-24 23:27:17 -04002862 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
Adam Langley95c29f32014-06-20 12:00:00 -07002863#endif
2864
David Benjamin03973092014-06-24 23:27:17 -04002865 /* There may be no extensions. */
2866 if (CBS_len(cbs) == 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002867 {
David Benjamin03973092014-06-24 23:27:17 -04002868 goto ri_check;
2869 }
2870
2871 if (!CBS_get_u16_length_prefixed(cbs, &extensions))
2872 {
2873 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002874 return 0;
2875 }
2876
David Benjamin03973092014-06-24 23:27:17 -04002877 while (CBS_len(&extensions) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002878 {
David Benjamin03973092014-06-24 23:27:17 -04002879 uint16_t type;
2880 CBS extension;
Adam Langley95c29f32014-06-20 12:00:00 -07002881
David Benjamin03973092014-06-24 23:27:17 -04002882 /* Decode the next extension. */
2883 if (!CBS_get_u16(&extensions, &type) ||
2884 !CBS_get_u16_length_prefixed(&extensions, &extension))
2885 {
2886 *out_alert = SSL_AD_DECODE_ERROR;
2887 return 0;
2888 }
Adam Langley95c29f32014-06-20 12:00:00 -07002889
2890 if (s->tlsext_debug_cb)
David Benjamin03973092014-06-24 23:27:17 -04002891 {
2892 s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension),
2893 CBS_len(&extension), s->tlsext_debug_arg);
2894 }
Adam Langley95c29f32014-06-20 12:00:00 -07002895
2896 if (type == TLSEXT_TYPE_server_name)
2897 {
David Benjamin03973092014-06-24 23:27:17 -04002898 /* The extension must be empty. */
2899 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002900 {
David Benjamin03973092014-06-24 23:27:17 -04002901 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002902 return 0;
2903 }
David Benjamin03973092014-06-24 23:27:17 -04002904 /* We must have sent it in ClientHello. */
2905 if (s->tlsext_hostname == NULL)
2906 {
2907 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2908 return 0;
2909 }
2910 tlsext_servername = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07002911 }
Adam Langley95c29f32014-06-20 12:00:00 -07002912#ifndef OPENSSL_NO_EC
2913 else if (type == TLSEXT_TYPE_ec_point_formats)
2914 {
David Benjamin03973092014-06-24 23:27:17 -04002915 CBS ec_point_format_list;
Adam Langley95c29f32014-06-20 12:00:00 -07002916
David Benjamin03973092014-06-24 23:27:17 -04002917 if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
2918 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002919 {
David Benjamin03973092014-06-24 23:27:17 -04002920 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002921 return 0;
2922 }
David Benjamin03973092014-06-24 23:27:17 -04002923
2924 if (!CBS_stow(&ec_point_format_list,
2925 &s->session->tlsext_ecpointformatlist,
2926 &s->session->tlsext_ecpointformatlist_length))
Adam Langley95c29f32014-06-20 12:00:00 -07002927 {
David Benjamin03973092014-06-24 23:27:17 -04002928 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002929 return 0;
2930 }
Adam Langley95c29f32014-06-20 12:00:00 -07002931 }
2932#endif /* OPENSSL_NO_EC */
Adam Langley95c29f32014-06-20 12:00:00 -07002933 else if (type == TLSEXT_TYPE_session_ticket)
2934 {
2935 if (s->tls_session_ticket_ext_cb &&
David Benjamin03973092014-06-24 23:27:17 -04002936 !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension),
2937 s->tls_session_ticket_ext_cb_arg))
Adam Langley95c29f32014-06-20 12:00:00 -07002938 {
David Benjamin03973092014-06-24 23:27:17 -04002939 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002940 return 0;
2941 }
David Benjamin03973092014-06-24 23:27:17 -04002942
2943 if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002944 {
David Benjamin03973092014-06-24 23:27:17 -04002945 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07002946 return 0;
2947 }
David Benjamin03973092014-06-24 23:27:17 -04002948
Adam Langley95c29f32014-06-20 12:00:00 -07002949 s->tlsext_ticket_expected = 1;
2950 }
2951#ifdef TLSEXT_TYPE_opaque_prf_input
2952 else if (type == TLSEXT_TYPE_opaque_prf_input)
2953 {
David Benjamin03973092014-06-24 23:27:17 -04002954 CBS opaque_prf_input_value;
Adam Langley95c29f32014-06-20 12:00:00 -07002955
David Benjamin03973092014-06-24 23:27:17 -04002956 if (!CBS_get_u16_length_prefixed(&extension, &opaque_prf_input_value) ||
2957 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002958 {
David Benjamin03973092014-06-24 23:27:17 -04002959 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002960 return 0;
2961 }
Adam Langley95c29f32014-06-20 12:00:00 -07002962
David Benjamin03973092014-06-24 23:27:17 -04002963 if (!CBS_stow(&opaque_prf_input_value,
2964 &s->s3->server_opaque_prf_input,
2965 &s->s3->server_opaque_prf_input_len))
Adam Langley95c29f32014-06-20 12:00:00 -07002966 {
David Benjamin03973092014-06-24 23:27:17 -04002967 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002968 return 0;
2969 }
2970 }
2971#endif
2972 else if (type == TLSEXT_TYPE_status_request)
2973 {
David Benjamin03973092014-06-24 23:27:17 -04002974 /* The extension MUST be empty and may only sent if
2975 * we've requested a status request message. */
2976 if (CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002977 {
David Benjamin03973092014-06-24 23:27:17 -04002978 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07002979 return 0;
2980 }
David Benjamin03973092014-06-24 23:27:17 -04002981 if (s->tlsext_status_type == -1)
2982 {
2983 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2984 return 0;
2985 }
2986 /* Set a flag to expect a CertificateStatus message */
Adam Langley95c29f32014-06-20 12:00:00 -07002987 s->tlsext_status_expected = 1;
2988 }
2989#ifndef OPENSSL_NO_NEXTPROTONEG
David Benjamin03973092014-06-24 23:27:17 -04002990 else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) {
2991 unsigned char *selected;
2992 unsigned char selected_len;
2993
2994 /* We must have requested it. */
2995 if (s->ctx->next_proto_select_cb == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002996 {
David Benjamin03973092014-06-24 23:27:17 -04002997 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
2998 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002999 }
Adam Langley95c29f32014-06-20 12:00:00 -07003000
David Benjamin03973092014-06-24 23:27:17 -04003001 /* The data must be valid. */
3002 if (!ssl_next_proto_validate(&extension))
3003 {
3004 *out_alert = SSL_AD_DECODE_ERROR;
3005 return 0;
3006 }
3007
3008 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len,
3009 CBS_data(&extension), CBS_len(&extension),
3010 s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
3011 {
3012 *out_alert = SSL_AD_INTERNAL_ERROR;
3013 return 0;
3014 }
3015
3016 s->next_proto_negotiated = BUF_memdup(selected, selected_len);
3017 if (s->next_proto_negotiated == NULL)
3018 {
3019 *out_alert = SSL_AD_INTERNAL_ERROR;
3020 return 0;
3021 }
3022 s->next_proto_negotiated_len = selected_len;
3023 s->s3->next_proto_neg_seen = 1;
3024 }
3025#endif
Adam Langley95c29f32014-06-20 12:00:00 -07003026 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
3027 {
David Benjamin03973092014-06-24 23:27:17 -04003028 CBS protocol_name_list, protocol_name;
Adam Langley95c29f32014-06-20 12:00:00 -07003029
3030 /* We must have requested it. */
3031 if (s->alpn_client_proto_list == NULL)
3032 {
David Benjamin03973092014-06-24 23:27:17 -04003033 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07003034 return 0;
3035 }
David Benjamin03973092014-06-24 23:27:17 -04003036
3037 /* The extension data consists of a ProtocolNameList
3038 * which must have exactly one ProtocolName. Each of
3039 * these is length-prefixed. */
3040 if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
3041 CBS_len(&extension) != 0 ||
3042 !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
3043 CBS_len(&protocol_name_list) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07003044 {
David Benjamin03973092014-06-24 23:27:17 -04003045 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07003046 return 0;
3047 }
David Benjamin03973092014-06-24 23:27:17 -04003048
3049 if (!CBS_stow(&protocol_name,
3050 &s->s3->alpn_selected,
3051 &s->s3->alpn_selected_len))
Adam Langley95c29f32014-06-20 12:00:00 -07003052 {
David Benjamin03973092014-06-24 23:27:17 -04003053 *out_alert = SSL_AD_INTERNAL_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07003054 return 0;
3055 }
Adam Langley95c29f32014-06-20 12:00:00 -07003056 }
3057
Adam Langley1258b6a2014-06-20 12:00:00 -07003058 else if (type == TLSEXT_TYPE_channel_id)
David Benjamin03973092014-06-24 23:27:17 -04003059 {
3060 if (CBS_len(&extension) != 0)
3061 {
3062 *out_alert = SSL_AD_DECODE_ERROR;
3063 return 0;
3064 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003065 s->s3->tlsext_channel_id_valid = 1;
David Benjamin03973092014-06-24 23:27:17 -04003066 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003067 else if (type == TLSEXT_TYPE_channel_id_new)
3068 {
David Benjamin03973092014-06-24 23:27:17 -04003069 if (CBS_len(&extension) != 0)
3070 {
3071 *out_alert = SSL_AD_DECODE_ERROR;
3072 return 0;
3073 }
Adam Langley1258b6a2014-06-20 12:00:00 -07003074 s->s3->tlsext_channel_id_valid = 1;
3075 s->s3->tlsext_channel_id_new = 1;
3076 }
3077
Adam Langley95c29f32014-06-20 12:00:00 -07003078 else if (type == TLSEXT_TYPE_renegotiate)
3079 {
David Benjamin03973092014-06-24 23:27:17 -04003080 if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07003081 return 0;
3082 renegotiate_seen = 1;
3083 }
3084#ifndef OPENSSL_NO_HEARTBEATS
3085 else if (type == TLSEXT_TYPE_heartbeat)
3086 {
David Benjamin03973092014-06-24 23:27:17 -04003087 uint8_t heartbeat_mode;
3088 if (!CBS_get_u8(&extension, &heartbeat_mode) ||
3089 CBS_len(&extension) != 0)
3090 {
3091 *alert = SSL_AD_DECODE_ERROR;
3092 return 0;
3093 }
3094 switch (heartbeat_mode)
Adam Langley95c29f32014-06-20 12:00:00 -07003095 {
3096 case 0x01: /* Server allows us to send HB requests */
3097 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
3098 break;
3099 case 0x02: /* Server doesn't accept HB requests */
3100 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
3101 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
3102 break;
David Benjamin03973092014-06-24 23:27:17 -04003103 default: *alert = SSL_AD_ILLEGAL_PARAMETER;
Adam Langley95c29f32014-06-20 12:00:00 -07003104 return 0;
3105 }
3106 }
3107#endif
3108 else if (type == TLSEXT_TYPE_use_srtp)
3109 {
David Benjamin03973092014-06-24 23:27:17 -04003110 if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert))
Adam Langley95c29f32014-06-20 12:00:00 -07003111 return 0;
3112 }
3113
3114 else if (type == TLSEXT_TYPE_server_authz)
3115 {
3116 /* We only support audit proofs. It's an error to send
3117 * an authz hello extension if the client
3118 * didn't request a proof. */
David Benjamin03973092014-06-24 23:27:17 -04003119 CBS authz_data_formats;
3120 uint8_t authz_data_format;
Adam Langley95c29f32014-06-20 12:00:00 -07003121
3122 if (!s->ctx->tlsext_authz_server_audit_proof_cb)
3123 {
David Benjamin03973092014-06-24 23:27:17 -04003124 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07003125 return 0;
3126 }
3127
David Benjamin03973092014-06-24 23:27:17 -04003128 if (!CBS_get_u8_length_prefixed(&extension, &authz_data_formats) ||
3129 CBS_len(&extension) != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07003130 {
David Benjamin03973092014-06-24 23:27:17 -04003131 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07003132 return 0;
3133 }
3134
3135 /* We only support audit proofs, so a legal ServerHello
3136 * authz list contains exactly one entry. */
David Benjamin03973092014-06-24 23:27:17 -04003137 if (!CBS_get_u8(&authz_data_formats, &authz_data_format) ||
3138 CBS_len(&authz_data_formats) != 0 ||
3139 authz_data_format != TLSEXT_AUTHZDATAFORMAT_audit_proof)
Adam Langley95c29f32014-06-20 12:00:00 -07003140 {
David Benjamin03973092014-06-24 23:27:17 -04003141 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
Adam Langley95c29f32014-06-20 12:00:00 -07003142 return 0;
3143 }
3144
3145 s->s3->tlsext_authz_server_promised = 1;
3146 }
3147
3148 /* If this extension type was not otherwise handled, but
3149 * matches a custom_cli_ext_record, then send it to the c
3150 * callback */
David Benjamin03973092014-06-24 23:27:17 -04003151 /* TODO(fork): Can this be removed or transitioned to a
3152 * CBS-based API? It's only used in certificate_transparency to
3153 * parse the signed_certificate_timestamp extension which should
3154 * just be built-in. */
Adam Langley95c29f32014-06-20 12:00:00 -07003155 else if (s->ctx->custom_cli_ext_records_count)
3156 {
3157 size_t i;
3158 custom_cli_ext_record* record;
3159
3160 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
3161 {
3162 record = &s->ctx->custom_cli_ext_records[i];
3163 if (record->ext_type == type)
3164 {
David Benjamin03973092014-06-24 23:27:17 -04003165 if (record->fn2 && !record->fn2(s, type, CBS_data(&extension), CBS_len(&extension), out_alert, record->arg))
Adam Langley95c29f32014-06-20 12:00:00 -07003166 return 0;
3167 break;
3168 }
3169 }
3170 }
3171
Adam Langley95c29f32014-06-20 12:00:00 -07003172 }
3173
3174 if (!s->hit && tlsext_servername == 1)
3175 {
3176 if (s->tlsext_hostname)
3177 {
3178 if (s->session->tlsext_hostname == NULL)
3179 {
3180 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
3181 if (!s->session->tlsext_hostname)
3182 {
David Benjamin03973092014-06-24 23:27:17 -04003183 *out_alert = SSL_AD_UNRECOGNIZED_NAME;
Adam Langley95c29f32014-06-20 12:00:00 -07003184 return 0;
3185 }
3186 }
3187 else
3188 {
David Benjamin03973092014-06-24 23:27:17 -04003189 *out_alert = SSL_AD_DECODE_ERROR;
Adam Langley95c29f32014-06-20 12:00:00 -07003190 return 0;
3191 }
3192 }
3193 }
3194
Adam Langley95c29f32014-06-20 12:00:00 -07003195 ri_check:
3196
3197 /* Determine if we need to see RI. Strictly speaking if we want to
3198 * avoid an attack we should *always* see RI even on initial server
3199 * hello because the client doesn't see any renegotiation during an
3200 * attack. However this would mean we could not connect to any server
3201 * which doesn't support RI so for the immediate future tolerate RI
3202 * absence on initial connect only.
3203 */
3204 if (!renegotiate_seen
3205 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
3206 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
3207 {
David Benjamin03973092014-06-24 23:27:17 -04003208 *out_alert = SSL_AD_HANDSHAKE_FAILURE;
Adam Langley95c29f32014-06-20 12:00:00 -07003209 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
3210 return 0;
3211 }
3212
3213 return 1;
3214 }
3215
3216
3217int ssl_prepare_clienthello_tlsext(SSL *s)
3218 {
3219
3220#ifdef TLSEXT_TYPE_opaque_prf_input
3221 {
3222 int r = 1;
3223
3224 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
3225 {
3226 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
3227 if (!r)
3228 return -1;
3229 }
3230
3231 if (s->tlsext_opaque_prf_input != NULL)
3232 {
3233 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
3234 OPENSSL_free(s->s3->client_opaque_prf_input);
3235
3236 if (s->tlsext_opaque_prf_input_len == 0)
3237 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
3238 else
3239 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
3240 if (s->s3->client_opaque_prf_input == NULL)
3241 {
3242 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_MALLOC_FAILURE);
3243 return -1;
3244 }
3245 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
3246 }
3247
3248 if (r == 2)
3249 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
3250 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
3251 }
3252#endif
3253
3254 return 1;
3255 }
3256
3257int ssl_prepare_serverhello_tlsext(SSL *s)
3258 {
3259 return 1;
3260 }
3261
3262static int ssl_check_clienthello_tlsext_early(SSL *s)
3263 {
3264 int ret=SSL_TLSEXT_ERR_NOACK;
3265 int al = SSL_AD_UNRECOGNIZED_NAME;
3266
3267#ifndef OPENSSL_NO_EC
3268 /* The handling of the ECPointFormats extension is done elsewhere, namely in
3269 * ssl3_choose_cipher in s3_lib.c.
3270 */
3271 /* The handling of the EllipticCurves extension is done elsewhere, namely in
3272 * ssl3_choose_cipher in s3_lib.c.
3273 */
3274#endif
3275
3276 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3277 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3278 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3279 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3280
3281#ifdef TLSEXT_TYPE_opaque_prf_input
3282 {
3283 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
3284 * but we might be sending an alert in response to the client hello,
3285 * so this has to happen here in
3286 * ssl_check_clienthello_tlsext_early(). */
3287
3288 int r = 1;
3289
3290 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
3291 {
3292 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
3293 if (!r)
3294 {
3295 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3296 al = SSL_AD_INTERNAL_ERROR;
3297 goto err;
3298 }
3299 }
3300
3301 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
3302 OPENSSL_free(s->s3->server_opaque_prf_input);
3303 s->s3->server_opaque_prf_input = NULL;
3304
3305 if (s->tlsext_opaque_prf_input != NULL)
3306 {
3307 if (s->s3->client_opaque_prf_input != NULL &&
3308 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
3309 {
3310 /* can only use this extension if we have a server opaque PRF input
3311 * of the same length as the client opaque PRF input! */
3312
3313 if (s->tlsext_opaque_prf_input_len == 0)
3314 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
3315 else
3316 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
3317 if (s->s3->server_opaque_prf_input == NULL)
3318 {
3319 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3320 al = SSL_AD_INTERNAL_ERROR;
3321 goto err;
3322 }
3323 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
3324 }
3325 }
3326
3327 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
3328 {
3329 /* The callback wants to enforce use of the extension,
3330 * but we can't do that with the client opaque PRF input;
3331 * abort the handshake.
3332 */
3333 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3334 al = SSL_AD_HANDSHAKE_FAILURE;
3335 }
3336 }
3337
3338 err:
3339#endif
3340 switch (ret)
3341 {
3342 case SSL_TLSEXT_ERR_ALERT_FATAL:
3343 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3344 return -1;
3345
3346 case SSL_TLSEXT_ERR_ALERT_WARNING:
3347 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3348 return 1;
3349
3350 case SSL_TLSEXT_ERR_NOACK:
3351 s->servername_done=0;
3352 default:
3353 return 1;
3354 }
3355 }
3356
3357int ssl_check_clienthello_tlsext_late(SSL *s)
3358 {
3359 int ret = SSL_TLSEXT_ERR_OK;
3360 int al;
3361
3362 /* If status request then ask callback what to do.
3363 * Note: this must be called after servername callbacks in case
3364 * the certificate has changed, and must be called after the cipher
3365 * has been chosen because this may influence which certificate is sent
3366 */
3367 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
3368 {
3369 int r;
3370 CERT_PKEY *certpkey;
3371 certpkey = ssl_get_server_send_pkey(s);
3372 /* If no certificate can't return certificate status */
3373 if (certpkey == NULL)
3374 {
3375 s->tlsext_status_expected = 0;
3376 return 1;
3377 }
3378 /* Set current certificate to one we will use so
3379 * SSL_get_certificate et al can pick it up.
3380 */
3381 s->cert->key = certpkey;
3382 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3383 switch (r)
3384 {
3385 /* We don't want to send a status request response */
3386 case SSL_TLSEXT_ERR_NOACK:
3387 s->tlsext_status_expected = 0;
3388 break;
3389 /* status request response should be sent */
3390 case SSL_TLSEXT_ERR_OK:
3391 if (s->tlsext_ocsp_resp)
3392 s->tlsext_status_expected = 1;
3393 else
3394 s->tlsext_status_expected = 0;
3395 break;
3396 /* something bad happened */
3397 case SSL_TLSEXT_ERR_ALERT_FATAL:
3398 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3399 al = SSL_AD_INTERNAL_ERROR;
3400 goto err;
3401 }
3402 }
3403 else
3404 s->tlsext_status_expected = 0;
3405
3406 err:
3407 switch (ret)
3408 {
3409 case SSL_TLSEXT_ERR_ALERT_FATAL:
3410 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3411 return -1;
3412
3413 case SSL_TLSEXT_ERR_ALERT_WARNING:
3414 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3415 return 1;
3416
3417 default:
3418 return 1;
3419 }
3420 }
3421
3422int ssl_check_serverhello_tlsext(SSL *s)
3423 {
3424 int ret=SSL_TLSEXT_ERR_NOACK;
3425 int al = SSL_AD_UNRECOGNIZED_NAME;
3426
3427#ifndef OPENSSL_NO_EC
3428 /* If we are client and using an elliptic curve cryptography cipher
3429 * suite, then if server returns an EC point formats lists extension
3430 * it must contain uncompressed.
3431 */
3432 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3433 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3434 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3435 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3436 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3437 {
3438 /* we are using an ECC cipher */
3439 size_t i;
3440 unsigned char *list;
3441 int found_uncompressed = 0;
3442 list = s->session->tlsext_ecpointformatlist;
3443 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3444 {
3445 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3446 {
3447 found_uncompressed = 1;
3448 break;
3449 }
3450 }
3451 if (!found_uncompressed)
3452 {
3453 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3454 return -1;
3455 }
3456 }
3457 ret = SSL_TLSEXT_ERR_OK;
3458#endif /* OPENSSL_NO_EC */
3459
3460 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3461 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3462 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3463 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3464
3465#ifdef TLSEXT_TYPE_opaque_prf_input
3466 if (s->s3->server_opaque_prf_input_len > 0)
3467 {
3468 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
3469 * So first verify that we really have a value from the server too. */
3470
3471 if (s->s3->server_opaque_prf_input == NULL)
3472 {
3473 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3474 al = SSL_AD_HANDSHAKE_FAILURE;
3475 }
3476
3477 /* Anytime the server *has* sent an opaque PRF input, we need to check
3478 * that we have a client opaque PRF input of the same size. */
3479 if (s->s3->client_opaque_prf_input == NULL ||
3480 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
3481 {
3482 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3483 al = SSL_AD_ILLEGAL_PARAMETER;
3484 }
3485 }
3486#endif
3487
3488 /* If we've requested certificate status and we wont get one
3489 * tell the callback
3490 */
3491 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3492 && s->ctx && s->ctx->tlsext_status_cb)
3493 {
3494 int r;
3495 /* Set resp to NULL, resplen to -1 so callback knows
3496 * there is no response.
3497 */
3498 if (s->tlsext_ocsp_resp)
3499 {
3500 OPENSSL_free(s->tlsext_ocsp_resp);
3501 s->tlsext_ocsp_resp = NULL;
3502 }
3503 s->tlsext_ocsp_resplen = -1;
3504 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3505 if (r == 0)
3506 {
3507 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3508 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3509 }
3510 if (r < 0)
3511 {
3512 al = SSL_AD_INTERNAL_ERROR;
3513 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3514 }
3515 }
3516
3517 switch (ret)
3518 {
3519 case SSL_TLSEXT_ERR_ALERT_FATAL:
3520 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3521 return -1;
3522
3523 case SSL_TLSEXT_ERR_ALERT_WARNING:
3524 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3525 return 1;
3526
3527 case SSL_TLSEXT_ERR_NOACK:
3528 s->servername_done=0;
3529 default:
3530 return 1;
3531 }
3532 }
3533
David Benjamin03973092014-06-24 23:27:17 -04003534int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07003535 {
David Benjamin03973092014-06-24 23:27:17 -04003536 int alert = -1;
Adam Langley95c29f32014-06-20 12:00:00 -07003537 if (s->version < SSL3_VERSION)
3538 return 1;
David Benjamin03973092014-06-24 23:27:17 -04003539
3540 if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07003541 {
David Benjamin03973092014-06-24 23:27:17 -04003542 ssl3_send_alert(s, SSL3_AL_FATAL, alert);
Adam Langley95c29f32014-06-20 12:00:00 -07003543 return 0;
3544 }
3545
David Benjamin03973092014-06-24 23:27:17 -04003546 if (ssl_check_serverhello_tlsext(s) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07003547 {
3548 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT);
3549 return 0;
3550 }
David Benjamin03973092014-06-24 23:27:17 -04003551
Adam Langley95c29f32014-06-20 12:00:00 -07003552 return 1;
David Benjamin03973092014-06-24 23:27:17 -04003553 }
Adam Langley95c29f32014-06-20 12:00:00 -07003554
3555/* Since the server cache lookup is done early on in the processing of the
3556 * ClientHello, and other operations depend on the result, we need to handle
3557 * any TLS session ticket extension at the same time.
3558 *
Adam Langleydc9b1412014-06-20 12:00:00 -07003559 * ctx: contains the early callback context, which is the result of a
3560 * shallow parse of the ClientHello.
Adam Langley95c29f32014-06-20 12:00:00 -07003561 * ret: (output) on return, if a ticket was decrypted, then this is set to
3562 * point to the resulting session.
3563 *
3564 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3565 * ciphersuite, in which case we have no use for session tickets and one will
3566 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3567 *
3568 * Returns:
3569 * -1: fatal error, either from parsing or decrypting the ticket.
3570 * 0: no ticket was found (or was ignored, based on settings).
3571 * 1: a zero length extension was found, indicating that the client supports
3572 * session tickets but doesn't currently have one to offer.
3573 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
3574 * couldn't be decrypted because of a non-fatal error.
3575 * 3: a ticket was successfully decrypted and *ret was set.
3576 *
3577 * Side effects:
3578 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3579 * a new session ticket to the client because the client indicated support
3580 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3581 * a session ticket or we couldn't use the one it gave us, or if
3582 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3583 * Otherwise, s->tlsext_ticket_expected is set to 0.
3584 */
Adam Langleydc9b1412014-06-20 12:00:00 -07003585int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
3586 SSL_SESSION **ret)
Adam Langley95c29f32014-06-20 12:00:00 -07003587 {
Adam Langley95c29f32014-06-20 12:00:00 -07003588 *ret = NULL;
3589 s->tlsext_ticket_expected = 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07003590 const unsigned char *data;
3591 size_t len;
3592 int r;
Adam Langley95c29f32014-06-20 12:00:00 -07003593
3594 /* If tickets disabled behave as if no ticket present
3595 * to permit stateful resumption.
3596 */
3597 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
3598 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07003599 if ((s->version <= SSL3_VERSION) && !ctx->extensions)
Adam Langley95c29f32014-06-20 12:00:00 -07003600 return 0;
Adam Langleydc9b1412014-06-20 12:00:00 -07003601 if (!SSL_early_callback_ctx_extension_get(
3602 ctx, TLSEXT_TYPE_session_ticket, &data, &len))
Adam Langley95c29f32014-06-20 12:00:00 -07003603 {
Adam Langleydc9b1412014-06-20 12:00:00 -07003604 return 0;
3605 }
3606 if (len == 0)
3607 {
3608 /* The client will accept a ticket but doesn't
3609 * currently have one. */
3610 s->tlsext_ticket_expected = 1;
3611 return 1;
3612 }
3613 if (s->tls_session_secret_cb)
3614 {
3615 /* Indicate that the ticket couldn't be
3616 * decrypted rather than generating the session
3617 * from ticket now, trigger abbreviated
3618 * handshake based on external mechanism to
3619 * calculate the master secret later. */
3620 return 2;
3621 }
3622 r = tls_decrypt_ticket(s, data, len, ctx->session_id,
3623 ctx->session_id_len, ret);
3624 switch (r)
3625 {
3626 case 2: /* ticket couldn't be decrypted */
3627 s->tlsext_ticket_expected = 1;
3628 return 2;
3629 case 3: /* ticket was decrypted */
3630 return r;
3631 case 4: /* ticket decrypted but need to renew */
3632 s->tlsext_ticket_expected = 1;
3633 return 3;
3634 default: /* fatal error */
Adam Langley95c29f32014-06-20 12:00:00 -07003635 return -1;
3636 }
Adam Langley95c29f32014-06-20 12:00:00 -07003637 }
3638
3639/* tls_decrypt_ticket attempts to decrypt a session ticket.
3640 *
3641 * etick: points to the body of the session ticket extension.
3642 * eticklen: the length of the session tickets extenion.
3643 * sess_id: points at the session ID.
3644 * sesslen: the length of the session ID.
3645 * psess: (output) on return, if a ticket was decrypted, then this is set to
3646 * point to the resulting session.
3647 *
3648 * Returns:
3649 * -1: fatal error, either from parsing or decrypting the ticket.
3650 * 2: the ticket couldn't be decrypted.
3651 * 3: a ticket was successfully decrypted and *psess was set.
3652 * 4: same as 3, but the ticket needs to be renewed.
3653 */
3654static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
3655 const unsigned char *sess_id, int sesslen,
3656 SSL_SESSION **psess)
3657 {
3658 SSL_SESSION *sess;
3659 unsigned char *sdec;
3660 const unsigned char *p;
3661 int slen, mlen, renew_ticket = 0;
3662 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3663 HMAC_CTX hctx;
3664 EVP_CIPHER_CTX ctx;
3665 SSL_CTX *tctx = s->initial_ctx;
3666 /* Need at least keyname + iv + some encrypted data */
3667 if (eticklen < 48)
3668 return 2;
3669 /* Initialize session ticket encryption and HMAC contexts */
3670 HMAC_CTX_init(&hctx);
3671 EVP_CIPHER_CTX_init(&ctx);
3672 if (tctx->tlsext_ticket_key_cb)
3673 {
3674 unsigned char *nctick = (unsigned char *)etick;
3675 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3676 &ctx, &hctx, 0);
3677 if (rv < 0)
3678 return -1;
3679 if (rv == 0)
3680 return 2;
3681 if (rv == 2)
3682 renew_ticket = 1;
3683 }
3684 else
3685 {
3686 /* Check key name matches */
3687 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3688 return 2;
3689 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3690 tlsext_tick_md(), NULL);
3691 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3692 tctx->tlsext_tick_aes_key, etick + 16);
3693 }
3694 /* Attempt to process session ticket, first conduct sanity and
3695 * integrity checks on ticket.
3696 */
3697 mlen = HMAC_size(&hctx);
3698 if (mlen < 0)
3699 {
3700 EVP_CIPHER_CTX_cleanup(&ctx);
3701 return -1;
3702 }
3703 eticklen -= mlen;
3704 /* Check HMAC of encrypted ticket */
3705 HMAC_Update(&hctx, etick, eticklen);
3706 HMAC_Final(&hctx, tick_hmac, NULL);
3707 HMAC_CTX_cleanup(&hctx);
3708 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
3709 return 2;
3710 /* Attempt to decrypt session data */
3711 /* Move p after IV to start of encrypted ticket, update length */
3712 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3713 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3714 sdec = OPENSSL_malloc(eticklen);
3715 if (!sdec)
3716 {
3717 EVP_CIPHER_CTX_cleanup(&ctx);
3718 return -1;
3719 }
3720 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
3721 if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0)
3722 return 2;
3723 slen += mlen;
3724 EVP_CIPHER_CTX_cleanup(&ctx);
3725 p = sdec;
3726
3727 sess = d2i_SSL_SESSION(NULL, &p, slen);
3728 OPENSSL_free(sdec);
3729 if (sess)
3730 {
3731 /* The session ID, if non-empty, is used by some clients to
3732 * detect that the ticket has been accepted. So we copy it to
3733 * the session structure. If it is empty set length to zero
3734 * as required by standard.
3735 */
3736 if (sesslen)
3737 memcpy(sess->session_id, sess_id, sesslen);
3738 sess->session_id_length = sesslen;
3739 *psess = sess;
3740 if (renew_ticket)
3741 return 4;
3742 else
3743 return 3;
3744 }
3745 ERR_clear_error();
3746 /* For session parse failure, indicate that we need to send a new
3747 * ticket. */
3748 return 2;
3749 }
3750
3751/* Tables to translate from NIDs to TLS v1.2 ids */
3752
3753typedef struct
3754 {
3755 int nid;
3756 int id;
3757 } tls12_lookup;
3758
3759static tls12_lookup tls12_md[] = {
3760 {NID_md5, TLSEXT_hash_md5},
3761 {NID_sha1, TLSEXT_hash_sha1},
3762 {NID_sha224, TLSEXT_hash_sha224},
3763 {NID_sha256, TLSEXT_hash_sha256},
3764 {NID_sha384, TLSEXT_hash_sha384},
3765 {NID_sha512, TLSEXT_hash_sha512}
3766};
3767
3768static tls12_lookup tls12_sig[] = {
3769 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3770 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3771 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3772};
3773
3774static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
3775 {
3776 size_t i;
3777 for (i = 0; i < tlen; i++)
3778 {
3779 if (table[i].nid == nid)
3780 return table[i].id;
3781 }
3782 return -1;
3783 }
3784
3785static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
3786 {
3787 size_t i;
3788 for (i = 0; i < tlen; i++)
3789 {
3790 if ((table[i].id) == id)
3791 return table[i].nid;
3792 }
3793 return NID_undef;
3794 }
3795
3796int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3797 {
3798 int sig_id, md_id;
3799 if (!md)
3800 return 0;
3801 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3802 sizeof(tls12_md)/sizeof(tls12_lookup));
3803 if (md_id == -1)
3804 return 0;
3805 sig_id = tls12_get_sigid(pk);
3806 if (sig_id == -1)
3807 return 0;
3808 p[0] = (unsigned char)md_id;
3809 p[1] = (unsigned char)sig_id;
3810 return 1;
3811 }
3812
3813int tls12_get_sigid(const EVP_PKEY *pk)
3814 {
3815 return tls12_find_id(pk->type, tls12_sig,
3816 sizeof(tls12_sig)/sizeof(tls12_lookup));
3817 }
3818
3819const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3820 {
3821 switch(hash_alg)
3822 {
3823#ifndef OPENSSL_NO_MD5
3824 case TLSEXT_hash_md5:
3825#ifdef OPENSSL_FIPS
3826 if (FIPS_mode())
3827 return NULL;
3828#endif
3829 return EVP_md5();
3830#endif
3831#ifndef OPENSSL_NO_SHA
3832 case TLSEXT_hash_sha1:
3833 return EVP_sha1();
3834#endif
3835#ifndef OPENSSL_NO_SHA256
3836 case TLSEXT_hash_sha224:
3837 return EVP_sha224();
3838
3839 case TLSEXT_hash_sha256:
3840 return EVP_sha256();
3841#endif
3842#ifndef OPENSSL_NO_SHA512
3843 case TLSEXT_hash_sha384:
3844 return EVP_sha384();
3845
3846 case TLSEXT_hash_sha512:
3847 return EVP_sha512();
3848#endif
3849 default:
3850 return NULL;
3851
3852 }
3853 }
3854
3855static int tls12_get_pkey_idx(unsigned char sig_alg)
3856 {
3857 switch(sig_alg)
3858 {
3859#ifndef OPENSSL_NO_RSA
3860 case TLSEXT_signature_rsa:
3861 return SSL_PKEY_RSA_SIGN;
3862#endif
3863#ifndef OPENSSL_NO_DSA
3864 case TLSEXT_signature_dsa:
3865 return SSL_PKEY_DSA_SIGN;
3866#endif
3867#ifndef OPENSSL_NO_ECDSA
3868 case TLSEXT_signature_ecdsa:
3869 return SSL_PKEY_ECC;
3870#endif
3871 }
3872 return -1;
3873 }
3874
3875/* Convert TLS 1.2 signature algorithm extension values into NIDs */
3876static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3877 int *psignhash_nid, const unsigned char *data)
3878 {
3879 int sign_nid = 0, hash_nid = 0;
3880 if (!phash_nid && !psign_nid && !psignhash_nid)
3881 return;
3882 if (phash_nid || psignhash_nid)
3883 {
3884 hash_nid = tls12_find_nid(data[0], tls12_md,
3885 sizeof(tls12_md)/sizeof(tls12_lookup));
3886 if (phash_nid)
3887 *phash_nid = hash_nid;
3888 }
3889 if (psign_nid || psignhash_nid)
3890 {
3891 sign_nid = tls12_find_nid(data[1], tls12_sig,
3892 sizeof(tls12_sig)/sizeof(tls12_lookup));
3893 if (psign_nid)
3894 *psign_nid = sign_nid;
3895 }
3896 if (psignhash_nid)
3897 {
3898 if (sign_nid && hash_nid)
3899 OBJ_find_sigid_by_algs(psignhash_nid,
3900 hash_nid, sign_nid);
3901 else
3902 *psignhash_nid = NID_undef;
3903 }
3904 }
3905/* Given preference and allowed sigalgs set shared sigalgs */
3906static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
3907 const unsigned char *pref, size_t preflen,
3908 const unsigned char *allow, size_t allowlen)
3909 {
3910 const unsigned char *ptmp, *atmp;
3911 size_t i, j, nmatch = 0;
3912 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
3913 {
3914 /* Skip disabled hashes or signature algorithms */
3915 if (tls12_get_hash(ptmp[0]) == NULL)
3916 continue;
3917 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3918 continue;
3919 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
3920 {
3921 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
3922 {
3923 nmatch++;
3924 if (shsig)
3925 {
3926 shsig->rhash = ptmp[0];
3927 shsig->rsign = ptmp[1];
3928 tls1_lookup_sigalg(&shsig->hash_nid,
3929 &shsig->sign_nid,
3930 &shsig->signandhash_nid,
3931 ptmp);
3932 shsig++;
3933 }
3934 break;
3935 }
3936 }
3937 }
3938 return nmatch;
3939 }
3940
3941/* Set shared signature algorithms for SSL structures */
3942static int tls1_set_shared_sigalgs(SSL *s)
3943 {
3944 const unsigned char *pref, *allow, *conf;
3945 size_t preflen, allowlen, conflen;
3946 size_t nmatch;
3947 TLS_SIGALGS *salgs = NULL;
3948 CERT *c = s->cert;
3949 unsigned int is_suiteb = tls1_suiteb(s);
Adam Langleydb4f9522014-06-20 12:00:00 -07003950 if (c->shared_sigalgs)
3951 {
3952 OPENSSL_free(c->shared_sigalgs);
3953 c->shared_sigalgs = NULL;
3954 }
Adam Langley95c29f32014-06-20 12:00:00 -07003955 /* If client use client signature algorithms if not NULL */
3956 if (!s->server && c->client_sigalgs && !is_suiteb)
3957 {
3958 conf = c->client_sigalgs;
3959 conflen = c->client_sigalgslen;
3960 }
3961 else if (c->conf_sigalgs && !is_suiteb)
3962 {
3963 conf = c->conf_sigalgs;
3964 conflen = c->conf_sigalgslen;
3965 }
3966 else
3967 conflen = tls12_get_psigalgs(s, &conf);
3968 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb)
3969 {
3970 pref = conf;
3971 preflen = conflen;
3972 allow = c->peer_sigalgs;
3973 allowlen = c->peer_sigalgslen;
3974 }
3975 else
3976 {
3977 allow = conf;
3978 allowlen = conflen;
3979 pref = c->peer_sigalgs;
3980 preflen = c->peer_sigalgslen;
3981 }
3982 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
3983 if (!nmatch)
3984 return 1;
3985 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3986 if (!salgs)
3987 return 0;
3988 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
3989 c->shared_sigalgs = salgs;
3990 c->shared_sigalgslen = nmatch;
3991 return 1;
3992 }
3993
3994
3995/* Set preferred digest for each key type */
3996
3997int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
3998 {
3999 int idx;
4000 size_t i;
4001 const EVP_MD *md;
4002 CERT *c = s->cert;
4003 TLS_SIGALGS *sigptr;
4004 /* Extension ignored for inappropriate versions */
4005 if (!SSL_USE_SIGALGS(s))
4006 return 1;
4007 /* Should never happen */
4008 if (!c)
4009 return 0;
4010
Adam Langleydb4f9522014-06-20 12:00:00 -07004011 if (c->peer_sigalgs)
4012 OPENSSL_free(c->peer_sigalgs);
Adam Langley95c29f32014-06-20 12:00:00 -07004013 c->peer_sigalgs = OPENSSL_malloc(dsize);
4014 if (!c->peer_sigalgs)
4015 return 0;
4016 c->peer_sigalgslen = dsize;
4017 memcpy(c->peer_sigalgs, data, dsize);
4018
4019 tls1_set_shared_sigalgs(s);
4020
4021#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
4022 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
4023 {
4024 /* Use first set signature preference to force message
4025 * digest, ignoring any peer preferences.
4026 */
4027 const unsigned char *sigs = NULL;
4028 if (s->server)
4029 sigs = c->conf_sigalgs;
4030 else
4031 sigs = c->client_sigalgs;
4032 if (sigs)
4033 {
4034 idx = tls12_get_pkey_idx(sigs[1]);
4035 md = tls12_get_hash(sigs[0]);
4036 c->pkeys[idx].digest = md;
4037 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
4038 if (idx == SSL_PKEY_RSA_SIGN)
4039 {
4040 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
4041 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
4042 }
4043 }
4044 }
4045#endif
4046
4047 for (i = 0, sigptr = c->shared_sigalgs;
4048 i < c->shared_sigalgslen; i++, sigptr++)
4049 {
4050 idx = tls12_get_pkey_idx(sigptr->rsign);
4051 if (idx > 0 && c->pkeys[idx].digest == NULL)
4052 {
4053 md = tls12_get_hash(sigptr->rhash);
4054 c->pkeys[idx].digest = md;
4055 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
4056 if (idx == SSL_PKEY_RSA_SIGN)
4057 {
4058 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
4059 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
4060 }
4061 }
4062
4063 }
4064 /* In strict mode leave unset digests as NULL to indicate we can't
4065 * use the certificate for signing.
4066 */
4067 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
4068 {
4069 /* Set any remaining keys to default values. NOTE: if alg is
4070 * not supported it stays as NULL.
4071 */
4072#ifndef OPENSSL_NO_DSA
4073 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
4074 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
4075#endif
4076#ifndef OPENSSL_NO_RSA
4077 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
4078 {
4079 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
4080 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
4081 }
4082#endif
4083#ifndef OPENSSL_NO_ECDSA
4084 if (!c->pkeys[SSL_PKEY_ECC].digest)
4085 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
4086#endif
4087 }
4088 return 1;
4089 }
4090
4091
4092int SSL_get_sigalgs(SSL *s, int idx,
4093 int *psign, int *phash, int *psignhash,
4094 unsigned char *rsig, unsigned char *rhash)
4095 {
4096 const unsigned char *psig = s->cert->peer_sigalgs;
4097 if (psig == NULL)
4098 return 0;
4099 if (idx >= 0)
4100 {
4101 idx <<= 1;
4102 if (idx >= (int)s->cert->peer_sigalgslen)
4103 return 0;
4104 psig += idx;
4105 if (rhash)
4106 *rhash = psig[0];
4107 if (rsig)
4108 *rsig = psig[1];
4109 tls1_lookup_sigalg(phash, psign, psignhash, psig);
4110 }
4111 return s->cert->peer_sigalgslen / 2;
4112 }
4113
4114int SSL_get_shared_sigalgs(SSL *s, int idx,
4115 int *psign, int *phash, int *psignhash,
4116 unsigned char *rsig, unsigned char *rhash)
4117 {
4118 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
4119 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
4120 return 0;
4121 shsigalgs += idx;
4122 if (phash)
4123 *phash = shsigalgs->hash_nid;
4124 if (psign)
4125 *psign = shsigalgs->sign_nid;
4126 if (psignhash)
4127 *psignhash = shsigalgs->signandhash_nid;
4128 if (rsig)
4129 *rsig = shsigalgs->rsign;
4130 if (rhash)
4131 *rhash = shsigalgs->rhash;
4132 return s->cert->shared_sigalgslen;
4133 }
4134
4135
4136#ifndef OPENSSL_NO_HEARTBEATS
4137int
4138tls1_process_heartbeat(SSL *s)
4139 {
4140 unsigned char *p = &s->s3->rrec.data[0], *pl;
4141 unsigned short hbtype;
4142 unsigned int payload;
4143 unsigned int padding = 16; /* Use minimum padding */
4144
Adam Langley95c29f32014-06-20 12:00:00 -07004145 if (s->msg_callback)
4146 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
4147 &s->s3->rrec.data[0], s->s3->rrec.length,
4148 s, s->msg_callback_arg);
4149
Adam Langley56475202014-06-20 12:00:00 -07004150 /* Read type and payload length first */
4151 if (1 + 2 + 16 > s->s3->rrec.length)
4152 return 0; /* silently discard */
4153 hbtype = *p++;
4154 n2s(p, payload);
4155 if (1 + 2 + payload + 16 > s->s3->rrec.length)
4156 return 0; /* silently discard per RFC 6520 sec. 4 */
4157 pl = p;
4158
Adam Langley95c29f32014-06-20 12:00:00 -07004159 if (hbtype == TLS1_HB_REQUEST)
4160 {
4161 unsigned char *buffer, *bp;
4162 int r;
4163
4164 /* Allocate memory for the response, size is 1 bytes
4165 * message type, plus 2 bytes payload length, plus
4166 * payload, plus padding
4167 */
4168 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
4169 bp = buffer;
4170
4171 /* Enter response type, length and copy payload */
4172 *bp++ = TLS1_HB_RESPONSE;
4173 s2n(payload, bp);
4174 memcpy(bp, pl, payload);
4175 bp += payload;
4176 /* Random padding */
4177 RAND_pseudo_bytes(bp, padding);
4178
4179 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
4180
4181 if (r >= 0 && s->msg_callback)
4182 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4183 buffer, 3 + payload + padding,
4184 s, s->msg_callback_arg);
4185
4186 OPENSSL_free(buffer);
4187
4188 if (r < 0)
4189 return r;
4190 }
4191 else if (hbtype == TLS1_HB_RESPONSE)
4192 {
4193 unsigned int seq;
4194
4195 /* We only send sequence numbers (2 bytes unsigned int),
4196 * and 16 random bytes, so we just try to read the
4197 * sequence number */
4198 n2s(pl, seq);
4199
4200 if (payload == 18 && seq == s->tlsext_hb_seq)
4201 {
4202 s->tlsext_hb_seq++;
4203 s->tlsext_hb_pending = 0;
4204 }
4205 }
4206
4207 return 0;
4208 }
4209
4210int
4211tls1_heartbeat(SSL *s)
4212 {
4213 unsigned char *buf, *p;
4214 int ret;
4215 unsigned int payload = 18; /* Sequence number + random bytes */
4216 unsigned int padding = 16; /* Use minimum padding */
4217
4218 /* Only send if peer supports and accepts HB requests... */
4219 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
4220 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
4221 {
4222 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
4223 return -1;
4224 }
4225
4226 /* ...and there is none in flight yet... */
4227 if (s->tlsext_hb_pending)
4228 {
4229 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_TLS_HEARTBEAT_PENDING);
4230 return -1;
4231 }
4232
4233 /* ...and no handshake in progress. */
4234 if (SSL_in_init(s) || s->in_handshake)
4235 {
4236 OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_UNEXPECTED_MESSAGE);
4237 return -1;
4238 }
4239
4240 /* Check if padding is too long, payload and padding
4241 * must not exceed 2^14 - 3 = 16381 bytes in total.
4242 */
4243 assert(payload + padding <= 16381);
4244
4245 /* Create HeartBeat message, we just use a sequence number
4246 * as payload to distuingish different messages and add
4247 * some random stuff.
4248 * - Message Type, 1 byte
4249 * - Payload Length, 2 bytes (unsigned int)
4250 * - Payload, the sequence number (2 bytes uint)
4251 * - Payload, random bytes (16 bytes uint)
4252 * - Padding
4253 */
4254 buf = OPENSSL_malloc(1 + 2 + payload + padding);
4255 p = buf;
4256 /* Message Type */
4257 *p++ = TLS1_HB_REQUEST;
4258 /* Payload length (18 bytes here) */
4259 s2n(payload, p);
4260 /* Sequence number */
4261 s2n(s->tlsext_hb_seq, p);
4262 /* 16 random bytes */
4263 RAND_pseudo_bytes(p, 16);
4264 p += 16;
4265 /* Random padding */
4266 RAND_pseudo_bytes(p, padding);
4267
4268 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
4269 if (ret >= 0)
4270 {
4271 if (s->msg_callback)
4272 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4273 buf, 3 + payload + padding,
4274 s, s->msg_callback_arg);
4275
4276 s->tlsext_hb_pending = 1;
4277 }
4278
4279 OPENSSL_free(buf);
4280
4281 return ret;
4282 }
4283#endif
4284
Adam Langley1258b6a2014-06-20 12:00:00 -07004285#if !defined(OPENSSL_NO_TLSEXT)
4286/* tls1_channel_id_hash calculates the signed data for a Channel ID on the given
4287 * SSL connection and writes it to |md|. */
4288int
4289tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s)
4290 {
4291 EVP_MD_CTX ctx;
4292 unsigned char temp_digest[EVP_MAX_MD_SIZE];
4293 unsigned temp_digest_len;
4294 int i;
4295 static const char kClientIDMagic[] = "TLS Channel ID signature";
4296
4297 if (s->s3->handshake_buffer)
4298 if (!ssl3_digest_cached_records(s))
4299 return 0;
4300
4301 EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
4302
4303 if (s->hit && s->s3->tlsext_channel_id_new)
4304 {
4305 static const char kResumptionMagic[] = "Resumption";
4306 EVP_DigestUpdate(md, kResumptionMagic,
4307 sizeof(kResumptionMagic));
4308 if (s->session->original_handshake_hash_len == 0)
4309 return 0;
4310 EVP_DigestUpdate(md, s->session->original_handshake_hash,
4311 s->session->original_handshake_hash_len);
4312 }
4313
4314 EVP_MD_CTX_init(&ctx);
4315 for (i = 0; i < SSL_MAX_DIGEST; i++)
4316 {
4317 if (s->s3->handshake_dgst[i] == NULL)
4318 continue;
4319 EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]);
4320 EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
4321 EVP_DigestUpdate(md, temp_digest, temp_digest_len);
4322 }
4323 EVP_MD_CTX_cleanup(&ctx);
4324
4325 return 1;
4326 }
4327#endif
4328
4329/* tls1_record_handshake_hashes_for_channel_id records the current handshake
4330 * hashes in |s->session| so that Channel ID resumptions can sign that data. */
4331int tls1_record_handshake_hashes_for_channel_id(SSL *s)
4332 {
4333 int digest_len;
4334 /* This function should never be called for a resumed session because
4335 * the handshake hashes that we wish to record are for the original,
4336 * full handshake. */
4337 if (s->hit)
4338 return -1;
4339 /* It only makes sense to call this function if Channel IDs have been
4340 * negotiated. */
4341 if (!s->s3->tlsext_channel_id_new)
4342 return -1;
4343
4344 digest_len = tls1_handshake_digest(
4345 s, s->session->original_handshake_hash,
4346 sizeof(s->session->original_handshake_hash));
4347 if (digest_len < 0)
4348 return -1;
4349
4350 s->session->original_handshake_hash_len = digest_len;
4351
4352 return 1;
4353 }
4354
Adam Langley95c29f32014-06-20 12:00:00 -07004355/* TODO(fork): remove */
4356#if 0
4357#define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
4358
4359typedef struct
4360 {
4361 size_t sigalgcnt;
4362 int sigalgs[MAX_SIGALGLEN];
4363 } sig_cb_st;
4364
4365static int sig_cb(const char *elem, int len, void *arg)
4366 {
4367 sig_cb_st *sarg = arg;
4368 size_t i;
4369 char etmp[20], *p;
4370 int sig_alg, hash_alg;
4371 if (sarg->sigalgcnt == MAX_SIGALGLEN)
4372 return 0;
4373 if (len > (int)(sizeof(etmp) - 1))
4374 return 0;
4375 memcpy(etmp, elem, len);
4376 etmp[len] = 0;
4377 p = strchr(etmp, '+');
4378 if (!p)
4379 return 0;
4380 *p = 0;
4381 p++;
4382 if (!*p)
4383 return 0;
4384
4385 if (!strcmp(etmp, "RSA"))
4386 sig_alg = EVP_PKEY_RSA;
4387 else if (!strcmp(etmp, "DSA"))
4388 sig_alg = EVP_PKEY_DSA;
4389 else if (!strcmp(etmp, "ECDSA"))
4390 sig_alg = EVP_PKEY_EC;
4391 else return 0;
4392
4393 hash_alg = OBJ_sn2nid(p);
4394 if (hash_alg == NID_undef)
4395 hash_alg = OBJ_ln2nid(p);
4396 if (hash_alg == NID_undef)
4397 return 0;
4398
4399 for (i = 0; i < sarg->sigalgcnt; i+=2)
4400 {
4401 if (sarg->sigalgs[i] == sig_alg
4402 && sarg->sigalgs[i + 1] == hash_alg)
4403 return 0;
4404 }
4405 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
4406 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
4407 return 1;
4408 }
4409
4410/* Set suppored signature algorithms based on a colon separated list
4411 * of the form sig+hash e.g. RSA+SHA512:DSA+SHA512 */
4412int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
4413 {
4414 sig_cb_st sig;
4415 sig.sigalgcnt = 0;
4416 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
4417 return 0;
4418 if (c == NULL)
4419 return 1;
4420 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
4421 }
4422#endif
4423
4424int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
4425 {
4426 unsigned char *sigalgs, *sptr;
4427 int rhash, rsign;
4428 size_t i;
4429 if (salglen & 1)
4430 return 0;
4431 sigalgs = OPENSSL_malloc(salglen);
4432 if (sigalgs == NULL)
4433 return 0;
4434 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
4435 {
4436 rhash = tls12_find_id(*psig_nids++, tls12_md,
4437 sizeof(tls12_md)/sizeof(tls12_lookup));
4438 rsign = tls12_find_id(*psig_nids++, tls12_sig,
4439 sizeof(tls12_sig)/sizeof(tls12_lookup));
4440
4441 if (rhash == -1 || rsign == -1)
4442 goto err;
4443 *sptr++ = rhash;
4444 *sptr++ = rsign;
4445 }
4446
4447 if (client)
4448 {
4449 if (c->client_sigalgs)
4450 OPENSSL_free(c->client_sigalgs);
4451 c->client_sigalgs = sigalgs;
4452 c->client_sigalgslen = salglen;
4453 }
4454 else
4455 {
4456 if (c->conf_sigalgs)
4457 OPENSSL_free(c->conf_sigalgs);
4458 c->conf_sigalgs = sigalgs;
4459 c->conf_sigalgslen = salglen;
4460 }
4461
4462 return 1;
4463
4464 err:
4465 OPENSSL_free(sigalgs);
4466 return 0;
4467 }
4468
4469static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
4470 {
4471 int sig_nid;
4472 size_t i;
4473 if (default_nid == -1)
4474 return 1;
4475 sig_nid = X509_get_signature_nid(x);
4476 if (default_nid)
4477 return sig_nid == default_nid ? 1 : 0;
4478 for (i = 0; i < c->shared_sigalgslen; i++)
4479 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
4480 return 1;
4481 return 0;
4482 }
4483/* Check to see if a certificate issuer name matches list of CA names */
4484static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
4485 {
4486 X509_NAME *nm;
4487 int i;
4488 nm = X509_get_issuer_name(x);
4489 for (i = 0; i < sk_X509_NAME_num(names); i++)
4490 {
4491 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
4492 return 1;
4493 }
4494 return 0;
4495 }
4496
4497/* Check certificate chain is consistent with TLS extensions and is
4498 * usable by server. This servers two purposes: it allows users to
4499 * check chains before passing them to the server and it allows the
4500 * server to check chains before attempting to use them.
4501 */
4502
4503/* Flags which need to be set for a certificate when stict mode not set */
4504
4505#define CERT_PKEY_VALID_FLAGS \
4506 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
4507/* Strict mode flags */
4508#define CERT_PKEY_STRICT_FLAGS \
4509 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
4510 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
4511
4512int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
4513 int idx)
4514 {
4515 int i;
4516 int rv = 0;
4517 int check_flags = 0, strict_mode;
4518 CERT_PKEY *cpk = NULL;
4519 CERT *c = s->cert;
4520 unsigned int suiteb_flags = tls1_suiteb(s);
4521 /* idx == -1 means checking server chains */
4522 if (idx != -1)
4523 {
4524 /* idx == -2 means checking client certificate chains */
4525 if (idx == -2)
4526 {
4527 cpk = c->key;
4528 idx = cpk - c->pkeys;
4529 }
4530 else
4531 cpk = c->pkeys + idx;
4532 x = cpk->x509;
4533 pk = cpk->privatekey;
4534 chain = cpk->chain;
4535 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
4536 /* If no cert or key, forget it */
4537 if (!x || !pk)
4538 goto end;
4539#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
4540 /* Allow any certificate to pass test */
4541 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
4542 {
4543 rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
4544 cpk->valid_flags = rv;
4545 return rv;
4546 }
4547#endif
4548 }
4549 else
4550 {
4551 if (!x || !pk)
4552 goto end;
4553 idx = ssl_cert_type(x, pk);
4554 if (idx == -1)
4555 goto end;
4556 cpk = c->pkeys + idx;
4557 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
4558 check_flags = CERT_PKEY_STRICT_FLAGS;
4559 else
4560 check_flags = CERT_PKEY_VALID_FLAGS;
4561 strict_mode = 1;
4562 }
4563
4564 if (suiteb_flags)
4565 {
4566 int ok;
4567 if (check_flags)
4568 check_flags |= CERT_PKEY_SUITEB;
4569 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
4570 if (ok != X509_V_OK)
4571 {
4572 if (check_flags)
4573 rv |= CERT_PKEY_SUITEB;
4574 else
4575 goto end;
4576 }
4577 }
4578
4579 /* Check all signature algorithms are consistent with
4580 * signature algorithms extension if TLS 1.2 or later
4581 * and strict mode.
4582 */
4583 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
4584 {
4585 int default_nid;
4586 unsigned char rsign = 0;
4587 if (c->peer_sigalgs)
4588 default_nid = 0;
4589 /* If no sigalgs extension use defaults from RFC5246 */
4590 else
4591 {
4592 switch(idx)
4593 {
4594 case SSL_PKEY_RSA_ENC:
4595 case SSL_PKEY_RSA_SIGN:
4596 case SSL_PKEY_DH_RSA:
4597 rsign = TLSEXT_signature_rsa;
4598 default_nid = NID_sha1WithRSAEncryption;
4599 break;
4600
4601 case SSL_PKEY_DSA_SIGN:
4602 case SSL_PKEY_DH_DSA:
4603 rsign = TLSEXT_signature_dsa;
4604 default_nid = NID_dsaWithSHA1;
4605 break;
4606
4607 case SSL_PKEY_ECC:
4608 rsign = TLSEXT_signature_ecdsa;
4609 default_nid = NID_ecdsa_with_SHA1;
4610 break;
4611
4612 default:
4613 default_nid = -1;
4614 break;
4615 }
4616 }
4617 /* If peer sent no signature algorithms extension and we
4618 * have set preferred signature algorithms check we support
4619 * sha1.
4620 */
4621 if (default_nid > 0 && c->conf_sigalgs)
4622 {
4623 size_t j;
4624 const unsigned char *p = c->conf_sigalgs;
4625 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
4626 {
4627 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4628 break;
4629 }
4630 if (j == c->conf_sigalgslen)
4631 {
4632 if (check_flags)
4633 goto skip_sigs;
4634 else
4635 goto end;
4636 }
4637 }
4638 /* Check signature algorithm of each cert in chain */
4639 if (!tls1_check_sig_alg(c, x, default_nid))
4640 {
4641 if (!check_flags) goto end;
4642 }
4643 else
4644 rv |= CERT_PKEY_EE_SIGNATURE;
4645 rv |= CERT_PKEY_CA_SIGNATURE;
4646 for (i = 0; i < sk_X509_num(chain); i++)
4647 {
4648 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
4649 default_nid))
4650 {
4651 if (check_flags)
4652 {
4653 rv &= ~CERT_PKEY_CA_SIGNATURE;
4654 break;
4655 }
4656 else
4657 goto end;
4658 }
4659 }
4660 }
4661 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4662 else if(check_flags)
4663 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
4664 skip_sigs:
4665 /* Check cert parameters are consistent */
4666 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4667 rv |= CERT_PKEY_EE_PARAM;
4668 else if (!check_flags)
4669 goto end;
4670 if (!s->server)
4671 rv |= CERT_PKEY_CA_PARAM;
4672 /* In strict mode check rest of chain too */
4673 else if (strict_mode)
4674 {
4675 rv |= CERT_PKEY_CA_PARAM;
4676 for (i = 0; i < sk_X509_num(chain); i++)
4677 {
4678 X509 *ca = sk_X509_value(chain, i);
4679 if (!tls1_check_cert_param(s, ca, 0))
4680 {
4681 if (check_flags)
4682 {
4683 rv &= ~CERT_PKEY_CA_PARAM;
4684 break;
4685 }
4686 else
4687 goto end;
4688 }
4689 }
4690 }
4691 if (!s->server && strict_mode)
4692 {
4693 STACK_OF(X509_NAME) *ca_dn;
4694 int check_type = 0;
4695 switch (pk->type)
4696 {
4697 case EVP_PKEY_RSA:
4698 check_type = TLS_CT_RSA_SIGN;
4699 break;
4700 case EVP_PKEY_DSA:
4701 check_type = TLS_CT_DSS_SIGN;
4702 break;
4703 case EVP_PKEY_EC:
4704 check_type = TLS_CT_ECDSA_SIGN;
4705 break;
4706 case EVP_PKEY_DH:
4707 case EVP_PKEY_DHX:
4708 {
4709 int cert_type = X509_certificate_type(x, pk);
4710 if (cert_type & EVP_PKS_RSA)
4711 check_type = TLS_CT_RSA_FIXED_DH;
4712 if (cert_type & EVP_PKS_DSA)
4713 check_type = TLS_CT_DSS_FIXED_DH;
4714 }
4715 }
4716 if (check_type)
4717 {
4718 const unsigned char *ctypes;
4719 int ctypelen;
4720 if (c->ctypes)
4721 {
4722 ctypes = c->ctypes;
4723 ctypelen = (int)c->ctype_num;
4724 }
4725 else
4726 {
4727 ctypes = (unsigned char *)s->s3->tmp.ctype;
4728 ctypelen = s->s3->tmp.ctype_num;
4729 }
4730 for (i = 0; i < ctypelen; i++)
4731 {
4732 if (ctypes[i] == check_type)
4733 {
4734 rv |= CERT_PKEY_CERT_TYPE;
4735 break;
4736 }
4737 }
4738 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4739 goto end;
4740 }
4741 else
4742 rv |= CERT_PKEY_CERT_TYPE;
4743
4744
4745 ca_dn = s->s3->tmp.ca_names;
4746
4747 if (!sk_X509_NAME_num(ca_dn))
4748 rv |= CERT_PKEY_ISSUER_NAME;
4749
4750 if (!(rv & CERT_PKEY_ISSUER_NAME))
4751 {
4752 if (ssl_check_ca_name(ca_dn, x))
4753 rv |= CERT_PKEY_ISSUER_NAME;
4754 }
4755 if (!(rv & CERT_PKEY_ISSUER_NAME))
4756 {
4757 for (i = 0; i < sk_X509_num(chain); i++)
4758 {
4759 X509 *xtmp = sk_X509_value(chain, i);
4760 if (ssl_check_ca_name(ca_dn, xtmp))
4761 {
4762 rv |= CERT_PKEY_ISSUER_NAME;
4763 break;
4764 }
4765 }
4766 }
4767 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4768 goto end;
4769 }
4770 else
4771 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
4772
4773 if (!check_flags || (rv & check_flags) == check_flags)
4774 rv |= CERT_PKEY_VALID;
4775
4776 end:
4777
4778 if (TLS1_get_version(s) >= TLS1_2_VERSION)
4779 {
4780 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
4781 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
4782 else if (cpk->digest)
4783 rv |= CERT_PKEY_SIGN;
4784 }
4785 else
4786 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
4787
4788 /* When checking a CERT_PKEY structure all flags are irrelevant
4789 * if the chain is invalid.
4790 */
4791 if (!check_flags)
4792 {
4793 if (rv & CERT_PKEY_VALID)
4794 cpk->valid_flags = rv;
4795 else
4796 {
4797 /* Preserve explicit sign flag, clear rest */
4798 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
4799 return 0;
4800 }
4801 }
4802 return rv;
4803 }
4804
4805/* Set validity of certificates in an SSL structure */
4806void tls1_set_cert_validity(SSL *s)
4807 {
4808 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4809 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4810 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4811 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4812 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4813 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4814 }
4815/* User level utiity function to check a chain is suitable */
4816int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4817 {
4818 return tls1_check_chain(s, x, pk, chain, -1);
4819 }
4820
4821#endif