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 RFC 4492 */
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 RFC 4492 */
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 #ifdef OPENSSL_NO_EC2M
439 if (cinfo->flags & TLS_CURVE_CHAR2)
442 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
445 /* Check a curve is one of our preferences */
446 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
448 const unsigned char *curves;
450 unsigned int suiteb_flags = tls1_suiteb(s);
451 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
453 /* Check curve matches Suite B preferences */
456 unsigned long cid = s->s3->tmp.new_cipher->id;
459 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
461 if (p[2] != TLSEXT_curve_P_256)
464 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
466 if (p[2] != TLSEXT_curve_P_384)
469 else /* Should never happen */
472 tls1_get_curvelist(s, 0, &curves, &curveslen);
473 for (i = 0; i < curveslen; i += 2, curves += 2)
475 if (p[1] == curves[0] && p[2] == curves[1])
476 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
481 /* Return nth shared curve. If nmatch == -1 return number of
482 * matches. For nmatch == -2 return the NID of the curve to use for
486 int tls1_shared_curve(SSL *s, int nmatch)
488 const unsigned char *pref, *supp;
489 size_t preflen, supplen, i, j;
491 /* Can't do anything on client side */
498 /* For Suite B ciphersuite determines curve: we
499 * already know these are acceptable due to previous
502 unsigned long cid = s->s3->tmp.new_cipher->id;
503 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
504 return NID_X9_62_prime256v1; /* P-256 */
505 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
506 return NID_secp384r1; /* P-384 */
507 /* Should never happen */
510 /* If not Suite B just return first preference shared curve */
513 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
515 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
520 for (i = 0; i < preflen; i++, pref+=2)
522 const unsigned char *tsupp = supp;
523 for (j = 0; j < supplen; j++, tsupp+=2)
525 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
527 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
531 int id = (pref[0] << 8) | pref[1];
532 return tls1_ec_curve_id2nid(id);
543 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
544 int *curves, size_t ncurves)
546 unsigned char *clist, *p;
548 /* Bitmap of curves included to detect duplicates: only works
549 * while curve ids < 32
551 unsigned long dup_list = 0;
552 clist = OPENSSL_malloc(ncurves * 2);
555 for (i = 0, p = clist; i < ncurves; i++)
557 unsigned long idmask;
559 id = tls1_ec_nid2curve_id(curves[i]);
561 if (!id || (dup_list & idmask))
572 *pextlen = ncurves * 2;
576 #define MAX_CURVELIST 28
581 int nid_arr[MAX_CURVELIST];
584 static int nid_cb(const char *elem, int len, void *arg)
586 nid_cb_st *narg = arg;
590 if (narg->nidcnt == MAX_CURVELIST)
592 if (len > (int)(sizeof(etmp) - 1))
594 memcpy(etmp, elem, len);
596 nid = EC_curve_nist2nid(etmp);
597 if (nid == NID_undef)
598 nid = OBJ_sn2nid(etmp);
599 if (nid == NID_undef)
600 nid = OBJ_ln2nid(etmp);
601 if (nid == NID_undef)
603 for (i = 0; i < narg->nidcnt; i++)
604 if (narg->nid_arr[i] == nid)
606 narg->nid_arr[narg->nidcnt++] = nid;
609 /* Set curves based on a colon separate list */
610 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
615 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
619 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
621 /* For an EC key set TLS id and required compression based on parameters */
622 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
627 const EC_METHOD *meth;
630 /* Determine if it is a prime field */
631 grp = EC_KEY_get0_group(ec);
634 meth = EC_GROUP_method_of(grp);
637 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
641 /* Determine curve ID */
642 id = EC_GROUP_get_curve_name(grp);
643 id = tls1_ec_nid2curve_id(id);
644 /* If we have an ID set it, otherwise set arbitrary explicit curve */
648 curve_id[1] = (unsigned char)id;
660 if (EC_KEY_get0_public_key(ec) == NULL)
662 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
665 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
667 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
670 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
674 /* Check an EC key is compatible with extensions */
675 static int tls1_check_ec_key(SSL *s,
676 unsigned char *curve_id, unsigned char *comp_id)
678 const unsigned char *p;
681 /* If point formats extension present check it, otherwise everything
682 * is supported (see RFC4492).
684 if (comp_id && s->session->tlsext_ecpointformatlist)
686 p = s->session->tlsext_ecpointformatlist;
687 plen = s->session->tlsext_ecpointformatlist_length;
688 for (i = 0; i < plen; i++, p++)
698 /* Check curve is consistent with client and server preferences */
699 for (j = 0; j <= 1; j++)
701 tls1_get_curvelist(s, j, &p, &plen);
702 for (i = 0; i < plen; i+=2, p+=2)
704 if (p[0] == curve_id[0] && p[1] == curve_id[1])
709 /* For clients can only check sent curve list */
716 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
719 /* If we have a custom point format list use it otherwise
721 if (s->tlsext_ecpointformatlist)
723 *pformats = s->tlsext_ecpointformatlist;
724 *pformatslen = s->tlsext_ecpointformatlist_length;
728 *pformats = ecformats_default;
729 /* For Suite B we don't support char2 fields */
731 *pformatslen = sizeof(ecformats_default) - 1;
733 *pformatslen = sizeof(ecformats_default);
737 /* Check cert parameters compatible with extensions: currently just checks
738 * EC certificates have compatible curves and compression.
740 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
742 unsigned char comp_id, curve_id[2];
745 pkey = X509_get_pubkey(x);
748 /* If not EC nothing to do */
749 if (pkey->type != EVP_PKEY_EC)
754 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
758 /* Can't check curve_id for client certs as we don't have a
759 * supported curves extension.
761 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
764 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
765 * SHA384+P-384, adjust digest if necessary.
767 if (set_ee_md && tls1_suiteb(s))
774 /* Check to see we have necessary signing algorithm */
775 if (curve_id[1] == TLSEXT_curve_P_256)
776 check_md = NID_ecdsa_with_SHA256;
777 else if (curve_id[1] == TLSEXT_curve_P_384)
778 check_md = NID_ecdsa_with_SHA384;
780 return 0; /* Should never happen */
781 for (i = 0; i < c->shared_sigalgslen; i++)
782 if (check_md == c->shared_sigalgs[i].signandhash_nid)
784 if (i == c->shared_sigalgslen)
788 if (check_md == NID_ecdsa_with_SHA256)
789 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
791 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
796 /* Check EC temporary key is compatible with client extensions */
797 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
799 unsigned char curve_id[2];
800 EC_KEY *ec = s->cert->ecdh_tmp;
801 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
802 /* Allow any curve: not just those peer supports */
803 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
806 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
807 * no other curves permitted.
811 /* Curve to check determined by ciphersuite */
812 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
813 curve_id[1] = TLSEXT_curve_P_256;
814 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
815 curve_id[1] = TLSEXT_curve_P_384;
819 /* Check this curve is acceptable */
820 if (!tls1_check_ec_key(s, curve_id, NULL))
822 /* If auto or setting curve from callback assume OK */
823 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
825 /* Otherwise check curve is acceptable */
828 unsigned char curve_tmp[2];
831 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
833 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
839 if (s->cert->ecdh_tmp_auto)
841 /* Need a shared curve */
842 if (tls1_shared_curve(s, 0))
848 if (s->cert->ecdh_tmp_cb)
853 if (!tls1_set_ec_id(curve_id, NULL, ec))
855 /* Set this to allow use of invalid curves for testing */
859 return tls1_check_ec_key(s, curve_id, NULL);
865 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
870 #endif /* OPENSSL_NO_EC */
872 #ifndef OPENSSL_NO_TLSEXT
874 /* List of supported signature algorithms and hashes. Should make this
875 * customisable at some point, for now include everything we support.
878 #ifdef OPENSSL_NO_RSA
879 #define tlsext_sigalg_rsa(md) /* */
881 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
884 #ifdef OPENSSL_NO_DSA
885 #define tlsext_sigalg_dsa(md) /* */
887 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
890 #ifdef OPENSSL_NO_ECDSA
891 #define tlsext_sigalg_ecdsa(md) /* */
893 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
896 #define tlsext_sigalg(md) \
897 tlsext_sigalg_rsa(md) \
898 tlsext_sigalg_dsa(md) \
899 tlsext_sigalg_ecdsa(md)
901 static unsigned char tls12_sigalgs[] = {
902 #ifndef OPENSSL_NO_SHA512
903 tlsext_sigalg(TLSEXT_hash_sha512)
904 tlsext_sigalg(TLSEXT_hash_sha384)
906 #ifndef OPENSSL_NO_SHA256
907 tlsext_sigalg(TLSEXT_hash_sha256)
908 tlsext_sigalg(TLSEXT_hash_sha224)
910 #ifndef OPENSSL_NO_SHA
911 tlsext_sigalg(TLSEXT_hash_sha1)
914 #ifndef OPENSSL_NO_ECDSA
915 static unsigned char suiteb_sigalgs[] = {
916 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
917 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
920 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
922 /* If Suite B mode use Suite B sigalgs only, ignore any other
925 #ifndef OPENSSL_NO_EC
926 switch (tls1_suiteb(s))
928 case SSL_CERT_FLAG_SUITEB_128_LOS:
929 *psigs = suiteb_sigalgs;
930 return sizeof(suiteb_sigalgs);
932 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
933 *psigs = suiteb_sigalgs;
936 case SSL_CERT_FLAG_SUITEB_192_LOS:
937 *psigs = suiteb_sigalgs + 2;
941 /* If server use client authentication sigalgs if not NULL */
942 if (s->server && s->cert->client_sigalgs)
944 *psigs = s->cert->client_sigalgs;
945 return s->cert->client_sigalgslen;
947 else if (s->cert->conf_sigalgs)
949 *psigs = s->cert->conf_sigalgs;
950 return s->cert->conf_sigalgslen;
954 *psigs = tls12_sigalgs;
955 return sizeof(tls12_sigalgs);
958 /* Check signature algorithm is consistent with sent supported signature
959 * algorithms and if so return relevant digest.
961 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
962 const unsigned char *sig, EVP_PKEY *pkey)
964 const unsigned char *sent_sigs;
965 size_t sent_sigslen, i;
966 int sigalg = tls12_get_sigid(pkey);
967 /* Should never happen */
970 /* Check key type is consistent with signature */
971 if (sigalg != (int)sig[1])
973 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
976 #ifndef OPENSSL_NO_EC
977 if (pkey->type == EVP_PKEY_EC)
979 unsigned char curve_id[2], comp_id;
980 /* Check compression and curve matches extensions */
981 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
983 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
985 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
988 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
993 if (curve_id[1] == TLSEXT_curve_P_256)
995 if (sig[0] != TLSEXT_hash_sha256)
997 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
998 SSL_R_ILLEGAL_SUITEB_DIGEST);
1002 else if (curve_id[1] == TLSEXT_curve_P_384)
1004 if (sig[0] != TLSEXT_hash_sha384)
1006 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1007 SSL_R_ILLEGAL_SUITEB_DIGEST);
1015 else if (tls1_suiteb(s))
1019 /* Check signature matches a type we sent */
1020 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1021 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
1023 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1026 /* Allow fallback to SHA1 if not strict mode */
1027 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
1029 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1032 *pmd = tls12_get_hash(sig[0]);
1035 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
1038 /* Make sure security callback allows algorithm */
1039 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1040 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
1043 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1046 /* Store the digest used so applications can retrieve it if they
1049 if (s->session && s->session->sess_cert)
1050 s->session->sess_cert->peer_key->digest = *pmd;
1054 /* Get a mask of disabled algorithms: an algorithm is disabled
1055 * if it isn't supported or doesn't appear in supported signature
1056 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1057 * session and not global settings.
1060 void ssl_set_client_disabled(SSL *s)
1065 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1066 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1067 c->mask_ssl = SSL_TLSV1_2;
1070 ssl_set_sig_mask(&c->mask_a, s, SSL_SECOP_SIGALG_MASK);
1071 /* Disable static DH if we don't include any appropriate
1072 * signature algorithms.
1074 if (c->mask_a & SSL_aRSA)
1075 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1076 if (c->mask_a & SSL_aDSS)
1077 c->mask_k |= SSL_kDHd;
1078 if (c->mask_a & SSL_aECDSA)
1079 c->mask_k |= SSL_kECDHe;
1080 #ifndef OPENSSL_NO_KRB5
1081 if (!kssl_tgt_is_available(s->kssl_ctx))
1083 c->mask_a |= SSL_aKRB5;
1084 c->mask_k |= SSL_kKRB5;
1087 #ifndef OPENSSL_NO_PSK
1088 /* with PSK there must be client callback set */
1089 if (!s->psk_client_callback)
1091 c->mask_a |= SSL_aPSK;
1092 c->mask_k |= SSL_kPSK;
1094 #endif /* OPENSSL_NO_PSK */
1095 #ifndef OPENSSL_NO_SRP
1096 if (!(s->srp_ctx.srp_Mask & SSL_kSRP))
1098 c->mask_a |= SSL_aSRP;
1099 c->mask_k |= SSL_kSRP;
1105 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1108 if (c->algorithm_ssl & ct->mask_ssl || c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a)
1110 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1113 static int tls_use_ticket(SSL *s)
1115 if (s->options & SSL_OP_NO_TICKET)
1117 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1120 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, int *al)
1123 unsigned char *orig = buf;
1124 unsigned char *ret = buf;
1125 #ifndef OPENSSL_NO_EC
1126 /* See if we support any ECC ciphersuites */
1128 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1131 unsigned long alg_k, alg_a;
1132 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1134 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1136 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1138 alg_k = c->algorithm_mkey;
1139 alg_a = c->algorithm_auth;
1140 if ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)
1141 || (alg_a & SSL_aECDSA)))
1150 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1151 if (s->client_version == SSL3_VERSION
1152 && !s->s3->send_connection_binding)
1157 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1159 if (s->tlsext_hostname != NULL)
1161 /* Add TLS extension servername to the Client Hello message */
1162 unsigned long size_str;
1165 /* check for enough space.
1166 4 for the servername type and entension length
1167 2 for servernamelist length
1168 1 for the hostname type
1169 2 for hostname length
1173 if ((lenmax = limit - ret - 9) < 0
1174 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1177 /* extension type and length */
1178 s2n(TLSEXT_TYPE_server_name,ret);
1179 s2n(size_str+5,ret);
1181 /* length of servername list */
1182 s2n(size_str+3,ret);
1184 /* hostname type, length and hostname */
1185 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1187 memcpy(ret, s->tlsext_hostname, size_str);
1191 /* Add RI if renegotiating */
1196 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1198 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1202 if((limit - ret - 4 - el) < 0) return NULL;
1204 s2n(TLSEXT_TYPE_renegotiate,ret);
1207 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1209 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1216 #ifndef OPENSSL_NO_SRP
1217 /* Add SRP username if there is one */
1218 if (s->srp_ctx.login != NULL)
1219 { /* Add TLS extension SRP username to the Client Hello message */
1221 int login_len = strlen(s->srp_ctx.login);
1222 if (login_len > 255 || login_len == 0)
1224 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1228 /* check for enough space.
1229 4 for the srp type type and entension length
1230 1 for the srp user identity
1231 + srp user identity length
1233 if ((limit - ret - 5 - login_len) < 0) return NULL;
1235 /* fill in the extension */
1236 s2n(TLSEXT_TYPE_srp,ret);
1237 s2n(login_len+1,ret);
1238 (*ret++) = (unsigned char) login_len;
1239 memcpy(ret, s->srp_ctx.login, login_len);
1244 #ifndef OPENSSL_NO_EC
1247 /* Add TLS extension ECPointFormats to the ClientHello message */
1249 const unsigned char *plist;
1252 unsigned char *etmp;
1254 tls1_get_formatlist(s, &plist, &plistlen);
1256 if ((lenmax = limit - ret - 5) < 0) return NULL;
1257 if (plistlen > (size_t)lenmax) return NULL;
1260 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1264 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1265 s2n(plistlen + 1,ret);
1266 *(ret++) = (unsigned char)plistlen ;
1267 memcpy(ret, plist, plistlen);
1270 /* Add TLS extension EllipticCurves to the ClientHello message */
1271 plist = s->tlsext_ellipticcurvelist;
1272 tls1_get_curvelist(s, 0, &plist, &plistlen);
1274 if ((lenmax = limit - ret - 6) < 0) return NULL;
1275 if (plistlen > (size_t)lenmax) return NULL;
1276 if (plistlen > 65532)
1278 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1283 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1285 /* Copy curve ID if supported */
1286 for (i = 0; i < plistlen; i += 2, plist += 2)
1288 if (tls_curve_allowed(s, plist, SSL_SECOP_CURVE_SUPPORTED))
1295 plistlen = etmp - ret - 4;
1297 s2n(plistlen + 2, ret);
1301 #endif /* OPENSSL_NO_EC */
1303 if (tls_use_ticket(s))
1306 if (!s->new_session && s->session && s->session->tlsext_tick)
1307 ticklen = s->session->tlsext_ticklen;
1308 else if (s->session && s->tlsext_session_ticket &&
1309 s->tlsext_session_ticket->data)
1311 ticklen = s->tlsext_session_ticket->length;
1312 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1313 if (!s->session->tlsext_tick)
1315 memcpy(s->session->tlsext_tick,
1316 s->tlsext_session_ticket->data,
1318 s->session->tlsext_ticklen = ticklen;
1322 if (ticklen == 0 && s->tlsext_session_ticket &&
1323 s->tlsext_session_ticket->data == NULL)
1325 /* Check for enough room 2 for extension type, 2 for len
1328 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1329 s2n(TLSEXT_TYPE_session_ticket,ret);
1333 memcpy(ret, s->session->tlsext_tick, ticklen);
1339 if (SSL_USE_SIGALGS(s))
1342 const unsigned char *salg;
1343 unsigned char *etmp;
1344 salglen = tls12_get_psigalgs(s, &salg);
1345 if ((size_t)(limit - ret) < salglen + 6)
1347 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1349 /* Skip over lengths for now */
1351 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1352 /* Fill in lengths */
1353 s2n(salglen + 2, etmp);
1358 #ifdef TLSEXT_TYPE_opaque_prf_input
1359 if (s->s3->client_opaque_prf_input != NULL)
1361 size_t col = s->s3->client_opaque_prf_input_len;
1363 if ((long)(limit - ret - 6 - col) < 0)
1365 if (col > 0xFFFD) /* can't happen */
1368 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1371 memcpy(ret, s->s3->client_opaque_prf_input, col);
1376 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1379 long extlen, idlen, itmp;
1383 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1385 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1386 itmp = i2d_OCSP_RESPID(id, NULL);
1392 if (s->tlsext_ocsp_exts)
1394 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1401 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1402 s2n(TLSEXT_TYPE_status_request, ret);
1403 if (extlen + idlen > 0xFFF0)
1405 s2n(extlen + idlen + 5, ret);
1406 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1408 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1410 /* save position of id len */
1411 unsigned char *q = ret;
1412 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1413 /* skip over id len */
1415 itmp = i2d_OCSP_RESPID(id, &ret);
1421 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1424 #ifndef OPENSSL_NO_HEARTBEATS
1425 /* Add Heartbeat extension */
1426 if ((limit - ret - 4 - 1) < 0)
1428 s2n(TLSEXT_TYPE_heartbeat,ret);
1431 * 1: peer may send requests
1432 * 2: peer not allowed to send requests
1434 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1435 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1437 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1440 #ifndef OPENSSL_NO_NEXTPROTONEG
1441 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1443 /* The client advertises an emtpy extension to indicate its
1444 * support for Next Protocol Negotiation */
1445 if (limit - ret - 4 < 0)
1447 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1452 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1454 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1456 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1457 s2n(2 + s->alpn_client_proto_list_len,ret);
1458 s2n(s->alpn_client_proto_list_len,ret);
1459 memcpy(ret, s->alpn_client_proto_list,
1460 s->alpn_client_proto_list_len);
1461 ret += s->alpn_client_proto_list_len;
1464 if(SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s))
1468 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1470 if((limit - ret - 4 - el) < 0) return NULL;
1472 s2n(TLSEXT_TYPE_use_srtp,ret);
1475 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1477 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1482 custom_ext_init(&s->cert->cli_ext);
1483 /* Add custom TLS Extensions to ClientHello */
1484 if (!custom_ext_add(s, 0, &ret, limit, al))
1486 #ifdef TLSEXT_TYPE_encrypt_then_mac
1487 if (s->version != SSL3_VERSION)
1489 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1494 /* Add padding to workaround bugs in F5 terminators.
1495 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
1497 * NB: because this code works out the length of all existing
1498 * extensions it MUST always appear last.
1500 if (s->options & SSL_OP_TLSEXT_PADDING)
1502 int hlen = ret - (unsigned char *)s->init_buf->data;
1503 /* The code in s23_clnt.c to build ClientHello messages
1504 * includes the 5-byte record header in the buffer, while
1505 * the code in s3_clnt.c does not.
1507 if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
1509 if (hlen > 0xff && hlen < 0x200)
1511 hlen = 0x200 - hlen;
1517 s2n(TLSEXT_TYPE_padding, ret);
1519 memset(ret, 0, hlen);
1524 if ((extdatalen = ret-orig-2)== 0)
1527 s2n(extdatalen, orig);
1531 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, int *al)
1534 unsigned char *orig = buf;
1535 unsigned char *ret = buf;
1536 #ifndef OPENSSL_NO_NEXTPROTONEG
1537 int next_proto_neg_seen;
1539 #ifndef OPENSSL_NO_EC
1540 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1541 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1542 int using_ecc = (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1543 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1545 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1546 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1550 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1552 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1554 if ((long)(limit - ret - 4) < 0) return NULL;
1556 s2n(TLSEXT_TYPE_server_name,ret);
1560 if(s->s3->send_connection_binding)
1564 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1566 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1570 if((limit - ret - 4 - el) < 0) return NULL;
1572 s2n(TLSEXT_TYPE_renegotiate,ret);
1575 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1577 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1584 #ifndef OPENSSL_NO_EC
1587 const unsigned char *plist;
1589 /* Add TLS extension ECPointFormats to the ServerHello message */
1592 tls1_get_formatlist(s, &plist, &plistlen);
1594 if ((lenmax = limit - ret - 5) < 0) return NULL;
1595 if (plistlen > (size_t)lenmax) return NULL;
1598 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1602 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1603 s2n(plistlen + 1,ret);
1604 *(ret++) = (unsigned char) plistlen;
1605 memcpy(ret, plist, plistlen);
1609 /* Currently the server should not respond with a SupportedCurves extension */
1610 #endif /* OPENSSL_NO_EC */
1612 if (s->tlsext_ticket_expected && tls_use_ticket(s))
1614 if ((long)(limit - ret - 4) < 0) return NULL;
1615 s2n(TLSEXT_TYPE_session_ticket,ret);
1619 if (s->tlsext_status_expected)
1621 if ((long)(limit - ret - 4) < 0) return NULL;
1622 s2n(TLSEXT_TYPE_status_request,ret);
1626 #ifdef TLSEXT_TYPE_opaque_prf_input
1627 if (s->s3->server_opaque_prf_input != NULL)
1629 size_t sol = s->s3->server_opaque_prf_input_len;
1631 if ((long)(limit - ret - 6 - sol) < 0)
1633 if (sol > 0xFFFD) /* can't happen */
1636 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1639 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1644 if(SSL_IS_DTLS(s) && s->srtp_profile)
1648 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1650 if((limit - ret - 4 - el) < 0) return NULL;
1652 s2n(TLSEXT_TYPE_use_srtp,ret);
1655 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1657 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1663 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1664 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1665 { const unsigned char cryptopro_ext[36] = {
1666 0xfd, 0xe8, /*65000*/
1667 0x00, 0x20, /*32 bytes length*/
1668 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1669 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1670 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1671 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1672 if (limit-ret<36) return NULL;
1673 memcpy(ret,cryptopro_ext,36);
1678 #ifndef OPENSSL_NO_HEARTBEATS
1679 /* Add Heartbeat extension if we've received one */
1680 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1682 if ((limit - ret - 4 - 1) < 0)
1684 s2n(TLSEXT_TYPE_heartbeat,ret);
1687 * 1: peer may send requests
1688 * 2: peer not allowed to send requests
1690 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1691 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1693 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1698 #ifndef OPENSSL_NO_NEXTPROTONEG
1699 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1700 s->s3->next_proto_neg_seen = 0;
1701 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1703 const unsigned char *npa;
1704 unsigned int npalen;
1707 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1708 if (r == SSL_TLSEXT_ERR_OK)
1710 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1711 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1713 memcpy(ret, npa, npalen);
1715 s->s3->next_proto_neg_seen = 1;
1719 if (!custom_ext_add(s, 1, &ret, limit, al))
1721 #ifdef TLSEXT_TYPE_encrypt_then_mac
1722 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC)
1724 /* Don't use encrypt_then_mac if AEAD, RC4 or SSL 3.0:
1725 * might want to disable for other cases too.
1727 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1728 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1729 || s->version == SSL3_VERSION)
1730 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1733 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1739 if (s->s3->alpn_selected)
1741 const unsigned char *selected = s->s3->alpn_selected;
1742 unsigned len = s->s3->alpn_selected_len;
1744 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1746 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1750 memcpy(ret, selected, len);
1754 if ((extdatalen = ret-orig-2)== 0)
1757 s2n(extdatalen, orig);
1761 /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1763 * data: the contents of the extension, not including the type and length.
1764 * data_len: the number of bytes in |data|
1765 * al: a pointer to the alert value to send in the event of a non-zero
1768 * returns: 0 on success. */
1769 static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
1770 unsigned data_len, int *al)
1774 const unsigned char *selected;
1775 unsigned char selected_len;
1778 if (s->ctx->alpn_select_cb == NULL)
1784 /* data should contain a uint16 length followed by a series of 8-bit,
1785 * length-prefixed strings. */
1786 i = ((unsigned) data[0]) << 8 |
1787 ((unsigned) data[1]);
1796 for (i = 0; i < data_len;)
1798 proto_len = data[i];
1804 if (i + proto_len < i || i + proto_len > data_len)
1810 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1811 s->ctx->alpn_select_cb_arg);
1812 if (r == SSL_TLSEXT_ERR_OK) {
1813 if (s->s3->alpn_selected)
1814 OPENSSL_free(s->s3->alpn_selected);
1815 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1816 if (!s->s3->alpn_selected)
1818 *al = SSL_AD_INTERNAL_ERROR;
1821 memcpy(s->s3->alpn_selected, selected, selected_len);
1822 s->s3->alpn_selected_len = selected_len;
1827 *al = SSL_AD_DECODE_ERROR;
1831 #ifndef OPENSSL_NO_EC
1832 /* ssl_check_for_safari attempts to fingerprint Safari using OS X
1833 * SecureTransport using the TLS extension block in |d|, of length |n|.
1834 * Safari, since 10.6, sends exactly these extensions, in this order:
1839 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1840 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1841 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1842 * 10.8..10.8.3 (which don't work).
1844 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
1845 unsigned short type, size;
1846 static const unsigned char kSafariExtensionsBlock[] = {
1847 0x00, 0x0a, /* elliptic_curves extension */
1848 0x00, 0x08, /* 8 bytes */
1849 0x00, 0x06, /* 6 bytes of curve ids */
1850 0x00, 0x17, /* P-256 */
1851 0x00, 0x18, /* P-384 */
1852 0x00, 0x19, /* P-521 */
1854 0x00, 0x0b, /* ec_point_formats */
1855 0x00, 0x02, /* 2 bytes */
1856 0x01, /* 1 point format */
1857 0x00, /* uncompressed */
1860 /* The following is only present in TLS 1.2 */
1861 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1862 0x00, 0x0d, /* signature_algorithms */
1863 0x00, 0x0c, /* 12 bytes */
1864 0x00, 0x0a, /* 10 bytes */
1865 0x05, 0x01, /* SHA-384/RSA */
1866 0x04, 0x01, /* SHA-256/RSA */
1867 0x02, 0x01, /* SHA-1/RSA */
1868 0x04, 0x03, /* SHA-256/ECDSA */
1869 0x02, 0x03, /* SHA-1/ECDSA */
1872 if (data >= (d+n-2))
1881 if (type != TLSEXT_TYPE_server_name)
1884 if (data+size > d+n)
1888 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1890 const size_t len1 = sizeof(kSafariExtensionsBlock);
1891 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1893 if (data + len1 + len2 != d+n)
1895 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1897 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1902 const size_t len = sizeof(kSafariExtensionsBlock);
1904 if (data + len != d+n)
1906 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
1910 s->s3->is_probably_safari = 1;
1912 #endif /* !OPENSSL_NO_EC */
1915 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1917 unsigned short type;
1918 unsigned short size;
1920 unsigned char *data = *p;
1921 int renegotiate_seen = 0;
1923 s->servername_done = 0;
1924 s->tlsext_status_type = -1;
1925 #ifndef OPENSSL_NO_NEXTPROTONEG
1926 s->s3->next_proto_neg_seen = 0;
1929 if (s->s3->alpn_selected)
1931 OPENSSL_free(s->s3->alpn_selected);
1932 s->s3->alpn_selected = NULL;
1935 #ifndef OPENSSL_NO_HEARTBEATS
1936 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1937 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1940 #ifndef OPENSSL_NO_EC
1941 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1942 ssl_check_for_safari(s, data, d, n);
1943 #endif /* !OPENSSL_NO_EC */
1945 /* Clear any signature algorithms extension received */
1946 if (s->cert->peer_sigalgs)
1948 OPENSSL_free(s->cert->peer_sigalgs);
1949 s->cert->peer_sigalgs = NULL;
1952 #ifdef TLSEXT_TYPE_encrypt_then_mac
1953 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1956 if (data >= (d+n-2))
1960 if (data > (d+n-len))
1963 while (data <= (d+n-4))
1968 if (data+size > (d+n))
1971 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1973 if (s->tlsext_debug_cb)
1974 s->tlsext_debug_cb(s, 0, type, data, size,
1975 s->tlsext_debug_arg);
1976 /* The servername extension is treated as follows:
1978 - Only the hostname type is supported with a maximum length of 255.
1979 - The servername is rejected if too long or if it contains zeros,
1980 in which case an fatal alert is generated.
1981 - The servername field is maintained together with the session cache.
1982 - When a session is resumed, the servername call back invoked in order
1983 to allow the application to position itself to the right context.
1984 - The servername is acknowledged if it is new for a session or when
1985 it is identical to a previously used for the same session.
1986 Applications can control the behaviour. They can at any time
1987 set a 'desirable' servername for a new SSL object. This can be the
1988 case for example with HTTPS when a Host: header field is received and
1989 a renegotiation is requested. In this case, a possible servername
1990 presented in the new client hello is only acknowledged if it matches
1991 the value of the Host: field.
1992 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1993 if they provide for changing an explicit servername context for the session,
1994 i.e. when the session has been established with a servername extension.
1995 - On session reconnect, the servername extension may be absent.
1999 if (type == TLSEXT_TYPE_server_name)
2001 unsigned char *sdata;
2007 *al = SSL_AD_DECODE_ERROR;
2014 *al = SSL_AD_DECODE_ERROR;
2021 servname_type = *(sdata++);
2027 *al = SSL_AD_DECODE_ERROR;
2030 if (s->servername_done == 0)
2031 switch (servname_type)
2033 case TLSEXT_NAMETYPE_host_name:
2036 if(s->session->tlsext_hostname)
2038 *al = SSL_AD_DECODE_ERROR;
2041 if (len > TLSEXT_MAXLEN_host_name)
2043 *al = TLS1_AD_UNRECOGNIZED_NAME;
2046 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
2048 *al = TLS1_AD_INTERNAL_ERROR;
2051 memcpy(s->session->tlsext_hostname, sdata, len);
2052 s->session->tlsext_hostname[len]='\0';
2053 if (strlen(s->session->tlsext_hostname) != len) {
2054 OPENSSL_free(s->session->tlsext_hostname);
2055 s->session->tlsext_hostname = NULL;
2056 *al = TLS1_AD_UNRECOGNIZED_NAME;
2059 s->servername_done = 1;
2063 s->servername_done = s->session->tlsext_hostname
2064 && strlen(s->session->tlsext_hostname) == len
2065 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2077 *al = SSL_AD_DECODE_ERROR;
2082 #ifndef OPENSSL_NO_SRP
2083 else if (type == TLSEXT_TYPE_srp)
2085 if (size <= 0 || ((len = data[0])) != (size -1))
2087 *al = SSL_AD_DECODE_ERROR;
2090 if (s->srp_ctx.login != NULL)
2092 *al = SSL_AD_DECODE_ERROR;
2095 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
2097 memcpy(s->srp_ctx.login, &data[1], len);
2098 s->srp_ctx.login[len]='\0';
2100 if (strlen(s->srp_ctx.login) != len)
2102 *al = SSL_AD_DECODE_ERROR;
2108 #ifndef OPENSSL_NO_EC
2109 else if (type == TLSEXT_TYPE_ec_point_formats)
2111 unsigned char *sdata = data;
2112 int ecpointformatlist_length = *(sdata++);
2114 if (ecpointformatlist_length != size - 1 ||
2115 ecpointformatlist_length < 1)
2117 *al = TLS1_AD_DECODE_ERROR;
2122 if(s->session->tlsext_ecpointformatlist)
2124 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2125 s->session->tlsext_ecpointformatlist = NULL;
2127 s->session->tlsext_ecpointformatlist_length = 0;
2128 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2130 *al = TLS1_AD_INTERNAL_ERROR;
2133 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2134 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2137 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2138 sdata = s->session->tlsext_ecpointformatlist;
2139 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2140 fprintf(stderr,"%i ",*(sdata++));
2141 fprintf(stderr,"\n");
2144 else if (type == TLSEXT_TYPE_elliptic_curves)
2146 unsigned char *sdata = data;
2147 int ellipticcurvelist_length = (*(sdata++) << 8);
2148 ellipticcurvelist_length += (*(sdata++));
2150 if (ellipticcurvelist_length != size - 2 ||
2151 ellipticcurvelist_length < 1)
2153 *al = TLS1_AD_DECODE_ERROR;
2158 if(s->session->tlsext_ellipticcurvelist)
2160 *al = TLS1_AD_DECODE_ERROR;
2163 s->session->tlsext_ellipticcurvelist_length = 0;
2164 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2166 *al = TLS1_AD_INTERNAL_ERROR;
2169 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2170 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2173 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2174 sdata = s->session->tlsext_ellipticcurvelist;
2175 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2176 fprintf(stderr,"%i ",*(sdata++));
2177 fprintf(stderr,"\n");
2180 #endif /* OPENSSL_NO_EC */
2181 #ifdef TLSEXT_TYPE_opaque_prf_input
2182 else if (type == TLSEXT_TYPE_opaque_prf_input)
2184 unsigned char *sdata = data;
2188 *al = SSL_AD_DECODE_ERROR;
2191 n2s(sdata, s->s3->client_opaque_prf_input_len);
2192 if (s->s3->client_opaque_prf_input_len != size - 2)
2194 *al = SSL_AD_DECODE_ERROR;
2198 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2199 OPENSSL_free(s->s3->client_opaque_prf_input);
2200 if (s->s3->client_opaque_prf_input_len == 0)
2201 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2203 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2204 if (s->s3->client_opaque_prf_input == NULL)
2206 *al = TLS1_AD_INTERNAL_ERROR;
2211 else if (type == TLSEXT_TYPE_session_ticket)
2213 if (s->tls_session_ticket_ext_cb &&
2214 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2216 *al = TLS1_AD_INTERNAL_ERROR;
2220 else if (type == TLSEXT_TYPE_renegotiate)
2222 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2224 renegotiate_seen = 1;
2226 else if (type == TLSEXT_TYPE_signature_algorithms)
2229 if (s->cert->peer_sigalgs || size < 2)
2231 *al = SSL_AD_DECODE_ERROR;
2236 if (dsize != size || dsize & 1 || !dsize)
2238 *al = SSL_AD_DECODE_ERROR;
2241 if (!tls1_save_sigalgs(s, data, dsize))
2243 *al = SSL_AD_DECODE_ERROR;
2247 else if (type == TLSEXT_TYPE_status_request)
2252 *al = SSL_AD_DECODE_ERROR;
2256 s->tlsext_status_type = *data++;
2258 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2260 const unsigned char *sdata;
2262 /* Read in responder_id_list */
2267 *al = SSL_AD_DECODE_ERROR;
2276 *al = SSL_AD_DECODE_ERROR;
2280 dsize -= 2 + idsize;
2284 *al = SSL_AD_DECODE_ERROR;
2289 id = d2i_OCSP_RESPID(NULL,
2293 *al = SSL_AD_DECODE_ERROR;
2298 OCSP_RESPID_free(id);
2299 *al = SSL_AD_DECODE_ERROR;
2302 if (!s->tlsext_ocsp_ids
2303 && !(s->tlsext_ocsp_ids =
2304 sk_OCSP_RESPID_new_null()))
2306 OCSP_RESPID_free(id);
2307 *al = SSL_AD_INTERNAL_ERROR;
2310 if (!sk_OCSP_RESPID_push(
2311 s->tlsext_ocsp_ids, id))
2313 OCSP_RESPID_free(id);
2314 *al = SSL_AD_INTERNAL_ERROR;
2319 /* Read in request_extensions */
2322 *al = SSL_AD_DECODE_ERROR;
2329 *al = SSL_AD_DECODE_ERROR;
2335 if (s->tlsext_ocsp_exts)
2337 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2338 X509_EXTENSION_free);
2341 s->tlsext_ocsp_exts =
2342 d2i_X509_EXTENSIONS(NULL,
2344 if (!s->tlsext_ocsp_exts
2345 || (data + dsize != sdata))
2347 *al = SSL_AD_DECODE_ERROR;
2352 /* We don't know what to do with any other type
2356 s->tlsext_status_type = -1;
2358 #ifndef OPENSSL_NO_HEARTBEATS
2359 else if (type == TLSEXT_TYPE_heartbeat)
2363 case 0x01: /* Client allows us to send HB requests */
2364 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2366 case 0x02: /* Client doesn't accept HB requests */
2367 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2368 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2370 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2375 #ifndef OPENSSL_NO_NEXTPROTONEG
2376 else if (type == TLSEXT_TYPE_next_proto_neg &&
2377 s->s3->tmp.finish_md_len == 0 &&
2378 s->s3->alpn_selected == NULL)
2380 /* We shouldn't accept this extension on a
2383 * s->new_session will be set on renegotiation, but we
2384 * probably shouldn't rely that it couldn't be set on
2385 * the initial renegotation too in certain cases (when
2386 * there's some other reason to disallow resuming an
2387 * earlier session -- the current code won't be doing
2388 * anything like that, but this might change).
2390 * A valid sign that there's been a previous handshake
2391 * in this connection is if s->s3->tmp.finish_md_len >
2392 * 0. (We are talking about a check that will happen
2393 * in the Hello protocol round, well before a new
2394 * Finished message could have been computed.) */
2395 s->s3->next_proto_neg_seen = 1;
2399 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2400 s->ctx->alpn_select_cb &&
2401 s->s3->tmp.finish_md_len == 0)
2403 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2405 #ifndef OPENSSL_NO_NEXTPROTONEG
2406 /* ALPN takes precedence over NPN. */
2407 s->s3->next_proto_neg_seen = 0;
2411 /* session ticket processed earlier */
2412 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2413 && type == TLSEXT_TYPE_use_srtp)
2415 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2419 #ifdef TLSEXT_TYPE_encrypt_then_mac
2420 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2422 if (s->version != SSL3_VERSION)
2423 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2426 /* If this ClientHello extension was unhandled and this is
2427 * a nonresumed connection, check whether the extension is a
2428 * custom TLS Extension (has a custom_srv_ext_record), and if
2429 * so call the callback and record the extension number so that
2430 * an appropriate ServerHello may be later returned.
2434 if (custom_ext_parse(s, 1, type, data, size, al) <= 0)
2445 /* Need RI if renegotiating */
2447 if (!renegotiate_seen && s->renegotiate &&
2448 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2450 *al = SSL_AD_HANDSHAKE_FAILURE;
2451 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2452 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2459 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2462 custom_ext_init(&s->cert->srv_ext);
2463 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2465 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2469 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2471 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2477 #ifndef OPENSSL_NO_NEXTPROTONEG
2478 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2479 * elements of zero length are allowed and the set of elements must exactly fill
2480 * the length of the block. */
2481 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2483 unsigned int off = 0;
2497 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2499 unsigned short length;
2500 unsigned short type;
2501 unsigned short size;
2502 unsigned char *data = *p;
2503 int tlsext_servername = 0;
2504 int renegotiate_seen = 0;
2506 #ifndef OPENSSL_NO_NEXTPROTONEG
2507 s->s3->next_proto_neg_seen = 0;
2510 if (s->s3->alpn_selected)
2512 OPENSSL_free(s->s3->alpn_selected);
2513 s->s3->alpn_selected = NULL;
2516 #ifndef OPENSSL_NO_HEARTBEATS
2517 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2518 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2521 #ifdef TLSEXT_TYPE_encrypt_then_mac
2522 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2525 if (data >= (d+n-2))
2529 if (data+length != d+n)
2531 *al = SSL_AD_DECODE_ERROR;
2535 while(data <= (d+n-4))
2540 if (data+size > (d+n))
2543 if (s->tlsext_debug_cb)
2544 s->tlsext_debug_cb(s, 1, type, data, size,
2545 s->tlsext_debug_arg);
2547 if (type == TLSEXT_TYPE_server_name)
2549 if (s->tlsext_hostname == NULL || size > 0)
2551 *al = TLS1_AD_UNRECOGNIZED_NAME;
2554 tlsext_servername = 1;
2557 #ifndef OPENSSL_NO_EC
2558 else if (type == TLSEXT_TYPE_ec_point_formats)
2560 unsigned char *sdata = data;
2561 int ecpointformatlist_length = *(sdata++);
2563 if (ecpointformatlist_length != size - 1)
2565 *al = TLS1_AD_DECODE_ERROR;
2570 s->session->tlsext_ecpointformatlist_length = 0;
2571 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2572 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2574 *al = TLS1_AD_INTERNAL_ERROR;
2577 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2578 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2581 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2582 sdata = s->session->tlsext_ecpointformatlist;
2583 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2584 fprintf(stderr,"%i ",*(sdata++));
2585 fprintf(stderr,"\n");
2588 #endif /* OPENSSL_NO_EC */
2590 else if (type == TLSEXT_TYPE_session_ticket)
2592 if (s->tls_session_ticket_ext_cb &&
2593 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2595 *al = TLS1_AD_INTERNAL_ERROR;
2598 if (!tls_use_ticket(s) || (size > 0))
2600 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2603 s->tlsext_ticket_expected = 1;
2605 #ifdef TLSEXT_TYPE_opaque_prf_input
2606 else if (type == TLSEXT_TYPE_opaque_prf_input)
2608 unsigned char *sdata = data;
2612 *al = SSL_AD_DECODE_ERROR;
2615 n2s(sdata, s->s3->server_opaque_prf_input_len);
2616 if (s->s3->server_opaque_prf_input_len != size - 2)
2618 *al = SSL_AD_DECODE_ERROR;
2622 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2623 OPENSSL_free(s->s3->server_opaque_prf_input);
2624 if (s->s3->server_opaque_prf_input_len == 0)
2625 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2627 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2629 if (s->s3->server_opaque_prf_input == NULL)
2631 *al = TLS1_AD_INTERNAL_ERROR;
2636 else if (type == TLSEXT_TYPE_status_request)
2638 /* MUST be empty and only sent if we've requested
2639 * a status request message.
2641 if ((s->tlsext_status_type == -1) || (size > 0))
2643 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2646 /* Set flag to expect CertificateStatus message */
2647 s->tlsext_status_expected = 1;
2649 #ifndef OPENSSL_NO_NEXTPROTONEG
2650 else if (type == TLSEXT_TYPE_next_proto_neg &&
2651 s->s3->tmp.finish_md_len == 0)
2653 unsigned char *selected;
2654 unsigned char selected_len;
2656 /* We must have requested it. */
2657 if (s->ctx->next_proto_select_cb == NULL)
2659 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2662 /* The data must be valid */
2663 if (!ssl_next_proto_validate(data, size))
2665 *al = TLS1_AD_DECODE_ERROR;
2668 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2670 *al = TLS1_AD_INTERNAL_ERROR;
2673 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2674 if (!s->next_proto_negotiated)
2676 *al = TLS1_AD_INTERNAL_ERROR;
2679 memcpy(s->next_proto_negotiated, selected, selected_len);
2680 s->next_proto_negotiated_len = selected_len;
2681 s->s3->next_proto_neg_seen = 1;
2685 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2689 /* We must have requested it. */
2690 if (s->alpn_client_proto_list == NULL)
2692 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2697 *al = TLS1_AD_DECODE_ERROR;
2700 /* The extension data consists of:
2701 * uint16 list_length
2702 * uint8 proto_length;
2703 * uint8 proto[proto_length]; */
2707 if (len != (unsigned) size - 2)
2709 *al = TLS1_AD_DECODE_ERROR;
2713 if (len != (unsigned) size - 3)
2715 *al = TLS1_AD_DECODE_ERROR;
2718 if (s->s3->alpn_selected)
2719 OPENSSL_free(s->s3->alpn_selected);
2720 s->s3->alpn_selected = OPENSSL_malloc(len);
2721 if (!s->s3->alpn_selected)
2723 *al = TLS1_AD_INTERNAL_ERROR;
2726 memcpy(s->s3->alpn_selected, data + 3, len);
2727 s->s3->alpn_selected_len = len;
2730 else if (type == TLSEXT_TYPE_renegotiate)
2732 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2734 renegotiate_seen = 1;
2736 #ifndef OPENSSL_NO_HEARTBEATS
2737 else if (type == TLSEXT_TYPE_heartbeat)
2741 case 0x01: /* Server allows us to send HB requests */
2742 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2744 case 0x02: /* Server doesn't accept HB requests */
2745 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2746 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2748 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2753 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp)
2755 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2759 #ifdef TLSEXT_TYPE_encrypt_then_mac
2760 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2762 /* Ignore if inappropriate ciphersuite or SSL 3.0 */
2763 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2764 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4
2765 && s->version != SSL3_VERSION)
2766 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2769 /* If this extension type was not otherwise handled, but
2770 * matches a custom_cli_ext_record, then send it to the c
2772 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2780 *al = SSL_AD_DECODE_ERROR;
2784 if (!s->hit && tlsext_servername == 1)
2786 if (s->tlsext_hostname)
2788 if (s->session->tlsext_hostname == NULL)
2790 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2791 if (!s->session->tlsext_hostname)
2793 *al = SSL_AD_UNRECOGNIZED_NAME;
2799 *al = SSL_AD_DECODE_ERROR;
2809 /* Determine if we need to see RI. Strictly speaking if we want to
2810 * avoid an attack we should *always* see RI even on initial server
2811 * hello because the client doesn't see any renegotiation during an
2812 * attack. However this would mean we could not connect to any server
2813 * which doesn't support RI so for the immediate future tolerate RI
2814 * absence on initial connect only.
2816 if (!renegotiate_seen
2817 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2818 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2820 *al = SSL_AD_HANDSHAKE_FAILURE;
2821 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2822 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2830 int ssl_prepare_clienthello_tlsext(SSL *s)
2833 #ifdef TLSEXT_TYPE_opaque_prf_input
2837 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2839 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2844 if (s->tlsext_opaque_prf_input != NULL)
2846 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2847 OPENSSL_free(s->s3->client_opaque_prf_input);
2849 if (s->tlsext_opaque_prf_input_len == 0)
2850 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2852 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2853 if (s->s3->client_opaque_prf_input == NULL)
2855 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2858 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2862 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2863 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2870 int ssl_prepare_serverhello_tlsext(SSL *s)
2875 static int ssl_check_clienthello_tlsext_early(SSL *s)
2877 int ret=SSL_TLSEXT_ERR_NOACK;
2878 int al = SSL_AD_UNRECOGNIZED_NAME;
2880 #ifndef OPENSSL_NO_EC
2881 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2882 * ssl3_choose_cipher in s3_lib.c.
2884 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2885 * ssl3_choose_cipher in s3_lib.c.
2889 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2890 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2891 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2892 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2894 #ifdef TLSEXT_TYPE_opaque_prf_input
2896 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2897 * but we might be sending an alert in response to the client hello,
2898 * so this has to happen here in
2899 * ssl_check_clienthello_tlsext_early(). */
2903 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2905 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2908 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2909 al = SSL_AD_INTERNAL_ERROR;
2914 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2915 OPENSSL_free(s->s3->server_opaque_prf_input);
2916 s->s3->server_opaque_prf_input = NULL;
2918 if (s->tlsext_opaque_prf_input != NULL)
2920 if (s->s3->client_opaque_prf_input != NULL &&
2921 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2923 /* can only use this extension if we have a server opaque PRF input
2924 * of the same length as the client opaque PRF input! */
2926 if (s->tlsext_opaque_prf_input_len == 0)
2927 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2929 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2930 if (s->s3->server_opaque_prf_input == NULL)
2932 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2933 al = SSL_AD_INTERNAL_ERROR;
2936 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2940 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2942 /* The callback wants to enforce use of the extension,
2943 * but we can't do that with the client opaque PRF input;
2944 * abort the handshake.
2946 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2947 al = SSL_AD_HANDSHAKE_FAILURE;
2955 case SSL_TLSEXT_ERR_ALERT_FATAL:
2956 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2959 case SSL_TLSEXT_ERR_ALERT_WARNING:
2960 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2963 case SSL_TLSEXT_ERR_NOACK:
2964 s->servername_done=0;
2970 int ssl_check_clienthello_tlsext_late(SSL *s)
2972 int ret = SSL_TLSEXT_ERR_OK;
2976 /* If status request then ask callback what to do.
2977 * Note: this must be called after servername callbacks in case
2978 * the certificate has changed, and must be called after the cipher
2979 * has been chosen because this may influence which certificate is sent
2981 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2984 CERT_PKEY *certpkey;
2985 certpkey = ssl_get_server_send_pkey(s);
2986 /* If no certificate can't return certificate status */
2987 if (certpkey == NULL)
2989 s->tlsext_status_expected = 0;
2992 /* Set current certificate to one we will use so
2993 * SSL_get_certificate et al can pick it up.
2995 s->cert->key = certpkey;
2996 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2999 /* We don't want to send a status request response */
3000 case SSL_TLSEXT_ERR_NOACK:
3001 s->tlsext_status_expected = 0;
3003 /* status request response should be sent */
3004 case SSL_TLSEXT_ERR_OK:
3005 if (s->tlsext_ocsp_resp)
3006 s->tlsext_status_expected = 1;
3008 s->tlsext_status_expected = 0;
3010 /* something bad happened */
3011 case SSL_TLSEXT_ERR_ALERT_FATAL:
3012 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3013 al = SSL_AD_INTERNAL_ERROR;
3018 s->tlsext_status_expected = 0;
3020 /* Clear any shared sigtnature algorithms */
3021 if (s->cert->shared_sigalgs)
3023 OPENSSL_free(s->cert->shared_sigalgs);
3024 s->cert->shared_sigalgs = NULL;
3026 /* Clear certificate digests and validity flags */
3027 for (i = 0; i < SSL_PKEY_NUM; i++)
3029 s->cert->pkeys[i].digest = NULL;
3030 s->cert->pkeys[i].valid_flags = 0;
3033 /* If sigalgs received process it. */
3034 if (s->cert->peer_sigalgs)
3036 if (!tls1_process_sigalgs(s))
3038 SSLerr(SSL_F_SSL_CHECK_CLIENTHELLO_TLSEXT_LATE,
3039 ERR_R_MALLOC_FAILURE);
3040 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3041 al = SSL_AD_INTERNAL_ERROR;
3044 /* Fatal error is no shared signature algorithms */
3045 if (!s->cert->shared_sigalgs)
3047 SSLerr(SSL_F_SSL_CHECK_CLIENTHELLO_TLSEXT_LATE,
3048 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
3049 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3050 al = SSL_AD_ILLEGAL_PARAMETER;
3055 ssl_cert_set_default_md(s->cert);
3060 case SSL_TLSEXT_ERR_ALERT_FATAL:
3061 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3064 case SSL_TLSEXT_ERR_ALERT_WARNING:
3065 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3073 int ssl_check_serverhello_tlsext(SSL *s)
3075 int ret=SSL_TLSEXT_ERR_NOACK;
3076 int al = SSL_AD_UNRECOGNIZED_NAME;
3078 #ifndef OPENSSL_NO_EC
3079 /* If we are client and using an elliptic curve cryptography cipher
3080 * suite, then if server returns an EC point formats lists extension
3081 * it must contain uncompressed.
3083 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3084 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3085 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3086 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3087 ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3089 /* we are using an ECC cipher */
3091 unsigned char *list;
3092 int found_uncompressed = 0;
3093 list = s->session->tlsext_ecpointformatlist;
3094 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3096 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3098 found_uncompressed = 1;
3102 if (!found_uncompressed)
3104 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3108 ret = SSL_TLSEXT_ERR_OK;
3109 #endif /* OPENSSL_NO_EC */
3111 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3112 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3113 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3114 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3116 #ifdef TLSEXT_TYPE_opaque_prf_input
3117 if (s->s3->server_opaque_prf_input_len > 0)
3119 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
3120 * So first verify that we really have a value from the server too. */
3122 if (s->s3->server_opaque_prf_input == NULL)
3124 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3125 al = SSL_AD_HANDSHAKE_FAILURE;
3128 /* Anytime the server *has* sent an opaque PRF input, we need to check
3129 * that we have a client opaque PRF input of the same size. */
3130 if (s->s3->client_opaque_prf_input == NULL ||
3131 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
3133 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3134 al = SSL_AD_ILLEGAL_PARAMETER;
3139 /* If we've requested certificate status and we wont get one
3142 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3143 && s->ctx && s->ctx->tlsext_status_cb)
3146 /* Set resp to NULL, resplen to -1 so callback knows
3147 * there is no response.
3149 if (s->tlsext_ocsp_resp)
3151 OPENSSL_free(s->tlsext_ocsp_resp);
3152 s->tlsext_ocsp_resp = NULL;
3154 s->tlsext_ocsp_resplen = -1;
3155 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3158 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3159 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3163 al = SSL_AD_INTERNAL_ERROR;
3164 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3170 case SSL_TLSEXT_ERR_ALERT_FATAL:
3171 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3174 case SSL_TLSEXT_ERR_ALERT_WARNING:
3175 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3178 case SSL_TLSEXT_ERR_NOACK:
3179 s->servername_done=0;
3185 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
3188 if (s->version < SSL3_VERSION)
3190 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0)
3192 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3196 if (ssl_check_serverhello_tlsext(s) <= 0)
3198 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,SSL_R_SERVERHELLO_TLSEXT);
3204 /* Since the server cache lookup is done early on in the processing of the
3205 * ClientHello, and other operations depend on the result, we need to handle
3206 * any TLS session ticket extension at the same time.
3208 * session_id: points at the session ID in the ClientHello. This code will
3209 * read past the end of this in order to parse out the session ticket
3210 * extension, if any.
3211 * len: the length of the session ID.
3212 * limit: a pointer to the first byte after the ClientHello.
3213 * ret: (output) on return, if a ticket was decrypted, then this is set to
3214 * point to the resulting session.
3216 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3217 * ciphersuite, in which case we have no use for session tickets and one will
3218 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3221 * -1: fatal error, either from parsing or decrypting the ticket.
3222 * 0: no ticket was found (or was ignored, based on settings).
3223 * 1: a zero length extension was found, indicating that the client supports
3224 * session tickets but doesn't currently have one to offer.
3225 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
3226 * couldn't be decrypted because of a non-fatal error.
3227 * 3: a ticket was successfully decrypted and *ret was set.
3230 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3231 * a new session ticket to the client because the client indicated support
3232 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3233 * a session ticket or we couldn't use the one it gave us, or if
3234 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3235 * Otherwise, s->tlsext_ticket_expected is set to 0.
3237 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
3238 const unsigned char *limit, SSL_SESSION **ret)
3240 /* Point after session ID in client hello */
3241 const unsigned char *p = session_id + len;
3245 s->tlsext_ticket_expected = 0;
3247 /* If tickets disabled behave as if no ticket present
3248 * to permit stateful resumption.
3250 if (!tls_use_ticket(s))
3252 if ((s->version <= SSL3_VERSION) || !limit)
3256 /* Skip past DTLS cookie */
3264 /* Skip past cipher list */
3269 /* Skip past compression algorithm list */
3274 /* Now at start of extensions */
3275 if ((p + 2) >= limit)
3278 while ((p + 4) <= limit)
3280 unsigned short type, size;
3283 if (p + size > limit)
3285 if (type == TLSEXT_TYPE_session_ticket)
3290 /* The client will accept a ticket but doesn't
3291 * currently have one. */
3292 s->tlsext_ticket_expected = 1;
3295 if (s->tls_session_secret_cb)
3297 /* Indicate that the ticket couldn't be
3298 * decrypted rather than generating the session
3299 * from ticket now, trigger abbreviated
3300 * handshake based on external mechanism to
3301 * calculate the master secret later. */
3304 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
3307 case 2: /* ticket couldn't be decrypted */
3308 s->tlsext_ticket_expected = 1;
3310 case 3: /* ticket was decrypted */
3312 case 4: /* ticket decrypted but need to renew */
3313 s->tlsext_ticket_expected = 1;
3315 default: /* fatal error */
3324 /* tls_decrypt_ticket attempts to decrypt a session ticket.
3326 * etick: points to the body of the session ticket extension.
3327 * eticklen: the length of the session tickets extenion.
3328 * sess_id: points at the session ID.
3329 * sesslen: the length of the session ID.
3330 * psess: (output) on return, if a ticket was decrypted, then this is set to
3331 * point to the resulting session.
3334 * -1: fatal error, either from parsing or decrypting the ticket.
3335 * 2: the ticket couldn't be decrypted.
3336 * 3: a ticket was successfully decrypted and *psess was set.
3337 * 4: same as 3, but the ticket needs to be renewed.
3339 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
3340 const unsigned char *sess_id, int sesslen,
3341 SSL_SESSION **psess)
3344 unsigned char *sdec;
3345 const unsigned char *p;
3346 int slen, mlen, renew_ticket = 0;
3347 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3350 SSL_CTX *tctx = s->initial_ctx;
3351 /* Need at least keyname + iv + some encrypted data */
3354 /* Initialize session ticket encryption and HMAC contexts */
3355 HMAC_CTX_init(&hctx);
3356 EVP_CIPHER_CTX_init(&ctx);
3357 if (tctx->tlsext_ticket_key_cb)
3359 unsigned char *nctick = (unsigned char *)etick;
3360 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3371 /* Check key name matches */
3372 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3374 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3375 tlsext_tick_md(), NULL);
3376 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3377 tctx->tlsext_tick_aes_key, etick + 16);
3379 /* Attempt to process session ticket, first conduct sanity and
3380 * integrity checks on ticket.
3382 mlen = HMAC_size(&hctx);
3385 EVP_CIPHER_CTX_cleanup(&ctx);
3389 /* Check HMAC of encrypted ticket */
3390 HMAC_Update(&hctx, etick, eticklen);
3391 HMAC_Final(&hctx, tick_hmac, NULL);
3392 HMAC_CTX_cleanup(&hctx);
3393 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
3395 EVP_CIPHER_CTX_cleanup(&ctx);
3398 /* Attempt to decrypt session data */
3399 /* Move p after IV to start of encrypted ticket, update length */
3400 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3401 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3402 sdec = OPENSSL_malloc(eticklen);
3405 EVP_CIPHER_CTX_cleanup(&ctx);
3408 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
3409 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
3411 EVP_CIPHER_CTX_cleanup(&ctx);
3416 EVP_CIPHER_CTX_cleanup(&ctx);
3419 sess = d2i_SSL_SESSION(NULL, &p, slen);
3423 /* The session ID, if non-empty, is used by some clients to
3424 * detect that the ticket has been accepted. So we copy it to
3425 * the session structure. If it is empty set length to zero
3426 * as required by standard.
3429 memcpy(sess->session_id, sess_id, sesslen);
3430 sess->session_id_length = sesslen;
3438 /* For session parse failure, indicate that we need to send a new
3443 /* Tables to translate from NIDs to TLS v1.2 ids */
3451 static tls12_lookup tls12_md[] = {
3452 {NID_md5, TLSEXT_hash_md5},
3453 {NID_sha1, TLSEXT_hash_sha1},
3454 {NID_sha224, TLSEXT_hash_sha224},
3455 {NID_sha256, TLSEXT_hash_sha256},
3456 {NID_sha384, TLSEXT_hash_sha384},
3457 {NID_sha512, TLSEXT_hash_sha512}
3460 static tls12_lookup tls12_sig[] = {
3461 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3462 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3463 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3466 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
3469 for (i = 0; i < tlen; i++)
3471 if (table[i].nid == nid)
3477 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
3480 for (i = 0; i < tlen; i++)
3482 if ((table[i].id) == id)
3483 return table[i].nid;
3488 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3493 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3494 sizeof(tls12_md)/sizeof(tls12_lookup));
3497 sig_id = tls12_get_sigid(pk);
3500 p[0] = (unsigned char)md_id;
3501 p[1] = (unsigned char)sig_id;
3505 int tls12_get_sigid(const EVP_PKEY *pk)
3507 return tls12_find_id(pk->type, tls12_sig,
3508 sizeof(tls12_sig)/sizeof(tls12_lookup));
3515 const EVP_MD *(*mfunc)(void);
3518 static const tls12_hash_info tls12_md_info[] = {
3519 #ifdef OPENSSL_NO_MD5
3522 {NID_md5, 64, EVP_md5},
3524 #ifdef OPENSSL_NO_SHA
3527 {NID_sha1, 80, EVP_sha1},
3529 #ifdef OPENSSL_NO_SHA256
3530 {NID_sha224, 112, 0},
3531 {NID_sha256, 128, 0},
3533 {NID_sha224, 112, EVP_sha224},
3534 {NID_sha256, 128, EVP_sha256},
3536 #ifdef OPENSSL_NO_SHA512
3537 {NID_sha384, 192, 0},
3538 {NID_sha512, 256, 0}
3540 {NID_sha384, 192, EVP_sha384},
3541 {NID_sha512, 256, EVP_sha512}
3545 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3549 if (hash_alg > sizeof(tls12_md_info)/sizeof(tls12_md_info[0]))
3551 return tls12_md_info + hash_alg - 1;
3554 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3556 const tls12_hash_info *inf;
3557 #ifndef OPENSSL_FIPS
3558 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3561 inf = tls12_get_hash_info(hash_alg);
3562 if (!inf || !inf->mfunc)
3564 return inf->mfunc();
3567 static int tls12_get_pkey_idx(unsigned char sig_alg)
3571 #ifndef OPENSSL_NO_RSA
3572 case TLSEXT_signature_rsa:
3573 return SSL_PKEY_RSA_SIGN;
3575 #ifndef OPENSSL_NO_DSA
3576 case TLSEXT_signature_dsa:
3577 return SSL_PKEY_DSA_SIGN;
3579 #ifndef OPENSSL_NO_ECDSA
3580 case TLSEXT_signature_ecdsa:
3581 return SSL_PKEY_ECC;