2 * Copyright 1995-2016 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>
23 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
24 const unsigned char *sess_id, int sesslen,
26 static int ssl_check_clienthello_tlsext_early(SSL *s);
27 static int ssl_check_serverhello_tlsext(SSL *s);
29 SSL3_ENC_METHOD const TLSv1_enc_data = {
33 tls1_generate_master_secret,
34 tls1_change_cipher_state,
35 tls1_final_finish_mac,
36 TLS1_FINISH_MAC_LENGTH,
37 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
38 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
40 tls1_export_keying_material,
42 SSL3_HM_HEADER_LENGTH,
43 ssl3_set_handshake_header,
47 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
51 tls1_generate_master_secret,
52 tls1_change_cipher_state,
53 tls1_final_finish_mac,
54 TLS1_FINISH_MAC_LENGTH,
55 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
56 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
58 tls1_export_keying_material,
59 SSL_ENC_FLAG_EXPLICIT_IV,
60 SSL3_HM_HEADER_LENGTH,
61 ssl3_set_handshake_header,
65 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
69 tls1_generate_master_secret,
70 tls1_change_cipher_state,
71 tls1_final_finish_mac,
72 TLS1_FINISH_MAC_LENGTH,
73 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
74 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
76 tls1_export_keying_material,
77 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
78 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
79 SSL3_HM_HEADER_LENGTH,
80 ssl3_set_handshake_header,
84 long tls1_default_timeout(void)
87 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
88 * http, the cache would over fill
97 s->method->ssl_clear(s);
101 void tls1_free(SSL *s)
103 OPENSSL_free(s->tlsext_session_ticket);
107 void tls1_clear(SSL *s)
110 if (s->method->version == TLS_ANY_VERSION)
111 s->version = TLS_MAX_VERSION;
113 s->version = s->method->version;
116 #ifndef OPENSSL_NO_EC
119 int nid; /* Curve NID */
120 int secbits; /* Bits of security (from SP800-57) */
121 unsigned int flags; /* Flags: currently just field type */
125 * Table of curve information.
126 * Do not delete entries or reorder this array! It is used as a lookup
127 * table: the index of each entry is one less than the TLS curve id.
129 static const tls_curve_info nid_list[] = {
130 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
131 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
132 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
133 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
134 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
135 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
136 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
137 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
138 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
139 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
140 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
141 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
142 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
143 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
144 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
145 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
146 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
147 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
148 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
149 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
150 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
151 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
152 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
153 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
154 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
155 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
156 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
157 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
158 {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
161 static const unsigned char ecformats_default[] = {
162 TLSEXT_ECPOINTFORMAT_uncompressed,
163 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
164 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
167 /* The default curves */
168 static const unsigned char eccurves_default[] = {
169 0, 29, /* X25519 (29) */
170 0, 23, /* secp256r1 (23) */
171 0, 25, /* secp521r1 (25) */
172 0, 24, /* secp384r1 (24) */
175 static const unsigned char eccurves_all[] = {
176 0, 29, /* X25519 (29) */
177 0, 23, /* secp256r1 (23) */
178 0, 25, /* secp521r1 (25) */
179 0, 24, /* secp384r1 (24) */
180 0, 26, /* brainpoolP256r1 (26) */
181 0, 27, /* brainpoolP384r1 (27) */
182 0, 28, /* brainpool512r1 (28) */
185 * Remaining curves disabled by default but still permitted if set
186 * via an explicit callback or parameters.
188 0, 22, /* secp256k1 (22) */
189 0, 14, /* sect571r1 (14) */
190 0, 13, /* sect571k1 (13) */
191 0, 11, /* sect409k1 (11) */
192 0, 12, /* sect409r1 (12) */
193 0, 9, /* sect283k1 (9) */
194 0, 10, /* sect283r1 (10) */
195 0, 20, /* secp224k1 (20) */
196 0, 21, /* secp224r1 (21) */
197 0, 18, /* secp192k1 (18) */
198 0, 19, /* secp192r1 (19) */
199 0, 15, /* secp160k1 (15) */
200 0, 16, /* secp160r1 (16) */
201 0, 17, /* secp160r2 (17) */
202 0, 8, /* sect239k1 (8) */
203 0, 6, /* sect233k1 (6) */
204 0, 7, /* sect233r1 (7) */
205 0, 4, /* sect193r1 (4) */
206 0, 5, /* sect193r2 (5) */
207 0, 1, /* sect163k1 (1) */
208 0, 2, /* sect163r1 (2) */
209 0, 3, /* sect163r2 (3) */
212 static const unsigned char suiteb_curves[] = {
213 0, TLSEXT_curve_P_256,
214 0, TLSEXT_curve_P_384
217 int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags)
219 const tls_curve_info *cinfo;
220 /* ECC curves from RFC 4492 and RFC 7027 */
221 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
223 cinfo = nid_list + curve_id - 1;
225 *pflags = cinfo->flags;
229 int tls1_ec_nid2curve_id(int nid)
232 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
233 if (nid_list[i].nid == nid)
240 * Get curves list, if "sess" is set return client curves otherwise
242 * Sets |num_curves| to the number of curves in the list, i.e.,
243 * the length of |pcurves| is 2 * num_curves.
244 * Returns 1 on success and 0 if the client curves list has invalid format.
245 * The latter indicates an internal error: we should not be accepting such
246 * lists in the first place.
247 * TODO(emilia): we should really be storing the curves list in explicitly
248 * parsed form instead. (However, this would affect binary compatibility
249 * so cannot happen in the 1.0.x series.)
251 static int tls1_get_curvelist(SSL *s, int sess,
252 const unsigned char **pcurves, size_t *num_curves)
254 size_t pcurveslen = 0;
256 *pcurves = s->session->tlsext_ellipticcurvelist;
257 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
259 /* For Suite B mode only include P-256, P-384 */
260 switch (tls1_suiteb(s)) {
261 case SSL_CERT_FLAG_SUITEB_128_LOS:
262 *pcurves = suiteb_curves;
263 pcurveslen = sizeof(suiteb_curves);
266 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
267 *pcurves = suiteb_curves;
271 case SSL_CERT_FLAG_SUITEB_192_LOS:
272 *pcurves = suiteb_curves + 2;
276 *pcurves = s->tlsext_ellipticcurvelist;
277 pcurveslen = s->tlsext_ellipticcurvelist_length;
280 *pcurves = eccurves_default;
281 pcurveslen = sizeof(eccurves_default);
285 /* We do not allow odd length arrays to enter the system. */
286 if (pcurveslen & 1) {
287 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
291 *num_curves = pcurveslen / 2;
296 /* See if curve is allowed by security callback */
297 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
299 const tls_curve_info *cinfo;
302 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
304 cinfo = &nid_list[curve[1] - 1];
305 # ifdef OPENSSL_NO_EC2M
306 if (cinfo->flags & TLS_CURVE_CHAR2)
309 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
312 /* Check a curve is one of our preferences */
313 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
315 const unsigned char *curves;
316 size_t num_curves, i;
317 unsigned int suiteb_flags = tls1_suiteb(s);
318 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
320 /* Check curve matches Suite B preferences */
322 unsigned long cid = s->s3->tmp.new_cipher->id;
325 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
326 if (p[2] != TLSEXT_curve_P_256)
328 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
329 if (p[2] != TLSEXT_curve_P_384)
331 } else /* Should never happen */
334 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
336 for (i = 0; i < num_curves; i++, curves += 2) {
337 if (p[1] == curves[0] && p[2] == curves[1])
338 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
344 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
345 * if there is no match.
346 * For nmatch == -1, return number of matches
347 * For nmatch == -2, return the NID of the curve to use for
348 * an EC tmp key, or NID_undef if there is no match.
350 int tls1_shared_curve(SSL *s, int nmatch)
352 const unsigned char *pref, *supp;
353 size_t num_pref, num_supp, i, j;
355 /* Can't do anything on client side */
359 if (tls1_suiteb(s)) {
361 * For Suite B ciphersuite determines curve: we already know
362 * these are acceptable due to previous checks.
364 unsigned long cid = s->s3->tmp.new_cipher->id;
365 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
366 return NID_X9_62_prime256v1; /* P-256 */
367 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
368 return NID_secp384r1; /* P-384 */
369 /* Should never happen */
372 /* If not Suite B just return first preference shared curve */
376 * Avoid truncation. tls1_get_curvelist takes an int
377 * but s->options is a long...
379 if (!tls1_get_curvelist
380 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
382 /* In practice, NID_undef == 0 but let's be precise. */
383 return nmatch == -1 ? 0 : NID_undef;
384 if (!tls1_get_curvelist
385 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, &num_pref))
386 return nmatch == -1 ? 0 : NID_undef;
389 * If the client didn't send the elliptic_curves extension all of them
392 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
394 num_supp = sizeof(eccurves_all) / 2;
395 } else if (num_pref == 0 &&
396 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
398 num_pref = sizeof(eccurves_all) / 2;
402 for (i = 0; i < num_pref; i++, pref += 2) {
403 const unsigned char *tsupp = supp;
404 for (j = 0; j < num_supp; j++, tsupp += 2) {
405 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
406 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
409 int id = (pref[0] << 8) | pref[1];
410 return tls1_ec_curve_id2nid(id, NULL);
418 /* Out of range (nmatch > k). */
422 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
423 int *curves, size_t ncurves)
425 unsigned char *clist, *p;
428 * Bitmap of curves included to detect duplicates: only works while curve
431 unsigned long dup_list = 0;
432 clist = OPENSSL_malloc(ncurves * 2);
435 for (i = 0, p = clist; i < ncurves; i++) {
436 unsigned long idmask;
438 id = tls1_ec_nid2curve_id(curves[i]);
440 if (!id || (dup_list & idmask)) {
449 *pextlen = ncurves * 2;
453 # define MAX_CURVELIST 28
457 int nid_arr[MAX_CURVELIST];
460 static int nid_cb(const char *elem, int len, void *arg)
462 nid_cb_st *narg = arg;
468 if (narg->nidcnt == MAX_CURVELIST)
470 if (len > (int)(sizeof(etmp) - 1))
472 memcpy(etmp, elem, len);
474 nid = EC_curve_nist2nid(etmp);
475 if (nid == NID_undef)
476 nid = OBJ_sn2nid(etmp);
477 if (nid == NID_undef)
478 nid = OBJ_ln2nid(etmp);
479 if (nid == NID_undef)
481 for (i = 0; i < narg->nidcnt; i++)
482 if (narg->nid_arr[i] == nid)
484 narg->nid_arr[narg->nidcnt++] = nid;
488 /* Set curves based on a colon separate list */
489 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen, const char *str)
493 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
497 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
500 /* For an EC key set TLS id and required compression based on parameters */
501 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
508 /* Determine if it is a prime field */
509 grp = EC_KEY_get0_group(ec);
512 /* Determine curve ID */
513 id = EC_GROUP_get_curve_name(grp);
514 id = tls1_ec_nid2curve_id(id);
515 /* If no id return error: we don't support arbitrary explicit curves */
519 curve_id[1] = (unsigned char)id;
521 if (EC_KEY_get0_public_key(ec) == NULL)
523 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
524 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
526 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
527 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
529 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
535 /* Check an EC key is compatible with extensions */
536 static int tls1_check_ec_key(SSL *s,
537 unsigned char *curve_id, unsigned char *comp_id)
539 const unsigned char *pformats, *pcurves;
540 size_t num_formats, num_curves, i;
543 * If point formats extension present check it, otherwise everything is
544 * supported (see RFC4492).
546 if (comp_id && s->session->tlsext_ecpointformatlist) {
547 pformats = s->session->tlsext_ecpointformatlist;
548 num_formats = s->session->tlsext_ecpointformatlist_length;
549 for (i = 0; i < num_formats; i++, pformats++) {
550 if (*comp_id == *pformats)
553 if (i == num_formats)
558 /* Check curve is consistent with client and server preferences */
559 for (j = 0; j <= 1; j++) {
560 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
562 if (j == 1 && num_curves == 0) {
564 * If we've not received any curves then skip this check.
565 * RFC 4492 does not require the supported elliptic curves extension
566 * so if it is not sent we can just choose any curve.
567 * It is invalid to send an empty list in the elliptic curves
568 * extension, so num_curves == 0 always means no extension.
572 for (i = 0; i < num_curves; i++, pcurves += 2) {
573 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
578 /* For clients can only check sent curve list */
585 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
589 * If we have a custom point format list use it otherwise use default
591 if (s->tlsext_ecpointformatlist) {
592 *pformats = s->tlsext_ecpointformatlist;
593 *num_formats = s->tlsext_ecpointformatlist_length;
595 *pformats = ecformats_default;
596 /* For Suite B we don't support char2 fields */
598 *num_formats = sizeof(ecformats_default) - 1;
600 *num_formats = sizeof(ecformats_default);
605 * Check cert parameters compatible with extensions: currently just checks EC
606 * certificates have compatible curves and compression.
608 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
610 unsigned char comp_id, curve_id[2];
613 pkey = X509_get0_pubkey(x);
616 /* If not EC nothing to do */
617 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
619 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
623 * Can't check curve_id for client certs as we don't have a supported
626 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
630 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
631 * SHA384+P-384, adjust digest if necessary.
633 if (set_ee_md && tls1_suiteb(s)) {
639 /* Check to see we have necessary signing algorithm */
640 if (curve_id[1] == TLSEXT_curve_P_256)
641 check_md = NID_ecdsa_with_SHA256;
642 else if (curve_id[1] == TLSEXT_curve_P_384)
643 check_md = NID_ecdsa_with_SHA384;
645 return 0; /* Should never happen */
646 for (i = 0; i < c->shared_sigalgslen; i++)
647 if (check_md == c->shared_sigalgs[i].signandhash_nid)
649 if (i == c->shared_sigalgslen)
651 if (set_ee_md == 2) {
652 if (check_md == NID_ecdsa_with_SHA256)
653 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
655 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
661 # ifndef OPENSSL_NO_EC
663 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
665 * @cid: Cipher ID we're considering using
667 * Checks that the kECDHE cipher suite we're considering using
668 * is compatible with the client extensions.
670 * Returns 0 when the cipher can't be used or 1 when it can.
672 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
675 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
678 if (tls1_suiteb(s)) {
679 unsigned char curve_id[2];
680 /* Curve to check determined by ciphersuite */
681 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
682 curve_id[1] = TLSEXT_curve_P_256;
683 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
684 curve_id[1] = TLSEXT_curve_P_384;
688 /* Check this curve is acceptable */
689 if (!tls1_check_ec_key(s, curve_id, NULL))
693 /* Need a shared curve */
694 if (tls1_shared_curve(s, 0))
698 # endif /* OPENSSL_NO_EC */
702 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
707 #endif /* OPENSSL_NO_EC */
710 * List of supported signature algorithms and hashes. Should make this
711 * customisable at some point, for now include everything we support.
714 #ifdef OPENSSL_NO_RSA
715 # define tlsext_sigalg_rsa(md) /* */
717 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
720 #ifdef OPENSSL_NO_DSA
721 # define tlsext_sigalg_dsa(md) /* */
723 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
727 # define tlsext_sigalg_ecdsa(md)/* */
729 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
732 #define tlsext_sigalg(md) \
733 tlsext_sigalg_rsa(md) \
734 tlsext_sigalg_dsa(md) \
735 tlsext_sigalg_ecdsa(md)
737 static const unsigned char tls12_sigalgs[] = {
738 tlsext_sigalg(TLSEXT_hash_sha512)
739 tlsext_sigalg(TLSEXT_hash_sha384)
740 tlsext_sigalg(TLSEXT_hash_sha256)
741 tlsext_sigalg(TLSEXT_hash_sha224)
742 tlsext_sigalg(TLSEXT_hash_sha1)
743 #ifndef OPENSSL_NO_GOST
744 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
745 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
746 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
750 #ifndef OPENSSL_NO_EC
751 static const unsigned char suiteb_sigalgs[] = {
752 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
753 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
756 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
759 * If Suite B mode use Suite B sigalgs only, ignore any other
762 #ifndef OPENSSL_NO_EC
763 switch (tls1_suiteb(s)) {
764 case SSL_CERT_FLAG_SUITEB_128_LOS:
765 *psigs = suiteb_sigalgs;
766 return sizeof(suiteb_sigalgs);
768 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
769 *psigs = suiteb_sigalgs;
772 case SSL_CERT_FLAG_SUITEB_192_LOS:
773 *psigs = suiteb_sigalgs + 2;
777 /* If server use client authentication sigalgs if not NULL */
778 if (s->server && s->cert->client_sigalgs) {
779 *psigs = s->cert->client_sigalgs;
780 return s->cert->client_sigalgslen;
781 } else if (s->cert->conf_sigalgs) {
782 *psigs = s->cert->conf_sigalgs;
783 return s->cert->conf_sigalgslen;
785 *psigs = tls12_sigalgs;
786 return sizeof(tls12_sigalgs);
791 * Check signature algorithm is consistent with sent supported signature
792 * algorithms and if so return relevant digest.
794 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
795 const unsigned char *sig, EVP_PKEY *pkey)
797 const unsigned char *sent_sigs;
798 size_t sent_sigslen, i;
799 int sigalg = tls12_get_sigid(pkey);
800 /* Should never happen */
803 /* Check key type is consistent with signature */
804 if (sigalg != (int)sig[1]) {
805 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
808 #ifndef OPENSSL_NO_EC
809 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
810 unsigned char curve_id[2], comp_id;
811 /* Check compression and curve matches extensions */
812 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
814 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
815 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
818 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
819 if (tls1_suiteb(s)) {
822 if (curve_id[1] == TLSEXT_curve_P_256) {
823 if (sig[0] != TLSEXT_hash_sha256) {
824 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
825 SSL_R_ILLEGAL_SUITEB_DIGEST);
828 } else if (curve_id[1] == TLSEXT_curve_P_384) {
829 if (sig[0] != TLSEXT_hash_sha384) {
830 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
831 SSL_R_ILLEGAL_SUITEB_DIGEST);
837 } else if (tls1_suiteb(s))
841 /* Check signature matches a type we sent */
842 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
843 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
844 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
847 /* Allow fallback to SHA1 if not strict mode */
848 if (i == sent_sigslen
849 && (sig[0] != TLSEXT_hash_sha1
850 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
851 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
854 *pmd = tls12_get_hash(sig[0]);
856 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
859 /* Make sure security callback allows algorithm */
860 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
861 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) {
862 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
866 * Store the digest used so applications can retrieve it if they wish.
868 s->s3->tmp.peer_md = *pmd;
873 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
874 * supported, doesn't appear in supported signature algorithms, isn't supported
875 * by the enabled protocol versions or by the security level.
877 * This function should only be used for checking which ciphers are supported
880 * Call ssl_cipher_disabled() to check that it's enabled or not.
882 void ssl_set_client_disabled(SSL *s)
884 s->s3->tmp.mask_a = 0;
885 s->s3->tmp.mask_k = 0;
886 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
887 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
888 #ifndef OPENSSL_NO_PSK
889 /* with PSK there must be client callback set */
890 if (!s->psk_client_callback) {
891 s->s3->tmp.mask_a |= SSL_aPSK;
892 s->s3->tmp.mask_k |= SSL_PSK;
894 #endif /* OPENSSL_NO_PSK */
895 #ifndef OPENSSL_NO_SRP
896 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
897 s->s3->tmp.mask_a |= SSL_aSRP;
898 s->s3->tmp.mask_k |= SSL_kSRP;
904 * ssl_cipher_disabled - check that a cipher is disabled or not
905 * @s: SSL connection that you want to use the cipher on
906 * @c: cipher to check
907 * @op: Security check that you want to do
909 * Returns 1 when it's disabled, 0 when enabled.
911 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
913 if (c->algorithm_mkey & s->s3->tmp.mask_k
914 || c->algorithm_auth & s->s3->tmp.mask_a)
916 if (s->s3->tmp.max_ver == 0)
918 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
919 || (c->max_tls < s->s3->tmp.min_ver)))
921 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
922 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
925 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
928 static int tls_use_ticket(SSL *s)
930 if (s->options & SSL_OP_NO_TICKET)
932 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
935 static int compare_uint(const void *p1, const void *p2)
937 unsigned int u1 = *((const unsigned int *)p1);
938 unsigned int u2 = *((const unsigned int *)p2);
948 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
949 * more than one extension of the same type in a ClientHello or ServerHello.
950 * This function does an initial scan over the extensions block to filter those
951 * out. It returns 1 if all extensions are unique, and 0 if the extensions
952 * contain duplicates, could not be successfully parsed, or an internal error
955 static int tls1_check_duplicate_extensions(const PACKET *packet)
957 PACKET extensions = *packet;
958 size_t num_extensions = 0, i = 0;
959 unsigned int *extension_types = NULL;
962 /* First pass: count the extensions. */
963 while (PACKET_remaining(&extensions) > 0) {
966 if (!PACKET_get_net_2(&extensions, &type) ||
967 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
973 if (num_extensions <= 1)
976 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
977 if (extension_types == NULL) {
978 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
982 /* Second pass: gather the extension types. */
983 extensions = *packet;
984 for (i = 0; i < num_extensions; i++) {
986 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
987 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
988 /* This should not happen. */
989 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
994 if (PACKET_remaining(&extensions) != 0) {
995 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
998 /* Sort the extensions and make sure there are no duplicates. */
999 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1000 for (i = 1; i < num_extensions; i++) {
1001 if (extension_types[i - 1] == extension_types[i])
1006 OPENSSL_free(extension_types);
1010 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1011 unsigned char *limit, int *al)
1014 unsigned char *orig = buf;
1015 unsigned char *ret = buf;
1016 #ifndef OPENSSL_NO_EC
1017 /* See if we support any ECC ciphersuites */
1019 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1021 unsigned long alg_k, alg_a;
1022 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1024 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1025 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1027 alg_k = c->algorithm_mkey;
1028 alg_a = c->algorithm_auth;
1029 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1030 || (alg_a & SSL_aECDSA)) {
1041 return NULL; /* this really never occurs, but ... */
1043 /* Add RI if renegotiating */
1044 if (s->renegotiate) {
1047 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1048 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1052 if ((limit - ret - 4 - el) < 0)
1055 s2n(TLSEXT_TYPE_renegotiate, ret);
1058 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1059 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1065 /* Only add RI for SSLv3 */
1066 if (s->client_version == SSL3_VERSION)
1069 if (s->tlsext_hostname != NULL) {
1070 /* Add TLS extension servername to the Client Hello message */
1071 unsigned long size_str;
1075 * check for enough space.
1076 * 4 for the servername type and extension length
1077 * 2 for servernamelist length
1078 * 1 for the hostname type
1079 * 2 for hostname length
1083 if ((lenmax = limit - ret - 9) < 0
1084 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1087 /* extension type and length */
1088 s2n(TLSEXT_TYPE_server_name, ret);
1089 s2n(size_str + 5, ret);
1091 /* length of servername list */
1092 s2n(size_str + 3, ret);
1094 /* hostname type, length and hostname */
1095 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1097 memcpy(ret, s->tlsext_hostname, size_str);
1100 #ifndef OPENSSL_NO_SRP
1101 /* Add SRP username if there is one */
1102 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1103 * Client Hello message */
1105 int login_len = strlen(s->srp_ctx.login);
1106 if (login_len > 255 || login_len == 0) {
1107 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1112 * check for enough space.
1113 * 4 for the srp type type and extension length
1114 * 1 for the srp user identity
1115 * + srp user identity length
1117 if ((limit - ret - 5 - login_len) < 0)
1120 /* fill in the extension */
1121 s2n(TLSEXT_TYPE_srp, ret);
1122 s2n(login_len + 1, ret);
1123 (*ret++) = (unsigned char)login_len;
1124 memcpy(ret, s->srp_ctx.login, login_len);
1129 #ifndef OPENSSL_NO_EC
1132 * Add TLS extension ECPointFormats to the ClientHello message
1135 const unsigned char *pcurves, *pformats;
1136 size_t num_curves, num_formats, curves_list_len;
1138 unsigned char *etmp;
1140 tls1_get_formatlist(s, &pformats, &num_formats);
1142 if ((lenmax = limit - ret - 5) < 0)
1144 if (num_formats > (size_t)lenmax)
1146 if (num_formats > 255) {
1147 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1151 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1152 /* The point format list has 1-byte length. */
1153 s2n(num_formats + 1, ret);
1154 *(ret++) = (unsigned char)num_formats;
1155 memcpy(ret, pformats, num_formats);
1159 * Add TLS extension EllipticCurves to the ClientHello message
1161 pcurves = s->tlsext_ellipticcurvelist;
1162 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1165 if ((lenmax = limit - ret - 6) < 0)
1167 if (num_curves > (size_t)lenmax / 2)
1169 if (num_curves > 65532 / 2) {
1170 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1174 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1176 /* Copy curve ID if supported */
1177 for (i = 0; i < num_curves; i++, pcurves += 2) {
1178 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1179 *etmp++ = pcurves[0];
1180 *etmp++ = pcurves[1];
1184 curves_list_len = etmp - ret - 4;
1186 s2n(curves_list_len + 2, ret);
1187 s2n(curves_list_len, ret);
1188 ret += curves_list_len;
1190 #endif /* OPENSSL_NO_EC */
1192 if (tls_use_ticket(s)) {
1194 if (!s->new_session && s->session && s->session->tlsext_tick)
1195 ticklen = s->session->tlsext_ticklen;
1196 else if (s->session && s->tlsext_session_ticket &&
1197 s->tlsext_session_ticket->data) {
1198 ticklen = s->tlsext_session_ticket->length;
1199 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1200 if (s->session->tlsext_tick == NULL)
1202 memcpy(s->session->tlsext_tick,
1203 s->tlsext_session_ticket->data, ticklen);
1204 s->session->tlsext_ticklen = ticklen;
1207 if (ticklen == 0 && s->tlsext_session_ticket &&
1208 s->tlsext_session_ticket->data == NULL)
1211 * Check for enough room 2 for extension type, 2 for len rest for
1214 if ((long)(limit - ret - 4 - ticklen) < 0)
1216 s2n(TLSEXT_TYPE_session_ticket, ret);
1219 memcpy(ret, s->session->tlsext_tick, ticklen);
1225 if (SSL_CLIENT_USE_SIGALGS(s)) {
1227 const unsigned char *salg;
1228 unsigned char *etmp;
1229 salglen = tls12_get_psigalgs(s, &salg);
1230 if ((size_t)(limit - ret) < salglen + 6)
1232 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1234 /* Skip over lengths for now */
1236 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1237 /* Fill in lengths */
1238 s2n(salglen + 2, etmp);
1242 #ifndef OPENSSL_NO_OCSP
1243 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1245 long extlen, idlen, itmp;
1249 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1250 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1251 itmp = i2d_OCSP_RESPID(id, NULL);
1257 if (s->tlsext_ocsp_exts) {
1258 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1264 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1266 s2n(TLSEXT_TYPE_status_request, ret);
1267 if (extlen + idlen > 0xFFF0)
1269 s2n(extlen + idlen + 5, ret);
1270 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1272 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1273 /* save position of id len */
1274 unsigned char *q = ret;
1275 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1276 /* skip over id len */
1278 itmp = i2d_OCSP_RESPID(id, &ret);
1284 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1287 #ifndef OPENSSL_NO_HEARTBEATS
1288 if (SSL_IS_DTLS(s)) {
1289 /* Add Heartbeat extension */
1290 if ((limit - ret - 4 - 1) < 0)
1292 s2n(TLSEXT_TYPE_heartbeat, ret);
1296 * 1: peer may send requests
1297 * 2: peer not allowed to send requests
1299 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1300 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1302 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1306 #ifndef OPENSSL_NO_NEXTPROTONEG
1307 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1309 * The client advertises an empty extension to indicate its support
1310 * for Next Protocol Negotiation
1312 if (limit - ret - 4 < 0)
1314 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1320 * finish_md_len is non-zero during a renegotiation, so
1321 * this avoids sending ALPN during the renegotiation
1322 * (see longer comment below)
1324 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1325 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
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);
1344 if ((limit - ret - 4 - el) < 0)
1347 s2n(TLSEXT_TYPE_use_srtp, ret);
1350 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1351 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1357 custom_ext_init(&s->cert->cli_ext);
1358 /* Add custom TLS Extensions to ClientHello */
1359 if (!custom_ext_add(s, 0, &ret, limit, al))
1361 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1363 #ifndef OPENSSL_NO_CT
1364 if (s->ct_validation_callback != NULL) {
1365 s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret);
1369 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1373 * Add padding to workaround bugs in F5 terminators. See
1374 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1375 * code works out the length of all existing extensions it MUST always
1378 if (s->options & SSL_OP_TLSEXT_PADDING) {
1379 int hlen = ret - (unsigned char *)s->init_buf->data;
1381 if (hlen > 0xff && hlen < 0x200) {
1382 hlen = 0x200 - hlen;
1388 s2n(TLSEXT_TYPE_padding, ret);
1390 memset(ret, 0, hlen);
1397 if ((extdatalen = ret - orig - 2) == 0)
1400 s2n(extdatalen, orig);
1404 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1405 unsigned char *limit, int *al)
1408 unsigned char *orig = buf;
1409 unsigned char *ret = buf;
1410 #ifndef OPENSSL_NO_NEXTPROTONEG
1411 int next_proto_neg_seen;
1413 #ifndef OPENSSL_NO_EC
1414 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1415 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1416 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1417 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1422 return NULL; /* this really never occurs, but ... */
1424 if (s->s3->send_connection_binding) {
1427 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1428 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1432 if ((limit - ret - 4 - el) < 0)
1435 s2n(TLSEXT_TYPE_renegotiate, ret);
1438 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1439 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1446 /* Only add RI for SSLv3 */
1447 if (s->version == SSL3_VERSION)
1450 if (!s->hit && s->servername_done == 1
1451 && s->session->tlsext_hostname != NULL) {
1452 if ((long)(limit - ret - 4) < 0)
1455 s2n(TLSEXT_TYPE_server_name, ret);
1458 #ifndef OPENSSL_NO_EC
1460 const unsigned char *plist;
1463 * Add TLS extension ECPointFormats to the ServerHello message
1467 tls1_get_formatlist(s, &plist, &plistlen);
1469 if ((lenmax = limit - ret - 5) < 0)
1471 if (plistlen > (size_t)lenmax)
1473 if (plistlen > 255) {
1474 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1478 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1479 s2n(plistlen + 1, ret);
1480 *(ret++) = (unsigned char)plistlen;
1481 memcpy(ret, plist, plistlen);
1486 * Currently the server should not respond with a SupportedCurves
1489 #endif /* OPENSSL_NO_EC */
1491 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1492 if ((long)(limit - ret - 4) < 0)
1494 s2n(TLSEXT_TYPE_session_ticket, ret);
1498 * if we don't add the above TLSEXT, we can't add a session ticket
1501 s->tlsext_ticket_expected = 0;
1504 if (s->tlsext_status_expected) {
1505 if ((long)(limit - ret - 4) < 0)
1507 s2n(TLSEXT_TYPE_status_request, ret);
1510 #ifndef OPENSSL_NO_SRTP
1511 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1514 /* Returns 0 on success!! */
1515 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1516 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1519 if ((limit - ret - 4 - el) < 0)
1522 s2n(TLSEXT_TYPE_use_srtp, ret);
1525 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1526 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1533 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1534 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1535 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1536 const unsigned char cryptopro_ext[36] = {
1537 0xfd, 0xe8, /* 65000 */
1538 0x00, 0x20, /* 32 bytes length */
1539 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1540 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1541 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1542 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1544 if (limit - ret < 36)
1546 memcpy(ret, cryptopro_ext, 36);
1550 #ifndef OPENSSL_NO_HEARTBEATS
1551 /* Add Heartbeat extension if we've received one */
1552 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1553 if ((limit - ret - 4 - 1) < 0)
1555 s2n(TLSEXT_TYPE_heartbeat, ret);
1559 * 1: peer may send requests
1560 * 2: peer not allowed to send requests
1562 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1563 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1565 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1570 #ifndef OPENSSL_NO_NEXTPROTONEG
1571 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1572 s->s3->next_proto_neg_seen = 0;
1573 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1574 const unsigned char *npa;
1575 unsigned int npalen;
1578 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1580 ctx->next_protos_advertised_cb_arg);
1581 if (r == SSL_TLSEXT_ERR_OK) {
1582 if ((long)(limit - ret - 4 - npalen) < 0)
1584 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1586 memcpy(ret, npa, npalen);
1588 s->s3->next_proto_neg_seen = 1;
1592 if (!custom_ext_add(s, 1, &ret, limit, al))
1594 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1596 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1597 * for other cases too.
1599 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1600 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1601 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1602 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1603 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1605 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1609 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1610 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1614 if (s->s3->alpn_selected != NULL) {
1615 const unsigned char *selected = s->s3->alpn_selected;
1616 unsigned int len = s->s3->alpn_selected_len;
1618 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1620 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1624 memcpy(ret, selected, len);
1630 if ((extdatalen = ret - orig - 2) == 0)
1633 s2n(extdatalen, orig);
1638 * Save the ALPN extension in a ClientHello.
1639 * pkt: the contents of the ALPN extension, not including type and length.
1640 * al: a pointer to the alert value to send in the event of a failure.
1641 * returns: 1 on success, 0 on error.
1643 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1645 PACKET protocol_list, save_protocol_list, protocol;
1647 *al = SSL_AD_DECODE_ERROR;
1649 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1650 || PACKET_remaining(&protocol_list) < 2) {
1654 save_protocol_list = protocol_list;
1656 /* Protocol names can't be empty. */
1657 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1658 || PACKET_remaining(&protocol) == 0) {
1661 } while (PACKET_remaining(&protocol_list) != 0);
1663 if (!PACKET_memdup(&save_protocol_list,
1664 &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
1665 *al = TLS1_AD_INTERNAL_ERROR;
1673 * Process the ALPN extension in a ClientHello.
1674 * al: a pointer to the alert value to send in the event of a failure.
1675 * returns 1 on success, 0 on error.
1677 static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
1679 const unsigned char *selected = NULL;
1680 unsigned char selected_len = 0;
1682 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1683 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1684 s->s3->alpn_proposed,
1685 s->s3->alpn_proposed_len,
1686 s->ctx->alpn_select_cb_arg);
1688 if (r == SSL_TLSEXT_ERR_OK) {
1689 OPENSSL_free(s->s3->alpn_selected);
1690 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1691 if (s->s3->alpn_selected == NULL) {
1692 *al = SSL_AD_INTERNAL_ERROR;
1695 s->s3->alpn_selected_len = selected_len;
1696 #ifndef OPENSSL_NO_NEXTPROTONEG
1697 /* ALPN takes precedence over NPN. */
1698 s->s3->next_proto_neg_seen = 0;
1701 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1709 #ifndef OPENSSL_NO_EC
1711 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1712 * SecureTransport using the TLS extension block in |pkt|.
1713 * Safari, since 10.6, sends exactly these extensions, in this order:
1718 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1719 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1720 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1721 * 10.8..10.8.3 (which don't work).
1723 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1729 static const unsigned char kSafariExtensionsBlock[] = {
1730 0x00, 0x0a, /* elliptic_curves extension */
1731 0x00, 0x08, /* 8 bytes */
1732 0x00, 0x06, /* 6 bytes of curve ids */
1733 0x00, 0x17, /* P-256 */
1734 0x00, 0x18, /* P-384 */
1735 0x00, 0x19, /* P-521 */
1737 0x00, 0x0b, /* ec_point_formats */
1738 0x00, 0x02, /* 2 bytes */
1739 0x01, /* 1 point format */
1740 0x00, /* uncompressed */
1741 /* The following is only present in TLS 1.2 */
1742 0x00, 0x0d, /* signature_algorithms */
1743 0x00, 0x0c, /* 12 bytes */
1744 0x00, 0x0a, /* 10 bytes */
1745 0x05, 0x01, /* SHA-384/RSA */
1746 0x04, 0x01, /* SHA-256/RSA */
1747 0x02, 0x01, /* SHA-1/RSA */
1748 0x04, 0x03, /* SHA-256/ECDSA */
1749 0x02, 0x03, /* SHA-1/ECDSA */
1752 /* Length of the common prefix (first two extensions). */
1753 static const size_t kSafariCommonExtensionsLength = 18;
1757 if (!PACKET_forward(&tmppkt, 2)
1758 || !PACKET_get_net_2(&tmppkt, &type)
1759 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1763 if (type != TLSEXT_TYPE_server_name)
1766 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1767 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1769 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1772 #endif /* !OPENSSL_NO_EC */
1775 * Parse ClientHello extensions and stash extension info in various parts of
1776 * the SSL object. Verify that there are no duplicate extensions.
1778 * Behaviour upon resumption is extension-specific. If the extension has no
1779 * effect during resumption, it is parsed (to verify its format) but otherwise
1782 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1783 * Upon failure, sets |al| to the appropriate alert.
1785 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1788 int renegotiate_seen = 0;
1791 *al = SSL_AD_DECODE_ERROR;
1792 s->servername_done = 0;
1793 s->tlsext_status_type = -1;
1794 #ifndef OPENSSL_NO_NEXTPROTONEG
1795 s->s3->next_proto_neg_seen = 0;
1798 OPENSSL_free(s->s3->alpn_selected);
1799 s->s3->alpn_selected = NULL;
1800 s->s3->alpn_selected_len = 0;
1801 OPENSSL_free(s->s3->alpn_proposed);
1802 s->s3->alpn_proposed = NULL;
1803 s->s3->alpn_proposed_len = 0;
1804 #ifndef OPENSSL_NO_HEARTBEATS
1805 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1806 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1809 #ifndef OPENSSL_NO_EC
1810 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1811 ssl_check_for_safari(s, pkt);
1812 #endif /* !OPENSSL_NO_EC */
1814 /* Clear any signature algorithms extension received */
1815 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1816 s->s3->tmp.peer_sigalgs = NULL;
1817 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1819 #ifndef OPENSSL_NO_SRP
1820 OPENSSL_free(s->srp_ctx.login);
1821 s->srp_ctx.login = NULL;
1824 s->srtp_profile = NULL;
1826 if (PACKET_remaining(pkt) == 0)
1829 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1832 if (!tls1_check_duplicate_extensions(&extensions))
1836 * We parse all extensions to ensure the ClientHello is well-formed but,
1837 * unless an extension specifies otherwise, we ignore extensions upon
1840 while (PACKET_get_net_2(&extensions, &type)) {
1842 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1845 if (s->tlsext_debug_cb)
1846 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1847 PACKET_remaining(&extension),
1848 s->tlsext_debug_arg);
1850 if (type == TLSEXT_TYPE_renegotiate) {
1851 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
1853 renegotiate_seen = 1;
1854 } else if (s->version == SSL3_VERSION) {
1857 * The servername extension is treated as follows:
1859 * - Only the hostname type is supported with a maximum length of 255.
1860 * - The servername is rejected if too long or if it contains zeros,
1861 * in which case an fatal alert is generated.
1862 * - The servername field is maintained together with the session cache.
1863 * - When a session is resumed, the servername call back invoked in order
1864 * to allow the application to position itself to the right context.
1865 * - The servername is acknowledged if it is new for a session or when
1866 * it is identical to a previously used for the same session.
1867 * Applications can control the behaviour. They can at any time
1868 * set a 'desirable' servername for a new SSL object. This can be the
1869 * case for example with HTTPS when a Host: header field is received and
1870 * a renegotiation is requested. In this case, a possible servername
1871 * presented in the new client hello is only acknowledged if it matches
1872 * the value of the Host: field.
1873 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1874 * if they provide for changing an explicit servername context for the
1875 * session, i.e. when the session has been established with a servername
1877 * - On session reconnect, the servername extension may be absent.
1881 else if (type == TLSEXT_TYPE_server_name) {
1882 unsigned int servname_type;
1883 PACKET sni, hostname;
1885 if (!PACKET_as_length_prefixed_2(&extension, &sni)
1886 /* ServerNameList must be at least 1 byte long. */
1887 || PACKET_remaining(&sni) == 0) {
1892 * Although the server_name extension was intended to be
1893 * extensible to new name types, RFC 4366 defined the
1894 * syntax inextensibility and OpenSSL 1.0.x parses it as
1896 * RFC 6066 corrected the mistake but adding new name types
1897 * is nevertheless no longer feasible, so act as if no other
1898 * SNI types can exist, to simplify parsing.
1900 * Also note that the RFC permits only one SNI value per type,
1901 * i.e., we can only have a single hostname.
1903 if (!PACKET_get_1(&sni, &servname_type)
1904 || servname_type != TLSEXT_NAMETYPE_host_name
1905 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1910 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1911 *al = TLS1_AD_UNRECOGNIZED_NAME;
1915 if (PACKET_contains_zero_byte(&hostname)) {
1916 *al = TLS1_AD_UNRECOGNIZED_NAME;
1920 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1921 *al = TLS1_AD_INTERNAL_ERROR;
1925 s->servername_done = 1;
1928 * TODO(openssl-team): if the SNI doesn't match, we MUST
1929 * fall back to a full handshake.
1931 s->servername_done = s->session->tlsext_hostname
1932 && PACKET_equal(&hostname, s->session->tlsext_hostname,
1933 strlen(s->session->tlsext_hostname));
1936 #ifndef OPENSSL_NO_SRP
1937 else if (type == TLSEXT_TYPE_srp) {
1940 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
1943 if (PACKET_contains_zero_byte(&srp_I))
1947 * TODO(openssl-team): currently, we re-authenticate the user
1948 * upon resumption. Instead, we MUST ignore the login.
1950 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
1951 *al = TLS1_AD_INTERNAL_ERROR;
1957 #ifndef OPENSSL_NO_EC
1958 else if (type == TLSEXT_TYPE_ec_point_formats) {
1959 PACKET ec_point_format_list;
1961 if (!PACKET_as_length_prefixed_1(&extension, &ec_point_format_list)
1962 || PACKET_remaining(&ec_point_format_list) == 0) {
1967 if (!PACKET_memdup(&ec_point_format_list,
1968 &s->session->tlsext_ecpointformatlist,
1970 session->tlsext_ecpointformatlist_length)) {
1971 *al = TLS1_AD_INTERNAL_ERROR;
1975 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1976 PACKET elliptic_curve_list;
1978 /* Each NamedCurve is 2 bytes and we must have at least 1. */
1979 if (!PACKET_as_length_prefixed_2(&extension, &elliptic_curve_list)
1980 || PACKET_remaining(&elliptic_curve_list) == 0
1981 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
1986 if (!PACKET_memdup(&elliptic_curve_list,
1987 &s->session->tlsext_ellipticcurvelist,
1989 session->tlsext_ellipticcurvelist_length)) {
1990 *al = TLS1_AD_INTERNAL_ERROR;
1995 #endif /* OPENSSL_NO_EC */
1996 else if (type == TLSEXT_TYPE_session_ticket) {
1997 if (s->tls_session_ticket_ext_cb &&
1998 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
1999 PACKET_remaining(&extension),
2000 s->tls_session_ticket_ext_cb_arg))
2002 *al = TLS1_AD_INTERNAL_ERROR;
2005 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2006 PACKET supported_sig_algs;
2008 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2009 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2010 || PACKET_remaining(&supported_sig_algs) == 0) {
2015 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2016 PACKET_remaining(&supported_sig_algs))) {
2020 } else if (type == TLSEXT_TYPE_status_request) {
2021 if (!PACKET_get_1(&extension,
2022 (unsigned int *)&s->tlsext_status_type)) {
2025 #ifndef OPENSSL_NO_OCSP
2026 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2027 const unsigned char *ext_data;
2028 PACKET responder_id_list, exts;
2029 if (!PACKET_get_length_prefixed_2
2030 (&extension, &responder_id_list))
2034 * We remove any OCSP_RESPIDs from a previous handshake
2035 * to prevent unbounded memory growth - CVE-2016-6304
2037 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2039 if (PACKET_remaining(&responder_id_list) > 0) {
2040 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2041 if (s->tlsext_ocsp_ids == NULL) {
2042 *al = SSL_AD_INTERNAL_ERROR;
2046 s->tlsext_ocsp_ids = NULL;
2049 while (PACKET_remaining(&responder_id_list) > 0) {
2051 PACKET responder_id;
2052 const unsigned char *id_data;
2054 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2056 || PACKET_remaining(&responder_id) == 0) {
2060 id_data = PACKET_data(&responder_id);
2061 id = d2i_OCSP_RESPID(NULL, &id_data,
2062 PACKET_remaining(&responder_id));
2066 if (id_data != PACKET_end(&responder_id)) {
2067 OCSP_RESPID_free(id);
2071 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2072 OCSP_RESPID_free(id);
2073 *al = SSL_AD_INTERNAL_ERROR;
2078 /* Read in request_extensions */
2079 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2082 if (PACKET_remaining(&exts) > 0) {
2083 ext_data = PACKET_data(&exts);
2084 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2085 X509_EXTENSION_free);
2086 s->tlsext_ocsp_exts =
2087 d2i_X509_EXTENSIONS(NULL, &ext_data,
2088 PACKET_remaining(&exts));
2089 if (s->tlsext_ocsp_exts == NULL
2090 || ext_data != PACKET_end(&exts)) {
2098 * We don't know what to do with any other type so ignore it.
2100 s->tlsext_status_type = -1;
2103 #ifndef OPENSSL_NO_HEARTBEATS
2104 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2105 unsigned int hbtype;
2107 if (!PACKET_get_1(&extension, &hbtype)
2108 || PACKET_remaining(&extension)) {
2109 *al = SSL_AD_DECODE_ERROR;
2113 case 0x01: /* Client allows us to send HB requests */
2114 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2116 case 0x02: /* Client doesn't accept HB requests */
2117 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2118 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2121 *al = SSL_AD_ILLEGAL_PARAMETER;
2126 #ifndef OPENSSL_NO_NEXTPROTONEG
2127 else if (type == TLSEXT_TYPE_next_proto_neg &&
2128 s->s3->tmp.finish_md_len == 0) {
2130 * We shouldn't accept this extension on a
2133 * s->new_session will be set on renegotiation, but we
2134 * probably shouldn't rely that it couldn't be set on
2135 * the initial renegotiation too in certain cases (when
2136 * there's some other reason to disallow resuming an
2137 * earlier session -- the current code won't be doing
2138 * anything like that, but this might change).
2140 * A valid sign that there's been a previous handshake
2141 * in this connection is if s->s3->tmp.finish_md_len >
2142 * 0. (We are talking about a check that will happen
2143 * in the Hello protocol round, well before a new
2144 * Finished message could have been computed.)
2146 s->s3->next_proto_neg_seen = 1;
2150 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2151 s->s3->tmp.finish_md_len == 0) {
2152 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2156 /* session ticket processed earlier */
2157 #ifndef OPENSSL_NO_SRTP
2158 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2159 && type == TLSEXT_TYPE_use_srtp) {
2160 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2164 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2165 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2167 * Note: extended master secret extension handled in
2168 * tls_check_serverhello_tlsext_early()
2172 * If this ClientHello extension was unhandled and this is a
2173 * nonresumed connection, check whether the extension is a custom
2174 * TLS Extension (has a custom_srv_ext_record), and if so call the
2175 * callback and record the extension number so that an appropriate
2176 * ServerHello may be later returned.
2179 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2180 PACKET_remaining(&extension), al) <= 0)
2185 if (PACKET_remaining(pkt) != 0) {
2187 * tls1_check_duplicate_extensions should ensure this never happens.
2189 *al = SSL_AD_INTERNAL_ERROR;
2195 /* Need RI if renegotiating */
2197 if (!renegotiate_seen && s->renegotiate &&
2198 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2199 *al = SSL_AD_HANDSHAKE_FAILURE;
2200 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2201 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2206 * This function currently has no state to clean up, so it returns directly.
2207 * If parsing fails at any point, the function returns early.
2208 * The SSL object may be left with partial data from extensions, but it must
2209 * then no longer be used, and clearing it up will free the leftovers.
2214 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2217 custom_ext_init(&s->cert->srv_ext);
2218 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2219 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2222 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2223 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2229 #ifndef OPENSSL_NO_NEXTPROTONEG
2231 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2232 * elements of zero length are allowed and the set of elements must exactly
2233 * fill the length of the block.
2235 static char ssl_next_proto_validate(PACKET *pkt)
2237 PACKET tmp_protocol;
2239 while (PACKET_remaining(pkt)) {
2240 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2241 || PACKET_remaining(&tmp_protocol) == 0)
2249 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2251 unsigned int length, type, size;
2252 int tlsext_servername = 0;
2253 int renegotiate_seen = 0;
2255 #ifndef OPENSSL_NO_NEXTPROTONEG
2256 s->s3->next_proto_neg_seen = 0;
2258 s->tlsext_ticket_expected = 0;
2260 OPENSSL_free(s->s3->alpn_selected);
2261 s->s3->alpn_selected = NULL;
2262 #ifndef OPENSSL_NO_HEARTBEATS
2263 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2264 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2267 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2269 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2271 if (!PACKET_get_net_2(pkt, &length))
2274 if (PACKET_remaining(pkt) != length) {
2275 *al = SSL_AD_DECODE_ERROR;
2279 if (!tls1_check_duplicate_extensions(pkt)) {
2280 *al = SSL_AD_DECODE_ERROR;
2284 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2285 const unsigned char *data;
2288 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2289 || !PACKET_peek_bytes(&spkt, &data, size))
2292 if (s->tlsext_debug_cb)
2293 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2295 if (type == TLSEXT_TYPE_renegotiate) {
2296 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2298 renegotiate_seen = 1;
2299 } else if (s->version == SSL3_VERSION) {
2300 } else if (type == TLSEXT_TYPE_server_name) {
2301 if (s->tlsext_hostname == NULL || size > 0) {
2302 *al = TLS1_AD_UNRECOGNIZED_NAME;
2305 tlsext_servername = 1;
2307 #ifndef OPENSSL_NO_EC
2308 else if (type == TLSEXT_TYPE_ec_point_formats) {
2309 unsigned int ecpointformatlist_length;
2310 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2311 || ecpointformatlist_length != size - 1) {
2312 *al = TLS1_AD_DECODE_ERROR;
2316 s->session->tlsext_ecpointformatlist_length = 0;
2317 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2318 if ((s->session->tlsext_ecpointformatlist =
2319 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2320 *al = TLS1_AD_INTERNAL_ERROR;
2323 s->session->tlsext_ecpointformatlist_length =
2324 ecpointformatlist_length;
2325 if (!PACKET_copy_bytes(&spkt,
2326 s->session->tlsext_ecpointformatlist,
2327 ecpointformatlist_length)) {
2328 *al = TLS1_AD_DECODE_ERROR;
2334 #endif /* OPENSSL_NO_EC */
2336 else if (type == TLSEXT_TYPE_session_ticket) {
2337 if (s->tls_session_ticket_ext_cb &&
2338 !s->tls_session_ticket_ext_cb(s, data, size,
2339 s->tls_session_ticket_ext_cb_arg))
2341 *al = TLS1_AD_INTERNAL_ERROR;
2344 if (!tls_use_ticket(s) || (size > 0)) {
2345 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2348 s->tlsext_ticket_expected = 1;
2349 } else if (type == TLSEXT_TYPE_status_request) {
2351 * MUST be empty and only sent if we've requested a status
2354 if ((s->tlsext_status_type == -1) || (size > 0)) {
2355 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2358 /* Set flag to expect CertificateStatus message */
2359 s->tlsext_status_expected = 1;
2361 #ifndef OPENSSL_NO_CT
2363 * Only take it if we asked for it - i.e if there is no CT validation
2364 * callback set, then a custom extension MAY be processing it, so we
2365 * need to let control continue to flow to that.
2367 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2368 s->ct_validation_callback != NULL) {
2369 /* Simply copy it off for later processing */
2370 if (s->tlsext_scts != NULL) {
2371 OPENSSL_free(s->tlsext_scts);
2372 s->tlsext_scts = NULL;
2374 s->tlsext_scts_len = size;
2376 s->tlsext_scts = OPENSSL_malloc(size);
2377 if (s->tlsext_scts == NULL) {
2378 *al = TLS1_AD_INTERNAL_ERROR;
2381 memcpy(s->tlsext_scts, data, size);
2385 #ifndef OPENSSL_NO_NEXTPROTONEG
2386 else if (type == TLSEXT_TYPE_next_proto_neg &&
2387 s->s3->tmp.finish_md_len == 0) {
2388 unsigned char *selected;
2389 unsigned char selected_len;
2390 /* We must have requested it. */
2391 if (s->ctx->next_proto_select_cb == NULL) {
2392 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2395 /* The data must be valid */
2396 if (!ssl_next_proto_validate(&spkt)) {
2397 *al = TLS1_AD_DECODE_ERROR;
2400 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2403 ctx->next_proto_select_cb_arg) !=
2404 SSL_TLSEXT_ERR_OK) {
2405 *al = TLS1_AD_INTERNAL_ERROR;
2408 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2409 if (s->next_proto_negotiated == NULL) {
2410 *al = TLS1_AD_INTERNAL_ERROR;
2413 memcpy(s->next_proto_negotiated, selected, selected_len);
2414 s->next_proto_negotiated_len = selected_len;
2415 s->s3->next_proto_neg_seen = 1;
2419 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2421 /* We must have requested it. */
2422 if (!s->s3->alpn_sent) {
2423 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2427 * The extension data consists of:
2428 * uint16 list_length
2429 * uint8 proto_length;
2430 * uint8 proto[proto_length];
2432 if (!PACKET_get_net_2(&spkt, &len)
2433 || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len)
2434 || PACKET_remaining(&spkt) != len) {
2435 *al = TLS1_AD_DECODE_ERROR;
2438 OPENSSL_free(s->s3->alpn_selected);
2439 s->s3->alpn_selected = OPENSSL_malloc(len);
2440 if (s->s3->alpn_selected == NULL) {
2441 *al = TLS1_AD_INTERNAL_ERROR;
2444 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2445 *al = TLS1_AD_DECODE_ERROR;
2448 s->s3->alpn_selected_len = len;
2450 #ifndef OPENSSL_NO_HEARTBEATS
2451 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2452 unsigned int hbtype;
2453 if (!PACKET_get_1(&spkt, &hbtype)) {
2454 *al = SSL_AD_DECODE_ERROR;
2458 case 0x01: /* Server allows us to send HB requests */
2459 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2461 case 0x02: /* Server doesn't accept HB requests */
2462 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2463 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2466 *al = SSL_AD_ILLEGAL_PARAMETER;
2471 #ifndef OPENSSL_NO_SRTP
2472 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2473 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2477 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2478 /* Ignore if inappropriate ciphersuite */
2479 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2480 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2481 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2482 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2483 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2485 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2488 * If this extension type was not otherwise handled, but matches a
2489 * custom_cli_ext_record, then send it to the c callback
2491 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2495 if (PACKET_remaining(pkt) != 0) {
2496 *al = SSL_AD_DECODE_ERROR;
2500 if (!s->hit && tlsext_servername == 1) {
2501 if (s->tlsext_hostname) {
2502 if (s->session->tlsext_hostname == NULL) {
2503 s->session->tlsext_hostname =
2504 OPENSSL_strdup(s->tlsext_hostname);
2505 if (!s->session->tlsext_hostname) {
2506 *al = SSL_AD_UNRECOGNIZED_NAME;
2510 *al = SSL_AD_DECODE_ERROR;
2519 * Determine if we need to see RI. Strictly speaking if we want to avoid
2520 * an attack we should *always* see RI even on initial server hello
2521 * because the client doesn't see any renegotiation during an attack.
2522 * However this would mean we could not connect to any server which
2523 * doesn't support RI so for the immediate future tolerate RI absence
2525 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2526 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2527 *al = SSL_AD_HANDSHAKE_FAILURE;
2528 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2529 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2535 * Check extended master secret extension is consistent with
2538 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2539 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2540 *al = SSL_AD_HANDSHAKE_FAILURE;
2541 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2549 int ssl_prepare_clienthello_tlsext(SSL *s)
2551 s->s3->alpn_sent = 0;
2555 int ssl_prepare_serverhello_tlsext(SSL *s)
2560 static int ssl_check_clienthello_tlsext_early(SSL *s)
2562 int ret = SSL_TLSEXT_ERR_NOACK;
2563 int al = SSL_AD_UNRECOGNIZED_NAME;
2565 #ifndef OPENSSL_NO_EC
2567 * The handling of the ECPointFormats extension is done elsewhere, namely
2568 * in ssl3_choose_cipher in s3_lib.c.
2571 * The handling of the EllipticCurves extension is done elsewhere, namely
2572 * in ssl3_choose_cipher in s3_lib.c.
2576 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2578 s->ctx->tlsext_servername_callback(s, &al,
2579 s->ctx->tlsext_servername_arg);
2580 else if (s->initial_ctx != NULL
2581 && s->initial_ctx->tlsext_servername_callback != 0)
2583 s->initial_ctx->tlsext_servername_callback(s, &al,
2585 initial_ctx->tlsext_servername_arg);
2588 case SSL_TLSEXT_ERR_ALERT_FATAL:
2589 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2592 case SSL_TLSEXT_ERR_ALERT_WARNING:
2593 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2596 case SSL_TLSEXT_ERR_NOACK:
2597 s->servername_done = 0;
2603 /* Initialise digests to default values */
2604 void ssl_set_default_md(SSL *s)
2606 const EVP_MD **pmd = s->s3->tmp.md;
2607 #ifndef OPENSSL_NO_DSA
2608 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2610 #ifndef OPENSSL_NO_RSA
2611 if (SSL_USE_SIGALGS(s))
2612 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2614 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2615 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2617 #ifndef OPENSSL_NO_EC
2618 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2620 #ifndef OPENSSL_NO_GOST
2621 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2622 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2623 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2627 int tls1_set_server_sigalgs(SSL *s)
2632 /* Clear any shared signature algorithms */
2633 OPENSSL_free(s->cert->shared_sigalgs);
2634 s->cert->shared_sigalgs = NULL;
2635 s->cert->shared_sigalgslen = 0;
2636 /* Clear certificate digests and validity flags */
2637 for (i = 0; i < SSL_PKEY_NUM; i++) {
2638 s->s3->tmp.md[i] = NULL;
2639 s->s3->tmp.valid_flags[i] = 0;
2642 /* If sigalgs received process it. */
2643 if (s->s3->tmp.peer_sigalgs) {
2644 if (!tls1_process_sigalgs(s)) {
2645 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2646 al = SSL_AD_INTERNAL_ERROR;
2649 /* Fatal error is no shared signature algorithms */
2650 if (!s->cert->shared_sigalgs) {
2651 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2652 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
2653 al = SSL_AD_ILLEGAL_PARAMETER;
2657 ssl_set_default_md(s);
2661 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2666 * Upon success, returns 1.
2667 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
2669 int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
2671 s->tlsext_status_expected = 0;
2674 * If status request then ask callback what to do. Note: this must be
2675 * called after servername callbacks in case the certificate has changed,
2676 * and must be called after the cipher has been chosen because this may
2677 * influence which certificate is sent
2679 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2681 CERT_PKEY *certpkey;
2682 certpkey = ssl_get_server_send_pkey(s);
2683 /* If no certificate can't return certificate status */
2684 if (certpkey != NULL) {
2686 * Set current certificate to one we will use so SSL_get_certificate
2687 * et al can pick it up.
2689 s->cert->key = certpkey;
2690 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2692 /* We don't want to send a status request response */
2693 case SSL_TLSEXT_ERR_NOACK:
2694 s->tlsext_status_expected = 0;
2696 /* status request response should be sent */
2697 case SSL_TLSEXT_ERR_OK:
2698 if (s->tlsext_ocsp_resp)
2699 s->tlsext_status_expected = 1;
2701 /* something bad happened */
2702 case SSL_TLSEXT_ERR_ALERT_FATAL:
2704 *al = SSL_AD_INTERNAL_ERROR;
2710 if (!tls1_alpn_handle_client_hello_late(s, al)) {
2717 int ssl_check_serverhello_tlsext(SSL *s)
2719 int ret = SSL_TLSEXT_ERR_NOACK;
2720 int al = SSL_AD_UNRECOGNIZED_NAME;
2722 #ifndef OPENSSL_NO_EC
2724 * If we are client and using an elliptic curve cryptography cipher
2725 * suite, then if server returns an EC point formats lists extension it
2726 * must contain uncompressed.
2728 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2729 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2730 if ((s->tlsext_ecpointformatlist != NULL)
2731 && (s->tlsext_ecpointformatlist_length > 0)
2732 && (s->session->tlsext_ecpointformatlist != NULL)
2733 && (s->session->tlsext_ecpointformatlist_length > 0)
2734 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2735 /* we are using an ECC cipher */
2737 unsigned char *list;
2738 int found_uncompressed = 0;
2739 list = s->session->tlsext_ecpointformatlist;
2740 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2741 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2742 found_uncompressed = 1;
2746 if (!found_uncompressed) {
2747 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2748 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2752 ret = SSL_TLSEXT_ERR_OK;
2753 #endif /* OPENSSL_NO_EC */
2755 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2757 s->ctx->tlsext_servername_callback(s, &al,
2758 s->ctx->tlsext_servername_arg);
2759 else if (s->initial_ctx != NULL
2760 && s->initial_ctx->tlsext_servername_callback != 0)
2762 s->initial_ctx->tlsext_servername_callback(s, &al,
2764 initial_ctx->tlsext_servername_arg);
2767 * Ensure we get sensible values passed to tlsext_status_cb in the event
2768 * that we don't receive a status message
2770 OPENSSL_free(s->tlsext_ocsp_resp);
2771 s->tlsext_ocsp_resp = NULL;
2772 s->tlsext_ocsp_resplen = -1;
2775 case SSL_TLSEXT_ERR_ALERT_FATAL:
2776 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2779 case SSL_TLSEXT_ERR_ALERT_WARNING:
2780 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2783 case SSL_TLSEXT_ERR_NOACK:
2784 s->servername_done = 0;
2790 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2793 if (s->version < SSL3_VERSION)
2795 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2796 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2800 if (ssl_check_serverhello_tlsext(s) <= 0) {
2801 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2808 * Since the server cache lookup is done early on in the processing of the
2809 * ClientHello and other operations depend on the result some extensions
2810 * need to be handled at the same time.
2812 * Two extensions are currently handled, session ticket and extended master
2815 * session_id: ClientHello session ID.
2816 * ext: ClientHello extensions (including length prefix)
2817 * ret: (output) on return, if a ticket was decrypted, then this is set to
2818 * point to the resulting session.
2820 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2821 * ciphersuite, in which case we have no use for session tickets and one will
2822 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2825 * -1: fatal error, either from parsing or decrypting the ticket.
2826 * 0: no ticket was found (or was ignored, based on settings).
2827 * 1: a zero length extension was found, indicating that the client supports
2828 * session tickets but doesn't currently have one to offer.
2829 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2830 * couldn't be decrypted because of a non-fatal error.
2831 * 3: a ticket was successfully decrypted and *ret was set.
2834 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2835 * a new session ticket to the client because the client indicated support
2836 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2837 * a session ticket or we couldn't use the one it gave us, or if
2838 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2839 * Otherwise, s->tlsext_ticket_expected is set to 0.
2841 * For extended master secret flag is set if the extension is present.
2844 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2845 const PACKET *session_id,
2849 PACKET local_ext = *ext;
2852 int have_ticket = 0;
2853 int use_ticket = tls_use_ticket(s);
2856 s->tlsext_ticket_expected = 0;
2857 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2860 * If tickets disabled behave as if no ticket present to permit stateful
2863 if ((s->version <= SSL3_VERSION))
2866 if (!PACKET_get_net_2(&local_ext, &i)) {
2870 while (PACKET_remaining(&local_ext) >= 4) {
2871 unsigned int type, size;
2873 if (!PACKET_get_net_2(&local_ext, &type)
2874 || !PACKET_get_net_2(&local_ext, &size)) {
2875 /* Shouldn't ever happen */
2879 if (PACKET_remaining(&local_ext) < size) {
2883 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
2885 const unsigned char *etick;
2887 /* Duplicate extension */
2888 if (have_ticket != 0) {
2896 * The client will accept a ticket but doesn't currently have
2899 s->tlsext_ticket_expected = 1;
2903 if (s->tls_session_secret_cb) {
2905 * Indicate that the ticket couldn't be decrypted rather than
2906 * generating the session from ticket now, trigger
2907 * abbreviated handshake based on external mechanism to
2908 * calculate the master secret later.
2913 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
2914 /* Shouldn't ever happen */
2918 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
2919 PACKET_remaining(session_id), ret);
2921 case 2: /* ticket couldn't be decrypted */
2922 s->tlsext_ticket_expected = 1;
2925 case 3: /* ticket was decrypted */
2928 case 4: /* ticket decrypted but need to renew */
2929 s->tlsext_ticket_expected = 1;
2932 default: /* fatal error */
2938 if (type == TLSEXT_TYPE_extended_master_secret)
2939 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2940 if (!PACKET_forward(&local_ext, size)) {
2946 if (have_ticket == 0)
2953 * tls_decrypt_ticket attempts to decrypt a session ticket.
2955 * etick: points to the body of the session ticket extension.
2956 * eticklen: the length of the session tickets extension.
2957 * sess_id: points at the session ID.
2958 * sesslen: the length of the session ID.
2959 * psess: (output) on return, if a ticket was decrypted, then this is set to
2960 * point to the resulting session.
2963 * -2: fatal error, malloc failure.
2964 * -1: fatal error, either from parsing or decrypting the ticket.
2965 * 2: the ticket couldn't be decrypted.
2966 * 3: a ticket was successfully decrypted and *psess was set.
2967 * 4: same as 3, but the ticket needs to be renewed.
2969 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2970 int eticklen, const unsigned char *sess_id,
2971 int sesslen, SSL_SESSION **psess)
2974 unsigned char *sdec;
2975 const unsigned char *p;
2976 int slen, mlen, renew_ticket = 0, ret = -1;
2977 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2978 HMAC_CTX *hctx = NULL;
2979 EVP_CIPHER_CTX *ctx;
2980 SSL_CTX *tctx = s->initial_ctx;
2982 /* Initialize session ticket encryption and HMAC contexts */
2983 hctx = HMAC_CTX_new();
2986 ctx = EVP_CIPHER_CTX_new();
2991 if (tctx->tlsext_ticket_key_cb) {
2992 unsigned char *nctick = (unsigned char *)etick;
2993 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3004 /* Check key name matches */
3005 if (memcmp(etick, tctx->tlsext_tick_key_name,
3006 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3010 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3011 sizeof(tctx->tlsext_tick_hmac_key),
3012 EVP_sha256(), NULL) <= 0
3013 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3014 tctx->tlsext_tick_aes_key,
3015 etick + sizeof(tctx->tlsext_tick_key_name)) <=
3021 * Attempt to process session ticket, first conduct sanity and integrity
3024 mlen = HMAC_size(hctx);
3028 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3030 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
3035 /* Check HMAC of encrypted ticket */
3036 if (HMAC_Update(hctx, etick, eticklen) <= 0
3037 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3040 HMAC_CTX_free(hctx);
3041 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3042 EVP_CIPHER_CTX_free(ctx);
3045 /* Attempt to decrypt session data */
3046 /* Move p after IV to start of encrypted ticket, update length */
3047 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3048 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3049 sdec = OPENSSL_malloc(eticklen);
3050 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3051 EVP_CIPHER_CTX_free(ctx);
3055 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3056 EVP_CIPHER_CTX_free(ctx);
3061 EVP_CIPHER_CTX_free(ctx);
3065 sess = d2i_SSL_SESSION(NULL, &p, slen);
3069 * The session ID, if non-empty, is used by some clients to detect
3070 * that the ticket has been accepted. So we copy it to the session
3071 * structure. If it is empty set length to zero as required by
3075 memcpy(sess->session_id, sess_id, sesslen);
3076 sess->session_id_length = sesslen;
3085 * For session parse failure, indicate that we need to send a new ticket.
3089 EVP_CIPHER_CTX_free(ctx);
3090 HMAC_CTX_free(hctx);
3094 /* Tables to translate from NIDs to TLS v1.2 ids */
3101 static const tls12_lookup tls12_md[] = {
3102 {NID_md5, TLSEXT_hash_md5},
3103 {NID_sha1, TLSEXT_hash_sha1},
3104 {NID_sha224, TLSEXT_hash_sha224},
3105 {NID_sha256, TLSEXT_hash_sha256},
3106 {NID_sha384, TLSEXT_hash_sha384},
3107 {NID_sha512, TLSEXT_hash_sha512},
3108 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3109 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3110 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3113 static const tls12_lookup tls12_sig[] = {
3114 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3115 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3116 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3117 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3118 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3119 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3122 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3125 for (i = 0; i < tlen; i++) {
3126 if (table[i].nid == nid)
3132 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3135 for (i = 0; i < tlen; i++) {
3136 if ((table[i].id) == id)
3137 return table[i].nid;
3142 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3147 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3150 sig_id = tls12_get_sigid(pk);
3153 p[0] = (unsigned char)md_id;
3154 p[1] = (unsigned char)sig_id;
3158 int tls12_get_sigid(const EVP_PKEY *pk)
3160 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3167 unsigned char tlsext_hash;
3170 static const tls12_hash_info tls12_md_info[] = {
3171 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3172 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3173 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3174 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3175 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3176 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3177 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3178 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX,
3179 TLSEXT_hash_gostr34112012_256},
3180 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX,
3181 TLSEXT_hash_gostr34112012_512},
3184 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3190 for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) {
3191 if (tls12_md_info[i].tlsext_hash == hash_alg)
3192 return tls12_md_info + i;
3198 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3200 const tls12_hash_info *inf;
3201 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3203 inf = tls12_get_hash_info(hash_alg);
3206 return ssl_md(inf->md_idx);
3209 static int tls12_get_pkey_idx(unsigned char sig_alg)
3212 #ifndef OPENSSL_NO_RSA
3213 case TLSEXT_signature_rsa:
3214 return SSL_PKEY_RSA_SIGN;
3216 #ifndef OPENSSL_NO_DSA
3217 case TLSEXT_signature_dsa:
3218 return SSL_PKEY_DSA_SIGN;
3220 #ifndef OPENSSL_NO_EC
3221 case TLSEXT_signature_ecdsa:
3222 return SSL_PKEY_ECC;
3224 #ifndef OPENSSL_NO_GOST
3225 case TLSEXT_signature_gostr34102001:
3226 return SSL_PKEY_GOST01;
3228 case TLSEXT_signature_gostr34102012_256:
3229 return SSL_PKEY_GOST12_256;
3231 case TLSEXT_signature_gostr34102012_512:
3232 return SSL_PKEY_GOST12_512;
3238 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3239 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3240 int *psignhash_nid, const unsigned char *data)
3242 int sign_nid = NID_undef, hash_nid = NID_undef;
3243 if (!phash_nid && !psign_nid && !psignhash_nid)
3245 if (phash_nid || psignhash_nid) {
3246 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3248 *phash_nid = hash_nid;
3250 if (psign_nid || psignhash_nid) {
3251 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3253 *psign_nid = sign_nid;
3255 if (psignhash_nid) {
3256 if (sign_nid == NID_undef || hash_nid == NID_undef
3257 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0)
3258 *psignhash_nid = NID_undef;
3262 /* Check to see if a signature algorithm is allowed */
3263 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3265 /* See if we have an entry in the hash table and it is enabled */
3266 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3267 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3269 /* See if public key algorithm allowed */
3270 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3272 /* Finally see if security callback allows it */
3273 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3277 * Get a mask of disabled public key algorithms based on supported signature
3278 * algorithms. For example if no signature algorithm supports RSA then RSA is
3282 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3284 const unsigned char *sigalgs;
3285 size_t i, sigalgslen;
3286 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3288 * Now go through all signature algorithms seeing if we support any for
3289 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3290 * down calls to security callback only check if we have to.
3292 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3293 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3294 switch (sigalgs[1]) {
3295 #ifndef OPENSSL_NO_RSA
3296 case TLSEXT_signature_rsa:
3297 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3301 #ifndef OPENSSL_NO_DSA
3302 case TLSEXT_signature_dsa:
3303 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3307 #ifndef OPENSSL_NO_EC
3308 case TLSEXT_signature_ecdsa:
3309 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3316 *pmask_a |= SSL_aRSA;
3318 *pmask_a |= SSL_aDSS;
3320 *pmask_a |= SSL_aECDSA;
3323 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3324 const unsigned char *psig, size_t psiglen)
3326 unsigned char *tmpout = out;
3328 for (i = 0; i < psiglen; i += 2, psig += 2) {
3329 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3330 *tmpout++ = psig[0];
3331 *tmpout++ = psig[1];
3334 return tmpout - out;
3337 /* Given preference and allowed sigalgs set shared sigalgs */
3338 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3339 const unsigned char *pref, size_t preflen,
3340 const unsigned char *allow, size_t allowlen)
3342 const unsigned char *ptmp, *atmp;
3343 size_t i, j, nmatch = 0;
3344 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3345 /* Skip disabled hashes or signature algorithms */
3346 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3348 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3349 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3352 shsig->rhash = ptmp[0];
3353 shsig->rsign = ptmp[1];
3354 tls1_lookup_sigalg(&shsig->hash_nid,
3356 &shsig->signandhash_nid, ptmp);
3366 /* Set shared signature algorithms for SSL structures */
3367 static int tls1_set_shared_sigalgs(SSL *s)
3369 const unsigned char *pref, *allow, *conf;
3370 size_t preflen, allowlen, conflen;
3372 TLS_SIGALGS *salgs = NULL;
3374 unsigned int is_suiteb = tls1_suiteb(s);
3376 OPENSSL_free(c->shared_sigalgs);
3377 c->shared_sigalgs = NULL;
3378 c->shared_sigalgslen = 0;
3379 /* If client use client signature algorithms if not NULL */
3380 if (!s->server && c->client_sigalgs && !is_suiteb) {
3381 conf = c->client_sigalgs;
3382 conflen = c->client_sigalgslen;
3383 } else if (c->conf_sigalgs && !is_suiteb) {
3384 conf = c->conf_sigalgs;
3385 conflen = c->conf_sigalgslen;
3387 conflen = tls12_get_psigalgs(s, &conf);
3388 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3391 allow = s->s3->tmp.peer_sigalgs;
3392 allowlen = s->s3->tmp.peer_sigalgslen;
3396 pref = s->s3->tmp.peer_sigalgs;
3397 preflen = s->s3->tmp.peer_sigalgslen;
3399 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3401 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3404 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3408 c->shared_sigalgs = salgs;
3409 c->shared_sigalgslen = nmatch;
3413 /* Set preferred digest for each key type */
3415 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3418 /* Extension ignored for inappropriate versions */
3419 if (!SSL_USE_SIGALGS(s))
3421 /* Should never happen */
3425 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3426 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3427 if (s->s3->tmp.peer_sigalgs == NULL)
3429 s->s3->tmp.peer_sigalgslen = dsize;
3430 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3434 int tls1_process_sigalgs(SSL *s)
3439 const EVP_MD **pmd = s->s3->tmp.md;
3440 uint32_t *pvalid = s->s3->tmp.valid_flags;
3442 TLS_SIGALGS *sigptr;
3443 if (!tls1_set_shared_sigalgs(s))
3446 for (i = 0, sigptr = c->shared_sigalgs;
3447 i < c->shared_sigalgslen; i++, sigptr++) {
3448 idx = tls12_get_pkey_idx(sigptr->rsign);
3449 if (idx > 0 && pmd[idx] == NULL) {
3450 md = tls12_get_hash(sigptr->rhash);
3452 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3453 if (idx == SSL_PKEY_RSA_SIGN) {
3454 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3455 pmd[SSL_PKEY_RSA_ENC] = md;
3461 * In strict mode leave unset digests as NULL to indicate we can't use
3462 * the certificate for signing.
3464 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3466 * Set any remaining keys to default values. NOTE: if alg is not
3467 * supported it stays as NULL.
3469 #ifndef OPENSSL_NO_DSA
3470 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3471 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3473 #ifndef OPENSSL_NO_RSA
3474 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3475 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3476 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3479 #ifndef OPENSSL_NO_EC
3480 if (pmd[SSL_PKEY_ECC] == NULL)
3481 pmd[SSL_PKEY_ECC] = EVP_sha1();
3483 #ifndef OPENSSL_NO_GOST
3484 if (pmd[SSL_PKEY_GOST01] == NULL)
3485 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3486 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3487 pmd[SSL_PKEY_GOST12_256] =
3488 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3489 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3490 pmd[SSL_PKEY_GOST12_512] =
3491 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3497 int SSL_get_sigalgs(SSL *s, int idx,
3498 int *psign, int *phash, int *psignhash,
3499 unsigned char *rsig, unsigned char *rhash)
3501 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3506 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3513 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3515 return s->s3->tmp.peer_sigalgslen / 2;
3518 int SSL_get_shared_sigalgs(SSL *s, int idx,
3519 int *psign, int *phash, int *psignhash,
3520 unsigned char *rsig, unsigned char *rhash)
3522 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3523 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3527 *phash = shsigalgs->hash_nid;
3529 *psign = shsigalgs->sign_nid;
3531 *psignhash = shsigalgs->signandhash_nid;
3533 *rsig = shsigalgs->rsign;
3535 *rhash = shsigalgs->rhash;
3536 return s->cert->shared_sigalgslen;
3539 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3543 int sigalgs[MAX_SIGALGLEN];
3546 static void get_sigorhash(int *psig, int *phash, const char *str)
3548 if (strcmp(str, "RSA") == 0) {
3549 *psig = EVP_PKEY_RSA;
3550 } else if (strcmp(str, "DSA") == 0) {
3551 *psig = EVP_PKEY_DSA;
3552 } else if (strcmp(str, "ECDSA") == 0) {
3553 *psig = EVP_PKEY_EC;
3555 *phash = OBJ_sn2nid(str);
3556 if (*phash == NID_undef)
3557 *phash = OBJ_ln2nid(str);
3561 static int sig_cb(const char *elem, int len, void *arg)
3563 sig_cb_st *sarg = arg;
3566 int sig_alg = NID_undef, hash_alg = NID_undef;
3569 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3571 if (len > (int)(sizeof(etmp) - 1))
3573 memcpy(etmp, elem, len);
3575 p = strchr(etmp, '+');
3583 get_sigorhash(&sig_alg, &hash_alg, etmp);
3584 get_sigorhash(&sig_alg, &hash_alg, p);
3586 if (sig_alg == NID_undef || hash_alg == NID_undef)
3589 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3590 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3593 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3594 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3599 * Set supported signature algorithms based on a colon separated list of the
3600 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3602 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3606 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3610 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3613 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3615 unsigned char *sigalgs, *sptr;
3620 sigalgs = OPENSSL_malloc(salglen);
3621 if (sigalgs == NULL)
3623 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3624 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3625 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3627 if (rhash == -1 || rsign == -1)
3634 OPENSSL_free(c->client_sigalgs);
3635 c->client_sigalgs = sigalgs;
3636 c->client_sigalgslen = salglen;
3638 OPENSSL_free(c->conf_sigalgs);
3639 c->conf_sigalgs = sigalgs;
3640 c->conf_sigalgslen = salglen;
3646 OPENSSL_free(sigalgs);
3650 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3654 if (default_nid == -1)
3656 sig_nid = X509_get_signature_nid(x);
3658 return sig_nid == default_nid ? 1 : 0;
3659 for (i = 0; i < c->shared_sigalgslen; i++)
3660 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3665 /* Check to see if a certificate issuer name matches list of CA names */
3666 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3670 nm = X509_get_issuer_name(x);
3671 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3672 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3679 * Check certificate chain is consistent with TLS extensions and is usable by
3680 * server. This servers two purposes: it allows users to check chains before
3681 * passing them to the server and it allows the server to check chains before
3682 * attempting to use them.
3685 /* Flags which need to be set for a certificate when stict mode not set */
3687 #define CERT_PKEY_VALID_FLAGS \
3688 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3689 /* Strict mode flags */
3690 #define CERT_PKEY_STRICT_FLAGS \
3691 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3692 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3694 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3699 int check_flags = 0, strict_mode;
3700 CERT_PKEY *cpk = NULL;
3703 unsigned int suiteb_flags = tls1_suiteb(s);
3704 /* idx == -1 means checking server chains */
3706 /* idx == -2 means checking client certificate chains */
3709 idx = cpk - c->pkeys;
3711 cpk = c->pkeys + idx;
3712 pvalid = s->s3->tmp.valid_flags + idx;
3714 pk = cpk->privatekey;
3716 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3717 /* If no cert or key, forget it */
3723 idx = ssl_cert_type(x, pk);
3726 pvalid = s->s3->tmp.valid_flags + idx;
3728 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3729 check_flags = CERT_PKEY_STRICT_FLAGS;
3731 check_flags = CERT_PKEY_VALID_FLAGS;
3738 check_flags |= CERT_PKEY_SUITEB;
3739 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3740 if (ok == X509_V_OK)
3741 rv |= CERT_PKEY_SUITEB;
3742 else if (!check_flags)
3747 * Check all signature algorithms are consistent with signature
3748 * algorithms extension if TLS 1.2 or later and strict mode.
3750 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3752 unsigned char rsign = 0;
3753 if (s->s3->tmp.peer_sigalgs)
3755 /* If no sigalgs extension use defaults from RFC5246 */
3758 case SSL_PKEY_RSA_ENC:
3759 case SSL_PKEY_RSA_SIGN:
3760 rsign = TLSEXT_signature_rsa;
3761 default_nid = NID_sha1WithRSAEncryption;
3764 case SSL_PKEY_DSA_SIGN:
3765 rsign = TLSEXT_signature_dsa;
3766 default_nid = NID_dsaWithSHA1;
3770 rsign = TLSEXT_signature_ecdsa;
3771 default_nid = NID_ecdsa_with_SHA1;
3774 case SSL_PKEY_GOST01:
3775 rsign = TLSEXT_signature_gostr34102001;
3776 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3779 case SSL_PKEY_GOST12_256:
3780 rsign = TLSEXT_signature_gostr34102012_256;
3781 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3784 case SSL_PKEY_GOST12_512:
3785 rsign = TLSEXT_signature_gostr34102012_512;
3786 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3795 * If peer sent no signature algorithms extension and we have set
3796 * preferred signature algorithms check we support sha1.
3798 if (default_nid > 0 && c->conf_sigalgs) {
3800 const unsigned char *p = c->conf_sigalgs;
3801 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3802 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3805 if (j == c->conf_sigalgslen) {
3812 /* Check signature algorithm of each cert in chain */
3813 if (!tls1_check_sig_alg(c, x, default_nid)) {
3817 rv |= CERT_PKEY_EE_SIGNATURE;
3818 rv |= CERT_PKEY_CA_SIGNATURE;
3819 for (i = 0; i < sk_X509_num(chain); i++) {
3820 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3822 rv &= ~CERT_PKEY_CA_SIGNATURE;
3829 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3830 else if (check_flags)
3831 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3833 /* Check cert parameters are consistent */
3834 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3835 rv |= CERT_PKEY_EE_PARAM;
3836 else if (!check_flags)
3839 rv |= CERT_PKEY_CA_PARAM;
3840 /* In strict mode check rest of chain too */
3841 else if (strict_mode) {
3842 rv |= CERT_PKEY_CA_PARAM;
3843 for (i = 0; i < sk_X509_num(chain); i++) {
3844 X509 *ca = sk_X509_value(chain, i);
3845 if (!tls1_check_cert_param(s, ca, 0)) {
3847 rv &= ~CERT_PKEY_CA_PARAM;
3854 if (!s->server && strict_mode) {
3855 STACK_OF(X509_NAME) *ca_dn;
3857 switch (EVP_PKEY_id(pk)) {
3859 check_type = TLS_CT_RSA_SIGN;
3862 check_type = TLS_CT_DSS_SIGN;
3865 check_type = TLS_CT_ECDSA_SIGN;
3869 const unsigned char *ctypes;
3873 ctypelen = (int)c->ctype_num;
3875 ctypes = (unsigned char *)s->s3->tmp.ctype;
3876 ctypelen = s->s3->tmp.ctype_num;
3878 for (i = 0; i < ctypelen; i++) {
3879 if (ctypes[i] == check_type) {
3880 rv |= CERT_PKEY_CERT_TYPE;
3884 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3887 rv |= CERT_PKEY_CERT_TYPE;
3889 ca_dn = s->s3->tmp.ca_names;
3891 if (!sk_X509_NAME_num(ca_dn))
3892 rv |= CERT_PKEY_ISSUER_NAME;
3894 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3895 if (ssl_check_ca_name(ca_dn, x))
3896 rv |= CERT_PKEY_ISSUER_NAME;
3898 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3899 for (i = 0; i < sk_X509_num(chain); i++) {
3900 X509 *xtmp = sk_X509_value(chain, i);
3901 if (ssl_check_ca_name(ca_dn, xtmp)) {
3902 rv |= CERT_PKEY_ISSUER_NAME;
3907 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3910 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3912 if (!check_flags || (rv & check_flags) == check_flags)
3913 rv |= CERT_PKEY_VALID;
3917 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3918 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3919 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3920 else if (s->s3->tmp.md[idx] != NULL)
3921 rv |= CERT_PKEY_SIGN;
3923 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3926 * When checking a CERT_PKEY structure all flags are irrelevant if the
3930 if (rv & CERT_PKEY_VALID)
3933 /* Preserve explicit sign flag, clear rest */
3934 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3941 /* Set validity of certificates in an SSL structure */
3942 void tls1_set_cert_validity(SSL *s)
3944 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3945 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3946 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3947 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3948 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
3949 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
3950 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
3953 /* User level utiity function to check a chain is suitable */
3954 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3956 return tls1_check_chain(s, x, pk, chain, -1);
3959 #ifndef OPENSSL_NO_DH
3960 DH *ssl_get_auto_dh(SSL *s)
3962 int dh_secbits = 80;
3963 if (s->cert->dh_tmp_auto == 2)
3964 return DH_get_1024_160();
3965 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
3966 if (s->s3->tmp.new_cipher->strength_bits == 256)
3971 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
3972 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
3975 if (dh_secbits >= 128) {
3983 if (dh_secbits >= 192)
3984 p = BN_get_rfc3526_prime_8192(NULL);
3986 p = BN_get_rfc3526_prime_3072(NULL);
3987 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
3995 if (dh_secbits >= 112)
3996 return DH_get_2048_224();
3997 return DH_get_1024_160();
4001 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4004 EVP_PKEY *pkey = X509_get0_pubkey(x);
4007 * If no parameters this will return -1 and fail using the default
4008 * security callback for any non-zero security level. This will
4009 * reject keys which omit parameters but this only affects DSA and
4010 * omission of parameters is never (?) done in practice.
4012 secbits = EVP_PKEY_security_bits(pkey);
4015 return ssl_security(s, op, secbits, 0, x);
4017 return ssl_ctx_security(ctx, op, secbits, 0, x);
4020 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4022 /* Lookup signature algorithm digest */
4023 int secbits = -1, md_nid = NID_undef, sig_nid;
4024 /* Don't check signature if self signed */
4025 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4027 sig_nid = X509_get_signature_nid(x);
4028 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4030 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4031 secbits = EVP_MD_size(md) * 4;
4034 return ssl_security(s, op, secbits, md_nid, x);
4036 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4039 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4042 vfy = SSL_SECOP_PEER;
4044 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4045 return SSL_R_EE_KEY_TOO_SMALL;
4047 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4048 return SSL_R_CA_KEY_TOO_SMALL;
4050 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4051 return SSL_R_CA_MD_TOO_WEAK;
4056 * Check security of a chain, if sk includes the end entity certificate then
4057 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4058 * one to the peer. Return values: 1 if ok otherwise error code to use
4061 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4063 int rv, start_idx, i;
4065 x = sk_X509_value(sk, 0);
4070 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4074 for (i = start_idx; i < sk_X509_num(sk); i++) {
4075 x = sk_X509_value(sk, i);
4076 rv = ssl_security_cert(s, NULL, x, vfy, 0);