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 #ifndef OPENSSL_NO_DH
119 #include <openssl/dh.h>
120 #include <openssl/bn.h>
122 #include "ssl_locl.h"
124 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
126 #ifndef OPENSSL_NO_TLSEXT
127 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
128 const unsigned char *sess_id, int sesslen,
129 SSL_SESSION **psess);
130 static int ssl_check_clienthello_tlsext_early(SSL *s);
131 int ssl_check_serverhello_tlsext(SSL *s);
134 SSL3_ENC_METHOD const TLSv1_enc_data={
137 tls1_setup_key_block,
138 tls1_generate_master_secret,
139 tls1_change_cipher_state,
140 tls1_final_finish_mac,
141 TLS1_FINISH_MAC_LENGTH,
142 tls1_cert_verify_mac,
143 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
144 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
146 tls1_export_keying_material,
148 SSL3_HM_HEADER_LENGTH,
149 ssl3_set_handshake_header,
153 SSL3_ENC_METHOD const TLSv1_1_enc_data={
156 tls1_setup_key_block,
157 tls1_generate_master_secret,
158 tls1_change_cipher_state,
159 tls1_final_finish_mac,
160 TLS1_FINISH_MAC_LENGTH,
161 tls1_cert_verify_mac,
162 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
163 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
165 tls1_export_keying_material,
166 SSL_ENC_FLAG_EXPLICIT_IV,
167 SSL3_HM_HEADER_LENGTH,
168 ssl3_set_handshake_header,
172 SSL3_ENC_METHOD const TLSv1_2_enc_data={
175 tls1_setup_key_block,
176 tls1_generate_master_secret,
177 tls1_change_cipher_state,
178 tls1_final_finish_mac,
179 TLS1_FINISH_MAC_LENGTH,
180 tls1_cert_verify_mac,
181 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
182 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
184 tls1_export_keying_material,
185 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
186 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
187 SSL3_HM_HEADER_LENGTH,
188 ssl3_set_handshake_header,
192 long tls1_default_timeout(void)
194 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
195 * is way too long for http, the cache would over fill */
201 if (!ssl3_new(s)) return(0);
202 s->method->ssl_clear(s);
206 void tls1_free(SSL *s)
208 #ifndef OPENSSL_NO_TLSEXT
209 if (s->tlsext_session_ticket)
211 OPENSSL_free(s->tlsext_session_ticket);
213 #endif /* OPENSSL_NO_TLSEXT */
217 void tls1_clear(SSL *s)
220 s->version = s->method->version;
223 #ifndef OPENSSL_NO_EC
227 int nid; /* Curve NID */
228 int secbits; /* Bits of security (from SP800-57) */
229 unsigned int flags; /* Flags: currently just field type */
232 #define TLS_CURVE_CHAR2 0x1
233 #define TLS_CURVE_PRIME 0x0
235 static const tls_curve_info nid_list[] =
237 {NID_sect163k1, 80, TLS_CURVE_CHAR2},/* sect163k1 (1) */
238 {NID_sect163r1, 80, TLS_CURVE_CHAR2},/* sect163r1 (2) */
239 {NID_sect163r2, 80, TLS_CURVE_CHAR2},/* sect163r2 (3) */
240 {NID_sect193r1, 80, TLS_CURVE_CHAR2},/* sect193r1 (4) */
241 {NID_sect193r2, 80, TLS_CURVE_CHAR2},/* sect193r2 (5) */
242 {NID_sect233k1, 112, TLS_CURVE_CHAR2},/* sect233k1 (6) */
243 {NID_sect233r1, 112, TLS_CURVE_CHAR2},/* sect233r1 (7) */
244 {NID_sect239k1, 112, TLS_CURVE_CHAR2},/* sect239k1 (8) */
245 {NID_sect283k1, 128, TLS_CURVE_CHAR2},/* sect283k1 (9) */
246 {NID_sect283r1, 128, TLS_CURVE_CHAR2},/* sect283r1 (10) */
247 {NID_sect409k1, 192, TLS_CURVE_CHAR2},/* sect409k1 (11) */
248 {NID_sect409r1, 192, TLS_CURVE_CHAR2},/* sect409r1 (12) */
249 {NID_sect571k1, 256, TLS_CURVE_CHAR2},/* sect571k1 (13) */
250 {NID_sect571r1, 256, TLS_CURVE_CHAR2},/* sect571r1 (14) */
251 {NID_secp160k1, 80, TLS_CURVE_PRIME},/* secp160k1 (15) */
252 {NID_secp160r1, 80, TLS_CURVE_PRIME},/* secp160r1 (16) */
253 {NID_secp160r2, 80, TLS_CURVE_PRIME},/* secp160r2 (17) */
254 {NID_secp192k1, 80, TLS_CURVE_PRIME},/* secp192k1 (18) */
255 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME},/* secp192r1 (19) */
256 {NID_secp224k1, 112, TLS_CURVE_PRIME},/* secp224k1 (20) */
257 {NID_secp224r1, 112, TLS_CURVE_PRIME},/* secp224r1 (21) */
258 {NID_secp256k1, 128, TLS_CURVE_PRIME},/* secp256k1 (22) */
259 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME},/* secp256r1 (23) */
260 {NID_secp384r1, 192, TLS_CURVE_PRIME},/* secp384r1 (24) */
261 {NID_secp521r1, 256, TLS_CURVE_PRIME},/* secp521r1 (25) */
262 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
263 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
264 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME},/* brainpool512r1 (28) */
268 static const unsigned char ecformats_default[] =
270 TLSEXT_ECPOINTFORMAT_uncompressed,
271 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
272 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
275 static const unsigned char eccurves_default[] =
277 0,14, /* sect571r1 (14) */
278 0,13, /* sect571k1 (13) */
279 0,25, /* secp521r1 (25) */
280 0,28, /* brainpool512r1 (28) */
281 0,11, /* sect409k1 (11) */
282 0,12, /* sect409r1 (12) */
283 0,27, /* brainpoolP384r1 (27) */
284 0,24, /* secp384r1 (24) */
285 0,9, /* sect283k1 (9) */
286 0,10, /* sect283r1 (10) */
287 0,26, /* brainpoolP256r1 (26) */
288 0,22, /* secp256k1 (22) */
289 0,23, /* secp256r1 (23) */
290 0,8, /* sect239k1 (8) */
291 0,6, /* sect233k1 (6) */
292 0,7, /* sect233r1 (7) */
293 0,20, /* secp224k1 (20) */
294 0,21, /* secp224r1 (21) */
295 0,4, /* sect193r1 (4) */
296 0,5, /* sect193r2 (5) */
297 0,18, /* secp192k1 (18) */
298 0,19, /* secp192r1 (19) */
299 0,1, /* sect163k1 (1) */
300 0,2, /* sect163r1 (2) */
301 0,3, /* sect163r2 (3) */
302 0,15, /* secp160k1 (15) */
303 0,16, /* secp160r1 (16) */
304 0,17, /* secp160r2 (17) */
307 static const unsigned char suiteb_curves[] =
309 0, TLSEXT_curve_P_256,
310 0, TLSEXT_curve_P_384
313 int tls1_ec_curve_id2nid(int curve_id)
315 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
316 if ((curve_id < 1) || ((unsigned int)curve_id >
317 sizeof(nid_list)/sizeof(nid_list[0])))
319 return nid_list[curve_id-1].nid;
322 int tls1_ec_nid2curve_id(int nid)
324 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
327 case NID_sect163k1: /* sect163k1 (1) */
329 case NID_sect163r1: /* sect163r1 (2) */
331 case NID_sect163r2: /* sect163r2 (3) */
333 case NID_sect193r1: /* sect193r1 (4) */
335 case NID_sect193r2: /* sect193r2 (5) */
337 case NID_sect233k1: /* sect233k1 (6) */
339 case NID_sect233r1: /* sect233r1 (7) */
341 case NID_sect239k1: /* sect239k1 (8) */
343 case NID_sect283k1: /* sect283k1 (9) */
345 case NID_sect283r1: /* sect283r1 (10) */
347 case NID_sect409k1: /* sect409k1 (11) */
349 case NID_sect409r1: /* sect409r1 (12) */
351 case NID_sect571k1: /* sect571k1 (13) */
353 case NID_sect571r1: /* sect571r1 (14) */
355 case NID_secp160k1: /* secp160k1 (15) */
357 case NID_secp160r1: /* secp160r1 (16) */
359 case NID_secp160r2: /* secp160r2 (17) */
361 case NID_secp192k1: /* secp192k1 (18) */
363 case NID_X9_62_prime192v1: /* secp192r1 (19) */
365 case NID_secp224k1: /* secp224k1 (20) */
367 case NID_secp224r1: /* secp224r1 (21) */
369 case NID_secp256k1: /* secp256k1 (22) */
371 case NID_X9_62_prime256v1: /* secp256r1 (23) */
373 case NID_secp384r1: /* secp384r1 (24) */
375 case NID_secp521r1: /* secp521r1 (25) */
377 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */
379 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */
381 case NID_brainpoolP512r1: /* brainpool512r1 (28) */
387 /* Get curves list, if "sess" is set return client curves otherwise
390 static void tls1_get_curvelist(SSL *s, int sess,
391 const unsigned char **pcurves,
396 *pcurves = s->session->tlsext_ellipticcurvelist;
397 *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
400 /* For Suite B mode only include P-256, P-384 */
401 switch (tls1_suiteb(s))
403 case SSL_CERT_FLAG_SUITEB_128_LOS:
404 *pcurves = suiteb_curves;
405 *pcurveslen = sizeof(suiteb_curves);
408 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
409 *pcurves = suiteb_curves;
413 case SSL_CERT_FLAG_SUITEB_192_LOS:
414 *pcurves = suiteb_curves + 2;
418 *pcurves = s->tlsext_ellipticcurvelist;
419 *pcurveslen = s->tlsext_ellipticcurvelist_length;
423 *pcurves = eccurves_default;
424 *pcurveslen = sizeof(eccurves_default);
428 /* See if curve is allowed by security callback */
429 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
431 const tls_curve_info *cinfo;
434 if ((curve[1] < 1) || ((size_t)curve[1] >
435 sizeof(nid_list)/sizeof(nid_list[0])))
437 cinfo = &nid_list[curve[1]-1];
438 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
441 /* Check a curve is one of our preferences */
442 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
444 const unsigned char *curves;
446 unsigned int suiteb_flags = tls1_suiteb(s);
447 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
449 /* Check curve matches Suite B preferences */
452 unsigned long cid = s->s3->tmp.new_cipher->id;
455 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
457 if (p[2] != TLSEXT_curve_P_256)
460 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
462 if (p[2] != TLSEXT_curve_P_384)
465 else /* Should never happen */
468 tls1_get_curvelist(s, 0, &curves, &curveslen);
469 for (i = 0; i < curveslen; i += 2, curves += 2)
471 if (p[1] == curves[0] && p[2] == curves[1])
472 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
477 /* Return nth shared curve. If nmatch == -1 return number of
478 * matches. For nmatch == -2 return the NID of the curve to use for
482 int tls1_shared_curve(SSL *s, int nmatch)
484 const unsigned char *pref, *supp;
485 size_t preflen, supplen, i, j;
487 /* Can't do anything on client side */
494 /* For Suite B ciphersuite determines curve: we
495 * already know these are acceptable due to previous
498 unsigned long cid = s->s3->tmp.new_cipher->id;
499 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
500 return NID_X9_62_prime256v1; /* P-256 */
501 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
502 return NID_secp384r1; /* P-384 */
503 /* Should never happen */
506 /* If not Suite B just return first preference shared curve */
509 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
511 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
516 for (i = 0; i < preflen; i++, pref+=2)
518 const unsigned char *tsupp = supp;
519 for (j = 0; j < supplen; j++, tsupp+=2)
521 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
523 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
527 int id = (pref[0] << 8) | pref[1];
528 return tls1_ec_curve_id2nid(id);
539 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
540 int *curves, size_t ncurves)
542 unsigned char *clist, *p;
544 /* Bitmap of curves included to detect duplicates: only works
545 * while curve ids < 32
547 unsigned long dup_list = 0;
548 clist = OPENSSL_malloc(ncurves * 2);
551 for (i = 0, p = clist; i < ncurves; i++)
553 unsigned long idmask;
555 id = tls1_ec_nid2curve_id(curves[i]);
557 if (!id || (dup_list & idmask))
568 *pextlen = ncurves * 2;
572 #define MAX_CURVELIST 28
577 int nid_arr[MAX_CURVELIST];
580 static int nid_cb(const char *elem, int len, void *arg)
582 nid_cb_st *narg = arg;
586 if (narg->nidcnt == MAX_CURVELIST)
588 if (len > (int)(sizeof(etmp) - 1))
590 memcpy(etmp, elem, len);
592 nid = EC_curve_nist2nid(etmp);
593 if (nid == NID_undef)
594 nid = OBJ_sn2nid(etmp);
595 if (nid == NID_undef)
596 nid = OBJ_ln2nid(etmp);
597 if (nid == NID_undef)
599 for (i = 0; i < narg->nidcnt; i++)
600 if (narg->nid_arr[i] == nid)
602 narg->nid_arr[narg->nidcnt++] = nid;
605 /* Set curves based on a colon separate list */
606 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
611 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
615 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
617 /* For an EC key set TLS id and required compression based on parameters */
618 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
623 const EC_METHOD *meth;
626 /* Determine if it is a prime field */
627 grp = EC_KEY_get0_group(ec);
630 meth = EC_GROUP_method_of(grp);
633 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
637 /* Determine curve ID */
638 id = EC_GROUP_get_curve_name(grp);
639 id = tls1_ec_nid2curve_id(id);
640 /* If we have an ID set it, otherwise set arbitrary explicit curve */
644 curve_id[1] = (unsigned char)id;
656 if (EC_KEY_get0_public_key(ec) == NULL)
658 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
661 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
663 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
666 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
670 /* Check an EC key is compatible with extensions */
671 static int tls1_check_ec_key(SSL *s,
672 unsigned char *curve_id, unsigned char *comp_id)
674 const unsigned char *p;
677 /* If point formats extension present check it, otherwise everything
678 * is supported (see RFC4492).
680 if (comp_id && s->session->tlsext_ecpointformatlist)
682 p = s->session->tlsext_ecpointformatlist;
683 plen = s->session->tlsext_ecpointformatlist_length;
684 for (i = 0; i < plen; i++, p++)
694 /* Check curve is consistent with client and server preferences */
695 for (j = 0; j <= 1; j++)
697 tls1_get_curvelist(s, j, &p, &plen);
698 for (i = 0; i < plen; i+=2, p+=2)
700 if (p[0] == curve_id[0] && p[1] == curve_id[1])
705 /* For clients can only check sent curve list */
712 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
715 /* If we have a custom point format list use it otherwise
717 if (s->tlsext_ecpointformatlist)
719 *pformats = s->tlsext_ecpointformatlist;
720 *pformatslen = s->tlsext_ecpointformatlist_length;
724 *pformats = ecformats_default;
725 /* For Suite B we don't support char2 fields */
727 *pformatslen = sizeof(ecformats_default) - 1;
729 *pformatslen = sizeof(ecformats_default);
733 /* Check cert parameters compatible with extensions: currently just checks
734 * EC certificates have compatible curves and compression.
736 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
738 unsigned char comp_id, curve_id[2];
741 pkey = X509_get_pubkey(x);
744 /* If not EC nothing to do */
745 if (pkey->type != EVP_PKEY_EC)
750 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
754 /* Can't check curve_id for client certs as we don't have a
755 * supported curves extension.
757 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
760 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
761 * SHA384+P-384, adjust digest if necessary.
763 if (set_ee_md && tls1_suiteb(s))
770 /* Check to see we have necessary signing algorithm */
771 if (curve_id[1] == TLSEXT_curve_P_256)
772 check_md = NID_ecdsa_with_SHA256;
773 else if (curve_id[1] == TLSEXT_curve_P_384)
774 check_md = NID_ecdsa_with_SHA384;
776 return 0; /* Should never happen */
777 for (i = 0; i < c->shared_sigalgslen; i++)
778 if (check_md == c->shared_sigalgs[i].signandhash_nid)
780 if (i == c->shared_sigalgslen)
784 if (check_md == NID_ecdsa_with_SHA256)
785 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
787 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
792 /* Check EC temporary key is compatible with client extensions */
793 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
795 unsigned char curve_id[2];
796 EC_KEY *ec = s->cert->ecdh_tmp;
797 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
798 /* Allow any curve: not just those peer supports */
799 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
802 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
803 * no other curves permitted.
807 /* Curve to check determined by ciphersuite */
808 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
809 curve_id[1] = TLSEXT_curve_P_256;
810 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
811 curve_id[1] = TLSEXT_curve_P_384;
815 /* Check this curve is acceptable */
816 if (!tls1_check_ec_key(s, curve_id, NULL))
818 /* If auto or setting curve from callback assume OK */
819 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
821 /* Otherwise check curve is acceptable */
824 unsigned char curve_tmp[2];
827 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
829 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
835 if (s->cert->ecdh_tmp_auto)
837 /* Need a shared curve */
838 if (tls1_shared_curve(s, 0))
844 if (s->cert->ecdh_tmp_cb)
849 if (!tls1_set_ec_id(curve_id, NULL, ec))
851 /* Set this to allow use of invalid curves for testing */
855 return tls1_check_ec_key(s, curve_id, NULL);
861 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
866 #endif /* OPENSSL_NO_EC */
868 #ifndef OPENSSL_NO_TLSEXT
870 /* List of supported signature algorithms and hashes. Should make this
871 * customisable at some point, for now include everything we support.
874 #ifdef OPENSSL_NO_RSA
875 #define tlsext_sigalg_rsa(md) /* */
877 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
880 #ifdef OPENSSL_NO_DSA
881 #define tlsext_sigalg_dsa(md) /* */
883 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
886 #ifdef OPENSSL_NO_ECDSA
887 #define tlsext_sigalg_ecdsa(md) /* */
889 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
892 #define tlsext_sigalg(md) \
893 tlsext_sigalg_rsa(md) \
894 tlsext_sigalg_dsa(md) \
895 tlsext_sigalg_ecdsa(md)
897 static unsigned char tls12_sigalgs[] = {
898 #ifndef OPENSSL_NO_SHA512
899 tlsext_sigalg(TLSEXT_hash_sha512)
900 tlsext_sigalg(TLSEXT_hash_sha384)
902 #ifndef OPENSSL_NO_SHA256
903 tlsext_sigalg(TLSEXT_hash_sha256)
904 tlsext_sigalg(TLSEXT_hash_sha224)
906 #ifndef OPENSSL_NO_SHA
907 tlsext_sigalg(TLSEXT_hash_sha1)
910 #ifndef OPENSSL_NO_ECDSA
911 static unsigned char suiteb_sigalgs[] = {
912 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
913 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
916 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
918 /* If Suite B mode use Suite B sigalgs only, ignore any other
921 #ifndef OPENSSL_NO_EC
922 switch (tls1_suiteb(s))
924 case SSL_CERT_FLAG_SUITEB_128_LOS:
925 *psigs = suiteb_sigalgs;
926 return sizeof(suiteb_sigalgs);
928 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
929 *psigs = suiteb_sigalgs;
932 case SSL_CERT_FLAG_SUITEB_192_LOS:
933 *psigs = suiteb_sigalgs + 2;
937 /* If server use client authentication sigalgs if not NULL */
938 if (s->server && s->cert->client_sigalgs)
940 *psigs = s->cert->client_sigalgs;
941 return s->cert->client_sigalgslen;
943 else if (s->cert->conf_sigalgs)
945 *psigs = s->cert->conf_sigalgs;
946 return s->cert->conf_sigalgslen;
950 *psigs = tls12_sigalgs;
951 return sizeof(tls12_sigalgs);
954 /* Check signature algorithm is consistent with sent supported signature
955 * algorithms and if so return relevant digest.
957 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
958 const unsigned char *sig, EVP_PKEY *pkey)
960 const unsigned char *sent_sigs;
961 size_t sent_sigslen, i;
962 int sigalg = tls12_get_sigid(pkey);
963 /* Should never happen */
966 /* Check key type is consistent with signature */
967 if (sigalg != (int)sig[1])
969 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
972 #ifndef OPENSSL_NO_EC
973 if (pkey->type == EVP_PKEY_EC)
975 unsigned char curve_id[2], comp_id;
976 /* Check compression and curve matches extensions */
977 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
979 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
981 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
984 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
989 if (curve_id[1] == TLSEXT_curve_P_256)
991 if (sig[0] != TLSEXT_hash_sha256)
993 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
994 SSL_R_ILLEGAL_SUITEB_DIGEST);
998 else if (curve_id[1] == TLSEXT_curve_P_384)
1000 if (sig[0] != TLSEXT_hash_sha384)
1002 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1003 SSL_R_ILLEGAL_SUITEB_DIGEST);
1011 else if (tls1_suiteb(s))
1015 /* Check signature matches a type we sent */
1016 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1017 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
1019 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1022 /* Allow fallback to SHA1 if not strict mode */
1023 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
1025 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1028 *pmd = tls12_get_hash(sig[0]);
1031 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
1034 /* Make sure security callback allows algorithm */
1035 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1036 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
1039 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1042 /* Store the digest used so applications can retrieve it if they
1045 if (s->session && s->session->sess_cert)
1046 s->session->sess_cert->peer_key->digest = *pmd;
1050 /* Get a mask of disabled algorithms: an algorithm is disabled
1051 * if it isn't supported or doesn't appear in supported signature
1052 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1053 * session and not global settings.
1056 void ssl_set_client_disabled(SSL *s)
1061 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1062 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1063 c->mask_ssl = SSL_TLSV1_2;
1066 ssl_set_sig_mask(&c->mask_a, s, SSL_SECOP_SIGALG_MASK);
1067 /* Disable static DH if we don't include any appropriate
1068 * signature algorithms.
1070 if (c->mask_a & SSL_aRSA)
1071 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1072 if (c->mask_a & SSL_aDSS)
1073 c->mask_k |= SSL_kDHd;
1074 if (c->mask_a & SSL_aECDSA)
1075 c->mask_k |= SSL_kECDHe;
1076 #ifndef OPENSSL_NO_KRB5
1077 if (!kssl_tgt_is_available(s->kssl_ctx))
1079 c->mask_a |= SSL_aKRB5;
1080 c->mask_k |= SSL_kKRB5;
1083 #ifndef OPENSSL_NO_PSK
1084 /* with PSK there must be client callback set */
1085 if (!s->psk_client_callback)
1087 c->mask_a |= SSL_aPSK;
1088 c->mask_k |= SSL_kPSK;
1090 #endif /* OPENSSL_NO_PSK */
1091 #ifndef OPENSSL_NO_SRP
1092 if (!(s->srp_ctx.srp_Mask & SSL_kSRP))
1094 c->mask_a |= SSL_aSRP;
1095 c->mask_k |= SSL_kSRP;
1101 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1104 if (c->algorithm_ssl & ct->mask_ssl || c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a)
1106 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1109 static int tls_use_ticket(SSL *s)
1111 if (s->options & SSL_OP_NO_TICKET)
1113 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1116 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, int *al)
1119 unsigned char *orig = buf;
1120 unsigned char *ret = buf;
1121 #ifndef OPENSSL_NO_EC
1122 /* See if we support any ECC ciphersuites */
1124 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1127 unsigned long alg_k, alg_a;
1128 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1130 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1132 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1134 alg_k = c->algorithm_mkey;
1135 alg_a = c->algorithm_auth;
1136 if ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)
1137 || (alg_a & SSL_aECDSA)))
1146 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1147 if (s->client_version == SSL3_VERSION
1148 && !s->s3->send_connection_binding)
1153 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1155 if (s->tlsext_hostname != NULL)
1157 /* Add TLS extension servername to the Client Hello message */
1158 unsigned long size_str;
1161 /* check for enough space.
1162 4 for the servername type and entension length
1163 2 for servernamelist length
1164 1 for the hostname type
1165 2 for hostname length
1169 if ((lenmax = limit - ret - 9) < 0
1170 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1173 /* extension type and length */
1174 s2n(TLSEXT_TYPE_server_name,ret);
1175 s2n(size_str+5,ret);
1177 /* length of servername list */
1178 s2n(size_str+3,ret);
1180 /* hostname type, length and hostname */
1181 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1183 memcpy(ret, s->tlsext_hostname, size_str);
1187 /* Add RI if renegotiating */
1192 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1194 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1198 if((limit - ret - 4 - el) < 0) return NULL;
1200 s2n(TLSEXT_TYPE_renegotiate,ret);
1203 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1205 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1212 #ifndef OPENSSL_NO_SRP
1213 /* Add SRP username if there is one */
1214 if (s->srp_ctx.login != NULL)
1215 { /* Add TLS extension SRP username to the Client Hello message */
1217 int login_len = strlen(s->srp_ctx.login);
1218 if (login_len > 255 || login_len == 0)
1220 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1224 /* check for enough space.
1225 4 for the srp type type and entension length
1226 1 for the srp user identity
1227 + srp user identity length
1229 if ((limit - ret - 5 - login_len) < 0) return NULL;
1231 /* fill in the extension */
1232 s2n(TLSEXT_TYPE_srp,ret);
1233 s2n(login_len+1,ret);
1234 (*ret++) = (unsigned char) login_len;
1235 memcpy(ret, s->srp_ctx.login, login_len);
1240 #ifndef OPENSSL_NO_EC
1243 /* Add TLS extension ECPointFormats to the ClientHello message */
1245 const unsigned char *plist;
1248 unsigned char *etmp;
1250 tls1_get_formatlist(s, &plist, &plistlen);
1252 if ((lenmax = limit - ret - 5) < 0) return NULL;
1253 if (plistlen > (size_t)lenmax) return NULL;
1256 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1260 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1261 s2n(plistlen + 1,ret);
1262 *(ret++) = (unsigned char)plistlen ;
1263 memcpy(ret, plist, plistlen);
1266 /* Add TLS extension EllipticCurves to the ClientHello message */
1267 plist = s->tlsext_ellipticcurvelist;
1268 tls1_get_curvelist(s, 0, &plist, &plistlen);
1270 if ((lenmax = limit - ret - 6) < 0) return NULL;
1271 if (plistlen > (size_t)lenmax) return NULL;
1272 if (plistlen > 65532)
1274 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1279 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1281 /* Copy curve ID if supported */
1282 for (i = 0; i < plistlen; i += 2, plist += 2)
1284 if (tls_curve_allowed(s, plist, SSL_SECOP_CURVE_SUPPORTED))
1291 plistlen = etmp - ret - 4;
1293 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1294 * elliptic_curve_list, but the examples use two bytes.
1295 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1296 * resolves this to two bytes.
1298 s2n(plistlen + 2, ret);
1302 #endif /* OPENSSL_NO_EC */
1304 if (tls_use_ticket(s))
1307 if (!s->new_session && s->session && s->session->tlsext_tick)
1308 ticklen = s->session->tlsext_ticklen;
1309 else if (s->session && s->tlsext_session_ticket &&
1310 s->tlsext_session_ticket->data)
1312 ticklen = s->tlsext_session_ticket->length;
1313 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1314 if (!s->session->tlsext_tick)
1316 memcpy(s->session->tlsext_tick,
1317 s->tlsext_session_ticket->data,
1319 s->session->tlsext_ticklen = ticklen;
1323 if (ticklen == 0 && s->tlsext_session_ticket &&
1324 s->tlsext_session_ticket->data == NULL)
1326 /* Check for enough room 2 for extension type, 2 for len
1329 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1330 s2n(TLSEXT_TYPE_session_ticket,ret);
1334 memcpy(ret, s->session->tlsext_tick, ticklen);
1340 if (SSL_USE_SIGALGS(s))
1343 const unsigned char *salg;
1344 unsigned char *etmp;
1345 salglen = tls12_get_psigalgs(s, &salg);
1346 if ((size_t)(limit - ret) < salglen + 6)
1348 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1350 /* Skip over lengths for now */
1352 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1353 /* Fill in lengths */
1354 s2n(salglen + 2, etmp);
1359 #ifdef TLSEXT_TYPE_opaque_prf_input
1360 if (s->s3->client_opaque_prf_input != NULL)
1362 size_t col = s->s3->client_opaque_prf_input_len;
1364 if ((long)(limit - ret - 6 - col) < 0)
1366 if (col > 0xFFFD) /* can't happen */
1369 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1372 memcpy(ret, s->s3->client_opaque_prf_input, col);
1377 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1380 long extlen, idlen, itmp;
1384 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1386 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1387 itmp = i2d_OCSP_RESPID(id, NULL);
1393 if (s->tlsext_ocsp_exts)
1395 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1402 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1403 s2n(TLSEXT_TYPE_status_request, ret);
1404 if (extlen + idlen > 0xFFF0)
1406 s2n(extlen + idlen + 5, ret);
1407 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1409 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1411 /* save position of id len */
1412 unsigned char *q = ret;
1413 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1414 /* skip over id len */
1416 itmp = i2d_OCSP_RESPID(id, &ret);
1422 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1425 #ifndef OPENSSL_NO_HEARTBEATS
1426 /* Add Heartbeat extension */
1427 if ((limit - ret - 4 - 1) < 0)
1429 s2n(TLSEXT_TYPE_heartbeat,ret);
1432 * 1: peer may send requests
1433 * 2: peer not allowed to send requests
1435 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1436 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1438 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1441 #ifndef OPENSSL_NO_NEXTPROTONEG
1442 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1444 /* The client advertises an emtpy extension to indicate its
1445 * support for Next Protocol Negotiation */
1446 if (limit - ret - 4 < 0)
1448 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1453 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1455 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1457 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1458 s2n(2 + s->alpn_client_proto_list_len,ret);
1459 s2n(s->alpn_client_proto_list_len,ret);
1460 memcpy(ret, s->alpn_client_proto_list,
1461 s->alpn_client_proto_list_len);
1462 ret += s->alpn_client_proto_list_len;
1465 if(SSL_get_srtp_profiles(s))
1469 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1471 if((limit - ret - 4 - el) < 0) return NULL;
1473 s2n(TLSEXT_TYPE_use_srtp,ret);
1476 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1478 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1483 custom_ext_init(&s->cert->cli_ext);
1484 /* Add custom TLS Extensions to ClientHello */
1485 if (!custom_ext_add(s, 0, &ret, limit, al))
1487 #ifdef TLSEXT_TYPE_encrypt_then_mac
1488 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1492 /* Add padding to workaround bugs in F5 terminators.
1493 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
1495 * NB: because this code works out the length of all existing
1496 * extensions it MUST always appear last.
1498 if (s->options & SSL_OP_TLSEXT_PADDING)
1500 int hlen = ret - (unsigned char *)s->init_buf->data;
1501 /* The code in s23_clnt.c to build ClientHello messages
1502 * includes the 5-byte record header in the buffer, while
1503 * the code in s3_clnt.c does not.
1505 if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
1507 if (hlen > 0xff && hlen < 0x200)
1509 hlen = 0x200 - hlen;
1515 s2n(TLSEXT_TYPE_padding, ret);
1517 memset(ret, 0, hlen);
1522 if ((extdatalen = ret-orig-2)== 0)
1525 s2n(extdatalen, orig);
1529 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, int *al)
1532 unsigned char *orig = buf;
1533 unsigned char *ret = buf;
1534 #ifndef OPENSSL_NO_NEXTPROTONEG
1535 int next_proto_neg_seen;
1537 #ifndef OPENSSL_NO_EC
1538 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1539 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1540 int using_ecc = (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1541 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1543 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1544 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1548 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1550 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1552 if ((long)(limit - ret - 4) < 0) return NULL;
1554 s2n(TLSEXT_TYPE_server_name,ret);
1558 if(s->s3->send_connection_binding)
1562 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1564 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1568 if((limit - ret - 4 - el) < 0) return NULL;
1570 s2n(TLSEXT_TYPE_renegotiate,ret);
1573 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1575 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1582 #ifndef OPENSSL_NO_EC
1585 const unsigned char *plist;
1587 /* Add TLS extension ECPointFormats to the ServerHello message */
1590 tls1_get_formatlist(s, &plist, &plistlen);
1592 if ((lenmax = limit - ret - 5) < 0) return NULL;
1593 if (plistlen > (size_t)lenmax) return NULL;
1596 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1600 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1601 s2n(plistlen + 1,ret);
1602 *(ret++) = (unsigned char) plistlen;
1603 memcpy(ret, plist, plistlen);
1607 /* Currently the server should not respond with a SupportedCurves extension */
1608 #endif /* OPENSSL_NO_EC */
1610 if (s->tlsext_ticket_expected && tls_use_ticket(s))
1612 if ((long)(limit - ret - 4) < 0) return NULL;
1613 s2n(TLSEXT_TYPE_session_ticket,ret);
1617 if (s->tlsext_status_expected)
1619 if ((long)(limit - ret - 4) < 0) return NULL;
1620 s2n(TLSEXT_TYPE_status_request,ret);
1624 #ifdef TLSEXT_TYPE_opaque_prf_input
1625 if (s->s3->server_opaque_prf_input != NULL)
1627 size_t sol = s->s3->server_opaque_prf_input_len;
1629 if ((long)(limit - ret - 6 - sol) < 0)
1631 if (sol > 0xFFFD) /* can't happen */
1634 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1637 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1646 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1648 if((limit - ret - 4 - el) < 0) return NULL;
1650 s2n(TLSEXT_TYPE_use_srtp,ret);
1653 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1655 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1661 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1662 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1663 { const unsigned char cryptopro_ext[36] = {
1664 0xfd, 0xe8, /*65000*/
1665 0x00, 0x20, /*32 bytes length*/
1666 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1667 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1668 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1669 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1670 if (limit-ret<36) return NULL;
1671 memcpy(ret,cryptopro_ext,36);
1676 #ifndef OPENSSL_NO_HEARTBEATS
1677 /* Add Heartbeat extension if we've received one */
1678 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1680 if ((limit - ret - 4 - 1) < 0)
1682 s2n(TLSEXT_TYPE_heartbeat,ret);
1685 * 1: peer may send requests
1686 * 2: peer not allowed to send requests
1688 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1689 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1691 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1696 #ifndef OPENSSL_NO_NEXTPROTONEG
1697 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1698 s->s3->next_proto_neg_seen = 0;
1699 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1701 const unsigned char *npa;
1702 unsigned int npalen;
1705 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1706 if (r == SSL_TLSEXT_ERR_OK)
1708 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1709 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1711 memcpy(ret, npa, npalen);
1713 s->s3->next_proto_neg_seen = 1;
1717 if (!custom_ext_add(s, 1, &ret, limit, al))
1719 #ifdef TLSEXT_TYPE_encrypt_then_mac
1720 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC)
1722 /* Don't use encrypt_then_mac if AEAD: might want
1723 * to disable for other ciphersuites too.
1725 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD)
1726 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1729 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1735 if (s->s3->alpn_selected)
1737 const unsigned char *selected = s->s3->alpn_selected;
1738 unsigned len = s->s3->alpn_selected_len;
1740 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1742 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1746 memcpy(ret, selected, len);
1750 if ((extdatalen = ret-orig-2)== 0)
1753 s2n(extdatalen, orig);
1757 /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1759 * data: the contents of the extension, not including the type and length.
1760 * data_len: the number of bytes in |data|
1761 * al: a pointer to the alert value to send in the event of a non-zero
1764 * returns: 0 on success. */
1765 static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
1766 unsigned data_len, int *al)
1770 const unsigned char *selected;
1771 unsigned char selected_len;
1774 if (s->ctx->alpn_select_cb == NULL)
1780 /* data should contain a uint16 length followed by a series of 8-bit,
1781 * length-prefixed strings. */
1782 i = ((unsigned) data[0]) << 8 |
1783 ((unsigned) data[1]);
1792 for (i = 0; i < data_len;)
1794 proto_len = data[i];
1800 if (i + proto_len < i || i + proto_len > data_len)
1806 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1807 s->ctx->alpn_select_cb_arg);
1808 if (r == SSL_TLSEXT_ERR_OK) {
1809 if (s->s3->alpn_selected)
1810 OPENSSL_free(s->s3->alpn_selected);
1811 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1812 if (!s->s3->alpn_selected)
1814 *al = SSL_AD_INTERNAL_ERROR;
1817 memcpy(s->s3->alpn_selected, selected, selected_len);
1818 s->s3->alpn_selected_len = selected_len;
1823 *al = SSL_AD_DECODE_ERROR;
1827 #ifndef OPENSSL_NO_EC
1828 /* ssl_check_for_safari attempts to fingerprint Safari using OS X
1829 * SecureTransport using the TLS extension block in |d|, of length |n|.
1830 * Safari, since 10.6, sends exactly these extensions, in this order:
1835 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1836 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1837 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1838 * 10.8..10.8.3 (which don't work).
1840 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
1841 unsigned short type, size;
1842 static const unsigned char kSafariExtensionsBlock[] = {
1843 0x00, 0x0a, /* elliptic_curves extension */
1844 0x00, 0x08, /* 8 bytes */
1845 0x00, 0x06, /* 6 bytes of curve ids */
1846 0x00, 0x17, /* P-256 */
1847 0x00, 0x18, /* P-384 */
1848 0x00, 0x19, /* P-521 */
1850 0x00, 0x0b, /* ec_point_formats */
1851 0x00, 0x02, /* 2 bytes */
1852 0x01, /* 1 point format */
1853 0x00, /* uncompressed */
1856 /* The following is only present in TLS 1.2 */
1857 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1858 0x00, 0x0d, /* signature_algorithms */
1859 0x00, 0x0c, /* 12 bytes */
1860 0x00, 0x0a, /* 10 bytes */
1861 0x05, 0x01, /* SHA-384/RSA */
1862 0x04, 0x01, /* SHA-256/RSA */
1863 0x02, 0x01, /* SHA-1/RSA */
1864 0x04, 0x03, /* SHA-256/ECDSA */
1865 0x02, 0x03, /* SHA-1/ECDSA */
1868 if (data >= (d+n-2))
1877 if (type != TLSEXT_TYPE_server_name)
1880 if (data+size > d+n)
1884 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1886 const size_t len1 = sizeof(kSafariExtensionsBlock);
1887 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1889 if (data + len1 + len2 != d+n)
1891 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1893 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1898 const size_t len = sizeof(kSafariExtensionsBlock);
1900 if (data + len != d+n)
1902 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
1906 s->s3->is_probably_safari = 1;
1908 #endif /* !OPENSSL_NO_EC */
1911 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1913 unsigned short type;
1914 unsigned short size;
1916 unsigned char *data = *p;
1917 int renegotiate_seen = 0;
1920 s->servername_done = 0;
1921 s->tlsext_status_type = -1;
1922 #ifndef OPENSSL_NO_NEXTPROTONEG
1923 s->s3->next_proto_neg_seen = 0;
1926 if (s->s3->alpn_selected)
1928 OPENSSL_free(s->s3->alpn_selected);
1929 s->s3->alpn_selected = NULL;
1932 #ifndef OPENSSL_NO_HEARTBEATS
1933 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1934 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1937 #ifndef OPENSSL_NO_EC
1938 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1939 ssl_check_for_safari(s, data, d, n);
1940 #endif /* !OPENSSL_NO_EC */
1942 /* Clear any signature algorithms extension received */
1943 if (s->cert->peer_sigalgs)
1945 OPENSSL_free(s->cert->peer_sigalgs);
1946 s->cert->peer_sigalgs = NULL;
1948 /* Clear any shared sigtnature algorithms */
1949 if (s->cert->shared_sigalgs)
1951 OPENSSL_free(s->cert->shared_sigalgs);
1952 s->cert->shared_sigalgs = NULL;
1954 /* Clear certificate digests and validity flags */
1955 for (i = 0; i < SSL_PKEY_NUM; i++)
1957 s->cert->pkeys[i].digest = NULL;
1958 s->cert->pkeys[i].valid_flags = 0;
1961 #ifdef TLSEXT_TYPE_encrypt_then_mac
1962 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1965 if (data >= (d+n-2))
1969 if (data > (d+n-len))
1972 while (data <= (d+n-4))
1977 if (data+size > (d+n))
1980 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1982 if (s->tlsext_debug_cb)
1983 s->tlsext_debug_cb(s, 0, type, data, size,
1984 s->tlsext_debug_arg);
1985 /* The servername extension is treated as follows:
1987 - Only the hostname type is supported with a maximum length of 255.
1988 - The servername is rejected if too long or if it contains zeros,
1989 in which case an fatal alert is generated.
1990 - The servername field is maintained together with the session cache.
1991 - When a session is resumed, the servername call back invoked in order
1992 to allow the application to position itself to the right context.
1993 - The servername is acknowledged if it is new for a session or when
1994 it is identical to a previously used for the same session.
1995 Applications can control the behaviour. They can at any time
1996 set a 'desirable' servername for a new SSL object. This can be the
1997 case for example with HTTPS when a Host: header field is received and
1998 a renegotiation is requested. In this case, a possible servername
1999 presented in the new client hello is only acknowledged if it matches
2000 the value of the Host: field.
2001 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2002 if they provide for changing an explicit servername context for the session,
2003 i.e. when the session has been established with a servername extension.
2004 - On session reconnect, the servername extension may be absent.
2008 if (type == TLSEXT_TYPE_server_name)
2010 unsigned char *sdata;
2016 *al = SSL_AD_DECODE_ERROR;
2023 *al = SSL_AD_DECODE_ERROR;
2030 servname_type = *(sdata++);
2036 *al = SSL_AD_DECODE_ERROR;
2039 if (s->servername_done == 0)
2040 switch (servname_type)
2042 case TLSEXT_NAMETYPE_host_name:
2045 if(s->session->tlsext_hostname)
2047 *al = SSL_AD_DECODE_ERROR;
2050 if (len > TLSEXT_MAXLEN_host_name)
2052 *al = TLS1_AD_UNRECOGNIZED_NAME;
2055 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
2057 *al = TLS1_AD_INTERNAL_ERROR;
2060 memcpy(s->session->tlsext_hostname, sdata, len);
2061 s->session->tlsext_hostname[len]='\0';
2062 if (strlen(s->session->tlsext_hostname) != len) {
2063 OPENSSL_free(s->session->tlsext_hostname);
2064 s->session->tlsext_hostname = NULL;
2065 *al = TLS1_AD_UNRECOGNIZED_NAME;
2068 s->servername_done = 1;
2072 s->servername_done = s->session->tlsext_hostname
2073 && strlen(s->session->tlsext_hostname) == len
2074 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2086 *al = SSL_AD_DECODE_ERROR;
2091 #ifndef OPENSSL_NO_SRP
2092 else if (type == TLSEXT_TYPE_srp)
2094 if (size <= 0 || ((len = data[0])) != (size -1))
2096 *al = SSL_AD_DECODE_ERROR;
2099 if (s->srp_ctx.login != NULL)
2101 *al = SSL_AD_DECODE_ERROR;
2104 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
2106 memcpy(s->srp_ctx.login, &data[1], len);
2107 s->srp_ctx.login[len]='\0';
2109 if (strlen(s->srp_ctx.login) != len)
2111 *al = SSL_AD_DECODE_ERROR;
2117 #ifndef OPENSSL_NO_EC
2118 else if (type == TLSEXT_TYPE_ec_point_formats)
2120 unsigned char *sdata = data;
2121 int ecpointformatlist_length = *(sdata++);
2123 if (ecpointformatlist_length != size - 1 ||
2124 ecpointformatlist_length < 1)
2126 *al = TLS1_AD_DECODE_ERROR;
2131 if(s->session->tlsext_ecpointformatlist)
2133 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2134 s->session->tlsext_ecpointformatlist = NULL;
2136 s->session->tlsext_ecpointformatlist_length = 0;
2137 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2139 *al = TLS1_AD_INTERNAL_ERROR;
2142 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2143 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2146 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2147 sdata = s->session->tlsext_ecpointformatlist;
2148 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2149 fprintf(stderr,"%i ",*(sdata++));
2150 fprintf(stderr,"\n");
2153 else if (type == TLSEXT_TYPE_elliptic_curves)
2155 unsigned char *sdata = data;
2156 int ellipticcurvelist_length = (*(sdata++) << 8);
2157 ellipticcurvelist_length += (*(sdata++));
2159 if (ellipticcurvelist_length != size - 2 ||
2160 ellipticcurvelist_length < 1)
2162 *al = TLS1_AD_DECODE_ERROR;
2167 if(s->session->tlsext_ellipticcurvelist)
2169 *al = TLS1_AD_DECODE_ERROR;
2172 s->session->tlsext_ellipticcurvelist_length = 0;
2173 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2175 *al = TLS1_AD_INTERNAL_ERROR;
2178 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2179 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2182 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2183 sdata = s->session->tlsext_ellipticcurvelist;
2184 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2185 fprintf(stderr,"%i ",*(sdata++));
2186 fprintf(stderr,"\n");
2189 #endif /* OPENSSL_NO_EC */
2190 #ifdef TLSEXT_TYPE_opaque_prf_input
2191 else if (type == TLSEXT_TYPE_opaque_prf_input)
2193 unsigned char *sdata = data;
2197 *al = SSL_AD_DECODE_ERROR;
2200 n2s(sdata, s->s3->client_opaque_prf_input_len);
2201 if (s->s3->client_opaque_prf_input_len != size - 2)
2203 *al = SSL_AD_DECODE_ERROR;
2207 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2208 OPENSSL_free(s->s3->client_opaque_prf_input);
2209 if (s->s3->client_opaque_prf_input_len == 0)
2210 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2212 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2213 if (s->s3->client_opaque_prf_input == NULL)
2215 *al = TLS1_AD_INTERNAL_ERROR;
2220 else if (type == TLSEXT_TYPE_session_ticket)
2222 if (s->tls_session_ticket_ext_cb &&
2223 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2225 *al = TLS1_AD_INTERNAL_ERROR;
2229 else if (type == TLSEXT_TYPE_renegotiate)
2231 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2233 renegotiate_seen = 1;
2235 else if (type == TLSEXT_TYPE_signature_algorithms)
2238 if (s->cert->peer_sigalgs || size < 2)
2240 *al = SSL_AD_DECODE_ERROR;
2245 if (dsize != size || dsize & 1 || !dsize)
2247 *al = SSL_AD_DECODE_ERROR;
2250 if (!tls1_process_sigalgs(s, data, dsize))
2252 *al = SSL_AD_DECODE_ERROR;
2255 /* If sigalgs received and no shared algorithms fatal
2258 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
2260 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2261 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2262 *al = SSL_AD_ILLEGAL_PARAMETER;
2266 else if (type == TLSEXT_TYPE_status_request)
2271 *al = SSL_AD_DECODE_ERROR;
2275 s->tlsext_status_type = *data++;
2277 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2279 const unsigned char *sdata;
2281 /* Read in responder_id_list */
2286 *al = SSL_AD_DECODE_ERROR;
2295 *al = SSL_AD_DECODE_ERROR;
2299 dsize -= 2 + idsize;
2303 *al = SSL_AD_DECODE_ERROR;
2308 id = d2i_OCSP_RESPID(NULL,
2312 *al = SSL_AD_DECODE_ERROR;
2317 OCSP_RESPID_free(id);
2318 *al = SSL_AD_DECODE_ERROR;
2321 if (!s->tlsext_ocsp_ids
2322 && !(s->tlsext_ocsp_ids =
2323 sk_OCSP_RESPID_new_null()))
2325 OCSP_RESPID_free(id);
2326 *al = SSL_AD_INTERNAL_ERROR;
2329 if (!sk_OCSP_RESPID_push(
2330 s->tlsext_ocsp_ids, id))
2332 OCSP_RESPID_free(id);
2333 *al = SSL_AD_INTERNAL_ERROR;
2338 /* Read in request_extensions */
2341 *al = SSL_AD_DECODE_ERROR;
2348 *al = SSL_AD_DECODE_ERROR;
2354 if (s->tlsext_ocsp_exts)
2356 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2357 X509_EXTENSION_free);
2360 s->tlsext_ocsp_exts =
2361 d2i_X509_EXTENSIONS(NULL,
2363 if (!s->tlsext_ocsp_exts
2364 || (data + dsize != sdata))
2366 *al = SSL_AD_DECODE_ERROR;
2371 /* We don't know what to do with any other type
2375 s->tlsext_status_type = -1;
2377 #ifndef OPENSSL_NO_HEARTBEATS
2378 else if (type == TLSEXT_TYPE_heartbeat)
2382 case 0x01: /* Client allows us to send HB requests */
2383 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2385 case 0x02: /* Client doesn't accept HB requests */
2386 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2387 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2389 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2394 #ifndef OPENSSL_NO_NEXTPROTONEG
2395 else if (type == TLSEXT_TYPE_next_proto_neg &&
2396 s->s3->tmp.finish_md_len == 0 &&
2397 s->s3->alpn_selected == NULL)
2399 /* We shouldn't accept this extension on a
2402 * s->new_session will be set on renegotiation, but we
2403 * probably shouldn't rely that it couldn't be set on
2404 * the initial renegotation too in certain cases (when
2405 * there's some other reason to disallow resuming an
2406 * earlier session -- the current code won't be doing
2407 * anything like that, but this might change).
2409 * A valid sign that there's been a previous handshake
2410 * in this connection is if s->s3->tmp.finish_md_len >
2411 * 0. (We are talking about a check that will happen
2412 * in the Hello protocol round, well before a new
2413 * Finished message could have been computed.) */
2414 s->s3->next_proto_neg_seen = 1;
2418 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2419 s->ctx->alpn_select_cb &&
2420 s->s3->tmp.finish_md_len == 0)
2422 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2424 #ifndef OPENSSL_NO_NEXTPROTONEG
2425 /* ALPN takes precedence over NPN. */
2426 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.
2445 if (custom_ext_parse(s, 1, type, data, size, al) <= 0)
2448 #ifdef TLSEXT_TYPE_encrypt_then_mac
2449 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2450 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2460 /* Need RI if renegotiating */
2462 if (!renegotiate_seen && s->renegotiate &&
2463 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2465 *al = SSL_AD_HANDSHAKE_FAILURE;
2466 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2467 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2470 /* If no signature algorithms extension set default values */
2471 if (!s->cert->peer_sigalgs)
2472 ssl_cert_set_default_md(s->cert);
2477 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2480 custom_ext_init(&s->cert->srv_ext);
2481 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2483 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2487 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2489 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2495 #ifndef OPENSSL_NO_NEXTPROTONEG
2496 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2497 * elements of zero length are allowed and the set of elements must exactly fill
2498 * the length of the block. */
2499 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2501 unsigned int off = 0;
2515 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2517 unsigned short length;
2518 unsigned short type;
2519 unsigned short size;
2520 unsigned char *data = *p;
2521 int tlsext_servername = 0;
2522 int renegotiate_seen = 0;
2524 #ifndef OPENSSL_NO_NEXTPROTONEG
2525 s->s3->next_proto_neg_seen = 0;
2528 if (s->s3->alpn_selected)
2530 OPENSSL_free(s->s3->alpn_selected);
2531 s->s3->alpn_selected = NULL;
2534 #ifndef OPENSSL_NO_HEARTBEATS
2535 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2536 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2539 #ifdef TLSEXT_TYPE_encrypt_then_mac
2540 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2543 if (data >= (d+n-2))
2547 if (data+length != d+n)
2549 *al = SSL_AD_DECODE_ERROR;
2553 while(data <= (d+n-4))
2558 if (data+size > (d+n))
2561 if (s->tlsext_debug_cb)
2562 s->tlsext_debug_cb(s, 1, type, data, size,
2563 s->tlsext_debug_arg);
2565 if (type == TLSEXT_TYPE_server_name)
2567 if (s->tlsext_hostname == NULL || size > 0)
2569 *al = TLS1_AD_UNRECOGNIZED_NAME;
2572 tlsext_servername = 1;
2575 #ifndef OPENSSL_NO_EC
2576 else if (type == TLSEXT_TYPE_ec_point_formats)
2578 unsigned char *sdata = data;
2579 int ecpointformatlist_length = *(sdata++);
2581 if (ecpointformatlist_length != size - 1)
2583 *al = TLS1_AD_DECODE_ERROR;
2588 s->session->tlsext_ecpointformatlist_length = 0;
2589 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2590 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2592 *al = TLS1_AD_INTERNAL_ERROR;
2595 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2596 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2599 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2600 sdata = s->session->tlsext_ecpointformatlist;
2601 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2602 fprintf(stderr,"%i ",*(sdata++));
2603 fprintf(stderr,"\n");
2606 #endif /* OPENSSL_NO_EC */
2608 else if (type == TLSEXT_TYPE_session_ticket)
2610 if (s->tls_session_ticket_ext_cb &&
2611 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2613 *al = TLS1_AD_INTERNAL_ERROR;
2616 if (!tls_use_ticket(s) || (size > 0))
2618 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2621 s->tlsext_ticket_expected = 1;
2623 #ifdef TLSEXT_TYPE_opaque_prf_input
2624 else if (type == TLSEXT_TYPE_opaque_prf_input)
2626 unsigned char *sdata = data;
2630 *al = SSL_AD_DECODE_ERROR;
2633 n2s(sdata, s->s3->server_opaque_prf_input_len);
2634 if (s->s3->server_opaque_prf_input_len != size - 2)
2636 *al = SSL_AD_DECODE_ERROR;
2640 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2641 OPENSSL_free(s->s3->server_opaque_prf_input);
2642 if (s->s3->server_opaque_prf_input_len == 0)
2643 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2645 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2647 if (s->s3->server_opaque_prf_input == NULL)
2649 *al = TLS1_AD_INTERNAL_ERROR;
2654 else if (type == TLSEXT_TYPE_status_request)
2656 /* MUST be empty and only sent if we've requested
2657 * a status request message.
2659 if ((s->tlsext_status_type == -1) || (size > 0))
2661 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2664 /* Set flag to expect CertificateStatus message */
2665 s->tlsext_status_expected = 1;
2667 #ifndef OPENSSL_NO_NEXTPROTONEG
2668 else if (type == TLSEXT_TYPE_next_proto_neg &&
2669 s->s3->tmp.finish_md_len == 0)
2671 unsigned char *selected;
2672 unsigned char selected_len;
2674 /* We must have requested it. */
2675 if (s->ctx->next_proto_select_cb == NULL)
2677 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2680 /* The data must be valid */
2681 if (!ssl_next_proto_validate(data, size))
2683 *al = TLS1_AD_DECODE_ERROR;
2686 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2688 *al = TLS1_AD_INTERNAL_ERROR;
2691 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2692 if (!s->next_proto_negotiated)
2694 *al = TLS1_AD_INTERNAL_ERROR;
2697 memcpy(s->next_proto_negotiated, selected, selected_len);
2698 s->next_proto_negotiated_len = selected_len;
2699 s->s3->next_proto_neg_seen = 1;
2703 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2707 /* We must have requested it. */
2708 if (s->alpn_client_proto_list == NULL)
2710 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2715 *al = TLS1_AD_DECODE_ERROR;
2718 /* The extension data consists of:
2719 * uint16 list_length
2720 * uint8 proto_length;
2721 * uint8 proto[proto_length]; */
2725 if (len != (unsigned) size - 2)
2727 *al = TLS1_AD_DECODE_ERROR;
2731 if (len != (unsigned) size - 3)
2733 *al = TLS1_AD_DECODE_ERROR;
2736 if (s->s3->alpn_selected)
2737 OPENSSL_free(s->s3->alpn_selected);
2738 s->s3->alpn_selected = OPENSSL_malloc(len);
2739 if (!s->s3->alpn_selected)
2741 *al = TLS1_AD_INTERNAL_ERROR;
2744 memcpy(s->s3->alpn_selected, data + 3, len);
2745 s->s3->alpn_selected_len = len;
2748 else if (type == TLSEXT_TYPE_renegotiate)
2750 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2752 renegotiate_seen = 1;
2754 #ifndef OPENSSL_NO_HEARTBEATS
2755 else if (type == TLSEXT_TYPE_heartbeat)
2759 case 0x01: /* Server allows us to send HB requests */
2760 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2762 case 0x02: /* Server doesn't accept HB requests */
2763 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2764 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2766 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2771 else if (type == TLSEXT_TYPE_use_srtp)
2773 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2777 /* If this extension type was not otherwise handled, but
2778 * matches a custom_cli_ext_record, then send it to the c
2780 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2782 #ifdef TLSEXT_TYPE_encrypt_then_mac
2783 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2785 /* Ignore if inappropriate ciphersuite */
2786 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD)
2787 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2796 *al = SSL_AD_DECODE_ERROR;
2800 if (!s->hit && tlsext_servername == 1)
2802 if (s->tlsext_hostname)
2804 if (s->session->tlsext_hostname == NULL)
2806 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2807 if (!s->session->tlsext_hostname)
2809 *al = SSL_AD_UNRECOGNIZED_NAME;
2815 *al = SSL_AD_DECODE_ERROR;
2825 /* Determine if we need to see RI. Strictly speaking if we want to
2826 * avoid an attack we should *always* see RI even on initial server
2827 * hello because the client doesn't see any renegotiation during an
2828 * attack. However this would mean we could not connect to any server
2829 * which doesn't support RI so for the immediate future tolerate RI
2830 * absence on initial connect only.
2832 if (!renegotiate_seen
2833 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2834 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2836 *al = SSL_AD_HANDSHAKE_FAILURE;
2837 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2838 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2846 int ssl_prepare_clienthello_tlsext(SSL *s)
2849 #ifdef TLSEXT_TYPE_opaque_prf_input
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);
2860 if (s->tlsext_opaque_prf_input != NULL)
2862 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2863 OPENSSL_free(s->s3->client_opaque_prf_input);
2865 if (s->tlsext_opaque_prf_input_len == 0)
2866 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2868 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2869 if (s->s3->client_opaque_prf_input == NULL)
2871 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2874 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2878 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2879 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2886 int ssl_prepare_serverhello_tlsext(SSL *s)
2891 static int ssl_check_clienthello_tlsext_early(SSL *s)
2893 int ret=SSL_TLSEXT_ERR_NOACK;
2894 int al = SSL_AD_UNRECOGNIZED_NAME;
2896 #ifndef OPENSSL_NO_EC
2897 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2898 * ssl3_choose_cipher in s3_lib.c.
2900 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2901 * ssl3_choose_cipher in s3_lib.c.
2905 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2906 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2907 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2908 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2910 #ifdef TLSEXT_TYPE_opaque_prf_input
2912 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2913 * but we might be sending an alert in response to the client hello,
2914 * so this has to happen here in
2915 * ssl_check_clienthello_tlsext_early(). */
2919 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2921 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2924 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2925 al = SSL_AD_INTERNAL_ERROR;
2930 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2931 OPENSSL_free(s->s3->server_opaque_prf_input);
2932 s->s3->server_opaque_prf_input = NULL;
2934 if (s->tlsext_opaque_prf_input != NULL)
2936 if (s->s3->client_opaque_prf_input != NULL &&
2937 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2939 /* can only use this extension if we have a server opaque PRF input
2940 * of the same length as the client opaque PRF input! */
2942 if (s->tlsext_opaque_prf_input_len == 0)
2943 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2945 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2946 if (s->s3->server_opaque_prf_input == NULL)
2948 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2949 al = SSL_AD_INTERNAL_ERROR;
2952 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2956 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2958 /* The callback wants to enforce use of the extension,
2959 * but we can't do that with the client opaque PRF input;
2960 * abort the handshake.
2962 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2963 al = SSL_AD_HANDSHAKE_FAILURE;
2971 case SSL_TLSEXT_ERR_ALERT_FATAL:
2972 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2975 case SSL_TLSEXT_ERR_ALERT_WARNING:
2976 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2979 case SSL_TLSEXT_ERR_NOACK:
2980 s->servername_done=0;
2986 int ssl_check_clienthello_tlsext_late(SSL *s)
2988 int ret = SSL_TLSEXT_ERR_OK;
2991 /* If status request then ask callback what to do.
2992 * Note: this must be called after servername callbacks in case
2993 * the certificate has changed, and must be called after the cipher
2994 * has been chosen because this may influence which certificate is sent
2996 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2999 CERT_PKEY *certpkey;
3000 certpkey = ssl_get_server_send_pkey(s);
3001 /* If no certificate can't return certificate status */
3002 if (certpkey == NULL)
3004 s->tlsext_status_expected = 0;
3007 /* Set current certificate to one we will use so
3008 * SSL_get_certificate et al can pick it up.
3010 s->cert->key = certpkey;
3011 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3014 /* We don't want to send a status request response */
3015 case SSL_TLSEXT_ERR_NOACK:
3016 s->tlsext_status_expected = 0;
3018 /* status request response should be sent */
3019 case SSL_TLSEXT_ERR_OK:
3020 if (s->tlsext_ocsp_resp)
3021 s->tlsext_status_expected = 1;
3023 s->tlsext_status_expected = 0;
3025 /* something bad happened */
3026 case SSL_TLSEXT_ERR_ALERT_FATAL:
3027 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3028 al = SSL_AD_INTERNAL_ERROR;
3033 s->tlsext_status_expected = 0;
3038 case SSL_TLSEXT_ERR_ALERT_FATAL:
3039 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3042 case SSL_TLSEXT_ERR_ALERT_WARNING:
3043 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3051 int ssl_check_serverhello_tlsext(SSL *s)
3053 int ret=SSL_TLSEXT_ERR_NOACK;
3054 int al = SSL_AD_UNRECOGNIZED_NAME;
3056 #ifndef OPENSSL_NO_EC
3057 /* If we are client and using an elliptic curve cryptography cipher
3058 * suite, then if server returns an EC point formats lists extension
3059 * it must contain uncompressed.
3061 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3062 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3063 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3064 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3065 ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3067 /* we are using an ECC cipher */
3069 unsigned char *list;
3070 int found_uncompressed = 0;
3071 list = s->session->tlsext_ecpointformatlist;
3072 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3074 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3076 found_uncompressed = 1;
3080 if (!found_uncompressed)
3082 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);