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 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
630 /* If we have a custom point format list use it otherwise
632 if (s->tlsext_ecpointformatlist)
634 *pformats = s->tlsext_ecpointformatlist;
635 *pformatslen = s->tlsext_ecpointformatlist_length;
639 *pformats = ecformats_default;
640 /* For Suite B we don't support char2 fields */
642 *pformatslen = sizeof(ecformats_default) - 1;
644 *pformatslen = sizeof(ecformats_default);
648 /* Check cert parameters compatible with extensions: currently just checks
649 * EC certificates have compatible curves and compression.
651 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
653 unsigned char comp_id, curve_id[2];
656 pkey = X509_get_pubkey(x);
659 /* If not EC nothing to do */
660 if (pkey->type != EVP_PKEY_EC)
665 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
669 /* Can't check curve_id for client certs as we don't have a
670 * supported curves extension.
672 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
675 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
676 * SHA384+P-384, adjust digest if necessary.
678 if (set_ee_md && tls1_suiteb(s))
685 /* Check to see we have necessary signing algorithm */
686 if (curve_id[1] == TLSEXT_curve_P_256)
687 check_md = NID_ecdsa_with_SHA256;
688 else if (curve_id[1] == TLSEXT_curve_P_384)
689 check_md = NID_ecdsa_with_SHA384;
691 return 0; /* Should never happen */
692 for (i = 0; i < c->shared_sigalgslen; i++)
693 if (check_md == c->shared_sigalgs[i].signandhash_nid)
695 if (i == c->shared_sigalgslen)
699 if (check_md == NID_ecdsa_with_SHA256)
700 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
702 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
707 /* Check EC temporary key is compatible with client extensions */
708 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
710 unsigned char curve_id[2];
711 EC_KEY *ec = s->cert->ecdh_tmp;
712 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
713 /* Allow any curve: not just those peer supports */
714 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
717 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
718 * no other curves permitted.
722 /* Curve to check determined by ciphersuite */
723 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
724 curve_id[1] = TLSEXT_curve_P_256;
725 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
726 curve_id[1] = TLSEXT_curve_P_384;
730 /* Check this curve is acceptable */
731 if (!tls1_check_ec_key(s, curve_id, NULL))
733 /* If auto or setting curve from callback assume OK */
734 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
736 /* Otherwise check curve is acceptable */
739 unsigned char curve_tmp[2];
742 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
744 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
750 if (s->cert->ecdh_tmp_auto)
752 /* Need a shared curve */
753 if (tls1_shared_curve(s, 0))
759 if (s->cert->ecdh_tmp_cb)
764 if (!tls1_set_ec_id(curve_id, NULL, ec))
766 /* Set this to allow use of invalid curves for testing */
770 return tls1_check_ec_key(s, curve_id, NULL);
776 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
781 #endif /* OPENSSL_NO_EC */
783 #ifndef OPENSSL_NO_TLSEXT
785 /* List of supported signature algorithms and hashes. Should make this
786 * customisable at some point, for now include everything we support.
789 #ifdef OPENSSL_NO_RSA
790 #define tlsext_sigalg_rsa(md) /* */
792 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
795 #ifdef OPENSSL_NO_DSA
796 #define tlsext_sigalg_dsa(md) /* */
798 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
801 #ifdef OPENSSL_NO_ECDSA
802 #define tlsext_sigalg_ecdsa(md) /* */
804 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
807 #define tlsext_sigalg(md) \
808 tlsext_sigalg_rsa(md) \
809 tlsext_sigalg_dsa(md) \
810 tlsext_sigalg_ecdsa(md)
812 static unsigned char tls12_sigalgs[] = {
813 #ifndef OPENSSL_NO_SHA512
814 tlsext_sigalg(TLSEXT_hash_sha512)
815 tlsext_sigalg(TLSEXT_hash_sha384)
817 #ifndef OPENSSL_NO_SHA256
818 tlsext_sigalg(TLSEXT_hash_sha256)
819 tlsext_sigalg(TLSEXT_hash_sha224)
821 #ifndef OPENSSL_NO_SHA
822 tlsext_sigalg(TLSEXT_hash_sha1)
824 #ifndef OPENSSL_NO_MD5
825 tlsext_sigalg_rsa(TLSEXT_hash_md5)
828 #ifndef OPENSSL_NO_ECDSA
829 static unsigned char suiteb_sigalgs[] = {
830 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
831 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
834 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
836 /* If Suite B mode use Suite B sigalgs only, ignore any other
839 #ifndef OPENSSL_NO_EC
840 switch (tls1_suiteb(s))
842 case SSL_CERT_FLAG_SUITEB_128_LOS:
843 *psigs = suiteb_sigalgs;
844 return sizeof(suiteb_sigalgs);
846 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
847 *psigs = suiteb_sigalgs;
850 case SSL_CERT_FLAG_SUITEB_192_LOS:
851 *psigs = suiteb_sigalgs + 2;
855 /* If server use client authentication sigalgs if not NULL */
856 if (s->server && s->cert->client_sigalgs)
858 *psigs = s->cert->client_sigalgs;
859 return s->cert->client_sigalgslen;
861 else if (s->cert->conf_sigalgs)
863 *psigs = s->cert->conf_sigalgs;
864 return s->cert->conf_sigalgslen;
868 *psigs = tls12_sigalgs;
870 /* If FIPS mode don't include MD5 which is last */
872 return sizeof(tls12_sigalgs) - 2;
875 return sizeof(tls12_sigalgs);
878 /* Check signature algorithm is consistent with sent supported signature
879 * algorithms and if so return relevant digest.
881 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
882 const unsigned char *sig, EVP_PKEY *pkey)
884 const unsigned char *sent_sigs;
885 size_t sent_sigslen, i;
886 int sigalg = tls12_get_sigid(pkey);
887 /* Should never happen */
890 /* Check key type is consistent with signature */
891 if (sigalg != (int)sig[1])
893 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
896 #ifndef OPENSSL_NO_EC
897 if (pkey->type == EVP_PKEY_EC)
899 unsigned char curve_id[2], comp_id;
900 /* Check compression and curve matches extensions */
901 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
903 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
905 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
908 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
913 if (curve_id[1] == TLSEXT_curve_P_256)
915 if (sig[0] != TLSEXT_hash_sha256)
917 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
918 SSL_R_ILLEGAL_SUITEB_DIGEST);
922 else if (curve_id[1] == TLSEXT_curve_P_384)
924 if (sig[0] != TLSEXT_hash_sha384)
926 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
927 SSL_R_ILLEGAL_SUITEB_DIGEST);
935 else if (tls1_suiteb(s))
939 /* Check signature matches a type we sent */
940 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
941 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
943 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
946 /* Allow fallback to SHA1 if not strict mode */
947 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
949 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
952 *pmd = tls12_get_hash(sig[0]);
955 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
958 /* Store the digest used so applications can retrieve it if they
961 if (s->session && s->session->sess_cert)
962 s->session->sess_cert->peer_key->digest = *pmd;
965 /* Get a mask of disabled algorithms: an algorithm is disabled
966 * if it isn't supported or doesn't appear in supported signature
967 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
968 * session and not global settings.
971 void ssl_set_client_disabled(SSL *s)
974 const unsigned char *sigalgs;
975 size_t i, sigalgslen;
976 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
979 /* If less than TLS 1.2 don't allow TLS 1.2 only ciphers */
980 if (TLS1_get_client_version(s) < TLS1_2_VERSION)
981 c->mask_ssl = SSL_TLSV1_2;
984 /* Now go through all signature algorithms seeing if we support
985 * any for RSA, DSA, ECDSA. Do this for all versions not just
988 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
989 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
993 #ifndef OPENSSL_NO_RSA
994 case TLSEXT_signature_rsa:
998 #ifndef OPENSSL_NO_DSA
999 case TLSEXT_signature_dsa:
1003 #ifndef OPENSSL_NO_ECDSA
1004 case TLSEXT_signature_ecdsa:
1010 /* Disable auth and static DH if we don't include any appropriate
1011 * signature algorithms.
1015 c->mask_a |= SSL_aRSA;
1016 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1020 c->mask_a |= SSL_aDSS;
1021 c->mask_k |= SSL_kDHd;
1025 c->mask_a |= SSL_aECDSA;
1026 c->mask_k |= SSL_kECDHe;
1028 #ifndef OPENSSL_NO_KRB5
1029 if (!kssl_tgt_is_available(s->kssl_ctx))
1031 c->mask_a |= SSL_aKRB5;
1032 c->mask_k |= SSL_kKRB5;
1035 #ifndef OPENSSL_NO_PSK
1036 /* with PSK there must be client callback set */
1037 if (!s->psk_client_callback)
1039 c->mask_a |= SSL_aPSK;
1040 c->mask_k |= SSL_kPSK;
1042 #endif /* OPENSSL_NO_PSK */
1046 /* byte_compare is a compare function for qsort(3) that compares bytes. */
1047 static int byte_compare(const void *in_a, const void *in_b)
1049 unsigned char a = *((const unsigned char*) in_a);
1050 unsigned char b = *((const unsigned char*) in_b);
1059 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1062 unsigned char *ret = p;
1063 #ifndef OPENSSL_NO_EC
1064 /* See if we support any ECC ciphersuites */
1066 if (s->version != DTLS1_VERSION && s->version >= TLS1_VERSION)
1069 unsigned long alg_k, alg_a;
1070 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1072 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1074 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1076 alg_k = c->algorithm_mkey;
1077 alg_a = c->algorithm_auth;
1078 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
1079 || (alg_a & SSL_aECDSA)))
1088 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1089 if (s->client_version == SSL3_VERSION
1090 && !s->s3->send_connection_binding)
1095 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1097 if (s->tlsext_hostname != NULL)
1099 /* Add TLS extension servername to the Client Hello message */
1100 unsigned long size_str;
1103 /* check for enough space.
1104 4 for the servername type and entension length
1105 2 for servernamelist length
1106 1 for the hostname type
1107 2 for hostname length
1111 if ((lenmax = limit - ret - 9) < 0
1112 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1115 /* extension type and length */
1116 s2n(TLSEXT_TYPE_server_name,ret);
1117 s2n(size_str+5,ret);
1119 /* length of servername list */
1120 s2n(size_str+3,ret);
1122 /* hostname type, length and hostname */
1123 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1125 memcpy(ret, s->tlsext_hostname, size_str);
1129 /* Add RI if renegotiating */
1134 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1136 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1140 if((limit - p - 4 - el) < 0) return NULL;
1142 s2n(TLSEXT_TYPE_renegotiate,ret);
1145 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1147 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1154 #ifndef OPENSSL_NO_SRP
1155 /* Add SRP username if there is one */
1156 if (s->srp_ctx.login != NULL)
1157 { /* Add TLS extension SRP username to the Client Hello message */
1159 int login_len = strlen(s->srp_ctx.login);
1160 if (login_len > 255 || login_len == 0)
1162 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1166 /* check for enough space.
1167 4 for the srp type type and entension length
1168 1 for the srp user identity
1169 + srp user identity length
1171 if ((limit - ret - 5 - login_len) < 0) return NULL;
1173 /* fill in the extension */
1174 s2n(TLSEXT_TYPE_srp,ret);
1175 s2n(login_len+1,ret);
1176 (*ret++) = (unsigned char) login_len;
1177 memcpy(ret, s->srp_ctx.login, login_len);
1182 #ifndef OPENSSL_NO_EC
1185 /* Add TLS extension ECPointFormats to the ClientHello message */
1187 const unsigned char *plist;
1190 tls1_get_formatlist(s, &plist, &plistlen);
1192 if ((lenmax = limit - ret - 5) < 0) return NULL;
1193 if (plistlen > (size_t)lenmax) return NULL;
1196 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1200 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1201 s2n(plistlen + 1,ret);
1202 *(ret++) = (unsigned char)plistlen ;
1203 memcpy(ret, plist, plistlen);
1206 /* Add TLS extension EllipticCurves to the ClientHello message */
1207 plist = s->tlsext_ellipticcurvelist;
1208 tls1_get_curvelist(s, 0, &plist, &plistlen);
1210 if ((lenmax = limit - ret - 6) < 0) return NULL;
1211 if (plistlen > (size_t)lenmax) return NULL;
1212 if (plistlen > 65532)
1214 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1218 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1219 s2n(plistlen + 2, ret);
1221 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
1222 * elliptic_curve_list, but the examples use two bytes.
1223 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
1224 * resolves this to two bytes.
1227 memcpy(ret, plist, plistlen);
1230 #endif /* OPENSSL_NO_EC */
1232 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1235 if (!s->new_session && s->session && s->session->tlsext_tick)
1236 ticklen = s->session->tlsext_ticklen;
1237 else if (s->session && s->tlsext_session_ticket &&
1238 s->tlsext_session_ticket->data)
1240 ticklen = s->tlsext_session_ticket->length;
1241 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1242 if (!s->session->tlsext_tick)
1244 memcpy(s->session->tlsext_tick,
1245 s->tlsext_session_ticket->data,
1247 s->session->tlsext_ticklen = ticklen;
1251 if (ticklen == 0 && s->tlsext_session_ticket &&
1252 s->tlsext_session_ticket->data == NULL)
1254 /* Check for enough room 2 for extension type, 2 for len
1257 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1258 s2n(TLSEXT_TYPE_session_ticket,ret);
1262 memcpy(ret, s->session->tlsext_tick, ticklen);
1268 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1271 const unsigned char *salg;
1272 salglen = tls12_get_psigalgs(s, &salg);
1273 if ((size_t)(limit - ret) < salglen + 6)
1275 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1276 s2n(salglen + 2, ret);
1278 memcpy(ret, salg, salglen);
1282 #ifdef TLSEXT_TYPE_opaque_prf_input
1283 if (s->s3->client_opaque_prf_input != NULL &&
1284 s->version != DTLS1_VERSION)
1286 size_t col = s->s3->client_opaque_prf_input_len;
1288 if ((long)(limit - ret - 6 - col < 0))
1290 if (col > 0xFFFD) /* can't happen */
1293 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1296 memcpy(ret, s->s3->client_opaque_prf_input, col);
1301 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
1302 s->version != DTLS1_VERSION)
1305 long extlen, idlen, itmp;
1309 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1311 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1312 itmp = i2d_OCSP_RESPID(id, NULL);
1318 if (s->tlsext_ocsp_exts)
1320 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1327 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1328 s2n(TLSEXT_TYPE_status_request, ret);
1329 if (extlen + idlen > 0xFFF0)
1331 s2n(extlen + idlen + 5, ret);
1332 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1334 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1336 /* save position of id len */
1337 unsigned char *q = ret;
1338 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1339 /* skip over id len */
1341 itmp = i2d_OCSP_RESPID(id, &ret);
1347 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1350 #ifndef OPENSSL_NO_HEARTBEATS
1351 /* Add Heartbeat extension */
1352 s2n(TLSEXT_TYPE_heartbeat,ret);
1355 * 1: peer may send requests
1356 * 2: peer not allowed to send requests
1358 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1359 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1361 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1364 #ifndef OPENSSL_NO_NEXTPROTONEG
1365 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1367 /* The client advertises an emtpy extension to indicate its
1368 * support for Next Protocol Negotiation */
1369 if (limit - ret - 4 < 0)
1371 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1376 if(SSL_get_srtp_profiles(s))
1380 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1382 if((limit - p - 4 - el) < 0) return NULL;
1384 s2n(TLSEXT_TYPE_use_srtp,ret);
1387 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1389 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1395 /* Add TLS extension Server_Authz_DataFormats to the ClientHello */
1396 /* 2 bytes for extension type */
1397 /* 2 bytes for extension length */
1398 /* 1 byte for the list length */
1399 /* 1 byte for the list (we only support audit proofs) */
1400 if (s->ctx->tlsext_authz_server_audit_proof_cb != NULL)
1402 const unsigned short ext_len = 2;
1403 const unsigned char list_len = 1;
1405 if (limit < ret + 6)
1408 s2n(TLSEXT_TYPE_server_authz, ret);
1409 /* Extension length: 2 bytes */
1411 *(ret++) = list_len;
1412 *(ret++) = TLSEXT_AUTHZDATAFORMAT_audit_proof;
1415 /* Add custom TLS Extensions to ClientHello */
1416 if (s->ctx->custom_cli_ext_records_count)
1419 custom_cli_ext_record* record;
1421 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
1423 const unsigned char* out = NULL;
1424 unsigned short outlen = 0;
1426 record = &s->ctx->custom_cli_ext_records[i];
1427 /* NULL callback sends empty extension */
1428 /* -1 from callback omits extension */
1432 cb_retval = record->fn1(s, record->ext_type,
1436 return NULL; /* error */
1437 if (cb_retval == -1)
1438 continue; /* skip this extension */
1440 if (limit < ret + 4 + outlen)
1442 s2n(record->ext_type, ret);
1444 memcpy(ret, out, outlen);
1449 if ((extdatalen = ret-p-2) == 0)
1456 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1459 unsigned char *ret = p;
1460 #ifndef OPENSSL_NO_NEXTPROTONEG
1461 int next_proto_neg_seen;
1463 #ifndef OPENSSL_NO_EC
1464 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1465 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1466 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1467 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1469 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1470 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1474 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1476 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1478 if ((long)(limit - ret - 4) < 0) return NULL;
1480 s2n(TLSEXT_TYPE_server_name,ret);
1484 if(s->s3->send_connection_binding)
1488 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1490 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1494 if((limit - p - 4 - el) < 0) return NULL;
1496 s2n(TLSEXT_TYPE_renegotiate,ret);
1499 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1501 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1508 #ifndef OPENSSL_NO_EC
1509 if (using_ecc && s->version != DTLS1_VERSION)
1511 const unsigned char *plist;
1513 /* Add TLS extension ECPointFormats to the ServerHello message */
1516 tls1_get_formatlist(s, &plist, &plistlen);
1518 if ((lenmax = limit - ret - 5) < 0) return NULL;
1519 if (plistlen > (size_t)lenmax) return NULL;
1522 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1526 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1527 s2n(plistlen + 1,ret);
1528 *(ret++) = (unsigned char) plistlen;
1529 memcpy(ret, plist, plistlen);
1533 /* Currently the server should not respond with a SupportedCurves extension */
1534 #endif /* OPENSSL_NO_EC */
1536 if (s->tlsext_ticket_expected
1537 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1539 if ((long)(limit - ret - 4) < 0) return NULL;
1540 s2n(TLSEXT_TYPE_session_ticket,ret);
1544 if (s->tlsext_status_expected)
1546 if ((long)(limit - ret - 4) < 0) return NULL;
1547 s2n(TLSEXT_TYPE_status_request,ret);
1551 #ifdef TLSEXT_TYPE_opaque_prf_input
1552 if (s->s3->server_opaque_prf_input != NULL &&
1553 s->version != DTLS1_VERSION)
1555 size_t sol = s->s3->server_opaque_prf_input_len;
1557 if ((long)(limit - ret - 6 - sol) < 0)
1559 if (sol > 0xFFFD) /* can't happen */
1562 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1565 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1574 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1576 if((limit - p - 4 - el) < 0) return NULL;
1578 s2n(TLSEXT_TYPE_use_srtp,ret);
1581 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1583 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1589 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1590 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1591 { const unsigned char cryptopro_ext[36] = {
1592 0xfd, 0xe8, /*65000*/
1593 0x00, 0x20, /*32 bytes length*/
1594 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1595 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1596 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1597 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1598 if (limit-ret<36) return NULL;
1599 memcpy(ret,cryptopro_ext,36);
1604 #ifndef OPENSSL_NO_HEARTBEATS
1605 /* Add Heartbeat extension if we've received one */
1606 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1608 s2n(TLSEXT_TYPE_heartbeat,ret);
1611 * 1: peer may send requests
1612 * 2: peer not allowed to send requests
1614 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1615 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1617 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1622 #ifndef OPENSSL_NO_NEXTPROTONEG
1623 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1624 s->s3->next_proto_neg_seen = 0;
1625 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1627 const unsigned char *npa;
1628 unsigned int npalen;
1631 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1632 if (r == SSL_TLSEXT_ERR_OK)
1634 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1635 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1637 memcpy(ret, npa, npalen);
1639 s->s3->next_proto_neg_seen = 1;
1644 /* If the client supports authz then see whether we have any to offer
1646 if (s->s3->tlsext_authz_client_types_len)
1648 size_t authz_length;
1649 /* By now we already know the new cipher, so we can look ahead
1650 * to see whether the cert we are going to send
1651 * has any authz data attached to it. */
1652 const unsigned char* authz = ssl_get_authz_data(s, &authz_length);
1653 const unsigned char* const orig_authz = authz;
1655 unsigned authz_count = 0;
1657 /* The authz data contains a number of the following structures:
1658 * uint8_t authz_type
1660 * uint8_t data[length]
1662 * First we walk over it to find the number of authz elements. */
1663 for (i = 0; i < authz_length; i++)
1665 unsigned short length;
1669 if (memchr(s->s3->tlsext_authz_client_types,
1671 s->s3->tlsext_authz_client_types_len) != NULL)
1675 /* n2s increments authz by 2 */
1683 /* Add TLS extension server_authz to the ServerHello message
1684 * 2 bytes for extension type
1685 * 2 bytes for extension length
1686 * 1 byte for the list length
1687 * n bytes for the list */
1688 const unsigned short ext_len = 1 + authz_count;
1690 if ((long)(limit - ret - 4 - ext_len) < 0) return NULL;
1691 s2n(TLSEXT_TYPE_server_authz, ret);
1693 *(ret++) = authz_count;
1694 s->s3->tlsext_authz_promised_to_client = 1;
1698 for (i = 0; i < authz_length; i++)
1700 unsigned short length;
1705 if (memchr(s->s3->tlsext_authz_client_types,
1707 s->s3->tlsext_authz_client_types_len) != NULL)
1710 /* n2s increments authz by 2 */
1717 /* If custom types were sent in ClientHello, add ServerHello responses */
1718 if (s->s3->tlsext_custom_types_count)
1722 for (i = 0; i < s->s3->tlsext_custom_types_count; i++)
1725 custom_srv_ext_record *record;
1727 for (j = 0; j < s->ctx->custom_srv_ext_records_count; j++)
1729 record = &s->ctx->custom_srv_ext_records[j];
1730 if (s->s3->tlsext_custom_types[i] == record->ext_type)
1732 const unsigned char *out = NULL;
1733 unsigned short outlen = 0;
1736 /* NULL callback or -1 omits extension */
1739 cb_retval = record->fn2(s, record->ext_type,
1743 return NULL; /* error */
1744 if (cb_retval == -1)
1745 break; /* skip this extension */
1746 if (limit < ret + 4 + outlen)
1748 s2n(record->ext_type, ret);
1750 memcpy(ret, out, outlen);
1758 if ((extdatalen = ret-p-2)== 0)
1765 #ifndef OPENSSL_NO_EC
1766 /* ssl_check_for_safari attempts to fingerprint Safari using OS X
1767 * SecureTransport using the TLS extension block in |d|, of length |n|.
1768 * Safari, since 10.6, sends exactly these extensions, in this order:
1773 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1774 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1775 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1776 * 10.8..10.8.3 (which don't work).
1778 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
1779 unsigned short type, size;
1780 static const unsigned char kSafariExtensionsBlock[] = {
1781 0x00, 0x0a, /* elliptic_curves extension */
1782 0x00, 0x08, /* 8 bytes */
1783 0x00, 0x06, /* 6 bytes of curve ids */
1784 0x00, 0x17, /* P-256 */
1785 0x00, 0x18, /* P-384 */
1786 0x00, 0x19, /* P-521 */
1788 0x00, 0x0b, /* ec_point_formats */
1789 0x00, 0x02, /* 2 bytes */
1790 0x01, /* 1 point format */
1791 0x00, /* uncompressed */
1794 /* The following is only present in TLS 1.2 */
1795 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1796 0x00, 0x0d, /* signature_algorithms */
1797 0x00, 0x0c, /* 12 bytes */
1798 0x00, 0x0a, /* 10 bytes */
1799 0x05, 0x01, /* SHA-384/RSA */
1800 0x04, 0x01, /* SHA-256/RSA */
1801 0x02, 0x01, /* SHA-1/RSA */
1802 0x04, 0x03, /* SHA-256/ECDSA */
1803 0x02, 0x03, /* SHA-1/ECDSA */
1806 if (data >= (d+n-2))
1815 if (type != TLSEXT_TYPE_server_name)
1818 if (data+size > d+n)
1822 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1824 const size_t len1 = sizeof(kSafariExtensionsBlock);
1825 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1827 if (data + len1 + len2 != d+n)
1829 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1831 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1836 const size_t len = sizeof(kSafariExtensionsBlock);
1838 if (data + len != d+n)
1840 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
1844 s->s3->is_probably_safari = 1;
1846 #endif /* !OPENSSL_NO_EC */
1848 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1850 unsigned short type;
1851 unsigned short size;
1853 unsigned char *data = *p;
1854 int renegotiate_seen = 0;
1857 s->servername_done = 0;
1858 s->tlsext_status_type = -1;
1859 #ifndef OPENSSL_NO_NEXTPROTONEG
1860 s->s3->next_proto_neg_seen = 0;
1863 #ifndef OPENSSL_NO_HEARTBEATS
1864 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1865 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1868 #ifndef OPENSSL_NO_EC
1869 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1870 ssl_check_for_safari(s, data, d, n);
1871 #endif /* !OPENSSL_NO_EC */
1873 /* Clear any signature algorithms extension received */
1874 if (s->cert->peer_sigalgs)
1876 OPENSSL_free(s->cert->peer_sigalgs);
1877 s->cert->peer_sigalgs = NULL;
1879 /* Clear any shared sigtnature algorithms */
1880 if (s->cert->shared_sigalgs)
1882 OPENSSL_free(s->cert->shared_sigalgs);
1883 s->cert->shared_sigalgs = NULL;
1885 /* Clear certificate digests and validity flags */
1886 for (i = 0; i < SSL_PKEY_NUM; i++)
1888 s->cert->pkeys[i].digest = NULL;
1889 s->cert->pkeys[i].valid_flags = 0;
1892 if (data >= (d+n-2))
1896 if (data > (d+n-len))
1899 while (data <= (d+n-4))
1904 if (data+size > (d+n))
1907 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1909 if (s->tlsext_debug_cb)
1910 s->tlsext_debug_cb(s, 0, type, data, size,
1911 s->tlsext_debug_arg);
1912 /* The servername extension is treated as follows:
1914 - Only the hostname type is supported with a maximum length of 255.
1915 - The servername is rejected if too long or if it contains zeros,
1916 in which case an fatal alert is generated.
1917 - The servername field is maintained together with the session cache.
1918 - When a session is resumed, the servername call back invoked in order
1919 to allow the application to position itself to the right context.
1920 - The servername is acknowledged if it is new for a session or when
1921 it is identical to a previously used for the same session.
1922 Applications can control the behaviour. They can at any time
1923 set a 'desirable' servername for a new SSL object. This can be the
1924 case for example with HTTPS when a Host: header field is received and
1925 a renegotiation is requested. In this case, a possible servername
1926 presented in the new client hello is only acknowledged if it matches
1927 the value of the Host: field.
1928 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1929 if they provide for changing an explicit servername context for the session,
1930 i.e. when the session has been established with a servername extension.
1931 - On session reconnect, the servername extension may be absent.
1935 if (type == TLSEXT_TYPE_server_name)
1937 unsigned char *sdata;
1943 *al = SSL_AD_DECODE_ERROR;
1950 *al = SSL_AD_DECODE_ERROR;
1957 servname_type = *(sdata++);
1963 *al = SSL_AD_DECODE_ERROR;
1966 if (s->servername_done == 0)
1967 switch (servname_type)
1969 case TLSEXT_NAMETYPE_host_name:
1972 if(s->session->tlsext_hostname)
1974 *al = SSL_AD_DECODE_ERROR;
1977 if (len > TLSEXT_MAXLEN_host_name)
1979 *al = TLS1_AD_UNRECOGNIZED_NAME;
1982 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
1984 *al = TLS1_AD_INTERNAL_ERROR;
1987 memcpy(s->session->tlsext_hostname, sdata, len);
1988 s->session->tlsext_hostname[len]='\0';
1989 if (strlen(s->session->tlsext_hostname) != len) {
1990 OPENSSL_free(s->session->tlsext_hostname);
1991 s->session->tlsext_hostname = NULL;
1992 *al = TLS1_AD_UNRECOGNIZED_NAME;
1995 s->servername_done = 1;
1999 s->servername_done = s->session->tlsext_hostname
2000 && strlen(s->session->tlsext_hostname) == len
2001 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2013 *al = SSL_AD_DECODE_ERROR;
2018 #ifndef OPENSSL_NO_SRP
2019 else if (type == TLSEXT_TYPE_srp)
2021 if (size <= 0 || ((len = data[0])) != (size -1))
2023 *al = SSL_AD_DECODE_ERROR;
2026 if (s->srp_ctx.login != NULL)
2028 *al = SSL_AD_DECODE_ERROR;
2031 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
2033 memcpy(s->srp_ctx.login, &data[1], len);
2034 s->srp_ctx.login[len]='\0';
2036 if (strlen(s->srp_ctx.login) != len)
2038 *al = SSL_AD_DECODE_ERROR;
2044 #ifndef OPENSSL_NO_EC
2045 else if (type == TLSEXT_TYPE_ec_point_formats &&
2046 s->version != DTLS1_VERSION)
2048 unsigned char *sdata = data;
2049 int ecpointformatlist_length = *(sdata++);
2051 if (ecpointformatlist_length != size - 1 ||
2052 ecpointformatlist_length < 1)
2054 *al = TLS1_AD_DECODE_ERROR;
2059 if(s->session->tlsext_ecpointformatlist)
2061 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2062 s->session->tlsext_ecpointformatlist = NULL;
2064 s->session->tlsext_ecpointformatlist_length = 0;
2065 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2067 *al = TLS1_AD_INTERNAL_ERROR;
2070 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2071 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2074 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2075 sdata = s->session->tlsext_ecpointformatlist;
2076 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2077 fprintf(stderr,"%i ",*(sdata++));
2078 fprintf(stderr,"\n");
2081 else if (type == TLSEXT_TYPE_elliptic_curves &&
2082 s->version != DTLS1_VERSION)
2084 unsigned char *sdata = data;
2085 int ellipticcurvelist_length = (*(sdata++) << 8);
2086 ellipticcurvelist_length += (*(sdata++));
2088 if (ellipticcurvelist_length != size - 2 ||
2089 ellipticcurvelist_length < 1)
2091 *al = TLS1_AD_DECODE_ERROR;
2096 if(s->session->tlsext_ellipticcurvelist)
2098 *al = TLS1_AD_DECODE_ERROR;
2101 s->session->tlsext_ellipticcurvelist_length = 0;
2102 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2104 *al = TLS1_AD_INTERNAL_ERROR;
2107 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2108 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2111 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2112 sdata = s->session->tlsext_ellipticcurvelist;
2113 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2114 fprintf(stderr,"%i ",*(sdata++));
2115 fprintf(stderr,"\n");
2118 #endif /* OPENSSL_NO_EC */
2119 #ifdef TLSEXT_TYPE_opaque_prf_input
2120 else if (type == TLSEXT_TYPE_opaque_prf_input &&
2121 s->version != DTLS1_VERSION)
2123 unsigned char *sdata = data;
2127 *al = SSL_AD_DECODE_ERROR;
2130 n2s(sdata, s->s3->client_opaque_prf_input_len);
2131 if (s->s3->client_opaque_prf_input_len != size - 2)
2133 *al = SSL_AD_DECODE_ERROR;
2137 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2138 OPENSSL_free(s->s3->client_opaque_prf_input);
2139 if (s->s3->client_opaque_prf_input_len == 0)
2140 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2142 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2143 if (s->s3->client_opaque_prf_input == NULL)
2145 *al = TLS1_AD_INTERNAL_ERROR;
2150 else if (type == TLSEXT_TYPE_session_ticket)
2152 if (s->tls_session_ticket_ext_cb &&
2153 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2155 *al = TLS1_AD_INTERNAL_ERROR;
2159 else if (type == TLSEXT_TYPE_renegotiate)
2161 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2163 renegotiate_seen = 1;
2165 else if (type == TLSEXT_TYPE_signature_algorithms)
2168 if (s->cert->peer_sigalgs || size < 2)
2170 *al = SSL_AD_DECODE_ERROR;
2175 if (dsize != size || dsize & 1 || !dsize)
2177 *al = SSL_AD_DECODE_ERROR;
2180 if (!tls1_process_sigalgs(s, data, dsize))
2182 *al = SSL_AD_DECODE_ERROR;
2185 /* If sigalgs received and no shared algorithms fatal
2188 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
2190 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2191 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2192 *al = SSL_AD_ILLEGAL_PARAMETER;
2196 else if (type == TLSEXT_TYPE_status_request &&
2197 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
2202 *al = SSL_AD_DECODE_ERROR;
2206 s->tlsext_status_type = *data++;
2208 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2210 const unsigned char *sdata;
2212 /* Read in responder_id_list */
2217 *al = SSL_AD_DECODE_ERROR;
2226 *al = SSL_AD_DECODE_ERROR;
2230 dsize -= 2 + idsize;
2234 *al = SSL_AD_DECODE_ERROR;
2239 id = d2i_OCSP_RESPID(NULL,
2243 *al = SSL_AD_DECODE_ERROR;
2248 OCSP_RESPID_free(id);
2249 *al = SSL_AD_DECODE_ERROR;
2252 if (!s->tlsext_ocsp_ids
2253 && !(s->tlsext_ocsp_ids =
2254 sk_OCSP_RESPID_new_null()))
2256 OCSP_RESPID_free(id);
2257 *al = SSL_AD_INTERNAL_ERROR;
2260 if (!sk_OCSP_RESPID_push(
2261 s->tlsext_ocsp_ids, id))
2263 OCSP_RESPID_free(id);
2264 *al = SSL_AD_INTERNAL_ERROR;
2269 /* Read in request_extensions */
2272 *al = SSL_AD_DECODE_ERROR;
2279 *al = SSL_AD_DECODE_ERROR;
2285 if (s->tlsext_ocsp_exts)
2287 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2288 X509_EXTENSION_free);
2291 s->tlsext_ocsp_exts =
2292 d2i_X509_EXTENSIONS(NULL,
2294 if (!s->tlsext_ocsp_exts
2295 || (data + dsize != sdata))
2297 *al = SSL_AD_DECODE_ERROR;
2302 /* We don't know what to do with any other type
2306 s->tlsext_status_type = -1;
2308 #ifndef OPENSSL_NO_HEARTBEATS
2309 else if (type == TLSEXT_TYPE_heartbeat)
2313 case 0x01: /* Client allows us to send HB requests */
2314 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2316 case 0x02: /* Client doesn't accept HB requests */
2317 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2318 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2320 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2325 #ifndef OPENSSL_NO_NEXTPROTONEG
2326 else if (type == TLSEXT_TYPE_next_proto_neg &&
2327 s->s3->tmp.finish_md_len == 0)
2329 /* We shouldn't accept this extension on a
2332 * s->new_session will be set on renegotiation, but we
2333 * probably shouldn't rely that it couldn't be set on
2334 * the initial renegotation too in certain cases (when
2335 * there's some other reason to disallow resuming an
2336 * earlier session -- the current code won't be doing
2337 * anything like that, but this might change).
2339 * A valid sign that there's been a previous handshake
2340 * in this connection is if s->s3->tmp.finish_md_len >
2341 * 0. (We are talking about a check that will happen
2342 * in the Hello protocol round, well before a new
2343 * Finished message could have been computed.) */
2344 s->s3->next_proto_neg_seen = 1;
2348 /* session ticket processed earlier */
2349 else if (type == TLSEXT_TYPE_use_srtp)
2351 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2356 else if (type == TLSEXT_TYPE_server_authz)
2358 unsigned char *sdata = data;
2359 unsigned char server_authz_dataformatlist_length;
2363 *al = TLS1_AD_DECODE_ERROR;
2367 server_authz_dataformatlist_length = *(sdata++);
2369 if (server_authz_dataformatlist_length != size - 1)
2371 *al = TLS1_AD_DECODE_ERROR;
2375 /* Successful session resumption uses the same authz
2376 * information as the original session so we ignore this
2377 * in the case of a session resumption. */
2380 if (s->s3->tlsext_authz_client_types != NULL)
2381 OPENSSL_free(s->s3->tlsext_authz_client_types);
2382 s->s3->tlsext_authz_client_types =
2383 OPENSSL_malloc(server_authz_dataformatlist_length);
2384 if (!s->s3->tlsext_authz_client_types)
2386 *al = TLS1_AD_INTERNAL_ERROR;
2390 s->s3->tlsext_authz_client_types_len =
2391 server_authz_dataformatlist_length;
2392 memcpy(s->s3->tlsext_authz_client_types,
2394 server_authz_dataformatlist_length);
2396 /* Sort the types in order to check for duplicates. */
2397 qsort(s->s3->tlsext_authz_client_types,
2398 server_authz_dataformatlist_length,
2399 1 /* element size */,
2402 for (i = 0; i < server_authz_dataformatlist_length; i++)
2405 s->s3->tlsext_authz_client_types[i] ==
2406 s->s3->tlsext_authz_client_types[i-1])
2408 *al = TLS1_AD_DECODE_ERROR;
2415 /* If this ClientHello extension was unhandled and this is
2416 * a nonresumed connection, check whether the extension is a
2417 * custom TLS Extension (has a custom_srv_ext_record), and if
2418 * so call the callback and record the extension number so that
2419 * an appropriate ServerHello may be later returned.
2421 else if (!s->hit && s->ctx->custom_srv_ext_records_count)
2423 custom_srv_ext_record *record;
2425 for (i=0; i < s->ctx->custom_srv_ext_records_count; i++)
2427 record = &s->ctx->custom_srv_ext_records[i];
2428 if (type == record->ext_type)
2432 /* Error on duplicate TLS Extensions */
2433 for (j = 0; j < s->s3->tlsext_custom_types_count; j++)
2435 if (type == s->s3->tlsext_custom_types[j])
2437 *al = TLS1_AD_DECODE_ERROR;
2442 /* NULL callback still notes the extension */
2443 if (record->fn1 && !record->fn1(s, type, data, size, al, record->arg))
2446 /* Add the (non-duplicated) entry */
2447 s->s3->tlsext_custom_types_count++;
2448 s->s3->tlsext_custom_types = OPENSSL_realloc(
2449 s->s3->tlsext_custom_types,
2450 s->s3->tlsext_custom_types_count * 2);
2451 if (s->s3->tlsext_custom_types == NULL)
2453 s->s3->tlsext_custom_types = 0;
2454 *al = TLS1_AD_INTERNAL_ERROR;
2457 s->s3->tlsext_custom_types[
2458 s->s3->tlsext_custom_types_count - 1] = type;
2470 /* Need RI if renegotiating */
2472 if (!renegotiate_seen && s->renegotiate &&
2473 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2475 *al = SSL_AD_HANDSHAKE_FAILURE;
2476 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2477 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2480 /* If no signature algorithms extension set default values */
2481 if (!s->cert->peer_sigalgs)
2482 ssl_cert_set_default_md(s->cert);
2487 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2490 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2492 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2496 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2498 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2504 #ifndef OPENSSL_NO_NEXTPROTONEG
2505 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2506 * elements of zero length are allowed and the set of elements must exactly fill
2507 * the length of the block. */
2508 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2510 unsigned int off = 0;
2524 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2526 unsigned short length;
2527 unsigned short type;
2528 unsigned short size;
2529 unsigned char *data = *p;
2530 int tlsext_servername = 0;
2531 int renegotiate_seen = 0;
2533 #ifndef OPENSSL_NO_NEXTPROTONEG
2534 s->s3->next_proto_neg_seen = 0;
2537 #ifndef OPENSSL_NO_HEARTBEATS
2538 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2539 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2542 if (data >= (d+n-2))
2546 if (data+length != d+n)
2548 *al = SSL_AD_DECODE_ERROR;
2552 while(data <= (d+n-4))
2557 if (data+size > (d+n))
2560 if (s->tlsext_debug_cb)
2561 s->tlsext_debug_cb(s, 1, type, data, size,
2562 s->tlsext_debug_arg);
2564 if (type == TLSEXT_TYPE_server_name)
2566 if (s->tlsext_hostname == NULL || size > 0)
2568 *al = TLS1_AD_UNRECOGNIZED_NAME;
2571 tlsext_servername = 1;
2574 #ifndef OPENSSL_NO_EC
2575 else if (type == TLSEXT_TYPE_ec_point_formats &&
2576 s->version != DTLS1_VERSION)
2578 unsigned char *sdata = data;
2579 int ecpointformatlist_length = *(sdata++);
2581 if (ecpointformatlist_length != size - 1)
2583 *al = TLS1_AD_DECODE_ERROR;
2586 s->session->tlsext_ecpointformatlist_length = 0;
2587 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2588 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2590 *al = TLS1_AD_INTERNAL_ERROR;
2593 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2594 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2596 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2597 sdata = s->session->tlsext_ecpointformatlist;
2598 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2599 fprintf(stderr,"%i ",*(sdata++));
2600 fprintf(stderr,"\n");
2603 #endif /* OPENSSL_NO_EC */
2605 else if (type == TLSEXT_TYPE_session_ticket)
2607 if (s->tls_session_ticket_ext_cb &&
2608 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2610 *al = TLS1_AD_INTERNAL_ERROR;
2613 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2616 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2619 s->tlsext_ticket_expected = 1;
2621 #ifdef TLSEXT_TYPE_opaque_prf_input
2622 else if (type == TLSEXT_TYPE_opaque_prf_input &&
2623 s->version != DTLS1_VERSION)
2625 unsigned char *sdata = data;
2629 *al = SSL_AD_DECODE_ERROR;
2632 n2s(sdata, s->s3->server_opaque_prf_input_len);
2633 if (s->s3->server_opaque_prf_input_len != size - 2)
2635 *al = SSL_AD_DECODE_ERROR;
2639 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2640 OPENSSL_free(s->s3->server_opaque_prf_input);
2641 if (s->s3->server_opaque_prf_input_len == 0)
2642 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2644 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2646 if (s->s3->server_opaque_prf_input == NULL)
2648 *al = TLS1_AD_INTERNAL_ERROR;
2653 else if (type == TLSEXT_TYPE_status_request &&
2654 s->version != DTLS1_VERSION)
2656 /* MUST be empty and only sent if we've requested
2657 * a status request message.
2659 if ((s->tlsext_status_type == -1) || (size > 0))
2661 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2664 /* Set flag to expect CertificateStatus message */
2665 s->tlsext_status_expected = 1;
2667 #ifndef OPENSSL_NO_NEXTPROTONEG
2668 else if (type == TLSEXT_TYPE_next_proto_neg &&
2669 s->s3->tmp.finish_md_len == 0)
2671 unsigned char *selected;
2672 unsigned char selected_len;
2674 /* We must have requested it. */
2675 if (s->ctx->next_proto_select_cb == NULL)
2677 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2680 /* The data must be valid */
2681 if (!ssl_next_proto_validate(data, size))
2683 *al = TLS1_AD_DECODE_ERROR;
2686 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2688 *al = TLS1_AD_INTERNAL_ERROR;
2691 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2692 if (!s->next_proto_negotiated)
2694 *al = TLS1_AD_INTERNAL_ERROR;
2697 memcpy(s->next_proto_negotiated, selected, selected_len);
2698 s->next_proto_negotiated_len = selected_len;
2699 s->s3->next_proto_neg_seen = 1;
2702 else if (type == TLSEXT_TYPE_renegotiate)
2704 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2706 renegotiate_seen = 1;
2708 #ifndef OPENSSL_NO_HEARTBEATS
2709 else if (type == TLSEXT_TYPE_heartbeat)
2713 case 0x01: /* Server allows us to send HB requests */
2714 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2716 case 0x02: /* Server doesn't accept HB requests */
2717 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2718 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2720 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2725 else if (type == TLSEXT_TYPE_use_srtp)
2727 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2732 else if (type == TLSEXT_TYPE_server_authz)
2734 /* We only support audit proofs. It's an error to send
2735 * an authz hello extension if the client
2736 * didn't request a proof. */
2737 unsigned char *sdata = data;
2738 unsigned char server_authz_dataformatlist_length;
2740 if (!s->ctx->tlsext_authz_server_audit_proof_cb)
2742 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2748 *al = TLS1_AD_DECODE_ERROR;
2752 server_authz_dataformatlist_length = *(sdata++);
2753 if (server_authz_dataformatlist_length != size - 1)
2755 *al = TLS1_AD_DECODE_ERROR;
2759 /* We only support audit proofs, so a legal ServerHello
2760 * authz list contains exactly one entry. */
2761 if (server_authz_dataformatlist_length != 1 ||
2762 sdata[0] != TLSEXT_AUTHZDATAFORMAT_audit_proof)
2764 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2768 s->s3->tlsext_authz_server_promised = 1;
2771 /* If this extension type was not otherwise handled, but
2772 * matches a custom_cli_ext_record, then send it to the c
2774 else if (s->ctx->custom_cli_ext_records_count)
2777 custom_cli_ext_record* record;
2779 for (i = 0; i < s->ctx->custom_cli_ext_records_count; i++)
2781 record = &s->ctx->custom_cli_ext_records[i];
2782 if (record->ext_type == type)
2784 if (record->fn2 && !record->fn2(s, type, data, size, al, record->arg))
2796 *al = SSL_AD_DECODE_ERROR;
2800 if (!s->hit && tlsext_servername == 1)
2802 if (s->tlsext_hostname)
2804 if (s->session->tlsext_hostname == NULL)
2806 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2807 if (!s->session->tlsext_hostname)
2809 *al = SSL_AD_UNRECOGNIZED_NAME;
2815 *al = SSL_AD_DECODE_ERROR;
2825 /* Determine if we need to see RI. Strictly speaking if we want to
2826 * avoid an attack we should *always* see RI even on initial server
2827 * hello because the client doesn't see any renegotiation during an
2828 * attack. However this would mean we could not connect to any server
2829 * which doesn't support RI so for the immediate future tolerate RI
2830 * absence on initial connect only.
2832 if (!renegotiate_seen
2833 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2834 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2836 *al = SSL_AD_HANDSHAKE_FAILURE;
2837 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2838 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2846 int ssl_prepare_clienthello_tlsext(SSL *s)
2849 #ifdef TLSEXT_TYPE_opaque_prf_input
2853 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2855 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2860 if (s->tlsext_opaque_prf_input != NULL)
2862 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2863 OPENSSL_free(s->s3->client_opaque_prf_input);
2865 if (s->tlsext_opaque_prf_input_len == 0)
2866 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2868 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2869 if (s->s3->client_opaque_prf_input == NULL)
2871 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2874 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2878 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2879 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2886 int ssl_prepare_serverhello_tlsext(SSL *s)
2891 static int ssl_check_clienthello_tlsext_early(SSL *s)
2893 int ret=SSL_TLSEXT_ERR_NOACK;
2894 int al = SSL_AD_UNRECOGNIZED_NAME;
2896 #ifndef OPENSSL_NO_EC
2897 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2898 * ssl3_choose_cipher in s3_lib.c.
2900 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2901 * ssl3_choose_cipher in s3_lib.c.
2905 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2906 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2907 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2908 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2910 #ifdef TLSEXT_TYPE_opaque_prf_input
2912 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2913 * but we might be sending an alert in response to the client hello,
2914 * so this has to happen here in
2915 * ssl_check_clienthello_tlsext_early(). */
2919 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2921 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2924 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2925 al = SSL_AD_INTERNAL_ERROR;
2930 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2931 OPENSSL_free(s->s3->server_opaque_prf_input);
2932 s->s3->server_opaque_prf_input = NULL;
2934 if (s->tlsext_opaque_prf_input != NULL)
2936 if (s->s3->client_opaque_prf_input != NULL &&
2937 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2939 /* can only use this extension if we have a server opaque PRF input
2940 * of the same length as the client opaque PRF input! */
2942 if (s->tlsext_opaque_prf_input_len == 0)
2943 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2945 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2946 if (s->s3->server_opaque_prf_input == NULL)
2948 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2949 al = SSL_AD_INTERNAL_ERROR;
2952 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2956 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2958 /* The callback wants to enforce use of the extension,
2959 * but we can't do that with the client opaque PRF input;
2960 * abort the handshake.
2962 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2963 al = SSL_AD_HANDSHAKE_FAILURE;
2971 case SSL_TLSEXT_ERR_ALERT_FATAL:
2972 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2975 case SSL_TLSEXT_ERR_ALERT_WARNING:
2976 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2979 case SSL_TLSEXT_ERR_NOACK:
2980 s->servername_done=0;
2986 int ssl_check_clienthello_tlsext_late(SSL *s)
2988 int ret = SSL_TLSEXT_ERR_OK;
2991 /* If status request then ask callback what to do.
2992 * Note: this must be called after servername callbacks in case
2993 * the certificate has changed, and must be called after the cipher
2994 * has been chosen because this may influence which certificate is sent
2996 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2999 CERT_PKEY *certpkey;
3000 certpkey = ssl_get_server_send_pkey(s);
3001 /* If no certificate can't return certificate status */
3002 if (certpkey == NULL)
3004 s->tlsext_status_expected = 0;
3007 /* Set current certificate to one we will use so
3008 * SSL_get_certificate et al can pick it up.
3010 s->cert->key = certpkey;
3011 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3014 /* We don't want to send a status request response */
3015 case SSL_TLSEXT_ERR_NOACK:
3016 s->tlsext_status_expected = 0;
3018 /* status request response should be sent */
3019 case SSL_TLSEXT_ERR_OK:
3020 if (s->tlsext_ocsp_resp)
3021 s->tlsext_status_expected = 1;
3023 s->tlsext_status_expected = 0;
3025 /* something bad happened */
3026 case SSL_TLSEXT_ERR_ALERT_FATAL:
3027 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3028 al = SSL_AD_INTERNAL_ERROR;
3033 s->tlsext_status_expected = 0;
3038 case SSL_TLSEXT_ERR_ALERT_FATAL:
3039 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3042 case SSL_TLSEXT_ERR_ALERT_WARNING:
3043 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3051 int ssl_check_serverhello_tlsext(SSL *s)
3053 int ret=SSL_TLSEXT_ERR_NOACK;
3054 int al = SSL_AD_UNRECOGNIZED_NAME;
3056 #ifndef OPENSSL_NO_EC
3057 /* If we are client and using an elliptic curve cryptography cipher
3058 * suite, then if server returns an EC point formats lists extension
3059 * it must contain uncompressed.
3061 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3062 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3063 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3064 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3065 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3067 /* we are using an ECC cipher */
3069 unsigned char *list;
3070 int found_uncompressed = 0;
3071 list = s->session->tlsext_ecpointformatlist;
3072 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3074 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3076 found_uncompressed = 1;
3080 if (!found_uncompressed)
3082 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3086 ret = SSL_TLSEXT_ERR_OK;
3087 #endif /* OPENSSL_NO_EC */
3089 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3090 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3091 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3092 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3094 #ifdef TLSEXT_TYPE_opaque_prf_input
3095 if (s->s3->server_opaque_prf_input_len > 0)
3097 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
3098 * So first verify that we really have a value from the server too. */
3100 if (s->s3->server_opaque_prf_input == NULL)
3102 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3103 al = SSL_AD_HANDSHAKE_FAILURE;
3106 /* Anytime the server *has* sent an opaque PRF input, we need to check
3107 * that we have a client opaque PRF input of the same size. */
3108 if (s->s3->client_opaque_prf_input == NULL ||
3109 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
3111 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3112 al = SSL_AD_ILLEGAL_PARAMETER;
3117 /* If we've requested certificate status and we wont get one
3120 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3121 && s->ctx && s->ctx->tlsext_status_cb)
3124 /* Set resp to NULL, resplen to -1 so callback knows
3125 * there is no response.
3127 if (s->tlsext_ocsp_resp)
3129 OPENSSL_free(s->tlsext_ocsp_resp);
3130 s->tlsext_ocsp_resp = NULL;
3132 s->tlsext_ocsp_resplen = -1;
3133 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3136 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3137 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3141 al = SSL_AD_INTERNAL_ERROR;
3142 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3148 case SSL_TLSEXT_ERR_ALERT_FATAL:
3149 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3152 case SSL_TLSEXT_ERR_ALERT_WARNING:
3153 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3156 case SSL_TLSEXT_ERR_NOACK:
3157 s->servername_done=0;
3163 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
3166 if (s->version < SSL3_VERSION)
3168 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0)
3170 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3174 if (ssl_check_serverhello_tlsext(s) <= 0)
3176 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,SSL_R_SERVERHELLO_TLSEXT);
3182 /* Since the server cache lookup is done early on in the processing of the
3183 * ClientHello, and other operations depend on the result, we need to handle
3184 * any TLS session ticket extension at the same time.
3186 * session_id: points at the session ID in the ClientHello. This code will
3187 * read past the end of this in order to parse out the session ticket
3188 * extension, if any.
3189 * len: the length of the session ID.
3190 * limit: a pointer to the first byte after the ClientHello.
3191 * ret: (output) on return, if a ticket was decrypted, then this is set to
3192 * point to the resulting session.
3194 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3195 * ciphersuite, in which case we have no use for session tickets and one will
3196 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3199 * -1: fatal error, either from parsing or decrypting the ticket.
3200 * 0: no ticket was found (or was ignored, based on settings).
3201 * 1: a zero length extension was found, indicating that the client supports
3202 * session tickets but doesn't currently have one to offer.
3203 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
3204 * couldn't be decrypted because of a non-fatal error.
3205 * 3: a ticket was successfully decrypted and *ret was set.
3208 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3209 * a new session ticket to the client because the client indicated support
3210 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3211 * a session ticket or we couldn't use the one it gave us, or if
3212 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3213 * Otherwise, s->tlsext_ticket_expected is set to 0.
3215 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
3216 const unsigned char *limit, SSL_SESSION **ret)
3218 /* Point after session ID in client hello */
3219 const unsigned char *p = session_id + len;
3223 s->tlsext_ticket_expected = 0;
3225 /* If tickets disabled behave as if no ticket present
3226 * to permit stateful resumption.
3228 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
3230 if ((s->version <= SSL3_VERSION) || !limit)
3234 /* Skip past DTLS cookie */
3235 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
3242 /* Skip past cipher list */
3247 /* Skip past compression algorithm list */
3252 /* Now at start of extensions */
3253 if ((p + 2) >= limit)
3256 while ((p + 4) <= limit)
3258 unsigned short type, size;
3261 if (p + size > limit)
3263 if (type == TLSEXT_TYPE_session_ticket)
3268 /* The client will accept a ticket but doesn't
3269 * currently have one. */
3270 s->tlsext_ticket_expected = 1;
3273 if (s->tls_session_secret_cb)
3275 /* Indicate that the ticket couldn't be
3276 * decrypted rather than generating the session
3277 * from ticket now, trigger abbreviated
3278 * handshake based on external mechanism to
3279 * calculate the master secret later. */
3282 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
3285 case 2: /* ticket couldn't be decrypted */
3286 s->tlsext_ticket_expected = 1;
3288 case 3: /* ticket was decrypted */
3290 case 4: /* ticket decrypted but need to renew */
3291 s->tlsext_ticket_expected = 1;
3293 default: /* fatal error */
3302 /* tls_decrypt_ticket attempts to decrypt a session ticket.
3304 * etick: points to the body of the session ticket extension.
3305 * eticklen: the length of the session tickets extenion.
3306 * sess_id: points at the session ID.
3307 * sesslen: the length of the session ID.
3308 * psess: (output) on return, if a ticket was decrypted, then this is set to
3309 * point to the resulting session.
3312 * -1: fatal error, either from parsing or decrypting the ticket.
3313 * 2: the ticket couldn't be decrypted.
3314 * 3: a ticket was successfully decrypted and *psess was set.
3315 * 4: same as 3, but the ticket needs to be renewed.
3317 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
3318 const unsigned char *sess_id, int sesslen,
3319 SSL_SESSION **psess)
3322 unsigned char *sdec;
3323 const unsigned char *p;
3324 int slen, mlen, renew_ticket = 0;
3325 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3328 SSL_CTX *tctx = s->initial_ctx;
3329 /* Need at least keyname + iv + some encrypted data */
3332 /* Initialize session ticket encryption and HMAC contexts */
3333 HMAC_CTX_init(&hctx);
3334 EVP_CIPHER_CTX_init(&ctx);
3335 if (tctx->tlsext_ticket_key_cb)
3337 unsigned char *nctick = (unsigned char *)etick;
3338 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3349 /* Check key name matches */
3350 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3352 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3353 tlsext_tick_md(), NULL);
3354 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3355 tctx->tlsext_tick_aes_key, etick + 16);
3357 /* Attempt to process session ticket, first conduct sanity and
3358 * integrity checks on ticket.
3360 mlen = HMAC_size(&hctx);
3363 EVP_CIPHER_CTX_cleanup(&ctx);
3367 /* Check HMAC of encrypted ticket */
3368 HMAC_Update(&hctx, etick, eticklen);
3369 HMAC_Final(&hctx, tick_hmac, NULL);
3370 HMAC_CTX_cleanup(&hctx);
3371 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
3373 /* Attempt to decrypt session data */
3374 /* Move p after IV to start of encrypted ticket, update length */
3375 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3376 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3377 sdec = OPENSSL_malloc(eticklen);
3380 EVP_CIPHER_CTX_cleanup(&ctx);
3383 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
3384 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
3387 EVP_CIPHER_CTX_cleanup(&ctx);
3390 sess = d2i_SSL_SESSION(NULL, &p, slen);
3394 /* The session ID, if non-empty, is used by some clients to
3395 * detect that the ticket has been accepted. So we copy it to
3396 * the session structure. If it is empty set length to zero
3397 * as required by standard.
3400 memcpy(sess->session_id, sess_id, sesslen);
3401 sess->session_id_length = sesslen;
3409 /* For session parse failure, indicate that we need to send a new
3414 /* Tables to translate from NIDs to TLS v1.2 ids */
3422 static tls12_lookup tls12_md[] = {
3423 {NID_md5, TLSEXT_hash_md5},
3424 {NID_sha1, TLSEXT_hash_sha1},
3425 {NID_sha224, TLSEXT_hash_sha224},
3426 {NID_sha256, TLSEXT_hash_sha256},
3427 {NID_sha384, TLSEXT_hash_sha384},
3428 {NID_sha512, TLSEXT_hash_sha512}
3431 static tls12_lookup tls12_sig[] = {
3432 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3433 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3434 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3437 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
3440 for (i = 0; i < tlen; i++)
3442 if (table[i].nid == nid)
3448 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
3451 for (i = 0; i < tlen; i++)
3453 if ((table[i].id) == id)
3454 return table[i].nid;
3459 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3464 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3465 sizeof(tls12_md)/sizeof(tls12_lookup));
3468 sig_id = tls12_get_sigid(pk);
3471 p[0] = (unsigned char)md_id;
3472 p[1] = (unsigned char)sig_id;
3476 int tls12_get_sigid(const EVP_PKEY *pk)
3478 return tls12_find_id(pk->type, tls12_sig,
3479 sizeof(tls12_sig)/sizeof(tls12_lookup));
3482 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3486 #ifndef OPENSSL_NO_MD5
3487 case TLSEXT_hash_md5:
3494 #ifndef OPENSSL_NO_SHA
3495 case TLSEXT_hash_sha1:
3498 #ifndef OPENSSL_NO_SHA256
3499 case TLSEXT_hash_sha224:
3500 return EVP_sha224();
3502 case TLSEXT_hash_sha256:
3503 return EVP_sha256();
3505 #ifndef OPENSSL_NO_SHA512
3506 case TLSEXT_hash_sha384:
3507 return EVP_sha384();
3509 case TLSEXT_hash_sha512:
3510 return EVP_sha512();
3518 static int tls12_get_pkey_idx(unsigned char sig_alg)
3522 #ifndef OPENSSL_NO_RSA
3523 case TLSEXT_signature_rsa:
3524 return SSL_PKEY_RSA_SIGN;
3526 #ifndef OPENSSL_NO_DSA
3527 case TLSEXT_signature_dsa:
3528 return SSL_PKEY_DSA_SIGN;
3530 #ifndef OPENSSL_NO_ECDSA
3531 case TLSEXT_signature_ecdsa:
3532 return SSL_PKEY_ECC;
3538 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3539 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3540 int *psignhash_nid, const unsigned char *data)
3542 int sign_nid = 0, hash_nid = 0;
3543 if (!phash_nid && !psign_nid && !psignhash_nid)
3545 if (phash_nid || psignhash_nid)
3547 hash_nid = tls12_find_nid(data[0], tls12_md,
3548 sizeof(tls12_md)/sizeof(tls12_lookup));
3550 *phash_nid = hash_nid;
3552 if (psign_nid || psignhash_nid)
3554 sign_nid = tls12_find_nid(data[1], tls12_sig,
3555 sizeof(tls12_sig)/sizeof(tls12_lookup));
3557 *psign_nid = sign_nid;
3561 if (sign_nid && hash_nid)
3562 OBJ_find_sigid_by_algs(psignhash_nid,
3563 hash_nid, sign_nid);
3565 *psignhash_nid = NID_undef;
3568 /* Given preference and allowed sigalgs set shared sigalgs */
3569 static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
3570 const unsigned char *pref, size_t preflen,
3571 const unsigned char *allow, size_t allowlen)
3573 const unsigned char *ptmp, *atmp;
3574 size_t i, j, nmatch = 0;
3575 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
3577 /* Skip disabled hashes or signature algorithms */
3578 if (tls12_get_hash(ptmp[0]) == NULL)
3580 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3582 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
3584 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
3589 shsig->rhash = ptmp[0];
3590 shsig->rsign = ptmp[1];
3591 tls1_lookup_sigalg(&shsig->hash_nid,
3593 &shsig->signandhash_nid,
3604 /* Set shared signature algorithms for SSL structures */
3605 static int tls1_set_shared_sigalgs(SSL *s)
3607 const unsigned char *pref, *allow, *conf;
3608 size_t preflen, allowlen, conflen;
3610 TLS_SIGALGS *salgs = NULL;
3612 unsigned int is_suiteb = tls1_suiteb(s);
3613 /* If client use client signature algorithms if not NULL */
3614 if (!s->server && c->client_sigalgs && !is_suiteb)
3616 conf = c->client_sigalgs;
3617 conflen = c->client_sigalgslen;
3619 else if (c->conf_sigalgs && !is_suiteb)
3621 conf = c->conf_sigalgs;
3622 conflen = c->conf_sigalgslen;
3625 conflen = tls12_get_psigalgs(s, &conf);
3626 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb)
3630 allow = c->peer_sigalgs;
3631 allowlen = c->peer_sigalgslen;
3637 pref = c->peer_sigalgs;
3638 preflen = c->peer_sigalgslen;
3640 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
3643 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3646 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
3647 c->shared_sigalgs = salgs;
3648 c->shared_sigalgslen = nmatch;
3653 /* Set preferred digest for each key type */
3655 int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
3661 TLS_SIGALGS *sigptr;
3662 /* Extension ignored for TLS versions below 1.2 */
3663 if (TLS1_get_version(s) < TLS1_2_VERSION)
3665 /* Should never happen */
3669 c->peer_sigalgs = OPENSSL_malloc(dsize);
3670 if (!c->peer_sigalgs)
3672 c->peer_sigalgslen = dsize;
3673 memcpy(c->peer_sigalgs, data, dsize);
3675 tls1_set_shared_sigalgs(s);
3677 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3678 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
3680 /* Use first set signature preference to force message
3681 * digest, ignoring any peer preferences.
3683 const unsigned char *sigs = NULL;
3685 sigs = c->conf_sigalgs;
3687 sigs = c->client_sigalgs;
3690 idx = tls12_get_pkey_idx(sigs[1]);
3691 md = tls12_get_hash(sigs[0]);
3692 c->pkeys[idx].digest = md;
3693 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3694 if (idx == SSL_PKEY_RSA_SIGN)
3696 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3697 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3703 for (i = 0, sigptr = c->shared_sigalgs;
3704 i < c->shared_sigalgslen; i++, sigptr++)
3706 idx = tls12_get_pkey_idx(sigptr->rsign);
3707 if (idx > 0 && c->pkeys[idx].digest == NULL)
3709 md = tls12_get_hash(sigptr->rhash);
3710 c->pkeys[idx].digest = md;
3711 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3712 if (idx == SSL_PKEY_RSA_SIGN)
3714 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3715 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3720 /* In strict mode leave unset digests as NULL to indicate we can't
3721 * use the certificate for signing.
3723 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
3725 /* Set any remaining keys to default values. NOTE: if alg is
3726 * not supported it stays as NULL.
3728 #ifndef OPENSSL_NO_DSA
3729 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
3730 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
3732 #ifndef OPENSSL_NO_RSA
3733 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
3735 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
3736 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
3739 #ifndef OPENSSL_NO_ECDSA
3740 if (!c->pkeys[SSL_PKEY_ECC].digest)
3741 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
3748 int SSL_get_sigalgs(SSL *s, int idx,
3749 int *psign, int *phash, int *psignhash,
3750 unsigned char *rsig, unsigned char *rhash)
3752 const unsigned char *psig = s->cert->peer_sigalgs;
3758 if (idx >= (int)s->cert->peer_sigalgslen)
3765 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3767 return s->cert->peer_sigalgslen / 2;
3770 int SSL_get_shared_sigalgs(SSL *s, int idx,
3771 int *psign, int *phash, int *psignhash,
3772 unsigned char *rsig, unsigned char *rhash)
3774 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3775 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3779 *phash = shsigalgs->hash_nid;
3781 *psign = shsigalgs->sign_nid;
3783 *psignhash = shsigalgs->signandhash_nid;
3785 *rsig = shsigalgs->rsign;
3787 *rhash = shsigalgs->rhash;
3788 return s->cert->shared_sigalgslen;
3792 #ifndef OPENSSL_NO_HEARTBEATS
3794 tls1_process_heartbeat(SSL *s)
3796 unsigned char *p = &s->s3->rrec.data[0], *pl;
3797 unsigned short hbtype;
3798 unsigned int payload;
3799 unsigned int padding = 16; /* Use minimum padding */
3801 /* Read type and payload length first */
3806 if (s->msg_callback)
3807 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
3808 &s->s3->rrec.data[0], s->s3->rrec.length,
3809 s, s->msg_callback_arg);
3811 if (hbtype == TLS1_HB_REQUEST)
3813 unsigned char *buffer, *bp;
3816 /* Allocate memory for the response, size is 1 bytes
3817 * message type, plus 2 bytes payload length, plus
3818 * payload, plus padding
3820 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
3823 /* Enter response type, length and copy payload */
3824 *bp++ = TLS1_HB_RESPONSE;
3826 memcpy(bp, pl, payload);
3828 /* Random padding */
3829 RAND_pseudo_bytes(bp, padding);
3831 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
3833 if (r >= 0 && s->msg_callback)
3834 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3835 buffer, 3 + payload + padding,
3836 s, s->msg_callback_arg);
3838 OPENSSL_free(buffer);
3843 else if (hbtype == TLS1_HB_RESPONSE)
3847 /* We only send sequence numbers (2 bytes unsigned int),
3848 * and 16 random bytes, so we just try to read the
3849 * sequence number */
3852 if (payload == 18 && seq == s->tlsext_hb_seq)
3855 s->tlsext_hb_pending = 0;
3863 tls1_heartbeat(SSL *s)
3865 unsigned char *buf, *p;
3867 unsigned int payload = 18; /* Sequence number + random bytes */
3868 unsigned int padding = 16; /* Use minimum padding */
3870 /* Only send if peer supports and accepts HB requests... */
3871 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
3872 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
3874 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
3878 /* ...and there is none in flight yet... */
3879 if (s->tlsext_hb_pending)
3881 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING);
3885 /* ...and no handshake in progress. */
3886 if (SSL_in_init(s) || s->in_handshake)
3888 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE);
3892 /* Check if padding is too long, payload and padding
3893 * must not exceed 2^14 - 3 = 16381 bytes in total.
3895 OPENSSL_assert(payload + padding <= 16381);
3897 /* Create HeartBeat message, we just use a sequence number
3898 * as payload to distuingish different messages and add
3899 * some random stuff.
3900 * - Message Type, 1 byte
3901 * - Payload Length, 2 bytes (unsigned int)
3902 * - Payload, the sequence number (2 bytes uint)
3903 * - Payload, random bytes (16 bytes uint)
3906 buf = OPENSSL_malloc(1 + 2 + payload + padding);
3909 *p++ = TLS1_HB_REQUEST;
3910 /* Payload length (18 bytes here) */
3912 /* Sequence number */
3913 s2n(s->tlsext_hb_seq, p);
3914 /* 16 random bytes */
3915 RAND_pseudo_bytes(p, 16);
3917 /* Random padding */
3918 RAND_pseudo_bytes(p, padding);
3920 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
3923 if (s->msg_callback)
3924 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3925 buf, 3 + payload + padding,
3926 s, s->msg_callback_arg);
3928 s->tlsext_hb_pending = 1;
3937 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3942 int sigalgs[MAX_SIGALGLEN];
3945 static int sig_cb(const char *elem, int len, void *arg)
3947 sig_cb_st *sarg = arg;
3950 int sig_alg, hash_alg;
3951 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3953 if (len > (int)(sizeof(etmp) - 1))
3955 memcpy(etmp, elem, len);
3957 p = strchr(etmp, '+');
3965 if (!strcmp(etmp, "RSA"))
3966 sig_alg = EVP_PKEY_RSA;
3967 else if (!strcmp(etmp, "DSA"))
3968 sig_alg = EVP_PKEY_DSA;
3969 else if (!strcmp(etmp, "ECDSA"))
3970 sig_alg = EVP_PKEY_EC;
3973 hash_alg = OBJ_sn2nid(p);
3974 if (hash_alg == NID_undef)
3975 hash_alg = OBJ_ln2nid(p);
3976 if (hash_alg == NID_undef)
3979 for (i = 0; i < sarg->sigalgcnt; i+=2)
3981 if (sarg->sigalgs[i] == sig_alg
3982 && sarg->sigalgs[i + 1] == hash_alg)
3985 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3986 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3990 /* Set suppored signature algorithms based on a colon separated list
3991 * of the form sig+hash e.g. RSA+SHA512:DSA+SHA512 */
3992 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3996 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
4000 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
4003 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
4005 unsigned char *sigalgs, *sptr;
4010 sigalgs = OPENSSL_malloc(salglen);
4011 if (sigalgs == NULL)
4013 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
4015 rhash = tls12_find_id(*psig_nids++, tls12_md,
4016 sizeof(tls12_md)/sizeof(tls12_lookup));
4017 rsign = tls12_find_id(*psig_nids++, tls12_sig,
4018 sizeof(tls12_sig)/sizeof(tls12_lookup));
4020 if (rhash == -1 || rsign == -1)
4028 if (c->client_sigalgs)
4029 OPENSSL_free(c->client_sigalgs);
4030 c->client_sigalgs = sigalgs;
4031 c->client_sigalgslen = salglen;
4035 if (c->conf_sigalgs)
4036 OPENSSL_free(c->conf_sigalgs);
4037 c->conf_sigalgs = sigalgs;
4038 c->conf_sigalgslen = salglen;
4044 OPENSSL_free(sigalgs);
4048 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
4052 if (default_nid == -1)
4054 sig_nid = X509_get_signature_nid(x);
4056 return sig_nid == default_nid ? 1 : 0;
4057 for (i = 0; i < c->shared_sigalgslen; i++)
4058 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
4062 /* Check to see if a certificate issuer name matches list of CA names */
4063 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
4067 nm = X509_get_issuer_name(x);
4068 for (i = 0; i < sk_X509_NAME_num(names); i++)
4070 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
4076 /* Check certificate chain is consistent with TLS extensions and is
4077 * usable by server. This servers two purposes: it allows users to
4078 * check chains before passing them to the server and it allows the
4079 * server to check chains before attempting to use them.
4082 /* Flags which need to be set for a certificate when stict mode not set */
4084 #define CERT_PKEY_VALID_FLAGS \
4085 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
4086 /* Strict mode flags */
4087 #define CERT_PKEY_STRICT_FLAGS \
4088 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
4089 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
4091 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
4096 int check_flags = 0, strict_mode;
4097 CERT_PKEY *cpk = NULL;
4099 unsigned int suiteb_flags = tls1_suiteb(s);
4100 /* idx == -1 means checking server chains */
4103 /* idx == -2 means checking client certificate chains */
4107 idx = cpk - c->pkeys;
4110 cpk = c->pkeys + idx;
4112 pk = cpk->privatekey;
4114 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
4115 /* If no cert or key, forget it */
4118 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
4119 /* Allow any certificate to pass test */
4120 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
4122 rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
4123 cpk->valid_flags = rv;
4132 idx = ssl_cert_type(x, pk);
4135 cpk = c->pkeys + idx;
4136 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
4137 check_flags = CERT_PKEY_STRICT_FLAGS;
4139 check_flags = CERT_PKEY_VALID_FLAGS;
4147 check_flags |= CERT_PKEY_SUITEB;
4148 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
4149 if (ok != X509_V_OK)
4152 rv |= CERT_PKEY_SUITEB;
4158 /* Check all signature algorithms are consistent with
4159 * signature algorithms extension if TLS 1.2 or later
4162 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
4165 unsigned char rsign = 0;
4166 if (c->peer_sigalgs)
4168 /* If no sigalgs extension use defaults from RFC5246 */
4173 case SSL_PKEY_RSA_ENC:
4174 case SSL_PKEY_RSA_SIGN:
4175 case SSL_PKEY_DH_RSA:
4176 rsign = TLSEXT_signature_rsa;
4177 default_nid = NID_sha1WithRSAEncryption;
4180 case SSL_PKEY_DSA_SIGN:
4181 case SSL_PKEY_DH_DSA:
4182 rsign = TLSEXT_signature_dsa;
4183 default_nid = NID_dsaWithSHA1;
4187 rsign = TLSEXT_signature_ecdsa;
4188 default_nid = NID_ecdsa_with_SHA1;
4196 /* If peer sent no signature algorithms extension and we
4197 * have set preferred signature algorithms check we support
4200 if (default_nid > 0 && c->conf_sigalgs)
4203 const unsigned char *p = c->conf_sigalgs;
4204 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
4206 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4209 if (j == c->conf_sigalgslen)
4217 /* Check signature algorithm of each cert in chain */
4218 if (!tls1_check_sig_alg(c, x, default_nid))
4220 if (!check_flags) goto end;
4223 rv |= CERT_PKEY_EE_SIGNATURE;
4224 rv |= CERT_PKEY_CA_SIGNATURE;
4225 for (i = 0; i < sk_X509_num(chain); i++)
4227 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
4232 rv &= ~CERT_PKEY_CA_SIGNATURE;
4240 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4241 else if(check_flags)
4242 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
4244 /* Check cert parameters are consistent */
4245 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4246 rv |= CERT_PKEY_EE_PARAM;
4247 else if (!check_flags)
4250 rv |= CERT_PKEY_CA_PARAM;
4251 /* In strict mode check rest of chain too */
4252 else if (strict_mode)
4254 rv |= CERT_PKEY_CA_PARAM;
4255 for (i = 0; i < sk_X509_num(chain); i++)
4257 X509 *ca = sk_X509_value(chain, i);
4258 if (!tls1_check_cert_param(s, ca, 0))
4262 rv &= ~CERT_PKEY_CA_PARAM;
4270 if (!s->server && strict_mode)
4272 STACK_OF(X509_NAME) *ca_dn;
4277 check_type = TLS_CT_RSA_SIGN;
4280 check_type = TLS_CT_DSS_SIGN;
4283 check_type = TLS_CT_ECDSA_SIGN;
4288 int cert_type = X509_certificate_type(x, pk);
4289 if (cert_type & EVP_PKS_RSA)
4290 check_type = TLS_CT_RSA_FIXED_DH;
4291 if (cert_type & EVP_PKS_DSA)
4292 check_type = TLS_CT_DSS_FIXED_DH;
4297 const unsigned char *ctypes;
4302 ctypelen = (int)c->ctype_num;
4306 ctypes = (unsigned char *)s->s3->tmp.ctype;
4307 ctypelen = s->s3->tmp.ctype_num;
4309 for (i = 0; i < ctypelen; i++)
4311 if (ctypes[i] == check_type)
4313 rv |= CERT_PKEY_CERT_TYPE;
4317 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4321 rv |= CERT_PKEY_CERT_TYPE;
4324 ca_dn = s->s3->tmp.ca_names;
4326 if (!sk_X509_NAME_num(ca_dn))
4327 rv |= CERT_PKEY_ISSUER_NAME;
4329 if (!(rv & CERT_PKEY_ISSUER_NAME))
4331 if (ssl_check_ca_name(ca_dn, x))
4332 rv |= CERT_PKEY_ISSUER_NAME;
4334 if (!(rv & CERT_PKEY_ISSUER_NAME))
4336 for (i = 0; i < sk_X509_num(chain); i++)
4338 X509 *xtmp = sk_X509_value(chain, i);
4339 if (ssl_check_ca_name(ca_dn, xtmp))
4341 rv |= CERT_PKEY_ISSUER_NAME;
4346 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4350 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
4352 if (!check_flags || (rv & check_flags) == check_flags)
4353 rv |= CERT_PKEY_VALID;
4357 if (TLS1_get_version(s) >= TLS1_2_VERSION)
4359 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
4360 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
4361 else if (cpk->digest)
4362 rv |= CERT_PKEY_SIGN;
4365 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
4367 /* When checking a CERT_PKEY structure all flags are irrelevant
4368 * if the chain is invalid.
4372 if (rv & CERT_PKEY_VALID)
4373 cpk->valid_flags = rv;
4376 /* Preserve explicit sign flag, clear rest */
4377 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
4384 /* Set validity of certificates in an SSL structure */
4385 void tls1_set_cert_validity(SSL *s)
4387 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4388 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4389 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4390 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4391 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4392 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4394 /* User level utiity function to check a chain is suitable */
4395 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4397 return tls1_check_chain(s, x, pk, chain, -1);