2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/hmac.h>
15 #include <openssl/ocsp.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/dh.h>
19 #include <openssl/bn.h>
21 #include <openssl/ct.h>
24 #define CHECKLEN(curr, val, limit) \
25 (((curr) >= (limit)) || (size_t)((limit) - (curr)) < (size_t)(val))
27 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
28 const unsigned char *sess_id, int sesslen,
30 static int ssl_check_clienthello_tlsext_early(SSL *s);
31 static int ssl_check_serverhello_tlsext(SSL *s);
33 SSL3_ENC_METHOD const TLSv1_enc_data = {
37 tls1_generate_master_secret,
38 tls1_change_cipher_state,
39 tls1_final_finish_mac,
40 TLS1_FINISH_MAC_LENGTH,
41 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
42 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
44 tls1_export_keying_material,
46 SSL3_HM_HEADER_LENGTH,
47 ssl3_set_handshake_header,
51 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
55 tls1_generate_master_secret,
56 tls1_change_cipher_state,
57 tls1_final_finish_mac,
58 TLS1_FINISH_MAC_LENGTH,
59 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
60 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
62 tls1_export_keying_material,
63 SSL_ENC_FLAG_EXPLICIT_IV,
64 SSL3_HM_HEADER_LENGTH,
65 ssl3_set_handshake_header,
69 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
73 tls1_generate_master_secret,
74 tls1_change_cipher_state,
75 tls1_final_finish_mac,
76 TLS1_FINISH_MAC_LENGTH,
77 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
78 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
80 tls1_export_keying_material,
81 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
82 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
83 SSL3_HM_HEADER_LENGTH,
84 ssl3_set_handshake_header,
88 long tls1_default_timeout(void)
91 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
92 * http, the cache would over fill
101 s->method->ssl_clear(s);
105 void tls1_free(SSL *s)
107 OPENSSL_free(s->tlsext_session_ticket);
111 void tls1_clear(SSL *s)
114 if (s->method->version == TLS_ANY_VERSION)
115 s->version = TLS_MAX_VERSION;
117 s->version = s->method->version;
120 #ifndef OPENSSL_NO_EC
123 int nid; /* Curve NID */
124 int secbits; /* Bits of security (from SP800-57) */
125 unsigned int flags; /* Flags: currently just field type */
129 * Table of curve information.
130 * Do not delete entries or reorder this array! It is used as a lookup
131 * table: the index of each entry is one less than the TLS curve id.
133 static const tls_curve_info nid_list[] = {
134 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
135 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
136 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
137 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
138 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
139 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
140 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
141 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
142 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
143 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
144 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
145 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
146 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
147 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
148 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
149 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
150 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
151 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
152 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
153 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
154 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
155 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
156 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
157 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
158 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
159 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
160 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
161 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
162 {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
165 static const unsigned char ecformats_default[] = {
166 TLSEXT_ECPOINTFORMAT_uncompressed,
167 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
168 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
171 /* The default curves */
172 static const unsigned char eccurves_default[] = {
173 0, 29, /* X25519 (29) */
174 0, 23, /* secp256r1 (23) */
175 0, 25, /* secp521r1 (25) */
176 0, 24, /* secp384r1 (24) */
179 static const unsigned char suiteb_curves[] = {
180 0, TLSEXT_curve_P_256,
181 0, TLSEXT_curve_P_384
184 int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags)
186 const tls_curve_info *cinfo;
187 /* ECC curves from RFC 4492 and RFC 7027 */
188 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
190 cinfo = nid_list + curve_id - 1;
192 *pflags = cinfo->flags;
196 int tls1_ec_nid2curve_id(int nid)
199 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
200 if (nid_list[i].nid == nid)
207 * Get curves list, if "sess" is set return client curves otherwise
209 * Sets |num_curves| to the number of curves in the list, i.e.,
210 * the length of |pcurves| is 2 * num_curves.
211 * Returns 1 on success and 0 if the client curves list has invalid format.
212 * The latter indicates an internal error: we should not be accepting such
213 * lists in the first place.
214 * TODO(emilia): we should really be storing the curves list in explicitly
215 * parsed form instead. (However, this would affect binary compatibility
216 * so cannot happen in the 1.0.x series.)
218 static int tls1_get_curvelist(SSL *s, int sess,
219 const unsigned char **pcurves, size_t *num_curves)
221 size_t pcurveslen = 0;
224 *pcurves = s->session->tlsext_ellipticcurvelist;
225 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
227 /* For Suite B mode only include P-256, P-384 */
228 switch (tls1_suiteb(s)) {
229 case SSL_CERT_FLAG_SUITEB_128_LOS:
230 *pcurves = suiteb_curves;
231 pcurveslen = sizeof(suiteb_curves);
234 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
235 *pcurves = suiteb_curves;
239 case SSL_CERT_FLAG_SUITEB_192_LOS:
240 *pcurves = suiteb_curves + 2;
244 *pcurves = s->tlsext_ellipticcurvelist;
245 pcurveslen = s->tlsext_ellipticcurvelist_length;
248 *pcurves = eccurves_default;
249 pcurveslen = sizeof(eccurves_default);
253 /* We do not allow odd length arrays to enter the system. */
254 if (pcurveslen & 1) {
255 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
259 *num_curves = pcurveslen / 2;
263 /* See if curve is allowed by security callback */
264 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
266 const tls_curve_info *cinfo;
269 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
271 cinfo = &nid_list[curve[1] - 1];
272 # ifdef OPENSSL_NO_EC2M
273 if (cinfo->flags & TLS_CURVE_CHAR2)
276 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
279 /* Check a curve is one of our preferences */
280 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
282 const unsigned char *curves;
283 size_t num_curves, i;
284 unsigned int suiteb_flags = tls1_suiteb(s);
285 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
287 /* Check curve matches Suite B preferences */
289 unsigned long cid = s->s3->tmp.new_cipher->id;
292 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
293 if (p[2] != TLSEXT_curve_P_256)
295 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
296 if (p[2] != TLSEXT_curve_P_384)
298 } else /* Should never happen */
301 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
303 for (i = 0; i < num_curves; i++, curves += 2) {
304 if (p[1] == curves[0] && p[2] == curves[1])
305 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
311 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
312 * if there is no match.
313 * For nmatch == -1, return number of matches
314 * For nmatch == -2, return the NID of the curve to use for
315 * an EC tmp key, or NID_undef if there is no match.
317 int tls1_shared_curve(SSL *s, int nmatch)
319 const unsigned char *pref, *supp;
320 size_t num_pref, num_supp, i, j;
323 /* Can't do anything on client side */
327 if (tls1_suiteb(s)) {
329 * For Suite B ciphersuite determines curve: we already know
330 * these are acceptable due to previous checks.
332 unsigned long cid = s->s3->tmp.new_cipher->id;
334 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
335 return NID_X9_62_prime256v1; /* P-256 */
336 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
337 return NID_secp384r1; /* P-384 */
338 /* Should never happen */
341 /* If not Suite B just return first preference shared curve */
345 * Avoid truncation. tls1_get_curvelist takes an int
346 * but s->options is a long...
348 if (!tls1_get_curvelist(s,
349 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0,
351 /* In practice, NID_undef == 0 but let's be precise. */
352 return nmatch == -1 ? 0 : NID_undef;
353 if (!tls1_get_curvelist(s,
354 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0,
356 return nmatch == -1 ? 0 : NID_undef;
358 for (k = 0, i = 0; i < num_pref; i++, pref += 2) {
359 const unsigned char *tsupp = supp;
361 for (j = 0; j < num_supp; j++, tsupp += 2) {
362 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
363 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
366 int id = (pref[0] << 8) | pref[1];
368 return tls1_ec_curve_id2nid(id, NULL);
376 /* Out of range (nmatch > k). */
380 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
381 int *curves, size_t ncurves)
383 unsigned char *clist, *p;
386 * Bitmap of curves included to detect duplicates: only works while curve
389 unsigned long dup_list = 0;
390 clist = OPENSSL_malloc(ncurves * 2);
393 for (i = 0, p = clist; i < ncurves; i++) {
394 unsigned long idmask;
396 id = tls1_ec_nid2curve_id(curves[i]);
398 if (!id || (dup_list & idmask)) {
407 *pextlen = ncurves * 2;
411 # define MAX_CURVELIST 28
415 int nid_arr[MAX_CURVELIST];
418 static int nid_cb(const char *elem, int len, void *arg)
420 nid_cb_st *narg = arg;
426 if (narg->nidcnt == MAX_CURVELIST)
428 if (len > (int)(sizeof(etmp) - 1))
430 memcpy(etmp, elem, len);
432 nid = EC_curve_nist2nid(etmp);
433 if (nid == NID_undef)
434 nid = OBJ_sn2nid(etmp);
435 if (nid == NID_undef)
436 nid = OBJ_ln2nid(etmp);
437 if (nid == NID_undef)
439 for (i = 0; i < narg->nidcnt; i++)
440 if (narg->nid_arr[i] == nid)
442 narg->nid_arr[narg->nidcnt++] = nid;
446 /* Set curves based on a colon separate list */
447 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen, const char *str)
451 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
455 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
458 /* For an EC key set TLS id and required compression based on parameters */
459 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
466 /* Determine if it is a prime field */
467 grp = EC_KEY_get0_group(ec);
470 /* Determine curve ID */
471 id = EC_GROUP_get_curve_name(grp);
472 id = tls1_ec_nid2curve_id(id);
473 /* If no id return error: we don't support arbitrary explicit curves */
477 curve_id[1] = (unsigned char)id;
479 if (EC_KEY_get0_public_key(ec) == NULL)
481 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
482 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
484 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
485 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
487 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
493 # define DONT_CHECK_OWN_GROUPS 0
494 # define CHECK_OWN_GROUPS 1
495 /* Check an EC key is compatible with extensions */
496 static int tls1_check_ec_key(SSL *s, unsigned char *curve_id,
497 unsigned char *comp_id, int check_own_groups)
499 const unsigned char *pformats, *pcurves;
500 size_t num_formats, num_curves, i;
504 * If point formats extension present check it, otherwise everything is
505 * supported (see RFC4492).
507 if (comp_id && s->session->tlsext_ecpointformatlist) {
508 pformats = s->session->tlsext_ecpointformatlist;
509 num_formats = s->session->tlsext_ecpointformatlist_length;
510 for (i = 0; i < num_formats; i++, pformats++) {
511 if (*comp_id == *pformats)
514 if (i == num_formats)
520 if (!s->server && !check_own_groups)
523 /* Check curve is consistent with client and server preferences */
524 for (j = check_own_groups ? 0 : 1; j <= 1; j++) {
525 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
527 if (j == 1 && num_curves == 0) {
529 * If we've not received any curves then skip this check.
530 * RFC 4492 does not require the supported elliptic curves extension
531 * so if it is not sent we can just choose any curve.
532 * It is invalid to send an empty list in the elliptic curves
533 * extension, so num_curves == 0 always means no extension.
537 for (i = 0; i < num_curves; i++, pcurves += 2) {
538 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
543 /* For clients can only check sent curve list */
550 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
554 * If we have a custom point format list use it otherwise use default
556 if (s->tlsext_ecpointformatlist) {
557 *pformats = s->tlsext_ecpointformatlist;
558 *num_formats = s->tlsext_ecpointformatlist_length;
560 *pformats = ecformats_default;
561 /* For Suite B we don't support char2 fields */
563 *num_formats = sizeof(ecformats_default) - 1;
565 *num_formats = sizeof(ecformats_default);
570 * Check cert parameters compatible with extensions: currently just checks EC
571 * certificates have compatible curves and compression.
573 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
575 unsigned char comp_id, curve_id[2];
578 pkey = X509_get0_pubkey(x);
581 /* If not EC nothing to do */
582 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
584 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
588 * Can't check curve_id for client certs as we don't have a supported
589 * curves extension. For server certs we will tolerate certificates that
590 * aren't in our own list of curves. If we've been configured to use an EC
591 * cert then we should use it - therefore we use DONT_CHECK_OWN_GROUPS here.
593 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id,
594 DONT_CHECK_OWN_GROUPS);
598 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
599 * SHA384+P-384, adjust digest if necessary.
601 if (set_ee_md && tls1_suiteb(s)) {
607 /* Check to see we have necessary signing algorithm */
608 if (curve_id[1] == TLSEXT_curve_P_256)
609 check_md = NID_ecdsa_with_SHA256;
610 else if (curve_id[1] == TLSEXT_curve_P_384)
611 check_md = NID_ecdsa_with_SHA384;
613 return 0; /* Should never happen */
614 for (i = 0; i < c->shared_sigalgslen; i++)
615 if (check_md == c->shared_sigalgs[i].signandhash_nid)
617 if (i == c->shared_sigalgslen)
619 if (set_ee_md == 2) {
620 if (check_md == NID_ecdsa_with_SHA256)
621 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
623 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
629 # ifndef OPENSSL_NO_EC
631 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
633 * @cid: Cipher ID we're considering using
635 * Checks that the kECDHE cipher suite we're considering using
636 * is compatible with the client extensions.
638 * Returns 0 when the cipher can't be used or 1 when it can.
640 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
643 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
646 if (tls1_suiteb(s)) {
647 unsigned char curve_id[2];
648 /* Curve to check determined by ciphersuite */
649 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
650 curve_id[1] = TLSEXT_curve_P_256;
651 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
652 curve_id[1] = TLSEXT_curve_P_384;
656 /* Check this curve is acceptable */
657 if (!tls1_check_ec_key(s, curve_id, NULL, CHECK_OWN_GROUPS))
661 /* Need a shared curve */
662 if (tls1_shared_curve(s, 0))
666 # endif /* OPENSSL_NO_EC */
670 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
675 #endif /* OPENSSL_NO_EC */
678 * List of supported signature algorithms and hashes. Should make this
679 * customisable at some point, for now include everything we support.
682 #ifdef OPENSSL_NO_RSA
683 # define tlsext_sigalg_rsa(md) /* */
685 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
688 #ifdef OPENSSL_NO_DSA
689 # define tlsext_sigalg_dsa(md) /* */
691 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
695 # define tlsext_sigalg_ecdsa(md)/* */
697 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
700 #define tlsext_sigalg(md) \
701 tlsext_sigalg_rsa(md) \
702 tlsext_sigalg_dsa(md) \
703 tlsext_sigalg_ecdsa(md)
705 static const unsigned char tls12_sigalgs[] = {
706 tlsext_sigalg(TLSEXT_hash_sha512)
707 tlsext_sigalg(TLSEXT_hash_sha384)
708 tlsext_sigalg(TLSEXT_hash_sha256)
709 tlsext_sigalg(TLSEXT_hash_sha224)
710 tlsext_sigalg(TLSEXT_hash_sha1)
711 #ifndef OPENSSL_NO_GOST
712 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
713 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
714 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
718 #ifndef OPENSSL_NO_EC
719 static const unsigned char suiteb_sigalgs[] = {
720 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
721 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
724 size_t tls12_get_psigalgs(SSL *s, int sent, const unsigned char **psigs)
727 * If Suite B mode use Suite B sigalgs only, ignore any other
730 #ifndef OPENSSL_NO_EC
731 switch (tls1_suiteb(s)) {
732 case SSL_CERT_FLAG_SUITEB_128_LOS:
733 *psigs = suiteb_sigalgs;
734 return sizeof(suiteb_sigalgs);
736 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
737 *psigs = suiteb_sigalgs;
740 case SSL_CERT_FLAG_SUITEB_192_LOS:
741 *psigs = suiteb_sigalgs + 2;
745 /* If server use client authentication sigalgs if not NULL */
746 if (s->server == sent && s->cert->client_sigalgs) {
747 *psigs = s->cert->client_sigalgs;
748 return s->cert->client_sigalgslen;
749 } else if (s->cert->conf_sigalgs) {
750 *psigs = s->cert->conf_sigalgs;
751 return s->cert->conf_sigalgslen;
753 *psigs = tls12_sigalgs;
754 return sizeof(tls12_sigalgs);
759 * Check signature algorithm received from the peer with a signature is
760 * consistent with the sent supported signature algorithms and if so return
763 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
764 const unsigned char *sig, EVP_PKEY *pkey)
766 const unsigned char *sent_sigs;
767 size_t sent_sigslen, i;
768 int sigalg = tls12_get_sigid(pkey);
769 /* Should never happen */
772 /* Check key type is consistent with signature */
773 if (sigalg != (int)sig[1]) {
774 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
777 #ifndef OPENSSL_NO_EC
778 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
779 unsigned char curve_id[2], comp_id;
780 /* Check compression and curve matches extensions */
781 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
783 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id,
785 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
788 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
789 if (tls1_suiteb(s)) {
792 if (curve_id[1] == TLSEXT_curve_P_256) {
793 if (sig[0] != TLSEXT_hash_sha256) {
794 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
795 SSL_R_ILLEGAL_SUITEB_DIGEST);
798 } else if (curve_id[1] == TLSEXT_curve_P_384) {
799 if (sig[0] != TLSEXT_hash_sha384) {
800 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
801 SSL_R_ILLEGAL_SUITEB_DIGEST);
807 } else if (tls1_suiteb(s))
811 /* Check signature matches a type we sent */
812 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
813 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
814 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
817 /* Allow fallback to SHA1 if not strict mode */
818 if (i == sent_sigslen
819 && (sig[0] != TLSEXT_hash_sha1
820 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
821 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
824 *pmd = tls12_get_hash(sig[0]);
826 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
829 /* Make sure security callback allows algorithm */
830 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
831 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) {
832 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
836 * Store the digest used so applications can retrieve it if they wish.
838 s->s3->tmp.peer_md = *pmd;
843 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
844 * supported, doesn't appear in supported signature algorithms, isn't supported
845 * by the enabled protocol versions or by the security level.
847 * This function should only be used for checking which ciphers are supported
850 * Call ssl_cipher_disabled() to check that it's enabled or not.
852 void ssl_set_client_disabled(SSL *s)
854 s->s3->tmp.mask_a = 0;
855 s->s3->tmp.mask_k = 0;
856 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
857 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
858 #ifndef OPENSSL_NO_PSK
859 /* with PSK there must be client callback set */
860 if (!s->psk_client_callback) {
861 s->s3->tmp.mask_a |= SSL_aPSK;
862 s->s3->tmp.mask_k |= SSL_PSK;
864 #endif /* OPENSSL_NO_PSK */
865 #ifndef OPENSSL_NO_SRP
866 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
867 s->s3->tmp.mask_a |= SSL_aSRP;
868 s->s3->tmp.mask_k |= SSL_kSRP;
874 * ssl_cipher_disabled - check that a cipher is disabled or not
875 * @s: SSL connection that you want to use the cipher on
876 * @c: cipher to check
877 * @op: Security check that you want to do
878 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
880 * Returns 1 when it's disabled, 0 when enabled.
882 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
884 if (c->algorithm_mkey & s->s3->tmp.mask_k
885 || c->algorithm_auth & s->s3->tmp.mask_a)
887 if (s->s3->tmp.max_ver == 0)
889 if (!SSL_IS_DTLS(s)) {
890 int min_tls = c->min_tls;
893 * For historical reasons we will allow ECHDE to be selected by a server
894 * in SSLv3 if we are a client
896 if (min_tls == TLS1_VERSION && ecdhe
897 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
898 min_tls = SSL3_VERSION;
900 if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver))
903 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
904 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
907 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
910 static int tls_use_ticket(SSL *s)
912 if (s->options & SSL_OP_NO_TICKET)
914 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
917 static int compare_uint(const void *p1, const void *p2)
919 unsigned int u1 = *((const unsigned int *)p1);
920 unsigned int u2 = *((const unsigned int *)p2);
930 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
931 * more than one extension of the same type in a ClientHello or ServerHello.
932 * This function does an initial scan over the extensions block to filter those
933 * out. It returns 1 if all extensions are unique, and 0 if the extensions
934 * contain duplicates, could not be successfully parsed, or an internal error
937 static int tls1_check_duplicate_extensions(const PACKET *packet)
939 PACKET extensions = *packet;
940 size_t num_extensions = 0, i = 0;
941 unsigned int *extension_types = NULL;
944 /* First pass: count the extensions. */
945 while (PACKET_remaining(&extensions) > 0) {
948 if (!PACKET_get_net_2(&extensions, &type) ||
949 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
955 if (num_extensions <= 1)
958 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
959 if (extension_types == NULL) {
960 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
964 /* Second pass: gather the extension types. */
965 extensions = *packet;
966 for (i = 0; i < num_extensions; i++) {
968 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
969 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
970 /* This should not happen. */
971 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
976 if (PACKET_remaining(&extensions) != 0) {
977 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
980 /* Sort the extensions and make sure there are no duplicates. */
981 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
982 for (i = 1; i < num_extensions; i++) {
983 if (extension_types[i - 1] == extension_types[i])
988 OPENSSL_free(extension_types);
992 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
993 unsigned char *limit, int *al)
996 unsigned char *orig = buf;
997 unsigned char *ret = buf;
998 #ifndef OPENSSL_NO_EC
999 /* See if we support any ECC ciphersuites */
1001 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1003 unsigned long alg_k, alg_a;
1004 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1006 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1007 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1009 alg_k = c->algorithm_mkey;
1010 alg_a = c->algorithm_auth;
1011 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1012 || (alg_a & SSL_aECDSA)) {
1023 return NULL; /* this really never occurs, but ... */
1025 /* Add RI if renegotiating */
1026 if (s->renegotiate) {
1029 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1030 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1034 if (CHECKLEN(ret, 4 + el, limit))
1037 s2n(TLSEXT_TYPE_renegotiate, ret);
1040 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1041 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1047 /* Only add RI for SSLv3 */
1048 if (s->client_version == SSL3_VERSION)
1051 if (s->tlsext_hostname != NULL) {
1052 /* Add TLS extension servername to the Client Hello message */
1056 * check for enough space.
1057 * 4 for the servername type and extension length
1058 * 2 for servernamelist length
1059 * 1 for the hostname type
1060 * 2 for hostname length
1063 size_str = strlen(s->tlsext_hostname);
1064 if (CHECKLEN(ret, 9 + size_str, limit))
1067 /* extension type and length */
1068 s2n(TLSEXT_TYPE_server_name, ret);
1069 s2n(size_str + 5, ret);
1071 /* length of servername list */
1072 s2n(size_str + 3, ret);
1074 /* hostname type, length and hostname */
1075 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1077 memcpy(ret, s->tlsext_hostname, size_str);
1080 #ifndef OPENSSL_NO_SRP
1081 /* Add SRP username if there is one */
1082 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1083 * Client Hello message */
1085 size_t login_len = strlen(s->srp_ctx.login);
1086 if (login_len > 255 || login_len == 0) {
1087 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1092 * check for enough space.
1093 * 4 for the srp type type and extension length
1094 * 1 for the srp user identity
1095 * + srp user identity length
1097 if (CHECKLEN(ret, 5 + login_len, limit))
1100 /* fill in the extension */
1101 s2n(TLSEXT_TYPE_srp, ret);
1102 s2n(login_len + 1, ret);
1103 (*ret++) = (unsigned char)login_len;
1104 memcpy(ret, s->srp_ctx.login, login_len);
1109 #ifndef OPENSSL_NO_EC
1112 * Add TLS extension ECPointFormats to the ClientHello message
1114 const unsigned char *pcurves, *pformats;
1115 size_t num_curves, num_formats, curves_list_len;
1117 unsigned char *etmp;
1119 tls1_get_formatlist(s, &pformats, &num_formats);
1121 if (num_formats > 255) {
1122 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1126 * check for enough space.
1127 * 4 bytes for the ec point formats type and extension length
1128 * 1 byte for the length of the formats
1131 if (CHECKLEN(ret, 5 + num_formats, limit))
1134 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1135 /* The point format list has 1-byte length. */
1136 s2n(num_formats + 1, ret);
1137 *(ret++) = (unsigned char)num_formats;
1138 memcpy(ret, pformats, num_formats);
1142 * Add TLS extension EllipticCurves to the ClientHello message
1144 pcurves = s->tlsext_ellipticcurvelist;
1145 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1148 if (num_curves > 65532 / 2) {
1149 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1153 * check for enough space.
1154 * 4 bytes for the ec curves type and extension length
1155 * 2 bytes for the curve list length
1156 * + curve list length
1158 if (CHECKLEN(ret, 6 + (num_curves * 2), limit))
1161 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1163 /* Copy curve ID if supported */
1164 for (i = 0; i < num_curves; i++, pcurves += 2) {
1165 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1166 *etmp++ = pcurves[0];
1167 *etmp++ = pcurves[1];
1171 curves_list_len = etmp - ret - 4;
1173 s2n(curves_list_len + 2, ret);
1174 s2n(curves_list_len, ret);
1175 ret += curves_list_len;
1177 #endif /* OPENSSL_NO_EC */
1179 if (tls_use_ticket(s)) {
1181 if (!s->new_session && s->session && s->session->tlsext_tick)
1182 ticklen = s->session->tlsext_ticklen;
1183 else if (s->session && s->tlsext_session_ticket &&
1184 s->tlsext_session_ticket->data) {
1185 ticklen = s->tlsext_session_ticket->length;
1186 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1187 if (s->session->tlsext_tick == NULL)
1189 memcpy(s->session->tlsext_tick,
1190 s->tlsext_session_ticket->data, ticklen);
1191 s->session->tlsext_ticklen = ticklen;
1194 if (ticklen == 0 && s->tlsext_session_ticket &&
1195 s->tlsext_session_ticket->data == NULL)
1198 * Check for enough room 2 for extension type, 2 for len rest for
1201 if (CHECKLEN(ret, 4 + ticklen, limit))
1203 s2n(TLSEXT_TYPE_session_ticket, ret);
1206 memcpy(ret, s->session->tlsext_tick, ticklen);
1212 #ifndef OPENSSL_NO_OCSP
1213 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1215 size_t extlen, idlen;
1220 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1221 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1222 lentmp = i2d_OCSP_RESPID(id, NULL);
1225 idlen += (size_t)lentmp + 2;
1228 if (s->tlsext_ocsp_exts) {
1229 lentmp = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1232 extlen = (size_t)lentmp;
1236 if (extlen + idlen > 0xFFF0)
1239 * 2 bytes for status request type
1240 * 2 bytes for status request len
1241 * 1 byte for OCSP request type
1242 * 2 bytes for length of ids
1243 * 2 bytes for length of extensions
1245 * + length of extensions
1247 if (CHECKLEN(ret, 9 + idlen + extlen, limit))
1250 s2n(TLSEXT_TYPE_status_request, ret);
1251 s2n(extlen + idlen + 5, ret);
1252 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1254 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1255 /* save position of id len */
1256 unsigned char *q = ret;
1257 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1258 /* skip over id len */
1260 lentmp = i2d_OCSP_RESPID(id, &ret);
1266 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1269 #ifndef OPENSSL_NO_HEARTBEATS
1270 if (SSL_IS_DTLS(s)) {
1271 /* Add Heartbeat extension */
1274 * check for enough space.
1275 * 4 bytes for the heartbeat ext type and extension length
1276 * 1 byte for the mode
1278 if (CHECKLEN(ret, 5, limit))
1281 s2n(TLSEXT_TYPE_heartbeat, ret);
1285 * 1: peer may send requests
1286 * 2: peer not allowed to send requests
1288 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1289 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1291 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1295 #ifndef OPENSSL_NO_NEXTPROTONEG
1296 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1298 * The client advertises an empty extension to indicate its support
1299 * for Next Protocol Negotiation
1303 * check for enough space.
1304 * 4 bytes for the NPN ext type and extension length
1306 if (CHECKLEN(ret, 4, limit))
1308 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1314 * finish_md_len is non-zero during a renegotiation, so
1315 * this avoids sending ALPN during the renegotiation
1316 * (see longer comment below)
1318 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1320 * check for enough space.
1321 * 4 bytes for the ALPN type and extension length
1322 * 2 bytes for the ALPN protocol list length
1323 * + ALPN protocol list length
1325 if (CHECKLEN(ret, 6 + s->alpn_client_proto_list_len, limit))
1327 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1328 s2n(2 + s->alpn_client_proto_list_len, ret);
1329 s2n(s->alpn_client_proto_list_len, ret);
1330 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1331 ret += s->alpn_client_proto_list_len;
1332 s->s3->alpn_sent = 1;
1334 #ifndef OPENSSL_NO_SRTP
1335 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1338 /* Returns 0 on success!! */
1339 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1340 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1345 * check for enough space.
1346 * 4 bytes for the SRTP type and extension length
1347 * + SRTP profiles length
1349 if (CHECKLEN(ret, 4 + el, limit))
1352 s2n(TLSEXT_TYPE_use_srtp, ret);
1355 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1356 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1362 custom_ext_init(&s->cert->cli_ext);
1363 /* Add custom TLS Extensions to ClientHello */
1364 if (!custom_ext_add(s, 0, &ret, limit, al))
1367 * In 1.1.0 before 1.1.0c we negotiated EtM with DTLS, then just
1368 * silently failed to actually do it. It is fixed in 1.1.1 but to
1369 * ease the transition especially from 1.1.0b to 1.1.0c, we just
1370 * disable it in 1.1.0.
1371 * Also skip if SSL_OP_NO_ENCRYPT_THEN_MAC is set.
1373 if (!SSL_IS_DTLS(s) && !(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) {
1375 * check for enough space.
1376 * 4 bytes for the ETM type and extension length
1378 if (CHECKLEN(ret, 4, limit))
1380 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1384 #ifndef OPENSSL_NO_CT
1385 if (s->ct_validation_callback != NULL) {
1387 * check for enough space.
1388 * 4 bytes for the SCT type and extension length
1390 if (CHECKLEN(ret, 4, limit))
1393 s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret);
1399 * check for enough space.
1400 * 4 bytes for the EMS type and extension length
1402 if (CHECKLEN(ret, 4, limit))
1404 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1408 * WebSphere application server can not handle having the
1409 * last extension be 0-length (e.g. EMS, EtM), so keep those
1412 if (SSL_CLIENT_USE_SIGALGS(s)) {
1414 const unsigned char *salg;
1415 unsigned char *etmp;
1416 salglen = tls12_get_psigalgs(s, 1, &salg);
1419 * check for enough space.
1420 * 4 bytes for the sigalgs type and extension length
1421 * 2 bytes for the sigalg list length
1422 * + sigalg list length
1424 if (CHECKLEN(ret, salglen + 6, limit))
1426 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1428 /* Skip over lengths for now */
1430 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1431 /* Fill in lengths */
1432 s2n(salglen + 2, etmp);
1438 * Add padding to workaround bugs in F5 terminators. See
1439 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1440 * code works out the length of all existing extensions it MUST always
1441 * appear last. WebSphere 7.x/8.x is intolerant of empty extensions
1442 * being last, so minimum length of 1.
1444 if (s->options & SSL_OP_TLSEXT_PADDING) {
1445 int hlen = ret - (unsigned char *)s->init_buf->data;
1447 if (hlen > 0xff && hlen < 0x200) {
1448 hlen = 0x200 - hlen;
1455 * check for enough space. Strictly speaking we know we've already
1456 * got enough space because to get here the message size is < 0x200,
1457 * but we know that we've allocated far more than that in the buffer
1458 * - but for consistency and robustness we're going to check anyway.
1460 * 4 bytes for the padding type and extension length
1463 if (CHECKLEN(ret, 4 + hlen, limit))
1465 s2n(TLSEXT_TYPE_padding, ret);
1467 memset(ret, 0, hlen);
1474 if ((extdatalen = ret - orig - 2) == 0)
1477 s2n(extdatalen, orig);
1481 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1482 unsigned char *limit, int *al)
1485 unsigned char *orig = buf;
1486 unsigned char *ret = buf;
1487 #ifndef OPENSSL_NO_NEXTPROTONEG
1488 int next_proto_neg_seen;
1490 #ifndef OPENSSL_NO_EC
1491 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1492 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1493 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1494 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1499 return NULL; /* this really never occurs, but ... */
1501 if (s->s3->send_connection_binding) {
1504 /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
1505 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1506 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1511 * check for enough space.
1512 * 4 bytes for the reneg type and extension length
1513 * + reneg data length
1515 if (CHECKLEN(ret, 4 + el, limit))
1518 s2n(TLSEXT_TYPE_renegotiate, ret);
1521 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1522 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1529 /* Only add RI for SSLv3 */
1530 if (s->version == SSL3_VERSION)
1533 if (!s->hit && s->servername_done == 1
1534 && s->session->tlsext_hostname != NULL) {
1536 * check for enough space.
1537 * 4 bytes for the server name type and extension length
1539 if (CHECKLEN(ret, 4, limit))
1542 s2n(TLSEXT_TYPE_server_name, ret);
1545 #ifndef OPENSSL_NO_EC
1547 const unsigned char *plist;
1550 * Add TLS extension ECPointFormats to the ServerHello message
1553 tls1_get_formatlist(s, &plist, &plistlen);
1555 if (plistlen > 255) {
1556 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1561 * check for enough space.
1562 * 4 bytes for the ec points format type and extension length
1563 * 1 byte for the points format list length
1564 * + length of points format list
1566 if (CHECKLEN(ret, 5 + plistlen, limit))
1569 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1570 s2n(plistlen + 1, ret);
1571 *(ret++) = (unsigned char)plistlen;
1572 memcpy(ret, plist, plistlen);
1577 * Currently the server should not respond with a SupportedCurves
1580 #endif /* OPENSSL_NO_EC */
1582 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1584 * check for enough space.
1585 * 4 bytes for the Ticket type and extension length
1587 if (CHECKLEN(ret, 4, limit))
1589 s2n(TLSEXT_TYPE_session_ticket, ret);
1593 * if we don't add the above TLSEXT, we can't add a session ticket
1596 s->tlsext_ticket_expected = 0;
1599 if (s->tlsext_status_expected) {
1601 * check for enough space.
1602 * 4 bytes for the Status request type and extension length
1604 if (CHECKLEN(ret, 4, limit))
1606 s2n(TLSEXT_TYPE_status_request, ret);
1609 #ifndef OPENSSL_NO_SRTP
1610 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1613 /* Returns 0 on success!! */
1614 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1615 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1619 * check for enough space.
1620 * 4 bytes for the SRTP profiles type and extension length
1621 * + length of the SRTP profiles list
1623 if (CHECKLEN(ret, 4 + el, limit))
1626 s2n(TLSEXT_TYPE_use_srtp, ret);
1629 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1630 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1637 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1638 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1639 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1640 const unsigned char cryptopro_ext[36] = {
1641 0xfd, 0xe8, /* 65000 */
1642 0x00, 0x20, /* 32 bytes length */
1643 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1644 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1645 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1646 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1649 /* check for enough space. */
1650 if (CHECKLEN(ret, sizeof(cryptopro_ext), limit))
1652 memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext));
1653 ret += sizeof(cryptopro_ext);
1656 #ifndef OPENSSL_NO_HEARTBEATS
1657 /* Add Heartbeat extension if we've received one */
1658 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1660 * check for enough space.
1661 * 4 bytes for the Heartbeat type and extension length
1662 * 1 byte for the mode
1664 if (CHECKLEN(ret, 5, limit))
1666 s2n(TLSEXT_TYPE_heartbeat, ret);
1670 * 1: peer may send requests
1671 * 2: peer not allowed to send requests
1673 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1674 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1676 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1681 #ifndef OPENSSL_NO_NEXTPROTONEG
1682 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1683 s->s3->next_proto_neg_seen = 0;
1684 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1685 const unsigned char *npa;
1686 unsigned int npalen;
1689 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1691 ctx->next_protos_advertised_cb_arg);
1692 if (r == SSL_TLSEXT_ERR_OK) {
1694 * check for enough space.
1695 * 4 bytes for the NPN type and extension length
1696 * + length of protocols list
1698 if (CHECKLEN(ret, 4 + npalen, limit))
1700 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1702 memcpy(ret, npa, npalen);
1704 s->s3->next_proto_neg_seen = 1;
1708 if (!custom_ext_add(s, 1, &ret, limit, al))
1710 if (s->tlsext_use_etm) {
1712 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1713 * for other cases too.
1715 if (SSL_IS_DTLS(s) || s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1716 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1717 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1718 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1719 s->tlsext_use_etm = 0;
1722 * check for enough space.
1723 * 4 bytes for the ETM type and extension length
1725 if (CHECKLEN(ret, 4, limit))
1727 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1731 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1733 * check for enough space.
1734 * 4 bytes for the EMS type and extension length
1736 if (CHECKLEN(ret, 4, limit))
1738 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1742 if (s->s3->alpn_selected != NULL) {
1743 const unsigned char *selected = s->s3->alpn_selected;
1744 size_t len = s->s3->alpn_selected_len;
1747 * check for enough space.
1748 * 4 bytes for the ALPN type and extension length
1749 * 2 bytes for ALPN data length
1750 * 1 byte for selected protocol length
1751 * + length of the selected protocol
1753 if (CHECKLEN(ret, 7 + len, limit))
1755 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1759 memcpy(ret, selected, len);
1765 if ((extdatalen = ret - orig - 2) == 0)
1768 s2n(extdatalen, orig);
1773 * Save the ALPN extension in a ClientHello.
1774 * pkt: the contents of the ALPN extension, not including type and length.
1775 * al: a pointer to the alert value to send in the event of a failure.
1776 * returns: 1 on success, 0 on error.
1778 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1780 PACKET protocol_list, save_protocol_list, protocol;
1782 *al = SSL_AD_DECODE_ERROR;
1784 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1785 || PACKET_remaining(&protocol_list) < 2) {
1789 save_protocol_list = protocol_list;
1791 /* Protocol names can't be empty. */
1792 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1793 || PACKET_remaining(&protocol) == 0) {
1796 } while (PACKET_remaining(&protocol_list) != 0);
1798 if (!PACKET_memdup(&save_protocol_list,
1799 &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
1800 *al = TLS1_AD_INTERNAL_ERROR;
1808 * Process the ALPN extension in a ClientHello.
1809 * al: a pointer to the alert value to send in the event of a failure.
1810 * returns 1 on success, 0 on error.
1812 static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
1814 const unsigned char *selected = NULL;
1815 unsigned char selected_len = 0;
1817 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1818 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1819 s->s3->alpn_proposed,
1820 s->s3->alpn_proposed_len,
1821 s->ctx->alpn_select_cb_arg);
1823 if (r == SSL_TLSEXT_ERR_OK) {
1824 OPENSSL_free(s->s3->alpn_selected);
1825 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1826 if (s->s3->alpn_selected == NULL) {
1827 *al = SSL_AD_INTERNAL_ERROR;
1830 s->s3->alpn_selected_len = selected_len;
1831 #ifndef OPENSSL_NO_NEXTPROTONEG
1832 /* ALPN takes precedence over NPN. */
1833 s->s3->next_proto_neg_seen = 0;
1835 } else if (r == SSL_TLSEXT_ERR_NOACK) {
1836 /* Behave as if no callback was present. */
1839 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1847 #ifndef OPENSSL_NO_EC
1849 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1850 * SecureTransport using the TLS extension block in |pkt|.
1851 * Safari, since 10.6, sends exactly these extensions, in this order:
1856 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1857 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1858 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1859 * 10.8..10.8.3 (which don't work).
1861 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1867 static const unsigned char kSafariExtensionsBlock[] = {
1868 0x00, 0x0a, /* elliptic_curves extension */
1869 0x00, 0x08, /* 8 bytes */
1870 0x00, 0x06, /* 6 bytes of curve ids */
1871 0x00, 0x17, /* P-256 */
1872 0x00, 0x18, /* P-384 */
1873 0x00, 0x19, /* P-521 */
1875 0x00, 0x0b, /* ec_point_formats */
1876 0x00, 0x02, /* 2 bytes */
1877 0x01, /* 1 point format */
1878 0x00, /* uncompressed */
1879 /* The following is only present in TLS 1.2 */
1880 0x00, 0x0d, /* signature_algorithms */
1881 0x00, 0x0c, /* 12 bytes */
1882 0x00, 0x0a, /* 10 bytes */
1883 0x05, 0x01, /* SHA-384/RSA */
1884 0x04, 0x01, /* SHA-256/RSA */
1885 0x02, 0x01, /* SHA-1/RSA */
1886 0x04, 0x03, /* SHA-256/ECDSA */
1887 0x02, 0x03, /* SHA-1/ECDSA */
1890 /* Length of the common prefix (first two extensions). */
1891 static const size_t kSafariCommonExtensionsLength = 18;
1895 if (!PACKET_forward(&tmppkt, 2)
1896 || !PACKET_get_net_2(&tmppkt, &type)
1897 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1901 if (type != TLSEXT_TYPE_server_name)
1904 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1905 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1907 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1910 #endif /* !OPENSSL_NO_EC */
1913 * Parse ClientHello extensions and stash extension info in various parts of
1914 * the SSL object. Verify that there are no duplicate extensions.
1916 * Behaviour upon resumption is extension-specific. If the extension has no
1917 * effect during resumption, it is parsed (to verify its format) but otherwise
1920 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1921 * Upon failure, sets |al| to the appropriate alert.
1923 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1926 int renegotiate_seen = 0;
1929 *al = SSL_AD_DECODE_ERROR;
1930 s->servername_done = 0;
1931 s->tlsext_status_type = -1;
1932 #ifndef OPENSSL_NO_NEXTPROTONEG
1933 s->s3->next_proto_neg_seen = 0;
1936 OPENSSL_free(s->s3->alpn_selected);
1937 s->s3->alpn_selected = NULL;
1938 s->s3->alpn_selected_len = 0;
1939 OPENSSL_free(s->s3->alpn_proposed);
1940 s->s3->alpn_proposed = NULL;
1941 s->s3->alpn_proposed_len = 0;
1942 #ifndef OPENSSL_NO_HEARTBEATS
1943 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1944 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1947 #ifndef OPENSSL_NO_EC
1948 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1949 ssl_check_for_safari(s, pkt);
1950 #endif /* !OPENSSL_NO_EC */
1952 /* Clear any signature algorithms extension received */
1953 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1954 s->s3->tmp.peer_sigalgs = NULL;
1955 s->tlsext_use_etm = 0;
1957 #ifndef OPENSSL_NO_SRP
1958 OPENSSL_free(s->srp_ctx.login);
1959 s->srp_ctx.login = NULL;
1962 s->srtp_profile = NULL;
1964 if (PACKET_remaining(pkt) == 0)
1967 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1970 if (!tls1_check_duplicate_extensions(&extensions))
1974 * We parse all extensions to ensure the ClientHello is well-formed but,
1975 * unless an extension specifies otherwise, we ignore extensions upon
1978 while (PACKET_get_net_2(&extensions, &type)) {
1980 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1983 if (s->tlsext_debug_cb)
1984 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1985 PACKET_remaining(&extension),
1986 s->tlsext_debug_arg);
1988 if (type == TLSEXT_TYPE_renegotiate) {
1989 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
1991 renegotiate_seen = 1;
1992 } else if (s->version == SSL3_VERSION) {
1995 * The servername extension is treated as follows:
1997 * - Only the hostname type is supported with a maximum length of 255.
1998 * - The servername is rejected if too long or if it contains zeros,
1999 * in which case an fatal alert is generated.
2000 * - The servername field is maintained together with the session cache.
2001 * - When a session is resumed, the servername call back invoked in order
2002 * to allow the application to position itself to the right context.
2003 * - The servername is acknowledged if it is new for a session or when
2004 * it is identical to a previously used for the same session.
2005 * Applications can control the behaviour. They can at any time
2006 * set a 'desirable' servername for a new SSL object. This can be the
2007 * case for example with HTTPS when a Host: header field is received and
2008 * a renegotiation is requested. In this case, a possible servername
2009 * presented in the new client hello is only acknowledged if it matches
2010 * the value of the Host: field.
2011 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2012 * if they provide for changing an explicit servername context for the
2013 * session, i.e. when the session has been established with a servername
2015 * - On session reconnect, the servername extension may be absent.
2019 else if (type == TLSEXT_TYPE_server_name) {
2020 unsigned int servname_type;
2021 PACKET sni, hostname;
2023 if (!PACKET_as_length_prefixed_2(&extension, &sni)
2024 /* ServerNameList must be at least 1 byte long. */
2025 || PACKET_remaining(&sni) == 0) {
2030 * Although the server_name extension was intended to be
2031 * extensible to new name types, RFC 4366 defined the
2032 * syntax inextensibility and OpenSSL 1.0.x parses it as
2034 * RFC 6066 corrected the mistake but adding new name types
2035 * is nevertheless no longer feasible, so act as if no other
2036 * SNI types can exist, to simplify parsing.
2038 * Also note that the RFC permits only one SNI value per type,
2039 * i.e., we can only have a single hostname.
2041 if (!PACKET_get_1(&sni, &servname_type)
2042 || servname_type != TLSEXT_NAMETYPE_host_name
2043 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
2048 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
2049 *al = TLS1_AD_UNRECOGNIZED_NAME;
2053 if (PACKET_contains_zero_byte(&hostname)) {
2054 *al = TLS1_AD_UNRECOGNIZED_NAME;
2058 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
2059 *al = TLS1_AD_INTERNAL_ERROR;
2063 s->servername_done = 1;
2066 * TODO(openssl-team): if the SNI doesn't match, we MUST
2067 * fall back to a full handshake.
2069 s->servername_done = s->session->tlsext_hostname
2070 && PACKET_equal(&hostname, s->session->tlsext_hostname,
2071 strlen(s->session->tlsext_hostname));
2074 #ifndef OPENSSL_NO_SRP
2075 else if (type == TLSEXT_TYPE_srp) {
2078 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
2081 if (PACKET_contains_zero_byte(&srp_I))
2085 * TODO(openssl-team): currently, we re-authenticate the user
2086 * upon resumption. Instead, we MUST ignore the login.
2088 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
2089 *al = TLS1_AD_INTERNAL_ERROR;
2095 #ifndef OPENSSL_NO_EC
2096 else if (type == TLSEXT_TYPE_ec_point_formats) {
2097 PACKET ec_point_format_list;
2099 if (!PACKET_as_length_prefixed_1(&extension, &ec_point_format_list)
2100 || PACKET_remaining(&ec_point_format_list) == 0) {
2105 if (!PACKET_memdup(&ec_point_format_list,
2106 &s->session->tlsext_ecpointformatlist,
2108 session->tlsext_ecpointformatlist_length)) {
2109 *al = TLS1_AD_INTERNAL_ERROR;
2113 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2114 PACKET elliptic_curve_list;
2116 /* Each NamedCurve is 2 bytes and we must have at least 1. */
2117 if (!PACKET_as_length_prefixed_2(&extension, &elliptic_curve_list)
2118 || PACKET_remaining(&elliptic_curve_list) == 0
2119 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
2124 if (!PACKET_memdup(&elliptic_curve_list,
2125 &s->session->tlsext_ellipticcurvelist,
2127 session->tlsext_ellipticcurvelist_length)) {
2128 *al = TLS1_AD_INTERNAL_ERROR;
2133 #endif /* OPENSSL_NO_EC */
2134 else if (type == TLSEXT_TYPE_session_ticket) {
2135 if (s->tls_session_ticket_ext_cb &&
2136 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
2137 PACKET_remaining(&extension),
2138 s->tls_session_ticket_ext_cb_arg))
2140 *al = TLS1_AD_INTERNAL_ERROR;
2143 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2144 PACKET supported_sig_algs;
2146 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2147 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2148 || PACKET_remaining(&supported_sig_algs) == 0) {
2153 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2154 PACKET_remaining(&supported_sig_algs))) {
2158 } else if (type == TLSEXT_TYPE_status_request) {
2159 if (!PACKET_get_1(&extension,
2160 (unsigned int *)&s->tlsext_status_type)) {
2163 #ifndef OPENSSL_NO_OCSP
2164 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2165 const unsigned char *ext_data;
2166 PACKET responder_id_list, exts;
2167 if (!PACKET_get_length_prefixed_2
2168 (&extension, &responder_id_list))
2172 * We remove any OCSP_RESPIDs from a previous handshake
2173 * to prevent unbounded memory growth - CVE-2016-6304
2175 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2177 if (PACKET_remaining(&responder_id_list) > 0) {
2178 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2179 if (s->tlsext_ocsp_ids == NULL) {
2180 *al = SSL_AD_INTERNAL_ERROR;
2184 s->tlsext_ocsp_ids = NULL;
2187 while (PACKET_remaining(&responder_id_list) > 0) {
2189 PACKET responder_id;
2190 const unsigned char *id_data;
2192 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2194 || PACKET_remaining(&responder_id) == 0) {
2198 id_data = PACKET_data(&responder_id);
2199 id = d2i_OCSP_RESPID(NULL, &id_data,
2200 PACKET_remaining(&responder_id));
2204 if (id_data != PACKET_end(&responder_id)) {
2205 OCSP_RESPID_free(id);
2209 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2210 OCSP_RESPID_free(id);
2211 *al = SSL_AD_INTERNAL_ERROR;
2216 /* Read in request_extensions */
2217 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2220 if (PACKET_remaining(&exts) > 0) {
2221 ext_data = PACKET_data(&exts);
2222 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2223 X509_EXTENSION_free);
2224 s->tlsext_ocsp_exts =
2225 d2i_X509_EXTENSIONS(NULL, &ext_data,
2226 PACKET_remaining(&exts));
2227 if (s->tlsext_ocsp_exts == NULL
2228 || ext_data != PACKET_end(&exts)) {
2236 * We don't know what to do with any other type so ignore it.
2238 s->tlsext_status_type = -1;
2241 #ifndef OPENSSL_NO_HEARTBEATS
2242 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2243 unsigned int hbtype;
2245 if (!PACKET_get_1(&extension, &hbtype)
2246 || PACKET_remaining(&extension)) {
2247 *al = SSL_AD_DECODE_ERROR;
2251 case 0x01: /* Client allows us to send HB requests */
2252 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2254 case 0x02: /* Client doesn't accept HB requests */
2255 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2256 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2259 *al = SSL_AD_ILLEGAL_PARAMETER;
2264 #ifndef OPENSSL_NO_NEXTPROTONEG
2265 else if (type == TLSEXT_TYPE_next_proto_neg &&
2266 s->s3->tmp.finish_md_len == 0) {
2268 * We shouldn't accept this extension on a
2271 * s->new_session will be set on renegotiation, but we
2272 * probably shouldn't rely that it couldn't be set on
2273 * the initial renegotiation too in certain cases (when
2274 * there's some other reason to disallow resuming an
2275 * earlier session -- the current code won't be doing
2276 * anything like that, but this might change).
2278 * A valid sign that there's been a previous handshake
2279 * in this connection is if s->s3->tmp.finish_md_len >
2280 * 0. (We are talking about a check that will happen
2281 * in the Hello protocol round, well before a new
2282 * Finished message could have been computed.)
2284 s->s3->next_proto_neg_seen = 1;
2288 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2289 s->s3->tmp.finish_md_len == 0) {
2290 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2294 /* session ticket processed earlier */
2295 #ifndef OPENSSL_NO_SRTP
2296 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2297 && type == TLSEXT_TYPE_use_srtp) {
2298 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2302 else if (type == TLSEXT_TYPE_encrypt_then_mac &&
2303 !(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
2304 s->tlsext_use_etm = 1;
2306 * Note: extended master secret extension handled in
2307 * tls_check_serverhello_tlsext_early()
2311 * If this ClientHello extension was unhandled and this is a
2312 * nonresumed connection, check whether the extension is a custom
2313 * TLS Extension (has a custom_srv_ext_record), and if so call the
2314 * callback and record the extension number so that an appropriate
2315 * ServerHello may be later returned.
2318 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2319 PACKET_remaining(&extension), al) <= 0)
2324 if (PACKET_remaining(pkt) != 0) {
2326 * tls1_check_duplicate_extensions should ensure this never happens.
2328 *al = SSL_AD_INTERNAL_ERROR;
2334 /* Need RI if renegotiating */
2336 if (!renegotiate_seen && s->renegotiate &&
2337 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2338 *al = SSL_AD_HANDSHAKE_FAILURE;
2339 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2340 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2345 * This function currently has no state to clean up, so it returns directly.
2346 * If parsing fails at any point, the function returns early.
2347 * The SSL object may be left with partial data from extensions, but it must
2348 * then no longer be used, and clearing it up will free the leftovers.
2353 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2356 custom_ext_init(&s->cert->srv_ext);
2357 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2358 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2361 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2362 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2368 #ifndef OPENSSL_NO_NEXTPROTONEG
2370 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2371 * elements of zero length are allowed and the set of elements must exactly
2372 * fill the length of the block.
2374 static char ssl_next_proto_validate(PACKET *pkt)
2376 PACKET tmp_protocol;
2378 while (PACKET_remaining(pkt)) {
2379 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2380 || PACKET_remaining(&tmp_protocol) == 0)
2388 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2390 unsigned int length, type, size;
2391 int tlsext_servername = 0;
2392 int renegotiate_seen = 0;
2394 #ifndef OPENSSL_NO_NEXTPROTONEG
2395 s->s3->next_proto_neg_seen = 0;
2397 s->tlsext_ticket_expected = 0;
2399 OPENSSL_free(s->s3->alpn_selected);
2400 s->s3->alpn_selected = NULL;
2401 #ifndef OPENSSL_NO_HEARTBEATS
2402 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2403 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2406 s->tlsext_use_etm = 0;
2408 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2410 if (!PACKET_get_net_2(pkt, &length))
2413 if (PACKET_remaining(pkt) != length) {
2414 *al = SSL_AD_DECODE_ERROR;
2418 if (!tls1_check_duplicate_extensions(pkt)) {
2419 *al = SSL_AD_DECODE_ERROR;
2423 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2424 const unsigned char *data;
2427 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2428 || !PACKET_peek_bytes(&spkt, &data, size))
2431 if (s->tlsext_debug_cb)
2432 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2434 if (type == TLSEXT_TYPE_renegotiate) {
2435 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2437 renegotiate_seen = 1;
2438 } else if (s->version == SSL3_VERSION) {
2439 } else if (type == TLSEXT_TYPE_server_name) {
2440 if (s->tlsext_hostname == NULL || size > 0) {
2441 *al = TLS1_AD_UNRECOGNIZED_NAME;
2444 tlsext_servername = 1;
2446 #ifndef OPENSSL_NO_EC
2447 else if (type == TLSEXT_TYPE_ec_point_formats) {
2448 unsigned int ecpointformatlist_length;
2449 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2450 || ecpointformatlist_length != size - 1) {
2451 *al = TLS1_AD_DECODE_ERROR;
2455 s->session->tlsext_ecpointformatlist_length = 0;
2456 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2457 if ((s->session->tlsext_ecpointformatlist =
2458 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2459 *al = TLS1_AD_INTERNAL_ERROR;
2462 s->session->tlsext_ecpointformatlist_length =
2463 ecpointformatlist_length;
2464 if (!PACKET_copy_bytes(&spkt,
2465 s->session->tlsext_ecpointformatlist,
2466 ecpointformatlist_length)) {
2467 *al = TLS1_AD_DECODE_ERROR;
2473 #endif /* OPENSSL_NO_EC */
2475 else if (type == TLSEXT_TYPE_session_ticket) {
2476 if (s->tls_session_ticket_ext_cb &&
2477 !s->tls_session_ticket_ext_cb(s, data, size,
2478 s->tls_session_ticket_ext_cb_arg))
2480 *al = TLS1_AD_INTERNAL_ERROR;
2483 if (!tls_use_ticket(s) || (size > 0)) {
2484 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2487 s->tlsext_ticket_expected = 1;
2488 } else if (type == TLSEXT_TYPE_status_request) {
2490 * MUST be empty and only sent if we've requested a status
2493 if ((s->tlsext_status_type == -1) || (size > 0)) {
2494 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2497 /* Set flag to expect CertificateStatus message */
2498 s->tlsext_status_expected = 1;
2500 #ifndef OPENSSL_NO_CT
2502 * Only take it if we asked for it - i.e if there is no CT validation
2503 * callback set, then a custom extension MAY be processing it, so we
2504 * need to let control continue to flow to that.
2506 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2507 s->ct_validation_callback != NULL) {
2508 /* Simply copy it off for later processing */
2509 if (s->tlsext_scts != NULL) {
2510 OPENSSL_free(s->tlsext_scts);
2511 s->tlsext_scts = NULL;
2513 s->tlsext_scts_len = size;
2515 s->tlsext_scts = OPENSSL_malloc(size);
2516 if (s->tlsext_scts == NULL) {
2517 *al = TLS1_AD_INTERNAL_ERROR;
2520 memcpy(s->tlsext_scts, data, size);
2524 #ifndef OPENSSL_NO_NEXTPROTONEG
2525 else if (type == TLSEXT_TYPE_next_proto_neg &&
2526 s->s3->tmp.finish_md_len == 0) {
2527 unsigned char *selected;
2528 unsigned char selected_len;
2529 /* We must have requested it. */
2530 if (s->ctx->next_proto_select_cb == NULL) {
2531 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2534 /* The data must be valid */
2535 if (!ssl_next_proto_validate(&spkt)) {
2536 *al = TLS1_AD_DECODE_ERROR;
2539 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2542 ctx->next_proto_select_cb_arg) !=
2543 SSL_TLSEXT_ERR_OK) {
2544 *al = TLS1_AD_INTERNAL_ERROR;
2548 * Could be non-NULL if server has sent multiple NPN extensions in
2549 * a single Serverhello
2551 OPENSSL_free(s->next_proto_negotiated);
2552 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2553 if (s->next_proto_negotiated == NULL) {
2554 *al = TLS1_AD_INTERNAL_ERROR;
2557 memcpy(s->next_proto_negotiated, selected, selected_len);
2558 s->next_proto_negotiated_len = selected_len;
2559 s->s3->next_proto_neg_seen = 1;
2563 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2565 /* We must have requested it. */
2566 if (!s->s3->alpn_sent) {
2567 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2571 * The extension data consists of:
2572 * uint16 list_length
2573 * uint8 proto_length;
2574 * uint8 proto[proto_length];
2576 if (!PACKET_get_net_2(&spkt, &len)
2577 || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len)
2578 || PACKET_remaining(&spkt) != len) {
2579 *al = TLS1_AD_DECODE_ERROR;
2582 OPENSSL_free(s->s3->alpn_selected);
2583 s->s3->alpn_selected = OPENSSL_malloc(len);
2584 if (s->s3->alpn_selected == NULL) {
2585 *al = TLS1_AD_INTERNAL_ERROR;
2588 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2589 *al = TLS1_AD_DECODE_ERROR;
2592 s->s3->alpn_selected_len = len;
2594 #ifndef OPENSSL_NO_HEARTBEATS
2595 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2596 unsigned int hbtype;
2597 if (!PACKET_get_1(&spkt, &hbtype)) {
2598 *al = SSL_AD_DECODE_ERROR;
2602 case 0x01: /* Server allows us to send HB requests */
2603 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2605 case 0x02: /* Server doesn't accept HB requests */
2606 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2607 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2610 *al = SSL_AD_ILLEGAL_PARAMETER;
2615 #ifndef OPENSSL_NO_SRTP
2616 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2617 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2621 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2622 /* Ignore if inappropriate ciphersuite */
2623 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) &&
2624 s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2625 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2626 s->tlsext_use_etm = 1;
2627 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2628 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2630 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2633 * If this extension type was not otherwise handled, but matches a
2634 * custom_cli_ext_record, then send it to the c callback
2636 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2640 if (PACKET_remaining(pkt) != 0) {
2641 *al = SSL_AD_DECODE_ERROR;
2645 if (!s->hit && tlsext_servername == 1) {
2646 if (s->tlsext_hostname) {
2647 if (s->session->tlsext_hostname == NULL) {
2648 s->session->tlsext_hostname =
2649 OPENSSL_strdup(s->tlsext_hostname);
2650 if (!s->session->tlsext_hostname) {
2651 *al = SSL_AD_UNRECOGNIZED_NAME;
2655 *al = SSL_AD_DECODE_ERROR;
2664 * Determine if we need to see RI. Strictly speaking if we want to avoid
2665 * an attack we should *always* see RI even on initial server hello
2666 * because the client doesn't see any renegotiation during an attack.
2667 * However this would mean we could not connect to any server which
2668 * doesn't support RI so for the immediate future tolerate RI absence
2670 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2671 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2672 *al = SSL_AD_HANDSHAKE_FAILURE;
2673 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2674 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2680 * Check extended master secret extension is consistent with
2683 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2684 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2685 *al = SSL_AD_HANDSHAKE_FAILURE;
2686 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2694 int ssl_prepare_clienthello_tlsext(SSL *s)
2696 s->s3->alpn_sent = 0;
2700 int ssl_prepare_serverhello_tlsext(SSL *s)
2705 static int ssl_check_clienthello_tlsext_early(SSL *s)
2707 int ret = SSL_TLSEXT_ERR_NOACK;
2708 int al = SSL_AD_UNRECOGNIZED_NAME;
2710 #ifndef OPENSSL_NO_EC
2712 * The handling of the ECPointFormats extension is done elsewhere, namely
2713 * in ssl3_choose_cipher in s3_lib.c.
2716 * The handling of the EllipticCurves extension is done elsewhere, namely
2717 * in ssl3_choose_cipher in s3_lib.c.
2721 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2723 s->ctx->tlsext_servername_callback(s, &al,
2724 s->ctx->tlsext_servername_arg);
2725 else if (s->session_ctx != NULL
2726 && s->session_ctx->tlsext_servername_callback != 0)
2728 s->session_ctx->tlsext_servername_callback(s, &al,
2730 session_ctx->tlsext_servername_arg);
2733 case SSL_TLSEXT_ERR_ALERT_FATAL:
2734 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2737 case SSL_TLSEXT_ERR_ALERT_WARNING:
2738 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2741 case SSL_TLSEXT_ERR_NOACK:
2742 s->servername_done = 0;
2749 /* Initialise digests to default values */
2750 void ssl_set_default_md(SSL *s)
2752 const EVP_MD **pmd = s->s3->tmp.md;
2753 #ifndef OPENSSL_NO_DSA
2754 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2756 #ifndef OPENSSL_NO_RSA
2757 if (SSL_USE_SIGALGS(s))
2758 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2760 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2761 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2763 #ifndef OPENSSL_NO_EC
2764 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2766 #ifndef OPENSSL_NO_GOST
2767 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2768 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2769 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2773 int tls1_set_server_sigalgs(SSL *s)
2778 /* Clear any shared signature algorithms */
2779 OPENSSL_free(s->cert->shared_sigalgs);
2780 s->cert->shared_sigalgs = NULL;
2781 s->cert->shared_sigalgslen = 0;
2782 /* Clear certificate digests and validity flags */
2783 for (i = 0; i < SSL_PKEY_NUM; i++) {
2784 s->s3->tmp.md[i] = NULL;
2785 s->s3->tmp.valid_flags[i] = 0;
2788 /* If sigalgs received process it. */
2789 if (s->s3->tmp.peer_sigalgs) {
2790 if (!tls1_process_sigalgs(s)) {
2791 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2792 al = SSL_AD_INTERNAL_ERROR;
2795 /* Fatal error is no shared signature algorithms */
2796 if (!s->cert->shared_sigalgs) {
2797 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2798 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
2799 al = SSL_AD_ILLEGAL_PARAMETER;
2803 ssl_set_default_md(s);
2807 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2812 * Upon success, returns 1.
2813 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
2815 int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
2817 s->tlsext_status_expected = 0;
2820 * If status request then ask callback what to do. Note: this must be
2821 * called after servername callbacks in case the certificate has changed,
2822 * and must be called after the cipher has been chosen because this may
2823 * influence which certificate is sent
2825 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2827 CERT_PKEY *certpkey;
2828 certpkey = ssl_get_server_send_pkey(s);
2829 /* If no certificate can't return certificate status */
2830 if (certpkey != NULL) {
2832 * Set current certificate to one we will use so SSL_get_certificate
2833 * et al can pick it up.
2835 s->cert->key = certpkey;
2836 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2838 /* We don't want to send a status request response */
2839 case SSL_TLSEXT_ERR_NOACK:
2840 s->tlsext_status_expected = 0;
2842 /* status request response should be sent */
2843 case SSL_TLSEXT_ERR_OK:
2844 if (s->tlsext_ocsp_resp)
2845 s->tlsext_status_expected = 1;
2847 /* something bad happened */
2848 case SSL_TLSEXT_ERR_ALERT_FATAL:
2850 *al = SSL_AD_INTERNAL_ERROR;
2856 if (!tls1_alpn_handle_client_hello_late(s, al)) {
2863 int ssl_check_serverhello_tlsext(SSL *s)
2865 int ret = SSL_TLSEXT_ERR_NOACK;
2866 int al = SSL_AD_UNRECOGNIZED_NAME;
2868 #ifndef OPENSSL_NO_EC
2870 * If we are client and using an elliptic curve cryptography cipher
2871 * suite, then if server returns an EC point formats lists extension it
2872 * must contain uncompressed.
2874 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2875 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2876 if ((s->tlsext_ecpointformatlist != NULL)
2877 && (s->tlsext_ecpointformatlist_length > 0)
2878 && (s->session->tlsext_ecpointformatlist != NULL)
2879 && (s->session->tlsext_ecpointformatlist_length > 0)
2880 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2881 /* we are using an ECC cipher */
2883 unsigned char *list;
2884 int found_uncompressed = 0;
2885 list = s->session->tlsext_ecpointformatlist;
2886 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2887 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2888 found_uncompressed = 1;
2892 if (!found_uncompressed) {
2893 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2894 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2898 ret = SSL_TLSEXT_ERR_OK;
2899 #endif /* OPENSSL_NO_EC */
2901 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2903 s->ctx->tlsext_servername_callback(s, &al,
2904 s->ctx->tlsext_servername_arg);
2905 else if (s->session_ctx != NULL
2906 && s->session_ctx->tlsext_servername_callback != 0)
2908 s->session_ctx->tlsext_servername_callback(s, &al,
2910 session_ctx->tlsext_servername_arg);
2913 * Ensure we get sensible values passed to tlsext_status_cb in the event
2914 * that we don't receive a status message
2916 OPENSSL_free(s->tlsext_ocsp_resp);
2917 s->tlsext_ocsp_resp = NULL;
2918 s->tlsext_ocsp_resplen = -1;
2921 case SSL_TLSEXT_ERR_ALERT_FATAL:
2922 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2925 case SSL_TLSEXT_ERR_ALERT_WARNING:
2926 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2929 case SSL_TLSEXT_ERR_NOACK:
2930 s->servername_done = 0;
2937 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2940 if (s->version < SSL3_VERSION)
2942 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2943 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2947 if (ssl_check_serverhello_tlsext(s) <= 0) {
2948 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2955 * Since the server cache lookup is done early on in the processing of the
2956 * ClientHello and other operations depend on the result some extensions
2957 * need to be handled at the same time.
2959 * Two extensions are currently handled, session ticket and extended master
2962 * session_id: ClientHello session ID.
2963 * ext: ClientHello extensions (including length prefix)
2964 * ret: (output) on return, if a ticket was decrypted, then this is set to
2965 * point to the resulting session.
2967 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2968 * ciphersuite, in which case we have no use for session tickets and one will
2969 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2972 * -1: fatal error, either from parsing or decrypting the ticket.
2973 * 0: no ticket was found (or was ignored, based on settings).
2974 * 1: a zero length extension was found, indicating that the client supports
2975 * session tickets but doesn't currently have one to offer.
2976 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2977 * couldn't be decrypted because of a non-fatal error.
2978 * 3: a ticket was successfully decrypted and *ret was set.
2981 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2982 * a new session ticket to the client because the client indicated support
2983 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2984 * a session ticket or we couldn't use the one it gave us, or if
2985 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2986 * Otherwise, s->tlsext_ticket_expected is set to 0.
2988 * For extended master secret flag is set if the extension is present.
2991 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2992 const PACKET *session_id,
2996 PACKET local_ext = *ext;
2999 int have_ticket = 0;
3000 int use_ticket = tls_use_ticket(s);
3003 s->tlsext_ticket_expected = 0;
3004 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
3007 * If tickets disabled behave as if no ticket present to permit stateful
3010 if ((s->version <= SSL3_VERSION))
3013 if (!PACKET_get_net_2(&local_ext, &i)) {
3017 while (PACKET_remaining(&local_ext) >= 4) {
3018 unsigned int type, size;
3020 if (!PACKET_get_net_2(&local_ext, &type)
3021 || !PACKET_get_net_2(&local_ext, &size)) {
3022 /* Shouldn't ever happen */
3026 if (PACKET_remaining(&local_ext) < size) {
3030 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
3032 const unsigned char *etick;
3034 /* Duplicate extension */
3035 if (have_ticket != 0) {
3043 * The client will accept a ticket but doesn't currently have
3046 s->tlsext_ticket_expected = 1;
3050 if (s->tls_session_secret_cb) {
3052 * Indicate that the ticket couldn't be decrypted rather than
3053 * generating the session from ticket now, trigger
3054 * abbreviated handshake based on external mechanism to
3055 * calculate the master secret later.
3060 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
3061 /* Shouldn't ever happen */
3065 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
3066 PACKET_remaining(session_id), ret);
3068 case 2: /* ticket couldn't be decrypted */
3069 s->tlsext_ticket_expected = 1;
3072 case 3: /* ticket was decrypted */
3075 case 4: /* ticket decrypted but need to renew */
3076 s->tlsext_ticket_expected = 1;
3079 default: /* fatal error */
3085 if (type == TLSEXT_TYPE_extended_master_secret)
3086 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
3087 if (!PACKET_forward(&local_ext, size)) {
3093 if (have_ticket == 0)
3100 * tls_decrypt_ticket attempts to decrypt a session ticket.
3102 * etick: points to the body of the session ticket extension.
3103 * eticklen: the length of the session tickets extension.
3104 * sess_id: points at the session ID.
3105 * sesslen: the length of the session ID.
3106 * psess: (output) on return, if a ticket was decrypted, then this is set to
3107 * point to the resulting session.
3110 * -2: fatal error, malloc failure.
3111 * -1: fatal error, either from parsing or decrypting the ticket.
3112 * 2: the ticket couldn't be decrypted.
3113 * 3: a ticket was successfully decrypted and *psess was set.
3114 * 4: same as 3, but the ticket needs to be renewed.
3116 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3117 int eticklen, const unsigned char *sess_id,
3118 int sesslen, SSL_SESSION **psess)
3121 unsigned char *sdec;
3122 const unsigned char *p;
3123 int slen, mlen, renew_ticket = 0, ret = -1;
3124 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3125 HMAC_CTX *hctx = NULL;
3126 EVP_CIPHER_CTX *ctx = NULL;
3127 SSL_CTX *tctx = s->session_ctx;
3129 /* Need at least keyname + iv */
3130 if (eticklen < TLSEXT_KEYNAME_LENGTH + EVP_MAX_IV_LENGTH) {
3135 /* Initialize session ticket encryption and HMAC contexts */
3136 hctx = HMAC_CTX_new();
3139 ctx = EVP_CIPHER_CTX_new();
3144 if (tctx->tlsext_ticket_key_cb) {
3145 unsigned char *nctick = (unsigned char *)etick;
3146 int rv = tctx->tlsext_ticket_key_cb(s, nctick,
3147 nctick + TLSEXT_KEYNAME_LENGTH,
3158 /* Check key name matches */
3159 if (memcmp(etick, tctx->tlsext_tick_key_name,
3160 TLSEXT_KEYNAME_LENGTH) != 0) {
3164 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3165 sizeof(tctx->tlsext_tick_hmac_key),
3166 EVP_sha256(), NULL) <= 0
3167 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3168 tctx->tlsext_tick_aes_key,
3169 etick + TLSEXT_KEYNAME_LENGTH) <= 0) {
3174 * Attempt to process session ticket, first conduct sanity and integrity
3177 mlen = HMAC_size(hctx);
3181 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3183 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
3188 /* Check HMAC of encrypted ticket */
3189 if (HMAC_Update(hctx, etick, eticklen) <= 0
3190 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3193 HMAC_CTX_free(hctx);
3194 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3195 EVP_CIPHER_CTX_free(ctx);
3198 /* Attempt to decrypt session data */
3199 /* Move p after IV to start of encrypted ticket, update length */
3200 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
3201 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
3202 sdec = OPENSSL_malloc(eticklen);
3203 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3204 EVP_CIPHER_CTX_free(ctx);
3208 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3209 EVP_CIPHER_CTX_free(ctx);
3214 EVP_CIPHER_CTX_free(ctx);
3218 sess = d2i_SSL_SESSION(NULL, &p, slen);
3222 /* Some additional consistency checks */
3223 if (slen != 0 || sess->session_id_length != 0) {
3224 SSL_SESSION_free(sess);
3228 * The session ID, if non-empty, is used by some clients to detect
3229 * that the ticket has been accepted. So we copy it to the session
3230 * structure. If it is empty set length to zero as required by
3234 memcpy(sess->session_id, sess_id, sesslen);
3235 sess->session_id_length = sesslen;
3244 * For session parse failure, indicate that we need to send a new ticket.
3248 EVP_CIPHER_CTX_free(ctx);
3249 HMAC_CTX_free(hctx);
3253 /* Tables to translate from NIDs to TLS v1.2 ids */
3260 static const tls12_lookup tls12_md[] = {
3261 {NID_md5, TLSEXT_hash_md5},
3262 {NID_sha1, TLSEXT_hash_sha1},
3263 {NID_sha224, TLSEXT_hash_sha224},
3264 {NID_sha256, TLSEXT_hash_sha256},
3265 {NID_sha384, TLSEXT_hash_sha384},
3266 {NID_sha512, TLSEXT_hash_sha512},
3267 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3268 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3269 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3272 static const tls12_lookup tls12_sig[] = {
3273 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3274 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3275 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3276 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3277 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3278 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3281 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3284 for (i = 0; i < tlen; i++) {
3285 if (table[i].nid == nid)
3291 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3294 for (i = 0; i < tlen; i++) {
3295 if ((table[i].id) == id)
3296 return table[i].nid;
3301 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3306 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3309 sig_id = tls12_get_sigid(pk);
3312 p[0] = (unsigned char)md_id;
3313 p[1] = (unsigned char)sig_id;
3317 int tls12_get_sigid(const EVP_PKEY *pk)
3319 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3326 unsigned char tlsext_hash;
3329 static const tls12_hash_info tls12_md_info[] = {
3330 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3331 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3332 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3333 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3334 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3335 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3336 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3337 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX,
3338 TLSEXT_hash_gostr34112012_256},
3339 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX,
3340 TLSEXT_hash_gostr34112012_512},
3343 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3349 for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) {
3350 if (tls12_md_info[i].tlsext_hash == hash_alg)
3351 return tls12_md_info + i;
3357 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3359 const tls12_hash_info *inf;
3360 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3362 inf = tls12_get_hash_info(hash_alg);
3365 return ssl_md(inf->md_idx);
3368 static int tls12_get_pkey_idx(unsigned char sig_alg)
3371 #ifndef OPENSSL_NO_RSA
3372 case TLSEXT_signature_rsa:
3373 return SSL_PKEY_RSA_SIGN;
3375 #ifndef OPENSSL_NO_DSA
3376 case TLSEXT_signature_dsa:
3377 return SSL_PKEY_DSA_SIGN;
3379 #ifndef OPENSSL_NO_EC
3380 case TLSEXT_signature_ecdsa:
3381 return SSL_PKEY_ECC;
3383 #ifndef OPENSSL_NO_GOST
3384 case TLSEXT_signature_gostr34102001:
3385 return SSL_PKEY_GOST01;
3387 case TLSEXT_signature_gostr34102012_256:
3388 return SSL_PKEY_GOST12_256;
3390 case TLSEXT_signature_gostr34102012_512:
3391 return SSL_PKEY_GOST12_512;
3397 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3398 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3399 int *psignhash_nid, const unsigned char *data)
3401 int sign_nid = NID_undef, hash_nid = NID_undef;
3402 if (!phash_nid && !psign_nid && !psignhash_nid)
3404 if (phash_nid || psignhash_nid) {
3405 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3407 *phash_nid = hash_nid;
3409 if (psign_nid || psignhash_nid) {
3410 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3412 *psign_nid = sign_nid;
3414 if (psignhash_nid) {
3415 if (sign_nid == NID_undef || hash_nid == NID_undef
3416 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0)
3417 *psignhash_nid = NID_undef;
3421 /* Check to see if a signature algorithm is allowed */
3422 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3424 /* See if we have an entry in the hash table and it is enabled */
3425 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3426 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3428 /* See if public key algorithm allowed */
3429 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3431 /* Finally see if security callback allows it */
3432 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3436 * Get a mask of disabled public key algorithms based on supported signature
3437 * algorithms. For example if no signature algorithm supports RSA then RSA is
3441 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3443 const unsigned char *sigalgs;
3444 size_t i, sigalgslen;
3445 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3447 * Now go through all signature algorithms seeing if we support any for
3448 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3449 * down calls to security callback only check if we have to.
3451 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
3452 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3453 switch (sigalgs[1]) {
3454 #ifndef OPENSSL_NO_RSA
3455 case TLSEXT_signature_rsa:
3456 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3460 #ifndef OPENSSL_NO_DSA
3461 case TLSEXT_signature_dsa:
3462 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3466 #ifndef OPENSSL_NO_EC
3467 case TLSEXT_signature_ecdsa:
3468 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3475 *pmask_a |= SSL_aRSA;
3477 *pmask_a |= SSL_aDSS;
3479 *pmask_a |= SSL_aECDSA;
3482 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3483 const unsigned char *psig, size_t psiglen)
3485 unsigned char *tmpout = out;
3487 for (i = 0; i < psiglen; i += 2, psig += 2) {
3488 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3489 *tmpout++ = psig[0];
3490 *tmpout++ = psig[1];
3493 return tmpout - out;
3496 /* Given preference and allowed sigalgs set shared sigalgs */
3497 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3498 const unsigned char *pref, size_t preflen,
3499 const unsigned char *allow, size_t allowlen)
3501 const unsigned char *ptmp, *atmp;
3502 size_t i, j, nmatch = 0;
3503 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3504 /* Skip disabled hashes or signature algorithms */
3505 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3507 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3508 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3511 shsig->rhash = ptmp[0];
3512 shsig->rsign = ptmp[1];
3513 tls1_lookup_sigalg(&shsig->hash_nid,
3515 &shsig->signandhash_nid, ptmp);
3525 /* Set shared signature algorithms for SSL structures */
3526 static int tls1_set_shared_sigalgs(SSL *s)
3528 const unsigned char *pref, *allow, *conf;
3529 size_t preflen, allowlen, conflen;
3531 TLS_SIGALGS *salgs = NULL;
3533 unsigned int is_suiteb = tls1_suiteb(s);
3535 OPENSSL_free(c->shared_sigalgs);
3536 c->shared_sigalgs = NULL;
3537 c->shared_sigalgslen = 0;
3538 /* If client use client signature algorithms if not NULL */
3539 if (!s->server && c->client_sigalgs && !is_suiteb) {
3540 conf = c->client_sigalgs;
3541 conflen = c->client_sigalgslen;
3542 } else if (c->conf_sigalgs && !is_suiteb) {
3543 conf = c->conf_sigalgs;
3544 conflen = c->conf_sigalgslen;
3546 conflen = tls12_get_psigalgs(s, 0, &conf);
3547 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3550 allow = s->s3->tmp.peer_sigalgs;
3551 allowlen = s->s3->tmp.peer_sigalgslen;
3555 pref = s->s3->tmp.peer_sigalgs;
3556 preflen = s->s3->tmp.peer_sigalgslen;
3558 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3560 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3563 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3567 c->shared_sigalgs = salgs;
3568 c->shared_sigalgslen = nmatch;
3572 /* Set preferred digest for each key type */
3574 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3577 /* Extension ignored for inappropriate versions */
3578 if (!SSL_USE_SIGALGS(s))
3580 /* Should never happen */
3584 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3585 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3586 if (s->s3->tmp.peer_sigalgs == NULL)
3588 s->s3->tmp.peer_sigalgslen = dsize;
3589 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3593 int tls1_process_sigalgs(SSL *s)
3598 const EVP_MD **pmd = s->s3->tmp.md;
3599 uint32_t *pvalid = s->s3->tmp.valid_flags;
3601 TLS_SIGALGS *sigptr;
3602 if (!tls1_set_shared_sigalgs(s))
3605 for (i = 0, sigptr = c->shared_sigalgs;
3606 i < c->shared_sigalgslen; i++, sigptr++) {
3607 idx = tls12_get_pkey_idx(sigptr->rsign);
3608 if (idx > 0 && pmd[idx] == NULL) {
3609 md = tls12_get_hash(sigptr->rhash);
3611 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3612 if (idx == SSL_PKEY_RSA_SIGN) {
3613 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3614 pmd[SSL_PKEY_RSA_ENC] = md;
3620 * In strict mode leave unset digests as NULL to indicate we can't use
3621 * the certificate for signing.
3623 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3625 * Set any remaining keys to default values. NOTE: if alg is not
3626 * supported it stays as NULL.
3628 #ifndef OPENSSL_NO_DSA
3629 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3630 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3632 #ifndef OPENSSL_NO_RSA
3633 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3634 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3635 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3638 #ifndef OPENSSL_NO_EC
3639 if (pmd[SSL_PKEY_ECC] == NULL)
3640 pmd[SSL_PKEY_ECC] = EVP_sha1();
3642 #ifndef OPENSSL_NO_GOST
3643 if (pmd[SSL_PKEY_GOST01] == NULL)
3644 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3645 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3646 pmd[SSL_PKEY_GOST12_256] =
3647 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3648 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3649 pmd[SSL_PKEY_GOST12_512] =
3650 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3656 int SSL_get_sigalgs(SSL *s, int idx,
3657 int *psign, int *phash, int *psignhash,
3658 unsigned char *rsig, unsigned char *rhash)
3660 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3665 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3672 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3674 return s->s3->tmp.peer_sigalgslen / 2;
3677 int SSL_get_shared_sigalgs(SSL *s, int idx,
3678 int *psign, int *phash, int *psignhash,
3679 unsigned char *rsig, unsigned char *rhash)
3681 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3682 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3686 *phash = shsigalgs->hash_nid;
3688 *psign = shsigalgs->sign_nid;
3690 *psignhash = shsigalgs->signandhash_nid;
3692 *rsig = shsigalgs->rsign;
3694 *rhash = shsigalgs->rhash;
3695 return s->cert->shared_sigalgslen;
3698 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3702 int sigalgs[MAX_SIGALGLEN];
3705 static void get_sigorhash(int *psig, int *phash, const char *str)
3707 if (strcmp(str, "RSA") == 0) {
3708 *psig = EVP_PKEY_RSA;
3709 } else if (strcmp(str, "DSA") == 0) {
3710 *psig = EVP_PKEY_DSA;
3711 } else if (strcmp(str, "ECDSA") == 0) {
3712 *psig = EVP_PKEY_EC;
3714 *phash = OBJ_sn2nid(str);
3715 if (*phash == NID_undef)
3716 *phash = OBJ_ln2nid(str);
3720 static int sig_cb(const char *elem, int len, void *arg)
3722 sig_cb_st *sarg = arg;
3725 int sig_alg = NID_undef, hash_alg = NID_undef;
3728 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3730 if (len > (int)(sizeof(etmp) - 1))
3732 memcpy(etmp, elem, len);
3734 p = strchr(etmp, '+');
3742 get_sigorhash(&sig_alg, &hash_alg, etmp);
3743 get_sigorhash(&sig_alg, &hash_alg, p);
3745 if (sig_alg == NID_undef || hash_alg == NID_undef)
3748 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3749 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3752 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3753 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3758 * Set supported signature algorithms based on a colon separated list of the
3759 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3761 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3765 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3769 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3772 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3774 unsigned char *sigalgs, *sptr;
3779 sigalgs = OPENSSL_malloc(salglen);
3780 if (sigalgs == NULL)
3782 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3783 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3784 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3786 if (rhash == -1 || rsign == -1)
3793 OPENSSL_free(c->client_sigalgs);
3794 c->client_sigalgs = sigalgs;
3795 c->client_sigalgslen = salglen;
3797 OPENSSL_free(c->conf_sigalgs);
3798 c->conf_sigalgs = sigalgs;
3799 c->conf_sigalgslen = salglen;
3805 OPENSSL_free(sigalgs);
3809 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3813 if (default_nid == -1)
3815 sig_nid = X509_get_signature_nid(x);
3817 return sig_nid == default_nid ? 1 : 0;
3818 for (i = 0; i < c->shared_sigalgslen; i++)
3819 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3824 /* Check to see if a certificate issuer name matches list of CA names */
3825 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3829 nm = X509_get_issuer_name(x);
3830 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3831 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3838 * Check certificate chain is consistent with TLS extensions and is usable by
3839 * server. This servers two purposes: it allows users to check chains before
3840 * passing them to the server and it allows the server to check chains before
3841 * attempting to use them.
3844 /* Flags which need to be set for a certificate when strict mode not set */
3846 #define CERT_PKEY_VALID_FLAGS \
3847 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3848 /* Strict mode flags */
3849 #define CERT_PKEY_STRICT_FLAGS \
3850 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3851 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3853 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3858 int check_flags = 0, strict_mode;
3859 CERT_PKEY *cpk = NULL;
3862 unsigned int suiteb_flags = tls1_suiteb(s);
3863 /* idx == -1 means checking server chains */
3865 /* idx == -2 means checking client certificate chains */
3868 idx = cpk - c->pkeys;
3870 cpk = c->pkeys + idx;
3871 pvalid = s->s3->tmp.valid_flags + idx;
3873 pk = cpk->privatekey;
3875 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3876 /* If no cert or key, forget it */
3882 idx = ssl_cert_type(x, pk);
3885 pvalid = s->s3->tmp.valid_flags + idx;
3887 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3888 check_flags = CERT_PKEY_STRICT_FLAGS;
3890 check_flags = CERT_PKEY_VALID_FLAGS;
3897 check_flags |= CERT_PKEY_SUITEB;
3898 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3899 if (ok == X509_V_OK)
3900 rv |= CERT_PKEY_SUITEB;
3901 else if (!check_flags)
3906 * Check all signature algorithms are consistent with signature
3907 * algorithms extension if TLS 1.2 or later and strict mode.
3909 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3911 unsigned char rsign = 0;
3912 if (s->s3->tmp.peer_sigalgs)
3914 /* If no sigalgs extension use defaults from RFC5246 */
3917 case SSL_PKEY_RSA_ENC:
3918 case SSL_PKEY_RSA_SIGN:
3919 rsign = TLSEXT_signature_rsa;
3920 default_nid = NID_sha1WithRSAEncryption;
3923 case SSL_PKEY_DSA_SIGN:
3924 rsign = TLSEXT_signature_dsa;
3925 default_nid = NID_dsaWithSHA1;
3929 rsign = TLSEXT_signature_ecdsa;
3930 default_nid = NID_ecdsa_with_SHA1;
3933 case SSL_PKEY_GOST01:
3934 rsign = TLSEXT_signature_gostr34102001;
3935 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3938 case SSL_PKEY_GOST12_256:
3939 rsign = TLSEXT_signature_gostr34102012_256;
3940 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3943 case SSL_PKEY_GOST12_512:
3944 rsign = TLSEXT_signature_gostr34102012_512;
3945 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3954 * If peer sent no signature algorithms extension and we have set
3955 * preferred signature algorithms check we support sha1.
3957 if (default_nid > 0 && c->conf_sigalgs) {
3959 const unsigned char *p = c->conf_sigalgs;
3960 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3961 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3964 if (j == c->conf_sigalgslen) {
3971 /* Check signature algorithm of each cert in chain */
3972 if (!tls1_check_sig_alg(c, x, default_nid)) {
3976 rv |= CERT_PKEY_EE_SIGNATURE;
3977 rv |= CERT_PKEY_CA_SIGNATURE;
3978 for (i = 0; i < sk_X509_num(chain); i++) {
3979 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3981 rv &= ~CERT_PKEY_CA_SIGNATURE;
3988 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3989 else if (check_flags)
3990 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3992 /* Check cert parameters are consistent */
3993 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3994 rv |= CERT_PKEY_EE_PARAM;
3995 else if (!check_flags)
3998 rv |= CERT_PKEY_CA_PARAM;
3999 /* In strict mode check rest of chain too */
4000 else if (strict_mode) {
4001 rv |= CERT_PKEY_CA_PARAM;
4002 for (i = 0; i < sk_X509_num(chain); i++) {
4003 X509 *ca = sk_X509_value(chain, i);
4004 if (!tls1_check_cert_param(s, ca, 0)) {
4006 rv &= ~CERT_PKEY_CA_PARAM;
4013 if (!s->server && strict_mode) {
4014 STACK_OF(X509_NAME) *ca_dn;
4016 switch (EVP_PKEY_id(pk)) {
4018 check_type = TLS_CT_RSA_SIGN;
4021 check_type = TLS_CT_DSS_SIGN;
4024 check_type = TLS_CT_ECDSA_SIGN;
4028 const unsigned char *ctypes;
4032 ctypelen = (int)c->ctype_num;
4034 ctypes = (unsigned char *)s->s3->tmp.ctype;
4035 ctypelen = s->s3->tmp.ctype_num;
4037 for (i = 0; i < ctypelen; i++) {
4038 if (ctypes[i] == check_type) {
4039 rv |= CERT_PKEY_CERT_TYPE;
4043 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4046 rv |= CERT_PKEY_CERT_TYPE;
4048 ca_dn = s->s3->tmp.ca_names;
4050 if (!sk_X509_NAME_num(ca_dn))
4051 rv |= CERT_PKEY_ISSUER_NAME;
4053 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4054 if (ssl_check_ca_name(ca_dn, x))
4055 rv |= CERT_PKEY_ISSUER_NAME;
4057 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4058 for (i = 0; i < sk_X509_num(chain); i++) {
4059 X509 *xtmp = sk_X509_value(chain, i);
4060 if (ssl_check_ca_name(ca_dn, xtmp)) {
4061 rv |= CERT_PKEY_ISSUER_NAME;
4066 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4069 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4071 if (!check_flags || (rv & check_flags) == check_flags)
4072 rv |= CERT_PKEY_VALID;
4076 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4077 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
4078 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4079 else if (s->s3->tmp.md[idx] != NULL)
4080 rv |= CERT_PKEY_SIGN;
4082 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4085 * When checking a CERT_PKEY structure all flags are irrelevant if the
4089 if (rv & CERT_PKEY_VALID)
4092 /* Preserve explicit sign flag, clear rest */
4093 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
4100 /* Set validity of certificates in an SSL structure */
4101 void tls1_set_cert_validity(SSL *s)
4103 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4104 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4105 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4106 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4107 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
4108 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
4109 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
4112 /* User level utility function to check a chain is suitable */
4113 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4115 return tls1_check_chain(s, x, pk, chain, -1);
4118 #ifndef OPENSSL_NO_DH
4119 DH *ssl_get_auto_dh(SSL *s)
4121 int dh_secbits = 80;
4122 if (s->cert->dh_tmp_auto == 2)
4123 return DH_get_1024_160();
4124 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
4125 if (s->s3->tmp.new_cipher->strength_bits == 256)
4130 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4131 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4134 if (dh_secbits >= 128) {
4142 if (dh_secbits >= 192)
4143 p = BN_get_rfc3526_prime_8192(NULL);
4145 p = BN_get_rfc3526_prime_3072(NULL);
4146 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4154 if (dh_secbits >= 112)
4155 return DH_get_2048_224();
4156 return DH_get_1024_160();
4160 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4163 EVP_PKEY *pkey = X509_get0_pubkey(x);
4166 * If no parameters this will return -1 and fail using the default
4167 * security callback for any non-zero security level. This will
4168 * reject keys which omit parameters but this only affects DSA and
4169 * omission of parameters is never (?) done in practice.
4171 secbits = EVP_PKEY_security_bits(pkey);
4174 return ssl_security(s, op, secbits, 0, x);
4176 return ssl_ctx_security(ctx, op, secbits, 0, x);
4179 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4181 /* Lookup signature algorithm digest */
4182 int secbits = -1, md_nid = NID_undef, sig_nid;
4183 /* Don't check signature if self signed */
4184 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4186 sig_nid = X509_get_signature_nid(x);
4187 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4189 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4190 secbits = EVP_MD_size(md) * 4;
4193 return ssl_security(s, op, secbits, md_nid, x);
4195 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4198 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4201 vfy = SSL_SECOP_PEER;
4203 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4204 return SSL_R_EE_KEY_TOO_SMALL;
4206 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4207 return SSL_R_CA_KEY_TOO_SMALL;
4209 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4210 return SSL_R_CA_MD_TOO_WEAK;
4215 * Check security of a chain, if |sk| includes the end entity certificate then
4216 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
4217 * one to the peer. Return values: 1 if ok otherwise error code to use
4220 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4222 int rv, start_idx, i;
4224 x = sk_X509_value(sk, 0);
4229 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4233 for (i = start_idx; i < sk_X509_num(sk); i++) {
4234 x = sk_X509_value(sk, i);
4235 rv = ssl_security_cert(s, NULL, x, vfy, 0);