2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include <openssl/ocsp.h>
117 #include <openssl/rand.h>
118 #include "ssl_locl.h"
120 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
122 #ifndef OPENSSL_NO_TLSEXT
123 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
124 const unsigned char *sess_id, int sesslen,
125 SSL_SESSION **psess);
126 static int ssl_check_clienthello_tlsext_early(SSL *s);
127 int ssl_check_serverhello_tlsext(SSL *s);
130 SSL3_ENC_METHOD TLSv1_enc_data={
133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
137 TLS1_FINISH_MAC_LENGTH,
138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
142 tls1_export_keying_material,
144 SSL3_HM_HEADER_LENGTH,
145 ssl3_set_handshake_header,
149 SSL3_ENC_METHOD TLSv1_1_enc_data={
152 tls1_setup_key_block,
153 tls1_generate_master_secret,
154 tls1_change_cipher_state,
155 tls1_final_finish_mac,
156 TLS1_FINISH_MAC_LENGTH,
157 tls1_cert_verify_mac,
158 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
159 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
161 tls1_export_keying_material,
162 SSL_ENC_FLAG_EXPLICIT_IV,
163 SSL3_HM_HEADER_LENGTH,
164 ssl3_set_handshake_header,
168 SSL3_ENC_METHOD TLSv1_2_enc_data={
171 tls1_setup_key_block,
172 tls1_generate_master_secret,
173 tls1_change_cipher_state,
174 tls1_final_finish_mac,
175 TLS1_FINISH_MAC_LENGTH,
176 tls1_cert_verify_mac,
177 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
178 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
180 tls1_export_keying_material,
181 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
182 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
183 SSL3_HM_HEADER_LENGTH,
184 ssl3_set_handshake_header,
188 long tls1_default_timeout(void)
190 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
191 * is way too long for http, the cache would over fill */
197 if (!ssl3_new(s)) return(0);
198 s->method->ssl_clear(s);
202 void tls1_free(SSL *s)
204 #ifndef OPENSSL_NO_TLSEXT
205 if (s->tlsext_session_ticket)
207 OPENSSL_free(s->tlsext_session_ticket);
209 #endif /* OPENSSL_NO_TLSEXT */
213 void tls1_clear(SSL *s)
216 s->version = s->method->version;
219 #ifndef OPENSSL_NO_EC
221 static int nid_list[] =
223 NID_sect163k1, /* sect163k1 (1) */
224 NID_sect163r1, /* sect163r1 (2) */
225 NID_sect163r2, /* sect163r2 (3) */
226 NID_sect193r1, /* sect193r1 (4) */
227 NID_sect193r2, /* sect193r2 (5) */
228 NID_sect233k1, /* sect233k1 (6) */
229 NID_sect233r1, /* sect233r1 (7) */
230 NID_sect239k1, /* sect239k1 (8) */
231 NID_sect283k1, /* sect283k1 (9) */
232 NID_sect283r1, /* sect283r1 (10) */
233 NID_sect409k1, /* sect409k1 (11) */
234 NID_sect409r1, /* sect409r1 (12) */
235 NID_sect571k1, /* sect571k1 (13) */
236 NID_sect571r1, /* sect571r1 (14) */
237 NID_secp160k1, /* secp160k1 (15) */
238 NID_secp160r1, /* secp160r1 (16) */
239 NID_secp160r2, /* secp160r2 (17) */
240 NID_secp192k1, /* secp192k1 (18) */
241 NID_X9_62_prime192v1, /* secp192r1 (19) */
242 NID_secp224k1, /* secp224k1 (20) */
243 NID_secp224r1, /* secp224r1 (21) */
244 NID_secp256k1, /* secp256k1 (22) */
245 NID_X9_62_prime256v1, /* secp256r1 (23) */
246 NID_secp384r1, /* secp384r1 (24) */
247 NID_secp521r1 /* secp521r1 (25) */
251 static const unsigned char ecformats_default[] =
253 TLSEXT_ECPOINTFORMAT_uncompressed,
254 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
255 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
258 static const unsigned char eccurves_default[] =
260 0,14, /* sect571r1 (14) */
261 0,13, /* sect571k1 (13) */
262 0,25, /* secp521r1 (25) */
263 0,11, /* sect409k1 (11) */
264 0,12, /* sect409r1 (12) */
265 0,24, /* secp384r1 (24) */
266 0,9, /* sect283k1 (9) */
267 0,10, /* sect283r1 (10) */
268 0,22, /* secp256k1 (22) */
269 0,23, /* secp256r1 (23) */
270 0,8, /* sect239k1 (8) */
271 0,6, /* sect233k1 (6) */
272 0,7, /* sect233r1 (7) */
273 0,20, /* secp224k1 (20) */
274 0,21, /* secp224r1 (21) */
275 0,4, /* sect193r1 (4) */
276 0,5, /* sect193r2 (5) */
277 0,18, /* secp192k1 (18) */
278 0,19, /* secp192r1 (19) */
279 0,1, /* sect163k1 (1) */
280 0,2, /* sect163r1 (2) */
281 0,3, /* sect163r2 (3) */
282 0,15, /* secp160k1 (15) */
283 0,16, /* secp160r1 (16) */
284 0,17, /* secp160r2 (17) */
287 static const unsigned char suiteb_curves[] =
289 0, TLSEXT_curve_P_256,
290 0, TLSEXT_curve_P_384
293 int tls1_ec_curve_id2nid(int curve_id)
295 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
296 if ((curve_id < 1) || ((unsigned int)curve_id >
297 sizeof(nid_list)/sizeof(nid_list[0])))
299 return nid_list[curve_id-1];
302 int tls1_ec_nid2curve_id(int nid)
304 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
307 case NID_sect163k1: /* sect163k1 (1) */
309 case NID_sect163r1: /* sect163r1 (2) */
311 case NID_sect163r2: /* sect163r2 (3) */
313 case NID_sect193r1: /* sect193r1 (4) */
315 case NID_sect193r2: /* sect193r2 (5) */
317 case NID_sect233k1: /* sect233k1 (6) */
319 case NID_sect233r1: /* sect233r1 (7) */
321 case NID_sect239k1: /* sect239k1 (8) */
323 case NID_sect283k1: /* sect283k1 (9) */
325 case NID_sect283r1: /* sect283r1 (10) */
327 case NID_sect409k1: /* sect409k1 (11) */
329 case NID_sect409r1: /* sect409r1 (12) */
331 case NID_sect571k1: /* sect571k1 (13) */
333 case NID_sect571r1: /* sect571r1 (14) */
335 case NID_secp160k1: /* secp160k1 (15) */
337 case NID_secp160r1: /* secp160r1 (16) */
339 case NID_secp160r2: /* secp160r2 (17) */
341 case NID_secp192k1: /* secp192k1 (18) */
343 case NID_X9_62_prime192v1: /* secp192r1 (19) */
345 case NID_secp224k1: /* secp224k1 (20) */
347 case NID_secp224r1: /* secp224r1 (21) */
349 case NID_secp256k1: /* secp256k1 (22) */
351 case NID_X9_62_prime256v1: /* secp256r1 (23) */
353 case NID_secp384r1: /* secp384r1 (24) */
355 case NID_secp521r1: /* secp521r1 (25) */
361 /* Get curves list, if "sess" is set return client curves otherwise
364 static void tls1_get_curvelist(SSL *s, int sess,
365 const unsigned char **pcurves,
370 *pcurves = s->session->tlsext_ellipticcurvelist;
371 *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
374 /* For Suite B mode only include P-256, P-384 */
375 switch (tls1_suiteb(s))
377 case SSL_CERT_FLAG_SUITEB_128_LOS:
378 *pcurves = suiteb_curves;
379 *pcurveslen = sizeof(suiteb_curves);
382 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
383 *pcurves = suiteb_curves;
387 case SSL_CERT_FLAG_SUITEB_192_LOS:
388 *pcurves = suiteb_curves + 2;
392 *pcurves = s->tlsext_ellipticcurvelist;
393 *pcurveslen = s->tlsext_ellipticcurvelist_length;
397 *pcurves = eccurves_default;
398 *pcurveslen = sizeof(eccurves_default);
401 /* Check a curve is one of our preferences */
402 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
404 const unsigned char *curves;
406 unsigned int suiteb_flags = tls1_suiteb(s);
407 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
409 /* Check curve matches Suite B preferences */
412 unsigned long cid = s->s3->tmp.new_cipher->id;
415 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
417 if (p[2] != TLSEXT_curve_P_256)
420 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
422 if (p[2] != TLSEXT_curve_P_384)
425 else /* Should never happen */
428 tls1_get_curvelist(s, 0, &curves, &curveslen);
429 for (i = 0; i < curveslen; i += 2, curves += 2)
431 if (p[1] == curves[0] && p[2] == curves[1])
437 /* Return nth shared curve. If nmatch == -1 return number of
438 * matches. For nmatch == -2 return the NID of the curve to use for
442 int tls1_shared_curve(SSL *s, int nmatch)
444 const unsigned char *pref, *supp;
445 size_t preflen, supplen, i, j;
447 /* Can't do anything on client side */
454 /* For Suite B ciphersuite determines curve: we
455 * already know these are acceptable due to previous
458 unsigned long cid = s->s3->tmp.new_cipher->id;
459 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
460 return NID_X9_62_prime256v1; /* P-256 */
461 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
462 return NID_secp384r1; /* P-384 */
463 /* Should never happen */
466 /* If not Suite B just return first preference shared curve */
469 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
471 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
476 for (i = 0; i < preflen; i++, pref+=2)
478 const unsigned char *tsupp = supp;
479 for (j = 0; j < supplen; j++, tsupp+=2)
481 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
485 int id = (pref[0] << 8) | pref[1];
486 return tls1_ec_curve_id2nid(id);
497 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
498 int *curves, size_t ncurves)
500 unsigned char *clist, *p;
502 /* Bitmap of curves included to detect duplicates: only works
503 * while curve ids < 32
505 unsigned long dup_list = 0;
506 clist = OPENSSL_malloc(ncurves * 2);
509 for (i = 0, p = clist; i < ncurves; i++)
511 unsigned long idmask;
513 id = tls1_ec_nid2curve_id(curves[i]);
515 if (!id || (dup_list & idmask))
526 *pextlen = ncurves * 2;
530 #define MAX_CURVELIST 25
535 int nid_arr[MAX_CURVELIST];
538 static int nid_cb(const char *elem, int len, void *arg)
540 nid_cb_st *narg = arg;
544 if (narg->nidcnt == MAX_CURVELIST)
546 if (len > (int)(sizeof(etmp) - 1))
548 memcpy(etmp, elem, len);
550 nid = EC_curve_nist2nid(etmp);
551 if (nid == NID_undef)
552 nid = OBJ_sn2nid(etmp);
553 if (nid == NID_undef)
554 nid = OBJ_ln2nid(etmp);
555 if (nid == NID_undef)
557 for (i = 0; i < narg->nidcnt; i++)
558 if (narg->nid_arr[i] == nid)
560 narg->nid_arr[narg->nidcnt++] = nid;
563 /* Set curves based on a colon separate list */
564 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
569 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
573 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
575 /* For an EC key set TLS id and required compression based on parameters */
576 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
582 const EC_METHOD *meth;
585 /* Determine if it is a prime field */
586 grp = EC_KEY_get0_group(ec);
587 pt = EC_KEY_get0_public_key(ec);
590 meth = EC_GROUP_method_of(grp);
593 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
597 /* Determine curve ID */
598 id = EC_GROUP_get_curve_name(grp);
599 id = tls1_ec_nid2curve_id(id);
600 /* If we have an ID set it, otherwise set arbitrary explicit curve */
604 curve_id[1] = (unsigned char)id;
616 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
619 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
621 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
624 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
628 /* Check an EC key is compatible with extensions */
629 static int tls1_check_ec_key(SSL *s,
630 unsigned char *curve_id, unsigned char *comp_id)
632 const unsigned char *p;
635 /* If point formats extension present check it, otherwise everything
636 * is supported (see RFC4492).
638 if (comp_id && s->session->tlsext_ecpointformatlist)
640 p = s->session->tlsext_ecpointformatlist;
641 plen = s->session->tlsext_ecpointformatlist_length;
642 for (i = 0; i < plen; i++, p++)
652 /* Check curve is consistent with client and server preferences */
653 for (j = 0; j <= 1; j++)
655 tls1_get_curvelist(s, j, &p, &plen);
656 for (i = 0; i < plen; i+=2, p+=2)
658 if (p[0] == curve_id[0] && p[1] == curve_id[1])
663 /* For clients can only check sent curve list */
670 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
673 /* If we have a custom point format list use it otherwise
675 if (s->tlsext_ecpointformatlist)
677 *pformats = s->tlsext_ecpointformatlist;
678 *pformatslen = s->tlsext_ecpointformatlist_length;
682 *pformats = ecformats_default;
683 /* For Suite B we don't support char2 fields */
685 *pformatslen = sizeof(ecformats_default) - 1;
687 *pformatslen = sizeof(ecformats_default);
691 /* Check cert parameters compatible with extensions: currently just checks
692 * EC certificates have compatible curves and compression.
694 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
696 unsigned char comp_id, curve_id[2];
699 pkey = X509_get_pubkey(x);
702 /* If not EC nothing to do */
703 if (pkey->type != EVP_PKEY_EC)
708 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
712 /* Can't check curve_id for client certs as we don't have a
713 * supported curves extension.
715 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
718 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
719 * SHA384+P-384, adjust digest if necessary.
721 if (set_ee_md && tls1_suiteb(s))
728 /* Check to see we have necessary signing algorithm */
729 if (curve_id[1] == TLSEXT_curve_P_256)
730 check_md = NID_ecdsa_with_SHA256;
731 else if (curve_id[1] == TLSEXT_curve_P_384)
732 check_md = NID_ecdsa_with_SHA384;
734 return 0; /* Should never happen */
735 for (i = 0; i < c->shared_sigalgslen; i++)
736 if (check_md == c->shared_sigalgs[i].signandhash_nid)
738 if (i == c->shared_sigalgslen)
742 if (check_md == NID_ecdsa_with_SHA256)
743 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
745 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
750 /* Check EC temporary key is compatible with client extensions */
751 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
753 unsigned char curve_id[2];
754 EC_KEY *ec = s->cert->ecdh_tmp;
755 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
756 /* Allow any curve: not just those peer supports */
757 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
760 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
761 * no other curves permitted.
765 /* Curve to check determined by ciphersuite */
766 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
767 curve_id[1] = TLSEXT_curve_P_256;
768 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
769 curve_id[1] = TLSEXT_curve_P_384;
773 /* Check this curve is acceptable */
774 if (!tls1_check_ec_key(s, curve_id, NULL))
776 /* If auto or setting curve from callback assume OK */
777 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
779 /* Otherwise check curve is acceptable */
782 unsigned char curve_tmp[2];
785 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
787 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
793 if (s->cert->ecdh_tmp_auto)
795 /* Need a shared curve */
796 if (tls1_shared_curve(s, 0))
802 if (s->cert->ecdh_tmp_cb)
807 if (!tls1_set_ec_id(curve_id, NULL, ec))
809 /* Set this to allow use of invalid curves for testing */
813 return tls1_check_ec_key(s, curve_id, NULL);
819 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
824 #endif /* OPENSSL_NO_EC */
826 #ifndef OPENSSL_NO_TLSEXT
828 /* List of supported signature algorithms and hashes. Should make this
829 * customisable at some point, for now include everything we support.
832 #ifdef OPENSSL_NO_RSA
833 #define tlsext_sigalg_rsa(md) /* */
835 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
838 #ifdef OPENSSL_NO_DSA
839 #define tlsext_sigalg_dsa(md) /* */
841 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
844 #ifdef OPENSSL_NO_ECDSA
845 #define tlsext_sigalg_ecdsa(md) /* */
847 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
850 #define tlsext_sigalg(md) \
851 tlsext_sigalg_rsa(md) \
852 tlsext_sigalg_dsa(md) \
853 tlsext_sigalg_ecdsa(md)
855 static unsigned char tls12_sigalgs[] = {
856 #ifndef OPENSSL_NO_SHA512
857 tlsext_sigalg(TLSEXT_hash_sha512)
858 tlsext_sigalg(TLSEXT_hash_sha384)
860 #ifndef OPENSSL_NO_SHA256
861 tlsext_sigalg(TLSEXT_hash_sha256)
862 tlsext_sigalg(TLSEXT_hash_sha224)
864 #ifndef OPENSSL_NO_SHA
865 tlsext_sigalg(TLSEXT_hash_sha1)
867 #ifndef OPENSSL_NO_MD5
868 tlsext_sigalg_rsa(TLSEXT_hash_md5)
871 #ifndef OPENSSL_NO_ECDSA
872 static unsigned char suiteb_sigalgs[] = {
873 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
874 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
877 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
879 /* If Suite B mode use Suite B sigalgs only, ignore any other
882 #ifndef OPENSSL_NO_EC
883 switch (tls1_suiteb(s))
885 case SSL_CERT_FLAG_SUITEB_128_LOS:
886 *psigs = suiteb_sigalgs;
887 return sizeof(suiteb_sigalgs);
889 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
890 *psigs = suiteb_sigalgs;
893 case SSL_CERT_FLAG_SUITEB_192_LOS:
894 *psigs = suiteb_sigalgs + 2;
898 /* If server use client authentication sigalgs if not NULL */
899 if (s->server && s->cert->client_sigalgs)
901 *psigs = s->cert->client_sigalgs;
902 return s->cert->client_sigalgslen;
904 else if (s->cert->conf_sigalgs)
906 *psigs = s->cert->conf_sigalgs;
907 return s->cert->conf_sigalgslen;
911 *psigs = tls12_sigalgs;
913 /* If FIPS mode don't include MD5 which is last */
915 return sizeof(tls12_sigalgs) - 2;
918 return sizeof(tls12_sigalgs);
921 /* Check signature algorithm is consistent with sent supported signature
922 * algorithms and if so return relevant digest.
924 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
925 const unsigned char *sig, EVP_PKEY *pkey)
927 const unsigned char *sent_sigs;
928 size_t sent_sigslen, i;
929 int sigalg = tls12_get_sigid(pkey);
930 /* Should never happen */
933 /* Check key type is consistent with signature */
934 if (sigalg != (int)sig[1])
936 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
939 #ifndef OPENSSL_NO_EC
940 if (pkey->type == EVP_PKEY_EC)
942 unsigned char curve_id[2], comp_id;
943 /* Check compression and curve matches extensions */
944 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
946 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
948 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
951 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
956 if (curve_id[1] == TLSEXT_curve_P_256)
958 if (sig[0] != TLSEXT_hash_sha256)
960 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
961 SSL_R_ILLEGAL_SUITEB_DIGEST);
965 else if (curve_id[1] == TLSEXT_curve_P_384)
967 if (sig[0] != TLSEXT_hash_sha384)
969 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
970 SSL_R_ILLEGAL_SUITEB_DIGEST);
978 else if (tls1_suiteb(s))
982 /* Check signature matches a type we sent */
983 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
984 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
986 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
989 /* Allow fallback to SHA1 if not strict mode */
990 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
992 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
995 *pmd = tls12_get_hash(sig[0]);
998 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
1001 /* Store the digest used so applications can retrieve it if they
1004 if (s->session && s->session->sess_cert)
1005 s->session->sess_cert->peer_key->digest = *pmd;
1008 /* Get a mask of disabled algorithms: an algorithm is disabled
1009 * if it isn't supported or doesn't appear in supported signature
1010 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1011 * session and not global settings.
1014 void ssl_set_client_disabled(SSL *s)
1017 const unsigned char *sigalgs;
1018 size_t i, sigalgslen;
1019 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1022 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1023 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1024 c->mask_ssl = SSL_TLSV1_2;
1027 /* Now go through all signature algorithms seeing if we support
1028 * any for RSA, DSA, ECDSA. Do this for all versions not just
1031 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
1032 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
1036 #ifndef OPENSSL_NO_RSA
1037 case TLSEXT_signature_rsa:
1041 #ifndef OPENSSL_NO_DSA
1042 case TLSEXT_signature_dsa:
1046 #ifndef OPENSSL_NO_ECDSA
1047 case TLSEXT_signature_ecdsa:
1053 /* Disable auth and static DH if we don't include any appropriate
1054 * signature algorithms.
1058 c->mask_a |= SSL_aRSA;
1059 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1063 c->mask_a |= SSL_aDSS;
1064 c->mask_k |= SSL_kDHd;
1068 c->mask_a |= SSL_aECDSA;
1069 c->mask_k |= SSL_kECDHe;
1071 #ifndef OPENSSL_NO_KRB5
1072 if (!kssl_tgt_is_available(s->kssl_ctx))
1074 c->mask_a |= SSL_aKRB5;
1075 c->mask_k |= SSL_kKRB5;
1078 #ifndef OPENSSL_NO_PSK
1079 /* with PSK there must be client callback set */
1080 if (!s->psk_client_callback)
1082 c->mask_a |= SSL_aPSK;
1083 c->mask_k |= SSL_kPSK;
1085 #endif /* OPENSSL_NO_PSK */
1089 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1092 unsigned char *ret = p;
1093 #ifndef OPENSSL_NO_EC
1094 /* See if we support any ECC ciphersuites */
1096 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1099 unsigned long alg_k, alg_a;
1100 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1102 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1104 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1106 alg_k = c->algorithm_mkey;
1107 alg_a = c->algorithm_auth;
1108 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
1109 || (alg_a & SSL_aECDSA)))
1118 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1119 if (s->client_version == SSL3_VERSION
1120 && !s->s3->send_connection_binding)
1125 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1127 if (s->tlsext_hostname != NULL)
1129 /* Add TLS extension servername to the Client Hello message */
1130 unsigned long size_str;
1133 /* check for enough space.
1134 4 for the servername type and entension length
1135 2 for servernamelist length
1136 1 for the hostname type
1137 2 for hostname length
1141 if ((lenmax = limit - ret - 9) < 0
1142 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1145 /* extension type and length */
1146 s2n(TLSEXT_TYPE_server_name,ret);
1147 s2n(size_str+5,ret);
1149 /* length of servername list */
1150 s2n(size_str+3,ret);
1152 /* hostname type, length and hostname */
1153 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1155 memcpy(ret, s->tlsext_hostname, size_str);
1159 /* Add RI if renegotiating */
1164 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1166 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1170 if((limit - p - 4 - el) < 0) return NULL;
1172 s2n(TLSEXT_TYPE_renegotiate,ret);
1175 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1177 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1184 #ifndef OPENSSL_NO_SRP
1185 /* Add SRP username if there is one */
1186 if (s->srp_ctx.login != NULL)
1187 { /* Add TLS extension SRP username to the Client Hello message */
1189 int login_len = strlen(s->srp_ctx.login);
1190 if (login_len > 255 || login_len == 0)
1192 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1196 /* check for enough space.
1197 4 for the srp type type and entension length
1198 1 for the srp user identity
1199 + srp user identity length
1201 if ((limit - ret - 5 - login_len) < 0) return NULL;
1203 /* fill in the extension */
1204 s2n(TLSEXT_TYPE_srp,ret);
1205 s2n(login_len+1,ret);
1206 (*ret++) = (unsigned char) login_len;
1207 memcpy(ret, s->srp_ctx.login, login_len);
1212 #ifndef OPENSSL_NO_EC
1215 /* Add TLS extension ECPointFormats to the ClientHello message */
1217 const unsigned char *plist;
1220 tls1_get_formatlist(s, &plist, &plistlen);
1222 if ((lenmax = limit - ret - 5) < 0) return NULL;
1223 if (plistlen > (size_t)lenmax) return NULL;
1226 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1230 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1231 s2n(plistlen + 1,ret);
1232 *(ret++) = (unsigned char)plistlen ;
1233 memcpy(ret, plist, plistlen);
1236 /* Add TLS extension EllipticCurves to the ClientHello message */
1237 plist = s->tlsext_ellipticcurvelist;
1238 tls1_get_curvelist(s, 0, &plist, &plistlen);
1240 if ((lenmax = limit - ret - 6) < 0) return NULL;
1241 if (plistlen > (size_t)lenmax) return NULL;
1242 if (plistlen > 65532)
1244 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1248 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1249 s2n(plistlen + 2, ret);
1251 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1252 * elliptic_curve_list, but the examples use two bytes.
1253 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1254 * resolves this to two bytes.
1257 memcpy(ret, plist, plistlen);
1260 #endif /* OPENSSL_NO_EC */
1262 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1265 if (!s->new_session && s->session && s->session->tlsext_tick)
1266 ticklen = s->session->tlsext_ticklen;
1267 else if (s->session && s->tlsext_session_ticket &&
1268 s->tlsext_session_ticket->data)
1270 ticklen = s->tlsext_session_ticket->length;
1271 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1272 if (!s->session->tlsext_tick)
1274 memcpy(s->session->tlsext_tick,
1275 s->tlsext_session_ticket->data,
1277 s->session->tlsext_ticklen = ticklen;
1281 if (ticklen == 0 && s->tlsext_session_ticket &&
1282 s->tlsext_session_ticket->data == NULL)
1284 /* Check for enough room 2 for extension type, 2 for len
1287 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1288 s2n(TLSEXT_TYPE_session_ticket,ret);
1292 memcpy(ret, s->session->tlsext_tick, ticklen);
1298 if (SSL_USE_SIGALGS(s))
1301 const unsigned char *salg;
1302 salglen = tls12_get_psigalgs(s, &salg);
1303 if ((size_t)(limit - ret) < salglen + 6)
1305 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1306 s2n(salglen + 2, ret);
1308 memcpy(ret, salg, salglen);
1312 #ifdef TLSEXT_TYPE_opaque_prf_input
1313 if (s->s3->client_opaque_prf_input != NULL)
1315 size_t col = s->s3->client_opaque_prf_input_len;
1317 if ((long)(limit - ret - 6 - col < 0))
1319 if (col > 0xFFFD) /* can't happen */
1322 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1325 memcpy(ret, s->s3->client_opaque_prf_input, col);
1330 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1333 long extlen, idlen, itmp;
1337 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1339 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1340 itmp = i2d_OCSP_RESPID(id, NULL);
1346 if (s->tlsext_ocsp_exts)
1348 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1355 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1356 s2n(TLSEXT_TYPE_status_request, ret);
1357 if (extlen + idlen > 0xFFF0)
1359 s2n(extlen + idlen + 5, ret);
1360 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1362 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1364 /* save position of id len */
1365 unsigned char *q = ret;
1366 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1367 /* skip over id len */
1369 itmp = i2d_OCSP_RESPID(id, &ret);
1375 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1378 #ifndef OPENSSL_NO_HEARTBEATS
1379 /* Add Heartbeat extension */
1380 s2n(TLSEXT_TYPE_heartbeat,ret);
1383 * 1: peer may send requests
1384 * 2: peer not allowed to send requests
1386 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1387 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1389 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1392 #ifndef OPENSSL_NO_NEXTPROTONEG
1393 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1395 /* The client advertises an emtpy extension to indicate its
1396 * support for Next Protocol Negotiation */
1397 if (limit - ret - 4 < 0)
1399 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1404 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1406 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1408 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1409 s2n(2 + s->alpn_client_proto_list_len,ret);
1410 s2n(s->alpn_client_proto_list_len,ret);
1411 memcpy(ret, s->alpn_client_proto_list,
1412 s->alpn_client_proto_list_len);
1413 ret += s->alpn_client_proto_list_len;
1416 if(SSL_get_srtp_profiles(s))
1420 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1422 if((limit - p - 4 - el) < 0) return NULL;
1424 s2n(TLSEXT_TYPE_use_srtp,ret);
1427 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1429 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1435 /* Add custom TLS Extensions to ClientHello */
1436 if (s->ctx->custom_cli_ext_records_count)
1439 custom_cli_ext_record* record;
1441 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
1443 const unsigned char* out = NULL;
1444 unsigned short outlen = 0;
1446 record = &s->ctx->custom_cli_ext_records[i];
1447 /* NULL callback sends empty extension */
1448 /* -1 from callback omits extension */
1452 cb_retval = record->fn1(s, record->ext_type,
1456 return NULL; /* error */
1457 if (cb_retval == -1)
1458 continue; /* skip this extension */
1460 if (limit < ret + 4 + outlen)
1462 s2n(record->ext_type, ret);
1464 memcpy(ret, out, outlen);
1469 if ((extdatalen = ret-p-2) == 0)
1476 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1479 unsigned char *ret = p;
1480 #ifndef OPENSSL_NO_NEXTPROTONEG
1481 int next_proto_neg_seen;
1483 #ifndef OPENSSL_NO_EC
1484 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1485 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1486 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1487 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1489 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1490 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1494 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1496 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1498 if ((long)(limit - ret - 4) < 0) return NULL;
1500 s2n(TLSEXT_TYPE_server_name,ret);
1504 if(s->s3->send_connection_binding)
1508 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1510 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1514 if((limit - p - 4 - el) < 0) return NULL;
1516 s2n(TLSEXT_TYPE_renegotiate,ret);
1519 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1521 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1528 #ifndef OPENSSL_NO_EC
1531 const unsigned char *plist;
1533 /* Add TLS extension ECPointFormats to the ServerHello message */
1536 tls1_get_formatlist(s, &plist, &plistlen);
1538 if ((lenmax = limit - ret - 5) < 0) return NULL;
1539 if (plistlen > (size_t)lenmax) return NULL;
1542 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1546 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1547 s2n(plistlen + 1,ret);
1548 *(ret++) = (unsigned char) plistlen;
1549 memcpy(ret, plist, plistlen);
1553 /* Currently the server should not respond with a SupportedCurves extension */
1554 #endif /* OPENSSL_NO_EC */
1556 if (s->tlsext_ticket_expected
1557 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1559 if ((long)(limit - ret - 4) < 0) return NULL;
1560 s2n(TLSEXT_TYPE_session_ticket,ret);
1564 if (s->tlsext_status_expected)
1566 if ((long)(limit - ret - 4) < 0) return NULL;
1567 s2n(TLSEXT_TYPE_status_request,ret);
1571 #ifdef TLSEXT_TYPE_opaque_prf_input
1572 if (s->s3->server_opaque_prf_input != NULL)
1574 size_t sol = s->s3->server_opaque_prf_input_len;
1576 if ((long)(limit - ret - 6 - sol) < 0)
1578 if (sol > 0xFFFD) /* can't happen */
1581 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1584 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1593 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1595 if((limit - p - 4 - el) < 0) return NULL;
1597 s2n(TLSEXT_TYPE_use_srtp,ret);
1600 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1602 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1608 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1609 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1610 { const unsigned char cryptopro_ext[36] = {
1611 0xfd, 0xe8, /*65000*/
1612 0x00, 0x20, /*32 bytes length*/
1613 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1614 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1615 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1616 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1617 if (limit-ret<36) return NULL;
1618 memcpy(ret,cryptopro_ext,36);
1623 #ifndef OPENSSL_NO_HEARTBEATS
1624 /* Add Heartbeat extension if we've received one */
1625 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1627 s2n(TLSEXT_TYPE_heartbeat,ret);
1630 * 1: peer may send requests
1631 * 2: peer not allowed to send requests
1633 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1634 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1636 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1641 #ifndef OPENSSL_NO_NEXTPROTONEG
1642 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1643 s->s3->next_proto_neg_seen = 0;
1644 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1646 const unsigned char *npa;
1647 unsigned int npalen;
1650 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1651 if (r == SSL_TLSEXT_ERR_OK)
1653 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1654 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1656 memcpy(ret, npa, npalen);
1658 s->s3->next_proto_neg_seen = 1;
1663 /* If custom types were sent in ClientHello, add ServerHello responses */
1664 if (s->s3->tlsext_custom_types_count)
1668 for (i = 0; i < s->s3->tlsext_custom_types_count; i++)
1671 custom_srv_ext_record *record;
1673 for (j = 0; j < s->ctx->custom_srv_ext_records_count; j++)
1675 record = &s->ctx->custom_srv_ext_records[j];
1676 if (s->s3->tlsext_custom_types[i] == record->ext_type)
1678 const unsigned char *out = NULL;
1679 unsigned short outlen = 0;
1682 /* NULL callback or -1 omits extension */
1685 cb_retval = record->fn2(s, record->ext_type,
1689 return NULL; /* error */
1690 if (cb_retval == -1)
1691 break; /* skip this extension */
1692 if (limit < ret + 4 + outlen)
1694 s2n(record->ext_type, ret);
1696 memcpy(ret, out, outlen);
1704 if (s->s3->alpn_selected)
1706 const unsigned char *selected = s->s3->alpn_selected;
1707 unsigned len = s->s3->alpn_selected_len;
1709 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1711 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1715 memcpy(ret, selected, len);
1719 if ((extdatalen = ret-p-2)== 0)
1726 /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1728 * data: the contents of the extension, not including the type and length.
1729 * data_len: the number of bytes in |data|
1730 * al: a pointer to the alert value to send in the event of a non-zero
1733 * returns: 0 on success. */
1734 static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
1735 unsigned data_len, int *al)
1739 const unsigned char *selected;
1740 unsigned char selected_len;
1743 if (s->ctx->alpn_select_cb == NULL)
1749 /* data should contain a uint16 length followed by a series of 8-bit,
1750 * length-prefixed strings. */
1751 i = ((unsigned) data[0]) << 8 |
1752 ((unsigned) data[1]);
1761 for (i = 0; i < data_len;)
1763 proto_len = data[i];
1769 if (i + proto_len < i || i + proto_len > data_len)
1775 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1776 s->ctx->alpn_select_cb_arg);
1777 if (r == SSL_TLSEXT_ERR_OK) {
1778 if (s->s3->alpn_selected)
1779 OPENSSL_free(s->s3->alpn_selected);
1780 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1781 if (!s->s3->alpn_selected)
1783 *al = SSL_AD_INTERNAL_ERROR;
1786 memcpy(s->s3->alpn_selected, selected, selected_len);
1787 s->s3->alpn_selected_len = selected_len;
1792 *al = SSL_AD_DECODE_ERROR;
1796 #ifndef OPENSSL_NO_EC
1797 /* ssl_check_for_safari attempts to fingerprint Safari using OS X
1798 * SecureTransport using the TLS extension block in |d|, of length |n|.
1799 * Safari, since 10.6, sends exactly these extensions, in this order:
1804 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1805 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1806 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1807 * 10.8..10.8.3 (which don't work).
1809 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
1810 unsigned short type, size;
1811 static const unsigned char kSafariExtensionsBlock[] = {
1812 0x00, 0x0a, /* elliptic_curves extension */
1813 0x00, 0x08, /* 8 bytes */
1814 0x00, 0x06, /* 6 bytes of curve ids */
1815 0x00, 0x17, /* P-256 */
1816 0x00, 0x18, /* P-384 */
1817 0x00, 0x19, /* P-521 */
1819 0x00, 0x0b, /* ec_point_formats */
1820 0x00, 0x02, /* 2 bytes */
1821 0x01, /* 1 point format */
1822 0x00, /* uncompressed */
1825 /* The following is only present in TLS 1.2 */
1826 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1827 0x00, 0x0d, /* signature_algorithms */
1828 0x00, 0x0c, /* 12 bytes */
1829 0x00, 0x0a, /* 10 bytes */
1830 0x05, 0x01, /* SHA-384/RSA */
1831 0x04, 0x01, /* SHA-256/RSA */
1832 0x02, 0x01, /* SHA-1/RSA */
1833 0x04, 0x03, /* SHA-256/ECDSA */
1834 0x02, 0x03, /* SHA-1/ECDSA */
1837 if (data >= (d+n-2))
1846 if (type != TLSEXT_TYPE_server_name)
1849 if (data+size > d+n)
1853 if (TLS1_get_version(s) >= TLS1_2_VERSION)
1855 const size_t len1 = sizeof(kSafariExtensionsBlock);
1856 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1858 if (data + len1 + len2 != d+n)
1860 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1862 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1867 const size_t len = sizeof(kSafariExtensionsBlock);
1869 if (data + len != d+n)
1871 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
1875 s->s3->is_probably_safari = 1;
1877 #endif /* OPENSSL_NO_EC */
1879 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1881 unsigned short type;
1882 unsigned short size;
1884 unsigned char *data = *p;
1885 int renegotiate_seen = 0;
1888 s->servername_done = 0;
1889 s->tlsext_status_type = -1;
1890 #ifndef OPENSSL_NO_NEXTPROTONEG
1891 s->s3->next_proto_neg_seen = 0;
1894 if (s->s3->alpn_selected)
1896 OPENSSL_free(s->s3->alpn_selected);
1897 s->s3->alpn_selected = NULL;
1900 /* Clear observed custom extensions */
1901 s->s3->tlsext_custom_types_count = 0;
1902 if (s->s3->tlsext_custom_types != NULL)
1904 OPENSSL_free(s->s3->tlsext_custom_types);
1905 s->s3->tlsext_custom_types = NULL;
1908 #ifndef OPENSSL_NO_HEARTBEATS
1909 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1910 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1913 #ifndef OPENSSL_NO_EC
1914 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1915 ssl_check_for_safari(s, data, d, n);
1916 #endif /* OPENSSL_NO_EC */
1918 /* Clear any signature algorithms extension received */
1919 if (s->cert->peer_sigalgs)
1921 OPENSSL_free(s->cert->peer_sigalgs);
1922 s->cert->peer_sigalgs = NULL;
1924 /* Clear any shared sigtnature algorithms */
1925 if (s->cert->shared_sigalgs)
1927 OPENSSL_free(s->cert->shared_sigalgs);
1928 s->cert->shared_sigalgs = NULL;
1930 /* Clear certificate digests and validity flags */
1931 for (i = 0; i < SSL_PKEY_NUM; i++)
1933 s->cert->pkeys[i].digest = NULL;
1934 s->cert->pkeys[i].valid_flags = 0;
1937 if (data >= (d+n-2))
1941 if (data > (d+n-len))
1944 while (data <= (d+n-4))
1949 if (data+size > (d+n))
1952 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1954 if (s->tlsext_debug_cb)
1955 s->tlsext_debug_cb(s, 0, type, data, size,
1956 s->tlsext_debug_arg);
1957 /* The servername extension is treated as follows:
1959 - Only the hostname type is supported with a maximum length of 255.
1960 - The servername is rejected if too long or if it contains zeros,
1961 in which case an fatal alert is generated.
1962 - The servername field is maintained together with the session cache.
1963 - When a session is resumed, the servername call back invoked in order
1964 to allow the application to position itself to the right context.
1965 - The servername is acknowledged if it is new for a session or when
1966 it is identical to a previously used for the same session.
1967 Applications can control the behaviour. They can at any time
1968 set a 'desirable' servername for a new SSL object. This can be the
1969 case for example with HTTPS when a Host: header field is received and
1970 a renegotiation is requested. In this case, a possible servername
1971 presented in the new client hello is only acknowledged if it matches
1972 the value of the Host: field.
1973 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1974 if they provide for changing an explicit servername context for the session,
1975 i.e. when the session has been established with a servername extension.
1976 - On session reconnect, the servername extension may be absent.
1980 if (type == TLSEXT_TYPE_server_name)
1982 unsigned char *sdata;
1988 *al = SSL_AD_DECODE_ERROR;
1995 *al = SSL_AD_DECODE_ERROR;
2002 servname_type = *(sdata++);
2008 *al = SSL_AD_DECODE_ERROR;
2011 if (s->servername_done == 0)
2012 switch (servname_type)
2014 case TLSEXT_NAMETYPE_host_name:
2017 if(s->session->tlsext_hostname)
2019 *al = SSL_AD_DECODE_ERROR;
2022 if (len > TLSEXT_MAXLEN_host_name)
2024 *al = TLS1_AD_UNRECOGNIZED_NAME;
2027 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
2029 *al = TLS1_AD_INTERNAL_ERROR;
2032 memcpy(s->session->tlsext_hostname, sdata, len);
2033 s->session->tlsext_hostname[len]='\0';
2034 if (strlen(s->session->tlsext_hostname) != len) {
2035 OPENSSL_free(s->session->tlsext_hostname);
2036 s->session->tlsext_hostname = NULL;
2037 *al = TLS1_AD_UNRECOGNIZED_NAME;
2040 s->servername_done = 1;
2044 s->servername_done = s->session->tlsext_hostname
2045 && strlen(s->session->tlsext_hostname) == len
2046 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2058 *al = SSL_AD_DECODE_ERROR;
2063 #ifndef OPENSSL_NO_SRP
2064 else if (type == TLSEXT_TYPE_srp)
2066 if (size <= 0 || ((len = data[0])) != (size -1))
2068 *al = SSL_AD_DECODE_ERROR;
2071 if (s->srp_ctx.login != NULL)
2073 *al = SSL_AD_DECODE_ERROR;
2076 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
2078 memcpy(s->srp_ctx.login, &data[1], len);
2079 s->srp_ctx.login[len]='\0';
2081 if (strlen(s->srp_ctx.login) != len)
2083 *al = SSL_AD_DECODE_ERROR;
2089 #ifndef OPENSSL_NO_EC
2090 else if (type == TLSEXT_TYPE_ec_point_formats)
2092 unsigned char *sdata = data;
2093 int ecpointformatlist_length = *(sdata++);
2095 if (ecpointformatlist_length != size - 1 ||
2096 ecpointformatlist_length < 1)
2098 *al = TLS1_AD_DECODE_ERROR;
2103 if(s->session->tlsext_ecpointformatlist)
2105 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2106 s->session->tlsext_ecpointformatlist = NULL;
2108 s->session->tlsext_ecpointformatlist_length = 0;
2109 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2111 *al = TLS1_AD_INTERNAL_ERROR;
2114 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2115 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2118 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2119 sdata = s->session->tlsext_ecpointformatlist;
2120 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2121 fprintf(stderr,"%i ",*(sdata++));
2122 fprintf(stderr,"\n");
2125 else if (type == TLSEXT_TYPE_elliptic_curves)
2127 unsigned char *sdata = data;
2128 int ellipticcurvelist_length = (*(sdata++) << 8);
2129 ellipticcurvelist_length += (*(sdata++));
2131 if (ellipticcurvelist_length != size - 2 ||
2132 ellipticcurvelist_length < 1)
2134 *al = TLS1_AD_DECODE_ERROR;
2139 if(s->session->tlsext_ellipticcurvelist)
2141 *al = TLS1_AD_DECODE_ERROR;
2144 s->session->tlsext_ellipticcurvelist_length = 0;
2145 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2147 *al = TLS1_AD_INTERNAL_ERROR;
2150 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2151 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2154 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2155 sdata = s->session->tlsext_ellipticcurvelist;
2156 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2157 fprintf(stderr,"%i ",*(sdata++));
2158 fprintf(stderr,"\n");
2161 #endif /* OPENSSL_NO_EC */
2162 #ifdef TLSEXT_TYPE_opaque_prf_input
2163 else if (type == TLSEXT_TYPE_opaque_prf_input)
2165 unsigned char *sdata = data;
2169 *al = SSL_AD_DECODE_ERROR;
2172 n2s(sdata, s->s3->client_opaque_prf_input_len);
2173 if (s->s3->client_opaque_prf_input_len != size - 2)
2175 *al = SSL_AD_DECODE_ERROR;
2179 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2180 OPENSSL_free(s->s3->client_opaque_prf_input);
2181 if (s->s3->client_opaque_prf_input_len == 0)
2182 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2184 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2185 if (s->s3->client_opaque_prf_input == NULL)
2187 *al = TLS1_AD_INTERNAL_ERROR;
2192 else if (type == TLSEXT_TYPE_session_ticket)
2194 if (s->tls_session_ticket_ext_cb &&
2195 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2197 *al = TLS1_AD_INTERNAL_ERROR;
2201 else if (type == TLSEXT_TYPE_renegotiate)
2203 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2205 renegotiate_seen = 1;
2207 else if (type == TLSEXT_TYPE_signature_algorithms)
2210 if (s->cert->peer_sigalgs || size < 2)
2212 *al = SSL_AD_DECODE_ERROR;
2217 if (dsize != size || dsize & 1 || !dsize)
2219 *al = SSL_AD_DECODE_ERROR;
2222 if (!tls1_process_sigalgs(s, data, dsize))
2224 *al = SSL_AD_DECODE_ERROR;
2227 /* If sigalgs received and no shared algorithms fatal
2230 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
2232 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2233 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2234 *al = SSL_AD_ILLEGAL_PARAMETER;
2238 else if (type == TLSEXT_TYPE_status_request
2239 && s->ctx->tlsext_status_cb)
2244 *al = SSL_AD_DECODE_ERROR;
2248 s->tlsext_status_type = *data++;
2250 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2252 const unsigned char *sdata;
2254 /* Read in responder_id_list */
2259 *al = SSL_AD_DECODE_ERROR;
2268 *al = SSL_AD_DECODE_ERROR;
2272 dsize -= 2 + idsize;
2276 *al = SSL_AD_DECODE_ERROR;
2281 id = d2i_OCSP_RESPID(NULL,
2285 *al = SSL_AD_DECODE_ERROR;
2290 OCSP_RESPID_free(id);
2291 *al = SSL_AD_DECODE_ERROR;
2294 if (!s->tlsext_ocsp_ids
2295 && !(s->tlsext_ocsp_ids =
2296 sk_OCSP_RESPID_new_null()))
2298 OCSP_RESPID_free(id);
2299 *al = SSL_AD_INTERNAL_ERROR;
2302 if (!sk_OCSP_RESPID_push(
2303 s->tlsext_ocsp_ids, id))
2305 OCSP_RESPID_free(id);
2306 *al = SSL_AD_INTERNAL_ERROR;
2311 /* Read in request_extensions */
2314 *al = SSL_AD_DECODE_ERROR;
2321 *al = SSL_AD_DECODE_ERROR;
2327 if (s->tlsext_ocsp_exts)
2329 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2330 X509_EXTENSION_free);
2333 s->tlsext_ocsp_exts =
2334 d2i_X509_EXTENSIONS(NULL,
2336 if (!s->tlsext_ocsp_exts
2337 || (data + dsize != sdata))
2339 *al = SSL_AD_DECODE_ERROR;
2344 /* We don't know what to do with any other type
2348 s->tlsext_status_type = -1;
2350 #ifndef OPENSSL_NO_HEARTBEATS
2351 else if (type == TLSEXT_TYPE_heartbeat)
2355 case 0x01: /* Client allows us to send HB requests */
2356 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2358 case 0x02: /* Client doesn't accept HB requests */
2359 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2360 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2362 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2367 #ifndef OPENSSL_NO_NEXTPROTONEG
2368 else if (type == TLSEXT_TYPE_next_proto_neg &&
2369 s->s3->tmp.finish_md_len == 0 &&
2370 s->s3->alpn_selected == NULL)
2372 /* We shouldn't accept this extension on a
2375 * s->new_session will be set on renegotiation, but we
2376 * probably shouldn't rely that it couldn't be set on
2377 * the initial renegotation too in certain cases (when
2378 * there's some other reason to disallow resuming an
2379 * earlier session -- the current code won't be doing
2380 * anything like that, but this might change).
2382 * A valid sign that there's been a previous handshake
2383 * in this connection is if s->s3->tmp.finish_md_len >
2384 * 0. (We are talking about a check that will happen
2385 * in the Hello protocol round, well before a new
2386 * Finished message could have been computed.) */
2387 s->s3->next_proto_neg_seen = 1;
2391 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2392 s->ctx->alpn_select_cb &&
2393 s->s3->tmp.finish_md_len == 0)
2395 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2397 /* ALPN takes precedence over NPN. */
2398 s->s3->next_proto_neg_seen = 0;
2401 /* session ticket processed earlier */
2402 else if (type == TLSEXT_TYPE_use_srtp)
2404 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2408 /* If this ClientHello extension was unhandled and this is
2409 * a nonresumed connection, check whether the extension is a
2410 * custom TLS Extension (has a custom_srv_ext_record), and if
2411 * so call the callback and record the extension number so that
2412 * an appropriate ServerHello may be later returned.
2414 else if (!s->hit && s->ctx->custom_srv_ext_records_count)
2416 custom_srv_ext_record *record;
2418 for (i=0; i < s->ctx->custom_srv_ext_records_count; i++)
2420 record = &s->ctx->custom_srv_ext_records[i];
2421 if (type == record->ext_type)
2425 /* Error on duplicate TLS Extensions */
2426 for (j = 0; j < s->s3->tlsext_custom_types_count; j++)
2428 if (type == s->s3->tlsext_custom_types[j])
2430 *al = TLS1_AD_DECODE_ERROR;
2435 /* NULL callback still notes the extension */
2436 if (record->fn1 && !record->fn1(s, type, data, size, al, record->arg))
2439 /* Add the (non-duplicated) entry */
2440 s->s3->tlsext_custom_types_count++;
2441 s->s3->tlsext_custom_types = OPENSSL_realloc(
2442 s->s3->tlsext_custom_types,
2443 s->s3->tlsext_custom_types_count * 2);
2444 if (s->s3->tlsext_custom_types == NULL)
2446 s->s3->tlsext_custom_types = 0;
2447 *al = TLS1_AD_INTERNAL_ERROR;
2450 s->s3->tlsext_custom_types[
2451 s->s3->tlsext_custom_types_count - 1] = type;
2463 /* Need RI if renegotiating */
2465 if (!renegotiate_seen && s->renegotiate &&
2466 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2468 *al = SSL_AD_HANDSHAKE_FAILURE;
2469 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2470 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2473 /* If no signature algorithms extension set default values */
2474 if (!s->cert->peer_sigalgs)
2475 ssl_cert_set_default_md(s->cert);
2480 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2483 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2485 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2489 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2491 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2497 #ifndef OPENSSL_NO_NEXTPROTONEG
2498 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2499 * elements of zero length are allowed and the set of elements must exactly fill
2500 * the length of the block. */
2501 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2503 unsigned int off = 0;
2517 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2519 unsigned short length;
2520 unsigned short type;
2521 unsigned short size;
2522 unsigned char *data = *p;
2523 int tlsext_servername = 0;
2524 int renegotiate_seen = 0;
2526 #ifndef OPENSSL_NO_NEXTPROTONEG
2527 s->s3->next_proto_neg_seen = 0;
2530 if (s->s3->alpn_selected)
2532 OPENSSL_free(s->s3->alpn_selected);
2533 s->s3->alpn_selected = NULL;
2536 #ifndef OPENSSL_NO_HEARTBEATS
2537 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2538 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2541 if (data >= (d+n-2))
2545 if (data+length != d+n)
2547 *al = SSL_AD_DECODE_ERROR;
2551 while(data <= (d+n-4))
2556 if (data+size > (d+n))
2559 if (s->tlsext_debug_cb)
2560 s->tlsext_debug_cb(s, 1, type, data, size,
2561 s->tlsext_debug_arg);
2563 if (type == TLSEXT_TYPE_server_name)
2565 if (s->tlsext_hostname == NULL || size > 0)
2567 *al = TLS1_AD_UNRECOGNIZED_NAME;
2570 tlsext_servername = 1;
2573 #ifndef OPENSSL_NO_EC
2574 else if (type == TLSEXT_TYPE_ec_point_formats)
2576 unsigned char *sdata = data;
2577 int ecpointformatlist_length = *(sdata++);
2579 if (ecpointformatlist_length != size - 1)
2581 *al = TLS1_AD_DECODE_ERROR;
2584 s->session->tlsext_ecpointformatlist_length = 0;
2585 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2586 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2588 *al = TLS1_AD_INTERNAL_ERROR;
2591 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2592 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2594 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2595 sdata = s->session->tlsext_ecpointformatlist;
2596 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2597 fprintf(stderr,"%i ",*(sdata++));
2598 fprintf(stderr,"\n");
2601 #endif /* OPENSSL_NO_EC */
2603 else if (type == TLSEXT_TYPE_session_ticket)
2605 if (s->tls_session_ticket_ext_cb &&
2606 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2608 *al = TLS1_AD_INTERNAL_ERROR;
2611 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2614 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2617 s->tlsext_ticket_expected = 1;
2619 #ifdef TLSEXT_TYPE_opaque_prf_input
2620 else if (type == TLSEXT_TYPE_opaque_prf_input)
2622 unsigned char *sdata = data;
2626 *al = SSL_AD_DECODE_ERROR;
2629 n2s(sdata, s->s3->server_opaque_prf_input_len);
2630 if (s->s3->server_opaque_prf_input_len != size - 2)
2632 *al = SSL_AD_DECODE_ERROR;
2636 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2637 OPENSSL_free(s->s3->server_opaque_prf_input);
2638 if (s->s3->server_opaque_prf_input_len == 0)
2639 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2641 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2643 if (s->s3->server_opaque_prf_input == NULL)
2645 *al = TLS1_AD_INTERNAL_ERROR;
2650 else if (type == TLSEXT_TYPE_status_request)
2652 /* MUST be empty and only sent if we've requested
2653 * a status request message.
2655 if ((s->tlsext_status_type == -1) || (size > 0))
2657 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2660 /* Set flag to expect CertificateStatus message */
2661 s->tlsext_status_expected = 1;
2663 #ifndef OPENSSL_NO_NEXTPROTONEG
2664 else if (type == TLSEXT_TYPE_next_proto_neg &&
2665 s->s3->tmp.finish_md_len == 0)
2667 unsigned char *selected;
2668 unsigned char selected_len;
2670 /* We must have requested it. */
2671 if (s->ctx->next_proto_select_cb == NULL)
2673 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2676 /* The data must be valid */
2677 if (!ssl_next_proto_validate(data, size))
2679 *al = TLS1_AD_DECODE_ERROR;
2682 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2684 *al = TLS1_AD_INTERNAL_ERROR;
2687 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2688 if (!s->next_proto_negotiated)
2690 *al = TLS1_AD_INTERNAL_ERROR;
2693 memcpy(s->next_proto_negotiated, selected, selected_len);
2694 s->next_proto_negotiated_len = selected_len;
2695 s->s3->next_proto_neg_seen = 1;
2699 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2703 /* We must have requested it. */
2704 if (s->alpn_client_proto_list == NULL)
2706 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2711 *al = TLS1_AD_DECODE_ERROR;
2714 /* The extension data consists of:
2715 * uint16 list_length
2716 * uint8 proto_length;
2717 * uint8 proto[proto_length]; */
2721 if (len != (unsigned) size - 2)
2723 *al = TLS1_AD_DECODE_ERROR;
2727 if (len != (unsigned) size - 3)
2729 *al = TLS1_AD_DECODE_ERROR;
2732 if (s->s3->alpn_selected)
2733 OPENSSL_free(s->s3->alpn_selected);
2734 s->s3->alpn_selected = OPENSSL_malloc(len);
2735 if (!s->s3->alpn_selected)
2737 *al = TLS1_AD_INTERNAL_ERROR;
2740 memcpy(s->s3->alpn_selected, data + 3, len);
2741 s->s3->alpn_selected_len = len;
2744 else if (type == TLSEXT_TYPE_renegotiate)
2746 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2748 renegotiate_seen = 1;
2750 #ifndef OPENSSL_NO_HEARTBEATS
2751 else if (type == TLSEXT_TYPE_heartbeat)
2755 case 0x01: /* Server allows us to send HB requests */
2756 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2758 case 0x02: /* Server doesn't accept HB requests */
2759 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2760 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2762 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2767 else if (type == TLSEXT_TYPE_use_srtp)
2769 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2773 /* If this extension type was not otherwise handled, but
2774 * matches a custom_cli_ext_record, then send it to the c
2776 else if (s->ctx->custom_cli_ext_records_count)
2779 custom_cli_ext_record* record;
2781 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
2783 record = &s->ctx->custom_cli_ext_records[i];
2784 if (record->ext_type == type)
2786 if (record->fn2 && !record->fn2(s, type, data, size, al, record->arg))
2798 *al = SSL_AD_DECODE_ERROR;
2802 if (!s->hit && tlsext_servername == 1)
2804 if (s->tlsext_hostname)
2806 if (s->session->tlsext_hostname == NULL)
2808 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2809 if (!s->session->tlsext_hostname)
2811 *al = SSL_AD_UNRECOGNIZED_NAME;
2817 *al = SSL_AD_DECODE_ERROR;
2827 /* Determine if we need to see RI. Strictly speaking if we want to
2828 * avoid an attack we should *always* see RI even on initial server
2829 * hello because the client doesn't see any renegotiation during an
2830 * attack. However this would mean we could not connect to any server
2831 * which doesn't support RI so for the immediate future tolerate RI
2832 * absence on initial connect only.
2834 if (!renegotiate_seen
2835 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2836 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2838 *al = SSL_AD_HANDSHAKE_FAILURE;
2839 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2840 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2848 int ssl_prepare_clienthello_tlsext(SSL *s)
2851 #ifdef TLSEXT_TYPE_opaque_prf_input
2855 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2857 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2862 if (s->tlsext_opaque_prf_input != NULL)
2864 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2865 OPENSSL_free(s->s3->client_opaque_prf_input);
2867 if (s->tlsext_opaque_prf_input_len == 0)
2868 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2870 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2871 if (s->s3->client_opaque_prf_input == NULL)
2873 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2876 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2880 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2881 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2888 int ssl_prepare_serverhello_tlsext(SSL *s)
2893 static int ssl_check_clienthello_tlsext_early(SSL *s)
2895 int ret=SSL_TLSEXT_ERR_NOACK;
2896 int al = SSL_AD_UNRECOGNIZED_NAME;
2898 #ifndef OPENSSL_NO_EC
2899 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2900 * ssl3_choose_cipher in s3_lib.c.
2902 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2903 * ssl3_choose_cipher in s3_lib.c.
2907 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2908 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2909 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2910 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2912 #ifdef TLSEXT_TYPE_opaque_prf_input
2914 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2915 * but we might be sending an alert in response to the client hello,
2916 * so this has to happen here in
2917 * ssl_check_clienthello_tlsext_early(). */
2921 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2923 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2926 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2927 al = SSL_AD_INTERNAL_ERROR;
2932 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2933 OPENSSL_free(s->s3->server_opaque_prf_input);
2934 s->s3->server_opaque_prf_input = NULL;
2936 if (s->tlsext_opaque_prf_input != NULL)
2938 if (s->s3->client_opaque_prf_input != NULL &&
2939 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2941 /* can only use this extension if we have a server opaque PRF input
2942 * of the same length as the client opaque PRF input! */
2944 if (s->tlsext_opaque_prf_input_len == 0)
2945 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2947 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2948 if (s->s3->server_opaque_prf_input == NULL)
2950 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2951 al = SSL_AD_INTERNAL_ERROR;
2954 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2958 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2960 /* The callback wants to enforce use of the extension,
2961 * but we can't do that with the client opaque PRF input;
2962 * abort the handshake.
2964 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2965 al = SSL_AD_HANDSHAKE_FAILURE;
2973 case SSL_TLSEXT_ERR_ALERT_FATAL:
2974 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2977 case SSL_TLSEXT_ERR_ALERT_WARNING:
2978 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2981 case SSL_TLSEXT_ERR_NOACK:
2982 s->servername_done=0;
2988 int ssl_check_clienthello_tlsext_late(SSL *s)
2990 int ret = SSL_TLSEXT_ERR_OK;
2993 /* If status request then ask callback what to do.
2994 * Note: this must be called after servername callbacks in case
2995 * the certificate has changed, and must be called after the cipher
2996 * has been chosen because this may influence which certificate is sent
2998 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
3001 CERT_PKEY *certpkey;
3002 certpkey = ssl_get_server_send_pkey(s);
3003 /* If no certificate can't return certificate status */
3004 if (certpkey == NULL)
3006 s->tlsext_status_expected = 0;
3009 /* Set current certificate to one we will use so
3010 * SSL_get_certificate et al can pick it up.
3012 s->cert->key = certpkey;
3013 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3016 /* We don't want to send a status request response */
3017 case SSL_TLSEXT_ERR_NOACK:
3018 s->tlsext_status_expected = 0;
3020 /* status request response should be sent */
3021 case SSL_TLSEXT_ERR_OK:
3022 if (s->tlsext_ocsp_resp)
3023 s->tlsext_status_expected = 1;
3025 s->tlsext_status_expected = 0;
3027 /* something bad happened */
3028 case SSL_TLSEXT_ERR_ALERT_FATAL:
3029 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3030 al = SSL_AD_INTERNAL_ERROR;
3035 s->tlsext_status_expected = 0;
3040 case SSL_TLSEXT_ERR_ALERT_FATAL:
3041 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3044 case SSL_TLSEXT_ERR_ALERT_WARNING:
3045 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3053 int ssl_check_serverhello_tlsext(SSL *s)
3055 int ret=SSL_TLSEXT_ERR_NOACK;
3056 int al = SSL_AD_UNRECOGNIZED_NAME;
3058 #ifndef OPENSSL_NO_EC
3059 /* If we are client and using an elliptic curve cryptography cipher
3060 * suite, then if server returns an EC point formats lists extension
3061 * it must contain uncompressed.
3063 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3064 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3065 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3066 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3067 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3069 /* we are using an ECC cipher */
3071 unsigned char *list;
3072 int found_uncompressed = 0;
3073 list = s->session->tlsext_ecpointformatlist;
3074 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3076 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3078 found_uncompressed = 1;
3082 if (!found_uncompressed)
3084 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3088 ret = SSL_TLSEXT_ERR_OK;
3089 #endif /* OPENSSL_NO_EC */
3091 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3092 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3093 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3094 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3096 #ifdef TLSEXT_TYPE_opaque_prf_input
3097 if (s->s3->server_opaque_prf_input_len > 0)
3099 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
3100 * So first verify that we really have a value from the server too. */
3102 if (s->s3->server_opaque_prf_input == NULL)
3104 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3105 al = SSL_AD_HANDSHAKE_FAILURE;
3108 /* Anytime the server *has* sent an opaque PRF input, we need to check
3109 * that we have a client opaque PRF input of the same size. */
3110 if (s->s3->client_opaque_prf_input == NULL ||
3111 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
3113 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3114 al = SSL_AD_ILLEGAL_PARAMETER;
3119 /* If we've requested certificate status and we wont get one
3122 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3123 && s->ctx && s->ctx->tlsext_status_cb)
3126 /* Set resp to NULL, resplen to -1 so callback knows
3127 * there is no response.
3129 if (s->tlsext_ocsp_resp)
3131 OPENSSL_free(s->tlsext_ocsp_resp);
3132 s->tlsext_ocsp_resp = NULL;
3134 s->tlsext_ocsp_resplen = -1;
3135 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3138 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3139 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3143 al = SSL_AD_INTERNAL_ERROR;
3144 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3150 case SSL_TLSEXT_ERR_ALERT_FATAL:
3151 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3154 case SSL_TLSEXT_ERR_ALERT_WARNING:
3155 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3158 case SSL_TLSEXT_ERR_NOACK:
3159 s->servername_done=0;
3165 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
3168 if (s->version < SSL3_VERSION)
3170 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0)
3172 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3176 if (ssl_check_serverhello_tlsext(s) <= 0)
3178 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,SSL_R_SERVERHELLO_TLSEXT);
3184 /* Since the server cache lookup is done early on in the processing of the
3185 * ClientHello, and other operations depend on the result, we need to handle
3186 * any TLS session ticket extension at the same time.
3188 * session_id: points at the session ID in the ClientHello. This code will
3189 * read past the end of this in order to parse out the session ticket
3190 * extension, if any.
3191 * len: the length of the session ID.
3192 * limit: a pointer to the first byte after the ClientHello.
3193 * ret: (output) on return, if a ticket was decrypted, then this is set to
3194 * point to the resulting session.
3196 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3197 * ciphersuite, in which case we have no use for session tickets and one will
3198 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3201 * -1: fatal error, either from parsing or decrypting the ticket.
3202 * 0: no ticket was found (or was ignored, based on settings).
3203 * 1: a zero length extension was found, indicating that the client supports
3204 * session tickets but doesn't currently have one to offer.
3205 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
3206 * couldn't be decrypted because of a non-fatal error.
3207 * 3: a ticket was successfully decrypted and *ret was set.
3210 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3211 * a new session ticket to the client because the client indicated support
3212 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3213 * a session ticket or we couldn't use the one it gave us, or if
3214 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3215 * Otherwise, s->tlsext_ticket_expected is set to 0.
3217 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
3218 const unsigned char *limit, SSL_SESSION **ret)
3220 /* Point after session ID in client hello */
3221 const unsigned char *p = session_id + len;
3225 s->tlsext_ticket_expected = 0;
3227 /* If tickets disabled behave as if no ticket present
3228 * to permit stateful resumption.
3230 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
3232 if ((s->version <= SSL3_VERSION) || !limit)
3236 /* Skip past DTLS cookie */
3244 /* Skip past cipher list */
3249 /* Skip past compression algorithm list */
3254 /* Now at start of extensions */
3255 if ((p + 2) >= limit)
3258 while ((p + 4) <= limit)
3260 unsigned short type, size;
3263 if (p + size > limit)
3265 if (type == TLSEXT_TYPE_session_ticket)
3270 /* The client will accept a ticket but doesn't
3271 * currently have one. */
3272 s->tlsext_ticket_expected = 1;
3275 if (s->tls_session_secret_cb)
3277 /* Indicate that the ticket couldn't be
3278 * decrypted rather than generating the session
3279 * from ticket now, trigger abbreviated
3280 * handshake based on external mechanism to
3281 * calculate the master secret later. */
3284 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
3287 case 2: /* ticket couldn't be decrypted */
3288 s->tlsext_ticket_expected = 1;
3290 case 3: /* ticket was decrypted */
3292 case 4: /* ticket decrypted but need to renew */
3293 s->tlsext_ticket_expected = 1;
3295 default: /* fatal error */
3304 /* tls_decrypt_ticket attempts to decrypt a session ticket.
3306 * etick: points to the body of the session ticket extension.
3307 * eticklen: the length of the session tickets extenion.
3308 * sess_id: points at the session ID.
3309 * sesslen: the length of the session ID.
3310 * psess: (output) on return, if a ticket was decrypted, then this is set to
3311 * point to the resulting session.
3314 * -1: fatal error, either from parsing or decrypting the ticket.
3315 * 2: the ticket couldn't be decrypted.
3316 * 3: a ticket was successfully decrypted and *psess was set.
3317 * 4: same as 3, but the ticket needs to be renewed.
3319 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
3320 const unsigned char *sess_id, int sesslen,
3321 SSL_SESSION **psess)
3324 unsigned char *sdec;
3325 const unsigned char *p;
3326 int slen, mlen, renew_ticket = 0;
3327 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3330 SSL_CTX *tctx = s->initial_ctx;
3331 /* Need at least keyname + iv + some encrypted data */
3334 /* Initialize session ticket encryption and HMAC contexts */
3335 HMAC_CTX_init(&hctx);
3336 EVP_CIPHER_CTX_init(&ctx);
3337 if (tctx->tlsext_ticket_key_cb)
3339 unsigned char *nctick = (unsigned char *)etick;
3340 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3351 /* Check key name matches */
3352 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3354 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3355 tlsext_tick_md(), NULL);
3356 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3357 tctx->tlsext_tick_aes_key, etick + 16);
3359 /* Attempt to process session ticket, first conduct sanity and
3360 * integrity checks on ticket.
3362 mlen = HMAC_size(&hctx);
3365 EVP_CIPHER_CTX_cleanup(&ctx);
3369 /* Check HMAC of encrypted ticket */
3370 HMAC_Update(&hctx, etick, eticklen);
3371 HMAC_Final(&hctx, tick_hmac, NULL);
3372 HMAC_CTX_cleanup(&hctx);
3373 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
3375 /* Attempt to decrypt session data */
3376 /* Move p after IV to start of encrypted ticket, update length */
3377 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3378 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3379 sdec = OPENSSL_malloc(eticklen);
3382 EVP_CIPHER_CTX_cleanup(&ctx);
3385 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
3386 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
3389 EVP_CIPHER_CTX_cleanup(&ctx);
3392 sess = d2i_SSL_SESSION(NULL, &p, slen);
3396 /* The session ID, if non-empty, is used by some clients to
3397 * detect that the ticket has been accepted. So we copy it to
3398 * the session structure. If it is empty set length to zero
3399 * as required by standard.
3402 memcpy(sess->session_id, sess_id, sesslen);
3403 sess->session_id_length = sesslen;
3411 /* For session parse failure, indicate that we need to send a new
3416 /* Tables to translate from NIDs to TLS v1.2 ids */
3424 static tls12_lookup tls12_md[] = {
3425 {NID_md5, TLSEXT_hash_md5},
3426 {NID_sha1, TLSEXT_hash_sha1},
3427 {NID_sha224, TLSEXT_hash_sha224},
3428 {NID_sha256, TLSEXT_hash_sha256},
3429 {NID_sha384, TLSEXT_hash_sha384},
3430 {NID_sha512, TLSEXT_hash_sha512}
3433 static tls12_lookup tls12_sig[] = {
3434 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3435 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3436 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3439 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
3442 for (i = 0; i < tlen; i++)
3444 if (table[i].nid == nid)
3450 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
3453 for (i = 0; i < tlen; i++)
3455 if ((table[i].id) == id)
3456 return table[i].nid;
3461 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3466 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3467 sizeof(tls12_md)/sizeof(tls12_lookup));
3470 sig_id = tls12_get_sigid(pk);
3473 p[0] = (unsigned char)md_id;
3474 p[1] = (unsigned char)sig_id;
3478 int tls12_get_sigid(const EVP_PKEY *pk)
3480 return tls12_find_id(pk->type, tls12_sig,
3481 sizeof(tls12_sig)/sizeof(tls12_lookup));
3484 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3488 #ifndef OPENSSL_NO_MD5
3489 case TLSEXT_hash_md5:
3496 #ifndef OPENSSL_NO_SHA
3497 case TLSEXT_hash_sha1:
3500 #ifndef OPENSSL_NO_SHA256
3501 case TLSEXT_hash_sha224:
3502 return EVP_sha224();
3504 case TLSEXT_hash_sha256:
3505 return EVP_sha256();
3507 #ifndef OPENSSL_NO_SHA512
3508 case TLSEXT_hash_sha384:
3509 return EVP_sha384();
3511 case TLSEXT_hash_sha512:
3512 return EVP_sha512();
3520 static int tls12_get_pkey_idx(unsigned char sig_alg)
3524 #ifndef OPENSSL_NO_RSA
3525 case TLSEXT_signature_rsa:
3526 return SSL_PKEY_RSA_SIGN;
3528 #ifndef OPENSSL_NO_DSA
3529 case TLSEXT_signature_dsa:
3530 return SSL_PKEY_DSA_SIGN;
3532 #ifndef OPENSSL_NO_ECDSA
3533 case TLSEXT_signature_ecdsa:
3534 return SSL_PKEY_ECC;
3540 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3541 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3542 int *psignhash_nid, const unsigned char *data)
3544 int sign_nid = 0, hash_nid = 0;
3545 if (!phash_nid && !psign_nid && !psignhash_nid)
3547 if (phash_nid || psignhash_nid)
3549 hash_nid = tls12_find_nid(data[0], tls12_md,
3550 sizeof(tls12_md)/sizeof(tls12_lookup));
3552 *phash_nid = hash_nid;
3554 if (psign_nid || psignhash_nid)
3556 sign_nid = tls12_find_nid(data[1], tls12_sig,
3557 sizeof(tls12_sig)/sizeof(tls12_lookup));
3559 *psign_nid = sign_nid;
3563 if (sign_nid && hash_nid)
3564 OBJ_find_sigid_by_algs(psignhash_nid,
3565 hash_nid, sign_nid);
3567 *psignhash_nid = NID_undef;
3570 /* Given preference and allowed sigalgs set shared sigalgs */
3571 static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
3572 const unsigned char *pref, size_t preflen,
3573 const unsigned char *allow, size_t allowlen)
3575 const unsigned char *ptmp, *atmp;
3576 size_t i, j, nmatch = 0;
3577 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
3579 /* Skip disabled hashes or signature algorithms */
3580 if (tls12_get_hash(ptmp[0]) == NULL)
3582 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3584 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
3586 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
3591 shsig->rhash = ptmp[0];
3592 shsig->rsign = ptmp[1];
3593 tls1_lookup_sigalg(&shsig->hash_nid,
3595 &shsig->signandhash_nid,
3606 /* Set shared signature algorithms for SSL structures */
3607 static int tls1_set_shared_sigalgs(SSL *s)
3609 const unsigned char *pref, *allow, *conf;
3610 size_t preflen, allowlen, conflen;
3612 TLS_SIGALGS *salgs = NULL;
3614 unsigned int is_suiteb = tls1_suiteb(s);
3615 /* If client use client signature algorithms if not NULL */
3616 if (!s->server && c->client_sigalgs && !is_suiteb)
3618 conf = c->client_sigalgs;