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) */
248 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
249 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
250 NID_brainpoolP512r1 /* brainpool512r1 (28) */
254 static const unsigned char ecformats_default[] =
256 TLSEXT_ECPOINTFORMAT_uncompressed,
257 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
258 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
261 static const unsigned char eccurves_default[] =
263 0,14, /* sect571r1 (14) */
264 0,13, /* sect571k1 (13) */
265 0,25, /* secp521r1 (25) */
266 0,28, /* brainpool512r1 (28) */
267 0,11, /* sect409k1 (11) */
268 0,12, /* sect409r1 (12) */
269 0,27, /* brainpoolP384r1 (27) */
270 0,24, /* secp384r1 (24) */
271 0,9, /* sect283k1 (9) */
272 0,10, /* sect283r1 (10) */
273 0,26, /* brainpoolP256r1 (26) */
274 0,22, /* secp256k1 (22) */
275 0,23, /* secp256r1 (23) */
276 0,8, /* sect239k1 (8) */
277 0,6, /* sect233k1 (6) */
278 0,7, /* sect233r1 (7) */
279 0,20, /* secp224k1 (20) */
280 0,21, /* secp224r1 (21) */
281 0,4, /* sect193r1 (4) */
282 0,5, /* sect193r2 (5) */
283 0,18, /* secp192k1 (18) */
284 0,19, /* secp192r1 (19) */
285 0,1, /* sect163k1 (1) */
286 0,2, /* sect163r1 (2) */
287 0,3, /* sect163r2 (3) */
288 0,15, /* secp160k1 (15) */
289 0,16, /* secp160r1 (16) */
290 0,17, /* secp160r2 (17) */
293 static const unsigned char suiteb_curves[] =
295 0, TLSEXT_curve_P_256,
296 0, TLSEXT_curve_P_384
299 int tls1_ec_curve_id2nid(int curve_id)
301 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
302 if ((curve_id < 1) || ((unsigned int)curve_id >
303 sizeof(nid_list)/sizeof(nid_list[0])))
305 return nid_list[curve_id-1];
308 int tls1_ec_nid2curve_id(int nid)
310 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
313 case NID_sect163k1: /* sect163k1 (1) */
315 case NID_sect163r1: /* sect163r1 (2) */
317 case NID_sect163r2: /* sect163r2 (3) */
319 case NID_sect193r1: /* sect193r1 (4) */
321 case NID_sect193r2: /* sect193r2 (5) */
323 case NID_sect233k1: /* sect233k1 (6) */
325 case NID_sect233r1: /* sect233r1 (7) */
327 case NID_sect239k1: /* sect239k1 (8) */
329 case NID_sect283k1: /* sect283k1 (9) */
331 case NID_sect283r1: /* sect283r1 (10) */
333 case NID_sect409k1: /* sect409k1 (11) */
335 case NID_sect409r1: /* sect409r1 (12) */
337 case NID_sect571k1: /* sect571k1 (13) */
339 case NID_sect571r1: /* sect571r1 (14) */
341 case NID_secp160k1: /* secp160k1 (15) */
343 case NID_secp160r1: /* secp160r1 (16) */
345 case NID_secp160r2: /* secp160r2 (17) */
347 case NID_secp192k1: /* secp192k1 (18) */
349 case NID_X9_62_prime192v1: /* secp192r1 (19) */
351 case NID_secp224k1: /* secp224k1 (20) */
353 case NID_secp224r1: /* secp224r1 (21) */
355 case NID_secp256k1: /* secp256k1 (22) */
357 case NID_X9_62_prime256v1: /* secp256r1 (23) */
359 case NID_secp384r1: /* secp384r1 (24) */
361 case NID_secp521r1: /* secp521r1 (25) */
367 /* Get curves list, if "sess" is set return client curves otherwise
370 static void tls1_get_curvelist(SSL *s, int sess,
371 const unsigned char **pcurves,
376 *pcurves = s->session->tlsext_ellipticcurvelist;
377 *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
380 /* For Suite B mode only include P-256, P-384 */
381 switch (tls1_suiteb(s))
383 case SSL_CERT_FLAG_SUITEB_128_LOS:
384 *pcurves = suiteb_curves;
385 *pcurveslen = sizeof(suiteb_curves);
388 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
389 *pcurves = suiteb_curves;
393 case SSL_CERT_FLAG_SUITEB_192_LOS:
394 *pcurves = suiteb_curves + 2;
398 *pcurves = s->tlsext_ellipticcurvelist;
399 *pcurveslen = s->tlsext_ellipticcurvelist_length;
403 *pcurves = eccurves_default;
404 *pcurveslen = sizeof(eccurves_default);
407 /* Check a curve is one of our preferences */
408 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
410 const unsigned char *curves;
412 unsigned int suiteb_flags = tls1_suiteb(s);
413 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
415 /* Check curve matches Suite B preferences */
418 unsigned long cid = s->s3->tmp.new_cipher->id;
421 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
423 if (p[2] != TLSEXT_curve_P_256)
426 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
428 if (p[2] != TLSEXT_curve_P_384)
431 else /* Should never happen */
434 tls1_get_curvelist(s, 0, &curves, &curveslen);
435 for (i = 0; i < curveslen; i += 2, curves += 2)
437 if (p[1] == curves[0] && p[2] == curves[1])
443 /* Return nth shared curve. If nmatch == -1 return number of
444 * matches. For nmatch == -2 return the NID of the curve to use for
448 int tls1_shared_curve(SSL *s, int nmatch)
450 const unsigned char *pref, *supp;
451 size_t preflen, supplen, i, j;
453 /* Can't do anything on client side */
460 /* For Suite B ciphersuite determines curve: we
461 * already know these are acceptable due to previous
464 unsigned long cid = s->s3->tmp.new_cipher->id;
465 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
466 return NID_X9_62_prime256v1; /* P-256 */
467 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
468 return NID_secp384r1; /* P-384 */
469 /* Should never happen */
472 /* If not Suite B just return first preference shared curve */
475 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
477 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
482 for (i = 0; i < preflen; i++, pref+=2)
484 const unsigned char *tsupp = supp;
485 for (j = 0; j < supplen; j++, tsupp+=2)
487 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
491 int id = (pref[0] << 8) | pref[1];
492 return tls1_ec_curve_id2nid(id);
503 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
504 int *curves, size_t ncurves)
506 unsigned char *clist, *p;
508 /* Bitmap of curves included to detect duplicates: only works
509 * while curve ids < 32
511 unsigned long dup_list = 0;
512 clist = OPENSSL_malloc(ncurves * 2);
515 for (i = 0, p = clist; i < ncurves; i++)
517 unsigned long idmask;
519 id = tls1_ec_nid2curve_id(curves[i]);
521 if (!id || (dup_list & idmask))
532 *pextlen = ncurves * 2;
536 #define MAX_CURVELIST 25
541 int nid_arr[MAX_CURVELIST];
544 static int nid_cb(const char *elem, int len, void *arg)
546 nid_cb_st *narg = arg;
550 if (narg->nidcnt == MAX_CURVELIST)
552 if (len > (int)(sizeof(etmp) - 1))
554 memcpy(etmp, elem, len);
556 nid = EC_curve_nist2nid(etmp);
557 if (nid == NID_undef)
558 nid = OBJ_sn2nid(etmp);
559 if (nid == NID_undef)
560 nid = OBJ_ln2nid(etmp);
561 if (nid == NID_undef)
563 for (i = 0; i < narg->nidcnt; i++)
564 if (narg->nid_arr[i] == nid)
566 narg->nid_arr[narg->nidcnt++] = nid;
569 /* Set curves based on a colon separate list */
570 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
575 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
579 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
581 /* For an EC key set TLS id and required compression based on parameters */
582 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
588 const EC_METHOD *meth;
591 /* Determine if it is a prime field */
592 grp = EC_KEY_get0_group(ec);
593 pt = EC_KEY_get0_public_key(ec);
596 meth = EC_GROUP_method_of(grp);
599 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
603 /* Determine curve ID */
604 id = EC_GROUP_get_curve_name(grp);
605 id = tls1_ec_nid2curve_id(id);
606 /* If we have an ID set it, otherwise set arbitrary explicit curve */
610 curve_id[1] = (unsigned char)id;
622 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
625 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
627 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
630 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
634 /* Check an EC key is compatible with extensions */
635 static int tls1_check_ec_key(SSL *s,
636 unsigned char *curve_id, unsigned char *comp_id)
638 const unsigned char *p;
641 /* If point formats extension present check it, otherwise everything
642 * is supported (see RFC4492).
644 if (comp_id && s->session->tlsext_ecpointformatlist)
646 p = s->session->tlsext_ecpointformatlist;
647 plen = s->session->tlsext_ecpointformatlist_length;
648 for (i = 0; i < plen; i++, p++)
658 /* Check curve is consistent with client and server preferences */
659 for (j = 0; j <= 1; j++)
661 tls1_get_curvelist(s, j, &p, &plen);
662 for (i = 0; i < plen; i+=2, p+=2)
664 if (p[0] == curve_id[0] && p[1] == curve_id[1])
669 /* For clients can only check sent curve list */
676 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
679 /* If we have a custom point format list use it otherwise
681 if (s->tlsext_ecpointformatlist)
683 *pformats = s->tlsext_ecpointformatlist;
684 *pformatslen = s->tlsext_ecpointformatlist_length;
688 *pformats = ecformats_default;
689 /* For Suite B we don't support char2 fields */
691 *pformatslen = sizeof(ecformats_default) - 1;
693 *pformatslen = sizeof(ecformats_default);
697 /* Check cert parameters compatible with extensions: currently just checks
698 * EC certificates have compatible curves and compression.
700 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
702 unsigned char comp_id, curve_id[2];
705 pkey = X509_get_pubkey(x);
708 /* If not EC nothing to do */
709 if (pkey->type != EVP_PKEY_EC)
714 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
718 /* Can't check curve_id for client certs as we don't have a
719 * supported curves extension.
721 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
724 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
725 * SHA384+P-384, adjust digest if necessary.
727 if (set_ee_md && tls1_suiteb(s))
734 /* Check to see we have necessary signing algorithm */
735 if (curve_id[1] == TLSEXT_curve_P_256)
736 check_md = NID_ecdsa_with_SHA256;
737 else if (curve_id[1] == TLSEXT_curve_P_384)
738 check_md = NID_ecdsa_with_SHA384;
740 return 0; /* Should never happen */
741 for (i = 0; i < c->shared_sigalgslen; i++)
742 if (check_md == c->shared_sigalgs[i].signandhash_nid)
744 if (i == c->shared_sigalgslen)
748 if (check_md == NID_ecdsa_with_SHA256)
749 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
751 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
756 /* Check EC temporary key is compatible with client extensions */
757 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
759 unsigned char curve_id[2];
760 EC_KEY *ec = s->cert->ecdh_tmp;
761 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
762 /* Allow any curve: not just those peer supports */
763 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
766 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
767 * no other curves permitted.
771 /* Curve to check determined by ciphersuite */
772 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
773 curve_id[1] = TLSEXT_curve_P_256;
774 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
775 curve_id[1] = TLSEXT_curve_P_384;
779 /* Check this curve is acceptable */
780 if (!tls1_check_ec_key(s, curve_id, NULL))
782 /* If auto or setting curve from callback assume OK */
783 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
785 /* Otherwise check curve is acceptable */
788 unsigned char curve_tmp[2];
791 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
793 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
799 if (s->cert->ecdh_tmp_auto)
801 /* Need a shared curve */
802 if (tls1_shared_curve(s, 0))
808 if (s->cert->ecdh_tmp_cb)
813 if (!tls1_set_ec_id(curve_id, NULL, ec))
815 /* Set this to allow use of invalid curves for testing */
819 return tls1_check_ec_key(s, curve_id, NULL);
825 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
830 #endif /* OPENSSL_NO_EC */
832 #ifndef OPENSSL_NO_TLSEXT
834 /* List of supported signature algorithms and hashes. Should make this
835 * customisable at some point, for now include everything we support.
838 #ifdef OPENSSL_NO_RSA
839 #define tlsext_sigalg_rsa(md) /* */
841 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
844 #ifdef OPENSSL_NO_DSA
845 #define tlsext_sigalg_dsa(md) /* */
847 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
850 #ifdef OPENSSL_NO_ECDSA
851 #define tlsext_sigalg_ecdsa(md) /* */
853 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
856 #define tlsext_sigalg(md) \
857 tlsext_sigalg_rsa(md) \
858 tlsext_sigalg_dsa(md) \
859 tlsext_sigalg_ecdsa(md)
861 static unsigned char tls12_sigalgs[] = {
862 #ifndef OPENSSL_NO_SHA512
863 tlsext_sigalg(TLSEXT_hash_sha512)
864 tlsext_sigalg(TLSEXT_hash_sha384)
866 #ifndef OPENSSL_NO_SHA256
867 tlsext_sigalg(TLSEXT_hash_sha256)
868 tlsext_sigalg(TLSEXT_hash_sha224)
870 #ifndef OPENSSL_NO_SHA
871 tlsext_sigalg(TLSEXT_hash_sha1)
873 #ifndef OPENSSL_NO_MD5
874 tlsext_sigalg_rsa(TLSEXT_hash_md5)
877 #ifndef OPENSSL_NO_ECDSA
878 static unsigned char suiteb_sigalgs[] = {
879 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
880 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
883 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
885 /* If Suite B mode use Suite B sigalgs only, ignore any other
888 #ifndef OPENSSL_NO_EC
889 switch (tls1_suiteb(s))
891 case SSL_CERT_FLAG_SUITEB_128_LOS:
892 *psigs = suiteb_sigalgs;
893 return sizeof(suiteb_sigalgs);
895 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
896 *psigs = suiteb_sigalgs;
899 case SSL_CERT_FLAG_SUITEB_192_LOS:
900 *psigs = suiteb_sigalgs + 2;
904 /* If server use client authentication sigalgs if not NULL */
905 if (s->server && s->cert->client_sigalgs)
907 *psigs = s->cert->client_sigalgs;
908 return s->cert->client_sigalgslen;
910 else if (s->cert->conf_sigalgs)
912 *psigs = s->cert->conf_sigalgs;
913 return s->cert->conf_sigalgslen;
917 *psigs = tls12_sigalgs;
919 /* If FIPS mode don't include MD5 which is last */
921 return sizeof(tls12_sigalgs) - 2;
924 return sizeof(tls12_sigalgs);
927 /* Check signature algorithm is consistent with sent supported signature
928 * algorithms and if so return relevant digest.
930 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
931 const unsigned char *sig, EVP_PKEY *pkey)
933 const unsigned char *sent_sigs;
934 size_t sent_sigslen, i;
935 int sigalg = tls12_get_sigid(pkey);
936 /* Should never happen */
939 /* Check key type is consistent with signature */
940 if (sigalg != (int)sig[1])
942 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
945 #ifndef OPENSSL_NO_EC
946 if (pkey->type == EVP_PKEY_EC)
948 unsigned char curve_id[2], comp_id;
949 /* Check compression and curve matches extensions */
950 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
952 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
954 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
957 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
962 if (curve_id[1] == TLSEXT_curve_P_256)
964 if (sig[0] != TLSEXT_hash_sha256)
966 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
967 SSL_R_ILLEGAL_SUITEB_DIGEST);
971 else if (curve_id[1] == TLSEXT_curve_P_384)
973 if (sig[0] != TLSEXT_hash_sha384)
975 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
976 SSL_R_ILLEGAL_SUITEB_DIGEST);
984 else if (tls1_suiteb(s))
988 /* Check signature matches a type we sent */
989 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
990 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
992 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
995 /* Allow fallback to SHA1 if not strict mode */
996 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
998 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1001 *pmd = tls12_get_hash(sig[0]);
1004 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
1007 /* Store the digest used so applications can retrieve it if they
1010 if (s->session && s->session->sess_cert)
1011 s->session->sess_cert->peer_key->digest = *pmd;
1014 /* Get a mask of disabled algorithms: an algorithm is disabled
1015 * if it isn't supported or doesn't appear in supported signature
1016 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1017 * session and not global settings.
1020 void ssl_set_client_disabled(SSL *s)
1023 const unsigned char *sigalgs;
1024 size_t i, sigalgslen;
1025 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1028 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1029 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1030 c->mask_ssl = SSL_TLSV1_2;
1033 /* Now go through all signature algorithms seeing if we support
1034 * any for RSA, DSA, ECDSA. Do this for all versions not just
1037 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
1038 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
1042 #ifndef OPENSSL_NO_RSA
1043 case TLSEXT_signature_rsa:
1047 #ifndef OPENSSL_NO_DSA
1048 case TLSEXT_signature_dsa:
1052 #ifndef OPENSSL_NO_ECDSA
1053 case TLSEXT_signature_ecdsa:
1059 /* Disable auth and static DH if we don't include any appropriate
1060 * signature algorithms.
1064 c->mask_a |= SSL_aRSA;
1065 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1069 c->mask_a |= SSL_aDSS;
1070 c->mask_k |= SSL_kDHd;
1074 c->mask_a |= SSL_aECDSA;
1075 c->mask_k |= SSL_kECDHe;
1077 #ifndef OPENSSL_NO_KRB5
1078 if (!kssl_tgt_is_available(s->kssl_ctx))
1080 c->mask_a |= SSL_aKRB5;
1081 c->mask_k |= SSL_kKRB5;
1084 #ifndef OPENSSL_NO_PSK
1085 /* with PSK there must be client callback set */
1086 if (!s->psk_client_callback)
1088 c->mask_a |= SSL_aPSK;
1089 c->mask_k |= SSL_kPSK;
1091 #endif /* OPENSSL_NO_PSK */
1095 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1098 unsigned char *ret = p;
1099 #ifndef OPENSSL_NO_EC
1100 /* See if we support any ECC ciphersuites */
1102 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1105 unsigned long alg_k, alg_a;
1106 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1108 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1110 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1112 alg_k = c->algorithm_mkey;
1113 alg_a = c->algorithm_auth;
1114 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
1115 || (alg_a & SSL_aECDSA)))
1124 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1125 if (s->client_version == SSL3_VERSION
1126 && !s->s3->send_connection_binding)
1131 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1133 if (s->tlsext_hostname != NULL)
1135 /* Add TLS extension servername to the Client Hello message */
1136 unsigned long size_str;
1139 /* check for enough space.
1140 4 for the servername type and entension length
1141 2 for servernamelist length
1142 1 for the hostname type
1143 2 for hostname length
1147 if ((lenmax = limit - ret - 9) < 0
1148 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1151 /* extension type and length */
1152 s2n(TLSEXT_TYPE_server_name,ret);
1153 s2n(size_str+5,ret);
1155 /* length of servername list */
1156 s2n(size_str+3,ret);
1158 /* hostname type, length and hostname */
1159 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1161 memcpy(ret, s->tlsext_hostname, size_str);
1165 /* Add RI if renegotiating */
1170 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1172 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1176 if((limit - p - 4 - el) < 0) return NULL;
1178 s2n(TLSEXT_TYPE_renegotiate,ret);
1181 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1183 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1190 #ifndef OPENSSL_NO_SRP
1191 /* Add SRP username if there is one */
1192 if (s->srp_ctx.login != NULL)
1193 { /* Add TLS extension SRP username to the Client Hello message */
1195 int login_len = strlen(s->srp_ctx.login);
1196 if (login_len > 255 || login_len == 0)
1198 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1202 /* check for enough space.
1203 4 for the srp type type and entension length
1204 1 for the srp user identity
1205 + srp user identity length
1207 if ((limit - ret - 5 - login_len) < 0) return NULL;
1209 /* fill in the extension */
1210 s2n(TLSEXT_TYPE_srp,ret);
1211 s2n(login_len+1,ret);
1212 (*ret++) = (unsigned char) login_len;
1213 memcpy(ret, s->srp_ctx.login, login_len);
1218 #ifndef OPENSSL_NO_EC
1221 /* Add TLS extension ECPointFormats to the ClientHello message */
1223 const unsigned char *plist;
1226 tls1_get_formatlist(s, &plist, &plistlen);
1228 if ((lenmax = limit - ret - 5) < 0) return NULL;
1229 if (plistlen > (size_t)lenmax) return NULL;
1232 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1236 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1237 s2n(plistlen + 1,ret);
1238 *(ret++) = (unsigned char)plistlen ;
1239 memcpy(ret, plist, plistlen);
1242 /* Add TLS extension EllipticCurves to the ClientHello message */
1243 plist = s->tlsext_ellipticcurvelist;
1244 tls1_get_curvelist(s, 0, &plist, &plistlen);
1246 if ((lenmax = limit - ret - 6) < 0) return NULL;
1247 if (plistlen > (size_t)lenmax) return NULL;
1248 if (plistlen > 65532)
1250 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1254 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1255 s2n(plistlen + 2, ret);
1257 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1258 * elliptic_curve_list, but the examples use two bytes.
1259 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1260 * resolves this to two bytes.
1263 memcpy(ret, plist, plistlen);
1266 #endif /* OPENSSL_NO_EC */
1268 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1271 if (!s->new_session && s->session && s->session->tlsext_tick)
1272 ticklen = s->session->tlsext_ticklen;
1273 else if (s->session && s->tlsext_session_ticket &&
1274 s->tlsext_session_ticket->data)
1276 ticklen = s->tlsext_session_ticket->length;
1277 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1278 if (!s->session->tlsext_tick)
1280 memcpy(s->session->tlsext_tick,
1281 s->tlsext_session_ticket->data,
1283 s->session->tlsext_ticklen = ticklen;
1287 if (ticklen == 0 && s->tlsext_session_ticket &&
1288 s->tlsext_session_ticket->data == NULL)
1290 /* Check for enough room 2 for extension type, 2 for len
1293 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1294 s2n(TLSEXT_TYPE_session_ticket,ret);
1298 memcpy(ret, s->session->tlsext_tick, ticklen);
1304 if (SSL_USE_SIGALGS(s))
1307 const unsigned char *salg;
1308 salglen = tls12_get_psigalgs(s, &salg);
1309 if ((size_t)(limit - ret) < salglen + 6)
1311 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1312 s2n(salglen + 2, ret);
1314 memcpy(ret, salg, salglen);
1318 #ifdef TLSEXT_TYPE_opaque_prf_input
1319 if (s->s3->client_opaque_prf_input != NULL)
1321 size_t col = s->s3->client_opaque_prf_input_len;
1323 if ((long)(limit - ret - 6 - col < 0))
1325 if (col > 0xFFFD) /* can't happen */
1328 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1331 memcpy(ret, s->s3->client_opaque_prf_input, col);
1336 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1339 long extlen, idlen, itmp;
1343 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1345 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1346 itmp = i2d_OCSP_RESPID(id, NULL);
1352 if (s->tlsext_ocsp_exts)
1354 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1361 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1362 s2n(TLSEXT_TYPE_status_request, ret);
1363 if (extlen + idlen > 0xFFF0)
1365 s2n(extlen + idlen + 5, ret);
1366 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1368 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1370 /* save position of id len */
1371 unsigned char *q = ret;
1372 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1373 /* skip over id len */
1375 itmp = i2d_OCSP_RESPID(id, &ret);
1381 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1384 #ifndef OPENSSL_NO_HEARTBEATS
1385 /* Add Heartbeat extension */
1386 s2n(TLSEXT_TYPE_heartbeat,ret);
1389 * 1: peer may send requests
1390 * 2: peer not allowed to send requests
1392 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1393 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1395 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1398 #ifndef OPENSSL_NO_NEXTPROTONEG
1399 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1401 /* The client advertises an emtpy extension to indicate its
1402 * support for Next Protocol Negotiation */
1403 if (limit - ret - 4 < 0)
1405 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1410 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1412 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1414 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1415 s2n(2 + s->alpn_client_proto_list_len,ret);
1416 s2n(s->alpn_client_proto_list_len,ret);
1417 memcpy(ret, s->alpn_client_proto_list,
1418 s->alpn_client_proto_list_len);
1419 ret += s->alpn_client_proto_list_len;
1422 if(SSL_get_srtp_profiles(s))
1426 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1428 if((limit - p - 4 - el) < 0) return NULL;
1430 s2n(TLSEXT_TYPE_use_srtp,ret);
1433 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1435 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1441 /* Add custom TLS Extensions to ClientHello */
1442 if (s->ctx->custom_cli_ext_records_count)
1445 custom_cli_ext_record* record;
1447 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
1449 const unsigned char* out = NULL;
1450 unsigned short outlen = 0;
1452 record = &s->ctx->custom_cli_ext_records[i];
1453 /* NULL callback sends empty extension */
1454 /* -1 from callback omits extension */
1458 cb_retval = record->fn1(s, record->ext_type,
1462 return NULL; /* error */
1463 if (cb_retval == -1)
1464 continue; /* skip this extension */
1466 if (limit < ret + 4 + outlen)
1468 s2n(record->ext_type, ret);
1470 memcpy(ret, out, outlen);
1474 #ifdef TLSEXT_TYPE_encrypt_then_mac
1475 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1479 if ((extdatalen = ret-p-2) == 0)
1486 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1489 unsigned char *ret = p;
1490 #ifndef OPENSSL_NO_NEXTPROTONEG
1491 int next_proto_neg_seen;
1493 #ifndef OPENSSL_NO_EC
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 custom types were sent in ClientHello, add ServerHello responses */
1674 if (s->s3->tlsext_custom_types_count)
1678 for (i = 0; i < s->s3->tlsext_custom_types_count; i++)
1681 custom_srv_ext_record *record;
1683 for (j = 0; j < s->ctx->custom_srv_ext_records_count; j++)
1685 record = &s->ctx->custom_srv_ext_records[j];
1686 if (s->s3->tlsext_custom_types[i] == record->ext_type)
1688 const unsigned char *out = NULL;
1689 unsigned short outlen = 0;
1692 /* NULL callback or -1 omits extension */
1695 cb_retval = record->fn2(s, record->ext_type,
1699 return NULL; /* error */
1700 if (cb_retval == -1)
1701 break; /* skip this extension */
1702 if (limit < ret + 4 + outlen)
1704 s2n(record->ext_type, ret);
1706 memcpy(ret, out, outlen);
1713 #ifdef TLSEXT_TYPE_encrypt_then_mac
1714 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC)
1716 /* Don't use encrypt_then_mac if AEAD: might want
1717 * to disable for other ciphersuites too.
1719 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD)
1720 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1723 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1729 if (s->s3->alpn_selected)
1731 const unsigned char *selected = s->s3->alpn_selected;
1732 unsigned len = s->s3->alpn_selected_len;
1734 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1736 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1740 memcpy(ret, selected, len);
1744 if ((extdatalen = ret-p-2)== 0)
1751 /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1753 * data: the contents of the extension, not including the type and length.
1754 * data_len: the number of bytes in |data|
1755 * al: a pointer to the alert value to send in the event of a non-zero
1758 * returns: 0 on success. */
1759 static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
1760 unsigned data_len, int *al)
1764 const unsigned char *selected;
1765 unsigned char selected_len;
1768 if (s->ctx->alpn_select_cb == NULL)
1774 /* data should contain a uint16 length followed by a series of 8-bit,
1775 * length-prefixed strings. */
1776 i = ((unsigned) data[0]) << 8 |
1777 ((unsigned) data[1]);
1786 for (i = 0; i < data_len;)
1788 proto_len = data[i];
1794 if (i + proto_len < i || i + proto_len > data_len)
1800 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1801 s->ctx->alpn_select_cb_arg);
1802 if (r == SSL_TLSEXT_ERR_OK) {
1803 if (s->s3->alpn_selected)
1804 OPENSSL_free(s->s3->alpn_selected);
1805 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1806 if (!s->s3->alpn_selected)
1808 *al = SSL_AD_INTERNAL_ERROR;
1811 memcpy(s->s3->alpn_selected, selected, selected_len);
1812 s->s3->alpn_selected_len = selected_len;
1817 *al = SSL_AD_DECODE_ERROR;
1821 #ifndef OPENSSL_NO_EC
1822 /* ssl_check_for_safari attempts to fingerprint Safari using OS X
1823 * SecureTransport using the TLS extension block in |d|, of length |n|.
1824 * Safari, since 10.6, sends exactly these extensions, in this order:
1829 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1830 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1831 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1832 * 10.8..10.8.3 (which don't work).
1834 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
1835 unsigned short type, size;
1836 static const unsigned char kSafariExtensionsBlock[] = {
1837 0x00, 0x0a, /* elliptic_curves extension */
1838 0x00, 0x08, /* 8 bytes */
1839 0x00, 0x06, /* 6 bytes of curve ids */
1840 0x00, 0x17, /* P-256 */
1841 0x00, 0x18, /* P-384 */
1842 0x00, 0x19, /* P-521 */
1844 0x00, 0x0b, /* ec_point_formats */
1845 0x00, 0x02, /* 2 bytes */
1846 0x01, /* 1 point format */
1847 0x00, /* uncompressed */
1850 /* The following is only present in TLS 1.2 */
1851 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1852 0x00, 0x0d, /* signature_algorithms */
1853 0x00, 0x0c, /* 12 bytes */
1854 0x00, 0x0a, /* 10 bytes */
1855 0x05, 0x01, /* SHA-384/RSA */
1856 0x04, 0x01, /* SHA-256/RSA */
1857 0x02, 0x01, /* SHA-1/RSA */
1858 0x04, 0x03, /* SHA-256/ECDSA */
1859 0x02, 0x03, /* SHA-1/ECDSA */
1862 if (data >= (d+n-2))
1871 if (type != TLSEXT_TYPE_server_name)
1874 if (data+size > d+n)
1878 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1880 const size_t len1 = sizeof(kSafariExtensionsBlock);
1881 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1883 if (data + len1 + len2 != d+n)
1885 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1887 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1892 const size_t len = sizeof(kSafariExtensionsBlock);
1894 if (data + len != d+n)
1896 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
1900 s->s3->is_probably_safari = 1;
1902 #endif /* !OPENSSL_NO_EC */
1904 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1906 unsigned short type;
1907 unsigned short size;
1909 unsigned char *data = *p;
1910 int renegotiate_seen = 0;
1913 s->servername_done = 0;
1914 s->tlsext_status_type = -1;
1915 #ifndef OPENSSL_NO_NEXTPROTONEG
1916 s->s3->next_proto_neg_seen = 0;
1919 if (s->s3->alpn_selected)
1921 OPENSSL_free(s->s3->alpn_selected);
1922 s->s3->alpn_selected = NULL;
1925 /* Clear observed custom extensions */
1926 s->s3->tlsext_custom_types_count = 0;
1927 if (s->s3->tlsext_custom_types != NULL)
1929 OPENSSL_free(s->s3->tlsext_custom_types);
1930 s->s3->tlsext_custom_types = NULL;
1933 #ifndef OPENSSL_NO_HEARTBEATS
1934 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1935 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1938 #ifndef OPENSSL_NO_EC
1939 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1940 ssl_check_for_safari(s, data, d, n);
1941 #endif /* !OPENSSL_NO_EC */
1943 /* Clear any signature algorithms extension received */
1944 if (s->cert->peer_sigalgs)
1946 OPENSSL_free(s->cert->peer_sigalgs);
1947 s->cert->peer_sigalgs = NULL;
1949 /* Clear any shared sigtnature algorithms */
1950 if (s->cert->shared_sigalgs)
1952 OPENSSL_free(s->cert->shared_sigalgs);
1953 s->cert->shared_sigalgs = NULL;
1955 /* Clear certificate digests and validity flags */
1956 for (i = 0; i < SSL_PKEY_NUM; i++)
1958 s->cert->pkeys[i].digest = NULL;
1959 s->cert->pkeys[i].valid_flags = 0;
1962 #ifdef TLSEXT_TYPE_encrypt_then_mac
1963 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1966 if (data >= (d+n-2))
1970 if (data > (d+n-len))
1973 while (data <= (d+n-4))
1978 if (data+size > (d+n))
1981 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1983 if (s->tlsext_debug_cb)
1984 s->tlsext_debug_cb(s, 0, type, data, size,
1985 s->tlsext_debug_arg);
1986 /* The servername extension is treated as follows:
1988 - Only the hostname type is supported with a maximum length of 255.
1989 - The servername is rejected if too long or if it contains zeros,
1990 in which case an fatal alert is generated.
1991 - The servername field is maintained together with the session cache.
1992 - When a session is resumed, the servername call back invoked in order
1993 to allow the application to position itself to the right context.
1994 - The servername is acknowledged if it is new for a session or when
1995 it is identical to a previously used for the same session.
1996 Applications can control the behaviour. They can at any time
1997 set a 'desirable' servername for a new SSL object. This can be the
1998 case for example with HTTPS when a Host: header field is received and
1999 a renegotiation is requested. In this case, a possible servername
2000 presented in the new client hello is only acknowledged if it matches
2001 the value of the Host: field.
2002 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2003 if they provide for changing an explicit servername context for the session,
2004 i.e. when the session has been established with a servername extension.
2005 - On session reconnect, the servername extension may be absent.
2009 if (type == TLSEXT_TYPE_server_name)
2011 unsigned char *sdata;
2017 *al = SSL_AD_DECODE_ERROR;
2024 *al = SSL_AD_DECODE_ERROR;
2031 servname_type = *(sdata++);
2037 *al = SSL_AD_DECODE_ERROR;
2040 if (s->servername_done == 0)
2041 switch (servname_type)
2043 case TLSEXT_NAMETYPE_host_name:
2046 if(s->session->tlsext_hostname)
2048 *al = SSL_AD_DECODE_ERROR;
2051 if (len > TLSEXT_MAXLEN_host_name)
2053 *al = TLS1_AD_UNRECOGNIZED_NAME;
2056 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
2058 *al = TLS1_AD_INTERNAL_ERROR;
2061 memcpy(s->session->tlsext_hostname, sdata, len);
2062 s->session->tlsext_hostname[len]='\0';
2063 if (strlen(s->session->tlsext_hostname) != len) {
2064 OPENSSL_free(s->session->tlsext_hostname);
2065 s->session->tlsext_hostname = NULL;
2066 *al = TLS1_AD_UNRECOGNIZED_NAME;
2069 s->servername_done = 1;
2073 s->servername_done = s->session->tlsext_hostname
2074 && strlen(s->session->tlsext_hostname) == len
2075 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2087 *al = SSL_AD_DECODE_ERROR;
2092 #ifndef OPENSSL_NO_SRP
2093 else if (type == TLSEXT_TYPE_srp)
2095 if (size <= 0 || ((len = data[0])) != (size -1))
2097 *al = SSL_AD_DECODE_ERROR;
2100 if (s->srp_ctx.login != NULL)
2102 *al = SSL_AD_DECODE_ERROR;
2105 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
2107 memcpy(s->srp_ctx.login, &data[1], len);
2108 s->srp_ctx.login[len]='\0';
2110 if (strlen(s->srp_ctx.login) != len)
2112 *al = SSL_AD_DECODE_ERROR;
2118 #ifndef OPENSSL_NO_EC
2119 else if (type == TLSEXT_TYPE_ec_point_formats)
2121 unsigned char *sdata = data;
2122 int ecpointformatlist_length = *(sdata++);
2124 if (ecpointformatlist_length != size - 1 ||
2125 ecpointformatlist_length < 1)
2127 *al = TLS1_AD_DECODE_ERROR;
2132 if(s->session->tlsext_ecpointformatlist)
2134 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2135 s->session->tlsext_ecpointformatlist = NULL;
2137 s->session->tlsext_ecpointformatlist_length = 0;
2138 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2140 *al = TLS1_AD_INTERNAL_ERROR;
2143 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2144 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2147 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2148 sdata = s->session->tlsext_ecpointformatlist;
2149 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2150 fprintf(stderr,"%i ",*(sdata++));
2151 fprintf(stderr,"\n");
2154 else if (type == TLSEXT_TYPE_elliptic_curves)
2156 unsigned char *sdata = data;
2157 int ellipticcurvelist_length = (*(sdata++) << 8);
2158 ellipticcurvelist_length += (*(sdata++));
2160 if (ellipticcurvelist_length != size - 2 ||
2161 ellipticcurvelist_length < 1)
2163 *al = TLS1_AD_DECODE_ERROR;
2168 if(s->session->tlsext_ellipticcurvelist)
2170 *al = TLS1_AD_DECODE_ERROR;
2173 s->session->tlsext_ellipticcurvelist_length = 0;
2174 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2176 *al = TLS1_AD_INTERNAL_ERROR;
2179 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2180 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2183 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2184 sdata = s->session->tlsext_ellipticcurvelist;
2185 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2186 fprintf(stderr,"%i ",*(sdata++));
2187 fprintf(stderr,"\n");
2190 #endif /* OPENSSL_NO_EC */
2191 #ifdef TLSEXT_TYPE_opaque_prf_input
2192 else if (type == TLSEXT_TYPE_opaque_prf_input)
2194 unsigned char *sdata = data;
2198 *al = SSL_AD_DECODE_ERROR;
2201 n2s(sdata, s->s3->client_opaque_prf_input_len);
2202 if (s->s3->client_opaque_prf_input_len != size - 2)
2204 *al = SSL_AD_DECODE_ERROR;
2208 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2209 OPENSSL_free(s->s3->client_opaque_prf_input);
2210 if (s->s3->client_opaque_prf_input_len == 0)
2211 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2213 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2214 if (s->s3->client_opaque_prf_input == NULL)
2216 *al = TLS1_AD_INTERNAL_ERROR;
2221 else if (type == TLSEXT_TYPE_session_ticket)
2223 if (s->tls_session_ticket_ext_cb &&
2224 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2226 *al = TLS1_AD_INTERNAL_ERROR;
2230 else if (type == TLSEXT_TYPE_renegotiate)
2232 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2234 renegotiate_seen = 1;
2236 else if (type == TLSEXT_TYPE_signature_algorithms)
2239 if (s->cert->peer_sigalgs || size < 2)
2241 *al = SSL_AD_DECODE_ERROR;
2246 if (dsize != size || dsize & 1 || !dsize)
2248 *al = SSL_AD_DECODE_ERROR;
2251 if (!tls1_process_sigalgs(s, data, dsize))
2253 *al = SSL_AD_DECODE_ERROR;
2256 /* If sigalgs received and no shared algorithms fatal
2259 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
2261 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2262 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2263 *al = SSL_AD_ILLEGAL_PARAMETER;
2267 else if (type == TLSEXT_TYPE_status_request
2268 && s->ctx->tlsext_status_cb)
2273 *al = SSL_AD_DECODE_ERROR;
2277 s->tlsext_status_type = *data++;
2279 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2281 const unsigned char *sdata;
2283 /* Read in responder_id_list */
2288 *al = SSL_AD_DECODE_ERROR;
2297 *al = SSL_AD_DECODE_ERROR;
2301 dsize -= 2 + idsize;
2305 *al = SSL_AD_DECODE_ERROR;
2310 id = d2i_OCSP_RESPID(NULL,
2314 *al = SSL_AD_DECODE_ERROR;
2319 OCSP_RESPID_free(id);
2320 *al = SSL_AD_DECODE_ERROR;
2323 if (!s->tlsext_ocsp_ids
2324 && !(s->tlsext_ocsp_ids =
2325 sk_OCSP_RESPID_new_null()))
2327 OCSP_RESPID_free(id);
2328 *al = SSL_AD_INTERNAL_ERROR;
2331 if (!sk_OCSP_RESPID_push(
2332 s->tlsext_ocsp_ids, id))
2334 OCSP_RESPID_free(id);
2335 *al = SSL_AD_INTERNAL_ERROR;
2340 /* Read in request_extensions */
2343 *al = SSL_AD_DECODE_ERROR;
2350 *al = SSL_AD_DECODE_ERROR;
2356 if (s->tlsext_ocsp_exts)
2358 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2359 X509_EXTENSION_free);
2362 s->tlsext_ocsp_exts =
2363 d2i_X509_EXTENSIONS(NULL,
2365 if (!s->tlsext_ocsp_exts
2366 || (data + dsize != sdata))
2368 *al = SSL_AD_DECODE_ERROR;
2373 /* We don't know what to do with any other type
2377 s->tlsext_status_type = -1;
2379 #ifndef OPENSSL_NO_HEARTBEATS
2380 else if (type == TLSEXT_TYPE_heartbeat)
2384 case 0x01: /* Client allows us to send HB requests */
2385 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2387 case 0x02: /* Client doesn't accept HB requests */
2388 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2389 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2391 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2396 #ifndef OPENSSL_NO_NEXTPROTONEG
2397 else if (type == TLSEXT_TYPE_next_proto_neg &&
2398 s->s3->tmp.finish_md_len == 0 &&
2399 s->s3->alpn_selected == NULL)
2401 /* We shouldn't accept this extension on a
2404 * s->new_session will be set on renegotiation, but we
2405 * probably shouldn't rely that it couldn't be set on
2406 * the initial renegotation too in certain cases (when
2407 * there's some other reason to disallow resuming an
2408 * earlier session -- the current code won't be doing
2409 * anything like that, but this might change).
2411 * A valid sign that there's been a previous handshake
2412 * in this connection is if s->s3->tmp.finish_md_len >
2413 * 0. (We are talking about a check that will happen
2414 * in the Hello protocol round, well before a new
2415 * Finished message could have been computed.) */
2416 s->s3->next_proto_neg_seen = 1;
2420 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2421 s->ctx->alpn_select_cb &&
2422 s->s3->tmp.finish_md_len == 0)
2424 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2426 /* ALPN takes precedence over NPN. */
2427 s->s3->next_proto_neg_seen = 0;
2430 /* session ticket processed earlier */
2431 else if (type == TLSEXT_TYPE_use_srtp)
2433 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2437 /* If this ClientHello extension was unhandled and this is
2438 * a nonresumed connection, check whether the extension is a
2439 * custom TLS Extension (has a custom_srv_ext_record), and if
2440 * so call the callback and record the extension number so that
2441 * an appropriate ServerHello may be later returned.
2443 else if (!s->hit && s->ctx->custom_srv_ext_records_count)
2445 custom_srv_ext_record *record;
2447 for (i=0; i < s->ctx->custom_srv_ext_records_count; i++)
2449 record = &s->ctx->custom_srv_ext_records[i];
2450 if (type == record->ext_type)
2454 /* Error on duplicate TLS Extensions */
2455 for (j = 0; j < s->s3->tlsext_custom_types_count; j++)
2457 if (type == s->s3->tlsext_custom_types[j])
2459 *al = TLS1_AD_DECODE_ERROR;
2464 /* NULL callback still notes the extension */
2465 if (record->fn1 && !record->fn1(s, type, data, size, al, record->arg))
2468 /* Add the (non-duplicated) entry */
2469 s->s3->tlsext_custom_types_count++;
2470 s->s3->tlsext_custom_types = OPENSSL_realloc(
2471 s->s3->tlsext_custom_types,
2472 s->s3->tlsext_custom_types_count * 2);
2473 if (s->s3->tlsext_custom_types == NULL)
2475 s->s3->tlsext_custom_types = 0;
2476 *al = TLS1_AD_INTERNAL_ERROR;
2479 s->s3->tlsext_custom_types[
2480 s->s3->tlsext_custom_types_count - 1] = type;
2484 #ifdef TLSEXT_TYPE_encrypt_then_mac
2485 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2486 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2496 /* Need RI if renegotiating */
2498 if (!renegotiate_seen && s->renegotiate &&
2499 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2501 *al = SSL_AD_HANDSHAKE_FAILURE;
2502 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2503 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2506 /* If no signature algorithms extension set default values */
2507 if (!s->cert->peer_sigalgs)
2508 ssl_cert_set_default_md(s->cert);
2513 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2516 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2518 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2522 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2524 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2530 #ifndef OPENSSL_NO_NEXTPROTONEG
2531 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2532 * elements of zero length are allowed and the set of elements must exactly fill
2533 * the length of the block. */
2534 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2536 unsigned int off = 0;
2550 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2552 unsigned short length;
2553 unsigned short type;
2554 unsigned short size;
2555 unsigned char *data = *p;
2556 int tlsext_servername = 0;
2557 int renegotiate_seen = 0;
2559 #ifndef OPENSSL_NO_NEXTPROTONEG
2560 s->s3->next_proto_neg_seen = 0;
2563 if (s->s3->alpn_selected)
2565 OPENSSL_free(s->s3->alpn_selected);
2566 s->s3->alpn_selected = NULL;
2569 #ifndef OPENSSL_NO_HEARTBEATS
2570 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2571 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2574 #ifdef TLSEXT_TYPE_encrypt_then_mac
2575 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2578 if (data >= (d+n-2))
2582 if (data+length != d+n)
2584 *al = SSL_AD_DECODE_ERROR;
2588 while(data <= (d+n-4))
2593 if (data+size > (d+n))
2596 if (s->tlsext_debug_cb)
2597 s->tlsext_debug_cb(s, 1, type, data, size,
2598 s->tlsext_debug_arg);
2600 if (type == TLSEXT_TYPE_server_name)
2602 if (s->tlsext_hostname == NULL || size > 0)
2604 *al = TLS1_AD_UNRECOGNIZED_NAME;
2607 tlsext_servername = 1;
2610 #ifndef OPENSSL_NO_EC
2611 else if (type == TLSEXT_TYPE_ec_point_formats)
2613 unsigned char *sdata = data;
2614 int ecpointformatlist_length = *(sdata++);
2616 if (ecpointformatlist_length != size - 1)
2618 *al = TLS1_AD_DECODE_ERROR;
2621 s->session->tlsext_ecpointformatlist_length = 0;
2622 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2623 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2625 *al = TLS1_AD_INTERNAL_ERROR;
2628 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2629 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2631 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2632 sdata = s->session->tlsext_ecpointformatlist;
2633 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2634 fprintf(stderr,"%i ",*(sdata++));
2635 fprintf(stderr,"\n");
2638 #endif /* OPENSSL_NO_EC */
2640 else if (type == TLSEXT_TYPE_session_ticket)
2642 if (s->tls_session_ticket_ext_cb &&
2643 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2645 *al = TLS1_AD_INTERNAL_ERROR;
2648 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2651 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2654 s->tlsext_ticket_expected = 1;
2656 #ifdef TLSEXT_TYPE_opaque_prf_input
2657 else if (type == TLSEXT_TYPE_opaque_prf_input)
2659 unsigned char *sdata = data;
2663 *al = SSL_AD_DECODE_ERROR;
2666 n2s(sdata, s->s3->server_opaque_prf_input_len);
2667 if (s->s3->server_opaque_prf_input_len != size - 2)
2669 *al = SSL_AD_DECODE_ERROR;
2673 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2674 OPENSSL_free(s->s3->server_opaque_prf_input);
2675 if (s->s3->server_opaque_prf_input_len == 0)
2676 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2678 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2680 if (s->s3->server_opaque_prf_input == NULL)
2682 *al = TLS1_AD_INTERNAL_ERROR;
2687 else if (type == TLSEXT_TYPE_status_request)
2689 /* MUST be empty and only sent if we've requested
2690 * a status request message.
2692 if ((s->tlsext_status_type == -1) || (size > 0))
2694 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2697 /* Set flag to expect CertificateStatus message */
2698 s->tlsext_status_expected = 1;
2700 #ifndef OPENSSL_NO_NEXTPROTONEG
2701 else if (type == TLSEXT_TYPE_next_proto_neg &&
2702 s->s3->tmp.finish_md_len == 0)
2704 unsigned char *selected;
2705 unsigned char selected_len;
2707 /* We must have requested it. */
2708 if (s->ctx->next_proto_select_cb == NULL)
2710 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2713 /* The data must be valid */
2714 if (!ssl_next_proto_validate(data, size))
2716 *al = TLS1_AD_DECODE_ERROR;
2719 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2721 *al = TLS1_AD_INTERNAL_ERROR;
2724 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2725 if (!s->next_proto_negotiated)
2727 *al = TLS1_AD_INTERNAL_ERROR;
2730 memcpy(s->next_proto_negotiated, selected, selected_len);
2731 s->next_proto_negotiated_len = selected_len;
2732 s->s3->next_proto_neg_seen = 1;
2736 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2740 /* We must have requested it. */
2741 if (s->alpn_client_proto_list == NULL)
2743 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2748 *al = TLS1_AD_DECODE_ERROR;
2751 /* The extension data consists of:
2752 * uint16 list_length
2753 * uint8 proto_length;
2754 * uint8 proto[proto_length]; */
2758 if (len != (unsigned) size - 2)
2760 *al = TLS1_AD_DECODE_ERROR;
2764 if (len != (unsigned) size - 3)
2766 *al = TLS1_AD_DECODE_ERROR;
2769 if (s->s3->alpn_selected)
2770 OPENSSL_free(s->s3->alpn_selected);
2771 s->s3->alpn_selected = OPENSSL_malloc(len);
2772 if (!s->s3->alpn_selected)
2774 *al = TLS1_AD_INTERNAL_ERROR;
2777 memcpy(s->s3->alpn_selected, data + 3, len);
2778 s->s3->alpn_selected_len = len;
2781 else if (type == TLSEXT_TYPE_renegotiate)
2783 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2785 renegotiate_seen = 1;
2787 #ifndef OPENSSL_NO_HEARTBEATS
2788 else if (type == TLSEXT_TYPE_heartbeat)
2792 case 0x01: /* Server allows us to send HB requests */
2793 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2795 case 0x02: /* Server doesn't accept HB requests */
2796 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2797 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2799 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2804 else if (type == TLSEXT_TYPE_use_srtp)
2806 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2810 /* If this extension type was not otherwise handled, but
2811 * matches a custom_cli_ext_record, then send it to the c
2813 else if (s->ctx->custom_cli_ext_records_count)
2816 custom_cli_ext_record* record;
2818 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
2820 record = &s->ctx->custom_cli_ext_records[i];
2821 if (record->ext_type == type)
2823 if (record->fn2 && !record->fn2(s, type, data, size, al, record->arg))
2829 #ifdef TLSEXT_TYPE_encrypt_then_mac
2830 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2832 /* Ignore if inappropriate ciphersuite */
2833 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD)
2834 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2843 *al = SSL_AD_DECODE_ERROR;
2847 if (!s->hit && tlsext_servername == 1)
2849 if (s->tlsext_hostname)
2851 if (s->session->tlsext_hostname == NULL)
2853 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2854 if (!s->session->tlsext_hostname)
2856 *al = SSL_AD_UNRECOGNIZED_NAME;
2862 *al = SSL_AD_DECODE_ERROR;
2872 /* Determine if we need to see RI. Strictly speaking if we want to
2873 * avoid an attack we should *always* see RI even on initial server
2874 * hello because the client doesn't see any renegotiation during an
2875 * attack. However this would mean we could not connect to any server
2876 * which doesn't support RI so for the immediate future tolerate RI
2877 * absence on initial connect only.
2879 if (!renegotiate_seen
2880 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2881 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2883 *al = SSL_AD_HANDSHAKE_FAILURE;
2884 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2885 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2893 int ssl_prepare_clienthello_tlsext(SSL *s)
2896 #ifdef TLSEXT_TYPE_opaque_prf_input
2900 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2902 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2907 if (s->tlsext_opaque_prf_input != NULL)
2909 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2910 OPENSSL_free(s->s3->client_opaque_prf_input);
2912 if (s->tlsext_opaque_prf_input_len == 0)
2913 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2915 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2916 if (s->s3->client_opaque_prf_input == NULL)
2918 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2921 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2925 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2926 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2933 int ssl_prepare_serverhello_tlsext(SSL *s)
2938 static int ssl_check_clienthello_tlsext_early(SSL *s)
2940 int ret=SSL_TLSEXT_ERR_NOACK;
2941 int al = SSL_AD_UNRECOGNIZED_NAME;
2943 #ifndef OPENSSL_NO_EC
2944 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2945 * ssl3_choose_cipher in s3_lib.c.
2947 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2948 * ssl3_choose_cipher in s3_lib.c.
2952 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2953 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2954 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2955 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2957 #ifdef TLSEXT_TYPE_opaque_prf_input
2959 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2960 * but we might be sending an alert in response to the client hello,
2961 * so this has to happen here in
2962 * ssl_check_clienthello_tlsext_early(). */
2966 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2968 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2971 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2972 al = SSL_AD_INTERNAL_ERROR;
2977 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2978 OPENSSL_free(s->s3->server_opaque_prf_input);
2979 s->s3->server_opaque_prf_input = NULL;
2981 if (s->tlsext_opaque_prf_input != NULL)
2983 if (s->s3->client_opaque_prf_input != NULL &&
2984 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2986 /* can only use this extension if we have a server opaque PRF input
2987 * of the same length as the client opaque PRF input! */
2989 if (s->tlsext_opaque_prf_input_len == 0)
2990 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2992 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2993 if (s->s3->server_opaque_prf_input == NULL)
2995 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2996 al = SSL_AD_INTERNAL_ERROR;
2999 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
3003 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
3005 /* The callback wants to enforce use of the extension,
3006 * but we can't do that with the client opaque PRF input;
3007 * abort the handshake.
3009 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3010 al = SSL_AD_HANDSHAKE_FAILURE;
3018 case SSL_TLSEXT_ERR_ALERT_FATAL:
3019 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3022 case SSL_TLSEXT_ERR_ALERT_WARNING:
3023 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3026 case SSL_TLSEXT_ERR_NOACK:
3027 s->servername_done=0;
3033 int ssl_check_clienthello_tlsext_late(SSL *s)
3035 int ret = SSL_TLSEXT_ERR_OK;
3038 /* If status request then ask callback what to do.
3039 * Note: this must be called after servername callbacks in case
3040 * the certificate has changed, and must be called after the cipher
3041 * has been chosen because this may influence which certificate is sent
3043 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
3046 CERT_PKEY *certpkey;
3047 certpkey = ssl_get_server_send_pkey(s);
3048 /* If no certificate can't return certificate status */
3049 if (certpkey == NULL)
3051 s->tlsext_status_expected = 0;
3054 /* Set current certificate to one we will use so
3055 * SSL_get_certificate et al can pick it up.
3057 s->cert->key = certpkey;
3058 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3061 /* We don't want to send a status request response */
3062 case SSL_TLSEXT_ERR_NOACK:
3063 s->tlsext_status_expected = 0;
3065 /* status request response should be sent */
3066 case SSL_TLSEXT_ERR_OK:
3067 if (s->tlsext_ocsp_resp)
3068 s->tlsext_status_expected = 1;
3070 s->tlsext_status_expected = 0;
3072 /* something bad happened */
3073 case SSL_TLSEXT_ERR_ALERT_FATAL:
3074 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3075 al = SSL_AD_INTERNAL_ERROR;
3080 s->tlsext_status_expected = 0;
3085 case SSL_TLSEXT_ERR_ALERT_FATAL:
3086 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3089 case SSL_TLSEXT_ERR_ALERT_WARNING:
3090 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3098 int ssl_check_serverhello_tlsext(SSL *s)
3100 int ret=SSL_TLSEXT_ERR_NOACK;
3101 int al = SSL_AD_UNRECOGNIZED_NAME;
3103 #ifndef OPENSSL_NO_EC
3104 /* If we are client and using an elliptic curve cryptography cipher
3105 * suite, then if server returns an EC point formats lists extension
3106 * it must contain uncompressed.
3108 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3109 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3110 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3111 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3112 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3114 /* we are using an ECC cipher */
3116 unsigned char *list;
3117 int found_uncompressed = 0;
3118 list = s->session->tlsext_ecpointformatlist;