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/rand.h>
19 #include <openssl/dh.h>
20 #include <openssl/bn.h>
22 #include <openssl/ct.h>
24 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
25 const unsigned char *sess_id, int sesslen,
27 static int ssl_check_clienthello_tlsext_early(SSL *s);
28 static int ssl_check_serverhello_tlsext(SSL *s);
30 SSL3_ENC_METHOD const TLSv1_enc_data = {
34 tls1_generate_master_secret,
35 tls1_change_cipher_state,
36 tls1_final_finish_mac,
37 TLS1_FINISH_MAC_LENGTH,
38 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
39 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
41 tls1_export_keying_material,
43 SSL3_HM_HEADER_LENGTH,
44 ssl3_set_handshake_header,
48 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
52 tls1_generate_master_secret,
53 tls1_change_cipher_state,
54 tls1_final_finish_mac,
55 TLS1_FINISH_MAC_LENGTH,
56 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
57 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
59 tls1_export_keying_material,
60 SSL_ENC_FLAG_EXPLICIT_IV,
61 SSL3_HM_HEADER_LENGTH,
62 ssl3_set_handshake_header,
66 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
70 tls1_generate_master_secret,
71 tls1_change_cipher_state,
72 tls1_final_finish_mac,
73 TLS1_FINISH_MAC_LENGTH,
74 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
75 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
77 tls1_export_keying_material,
78 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
79 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
80 SSL3_HM_HEADER_LENGTH,
81 ssl3_set_handshake_header,
85 long tls1_default_timeout(void)
88 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
89 * http, the cache would over fill
98 s->method->ssl_clear(s);
102 void tls1_free(SSL *s)
104 OPENSSL_free(s->tlsext_session_ticket);
108 void tls1_clear(SSL *s)
111 if (s->method->version == TLS_ANY_VERSION)
112 s->version = TLS_MAX_VERSION;
114 s->version = s->method->version;
117 #ifndef OPENSSL_NO_EC
120 int nid; /* Curve NID */
121 int secbits; /* Bits of security (from SP800-57) */
122 unsigned int flags; /* Flags: currently just field type */
125 /* Mask for curve type */
126 # define TLS_CURVE_TYPE 0x3
127 # define TLS_CURVE_PRIME 0x0
128 # define TLS_CURVE_CHAR2 0x1
129 # define TLS_CURVE_CUSTOM 0x2
132 * Table of curve information.
133 * Do not delete entries or reorder this array! It is used as a lookup
134 * table: the index of each entry is one less than the TLS curve id.
136 static const tls_curve_info nid_list[] = {
137 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
138 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
139 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
140 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
141 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
142 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
143 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
144 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
145 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
146 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
147 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
148 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
149 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
150 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
151 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
152 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
153 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
154 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
155 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
156 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
157 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
158 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
159 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
160 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
161 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
162 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
163 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
164 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
166 {NID_X25519, 128, TLS_CURVE_CUSTOM},
169 static const unsigned char ecformats_default[] = {
170 TLSEXT_ECPOINTFORMAT_uncompressed,
171 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
172 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
175 /* The default curves */
176 static const unsigned char eccurves_default[] = {
177 0, 29, /* X25519 (29) */
178 0, 23, /* secp256r1 (23) */
179 0, 25, /* secp521r1 (25) */
180 0, 24, /* secp384r1 (24) */
183 static const unsigned char eccurves_all[] = {
184 0, 29, /* X25519 (29) */
185 0, 23, /* secp256r1 (23) */
186 0, 25, /* secp521r1 (25) */
187 0, 24, /* secp384r1 (24) */
188 0, 26, /* brainpoolP256r1 (26) */
189 0, 27, /* brainpoolP384r1 (27) */
190 0, 28, /* brainpool512r1 (28) */
193 * Remaining curves disabled by default but still permitted if set
194 * via an explicit callback or parameters.
196 0, 22, /* secp256k1 (22) */
197 0, 14, /* sect571r1 (14) */
198 0, 13, /* sect571k1 (13) */
199 0, 11, /* sect409k1 (11) */
200 0, 12, /* sect409r1 (12) */
201 0, 9, /* sect283k1 (9) */
202 0, 10, /* sect283r1 (10) */
203 0, 20, /* secp224k1 (20) */
204 0, 21, /* secp224r1 (21) */
205 0, 18, /* secp192k1 (18) */
206 0, 19, /* secp192r1 (19) */
207 0, 15, /* secp160k1 (15) */
208 0, 16, /* secp160r1 (16) */
209 0, 17, /* secp160r2 (17) */
210 0, 8, /* sect239k1 (8) */
211 0, 6, /* sect233k1 (6) */
212 0, 7, /* sect233r1 (7) */
213 0, 4, /* sect193r1 (4) */
214 0, 5, /* sect193r2 (5) */
215 0, 1, /* sect163k1 (1) */
216 0, 2, /* sect163r1 (2) */
217 0, 3, /* sect163r2 (3) */
221 static const unsigned char suiteb_curves[] = {
222 0, TLSEXT_curve_P_256,
223 0, TLSEXT_curve_P_384
226 int tls1_ec_curve_id2nid(int curve_id)
228 /* ECC curves from RFC 4492 and RFC 7027 */
229 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
231 return nid_list[curve_id - 1].nid;
234 int tls1_ec_nid2curve_id(int nid)
237 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
238 if (nid_list[i].nid == nid)
245 * Get curves list, if "sess" is set return client curves otherwise
247 * Sets |num_curves| to the number of curves in the list, i.e.,
248 * the length of |pcurves| is 2 * num_curves.
249 * Returns 1 on success and 0 if the client curves list has invalid format.
250 * The latter indicates an internal error: we should not be accepting such
251 * lists in the first place.
252 * TODO(emilia): we should really be storing the curves list in explicitly
253 * parsed form instead. (However, this would affect binary compatibility
254 * so cannot happen in the 1.0.x series.)
256 static int tls1_get_curvelist(SSL *s, int sess,
257 const unsigned char **pcurves,
260 size_t pcurveslen = 0;
262 *pcurves = s->session->tlsext_ellipticcurvelist;
263 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
265 /* For Suite B mode only include P-256, P-384 */
266 switch (tls1_suiteb(s)) {
267 case SSL_CERT_FLAG_SUITEB_128_LOS:
268 *pcurves = suiteb_curves;
269 pcurveslen = sizeof(suiteb_curves);
272 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
273 *pcurves = suiteb_curves;
277 case SSL_CERT_FLAG_SUITEB_192_LOS:
278 *pcurves = suiteb_curves + 2;
282 *pcurves = s->tlsext_ellipticcurvelist;
283 pcurveslen = s->tlsext_ellipticcurvelist_length;
286 *pcurves = eccurves_default;
287 pcurveslen = sizeof(eccurves_default);
291 /* We do not allow odd length arrays to enter the system. */
292 if (pcurveslen & 1) {
293 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
297 *num_curves = pcurveslen / 2;
302 /* See if curve is allowed by security callback */
303 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
305 const tls_curve_info *cinfo;
308 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
310 cinfo = &nid_list[curve[1] - 1];
311 # ifdef OPENSSL_NO_EC2M
312 if (cinfo->flags & TLS_CURVE_CHAR2)
315 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
318 /* Check a curve is one of our preferences */
319 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
321 const unsigned char *curves;
322 size_t num_curves, i;
323 unsigned int suiteb_flags = tls1_suiteb(s);
324 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
326 /* Check curve matches Suite B preferences */
328 unsigned long cid = s->s3->tmp.new_cipher->id;
331 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
332 if (p[2] != TLSEXT_curve_P_256)
334 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
335 if (p[2] != TLSEXT_curve_P_384)
337 } else /* Should never happen */
340 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
342 for (i = 0; i < num_curves; i++, curves += 2) {
343 if (p[1] == curves[0] && p[2] == curves[1])
344 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
350 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
351 * if there is no match.
352 * For nmatch == -1, return number of matches
353 * For nmatch == -2, return the NID of the curve to use for
354 * an EC tmp key, or NID_undef if there is no match.
356 int tls1_shared_curve(SSL *s, int nmatch)
358 const unsigned char *pref, *supp;
359 size_t num_pref, num_supp, i, j;
361 /* Can't do anything on client side */
365 if (tls1_suiteb(s)) {
367 * For Suite B ciphersuite determines curve: we already know
368 * these are acceptable due to previous checks.
370 unsigned long cid = s->s3->tmp.new_cipher->id;
371 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
372 return NID_X9_62_prime256v1; /* P-256 */
373 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
374 return NID_secp384r1; /* P-384 */
375 /* Should never happen */
378 /* If not Suite B just return first preference shared curve */
382 * Avoid truncation. tls1_get_curvelist takes an int
383 * but s->options is a long...
385 if (!tls1_get_curvelist
386 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
388 /* In practice, NID_undef == 0 but let's be precise. */
389 return nmatch == -1 ? 0 : NID_undef;
390 if (!tls1_get_curvelist
391 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
393 return nmatch == -1 ? 0 : NID_undef;
396 * If the client didn't send the elliptic_curves extension all of them
399 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
401 num_supp = sizeof(eccurves_all) / 2;
402 } else if (num_pref == 0 &&
403 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
405 num_pref = sizeof(eccurves_all) / 2;
409 for (i = 0; i < num_pref; i++, pref += 2) {
410 const unsigned char *tsupp = supp;
411 for (j = 0; j < num_supp; j++, tsupp += 2) {
412 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
413 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
416 int id = (pref[0] << 8) | pref[1];
417 return tls1_ec_curve_id2nid(id);
425 /* Out of range (nmatch > k). */
429 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
430 int *curves, size_t ncurves)
432 unsigned char *clist, *p;
435 * Bitmap of curves included to detect duplicates: only works while curve
438 unsigned long dup_list = 0;
439 clist = OPENSSL_malloc(ncurves * 2);
442 for (i = 0, p = clist; i < ncurves; i++) {
443 unsigned long idmask;
445 id = tls1_ec_nid2curve_id(curves[i]);
447 if (!id || (dup_list & idmask)) {
456 *pextlen = ncurves * 2;
460 # define MAX_CURVELIST 28
464 int nid_arr[MAX_CURVELIST];
467 static int nid_cb(const char *elem, int len, void *arg)
469 nid_cb_st *narg = arg;
475 if (narg->nidcnt == MAX_CURVELIST)
477 if (len > (int)(sizeof(etmp) - 1))
479 memcpy(etmp, elem, len);
481 nid = EC_curve_nist2nid(etmp);
482 if (nid == NID_undef)
483 nid = OBJ_sn2nid(etmp);
484 if (nid == NID_undef)
485 nid = OBJ_ln2nid(etmp);
486 if (nid == NID_undef)
488 for (i = 0; i < narg->nidcnt; i++)
489 if (narg->nid_arr[i] == nid)
491 narg->nid_arr[narg->nidcnt++] = nid;
495 /* Set curves based on a colon separate list */
496 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
501 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
505 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
508 /* For an EC key set TLS id and required compression based on parameters */
509 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
516 /* Determine if it is a prime field */
517 grp = EC_KEY_get0_group(ec);
520 /* Determine curve ID */
521 id = EC_GROUP_get_curve_name(grp);
522 id = tls1_ec_nid2curve_id(id);
523 /* If no id return error: we don't support arbitrary explicit curves */
527 curve_id[1] = (unsigned char)id;
529 if (EC_KEY_get0_public_key(ec) == NULL)
531 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
532 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
534 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
535 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
537 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
543 /* Check an EC key is compatible with extensions */
544 static int tls1_check_ec_key(SSL *s,
545 unsigned char *curve_id, unsigned char *comp_id)
547 const unsigned char *pformats, *pcurves;
548 size_t num_formats, num_curves, i;
551 * If point formats extension present check it, otherwise everything is
552 * supported (see RFC4492).
554 if (comp_id && s->session->tlsext_ecpointformatlist) {
555 pformats = s->session->tlsext_ecpointformatlist;
556 num_formats = s->session->tlsext_ecpointformatlist_length;
557 for (i = 0; i < num_formats; i++, pformats++) {
558 if (*comp_id == *pformats)
561 if (i == num_formats)
566 /* Check curve is consistent with client and server preferences */
567 for (j = 0; j <= 1; j++) {
568 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
570 if (j == 1 && num_curves == 0) {
572 * If we've not received any curves then skip this check.
573 * RFC 4492 does not require the supported elliptic curves extension
574 * so if it is not sent we can just choose any curve.
575 * It is invalid to send an empty list in the elliptic curves
576 * extension, so num_curves == 0 always means no extension.
580 for (i = 0; i < num_curves; i++, pcurves += 2) {
581 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
586 /* For clients can only check sent curve list */
593 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
597 * If we have a custom point format list use it otherwise use default
599 if (s->tlsext_ecpointformatlist) {
600 *pformats = s->tlsext_ecpointformatlist;
601 *num_formats = s->tlsext_ecpointformatlist_length;
603 *pformats = ecformats_default;
604 /* For Suite B we don't support char2 fields */
606 *num_formats = sizeof(ecformats_default) - 1;
608 *num_formats = sizeof(ecformats_default);
613 * Check cert parameters compatible with extensions: currently just checks EC
614 * certificates have compatible curves and compression.
616 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
618 unsigned char comp_id, curve_id[2];
621 pkey = X509_get0_pubkey(x);
624 /* If not EC nothing to do */
625 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
627 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
631 * Can't check curve_id for client certs as we don't have a supported
634 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
638 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
639 * SHA384+P-384, adjust digest if necessary.
641 if (set_ee_md && tls1_suiteb(s)) {
647 /* Check to see we have necessary signing algorithm */
648 if (curve_id[1] == TLSEXT_curve_P_256)
649 check_md = NID_ecdsa_with_SHA256;
650 else if (curve_id[1] == TLSEXT_curve_P_384)
651 check_md = NID_ecdsa_with_SHA384;
653 return 0; /* Should never happen */
654 for (i = 0; i < c->shared_sigalgslen; i++)
655 if (check_md == c->shared_sigalgs[i].signandhash_nid)
657 if (i == c->shared_sigalgslen)
659 if (set_ee_md == 2) {
660 if (check_md == NID_ecdsa_with_SHA256)
661 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
663 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
669 # ifndef OPENSSL_NO_EC
671 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
673 * @cid: Cipher ID we're considering using
675 * Checks that the kECDHE cipher suite we're considering using
676 * is compatible with the client extensions.
678 * Returns 0 when the cipher can't be used or 1 when it can.
680 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
683 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
686 if (tls1_suiteb(s)) {
687 unsigned char curve_id[2];
688 /* Curve to check determined by ciphersuite */
689 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
690 curve_id[1] = TLSEXT_curve_P_256;
691 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
692 curve_id[1] = TLSEXT_curve_P_384;
696 /* Check this curve is acceptable */
697 if (!tls1_check_ec_key(s, curve_id, NULL))
701 /* Need a shared curve */
702 if (tls1_shared_curve(s, 0))
706 # endif /* OPENSSL_NO_EC */
710 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
715 #endif /* OPENSSL_NO_EC */
718 * List of supported signature algorithms and hashes. Should make this
719 * customisable at some point, for now include everything we support.
722 #ifdef OPENSSL_NO_RSA
723 # define tlsext_sigalg_rsa(md) /* */
725 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
728 #ifdef OPENSSL_NO_DSA
729 # define tlsext_sigalg_dsa(md) /* */
731 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
735 # define tlsext_sigalg_ecdsa(md) /* */
737 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
740 #define tlsext_sigalg(md) \
741 tlsext_sigalg_rsa(md) \
742 tlsext_sigalg_dsa(md) \
743 tlsext_sigalg_ecdsa(md)
745 static const unsigned char tls12_sigalgs[] = {
746 tlsext_sigalg(TLSEXT_hash_sha512)
747 tlsext_sigalg(TLSEXT_hash_sha384)
748 tlsext_sigalg(TLSEXT_hash_sha256)
749 tlsext_sigalg(TLSEXT_hash_sha224)
750 tlsext_sigalg(TLSEXT_hash_sha1)
751 #ifndef OPENSSL_NO_GOST
752 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
753 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
754 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
758 #ifndef OPENSSL_NO_EC
759 static const unsigned char suiteb_sigalgs[] = {
760 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
761 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
764 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
767 * If Suite B mode use Suite B sigalgs only, ignore any other
770 #ifndef OPENSSL_NO_EC
771 switch (tls1_suiteb(s)) {
772 case SSL_CERT_FLAG_SUITEB_128_LOS:
773 *psigs = suiteb_sigalgs;
774 return sizeof(suiteb_sigalgs);
776 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
777 *psigs = suiteb_sigalgs;
780 case SSL_CERT_FLAG_SUITEB_192_LOS:
781 *psigs = suiteb_sigalgs + 2;
785 /* If server use client authentication sigalgs if not NULL */
786 if (s->server && s->cert->client_sigalgs) {
787 *psigs = s->cert->client_sigalgs;
788 return s->cert->client_sigalgslen;
789 } else if (s->cert->conf_sigalgs) {
790 *psigs = s->cert->conf_sigalgs;
791 return s->cert->conf_sigalgslen;
793 *psigs = tls12_sigalgs;
794 return sizeof(tls12_sigalgs);
799 * Check signature algorithm is consistent with sent supported signature
800 * algorithms and if so return relevant digest.
802 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
803 const unsigned char *sig, EVP_PKEY *pkey)
805 const unsigned char *sent_sigs;
806 size_t sent_sigslen, i;
807 int sigalg = tls12_get_sigid(pkey);
808 /* Should never happen */
811 /* Check key type is consistent with signature */
812 if (sigalg != (int)sig[1]) {
813 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
816 #ifndef OPENSSL_NO_EC
817 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
818 unsigned char curve_id[2], comp_id;
819 /* Check compression and curve matches extensions */
820 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
822 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
823 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
826 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
827 if (tls1_suiteb(s)) {
830 if (curve_id[1] == TLSEXT_curve_P_256) {
831 if (sig[0] != TLSEXT_hash_sha256) {
832 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
833 SSL_R_ILLEGAL_SUITEB_DIGEST);
836 } else if (curve_id[1] == TLSEXT_curve_P_384) {
837 if (sig[0] != TLSEXT_hash_sha384) {
838 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
839 SSL_R_ILLEGAL_SUITEB_DIGEST);
845 } else if (tls1_suiteb(s))
849 /* Check signature matches a type we sent */
850 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
851 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
852 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
855 /* Allow fallback to SHA1 if not strict mode */
856 if (i == sent_sigslen
857 && (sig[0] != TLSEXT_hash_sha1
858 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
859 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
862 *pmd = tls12_get_hash(sig[0]);
864 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
867 /* Make sure security callback allows algorithm */
868 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
869 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
871 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
875 * Store the digest used so applications can retrieve it if they wish.
877 s->s3->tmp.peer_md = *pmd;
882 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
883 * supported, doesn't appear in supported signature algorithms, isn't supported
884 * by the enabled protocol versions or by the security level.
886 * This function should only be used for checking which ciphers are supported
889 * Call ssl_cipher_disabled() to check that it's enabled or not.
891 void ssl_set_client_disabled(SSL *s)
893 s->s3->tmp.mask_a = 0;
894 s->s3->tmp.mask_k = 0;
895 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
896 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
897 # ifndef OPENSSL_NO_PSK
898 /* with PSK there must be client callback set */
899 if (!s->psk_client_callback) {
900 s->s3->tmp.mask_a |= SSL_aPSK;
901 s->s3->tmp.mask_k |= SSL_PSK;
903 #endif /* OPENSSL_NO_PSK */
904 #ifndef OPENSSL_NO_SRP
905 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
906 s->s3->tmp.mask_a |= SSL_aSRP;
907 s->s3->tmp.mask_k |= SSL_kSRP;
913 * ssl_cipher_disabled - check that a cipher is disabled or not
914 * @s: SSL connection that you want to use the cipher on
915 * @c: cipher to check
916 * @op: Security check that you want to do
918 * Returns 1 when it's disabled, 0 when enabled.
920 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
922 if (c->algorithm_mkey & s->s3->tmp.mask_k
923 || c->algorithm_auth & s->s3->tmp.mask_a)
925 if (s->s3->tmp.max_ver == 0)
927 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
928 || (c->max_tls < s->s3->tmp.min_ver)))
930 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
931 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
934 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
937 static int tls_use_ticket(SSL *s)
939 if (s->options & SSL_OP_NO_TICKET)
941 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
944 static int compare_uint(const void *p1, const void *p2) {
945 unsigned int u1 = *((const unsigned int *)p1);
946 unsigned int u2 = *((const unsigned int *)p2);
956 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
957 * more than one extension of the same type in a ClientHello or ServerHello.
958 * This function does an initial scan over the extensions block to filter those
959 * out. It returns 1 if all extensions are unique, and 0 if the extensions
960 * contain duplicates, could not be successfully parsed, or an internal error
963 static int tls1_check_duplicate_extensions(const PACKET *packet) {
964 PACKET extensions = *packet;
965 size_t num_extensions = 0, i = 0;
966 unsigned int *extension_types = NULL;
969 /* First pass: count the extensions. */
970 while (PACKET_remaining(&extensions) > 0) {
973 if (!PACKET_get_net_2(&extensions, &type) ||
974 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
980 if (num_extensions <= 1)
983 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
984 if (extension_types == NULL) {
985 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
989 /* Second pass: gather the extension types. */
990 extensions = *packet;
991 for (i = 0; i < num_extensions; i++) {
993 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
994 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
995 /* This should not happen. */
996 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1001 if (PACKET_remaining(&extensions) != 0) {
1002 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1005 /* Sort the extensions and make sure there are no duplicates. */
1006 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1007 for (i = 1; i < num_extensions; i++) {
1008 if (extension_types[i - 1] == extension_types[i])
1013 OPENSSL_free(extension_types);
1017 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1018 unsigned char *limit, int *al)
1021 unsigned char *orig = buf;
1022 unsigned char *ret = buf;
1023 #ifndef OPENSSL_NO_EC
1024 /* See if we support any ECC ciphersuites */
1026 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1028 unsigned long alg_k, alg_a;
1029 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1031 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1032 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1034 alg_k = c->algorithm_mkey;
1035 alg_a = c->algorithm_auth;
1036 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1037 || (alg_a & SSL_aECDSA)) {
1048 return NULL; /* this really never occurs, but ... */
1050 /* Add RI if renegotiating */
1051 if (s->renegotiate) {
1054 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1055 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1059 if ((limit - ret - 4 - el) < 0)
1062 s2n(TLSEXT_TYPE_renegotiate, ret);
1065 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1066 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1072 /* Only add RI for SSLv3 */
1073 if (s->client_version == SSL3_VERSION)
1076 if (s->tlsext_hostname != NULL) {
1077 /* Add TLS extension servername to the Client Hello message */
1078 unsigned long size_str;
1082 * check for enough space.
1083 * 4 for the servername type and extension length
1084 * 2 for servernamelist length
1085 * 1 for the hostname type
1086 * 2 for hostname length
1090 if ((lenmax = limit - ret - 9) < 0
1092 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1095 /* extension type and length */
1096 s2n(TLSEXT_TYPE_server_name, ret);
1097 s2n(size_str + 5, ret);
1099 /* length of servername list */
1100 s2n(size_str + 3, ret);
1102 /* hostname type, length and hostname */
1103 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1105 memcpy(ret, s->tlsext_hostname, size_str);
1108 #ifndef OPENSSL_NO_SRP
1109 /* Add SRP username if there is one */
1110 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1111 * Client Hello message */
1113 int login_len = strlen(s->srp_ctx.login);
1114 if (login_len > 255 || login_len == 0) {
1115 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1120 * check for enough space.
1121 * 4 for the srp type type and extension length
1122 * 1 for the srp user identity
1123 * + srp user identity length
1125 if ((limit - ret - 5 - login_len) < 0)
1128 /* fill in the extension */
1129 s2n(TLSEXT_TYPE_srp, ret);
1130 s2n(login_len + 1, ret);
1131 (*ret++) = (unsigned char)login_len;
1132 memcpy(ret, s->srp_ctx.login, login_len);
1137 #ifndef OPENSSL_NO_EC
1140 * Add TLS extension ECPointFormats to the ClientHello message
1143 const unsigned char *pcurves, *pformats;
1144 size_t num_curves, num_formats, curves_list_len;
1146 unsigned char *etmp;
1148 tls1_get_formatlist(s, &pformats, &num_formats);
1150 if ((lenmax = limit - ret - 5) < 0)
1152 if (num_formats > (size_t)lenmax)
1154 if (num_formats > 255) {
1155 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1159 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1160 /* The point format list has 1-byte length. */
1161 s2n(num_formats + 1, ret);
1162 *(ret++) = (unsigned char)num_formats;
1163 memcpy(ret, pformats, num_formats);
1167 * Add TLS extension EllipticCurves to the ClientHello message
1169 pcurves = s->tlsext_ellipticcurvelist;
1170 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1173 if ((lenmax = limit - ret - 6) < 0)
1175 if (num_curves > (size_t)lenmax / 2)
1177 if (num_curves > 65532 / 2) {
1178 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1182 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1184 /* Copy curve ID if supported */
1185 for (i = 0; i < num_curves; i++, pcurves += 2) {
1186 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1187 *etmp++ = pcurves[0];
1188 *etmp++ = pcurves[1];
1192 curves_list_len = etmp - ret - 4;
1194 s2n(curves_list_len + 2, ret);
1195 s2n(curves_list_len, ret);
1196 ret += curves_list_len;
1198 #endif /* OPENSSL_NO_EC */
1200 if (tls_use_ticket(s)) {
1202 if (!s->new_session && s->session && s->session->tlsext_tick)
1203 ticklen = s->session->tlsext_ticklen;
1204 else if (s->session && s->tlsext_session_ticket &&
1205 s->tlsext_session_ticket->data) {
1206 ticklen = s->tlsext_session_ticket->length;
1207 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1208 if (s->session->tlsext_tick == NULL)
1210 memcpy(s->session->tlsext_tick,
1211 s->tlsext_session_ticket->data, ticklen);
1212 s->session->tlsext_ticklen = ticklen;
1215 if (ticklen == 0 && s->tlsext_session_ticket &&
1216 s->tlsext_session_ticket->data == NULL)
1219 * Check for enough room 2 for extension type, 2 for len rest for
1222 if ((long)(limit - ret - 4 - ticklen) < 0)
1224 s2n(TLSEXT_TYPE_session_ticket, ret);
1227 memcpy(ret, s->session->tlsext_tick, ticklen);
1233 if (SSL_CLIENT_USE_SIGALGS(s)) {
1235 const unsigned char *salg;
1236 unsigned char *etmp;
1237 salglen = tls12_get_psigalgs(s, &salg);
1238 if ((size_t)(limit - ret) < salglen + 6)
1240 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1242 /* Skip over lengths for now */
1244 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1245 /* Fill in lengths */
1246 s2n(salglen + 2, etmp);
1251 #ifndef OPENSSL_NO_OCSP
1252 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1254 long extlen, idlen, itmp;
1258 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1259 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1260 itmp = i2d_OCSP_RESPID(id, NULL);
1266 if (s->tlsext_ocsp_exts) {
1267 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1273 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1275 s2n(TLSEXT_TYPE_status_request, ret);
1276 if (extlen + idlen > 0xFFF0)
1278 s2n(extlen + idlen + 5, ret);
1279 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1281 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1282 /* save position of id len */
1283 unsigned char *q = ret;
1284 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1285 /* skip over id len */
1287 itmp = i2d_OCSP_RESPID(id, &ret);
1293 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1296 #ifndef OPENSSL_NO_HEARTBEATS
1297 if (SSL_IS_DTLS(s)) {
1298 /* Add Heartbeat extension */
1299 if ((limit - ret - 4 - 1) < 0)
1301 s2n(TLSEXT_TYPE_heartbeat, ret);
1305 * 1: peer may send requests
1306 * 2: peer not allowed to send requests
1308 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1309 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1311 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1315 #ifndef OPENSSL_NO_NEXTPROTONEG
1316 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1318 * The client advertises an empty extension to indicate its support
1319 * for Next Protocol Negotiation
1321 if (limit - ret - 4 < 0)
1323 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1329 * finish_md_len is non-zero during a renegotiation, so
1330 * this avoids sending ALPN during the renegotiation
1331 * (see longer comment below)
1333 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1334 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1336 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1337 s2n(2 + s->alpn_client_proto_list_len, ret);
1338 s2n(s->alpn_client_proto_list_len, ret);
1339 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1340 ret += s->alpn_client_proto_list_len;
1341 s->s3->alpn_sent = 1;
1343 #ifndef OPENSSL_NO_SRTP
1344 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1347 /* Returns 0 on success!! */
1348 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1349 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1353 if ((limit - ret - 4 - el) < 0)
1356 s2n(TLSEXT_TYPE_use_srtp, ret);
1359 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1360 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1366 custom_ext_init(&s->cert->cli_ext);
1367 /* Add custom TLS Extensions to ClientHello */
1368 if (!custom_ext_add(s, 0, &ret, limit, al))
1370 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1372 #ifndef OPENSSL_NO_CT
1373 if (s->ct_validation_callback != NULL) {
1374 s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret);
1378 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1382 * Add padding to workaround bugs in F5 terminators. See
1383 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1384 * code works out the length of all existing extensions it MUST always
1387 if (s->options & SSL_OP_TLSEXT_PADDING) {
1388 int hlen = ret - (unsigned char *)s->init_buf->data;
1390 if (hlen > 0xff && hlen < 0x200) {
1391 hlen = 0x200 - hlen;
1397 s2n(TLSEXT_TYPE_padding, ret);
1399 memset(ret, 0, hlen);
1406 if ((extdatalen = ret - orig - 2) == 0)
1409 s2n(extdatalen, orig);
1413 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1414 unsigned char *limit, int *al)
1417 unsigned char *orig = buf;
1418 unsigned char *ret = buf;
1419 #ifndef OPENSSL_NO_NEXTPROTONEG
1420 int next_proto_neg_seen;
1422 #ifndef OPENSSL_NO_EC
1423 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1424 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1425 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1426 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1431 return NULL; /* this really never occurs, but ... */
1433 if (s->s3->send_connection_binding) {
1436 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1437 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1441 if ((limit - ret - 4 - el) < 0)
1444 s2n(TLSEXT_TYPE_renegotiate, ret);
1447 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1448 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1455 /* Only add RI for SSLv3 */
1456 if (s->version == SSL3_VERSION)
1459 if (!s->hit && s->servername_done == 1
1460 && s->session->tlsext_hostname != NULL) {
1461 if ((long)(limit - ret - 4) < 0)
1464 s2n(TLSEXT_TYPE_server_name, ret);
1467 #ifndef OPENSSL_NO_EC
1469 const unsigned char *plist;
1472 * Add TLS extension ECPointFormats to the ServerHello message
1476 tls1_get_formatlist(s, &plist, &plistlen);
1478 if ((lenmax = limit - ret - 5) < 0)
1480 if (plistlen > (size_t)lenmax)
1482 if (plistlen > 255) {
1483 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1487 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1488 s2n(plistlen + 1, ret);
1489 *(ret++) = (unsigned char)plistlen;
1490 memcpy(ret, plist, plistlen);
1495 * Currently the server should not respond with a SupportedCurves
1498 #endif /* OPENSSL_NO_EC */
1500 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1501 if ((long)(limit - ret - 4) < 0)
1503 s2n(TLSEXT_TYPE_session_ticket, ret);
1507 if (s->tlsext_status_expected) {
1508 if ((long)(limit - ret - 4) < 0)
1510 s2n(TLSEXT_TYPE_status_request, ret);
1514 #ifndef OPENSSL_NO_SRTP
1515 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1518 /* Returns 0 on success!! */
1519 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1520 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1523 if ((limit - ret - 4 - el) < 0)
1526 s2n(TLSEXT_TYPE_use_srtp, ret);
1529 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1530 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1537 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1538 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1539 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1540 const unsigned char cryptopro_ext[36] = {
1541 0xfd, 0xe8, /* 65000 */
1542 0x00, 0x20, /* 32 bytes length */
1543 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1544 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1545 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1546 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1548 if (limit - ret < 36)
1550 memcpy(ret, cryptopro_ext, 36);
1554 #ifndef OPENSSL_NO_HEARTBEATS
1555 /* Add Heartbeat extension if we've received one */
1556 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1557 if ((limit - ret - 4 - 1) < 0)
1559 s2n(TLSEXT_TYPE_heartbeat, ret);
1563 * 1: peer may send requests
1564 * 2: peer not allowed to send requests
1566 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1567 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1569 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1574 #ifndef OPENSSL_NO_NEXTPROTONEG
1575 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1576 s->s3->next_proto_neg_seen = 0;
1577 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1578 const unsigned char *npa;
1579 unsigned int npalen;
1582 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1584 ctx->next_protos_advertised_cb_arg);
1585 if (r == SSL_TLSEXT_ERR_OK) {
1586 if ((long)(limit - ret - 4 - npalen) < 0)
1588 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1590 memcpy(ret, npa, npalen);
1592 s->s3->next_proto_neg_seen = 1;
1596 if (!custom_ext_add(s, 1, &ret, limit, al))
1598 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1600 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1601 * for other cases too.
1603 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1604 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1605 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1606 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1607 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1609 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1613 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1614 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1618 if (s->s3->alpn_selected != NULL) {
1619 const unsigned char *selected = s->s3->alpn_selected;
1620 unsigned int len = s->s3->alpn_selected_len;
1622 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1624 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1628 memcpy(ret, selected, len);
1634 if ((extdatalen = ret - orig - 2) == 0)
1637 s2n(extdatalen, orig);
1642 * Save the ALPN extension in a ClientHello.
1643 * pkt: the contents of the ALPN extension, not including type and length.
1644 * al: a pointer to the alert value to send in the event of a failure.
1645 * returns: 1 on success, 0 on error.
1647 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1649 PACKET protocol_list, save_protocol_list, protocol;
1651 *al = SSL_AD_DECODE_ERROR;
1653 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1654 || PACKET_remaining(&protocol_list) < 2) {
1658 save_protocol_list = protocol_list;
1660 /* Protocol names can't be empty. */
1661 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1662 || PACKET_remaining(&protocol) == 0) {
1665 } while (PACKET_remaining(&protocol_list) != 0);
1667 if (!PACKET_memdup(&save_protocol_list,
1668 &s->s3->alpn_proposed,
1669 &s->s3->alpn_proposed_len)) {
1670 *al = TLS1_AD_INTERNAL_ERROR;
1678 * Process the ALPN extension in a ClientHello.
1679 * ret: a pointer to the TLSEXT return value: SSL_TLSEXT_ERR_*
1680 * al: a pointer to the alert value to send in the event of a failure.
1681 * returns 1 on success, 0
1683 static int tls1_alpn_handle_client_hello_late(SSL *s, int *ret, int *al)
1685 const unsigned char *selected = NULL;
1686 unsigned char selected_len = 0;
1688 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1689 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1690 s->s3->alpn_proposed,
1691 s->s3->alpn_proposed_len,
1692 s->ctx->alpn_select_cb_arg);
1694 if (r == SSL_TLSEXT_ERR_OK) {
1695 OPENSSL_free(s->s3->alpn_selected);
1696 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1697 if (s->s3->alpn_selected == NULL) {
1698 *al = SSL_AD_INTERNAL_ERROR;
1699 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1702 s->s3->alpn_selected_len = selected_len;
1703 #ifndef OPENSSL_NO_NEXTPROTONEG
1704 /* ALPN takes precedence over NPN. */
1705 s->s3->next_proto_neg_seen = 0;
1708 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1709 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1717 #ifndef OPENSSL_NO_EC
1719 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1720 * SecureTransport using the TLS extension block in |pkt|.
1721 * Safari, since 10.6, sends exactly these extensions, in this order:
1726 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1727 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1728 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1729 * 10.8..10.8.3 (which don't work).
1731 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1737 static const unsigned char kSafariExtensionsBlock[] = {
1738 0x00, 0x0a, /* elliptic_curves extension */
1739 0x00, 0x08, /* 8 bytes */
1740 0x00, 0x06, /* 6 bytes of curve ids */
1741 0x00, 0x17, /* P-256 */
1742 0x00, 0x18, /* P-384 */
1743 0x00, 0x19, /* P-521 */
1745 0x00, 0x0b, /* ec_point_formats */
1746 0x00, 0x02, /* 2 bytes */
1747 0x01, /* 1 point format */
1748 0x00, /* uncompressed */
1749 /* The following is only present in TLS 1.2 */
1750 0x00, 0x0d, /* signature_algorithms */
1751 0x00, 0x0c, /* 12 bytes */
1752 0x00, 0x0a, /* 10 bytes */
1753 0x05, 0x01, /* SHA-384/RSA */
1754 0x04, 0x01, /* SHA-256/RSA */
1755 0x02, 0x01, /* SHA-1/RSA */
1756 0x04, 0x03, /* SHA-256/ECDSA */
1757 0x02, 0x03, /* SHA-1/ECDSA */
1760 /* Length of the common prefix (first two extensions). */
1761 static const size_t kSafariCommonExtensionsLength = 18;
1765 if (!PACKET_forward(&tmppkt, 2)
1766 || !PACKET_get_net_2(&tmppkt, &type)
1767 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1771 if (type != TLSEXT_TYPE_server_name)
1774 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1775 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1777 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1780 #endif /* !OPENSSL_NO_EC */
1783 * Parse ClientHello extensions and stash extension info in various parts of
1784 * the SSL object. Verify that there are no duplicate extensions.
1786 * Behaviour upon resumption is extension-specific. If the extension has no
1787 * effect during resumption, it is parsed (to verify its format) but otherwise
1790 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1791 * Upon failure, sets |al| to the appropriate alert.
1793 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1796 int renegotiate_seen = 0;
1799 *al = SSL_AD_DECODE_ERROR;
1800 s->servername_done = 0;
1801 s->tlsext_status_type = -1;
1802 #ifndef OPENSSL_NO_NEXTPROTONEG
1803 s->s3->next_proto_neg_seen = 0;
1806 OPENSSL_free(s->s3->alpn_selected);
1807 s->s3->alpn_selected = NULL;
1808 s->s3->alpn_selected_len = 0;
1809 OPENSSL_free(s->s3->alpn_proposed);
1810 s->s3->alpn_proposed = NULL;
1811 s->s3->alpn_proposed_len = 0;
1812 #ifndef OPENSSL_NO_HEARTBEATS
1813 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1814 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1817 #ifndef OPENSSL_NO_EC
1818 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1819 ssl_check_for_safari(s, pkt);
1820 # endif /* !OPENSSL_NO_EC */
1822 /* Clear any signature algorithms extension received */
1823 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1824 s->s3->tmp.peer_sigalgs = NULL;
1825 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1827 #ifndef OPENSSL_NO_SRP
1828 OPENSSL_free(s->srp_ctx.login);
1829 s->srp_ctx.login = NULL;
1832 s->srtp_profile = NULL;
1834 if (PACKET_remaining(pkt) == 0)
1837 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1840 if (!tls1_check_duplicate_extensions(&extensions))
1844 * We parse all extensions to ensure the ClientHello is well-formed but,
1845 * unless an extension specifies otherwise, we ignore extensions upon
1848 while (PACKET_get_net_2(&extensions, &type)) {
1850 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1853 if (s->tlsext_debug_cb)
1854 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1855 PACKET_remaining(&extension),
1856 s->tlsext_debug_arg);
1858 if (type == TLSEXT_TYPE_renegotiate) {
1859 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
1861 renegotiate_seen = 1;
1862 } else if (s->version == SSL3_VERSION) {
1865 * The servername extension is treated as follows:
1867 * - Only the hostname type is supported with a maximum length of 255.
1868 * - The servername is rejected if too long or if it contains zeros,
1869 * in which case an fatal alert is generated.
1870 * - The servername field is maintained together with the session cache.
1871 * - When a session is resumed, the servername call back invoked in order
1872 * to allow the application to position itself to the right context.
1873 * - The servername is acknowledged if it is new for a session or when
1874 * it is identical to a previously used for the same session.
1875 * Applications can control the behaviour. They can at any time
1876 * set a 'desirable' servername for a new SSL object. This can be the
1877 * case for example with HTTPS when a Host: header field is received and
1878 * a renegotiation is requested. In this case, a possible servername
1879 * presented in the new client hello is only acknowledged if it matches
1880 * the value of the Host: field.
1881 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1882 * if they provide for changing an explicit servername context for the
1883 * session, i.e. when the session has been established with a servername
1885 * - On session reconnect, the servername extension may be absent.
1889 else if (type == TLSEXT_TYPE_server_name) {
1890 unsigned int servname_type;
1891 PACKET sni, hostname;
1893 if (!PACKET_as_length_prefixed_2(&extension, &sni)
1894 /* ServerNameList must be at least 1 byte long. */
1895 || PACKET_remaining(&sni) == 0) {
1900 * Although the server_name extension was intended to be
1901 * extensible to new name types, RFC 4366 defined the
1902 * syntax inextensibility and OpenSSL 1.0.x parses it as
1904 * RFC 6066 corrected the mistake but adding new name types
1905 * is nevertheless no longer feasible, so act as if no other
1906 * SNI types can exist, to simplify parsing.
1908 * Also note that the RFC permits only one SNI value per type,
1909 * i.e., we can only have a single hostname.
1911 if (!PACKET_get_1(&sni, &servname_type)
1912 || servname_type != TLSEXT_NAMETYPE_host_name
1913 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1918 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1919 *al = TLS1_AD_UNRECOGNIZED_NAME;
1923 if (PACKET_contains_zero_byte(&hostname)) {
1924 *al = TLS1_AD_UNRECOGNIZED_NAME;
1928 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1929 *al = TLS1_AD_INTERNAL_ERROR;
1933 s->servername_done = 1;
1936 * TODO(openssl-team): if the SNI doesn't match, we MUST
1937 * fall back to a full handshake.
1939 s->servername_done = s->session->tlsext_hostname
1940 && PACKET_equal(&hostname, s->session->tlsext_hostname,
1941 strlen(s->session->tlsext_hostname));
1944 #ifndef OPENSSL_NO_SRP
1945 else if (type == TLSEXT_TYPE_srp) {
1948 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
1951 if (PACKET_contains_zero_byte(&srp_I))
1955 * TODO(openssl-team): currently, we re-authenticate the user
1956 * upon resumption. Instead, we MUST ignore the login.
1958 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
1959 *al = TLS1_AD_INTERNAL_ERROR;
1965 #ifndef OPENSSL_NO_EC
1966 else if (type == TLSEXT_TYPE_ec_point_formats) {
1967 PACKET ec_point_format_list;
1969 if (!PACKET_as_length_prefixed_1(&extension,
1970 &ec_point_format_list)
1971 || PACKET_remaining(&ec_point_format_list) == 0) {
1976 if (!PACKET_memdup(&ec_point_format_list,
1977 &s->session->tlsext_ecpointformatlist,
1978 &s->session->tlsext_ecpointformatlist_length)) {
1979 *al = TLS1_AD_INTERNAL_ERROR;
1983 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1984 PACKET elliptic_curve_list;
1986 /* Each NamedCurve is 2 bytes and we must have at least 1. */
1987 if (!PACKET_as_length_prefixed_2(&extension,
1988 &elliptic_curve_list)
1989 || PACKET_remaining(&elliptic_curve_list) == 0
1990 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
1995 if (!PACKET_memdup(&elliptic_curve_list,
1996 &s->session->tlsext_ellipticcurvelist,
1997 &s->session->tlsext_ellipticcurvelist_length)) {
1998 *al = TLS1_AD_INTERNAL_ERROR;
2003 #endif /* OPENSSL_NO_EC */
2004 else if (type == TLSEXT_TYPE_session_ticket) {
2005 if (s->tls_session_ticket_ext_cb &&
2006 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
2007 PACKET_remaining(&extension),
2008 s->tls_session_ticket_ext_cb_arg)) {
2009 *al = TLS1_AD_INTERNAL_ERROR;
2012 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2013 PACKET supported_sig_algs;
2015 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2016 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2017 || PACKET_remaining(&supported_sig_algs) == 0) {
2022 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2023 PACKET_remaining(&supported_sig_algs))) {
2027 } else if (type == TLSEXT_TYPE_status_request) {
2028 if (!PACKET_get_1(&extension,
2029 (unsigned int *)&s->tlsext_status_type)) {
2033 #ifndef OPENSSL_NO_OCSP
2034 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2035 const unsigned char *ext_data;
2036 PACKET responder_id_list, exts;
2037 if (!PACKET_get_length_prefixed_2(&extension, &responder_id_list))
2040 while (PACKET_remaining(&responder_id_list) > 0) {
2042 PACKET responder_id;
2043 const unsigned char *id_data;
2045 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2047 || PACKET_remaining(&responder_id) == 0) {
2051 if (s->tlsext_ocsp_ids == NULL
2052 && (s->tlsext_ocsp_ids =
2053 sk_OCSP_RESPID_new_null()) == NULL) {
2054 *al = SSL_AD_INTERNAL_ERROR;
2058 id_data = PACKET_data(&responder_id);
2059 id = d2i_OCSP_RESPID(NULL, &id_data,
2060 PACKET_remaining(&responder_id));
2064 if (id_data != PACKET_end(&responder_id)) {
2065 OCSP_RESPID_free(id);
2069 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2070 OCSP_RESPID_free(id);
2071 *al = SSL_AD_INTERNAL_ERROR;
2076 /* Read in request_extensions */
2077 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2080 if (PACKET_remaining(&exts) > 0) {
2081 ext_data = PACKET_data(&exts);
2082 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2083 X509_EXTENSION_free);
2084 s->tlsext_ocsp_exts =
2085 d2i_X509_EXTENSIONS(NULL, &ext_data,
2086 PACKET_remaining(&exts));
2087 if (s->tlsext_ocsp_exts == NULL
2088 || ext_data != PACKET_end(&exts)) {
2096 * We don't know what to do with any other type so ignore it.
2098 s->tlsext_status_type = -1;
2101 #ifndef OPENSSL_NO_HEARTBEATS
2102 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2103 unsigned int hbtype;
2105 if (!PACKET_get_1(&extension, &hbtype)
2106 || PACKET_remaining(&extension)) {
2107 *al = SSL_AD_DECODE_ERROR;
2111 case 0x01: /* Client allows us to send HB requests */
2112 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2114 case 0x02: /* Client doesn't accept HB requests */
2115 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2116 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2119 *al = SSL_AD_ILLEGAL_PARAMETER;
2124 #ifndef OPENSSL_NO_NEXTPROTONEG
2125 else if (type == TLSEXT_TYPE_next_proto_neg &&
2126 s->s3->tmp.finish_md_len == 0) {
2128 * We shouldn't accept this extension on a
2131 * s->new_session will be set on renegotiation, but we
2132 * probably shouldn't rely that it couldn't be set on
2133 * the initial renegotiation too in certain cases (when
2134 * there's some other reason to disallow resuming an
2135 * earlier session -- the current code won't be doing
2136 * anything like that, but this might change).
2138 * A valid sign that there's been a previous handshake
2139 * in this connection is if s->s3->tmp.finish_md_len >
2140 * 0. (We are talking about a check that will happen
2141 * in the Hello protocol round, well before a new
2142 * Finished message could have been computed.)
2144 s->s3->next_proto_neg_seen = 1;
2148 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2149 s->s3->tmp.finish_md_len == 0) {
2150 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2154 /* session ticket processed earlier */
2155 #ifndef OPENSSL_NO_SRTP
2156 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2157 && type == TLSEXT_TYPE_use_srtp) {
2158 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2162 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2163 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2165 * Note: extended master secret extension handled in
2166 * tls_check_serverhello_tlsext_early()
2170 * If this ClientHello extension was unhandled and this is a
2171 * nonresumed connection, check whether the extension is a custom
2172 * TLS Extension (has a custom_srv_ext_record), and if so call the
2173 * callback and record the extension number so that an appropriate
2174 * ServerHello may be later returned.
2177 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2178 PACKET_remaining(&extension), al) <= 0)
2183 if (PACKET_remaining(pkt) != 0) {
2184 /* tls1_check_duplicate_extensions should ensure this never happens. */
2185 *al = SSL_AD_INTERNAL_ERROR;
2191 /* Need RI if renegotiating */
2193 if (!renegotiate_seen && s->renegotiate &&
2194 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2195 *al = SSL_AD_HANDSHAKE_FAILURE;
2196 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2197 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2202 * This function currently has no state to clean up, so it returns directly.
2203 * If parsing fails at any point, the function returns early.
2204 * The SSL object may be left with partial data from extensions, but it must
2205 * then no longer be used, and clearing it up will free the leftovers.
2210 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2213 custom_ext_init(&s->cert->srv_ext);
2214 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2215 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2218 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2219 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2225 #ifndef OPENSSL_NO_NEXTPROTONEG
2227 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2228 * elements of zero length are allowed and the set of elements must exactly
2229 * fill the length of the block.
2231 static char ssl_next_proto_validate(PACKET *pkt)
2233 PACKET tmp_protocol;
2235 while (PACKET_remaining(pkt)) {
2236 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2237 || PACKET_remaining(&tmp_protocol) == 0)
2245 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2247 unsigned int length, type, size;
2248 int tlsext_servername = 0;
2249 int renegotiate_seen = 0;
2251 #ifndef OPENSSL_NO_NEXTPROTONEG
2252 s->s3->next_proto_neg_seen = 0;
2254 s->tlsext_ticket_expected = 0;
2256 OPENSSL_free(s->s3->alpn_selected);
2257 s->s3->alpn_selected = NULL;
2258 #ifndef OPENSSL_NO_HEARTBEATS
2259 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2260 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2263 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2265 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2267 if (!PACKET_get_net_2(pkt, &length))
2270 if (PACKET_remaining(pkt) != length) {
2271 *al = SSL_AD_DECODE_ERROR;
2275 if (!tls1_check_duplicate_extensions(pkt)) {
2276 *al = SSL_AD_DECODE_ERROR;
2280 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2281 const unsigned char *data;
2284 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2285 || !PACKET_peek_bytes(&spkt, &data, size))
2288 if (s->tlsext_debug_cb)
2289 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2291 if (type == TLSEXT_TYPE_renegotiate) {
2292 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2294 renegotiate_seen = 1;
2295 } else if (s->version == SSL3_VERSION) {
2296 } else if (type == TLSEXT_TYPE_server_name) {
2297 if (s->tlsext_hostname == NULL || size > 0) {
2298 *al = TLS1_AD_UNRECOGNIZED_NAME;
2301 tlsext_servername = 1;
2303 #ifndef OPENSSL_NO_EC
2304 else if (type == TLSEXT_TYPE_ec_point_formats) {
2305 unsigned int ecpointformatlist_length;
2306 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2307 || ecpointformatlist_length != size - 1) {
2308 *al = TLS1_AD_DECODE_ERROR;
2312 s->session->tlsext_ecpointformatlist_length = 0;
2313 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2314 if ((s->session->tlsext_ecpointformatlist =
2315 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2316 *al = TLS1_AD_INTERNAL_ERROR;
2319 s->session->tlsext_ecpointformatlist_length =
2320 ecpointformatlist_length;
2321 if (!PACKET_copy_bytes(&spkt,
2322 s->session->tlsext_ecpointformatlist,
2323 ecpointformatlist_length)) {
2324 *al = TLS1_AD_DECODE_ERROR;
2330 #endif /* OPENSSL_NO_EC */
2332 else if (type == TLSEXT_TYPE_session_ticket) {
2333 if (s->tls_session_ticket_ext_cb &&
2334 !s->tls_session_ticket_ext_cb(s, data, size,
2335 s->tls_session_ticket_ext_cb_arg))
2337 *al = TLS1_AD_INTERNAL_ERROR;
2340 if (!tls_use_ticket(s) || (size > 0)) {
2341 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2344 s->tlsext_ticket_expected = 1;
2346 else if (type == TLSEXT_TYPE_status_request) {
2348 * MUST be empty and only sent if we've requested a status
2351 if ((s->tlsext_status_type == -1) || (size > 0)) {
2352 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2355 /* Set flag to expect CertificateStatus message */
2356 s->tlsext_status_expected = 1;
2358 #ifndef OPENSSL_NO_CT
2360 * Only take it if we asked for it - i.e if there is no CT validation
2361 * callback set, then a custom extension MAY be processing it, so we
2362 * need to let control continue to flow to that.
2364 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2365 s->ct_validation_callback != NULL) {
2366 /* Simply copy it off for later processing */
2367 if (s->tlsext_scts != NULL) {
2368 OPENSSL_free(s->tlsext_scts);
2369 s->tlsext_scts = NULL;
2371 s->tlsext_scts_len = size;
2373 s->tlsext_scts = OPENSSL_malloc(size);
2374 if (s->tlsext_scts == NULL) {
2375 *al = TLS1_AD_INTERNAL_ERROR;
2378 memcpy(s->tlsext_scts, data, size);
2382 #ifndef OPENSSL_NO_NEXTPROTONEG
2383 else if (type == TLSEXT_TYPE_next_proto_neg &&
2384 s->s3->tmp.finish_md_len == 0) {
2385 unsigned char *selected;
2386 unsigned char selected_len;
2387 /* We must have requested it. */
2388 if (s->ctx->next_proto_select_cb == NULL) {
2389 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2392 /* The data must be valid */
2393 if (!ssl_next_proto_validate(&spkt)) {
2394 *al = TLS1_AD_DECODE_ERROR;
2398 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2400 s->ctx->next_proto_select_cb_arg) !=
2401 SSL_TLSEXT_ERR_OK) {
2402 *al = TLS1_AD_INTERNAL_ERROR;
2405 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2406 if (s->next_proto_negotiated == NULL) {
2407 *al = TLS1_AD_INTERNAL_ERROR;
2410 memcpy(s->next_proto_negotiated, selected, selected_len);
2411 s->next_proto_negotiated_len = selected_len;
2412 s->s3->next_proto_neg_seen = 1;
2416 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2418 /* We must have requested it. */
2419 if (!s->s3->alpn_sent) {
2420 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2424 * The extension data consists of:
2425 * uint16 list_length
2426 * uint8 proto_length;
2427 * uint8 proto[proto_length];
2429 if (!PACKET_get_net_2(&spkt, &len)
2430 || PACKET_remaining(&spkt) != len
2431 || !PACKET_get_1(&spkt, &len)
2432 || PACKET_remaining(&spkt) != len) {
2433 *al = TLS1_AD_DECODE_ERROR;
2436 OPENSSL_free(s->s3->alpn_selected);
2437 s->s3->alpn_selected = OPENSSL_malloc(len);
2438 if (s->s3->alpn_selected == NULL) {
2439 *al = TLS1_AD_INTERNAL_ERROR;
2442 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2443 *al = TLS1_AD_DECODE_ERROR;
2446 s->s3->alpn_selected_len = len;
2448 #ifndef OPENSSL_NO_HEARTBEATS
2449 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2450 unsigned int hbtype;
2451 if (!PACKET_get_1(&spkt, &hbtype)) {
2452 *al = SSL_AD_DECODE_ERROR;
2456 case 0x01: /* Server allows us to send HB requests */
2457 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2459 case 0x02: /* Server doesn't accept HB requests */
2460 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2461 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2464 *al = SSL_AD_ILLEGAL_PARAMETER;
2469 #ifndef OPENSSL_NO_SRTP
2470 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2471 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2475 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2476 /* Ignore if inappropriate ciphersuite */
2477 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2478 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2479 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2481 else if (type == TLSEXT_TYPE_extended_master_secret) {
2482 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2484 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2487 * If this extension type was not otherwise handled, but matches a
2488 * custom_cli_ext_record, then send it to the c callback
2490 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2494 if (PACKET_remaining(pkt) != 0) {
2495 *al = SSL_AD_DECODE_ERROR;
2499 if (!s->hit && tlsext_servername == 1) {
2500 if (s->tlsext_hostname) {
2501 if (s->session->tlsext_hostname == NULL) {
2502 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
2503 if (!s->session->tlsext_hostname) {
2504 *al = SSL_AD_UNRECOGNIZED_NAME;
2508 *al = SSL_AD_DECODE_ERROR;
2517 * Determine if we need to see RI. Strictly speaking if we want to avoid
2518 * an attack we should *always* see RI even on initial server hello
2519 * because the client doesn't see any renegotiation during an attack.
2520 * However this would mean we could not connect to any server which
2521 * doesn't support RI so for the immediate future tolerate RI absence on
2522 * initial connect only.
2524 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2525 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2526 *al = SSL_AD_HANDSHAKE_FAILURE;
2527 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2528 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2534 * Check extended master secret extension is consistent with
2537 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2538 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2539 *al = SSL_AD_HANDSHAKE_FAILURE;
2540 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2548 int ssl_prepare_clienthello_tlsext(SSL *s)
2550 s->s3->alpn_sent = 0;
2554 int ssl_prepare_serverhello_tlsext(SSL *s)
2559 static int ssl_check_clienthello_tlsext_early(SSL *s)
2561 int ret = SSL_TLSEXT_ERR_NOACK;
2562 int al = SSL_AD_UNRECOGNIZED_NAME;
2564 #ifndef OPENSSL_NO_EC
2566 * The handling of the ECPointFormats extension is done elsewhere, namely
2567 * in ssl3_choose_cipher in s3_lib.c.
2570 * The handling of the EllipticCurves extension is done elsewhere, namely
2571 * in ssl3_choose_cipher in s3_lib.c.
2575 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2577 s->ctx->tlsext_servername_callback(s, &al,
2578 s->ctx->tlsext_servername_arg);
2579 else if (s->initial_ctx != NULL
2580 && s->initial_ctx->tlsext_servername_callback != 0)
2582 s->initial_ctx->tlsext_servername_callback(s, &al,
2584 initial_ctx->tlsext_servername_arg);
2587 case SSL_TLSEXT_ERR_ALERT_FATAL:
2588 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2591 case SSL_TLSEXT_ERR_ALERT_WARNING:
2592 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2595 case SSL_TLSEXT_ERR_NOACK:
2596 s->servername_done = 0;
2601 /* Initialise digests to default values */
2602 void ssl_set_default_md(SSL *s)
2604 const EVP_MD **pmd = s->s3->tmp.md;
2605 #ifndef OPENSSL_NO_DSA
2606 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2608 #ifndef OPENSSL_NO_RSA
2609 if (SSL_USE_SIGALGS(s))
2610 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2612 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2613 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2615 #ifndef OPENSSL_NO_EC
2616 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2618 #ifndef OPENSSL_NO_GOST
2619 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2620 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2621 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2625 int tls1_set_server_sigalgs(SSL *s)
2630 /* Clear any shared signature algorithms */
2631 OPENSSL_free(s->cert->shared_sigalgs);
2632 s->cert->shared_sigalgs = NULL;
2633 s->cert->shared_sigalgslen = 0;
2634 /* Clear certificate digests and validity flags */
2635 for (i = 0; i < SSL_PKEY_NUM; i++) {
2636 s->s3->tmp.md[i] = NULL;
2637 s->s3->tmp.valid_flags[i] = 0;
2640 /* If sigalgs received process it. */
2641 if (s->s3->tmp.peer_sigalgs) {
2642 if (!tls1_process_sigalgs(s)) {
2643 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2644 al = SSL_AD_INTERNAL_ERROR;
2647 /* Fatal error is no shared signature algorithms */
2648 if (!s->cert->shared_sigalgs) {
2649 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2650 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2651 al = SSL_AD_ILLEGAL_PARAMETER;
2655 ssl_set_default_md(s);
2659 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2663 int ssl_check_clienthello_tlsext_late(SSL *s)
2665 int ret = SSL_TLSEXT_ERR_OK;
2666 int al = SSL_AD_INTERNAL_ERROR;
2669 * If status request then ask callback what to do. Note: this must be
2670 * called after servername callbacks in case the certificate has changed,
2671 * and must be called after the cipher has been chosen because this may
2672 * influence which certificate is sent
2674 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2676 CERT_PKEY *certpkey;
2677 certpkey = ssl_get_server_send_pkey(s);
2678 /* If no certificate can't return certificate status */
2679 if (certpkey == NULL) {
2680 s->tlsext_status_expected = 0;
2684 * Set current certificate to one we will use so SSL_get_certificate
2685 * et al can pick it up.
2687 s->cert->key = certpkey;
2688 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2690 /* We don't want to send a status request response */
2691 case SSL_TLSEXT_ERR_NOACK:
2692 s->tlsext_status_expected = 0;
2694 /* status request response should be sent */
2695 case SSL_TLSEXT_ERR_OK:
2696 if (s->tlsext_ocsp_resp)
2697 s->tlsext_status_expected = 1;
2699 s->tlsext_status_expected = 0;
2701 /* something bad happened */
2702 case SSL_TLSEXT_ERR_ALERT_FATAL:
2703 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2704 al = SSL_AD_INTERNAL_ERROR;
2708 s->tlsext_status_expected = 0;
2710 if (!tls1_alpn_handle_client_hello_late(s, &ret, &al)) {
2716 case SSL_TLSEXT_ERR_ALERT_FATAL:
2717 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2720 case SSL_TLSEXT_ERR_ALERT_WARNING:
2721 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2729 int ssl_check_serverhello_tlsext(SSL *s)
2731 int ret = SSL_TLSEXT_ERR_NOACK;
2732 int al = SSL_AD_UNRECOGNIZED_NAME;
2734 #ifndef OPENSSL_NO_EC
2736 * If we are client and using an elliptic curve cryptography cipher
2737 * suite, then if server returns an EC point formats lists extension it
2738 * must contain uncompressed.
2740 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2741 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2742 if ((s->tlsext_ecpointformatlist != NULL)
2743 && (s->tlsext_ecpointformatlist_length > 0)
2744 && (s->session->tlsext_ecpointformatlist != NULL)
2745 && (s->session->tlsext_ecpointformatlist_length > 0)
2746 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2747 /* we are using an ECC cipher */
2749 unsigned char *list;
2750 int found_uncompressed = 0;
2751 list = s->session->tlsext_ecpointformatlist;
2752 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2753 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2754 found_uncompressed = 1;
2758 if (!found_uncompressed) {
2759 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2760 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2764 ret = SSL_TLSEXT_ERR_OK;
2765 #endif /* OPENSSL_NO_EC */
2767 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2769 s->ctx->tlsext_servername_callback(s, &al,
2770 s->ctx->tlsext_servername_arg);
2771 else if (s->initial_ctx != NULL
2772 && s->initial_ctx->tlsext_servername_callback != 0)
2774 s->initial_ctx->tlsext_servername_callback(s, &al,
2776 initial_ctx->tlsext_servername_arg);
2779 * Ensure we get sensible values passed to tlsext_status_cb in the event
2780 * that we don't receive a status message
2782 OPENSSL_free(s->tlsext_ocsp_resp);
2783 s->tlsext_ocsp_resp = NULL;
2784 s->tlsext_ocsp_resplen = -1;
2787 case SSL_TLSEXT_ERR_ALERT_FATAL:
2788 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2791 case SSL_TLSEXT_ERR_ALERT_WARNING:
2792 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2795 case SSL_TLSEXT_ERR_NOACK:
2796 s->servername_done = 0;
2802 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2805 if (s->version < SSL3_VERSION)
2807 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2808 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2812 if (ssl_check_serverhello_tlsext(s) <= 0) {
2813 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2820 * Since the server cache lookup is done early on in the processing of the
2821 * ClientHello and other operations depend on the result some extensions
2822 * need to be handled at the same time.
2824 * Two extensions are currently handled, session ticket and extended master
2827 * session_id: ClientHello session ID.
2828 * ext: ClientHello extensions (including length prefix)
2829 * ret: (output) on return, if a ticket was decrypted, then this is set to
2830 * point to the resulting session.
2832 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2833 * ciphersuite, in which case we have no use for session tickets and one will
2834 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2837 * -1: fatal error, either from parsing or decrypting the ticket.
2838 * 0: no ticket was found (or was ignored, based on settings).
2839 * 1: a zero length extension was found, indicating that the client supports
2840 * session tickets but doesn't currently have one to offer.
2841 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2842 * couldn't be decrypted because of a non-fatal error.
2843 * 3: a ticket was successfully decrypted and *ret was set.
2846 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2847 * a new session ticket to the client because the client indicated support
2848 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2849 * a session ticket or we couldn't use the one it gave us, or if
2850 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2851 * Otherwise, s->tlsext_ticket_expected is set to 0.
2853 * For extended master secret flag is set if the extension is present.
2856 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2857 const PACKET *session_id,
2861 PACKET local_ext = *ext;
2864 int have_ticket = 0;
2865 int use_ticket = tls_use_ticket(s);
2868 s->tlsext_ticket_expected = 0;
2869 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2872 * If tickets disabled behave as if no ticket present to permit stateful
2875 if ((s->version <= SSL3_VERSION))
2878 if (!PACKET_get_net_2(&local_ext, &i)) {
2882 while (PACKET_remaining(&local_ext) >= 4) {
2883 unsigned int type, size;
2885 if (!PACKET_get_net_2(&local_ext, &type)
2886 || !PACKET_get_net_2(&local_ext, &size)) {
2887 /* Shouldn't ever happen */
2891 if (PACKET_remaining(&local_ext) < size) {
2895 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
2897 const unsigned char *etick;
2899 /* Duplicate extension */
2900 if (have_ticket != 0) {
2908 * The client will accept a ticket but doesn't currently have
2911 s->tlsext_ticket_expected = 1;
2915 if (s->tls_session_secret_cb) {
2917 * Indicate that the ticket couldn't be decrypted rather than
2918 * generating the session from ticket now, trigger
2919 * abbreviated handshake based on external mechanism to
2920 * calculate the master secret later.
2925 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
2926 /* Shouldn't ever happen */
2930 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
2931 PACKET_remaining(session_id), ret);
2933 case 2: /* ticket couldn't be decrypted */
2934 s->tlsext_ticket_expected = 1;
2937 case 3: /* ticket was decrypted */
2940 case 4: /* ticket decrypted but need to renew */
2941 s->tlsext_ticket_expected = 1;
2944 default: /* fatal error */
2950 if (type == TLSEXT_TYPE_extended_master_secret)
2951 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2952 if (!PACKET_forward(&local_ext, size)) {
2958 if (have_ticket == 0)
2965 * tls_decrypt_ticket attempts to decrypt a session ticket.
2967 * etick: points to the body of the session ticket extension.
2968 * eticklen: the length of the session tickets extension.
2969 * sess_id: points at the session ID.
2970 * sesslen: the length of the session ID.
2971 * psess: (output) on return, if a ticket was decrypted, then this is set to
2972 * point to the resulting session.
2975 * -2: fatal error, malloc failure.
2976 * -1: fatal error, either from parsing or decrypting the ticket.
2977 * 2: the ticket couldn't be decrypted.
2978 * 3: a ticket was successfully decrypted and *psess was set.
2979 * 4: same as 3, but the ticket needs to be renewed.
2981 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2982 int eticklen, const unsigned char *sess_id,
2983 int sesslen, SSL_SESSION **psess)
2986 unsigned char *sdec;
2987 const unsigned char *p;
2988 int slen, mlen, renew_ticket = 0, ret = -1;
2989 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2990 HMAC_CTX *hctx = NULL;
2991 EVP_CIPHER_CTX *ctx;
2992 SSL_CTX *tctx = s->initial_ctx;
2993 /* Need at least keyname + iv + some encrypted data */
2996 /* Initialize session ticket encryption and HMAC contexts */
2997 hctx = HMAC_CTX_new();
3000 ctx = EVP_CIPHER_CTX_new();
3005 if (tctx->tlsext_ticket_key_cb) {
3006 unsigned char *nctick = (unsigned char *)etick;
3007 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3018 /* Check key name matches */
3019 if (memcmp(etick, tctx->tlsext_tick_key_name,
3020 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3024 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3025 sizeof(tctx->tlsext_tick_hmac_key),
3026 EVP_sha256(), NULL) <= 0
3027 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3028 tctx->tlsext_tick_aes_key,
3029 etick + sizeof(tctx->tlsext_tick_key_name)) <= 0) {
3034 * Attempt to process session ticket, first conduct sanity and integrity
3037 mlen = HMAC_size(hctx);
3042 /* Check HMAC of encrypted ticket */
3043 if (HMAC_Update(hctx, etick, eticklen) <= 0
3044 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3047 HMAC_CTX_free(hctx);
3048 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3049 EVP_CIPHER_CTX_free(ctx);
3052 /* Attempt to decrypt session data */
3053 /* Move p after IV to start of encrypted ticket, update length */
3054 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3055 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3056 sdec = OPENSSL_malloc(eticklen);
3058 || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3059 EVP_CIPHER_CTX_free(ctx);
3063 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3064 EVP_CIPHER_CTX_free(ctx);
3069 EVP_CIPHER_CTX_free(ctx);
3073 sess = d2i_SSL_SESSION(NULL, &p, slen);
3077 * The session ID, if non-empty, is used by some clients to detect
3078 * that the ticket has been accepted. So we copy it to the session
3079 * structure. If it is empty set length to zero as required by
3083 memcpy(sess->session_id, sess_id, sesslen);
3084 sess->session_id_length = sesslen;
3093 * For session parse failure, indicate that we need to send a new ticket.
3097 EVP_CIPHER_CTX_free(ctx);
3098 HMAC_CTX_free(hctx);
3102 /* Tables to translate from NIDs to TLS v1.2 ids */
3109 static const tls12_lookup tls12_md[] = {
3110 {NID_md5, TLSEXT_hash_md5},
3111 {NID_sha1, TLSEXT_hash_sha1},
3112 {NID_sha224, TLSEXT_hash_sha224},
3113 {NID_sha256, TLSEXT_hash_sha256},
3114 {NID_sha384, TLSEXT_hash_sha384},
3115 {NID_sha512, TLSEXT_hash_sha512},
3116 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3117 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3118 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3121 static const tls12_lookup tls12_sig[] = {
3122 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3123 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3124 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3125 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3126 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3127 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3130 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3133 for (i = 0; i < tlen; i++) {
3134 if (table[i].nid == nid)
3140 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3143 for (i = 0; i < tlen; i++) {
3144 if ((table[i].id) == id)
3145 return table[i].nid;
3150 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3156 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3159 sig_id = tls12_get_sigid(pk);
3162 p[0] = (unsigned char)md_id;
3163 p[1] = (unsigned char)sig_id;
3167 int tls12_get_sigid(const EVP_PKEY *pk)
3169 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3176 unsigned char tlsext_hash;
3179 static const tls12_hash_info tls12_md_info[] = {
3180 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3181 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3182 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3183 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3184 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3185 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3186 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3187 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3188 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3191 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3197 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3199 if (tls12_md_info[i].tlsext_hash == hash_alg)
3200 return tls12_md_info + i;
3206 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3208 const tls12_hash_info *inf;
3209 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3211 inf = tls12_get_hash_info(hash_alg);
3214 return ssl_md(inf->md_idx);
3217 static int tls12_get_pkey_idx(unsigned char sig_alg)
3220 #ifndef OPENSSL_NO_RSA
3221 case TLSEXT_signature_rsa:
3222 return SSL_PKEY_RSA_SIGN;
3224 #ifndef OPENSSL_NO_DSA
3225 case TLSEXT_signature_dsa:
3226 return SSL_PKEY_DSA_SIGN;
3228 #ifndef OPENSSL_NO_EC
3229 case TLSEXT_signature_ecdsa:
3230 return SSL_PKEY_ECC;
3232 # ifndef OPENSSL_NO_GOST
3233 case TLSEXT_signature_gostr34102001:
3234 return SSL_PKEY_GOST01;
3236 case TLSEXT_signature_gostr34102012_256:
3237 return SSL_PKEY_GOST12_256;
3239 case TLSEXT_signature_gostr34102012_512:
3240 return SSL_PKEY_GOST12_512;
3246 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3247 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3248 int *psignhash_nid, const unsigned char *data)
3250 int sign_nid = NID_undef, hash_nid = NID_undef;
3251 if (!phash_nid && !psign_nid && !psignhash_nid)
3253 if (phash_nid || psignhash_nid) {
3254 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3256 *phash_nid = hash_nid;
3258 if (psign_nid || psignhash_nid) {
3259 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3261 *psign_nid = sign_nid;
3263 if (psignhash_nid) {
3264 if (sign_nid == NID_undef || hash_nid == NID_undef
3265 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3267 *psignhash_nid = NID_undef;
3271 /* Check to see if a signature algorithm is allowed */
3272 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3274 /* See if we have an entry in the hash table and it is enabled */
3275 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3276 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3278 /* See if public key algorithm allowed */
3279 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3281 /* Finally see if security callback allows it */
3282 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3286 * Get a mask of disabled public key algorithms based on supported signature
3287 * algorithms. For example if no signature algorithm supports RSA then RSA is
3291 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3293 const unsigned char *sigalgs;
3294 size_t i, sigalgslen;
3295 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3297 * Now go through all signature algorithms seeing if we support any for
3298 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3299 * down calls to security callback only check if we have to.
3301 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3302 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3303 switch (sigalgs[1]) {
3304 #ifndef OPENSSL_NO_RSA
3305 case TLSEXT_signature_rsa:
3306 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3310 #ifndef OPENSSL_NO_DSA
3311 case TLSEXT_signature_dsa:
3312 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3316 #ifndef OPENSSL_NO_EC
3317 case TLSEXT_signature_ecdsa:
3318 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3325 *pmask_a |= SSL_aRSA;
3327 *pmask_a |= SSL_aDSS;
3329 *pmask_a |= SSL_aECDSA;
3332 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3333 const unsigned char *psig, size_t psiglen)
3335 unsigned char *tmpout = out;
3337 for (i = 0; i < psiglen; i += 2, psig += 2) {
3338 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3339 *tmpout++ = psig[0];
3340 *tmpout++ = psig[1];
3343 return tmpout - out;
3346 /* Given preference and allowed sigalgs set shared sigalgs */
3347 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3348 const unsigned char *pref, size_t preflen,
3349 const unsigned char *allow, size_t allowlen)
3351 const unsigned char *ptmp, *atmp;
3352 size_t i, j, nmatch = 0;
3353 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3354 /* Skip disabled hashes or signature algorithms */
3355 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3357 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3358 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3361 shsig->rhash = ptmp[0];
3362 shsig->rsign = ptmp[1];
3363 tls1_lookup_sigalg(&shsig->hash_nid,
3365 &shsig->signandhash_nid, ptmp);
3375 /* Set shared signature algorithms for SSL structures */
3376 static int tls1_set_shared_sigalgs(SSL *s)
3378 const unsigned char *pref, *allow, *conf;
3379 size_t preflen, allowlen, conflen;
3381 TLS_SIGALGS *salgs = NULL;
3383 unsigned int is_suiteb = tls1_suiteb(s);
3385 OPENSSL_free(c->shared_sigalgs);
3386 c->shared_sigalgs = NULL;
3387 c->shared_sigalgslen = 0;
3388 /* If client use client signature algorithms if not NULL */
3389 if (!s->server && c->client_sigalgs && !is_suiteb) {
3390 conf = c->client_sigalgs;
3391 conflen = c->client_sigalgslen;
3392 } else if (c->conf_sigalgs && !is_suiteb) {
3393 conf = c->conf_sigalgs;
3394 conflen = c->conf_sigalgslen;
3396 conflen = tls12_get_psigalgs(s, &conf);
3397 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3400 allow = s->s3->tmp.peer_sigalgs;
3401 allowlen = s->s3->tmp.peer_sigalgslen;
3405 pref = s->s3->tmp.peer_sigalgs;
3406 preflen = s->s3->tmp.peer_sigalgslen;
3408 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3410 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3413 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3417 c->shared_sigalgs = salgs;
3418 c->shared_sigalgslen = nmatch;
3422 /* Set preferred digest for each key type */
3424 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3427 /* Extension ignored for inappropriate versions */
3428 if (!SSL_USE_SIGALGS(s))
3430 /* Should never happen */
3434 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3435 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3436 if (s->s3->tmp.peer_sigalgs == NULL)
3438 s->s3->tmp.peer_sigalgslen = dsize;
3439 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3443 int tls1_process_sigalgs(SSL *s)
3448 const EVP_MD **pmd = s->s3->tmp.md;
3449 uint32_t *pvalid = s->s3->tmp.valid_flags;
3451 TLS_SIGALGS *sigptr;
3452 if (!tls1_set_shared_sigalgs(s))
3455 for (i = 0, sigptr = c->shared_sigalgs;
3456 i < c->shared_sigalgslen; i++, sigptr++) {
3457 idx = tls12_get_pkey_idx(sigptr->rsign);
3458 if (idx > 0 && pmd[idx] == NULL) {
3459 md = tls12_get_hash(sigptr->rhash);
3461 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3462 if (idx == SSL_PKEY_RSA_SIGN) {
3463 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3464 pmd[SSL_PKEY_RSA_ENC] = md;
3470 * In strict mode leave unset digests as NULL to indicate we can't use
3471 * the certificate for signing.
3473 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3475 * Set any remaining keys to default values. NOTE: if alg is not
3476 * supported it stays as NULL.
3478 #ifndef OPENSSL_NO_DSA
3479 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3480 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3482 #ifndef OPENSSL_NO_RSA
3483 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3484 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3485 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3488 #ifndef OPENSSL_NO_EC
3489 if (pmd[SSL_PKEY_ECC] == NULL)
3490 pmd[SSL_PKEY_ECC] = EVP_sha1();
3492 # ifndef OPENSSL_NO_GOST
3493 if (pmd[SSL_PKEY_GOST01] == NULL)
3494 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3495 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3496 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3497 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3498 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3504 int SSL_get_sigalgs(SSL *s, int idx,
3505 int *psign, int *phash, int *psignhash,
3506 unsigned char *rsig, unsigned char *rhash)
3508 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3513 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3520 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3522 return s->s3->tmp.peer_sigalgslen / 2;
3525 int SSL_get_shared_sigalgs(SSL *s, int idx,
3526 int *psign, int *phash, int *psignhash,
3527 unsigned char *rsig, unsigned char *rhash)
3529 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3530 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3534 *phash = shsigalgs->hash_nid;
3536 *psign = shsigalgs->sign_nid;
3538 *psignhash = shsigalgs->signandhash_nid;
3540 *rsig = shsigalgs->rsign;
3542 *rhash = shsigalgs->rhash;
3543 return s->cert->shared_sigalgslen;
3546 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3550 int sigalgs[MAX_SIGALGLEN];
3553 static void get_sigorhash(int *psig, int *phash, const char *str)
3555 if (strcmp(str, "RSA") == 0) {
3556 *psig = EVP_PKEY_RSA;
3557 } else if (strcmp(str, "DSA") == 0) {
3558 *psig = EVP_PKEY_DSA;
3559 } else if (strcmp(str, "ECDSA") == 0) {
3560 *psig = EVP_PKEY_EC;
3562 *phash = OBJ_sn2nid(str);
3563 if (*phash == NID_undef)
3564 *phash = OBJ_ln2nid(str);
3568 static int sig_cb(const char *elem, int len, void *arg)
3570 sig_cb_st *sarg = arg;
3573 int sig_alg = NID_undef, hash_alg = NID_undef;
3576 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3578 if (len > (int)(sizeof(etmp) - 1))
3580 memcpy(etmp, elem, len);
3582 p = strchr(etmp, '+');
3590 get_sigorhash(&sig_alg, &hash_alg, etmp);
3591 get_sigorhash(&sig_alg, &hash_alg, p);
3593 if (sig_alg == NID_undef || hash_alg == NID_undef)
3596 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3597 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3600 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3601 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3606 * Set suppored signature algorithms based on a colon separated list of the
3607 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3609 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3613 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3617 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3620 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3623 unsigned char *sigalgs, *sptr;
3628 sigalgs = OPENSSL_malloc(salglen);
3629 if (sigalgs == NULL)
3631 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3632 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3633 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3635 if (rhash == -1 || rsign == -1)
3642 OPENSSL_free(c->client_sigalgs);
3643 c->client_sigalgs = sigalgs;
3644 c->client_sigalgslen = salglen;
3646 OPENSSL_free(c->conf_sigalgs);
3647 c->conf_sigalgs = sigalgs;
3648 c->conf_sigalgslen = salglen;
3654 OPENSSL_free(sigalgs);
3658 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3662 if (default_nid == -1)
3664 sig_nid = X509_get_signature_nid(x);
3666 return sig_nid == default_nid ? 1 : 0;
3667 for (i = 0; i < c->shared_sigalgslen; i++)
3668 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3673 /* Check to see if a certificate issuer name matches list of CA names */
3674 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3678 nm = X509_get_issuer_name(x);
3679 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3680 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3687 * Check certificate chain is consistent with TLS extensions and is usable by
3688 * server. This servers two purposes: it allows users to check chains before
3689 * passing them to the server and it allows the server to check chains before
3690 * attempting to use them.
3693 /* Flags which need to be set for a certificate when stict mode not set */
3695 #define CERT_PKEY_VALID_FLAGS \
3696 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3697 /* Strict mode flags */
3698 #define CERT_PKEY_STRICT_FLAGS \
3699 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3700 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3702 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3707 int check_flags = 0, strict_mode;
3708 CERT_PKEY *cpk = NULL;
3711 unsigned int suiteb_flags = tls1_suiteb(s);
3712 /* idx == -1 means checking server chains */
3714 /* idx == -2 means checking client certificate chains */
3717 idx = cpk - c->pkeys;
3719 cpk = c->pkeys + idx;
3720 pvalid = s->s3->tmp.valid_flags + idx;
3722 pk = cpk->privatekey;
3724 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3725 /* If no cert or key, forget it */
3731 idx = ssl_cert_type(x, pk);
3734 pvalid = s->s3->tmp.valid_flags + idx;
3736 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3737 check_flags = CERT_PKEY_STRICT_FLAGS;
3739 check_flags = CERT_PKEY_VALID_FLAGS;
3746 check_flags |= CERT_PKEY_SUITEB;
3747 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3748 if (ok == X509_V_OK)
3749 rv |= CERT_PKEY_SUITEB;
3750 else if (!check_flags)
3755 * Check all signature algorithms are consistent with signature
3756 * algorithms extension if TLS 1.2 or later and strict mode.
3758 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3760 unsigned char rsign = 0;
3761 if (s->s3->tmp.peer_sigalgs)
3763 /* If no sigalgs extension use defaults from RFC5246 */
3766 case SSL_PKEY_RSA_ENC:
3767 case SSL_PKEY_RSA_SIGN:
3768 rsign = TLSEXT_signature_rsa;
3769 default_nid = NID_sha1WithRSAEncryption;
3772 case SSL_PKEY_DSA_SIGN:
3773 rsign = TLSEXT_signature_dsa;
3774 default_nid = NID_dsaWithSHA1;
3778 rsign = TLSEXT_signature_ecdsa;
3779 default_nid = NID_ecdsa_with_SHA1;
3782 case SSL_PKEY_GOST01:
3783 rsign = TLSEXT_signature_gostr34102001;
3784 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3787 case SSL_PKEY_GOST12_256:
3788 rsign = TLSEXT_signature_gostr34102012_256;
3789 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3792 case SSL_PKEY_GOST12_512:
3793 rsign = TLSEXT_signature_gostr34102012_512;
3794 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3803 * If peer sent no signature algorithms extension and we have set
3804 * preferred signature algorithms check we support sha1.
3806 if (default_nid > 0 && c->conf_sigalgs) {
3808 const unsigned char *p = c->conf_sigalgs;
3809 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3810 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3813 if (j == c->conf_sigalgslen) {
3820 /* Check signature algorithm of each cert in chain */
3821 if (!tls1_check_sig_alg(c, x, default_nid)) {
3825 rv |= CERT_PKEY_EE_SIGNATURE;
3826 rv |= CERT_PKEY_CA_SIGNATURE;
3827 for (i = 0; i < sk_X509_num(chain); i++) {
3828 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3830 rv &= ~CERT_PKEY_CA_SIGNATURE;
3837 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3838 else if (check_flags)
3839 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3841 /* Check cert parameters are consistent */
3842 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3843 rv |= CERT_PKEY_EE_PARAM;
3844 else if (!check_flags)
3847 rv |= CERT_PKEY_CA_PARAM;
3848 /* In strict mode check rest of chain too */
3849 else if (strict_mode) {
3850 rv |= CERT_PKEY_CA_PARAM;
3851 for (i = 0; i < sk_X509_num(chain); i++) {
3852 X509 *ca = sk_X509_value(chain, i);
3853 if (!tls1_check_cert_param(s, ca, 0)) {
3855 rv &= ~CERT_PKEY_CA_PARAM;
3862 if (!s->server && strict_mode) {
3863 STACK_OF(X509_NAME) *ca_dn;
3865 switch (EVP_PKEY_id(pk)) {
3867 check_type = TLS_CT_RSA_SIGN;
3870 check_type = TLS_CT_DSS_SIGN;
3873 check_type = TLS_CT_ECDSA_SIGN;
3877 const unsigned char *ctypes;
3881 ctypelen = (int)c->ctype_num;
3883 ctypes = (unsigned char *)s->s3->tmp.ctype;
3884 ctypelen = s->s3->tmp.ctype_num;
3886 for (i = 0; i < ctypelen; i++) {
3887 if (ctypes[i] == check_type) {
3888 rv |= CERT_PKEY_CERT_TYPE;
3892 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3895 rv |= CERT_PKEY_CERT_TYPE;
3897 ca_dn = s->s3->tmp.ca_names;
3899 if (!sk_X509_NAME_num(ca_dn))
3900 rv |= CERT_PKEY_ISSUER_NAME;
3902 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3903 if (ssl_check_ca_name(ca_dn, x))
3904 rv |= CERT_PKEY_ISSUER_NAME;
3906 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3907 for (i = 0; i < sk_X509_num(chain); i++) {
3908 X509 *xtmp = sk_X509_value(chain, i);
3909 if (ssl_check_ca_name(ca_dn, xtmp)) {
3910 rv |= CERT_PKEY_ISSUER_NAME;
3915 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3918 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3920 if (!check_flags || (rv & check_flags) == check_flags)
3921 rv |= CERT_PKEY_VALID;
3925 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3926 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3927 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3928 else if (s->s3->tmp.md[idx] != NULL)
3929 rv |= CERT_PKEY_SIGN;
3931 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3934 * When checking a CERT_PKEY structure all flags are irrelevant if the
3938 if (rv & CERT_PKEY_VALID)
3941 /* Preserve explicit sign flag, clear rest */
3942 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3949 /* Set validity of certificates in an SSL structure */
3950 void tls1_set_cert_validity(SSL *s)
3952 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3953 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3954 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3955 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3956 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
3957 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
3958 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
3961 /* User level utiity function to check a chain is suitable */
3962 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3964 return tls1_check_chain(s, x, pk, chain, -1);
3968 #ifndef OPENSSL_NO_DH
3969 DH *ssl_get_auto_dh(SSL *s)
3971 int dh_secbits = 80;
3972 if (s->cert->dh_tmp_auto == 2)
3973 return DH_get_1024_160();
3974 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
3975 if (s->s3->tmp.new_cipher->strength_bits == 256)
3980 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
3981 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
3984 if (dh_secbits >= 128) {
3992 if (dh_secbits >= 192)
3993 p = BN_get_rfc3526_prime_8192(NULL);
3995 p = BN_get_rfc3526_prime_3072(NULL);
3996 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4004 if (dh_secbits >= 112)
4005 return DH_get_2048_224();
4006 return DH_get_1024_160();
4010 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4013 EVP_PKEY *pkey = X509_get0_pubkey(x);
4016 * If no parameters this will return -1 and fail using the default
4017 * security callback for any non-zero security level. This will
4018 * reject keys which omit parameters but this only affects DSA and
4019 * omission of parameters is never (?) done in practice.
4021 secbits = EVP_PKEY_security_bits(pkey);
4024 return ssl_security(s, op, secbits, 0, x);
4026 return ssl_ctx_security(ctx, op, secbits, 0, x);
4029 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4031 /* Lookup signature algorithm digest */
4032 int secbits = -1, md_nid = NID_undef, sig_nid;
4033 /* Don't check signature if self signed */
4034 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4036 sig_nid = X509_get_signature_nid(x);
4037 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4039 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4040 secbits = EVP_MD_size(md) * 4;
4043 return ssl_security(s, op, secbits, md_nid, x);
4045 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4048 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4051 vfy = SSL_SECOP_PEER;
4053 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4054 return SSL_R_EE_KEY_TOO_SMALL;
4056 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4057 return SSL_R_CA_KEY_TOO_SMALL;
4059 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4060 return SSL_R_CA_MD_TOO_WEAK;
4065 * Check security of a chain, if sk includes the end entity certificate then
4066 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4067 * one to the peer. Return values: 1 if ok otherwise error code to use
4070 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4072 int rv, start_idx, i;
4074 x = sk_X509_value(sk, 0);
4079 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4083 for (i = start_idx; i < sk_X509_num(sk); i++) {
4084 x = sk_X509_value(sk, i);
4085 rv = ssl_security_cert(s, NULL, x, vfy, 0);