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>
24 #define CHECKLEN(curr, val, limit) \
25 (((curr) >= (limit)) || (size_t)((limit) - (curr)) < (size_t)(val))
27 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
28 const unsigned char *sess_id, int sesslen,
30 static int ssl_check_clienthello_tlsext_early(SSL *s);
31 static int ssl_check_serverhello_tlsext(SSL *s);
33 SSL3_ENC_METHOD const TLSv1_enc_data = {
37 tls1_generate_master_secret,
38 tls1_change_cipher_state,
39 tls1_final_finish_mac,
40 TLS1_FINISH_MAC_LENGTH,
41 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
42 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
44 tls1_export_keying_material,
46 SSL3_HM_HEADER_LENGTH,
47 ssl3_set_handshake_header,
51 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
55 tls1_generate_master_secret,
56 tls1_change_cipher_state,
57 tls1_final_finish_mac,
58 TLS1_FINISH_MAC_LENGTH,
59 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
60 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
62 tls1_export_keying_material,
63 SSL_ENC_FLAG_EXPLICIT_IV,
64 SSL3_HM_HEADER_LENGTH,
65 ssl3_set_handshake_header,
69 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
73 tls1_generate_master_secret,
74 tls1_change_cipher_state,
75 tls1_final_finish_mac,
76 TLS1_FINISH_MAC_LENGTH,
77 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
78 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
80 tls1_export_keying_material,
81 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
82 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
83 SSL3_HM_HEADER_LENGTH,
84 ssl3_set_handshake_header,
88 long tls1_default_timeout(void)
91 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
92 * http, the cache would over fill
101 s->method->ssl_clear(s);
105 void tls1_free(SSL *s)
107 OPENSSL_free(s->tlsext_session_ticket);
111 void tls1_clear(SSL *s)
114 if (s->method->version == TLS_ANY_VERSION)
115 s->version = TLS_MAX_VERSION;
117 s->version = s->method->version;
120 #ifndef OPENSSL_NO_EC
123 int nid; /* Curve NID */
124 int secbits; /* Bits of security (from SP800-57) */
125 unsigned int flags; /* Flags: currently just field type */
129 * Table of curve information.
130 * Do not delete entries or reorder this array! It is used as a lookup
131 * table: the index of each entry is one less than the TLS curve id.
133 static const tls_curve_info nid_list[] = {
134 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
135 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
136 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
137 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
138 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
139 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
140 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
141 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
142 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
143 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
144 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
145 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
146 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
147 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
148 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
149 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
150 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
151 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
152 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
153 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
154 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
155 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
156 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
157 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
158 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
159 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
160 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
161 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
162 {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
165 static const unsigned char ecformats_default[] = {
166 TLSEXT_ECPOINTFORMAT_uncompressed,
167 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
168 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
171 /* The default curves */
172 static const unsigned char eccurves_default[] = {
173 0, 29, /* X25519 (29) */
174 0, 23, /* secp256r1 (23) */
175 0, 25, /* secp521r1 (25) */
176 0, 24, /* secp384r1 (24) */
179 static const unsigned char eccurves_all[] = {
180 0, 29, /* X25519 (29) */
181 0, 23, /* secp256r1 (23) */
182 0, 25, /* secp521r1 (25) */
183 0, 24, /* secp384r1 (24) */
184 0, 26, /* brainpoolP256r1 (26) */
185 0, 27, /* brainpoolP384r1 (27) */
186 0, 28, /* brainpool512r1 (28) */
189 * Remaining curves disabled by default but still permitted if set
190 * via an explicit callback or parameters.
192 0, 22, /* secp256k1 (22) */
193 0, 14, /* sect571r1 (14) */
194 0, 13, /* sect571k1 (13) */
195 0, 11, /* sect409k1 (11) */
196 0, 12, /* sect409r1 (12) */
197 0, 9, /* sect283k1 (9) */
198 0, 10, /* sect283r1 (10) */
199 0, 20, /* secp224k1 (20) */
200 0, 21, /* secp224r1 (21) */
201 0, 18, /* secp192k1 (18) */
202 0, 19, /* secp192r1 (19) */
203 0, 15, /* secp160k1 (15) */
204 0, 16, /* secp160r1 (16) */
205 0, 17, /* secp160r2 (17) */
206 0, 8, /* sect239k1 (8) */
207 0, 6, /* sect233k1 (6) */
208 0, 7, /* sect233r1 (7) */
209 0, 4, /* sect193r1 (4) */
210 0, 5, /* sect193r2 (5) */
211 0, 1, /* sect163k1 (1) */
212 0, 2, /* sect163r1 (2) */
213 0, 3, /* sect163r2 (3) */
216 static const unsigned char suiteb_curves[] = {
217 0, TLSEXT_curve_P_256,
218 0, TLSEXT_curve_P_384
221 int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags)
223 const tls_curve_info *cinfo;
224 /* ECC curves from RFC 4492 and RFC 7027 */
225 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
227 cinfo = nid_list + curve_id - 1;
229 *pflags = cinfo->flags;
233 int tls1_ec_nid2curve_id(int nid)
236 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
237 if (nid_list[i].nid == nid)
244 * Get curves list, if "sess" is set return client curves otherwise
246 * Sets |num_curves| to the number of curves in the list, i.e.,
247 * the length of |pcurves| is 2 * num_curves.
248 * Returns 1 on success and 0 if the client curves list has invalid format.
249 * The latter indicates an internal error: we should not be accepting such
250 * lists in the first place.
251 * TODO(emilia): we should really be storing the curves list in explicitly
252 * parsed form instead. (However, this would affect binary compatibility
253 * so cannot happen in the 1.0.x series.)
255 static int tls1_get_curvelist(SSL *s, int sess,
256 const unsigned char **pcurves, size_t *num_curves)
258 size_t pcurveslen = 0;
260 *pcurves = s->session->tlsext_ellipticcurvelist;
261 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
263 /* For Suite B mode only include P-256, P-384 */
264 switch (tls1_suiteb(s)) {
265 case SSL_CERT_FLAG_SUITEB_128_LOS:
266 *pcurves = suiteb_curves;
267 pcurveslen = sizeof(suiteb_curves);
270 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
271 *pcurves = suiteb_curves;
275 case SSL_CERT_FLAG_SUITEB_192_LOS:
276 *pcurves = suiteb_curves + 2;
280 *pcurves = s->tlsext_ellipticcurvelist;
281 pcurveslen = s->tlsext_ellipticcurvelist_length;
284 *pcurves = eccurves_default;
285 pcurveslen = sizeof(eccurves_default);
289 /* We do not allow odd length arrays to enter the system. */
290 if (pcurveslen & 1) {
291 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
295 *num_curves = pcurveslen / 2;
300 /* See if curve is allowed by security callback */
301 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
303 const tls_curve_info *cinfo;
306 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
308 cinfo = &nid_list[curve[1] - 1];
309 # ifdef OPENSSL_NO_EC2M
310 if (cinfo->flags & TLS_CURVE_CHAR2)
313 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
316 /* Check a curve is one of our preferences */
317 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
319 const unsigned char *curves;
320 size_t num_curves, i;
321 unsigned int suiteb_flags = tls1_suiteb(s);
322 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
324 /* Check curve matches Suite B preferences */
326 unsigned long cid = s->s3->tmp.new_cipher->id;
329 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
330 if (p[2] != TLSEXT_curve_P_256)
332 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
333 if (p[2] != TLSEXT_curve_P_384)
335 } else /* Should never happen */
338 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
340 for (i = 0; i < num_curves; i++, curves += 2) {
341 if (p[1] == curves[0] && p[2] == curves[1])
342 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
348 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
349 * if there is no match.
350 * For nmatch == -1, return number of matches
351 * For nmatch == -2, return the NID of the curve to use for
352 * an EC tmp key, or NID_undef if there is no match.
354 int tls1_shared_curve(SSL *s, int nmatch)
356 const unsigned char *pref, *supp;
357 size_t num_pref, num_supp, i, j;
359 /* Can't do anything on client side */
363 if (tls1_suiteb(s)) {
365 * For Suite B ciphersuite determines curve: we already know
366 * these are acceptable due to previous checks.
368 unsigned long cid = s->s3->tmp.new_cipher->id;
369 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
370 return NID_X9_62_prime256v1; /* P-256 */
371 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
372 return NID_secp384r1; /* P-384 */
373 /* Should never happen */
376 /* If not Suite B just return first preference shared curve */
380 * Avoid truncation. tls1_get_curvelist takes an int
381 * but s->options is a long...
383 if (!tls1_get_curvelist
384 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
386 /* In practice, NID_undef == 0 but let's be precise. */
387 return nmatch == -1 ? 0 : NID_undef;
388 if (!tls1_get_curvelist
389 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, &num_pref))
390 return nmatch == -1 ? 0 : NID_undef;
393 * If the client didn't send the elliptic_curves extension all of them
396 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
398 num_supp = sizeof(eccurves_all) / 2;
399 } else if (num_pref == 0 &&
400 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
402 num_pref = sizeof(eccurves_all) / 2;
406 for (i = 0; i < num_pref; i++, pref += 2) {
407 const unsigned char *tsupp = supp;
408 for (j = 0; j < num_supp; j++, tsupp += 2) {
409 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
410 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
413 int id = (pref[0] << 8) | pref[1];
414 return tls1_ec_curve_id2nid(id, NULL);
422 /* Out of range (nmatch > k). */
426 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
427 int *curves, size_t ncurves)
429 unsigned char *clist, *p;
432 * Bitmap of curves included to detect duplicates: only works while curve
435 unsigned long dup_list = 0;
436 clist = OPENSSL_malloc(ncurves * 2);
439 for (i = 0, p = clist; i < ncurves; i++) {
440 unsigned long idmask;
442 id = tls1_ec_nid2curve_id(curves[i]);
444 if (!id || (dup_list & idmask)) {
453 *pextlen = ncurves * 2;
457 # define MAX_CURVELIST 28
461 int nid_arr[MAX_CURVELIST];
464 static int nid_cb(const char *elem, int len, void *arg)
466 nid_cb_st *narg = arg;
472 if (narg->nidcnt == MAX_CURVELIST)
474 if (len > (int)(sizeof(etmp) - 1))
476 memcpy(etmp, elem, len);
478 nid = EC_curve_nist2nid(etmp);
479 if (nid == NID_undef)
480 nid = OBJ_sn2nid(etmp);
481 if (nid == NID_undef)
482 nid = OBJ_ln2nid(etmp);
483 if (nid == NID_undef)
485 for (i = 0; i < narg->nidcnt; i++)
486 if (narg->nid_arr[i] == nid)
488 narg->nid_arr[narg->nidcnt++] = nid;
492 /* Set curves based on a colon separate list */
493 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen, const char *str)
497 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
501 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
504 /* For an EC key set TLS id and required compression based on parameters */
505 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
512 /* Determine if it is a prime field */
513 grp = EC_KEY_get0_group(ec);
516 /* Determine curve ID */
517 id = EC_GROUP_get_curve_name(grp);
518 id = tls1_ec_nid2curve_id(id);
519 /* If no id return error: we don't support arbitrary explicit curves */
523 curve_id[1] = (unsigned char)id;
525 if (EC_KEY_get0_public_key(ec) == NULL)
527 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
528 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
530 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
531 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
533 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
539 /* Check an EC key is compatible with extensions */
540 static int tls1_check_ec_key(SSL *s,
541 unsigned char *curve_id, unsigned char *comp_id)
543 const unsigned char *pformats, *pcurves;
544 size_t num_formats, num_curves, i;
547 * If point formats extension present check it, otherwise everything is
548 * supported (see RFC4492).
550 if (comp_id && s->session->tlsext_ecpointformatlist) {
551 pformats = s->session->tlsext_ecpointformatlist;
552 num_formats = s->session->tlsext_ecpointformatlist_length;
553 for (i = 0; i < num_formats; i++, pformats++) {
554 if (*comp_id == *pformats)
557 if (i == num_formats)
562 /* Check curve is consistent with client and server preferences */
563 for (j = 0; j <= 1; j++) {
564 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
566 if (j == 1 && num_curves == 0) {
568 * If we've not received any curves then skip this check.
569 * RFC 4492 does not require the supported elliptic curves extension
570 * so if it is not sent we can just choose any curve.
571 * It is invalid to send an empty list in the elliptic curves
572 * extension, so num_curves == 0 always means no extension.
576 for (i = 0; i < num_curves; i++, pcurves += 2) {
577 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
582 /* For clients can only check sent curve list */
589 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
593 * If we have a custom point format list use it otherwise use default
595 if (s->tlsext_ecpointformatlist) {
596 *pformats = s->tlsext_ecpointformatlist;
597 *num_formats = s->tlsext_ecpointformatlist_length;
599 *pformats = ecformats_default;
600 /* For Suite B we don't support char2 fields */
602 *num_formats = sizeof(ecformats_default) - 1;
604 *num_formats = sizeof(ecformats_default);
609 * Check cert parameters compatible with extensions: currently just checks EC
610 * certificates have compatible curves and compression.
612 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
614 unsigned char comp_id, curve_id[2];
617 pkey = X509_get0_pubkey(x);
620 /* If not EC nothing to do */
621 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
623 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
627 * Can't check curve_id for client certs as we don't have a supported
630 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
634 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
635 * SHA384+P-384, adjust digest if necessary.
637 if (set_ee_md && tls1_suiteb(s)) {
643 /* Check to see we have necessary signing algorithm */
644 if (curve_id[1] == TLSEXT_curve_P_256)
645 check_md = NID_ecdsa_with_SHA256;
646 else if (curve_id[1] == TLSEXT_curve_P_384)
647 check_md = NID_ecdsa_with_SHA384;
649 return 0; /* Should never happen */
650 for (i = 0; i < c->shared_sigalgslen; i++)
651 if (check_md == c->shared_sigalgs[i].signandhash_nid)
653 if (i == c->shared_sigalgslen)
655 if (set_ee_md == 2) {
656 if (check_md == NID_ecdsa_with_SHA256)
657 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
659 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
665 # ifndef OPENSSL_NO_EC
667 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
669 * @cid: Cipher ID we're considering using
671 * Checks that the kECDHE cipher suite we're considering using
672 * is compatible with the client extensions.
674 * Returns 0 when the cipher can't be used or 1 when it can.
676 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
679 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
682 if (tls1_suiteb(s)) {
683 unsigned char curve_id[2];
684 /* Curve to check determined by ciphersuite */
685 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
686 curve_id[1] = TLSEXT_curve_P_256;
687 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
688 curve_id[1] = TLSEXT_curve_P_384;
692 /* Check this curve is acceptable */
693 if (!tls1_check_ec_key(s, curve_id, NULL))
697 /* Need a shared curve */
698 if (tls1_shared_curve(s, 0))
702 # endif /* OPENSSL_NO_EC */
706 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
711 #endif /* OPENSSL_NO_EC */
714 * List of supported signature algorithms and hashes. Should make this
715 * customisable at some point, for now include everything we support.
718 #ifdef OPENSSL_NO_RSA
719 # define tlsext_sigalg_rsa(md) /* */
721 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
724 #ifdef OPENSSL_NO_DSA
725 # define tlsext_sigalg_dsa(md) /* */
727 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
731 # define tlsext_sigalg_ecdsa(md)/* */
733 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
736 #define tlsext_sigalg(md) \
737 tlsext_sigalg_rsa(md) \
738 tlsext_sigalg_dsa(md) \
739 tlsext_sigalg_ecdsa(md)
741 static const unsigned char tls12_sigalgs[] = {
742 tlsext_sigalg(TLSEXT_hash_sha512)
743 tlsext_sigalg(TLSEXT_hash_sha384)
744 tlsext_sigalg(TLSEXT_hash_sha256)
745 tlsext_sigalg(TLSEXT_hash_sha224)
746 tlsext_sigalg(TLSEXT_hash_sha1)
747 #ifndef OPENSSL_NO_GOST
748 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
749 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
750 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
754 #ifndef OPENSSL_NO_EC
755 static const unsigned char suiteb_sigalgs[] = {
756 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
757 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
760 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
763 * If Suite B mode use Suite B sigalgs only, ignore any other
766 #ifndef OPENSSL_NO_EC
767 switch (tls1_suiteb(s)) {
768 case SSL_CERT_FLAG_SUITEB_128_LOS:
769 *psigs = suiteb_sigalgs;
770 return sizeof(suiteb_sigalgs);
772 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
773 *psigs = suiteb_sigalgs;
776 case SSL_CERT_FLAG_SUITEB_192_LOS:
777 *psigs = suiteb_sigalgs + 2;
781 /* If server use client authentication sigalgs if not NULL */
782 if (s->server && s->cert->client_sigalgs) {
783 *psigs = s->cert->client_sigalgs;
784 return s->cert->client_sigalgslen;
785 } else if (s->cert->conf_sigalgs) {
786 *psigs = s->cert->conf_sigalgs;
787 return s->cert->conf_sigalgslen;
789 *psigs = tls12_sigalgs;
790 return sizeof(tls12_sigalgs);
795 * Check signature algorithm is consistent with sent supported signature
796 * algorithms and if so return relevant digest.
798 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
799 const unsigned char *sig, EVP_PKEY *pkey)
801 const unsigned char *sent_sigs;
802 size_t sent_sigslen, i;
803 int sigalg = tls12_get_sigid(pkey);
804 /* Should never happen */
807 /* Check key type is consistent with signature */
808 if (sigalg != (int)sig[1]) {
809 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
812 #ifndef OPENSSL_NO_EC
813 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
814 unsigned char curve_id[2], comp_id;
815 /* Check compression and curve matches extensions */
816 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
818 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
819 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
822 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
823 if (tls1_suiteb(s)) {
826 if (curve_id[1] == TLSEXT_curve_P_256) {
827 if (sig[0] != TLSEXT_hash_sha256) {
828 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
829 SSL_R_ILLEGAL_SUITEB_DIGEST);
832 } else if (curve_id[1] == TLSEXT_curve_P_384) {
833 if (sig[0] != TLSEXT_hash_sha384) {
834 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
835 SSL_R_ILLEGAL_SUITEB_DIGEST);
841 } else if (tls1_suiteb(s))
845 /* Check signature matches a type we sent */
846 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
847 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
848 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
851 /* Allow fallback to SHA1 if not strict mode */
852 if (i == sent_sigslen
853 && (sig[0] != TLSEXT_hash_sha1
854 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
855 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
858 *pmd = tls12_get_hash(sig[0]);
860 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
863 /* Make sure security callback allows algorithm */
864 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
865 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) {
866 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
870 * Store the digest used so applications can retrieve it if they wish.
872 s->s3->tmp.peer_md = *pmd;
877 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
878 * supported, doesn't appear in supported signature algorithms, isn't supported
879 * by the enabled protocol versions or by the security level.
881 * This function should only be used for checking which ciphers are supported
884 * Call ssl_cipher_disabled() to check that it's enabled or not.
886 void ssl_set_client_disabled(SSL *s)
888 s->s3->tmp.mask_a = 0;
889 s->s3->tmp.mask_k = 0;
890 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
891 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
892 #ifndef OPENSSL_NO_PSK
893 /* with PSK there must be client callback set */
894 if (!s->psk_client_callback) {
895 s->s3->tmp.mask_a |= SSL_aPSK;
896 s->s3->tmp.mask_k |= SSL_PSK;
898 #endif /* OPENSSL_NO_PSK */
899 #ifndef OPENSSL_NO_SRP
900 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
901 s->s3->tmp.mask_a |= SSL_aSRP;
902 s->s3->tmp.mask_k |= SSL_kSRP;
908 * ssl_cipher_disabled - check that a cipher is disabled or not
909 * @s: SSL connection that you want to use the cipher on
910 * @c: cipher to check
911 * @op: Security check that you want to do
913 * Returns 1 when it's disabled, 0 when enabled.
915 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
917 if (c->algorithm_mkey & s->s3->tmp.mask_k
918 || c->algorithm_auth & s->s3->tmp.mask_a)
920 if (s->s3->tmp.max_ver == 0)
922 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
923 || (c->max_tls < s->s3->tmp.min_ver)))
925 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
926 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
929 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
932 static int tls_use_ticket(SSL *s)
934 if (s->options & SSL_OP_NO_TICKET)
936 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
939 static int compare_uint(const void *p1, const void *p2)
941 unsigned int u1 = *((const unsigned int *)p1);
942 unsigned int u2 = *((const unsigned int *)p2);
952 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
953 * more than one extension of the same type in a ClientHello or ServerHello.
954 * This function does an initial scan over the extensions block to filter those
955 * out. It returns 1 if all extensions are unique, and 0 if the extensions
956 * contain duplicates, could not be successfully parsed, or an internal error
959 static int tls1_check_duplicate_extensions(const PACKET *packet)
961 PACKET extensions = *packet;
962 size_t num_extensions = 0, i = 0;
963 unsigned int *extension_types = NULL;
966 /* First pass: count the extensions. */
967 while (PACKET_remaining(&extensions) > 0) {
970 if (!PACKET_get_net_2(&extensions, &type) ||
971 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
977 if (num_extensions <= 1)
980 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
981 if (extension_types == NULL) {
982 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
986 /* Second pass: gather the extension types. */
987 extensions = *packet;
988 for (i = 0; i < num_extensions; i++) {
990 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
991 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
992 /* This should not happen. */
993 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
998 if (PACKET_remaining(&extensions) != 0) {
999 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1002 /* Sort the extensions and make sure there are no duplicates. */
1003 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1004 for (i = 1; i < num_extensions; i++) {
1005 if (extension_types[i - 1] == extension_types[i])
1010 OPENSSL_free(extension_types);
1014 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1015 unsigned char *limit, int *al)
1018 unsigned char *orig = buf;
1019 unsigned char *ret = buf;
1020 #ifndef OPENSSL_NO_EC
1021 /* See if we support any ECC ciphersuites */
1023 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1025 unsigned long alg_k, alg_a;
1026 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1028 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1029 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1031 alg_k = c->algorithm_mkey;
1032 alg_a = c->algorithm_auth;
1033 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1034 || (alg_a & SSL_aECDSA)) {
1045 return NULL; /* this really never occurs, but ... */
1047 /* Add RI if renegotiating */
1048 if (s->renegotiate) {
1051 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1052 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1056 if (CHECKLEN(ret, 4 + el, limit))
1059 s2n(TLSEXT_TYPE_renegotiate, ret);
1062 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1063 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1069 /* Only add RI for SSLv3 */
1070 if (s->client_version == SSL3_VERSION)
1073 if (s->tlsext_hostname != NULL) {
1074 /* Add TLS extension servername to the Client Hello message */
1078 * check for enough space.
1079 * 4 for the servername type and extension length
1080 * 2 for servernamelist length
1081 * 1 for the hostname type
1082 * 2 for hostname length
1085 size_str = strlen(s->tlsext_hostname);
1086 if (CHECKLEN(ret, 9 + size_str, limit))
1089 /* extension type and length */
1090 s2n(TLSEXT_TYPE_server_name, ret);
1091 s2n(size_str + 5, ret);
1093 /* length of servername list */
1094 s2n(size_str + 3, ret);
1096 /* hostname type, length and hostname */
1097 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1099 memcpy(ret, s->tlsext_hostname, size_str);
1102 #ifndef OPENSSL_NO_SRP
1103 /* Add SRP username if there is one */
1104 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1105 * Client Hello message */
1107 size_t login_len = strlen(s->srp_ctx.login);
1108 if (login_len > 255 || login_len == 0) {
1109 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1114 * check for enough space.
1115 * 4 for the srp type type and extension length
1116 * 1 for the srp user identity
1117 * + srp user identity length
1119 if (CHECKLEN(ret, 5 + login_len, limit))
1122 /* fill in the extension */
1123 s2n(TLSEXT_TYPE_srp, ret);
1124 s2n(login_len + 1, ret);
1125 (*ret++) = (unsigned char)login_len;
1126 memcpy(ret, s->srp_ctx.login, login_len);
1131 #ifndef OPENSSL_NO_EC
1134 * Add TLS extension ECPointFormats to the ClientHello message
1136 const unsigned char *pcurves, *pformats;
1137 size_t num_curves, num_formats, curves_list_len;
1139 unsigned char *etmp;
1141 tls1_get_formatlist(s, &pformats, &num_formats);
1143 if (num_formats > 255) {
1144 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1148 * check for enough space.
1149 * 4 bytes for the ec point formats type and extension length
1150 * 1 byte for the length of the formats
1153 if (CHECKLEN(ret, 5 + num_formats, limit))
1156 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1157 /* The point format list has 1-byte length. */
1158 s2n(num_formats + 1, ret);
1159 *(ret++) = (unsigned char)num_formats;
1160 memcpy(ret, pformats, num_formats);
1164 * Add TLS extension EllipticCurves to the ClientHello message
1166 pcurves = s->tlsext_ellipticcurvelist;
1167 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1170 if (num_curves > 65532 / 2) {
1171 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1175 * check for enough space.
1176 * 4 bytes for the ec curves type and extension length
1177 * 2 bytes for the curve list length
1178 * + curve list length
1180 if (CHECKLEN(ret, 6 + (num_curves * 2), limit))
1183 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1185 /* Copy curve ID if supported */
1186 for (i = 0; i < num_curves; i++, pcurves += 2) {
1187 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1188 *etmp++ = pcurves[0];
1189 *etmp++ = pcurves[1];
1193 curves_list_len = etmp - ret - 4;
1195 s2n(curves_list_len + 2, ret);
1196 s2n(curves_list_len, ret);
1197 ret += curves_list_len;
1199 #endif /* OPENSSL_NO_EC */
1201 if (tls_use_ticket(s)) {
1203 if (!s->new_session && s->session && s->session->tlsext_tick)
1204 ticklen = s->session->tlsext_ticklen;
1205 else if (s->session && s->tlsext_session_ticket &&
1206 s->tlsext_session_ticket->data) {
1207 ticklen = s->tlsext_session_ticket->length;
1208 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1209 if (s->session->tlsext_tick == NULL)
1211 memcpy(s->session->tlsext_tick,
1212 s->tlsext_session_ticket->data, ticklen);
1213 s->session->tlsext_ticklen = ticklen;
1216 if (ticklen == 0 && s->tlsext_session_ticket &&
1217 s->tlsext_session_ticket->data == NULL)
1220 * Check for enough room 2 for extension type, 2 for len rest for
1223 if (CHECKLEN(ret, 4 + ticklen, limit))
1225 s2n(TLSEXT_TYPE_session_ticket, ret);
1228 memcpy(ret, s->session->tlsext_tick, ticklen);
1234 if (SSL_CLIENT_USE_SIGALGS(s)) {
1236 const unsigned char *salg;
1237 unsigned char *etmp;
1238 salglen = tls12_get_psigalgs(s, &salg);
1241 * check for enough space.
1242 * 4 bytes for the sigalgs type and extension length
1243 * 2 bytes for the sigalg list length
1244 * + sigalg list length
1246 if (CHECKLEN(ret, salglen + 6, limit))
1248 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1250 /* Skip over lengths for now */
1252 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1253 /* Fill in lengths */
1254 s2n(salglen + 2, etmp);
1258 #ifndef OPENSSL_NO_OCSP
1259 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1261 size_t extlen, idlen;
1266 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1267 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1268 lentmp = i2d_OCSP_RESPID(id, NULL);
1271 idlen += (size_t)lentmp + 2;
1274 if (s->tlsext_ocsp_exts) {
1275 lentmp = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1278 extlen = (size_t)lentmp;
1282 if (extlen + idlen > 0xFFF0)
1285 * 2 bytes for status request type
1286 * 2 bytes for status request len
1287 * 1 byte for OCSP request type
1288 * 2 bytes for length of ids
1289 * 2 bytes for length of extensions
1291 * + length of extensions
1293 if (CHECKLEN(ret, 9 + idlen + extlen, limit))
1296 s2n(TLSEXT_TYPE_status_request, ret);
1297 s2n(extlen + idlen + 5, ret);
1298 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1300 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1301 /* save position of id len */
1302 unsigned char *q = ret;
1303 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1304 /* skip over id len */
1306 lentmp = i2d_OCSP_RESPID(id, &ret);
1312 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1315 #ifndef OPENSSL_NO_HEARTBEATS
1316 if (SSL_IS_DTLS(s)) {
1317 /* Add Heartbeat extension */
1320 * check for enough space.
1321 * 4 bytes for the heartbeat ext type and extension length
1322 * 1 byte for the mode
1324 if (CHECKLEN(ret, 5, limit))
1327 s2n(TLSEXT_TYPE_heartbeat, ret);
1331 * 1: peer may send requests
1332 * 2: peer not allowed to send requests
1334 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1335 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1337 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1341 #ifndef OPENSSL_NO_NEXTPROTONEG
1342 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1344 * The client advertises an empty extension to indicate its support
1345 * for Next Protocol Negotiation
1349 * check for enough space.
1350 * 4 bytes for the NPN ext type and extension length
1352 if (CHECKLEN(ret, 4, limit))
1354 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1360 * finish_md_len is non-zero during a renegotiation, so
1361 * this avoids sending ALPN during the renegotiation
1362 * (see longer comment below)
1364 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1366 * check for enough space.
1367 * 4 bytes for the ALPN type and extension length
1368 * 2 bytes for the ALPN protocol list length
1369 * + ALPN protocol list length
1371 if (CHECKLEN(ret, 6 + s->alpn_client_proto_list_len, limit))
1373 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1374 s2n(2 + s->alpn_client_proto_list_len, ret);
1375 s2n(s->alpn_client_proto_list_len, ret);
1376 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1377 ret += s->alpn_client_proto_list_len;
1378 s->s3->alpn_sent = 1;
1380 #ifndef OPENSSL_NO_SRTP
1381 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1384 /* Returns 0 on success!! */
1385 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1386 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1391 * check for enough space.
1392 * 4 bytes for the SRTP type and extension length
1393 * + SRTP profiles length
1395 if (CHECKLEN(ret, 4 + el, limit))
1398 s2n(TLSEXT_TYPE_use_srtp, ret);
1401 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1402 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1408 custom_ext_init(&s->cert->cli_ext);
1409 /* Add custom TLS Extensions to ClientHello */
1410 if (!custom_ext_add(s, 0, &ret, limit, al))
1413 * In 1.1.0 before 1.1.0c we negotiated EtM with DTLS, then just
1414 * silently failed to actually do it. It is fixed in 1.1.1 but to
1415 * ease the transition especially from 1.1.0b to 1.1.0c, we just
1416 * disable it in 1.1.0.
1418 if (!SSL_IS_DTLS(s)) {
1420 * check for enough space.
1421 * 4 bytes for the ETM type and extension length
1423 if (CHECKLEN(ret, 4, limit))
1425 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1429 #ifndef OPENSSL_NO_CT
1430 if (s->ct_validation_callback != NULL) {
1432 * check for enough space.
1433 * 4 bytes for the SCT type and extension length
1435 if (CHECKLEN(ret, 4, limit))
1438 s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret);
1444 * check for enough space.
1445 * 4 bytes for the EMS type and extension length
1447 if (CHECKLEN(ret, 4, limit))
1449 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1453 * Add padding to workaround bugs in F5 terminators. See
1454 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1455 * code works out the length of all existing extensions it MUST always
1458 if (s->options & SSL_OP_TLSEXT_PADDING) {
1459 int hlen = ret - (unsigned char *)s->init_buf->data;
1461 if (hlen > 0xff && hlen < 0x200) {
1462 hlen = 0x200 - hlen;
1469 * check for enough space. Strictly speaking we know we've already
1470 * got enough space because to get here the message size is < 0x200,
1471 * but we know that we've allocated far more than that in the buffer
1472 * - but for consistency and robustness we're going to check anyway.
1474 * 4 bytes for the padding type and extension length
1477 if (CHECKLEN(ret, 4 + hlen, limit))
1479 s2n(TLSEXT_TYPE_padding, ret);
1481 memset(ret, 0, hlen);
1488 if ((extdatalen = ret - orig - 2) == 0)
1491 s2n(extdatalen, orig);
1495 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1496 unsigned char *limit, int *al)
1499 unsigned char *orig = buf;
1500 unsigned char *ret = buf;
1501 #ifndef OPENSSL_NO_NEXTPROTONEG
1502 int next_proto_neg_seen;
1504 #ifndef OPENSSL_NO_EC
1505 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1506 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1507 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1508 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1513 return NULL; /* this really never occurs, but ... */
1515 if (s->s3->send_connection_binding) {
1518 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1519 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1524 * check for enough space.
1525 * 4 bytes for the reneg type and extension length
1526 * + reneg data length
1528 if (CHECKLEN(ret, 4 + el, limit))
1531 s2n(TLSEXT_TYPE_renegotiate, ret);
1534 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1535 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1542 /* Only add RI for SSLv3 */
1543 if (s->version == SSL3_VERSION)
1546 if (!s->hit && s->servername_done == 1
1547 && s->session->tlsext_hostname != NULL) {
1549 * check for enough space.
1550 * 4 bytes for the server name type and extension length
1552 if (CHECKLEN(ret, 4, limit))
1555 s2n(TLSEXT_TYPE_server_name, ret);
1558 #ifndef OPENSSL_NO_EC
1560 const unsigned char *plist;
1563 * Add TLS extension ECPointFormats to the ServerHello message
1566 tls1_get_formatlist(s, &plist, &plistlen);
1568 if (plistlen > 255) {
1569 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1574 * check for enough space.
1575 * 4 bytes for the ec points format type and extension length
1576 * 1 byte for the points format list length
1577 * + length of points format list
1579 if (CHECKLEN(ret, 5 + plistlen, limit))
1582 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1583 s2n(plistlen + 1, ret);
1584 *(ret++) = (unsigned char)plistlen;
1585 memcpy(ret, plist, plistlen);
1590 * Currently the server should not respond with a SupportedCurves
1593 #endif /* OPENSSL_NO_EC */
1595 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1597 * check for enough space.
1598 * 4 bytes for the Ticket type and extension length
1600 if (CHECKLEN(ret, 4, limit))
1602 s2n(TLSEXT_TYPE_session_ticket, ret);
1606 * if we don't add the above TLSEXT, we can't add a session ticket
1609 s->tlsext_ticket_expected = 0;
1612 if (s->tlsext_status_expected) {
1614 * check for enough space.
1615 * 4 bytes for the Status request type and extension length
1617 if (CHECKLEN(ret, 4, limit))
1619 s2n(TLSEXT_TYPE_status_request, ret);
1622 #ifndef OPENSSL_NO_SRTP
1623 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1626 /* Returns 0 on success!! */
1627 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1628 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1632 * check for enough space.
1633 * 4 bytes for the SRTP profiles type and extension length
1634 * + length of the SRTP profiles list
1636 if (CHECKLEN(ret, 4 + el, limit))
1639 s2n(TLSEXT_TYPE_use_srtp, ret);
1642 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1643 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1650 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1651 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1652 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1653 const unsigned char cryptopro_ext[36] = {
1654 0xfd, 0xe8, /* 65000 */
1655 0x00, 0x20, /* 32 bytes length */
1656 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1657 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1658 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1659 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1662 /* check for enough space. */
1663 if (CHECKLEN(ret, sizeof(cryptopro_ext), limit))
1665 memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext));
1666 ret += sizeof(cryptopro_ext);
1669 #ifndef OPENSSL_NO_HEARTBEATS
1670 /* Add Heartbeat extension if we've received one */
1671 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1673 * check for enough space.
1674 * 4 bytes for the Heartbeat type and extension length
1675 * 1 byte for the mode
1677 if (CHECKLEN(ret, 5, limit))
1679 s2n(TLSEXT_TYPE_heartbeat, ret);
1683 * 1: peer may send requests
1684 * 2: peer not allowed to send requests
1686 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1687 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1689 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1694 #ifndef OPENSSL_NO_NEXTPROTONEG
1695 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1696 s->s3->next_proto_neg_seen = 0;
1697 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1698 const unsigned char *npa;
1699 unsigned int npalen;
1702 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1704 ctx->next_protos_advertised_cb_arg);
1705 if (r == SSL_TLSEXT_ERR_OK) {
1707 * check for enough space.
1708 * 4 bytes for the NPN type and extension length
1709 * + length of protocols list
1711 if (CHECKLEN(ret, 4 + npalen, limit))
1713 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1715 memcpy(ret, npa, npalen);
1717 s->s3->next_proto_neg_seen = 1;
1721 if (!custom_ext_add(s, 1, &ret, limit, al))
1723 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1725 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1726 * for other cases too.
1728 if (SSL_IS_DTLS(s) || s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1729 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1730 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1731 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1732 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1735 * check for enough space.
1736 * 4 bytes for the ETM type and extension length
1738 if (CHECKLEN(ret, 4, limit))
1740 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1744 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1746 * check for enough space.
1747 * 4 bytes for the EMS type and extension length
1749 if (CHECKLEN(ret, 4, limit))
1751 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1755 if (s->s3->alpn_selected != NULL) {
1756 const unsigned char *selected = s->s3->alpn_selected;
1757 size_t len = s->s3->alpn_selected_len;
1760 * check for enough space.
1761 * 4 bytes for the ALPN type and extension length
1762 * 2 bytes for ALPN data length
1763 * 1 byte for selected protocol length
1764 * + length of the selected protocol
1766 if (CHECKLEN(ret, 7 + len, limit))
1768 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1772 memcpy(ret, selected, len);
1778 if ((extdatalen = ret - orig - 2) == 0)
1781 s2n(extdatalen, orig);
1786 * Save the ALPN extension in a ClientHello.
1787 * pkt: the contents of the ALPN extension, not including type and length.
1788 * al: a pointer to the alert value to send in the event of a failure.
1789 * returns: 1 on success, 0 on error.
1791 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1793 PACKET protocol_list, save_protocol_list, protocol;
1795 *al = SSL_AD_DECODE_ERROR;
1797 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1798 || PACKET_remaining(&protocol_list) < 2) {
1802 save_protocol_list = protocol_list;
1804 /* Protocol names can't be empty. */
1805 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1806 || PACKET_remaining(&protocol) == 0) {
1809 } while (PACKET_remaining(&protocol_list) != 0);
1811 if (!PACKET_memdup(&save_protocol_list,
1812 &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
1813 *al = TLS1_AD_INTERNAL_ERROR;
1821 * Process the ALPN extension in a ClientHello.
1822 * al: a pointer to the alert value to send in the event of a failure.
1823 * returns 1 on success, 0 on error.
1825 static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
1827 const unsigned char *selected = NULL;
1828 unsigned char selected_len = 0;
1830 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1831 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1832 s->s3->alpn_proposed,
1833 s->s3->alpn_proposed_len,
1834 s->ctx->alpn_select_cb_arg);
1836 if (r == SSL_TLSEXT_ERR_OK) {
1837 OPENSSL_free(s->s3->alpn_selected);
1838 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1839 if (s->s3->alpn_selected == NULL) {
1840 *al = SSL_AD_INTERNAL_ERROR;
1843 s->s3->alpn_selected_len = selected_len;
1844 #ifndef OPENSSL_NO_NEXTPROTONEG
1845 /* ALPN takes precedence over NPN. */
1846 s->s3->next_proto_neg_seen = 0;
1849 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1857 #ifndef OPENSSL_NO_EC
1859 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1860 * SecureTransport using the TLS extension block in |pkt|.
1861 * Safari, since 10.6, sends exactly these extensions, in this order:
1866 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1867 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1868 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1869 * 10.8..10.8.3 (which don't work).
1871 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1877 static const unsigned char kSafariExtensionsBlock[] = {
1878 0x00, 0x0a, /* elliptic_curves extension */
1879 0x00, 0x08, /* 8 bytes */
1880 0x00, 0x06, /* 6 bytes of curve ids */
1881 0x00, 0x17, /* P-256 */
1882 0x00, 0x18, /* P-384 */
1883 0x00, 0x19, /* P-521 */
1885 0x00, 0x0b, /* ec_point_formats */
1886 0x00, 0x02, /* 2 bytes */
1887 0x01, /* 1 point format */
1888 0x00, /* uncompressed */
1889 /* The following is only present in TLS 1.2 */
1890 0x00, 0x0d, /* signature_algorithms */
1891 0x00, 0x0c, /* 12 bytes */
1892 0x00, 0x0a, /* 10 bytes */
1893 0x05, 0x01, /* SHA-384/RSA */
1894 0x04, 0x01, /* SHA-256/RSA */
1895 0x02, 0x01, /* SHA-1/RSA */
1896 0x04, 0x03, /* SHA-256/ECDSA */
1897 0x02, 0x03, /* SHA-1/ECDSA */
1900 /* Length of the common prefix (first two extensions). */
1901 static const size_t kSafariCommonExtensionsLength = 18;
1905 if (!PACKET_forward(&tmppkt, 2)
1906 || !PACKET_get_net_2(&tmppkt, &type)
1907 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1911 if (type != TLSEXT_TYPE_server_name)
1914 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1915 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1917 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1920 #endif /* !OPENSSL_NO_EC */
1923 * Parse ClientHello extensions and stash extension info in various parts of
1924 * the SSL object. Verify that there are no duplicate extensions.
1926 * Behaviour upon resumption is extension-specific. If the extension has no
1927 * effect during resumption, it is parsed (to verify its format) but otherwise
1930 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1931 * Upon failure, sets |al| to the appropriate alert.
1933 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1936 int renegotiate_seen = 0;
1939 *al = SSL_AD_DECODE_ERROR;
1940 s->servername_done = 0;
1941 s->tlsext_status_type = -1;
1942 #ifndef OPENSSL_NO_NEXTPROTONEG
1943 s->s3->next_proto_neg_seen = 0;
1946 OPENSSL_free(s->s3->alpn_selected);
1947 s->s3->alpn_selected = NULL;
1948 s->s3->alpn_selected_len = 0;
1949 OPENSSL_free(s->s3->alpn_proposed);
1950 s->s3->alpn_proposed = NULL;
1951 s->s3->alpn_proposed_len = 0;
1952 #ifndef OPENSSL_NO_HEARTBEATS
1953 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1954 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1957 #ifndef OPENSSL_NO_EC
1958 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1959 ssl_check_for_safari(s, pkt);
1960 #endif /* !OPENSSL_NO_EC */
1962 /* Clear any signature algorithms extension received */
1963 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1964 s->s3->tmp.peer_sigalgs = NULL;
1965 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1967 #ifndef OPENSSL_NO_SRP
1968 OPENSSL_free(s->srp_ctx.login);
1969 s->srp_ctx.login = NULL;
1972 s->srtp_profile = NULL;
1974 if (PACKET_remaining(pkt) == 0)
1977 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1980 if (!tls1_check_duplicate_extensions(&extensions))
1984 * We parse all extensions to ensure the ClientHello is well-formed but,
1985 * unless an extension specifies otherwise, we ignore extensions upon
1988 while (PACKET_get_net_2(&extensions, &type)) {
1990 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1993 if (s->tlsext_debug_cb)
1994 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1995 PACKET_remaining(&extension),
1996 s->tlsext_debug_arg);
1998 if (type == TLSEXT_TYPE_renegotiate) {
1999 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
2001 renegotiate_seen = 1;
2002 } else if (s->version == SSL3_VERSION) {
2005 * The servername extension is treated as follows:
2007 * - Only the hostname type is supported with a maximum length of 255.
2008 * - The servername is rejected if too long or if it contains zeros,
2009 * in which case an fatal alert is generated.
2010 * - The servername field is maintained together with the session cache.
2011 * - When a session is resumed, the servername call back invoked in order
2012 * to allow the application to position itself to the right context.
2013 * - The servername is acknowledged if it is new for a session or when
2014 * it is identical to a previously used for the same session.
2015 * Applications can control the behaviour. They can at any time
2016 * set a 'desirable' servername for a new SSL object. This can be the
2017 * case for example with HTTPS when a Host: header field is received and
2018 * a renegotiation is requested. In this case, a possible servername
2019 * presented in the new client hello is only acknowledged if it matches
2020 * the value of the Host: field.
2021 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2022 * if they provide for changing an explicit servername context for the
2023 * session, i.e. when the session has been established with a servername
2025 * - On session reconnect, the servername extension may be absent.
2029 else if (type == TLSEXT_TYPE_server_name) {
2030 unsigned int servname_type;
2031 PACKET sni, hostname;
2033 if (!PACKET_as_length_prefixed_2(&extension, &sni)
2034 /* ServerNameList must be at least 1 byte long. */
2035 || PACKET_remaining(&sni) == 0) {
2040 * Although the server_name extension was intended to be
2041 * extensible to new name types, RFC 4366 defined the
2042 * syntax inextensibility and OpenSSL 1.0.x parses it as
2044 * RFC 6066 corrected the mistake but adding new name types
2045 * is nevertheless no longer feasible, so act as if no other
2046 * SNI types can exist, to simplify parsing.
2048 * Also note that the RFC permits only one SNI value per type,
2049 * i.e., we can only have a single hostname.
2051 if (!PACKET_get_1(&sni, &servname_type)
2052 || servname_type != TLSEXT_NAMETYPE_host_name
2053 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
2058 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
2059 *al = TLS1_AD_UNRECOGNIZED_NAME;
2063 if (PACKET_contains_zero_byte(&hostname)) {
2064 *al = TLS1_AD_UNRECOGNIZED_NAME;
2068 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
2069 *al = TLS1_AD_INTERNAL_ERROR;
2073 s->servername_done = 1;
2076 * TODO(openssl-team): if the SNI doesn't match, we MUST
2077 * fall back to a full handshake.
2079 s->servername_done = s->session->tlsext_hostname
2080 && PACKET_equal(&hostname, s->session->tlsext_hostname,
2081 strlen(s->session->tlsext_hostname));
2084 #ifndef OPENSSL_NO_SRP
2085 else if (type == TLSEXT_TYPE_srp) {
2088 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
2091 if (PACKET_contains_zero_byte(&srp_I))
2095 * TODO(openssl-team): currently, we re-authenticate the user
2096 * upon resumption. Instead, we MUST ignore the login.
2098 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
2099 *al = TLS1_AD_INTERNAL_ERROR;
2105 #ifndef OPENSSL_NO_EC
2106 else if (type == TLSEXT_TYPE_ec_point_formats) {
2107 PACKET ec_point_format_list;
2109 if (!PACKET_as_length_prefixed_1(&extension, &ec_point_format_list)
2110 || PACKET_remaining(&ec_point_format_list) == 0) {
2115 if (!PACKET_memdup(&ec_point_format_list,
2116 &s->session->tlsext_ecpointformatlist,
2118 session->tlsext_ecpointformatlist_length)) {
2119 *al = TLS1_AD_INTERNAL_ERROR;
2123 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2124 PACKET elliptic_curve_list;
2126 /* Each NamedCurve is 2 bytes and we must have at least 1. */
2127 if (!PACKET_as_length_prefixed_2(&extension, &elliptic_curve_list)
2128 || PACKET_remaining(&elliptic_curve_list) == 0
2129 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
2134 if (!PACKET_memdup(&elliptic_curve_list,
2135 &s->session->tlsext_ellipticcurvelist,
2137 session->tlsext_ellipticcurvelist_length)) {
2138 *al = TLS1_AD_INTERNAL_ERROR;
2143 #endif /* OPENSSL_NO_EC */
2144 else if (type == TLSEXT_TYPE_session_ticket) {
2145 if (s->tls_session_ticket_ext_cb &&
2146 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
2147 PACKET_remaining(&extension),
2148 s->tls_session_ticket_ext_cb_arg))
2150 *al = TLS1_AD_INTERNAL_ERROR;
2153 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2154 PACKET supported_sig_algs;
2156 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2157 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2158 || PACKET_remaining(&supported_sig_algs) == 0) {
2163 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2164 PACKET_remaining(&supported_sig_algs))) {
2168 } else if (type == TLSEXT_TYPE_status_request) {
2169 if (!PACKET_get_1(&extension,
2170 (unsigned int *)&s->tlsext_status_type)) {
2173 #ifndef OPENSSL_NO_OCSP
2174 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2175 const unsigned char *ext_data;
2176 PACKET responder_id_list, exts;
2177 if (!PACKET_get_length_prefixed_2
2178 (&extension, &responder_id_list))
2182 * We remove any OCSP_RESPIDs from a previous handshake
2183 * to prevent unbounded memory growth - CVE-2016-6304
2185 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2187 if (PACKET_remaining(&responder_id_list) > 0) {
2188 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2189 if (s->tlsext_ocsp_ids == NULL) {
2190 *al = SSL_AD_INTERNAL_ERROR;
2194 s->tlsext_ocsp_ids = NULL;
2197 while (PACKET_remaining(&responder_id_list) > 0) {
2199 PACKET responder_id;
2200 const unsigned char *id_data;
2202 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2204 || PACKET_remaining(&responder_id) == 0) {
2208 id_data = PACKET_data(&responder_id);
2209 id = d2i_OCSP_RESPID(NULL, &id_data,
2210 PACKET_remaining(&responder_id));
2214 if (id_data != PACKET_end(&responder_id)) {
2215 OCSP_RESPID_free(id);
2219 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2220 OCSP_RESPID_free(id);
2221 *al = SSL_AD_INTERNAL_ERROR;
2226 /* Read in request_extensions */
2227 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2230 if (PACKET_remaining(&exts) > 0) {
2231 ext_data = PACKET_data(&exts);
2232 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2233 X509_EXTENSION_free);
2234 s->tlsext_ocsp_exts =
2235 d2i_X509_EXTENSIONS(NULL, &ext_data,
2236 PACKET_remaining(&exts));
2237 if (s->tlsext_ocsp_exts == NULL
2238 || ext_data != PACKET_end(&exts)) {
2246 * We don't know what to do with any other type so ignore it.
2248 s->tlsext_status_type = -1;
2251 #ifndef OPENSSL_NO_HEARTBEATS
2252 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2253 unsigned int hbtype;
2255 if (!PACKET_get_1(&extension, &hbtype)
2256 || PACKET_remaining(&extension)) {
2257 *al = SSL_AD_DECODE_ERROR;
2261 case 0x01: /* Client allows us to send HB requests */
2262 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2264 case 0x02: /* Client doesn't accept HB requests */
2265 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2266 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2269 *al = SSL_AD_ILLEGAL_PARAMETER;
2274 #ifndef OPENSSL_NO_NEXTPROTONEG
2275 else if (type == TLSEXT_TYPE_next_proto_neg &&
2276 s->s3->tmp.finish_md_len == 0) {
2278 * We shouldn't accept this extension on a
2281 * s->new_session will be set on renegotiation, but we
2282 * probably shouldn't rely that it couldn't be set on
2283 * the initial renegotiation too in certain cases (when
2284 * there's some other reason to disallow resuming an
2285 * earlier session -- the current code won't be doing
2286 * anything like that, but this might change).
2288 * A valid sign that there's been a previous handshake
2289 * in this connection is if s->s3->tmp.finish_md_len >
2290 * 0. (We are talking about a check that will happen
2291 * in the Hello protocol round, well before a new
2292 * Finished message could have been computed.)
2294 s->s3->next_proto_neg_seen = 1;
2298 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2299 s->s3->tmp.finish_md_len == 0) {
2300 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2304 /* session ticket processed earlier */
2305 #ifndef OPENSSL_NO_SRTP
2306 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2307 && type == TLSEXT_TYPE_use_srtp) {
2308 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2312 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2313 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2315 * Note: extended master secret extension handled in
2316 * tls_check_serverhello_tlsext_early()
2320 * If this ClientHello extension was unhandled and this is a
2321 * nonresumed connection, check whether the extension is a custom
2322 * TLS Extension (has a custom_srv_ext_record), and if so call the
2323 * callback and record the extension number so that an appropriate
2324 * ServerHello may be later returned.
2327 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2328 PACKET_remaining(&extension), al) <= 0)
2333 if (PACKET_remaining(pkt) != 0) {
2335 * tls1_check_duplicate_extensions should ensure this never happens.
2337 *al = SSL_AD_INTERNAL_ERROR;
2343 /* Need RI if renegotiating */
2345 if (!renegotiate_seen && s->renegotiate &&
2346 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2347 *al = SSL_AD_HANDSHAKE_FAILURE;
2348 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2349 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2354 * This function currently has no state to clean up, so it returns directly.
2355 * If parsing fails at any point, the function returns early.
2356 * The SSL object may be left with partial data from extensions, but it must
2357 * then no longer be used, and clearing it up will free the leftovers.
2362 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2365 custom_ext_init(&s->cert->srv_ext);
2366 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2367 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2370 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2371 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2377 #ifndef OPENSSL_NO_NEXTPROTONEG
2379 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2380 * elements of zero length are allowed and the set of elements must exactly
2381 * fill the length of the block.
2383 static char ssl_next_proto_validate(PACKET *pkt)
2385 PACKET tmp_protocol;
2387 while (PACKET_remaining(pkt)) {
2388 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2389 || PACKET_remaining(&tmp_protocol) == 0)
2397 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2399 unsigned int length, type, size;
2400 int tlsext_servername = 0;
2401 int renegotiate_seen = 0;
2403 #ifndef OPENSSL_NO_NEXTPROTONEG
2404 s->s3->next_proto_neg_seen = 0;
2406 s->tlsext_ticket_expected = 0;
2408 OPENSSL_free(s->s3->alpn_selected);
2409 s->s3->alpn_selected = NULL;
2410 #ifndef OPENSSL_NO_HEARTBEATS
2411 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2412 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2415 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2417 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2419 if (!PACKET_get_net_2(pkt, &length))
2422 if (PACKET_remaining(pkt) != length) {
2423 *al = SSL_AD_DECODE_ERROR;
2427 if (!tls1_check_duplicate_extensions(pkt)) {
2428 *al = SSL_AD_DECODE_ERROR;
2432 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2433 const unsigned char *data;
2436 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2437 || !PACKET_peek_bytes(&spkt, &data, size))
2440 if (s->tlsext_debug_cb)
2441 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2443 if (type == TLSEXT_TYPE_renegotiate) {
2444 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2446 renegotiate_seen = 1;
2447 } else if (s->version == SSL3_VERSION) {
2448 } else if (type == TLSEXT_TYPE_server_name) {
2449 if (s->tlsext_hostname == NULL || size > 0) {
2450 *al = TLS1_AD_UNRECOGNIZED_NAME;
2453 tlsext_servername = 1;
2455 #ifndef OPENSSL_NO_EC
2456 else if (type == TLSEXT_TYPE_ec_point_formats) {
2457 unsigned int ecpointformatlist_length;
2458 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2459 || ecpointformatlist_length != size - 1) {
2460 *al = TLS1_AD_DECODE_ERROR;
2464 s->session->tlsext_ecpointformatlist_length = 0;
2465 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2466 if ((s->session->tlsext_ecpointformatlist =
2467 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2468 *al = TLS1_AD_INTERNAL_ERROR;
2471 s->session->tlsext_ecpointformatlist_length =
2472 ecpointformatlist_length;
2473 if (!PACKET_copy_bytes(&spkt,
2474 s->session->tlsext_ecpointformatlist,
2475 ecpointformatlist_length)) {
2476 *al = TLS1_AD_DECODE_ERROR;
2482 #endif /* OPENSSL_NO_EC */
2484 else if (type == TLSEXT_TYPE_session_ticket) {
2485 if (s->tls_session_ticket_ext_cb &&
2486 !s->tls_session_ticket_ext_cb(s, data, size,
2487 s->tls_session_ticket_ext_cb_arg))
2489 *al = TLS1_AD_INTERNAL_ERROR;
2492 if (!tls_use_ticket(s) || (size > 0)) {
2493 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2496 s->tlsext_ticket_expected = 1;
2497 } else if (type == TLSEXT_TYPE_status_request) {
2499 * MUST be empty and only sent if we've requested a status
2502 if ((s->tlsext_status_type == -1) || (size > 0)) {
2503 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2506 /* Set flag to expect CertificateStatus message */
2507 s->tlsext_status_expected = 1;
2509 #ifndef OPENSSL_NO_CT
2511 * Only take it if we asked for it - i.e if there is no CT validation
2512 * callback set, then a custom extension MAY be processing it, so we
2513 * need to let control continue to flow to that.
2515 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2516 s->ct_validation_callback != NULL) {
2517 /* Simply copy it off for later processing */
2518 if (s->tlsext_scts != NULL) {
2519 OPENSSL_free(s->tlsext_scts);
2520 s->tlsext_scts = NULL;
2522 s->tlsext_scts_len = size;
2524 s->tlsext_scts = OPENSSL_malloc(size);
2525 if (s->tlsext_scts == NULL) {
2526 *al = TLS1_AD_INTERNAL_ERROR;
2529 memcpy(s->tlsext_scts, data, size);
2533 #ifndef OPENSSL_NO_NEXTPROTONEG
2534 else if (type == TLSEXT_TYPE_next_proto_neg &&
2535 s->s3->tmp.finish_md_len == 0) {
2536 unsigned char *selected;
2537 unsigned char selected_len;
2538 /* We must have requested it. */
2539 if (s->ctx->next_proto_select_cb == NULL) {
2540 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2543 /* The data must be valid */
2544 if (!ssl_next_proto_validate(&spkt)) {
2545 *al = TLS1_AD_DECODE_ERROR;
2548 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2551 ctx->next_proto_select_cb_arg) !=
2552 SSL_TLSEXT_ERR_OK) {
2553 *al = TLS1_AD_INTERNAL_ERROR;
2557 * Could be non-NULL if server has sent multiple NPN extensions in
2558 * a single Serverhello
2560 OPENSSL_free(s->next_proto_negotiated);
2561 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2562 if (s->next_proto_negotiated == NULL) {
2563 *al = TLS1_AD_INTERNAL_ERROR;
2566 memcpy(s->next_proto_negotiated, selected, selected_len);
2567 s->next_proto_negotiated_len = selected_len;
2568 s->s3->next_proto_neg_seen = 1;
2572 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2574 /* We must have requested it. */
2575 if (!s->s3->alpn_sent) {
2576 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2580 * The extension data consists of:
2581 * uint16 list_length
2582 * uint8 proto_length;
2583 * uint8 proto[proto_length];
2585 if (!PACKET_get_net_2(&spkt, &len)
2586 || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len)
2587 || PACKET_remaining(&spkt) != len) {
2588 *al = TLS1_AD_DECODE_ERROR;
2591 OPENSSL_free(s->s3->alpn_selected);
2592 s->s3->alpn_selected = OPENSSL_malloc(len);
2593 if (s->s3->alpn_selected == NULL) {
2594 *al = TLS1_AD_INTERNAL_ERROR;
2597 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2598 *al = TLS1_AD_DECODE_ERROR;
2601 s->s3->alpn_selected_len = len;
2603 #ifndef OPENSSL_NO_HEARTBEATS
2604 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2605 unsigned int hbtype;
2606 if (!PACKET_get_1(&spkt, &hbtype)) {
2607 *al = SSL_AD_DECODE_ERROR;
2611 case 0x01: /* Server allows us to send HB requests */
2612 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2614 case 0x02: /* Server doesn't accept HB requests */
2615 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2616 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2619 *al = SSL_AD_ILLEGAL_PARAMETER;
2624 #ifndef OPENSSL_NO_SRTP
2625 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2626 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2630 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2631 /* Ignore if inappropriate ciphersuite */
2632 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2633 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2634 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2635 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2636 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2638 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2641 * If this extension type was not otherwise handled, but matches a
2642 * custom_cli_ext_record, then send it to the c callback
2644 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2648 if (PACKET_remaining(pkt) != 0) {
2649 *al = SSL_AD_DECODE_ERROR;
2653 if (!s->hit && tlsext_servername == 1) {
2654 if (s->tlsext_hostname) {
2655 if (s->session->tlsext_hostname == NULL) {
2656 s->session->tlsext_hostname =
2657 OPENSSL_strdup(s->tlsext_hostname);
2658 if (!s->session->tlsext_hostname) {
2659 *al = SSL_AD_UNRECOGNIZED_NAME;
2663 *al = SSL_AD_DECODE_ERROR;
2672 * Determine if we need to see RI. Strictly speaking if we want to avoid
2673 * an attack we should *always* see RI even on initial server hello
2674 * because the client doesn't see any renegotiation during an attack.
2675 * However this would mean we could not connect to any server which
2676 * doesn't support RI so for the immediate future tolerate RI absence
2678 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2679 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2680 *al = SSL_AD_HANDSHAKE_FAILURE;
2681 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2682 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2688 * Check extended master secret extension is consistent with
2691 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2692 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2693 *al = SSL_AD_HANDSHAKE_FAILURE;
2694 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2702 int ssl_prepare_clienthello_tlsext(SSL *s)
2704 s->s3->alpn_sent = 0;
2708 int ssl_prepare_serverhello_tlsext(SSL *s)
2713 static int ssl_check_clienthello_tlsext_early(SSL *s)
2715 int ret = SSL_TLSEXT_ERR_NOACK;
2716 int al = SSL_AD_UNRECOGNIZED_NAME;
2718 #ifndef OPENSSL_NO_EC
2720 * The handling of the ECPointFormats extension is done elsewhere, namely
2721 * in ssl3_choose_cipher in s3_lib.c.
2724 * The handling of the EllipticCurves extension is done elsewhere, namely
2725 * in ssl3_choose_cipher in s3_lib.c.
2729 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2731 s->ctx->tlsext_servername_callback(s, &al,
2732 s->ctx->tlsext_servername_arg);
2733 else if (s->initial_ctx != NULL
2734 && s->initial_ctx->tlsext_servername_callback != 0)
2736 s->initial_ctx->tlsext_servername_callback(s, &al,
2738 initial_ctx->tlsext_servername_arg);
2741 case SSL_TLSEXT_ERR_ALERT_FATAL:
2742 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2745 case SSL_TLSEXT_ERR_ALERT_WARNING:
2746 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2749 case SSL_TLSEXT_ERR_NOACK:
2750 s->servername_done = 0;
2756 /* Initialise digests to default values */
2757 void ssl_set_default_md(SSL *s)
2759 const EVP_MD **pmd = s->s3->tmp.md;
2760 #ifndef OPENSSL_NO_DSA
2761 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2763 #ifndef OPENSSL_NO_RSA
2764 if (SSL_USE_SIGALGS(s))
2765 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2767 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2768 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2770 #ifndef OPENSSL_NO_EC
2771 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2773 #ifndef OPENSSL_NO_GOST
2774 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2775 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2776 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2780 int tls1_set_server_sigalgs(SSL *s)
2785 /* Clear any shared signature algorithms */
2786 OPENSSL_free(s->cert->shared_sigalgs);
2787 s->cert->shared_sigalgs = NULL;
2788 s->cert->shared_sigalgslen = 0;
2789 /* Clear certificate digests and validity flags */
2790 for (i = 0; i < SSL_PKEY_NUM; i++) {
2791 s->s3->tmp.md[i] = NULL;
2792 s->s3->tmp.valid_flags[i] = 0;
2795 /* If sigalgs received process it. */
2796 if (s->s3->tmp.peer_sigalgs) {
2797 if (!tls1_process_sigalgs(s)) {
2798 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2799 al = SSL_AD_INTERNAL_ERROR;
2802 /* Fatal error is no shared signature algorithms */
2803 if (!s->cert->shared_sigalgs) {
2804 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2805 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
2806 al = SSL_AD_ILLEGAL_PARAMETER;
2810 ssl_set_default_md(s);
2814 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2819 * Upon success, returns 1.
2820 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
2822 int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
2824 s->tlsext_status_expected = 0;
2827 * If status request then ask callback what to do. Note: this must be
2828 * called after servername callbacks in case the certificate has changed,
2829 * and must be called after the cipher has been chosen because this may
2830 * influence which certificate is sent
2832 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2834 CERT_PKEY *certpkey;
2835 certpkey = ssl_get_server_send_pkey(s);
2836 /* If no certificate can't return certificate status */
2837 if (certpkey != NULL) {
2839 * Set current certificate to one we will use so SSL_get_certificate
2840 * et al can pick it up.
2842 s->cert->key = certpkey;
2843 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2845 /* We don't want to send a status request response */
2846 case SSL_TLSEXT_ERR_NOACK:
2847 s->tlsext_status_expected = 0;
2849 /* status request response should be sent */
2850 case SSL_TLSEXT_ERR_OK:
2851 if (s->tlsext_ocsp_resp)
2852 s->tlsext_status_expected = 1;
2854 /* something bad happened */
2855 case SSL_TLSEXT_ERR_ALERT_FATAL:
2857 *al = SSL_AD_INTERNAL_ERROR;
2863 if (!tls1_alpn_handle_client_hello_late(s, al)) {
2870 int ssl_check_serverhello_tlsext(SSL *s)
2872 int ret = SSL_TLSEXT_ERR_NOACK;
2873 int al = SSL_AD_UNRECOGNIZED_NAME;
2875 #ifndef OPENSSL_NO_EC
2877 * If we are client and using an elliptic curve cryptography cipher
2878 * suite, then if server returns an EC point formats lists extension it
2879 * must contain uncompressed.
2881 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2882 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2883 if ((s->tlsext_ecpointformatlist != NULL)
2884 && (s->tlsext_ecpointformatlist_length > 0)
2885 && (s->session->tlsext_ecpointformatlist != NULL)
2886 && (s->session->tlsext_ecpointformatlist_length > 0)
2887 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2888 /* we are using an ECC cipher */
2890 unsigned char *list;
2891 int found_uncompressed = 0;
2892 list = s->session->tlsext_ecpointformatlist;
2893 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2894 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2895 found_uncompressed = 1;
2899 if (!found_uncompressed) {
2900 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2901 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2905 ret = SSL_TLSEXT_ERR_OK;
2906 #endif /* OPENSSL_NO_EC */
2908 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2910 s->ctx->tlsext_servername_callback(s, &al,
2911 s->ctx->tlsext_servername_arg);
2912 else if (s->initial_ctx != NULL
2913 && s->initial_ctx->tlsext_servername_callback != 0)
2915 s->initial_ctx->tlsext_servername_callback(s, &al,
2917 initial_ctx->tlsext_servername_arg);
2920 * Ensure we get sensible values passed to tlsext_status_cb in the event
2921 * that we don't receive a status message
2923 OPENSSL_free(s->tlsext_ocsp_resp);
2924 s->tlsext_ocsp_resp = NULL;
2925 s->tlsext_ocsp_resplen = -1;
2928 case SSL_TLSEXT_ERR_ALERT_FATAL:
2929 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2932 case SSL_TLSEXT_ERR_ALERT_WARNING:
2933 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2936 case SSL_TLSEXT_ERR_NOACK:
2937 s->servername_done = 0;
2943 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2946 if (s->version < SSL3_VERSION)
2948 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2949 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2953 if (ssl_check_serverhello_tlsext(s) <= 0) {
2954 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2961 * Since the server cache lookup is done early on in the processing of the
2962 * ClientHello and other operations depend on the result some extensions
2963 * need to be handled at the same time.
2965 * Two extensions are currently handled, session ticket and extended master
2968 * session_id: ClientHello session ID.
2969 * ext: ClientHello extensions (including length prefix)
2970 * ret: (output) on return, if a ticket was decrypted, then this is set to
2971 * point to the resulting session.
2973 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2974 * ciphersuite, in which case we have no use for session tickets and one will
2975 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2978 * -1: fatal error, either from parsing or decrypting the ticket.
2979 * 0: no ticket was found (or was ignored, based on settings).
2980 * 1: a zero length extension was found, indicating that the client supports
2981 * session tickets but doesn't currently have one to offer.
2982 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2983 * couldn't be decrypted because of a non-fatal error.
2984 * 3: a ticket was successfully decrypted and *ret was set.
2987 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2988 * a new session ticket to the client because the client indicated support
2989 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2990 * a session ticket or we couldn't use the one it gave us, or if
2991 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2992 * Otherwise, s->tlsext_ticket_expected is set to 0.
2994 * For extended master secret flag is set if the extension is present.
2997 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2998 const PACKET *session_id,
3002 PACKET local_ext = *ext;
3005 int have_ticket = 0;
3006 int use_ticket = tls_use_ticket(s);
3009 s->tlsext_ticket_expected = 0;
3010 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
3013 * If tickets disabled behave as if no ticket present to permit stateful
3016 if ((s->version <= SSL3_VERSION))
3019 if (!PACKET_get_net_2(&local_ext, &i)) {
3023 while (PACKET_remaining(&local_ext) >= 4) {
3024 unsigned int type, size;
3026 if (!PACKET_get_net_2(&local_ext, &type)
3027 || !PACKET_get_net_2(&local_ext, &size)) {
3028 /* Shouldn't ever happen */
3032 if (PACKET_remaining(&local_ext) < size) {
3036 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
3038 const unsigned char *etick;
3040 /* Duplicate extension */
3041 if (have_ticket != 0) {
3049 * The client will accept a ticket but doesn't currently have
3052 s->tlsext_ticket_expected = 1;
3056 if (s->tls_session_secret_cb) {
3058 * Indicate that the ticket couldn't be decrypted rather than
3059 * generating the session from ticket now, trigger
3060 * abbreviated handshake based on external mechanism to
3061 * calculate the master secret later.
3066 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
3067 /* Shouldn't ever happen */
3071 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
3072 PACKET_remaining(session_id), ret);
3074 case 2: /* ticket couldn't be decrypted */
3075 s->tlsext_ticket_expected = 1;
3078 case 3: /* ticket was decrypted */
3081 case 4: /* ticket decrypted but need to renew */
3082 s->tlsext_ticket_expected = 1;
3085 default: /* fatal error */
3091 if (type == TLSEXT_TYPE_extended_master_secret)
3092 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
3093 if (!PACKET_forward(&local_ext, size)) {
3099 if (have_ticket == 0)
3106 * tls_decrypt_ticket attempts to decrypt a session ticket.
3108 * etick: points to the body of the session ticket extension.
3109 * eticklen: the length of the session tickets extension.
3110 * sess_id: points at the session ID.
3111 * sesslen: the length of the session ID.
3112 * psess: (output) on return, if a ticket was decrypted, then this is set to
3113 * point to the resulting session.
3116 * -2: fatal error, malloc failure.
3117 * -1: fatal error, either from parsing or decrypting the ticket.
3118 * 2: the ticket couldn't be decrypted.
3119 * 3: a ticket was successfully decrypted and *psess was set.
3120 * 4: same as 3, but the ticket needs to be renewed.
3122 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3123 int eticklen, const unsigned char *sess_id,
3124 int sesslen, SSL_SESSION **psess)
3127 unsigned char *sdec;
3128 const unsigned char *p;
3129 int slen, mlen, renew_ticket = 0, ret = -1;
3130 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3131 HMAC_CTX *hctx = NULL;
3132 EVP_CIPHER_CTX *ctx;
3133 SSL_CTX *tctx = s->initial_ctx;
3135 /* Initialize session ticket encryption and HMAC contexts */
3136 hctx = HMAC_CTX_new();
3139 ctx = EVP_CIPHER_CTX_new();
3144 if (tctx->tlsext_ticket_key_cb) {
3145 unsigned char *nctick = (unsigned char *)etick;
3146 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3157 /* Check key name matches */
3158 if (memcmp(etick, tctx->tlsext_tick_key_name,
3159 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3163 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3164 sizeof(tctx->tlsext_tick_hmac_key),
3165 EVP_sha256(), NULL) <= 0
3166 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3167 tctx->tlsext_tick_aes_key,
3168 etick + sizeof(tctx->tlsext_tick_key_name)) <=
3174 * Attempt to process session ticket, first conduct sanity and integrity
3177 mlen = HMAC_size(hctx);
3181 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3183 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
3188 /* Check HMAC of encrypted ticket */
3189 if (HMAC_Update(hctx, etick, eticklen) <= 0
3190 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3193 HMAC_CTX_free(hctx);
3194 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3195 EVP_CIPHER_CTX_free(ctx);
3198 /* Attempt to decrypt session data */
3199 /* Move p after IV to start of encrypted ticket, update length */
3200 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3201 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3202 sdec = OPENSSL_malloc(eticklen);
3203 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3204 EVP_CIPHER_CTX_free(ctx);
3208 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3209 EVP_CIPHER_CTX_free(ctx);
3214 EVP_CIPHER_CTX_free(ctx);
3218 sess = d2i_SSL_SESSION(NULL, &p, slen);
3222 * The session ID, if non-empty, is used by some clients to detect
3223 * that the ticket has been accepted. So we copy it to the session
3224 * structure. If it is empty set length to zero as required by
3228 memcpy(sess->session_id, sess_id, sesslen);
3229 sess->session_id_length = sesslen;
3238 * For session parse failure, indicate that we need to send a new ticket.
3242 EVP_CIPHER_CTX_free(ctx);
3243 HMAC_CTX_free(hctx);
3247 /* Tables to translate from NIDs to TLS v1.2 ids */
3254 static const tls12_lookup tls12_md[] = {
3255 {NID_md5, TLSEXT_hash_md5},
3256 {NID_sha1, TLSEXT_hash_sha1},
3257 {NID_sha224, TLSEXT_hash_sha224},
3258 {NID_sha256, TLSEXT_hash_sha256},
3259 {NID_sha384, TLSEXT_hash_sha384},
3260 {NID_sha512, TLSEXT_hash_sha512},
3261 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3262 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3263 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3266 static const tls12_lookup tls12_sig[] = {
3267 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3268 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3269 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3270 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3271 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3272 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3275 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3278 for (i = 0; i < tlen; i++) {
3279 if (table[i].nid == nid)
3285 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3288 for (i = 0; i < tlen; i++) {
3289 if ((table[i].id) == id)
3290 return table[i].nid;
3295 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md)
3300 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3303 sig_id = tls12_get_sigid(pk);
3306 p[0] = (unsigned char)md_id;
3307 p[1] = (unsigned char)sig_id;
3311 int tls12_get_sigid(const EVP_PKEY *pk)
3313 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3320 unsigned char tlsext_hash;
3323 static const tls12_hash_info tls12_md_info[] = {
3324 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3325 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3326 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3327 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3328 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3329 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3330 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3331 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX,
3332 TLSEXT_hash_gostr34112012_256},
3333 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX,
3334 TLSEXT_hash_gostr34112012_512},
3337 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3343 for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) {
3344 if (tls12_md_info[i].tlsext_hash == hash_alg)
3345 return tls12_md_info + i;
3351 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3353 const tls12_hash_info *inf;
3354 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3356 inf = tls12_get_hash_info(hash_alg);
3359 return ssl_md(inf->md_idx);
3362 static int tls12_get_pkey_idx(unsigned char sig_alg)
3365 #ifndef OPENSSL_NO_RSA
3366 case TLSEXT_signature_rsa:
3367 return SSL_PKEY_RSA_SIGN;
3369 #ifndef OPENSSL_NO_DSA
3370 case TLSEXT_signature_dsa:
3371 return SSL_PKEY_DSA_SIGN;
3373 #ifndef OPENSSL_NO_EC
3374 case TLSEXT_signature_ecdsa:
3375 return SSL_PKEY_ECC;
3377 #ifndef OPENSSL_NO_GOST
3378 case TLSEXT_signature_gostr34102001:
3379 return SSL_PKEY_GOST01;
3381 case TLSEXT_signature_gostr34102012_256:
3382 return SSL_PKEY_GOST12_256;
3384 case TLSEXT_signature_gostr34102012_512:
3385 return SSL_PKEY_GOST12_512;
3391 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3392 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3393 int *psignhash_nid, const unsigned char *data)
3395 int sign_nid = NID_undef, hash_nid = NID_undef;
3396 if (!phash_nid && !psign_nid && !psignhash_nid)
3398 if (phash_nid || psignhash_nid) {
3399 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3401 *phash_nid = hash_nid;
3403 if (psign_nid || psignhash_nid) {
3404 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3406 *psign_nid = sign_nid;
3408 if (psignhash_nid) {
3409 if (sign_nid == NID_undef || hash_nid == NID_undef
3410 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0)
3411 *psignhash_nid = NID_undef;
3415 /* Check to see if a signature algorithm is allowed */
3416 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3418 /* See if we have an entry in the hash table and it is enabled */
3419 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3420 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3422 /* See if public key algorithm allowed */
3423 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3425 /* Finally see if security callback allows it */
3426 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3430 * Get a mask of disabled public key algorithms based on supported signature
3431 * algorithms. For example if no signature algorithm supports RSA then RSA is
3435 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3437 const unsigned char *sigalgs;
3438 size_t i, sigalgslen;
3439 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3441 * Now go through all signature algorithms seeing if we support any for
3442 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3443 * down calls to security callback only check if we have to.
3445 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3446 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3447 switch (sigalgs[1]) {
3448 #ifndef OPENSSL_NO_RSA
3449 case TLSEXT_signature_rsa:
3450 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3454 #ifndef OPENSSL_NO_DSA
3455 case TLSEXT_signature_dsa:
3456 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3460 #ifndef OPENSSL_NO_EC
3461 case TLSEXT_signature_ecdsa:
3462 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3469 *pmask_a |= SSL_aRSA;
3471 *pmask_a |= SSL_aDSS;
3473 *pmask_a |= SSL_aECDSA;
3476 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3477 const unsigned char *psig, size_t psiglen)
3479 unsigned char *tmpout = out;
3481 for (i = 0; i < psiglen; i += 2, psig += 2) {
3482 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3483 *tmpout++ = psig[0];
3484 *tmpout++ = psig[1];
3487 return tmpout - out;
3490 /* Given preference and allowed sigalgs set shared sigalgs */
3491 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3492 const unsigned char *pref, size_t preflen,
3493 const unsigned char *allow, size_t allowlen)
3495 const unsigned char *ptmp, *atmp;
3496 size_t i, j, nmatch = 0;
3497 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3498 /* Skip disabled hashes or signature algorithms */
3499 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3501 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3502 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3505 shsig->rhash = ptmp[0];
3506 shsig->rsign = ptmp[1];
3507 tls1_lookup_sigalg(&shsig->hash_nid,
3509 &shsig->signandhash_nid, ptmp);
3519 /* Set shared signature algorithms for SSL structures */
3520 static int tls1_set_shared_sigalgs(SSL *s)
3522 const unsigned char *pref, *allow, *conf;
3523 size_t preflen, allowlen, conflen;
3525 TLS_SIGALGS *salgs = NULL;
3527 unsigned int is_suiteb = tls1_suiteb(s);
3529 OPENSSL_free(c->shared_sigalgs);
3530 c->shared_sigalgs = NULL;
3531 c->shared_sigalgslen = 0;
3532 /* If client use client signature algorithms if not NULL */
3533 if (!s->server && c->client_sigalgs && !is_suiteb) {
3534 conf = c->client_sigalgs;
3535 conflen = c->client_sigalgslen;
3536 } else if (c->conf_sigalgs && !is_suiteb) {
3537 conf = c->conf_sigalgs;
3538 conflen = c->conf_sigalgslen;
3540 conflen = tls12_get_psigalgs(s, &conf);
3541 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3544 allow = s->s3->tmp.peer_sigalgs;
3545 allowlen = s->s3->tmp.peer_sigalgslen;
3549 pref = s->s3->tmp.peer_sigalgs;
3550 preflen = s->s3->tmp.peer_sigalgslen;
3552 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3554 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3557 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3561 c->shared_sigalgs = salgs;
3562 c->shared_sigalgslen = nmatch;
3566 /* Set preferred digest for each key type */
3568 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3571 /* Extension ignored for inappropriate versions */
3572 if (!SSL_USE_SIGALGS(s))
3574 /* Should never happen */
3578 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3579 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3580 if (s->s3->tmp.peer_sigalgs == NULL)
3582 s->s3->tmp.peer_sigalgslen = dsize;
3583 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3587 int tls1_process_sigalgs(SSL *s)
3592 const EVP_MD **pmd = s->s3->tmp.md;
3593 uint32_t *pvalid = s->s3->tmp.valid_flags;
3595 TLS_SIGALGS *sigptr;
3596 if (!tls1_set_shared_sigalgs(s))
3599 for (i = 0, sigptr = c->shared_sigalgs;
3600 i < c->shared_sigalgslen; i++, sigptr++) {
3601 idx = tls12_get_pkey_idx(sigptr->rsign);
3602 if (idx > 0 && pmd[idx] == NULL) {
3603 md = tls12_get_hash(sigptr->rhash);
3605 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3606 if (idx == SSL_PKEY_RSA_SIGN) {
3607 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3608 pmd[SSL_PKEY_RSA_ENC] = md;
3614 * In strict mode leave unset digests as NULL to indicate we can't use
3615 * the certificate for signing.
3617 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3619 * Set any remaining keys to default values. NOTE: if alg is not
3620 * supported it stays as NULL.
3622 #ifndef OPENSSL_NO_DSA
3623 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3624 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3626 #ifndef OPENSSL_NO_RSA
3627 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3628 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3629 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3632 #ifndef OPENSSL_NO_EC
3633 if (pmd[SSL_PKEY_ECC] == NULL)
3634 pmd[SSL_PKEY_ECC] = EVP_sha1();
3636 #ifndef OPENSSL_NO_GOST
3637 if (pmd[SSL_PKEY_GOST01] == NULL)
3638 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3639 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3640 pmd[SSL_PKEY_GOST12_256] =
3641 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3642 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3643 pmd[SSL_PKEY_GOST12_512] =
3644 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3650 int SSL_get_sigalgs(SSL *s, int idx,
3651 int *psign, int *phash, int *psignhash,
3652 unsigned char *rsig, unsigned char *rhash)
3654 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3659 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3666 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3668 return s->s3->tmp.peer_sigalgslen / 2;
3671 int SSL_get_shared_sigalgs(SSL *s, int idx,
3672 int *psign, int *phash, int *psignhash,
3673 unsigned char *rsig, unsigned char *rhash)
3675 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3676 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3680 *phash = shsigalgs->hash_nid;
3682 *psign = shsigalgs->sign_nid;
3684 *psignhash = shsigalgs->signandhash_nid;
3686 *rsig = shsigalgs->rsign;
3688 *rhash = shsigalgs->rhash;
3689 return s->cert->shared_sigalgslen;
3692 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3696 int sigalgs[MAX_SIGALGLEN];
3699 static void get_sigorhash(int *psig, int *phash, const char *str)
3701 if (strcmp(str, "RSA") == 0) {
3702 *psig = EVP_PKEY_RSA;
3703 } else if (strcmp(str, "DSA") == 0) {
3704 *psig = EVP_PKEY_DSA;
3705 } else if (strcmp(str, "ECDSA") == 0) {
3706 *psig = EVP_PKEY_EC;
3708 *phash = OBJ_sn2nid(str);
3709 if (*phash == NID_undef)
3710 *phash = OBJ_ln2nid(str);
3714 static int sig_cb(const char *elem, int len, void *arg)
3716 sig_cb_st *sarg = arg;
3719 int sig_alg = NID_undef, hash_alg = NID_undef;
3722 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3724 if (len > (int)(sizeof(etmp) - 1))
3726 memcpy(etmp, elem, len);
3728 p = strchr(etmp, '+');
3736 get_sigorhash(&sig_alg, &hash_alg, etmp);
3737 get_sigorhash(&sig_alg, &hash_alg, p);
3739 if (sig_alg == NID_undef || hash_alg == NID_undef)
3742 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3743 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3746 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3747 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3752 * Set supported signature algorithms based on a colon separated list of the
3753 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3755 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3759 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3763 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3766 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3768 unsigned char *sigalgs, *sptr;
3773 sigalgs = OPENSSL_malloc(salglen);
3774 if (sigalgs == NULL)
3776 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3777 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3778 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3780 if (rhash == -1 || rsign == -1)
3787 OPENSSL_free(c->client_sigalgs);
3788 c->client_sigalgs = sigalgs;
3789 c->client_sigalgslen = salglen;
3791 OPENSSL_free(c->conf_sigalgs);
3792 c->conf_sigalgs = sigalgs;
3793 c->conf_sigalgslen = salglen;
3799 OPENSSL_free(sigalgs);
3803 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3807 if (default_nid == -1)
3809 sig_nid = X509_get_signature_nid(x);
3811 return sig_nid == default_nid ? 1 : 0;
3812 for (i = 0; i < c->shared_sigalgslen; i++)
3813 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3818 /* Check to see if a certificate issuer name matches list of CA names */
3819 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3823 nm = X509_get_issuer_name(x);
3824 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3825 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3832 * Check certificate chain is consistent with TLS extensions and is usable by
3833 * server. This servers two purposes: it allows users to check chains before
3834 * passing them to the server and it allows the server to check chains before
3835 * attempting to use them.
3838 /* Flags which need to be set for a certificate when stict mode not set */
3840 #define CERT_PKEY_VALID_FLAGS \
3841 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3842 /* Strict mode flags */
3843 #define CERT_PKEY_STRICT_FLAGS \
3844 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3845 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3847 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3852 int check_flags = 0, strict_mode;
3853 CERT_PKEY *cpk = NULL;
3856 unsigned int suiteb_flags = tls1_suiteb(s);
3857 /* idx == -1 means checking server chains */
3859 /* idx == -2 means checking client certificate chains */
3862 idx = cpk - c->pkeys;
3864 cpk = c->pkeys + idx;
3865 pvalid = s->s3->tmp.valid_flags + idx;
3867 pk = cpk->privatekey;
3869 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3870 /* If no cert or key, forget it */
3876 idx = ssl_cert_type(x, pk);
3879 pvalid = s->s3->tmp.valid_flags + idx;
3881 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3882 check_flags = CERT_PKEY_STRICT_FLAGS;
3884 check_flags = CERT_PKEY_VALID_FLAGS;
3891 check_flags |= CERT_PKEY_SUITEB;
3892 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3893 if (ok == X509_V_OK)
3894 rv |= CERT_PKEY_SUITEB;
3895 else if (!check_flags)
3900 * Check all signature algorithms are consistent with signature
3901 * algorithms extension if TLS 1.2 or later and strict mode.
3903 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3905 unsigned char rsign = 0;
3906 if (s->s3->tmp.peer_sigalgs)
3908 /* If no sigalgs extension use defaults from RFC5246 */
3911 case SSL_PKEY_RSA_ENC:
3912 case SSL_PKEY_RSA_SIGN:
3913 rsign = TLSEXT_signature_rsa;
3914 default_nid = NID_sha1WithRSAEncryption;
3917 case SSL_PKEY_DSA_SIGN:
3918 rsign = TLSEXT_signature_dsa;
3919 default_nid = NID_dsaWithSHA1;
3923 rsign = TLSEXT_signature_ecdsa;
3924 default_nid = NID_ecdsa_with_SHA1;
3927 case SSL_PKEY_GOST01:
3928 rsign = TLSEXT_signature_gostr34102001;
3929 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3932 case SSL_PKEY_GOST12_256:
3933 rsign = TLSEXT_signature_gostr34102012_256;
3934 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3937 case SSL_PKEY_GOST12_512:
3938 rsign = TLSEXT_signature_gostr34102012_512;
3939 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3948 * If peer sent no signature algorithms extension and we have set
3949 * preferred signature algorithms check we support sha1.
3951 if (default_nid > 0 && c->conf_sigalgs) {
3953 const unsigned char *p = c->conf_sigalgs;
3954 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3955 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3958 if (j == c->conf_sigalgslen) {
3965 /* Check signature algorithm of each cert in chain */
3966 if (!tls1_check_sig_alg(c, x, default_nid)) {
3970 rv |= CERT_PKEY_EE_SIGNATURE;
3971 rv |= CERT_PKEY_CA_SIGNATURE;
3972 for (i = 0; i < sk_X509_num(chain); i++) {
3973 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3975 rv &= ~CERT_PKEY_CA_SIGNATURE;
3982 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3983 else if (check_flags)
3984 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3986 /* Check cert parameters are consistent */
3987 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3988 rv |= CERT_PKEY_EE_PARAM;
3989 else if (!check_flags)
3992 rv |= CERT_PKEY_CA_PARAM;
3993 /* In strict mode check rest of chain too */
3994 else if (strict_mode) {
3995 rv |= CERT_PKEY_CA_PARAM;
3996 for (i = 0; i < sk_X509_num(chain); i++) {
3997 X509 *ca = sk_X509_value(chain, i);
3998 if (!tls1_check_cert_param(s, ca, 0)) {
4000 rv &= ~CERT_PKEY_CA_PARAM;
4007 if (!s->server && strict_mode) {
4008 STACK_OF(X509_NAME) *ca_dn;
4010 switch (EVP_PKEY_id(pk)) {
4012 check_type = TLS_CT_RSA_SIGN;
4015 check_type = TLS_CT_DSS_SIGN;
4018 check_type = TLS_CT_ECDSA_SIGN;
4022 const unsigned char *ctypes;
4026 ctypelen = (int)c->ctype_num;
4028 ctypes = (unsigned char *)s->s3->tmp.ctype;
4029 ctypelen = s->s3->tmp.ctype_num;
4031 for (i = 0; i < ctypelen; i++) {
4032 if (ctypes[i] == check_type) {
4033 rv |= CERT_PKEY_CERT_TYPE;
4037 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4040 rv |= CERT_PKEY_CERT_TYPE;
4042 ca_dn = s->s3->tmp.ca_names;
4044 if (!sk_X509_NAME_num(ca_dn))
4045 rv |= CERT_PKEY_ISSUER_NAME;
4047 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4048 if (ssl_check_ca_name(ca_dn, x))
4049 rv |= CERT_PKEY_ISSUER_NAME;
4051 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4052 for (i = 0; i < sk_X509_num(chain); i++) {
4053 X509 *xtmp = sk_X509_value(chain, i);
4054 if (ssl_check_ca_name(ca_dn, xtmp)) {
4055 rv |= CERT_PKEY_ISSUER_NAME;
4060 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4063 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4065 if (!check_flags || (rv & check_flags) == check_flags)
4066 rv |= CERT_PKEY_VALID;
4070 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4071 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
4072 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4073 else if (s->s3->tmp.md[idx] != NULL)
4074 rv |= CERT_PKEY_SIGN;
4076 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4079 * When checking a CERT_PKEY structure all flags are irrelevant if the
4083 if (rv & CERT_PKEY_VALID)
4086 /* Preserve explicit sign flag, clear rest */
4087 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
4094 /* Set validity of certificates in an SSL structure */
4095 void tls1_set_cert_validity(SSL *s)
4097 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4098 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4099 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4100 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4101 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
4102 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
4103 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
4106 /* User level utiity function to check a chain is suitable */
4107 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4109 return tls1_check_chain(s, x, pk, chain, -1);
4112 #ifndef OPENSSL_NO_DH
4113 DH *ssl_get_auto_dh(SSL *s)
4115 int dh_secbits = 80;
4116 if (s->cert->dh_tmp_auto == 2)
4117 return DH_get_1024_160();
4118 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
4119 if (s->s3->tmp.new_cipher->strength_bits == 256)
4124 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4125 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4128 if (dh_secbits >= 128) {
4136 if (dh_secbits >= 192)
4137 p = BN_get_rfc3526_prime_8192(NULL);
4139 p = BN_get_rfc3526_prime_3072(NULL);
4140 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4148 if (dh_secbits >= 112)
4149 return DH_get_2048_224();
4150 return DH_get_1024_160();
4154 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4157 EVP_PKEY *pkey = X509_get0_pubkey(x);
4160 * If no parameters this will return -1 and fail using the default
4161 * security callback for any non-zero security level. This will
4162 * reject keys which omit parameters but this only affects DSA and
4163 * omission of parameters is never (?) done in practice.
4165 secbits = EVP_PKEY_security_bits(pkey);
4168 return ssl_security(s, op, secbits, 0, x);
4170 return ssl_ctx_security(ctx, op, secbits, 0, x);
4173 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4175 /* Lookup signature algorithm digest */
4176 int secbits = -1, md_nid = NID_undef, sig_nid;
4177 /* Don't check signature if self signed */
4178 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4180 sig_nid = X509_get_signature_nid(x);
4181 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4183 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4184 secbits = EVP_MD_size(md) * 4;
4187 return ssl_security(s, op, secbits, md_nid, x);
4189 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4192 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4195 vfy = SSL_SECOP_PEER;
4197 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4198 return SSL_R_EE_KEY_TOO_SMALL;
4200 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4201 return SSL_R_CA_KEY_TOO_SMALL;
4203 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4204 return SSL_R_CA_MD_TOO_WEAK;
4209 * Check security of a chain, if sk includes the end entity certificate then
4210 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4211 * one to the peer. Return values: 1 if ok otherwise error code to use
4214 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4216 int rv, start_idx, i;
4218 x = sk_X509_value(sk, 0);
4223 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4227 for (i = start_idx; i < sk_X509_num(sk); i++) {
4228 x = sk_X509_value(sk, i);
4229 rv = ssl_security_cert(s, NULL, x, vfy, 0);