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(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 int tls1_ec_curve_id2nid(int curve_id)
246 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
247 if ((curve_id < 1) || ((unsigned int)curve_id >
248 sizeof(nid_list)/sizeof(nid_list[0])))
250 return nid_list[curve_id-1];
253 int tls1_ec_nid2curve_id(int nid)
255 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
258 case NID_sect163k1: /* sect163k1 (1) */
260 case NID_sect163r1: /* sect163r1 (2) */
262 case NID_sect163r2: /* sect163r2 (3) */
264 case NID_sect193r1: /* sect193r1 (4) */
266 case NID_sect193r2: /* sect193r2 (5) */
268 case NID_sect233k1: /* sect233k1 (6) */
270 case NID_sect233r1: /* sect233r1 (7) */
272 case NID_sect239k1: /* sect239k1 (8) */
274 case NID_sect283k1: /* sect283k1 (9) */
276 case NID_sect283r1: /* sect283r1 (10) */
278 case NID_sect409k1: /* sect409k1 (11) */
280 case NID_sect409r1: /* sect409r1 (12) */
282 case NID_sect571k1: /* sect571k1 (13) */
284 case NID_sect571r1: /* sect571r1 (14) */
286 case NID_secp160k1: /* secp160k1 (15) */
288 case NID_secp160r1: /* secp160r1 (16) */
290 case NID_secp160r2: /* secp160r2 (17) */
292 case NID_secp192k1: /* secp192k1 (18) */
294 case NID_X9_62_prime192v1: /* secp192r1 (19) */
296 case NID_secp224k1: /* secp224k1 (20) */
298 case NID_secp224r1: /* secp224r1 (21) */
300 case NID_secp256k1: /* secp256k1 (22) */
302 case NID_X9_62_prime256v1: /* secp256r1 (23) */
304 case NID_secp384r1: /* secp384r1 (24) */
306 case NID_secp521r1: /* secp521r1 (25) */
312 /* Get curves list, if "sess" is set return client curves otherwise
315 static void tls1_get_curvelist(SSL *s, int sess,
316 const unsigned char **pcurves,
321 *pcurves = s->session->tlsext_ellipticcurvelist;
322 *pcurveslen = s->session->tlsext_ellipticcurvelist_length;
326 *pcurves = s->tlsext_ellipticcurvelist;
327 *pcurveslen = s->tlsext_ellipticcurvelist_length;
329 /* If not set use default: for now static structure */
332 *pcurves = eccurves_default;
333 *pcurveslen = sizeof(eccurves_default);
336 /* Check a curve is one of our preferences */
337 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
339 const unsigned char *curves;
341 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
343 tls1_get_curvelist(s, 0, &curves, &curveslen);
344 for (i = 0; i < curveslen; i += 2, curves += 2)
346 if (p[1] == curves[0] && p[2] == curves[1])
352 /* Return nth shared curve. If nmatch == -1 return number of
356 int tls1_shared_curve(SSL *s, int nmatch)
358 const unsigned char *pref, *supp;
359 size_t preflen, supplen, i, j;
361 /* Can't do anything on client side */
364 tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
366 tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
371 for (i = 0; i < preflen; i++, pref+=2)
373 const unsigned char *tsupp = supp;
374 for (j = 0; j < supplen; j++, tsupp+=2)
376 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
380 int id = (pref[0] << 8) | pref[1];
381 return tls1_ec_curve_id2nid(id);
392 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
393 int *curves, size_t ncurves)
395 unsigned char *clist, *p;
397 /* Bitmap of curves included to detect duplicates: only works
398 * while curve ids < 32
400 unsigned long dup_list = 0;
401 clist = OPENSSL_malloc(ncurves * 2);
404 for (i = 0, p = clist; i < ncurves; i++)
406 unsigned long idmask;
408 id = tls1_ec_nid2curve_id(curves[i]);
410 if (!id || (dup_list & idmask))
421 *pextlen = ncurves * 2;
425 #define MAX_CURVELIST 25
430 int nid_arr[MAX_CURVELIST];
433 static int nid_cb(const char *elem, int len, void *arg)
435 nid_cb_st *narg = arg;
439 if (narg->nidcnt == MAX_CURVELIST)
441 if (len > (int)(sizeof(etmp) - 1))
443 memcpy(etmp, elem, len);
445 nid = EC_curve_nist2nid(etmp);
446 if (nid == NID_undef)
447 nid = OBJ_sn2nid(etmp);
448 if (nid == NID_undef)
449 nid = OBJ_ln2nid(etmp);
450 if (nid == NID_undef)
452 for (i = 0; i < narg->nidcnt; i++)
453 if (narg->nid_arr[i] == nid)
455 narg->nid_arr[narg->nidcnt++] = nid;
458 /* Set curves based on a colon separate list */
459 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
464 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
466 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
468 /* For an EC key set TLS id and required compression based on parameters */
469 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
475 const EC_METHOD *meth;
478 /* Determine if it is a prime field */
479 grp = EC_KEY_get0_group(ec);
480 pt = EC_KEY_get0_public_key(ec);
483 meth = EC_GROUP_method_of(grp);
486 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
490 /* Determine curve ID */
491 id = EC_GROUP_get_curve_name(grp);
492 id = tls1_ec_nid2curve_id(id);
493 /* If we have an ID set it, otherwise set arbitrary explicit curve */
497 curve_id[1] = (unsigned char)id;
509 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
512 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
514 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
517 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
521 /* Check an EC key is compatible with extensions */
522 static int tls1_check_ec_key(SSL *s,
523 unsigned char *curve_id, unsigned char *comp_id)
525 const unsigned char *p;
528 /* If point formats extension present check it, otherwise everything
529 * is supported (see RFC4492).
531 if (comp_id && s->session->tlsext_ecpointformatlist)
533 p = s->session->tlsext_ecpointformatlist;
534 plen = s->session->tlsext_ecpointformatlist_length;
535 for (i = 0; i < plen; i++, p++)
543 /* Check curve is consistent with client and server preferences */
544 for (j = 0; j <= 1; j++)
546 tls1_get_curvelist(s, j, &p, &plen);
547 for (i = 0; i < plen; i+=2, p+=2)
549 if (p[0] == curve_id[0] && p[1] == curve_id[1])
558 /* Check cert parameters compatible with extensions: currently just checks
559 * EC certificates have compatible curves and compression.
561 static int tls1_check_cert_param(SSL *s, X509 *x)
563 unsigned char comp_id, curve_id[2];
566 pkey = X509_get_pubkey(x);
569 /* If not EC nothing to do */
570 if (pkey->type != EVP_PKEY_EC)
575 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
579 return tls1_check_ec_key(s, curve_id, &comp_id);
581 /* Check EC temporary key is compatible with client extensions */
582 int tls1_check_ec_tmp_key(SSL *s)
584 unsigned char curve_id[2];
585 EC_KEY *ec = s->cert->ecdh_tmp;
586 if (s->cert->ecdh_tmp_auto)
588 /* Need a shared curve */
589 if (tls1_shared_curve(s, 0))
595 if (s->cert->ecdh_tmp_cb)
600 if (!tls1_set_ec_id(curve_id, NULL, ec))
602 /* Set this to allow use of invalid curves for testing */
606 return tls1_check_ec_key(s, curve_id, NULL);
610 #endif /* OPENSSL_NO_EC */
612 #ifndef OPENSSL_NO_TLSEXT
614 /* List of supported signature algorithms and hashes. Should make this
615 * customisable at some point, for now include everything we support.
618 #ifdef OPENSSL_NO_RSA
619 #define tlsext_sigalg_rsa(md) /* */
621 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
624 #ifdef OPENSSL_NO_DSA
625 #define tlsext_sigalg_dsa(md) /* */
627 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
630 #ifdef OPENSSL_NO_ECDSA
631 #define tlsext_sigalg_ecdsa(md) /* */
633 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
636 #define tlsext_sigalg(md) \
637 tlsext_sigalg_rsa(md) \
638 tlsext_sigalg_dsa(md) \
639 tlsext_sigalg_ecdsa(md)
641 static unsigned char tls12_sigalgs[] = {
642 #ifndef OPENSSL_NO_SHA512
643 tlsext_sigalg(TLSEXT_hash_sha512)
644 tlsext_sigalg(TLSEXT_hash_sha384)
646 #ifndef OPENSSL_NO_SHA256
647 tlsext_sigalg(TLSEXT_hash_sha256)
648 tlsext_sigalg(TLSEXT_hash_sha224)
650 #ifndef OPENSSL_NO_SHA
651 tlsext_sigalg(TLSEXT_hash_sha1)
653 #ifndef OPENSSL_NO_MD5
654 tlsext_sigalg_rsa(TLSEXT_hash_md5)
658 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
660 /* If server use client authentication sigalgs if not NULL */
661 if (s->server && s->cert->client_sigalgs)
663 *psigs = s->cert->client_sigalgs;
664 return s->cert->client_sigalgslen;
666 else if (s->cert->conf_sigalgs)
668 *psigs = s->cert->conf_sigalgs;
669 return s->cert->conf_sigalgslen;
673 *psigs = tls12_sigalgs;
675 /* If FIPS mode don't include MD5 which is last */
677 return sizeof(tls12_sigalgs) - 2;
680 return sizeof(tls12_sigalgs);
683 /* Check signature algorithm is consistent with sent supported signature
684 * algorithms and if so return relevant digest.
686 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
687 const unsigned char *sig, EVP_PKEY *pkey)
689 const unsigned char *sent_sigs;
690 size_t sent_sigslen, i;
691 int sigalg = tls12_get_sigid(pkey);
692 /* Should never happen */
695 /* Check key type is consistent with signature */
696 if (sigalg != (int)sig[1])
698 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
701 /* Check signature matches a type we sent */
702 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
703 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
705 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
708 /* Allow fallback to SHA1 if not strict mode */
709 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAG_TLS_STRICT))
711 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
714 *pmd = tls12_get_hash(sig[0]);
717 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
722 /* Get a mask of disabled algorithms: an algorithm is disabled
723 * if it isn't supported or doesn't appear in supported signature
724 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
725 * session and not global settings.
728 void ssl_set_client_disabled(SSL *s)
731 const unsigned char *sigalgs;
732 size_t i, sigalgslen;
733 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
736 /* If less than TLS 1.2 don't allow TLS 1.2 only ciphers */
737 if (TLS1_get_version(s) < TLS1_2_VERSION)
738 c->mask_ssl = SSL_TLSV1_2;
741 /* Now go through all signature algorithms seeing if we support
742 * any for RSA, DSA, ECDSA. Do this for all versions not just
745 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
746 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
750 #ifndef OPENSSL_NO_RSA
751 case TLSEXT_signature_rsa:
755 #ifndef OPENSSL_NO_DSA
756 case TLSEXT_signature_dsa:
760 #ifndef OPENSSL_NO_ECDSA
761 case TLSEXT_signature_ecdsa:
767 /* Disable auth and static DH if we don't include any appropriate
768 * signature algorithms.
772 c->mask_a |= SSL_aRSA;
773 c->mask_k |= SSL_kDHr|SSL_kECDHr;
777 c->mask_a |= SSL_aDSS;
778 c->mask_k |= SSL_kDHd;
782 c->mask_a |= SSL_aECDSA;
783 c->mask_k |= SSL_kECDHe;
785 #ifndef OPENSSL_NO_KRB5
786 if (!kssl_tgt_is_available(s->kssl_ctx))
788 c->mask_a |= SSL_aKRB5;
789 c->mask_k |= SSL_kKRB5;
792 #ifndef OPENSSL_NO_PSK
793 /* with PSK there must be client callback set */
794 if (!s->psk_client_callback)
796 c->mask_a |= SSL_aPSK;
797 c->mask_k |= SSL_kPSK;
799 #endif /* OPENSSL_NO_PSK */
803 /* byte_compare is a compare function for qsort(3) that compares bytes. */
804 static int byte_compare(const void *in_a, const void *in_b)
806 unsigned char a = *((const unsigned char*) in_a);
807 unsigned char b = *((const unsigned char*) in_b);
816 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
819 unsigned char *ret = p;
820 #ifndef OPENSSL_NO_EC
821 /* See if we support any ECC ciphersuites */
823 if (s->version != DTLS1_VERSION && s->version >= TLS1_VERSION)
826 unsigned long alg_k, alg_a;
827 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
829 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
831 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
833 alg_k = c->algorithm_mkey;
834 alg_a = c->algorithm_auth;
835 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)
836 || (alg_a & SSL_aECDSA)))
845 /* don't add extensions for SSLv3 unless doing secure renegotiation */
846 if (s->client_version == SSL3_VERSION
847 && !s->s3->send_connection_binding)
852 if (ret>=limit) return NULL; /* this really never occurs, but ... */
854 if (s->tlsext_hostname != NULL)
856 /* Add TLS extension servername to the Client Hello message */
857 unsigned long size_str;
860 /* check for enough space.
861 4 for the servername type and entension length
862 2 for servernamelist length
863 1 for the hostname type
864 2 for hostname length
868 if ((lenmax = limit - ret - 9) < 0
869 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
872 /* extension type and length */
873 s2n(TLSEXT_TYPE_server_name,ret);
876 /* length of servername list */
879 /* hostname type, length and hostname */
880 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
882 memcpy(ret, s->tlsext_hostname, size_str);
886 /* Add RI if renegotiating */
891 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
893 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
897 if((limit - p - 4 - el) < 0) return NULL;
899 s2n(TLSEXT_TYPE_renegotiate,ret);
902 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
904 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
911 #ifndef OPENSSL_NO_SRP
912 /* Add SRP username if there is one */
913 if (s->srp_ctx.login != NULL)
914 { /* Add TLS extension SRP username to the Client Hello message */
916 int login_len = strlen(s->srp_ctx.login);
917 if (login_len > 255 || login_len == 0)
919 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
923 /* check for enough space.
924 4 for the srp type type and entension length
925 1 for the srp user identity
926 + srp user identity length
928 if ((limit - ret - 5 - login_len) < 0) return NULL;
930 /* fill in the extension */
931 s2n(TLSEXT_TYPE_srp,ret);
932 s2n(login_len+1,ret);
933 (*ret++) = (unsigned char) login_len;
934 memcpy(ret, s->srp_ctx.login, login_len);
939 #ifndef OPENSSL_NO_EC
942 /* Add TLS extension ECPointFormats to the ClientHello message */
944 const unsigned char *plist;
946 /* If we have a custom point format list use it otherwise
948 plist = s->tlsext_ecpointformatlist;
950 plistlen = s->tlsext_ecpointformatlist_length;
953 plist = ecformats_default;
954 plistlen = sizeof(ecformats_default);
957 if ((lenmax = limit - ret - 5) < 0) return NULL;
958 if (plistlen > (size_t)lenmax) return NULL;
961 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
965 s2n(TLSEXT_TYPE_ec_point_formats,ret);
966 s2n(plistlen + 1,ret);
967 *(ret++) = (unsigned char)plistlen ;
968 memcpy(ret, plist, plistlen);
971 /* Add TLS extension EllipticCurves to the ClientHello message */
972 plist = s->tlsext_ellipticcurvelist;
973 tls1_get_curvelist(s, 0, &plist, &plistlen);
975 if ((lenmax = limit - ret - 6) < 0) return NULL;
976 if (plistlen > (size_t)lenmax) return NULL;
977 if (plistlen > 65532)
979 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
983 s2n(TLSEXT_TYPE_elliptic_curves,ret);
984 s2n(plistlen + 2, ret);
986 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
987 * elliptic_curve_list, but the examples use two bytes.
988 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
989 * resolves this to two bytes.
992 memcpy(ret, plist, plistlen);
995 #endif /* OPENSSL_NO_EC */
997 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
1000 if (!s->new_session && s->session && s->session->tlsext_tick)
1001 ticklen = s->session->tlsext_ticklen;
1002 else if (s->session && s->tlsext_session_ticket &&
1003 s->tlsext_session_ticket->data)
1005 ticklen = s->tlsext_session_ticket->length;
1006 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1007 if (!s->session->tlsext_tick)
1009 memcpy(s->session->tlsext_tick,
1010 s->tlsext_session_ticket->data,
1012 s->session->tlsext_ticklen = ticklen;
1016 if (ticklen == 0 && s->tlsext_session_ticket &&
1017 s->tlsext_session_ticket->data == NULL)
1019 /* Check for enough room 2 for extension type, 2 for len
1022 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1023 s2n(TLSEXT_TYPE_session_ticket,ret);
1027 memcpy(ret, s->session->tlsext_tick, ticklen);
1033 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1036 const unsigned char *salg;
1037 salglen = tls12_get_psigalgs(s, &salg);
1038 if ((size_t)(limit - ret) < salglen + 6)
1040 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1041 s2n(salglen + 2, ret);
1043 memcpy(ret, salg, salglen);
1047 #ifdef TLSEXT_TYPE_opaque_prf_input
1048 if (s->s3->client_opaque_prf_input != NULL &&
1049 s->version != DTLS1_VERSION)
1051 size_t col = s->s3->client_opaque_prf_input_len;
1053 if ((long)(limit - ret - 6 - col < 0))
1055 if (col > 0xFFFD) /* can't happen */
1058 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1061 memcpy(ret, s->s3->client_opaque_prf_input, col);
1066 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
1067 s->version != DTLS1_VERSION)
1070 long extlen, idlen, itmp;
1074 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1076 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1077 itmp = i2d_OCSP_RESPID(id, NULL);
1083 if (s->tlsext_ocsp_exts)
1085 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1092 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1093 s2n(TLSEXT_TYPE_status_request, ret);
1094 if (extlen + idlen > 0xFFF0)
1096 s2n(extlen + idlen + 5, ret);
1097 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1099 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1101 /* save position of id len */
1102 unsigned char *q = ret;
1103 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1104 /* skip over id len */
1106 itmp = i2d_OCSP_RESPID(id, &ret);
1112 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1115 #ifndef OPENSSL_NO_HEARTBEATS
1116 /* Add Heartbeat extension */
1117 s2n(TLSEXT_TYPE_heartbeat,ret);
1120 * 1: peer may send requests
1121 * 2: peer not allowed to send requests
1123 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1124 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1126 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1129 #ifndef OPENSSL_NO_NEXTPROTONEG
1130 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1132 /* The client advertises an emtpy extension to indicate its
1133 * support for Next Protocol Negotiation */
1134 if (limit - ret - 4 < 0)
1136 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1141 if(SSL_get_srtp_profiles(s))
1145 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1147 if((limit - p - 4 - el) < 0) return NULL;
1149 s2n(TLSEXT_TYPE_use_srtp,ret);
1152 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1154 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1160 /* Add TLS extension Server_Authz_DataFormats to the ClientHello */
1161 /* 2 bytes for extension type */
1162 /* 2 bytes for extension length */
1163 /* 1 byte for the list length */
1164 /* 1 byte for the list (we only support audit proofs) */
1165 if (s->ctx->tlsext_authz_server_audit_proof_cb != NULL)
1168 const unsigned short ext_len = 2;
1169 const unsigned char list_len = 1;
1171 if ((lenmax = limit - ret - 6) < 0) return NULL;
1173 s2n(TLSEXT_TYPE_server_authz, ret);
1174 /* Extension length: 2 bytes */
1176 *(ret++) = list_len;
1177 *(ret++) = TLSEXT_AUTHZDATAFORMAT_audit_proof;
1180 if ((extdatalen = ret-p-2) == 0)
1187 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
1190 unsigned char *ret = p;
1191 #ifndef OPENSSL_NO_NEXTPROTONEG
1192 int next_proto_neg_seen;
1195 /* don't add extensions for SSLv3, unless doing secure renegotiation */
1196 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1200 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1202 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1204 if ((long)(limit - ret - 4) < 0) return NULL;
1206 s2n(TLSEXT_TYPE_server_name,ret);
1210 if(s->s3->send_connection_binding)
1214 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1216 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1220 if((limit - p - 4 - el) < 0) return NULL;
1222 s2n(TLSEXT_TYPE_renegotiate,ret);
1225 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1227 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1234 #ifndef OPENSSL_NO_EC
1235 if (s->tlsext_ecpointformatlist != NULL &&
1236 s->version != DTLS1_VERSION)
1238 /* Add TLS extension ECPointFormats to the ServerHello message */
1241 if ((lenmax = limit - ret - 5) < 0) return NULL;
1242 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
1243 if (s->tlsext_ecpointformatlist_length > 255)
1245 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1249 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1250 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
1251 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
1252 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
1253 ret+=s->tlsext_ecpointformatlist_length;
1256 /* Currently the server should not respond with a SupportedCurves extension */
1257 #endif /* OPENSSL_NO_EC */
1259 if (s->tlsext_ticket_expected
1260 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
1262 if ((long)(limit - ret - 4) < 0) return NULL;
1263 s2n(TLSEXT_TYPE_session_ticket,ret);
1267 if (s->tlsext_status_expected)
1269 if ((long)(limit - ret - 4) < 0) return NULL;
1270 s2n(TLSEXT_TYPE_status_request,ret);
1274 #ifdef TLSEXT_TYPE_opaque_prf_input
1275 if (s->s3->server_opaque_prf_input != NULL &&
1276 s->version != DTLS1_VERSION)
1278 size_t sol = s->s3->server_opaque_prf_input_len;
1280 if ((long)(limit - ret - 6 - sol) < 0)
1282 if (sol > 0xFFFD) /* can't happen */
1285 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1288 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1297 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1299 if((limit - p - 4 - el) < 0) return NULL;
1301 s2n(TLSEXT_TYPE_use_srtp,ret);
1304 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1306 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1312 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1313 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1314 { const unsigned char cryptopro_ext[36] = {
1315 0xfd, 0xe8, /*65000*/
1316 0x00, 0x20, /*32 bytes length*/
1317 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1318 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1319 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1320 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1321 if (limit-ret<36) return NULL;
1322 memcpy(ret,cryptopro_ext,36);
1327 #ifndef OPENSSL_NO_HEARTBEATS
1328 /* Add Heartbeat extension if we've received one */
1329 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1331 s2n(TLSEXT_TYPE_heartbeat,ret);
1334 * 1: peer may send requests
1335 * 2: peer not allowed to send requests
1337 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1338 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1340 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1345 #ifndef OPENSSL_NO_NEXTPROTONEG
1346 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1347 s->s3->next_proto_neg_seen = 0;
1348 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1350 const unsigned char *npa;
1351 unsigned int npalen;
1354 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1355 if (r == SSL_TLSEXT_ERR_OK)
1357 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1358 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1360 memcpy(ret, npa, npalen);
1362 s->s3->next_proto_neg_seen = 1;
1367 /* If the client supports authz then see whether we have any to offer
1369 if (s->s3->tlsext_authz_client_types_len)
1371 size_t authz_length;
1372 /* By now we already know the new cipher, so we can look ahead
1373 * to see whether the cert we are going to send
1374 * has any authz data attached to it. */
1375 const unsigned char* authz = ssl_get_authz_data(s, &authz_length);
1376 const unsigned char* const orig_authz = authz;
1378 unsigned authz_count = 0;
1380 /* The authz data contains a number of the following structures:
1381 * uint8_t authz_type
1383 * uint8_t data[length]
1385 * First we walk over it to find the number of authz elements. */
1386 for (i = 0; i < authz_length; i++)
1388 unsigned short length;
1392 if (memchr(s->s3->tlsext_authz_client_types,
1394 s->s3->tlsext_authz_client_types_len) != NULL)
1398 /* n2s increments authz by 2 */
1406 /* Add TLS extension server_authz to the ServerHello message
1407 * 2 bytes for extension type
1408 * 2 bytes for extension length
1409 * 1 byte for the list length
1410 * n bytes for the list */
1411 const unsigned short ext_len = 1 + authz_count;
1413 if ((long)(limit - ret - 4 - ext_len) < 0) return NULL;
1414 s2n(TLSEXT_TYPE_server_authz, ret);
1416 *(ret++) = authz_count;
1417 s->s3->tlsext_authz_promised_to_client = 1;
1421 for (i = 0; i < authz_length; i++)
1423 unsigned short length;
1428 if (memchr(s->s3->tlsext_authz_client_types,
1430 s->s3->tlsext_authz_client_types_len) != NULL)
1433 /* n2s increments authz by 2 */
1440 if ((extdatalen = ret-p-2)== 0)
1447 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1449 unsigned short type;
1450 unsigned short size;
1452 unsigned char *data = *p;
1453 int renegotiate_seen = 0;
1456 s->servername_done = 0;
1457 s->tlsext_status_type = -1;
1458 #ifndef OPENSSL_NO_NEXTPROTONEG
1459 s->s3->next_proto_neg_seen = 0;
1462 #ifndef OPENSSL_NO_HEARTBEATS
1463 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1464 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1466 /* Clear any signature algorithms extension received */
1467 if (s->cert->peer_sigalgs)
1469 OPENSSL_free(s->cert->peer_sigalgs);
1470 s->cert->peer_sigalgs = NULL;
1472 /* Clear any shared sigtnature algorithms */
1473 if (s->cert->shared_sigalgs)
1475 OPENSSL_free(s->cert->shared_sigalgs);
1476 s->cert->shared_sigalgs = NULL;
1478 /* Clear certificate digests and validity flags */
1479 for (i = 0; i < SSL_PKEY_NUM; i++)
1481 s->cert->pkeys[i].digest = NULL;
1482 s->cert->pkeys[i].valid_flags = 0;
1485 if (data >= (d+n-2))
1489 if (data > (d+n-len))
1492 while (data <= (d+n-4))
1497 if (data+size > (d+n))
1500 fprintf(stderr,"Received extension type %d size %d\n",type,size);
1502 if (s->tlsext_debug_cb)
1503 s->tlsext_debug_cb(s, 0, type, data, size,
1504 s->tlsext_debug_arg);
1505 /* The servername extension is treated as follows:
1507 - Only the hostname type is supported with a maximum length of 255.
1508 - The servername is rejected if too long or if it contains zeros,
1509 in which case an fatal alert is generated.
1510 - The servername field is maintained together with the session cache.
1511 - When a session is resumed, the servername call back invoked in order
1512 to allow the application to position itself to the right context.
1513 - The servername is acknowledged if it is new for a session or when
1514 it is identical to a previously used for the same session.
1515 Applications can control the behaviour. They can at any time
1516 set a 'desirable' servername for a new SSL object. This can be the
1517 case for example with HTTPS when a Host: header field is received and
1518 a renegotiation is requested. In this case, a possible servername
1519 presented in the new client hello is only acknowledged if it matches
1520 the value of the Host: field.
1521 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1522 if they provide for changing an explicit servername context for the session,
1523 i.e. when the session has been established with a servername extension.
1524 - On session reconnect, the servername extension may be absent.
1528 if (type == TLSEXT_TYPE_server_name)
1530 unsigned char *sdata;
1536 *al = SSL_AD_DECODE_ERROR;
1543 *al = SSL_AD_DECODE_ERROR;
1550 servname_type = *(sdata++);
1556 *al = SSL_AD_DECODE_ERROR;
1559 if (s->servername_done == 0)
1560 switch (servname_type)
1562 case TLSEXT_NAMETYPE_host_name:
1565 if(s->session->tlsext_hostname)
1567 *al = SSL_AD_DECODE_ERROR;
1570 if (len > TLSEXT_MAXLEN_host_name)
1572 *al = TLS1_AD_UNRECOGNIZED_NAME;
1575 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
1577 *al = TLS1_AD_INTERNAL_ERROR;
1580 memcpy(s->session->tlsext_hostname, sdata, len);
1581 s->session->tlsext_hostname[len]='\0';
1582 if (strlen(s->session->tlsext_hostname) != len) {
1583 OPENSSL_free(s->session->tlsext_hostname);
1584 s->session->tlsext_hostname = NULL;
1585 *al = TLS1_AD_UNRECOGNIZED_NAME;
1588 s->servername_done = 1;
1592 s->servername_done = s->session->tlsext_hostname
1593 && strlen(s->session->tlsext_hostname) == len
1594 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
1606 *al = SSL_AD_DECODE_ERROR;
1611 #ifndef OPENSSL_NO_SRP
1612 else if (type == TLSEXT_TYPE_srp)
1614 if (size <= 0 || ((len = data[0])) != (size -1))
1616 *al = SSL_AD_DECODE_ERROR;
1619 if (s->srp_ctx.login != NULL)
1621 *al = SSL_AD_DECODE_ERROR;
1624 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
1626 memcpy(s->srp_ctx.login, &data[1], len);
1627 s->srp_ctx.login[len]='\0';
1629 if (strlen(s->srp_ctx.login) != len)
1631 *al = SSL_AD_DECODE_ERROR;
1637 #ifndef OPENSSL_NO_EC
1638 else if (type == TLSEXT_TYPE_ec_point_formats &&
1639 s->version != DTLS1_VERSION)
1641 unsigned char *sdata = data;
1642 int ecpointformatlist_length = *(sdata++);
1644 if (ecpointformatlist_length != size - 1)
1646 *al = TLS1_AD_DECODE_ERROR;
1651 if(s->session->tlsext_ecpointformatlist)
1653 OPENSSL_free(s->session->tlsext_ecpointformatlist);
1654 s->session->tlsext_ecpointformatlist = NULL;
1656 s->session->tlsext_ecpointformatlist_length = 0;
1657 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
1659 *al = TLS1_AD_INTERNAL_ERROR;
1662 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
1663 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
1666 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
1667 sdata = s->session->tlsext_ecpointformatlist;
1668 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1669 fprintf(stderr,"%i ",*(sdata++));
1670 fprintf(stderr,"\n");
1673 else if (type == TLSEXT_TYPE_elliptic_curves &&
1674 s->version != DTLS1_VERSION)
1676 unsigned char *sdata = data;
1677 int ellipticcurvelist_length = (*(sdata++) << 8);
1678 ellipticcurvelist_length += (*(sdata++));
1680 if (ellipticcurvelist_length != size - 2)
1682 *al = TLS1_AD_DECODE_ERROR;
1687 if(s->session->tlsext_ellipticcurvelist)
1689 *al = TLS1_AD_DECODE_ERROR;
1692 s->session->tlsext_ellipticcurvelist_length = 0;
1693 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
1695 *al = TLS1_AD_INTERNAL_ERROR;
1698 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
1699 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
1702 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
1703 sdata = s->session->tlsext_ellipticcurvelist;
1704 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
1705 fprintf(stderr,"%i ",*(sdata++));
1706 fprintf(stderr,"\n");
1709 #endif /* OPENSSL_NO_EC */
1710 #ifdef TLSEXT_TYPE_opaque_prf_input
1711 else if (type == TLSEXT_TYPE_opaque_prf_input &&
1712 s->version != DTLS1_VERSION)
1714 unsigned char *sdata = data;
1718 *al = SSL_AD_DECODE_ERROR;
1721 n2s(sdata, s->s3->client_opaque_prf_input_len);
1722 if (s->s3->client_opaque_prf_input_len != size - 2)
1724 *al = SSL_AD_DECODE_ERROR;
1728 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
1729 OPENSSL_free(s->s3->client_opaque_prf_input);
1730 if (s->s3->client_opaque_prf_input_len == 0)
1731 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
1733 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
1734 if (s->s3->client_opaque_prf_input == NULL)
1736 *al = TLS1_AD_INTERNAL_ERROR;
1741 else if (type == TLSEXT_TYPE_session_ticket)
1743 if (s->tls_session_ticket_ext_cb &&
1744 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
1746 *al = TLS1_AD_INTERNAL_ERROR;
1750 else if (type == TLSEXT_TYPE_renegotiate)
1752 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
1754 renegotiate_seen = 1;
1756 else if (type == TLSEXT_TYPE_signature_algorithms)
1759 if (s->cert->peer_sigalgs || size < 2)
1761 *al = SSL_AD_DECODE_ERROR;
1766 if (dsize != size || dsize & 1 || !dsize)
1768 *al = SSL_AD_DECODE_ERROR;
1771 if (!tls1_process_sigalgs(s, data, dsize))
1773 *al = SSL_AD_DECODE_ERROR;
1776 /* If sigalgs received and no shared algorithms fatal
1779 if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs)
1781 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
1782 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
1783 *al = SSL_AD_ILLEGAL_PARAMETER;
1787 else if (type == TLSEXT_TYPE_status_request &&
1788 s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb)
1793 *al = SSL_AD_DECODE_ERROR;
1797 s->tlsext_status_type = *data++;
1799 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1801 const unsigned char *sdata;
1803 /* Read in responder_id_list */
1808 *al = SSL_AD_DECODE_ERROR;
1817 *al = SSL_AD_DECODE_ERROR;
1821 dsize -= 2 + idsize;
1825 *al = SSL_AD_DECODE_ERROR;
1830 id = d2i_OCSP_RESPID(NULL,
1834 *al = SSL_AD_DECODE_ERROR;
1839 OCSP_RESPID_free(id);
1840 *al = SSL_AD_DECODE_ERROR;
1843 if (!s->tlsext_ocsp_ids
1844 && !(s->tlsext_ocsp_ids =
1845 sk_OCSP_RESPID_new_null()))
1847 OCSP_RESPID_free(id);
1848 *al = SSL_AD_INTERNAL_ERROR;
1851 if (!sk_OCSP_RESPID_push(
1852 s->tlsext_ocsp_ids, id))
1854 OCSP_RESPID_free(id);
1855 *al = SSL_AD_INTERNAL_ERROR;
1860 /* Read in request_extensions */
1863 *al = SSL_AD_DECODE_ERROR;
1870 *al = SSL_AD_DECODE_ERROR;
1876 if (s->tlsext_ocsp_exts)
1878 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
1879 X509_EXTENSION_free);
1882 s->tlsext_ocsp_exts =
1883 d2i_X509_EXTENSIONS(NULL,
1885 if (!s->tlsext_ocsp_exts
1886 || (data + dsize != sdata))
1888 *al = SSL_AD_DECODE_ERROR;
1893 /* We don't know what to do with any other type
1897 s->tlsext_status_type = -1;
1899 #ifndef OPENSSL_NO_HEARTBEATS
1900 else if (type == TLSEXT_TYPE_heartbeat)
1904 case 0x01: /* Client allows us to send HB requests */
1905 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1907 case 0x02: /* Client doesn't accept HB requests */
1908 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1909 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1911 default: *al = SSL_AD_ILLEGAL_PARAMETER;
1916 #ifndef OPENSSL_NO_NEXTPROTONEG
1917 else if (type == TLSEXT_TYPE_next_proto_neg &&
1918 s->s3->tmp.finish_md_len == 0)
1920 /* We shouldn't accept this extension on a
1923 * s->new_session will be set on renegotiation, but we
1924 * probably shouldn't rely that it couldn't be set on
1925 * the initial renegotation too in certain cases (when
1926 * there's some other reason to disallow resuming an
1927 * earlier session -- the current code won't be doing
1928 * anything like that, but this might change).
1930 * A valid sign that there's been a previous handshake
1931 * in this connection is if s->s3->tmp.finish_md_len >
1932 * 0. (We are talking about a check that will happen
1933 * in the Hello protocol round, well before a new
1934 * Finished message could have been computed.) */
1935 s->s3->next_proto_neg_seen = 1;
1939 /* session ticket processed earlier */
1940 else if (type == TLSEXT_TYPE_use_srtp)
1942 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
1947 else if (type == TLSEXT_TYPE_server_authz)
1949 unsigned char *sdata = data;
1950 unsigned char server_authz_dataformatlist_length;
1954 *al = TLS1_AD_DECODE_ERROR;
1958 server_authz_dataformatlist_length = *(sdata++);
1960 if (server_authz_dataformatlist_length != size - 1)
1962 *al = TLS1_AD_DECODE_ERROR;
1966 /* Successful session resumption uses the same authz
1967 * information as the original session so we ignore this
1968 * in the case of a session resumption. */
1971 if (s->s3->tlsext_authz_client_types != NULL)
1972 OPENSSL_free(s->s3->tlsext_authz_client_types);
1973 s->s3->tlsext_authz_client_types =
1974 OPENSSL_malloc(server_authz_dataformatlist_length);
1975 if (!s->s3->tlsext_authz_client_types)
1977 *al = TLS1_AD_INTERNAL_ERROR;
1981 s->s3->tlsext_authz_client_types_len =
1982 server_authz_dataformatlist_length;
1983 memcpy(s->s3->tlsext_authz_client_types,
1985 server_authz_dataformatlist_length);
1987 /* Sort the types in order to check for duplicates. */
1988 qsort(s->s3->tlsext_authz_client_types,
1989 server_authz_dataformatlist_length,
1990 1 /* element size */,
1993 for (i = 0; i < server_authz_dataformatlist_length; i++)
1996 s->s3->tlsext_authz_client_types[i] ==
1997 s->s3->tlsext_authz_client_types[i-1])
1999 *al = TLS1_AD_DECODE_ERROR;
2013 /* Need RI if renegotiating */
2015 if (!renegotiate_seen && s->renegotiate &&
2016 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2018 *al = SSL_AD_HANDSHAKE_FAILURE;
2019 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2020 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2023 /* If no signature algorithms extension set default values */
2024 if (!s->cert->peer_sigalgs)
2025 ssl_cert_set_default_md(s->cert);
2030 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2033 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2035 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2039 if (ssl_check_clienthello_tlsext(s) <= 0)
2041 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2047 #ifndef OPENSSL_NO_NEXTPROTONEG
2048 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2049 * elements of zero length are allowed and the set of elements must exactly fill
2050 * the length of the block. */
2051 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2053 unsigned int off = 0;
2067 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2069 unsigned short length;
2070 unsigned short type;
2071 unsigned short size;
2072 unsigned char *data = *p;
2073 int tlsext_servername = 0;
2074 int renegotiate_seen = 0;
2076 #ifndef OPENSSL_NO_NEXTPROTONEG
2077 s->s3->next_proto_neg_seen = 0;
2080 #ifndef OPENSSL_NO_HEARTBEATS
2081 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2082 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2085 if (data >= (d+n-2))
2089 if (data+length != d+n)
2091 *al = SSL_AD_DECODE_ERROR;
2095 while(data <= (d+n-4))
2100 if (data+size > (d+n))
2103 if (s->tlsext_debug_cb)
2104 s->tlsext_debug_cb(s, 1, type, data, size,
2105 s->tlsext_debug_arg);
2107 if (type == TLSEXT_TYPE_server_name)
2109 if (s->tlsext_hostname == NULL || size > 0)
2111 *al = TLS1_AD_UNRECOGNIZED_NAME;
2114 tlsext_servername = 1;
2117 #ifndef OPENSSL_NO_EC
2118 else if (type == TLSEXT_TYPE_ec_point_formats &&
2119 s->version != DTLS1_VERSION)
2121 unsigned char *sdata = data;
2122 int ecpointformatlist_length = *(sdata++);
2124 if (ecpointformatlist_length != size - 1)
2126 *al = TLS1_AD_DECODE_ERROR;
2129 s->session->tlsext_ecpointformatlist_length = 0;
2130 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2131 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2133 *al = TLS1_AD_INTERNAL_ERROR;
2136 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2137 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2139 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2140 sdata = s->session->tlsext_ecpointformatlist;
2141 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2142 fprintf(stderr,"%i ",*(sdata++));
2143 fprintf(stderr,"\n");
2146 #endif /* OPENSSL_NO_EC */
2148 else if (type == TLSEXT_TYPE_session_ticket)
2150 if (s->tls_session_ticket_ext_cb &&
2151 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2153 *al = TLS1_AD_INTERNAL_ERROR;
2156 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2159 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2162 s->tlsext_ticket_expected = 1;
2164 #ifdef TLSEXT_TYPE_opaque_prf_input
2165 else if (type == TLSEXT_TYPE_opaque_prf_input &&
2166 s->version != DTLS1_VERSION)
2168 unsigned char *sdata = data;
2172 *al = SSL_AD_DECODE_ERROR;
2175 n2s(sdata, s->s3->server_opaque_prf_input_len);
2176 if (s->s3->server_opaque_prf_input_len != size - 2)
2178 *al = SSL_AD_DECODE_ERROR;
2182 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2183 OPENSSL_free(s->s3->server_opaque_prf_input);
2184 if (s->s3->server_opaque_prf_input_len == 0)
2185 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2187 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2189 if (s->s3->server_opaque_prf_input == NULL)
2191 *al = TLS1_AD_INTERNAL_ERROR;
2196 else if (type == TLSEXT_TYPE_status_request &&
2197 s->version != DTLS1_VERSION)
2199 /* MUST be empty and only sent if we've requested
2200 * a status request message.
2202 if ((s->tlsext_status_type == -1) || (size > 0))
2204 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2207 /* Set flag to expect CertificateStatus message */
2208 s->tlsext_status_expected = 1;
2210 #ifndef OPENSSL_NO_NEXTPROTONEG
2211 else if (type == TLSEXT_TYPE_next_proto_neg &&
2212 s->s3->tmp.finish_md_len == 0)
2214 unsigned char *selected;
2215 unsigned char selected_len;
2217 /* We must have requested it. */
2218 if ((s->ctx->next_proto_select_cb == NULL))
2220 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2223 /* The data must be valid */
2224 if (!ssl_next_proto_validate(data, size))
2226 *al = TLS1_AD_DECODE_ERROR;
2229 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2231 *al = TLS1_AD_INTERNAL_ERROR;
2234 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2235 if (!s->next_proto_negotiated)
2237 *al = TLS1_AD_INTERNAL_ERROR;
2240 memcpy(s->next_proto_negotiated, selected, selected_len);
2241 s->next_proto_negotiated_len = selected_len;
2242 s->s3->next_proto_neg_seen = 1;
2245 else if (type == TLSEXT_TYPE_renegotiate)
2247 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2249 renegotiate_seen = 1;
2251 #ifndef OPENSSL_NO_HEARTBEATS
2252 else if (type == TLSEXT_TYPE_heartbeat)
2256 case 0x01: /* Server allows us to send HB requests */
2257 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2259 case 0x02: /* Server doesn't accept HB requests */
2260 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2261 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2263 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2268 else if (type == TLSEXT_TYPE_use_srtp)
2270 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2275 else if (type == TLSEXT_TYPE_server_authz)
2277 /* We only support audit proofs. It's an error to send
2278 * an authz hello extension if the client
2279 * didn't request a proof. */
2280 unsigned char *sdata = data;
2281 unsigned char server_authz_dataformatlist_length;
2283 if (!s->ctx->tlsext_authz_server_audit_proof_cb)
2285 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2291 *al = TLS1_AD_DECODE_ERROR;
2295 server_authz_dataformatlist_length = *(sdata++);
2296 if (server_authz_dataformatlist_length != size - 1)
2298 *al = TLS1_AD_DECODE_ERROR;
2302 /* We only support audit proofs, so a legal ServerHello
2303 * authz list contains exactly one entry. */
2304 if (server_authz_dataformatlist_length != 1 ||
2305 sdata[0] != TLSEXT_AUTHZDATAFORMAT_audit_proof)
2307 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2311 s->s3->tlsext_authz_server_promised = 1;
2319 *al = SSL_AD_DECODE_ERROR;
2323 if (!s->hit && tlsext_servername == 1)
2325 if (s->tlsext_hostname)
2327 if (s->session->tlsext_hostname == NULL)
2329 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2330 if (!s->session->tlsext_hostname)
2332 *al = SSL_AD_UNRECOGNIZED_NAME;
2338 *al = SSL_AD_DECODE_ERROR;
2348 /* Determine if we need to see RI. Strictly speaking if we want to
2349 * avoid an attack we should *always* see RI even on initial server
2350 * hello because the client doesn't see any renegotiation during an
2351 * attack. However this would mean we could not connect to any server
2352 * which doesn't support RI so for the immediate future tolerate RI
2353 * absence on initial connect only.
2355 if (!renegotiate_seen
2356 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2357 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2359 *al = SSL_AD_HANDSHAKE_FAILURE;
2360 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2361 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2369 int ssl_prepare_clienthello_tlsext(SSL *s)
2372 #ifdef TLSEXT_TYPE_opaque_prf_input
2376 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2378 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2383 if (s->tlsext_opaque_prf_input != NULL)
2385 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2386 OPENSSL_free(s->s3->client_opaque_prf_input);
2388 if (s->tlsext_opaque_prf_input_len == 0)
2389 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2391 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2392 if (s->s3->client_opaque_prf_input == NULL)
2394 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2397 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2401 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2402 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2409 int ssl_prepare_serverhello_tlsext(SSL *s)
2411 #ifndef OPENSSL_NO_EC
2412 /* If we are server and using an ECC cipher suite, send the point formats we support
2413 * if the client sent us an ECPointsFormat extension. Note that the server is not
2414 * supposed to send an EllipticCurves extension.
2417 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2418 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2419 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
2420 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
2424 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
2425 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
2427 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2430 s->tlsext_ecpointformatlist_length = 3;
2431 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
2432 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
2433 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
2435 #endif /* OPENSSL_NO_EC */
2440 static int ssl_check_clienthello_tlsext(SSL *s)
2442 int ret=SSL_TLSEXT_ERR_NOACK;
2443 int al = SSL_AD_UNRECOGNIZED_NAME;
2445 #ifndef OPENSSL_NO_EC
2446 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2447 * ssl3_choose_cipher in s3_lib.c.
2449 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2450 * ssl3_choose_cipher in s3_lib.c.
2454 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2455 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2456 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2457 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2459 /* If status request then ask callback what to do.
2460 * Note: this must be called after servername callbacks in case
2461 * the certificate has changed.
2463 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
2466 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2469 /* We don't want to send a status request response */
2470 case SSL_TLSEXT_ERR_NOACK:
2471 s->tlsext_status_expected = 0;
2473 /* status request response should be sent */
2474 case SSL_TLSEXT_ERR_OK:
2475 if (s->tlsext_ocsp_resp)
2476 s->tlsext_status_expected = 1;
2478 s->tlsext_status_expected = 0;
2480 /* something bad happened */
2481 case SSL_TLSEXT_ERR_ALERT_FATAL:
2482 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2483 al = SSL_AD_INTERNAL_ERROR;
2488 s->tlsext_status_expected = 0;
2490 #ifdef TLSEXT_TYPE_opaque_prf_input
2492 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2493 * but we might be sending an alert in response to the client hello,
2494 * so this has to happen here in ssl_check_clienthello_tlsext(). */
2498 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2500 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2503 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2504 al = SSL_AD_INTERNAL_ERROR;
2509 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2510 OPENSSL_free(s->s3->server_opaque_prf_input);
2511 s->s3->server_opaque_prf_input = NULL;
2513 if (s->tlsext_opaque_prf_input != NULL)
2515 if (s->s3->client_opaque_prf_input != NULL &&
2516 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2518 /* can only use this extension if we have a server opaque PRF input
2519 * of the same length as the client opaque PRF input! */
2521 if (s->tlsext_opaque_prf_input_len == 0)
2522 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2524 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2525 if (s->s3->server_opaque_prf_input == NULL)
2527 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2528 al = SSL_AD_INTERNAL_ERROR;
2531 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2535 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2537 /* The callback wants to enforce use of the extension,
2538 * but we can't do that with the client opaque PRF input;
2539 * abort the handshake.
2541 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2542 al = SSL_AD_HANDSHAKE_FAILURE;
2550 case SSL_TLSEXT_ERR_ALERT_FATAL:
2551 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2554 case SSL_TLSEXT_ERR_ALERT_WARNING:
2555 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2558 case SSL_TLSEXT_ERR_NOACK:
2559 s->servername_done=0;
2565 int ssl_check_serverhello_tlsext(SSL *s)
2567 int ret=SSL_TLSEXT_ERR_NOACK;
2568 int al = SSL_AD_UNRECOGNIZED_NAME;
2570 #ifndef OPENSSL_NO_EC
2571 /* If we are client and using an elliptic curve cryptography cipher
2572 * suite, then if server returns an EC point formats lists extension
2573 * it must contain uncompressed.
2575 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2576 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2577 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
2578 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
2579 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
2581 /* we are using an ECC cipher */
2583 unsigned char *list;
2584 int found_uncompressed = 0;
2585 list = s->session->tlsext_ecpointformatlist;
2586 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2588 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
2590 found_uncompressed = 1;
2594 if (!found_uncompressed)
2596 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2600 ret = SSL_TLSEXT_ERR_OK;
2601 #endif /* OPENSSL_NO_EC */
2603 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2604 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2605 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2606 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2608 #ifdef TLSEXT_TYPE_opaque_prf_input
2609 if (s->s3->server_opaque_prf_input_len > 0)
2611 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
2612 * So first verify that we really have a value from the server too. */
2614 if (s->s3->server_opaque_prf_input == NULL)
2616 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2617 al = SSL_AD_HANDSHAKE_FAILURE;
2620 /* Anytime the server *has* sent an opaque PRF input, we need to check
2621 * that we have a client opaque PRF input of the same size. */
2622 if (s->s3->client_opaque_prf_input == NULL ||
2623 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
2625 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2626 al = SSL_AD_ILLEGAL_PARAMETER;
2631 /* If we've requested certificate status and we wont get one
2634 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2635 && s->ctx && s->ctx->tlsext_status_cb)
2638 /* Set resp to NULL, resplen to -1 so callback knows
2639 * there is no response.
2641 if (s->tlsext_ocsp_resp)
2643 OPENSSL_free(s->tlsext_ocsp_resp);
2644 s->tlsext_ocsp_resp = NULL;
2646 s->tlsext_ocsp_resplen = -1;
2647 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2650 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2651 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2655 al = SSL_AD_INTERNAL_ERROR;
2656 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2662 case SSL_TLSEXT_ERR_ALERT_FATAL:
2663 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2666 case SSL_TLSEXT_ERR_ALERT_WARNING:
2667 ssl3_send_alert(s,SSL3_AL_WARNING,al);
2670 case SSL_TLSEXT_ERR_NOACK:
2671 s->servername_done=0;
2677 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2680 if (s->version < SSL3_VERSION)
2682 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0)
2684 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2688 if (ssl_check_serverhello_tlsext(s) <= 0)
2690 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,SSL_R_SERVERHELLO_TLSEXT);
2696 /* Since the server cache lookup is done early on in the processing of the
2697 * ClientHello, and other operations depend on the result, we need to handle
2698 * any TLS session ticket extension at the same time.
2700 * session_id: points at the session ID in the ClientHello. This code will
2701 * read past the end of this in order to parse out the session ticket
2702 * extension, if any.
2703 * len: the length of the session ID.
2704 * limit: a pointer to the first byte after the ClientHello.
2705 * ret: (output) on return, if a ticket was decrypted, then this is set to
2706 * point to the resulting session.
2708 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2709 * ciphersuite, in which case we have no use for session tickets and one will
2710 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2713 * -1: fatal error, either from parsing or decrypting the ticket.
2714 * 0: no ticket was found (or was ignored, based on settings).
2715 * 1: a zero length extension was found, indicating that the client supports
2716 * session tickets but doesn't currently have one to offer.
2717 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2718 * couldn't be decrypted because of a non-fatal error.
2719 * 3: a ticket was successfully decrypted and *ret was set.
2722 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2723 * a new session ticket to the client because the client indicated support
2724 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2725 * a session ticket or we couldn't use the one it gave us, or if
2726 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2727 * Otherwise, s->tlsext_ticket_expected is set to 0.
2729 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
2730 const unsigned char *limit, SSL_SESSION **ret)
2732 /* Point after session ID in client hello */
2733 const unsigned char *p = session_id + len;
2737 s->tlsext_ticket_expected = 0;
2739 /* If tickets disabled behave as if no ticket present
2740 * to permit stateful resumption.
2742 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2744 if ((s->version <= SSL3_VERSION) || !limit)
2748 /* Skip past DTLS cookie */
2749 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
2756 /* Skip past cipher list */
2761 /* Skip past compression algorithm list */
2766 /* Now at start of extensions */
2767 if ((p + 2) >= limit)
2770 while ((p + 4) <= limit)
2772 unsigned short type, size;
2775 if (p + size > limit)
2777 if (type == TLSEXT_TYPE_session_ticket)
2782 /* The client will accept a ticket but doesn't
2783 * currently have one. */
2784 s->tlsext_ticket_expected = 1;
2787 if (s->tls_session_secret_cb)
2789 /* Indicate that the ticket couldn't be
2790 * decrypted rather than generating the session
2791 * from ticket now, trigger abbreviated
2792 * handshake based on external mechanism to
2793 * calculate the master secret later. */
2796 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
2799 case 2: /* ticket couldn't be decrypted */
2800 s->tlsext_ticket_expected = 1;
2802 case 3: /* ticket was decrypted */
2804 case 4: /* ticket decrypted but need to renew */
2805 s->tlsext_ticket_expected = 1;
2807 default: /* fatal error */
2816 /* tls_decrypt_ticket attempts to decrypt a session ticket.
2818 * etick: points to the body of the session ticket extension.
2819 * eticklen: the length of the session tickets extenion.
2820 * sess_id: points at the session ID.
2821 * sesslen: the length of the session ID.
2822 * psess: (output) on return, if a ticket was decrypted, then this is set to
2823 * point to the resulting session.
2826 * -1: fatal error, either from parsing or decrypting the ticket.
2827 * 2: the ticket couldn't be decrypted.
2828 * 3: a ticket was successfully decrypted and *psess was set.
2829 * 4: same as 3, but the ticket needs to be renewed.
2831 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
2832 const unsigned char *sess_id, int sesslen,
2833 SSL_SESSION **psess)
2836 unsigned char *sdec;
2837 const unsigned char *p;
2838 int slen, mlen, renew_ticket = 0;
2839 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2842 SSL_CTX *tctx = s->initial_ctx;
2843 /* Need at least keyname + iv + some encrypted data */
2846 /* Initialize session ticket encryption and HMAC contexts */
2847 HMAC_CTX_init(&hctx);
2848 EVP_CIPHER_CTX_init(&ctx);
2849 if (tctx->tlsext_ticket_key_cb)
2851 unsigned char *nctick = (unsigned char *)etick;
2852 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2863 /* Check key name matches */
2864 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2866 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2867 tlsext_tick_md(), NULL);
2868 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2869 tctx->tlsext_tick_aes_key, etick + 16);
2871 /* Attempt to process session ticket, first conduct sanity and
2872 * integrity checks on ticket.
2874 mlen = HMAC_size(&hctx);
2877 EVP_CIPHER_CTX_cleanup(&ctx);
2881 /* Check HMAC of encrypted ticket */
2882 HMAC_Update(&hctx, etick, eticklen);
2883 HMAC_Final(&hctx, tick_hmac, NULL);
2884 HMAC_CTX_cleanup(&hctx);
2885 if (memcmp(tick_hmac, etick + eticklen, mlen))
2887 /* Attempt to decrypt session data */
2888 /* Move p after IV to start of encrypted ticket, update length */
2889 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2890 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2891 sdec = OPENSSL_malloc(eticklen);
2894 EVP_CIPHER_CTX_cleanup(&ctx);
2897 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2898 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
2901 EVP_CIPHER_CTX_cleanup(&ctx);
2904 sess = d2i_SSL_SESSION(NULL, &p, slen);
2908 /* The session ID, if non-empty, is used by some clients to
2909 * detect that the ticket has been accepted. So we copy it to
2910 * the session structure. If it is empty set length to zero
2911 * as required by standard.
2914 memcpy(sess->session_id, sess_id, sesslen);
2915 sess->session_id_length = sesslen;
2923 /* For session parse failure, indicate that we need to send a new
2928 /* Tables to translate from NIDs to TLS v1.2 ids */
2936 static tls12_lookup tls12_md[] = {
2937 {NID_md5, TLSEXT_hash_md5},
2938 {NID_sha1, TLSEXT_hash_sha1},
2939 {NID_sha224, TLSEXT_hash_sha224},
2940 {NID_sha256, TLSEXT_hash_sha256},
2941 {NID_sha384, TLSEXT_hash_sha384},
2942 {NID_sha512, TLSEXT_hash_sha512}
2945 static tls12_lookup tls12_sig[] = {
2946 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
2947 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
2948 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2951 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
2954 for (i = 0; i < tlen; i++)
2956 if (table[i].nid == nid)
2962 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
2965 for (i = 0; i < tlen; i++)
2967 if ((table[i].id) == id)
2968 return table[i].nid;
2973 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
2978 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2979 sizeof(tls12_md)/sizeof(tls12_lookup));
2982 sig_id = tls12_get_sigid(pk);
2985 p[0] = (unsigned char)md_id;
2986 p[1] = (unsigned char)sig_id;
2990 int tls12_get_sigid(const EVP_PKEY *pk)
2992 return tls12_find_id(pk->type, tls12_sig,
2993 sizeof(tls12_sig)/sizeof(tls12_lookup));
2996 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3000 #ifndef OPENSSL_NO_MD5
3001 case TLSEXT_hash_md5:
3008 #ifndef OPENSSL_NO_SHA
3009 case TLSEXT_hash_sha1:
3012 #ifndef OPENSSL_NO_SHA256
3013 case TLSEXT_hash_sha224:
3014 return EVP_sha224();
3016 case TLSEXT_hash_sha256:
3017 return EVP_sha256();
3019 #ifndef OPENSSL_NO_SHA512
3020 case TLSEXT_hash_sha384:
3021 return EVP_sha384();
3023 case TLSEXT_hash_sha512:
3024 return EVP_sha512();
3032 static int tls12_get_pkey_idx(unsigned char sig_alg)
3036 #ifndef OPENSSL_NO_RSA
3037 case TLSEXT_signature_rsa:
3038 return SSL_PKEY_RSA_SIGN;
3040 #ifndef OPENSSL_NO_DSA
3041 case TLSEXT_signature_dsa:
3042 return SSL_PKEY_DSA_SIGN;
3044 #ifndef OPENSSL_NO_ECDSA
3045 case TLSEXT_signature_ecdsa:
3046 return SSL_PKEY_ECC;
3052 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3053 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3054 int *psignhash_nid, const unsigned char *data)
3056 int sign_nid = 0, hash_nid = 0;
3057 if (!phash_nid && !psign_nid && !psignhash_nid)
3059 if (phash_nid || psignhash_nid)
3061 hash_nid = tls12_find_nid(data[0], tls12_md,
3062 sizeof(tls12_md)/sizeof(tls12_lookup));
3064 *phash_nid = hash_nid;
3066 if (psign_nid || psignhash_nid)
3068 sign_nid = tls12_find_nid(data[1], tls12_sig,
3069 sizeof(tls12_sig)/sizeof(tls12_lookup));
3071 *psign_nid = sign_nid;
3075 if (sign_nid && hash_nid)
3076 OBJ_find_sigid_by_algs(psignhash_nid,
3077 hash_nid, sign_nid);
3079 *psignhash_nid = NID_undef;
3082 /* Given preference and allowed sigalgs set shared sigalgs */
3083 static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
3084 const unsigned char *pref, size_t preflen,
3085 const unsigned char *allow, size_t allowlen)
3087 const unsigned char *ptmp, *atmp;
3088 size_t i, j, nmatch = 0;
3089 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
3091 /* Skip disabled hashes or signature algorithms */
3092 if (tls12_get_hash(ptmp[0]) == NULL)
3094 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3096 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
3098 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
3103 shsig->rhash = ptmp[0];
3104 shsig->rsign = ptmp[1];
3105 tls1_lookup_sigalg(&shsig->hash_nid,
3107 &shsig->signandhash_nid,
3118 /* Set shared signature algorithms for SSL structures */
3119 static int tls1_set_shared_sigalgs(SSL *s)
3121 const unsigned char *pref, *allow, *conf;
3122 size_t preflen, allowlen, conflen;
3124 TLS_SIGALGS *salgs = NULL;
3126 /* If client use client signature algorithms if not NULL */
3127 if (!s->server && c->client_sigalgs)
3129 conf = c->client_sigalgs;
3130 conflen = c->client_sigalgslen;
3132 else if (c->conf_sigalgs)
3134 conf = c->conf_sigalgs;
3135 conflen = c->conf_sigalgslen;
3139 conf = tls12_sigalgs;
3140 conflen = sizeof(tls12_sigalgs);
3146 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
3150 allow = c->peer_sigalgs;
3151 allowlen = c->peer_sigalgslen;
3157 pref = c->peer_sigalgs;
3158 preflen = c->peer_sigalgslen;
3160 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
3163 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3166 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
3167 c->shared_sigalgs = salgs;
3168 c->shared_sigalgslen = nmatch;
3173 /* Set preferred digest for each key type */
3175 int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
3181 TLS_SIGALGS *sigptr;
3182 /* Extension ignored for TLS versions below 1.2 */
3183 if (TLS1_get_version(s) < TLS1_2_VERSION)
3185 /* Should never happen */
3189 c->peer_sigalgs = OPENSSL_malloc(dsize);
3190 if (!c->peer_sigalgs)
3192 c->peer_sigalgslen = dsize;
3193 memcpy(c->peer_sigalgs, data, dsize);
3195 tls1_set_shared_sigalgs(s);
3197 for (i = 0, sigptr = c->shared_sigalgs;
3198 i < c->shared_sigalgslen; i++, sigptr++)
3200 idx = tls12_get_pkey_idx(sigptr->rsign);
3201 if (idx > 0 && c->pkeys[idx].digest == NULL)
3203 md = tls12_get_hash(sigptr->rhash);
3204 c->pkeys[idx].digest = md;
3205 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3206 if (idx == SSL_PKEY_RSA_SIGN)
3208 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3209 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3214 /* In strict mode leave unset digests as NULL to indicate we can't
3215 * use the certificate for signing.
3217 if (!(s->cert->cert_flags & SSL_CERT_FLAG_TLS_STRICT))
3219 /* Set any remaining keys to default values. NOTE: if alg is
3220 * not supported it stays as NULL.
3222 #ifndef OPENSSL_NO_DSA
3223 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
3224 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
3226 #ifndef OPENSSL_NO_RSA
3227 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
3229 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
3230 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
3233 #ifndef OPENSSL_NO_ECDSA
3234 if (!c->pkeys[SSL_PKEY_ECC].digest)
3235 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
3242 int SSL_get_sigalgs(SSL *s, int idx,
3243 int *psign, int *phash, int *psignhash,
3244 unsigned char *rsig, unsigned char *rhash)
3246 const unsigned char *psig = s->cert->peer_sigalgs;
3252 if (idx >= (int)s->cert->peer_sigalgslen)
3259 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3261 return s->cert->peer_sigalgslen / 2;
3264 int SSL_get_shared_sigalgs(SSL *s, int idx,
3265 int *psign, int *phash, int *psignhash,
3266 unsigned char *rsig, unsigned char *rhash)
3268 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3269 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3273 *phash = shsigalgs->hash_nid;
3275 *psign = shsigalgs->sign_nid;
3277 *psignhash = shsigalgs->signandhash_nid;
3279 *rsig = shsigalgs->rsign;
3281 *rhash = shsigalgs->rhash;
3282 return s->cert->shared_sigalgslen;
3286 #ifndef OPENSSL_NO_HEARTBEATS
3288 tls1_process_heartbeat(SSL *s)
3290 unsigned char *p = &s->s3->rrec.data[0], *pl;
3291 unsigned short hbtype;
3292 unsigned int payload;
3293 unsigned int padding = 16; /* Use minimum padding */
3295 /* Read type and payload length first */
3300 if (s->msg_callback)
3301 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
3302 &s->s3->rrec.data[0], s->s3->rrec.length,
3303 s, s->msg_callback_arg);
3305 if (hbtype == TLS1_HB_REQUEST)
3307 unsigned char *buffer, *bp;
3310 /* Allocate memory for the response, size is 1 bytes
3311 * message type, plus 2 bytes payload length, plus
3312 * payload, plus padding
3314 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
3317 /* Enter response type, length and copy payload */
3318 *bp++ = TLS1_HB_RESPONSE;
3320 memcpy(bp, pl, payload);
3322 /* Random padding */
3323 RAND_pseudo_bytes(bp, padding);
3325 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
3327 if (r >= 0 && s->msg_callback)
3328 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3329 buffer, 3 + payload + padding,
3330 s, s->msg_callback_arg);
3332 OPENSSL_free(buffer);
3337 else if (hbtype == TLS1_HB_RESPONSE)
3341 /* We only send sequence numbers (2 bytes unsigned int),
3342 * and 16 random bytes, so we just try to read the
3343 * sequence number */
3346 if (payload == 18 && seq == s->tlsext_hb_seq)
3349 s->tlsext_hb_pending = 0;
3357 tls1_heartbeat(SSL *s)
3359 unsigned char *buf, *p;
3361 unsigned int payload = 18; /* Sequence number + random bytes */
3362 unsigned int padding = 16; /* Use minimum padding */
3364 /* Only send if peer supports and accepts HB requests... */
3365 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
3366 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
3368 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
3372 /* ...and there is none in flight yet... */
3373 if (s->tlsext_hb_pending)
3375 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING);
3379 /* ...and no handshake in progress. */
3380 if (SSL_in_init(s) || s->in_handshake)
3382 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE);
3386 /* Check if padding is too long, payload and padding
3387 * must not exceed 2^14 - 3 = 16381 bytes in total.
3389 OPENSSL_assert(payload + padding <= 16381);
3391 /* Create HeartBeat message, we just use a sequence number
3392 * as payload to distuingish different messages and add
3393 * some random stuff.
3394 * - Message Type, 1 byte
3395 * - Payload Length, 2 bytes (unsigned int)
3396 * - Payload, the sequence number (2 bytes uint)
3397 * - Payload, random bytes (16 bytes uint)
3400 buf = OPENSSL_malloc(1 + 2 + payload + padding);
3403 *p++ = TLS1_HB_REQUEST;
3404 /* Payload length (18 bytes here) */
3406 /* Sequence number */
3407 s2n(s->tlsext_hb_seq, p);
3408 /* 16 random bytes */
3409 RAND_pseudo_bytes(p, 16);
3411 /* Random padding */
3412 RAND_pseudo_bytes(p, padding);
3414 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
3417 if (s->msg_callback)
3418 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3419 buf, 3 + payload + padding,
3420 s, s->msg_callback_arg);
3422 s->tlsext_hb_pending = 1;
3431 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3436 int sigalgs[MAX_SIGALGLEN];
3439 static int sig_cb(const char *elem, int len, void *arg)
3441 sig_cb_st *sarg = arg;
3444 int sig_alg, hash_alg;
3445 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3447 if (len > (int)(sizeof(etmp) - 1))
3449 memcpy(etmp, elem, len);
3451 p = strchr(etmp, '+');
3459 if (!strcmp(etmp, "RSA"))
3460 sig_alg = EVP_PKEY_RSA;
3461 else if (!strcmp(etmp, "DSA"))
3462 sig_alg = EVP_PKEY_DSA;
3463 else if (!strcmp(etmp, "ECDSA"))
3464 sig_alg = EVP_PKEY_EC;
3467 hash_alg = OBJ_sn2nid(p);
3468 if (hash_alg == NID_undef)
3469 hash_alg = OBJ_ln2nid(p);
3470 if (hash_alg == NID_undef)
3473 for (i = 0; i < sarg->sigalgcnt; i+=2)
3475 if (sarg->sigalgs[i] == sig_alg
3476 && sarg->sigalgs[i + 1] == hash_alg)
3479 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3480 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3484 /* Set suppored signature algorithms based on a colon separated list
3485 * of the form sig+hash e.g. RSA+SHA512:DSA+SHA512 */
3486 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3490 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3492 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3495 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3497 unsigned char *sigalgs, *sptr;
3502 sigalgs = OPENSSL_malloc(salglen);
3503 if (sigalgs == NULL)
3505 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
3507 rhash = tls12_find_id(*psig_nids++, tls12_md,
3508 sizeof(tls12_md)/sizeof(tls12_lookup));
3509 rsign = tls12_find_id(*psig_nids++, tls12_sig,
3510 sizeof(tls12_sig)/sizeof(tls12_lookup));
3512 if (rhash == -1 || rsign == -1)
3520 if (c->client_sigalgs)
3521 OPENSSL_free(c->client_sigalgs);
3522 c->client_sigalgs = sigalgs;
3523 c->client_sigalgslen = salglen;
3527 if (c->conf_sigalgs)
3528 OPENSSL_free(c->conf_sigalgs);
3529 c->conf_sigalgs = sigalgs;
3530 c->conf_sigalgslen = salglen;
3536 OPENSSL_free(sigalgs);
3540 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3544 if (default_nid == -1)
3546 sig_nid = X509_get_signature_nid(x);
3548 return sig_nid == default_nid ? 1 : 0;
3549 for (i = 0; i < c->shared_sigalgslen; i++)
3550 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3554 /* Check to see if a certificate issuer name matches list of CA names */
3555 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3559 nm = X509_get_issuer_name(x);
3560 for (i = 0; i < sk_X509_NAME_num(names); i++)
3562 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3568 /* Check certificate chain is consistent with TLS extensions and is
3569 * usable by server. This servers two purposes: it allows users to
3570 * check chains before passing them to the server and it allows the
3571 * server to check chains before attempting to use them.
3574 /* Flags which need to be set for a certificate when stict mode not set */
3576 #define CERT_PKEY_VALID_FLAGS \
3577 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3578 /* Strict mode flags */
3579 #define CERT_PKEY_STRICT_FLAGS \
3580 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3581 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3583 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3588 int check_flags = 0, strict_mode;
3589 CERT_PKEY *cpk = NULL;
3591 /* idx != -1 means checking server chains */
3594 cpk = c->pkeys + idx;
3596 pk = cpk->privatekey;
3598 strict_mode = c->cert_flags & SSL_CERT_FLAG_TLS_STRICT;
3599 /* If no cert or key, forget it */
3607 idx = ssl_cert_type(x, pk);
3610 cpk = c->pkeys + idx;
3611 if (c->cert_flags & SSL_CERT_FLAG_TLS_STRICT)
3612 check_flags = CERT_PKEY_STRICT_FLAGS;
3614 check_flags = CERT_PKEY_VALID_FLAGS;
3619 /* Check all signature algorithms are consistent with
3620 * signature algorithms extension if TLS 1.2 or later
3623 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
3626 unsigned char rsign = 0;
3627 if (c->peer_sigalgs)
3629 /* If no sigalgs extension use defaults from RFC5246 */
3634 case SSL_PKEY_RSA_ENC:
3635 case SSL_PKEY_RSA_SIGN:
3636 case SSL_PKEY_DH_RSA:
3637 rsign = TLSEXT_signature_rsa;
3638 default_nid = NID_sha1WithRSAEncryption;
3641 case SSL_PKEY_DSA_SIGN:
3642 case SSL_PKEY_DH_DSA:
3643 rsign = TLSEXT_signature_dsa;
3644 default_nid = NID_dsaWithSHA1;
3648 rsign = TLSEXT_signature_ecdsa;
3649 default_nid = NID_ecdsa_with_SHA1;
3657 /* If peer sent no signature algorithms extension and we
3658 * have set preferred signature algorithms check we support
3661 if (default_nid > 0 && c->conf_sigalgs)
3664 const unsigned char *p = c->conf_sigalgs;
3665 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
3667 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3670 if (j == c->conf_sigalgslen)
3678 /* Check signature algorithm of each cert in chain */
3679 if (!tls1_check_sig_alg(c, x, default_nid))
3681 if (!check_flags) goto end;
3684 rv |= CERT_PKEY_EE_SIGNATURE;
3685 rv |= CERT_PKEY_CA_SIGNATURE;
3686 for (i = 0; i < sk_X509_num(chain); i++)
3688 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
3693 rv &= ~CERT_PKEY_CA_SIGNATURE;
3701 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3702 else if(check_flags)
3703 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
3705 /* Check cert parameters are consistent: server certs only */
3706 if (!s->server || tls1_check_cert_param(s, x))
3707 rv |= CERT_PKEY_EE_PARAM;
3708 else if (!check_flags)
3711 rv |= CERT_PKEY_CA_PARAM;
3712 /* In strict mode check rest of chain too */
3713 else if (strict_mode)
3715 rv |= CERT_PKEY_CA_PARAM;
3716 for (i = 0; i < sk_X509_num(chain); i++)
3718 if (!tls1_check_cert_param(s, sk_X509_value(chain, i)))
3722 rv &= ~CERT_PKEY_CA_PARAM;
3730 if (!s->server && strict_mode)
3732 STACK_OF(X509_NAME) *ca_dn;
3737 check_type = TLS_CT_RSA_SIGN;
3740 check_type = TLS_CT_DSS_SIGN;
3743 check_type = TLS_CT_ECDSA_SIGN;
3748 int cert_type = X509_certificate_type(x, pk);
3749 if (cert_type & EVP_PKS_RSA)
3750 check_type = TLS_CT_RSA_FIXED_DH;
3751 if (cert_type & EVP_PKS_DSA)
3752 check_type = TLS_CT_DSS_FIXED_DH;
3757 const unsigned char *ctypes;
3762 ctypelen = (int)c->ctype_num;
3766 ctypes = (unsigned char *)s->s3->tmp.ctype;
3767 ctypelen = s->s3->tmp.ctype_num;
3769 for (i = 0; i < ctypelen; i++)
3771 if (ctypes[i] == check_type)
3773 rv |= CERT_PKEY_CERT_TYPE;
3777 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3781 rv |= CERT_PKEY_CERT_TYPE;
3784 ca_dn = s->s3->tmp.ca_names;
3786 if (!sk_X509_NAME_num(ca_dn))
3787 rv |= CERT_PKEY_ISSUER_NAME;
3789 if (!(rv & CERT_PKEY_ISSUER_NAME))
3791 if (ssl_check_ca_name(ca_dn, x))
3792 rv |= CERT_PKEY_ISSUER_NAME;
3794 if (!(rv & CERT_PKEY_ISSUER_NAME))
3796 for (i = 0; i < sk_X509_num(chain); i++)
3798 X509 *xtmp = sk_X509_value(chain, i);
3799 if (ssl_check_ca_name(ca_dn, xtmp))
3801 rv |= CERT_PKEY_ISSUER_NAME;
3806 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3810 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
3812 if (!check_flags || (rv & check_flags) == check_flags)
3813 rv |= CERT_PKEY_VALID;
3817 if (TLS1_get_version(s) >= TLS1_2_VERSION)
3819 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
3820 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
3821 else if (cpk->digest)
3822 rv |= CERT_PKEY_SIGN;
3825 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
3827 /* When checking a CERT_PKEY structure all flags are irrelevant
3828 * if the chain is invalid.
3832 if (rv & CERT_PKEY_VALID)
3833 cpk->valid_flags = rv;
3835 cpk->valid_flags = 0;
3840 /* Set validity of certificates in an SSL structure */
3841 void tls1_set_cert_validity(SSL *s)
3843 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3844 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3845 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3846 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
3847 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
3848 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3850 /* User level utiity function to check a chain is suitable */
3851 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3853 return tls1_check_chain(s, x, pk, chain, -1);