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);
817 #endif /* OPENSSL_NO_EC */
819 #ifndef OPENSSL_NO_TLSEXT
821 /* List of supported signature algorithms and hashes. Should make this
822 * customisable at some point, for now include everything we support.
825 #ifdef OPENSSL_NO_RSA
826 #define tlsext_sigalg_rsa(md) /* */
828 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
831 #ifdef OPENSSL_NO_DSA
832 #define tlsext_sigalg_dsa(md) /* */
834 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
837 #ifdef OPENSSL_NO_ECDSA
838 #define tlsext_sigalg_ecdsa(md) /* */
840 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
843 #define tlsext_sigalg(md) \
844 tlsext_sigalg_rsa(md) \
845 tlsext_sigalg_dsa(md) \
846 tlsext_sigalg_ecdsa(md)
848 static unsigned char tls12_sigalgs[] = {
849 #ifndef OPENSSL_NO_SHA512
850 tlsext_sigalg(TLSEXT_hash_sha512)
851 tlsext_sigalg(TLSEXT_hash_sha384)
853 #ifndef OPENSSL_NO_SHA256
854 tlsext_sigalg(TLSEXT_hash_sha256)
855 tlsext_sigalg(TLSEXT_hash_sha224)
857 #ifndef OPENSSL_NO_SHA
858 tlsext_sigalg(TLSEXT_hash_sha1)
860 #ifndef OPENSSL_NO_MD5
861 tlsext_sigalg_rsa(TLSEXT_hash_md5)
865 static unsigned char suiteb_sigalgs[] = {
866 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
867 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
870 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
872 /* If Suite B mode use Suite B sigalgs only, ignore any other
875 switch (tls1_suiteb(s))
877 case SSL_CERT_FLAG_SUITEB_128_LOS:
878 *psigs = suiteb_sigalgs;
879 return sizeof(suiteb_sigalgs);
881 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
882 *psigs = suiteb_sigalgs;
885 case SSL_CERT_FLAG_SUITEB_192_LOS:
886 *psigs = suiteb_sigalgs + 2;
890 /* If server use client authentication sigalgs if not NULL */
891 if (s->server && s->cert->client_sigalgs)
893 *psigs = s->cert->client_sigalgs;
894 return s->cert->client_sigalgslen;
896 else if (s->cert->conf_sigalgs)
898 *psigs = s->cert->conf_sigalgs;
899 return s->cert->conf_sigalgslen;
903 *psigs = tls12_sigalgs;
905 /* If FIPS mode don't include MD5 which is last */
907 return sizeof(tls12_sigalgs) - 2;
910 return sizeof(tls12_sigalgs);
913 /* Check signature algorithm is consistent with sent supported signature
914 * algorithms and if so return relevant digest.
916 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
917 const unsigned char *sig, EVP_PKEY *pkey)
919 const unsigned char *sent_sigs;
920 size_t sent_sigslen, i;
921 int sigalg = tls12_get_sigid(pkey);
922 /* Should never happen */
925 /* Check key type is consistent with signature */
926 if (sigalg != (int)sig[1])
928 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
931 if (pkey->type == EVP_PKEY_EC)
933 unsigned char curve_id[2], comp_id;
934 /* Check compression and curve matches extensions */
935 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
937 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
939 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
942 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
947 if (curve_id[1] == TLSEXT_curve_P_256)
949 if (sig[0] != TLSEXT_hash_sha256)
951 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
952 SSL_R_ILLEGAL_SUITEB_DIGEST);
956 else if (curve_id[1] == TLSEXT_curve_P_384)
958 if (sig[0] != TLSEXT_hash_sha384)
960 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
961 SSL_R_ILLEGAL_SUITEB_DIGEST);
969 else if (tls1_suiteb(s))
972 /* Check signature matches a type we sent */
973 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
974 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
976 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
979 /* Allow fallback to SHA1 if not strict mode */
980 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
982 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
985 *pmd = tls12_get_hash(sig[0]);
988 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
991 /* Store the digest used so applications can retrieve it if they
994 if (s->session && s->session->sess_cert)
995 s->session->sess_cert->peer_key->digest = *pmd;
998 /* Get a mask of disabled algorithms: an algorithm is disabled
999 * if it isn't supported or doesn't appear in supported signature
1000 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1001 * session and not global settings.
1004 void ssl_set_client_disabled(SSL *s)
1007 const unsigned char *sigalgs;
1008 size_t i, sigalgslen;
1009 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1012 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1013 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1014 c->mask_ssl = SSL_TLSV1_2;
1017 /* Now go through all signature algorithms seeing if we support
1018 * any for RSA, DSA, ECDSA. Do this for all versions not just
1021 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
1022 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
1026 #ifndef OPENSSL_NO_RSA
1027 case TLSEXT_signature_rsa:
1031 #ifndef OPENSSL_NO_DSA
1032 case TLSEXT_signature_dsa:
1036 #ifndef OPENSSL_NO_ECDSA
1037 case TLSEXT_signature_ecdsa:
1043 /* Disable auth and static DH if we don't include any appropriate
1044 * signature algorithms.
1048 c->mask_a |= SSL_aRSA;
1049 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1053 c->mask_a |= SSL_aDSS;
1054 c->mask_k |= SSL_kDHd;
1058 c->mask_a |= SSL_aECDSA;
1059 c->mask_k |= SSL_kECDHe;
1061 #ifndef OPENSSL_NO_KRB5
1062 if (!kssl_tgt_is_available(s->kssl_ctx))
1064 c->mask_a |= SSL_aKRB5;
1065 c->mask_k |= SSL_kKRB5;
1068 #ifndef OPENSSL_NO_PSK
1069 /* with PSK there must be client callback set */
1070 if (!s->psk_client_callback)
1072 c->mask_a |= SSL_aPSK;
1073 c->mask_k |= SSL_kPSK;
1075 #endif /* OPENSSL_NO_PSK */
1079 /* byte_compare is a compare function for qsort(3) that compares bytes. */
1080 static int byte_compare(const void *in_a, const void *in_b)
1082 unsigned char a = *((const unsigned char*) in_a);
1083 unsigned char b = *((const unsigned char*) in_b);
1092 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1095 unsigned char *ret = p;
1096 #ifndef OPENSSL_NO_EC
1097 /* See if we support any ECC ciphersuites */
1099 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1102 unsigned long alg_k, alg_a;
1103 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1105 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1107 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1109 alg_k = c->algorithm_mkey;
1110 alg_a = c->algorithm_auth;
1111 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
1112 || (alg_a & SSL_aECDSA)))
1121 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1122 if (s->client_version == SSL3_VERSION
1123 && !s->s3->send_connection_binding)
1128 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1130 if (s->tlsext_hostname != NULL)
1132 /* Add TLS extension servername to the Client Hello message */
1133 unsigned long size_str;
1136 /* check for enough space.
1137 4 for the servername type and entension length
1138 2 for servernamelist length
1139 1 for the hostname type
1140 2 for hostname length
1144 if ((lenmax = limit - ret - 9) < 0
1145 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1148 /* extension type and length */
1149 s2n(TLSEXT_TYPE_server_name,ret);
1150 s2n(size_str+5,ret);
1152 /* length of servername list */
1153 s2n(size_str+3,ret);
1155 /* hostname type, length and hostname */
1156 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1158 memcpy(ret, s->tlsext_hostname, size_str);
1162 /* Add RI if renegotiating */
1167 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1169 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1173 if((limit - p - 4 - el) < 0) return NULL;
1175 s2n(TLSEXT_TYPE_renegotiate,ret);
1178 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1180 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1187 #ifndef OPENSSL_NO_SRP
1188 /* Add SRP username if there is one */
1189 if (s->srp_ctx.login != NULL)
1190 { /* Add TLS extension SRP username to the Client Hello message */
1192 int login_len = strlen(s->srp_ctx.login);
1193 if (login_len > 255 || login_len == 0)
1195 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1199 /* check for enough space.
1200 4 for the srp type type and entension length
1201 1 for the srp user identity
1202 + srp user identity length
1204 if ((limit - ret - 5 - login_len) < 0) return NULL;
1206 /* fill in the extension */
1207 s2n(TLSEXT_TYPE_srp,ret);
1208 s2n(login_len+1,ret);
1209 (*ret++) = (unsigned char) login_len;
1210 memcpy(ret, s->srp_ctx.login, login_len);
1215 #ifndef OPENSSL_NO_EC
1218 /* Add TLS extension ECPointFormats to the ClientHello message */
1220 const unsigned char *plist;
1223 tls1_get_formatlist(s, &plist, &plistlen);
1225 if ((lenmax = limit - ret - 5) < 0) return NULL;
1226 if (plistlen > (size_t)lenmax) return NULL;
1229 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1233 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1234 s2n(plistlen + 1,ret);
1235 *(ret++) = (unsigned char)plistlen ;
1236 memcpy(ret, plist, plistlen);
1239 /* Add TLS extension EllipticCurves to the ClientHello message */
1240 plist = s->tlsext_ellipticcurvelist;
1241 tls1_get_curvelist(s, 0, &plist, &plistlen);
1243 if ((lenmax = limit - ret - 6) < 0) return NULL;
1244 if (plistlen > (size_t)lenmax) return NULL;
1245 if (plistlen > 65532)
1247 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1251 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1252 s2n(plistlen + 2, ret);
1254 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1255 * elliptic_curve_list, but the examples use two bytes.
1256 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1257 * resolves this to two bytes.
1260 memcpy(ret, plist, plistlen);
1263 #endif /* OPENSSL_NO_EC */
1265 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1268 if (!s->new_session && s->session && s->session->tlsext_tick)
1269 ticklen = s->session->tlsext_ticklen;
1270 else if (s->session && s->tlsext_session_ticket &&
1271 s->tlsext_session_ticket->data)
1273 ticklen = s->tlsext_session_ticket->length;
1274 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1275 if (!s->session->tlsext_tick)
1277 memcpy(s->session->tlsext_tick,
1278 s->tlsext_session_ticket->data,
1280 s->session->tlsext_ticklen = ticklen;
1284 if (ticklen == 0 && s->tlsext_session_ticket &&
1285 s->tlsext_session_ticket->data == NULL)
1287 /* Check for enough room 2 for extension type, 2 for len
1290 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1291 s2n(TLSEXT_TYPE_session_ticket,ret);
1295 memcpy(ret, s->session->tlsext_tick, ticklen);
1301 if (SSL_USE_SIGALGS(s))
1304 const unsigned char *salg;
1305 salglen = tls12_get_psigalgs(s, &salg);
1306 if ((size_t)(limit - ret) < salglen + 6)
1308 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1309 s2n(salglen + 2, ret);
1311 memcpy(ret, salg, salglen);
1315 #ifdef TLSEXT_TYPE_opaque_prf_input
1316 if (s->s3->client_opaque_prf_input != NULL)
1318 size_t col = s->s3->client_opaque_prf_input_len;
1320 if ((long)(limit - ret - 6 - col < 0))
1322 if (col > 0xFFFD) /* can't happen */
1325 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1328 memcpy(ret, s->s3->client_opaque_prf_input, col);
1333 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1336 long extlen, idlen, itmp;
1340 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1342 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1343 itmp = i2d_OCSP_RESPID(id, NULL);
1349 if (s->tlsext_ocsp_exts)
1351 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1358 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1359 s2n(TLSEXT_TYPE_status_request, ret);
1360 if (extlen + idlen > 0xFFF0)
1362 s2n(extlen + idlen + 5, ret);
1363 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1365 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1367 /* save position of id len */
1368 unsigned char *q = ret;
1369 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1370 /* skip over id len */
1372 itmp = i2d_OCSP_RESPID(id, &ret);
1378 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1381 #ifndef OPENSSL_NO_HEARTBEATS
1382 /* Add Heartbeat extension */
1383 s2n(TLSEXT_TYPE_heartbeat,ret);
1386 * 1: peer may send requests
1387 * 2: peer not allowed to send requests
1389 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1390 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1392 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1395 #ifndef OPENSSL_NO_NEXTPROTONEG
1396 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1398 /* The client advertises an emtpy extension to indicate its
1399 * support for Next Protocol Negotiation */
1400 if (limit - ret - 4 < 0)
1402 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1407 if(SSL_get_srtp_profiles(s))
1411 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1413 if((limit - p - 4 - el) < 0) return NULL;
1415 s2n(TLSEXT_TYPE_use_srtp,ret);
1418 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1420 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1426 /* Add TLS extension Server_Authz_DataFormats to the ClientHello */
1427 /* 2 bytes for extension type */
1428 /* 2 bytes for extension length */
1429 /* 1 byte for the list length */
1430 /* 1 byte for the list (we only support audit proofs) */
1431 if (s->ctx->tlsext_authz_server_audit_proof_cb != NULL)
1433 const unsigned short ext_len = 2;
1434 const unsigned char list_len = 1;
1436 if (limit < ret + 6)
1439 s2n(TLSEXT_TYPE_server_authz, ret);
1440 /* Extension length: 2 bytes */
1442 *(ret++) = list_len;
1443 *(ret++) = TLSEXT_AUTHZDATAFORMAT_audit_proof;
1446 /* Add custom TLS Extensions to ClientHello */
1447 if (s->ctx->custom_cli_ext_records_count)
1450 custom_cli_ext_record* record;
1452 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
1454 const unsigned char* out = NULL;
1455 unsigned short outlen = 0;
1457 record = &s->ctx->custom_cli_ext_records[i];
1458 /* NULL callback sends empty extension */
1459 /* -1 from callback omits extension */
1463 cb_retval = record->fn1(s, record->ext_type,
1467 return NULL; /* error */
1468 if (cb_retval == -1)
1469 continue; /* skip this extension */
1471 if (limit < ret + 4 + outlen)
1473 s2n(record->ext_type, ret);
1475 memcpy(ret, out, outlen);
1480 if ((extdatalen = ret-p-2) == 0)
1487 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1490 unsigned char *ret = p;
1491 #ifndef OPENSSL_NO_NEXTPROTONEG
1492 int next_proto_neg_seen;
1494 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1495 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1496 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1497 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1499 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1500 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1504 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1506 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1508 if ((long)(limit - ret - 4) < 0) return NULL;
1510 s2n(TLSEXT_TYPE_server_name,ret);
1514 if(s->s3->send_connection_binding)
1518 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1520 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1524 if((limit - p - 4 - el) < 0) return NULL;
1526 s2n(TLSEXT_TYPE_renegotiate,ret);
1529 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1531 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1538 #ifndef OPENSSL_NO_EC
1541 const unsigned char *plist;
1543 /* Add TLS extension ECPointFormats to the ServerHello message */
1546 tls1_get_formatlist(s, &plist, &plistlen);
1548 if ((lenmax = limit - ret - 5) < 0) return NULL;
1549 if (plistlen > (size_t)lenmax) return NULL;
1552 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1556 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1557 s2n(plistlen + 1,ret);
1558 *(ret++) = (unsigned char) plistlen;
1559 memcpy(ret, plist, plistlen);
1563 /* Currently the server should not respond with a SupportedCurves extension */
1564 #endif /* OPENSSL_NO_EC */
1566 if (s->tlsext_ticket_expected
1567 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1569 if ((long)(limit - ret - 4) < 0) return NULL;
1570 s2n(TLSEXT_TYPE_session_ticket,ret);
1574 if (s->tlsext_status_expected)
1576 if ((long)(limit - ret - 4) < 0) return NULL;
1577 s2n(TLSEXT_TYPE_status_request,ret);
1581 #ifdef TLSEXT_TYPE_opaque_prf_input
1582 if (s->s3->server_opaque_prf_input != NULL)
1584 size_t sol = s->s3->server_opaque_prf_input_len;
1586 if ((long)(limit - ret - 6 - sol) < 0)
1588 if (sol > 0xFFFD) /* can't happen */
1591 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1594 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1603 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1605 if((limit - p - 4 - el) < 0) return NULL;
1607 s2n(TLSEXT_TYPE_use_srtp,ret);
1610 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1612 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1618 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1619 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1620 { const unsigned char cryptopro_ext[36] = {
1621 0xfd, 0xe8, /*65000*/
1622 0x00, 0x20, /*32 bytes length*/
1623 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1624 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1625 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1626 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1627 if (limit-ret<36) return NULL;
1628 memcpy(ret,cryptopro_ext,36);
1633 #ifndef OPENSSL_NO_HEARTBEATS
1634 /* Add Heartbeat extension if we've received one */
1635 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1637 s2n(TLSEXT_TYPE_heartbeat,ret);
1640 * 1: peer may send requests
1641 * 2: peer not allowed to send requests
1643 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1644 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1646 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1651 #ifndef OPENSSL_NO_NEXTPROTONEG
1652 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1653 s->s3->next_proto_neg_seen = 0;
1654 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1656 const unsigned char *npa;
1657 unsigned int npalen;
1660 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1661 if (r == SSL_TLSEXT_ERR_OK)
1663 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1664 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1666 memcpy(ret, npa, npalen);
1668 s->s3->next_proto_neg_seen = 1;
1673 /* If the client supports authz then see whether we have any to offer
1675 if (s->s3->tlsext_authz_client_types_len)
1677 size_t authz_length;
1678 /* By now we already know the new cipher, so we can look ahead
1679 * to see whether the cert we are going to send
1680 * has any authz data attached to it. */
1681 const unsigned char* authz = ssl_get_authz_data(s, &authz_length);
1682 const unsigned char* const orig_authz = authz;
1684 unsigned authz_count = 0;
1686 /* The authz data contains a number of the following structures:
1687 * uint8_t authz_type
1689 * uint8_t data[length]
1691 * First we walk over it to find the number of authz elements. */
1692 for (i = 0; i < authz_length; i++)
1694 unsigned short length;
1698 if (memchr(s->s3->tlsext_authz_client_types,
1700 s->s3->tlsext_authz_client_types_len) != NULL)
1704 /* n2s increments authz by 2 */
1712 /* Add TLS extension server_authz to the ServerHello message
1713 * 2 bytes for extension type
1714 * 2 bytes for extension length
1715 * 1 byte for the list length
1716 * n bytes for the list */
1717 const unsigned short ext_len = 1 + authz_count;
1719 if ((long)(limit - ret - 4 - ext_len) < 0) return NULL;
1720 s2n(TLSEXT_TYPE_server_authz, ret);
1722 *(ret++) = authz_count;
1723 s->s3->tlsext_authz_promised_to_client = 1;
1727 for (i = 0; i < authz_length; i++)
1729 unsigned short length;
1734 if (memchr(s->s3->tlsext_authz_client_types,
1736 s->s3->tlsext_authz_client_types_len) != NULL)
1739 /* n2s increments authz by 2 */
1746 /* If custom types were sent in ClientHello, add ServerHello responses */
1747 if (s->s3->tlsext_custom_types_count)
1751 for (i = 0; i < s->s3->tlsext_custom_types_count; i++)
1754 custom_srv_ext_record *record;
1756 for (j = 0; j < s->ctx->custom_srv_ext_records_count; j++)
1758 record = &s->ctx->custom_srv_ext_records[j];
1759 if (s->s3->tlsext_custom_types[i] == record->ext_type)
1761 const unsigned char *out = NULL;
1762 unsigned short outlen = 0;
1765 /* NULL callback or -1 omits extension */
1768 cb_retval = record->fn2(s, record->ext_type,
1772 return NULL; /* error */
1773 if (cb_retval == -1)
1774 break; /* skip this extension */
1775 if (limit < ret + 4 + outlen)
1777 s2n(record->ext_type, ret);
1779 memcpy(ret, out, outlen);
1787 if ((extdatalen = ret-p-2)== 0)
1794 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1796 unsigned short type;
1797 unsigned short size;
1799 unsigned char *data = *p;
1800 int renegotiate_seen = 0;
1803 s->servername_done = 0;
1804 s->tlsext_status_type = -1;
1805 #ifndef OPENSSL_NO_NEXTPROTONEG
1806 s->s3->next_proto_neg_seen = 0;
1809 #ifndef OPENSSL_NO_HEARTBEATS
1810 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1811 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1813 /* Clear any signature algorithms extension received */
1814 if (s->cert->peer_sigalgs)
1816 OPENSSL_free(s->cert->peer_sigalgs);
1817 s->cert->peer_sigalgs = NULL;
1819 /* Clear any shared sigtnature algorithms */
1820 if (s->cert->shared_sigalgs)
1822 OPENSSL_free(s->cert->shared_sigalgs);
1823 s->cert->shared_sigalgs = NULL;
1825 /* Clear certificate digests and validity flags */
1826 for (i = 0; i < SSL_PKEY_NUM; i++)
1828 s->cert->pkeys[i].digest = NULL;
1829 s->cert->pkeys[i].valid_flags = 0;
1832 if (data >= (d+n-2))
1836 if (data > (d+n-len))
1839 while (data <= (d+n-4))
1844 if (data+size > (d+n))
1847 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1849 if (s->tlsext_debug_cb)
1850 s->tlsext_debug_cb(s, 0, type, data, size,
1851 s->tlsext_debug_arg);
1852 /* The servername extension is treated as follows:
1854 - Only the hostname type is supported with a maximum length of 255.
1855 - The servername is rejected if too long or if it contains zeros,
1856 in which case an fatal alert is generated.
1857 - The servername field is maintained together with the session cache.
1858 - When a session is resumed, the servername call back invoked in order
1859 to allow the application to position itself to the right context.
1860 - The servername is acknowledged if it is new for a session or when
1861 it is identical to a previously used for the same session.
1862 Applications can control the behaviour. They can at any time
1863 set a 'desirable' servername for a new SSL object. This can be the
1864 case for example with HTTPS when a Host: header field is received and
1865 a renegotiation is requested. In this case, a possible servername
1866 presented in the new client hello is only acknowledged if it matches
1867 the value of the Host: field.
1868 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1869 if they provide for changing an explicit servername context for the session,
1870 i.e. when the session has been established with a servername extension.
1871 - On session reconnect, the servername extension may be absent.
1875 if (type == TLSEXT_TYPE_server_name)
1877 unsigned char *sdata;
1883 *al = SSL_AD_DECODE_ERROR;
1890 *al = SSL_AD_DECODE_ERROR;
1897 servname_type = *(sdata++);
1903 *al = SSL_AD_DECODE_ERROR;
1906 if (s->servername_done == 0)
1907 switch (servname_type)
1909 case TLSEXT_NAMETYPE_host_name:
1912 if(s->session->tlsext_hostname)
1914 *al = SSL_AD_DECODE_ERROR;
1917 if (len > TLSEXT_MAXLEN_host_name)
1919 *al = TLS1_AD_UNRECOGNIZED_NAME;
1922 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
1924 *al = TLS1_AD_INTERNAL_ERROR;
1927 memcpy(s->session->tlsext_hostname, sdata, len);
1928 s->session->tlsext_hostname[len]='\0';
1929 if (strlen(s->session->tlsext_hostname) != len) {
1930 OPENSSL_free(s->session->tlsext_hostname);
1931 s->session->tlsext_hostname = NULL;
1932 *al = TLS1_AD_UNRECOGNIZED_NAME;
1935 s->servername_done = 1;
1939 s->servername_done = s->session->tlsext_hostname
1940 && strlen(s->session->tlsext_hostname) == len
1941 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
1953 *al = SSL_AD_DECODE_ERROR;
1958 #ifndef OPENSSL_NO_SRP
1959 else if (type == TLSEXT_TYPE_srp)
1961 if (size <= 0 || ((len = data[0])) != (size -1))
1963 *al = SSL_AD_DECODE_ERROR;
1966 if (s->srp_ctx.login != NULL)
1968 *al = SSL_AD_DECODE_ERROR;
1971 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
1973 memcpy(s->srp_ctx.login, &data[1], len);
1974 s->srp_ctx.login[len]='\0';
1976 if (strlen(s->srp_ctx.login) != len)
1978 *al = SSL_AD_DECODE_ERROR;
1984 #ifndef OPENSSL_NO_EC
1985 else if (type == TLSEXT_TYPE_ec_point_formats)
1987 unsigned char *sdata = data;
1988 int ecpointformatlist_length = *(sdata++);
1990 if (ecpointformatlist_length != size - 1 ||
1991 ecpointformatlist_length < 1)
1993 *al = TLS1_AD_DECODE_ERROR;
1998 if(s->session->tlsext_ecpointformatlist)
2000 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2001 s->session->tlsext_ecpointformatlist = NULL;
2003 s->session->tlsext_ecpointformatlist_length = 0;
2004 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2006 *al = TLS1_AD_INTERNAL_ERROR;
2009 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2010 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2013 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2014 sdata = s->session->tlsext_ecpointformatlist;
2015 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2016 fprintf(stderr,"%i ",*(sdata++));
2017 fprintf(stderr,"\n");
2020 else if (type == TLSEXT_TYPE_elliptic_curves)
2022 unsigned char *sdata = data;
2023 int ellipticcurvelist_length = (*(sdata++) << 8);
2024 ellipticcurvelist_length += (*(sdata++));
2026 if (ellipticcurvelist_length != size - 2 ||
2027 ellipticcurvelist_length < 1)
2029 *al = TLS1_AD_DECODE_ERROR;
2034 if(s->session->tlsext_ellipticcurvelist)
2036 *al = TLS1_AD_DECODE_ERROR;
2039 s->session->tlsext_ellipticcurvelist_length = 0;
2040 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2042 *al = TLS1_AD_INTERNAL_ERROR;
2045 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2046 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2049 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2050 sdata = s->session->tlsext_ellipticcurvelist;
2051 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2052 fprintf(stderr,"%i ",*(sdata++));
2053 fprintf(stderr,"\n");
2056 #endif /* OPENSSL_NO_EC */
2057 #ifdef TLSEXT_TYPE_opaque_prf_input
2058 else if (type == TLSEXT_TYPE_opaque_prf_input)
2060 unsigned char *sdata = data;
2064 *al = SSL_AD_DECODE_ERROR;
2067 n2s(sdata, s->s3->client_opaque_prf_input_len);
2068 if (s->s3->client_opaque_prf_input_len != size - 2)
2070 *al = SSL_AD_DECODE_ERROR;
2074 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2075 OPENSSL_free(s->s3->client_opaque_prf_input);
2076 if (s->s3->client_opaque_prf_input_len == 0)
2077 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2079 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2080 if (s->s3->client_opaque_prf_input == NULL)
2082 *al = TLS1_AD_INTERNAL_ERROR;
2087 else if (type == TLSEXT_TYPE_session_ticket)
2089 if (s->tls_session_ticket_ext_cb &&
2090 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2092 *al = TLS1_AD_INTERNAL_ERROR;
2096 else if (type == TLSEXT_TYPE_renegotiate)
2098 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2100 renegotiate_seen = 1;
2102 else if (type == TLSEXT_TYPE_signature_algorithms)
2105 if (s->cert->peer_sigalgs || size < 2)
2107 *al = SSL_AD_DECODE_ERROR;
2112 if (dsize != size || dsize & 1 || !dsize)
2114 *al = SSL_AD_DECODE_ERROR;
2117 if (!tls1_process_sigalgs(s, data, dsize))
2119 *al = SSL_AD_DECODE_ERROR;
2122 /* If sigalgs received and no shared algorithms fatal
2125 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
2127 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2128 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2129 *al = SSL_AD_ILLEGAL_PARAMETER;
2133 else if (type == TLSEXT_TYPE_status_request
2134 && s->ctx->tlsext_status_cb)
2139 *al = SSL_AD_DECODE_ERROR;
2143 s->tlsext_status_type = *data++;
2145 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2147 const unsigned char *sdata;
2149 /* Read in responder_id_list */
2154 *al = SSL_AD_DECODE_ERROR;
2163 *al = SSL_AD_DECODE_ERROR;
2167 dsize -= 2 + idsize;
2171 *al = SSL_AD_DECODE_ERROR;
2176 id = d2i_OCSP_RESPID(NULL,
2180 *al = SSL_AD_DECODE_ERROR;
2185 OCSP_RESPID_free(id);
2186 *al = SSL_AD_DECODE_ERROR;
2189 if (!s->tlsext_ocsp_ids
2190 && !(s->tlsext_ocsp_ids =
2191 sk_OCSP_RESPID_new_null()))
2193 OCSP_RESPID_free(id);
2194 *al = SSL_AD_INTERNAL_ERROR;
2197 if (!sk_OCSP_RESPID_push(
2198 s->tlsext_ocsp_ids, id))
2200 OCSP_RESPID_free(id);
2201 *al = SSL_AD_INTERNAL_ERROR;
2206 /* Read in request_extensions */
2209 *al = SSL_AD_DECODE_ERROR;
2216 *al = SSL_AD_DECODE_ERROR;
2222 if (s->tlsext_ocsp_exts)
2224 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2225 X509_EXTENSION_free);
2228 s->tlsext_ocsp_exts =
2229 d2i_X509_EXTENSIONS(NULL,
2231 if (!s->tlsext_ocsp_exts
2232 || (data + dsize != sdata))
2234 *al = SSL_AD_DECODE_ERROR;
2239 /* We don't know what to do with any other type
2243 s->tlsext_status_type = -1;
2245 #ifndef OPENSSL_NO_HEARTBEATS
2246 else if (type == TLSEXT_TYPE_heartbeat)
2250 case 0x01: /* Client allows us to send HB requests */
2251 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2253 case 0x02: /* Client doesn't accept HB requests */
2254 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2255 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2257 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2262 #ifndef OPENSSL_NO_NEXTPROTONEG
2263 else if (type == TLSEXT_TYPE_next_proto_neg &&
2264 s->s3->tmp.finish_md_len == 0)
2266 /* We shouldn't accept this extension on a
2269 * s->new_session will be set on renegotiation, but we
2270 * probably shouldn't rely that it couldn't be set on
2271 * the initial renegotation too in certain cases (when
2272 * there's some other reason to disallow resuming an
2273 * earlier session -- the current code won't be doing
2274 * anything like that, but this might change).
2276 * A valid sign that there's been a previous handshake
2277 * in this connection is if s->s3->tmp.finish_md_len >
2278 * 0. (We are talking about a check that will happen
2279 * in the Hello protocol round, well before a new
2280 * Finished message could have been computed.) */
2281 s->s3->next_proto_neg_seen = 1;
2285 /* session ticket processed earlier */
2286 else if (type == TLSEXT_TYPE_use_srtp)
2288 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2293 else if (type == TLSEXT_TYPE_server_authz)
2295 unsigned char *sdata = data;
2296 unsigned char server_authz_dataformatlist_length;
2300 *al = TLS1_AD_DECODE_ERROR;
2304 server_authz_dataformatlist_length = *(sdata++);
2306 if (server_authz_dataformatlist_length != size - 1)
2308 *al = TLS1_AD_DECODE_ERROR;
2312 /* Successful session resumption uses the same authz
2313 * information as the original session so we ignore this
2314 * in the case of a session resumption. */
2317 if (s->s3->tlsext_authz_client_types != NULL)
2318 OPENSSL_free(s->s3->tlsext_authz_client_types);
2319 s->s3->tlsext_authz_client_types =
2320 OPENSSL_malloc(server_authz_dataformatlist_length);
2321 if (!s->s3->tlsext_authz_client_types)
2323 *al = TLS1_AD_INTERNAL_ERROR;
2327 s->s3->tlsext_authz_client_types_len =
2328 server_authz_dataformatlist_length;
2329 memcpy(s->s3->tlsext_authz_client_types,
2331 server_authz_dataformatlist_length);
2333 /* Sort the types in order to check for duplicates. */
2334 qsort(s->s3->tlsext_authz_client_types,
2335 server_authz_dataformatlist_length,
2336 1 /* element size */,
2339 for (i = 0; i < server_authz_dataformatlist_length; i++)
2342 s->s3->tlsext_authz_client_types[i] ==
2343 s->s3->tlsext_authz_client_types[i-1])
2345 *al = TLS1_AD_DECODE_ERROR;
2352 /* If this ClientHello extension was unhandled and this is
2353 * a nonresumed connection, check whether the extension is a
2354 * custom TLS Extension (has a custom_srv_ext_record), and if
2355 * so call the callback and record the extension number so that
2356 * an appropriate ServerHello may be later returned.
2358 else if (!s->hit && s->ctx->custom_srv_ext_records_count)
2360 custom_srv_ext_record *record;
2362 for (i=0; i < s->ctx->custom_srv_ext_records_count; i++)
2364 record = &s->ctx->custom_srv_ext_records[i];
2365 if (type == record->ext_type)
2369 /* Error on duplicate TLS Extensions */
2370 for (j = 0; j < s->s3->tlsext_custom_types_count; j++)
2372 if (type == s->s3->tlsext_custom_types[j])
2374 *al = TLS1_AD_DECODE_ERROR;
2379 /* NULL callback still notes the extension */
2380 if (record->fn1 && !record->fn1(s, type, data, size, al, record->arg))
2383 /* Add the (non-duplicated) entry */
2384 s->s3->tlsext_custom_types_count++;
2385 s->s3->tlsext_custom_types = OPENSSL_realloc(
2386 s->s3->tlsext_custom_types,
2387 s->s3->tlsext_custom_types_count * 2);
2388 if (s->s3->tlsext_custom_types == NULL)
2390 s->s3->tlsext_custom_types = 0;
2391 *al = TLS1_AD_INTERNAL_ERROR;
2394 s->s3->tlsext_custom_types[
2395 s->s3->tlsext_custom_types_count - 1] = type;
2407 /* Need RI if renegotiating */
2409 if (!renegotiate_seen && s->renegotiate &&
2410 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2412 *al = SSL_AD_HANDSHAKE_FAILURE;
2413 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2414 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2417 /* If no signature algorithms extension set default values */
2418 if (!s->cert->peer_sigalgs)
2419 ssl_cert_set_default_md(s->cert);
2424 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2427 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2429 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2433 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2435 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2441 #ifndef OPENSSL_NO_NEXTPROTONEG
2442 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2443 * elements of zero length are allowed and the set of elements must exactly fill
2444 * the length of the block. */
2445 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2447 unsigned int off = 0;
2461 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2463 unsigned short length;
2464 unsigned short type;
2465 unsigned short size;
2466 unsigned char *data = *p;
2467 int tlsext_servername = 0;
2468 int renegotiate_seen = 0;
2470 #ifndef OPENSSL_NO_NEXTPROTONEG
2471 s->s3->next_proto_neg_seen = 0;
2474 #ifndef OPENSSL_NO_HEARTBEATS
2475 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2476 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2479 if (data >= (d+n-2))
2483 if (data+length != d+n)
2485 *al = SSL_AD_DECODE_ERROR;
2489 while(data <= (d+n-4))
2494 if (data+size > (d+n))
2497 if (s->tlsext_debug_cb)
2498 s->tlsext_debug_cb(s, 1, type, data, size,
2499 s->tlsext_debug_arg);
2501 if (type == TLSEXT_TYPE_server_name)
2503 if (s->tlsext_hostname == NULL || size > 0)
2505 *al = TLS1_AD_UNRECOGNIZED_NAME;
2508 tlsext_servername = 1;
2511 #ifndef OPENSSL_NO_EC
2512 else if (type == TLSEXT_TYPE_ec_point_formats)
2514 unsigned char *sdata = data;
2515 int ecpointformatlist_length = *(sdata++);
2517 if (ecpointformatlist_length != size - 1)
2519 *al = TLS1_AD_DECODE_ERROR;
2522 s->session->tlsext_ecpointformatlist_length = 0;
2523 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2524 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2526 *al = TLS1_AD_INTERNAL_ERROR;
2529 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2530 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2532 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2533 sdata = s->session->tlsext_ecpointformatlist;
2534 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2535 fprintf(stderr,"%i ",*(sdata++));
2536 fprintf(stderr,"\n");
2539 #endif /* OPENSSL_NO_EC */
2541 else if (type == TLSEXT_TYPE_session_ticket)
2543 if (s->tls_session_ticket_ext_cb &&
2544 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2546 *al = TLS1_AD_INTERNAL_ERROR;
2549 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2552 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2555 s->tlsext_ticket_expected = 1;
2557 #ifdef TLSEXT_TYPE_opaque_prf_input
2558 else if (type == TLSEXT_TYPE_opaque_prf_input)
2560 unsigned char *sdata = data;
2564 *al = SSL_AD_DECODE_ERROR;
2567 n2s(sdata, s->s3->server_opaque_prf_input_len);
2568 if (s->s3->server_opaque_prf_input_len != size - 2)
2570 *al = SSL_AD_DECODE_ERROR;
2574 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2575 OPENSSL_free(s->s3->server_opaque_prf_input);
2576 if (s->s3->server_opaque_prf_input_len == 0)
2577 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2579 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2581 if (s->s3->server_opaque_prf_input == NULL)
2583 *al = TLS1_AD_INTERNAL_ERROR;
2588 else if (type == TLSEXT_TYPE_status_request)
2590 /* MUST be empty and only sent if we've requested
2591 * a status request message.
2593 if ((s->tlsext_status_type == -1) || (size > 0))
2595 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2598 /* Set flag to expect CertificateStatus message */
2599 s->tlsext_status_expected = 1;
2601 #ifndef OPENSSL_NO_NEXTPROTONEG
2602 else if (type == TLSEXT_TYPE_next_proto_neg &&
2603 s->s3->tmp.finish_md_len == 0)
2605 unsigned char *selected;
2606 unsigned char selected_len;
2608 /* We must have requested it. */
2609 if (s->ctx->next_proto_select_cb == NULL)
2611 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2614 /* The data must be valid */
2615 if (!ssl_next_proto_validate(data, size))
2617 *al = TLS1_AD_DECODE_ERROR;
2620 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2622 *al = TLS1_AD_INTERNAL_ERROR;
2625 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2626 if (!s->next_proto_negotiated)
2628 *al = TLS1_AD_INTERNAL_ERROR;
2631 memcpy(s->next_proto_negotiated, selected, selected_len);
2632 s->next_proto_negotiated_len = selected_len;
2633 s->s3->next_proto_neg_seen = 1;
2636 else if (type == TLSEXT_TYPE_renegotiate)
2638 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2640 renegotiate_seen = 1;
2642 #ifndef OPENSSL_NO_HEARTBEATS
2643 else if (type == TLSEXT_TYPE_heartbeat)
2647 case 0x01: /* Server allows us to send HB requests */
2648 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2650 case 0x02: /* Server doesn't accept HB requests */
2651 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2652 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2654 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2659 else if (type == TLSEXT_TYPE_use_srtp)
2661 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2666 else if (type == TLSEXT_TYPE_server_authz)
2668 /* We only support audit proofs. It's an error to send
2669 * an authz hello extension if the client
2670 * didn't request a proof. */
2671 unsigned char *sdata = data;
2672 unsigned char server_authz_dataformatlist_length;
2674 if (!s->ctx->tlsext_authz_server_audit_proof_cb)
2676 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2682 *al = TLS1_AD_DECODE_ERROR;
2686 server_authz_dataformatlist_length = *(sdata++);
2687 if (server_authz_dataformatlist_length != size - 1)
2689 *al = TLS1_AD_DECODE_ERROR;
2693 /* We only support audit proofs, so a legal ServerHello
2694 * authz list contains exactly one entry. */
2695 if (server_authz_dataformatlist_length != 1 ||
2696 sdata[0] != TLSEXT_AUTHZDATAFORMAT_audit_proof)
2698 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2702 s->s3->tlsext_authz_server_promised = 1;
2705 /* If this extension type was not otherwise handled, but
2706 * matches a custom_cli_ext_record, then send it to the c
2708 else if (s->ctx->custom_cli_ext_records_count)
2711 custom_cli_ext_record* record;
2713 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
2715 record = &s->ctx->custom_cli_ext_records[i];
2716 if (record->ext_type == type)
2718 if (record->fn2 && !record->fn2(s, type, data, size, al, record->arg))
2730 *al = SSL_AD_DECODE_ERROR;
2734 if (!s->hit && tlsext_servername == 1)
2736 if (s->tlsext_hostname)
2738 if (s->session->tlsext_hostname == NULL)
2740 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2741 if (!s->session->tlsext_hostname)
2743 *al = SSL_AD_UNRECOGNIZED_NAME;
2749 *al = SSL_AD_DECODE_ERROR;
2759 /* Determine if we need to see RI. Strictly speaking if we want to
2760 * avoid an attack we should *always* see RI even on initial server
2761 * hello because the client doesn't see any renegotiation during an
2762 * attack. However this would mean we could not connect to any server
2763 * which doesn't support RI so for the immediate future tolerate RI
2764 * absence on initial connect only.
2766 if (!renegotiate_seen
2767 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2768 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2770 *al = SSL_AD_HANDSHAKE_FAILURE;
2771 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2772 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2780 int ssl_prepare_clienthello_tlsext(SSL *s)
2783 #ifdef TLSEXT_TYPE_opaque_prf_input
2787 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2789 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2794 if (s->tlsext_opaque_prf_input != NULL)
2796 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2797 OPENSSL_free(s->s3->client_opaque_prf_input);
2799 if (s->tlsext_opaque_prf_input_len == 0)
2800 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2802 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2803 if (s->s3->client_opaque_prf_input == NULL)
2805 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2808 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2812 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2813 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2820 int ssl_prepare_serverhello_tlsext(SSL *s)
2825 static int ssl_check_clienthello_tlsext_early(SSL *s)
2827 int ret=SSL_TLSEXT_ERR_NOACK;
2828 int al = SSL_AD_UNRECOGNIZED_NAME;
2830 #ifndef OPENSSL_NO_EC
2831 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2832 * ssl3_choose_cipher in s3_lib.c.
2834 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2835 * ssl3_choose_cipher in s3_lib.c.
2839 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2840 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2841 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2842 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2844 #ifdef TLSEXT_TYPE_opaque_prf_input
2846 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2847 * but we might be sending an alert in response to the client hello,
2848 * so this has to happen here in
2849 * ssl_check_clienthello_tlsext_early(). */
2853 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2855 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2858 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2859 al = SSL_AD_INTERNAL_ERROR;
2864 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2865 OPENSSL_free(s->s3->server_opaque_prf_input);
2866 s->s3->server_opaque_prf_input = NULL;
2868 if (s->tlsext_opaque_prf_input != NULL)
2870 if (s->s3->client_opaque_prf_input != NULL &&
2871 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2873 /* can only use this extension if we have a server opaque PRF input
2874 * of the same length as the client opaque PRF input! */
2876 if (s->tlsext_opaque_prf_input_len == 0)
2877 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2879 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2880 if (s->s3->server_opaque_prf_input == NULL)
2882 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2883 al = SSL_AD_INTERNAL_ERROR;
2886 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2890 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2892 /* The callback wants to enforce use of the extension,
2893 * but we can't do that with the client opaque PRF input;
2894 * abort the handshake.
2896 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2897 al = SSL_AD_HANDSHAKE_FAILURE;
2905 case SSL_TLSEXT_ERR_ALERT_FATAL:
2906 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2909 case SSL_TLSEXT_ERR_ALERT_WARNING:
2910 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2913 case SSL_TLSEXT_ERR_NOACK:
2914 s->servername_done=0;
2920 int ssl_check_clienthello_tlsext_late(SSL *s)
2922 int ret = SSL_TLSEXT_ERR_OK;
2925 /* If status request then ask callback what to do.
2926 * Note: this must be called after servername callbacks in case
2927 * the certificate has changed, and must be called after the cipher
2928 * has been chosen because this may influence which certificate is sent
2930 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2933 CERT_PKEY *certpkey;
2934 certpkey = ssl_get_server_send_pkey(s);
2935 /* If no certificate can't return certificate status */
2936 if (certpkey == NULL)
2938 s->tlsext_status_expected = 0;
2941 /* Set current certificate to one we will use so
2942 * SSL_get_certificate et al can pick it up.
2944 s->cert->key = certpkey;
2945 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2948 /* We don't want to send a status request response */
2949 case SSL_TLSEXT_ERR_NOACK:
2950 s->tlsext_status_expected = 0;
2952 /* status request response should be sent */
2953 case SSL_TLSEXT_ERR_OK:
2954 if (s->tlsext_ocsp_resp)
2955 s->tlsext_status_expected = 1;
2957 s->tlsext_status_expected = 0;
2959 /* something bad happened */
2960 case SSL_TLSEXT_ERR_ALERT_FATAL:
2961 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2962 al = SSL_AD_INTERNAL_ERROR;
2967 s->tlsext_status_expected = 0;
2972 case SSL_TLSEXT_ERR_ALERT_FATAL:
2973 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2976 case SSL_TLSEXT_ERR_ALERT_WARNING:
2977 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2985 int ssl_check_serverhello_tlsext(SSL *s)
2987 int ret=SSL_TLSEXT_ERR_NOACK;
2988 int al = SSL_AD_UNRECOGNIZED_NAME;
2990 #ifndef OPENSSL_NO_EC
2991 /* If we are client and using an elliptic curve cryptography cipher
2992 * suite, then if server returns an EC point formats lists extension
2993 * it must contain uncompressed.
2995 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2996 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2997 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2998 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
2999 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3001 /* we are using an ECC cipher */
3003 unsigned char *list;
3004 int found_uncompressed = 0;
3005 list = s->session->tlsext_ecpointformatlist;
3006 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3008 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3010 found_uncompressed = 1;
3014 if (!found_uncompressed)
3016 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3020 ret = SSL_TLSEXT_ERR_OK;
3021 #endif /* OPENSSL_NO_EC */
3023 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3024 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3025 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3026 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3028 #ifdef TLSEXT_TYPE_opaque_prf_input
3029 if (s->s3->server_opaque_prf_input_len > 0)
3031 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
3032 * So first verify that we really have a value from the server too. */
3034 if (s->s3->server_opaque_prf_input == NULL)
3036 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3037 al = SSL_AD_HANDSHAKE_FAILURE;
3040 /* Anytime the server *has* sent an opaque PRF input, we need to check
3041 * that we have a client opaque PRF input of the same size. */
3042 if (s->s3->client_opaque_prf_input == NULL ||
3043 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
3045 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3046 al = SSL_AD_ILLEGAL_PARAMETER;
3051 /* If we've requested certificate status and we wont get one
3054 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3055 && s->ctx && s->ctx->tlsext_status_cb)
3058 /* Set resp to NULL, resplen to -1 so callback knows
3059 * there is no response.
3061 if (s->tlsext_ocsp_resp)
3063 OPENSSL_free(s->tlsext_ocsp_resp);
3064 s->tlsext_ocsp_resp = NULL;
3066 s->tlsext_ocsp_resplen = -1;
3067 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3070 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3071 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3075 al = SSL_AD_INTERNAL_ERROR;
3076 ret = SSL_TLSEXT_ERR_ALERT_FATAL;