2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include <openssl/ocsp.h>
117 #include <openssl/rand.h>
118 #include "ssl_locl.h"
120 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
122 #ifndef OPENSSL_NO_TLSEXT
123 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
124 const unsigned char *sess_id, int sesslen,
125 SSL_SESSION **psess);
126 static int ssl_check_clienthello_tlsext_early(SSL *s);
127 int ssl_check_serverhello_tlsext(SSL *s);
130 SSL3_ENC_METHOD TLSv1_enc_data={
133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
137 TLS1_FINISH_MAC_LENGTH,
138 tls1_cert_verify_mac,
139 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
140 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
142 tls1_export_keying_material,
145 long tls1_default_timeout(void)
147 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
148 * is way too long for http, the cache would over fill */
154 if (!ssl3_new(s)) return(0);
155 s->method->ssl_clear(s);
159 void tls1_free(SSL *s)
161 #ifndef OPENSSL_NO_TLSEXT
162 if (s->tlsext_session_ticket)
164 OPENSSL_free(s->tlsext_session_ticket);
166 #endif /* OPENSSL_NO_TLSEXT */
170 void tls1_clear(SSL *s)
173 s->version = s->method->version;
176 #ifndef OPENSSL_NO_EC
178 static int nid_list[] =
180 NID_sect163k1, /* sect163k1 (1) */
181 NID_sect163r1, /* sect163r1 (2) */
182 NID_sect163r2, /* sect163r2 (3) */
183 NID_sect193r1, /* sect193r1 (4) */
184 NID_sect193r2, /* sect193r2 (5) */
185 NID_sect233k1, /* sect233k1 (6) */
186 NID_sect233r1, /* sect233r1 (7) */
187 NID_sect239k1, /* sect239k1 (8) */
188 NID_sect283k1, /* sect283k1 (9) */
189 NID_sect283r1, /* sect283r1 (10) */
190 NID_sect409k1, /* sect409k1 (11) */
191 NID_sect409r1, /* sect409r1 (12) */
192 NID_sect571k1, /* sect571k1 (13) */
193 NID_sect571r1, /* sect571r1 (14) */
194 NID_secp160k1, /* secp160k1 (15) */
195 NID_secp160r1, /* secp160r1 (16) */
196 NID_secp160r2, /* secp160r2 (17) */
197 NID_secp192k1, /* secp192k1 (18) */
198 NID_X9_62_prime192v1, /* secp192r1 (19) */
199 NID_secp224k1, /* secp224k1 (20) */
200 NID_secp224r1, /* secp224r1 (21) */
201 NID_secp256k1, /* secp256k1 (22) */
202 NID_X9_62_prime256v1, /* secp256r1 (23) */
203 NID_secp384r1, /* secp384r1 (24) */
204 NID_secp521r1 /* secp521r1 (25) */
208 static const unsigned char ecformats_default[] =
210 TLSEXT_ECPOINTFORMAT_uncompressed,
211 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
212 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
215 static const unsigned char eccurves_default[] =
217 0,14, /* sect571r1 (14) */
218 0,13, /* sect571k1 (13) */
219 0,25, /* secp521r1 (25) */
220 0,11, /* sect409k1 (11) */
221 0,12, /* sect409r1 (12) */
222 0,24, /* secp384r1 (24) */
223 0,9, /* sect283k1 (9) */
224 0,10, /* sect283r1 (10) */
225 0,22, /* secp256k1 (22) */
226 0,23, /* secp256r1 (23) */
227 0,8, /* sect239k1 (8) */
228 0,6, /* sect233k1 (6) */
229 0,7, /* sect233r1 (7) */
230 0,20, /* secp224k1 (20) */
231 0,21, /* secp224r1 (21) */
232 0,4, /* sect193r1 (4) */
233 0,5, /* sect193r2 (5) */
234 0,18, /* secp192k1 (18) */
235 0,19, /* secp192r1 (19) */
236 0,1, /* sect163k1 (1) */
237 0,2, /* sect163r1 (2) */
238 0,3, /* sect163r2 (3) */
239 0,15, /* secp160k1 (15) */
240 0,16, /* secp160r1 (16) */
241 0,17, /* secp160r2 (17) */
244 static const unsigned char suiteb_curves[] =
246 0, TLSEXT_curve_P_256,
247 0, TLSEXT_curve_P_384
250 int tls1_ec_curve_id2nid(int curve_id)
252 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
253 if ((curve_id < 1) || ((unsigned int)curve_id >
254 sizeof(nid_list)/sizeof(nid_list[0])))
256 return nid_list[curve_id-1];
259 int tls1_ec_nid2curve_id(int nid)
261 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
264 case NID_sect163k1: /* sect163k1 (1) */
266 case NID_sect163r1: /* sect163r1 (2) */
268 case NID_sect163r2: /* sect163r2 (3) */
270 case NID_sect193r1: /* sect193r1 (4) */
272 case NID_sect193r2: /* sect193r2 (5) */
274 case NID_sect233k1: /* sect233k1 (6) */
276 case NID_sect233r1: /* sect233r1 (7) */
278 case NID_sect239k1: /* sect239k1 (8) */
280 case NID_sect283k1: /* sect283k1 (9) */
282 case NID_sect283r1: /* sect283r1 (10) */
284 case NID_sect409k1: /* sect409k1 (11) */
286 case NID_sect409r1: /* sect409r1 (12) */
288 case NID_sect571k1: /* sect571k1 (13) */
290 case NID_sect571r1: /* sect571r1 (14) */
292 case NID_secp160k1: /* secp160k1 (15) */
294 case NID_secp160r1: /* secp160r1 (16) */
296 case NID_secp160r2: /* secp160r2 (17) */
298 case NID_secp192k1: /* secp192k1 (18) */
300 case NID_X9_62_prime192v1: /* secp192r1 (19) */
302 case NID_secp224k1: /* secp224k1 (20) */
304 case NID_secp224r1: /* secp224r1 (21) */
306 case NID_secp256k1: /* secp256k1 (22) */
308 case NID_X9_62_prime256v1: /* secp256r1 (23) */
310 case NID_secp384r1: /* secp384r1 (24) */
312 case NID_secp521r1: /* secp521r1 (25) */
318 /* Get curves list, if "sess" is set return client curves otherwise
321 static void tls1_get_curvelist(SSL *s, int sess,
322 const unsigned char **pcurves,
327 *pcurves = s->session->tlsext_ellipticcurvelist;
328 *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
331 /* For Suite B mode only include P-256, P-384 */
332 switch (tls1_suiteb(s))
334 case SSL_CERT_FLAG_SUITEB_128_LOS:
335 *pcurves = suiteb_curves;
336 *pcurveslen = sizeof(suiteb_curves);
339 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
340 *pcurves = suiteb_curves;
344 case SSL_CERT_FLAG_SUITEB_192_LOS:
345 *pcurves = suiteb_curves + 2;
349 *pcurves = s->tlsext_ellipticcurvelist;
350 *pcurveslen = s->tlsext_ellipticcurvelist_length;
354 *pcurves = eccurves_default;
355 *pcurveslen = sizeof(eccurves_default);
358 /* Check a curve is one of our preferences */
359 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
361 const unsigned char *curves;
363 unsigned int suiteb_flags = tls1_suiteb(s);
364 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
366 /* Check curve matches Suite B preferences */
369 unsigned long cid = s->s3->tmp.new_cipher->id;
372 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
374 if (p[2] != TLSEXT_curve_P_256)
377 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
379 if (p[2] != TLSEXT_curve_P_384)
382 else /* Should never happen */
385 tls1_get_curvelist(s, 0, &curves, &curveslen);
386 for (i = 0; i < curveslen; i += 2, curves += 2)
388 if (p[1] == curves[0] && p[2] == curves[1])
394 /* Return nth shared curve. If nmatch == -1 return number of
395 * matches. For nmatch == -2 return the NID of the curve to use for
399 int tls1_shared_curve(SSL *s, int nmatch)
401 const unsigned char *pref, *supp;
402 size_t preflen, supplen, i, j;
404 /* Can't do anything on client side */
411 /* For Suite B ciphersuite determines curve: we
412 * already know these are acceptable due to previous
415 unsigned long cid = s->s3->tmp.new_cipher->id;
416 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
417 return NID_X9_62_prime256v1; /* P-256 */
418 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
419 return NID_secp384r1; /* P-384 */
420 /* Should never happen */
423 /* If not Suite B just return first preference shared curve */
426 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
428 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
433 for (i = 0; i < preflen; i++, pref+=2)
435 const unsigned char *tsupp = supp;
436 for (j = 0; j < supplen; j++, tsupp+=2)
438 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
442 int id = (pref[0] << 8) | pref[1];
443 return tls1_ec_curve_id2nid(id);
454 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
455 int *curves, size_t ncurves)
457 unsigned char *clist, *p;
459 /* Bitmap of curves included to detect duplicates: only works
460 * while curve ids < 32
462 unsigned long dup_list = 0;
463 clist = OPENSSL_malloc(ncurves * 2);
466 for (i = 0, p = clist; i < ncurves; i++)
468 unsigned long idmask;
470 id = tls1_ec_nid2curve_id(curves[i]);
472 if (!id || (dup_list & idmask))
483 *pextlen = ncurves * 2;
487 #define MAX_CURVELIST 25
492 int nid_arr[MAX_CURVELIST];
495 static int nid_cb(const char *elem, int len, void *arg)
497 nid_cb_st *narg = arg;
501 if (narg->nidcnt == MAX_CURVELIST)
503 if (len > (int)(sizeof(etmp) - 1))
505 memcpy(etmp, elem, len);
507 nid = EC_curve_nist2nid(etmp);
508 if (nid == NID_undef)
509 nid = OBJ_sn2nid(etmp);
510 if (nid == NID_undef)
511 nid = OBJ_ln2nid(etmp);
512 if (nid == NID_undef)
514 for (i = 0; i < narg->nidcnt; i++)
515 if (narg->nid_arr[i] == nid)
517 narg->nid_arr[narg->nidcnt++] = nid;
520 /* Set curves based on a colon separate list */
521 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
526 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
530 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
532 /* For an EC key set TLS id and required compression based on parameters */
533 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
539 const EC_METHOD *meth;
542 /* Determine if it is a prime field */
543 grp = EC_KEY_get0_group(ec);
544 pt = EC_KEY_get0_public_key(ec);
547 meth = EC_GROUP_method_of(grp);
550 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
554 /* Determine curve ID */
555 id = EC_GROUP_get_curve_name(grp);
556 id = tls1_ec_nid2curve_id(id);
557 /* If we have an ID set it, otherwise set arbitrary explicit curve */
561 curve_id[1] = (unsigned char)id;
573 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
576 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
578 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
581 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
585 /* Check an EC key is compatible with extensions */
586 static int tls1_check_ec_key(SSL *s,
587 unsigned char *curve_id, unsigned char *comp_id)
589 const unsigned char *p;
592 /* If point formats extension present check it, otherwise everything
593 * is supported (see RFC4492).
595 if (comp_id && s->session->tlsext_ecpointformatlist)
597 p = s->session->tlsext_ecpointformatlist;
598 plen = s->session->tlsext_ecpointformatlist_length;
599 for (i = 0; i < plen; i++, p++)
609 /* Check curve is consistent with client and server preferences */
610 for (j = 0; j <= 1; j++)
612 tls1_get_curvelist(s, j, &p, &plen);
613 for (i = 0; i < plen; i+=2, p+=2)
615 if (p[0] == curve_id[0] && p[1] == curve_id[1])
620 /* For clients can only check sent curve list */
627 /* Check cert parameters compatible with extensions: currently just checks
628 * EC certificates have compatible curves and compression.
630 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
632 unsigned char comp_id, curve_id[2];
635 pkey = X509_get_pubkey(x);
638 /* If not EC nothing to do */
639 if (pkey->type != EVP_PKEY_EC)
644 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
648 /* Can't check curve_id for client certs as we don't have a
649 * supported curves extension.
651 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
654 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
655 * SHA384+P-384, adjust digest if necessary.
657 if (set_ee_md && tls1_suiteb(s))
664 /* Check to see we have necessary signing algorithm */
665 if (curve_id[1] == TLSEXT_curve_P_256)
666 check_md = NID_ecdsa_with_SHA256;
667 else if (curve_id[1] == TLSEXT_curve_P_384)
668 check_md = NID_ecdsa_with_SHA384;
670 return 0; /* Should never happen */
671 for (i = 0; i < c->shared_sigalgslen; i++)
672 if (check_md == c->shared_sigalgs[i].signandhash_nid)
674 if (i == c->shared_sigalgslen)
678 if (check_md == NID_ecdsa_with_SHA256)
679 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
681 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
686 /* Check EC temporary key is compatible with client extensions */
687 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
689 unsigned char curve_id[2];
690 EC_KEY *ec = s->cert->ecdh_tmp;
691 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
692 /* Allow any curve: not just those peer supports */
693 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
696 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
697 * no other curves permitted.
701 /* Curve to check determined by ciphersuite */
702 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
703 curve_id[1] = TLSEXT_curve_P_256;
704 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
705 curve_id[1] = TLSEXT_curve_P_384;
709 /* Check this curve is acceptable */
710 if (!tls1_check_ec_key(s, curve_id, NULL))
712 /* If auto or setting curve from callback assume OK */
713 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
715 /* Otherwise check curve is acceptable */
718 unsigned char curve_tmp[2];
721 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
723 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
729 if (s->cert->ecdh_tmp_auto)
731 /* Need a shared curve */
732 if (tls1_shared_curve(s, 0))
738 if (s->cert->ecdh_tmp_cb)
743 if (!tls1_set_ec_id(curve_id, NULL, ec))
745 /* Set this to allow use of invalid curves for testing */
749 return tls1_check_ec_key(s, curve_id, NULL);
753 #endif /* OPENSSL_NO_EC */
755 #ifndef OPENSSL_NO_TLSEXT
757 /* List of supported signature algorithms and hashes. Should make this
758 * customisable at some point, for now include everything we support.
761 #ifdef OPENSSL_NO_RSA
762 #define tlsext_sigalg_rsa(md) /* */
764 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
767 #ifdef OPENSSL_NO_DSA
768 #define tlsext_sigalg_dsa(md) /* */
770 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
773 #ifdef OPENSSL_NO_ECDSA
774 #define tlsext_sigalg_ecdsa(md) /* */
776 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
779 #define tlsext_sigalg(md) \
780 tlsext_sigalg_rsa(md) \
781 tlsext_sigalg_dsa(md) \
782 tlsext_sigalg_ecdsa(md)
784 static unsigned char tls12_sigalgs[] = {
785 #ifndef OPENSSL_NO_SHA512
786 tlsext_sigalg(TLSEXT_hash_sha512)
787 tlsext_sigalg(TLSEXT_hash_sha384)
789 #ifndef OPENSSL_NO_SHA256
790 tlsext_sigalg(TLSEXT_hash_sha256)
791 tlsext_sigalg(TLSEXT_hash_sha224)
793 #ifndef OPENSSL_NO_SHA
794 tlsext_sigalg(TLSEXT_hash_sha1)
796 #ifndef OPENSSL_NO_MD5
797 tlsext_sigalg_rsa(TLSEXT_hash_md5)
801 static unsigned char suiteb_sigalgs[] = {
802 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
803 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
806 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
808 /* If Suite B mode use Suite B sigalgs only, ignore any other
811 switch (tls1_suiteb(s))
813 case SSL_CERT_FLAG_SUITEB_128_LOS:
814 *psigs = suiteb_sigalgs;
815 return sizeof(suiteb_sigalgs);
817 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
818 *psigs = suiteb_sigalgs;
821 case SSL_CERT_FLAG_SUITEB_192_LOS:
822 *psigs = suiteb_sigalgs + 2;
826 /* If server use client authentication sigalgs if not NULL */
827 if (s->server && s->cert->client_sigalgs)
829 *psigs = s->cert->client_sigalgs;
830 return s->cert->client_sigalgslen;
832 else if (s->cert->conf_sigalgs)
834 *psigs = s->cert->conf_sigalgs;
835 return s->cert->conf_sigalgslen;
839 *psigs = tls12_sigalgs;
841 /* If FIPS mode don't include MD5 which is last */
843 return sizeof(tls12_sigalgs) - 2;
846 return sizeof(tls12_sigalgs);
849 /* Check signature algorithm is consistent with sent supported signature
850 * algorithms and if so return relevant digest.
852 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
853 const unsigned char *sig, EVP_PKEY *pkey)
855 const unsigned char *sent_sigs;
856 size_t sent_sigslen, i;
857 int sigalg = tls12_get_sigid(pkey);
858 /* Should never happen */
861 /* Check key type is consistent with signature */
862 if (sigalg != (int)sig[1])
864 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
867 if (pkey->type == EVP_PKEY_EC)
869 unsigned char curve_id[2], comp_id;
870 /* Check compression and curve matches extensions */
871 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
873 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
875 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
878 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
883 if (curve_id[1] == TLSEXT_curve_P_256)
885 if (sig[0] != TLSEXT_hash_sha256)
887 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
888 SSL_R_ILLEGAL_SUITEB_DIGEST);
892 else if (curve_id[1] == TLSEXT_curve_P_384)
894 if (sig[0] != TLSEXT_hash_sha384)
896 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
897 SSL_R_ILLEGAL_SUITEB_DIGEST);
905 else if (tls1_suiteb(s))
908 /* Check signature matches a type we sent */
909 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
910 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
912 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
915 /* Allow fallback to SHA1 if not strict mode */
916 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
918 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
921 *pmd = tls12_get_hash(sig[0]);
924 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
927 /* Store the digest used so applications can retrieve it if they
930 if (s->session && s->session->sess_cert)
931 s->session->sess_cert->peer_key->digest = *pmd;
934 /* Get a mask of disabled algorithms: an algorithm is disabled
935 * if it isn't supported or doesn't appear in supported signature
936 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
937 * session and not global settings.
940 void ssl_set_client_disabled(SSL *s)
943 const unsigned char *sigalgs;
944 size_t i, sigalgslen;
945 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
948 /* If less than TLS 1.2 don't allow TLS 1.2 only ciphers */
949 if (TLS1_get_version(s) < TLS1_2_VERSION)
950 c->mask_ssl = SSL_TLSV1_2;
953 /* Now go through all signature algorithms seeing if we support
954 * any for RSA, DSA, ECDSA. Do this for all versions not just
957 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
958 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
962 #ifndef OPENSSL_NO_RSA
963 case TLSEXT_signature_rsa:
967 #ifndef OPENSSL_NO_DSA
968 case TLSEXT_signature_dsa:
972 #ifndef OPENSSL_NO_ECDSA
973 case TLSEXT_signature_ecdsa:
979 /* Disable auth and static DH if we don't include any appropriate
980 * signature algorithms.
984 c->mask_a |= SSL_aRSA;
985 c->mask_k |= SSL_kDHr|SSL_kECDHr;
989 c->mask_a |= SSL_aDSS;
990 c->mask_k |= SSL_kDHd;
994 c->mask_a |= SSL_aECDSA;
995 c->mask_k |= SSL_kECDHe;
997 #ifndef OPENSSL_NO_KRB5
998 if (!kssl_tgt_is_available(s->kssl_ctx))
1000 c->mask_a |= SSL_aKRB5;
1001 c->mask_k |= SSL_kKRB5;
1004 #ifndef OPENSSL_NO_PSK
1005 /* with PSK there must be client callback set */
1006 if (!s->psk_client_callback)
1008 c->mask_a |= SSL_aPSK;
1009 c->mask_k |= SSL_kPSK;
1011 #endif /* OPENSSL_NO_PSK */
1015 /* byte_compare is a compare function for qsort(3) that compares bytes. */
1016 static int byte_compare(const void *in_a, const void *in_b)
1018 unsigned char a = *((const unsigned char*) in_a);
1019 unsigned char b = *((const unsigned char*) in_b);
1028 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1031 unsigned char *ret = p;
1032 #ifndef OPENSSL_NO_EC
1033 /* See if we support any ECC ciphersuites */
1035 if (s->version != DTLS1_VERSION && s->version >= TLS1_VERSION)
1038 unsigned long alg_k, alg_a;
1039 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1041 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1043 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1045 alg_k = c->algorithm_mkey;
1046 alg_a = c->algorithm_auth;
1047 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
1048 || (alg_a & SSL_aECDSA)))
1057 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1058 if (s->client_version == SSL3_VERSION
1059 && !s->s3->send_connection_binding)
1064 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1066 if (s->tlsext_hostname != NULL)
1068 /* Add TLS extension servername to the Client Hello message */
1069 unsigned long size_str;
1072 /* check for enough space.
1073 4 for the servername type and entension length
1074 2 for servernamelist length
1075 1 for the hostname type
1076 2 for hostname length
1080 if ((lenmax = limit - ret - 9) < 0
1081 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1084 /* extension type and length */
1085 s2n(TLSEXT_TYPE_server_name,ret);
1086 s2n(size_str+5,ret);
1088 /* length of servername list */
1089 s2n(size_str+3,ret);
1091 /* hostname type, length and hostname */
1092 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1094 memcpy(ret, s->tlsext_hostname, size_str);
1098 /* Add RI if renegotiating */
1103 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1105 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1109 if((limit - p - 4 - el) < 0) return NULL;
1111 s2n(TLSEXT_TYPE_renegotiate,ret);
1114 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1116 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1123 #ifndef OPENSSL_NO_SRP
1124 /* Add SRP username if there is one */
1125 if (s->srp_ctx.login != NULL)
1126 { /* Add TLS extension SRP username to the Client Hello message */
1128 int login_len = strlen(s->srp_ctx.login);
1129 if (login_len > 255 || login_len == 0)
1131 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1135 /* check for enough space.
1136 4 for the srp type type and entension length
1137 1 for the srp user identity
1138 + srp user identity length
1140 if ((limit - ret - 5 - login_len) < 0) return NULL;
1142 /* fill in the extension */
1143 s2n(TLSEXT_TYPE_srp,ret);
1144 s2n(login_len+1,ret);
1145 (*ret++) = (unsigned char) login_len;
1146 memcpy(ret, s->srp_ctx.login, login_len);
1151 #ifndef OPENSSL_NO_EC
1154 /* Add TLS extension ECPointFormats to the ClientHello message */
1156 const unsigned char *plist;
1158 /* If we have a custom point format list use it otherwise
1160 plist = s->tlsext_ecpointformatlist;
1162 plistlen = s->tlsext_ecpointformatlist_length;
1165 plist = ecformats_default;
1166 plistlen = sizeof(ecformats_default);
1169 if ((lenmax = limit - ret - 5) < 0) return NULL;
1170 if (plistlen > (size_t)lenmax) return NULL;
1173 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1177 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1178 s2n(plistlen + 1,ret);
1179 *(ret++) = (unsigned char)plistlen ;
1180 memcpy(ret, plist, plistlen);
1183 /* Add TLS extension EllipticCurves to the ClientHello message */
1184 plist = s->tlsext_ellipticcurvelist;
1185 tls1_get_curvelist(s, 0, &plist, &plistlen);
1187 if ((lenmax = limit - ret - 6) < 0) return NULL;
1188 if (plistlen > (size_t)lenmax) return NULL;
1189 if (plistlen > 65532)
1191 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1195 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1196 s2n(plistlen + 2, ret);
1198 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1199 * elliptic_curve_list, but the examples use two bytes.
1200 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1201 * resolves this to two bytes.
1204 memcpy(ret, plist, plistlen);
1207 #endif /* OPENSSL_NO_EC */
1209 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1212 if (!s->new_session && s->session && s->session->tlsext_tick)
1213 ticklen = s->session->tlsext_ticklen;
1214 else if (s->session && s->tlsext_session_ticket &&
1215 s->tlsext_session_ticket->data)
1217 ticklen = s->tlsext_session_ticket->length;
1218 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1219 if (!s->session->tlsext_tick)
1221 memcpy(s->session->tlsext_tick,
1222 s->tlsext_session_ticket->data,
1224 s->session->tlsext_ticklen = ticklen;
1228 if (ticklen == 0 && s->tlsext_session_ticket &&
1229 s->tlsext_session_ticket->data == NULL)
1231 /* Check for enough room 2 for extension type, 2 for len
1234 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1235 s2n(TLSEXT_TYPE_session_ticket,ret);
1239 memcpy(ret, s->session->tlsext_tick, ticklen);
1245 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1248 const unsigned char *salg;
1249 salglen = tls12_get_psigalgs(s, &salg);
1250 if ((size_t)(limit - ret) < salglen + 6)
1252 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1253 s2n(salglen + 2, ret);
1255 memcpy(ret, salg, salglen);
1259 #ifdef TLSEXT_TYPE_opaque_prf_input
1260 if (s->s3->client_opaque_prf_input != NULL &&
1261 s->version != DTLS1_VERSION)
1263 size_t col = s->s3->client_opaque_prf_input_len;
1265 if ((long)(limit - ret - 6 - col < 0))
1267 if (col > 0xFFFD) /* can't happen */
1270 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1273 memcpy(ret, s->s3->client_opaque_prf_input, col);
1278 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
1279 s->version != DTLS1_VERSION)
1282 long extlen, idlen, itmp;
1286 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1288 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1289 itmp = i2d_OCSP_RESPID(id, NULL);
1295 if (s->tlsext_ocsp_exts)
1297 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1304 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1305 s2n(TLSEXT_TYPE_status_request, ret);
1306 if (extlen + idlen > 0xFFF0)
1308 s2n(extlen + idlen + 5, ret);
1309 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1311 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1313 /* save position of id len */
1314 unsigned char *q = ret;
1315 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1316 /* skip over id len */
1318 itmp = i2d_OCSP_RESPID(id, &ret);
1324 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1327 #ifndef OPENSSL_NO_HEARTBEATS
1328 /* Add Heartbeat extension */
1329 s2n(TLSEXT_TYPE_heartbeat,ret);
1332 * 1: peer may send requests
1333 * 2: peer not allowed to send requests
1335 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1336 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1338 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1341 #ifndef OPENSSL_NO_NEXTPROTONEG
1342 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1344 /* The client advertises an emtpy extension to indicate its
1345 * support for Next Protocol Negotiation */
1346 if (limit - ret - 4 < 0)
1348 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1353 if(SSL_get_srtp_profiles(s))
1357 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1359 if((limit - p - 4 - el) < 0) return NULL;
1361 s2n(TLSEXT_TYPE_use_srtp,ret);
1364 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1366 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1372 /* Add TLS extension Server_Authz_DataFormats to the ClientHello */
1373 /* 2 bytes for extension type */
1374 /* 2 bytes for extension length */
1375 /* 1 byte for the list length */
1376 /* 1 byte for the list (we only support audit proofs) */
1377 if (s->ctx->tlsext_authz_server_audit_proof_cb != NULL)
1380 const unsigned short ext_len = 2;
1381 const unsigned char list_len = 1;
1383 if ((lenmax = limit - ret - 6) < 0) return NULL;
1385 s2n(TLSEXT_TYPE_server_authz, ret);
1386 /* Extension length: 2 bytes */
1388 *(ret++) = list_len;
1389 *(ret++) = TLSEXT_AUTHZDATAFORMAT_audit_proof;
1392 if ((extdatalen = ret-p-2) == 0)
1399 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1402 unsigned char *ret = p;
1403 #ifndef OPENSSL_NO_NEXTPROTONEG
1404 int next_proto_neg_seen;
1407 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1408 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1412 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1414 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1416 if ((long)(limit - ret - 4) < 0) return NULL;
1418 s2n(TLSEXT_TYPE_server_name,ret);
1422 if(s->s3->send_connection_binding)
1426 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1428 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1432 if((limit - p - 4 - el) < 0) return NULL;
1434 s2n(TLSEXT_TYPE_renegotiate,ret);
1437 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1439 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1446 #ifndef OPENSSL_NO_EC
1447 if (s->tlsext_ecpointformatlist != NULL &&
1448 s->version != DTLS1_VERSION)
1450 /* Add TLS extension ECPointFormats to the ServerHello message */
1453 if ((lenmax = limit - ret - 5) < 0) return NULL;
1454 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
1455 if (s->tlsext_ecpointformatlist_length > 255)
1457 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1461 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1462 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
1463 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
1464 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
1465 ret+=s->tlsext_ecpointformatlist_length;
1468 /* Currently the server should not respond with a SupportedCurves extension */
1469 #endif /* OPENSSL_NO_EC */
1471 if (s->tlsext_ticket_expected
1472 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1474 if ((long)(limit - ret - 4) < 0) return NULL;
1475 s2n(TLSEXT_TYPE_session_ticket,ret);
1479 if (s->tlsext_status_expected)
1481 if ((long)(limit - ret - 4) < 0) return NULL;
1482 s2n(TLSEXT_TYPE_status_request,ret);
1486 #ifdef TLSEXT_TYPE_opaque_prf_input
1487 if (s->s3->server_opaque_prf_input != NULL &&
1488 s->version != DTLS1_VERSION)
1490 size_t sol = s->s3->server_opaque_prf_input_len;
1492 if ((long)(limit - ret - 6 - sol) < 0)
1494 if (sol > 0xFFFD) /* can't happen */
1497 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1500 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1509 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1511 if((limit - p - 4 - el) < 0) return NULL;
1513 s2n(TLSEXT_TYPE_use_srtp,ret);
1516 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1518 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1524 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1525 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1526 { const unsigned char cryptopro_ext[36] = {
1527 0xfd, 0xe8, /*65000*/
1528 0x00, 0x20, /*32 bytes length*/
1529 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1530 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1531 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1532 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1533 if (limit-ret<36) return NULL;
1534 memcpy(ret,cryptopro_ext,36);
1539 #ifndef OPENSSL_NO_HEARTBEATS
1540 /* Add Heartbeat extension if we've received one */
1541 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1543 s2n(TLSEXT_TYPE_heartbeat,ret);
1546 * 1: peer may send requests
1547 * 2: peer not allowed to send requests
1549 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1550 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1552 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1557 #ifndef OPENSSL_NO_NEXTPROTONEG
1558 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1559 s->s3->next_proto_neg_seen = 0;
1560 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1562 const unsigned char *npa;
1563 unsigned int npalen;
1566 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1567 if (r == SSL_TLSEXT_ERR_OK)
1569 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1570 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1572 memcpy(ret, npa, npalen);
1574 s->s3->next_proto_neg_seen = 1;
1579 /* If the client supports authz then see whether we have any to offer
1581 if (s->s3->tlsext_authz_client_types_len)
1583 size_t authz_length;
1584 /* By now we already know the new cipher, so we can look ahead
1585 * to see whether the cert we are going to send
1586 * has any authz data attached to it. */
1587 const unsigned char* authz = ssl_get_authz_data(s, &authz_length);
1588 const unsigned char* const orig_authz = authz;
1590 unsigned authz_count = 0;
1592 /* The authz data contains a number of the following structures:
1593 * uint8_t authz_type
1595 * uint8_t data[length]
1597 * First we walk over it to find the number of authz elements. */
1598 for (i = 0; i < authz_length; i++)
1600 unsigned short length;
1604 if (memchr(s->s3->tlsext_authz_client_types,
1606 s->s3->tlsext_authz_client_types_len) != NULL)
1610 /* n2s increments authz by 2 */
1618 /* Add TLS extension server_authz to the ServerHello message
1619 * 2 bytes for extension type
1620 * 2 bytes for extension length
1621 * 1 byte for the list length
1622 * n bytes for the list */
1623 const unsigned short ext_len = 1 + authz_count;
1625 if ((long)(limit - ret - 4 - ext_len) < 0) return NULL;
1626 s2n(TLSEXT_TYPE_server_authz, ret);
1628 *(ret++) = authz_count;
1629 s->s3->tlsext_authz_promised_to_client = 1;
1633 for (i = 0; i < authz_length; i++)
1635 unsigned short length;
1640 if (memchr(s->s3->tlsext_authz_client_types,
1642 s->s3->tlsext_authz_client_types_len) != NULL)
1645 /* n2s increments authz by 2 */
1652 if ((extdatalen = ret-p-2)== 0)
1659 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1661 unsigned short type;
1662 unsigned short size;
1664 unsigned char *data = *p;
1665 int renegotiate_seen = 0;
1668 s->servername_done = 0;
1669 s->tlsext_status_type = -1;
1670 #ifndef OPENSSL_NO_NEXTPROTONEG
1671 s->s3->next_proto_neg_seen = 0;
1674 #ifndef OPENSSL_NO_HEARTBEATS
1675 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1676 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1678 /* Clear any signature algorithms extension received */
1679 if (s->cert->peer_sigalgs)
1681 OPENSSL_free(s->cert->peer_sigalgs);
1682 s->cert->peer_sigalgs = NULL;
1684 /* Clear any shared sigtnature algorithms */
1685 if (s->cert->shared_sigalgs)
1687 OPENSSL_free(s->cert->shared_sigalgs);
1688 s->cert->shared_sigalgs = NULL;
1690 /* Clear certificate digests and validity flags */
1691 for (i = 0; i < SSL_PKEY_NUM; i++)
1693 s->cert->pkeys[i].digest = NULL;
1694 s->cert->pkeys[i].valid_flags = 0;
1697 if (data >= (d+n-2))
1701 if (data > (d+n-len))
1704 while (data <= (d+n-4))
1709 if (data+size > (d+n))
1712 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1714 if (s->tlsext_debug_cb)
1715 s->tlsext_debug_cb(s, 0, type, data, size,
1716 s->tlsext_debug_arg);
1717 /* The servername extension is treated as follows:
1719 - Only the hostname type is supported with a maximum length of 255.
1720 - The servername is rejected if too long or if it contains zeros,
1721 in which case an fatal alert is generated.
1722 - The servername field is maintained together with the session cache.
1723 - When a session is resumed, the servername call back invoked in order
1724 to allow the application to position itself to the right context.
1725 - The servername is acknowledged if it is new for a session or when
1726 it is identical to a previously used for the same session.
1727 Applications can control the behaviour. They can at any time
1728 set a 'desirable' servername for a new SSL object. This can be the
1729 case for example with HTTPS when a Host: header field is received and
1730 a renegotiation is requested. In this case, a possible servername
1731 presented in the new client hello is only acknowledged if it matches
1732 the value of the Host: field.
1733 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1734 if they provide for changing an explicit servername context for the session,
1735 i.e. when the session has been established with a servername extension.
1736 - On session reconnect, the servername extension may be absent.
1740 if (type == TLSEXT_TYPE_server_name)
1742 unsigned char *sdata;
1748 *al = SSL_AD_DECODE_ERROR;
1755 *al = SSL_AD_DECODE_ERROR;
1762 servname_type = *(sdata++);
1768 *al = SSL_AD_DECODE_ERROR;
1771 if (s->servername_done == 0)
1772 switch (servname_type)
1774 case TLSEXT_NAMETYPE_host_name:
1777 if(s->session->tlsext_hostname)
1779 *al = SSL_AD_DECODE_ERROR;
1782 if (len > TLSEXT_MAXLEN_host_name)
1784 *al = TLS1_AD_UNRECOGNIZED_NAME;
1787 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
1789 *al = TLS1_AD_INTERNAL_ERROR;
1792 memcpy(s->session->tlsext_hostname, sdata, len);
1793 s->session->tlsext_hostname[len]='\0';
1794 if (strlen(s->session->tlsext_hostname) != len) {
1795 OPENSSL_free(s->session->tlsext_hostname);
1796 s->session->tlsext_hostname = NULL;
1797 *al = TLS1_AD_UNRECOGNIZED_NAME;
1800 s->servername_done = 1;
1804 s->servername_done = s->session->tlsext_hostname
1805 && strlen(s->session->tlsext_hostname) == len
1806 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
1818 *al = SSL_AD_DECODE_ERROR;
1823 #ifndef OPENSSL_NO_SRP
1824 else if (type == TLSEXT_TYPE_srp)
1826 if (size <= 0 || ((len = data[0])) != (size -1))
1828 *al = SSL_AD_DECODE_ERROR;
1831 if (s->srp_ctx.login != NULL)
1833 *al = SSL_AD_DECODE_ERROR;
1836 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
1838 memcpy(s->srp_ctx.login, &data[1], len);
1839 s->srp_ctx.login[len]='\0';
1841 if (strlen(s->srp_ctx.login) != len)
1843 *al = SSL_AD_DECODE_ERROR;
1849 #ifndef OPENSSL_NO_EC
1850 else if (type == TLSEXT_TYPE_ec_point_formats &&
1851 s->version != DTLS1_VERSION)
1853 unsigned char *sdata = data;
1854 int ecpointformatlist_length = *(sdata++);
1856 if (ecpointformatlist_length != size - 1 ||
1857 ecpointformatlist_length < 1)
1859 *al = TLS1_AD_DECODE_ERROR;
1864 if(s->session->tlsext_ecpointformatlist)
1866 OPENSSL_free(s->session->tlsext_ecpointformatlist);
1867 s->session->tlsext_ecpointformatlist = NULL;
1869 s->session->tlsext_ecpointformatlist_length = 0;
1870 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
1872 *al = TLS1_AD_INTERNAL_ERROR;
1875 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
1876 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
1879 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
1880 sdata = s->session->tlsext_ecpointformatlist;
1881 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1882 fprintf(stderr,"%i ",*(sdata++));
1883 fprintf(stderr,"\n");
1886 else if (type == TLSEXT_TYPE_elliptic_curves &&
1887 s->version != DTLS1_VERSION)
1889 unsigned char *sdata = data;
1890 int ellipticcurvelist_length = (*(sdata++) << 8);
1891 ellipticcurvelist_length += (*(sdata++));
1893 if (ellipticcurvelist_length != size - 2 ||
1894 ellipticcurvelist_length < 1)
1896 *al = TLS1_AD_DECODE_ERROR;
1901 if(s->session->tlsext_ellipticcurvelist)
1903 *al = TLS1_AD_DECODE_ERROR;
1906 s->session->tlsext_ellipticcurvelist_length = 0;
1907 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
1909 *al = TLS1_AD_INTERNAL_ERROR;
1912 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
1913 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
1916 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
1917 sdata = s->session->tlsext_ellipticcurvelist;
1918 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
1919 fprintf(stderr,"%i ",*(sdata++));
1920 fprintf(stderr,"\n");
1923 #endif /* OPENSSL_NO_EC */
1924 #ifdef TLSEXT_TYPE_opaque_prf_input
1925 else if (type == TLSEXT_TYPE_opaque_prf_input &&
1926 s->version != DTLS1_VERSION)
1928 unsigned char *sdata = data;
1932 *al = SSL_AD_DECODE_ERROR;
1935 n2s(sdata, s->s3->client_opaque_prf_input_len);
1936 if (s->s3->client_opaque_prf_input_len != size - 2)
1938 *al = SSL_AD_DECODE_ERROR;
1942 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
1943 OPENSSL_free(s->s3->client_opaque_prf_input);
1944 if (s->s3->client_opaque_prf_input_len == 0)
1945 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1947 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
1948 if (s->s3->client_opaque_prf_input == NULL)
1950 *al = TLS1_AD_INTERNAL_ERROR;
1955 else if (type == TLSEXT_TYPE_session_ticket)
1957 if (s->tls_session_ticket_ext_cb &&
1958 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1960 *al = TLS1_AD_INTERNAL_ERROR;
1964 else if (type == TLSEXT_TYPE_renegotiate)
1966 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
1968 renegotiate_seen = 1;
1970 else if (type == TLSEXT_TYPE_signature_algorithms)
1973 if (s->cert->peer_sigalgs || size < 2)
1975 *al = SSL_AD_DECODE_ERROR;
1980 if (dsize != size || dsize & 1 || !dsize)
1982 *al = SSL_AD_DECODE_ERROR;
1985 if (!tls1_process_sigalgs(s, data, dsize))
1987 *al = SSL_AD_DECODE_ERROR;
1990 /* If sigalgs received and no shared algorithms fatal
1993 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1995 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
1996 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1997 *al = SSL_AD_ILLEGAL_PARAMETER;
2001 else if (type == TLSEXT_TYPE_status_request &&
2002 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
2007 *al = SSL_AD_DECODE_ERROR;
2011 s->tlsext_status_type = *data++;
2013 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2015 const unsigned char *sdata;
2017 /* Read in responder_id_list */
2022 *al = SSL_AD_DECODE_ERROR;
2031 *al = SSL_AD_DECODE_ERROR;
2035 dsize -= 2 + idsize;
2039 *al = SSL_AD_DECODE_ERROR;
2044 id = d2i_OCSP_RESPID(NULL,
2048 *al = SSL_AD_DECODE_ERROR;
2053 OCSP_RESPID_free(id);
2054 *al = SSL_AD_DECODE_ERROR;
2057 if (!s->tlsext_ocsp_ids
2058 && !(s->tlsext_ocsp_ids =
2059 sk_OCSP_RESPID_new_null()))
2061 OCSP_RESPID_free(id);
2062 *al = SSL_AD_INTERNAL_ERROR;
2065 if (!sk_OCSP_RESPID_push(
2066 s->tlsext_ocsp_ids, id))
2068 OCSP_RESPID_free(id);
2069 *al = SSL_AD_INTERNAL_ERROR;
2074 /* Read in request_extensions */
2077 *al = SSL_AD_DECODE_ERROR;
2084 *al = SSL_AD_DECODE_ERROR;
2090 if (s->tlsext_ocsp_exts)
2092 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2093 X509_EXTENSION_free);
2096 s->tlsext_ocsp_exts =
2097 d2i_X509_EXTENSIONS(NULL,
2099 if (!s->tlsext_ocsp_exts
2100 || (data + dsize != sdata))
2102 *al = SSL_AD_DECODE_ERROR;
2107 /* We don't know what to do with any other type
2111 s->tlsext_status_type = -1;
2113 #ifndef OPENSSL_NO_HEARTBEATS
2114 else if (type == TLSEXT_TYPE_heartbeat)
2118 case 0x01: /* Client allows us to send HB requests */
2119 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2121 case 0x02: /* Client doesn't accept HB requests */
2122 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2123 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2125 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2130 #ifndef OPENSSL_NO_NEXTPROTONEG
2131 else if (type == TLSEXT_TYPE_next_proto_neg &&
2132 s->s3->tmp.finish_md_len == 0)
2134 /* We shouldn't accept this extension on a
2137 * s->new_session will be set on renegotiation, but we
2138 * probably shouldn't rely that it couldn't be set on
2139 * the initial renegotation too in certain cases (when
2140 * there's some other reason to disallow resuming an
2141 * earlier session -- the current code won't be doing
2142 * anything like that, but this might change).
2144 * A valid sign that there's been a previous handshake
2145 * in this connection is if s->s3->tmp.finish_md_len >
2146 * 0. (We are talking about a check that will happen
2147 * in the Hello protocol round, well before a new
2148 * Finished message could have been computed.) */
2149 s->s3->next_proto_neg_seen = 1;
2153 /* session ticket processed earlier */
2154 else if (type == TLSEXT_TYPE_use_srtp)
2156 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2161 else if (type == TLSEXT_TYPE_server_authz)
2163 unsigned char *sdata = data;
2164 unsigned char server_authz_dataformatlist_length;
2168 *al = TLS1_AD_DECODE_ERROR;
2172 server_authz_dataformatlist_length = *(sdata++);
2174 if (server_authz_dataformatlist_length != size - 1)
2176 *al = TLS1_AD_DECODE_ERROR;
2180 /* Successful session resumption uses the same authz
2181 * information as the original session so we ignore this
2182 * in the case of a session resumption. */
2185 if (s->s3->tlsext_authz_client_types != NULL)
2186 OPENSSL_free(s->s3->tlsext_authz_client_types);
2187 s->s3->tlsext_authz_client_types =
2188 OPENSSL_malloc(server_authz_dataformatlist_length);
2189 if (!s->s3->tlsext_authz_client_types)
2191 *al = TLS1_AD_INTERNAL_ERROR;
2195 s->s3->tlsext_authz_client_types_len =
2196 server_authz_dataformatlist_length;
2197 memcpy(s->s3->tlsext_authz_client_types,
2199 server_authz_dataformatlist_length);
2201 /* Sort the types in order to check for duplicates. */
2202 qsort(s->s3->tlsext_authz_client_types,
2203 server_authz_dataformatlist_length,
2204 1 /* element size */,
2207 for (i = 0; i < server_authz_dataformatlist_length; i++)
2210 s->s3->tlsext_authz_client_types[i] ==
2211 s->s3->tlsext_authz_client_types[i-1])
2213 *al = TLS1_AD_DECODE_ERROR;
2227 /* Need RI if renegotiating */
2229 if (!renegotiate_seen && s->renegotiate &&
2230 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2232 *al = SSL_AD_HANDSHAKE_FAILURE;
2233 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2234 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2237 /* If no signature algorithms extension set default values */
2238 if (!s->cert->peer_sigalgs)
2239 ssl_cert_set_default_md(s->cert);
2244 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2247 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2249 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2253 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2255 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2261 #ifndef OPENSSL_NO_NEXTPROTONEG
2262 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2263 * elements of zero length are allowed and the set of elements must exactly fill
2264 * the length of the block. */
2265 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2267 unsigned int off = 0;
2281 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2283 unsigned short length;
2284 unsigned short type;
2285 unsigned short size;
2286 unsigned char *data = *p;
2287 int tlsext_servername = 0;
2288 int renegotiate_seen = 0;
2290 #ifndef OPENSSL_NO_NEXTPROTONEG
2291 s->s3->next_proto_neg_seen = 0;
2294 #ifndef OPENSSL_NO_HEARTBEATS
2295 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2296 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2299 if (data >= (d+n-2))
2303 if (data+length != d+n)
2305 *al = SSL_AD_DECODE_ERROR;
2309 while(data <= (d+n-4))
2314 if (data+size > (d+n))
2317 if (s->tlsext_debug_cb)
2318 s->tlsext_debug_cb(s, 1, type, data, size,
2319 s->tlsext_debug_arg);
2321 if (type == TLSEXT_TYPE_server_name)
2323 if (s->tlsext_hostname == NULL || size > 0)
2325 *al = TLS1_AD_UNRECOGNIZED_NAME;
2328 tlsext_servername = 1;
2331 #ifndef OPENSSL_NO_EC
2332 else if (type == TLSEXT_TYPE_ec_point_formats &&
2333 s->version != DTLS1_VERSION)
2335 unsigned char *sdata = data;
2336 int ecpointformatlist_length = *(sdata++);
2338 if (ecpointformatlist_length != size - 1)
2340 *al = TLS1_AD_DECODE_ERROR;
2343 s->session->tlsext_ecpointformatlist_length = 0;
2344 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2345 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2347 *al = TLS1_AD_INTERNAL_ERROR;
2350 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2351 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2353 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2354 sdata = s->session->tlsext_ecpointformatlist;
2355 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2356 fprintf(stderr,"%i ",*(sdata++));
2357 fprintf(stderr,"\n");
2360 #endif /* OPENSSL_NO_EC */
2362 else if (type == TLSEXT_TYPE_session_ticket)
2364 if (s->tls_session_ticket_ext_cb &&
2365 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2367 *al = TLS1_AD_INTERNAL_ERROR;
2370 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2373 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2376 s->tlsext_ticket_expected = 1;
2378 #ifdef TLSEXT_TYPE_opaque_prf_input
2379 else if (type == TLSEXT_TYPE_opaque_prf_input &&
2380 s->version != DTLS1_VERSION)
2382 unsigned char *sdata = data;
2386 *al = SSL_AD_DECODE_ERROR;
2389 n2s(sdata, s->s3->server_opaque_prf_input_len);
2390 if (s->s3->server_opaque_prf_input_len != size - 2)
2392 *al = SSL_AD_DECODE_ERROR;
2396 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2397 OPENSSL_free(s->s3->server_opaque_prf_input);
2398 if (s->s3->server_opaque_prf_input_len == 0)
2399 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2401 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2403 if (s->s3->server_opaque_prf_input == NULL)
2405 *al = TLS1_AD_INTERNAL_ERROR;
2410 else if (type == TLSEXT_TYPE_status_request &&
2411 s->version != DTLS1_VERSION)
2413 /* MUST be empty and only sent if we've requested
2414 * a status request message.
2416 if ((s->tlsext_status_type == -1) || (size > 0))
2418 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2421 /* Set flag to expect CertificateStatus message */
2422 s->tlsext_status_expected = 1;
2424 #ifndef OPENSSL_NO_NEXTPROTONEG
2425 else if (type == TLSEXT_TYPE_next_proto_neg &&
2426 s->s3->tmp.finish_md_len == 0)
2428 unsigned char *selected;
2429 unsigned char selected_len;
2431 /* We must have requested it. */
2432 if ((s->ctx->next_proto_select_cb == NULL))
2434 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2437 /* The data must be valid */
2438 if (!ssl_next_proto_validate(data, size))
2440 *al = TLS1_AD_DECODE_ERROR;
2443 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2445 *al = TLS1_AD_INTERNAL_ERROR;
2448 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2449 if (!s->next_proto_negotiated)
2451 *al = TLS1_AD_INTERNAL_ERROR;
2454 memcpy(s->next_proto_negotiated, selected, selected_len);
2455 s->next_proto_negotiated_len = selected_len;
2456 s->s3->next_proto_neg_seen = 1;
2459 else if (type == TLSEXT_TYPE_renegotiate)
2461 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2463 renegotiate_seen = 1;
2465 #ifndef OPENSSL_NO_HEARTBEATS
2466 else if (type == TLSEXT_TYPE_heartbeat)
2470 case 0x01: /* Server allows us to send HB requests */
2471 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2473 case 0x02: /* Server doesn't accept HB requests */
2474 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2475 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2477 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2482 else if (type == TLSEXT_TYPE_use_srtp)
2484 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2489 else if (type == TLSEXT_TYPE_server_authz)
2491 /* We only support audit proofs. It's an error to send
2492 * an authz hello extension if the client
2493 * didn't request a proof. */
2494 unsigned char *sdata = data;
2495 unsigned char server_authz_dataformatlist_length;
2497 if (!s->ctx->tlsext_authz_server_audit_proof_cb)
2499 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2505 *al = TLS1_AD_DECODE_ERROR;
2509 server_authz_dataformatlist_length = *(sdata++);
2510 if (server_authz_dataformatlist_length != size - 1)
2512 *al = TLS1_AD_DECODE_ERROR;
2516 /* We only support audit proofs, so a legal ServerHello
2517 * authz list contains exactly one entry. */
2518 if (server_authz_dataformatlist_length != 1 ||
2519 sdata[0] != TLSEXT_AUTHZDATAFORMAT_audit_proof)
2521 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2525 s->s3->tlsext_authz_server_promised = 1;
2533 *al = SSL_AD_DECODE_ERROR;
2537 if (!s->hit && tlsext_servername == 1)
2539 if (s->tlsext_hostname)
2541 if (s->session->tlsext_hostname == NULL)
2543 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2544 if (!s->session->tlsext_hostname)
2546 *al = SSL_AD_UNRECOGNIZED_NAME;
2552 *al = SSL_AD_DECODE_ERROR;
2562 /* Determine if we need to see RI. Strictly speaking if we want to
2563 * avoid an attack we should *always* see RI even on initial server
2564 * hello because the client doesn't see any renegotiation during an
2565 * attack. However this would mean we could not connect to any server
2566 * which doesn't support RI so for the immediate future tolerate RI
2567 * absence on initial connect only.
2569 if (!renegotiate_seen
2570 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2571 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2573 *al = SSL_AD_HANDSHAKE_FAILURE;
2574 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2575 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2583 int ssl_prepare_clienthello_tlsext(SSL *s)
2586 #ifdef TLSEXT_TYPE_opaque_prf_input
2590 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2592 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2597 if (s->tlsext_opaque_prf_input != NULL)
2599 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2600 OPENSSL_free(s->s3->client_opaque_prf_input);
2602 if (s->tlsext_opaque_prf_input_len == 0)
2603 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2605 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2606 if (s->s3->client_opaque_prf_input == NULL)
2608 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2611 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2615 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2616 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2623 int ssl_prepare_serverhello_tlsext(SSL *s)
2625 #ifndef OPENSSL_NO_EC
2626 /* If we are server and using an ECC cipher suite, send the point formats we support
2627 * if the client sent us an ECPointsFormat extension. Note that the server is not
2628 * supposed to send an EllipticCurves extension.
2631 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2632 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2633 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
2634 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
2638 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
2639 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
2641 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2644 s->tlsext_ecpointformatlist_length = 3;
2645 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
2646 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
2647 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
2649 #endif /* OPENSSL_NO_EC */
2654 static int ssl_check_clienthello_tlsext_early(SSL *s)
2656 int ret=SSL_TLSEXT_ERR_NOACK;
2657 int al = SSL_AD_UNRECOGNIZED_NAME;
2659 #ifndef OPENSSL_NO_EC
2660 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2661 * ssl3_choose_cipher in s3_lib.c.
2663 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2664 * ssl3_choose_cipher in s3_lib.c.
2668 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2669 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2670 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2671 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2673 #ifdef TLSEXT_TYPE_opaque_prf_input
2675 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2676 * but we might be sending an alert in response to the client hello,
2677 * so this has to happen here in
2678 * ssl_check_clienthello_tlsext_early(). */
2682 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2684 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2687 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2688 al = SSL_AD_INTERNAL_ERROR;
2693 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2694 OPENSSL_free(s->s3->server_opaque_prf_input);
2695 s->s3->server_opaque_prf_input = NULL;
2697 if (s->tlsext_opaque_prf_input != NULL)
2699 if (s->s3->client_opaque_prf_input != NULL &&
2700 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2702 /* can only use this extension if we have a server opaque PRF input
2703 * of the same length as the client opaque PRF input! */
2705 if (s->tlsext_opaque_prf_input_len == 0)
2706 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2708 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2709 if (s->s3->server_opaque_prf_input == NULL)
2711 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2712 al = SSL_AD_INTERNAL_ERROR;
2715 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2719 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2721 /* The callback wants to enforce use of the extension,
2722 * but we can't do that with the client opaque PRF input;
2723 * abort the handshake.
2725 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2726 al = SSL_AD_HANDSHAKE_FAILURE;
2734 case SSL_TLSEXT_ERR_ALERT_FATAL:
2735 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2738 case SSL_TLSEXT_ERR_ALERT_WARNING:
2739 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2742 case SSL_TLSEXT_ERR_NOACK:
2743 s->servername_done=0;
2749 int ssl_check_clienthello_tlsext_late(SSL *s)
2751 int ret = SSL_TLSEXT_ERR_OK;
2754 /* If status request then ask callback what to do.
2755 * Note: this must be called after servername callbacks in case
2756 * the certificate has changed, and must be called after the cipher
2757 * has been chosen because this may influence which certificate is sent
2759 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2762 CERT_PKEY *certpkey;
2763 certpkey = ssl_get_server_send_pkey(s);
2764 /* If no certificate can't return certificate status */
2765 if (certpkey == NULL)
2767 s->tlsext_status_expected = 0;
2770 /* Set current certificate to one we will use so
2771 * SSL_get_certificate et al can pick it up.
2773 s->cert->key = certpkey;
2774 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2777 /* We don't want to send a status request response */
2778 case SSL_TLSEXT_ERR_NOACK:
2779 s->tlsext_status_expected = 0;
2781 /* status request response should be sent */
2782 case SSL_TLSEXT_ERR_OK:
2783 if (s->tlsext_ocsp_resp)
2784 s->tlsext_status_expected = 1;
2786 s->tlsext_status_expected = 0;
2788 /* something bad happened */
2789 case SSL_TLSEXT_ERR_ALERT_FATAL:
2790 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2791 al = SSL_AD_INTERNAL_ERROR;
2796 s->tlsext_status_expected = 0;
2801 case SSL_TLSEXT_ERR_ALERT_FATAL:
2802 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2805 case SSL_TLSEXT_ERR_ALERT_WARNING:
2806 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2814 int ssl_check_serverhello_tlsext(SSL *s)
2816 int ret=SSL_TLSEXT_ERR_NOACK;
2817 int al = SSL_AD_UNRECOGNIZED_NAME;
2819 #ifndef OPENSSL_NO_EC
2820 /* If we are client and using an elliptic curve cryptography cipher
2821 * suite, then if server returns an EC point formats lists extension
2822 * it must contain uncompressed.
2824 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2825 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2826 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2827 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
2828 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
2830 /* we are using an ECC cipher */
2832 unsigned char *list;
2833 int found_uncompressed = 0;
2834 list = s->session->tlsext_ecpointformatlist;
2835 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2837 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
2839 found_uncompressed = 1;
2843 if (!found_uncompressed)
2845 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2849 ret = SSL_TLSEXT_ERR_OK;
2850 #endif /* OPENSSL_NO_EC */
2852 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2853 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2854 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2855 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2857 #ifdef TLSEXT_TYPE_opaque_prf_input
2858 if (s->s3->server_opaque_prf_input_len > 0)
2860 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
2861 * So first verify that we really have a value from the server too. */
2863 if (s->s3->server_opaque_prf_input == NULL)
2865 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2866 al = SSL_AD_HANDSHAKE_FAILURE;
2869 /* Anytime the server *has* sent an opaque PRF input, we need to check
2870 * that we have a client opaque PRF input of the same size. */
2871 if (s->s3->client_opaque_prf_input == NULL ||
2872 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
2874 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2875 al = SSL_AD_ILLEGAL_PARAMETER;
2880 /* If we've requested certificate status and we wont get one
2883 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2884 && s->ctx && s->ctx->tlsext_status_cb)
2887 /* Set resp to NULL, resplen to -1 so callback knows
2888 * there is no response.
2890 if (s->tlsext_ocsp_resp)
2892 OPENSSL_free(s->tlsext_ocsp_resp);
2893 s->tlsext_ocsp_resp = NULL;
2895 s->tlsext_ocsp_resplen = -1;
2896 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2899 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2900 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2904 al = SSL_AD_INTERNAL_ERROR;
2905 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2911 case SSL_TLSEXT_ERR_ALERT_FATAL:
2912 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2915 case SSL_TLSEXT_ERR_ALERT_WARNING:
2916 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2919 case SSL_TLSEXT_ERR_NOACK:
2920 s->servername_done=0;
2926 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2929 if (s->version < SSL3_VERSION)
2931 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0)
2933 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2937 if (ssl_check_serverhello_tlsext(s) <= 0)
2939 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,SSL_R_SERVERHELLO_TLSEXT);
2945 /* Since the server cache lookup is done early on in the processing of the
2946 * ClientHello, and other operations depend on the result, we need to handle
2947 * any TLS session ticket extension at the same time.
2949 * session_id: points at the session ID in the ClientHello. This code will
2950 * read past the end of this in order to parse out the session ticket
2951 * extension, if any.
2952 * len: the length of the session ID.
2953 * limit: a pointer to the first byte after the ClientHello.
2954 * ret: (output) on return, if a ticket was decrypted, then this is set to
2955 * point to the resulting session.
2957 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2958 * ciphersuite, in which case we have no use for session tickets and one will
2959 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2962 * -1: fatal error, either from parsing or decrypting the ticket.
2963 * 0: no ticket was found (or was ignored, based on settings).
2964 * 1: a zero length extension was found, indicating that the client supports
2965 * session tickets but doesn't currently have one to offer.
2966 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2967 * couldn't be decrypted because of a non-fatal error.
2968 * 3: a ticket was successfully decrypted and *ret was set.
2971 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2972 * a new session ticket to the client because the client indicated support
2973 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2974 * a session ticket or we couldn't use the one it gave us, or if
2975 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2976 * Otherwise, s->tlsext_ticket_expected is set to 0.
2978 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
2979 const unsigned char *limit, SSL_SESSION **ret)
2981 /* Point after session ID in client hello */
2982 const unsigned char *p = session_id + len;
2986 s->tlsext_ticket_expected = 0;
2988 /* If tickets disabled behave as if no ticket present
2989 * to permit stateful resumption.
2991 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2993 if ((s->version <= SSL3_VERSION) || !limit)
2997 /* Skip past DTLS cookie */
2998 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
3005 /* Skip past cipher list */
3010 /* Skip past compression algorithm list */
3015 /* Now at start of extensions */
3016 if ((p + 2) >= limit)
3019 while ((p + 4) <= limit)
3021 unsigned short type, size;
3024 if (p + size > limit)
3026 if (type == TLSEXT_TYPE_session_ticket)
3031 /* The client will accept a ticket but doesn't
3032 * currently have one. */
3033 s->tlsext_ticket_expected = 1;
3036 if (s->tls_session_secret_cb)
3038 /* Indicate that the ticket couldn't be
3039 * decrypted rather than generating the session
3040 * from ticket now, trigger abbreviated
3041 * handshake based on external mechanism to
3042 * calculate the master secret later. */