2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include <openssl/ocsp.h>
117 #include <openssl/rand.h>
118 #ifndef OPENSSL_NO_DH
119 #include <openssl/dh.h>
120 #include <openssl/bn.h>
122 #include "ssl_locl.h"
124 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
126 #ifndef OPENSSL_NO_TLSEXT
127 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
128 const unsigned char *sess_id, int sesslen,
129 SSL_SESSION **psess);
130 static int ssl_check_clienthello_tlsext_early(SSL *s);
131 int ssl_check_serverhello_tlsext(SSL *s);
134 SSL3_ENC_METHOD const TLSv1_enc_data={
137 tls1_setup_key_block,
138 tls1_generate_master_secret,
139 tls1_change_cipher_state,
140 tls1_final_finish_mac,
141 TLS1_FINISH_MAC_LENGTH,
142 tls1_cert_verify_mac,
143 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
144 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
146 tls1_export_keying_material,
148 SSL3_HM_HEADER_LENGTH,
149 ssl3_set_handshake_header,
153 SSL3_ENC_METHOD const TLSv1_1_enc_data={
156 tls1_setup_key_block,
157 tls1_generate_master_secret,
158 tls1_change_cipher_state,
159 tls1_final_finish_mac,
160 TLS1_FINISH_MAC_LENGTH,
161 tls1_cert_verify_mac,
162 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
163 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
165 tls1_export_keying_material,
166 SSL_ENC_FLAG_EXPLICIT_IV,
167 SSL3_HM_HEADER_LENGTH,
168 ssl3_set_handshake_header,
172 SSL3_ENC_METHOD const TLSv1_2_enc_data={
175 tls1_setup_key_block,
176 tls1_generate_master_secret,
177 tls1_change_cipher_state,
178 tls1_final_finish_mac,
179 TLS1_FINISH_MAC_LENGTH,
180 tls1_cert_verify_mac,
181 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
182 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
184 tls1_export_keying_material,
185 SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF
186 |SSL_ENC_FLAG_TLS1_2_CIPHERS,
187 SSL3_HM_HEADER_LENGTH,
188 ssl3_set_handshake_header,
192 long tls1_default_timeout(void)
194 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
195 * is way too long for http, the cache would over fill */
201 if (!ssl3_new(s)) return(0);
202 s->method->ssl_clear(s);
206 void tls1_free(SSL *s)
208 #ifndef OPENSSL_NO_TLSEXT
209 if (s->tlsext_session_ticket)
211 OPENSSL_free(s->tlsext_session_ticket);
213 #endif /* OPENSSL_NO_TLSEXT */
217 void tls1_clear(SSL *s)
220 s->version = s->method->version;
223 #ifndef OPENSSL_NO_EC
227 int nid; /* Curve NID */
228 int secbits; /* Bits of security (from SP800-57) */
229 unsigned int flags; /* Flags: currently just field type */
232 #define TLS_CURVE_CHAR2 0x1
233 #define TLS_CURVE_PRIME 0x0
235 static const tls_curve_info nid_list[] =
237 {NID_sect163k1, 80, TLS_CURVE_CHAR2},/* sect163k1 (1) */
238 {NID_sect163r1, 80, TLS_CURVE_CHAR2},/* sect163r1 (2) */
239 {NID_sect163r2, 80, TLS_CURVE_CHAR2},/* sect163r2 (3) */
240 {NID_sect193r1, 80, TLS_CURVE_CHAR2},/* sect193r1 (4) */
241 {NID_sect193r2, 80, TLS_CURVE_CHAR2},/* sect193r2 (5) */
242 {NID_sect233k1, 112, TLS_CURVE_CHAR2},/* sect233k1 (6) */
243 {NID_sect233r1, 112, TLS_CURVE_CHAR2},/* sect233r1 (7) */
244 {NID_sect239k1, 112, TLS_CURVE_CHAR2},/* sect239k1 (8) */
245 {NID_sect283k1, 128, TLS_CURVE_CHAR2},/* sect283k1 (9) */
246 {NID_sect283r1, 128, TLS_CURVE_CHAR2},/* sect283r1 (10) */
247 {NID_sect409k1, 192, TLS_CURVE_CHAR2},/* sect409k1 (11) */
248 {NID_sect409r1, 192, TLS_CURVE_CHAR2},/* sect409r1 (12) */
249 {NID_sect571k1, 256, TLS_CURVE_CHAR2},/* sect571k1 (13) */
250 {NID_sect571r1, 256, TLS_CURVE_CHAR2},/* sect571r1 (14) */
251 {NID_secp160k1, 80, TLS_CURVE_PRIME},/* secp160k1 (15) */
252 {NID_secp160r1, 80, TLS_CURVE_PRIME},/* secp160r1 (16) */
253 {NID_secp160r2, 80, TLS_CURVE_PRIME},/* secp160r2 (17) */
254 {NID_secp192k1, 80, TLS_CURVE_PRIME},/* secp192k1 (18) */
255 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME},/* secp192r1 (19) */
256 {NID_secp224k1, 112, TLS_CURVE_PRIME},/* secp224k1 (20) */
257 {NID_secp224r1, 112, TLS_CURVE_PRIME},/* secp224r1 (21) */
258 {NID_secp256k1, 128, TLS_CURVE_PRIME},/* secp256k1 (22) */
259 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME},/* secp256r1 (23) */
260 {NID_secp384r1, 192, TLS_CURVE_PRIME},/* secp384r1 (24) */
261 {NID_secp521r1, 256, TLS_CURVE_PRIME},/* secp521r1 (25) */
262 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
263 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
264 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME},/* brainpool512r1 (28) */
268 static const unsigned char ecformats_default[] =
270 TLSEXT_ECPOINTFORMAT_uncompressed,
271 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
272 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
275 static const unsigned char eccurves_default[] =
277 0,14, /* sect571r1 (14) */
278 0,13, /* sect571k1 (13) */
279 0,25, /* secp521r1 (25) */
280 0,28, /* brainpool512r1 (28) */
281 0,11, /* sect409k1 (11) */
282 0,12, /* sect409r1 (12) */
283 0,27, /* brainpoolP384r1 (27) */
284 0,24, /* secp384r1 (24) */
285 0,9, /* sect283k1 (9) */
286 0,10, /* sect283r1 (10) */
287 0,26, /* brainpoolP256r1 (26) */
288 0,22, /* secp256k1 (22) */
289 0,23, /* secp256r1 (23) */
290 0,8, /* sect239k1 (8) */
291 0,6, /* sect233k1 (6) */
292 0,7, /* sect233r1 (7) */
293 0,20, /* secp224k1 (20) */
294 0,21, /* secp224r1 (21) */
295 0,4, /* sect193r1 (4) */
296 0,5, /* sect193r2 (5) */
297 0,18, /* secp192k1 (18) */
298 0,19, /* secp192r1 (19) */
299 0,1, /* sect163k1 (1) */
300 0,2, /* sect163r1 (2) */
301 0,3, /* sect163r2 (3) */
302 0,15, /* secp160k1 (15) */
303 0,16, /* secp160r1 (16) */
304 0,17, /* secp160r2 (17) */
307 static const unsigned char suiteb_curves[] =
309 0, TLSEXT_curve_P_256,
310 0, TLSEXT_curve_P_384
313 int tls1_ec_curve_id2nid(int curve_id)
315 /* ECC curves from RFC 4492 and RFC 7027 */
316 if ((curve_id < 1) || ((unsigned int)curve_id >
317 sizeof(nid_list)/sizeof(nid_list[0])))
319 return nid_list[curve_id-1].nid;
322 int tls1_ec_nid2curve_id(int nid)
324 /* ECC curves from RFC 4492 and RFC 7027 */
327 case NID_sect163k1: /* sect163k1 (1) */
329 case NID_sect163r1: /* sect163r1 (2) */
331 case NID_sect163r2: /* sect163r2 (3) */
333 case NID_sect193r1: /* sect193r1 (4) */
335 case NID_sect193r2: /* sect193r2 (5) */
337 case NID_sect233k1: /* sect233k1 (6) */
339 case NID_sect233r1: /* sect233r1 (7) */
341 case NID_sect239k1: /* sect239k1 (8) */
343 case NID_sect283k1: /* sect283k1 (9) */
345 case NID_sect283r1: /* sect283r1 (10) */
347 case NID_sect409k1: /* sect409k1 (11) */
349 case NID_sect409r1: /* sect409r1 (12) */
351 case NID_sect571k1: /* sect571k1 (13) */
353 case NID_sect571r1: /* sect571r1 (14) */
355 case NID_secp160k1: /* secp160k1 (15) */
357 case NID_secp160r1: /* secp160r1 (16) */
359 case NID_secp160r2: /* secp160r2 (17) */
361 case NID_secp192k1: /* secp192k1 (18) */
363 case NID_X9_62_prime192v1: /* secp192r1 (19) */
365 case NID_secp224k1: /* secp224k1 (20) */
367 case NID_secp224r1: /* secp224r1 (21) */
369 case NID_secp256k1: /* secp256k1 (22) */
371 case NID_X9_62_prime256v1: /* secp256r1 (23) */
373 case NID_secp384r1: /* secp384r1 (24) */
375 case NID_secp521r1: /* secp521r1 (25) */
377 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */
379 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */
381 case NID_brainpoolP512r1: /* brainpool512r1 (28) */
388 * Get curves list, if "sess" is set return client curves otherwise
390 * Sets |num_curves| to the number of curves in the list, i.e.,
391 * the length of |pcurves| is 2 * num_curves.
392 * Returns 1 on success and 0 if the client curves list has invalid format.
393 * The latter indicates an internal error: we should not be accepting such
394 * lists in the first place.
395 * TODO(emilia): we should really be storing the curves list in explicitly
396 * parsed form instead. (However, this would affect binary compatibility
397 * so cannot happen in the 1.0.x series.)
399 static int tls1_get_curvelist(SSL *s, int sess,
400 const unsigned char **pcurves,
403 size_t pcurveslen = 0;
406 *pcurves = s->session->tlsext_ellipticcurvelist;
407 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
411 /* For Suite B mode only include P-256, P-384 */
412 switch (tls1_suiteb(s))
414 case SSL_CERT_FLAG_SUITEB_128_LOS:
415 *pcurves = suiteb_curves;
416 pcurveslen = sizeof(suiteb_curves);
419 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
420 *pcurves = suiteb_curves;
424 case SSL_CERT_FLAG_SUITEB_192_LOS:
425 *pcurves = suiteb_curves + 2;
429 *pcurves = s->tlsext_ellipticcurvelist;
430 pcurveslen = s->tlsext_ellipticcurvelist_length;
434 *pcurves = eccurves_default;
435 pcurveslen = sizeof(eccurves_default);
439 /* We do not allow odd length arrays to enter the system. */
442 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
448 *num_curves = pcurveslen / 2;
453 /* See if curve is allowed by security callback */
454 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
456 const tls_curve_info *cinfo;
459 if ((curve[1] < 1) || ((size_t)curve[1] >
460 sizeof(nid_list)/sizeof(nid_list[0])))
462 cinfo = &nid_list[curve[1]-1];
463 #ifdef OPENSSL_NO_EC2M
464 if (cinfo->flags & TLS_CURVE_CHAR2)
467 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
470 /* Check a curve is one of our preferences */
471 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
473 const unsigned char *curves;
474 size_t num_curves, i;
475 unsigned int suiteb_flags = tls1_suiteb(s);
476 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
478 /* Check curve matches Suite B preferences */
481 unsigned long cid = s->s3->tmp.new_cipher->id;
484 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
486 if (p[2] != TLSEXT_curve_P_256)
489 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
491 if (p[2] != TLSEXT_curve_P_384)
494 else /* Should never happen */
497 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
499 for (i = 0; i < num_curves; i++, curves += 2)
501 if (p[1] == curves[0] && p[2] == curves[1])
502 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
508 * Return |nmatch|th shared curve or NID_undef if there is no match.
509 * For nmatch == -1, return number of matches
510 * For nmatch == -2, return the NID of the curve to use for
511 * an EC tmp key, or NID_undef if there is no match.
513 int tls1_shared_curve(SSL *s, int nmatch)
515 const unsigned char *pref, *supp;
516 size_t num_pref, num_supp, i, j;
518 /* Can't do anything on client side */
525 /* For Suite B ciphersuite determines curve: we
526 * already know these are acceptable due to previous
529 unsigned long cid = s->s3->tmp.new_cipher->id;
530 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
531 return NID_X9_62_prime256v1; /* P-256 */
532 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
533 return NID_secp384r1; /* P-384 */
534 /* Should never happen */
537 /* If not Suite B just return first preference shared curve */
541 * Avoid truncation. tls1_get_curvelist takes an int
542 * but s->options is a long...
544 if (!tls1_get_curvelist(s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0,
546 /* In practice, NID_undef == 0 but let's be precise. */
547 return nmatch == -1 ? 0 : NID_undef;
548 if(!tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE),
550 return nmatch == -1 ? 0 : NID_undef;
552 for (i = 0; i < num_pref; i++, pref+=2)
554 const unsigned char *tsupp = supp;
555 for (j = 0; j < num_supp; j++, tsupp+=2)
557 if (pref[0] == tsupp[0] && pref[1] == tsupp[1])
559 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
563 int id = (pref[0] << 8) | pref[1];
564 return tls1_ec_curve_id2nid(id);
572 /* Out of range (nmatch > k). */
576 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
577 int *curves, size_t ncurves)
579 unsigned char *clist, *p;
581 /* Bitmap of curves included to detect duplicates: only works
582 * while curve ids < 32
584 unsigned long dup_list = 0;
585 clist = OPENSSL_malloc(ncurves * 2);
588 for (i = 0, p = clist; i < ncurves; i++)
590 unsigned long idmask;
592 id = tls1_ec_nid2curve_id(curves[i]);
594 if (!id || (dup_list & idmask))
605 *pextlen = ncurves * 2;
609 #define MAX_CURVELIST 28
614 int nid_arr[MAX_CURVELIST];
617 static int nid_cb(const char *elem, int len, void *arg)
619 nid_cb_st *narg = arg;
623 if (narg->nidcnt == MAX_CURVELIST)
625 if (len > (int)(sizeof(etmp) - 1))
627 memcpy(etmp, elem, len);
629 nid = EC_curve_nist2nid(etmp);
630 if (nid == NID_undef)
631 nid = OBJ_sn2nid(etmp);
632 if (nid == NID_undef)
633 nid = OBJ_ln2nid(etmp);
634 if (nid == NID_undef)
636 for (i = 0; i < narg->nidcnt; i++)
637 if (narg->nid_arr[i] == nid)
639 narg->nid_arr[narg->nidcnt++] = nid;
642 /* Set curves based on a colon separate list */
643 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
648 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
652 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
654 /* For an EC key set TLS id and required compression based on parameters */
655 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
660 const EC_METHOD *meth;
663 /* Determine if it is a prime field */
664 grp = EC_KEY_get0_group(ec);
667 meth = EC_GROUP_method_of(grp);
670 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
674 /* Determine curve ID */
675 id = EC_GROUP_get_curve_name(grp);
676 id = tls1_ec_nid2curve_id(id);
677 /* If we have an ID set it, otherwise set arbitrary explicit curve */
681 curve_id[1] = (unsigned char)id;
693 if (EC_KEY_get0_public_key(ec) == NULL)
695 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED)
698 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
700 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
703 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
707 /* Check an EC key is compatible with extensions */
708 static int tls1_check_ec_key(SSL *s,
709 unsigned char *curve_id, unsigned char *comp_id)
711 const unsigned char *pformats, *pcurves;
712 size_t num_formats, num_curves, i;
714 /* If point formats extension present check it, otherwise everything
715 * is supported (see RFC4492).
717 if (comp_id && s->session->tlsext_ecpointformatlist)
719 pformats = s->session->tlsext_ecpointformatlist;
720 num_formats = s->session->tlsext_ecpointformatlist_length;
721 for (i = 0; i < num_formats; i++, pformats++)
723 if (*comp_id == *pformats)
726 if (i == num_formats)
731 /* Check curve is consistent with client and server preferences */
732 for (j = 0; j <= 1; j++)
734 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
736 for (i = 0; i < num_curves; i++, pcurves += 2)
738 if (pcurves[0] == curve_id[0] &&
739 pcurves[1] == curve_id[1])
744 /* For clients can only check sent curve list */
751 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
754 /* If we have a custom point format list use it otherwise
756 if (s->tlsext_ecpointformatlist)
758 *pformats = s->tlsext_ecpointformatlist;
759 *num_formats = s->tlsext_ecpointformatlist_length;
763 *pformats = ecformats_default;
764 /* For Suite B we don't support char2 fields */
766 *num_formats = sizeof(ecformats_default) - 1;
768 *num_formats = sizeof(ecformats_default);
772 /* Check cert parameters compatible with extensions: currently just checks
773 * EC certificates have compatible curves and compression.
775 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
777 unsigned char comp_id, curve_id[2];
780 pkey = X509_get_pubkey(x);
783 /* If not EC nothing to do */
784 if (pkey->type != EVP_PKEY_EC)
789 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
793 /* Can't check curve_id for client certs as we don't have a
794 * supported curves extension.
796 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
799 /* Special case for suite B. We *MUST* sign using SHA256+P-256 or
800 * SHA384+P-384, adjust digest if necessary.
802 if (set_ee_md && tls1_suiteb(s))
809 /* Check to see we have necessary signing algorithm */
810 if (curve_id[1] == TLSEXT_curve_P_256)
811 check_md = NID_ecdsa_with_SHA256;
812 else if (curve_id[1] == TLSEXT_curve_P_384)
813 check_md = NID_ecdsa_with_SHA384;
815 return 0; /* Should never happen */
816 for (i = 0; i < c->shared_sigalgslen; i++)
817 if (check_md == c->shared_sigalgs[i].signandhash_nid)
819 if (i == c->shared_sigalgslen)
823 if (check_md == NID_ecdsa_with_SHA256)
824 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
826 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
831 #ifndef OPENSSL_NO_ECDH
832 /* Check EC temporary key is compatible with client extensions */
833 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
835 unsigned char curve_id[2];
836 EC_KEY *ec = s->cert->ecdh_tmp;
837 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
838 /* Allow any curve: not just those peer supports */
839 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
842 /* If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384,
843 * no other curves permitted.
847 /* Curve to check determined by ciphersuite */
848 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
849 curve_id[1] = TLSEXT_curve_P_256;
850 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
851 curve_id[1] = TLSEXT_curve_P_384;
855 /* Check this curve is acceptable */
856 if (!tls1_check_ec_key(s, curve_id, NULL))
858 /* If auto or setting curve from callback assume OK */
859 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
861 /* Otherwise check curve is acceptable */
864 unsigned char curve_tmp[2];
867 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
869 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
875 if (s->cert->ecdh_tmp_auto)
877 /* Need a shared curve */
878 if (tls1_shared_curve(s, 0))
884 if (s->cert->ecdh_tmp_cb)
889 if (!tls1_set_ec_id(curve_id, NULL, ec))
891 /* Set this to allow use of invalid curves for testing */
895 return tls1_check_ec_key(s, curve_id, NULL);
898 #endif /* OPENSSL_NO_ECDH */
902 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
907 #endif /* OPENSSL_NO_EC */
909 #ifndef OPENSSL_NO_TLSEXT
911 /* List of supported signature algorithms and hashes. Should make this
912 * customisable at some point, for now include everything we support.
915 #ifdef OPENSSL_NO_RSA
916 #define tlsext_sigalg_rsa(md) /* */
918 #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
921 #ifdef OPENSSL_NO_DSA
922 #define tlsext_sigalg_dsa(md) /* */
924 #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
927 #ifdef OPENSSL_NO_ECDSA
928 #define tlsext_sigalg_ecdsa(md) /* */
930 #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
933 #define tlsext_sigalg(md) \
934 tlsext_sigalg_rsa(md) \
935 tlsext_sigalg_dsa(md) \
936 tlsext_sigalg_ecdsa(md)
938 static const unsigned char tls12_sigalgs[] = {
939 #ifndef OPENSSL_NO_SHA512
940 tlsext_sigalg(TLSEXT_hash_sha512)
941 tlsext_sigalg(TLSEXT_hash_sha384)
943 #ifndef OPENSSL_NO_SHA256
944 tlsext_sigalg(TLSEXT_hash_sha256)
945 tlsext_sigalg(TLSEXT_hash_sha224)
947 #ifndef OPENSSL_NO_SHA
948 tlsext_sigalg(TLSEXT_hash_sha1)
951 #ifndef OPENSSL_NO_ECDSA
952 static const unsigned char suiteb_sigalgs[] = {
953 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
954 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
957 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
959 /* If Suite B mode use Suite B sigalgs only, ignore any other
962 #ifndef OPENSSL_NO_EC
963 switch (tls1_suiteb(s))
965 case SSL_CERT_FLAG_SUITEB_128_LOS:
966 *psigs = suiteb_sigalgs;
967 return sizeof(suiteb_sigalgs);
969 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
970 *psigs = suiteb_sigalgs;
973 case SSL_CERT_FLAG_SUITEB_192_LOS:
974 *psigs = suiteb_sigalgs + 2;
978 /* If server use client authentication sigalgs if not NULL */
979 if (s->server && s->cert->client_sigalgs)
981 *psigs = s->cert->client_sigalgs;
982 return s->cert->client_sigalgslen;
984 else if (s->cert->conf_sigalgs)
986 *psigs = s->cert->conf_sigalgs;
987 return s->cert->conf_sigalgslen;
991 *psigs = tls12_sigalgs;
992 return sizeof(tls12_sigalgs);
995 /* Check signature algorithm is consistent with sent supported signature
996 * algorithms and if so return relevant digest.
998 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
999 const unsigned char *sig, EVP_PKEY *pkey)
1001 const unsigned char *sent_sigs;
1002 size_t sent_sigslen, i;
1003 int sigalg = tls12_get_sigid(pkey);
1004 /* Should never happen */
1007 /* Check key type is consistent with signature */
1008 if (sigalg != (int)sig[1])
1010 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1013 #ifndef OPENSSL_NO_EC
1014 if (pkey->type == EVP_PKEY_EC)
1016 unsigned char curve_id[2], comp_id;
1017 /* Check compression and curve matches extensions */
1018 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
1020 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id))
1022 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_CURVE);
1025 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
1030 if (curve_id[1] == TLSEXT_curve_P_256)
1032 if (sig[0] != TLSEXT_hash_sha256)
1034 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1035 SSL_R_ILLEGAL_SUITEB_DIGEST);
1039 else if (curve_id[1] == TLSEXT_curve_P_384)
1041 if (sig[0] != TLSEXT_hash_sha384)
1043 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1044 SSL_R_ILLEGAL_SUITEB_DIGEST);
1052 else if (tls1_suiteb(s))
1056 /* Check signature matches a type we sent */
1057 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1058 for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2)
1060 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1063 /* Allow fallback to SHA1 if not strict mode */
1064 if (i == sent_sigslen && (sig[0] != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
1066 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1069 *pmd = tls12_get_hash(sig[0]);
1072 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_UNKNOWN_DIGEST);
1075 /* Make sure security callback allows algorithm */
1076 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1077 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
1080 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,SSL_R_WRONG_SIGNATURE_TYPE);
1083 /* Store the digest used so applications can retrieve it if they
1086 if (s->session && s->session->sess_cert)
1087 s->session->sess_cert->peer_key->digest = *pmd;
1091 /* Get a mask of disabled algorithms: an algorithm is disabled
1092 * if it isn't supported or doesn't appear in supported signature
1093 * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific
1094 * session and not global settings.
1097 void ssl_set_client_disabled(SSL *s)
1102 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1103 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1104 c->mask_ssl = SSL_TLSV1_2;
1107 ssl_set_sig_mask(&c->mask_a, s, SSL_SECOP_SIGALG_MASK);
1108 /* Disable static DH if we don't include any appropriate
1109 * signature algorithms.
1111 if (c->mask_a & SSL_aRSA)
1112 c->mask_k |= SSL_kDHr|SSL_kECDHr;
1113 if (c->mask_a & SSL_aDSS)
1114 c->mask_k |= SSL_kDHd;
1115 if (c->mask_a & SSL_aECDSA)
1116 c->mask_k |= SSL_kECDHe;
1117 #ifndef OPENSSL_NO_KRB5
1118 if (!kssl_tgt_is_available(s->kssl_ctx))
1120 c->mask_a |= SSL_aKRB5;
1121 c->mask_k |= SSL_kKRB5;
1124 #ifndef OPENSSL_NO_PSK
1125 /* with PSK there must be client callback set */
1126 if (!s->psk_client_callback)
1128 c->mask_a |= SSL_aPSK;
1129 c->mask_k |= SSL_kPSK;
1131 #endif /* OPENSSL_NO_PSK */
1132 #ifndef OPENSSL_NO_SRP
1133 if (!(s->srp_ctx.srp_Mask & SSL_kSRP))
1135 c->mask_a |= SSL_aSRP;
1136 c->mask_k |= SSL_kSRP;
1142 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1145 if (c->algorithm_ssl & ct->mask_ssl || c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a)
1147 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1150 static int tls_use_ticket(SSL *s)
1152 if (s->options & SSL_OP_NO_TICKET)
1154 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1157 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, int *al)
1160 unsigned char *orig = buf;
1161 unsigned char *ret = buf;
1162 #ifndef OPENSSL_NO_EC
1163 /* See if we support any ECC ciphersuites */
1165 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s))
1168 unsigned long alg_k, alg_a;
1169 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1171 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
1173 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1175 alg_k = c->algorithm_mkey;
1176 alg_a = c->algorithm_auth;
1177 if ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)
1178 || (alg_a & SSL_aECDSA)))
1189 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1191 /* Add RI if renegotiating */
1196 if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
1198 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1202 if((limit - ret - 4 - el) < 0) return NULL;
1204 s2n(TLSEXT_TYPE_renegotiate,ret);
1207 if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
1209 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1215 /* Only add RI for SSLv3 */
1216 if (s->client_version == SSL3_VERSION)
1219 if (s->tlsext_hostname != NULL)
1221 /* Add TLS extension servername to the Client Hello message */
1222 unsigned long size_str;
1226 * check for enough space.
1227 * 4 for the servername type and entension length
1228 * 2 for servernamelist length
1229 * 1 for the hostname type
1230 * 2 for hostname length
1234 if ((lenmax = limit - ret - 9) < 0
1235 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1238 /* extension type and length */
1239 s2n(TLSEXT_TYPE_server_name,ret);
1240 s2n(size_str+5,ret);
1242 /* length of servername list */
1243 s2n(size_str+3,ret);
1245 /* hostname type, length and hostname */
1246 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
1248 memcpy(ret, s->tlsext_hostname, size_str);
1252 #ifndef OPENSSL_NO_SRP
1253 /* Add SRP username if there is one */
1254 if (s->srp_ctx.login != NULL)
1255 { /* Add TLS extension SRP username to the Client Hello message */
1257 int login_len = strlen(s->srp_ctx.login);
1258 if (login_len > 255 || login_len == 0)
1260 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1265 * check for enough space.
1266 * 4 for the srp type type and entension length
1267 * 1 for the srp user identity
1268 * + srp user identity length
1270 if ((limit - ret - 5 - login_len) < 0) return NULL;
1272 /* fill in the extension */
1273 s2n(TLSEXT_TYPE_srp,ret);
1274 s2n(login_len+1,ret);
1275 (*ret++) = (unsigned char) login_len;
1276 memcpy(ret, s->srp_ctx.login, login_len);
1281 #ifndef OPENSSL_NO_EC
1284 /* Add TLS extension ECPointFormats to the ClientHello message */
1286 const unsigned char *pcurves, *pformats;
1287 size_t num_curves, num_formats, curves_list_len;
1289 unsigned char *etmp;
1291 tls1_get_formatlist(s, &pformats, &num_formats);
1293 if ((lenmax = limit - ret - 5) < 0) return NULL;
1294 if (num_formats > (size_t)lenmax) return NULL;
1295 if (num_formats > 255)
1297 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1301 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1302 /* The point format list has 1-byte length. */
1303 s2n(num_formats + 1,ret);
1304 *(ret++) = (unsigned char)num_formats ;
1305 memcpy(ret, pformats, num_formats);
1308 /* Add TLS extension EllipticCurves to the ClientHello message */
1309 pcurves = s->tlsext_ellipticcurvelist;
1310 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1313 if ((lenmax = limit - ret - 6) < 0) return NULL;
1314 if (num_curves > (size_t)lenmax / 2) return NULL;
1315 if (num_curves > 65532 / 2)
1317 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1322 s2n(TLSEXT_TYPE_elliptic_curves,ret);
1324 /* Copy curve ID if supported */
1325 for (i = 0; i < num_curves; i++, pcurves += 2)
1327 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED))
1329 *etmp++ = pcurves[0];
1330 *etmp++ = pcurves[1];
1334 curves_list_len = etmp - ret - 4;
1336 s2n(curves_list_len + 2, ret);
1337 s2n(curves_list_len, ret);
1338 ret += curves_list_len;
1340 #endif /* OPENSSL_NO_EC */
1342 if (tls_use_ticket(s))
1345 if (!s->new_session && s->session && s->session->tlsext_tick)
1346 ticklen = s->session->tlsext_ticklen;
1347 else if (s->session && s->tlsext_session_ticket &&
1348 s->tlsext_session_ticket->data)
1350 ticklen = s->tlsext_session_ticket->length;
1351 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1352 if (!s->session->tlsext_tick)
1354 memcpy(s->session->tlsext_tick,
1355 s->tlsext_session_ticket->data,
1357 s->session->tlsext_ticklen = ticklen;
1361 if (ticklen == 0 && s->tlsext_session_ticket &&
1362 s->tlsext_session_ticket->data == NULL)
1364 /* Check for enough room 2 for extension type, 2 for len
1367 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
1368 s2n(TLSEXT_TYPE_session_ticket,ret);
1372 memcpy(ret, s->session->tlsext_tick, ticklen);
1378 if (SSL_USE_SIGALGS(s))
1381 const unsigned char *salg;
1382 unsigned char *etmp;
1383 salglen = tls12_get_psigalgs(s, &salg);
1384 if ((size_t)(limit - ret) < salglen + 6)
1386 s2n(TLSEXT_TYPE_signature_algorithms,ret);
1388 /* Skip over lengths for now */
1390 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1391 /* Fill in lengths */
1392 s2n(salglen + 2, etmp);
1397 #ifdef TLSEXT_TYPE_opaque_prf_input
1398 if (s->s3->client_opaque_prf_input != NULL)
1400 size_t col = s->s3->client_opaque_prf_input_len;
1402 if ((long)(limit - ret - 6 - col) < 0)
1404 if (col > 0xFFFD) /* can't happen */
1407 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1410 memcpy(ret, s->s3->client_opaque_prf_input, col);
1415 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
1418 long extlen, idlen, itmp;
1422 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1424 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1425 itmp = i2d_OCSP_RESPID(id, NULL);
1431 if (s->tlsext_ocsp_exts)
1433 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1440 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
1441 s2n(TLSEXT_TYPE_status_request, ret);
1442 if (extlen + idlen > 0xFFF0)
1444 s2n(extlen + idlen + 5, ret);
1445 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1447 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
1449 /* save position of id len */
1450 unsigned char *q = ret;
1451 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1452 /* skip over id len */
1454 itmp = i2d_OCSP_RESPID(id, &ret);
1460 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1463 #ifndef OPENSSL_NO_HEARTBEATS
1464 /* Add Heartbeat extension */
1465 if ((limit - ret - 4 - 1) < 0)
1467 s2n(TLSEXT_TYPE_heartbeat,ret);
1471 * 1: peer may send requests
1472 * 2: peer not allowed to send requests
1474 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1475 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1477 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1480 #ifndef OPENSSL_NO_NEXTPROTONEG
1481 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len)
1483 /* The client advertises an emtpy extension to indicate its
1484 * support for Next Protocol Negotiation */
1485 if (limit - ret - 4 < 0)
1487 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1492 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len)
1494 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1496 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1497 s2n(2 + s->alpn_client_proto_list_len,ret);
1498 s2n(s->alpn_client_proto_list_len,ret);
1499 memcpy(ret, s->alpn_client_proto_list,
1500 s->alpn_client_proto_list_len);
1501 ret += s->alpn_client_proto_list_len;
1504 if(SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s))
1508 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1510 if((limit - ret - 4 - el) < 0) return NULL;
1512 s2n(TLSEXT_TYPE_use_srtp,ret);
1515 if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el))
1517 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1522 custom_ext_init(&s->cert->cli_ext);
1523 /* Add custom TLS Extensions to ClientHello */
1524 if (!custom_ext_add(s, 0, &ret, limit, al))
1526 #ifdef TLSEXT_TYPE_encrypt_then_mac
1527 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1531 /* Add padding to workaround bugs in F5 terminators.
1532 * See https://tools.ietf.org/html/draft-agl-tls-padding-03
1534 * NB: because this code works out the length of all existing
1535 * extensions it MUST always appear last.
1537 if (s->options & SSL_OP_TLSEXT_PADDING)
1539 int hlen = ret - (unsigned char *)s->init_buf->data;
1540 /* The code in s23_clnt.c to build ClientHello messages
1541 * includes the 5-byte record header in the buffer, while
1542 * the code in s3_clnt.c does not.
1544 if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
1546 if (hlen > 0xff && hlen < 0x200)
1548 hlen = 0x200 - hlen;
1554 s2n(TLSEXT_TYPE_padding, ret);
1556 memset(ret, 0, hlen);
1563 if ((extdatalen = ret-orig-2)== 0)
1566 s2n(extdatalen, orig);
1570 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, int *al)
1573 unsigned char *orig = buf;
1574 unsigned char *ret = buf;
1575 #ifndef OPENSSL_NO_NEXTPROTONEG
1576 int next_proto_neg_seen;
1578 #ifndef OPENSSL_NO_EC
1579 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1580 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1581 int using_ecc = (alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
1582 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1586 if (ret>=limit) return NULL; /* this really never occurs, but ... */
1588 if(s->s3->send_connection_binding)
1592 if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
1594 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1598 if((limit - ret - 4 - el) < 0) return NULL;
1600 s2n(TLSEXT_TYPE_renegotiate,ret);
1603 if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
1605 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1612 /* Only add RI for SSLv3 */
1613 if (s->version == SSL3_VERSION)
1616 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
1618 if ((long)(limit - ret - 4) < 0) return NULL;
1620 s2n(TLSEXT_TYPE_server_name,ret);
1624 #ifndef OPENSSL_NO_EC
1627 const unsigned char *plist;
1629 /* Add TLS extension ECPointFormats to the ServerHello message */
1632 tls1_get_formatlist(s, &plist, &plistlen);
1634 if ((lenmax = limit - ret - 5) < 0) return NULL;
1635 if (plistlen > (size_t)lenmax) return NULL;
1638 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1642 s2n(TLSEXT_TYPE_ec_point_formats,ret);
1643 s2n(plistlen + 1,ret);
1644 *(ret++) = (unsigned char) plistlen;
1645 memcpy(ret, plist, plistlen);
1649 /* Currently the server should not respond with a SupportedCurves extension */
1650 #endif /* OPENSSL_NO_EC */
1652 if (s->tlsext_ticket_expected && tls_use_ticket(s))
1654 if ((long)(limit - ret - 4) < 0) return NULL;
1655 s2n(TLSEXT_TYPE_session_ticket,ret);
1659 if (s->tlsext_status_expected)
1661 if ((long)(limit - ret - 4) < 0) return NULL;
1662 s2n(TLSEXT_TYPE_status_request,ret);
1666 #ifdef TLSEXT_TYPE_opaque_prf_input
1667 if (s->s3->server_opaque_prf_input != NULL)
1669 size_t sol = s->s3->server_opaque_prf_input_len;
1671 if ((long)(limit - ret - 6 - sol) < 0)
1673 if (sol > 0xFFFD) /* can't happen */
1676 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1679 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1684 if(SSL_IS_DTLS(s) && s->srtp_profile)
1688 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1690 if((limit - ret - 4 - el) < 0) return NULL;
1692 s2n(TLSEXT_TYPE_use_srtp,ret);
1695 if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el))
1697 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1703 if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81)
1704 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG))
1705 { const unsigned char cryptopro_ext[36] = {
1706 0xfd, 0xe8, /*65000*/
1707 0x00, 0x20, /*32 bytes length*/
1708 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1709 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1710 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1711 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17};
1712 if (limit-ret<36) return NULL;
1713 memcpy(ret,cryptopro_ext,36);
1718 #ifndef OPENSSL_NO_HEARTBEATS
1719 /* Add Heartbeat extension if we've received one */
1720 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED)
1722 if ((limit - ret - 4 - 1) < 0)
1724 s2n(TLSEXT_TYPE_heartbeat,ret);
1728 * 1: peer may send requests
1729 * 2: peer not allowed to send requests
1731 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1732 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1734 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1739 #ifndef OPENSSL_NO_NEXTPROTONEG
1740 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1741 s->s3->next_proto_neg_seen = 0;
1742 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb)
1744 const unsigned char *npa;
1745 unsigned int npalen;
1748 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
1749 if (r == SSL_TLSEXT_ERR_OK)
1751 if ((long)(limit - ret - 4 - npalen) < 0) return NULL;
1752 s2n(TLSEXT_TYPE_next_proto_neg,ret);
1754 memcpy(ret, npa, npalen);
1756 s->s3->next_proto_neg_seen = 1;
1760 if (!custom_ext_add(s, 1, &ret, limit, al))
1762 #ifdef TLSEXT_TYPE_encrypt_then_mac
1763 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC)
1765 /* Don't use encrypt_then_mac if AEAD or RC4
1766 * might want to disable for other cases too.
1768 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1769 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4)
1770 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1773 s2n(TLSEXT_TYPE_encrypt_then_mac,ret);
1779 if (s->s3->alpn_selected)
1781 const unsigned char *selected = s->s3->alpn_selected;
1782 unsigned len = s->s3->alpn_selected_len;
1784 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1786 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret);
1790 memcpy(ret, selected, len);
1796 if ((extdatalen = ret-orig-2)== 0)
1799 s2n(extdatalen, orig);
1803 /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1805 * data: the contents of the extension, not including the type and length.
1806 * data_len: the number of bytes in |data|
1807 * al: a pointer to the alert value to send in the event of a non-zero
1810 * returns: 0 on success. */
1811 static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
1812 unsigned data_len, int *al)
1816 const unsigned char *selected;
1817 unsigned char selected_len;
1820 if (s->ctx->alpn_select_cb == NULL)
1826 /* data should contain a uint16 length followed by a series of 8-bit,
1827 * length-prefixed strings. */
1828 i = ((unsigned) data[0]) << 8 |
1829 ((unsigned) data[1]);
1838 for (i = 0; i < data_len;)
1840 proto_len = data[i];
1846 if (i + proto_len < i || i + proto_len > data_len)
1852 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1853 s->ctx->alpn_select_cb_arg);
1854 if (r == SSL_TLSEXT_ERR_OK) {
1855 if (s->s3->alpn_selected)
1856 OPENSSL_free(s->s3->alpn_selected);
1857 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1858 if (!s->s3->alpn_selected)
1860 *al = SSL_AD_INTERNAL_ERROR;
1863 memcpy(s->s3->alpn_selected, selected, selected_len);
1864 s->s3->alpn_selected_len = selected_len;
1869 *al = SSL_AD_DECODE_ERROR;
1873 #ifndef OPENSSL_NO_EC
1875 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1876 * SecureTransport using the TLS extension block in |d|, of length |n|.
1877 * Safari, since 10.6, sends exactly these extensions, in this order:
1882 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1883 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1884 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1885 * 10.8..10.8.3 (which don't work).
1887 static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) {
1888 unsigned short type, size;
1889 static const unsigned char kSafariExtensionsBlock[] = {
1890 0x00, 0x0a, /* elliptic_curves extension */
1891 0x00, 0x08, /* 8 bytes */
1892 0x00, 0x06, /* 6 bytes of curve ids */
1893 0x00, 0x17, /* P-256 */
1894 0x00, 0x18, /* P-384 */
1895 0x00, 0x19, /* P-521 */
1897 0x00, 0x0b, /* ec_point_formats */
1898 0x00, 0x02, /* 2 bytes */
1899 0x01, /* 1 point format */
1900 0x00, /* uncompressed */
1903 /* The following is only present in TLS 1.2 */
1904 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1905 0x00, 0x0d, /* signature_algorithms */
1906 0x00, 0x0c, /* 12 bytes */
1907 0x00, 0x0a, /* 10 bytes */
1908 0x05, 0x01, /* SHA-384/RSA */
1909 0x04, 0x01, /* SHA-256/RSA */
1910 0x02, 0x01, /* SHA-1/RSA */
1911 0x04, 0x03, /* SHA-256/ECDSA */
1912 0x02, 0x03, /* SHA-1/ECDSA */
1915 if (data >= (d+n-2))
1924 if (type != TLSEXT_TYPE_server_name)
1927 if (data+size > d+n)
1931 if (TLS1_get_client_version(s) >= TLS1_2_VERSION)
1933 const size_t len1 = sizeof(kSafariExtensionsBlock);
1934 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1936 if (data + len1 + len2 != d+n)
1938 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1940 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1945 const size_t len = sizeof(kSafariExtensionsBlock);
1947 if (data + len != d+n)
1949 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
1953 s->s3->is_probably_safari = 1;
1955 #endif /* !OPENSSL_NO_EC */
1958 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
1960 unsigned short type;
1961 unsigned short size;
1963 unsigned char *data = *p;
1964 int renegotiate_seen = 0;
1966 s->servername_done = 0;
1967 s->tlsext_status_type = -1;
1968 #ifndef OPENSSL_NO_NEXTPROTONEG
1969 s->s3->next_proto_neg_seen = 0;
1972 if (s->s3->alpn_selected)
1974 OPENSSL_free(s->s3->alpn_selected);
1975 s->s3->alpn_selected = NULL;
1978 #ifndef OPENSSL_NO_HEARTBEATS
1979 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1980 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1983 #ifndef OPENSSL_NO_EC
1984 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1985 ssl_check_for_safari(s, data, d, n);
1986 #endif /* !OPENSSL_NO_EC */
1988 /* Clear any signature algorithms extension received */
1989 if (s->cert->peer_sigalgs)
1991 OPENSSL_free(s->cert->peer_sigalgs);
1992 s->cert->peer_sigalgs = NULL;
1995 #ifdef TLSEXT_TYPE_encrypt_then_mac
1996 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1999 if (data >= (d+n-2))
2003 if (data > (d+n-len))
2006 while (data <= (d+n-4))
2011 if (data+size > (d+n))
2014 fprintf(stderr,"Received extension type %d size %d\n",type,size);
2016 if (s->tlsext_debug_cb)
2017 s->tlsext_debug_cb(s, 0, type, data, size,
2018 s->tlsext_debug_arg);
2019 if (type == TLSEXT_TYPE_renegotiate)
2021 if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2023 renegotiate_seen = 1;
2025 else if (s->version == SSL3_VERSION)
2028 * The servername extension is treated as follows:
2030 * - Only the hostname type is supported with a maximum length of 255.
2031 * - The servername is rejected if too long or if it contains zeros,
2032 * in which case an fatal alert is generated.
2033 * - The servername field is maintained together with the session cache.
2034 * - When a session is resumed, the servername call back invoked in order
2035 * to allow the application to position itself to the right context.
2036 * - The servername is acknowledged if it is new for a session or when
2037 * it is identical to a previously used for the same session.
2038 * Applications can control the behaviour. They can at any time
2039 * set a 'desirable' servername for a new SSL object. This can be the
2040 * case for example with HTTPS when a Host: header field is received and
2041 * a renegotiation is requested. In this case, a possible servername
2042 * presented in the new client hello is only acknowledged if it matches
2043 * the value of the Host: field.
2044 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2045 * if they provide for changing an explicit servername context for the
2046 * session, i.e. when the session has been established with a servername
2048 * - On session reconnect, the servername extension may be absent.
2052 else if (type == TLSEXT_TYPE_server_name)
2054 unsigned char *sdata;
2060 *al = SSL_AD_DECODE_ERROR;
2067 *al = SSL_AD_DECODE_ERROR;
2074 servname_type = *(sdata++);
2080 *al = SSL_AD_DECODE_ERROR;
2083 if (s->servername_done == 0)
2084 switch (servname_type)
2086 case TLSEXT_NAMETYPE_host_name:
2089 if(s->session->tlsext_hostname)
2091 *al = SSL_AD_DECODE_ERROR;
2094 if (len > TLSEXT_MAXLEN_host_name)
2096 *al = TLS1_AD_UNRECOGNIZED_NAME;
2099 if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL)
2101 *al = TLS1_AD_INTERNAL_ERROR;
2104 memcpy(s->session->tlsext_hostname, sdata, len);
2105 s->session->tlsext_hostname[len]='\0';
2106 if (strlen(s->session->tlsext_hostname) != len) {
2107 OPENSSL_free(s->session->tlsext_hostname);
2108 s->session->tlsext_hostname = NULL;
2109 *al = TLS1_AD_UNRECOGNIZED_NAME;
2112 s->servername_done = 1;
2116 s->servername_done = s->session->tlsext_hostname
2117 && strlen(s->session->tlsext_hostname) == len
2118 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
2130 *al = SSL_AD_DECODE_ERROR;
2135 #ifndef OPENSSL_NO_SRP
2136 else if (type == TLSEXT_TYPE_srp)
2138 if (size <= 0 || ((len = data[0])) != (size -1))
2140 *al = SSL_AD_DECODE_ERROR;
2143 if (s->srp_ctx.login != NULL)
2145 *al = SSL_AD_DECODE_ERROR;
2148 if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL)
2150 memcpy(s->srp_ctx.login, &data[1], len);
2151 s->srp_ctx.login[len]='\0';
2153 if (strlen(s->srp_ctx.login) != len)
2155 *al = SSL_AD_DECODE_ERROR;
2161 #ifndef OPENSSL_NO_EC
2162 else if (type == TLSEXT_TYPE_ec_point_formats)
2164 unsigned char *sdata = data;
2165 int ecpointformatlist_length = *(sdata++);
2167 if (ecpointformatlist_length != size - 1 ||
2168 ecpointformatlist_length < 1)
2170 *al = TLS1_AD_DECODE_ERROR;
2175 if(s->session->tlsext_ecpointformatlist)
2177 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2178 s->session->tlsext_ecpointformatlist = NULL;
2180 s->session->tlsext_ecpointformatlist_length = 0;
2181 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2183 *al = TLS1_AD_INTERNAL_ERROR;
2186 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2187 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2190 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
2191 sdata = s->session->tlsext_ecpointformatlist;
2192 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2193 fprintf(stderr,"%i ",*(sdata++));
2194 fprintf(stderr,"\n");
2197 else if (type == TLSEXT_TYPE_elliptic_curves)
2199 unsigned char *sdata = data;
2200 int ellipticcurvelist_length = (*(sdata++) << 8);
2201 ellipticcurvelist_length += (*(sdata++));
2203 if (ellipticcurvelist_length != size - 2 ||
2204 ellipticcurvelist_length < 1 ||
2205 /* Each NamedCurve is 2 bytes. */
2206 ellipticcurvelist_length & 1)
2208 *al = TLS1_AD_DECODE_ERROR;
2213 if(s->session->tlsext_ellipticcurvelist)
2215 *al = TLS1_AD_DECODE_ERROR;
2218 s->session->tlsext_ellipticcurvelist_length = 0;
2219 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
2221 *al = TLS1_AD_INTERNAL_ERROR;
2224 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
2225 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
2228 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
2229 sdata = s->session->tlsext_ellipticcurvelist;
2230 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2231 fprintf(stderr,"%i ",*(sdata++));
2232 fprintf(stderr,"\n");
2235 #endif /* OPENSSL_NO_EC */
2236 #ifdef TLSEXT_TYPE_opaque_prf_input
2237 else if (type == TLSEXT_TYPE_opaque_prf_input)
2239 unsigned char *sdata = data;
2243 *al = SSL_AD_DECODE_ERROR;
2246 n2s(sdata, s->s3->client_opaque_prf_input_len);
2247 if (s->s3->client_opaque_prf_input_len != size - 2)
2249 *al = SSL_AD_DECODE_ERROR;
2253 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2254 OPENSSL_free(s->s3->client_opaque_prf_input);
2255 if (s->s3->client_opaque_prf_input_len == 0)
2256 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2258 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2259 if (s->s3->client_opaque_prf_input == NULL)
2261 *al = TLS1_AD_INTERNAL_ERROR;
2266 else if (type == TLSEXT_TYPE_session_ticket)
2268 if (s->tls_session_ticket_ext_cb &&
2269 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2271 *al = TLS1_AD_INTERNAL_ERROR;
2275 else if (type == TLSEXT_TYPE_signature_algorithms)
2278 if (s->cert->peer_sigalgs || size < 2)
2280 *al = SSL_AD_DECODE_ERROR;
2285 if (dsize != size || dsize & 1 || !dsize)
2287 *al = SSL_AD_DECODE_ERROR;
2290 if (!tls1_save_sigalgs(s, data, dsize))
2292 *al = SSL_AD_DECODE_ERROR;
2296 else if (type == TLSEXT_TYPE_status_request)
2301 *al = SSL_AD_DECODE_ERROR;
2305 s->tlsext_status_type = *data++;
2307 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
2309 const unsigned char *sdata;
2311 /* Read in responder_id_list */
2316 *al = SSL_AD_DECODE_ERROR;
2325 *al = SSL_AD_DECODE_ERROR;
2329 dsize -= 2 + idsize;
2333 *al = SSL_AD_DECODE_ERROR;
2338 id = d2i_OCSP_RESPID(NULL,
2342 *al = SSL_AD_DECODE_ERROR;
2347 OCSP_RESPID_free(id);
2348 *al = SSL_AD_DECODE_ERROR;
2351 if (!s->tlsext_ocsp_ids
2352 && !(s->tlsext_ocsp_ids =
2353 sk_OCSP_RESPID_new_null()))
2355 OCSP_RESPID_free(id);
2356 *al = SSL_AD_INTERNAL_ERROR;
2359 if (!sk_OCSP_RESPID_push(
2360 s->tlsext_ocsp_ids, id))
2362 OCSP_RESPID_free(id);
2363 *al = SSL_AD_INTERNAL_ERROR;
2368 /* Read in request_extensions */
2371 *al = SSL_AD_DECODE_ERROR;
2378 *al = SSL_AD_DECODE_ERROR;
2384 if (s->tlsext_ocsp_exts)
2386 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2387 X509_EXTENSION_free);
2390 s->tlsext_ocsp_exts =
2391 d2i_X509_EXTENSIONS(NULL,
2393 if (!s->tlsext_ocsp_exts
2394 || (data + dsize != sdata))
2396 *al = SSL_AD_DECODE_ERROR;
2401 /* We don't know what to do with any other type
2405 s->tlsext_status_type = -1;
2407 #ifndef OPENSSL_NO_HEARTBEATS
2408 else if (type == TLSEXT_TYPE_heartbeat)
2412 case 0x01: /* Client allows us to send HB requests */
2413 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2415 case 0x02: /* Client doesn't accept HB requests */
2416 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2417 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2419 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2424 #ifndef OPENSSL_NO_NEXTPROTONEG
2425 else if (type == TLSEXT_TYPE_next_proto_neg &&
2426 s->s3->tmp.finish_md_len == 0 &&
2427 s->s3->alpn_selected == NULL)
2430 * We shouldn't accept this extension on a
2433 * s->new_session will be set on renegotiation, but we
2434 * probably shouldn't rely that it couldn't be set on
2435 * the initial renegotation too in certain cases (when
2436 * there's some other reason to disallow resuming an
2437 * earlier session -- the current code won't be doing
2438 * anything like that, but this might change).
2440 * A valid sign that there's been a previous handshake
2441 * in this connection is if s->s3->tmp.finish_md_len >
2442 * 0. (We are talking about a check that will happen
2443 * in the Hello protocol round, well before a new
2444 * Finished message could have been computed.)
2446 s->s3->next_proto_neg_seen = 1;
2450 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2451 s->ctx->alpn_select_cb &&
2452 s->s3->tmp.finish_md_len == 0)
2454 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2456 #ifndef OPENSSL_NO_NEXTPROTONEG
2457 /* ALPN takes precedence over NPN. */
2458 s->s3->next_proto_neg_seen = 0;
2462 /* session ticket processed earlier */
2463 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2464 && type == TLSEXT_TYPE_use_srtp)
2466 if(ssl_parse_clienthello_use_srtp_ext(s, data, size,
2470 #ifdef TLSEXT_TYPE_encrypt_then_mac
2471 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2472 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2474 /* If this ClientHello extension was unhandled and this is
2475 * a nonresumed connection, check whether the extension is a
2476 * custom TLS Extension (has a custom_srv_ext_record), and if
2477 * so call the callback and record the extension number so that
2478 * an appropriate ServerHello may be later returned.
2482 if (custom_ext_parse(s, 1, type, data, size, al) <= 0)
2493 /* Need RI if renegotiating */
2495 if (!renegotiate_seen && s->renegotiate &&
2496 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2498 *al = SSL_AD_HANDSHAKE_FAILURE;
2499 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2500 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2507 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
2510 custom_ext_init(&s->cert->srv_ext);
2511 if (ssl_scan_clienthello_tlsext(s, p, d, n, &al) <= 0)
2513 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2517 if (ssl_check_clienthello_tlsext_early(s) <= 0)
2519 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,SSL_R_CLIENTHELLO_TLSEXT);
2525 #ifndef OPENSSL_NO_NEXTPROTONEG
2526 /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2527 * elements of zero length are allowed and the set of elements must exactly fill
2528 * the length of the block. */
2529 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2531 unsigned int off = 0;
2545 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
2547 unsigned short length;
2548 unsigned short type;
2549 unsigned short size;
2550 unsigned char *data = *p;
2551 int tlsext_servername = 0;
2552 int renegotiate_seen = 0;
2554 #ifndef OPENSSL_NO_NEXTPROTONEG
2555 s->s3->next_proto_neg_seen = 0;
2557 s->tlsext_ticket_expected = 0;
2559 if (s->s3->alpn_selected)
2561 OPENSSL_free(s->s3->alpn_selected);
2562 s->s3->alpn_selected = NULL;
2565 #ifndef OPENSSL_NO_HEARTBEATS
2566 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2567 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2570 #ifdef TLSEXT_TYPE_encrypt_then_mac
2571 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2574 if (data >= (d+n-2))
2578 if (data+length != d+n)
2580 *al = SSL_AD_DECODE_ERROR;
2584 while(data <= (d+n-4))
2589 if (data+size > (d+n))
2592 if (s->tlsext_debug_cb)
2593 s->tlsext_debug_cb(s, 1, type, data, size,
2594 s->tlsext_debug_arg);
2597 if (type == TLSEXT_TYPE_renegotiate)
2599 if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2601 renegotiate_seen = 1;
2603 else if (s->version == SSL3_VERSION)
2605 else if (type == TLSEXT_TYPE_server_name)
2607 if (s->tlsext_hostname == NULL || size > 0)
2609 *al = TLS1_AD_UNRECOGNIZED_NAME;
2612 tlsext_servername = 1;
2615 #ifndef OPENSSL_NO_EC
2616 else if (type == TLSEXT_TYPE_ec_point_formats)
2618 unsigned char *sdata = data;
2619 int ecpointformatlist_length = *(sdata++);
2621 if (ecpointformatlist_length != size - 1)
2623 *al = TLS1_AD_DECODE_ERROR;
2628 s->session->tlsext_ecpointformatlist_length = 0;
2629 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
2630 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
2632 *al = TLS1_AD_INTERNAL_ERROR;
2635 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
2636 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
2639 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2640 sdata = s->session->tlsext_ecpointformatlist;
2641 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2642 fprintf(stderr,"%i ",*(sdata++));
2643 fprintf(stderr,"\n");
2646 #endif /* OPENSSL_NO_EC */
2648 else if (type == TLSEXT_TYPE_session_ticket)
2650 if (s->tls_session_ticket_ext_cb &&
2651 !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg))
2653 *al = TLS1_AD_INTERNAL_ERROR;
2656 if (!tls_use_ticket(s) || (size > 0))
2658 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2661 s->tlsext_ticket_expected = 1;
2663 #ifdef TLSEXT_TYPE_opaque_prf_input
2664 else if (type == TLSEXT_TYPE_opaque_prf_input)
2666 unsigned char *sdata = data;
2670 *al = SSL_AD_DECODE_ERROR;
2673 n2s(sdata, s->s3->server_opaque_prf_input_len);
2674 if (s->s3->server_opaque_prf_input_len != size - 2)
2676 *al = SSL_AD_DECODE_ERROR;
2680 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2681 OPENSSL_free(s->s3->server_opaque_prf_input);
2682 if (s->s3->server_opaque_prf_input_len == 0)
2683 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2685 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2687 if (s->s3->server_opaque_prf_input == NULL)
2689 *al = TLS1_AD_INTERNAL_ERROR;
2694 else if (type == TLSEXT_TYPE_status_request)
2696 /* MUST be empty and only sent if we've requested
2697 * a status request message.
2699 if ((s->tlsext_status_type == -1) || (size > 0))
2701 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2704 /* Set flag to expect CertificateStatus message */
2705 s->tlsext_status_expected = 1;
2707 #ifndef OPENSSL_NO_NEXTPROTONEG
2708 else if (type == TLSEXT_TYPE_next_proto_neg &&
2709 s->s3->tmp.finish_md_len == 0)
2711 unsigned char *selected;
2712 unsigned char selected_len;
2714 /* We must have requested it. */
2715 if (s->ctx->next_proto_select_cb == NULL)
2717 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2720 /* The data must be valid */
2721 if (!ssl_next_proto_validate(data, size))
2723 *al = TLS1_AD_DECODE_ERROR;
2726 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK)
2728 *al = TLS1_AD_INTERNAL_ERROR;
2731 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2732 if (!s->next_proto_negotiated)
2734 *al = TLS1_AD_INTERNAL_ERROR;
2737 memcpy(s->next_proto_negotiated, selected, selected_len);
2738 s->next_proto_negotiated_len = selected_len;
2739 s->s3->next_proto_neg_seen = 1;
2743 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation)
2747 /* We must have requested it. */
2748 if (s->alpn_client_proto_list == NULL)
2750 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2755 *al = TLS1_AD_DECODE_ERROR;
2759 * The extension data consists of:
2760 * uint16 list_length
2761 * uint8 proto_length;
2762 * uint8 proto[proto_length];
2767 if (len != (unsigned) size - 2)
2769 *al = TLS1_AD_DECODE_ERROR;
2773 if (len != (unsigned) size - 3)
2775 *al = TLS1_AD_DECODE_ERROR;
2778 if (s->s3->alpn_selected)
2779 OPENSSL_free(s->s3->alpn_selected);
2780 s->s3->alpn_selected = OPENSSL_malloc(len);
2781 if (!s->s3->alpn_selected)
2783 *al = TLS1_AD_INTERNAL_ERROR;
2786 memcpy(s->s3->alpn_selected, data + 3, len);
2787 s->s3->alpn_selected_len = len;
2789 #ifndef OPENSSL_NO_HEARTBEATS
2790 else if (type == TLSEXT_TYPE_heartbeat)
2794 case 0x01: /* Server allows us to send HB requests */
2795 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2797 case 0x02: /* Server doesn't accept HB requests */
2798 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2799 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2801 default: *al = SSL_AD_ILLEGAL_PARAMETER;
2806 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp)
2808 if(ssl_parse_serverhello_use_srtp_ext(s, data, size,
2812 #ifdef TLSEXT_TYPE_encrypt_then_mac
2813 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2815 /* Ignore if inappropriate ciphersuite */
2816 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2817 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2818 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2821 /* If this extension type was not otherwise handled, but
2822 * matches a custom_cli_ext_record, then send it to the c
2824 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2832 *al = SSL_AD_DECODE_ERROR;
2836 if (!s->hit && tlsext_servername == 1)
2838 if (s->tlsext_hostname)
2840 if (s->session->tlsext_hostname == NULL)
2842 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2843 if (!s->session->tlsext_hostname)
2845 *al = SSL_AD_UNRECOGNIZED_NAME;
2851 *al = SSL_AD_DECODE_ERROR;
2861 /* Determine if we need to see RI. Strictly speaking if we want to
2862 * avoid an attack we should *always* see RI even on initial server
2863 * hello because the client doesn't see any renegotiation during an
2864 * attack. However this would mean we could not connect to any server
2865 * which doesn't support RI so for the immediate future tolerate RI
2866 * absence on initial connect only.
2868 if (!renegotiate_seen
2869 && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2870 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
2872 *al = SSL_AD_HANDSHAKE_FAILURE;
2873 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2874 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2882 int ssl_prepare_clienthello_tlsext(SSL *s)
2885 #ifdef TLSEXT_TYPE_opaque_prf_input
2889 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2891 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2896 if (s->tlsext_opaque_prf_input != NULL)
2898 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
2899 OPENSSL_free(s->s3->client_opaque_prf_input);
2901 if (s->tlsext_opaque_prf_input_len == 0)
2902 s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2904 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2905 if (s->s3->client_opaque_prf_input == NULL)
2907 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
2910 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2914 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
2915 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2922 int ssl_prepare_serverhello_tlsext(SSL *s)
2927 static int ssl_check_clienthello_tlsext_early(SSL *s)
2929 int ret=SSL_TLSEXT_ERR_NOACK;
2930 int al = SSL_AD_UNRECOGNIZED_NAME;
2932 #ifndef OPENSSL_NO_EC
2933 /* The handling of the ECPointFormats extension is done elsewhere, namely in
2934 * ssl3_choose_cipher in s3_lib.c.
2936 /* The handling of the EllipticCurves extension is done elsewhere, namely in
2937 * ssl3_choose_cipher in s3_lib.c.
2941 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2942 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
2943 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
2944 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
2946 #ifdef TLSEXT_TYPE_opaque_prf_input
2948 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
2949 * but we might be sending an alert in response to the client hello,
2950 * so this has to happen here in
2951 * ssl_check_clienthello_tlsext_early(). */
2955 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
2957 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
2960 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2961 al = SSL_AD_INTERNAL_ERROR;
2966 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
2967 OPENSSL_free(s->s3->server_opaque_prf_input);
2968 s->s3->server_opaque_prf_input = NULL;
2970 if (s->tlsext_opaque_prf_input != NULL)
2972 if (s->s3->client_opaque_prf_input != NULL &&
2973 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
2975 /* can only use this extension if we have a server opaque PRF input
2976 * of the same length as the client opaque PRF input! */
2978 if (s->tlsext_opaque_prf_input_len == 0)
2979 s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */
2981 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
2982 if (s->s3->server_opaque_prf_input == NULL)
2984 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2985 al = SSL_AD_INTERNAL_ERROR;
2988 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
2992 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
2994 /* The callback wants to enforce use of the extension,
2995 * but we can't do that with the client opaque PRF input;
2996 * abort the handshake.
2998 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2999 al = SSL_AD_HANDSHAKE_FAILURE;
3007 case SSL_TLSEXT_ERR_ALERT_FATAL:
3008 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3011 case SSL_TLSEXT_ERR_ALERT_WARNING:
3012 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3015 case SSL_TLSEXT_ERR_NOACK:
3016 s->servername_done=0;
3022 int tls1_set_server_sigalgs(SSL *s)
3026 /* Clear any shared sigtnature algorithms */
3027 if (s->cert->shared_sigalgs)
3029 OPENSSL_free(s->cert->shared_sigalgs);
3030 s->cert->shared_sigalgs = NULL;
3032 /* Clear certificate digests and validity flags */
3033 for (i = 0; i < SSL_PKEY_NUM; i++)
3035 s->cert->pkeys[i].digest = NULL;
3036 s->cert->pkeys[i].valid_flags = 0;
3039 /* If sigalgs received process it. */
3040 if (s->cert->peer_sigalgs)
3042 if (!tls1_process_sigalgs(s))
3044 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
3045 ERR_R_MALLOC_FAILURE);
3046 al = SSL_AD_INTERNAL_ERROR;
3049 /* Fatal error is no shared signature algorithms */
3050 if (!s->cert->shared_sigalgs)
3052 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
3053 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
3054 al = SSL_AD_ILLEGAL_PARAMETER;
3059 ssl_cert_set_default_md(s->cert);
3062 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3066 int ssl_check_clienthello_tlsext_late(SSL *s)
3068 int ret = SSL_TLSEXT_ERR_OK;
3071 /* If status request then ask callback what to do.
3072 * Note: this must be called after servername callbacks in case
3073 * the certificate has changed, and must be called after the cipher
3074 * has been chosen because this may influence which certificate is sent
3076 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb)
3079 CERT_PKEY *certpkey;
3080 certpkey = ssl_get_server_send_pkey(s);
3081 /* If no certificate can't return certificate status */
3082 if (certpkey == NULL)
3084 s->tlsext_status_expected = 0;
3087 /* Set current certificate to one we will use so
3088 * SSL_get_certificate et al can pick it up.
3090 s->cert->key = certpkey;
3091 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3094 /* We don't want to send a status request response */
3095 case SSL_TLSEXT_ERR_NOACK:
3096 s->tlsext_status_expected = 0;
3098 /* status request response should be sent */
3099 case SSL_TLSEXT_ERR_OK:
3100 if (s->tlsext_ocsp_resp)
3101 s->tlsext_status_expected = 1;
3103 s->tlsext_status_expected = 0;
3105 /* something bad happened */
3106 case SSL_TLSEXT_ERR_ALERT_FATAL:
3107 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3108 al = SSL_AD_INTERNAL_ERROR;
3113 s->tlsext_status_expected = 0;
3118 case SSL_TLSEXT_ERR_ALERT_FATAL:
3119 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3122 case SSL_TLSEXT_ERR_ALERT_WARNING:
3123 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3131 int ssl_check_serverhello_tlsext(SSL *s)
3133 int ret=SSL_TLSEXT_ERR_NOACK;
3134 int al = SSL_AD_UNRECOGNIZED_NAME;
3136 #ifndef OPENSSL_NO_EC
3137 /* If we are client and using an elliptic curve cryptography cipher
3138 * suite, then if server returns an EC point formats lists extension
3139 * it must contain uncompressed.
3141 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3142 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3143 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
3144 (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) &&
3145 ((alg_k & (SSL_kECDHE|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
3147 /* we are using an ECC cipher */
3149 unsigned char *list;
3150 int found_uncompressed = 0;
3151 list = s->session->tlsext_ecpointformatlist;
3152 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
3154 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
3156 found_uncompressed = 1;
3160 if (!found_uncompressed)
3162 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3166 ret = SSL_TLSEXT_ERR_OK;
3167 #endif /* OPENSSL_NO_EC */
3169 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3170 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
3171 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
3172 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
3174 #ifdef TLSEXT_TYPE_opaque_prf_input
3175 if (s->s3->server_opaque_prf_input_len > 0)
3177 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
3178 * So first verify that we really have a value from the server too. */
3180 if (s->s3->server_opaque_prf_input == NULL)
3182 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3183 al = SSL_AD_HANDSHAKE_FAILURE;
3186 /* Anytime the server *has* sent an opaque PRF input, we need to check
3187 * that we have a client opaque PRF input of the same size. */
3188 if (s->s3->client_opaque_prf_input == NULL ||
3189 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
3191 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3192 al = SSL_AD_ILLEGAL_PARAMETER;
3197 /* If we've requested certificate status and we wont get one
3200 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3201 && s->ctx && s->ctx->tlsext_status_cb)
3204 /* Set resp to NULL, resplen to -1 so callback knows
3205 * there is no response.
3207 if (s->tlsext_ocsp_resp)
3209 OPENSSL_free(s->tlsext_ocsp_resp);
3210 s->tlsext_ocsp_resp = NULL;
3212 s->tlsext_ocsp_resplen = -1;
3213 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3216 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3217 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3221 al = SSL_AD_INTERNAL_ERROR;
3222 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3228 case SSL_TLSEXT_ERR_ALERT_FATAL:
3229 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3232 case SSL_TLSEXT_ERR_ALERT_WARNING:
3233 ssl3_send_alert(s,SSL3_AL_WARNING,al);
3236 case SSL_TLSEXT_ERR_NOACK:
3237 s->servername_done=0;
3243 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n)
3246 if (s->version < SSL3_VERSION)
3248 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0)
3250 ssl3_send_alert(s,SSL3_AL_FATAL,al);
3254 if (ssl_check_serverhello_tlsext(s) <= 0)
3256 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,SSL_R_SERVERHELLO_TLSEXT);
3263 * Since the server cache lookup is done early on in the processing of the
3264 * ClientHello, and other operations depend on the result, we need to handle
3265 * any TLS session ticket extension at the same time.
3267 * session_id: points at the session ID in the ClientHello. This code will
3268 * read past the end of this in order to parse out the session ticket
3269 * extension, if any.
3270 * len: the length of the session ID.
3271 * limit: a pointer to the first byte after the ClientHello.
3272 * ret: (output) on return, if a ticket was decrypted, then this is set to
3273 * point to the resulting session.
3275 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3276 * ciphersuite, in which case we have no use for session tickets and one will
3277 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3280 * -1: fatal error, either from parsing or decrypting the ticket.
3281 * 0: no ticket was found (or was ignored, based on settings).
3282 * 1: a zero length extension was found, indicating that the client supports
3283 * session tickets but doesn't currently have one to offer.
3284 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
3285 * couldn't be decrypted because of a non-fatal error.
3286 * 3: a ticket was successfully decrypted and *ret was set.
3289 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3290 * a new session ticket to the client because the client indicated support
3291 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3292 * a session ticket or we couldn't use the one it gave us, or if
3293 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3294 * Otherwise, s->tlsext_ticket_expected is set to 0.
3296 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
3297 const unsigned char *limit, SSL_SESSION **ret)
3299 /* Point after session ID in client hello */
3300 const unsigned char *p = session_id + len;
3304 s->tlsext_ticket_expected = 0;
3306 /* If tickets disabled behave as if no ticket present
3307 * to permit stateful resumption.
3309 if (!tls_use_ticket(s))
3311 if ((s->version <= SSL3_VERSION) || !limit)
3315 /* Skip past DTLS cookie */
3323 /* Skip past cipher list */
3328 /* Skip past compression algorithm list */
3333 /* Now at start of extensions */
3334 if ((p + 2) >= limit)
3337 while ((p + 4) <= limit)
3339 unsigned short type, size;
3342 if (p + size > limit)
3344 if (type == TLSEXT_TYPE_session_ticket)
3349 /* The client will accept a ticket but doesn't
3350 * currently have one. */
3351 s->tlsext_ticket_expected = 1;
3354 if (s->tls_session_secret_cb)
3356 /* Indicate that the ticket couldn't be
3357 * decrypted rather than generating the session
3358 * from ticket now, trigger abbreviated
3359 * handshake based on external mechanism to
3360 * calculate the master secret later. */
3363 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
3366 case 2: /* ticket couldn't be decrypted */
3367 s->tlsext_ticket_expected = 1;
3369 case 3: /* ticket was decrypted */
3371 case 4: /* ticket decrypted but need to renew */
3372 s->tlsext_ticket_expected = 1;
3374 default: /* fatal error */
3384 * tls_decrypt_ticket attempts to decrypt a session ticket.
3386 * etick: points to the body of the session ticket extension.
3387 * eticklen: the length of the session tickets extenion.
3388 * sess_id: points at the session ID.
3389 * sesslen: the length of the session ID.
3390 * psess: (output) on return, if a ticket was decrypted, then this is set to
3391 * point to the resulting session.
3394 * -1: fatal error, either from parsing or decrypting the ticket.
3395 * 2: the ticket couldn't be decrypted.
3396 * 3: a ticket was successfully decrypted and *psess was set.
3397 * 4: same as 3, but the ticket needs to be renewed.
3399 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
3400 const unsigned char *sess_id, int sesslen,
3401 SSL_SESSION **psess)
3404 unsigned char *sdec;
3405 const unsigned char *p;
3406 int slen, mlen, renew_ticket = 0;
3407 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3410 SSL_CTX *tctx = s->initial_ctx;
3411 /* Need at least keyname + iv + some encrypted data */
3414 /* Initialize session ticket encryption and HMAC contexts */
3415 HMAC_CTX_init(&hctx);
3416 EVP_CIPHER_CTX_init(&ctx);
3417 if (tctx->tlsext_ticket_key_cb)
3419 unsigned char *nctick = (unsigned char *)etick;
3420 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3431 /* Check key name matches */
3432 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3434 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3435 tlsext_tick_md(), NULL);
3436 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3437 tctx->tlsext_tick_aes_key, etick + 16);
3439 /* Attempt to process session ticket, first conduct sanity and
3440 * integrity checks on ticket.
3442 mlen = HMAC_size(&hctx);
3445 EVP_CIPHER_CTX_cleanup(&ctx);
3449 /* Check HMAC of encrypted ticket */
3450 HMAC_Update(&hctx, etick, eticklen);
3451 HMAC_Final(&hctx, tick_hmac, NULL);
3452 HMAC_CTX_cleanup(&hctx);
3453 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen))
3455 EVP_CIPHER_CTX_cleanup(&ctx);
3458 /* Attempt to decrypt session data */
3459 /* Move p after IV to start of encrypted ticket, update length */
3460 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3461 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3462 sdec = OPENSSL_malloc(eticklen);
3465 EVP_CIPHER_CTX_cleanup(&ctx);
3468 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
3469 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
3471 EVP_CIPHER_CTX_cleanup(&ctx);
3476 EVP_CIPHER_CTX_cleanup(&ctx);
3479 sess = d2i_SSL_SESSION(NULL, &p, slen);
3483 /* The session ID, if non-empty, is used by some clients to
3484 * detect that the ticket has been accepted. So we copy it to
3485 * the session structure. If it is empty set length to zero
3486 * as required by standard.
3489 memcpy(sess->session_id, sess_id, sesslen);
3490 sess->session_id_length = sesslen;
3498 /* For session parse failure, indicate that we need to send a new
3503 /* Tables to translate from NIDs to TLS v1.2 ids */
3511 static const tls12_lookup tls12_md[] = {
3512 {NID_md5, TLSEXT_hash_md5},
3513 {NID_sha1, TLSEXT_hash_sha1},
3514 {NID_sha224, TLSEXT_hash_sha224},
3515 {NID_sha256, TLSEXT_hash_sha256},
3516 {NID_sha384, TLSEXT_hash_sha384},
3517 {NID_sha512, TLSEXT_hash_sha512}
3520 static const tls12_lookup tls12_sig[] = {
3521 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3522 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3523 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3526 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3529 for (i = 0; i < tlen; i++)
3531 if (table[i].nid == nid)
3537 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3540 for (i = 0; i < tlen; i++)
3542 if ((table[i].id) == id)
3543 return table[i].nid;
3548 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3553 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3554 sizeof(tls12_md)/sizeof(tls12_lookup));
3557 sig_id = tls12_get_sigid(pk);
3560 p[0] = (unsigned char)md_id;
3561 p[1] = (unsigned char)sig_id;
3565 int tls12_get_sigid(const EVP_PKEY *pk)
3567 return tls12_find_id(pk->type, tls12_sig,
3568 sizeof(tls12_sig)/sizeof(tls12_lookup));
3575 const EVP_MD *(*mfunc)(void);
3578 static const tls12_hash_info tls12_md_info[] = {
3579 #ifdef OPENSSL_NO_MD5
3582 {NID_md5, 64, EVP_md5},
3584 #ifdef OPENSSL_NO_SHA
3587 {NID_sha1, 80, EVP_sha1},
3589 #ifdef OPENSSL_NO_SHA256
3590 {NID_sha224, 112, 0},
3591 {NID_sha256, 128, 0},
3593 {NID_sha224, 112, EVP_sha224},
3594 {NID_sha256, 128, EVP_sha256},
3596 #ifdef OPENSSL_NO_SHA512
3597 {NID_sha384, 192, 0},
3598 {NID_sha512, 256, 0}
3600 {NID_sha384, 192, EVP_sha384},
3601 {NID_sha512, 256, EVP_sha512}
3605 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3609 if (hash_alg > sizeof(tls12_md_info)/sizeof(tls12_md_info[0]))
3611 return tls12_md_info + hash_alg - 1;
3614 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3616 const tls12_hash_info *inf;
3617 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3619 inf = tls12_get_hash_info(hash_alg);
3620 if (!inf || !inf->mfunc)
3622 return inf->mfunc();
3625 static int tls12_get_pkey_idx(unsigned char sig_alg)
3629 #ifndef OPENSSL_NO_RSA
3630 case TLSEXT_signature_rsa:
3631 return SSL_PKEY_RSA_SIGN;
3633 #ifndef OPENSSL_NO_DSA
3634 case TLSEXT_signature_dsa:
3635 return SSL_PKEY_DSA_SIGN;
3637 #ifndef OPENSSL_NO_ECDSA
3638 case TLSEXT_signature_ecdsa:
3639 return SSL_PKEY_ECC;
3645 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3646 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3647 int *psignhash_nid, const unsigned char *data)
3649 int sign_nid = 0, hash_nid = 0;
3650 if (!phash_nid && !psign_nid && !psignhash_nid)
3652 if (phash_nid || psignhash_nid)
3654 hash_nid = tls12_find_nid(data[0], tls12_md,
3655 sizeof(tls12_md)/sizeof(tls12_lookup));
3657 *phash_nid = hash_nid;
3659 if (psign_nid || psignhash_nid)
3661 sign_nid = tls12_find_nid(data[1], tls12_sig,
3662 sizeof(tls12_sig)/sizeof(tls12_lookup));
3664 *psign_nid = sign_nid;
3668 if (sign_nid && hash_nid)
3669 OBJ_find_sigid_by_algs(psignhash_nid,
3670 hash_nid, sign_nid);
3672 *psignhash_nid = NID_undef;
3675 /* Check to see if a signature algorithm is allowed */
3676 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3678 /* See if we have an entry in the hash table and it is enabled */
3679 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3680 if (!hinf || !hinf->mfunc)
3682 /* See if public key algorithm allowed */
3683 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3685 /* Finally see if security callback allows it */
3686 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3689 /* Get a mask of disabled public key algorithms based on supported
3690 * signature algorithms. For example if no signature algorithm supports RSA
3691 * then RSA is disabled.
3694 void ssl_set_sig_mask(unsigned long *pmask_a, SSL *s, int op)
3696 const unsigned char *sigalgs;
3697 size_t i, sigalgslen;
3698 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3699 /* Now go through all signature algorithms seeing if we support
3700 * any for RSA, DSA, ECDSA. Do this for all versions not just
3701 * TLS 1.2. To keep down calls to security callback only check
3704 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3705 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2)
3709 #ifndef OPENSSL_NO_RSA
3710 case TLSEXT_signature_rsa:
3711 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3715 #ifndef OPENSSL_NO_DSA
3716 case TLSEXT_signature_dsa:
3717 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3721 #ifndef OPENSSL_NO_ECDSA
3722 case TLSEXT_signature_ecdsa:
3723 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3730 *pmask_a |= SSL_aRSA;
3732 *pmask_a |= SSL_aDSS;
3734 *pmask_a |= SSL_aECDSA;
3737 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3738 const unsigned char *psig, size_t psiglen)
3740 unsigned char *tmpout = out;
3742 for (i = 0; i < psiglen; i += 2, psig += 2)
3744 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig))
3746 *tmpout++ = psig[0];
3747 *tmpout++ = psig[1];
3750 return tmpout - out;
3753 /* Given preference and allowed sigalgs set shared sigalgs */
3754 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3755 const unsigned char *pref, size_t preflen,
3756 const unsigned char *allow, size_t allowlen)
3758 const unsigned char *ptmp, *atmp;
3759 size_t i, j, nmatch = 0;
3760 for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2)
3762 /* Skip disabled hashes or signature algorithms */
3763 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3765 for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2)
3767 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1])
3772 shsig->rhash = ptmp[0];
3773 shsig->rsign = ptmp[1];
3774 tls1_lookup_sigalg(&shsig->hash_nid,
3776 &shsig->signandhash_nid,
3787 /* Set shared signature algorithms for SSL structures */
3788 static int tls1_set_shared_sigalgs(SSL *s)
3790 const unsigned char *pref, *allow, *conf;
3791 size_t preflen, allowlen, conflen;
3793 TLS_SIGALGS *salgs = NULL;
3795 unsigned int is_suiteb = tls1_suiteb(s);
3796 if (c->shared_sigalgs)
3798 OPENSSL_free(c->shared_sigalgs);
3799 c->shared_sigalgs = NULL;
3801 /* If client use client signature algorithms if not NULL */
3802 if (!s->server && c->client_sigalgs && !is_suiteb)
3804 conf = c->client_sigalgs;
3805 conflen = c->client_sigalgslen;
3807 else if (c->conf_sigalgs && !is_suiteb)
3809 conf = c->conf_sigalgs;
3810 conflen = c->conf_sigalgslen;
3813 conflen = tls12_get_psigalgs(s, &conf);
3814 if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb)
3818 allow = c->peer_sigalgs;
3819 allowlen = c->peer_sigalgslen;
3825 pref = c->peer_sigalgs;
3826 preflen = c->peer_sigalgslen;
3828 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3831 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3834 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3835 c->shared_sigalgs = salgs;
3836 c->shared_sigalgslen = nmatch;
3841 /* Set preferred digest for each key type */
3843 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3846 /* Extension ignored for inappropriate versions */
3847 if (!SSL_USE_SIGALGS(s))
3849 /* Should never happen */
3853 if (c->peer_sigalgs)
3854 OPENSSL_free(c->peer_sigalgs);
3855 c->peer_sigalgs = OPENSSL_malloc(dsize);
3856 if (!c->peer_sigalgs)
3858 c->peer_sigalgslen = dsize;
3859 memcpy(c->peer_sigalgs, data, dsize);
3863 int tls1_process_sigalgs(SSL *s)
3869 TLS_SIGALGS *sigptr;
3870 if (!tls1_set_shared_sigalgs(s))
3873 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3874 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
3876 /* Use first set signature preference to force message
3877 * digest, ignoring any peer preferences.
3879 const unsigned char *sigs = NULL;
3881 sigs = c->conf_sigalgs;
3883 sigs = c->client_sigalgs;
3886 idx = tls12_get_pkey_idx(sigs[1]);
3887 md = tls12_get_hash(sigs[0]);
3888 c->pkeys[idx].digest = md;
3889 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3890 if (idx == SSL_PKEY_RSA_SIGN)
3892 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3893 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3899 for (i = 0, sigptr = c->shared_sigalgs;
3900 i < c->shared_sigalgslen; i++, sigptr++)
3902 idx = tls12_get_pkey_idx(sigptr->rsign);
3903 if (idx > 0 && c->pkeys[idx].digest == NULL)
3905 md = tls12_get_hash(sigptr->rhash);
3906 c->pkeys[idx].digest = md;
3907 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3908 if (idx == SSL_PKEY_RSA_SIGN)
3910 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3911 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3916 /* In strict mode leave unset digests as NULL to indicate we can't
3917 * use the certificate for signing.
3919 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT))
3921 /* Set any remaining keys to default values. NOTE: if alg is
3922 * not supported it stays as NULL.
3924 #ifndef OPENSSL_NO_DSA
3925 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
3926 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
3928 #ifndef OPENSSL_NO_RSA
3929 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest)
3931 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
3932 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
3935 #ifndef OPENSSL_NO_ECDSA
3936 if (!c->pkeys[SSL_PKEY_ECC].digest)
3937 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
3944 int SSL_get_sigalgs(SSL *s, int idx,
3945 int *psign, int *phash, int *psignhash,
3946 unsigned char *rsig, unsigned char *rhash)
3948 const unsigned char *psig = s->cert->peer_sigalgs;
3954 if (idx >= (int)s->cert->peer_sigalgslen)
3961 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3963 return s->cert->peer_sigalgslen / 2;
3966 int SSL_get_shared_sigalgs(SSL *s, int idx,
3967 int *psign, int *phash, int *psignhash,
3968 unsigned char *rsig, unsigned char *rhash)
3970 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3971 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3975 *phash = shsigalgs->hash_nid;
3977 *psign = shsigalgs->sign_nid;
3979 *psignhash = shsigalgs->signandhash_nid;
3981 *rsig = shsigalgs->rsign;
3983 *rhash = shsigalgs->rhash;
3984 return s->cert->shared_sigalgslen;
3988 #ifndef OPENSSL_NO_HEARTBEATS
3990 tls1_process_heartbeat(SSL *s)
3992 unsigned char *p = &s->s3->rrec.data[0], *pl;
3993 unsigned short hbtype;
3994 unsigned int payload;
3995 unsigned int padding = 16; /* Use minimum padding */
3997 if (s->msg_callback)
3998 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
3999 &s->s3->rrec.data[0], s->s3->rrec.length,
4000 s, s->msg_callback_arg);
4002 /* Read type and payload length first */
4003 if (1 + 2 + 16 > s->s3->rrec.length)
4004 return 0; /* silently discard */
4007 if (1 + 2 + payload + 16 > s->s3->rrec.length)
4008 return 0; /* silently discard per RFC 6520 sec. 4 */
4011 if (hbtype == TLS1_HB_REQUEST)
4013 unsigned char *buffer, *bp;
4016 /* Allocate memory for the response, size is 1 bytes
4017 * message type, plus 2 bytes payload length, plus
4018 * payload, plus padding
4020 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
4023 SSLerr(SSL_F_TLS1_PROCESS_HEARTBEAT,ERR_R_MALLOC_FAILURE);
4028 /* Enter response type, length and copy payload */
4029 *bp++ = TLS1_HB_RESPONSE;
4031 memcpy(bp, pl, payload);
4033 /* Random padding */
4034 RAND_pseudo_bytes(bp, padding);
4036 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding);
4038 if (r >= 0 && s->msg_callback)
4039 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4040 buffer, 3 + payload + padding,
4041 s, s->msg_callback_arg);
4043 OPENSSL_free(buffer);
4048 else if (hbtype == TLS1_HB_RESPONSE)
4052 /* We only send sequence numbers (2 bytes unsigned int),
4053 * and 16 random bytes, so we just try to read the
4054 * sequence number */
4057 if (payload == 18 && seq == s->tlsext_hb_seq)
4060 s->tlsext_hb_pending = 0;
4068 tls1_heartbeat(SSL *s)
4070 unsigned char *buf, *p;
4072 unsigned int payload = 18; /* Sequence number + random bytes */
4073 unsigned int padding = 16; /* Use minimum padding */
4075 /* Only send if peer supports and accepts HB requests... */
4076 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
4077 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS)
4079 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
4083 /* ...and there is none in flight yet... */
4084 if (s->tlsext_hb_pending)
4086 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING);
4090 /* ...and no handshake in progress. */
4091 if (SSL_in_init(s) || s->in_handshake)
4093 SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE);
4097 /* Check if padding is too long, payload and padding
4098 * must not exceed 2^14 - 3 = 16381 bytes in total.
4100 OPENSSL_assert(payload + padding <= 16381);
4103 * Create HeartBeat message, we just use a sequence number
4104 * as payload to distuingish different messages and add
4105 * some random stuff.
4106 * - Message Type, 1 byte
4107 * - Payload Length, 2 bytes (unsigned int)
4108 * - Payload, the sequence number (2 bytes uint)
4109 * - Payload, random bytes (16 bytes uint)
4112 buf = OPENSSL_malloc(1 + 2 + payload + padding);
4115 SSLerr(SSL_F_TLS1_HEARTBEAT,ERR_R_MALLOC_FAILURE);
4120 *p++ = TLS1_HB_REQUEST;
4121 /* Payload length (18 bytes here) */
4123 /* Sequence number */
4124 s2n(s->tlsext_hb_seq, p);
4125 /* 16 random bytes */
4126 RAND_pseudo_bytes(p, 16);
4128 /* Random padding */
4129 RAND_pseudo_bytes(p, padding);
4131 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
4134 if (s->msg_callback)
4135 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4136 buf, 3 + payload + padding,
4137 s, s->msg_callback_arg);
4139 s->tlsext_hb_pending = 1;
4148 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
4153 int sigalgs[MAX_SIGALGLEN];
4156 static int sig_cb(const char *elem, int len, void *arg)
4158 sig_cb_st *sarg = arg;
4161 int sig_alg, hash_alg;
4162 if (sarg->sigalgcnt == MAX_SIGALGLEN)
4164 if (len > (int)(sizeof(etmp) - 1))
4166 memcpy(etmp, elem, len);
4168 p = strchr(etmp, '+');
4176 if (!strcmp(etmp, "RSA"))
4177 sig_alg = EVP_PKEY_RSA;
4178 else if (!strcmp(etmp, "DSA"))
4179 sig_alg = EVP_PKEY_DSA;
4180 else if (!strcmp(etmp, "ECDSA"))
4181 sig_alg = EVP_PKEY_EC;
4184 hash_alg = OBJ_sn2nid(p);
4185 if (hash_alg == NID_undef)
4186 hash_alg = OBJ_ln2nid(p);
4187 if (hash_alg == NID_undef)
4190 for (i = 0; i < sarg->sigalgcnt; i+=2)
4192 if (sarg->sigalgs[i] == sig_alg
4193 && sarg->sigalgs[i + 1] == hash_alg)
4196 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
4197 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
4201 /* Set suppored signature algorithms based on a colon separated list
4202 * of the form sig+hash e.g. RSA+SHA512:DSA+SHA512 */
4203 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
4207 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
4211 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
4214 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
4216 unsigned char *sigalgs, *sptr;
4221 sigalgs = OPENSSL_malloc(salglen);
4222 if (sigalgs == NULL)
4224 for (i = 0, sptr = sigalgs; i < salglen; i+=2)
4226 rhash = tls12_find_id(*psig_nids++, tls12_md,
4227 sizeof(tls12_md)/sizeof(tls12_lookup));
4228 rsign = tls12_find_id(*psig_nids++, tls12_sig,
4229 sizeof(tls12_sig)/sizeof(tls12_lookup));
4231 if (rhash == -1 || rsign == -1)
4239 if (c->client_sigalgs)
4240 OPENSSL_free(c->client_sigalgs);
4241 c->client_sigalgs = sigalgs;
4242 c->client_sigalgslen = salglen;
4246 if (c->conf_sigalgs)
4247 OPENSSL_free(c->conf_sigalgs);
4248 c->conf_sigalgs = sigalgs;
4249 c->conf_sigalgslen = salglen;
4255 OPENSSL_free(sigalgs);
4259 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
4263 if (default_nid == -1)
4265 sig_nid = X509_get_signature_nid(x);
4267 return sig_nid == default_nid ? 1 : 0;
4268 for (i = 0; i < c->shared_sigalgslen; i++)
4269 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
4273 /* Check to see if a certificate issuer name matches list of CA names */
4274 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
4278 nm = X509_get_issuer_name(x);
4279 for (i = 0; i < sk_X509_NAME_num(names); i++)
4281 if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
4287 /* Check certificate chain is consistent with TLS extensions and is
4288 * usable by server. This servers two purposes: it allows users to
4289 * check chains before passing them to the server and it allows the
4290 * server to check chains before attempting to use them.
4293 /* Flags which need to be set for a certificate when stict mode not set */
4295 #define CERT_PKEY_VALID_FLAGS \
4296 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
4297 /* Strict mode flags */
4298 #define CERT_PKEY_STRICT_FLAGS \
4299 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
4300 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
4302 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
4307 int check_flags = 0, strict_mode;
4308 CERT_PKEY *cpk = NULL;
4310 unsigned int suiteb_flags = tls1_suiteb(s);
4311 /* idx == -1 means checking server chains */
4314 /* idx == -2 means checking client certificate chains */
4318 idx = cpk - c->pkeys;
4321 cpk = c->pkeys + idx;
4323 pk = cpk->privatekey;
4325 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
4326 /* If no cert or key, forget it */
4329 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
4330 /* Allow any certificate to pass test */
4331 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
4333 rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN;
4334 cpk->valid_flags = rv;
4343 idx = ssl_cert_type(x, pk);
4346 cpk = c->pkeys + idx;
4347 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
4348 check_flags = CERT_PKEY_STRICT_FLAGS;
4350 check_flags = CERT_PKEY_VALID_FLAGS;
4358 check_flags |= CERT_PKEY_SUITEB;
4359 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
4360 if (ok == X509_V_OK)
4361 rv |= CERT_PKEY_SUITEB;
4362 else if (!check_flags)
4366 /* Check all signature algorithms are consistent with
4367 * signature algorithms extension if TLS 1.2 or later
4370 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode)
4373 unsigned char rsign = 0;
4374 if (c->peer_sigalgs)
4376 /* If no sigalgs extension use defaults from RFC5246 */
4381 case SSL_PKEY_RSA_ENC:
4382 case SSL_PKEY_RSA_SIGN:
4383 case SSL_PKEY_DH_RSA:
4384 rsign = TLSEXT_signature_rsa;
4385 default_nid = NID_sha1WithRSAEncryption;
4388 case SSL_PKEY_DSA_SIGN:
4389 case SSL_PKEY_DH_DSA:
4390 rsign = TLSEXT_signature_dsa;
4391 default_nid = NID_dsaWithSHA1;
4395 rsign = TLSEXT_signature_ecdsa;
4396 default_nid = NID_ecdsa_with_SHA1;
4404 /* If peer sent no signature algorithms extension and we
4405 * have set preferred signature algorithms check we support
4408 if (default_nid > 0 && c->conf_sigalgs)
4411 const unsigned char *p = c->conf_sigalgs;
4412 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2)
4414 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4417 if (j == c->conf_sigalgslen)
4425 /* Check signature algorithm of each cert in chain */
4426 if (!tls1_check_sig_alg(c, x, default_nid))
4428 if (!check_flags) goto end;
4431 rv |= CERT_PKEY_EE_SIGNATURE;
4432 rv |= CERT_PKEY_CA_SIGNATURE;
4433 for (i = 0; i < sk_X509_num(chain); i++)
4435 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i),
4440 rv &= ~CERT_PKEY_CA_SIGNATURE;
4448 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4449 else if(check_flags)
4450 rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE;
4452 /* Check cert parameters are consistent */
4453 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4454 rv |= CERT_PKEY_EE_PARAM;
4455 else if (!check_flags)
4458 rv |= CERT_PKEY_CA_PARAM;
4459 /* In strict mode check rest of chain too */
4460 else if (strict_mode)
4462 rv |= CERT_PKEY_CA_PARAM;
4463 for (i = 0; i < sk_X509_num(chain); i++)
4465 X509 *ca = sk_X509_value(chain, i);
4466 if (!tls1_check_cert_param(s, ca, 0))
4470 rv &= ~CERT_PKEY_CA_PARAM;
4478 if (!s->server && strict_mode)
4480 STACK_OF(X509_NAME) *ca_dn;
4485 check_type = TLS_CT_RSA_SIGN;
4488 check_type = TLS_CT_DSS_SIGN;
4491 check_type = TLS_CT_ECDSA_SIGN;
4496 int cert_type = X509_certificate_type(x, pk);
4497 if (cert_type & EVP_PKS_RSA)
4498 check_type = TLS_CT_RSA_FIXED_DH;
4499 if (cert_type & EVP_PKS_DSA)
4500 check_type = TLS_CT_DSS_FIXED_DH;
4505 const unsigned char *ctypes;
4510 ctypelen = (int)c->ctype_num;
4514 ctypes = (unsigned char *)s->s3->tmp.ctype;
4515 ctypelen = s->s3->tmp.ctype_num;
4517 for (i = 0; i < ctypelen; i++)
4519 if (ctypes[i] == check_type)
4521 rv |= CERT_PKEY_CERT_TYPE;
4525 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4529 rv |= CERT_PKEY_CERT_TYPE;
4532 ca_dn = s->s3->tmp.ca_names;
4534 if (!sk_X509_NAME_num(ca_dn))
4535 rv |= CERT_PKEY_ISSUER_NAME;
4537 if (!(rv & CERT_PKEY_ISSUER_NAME))
4539 if (ssl_check_ca_name(ca_dn, x))
4540 rv |= CERT_PKEY_ISSUER_NAME;
4542 if (!(rv & CERT_PKEY_ISSUER_NAME))
4544 for (i = 0; i < sk_X509_num(chain); i++)
4546 X509 *xtmp = sk_X509_value(chain, i);
4547 if (ssl_check_ca_name(ca_dn, xtmp))
4549 rv |= CERT_PKEY_ISSUER_NAME;
4554 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4558 rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE;
4560 if (!check_flags || (rv & check_flags) == check_flags)
4561 rv |= CERT_PKEY_VALID;
4565 if (TLS1_get_version(s) >= TLS1_2_VERSION)
4567 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
4568 rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN;
4569 else if (cpk->digest)
4570 rv |= CERT_PKEY_SIGN;
4573 rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN;
4575 /* When checking a CERT_PKEY structure all flags are irrelevant
4576 * if the chain is invalid.
4580 if (rv & CERT_PKEY_VALID)
4581 cpk->valid_flags = rv;
4584 /* Preserve explicit sign flag, clear rest */
4585 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
4592 /* Set validity of certificates in an SSL structure */
4593 void tls1_set_cert_validity(SSL *s)
4595 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4596 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4597 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4598 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4599 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4600 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4602 /* User level utiity function to check a chain is suitable */
4603 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4605 return tls1_check_chain(s, x, pk, chain, -1);
4610 #ifndef OPENSSL_NO_DH
4611 DH *ssl_get_auto_dh(SSL *s)
4613 int dh_secbits = 80;
4614 if (s->cert->dh_tmp_auto == 2)
4615 return DH_get_1024_160();
4616 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
4618 if (s->s3->tmp.new_cipher->strength_bits == 256)
4625 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4626 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4629 if (dh_secbits >= 128)
4636 BN_set_word(dhp->g, 2);
4637 if (dh_secbits >= 192)
4638 dhp->p = get_rfc3526_prime_8192(NULL);
4640 dhp->p = get_rfc3526_prime_3072(NULL);
4641 if (!dhp->p || !dhp->g)
4648 if (dh_secbits >= 112)
4649 return DH_get_2048_224();
4650 return DH_get_1024_160();
4654 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4657 EVP_PKEY *pkey = X509_get_pubkey(x);
4660 secbits = EVP_PKEY_security_bits(pkey);
4661 EVP_PKEY_free(pkey);
4666 return ssl_security(s, op, secbits, 0, x);
4668 return ssl_ctx_security(ctx, op, secbits, 0, x);
4671 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4673 /* Lookup signature algorithm digest */
4674 int secbits = -1, md_nid = NID_undef, sig_nid;
4675 sig_nid = X509_get_signature_nid(x);
4676 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL))
4679 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4680 secbits = EVP_MD_size(md) * 4;
4683 return ssl_security(s, op, secbits, md_nid, x);
4685 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4688 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4691 vfy = SSL_SECOP_PEER;
4694 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4695 return SSL_R_EE_KEY_TOO_SMALL;
4699 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4700 return SSL_R_CA_KEY_TOO_SMALL;
4702 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4703 return SSL_R_CA_MD_TOO_WEAK;
4707 /* Check security of a chain, if sk includes the end entity certificate
4708 * then x is NULL. If vfy is 1 then we are verifying a peer chain and
4709 * not sending one to the peer.
4710 * Return values: 1 if ok otherwise error code to use
4713 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4715 int rv, start_idx, i;
4718 x = sk_X509_value(sk, 0);
4724 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4728 for (i = start_idx; i < sk_X509_num(sk); i++)
4730 x = sk_X509_value(sk, i);
4731 rv = ssl_security_cert(s, NULL, x, vfy, 0);