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 #ifdef TLSEXT_TYPE_encrypt_then_mac
1371 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1374 #ifndef OPENSSL_NO_CT
1375 if (s->ct_validation_callback != NULL) {
1376 s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret);
1380 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1384 * Add padding to workaround bugs in F5 terminators. See
1385 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1386 * code works out the length of all existing extensions it MUST always
1389 if (s->options & SSL_OP_TLSEXT_PADDING) {
1390 int hlen = ret - (unsigned char *)s->init_buf->data;
1392 if (hlen > 0xff && hlen < 0x200) {
1393 hlen = 0x200 - hlen;
1399 s2n(TLSEXT_TYPE_padding, ret);
1401 memset(ret, 0, hlen);
1408 if ((extdatalen = ret - orig - 2) == 0)
1411 s2n(extdatalen, orig);
1415 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1416 unsigned char *limit, int *al)
1419 unsigned char *orig = buf;
1420 unsigned char *ret = buf;
1421 #ifndef OPENSSL_NO_NEXTPROTONEG
1422 int next_proto_neg_seen;
1424 #ifndef OPENSSL_NO_EC
1425 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1426 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1427 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1428 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1433 return NULL; /* this really never occurs, but ... */
1435 if (s->s3->send_connection_binding) {
1438 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1439 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1443 if ((limit - ret - 4 - el) < 0)
1446 s2n(TLSEXT_TYPE_renegotiate, ret);
1449 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1450 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1457 /* Only add RI for SSLv3 */
1458 if (s->version == SSL3_VERSION)
1461 if (!s->hit && s->servername_done == 1
1462 && s->session->tlsext_hostname != NULL) {
1463 if ((long)(limit - ret - 4) < 0)
1466 s2n(TLSEXT_TYPE_server_name, ret);
1469 #ifndef OPENSSL_NO_EC
1471 const unsigned char *plist;
1474 * Add TLS extension ECPointFormats to the ServerHello message
1478 tls1_get_formatlist(s, &plist, &plistlen);
1480 if ((lenmax = limit - ret - 5) < 0)
1482 if (plistlen > (size_t)lenmax)
1484 if (plistlen > 255) {
1485 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1489 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1490 s2n(plistlen + 1, ret);
1491 *(ret++) = (unsigned char)plistlen;
1492 memcpy(ret, plist, plistlen);
1497 * Currently the server should not respond with a SupportedCurves
1500 #endif /* OPENSSL_NO_EC */
1502 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1503 if ((long)(limit - ret - 4) < 0)
1505 s2n(TLSEXT_TYPE_session_ticket, ret);
1509 if (s->tlsext_status_expected) {
1510 if ((long)(limit - ret - 4) < 0)
1512 s2n(TLSEXT_TYPE_status_request, ret);
1516 #ifndef OPENSSL_NO_SRTP
1517 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1520 /* Returns 0 on success!! */
1521 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1522 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1525 if ((limit - ret - 4 - el) < 0)
1528 s2n(TLSEXT_TYPE_use_srtp, ret);
1531 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1532 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1539 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1540 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1541 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1542 const unsigned char cryptopro_ext[36] = {
1543 0xfd, 0xe8, /* 65000 */
1544 0x00, 0x20, /* 32 bytes length */
1545 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1546 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1547 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1548 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1550 if (limit - ret < 36)
1552 memcpy(ret, cryptopro_ext, 36);
1556 #ifndef OPENSSL_NO_HEARTBEATS
1557 /* Add Heartbeat extension if we've received one */
1558 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1559 if ((limit - ret - 4 - 1) < 0)
1561 s2n(TLSEXT_TYPE_heartbeat, ret);
1565 * 1: peer may send requests
1566 * 2: peer not allowed to send requests
1568 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1569 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1571 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1576 #ifndef OPENSSL_NO_NEXTPROTONEG
1577 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1578 s->s3->next_proto_neg_seen = 0;
1579 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1580 const unsigned char *npa;
1581 unsigned int npalen;
1584 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1586 ctx->next_protos_advertised_cb_arg);
1587 if (r == SSL_TLSEXT_ERR_OK) {
1588 if ((long)(limit - ret - 4 - npalen) < 0)
1590 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1592 memcpy(ret, npa, npalen);
1594 s->s3->next_proto_neg_seen = 1;
1598 if (!custom_ext_add(s, 1, &ret, limit, al))
1600 #ifdef TLSEXT_TYPE_encrypt_then_mac
1601 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1603 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1604 * for other cases too.
1606 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1607 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1608 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1609 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1610 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1612 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1617 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1618 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1622 if (s->s3->alpn_selected != NULL) {
1623 const unsigned char *selected = s->s3->alpn_selected;
1624 unsigned int len = s->s3->alpn_selected_len;
1626 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1628 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1632 memcpy(ret, selected, len);
1638 if ((extdatalen = ret - orig - 2) == 0)
1641 s2n(extdatalen, orig);
1646 * Save the ALPN extension in a ClientHello.
1647 * pkt: the contents of the ALPN extension, not including type and length.
1648 * al: a pointer to the alert value to send in the event of a failure.
1649 * returns: 1 on success, 0 on error.
1651 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1653 PACKET protocol_list, save_protocol_list, protocol;
1655 *al = SSL_AD_DECODE_ERROR;
1657 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1658 || PACKET_remaining(&protocol_list) < 2) {
1662 save_protocol_list = protocol_list;
1664 /* Protocol names can't be empty. */
1665 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1666 || PACKET_remaining(&protocol) == 0) {
1669 } while (PACKET_remaining(&protocol_list) != 0);
1671 if (!PACKET_memdup(&save_protocol_list,
1672 &s->s3->alpn_proposed,
1673 &s->s3->alpn_proposed_len)) {
1674 *al = TLS1_AD_INTERNAL_ERROR;
1682 * Process the ALPN extension in a ClientHello.
1683 * ret: a pointer to the TLSEXT return value: SSL_TLSEXT_ERR_*
1684 * al: a pointer to the alert value to send in the event of a failure.
1685 * returns 1 on success, 0
1687 static int tls1_alpn_handle_client_hello_late(SSL *s, int *ret, int *al)
1689 const unsigned char *selected = NULL;
1690 unsigned char selected_len = 0;
1692 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1693 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1694 s->s3->alpn_proposed,
1695 s->s3->alpn_proposed_len,
1696 s->ctx->alpn_select_cb_arg);
1698 if (r == SSL_TLSEXT_ERR_OK) {
1699 OPENSSL_free(s->s3->alpn_selected);
1700 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1701 if (s->s3->alpn_selected == NULL) {
1702 *al = SSL_AD_INTERNAL_ERROR;
1703 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1706 s->s3->alpn_selected_len = selected_len;
1707 #ifndef OPENSSL_NO_NEXTPROTONEG
1708 /* ALPN takes precedence over NPN. */
1709 s->s3->next_proto_neg_seen = 0;
1712 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1713 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1721 #ifndef OPENSSL_NO_EC
1723 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1724 * SecureTransport using the TLS extension block in |pkt|.
1725 * Safari, since 10.6, sends exactly these extensions, in this order:
1730 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1731 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1732 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1733 * 10.8..10.8.3 (which don't work).
1735 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1741 static const unsigned char kSafariExtensionsBlock[] = {
1742 0x00, 0x0a, /* elliptic_curves extension */
1743 0x00, 0x08, /* 8 bytes */
1744 0x00, 0x06, /* 6 bytes of curve ids */
1745 0x00, 0x17, /* P-256 */
1746 0x00, 0x18, /* P-384 */
1747 0x00, 0x19, /* P-521 */
1749 0x00, 0x0b, /* ec_point_formats */
1750 0x00, 0x02, /* 2 bytes */
1751 0x01, /* 1 point format */
1752 0x00, /* uncompressed */
1753 /* The following is only present in TLS 1.2 */
1754 0x00, 0x0d, /* signature_algorithms */
1755 0x00, 0x0c, /* 12 bytes */
1756 0x00, 0x0a, /* 10 bytes */
1757 0x05, 0x01, /* SHA-384/RSA */
1758 0x04, 0x01, /* SHA-256/RSA */
1759 0x02, 0x01, /* SHA-1/RSA */
1760 0x04, 0x03, /* SHA-256/ECDSA */
1761 0x02, 0x03, /* SHA-1/ECDSA */
1764 /* Length of the common prefix (first two extensions). */
1765 static const size_t kSafariCommonExtensionsLength = 18;
1769 if (!PACKET_forward(&tmppkt, 2)
1770 || !PACKET_get_net_2(&tmppkt, &type)
1771 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1775 if (type != TLSEXT_TYPE_server_name)
1778 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1779 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1781 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1784 #endif /* !OPENSSL_NO_EC */
1787 * Parse ClientHello extensions and stash extension info in various parts of
1788 * the SSL object. Verify that there are no duplicate extensions.
1790 * Behaviour upon resumption is extension-specific. If the extension has no
1791 * effect during resumption, it is parsed (to verify its format) but otherwise
1794 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1795 * Upon failure, sets |al| to the appropriate alert.
1797 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1800 int renegotiate_seen = 0;
1803 *al = SSL_AD_DECODE_ERROR;
1804 s->servername_done = 0;
1805 s->tlsext_status_type = -1;
1806 #ifndef OPENSSL_NO_NEXTPROTONEG
1807 s->s3->next_proto_neg_seen = 0;
1810 OPENSSL_free(s->s3->alpn_selected);
1811 s->s3->alpn_selected = NULL;
1812 s->s3->alpn_selected_len = 0;
1813 OPENSSL_free(s->s3->alpn_proposed);
1814 s->s3->alpn_proposed = NULL;
1815 s->s3->alpn_proposed_len = 0;
1816 #ifndef OPENSSL_NO_HEARTBEATS
1817 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1818 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1821 #ifndef OPENSSL_NO_EC
1822 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1823 ssl_check_for_safari(s, pkt);
1824 # endif /* !OPENSSL_NO_EC */
1826 /* Clear any signature algorithms extension received */
1827 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1828 s->s3->tmp.peer_sigalgs = NULL;
1829 #ifdef TLSEXT_TYPE_encrypt_then_mac
1830 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1833 #ifndef OPENSSL_NO_SRP
1834 OPENSSL_free(s->srp_ctx.login);
1835 s->srp_ctx.login = NULL;
1838 s->srtp_profile = NULL;
1840 if (PACKET_remaining(pkt) == 0)
1843 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1846 if (!tls1_check_duplicate_extensions(&extensions))
1850 * We parse all extensions to ensure the ClientHello is well-formed but,
1851 * unless an extension specifies otherwise, we ignore extensions upon
1854 while (PACKET_get_net_2(&extensions, &type)) {
1856 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1859 if (s->tlsext_debug_cb)
1860 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1861 PACKET_remaining(&extension),
1862 s->tlsext_debug_arg);
1864 if (type == TLSEXT_TYPE_renegotiate) {
1865 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
1867 renegotiate_seen = 1;
1868 } else if (s->version == SSL3_VERSION) {
1871 * The servername extension is treated as follows:
1873 * - Only the hostname type is supported with a maximum length of 255.
1874 * - The servername is rejected if too long or if it contains zeros,
1875 * in which case an fatal alert is generated.
1876 * - The servername field is maintained together with the session cache.
1877 * - When a session is resumed, the servername call back invoked in order
1878 * to allow the application to position itself to the right context.
1879 * - The servername is acknowledged if it is new for a session or when
1880 * it is identical to a previously used for the same session.
1881 * Applications can control the behaviour. They can at any time
1882 * set a 'desirable' servername for a new SSL object. This can be the
1883 * case for example with HTTPS when a Host: header field is received and
1884 * a renegotiation is requested. In this case, a possible servername
1885 * presented in the new client hello is only acknowledged if it matches
1886 * the value of the Host: field.
1887 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1888 * if they provide for changing an explicit servername context for the
1889 * session, i.e. when the session has been established with a servername
1891 * - On session reconnect, the servername extension may be absent.
1895 else if (type == TLSEXT_TYPE_server_name) {
1896 unsigned int servname_type;
1897 PACKET sni, hostname;
1899 if (!PACKET_as_length_prefixed_2(&extension, &sni)
1900 /* ServerNameList must be at least 1 byte long. */
1901 || PACKET_remaining(&sni) == 0) {
1906 * Although the server_name extension was intended to be
1907 * extensible to new name types, RFC 4366 defined the
1908 * syntax inextensibility and OpenSSL 1.0.x parses it as
1910 * RFC 6066 corrected the mistake but adding new name types
1911 * is nevertheless no longer feasible, so act as if no other
1912 * SNI types can exist, to simplify parsing.
1914 * Also note that the RFC permits only one SNI value per type,
1915 * i.e., we can only have a single hostname.
1917 if (!PACKET_get_1(&sni, &servname_type)
1918 || servname_type != TLSEXT_NAMETYPE_host_name
1919 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1924 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1925 *al = TLS1_AD_UNRECOGNIZED_NAME;
1929 if (PACKET_contains_zero_byte(&hostname)) {
1930 *al = TLS1_AD_UNRECOGNIZED_NAME;
1934 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1935 *al = TLS1_AD_INTERNAL_ERROR;
1939 s->servername_done = 1;
1942 * TODO(openssl-team): if the SNI doesn't match, we MUST
1943 * fall back to a full handshake.
1945 s->servername_done = s->session->tlsext_hostname
1946 && PACKET_equal(&hostname, s->session->tlsext_hostname,
1947 strlen(s->session->tlsext_hostname));
1950 #ifndef OPENSSL_NO_SRP
1951 else if (type == TLSEXT_TYPE_srp) {
1954 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
1957 if (PACKET_contains_zero_byte(&srp_I))
1961 * TODO(openssl-team): currently, we re-authenticate the user
1962 * upon resumption. Instead, we MUST ignore the login.
1964 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
1965 *al = TLS1_AD_INTERNAL_ERROR;
1971 #ifndef OPENSSL_NO_EC
1972 else if (type == TLSEXT_TYPE_ec_point_formats) {
1973 PACKET ec_point_format_list;
1975 if (!PACKET_as_length_prefixed_1(&extension,
1976 &ec_point_format_list)
1977 || PACKET_remaining(&ec_point_format_list) == 0) {
1982 if (!PACKET_memdup(&ec_point_format_list,
1983 &s->session->tlsext_ecpointformatlist,
1984 &s->session->tlsext_ecpointformatlist_length)) {
1985 *al = TLS1_AD_INTERNAL_ERROR;
1989 } else if (type == TLSEXT_TYPE_elliptic_curves) {
1990 PACKET elliptic_curve_list;
1992 /* Each NamedCurve is 2 bytes and we must have at least 1. */
1993 if (!PACKET_as_length_prefixed_2(&extension,
1994 &elliptic_curve_list)
1995 || PACKET_remaining(&elliptic_curve_list) == 0
1996 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
2001 if (!PACKET_memdup(&elliptic_curve_list,
2002 &s->session->tlsext_ellipticcurvelist,
2003 &s->session->tlsext_ellipticcurvelist_length)) {
2004 *al = TLS1_AD_INTERNAL_ERROR;
2009 #endif /* OPENSSL_NO_EC */
2010 else if (type == TLSEXT_TYPE_session_ticket) {
2011 if (s->tls_session_ticket_ext_cb &&
2012 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
2013 PACKET_remaining(&extension),
2014 s->tls_session_ticket_ext_cb_arg)) {
2015 *al = TLS1_AD_INTERNAL_ERROR;
2018 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2019 PACKET supported_sig_algs;
2021 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2022 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2023 || PACKET_remaining(&supported_sig_algs) == 0) {
2028 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2029 PACKET_remaining(&supported_sig_algs))) {
2033 } else if (type == TLSEXT_TYPE_status_request) {
2034 if (!PACKET_get_1(&extension,
2035 (unsigned int *)&s->tlsext_status_type)) {
2039 #ifndef OPENSSL_NO_OCSP
2040 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2041 const unsigned char *ext_data;
2042 PACKET responder_id_list, exts;
2043 if (!PACKET_get_length_prefixed_2(&extension, &responder_id_list))
2046 while (PACKET_remaining(&responder_id_list) > 0) {
2048 PACKET responder_id;
2049 const unsigned char *id_data;
2051 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2053 || PACKET_remaining(&responder_id) == 0) {
2057 if (s->tlsext_ocsp_ids == NULL
2058 && (s->tlsext_ocsp_ids =
2059 sk_OCSP_RESPID_new_null()) == NULL) {
2060 *al = SSL_AD_INTERNAL_ERROR;
2064 id_data = PACKET_data(&responder_id);
2065 id = d2i_OCSP_RESPID(NULL, &id_data,
2066 PACKET_remaining(&responder_id));
2070 if (id_data != PACKET_end(&responder_id)) {
2071 OCSP_RESPID_free(id);
2075 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2076 OCSP_RESPID_free(id);
2077 *al = SSL_AD_INTERNAL_ERROR;
2082 /* Read in request_extensions */
2083 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2086 if (PACKET_remaining(&exts) > 0) {
2087 ext_data = PACKET_data(&exts);
2088 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2089 X509_EXTENSION_free);
2090 s->tlsext_ocsp_exts =
2091 d2i_X509_EXTENSIONS(NULL, &ext_data,
2092 PACKET_remaining(&exts));
2093 if (s->tlsext_ocsp_exts == NULL
2094 || ext_data != PACKET_end(&exts)) {
2102 * We don't know what to do with any other type so ignore it.
2104 s->tlsext_status_type = -1;
2107 #ifndef OPENSSL_NO_HEARTBEATS
2108 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2109 unsigned int hbtype;
2111 if (!PACKET_get_1(&extension, &hbtype)
2112 || PACKET_remaining(&extension)) {
2113 *al = SSL_AD_DECODE_ERROR;
2117 case 0x01: /* Client allows us to send HB requests */
2118 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2120 case 0x02: /* Client doesn't accept HB requests */
2121 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2122 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2125 *al = SSL_AD_ILLEGAL_PARAMETER;
2130 #ifndef OPENSSL_NO_NEXTPROTONEG
2131 else if (type == TLSEXT_TYPE_next_proto_neg &&
2132 s->s3->tmp.finish_md_len == 0) {
2134 * We shouldn't accept this extension on a
2137 * s->new_session will be set on renegotiation, but we
2138 * probably shouldn't rely that it couldn't be set on
2139 * the initial renegotiation too in certain cases (when
2140 * there's some other reason to disallow resuming an
2141 * earlier session -- the current code won't be doing
2142 * anything like that, but this might change).
2144 * A valid sign that there's been a previous handshake
2145 * in this connection is if s->s3->tmp.finish_md_len >
2146 * 0. (We are talking about a check that will happen
2147 * in the Hello protocol round, well before a new
2148 * Finished message could have been computed.)
2150 s->s3->next_proto_neg_seen = 1;
2154 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2155 s->s3->tmp.finish_md_len == 0) {
2156 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2160 /* session ticket processed earlier */
2161 #ifndef OPENSSL_NO_SRTP
2162 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2163 && type == TLSEXT_TYPE_use_srtp) {
2164 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2168 #ifdef TLSEXT_TYPE_encrypt_then_mac
2169 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2170 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2173 * Note: extended master secret extension handled in
2174 * tls_check_serverhello_tlsext_early()
2178 * If this ClientHello extension was unhandled and this is a
2179 * nonresumed connection, check whether the extension is a custom
2180 * TLS Extension (has a custom_srv_ext_record), and if so call the
2181 * callback and record the extension number so that an appropriate
2182 * ServerHello may be later returned.
2185 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2186 PACKET_remaining(&extension), al) <= 0)
2191 if (PACKET_remaining(pkt) != 0) {
2192 /* tls1_check_duplicate_extensions should ensure this never happens. */
2193 *al = SSL_AD_INTERNAL_ERROR;
2199 /* Need RI if renegotiating */
2201 if (!renegotiate_seen && s->renegotiate &&
2202 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2203 *al = SSL_AD_HANDSHAKE_FAILURE;
2204 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2205 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2210 * This function currently has no state to clean up, so it returns directly.
2211 * If parsing fails at any point, the function returns early.
2212 * The SSL object may be left with partial data from extensions, but it must
2213 * then no longer be used, and clearing it up will free the leftovers.
2218 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2221 custom_ext_init(&s->cert->srv_ext);
2222 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2223 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2226 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2227 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2233 #ifndef OPENSSL_NO_NEXTPROTONEG
2235 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2236 * elements of zero length are allowed and the set of elements must exactly
2237 * fill the length of the block.
2239 static char ssl_next_proto_validate(PACKET *pkt)
2241 PACKET tmp_protocol;
2243 while (PACKET_remaining(pkt)) {
2244 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2245 || PACKET_remaining(&tmp_protocol) == 0)
2253 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2255 unsigned int length, type, size;
2256 int tlsext_servername = 0;
2257 int renegotiate_seen = 0;
2259 #ifndef OPENSSL_NO_NEXTPROTONEG
2260 s->s3->next_proto_neg_seen = 0;
2262 s->tlsext_ticket_expected = 0;
2264 OPENSSL_free(s->s3->alpn_selected);
2265 s->s3->alpn_selected = NULL;
2266 #ifndef OPENSSL_NO_HEARTBEATS
2267 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2268 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2271 #ifdef TLSEXT_TYPE_encrypt_then_mac
2272 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2275 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2277 if (!PACKET_get_net_2(pkt, &length))
2280 if (PACKET_remaining(pkt) != length) {
2281 *al = SSL_AD_DECODE_ERROR;
2285 if (!tls1_check_duplicate_extensions(pkt)) {
2286 *al = SSL_AD_DECODE_ERROR;
2290 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2291 const unsigned char *data;
2294 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2295 || !PACKET_peek_bytes(&spkt, &data, size))
2298 if (s->tlsext_debug_cb)
2299 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2301 if (type == TLSEXT_TYPE_renegotiate) {
2302 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2304 renegotiate_seen = 1;
2305 } else if (s->version == SSL3_VERSION) {
2306 } else if (type == TLSEXT_TYPE_server_name) {
2307 if (s->tlsext_hostname == NULL || size > 0) {
2308 *al = TLS1_AD_UNRECOGNIZED_NAME;
2311 tlsext_servername = 1;
2313 #ifndef OPENSSL_NO_EC
2314 else if (type == TLSEXT_TYPE_ec_point_formats) {
2315 unsigned int ecpointformatlist_length;
2316 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2317 || ecpointformatlist_length != size - 1) {
2318 *al = TLS1_AD_DECODE_ERROR;
2322 s->session->tlsext_ecpointformatlist_length = 0;
2323 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2324 if ((s->session->tlsext_ecpointformatlist =
2325 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2326 *al = TLS1_AD_INTERNAL_ERROR;
2329 s->session->tlsext_ecpointformatlist_length =
2330 ecpointformatlist_length;
2331 if (!PACKET_copy_bytes(&spkt,
2332 s->session->tlsext_ecpointformatlist,
2333 ecpointformatlist_length)) {
2334 *al = TLS1_AD_DECODE_ERROR;
2340 #endif /* OPENSSL_NO_EC */
2342 else if (type == TLSEXT_TYPE_session_ticket) {
2343 if (s->tls_session_ticket_ext_cb &&
2344 !s->tls_session_ticket_ext_cb(s, data, size,
2345 s->tls_session_ticket_ext_cb_arg))
2347 *al = TLS1_AD_INTERNAL_ERROR;
2350 if (!tls_use_ticket(s) || (size > 0)) {
2351 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2354 s->tlsext_ticket_expected = 1;
2356 else if (type == TLSEXT_TYPE_status_request) {
2358 * MUST be empty and only sent if we've requested a status
2361 if ((s->tlsext_status_type == -1) || (size > 0)) {
2362 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2365 /* Set flag to expect CertificateStatus message */
2366 s->tlsext_status_expected = 1;
2368 #ifndef OPENSSL_NO_CT
2370 * Only take it if we asked for it - i.e if there is no CT validation
2371 * callback set, then a custom extension MAY be processing it, so we
2372 * need to let control continue to flow to that.
2374 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2375 s->ct_validation_callback != NULL) {
2376 /* Simply copy it off for later processing */
2377 if (s->tlsext_scts != NULL) {
2378 OPENSSL_free(s->tlsext_scts);
2379 s->tlsext_scts = NULL;
2381 s->tlsext_scts_len = size;
2383 s->tlsext_scts = OPENSSL_malloc(size);
2384 if (s->tlsext_scts == NULL) {
2385 *al = TLS1_AD_INTERNAL_ERROR;
2388 memcpy(s->tlsext_scts, data, size);
2392 #ifndef OPENSSL_NO_NEXTPROTONEG
2393 else if (type == TLSEXT_TYPE_next_proto_neg &&
2394 s->s3->tmp.finish_md_len == 0) {
2395 unsigned char *selected;
2396 unsigned char selected_len;
2397 /* We must have requested it. */
2398 if (s->ctx->next_proto_select_cb == NULL) {
2399 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2402 /* The data must be valid */
2403 if (!ssl_next_proto_validate(&spkt)) {
2404 *al = TLS1_AD_DECODE_ERROR;
2408 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2410 s->ctx->next_proto_select_cb_arg) !=
2411 SSL_TLSEXT_ERR_OK) {
2412 *al = TLS1_AD_INTERNAL_ERROR;
2415 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2416 if (s->next_proto_negotiated == NULL) {
2417 *al = TLS1_AD_INTERNAL_ERROR;
2420 memcpy(s->next_proto_negotiated, selected, selected_len);
2421 s->next_proto_negotiated_len = selected_len;
2422 s->s3->next_proto_neg_seen = 1;
2426 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2428 /* We must have requested it. */
2429 if (!s->s3->alpn_sent) {
2430 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2434 * The extension data consists of:
2435 * uint16 list_length
2436 * uint8 proto_length;
2437 * uint8 proto[proto_length];
2439 if (!PACKET_get_net_2(&spkt, &len)
2440 || PACKET_remaining(&spkt) != len
2441 || !PACKET_get_1(&spkt, &len)
2442 || PACKET_remaining(&spkt) != len) {
2443 *al = TLS1_AD_DECODE_ERROR;
2446 OPENSSL_free(s->s3->alpn_selected);
2447 s->s3->alpn_selected = OPENSSL_malloc(len);
2448 if (s->s3->alpn_selected == NULL) {
2449 *al = TLS1_AD_INTERNAL_ERROR;
2452 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2453 *al = TLS1_AD_DECODE_ERROR;
2456 s->s3->alpn_selected_len = len;
2458 #ifndef OPENSSL_NO_HEARTBEATS
2459 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2460 unsigned int hbtype;
2461 if (!PACKET_get_1(&spkt, &hbtype)) {
2462 *al = SSL_AD_DECODE_ERROR;
2466 case 0x01: /* Server allows us to send HB requests */
2467 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2469 case 0x02: /* Server doesn't accept HB requests */
2470 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2471 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2474 *al = SSL_AD_ILLEGAL_PARAMETER;
2479 #ifndef OPENSSL_NO_SRTP
2480 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2481 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2485 #ifdef TLSEXT_TYPE_encrypt_then_mac
2486 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2487 /* Ignore if inappropriate ciphersuite */
2488 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2489 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2490 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2493 else if (type == TLSEXT_TYPE_extended_master_secret) {
2494 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2496 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2499 * If this extension type was not otherwise handled, but matches a
2500 * custom_cli_ext_record, then send it to the c callback
2502 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2506 if (PACKET_remaining(pkt) != 0) {
2507 *al = SSL_AD_DECODE_ERROR;
2511 if (!s->hit && tlsext_servername == 1) {
2512 if (s->tlsext_hostname) {
2513 if (s->session->tlsext_hostname == NULL) {
2514 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
2515 if (!s->session->tlsext_hostname) {
2516 *al = SSL_AD_UNRECOGNIZED_NAME;
2520 *al = SSL_AD_DECODE_ERROR;
2529 * Determine if we need to see RI. Strictly speaking if we want to avoid
2530 * an attack we should *always* see RI even on initial server hello
2531 * because the client doesn't see any renegotiation during an attack.
2532 * However this would mean we could not connect to any server which
2533 * doesn't support RI so for the immediate future tolerate RI absence on
2534 * initial connect only.
2536 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2537 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2538 *al = SSL_AD_HANDSHAKE_FAILURE;
2539 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2540 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2546 * Check extended master secret extension is consistent with
2549 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2550 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2551 *al = SSL_AD_HANDSHAKE_FAILURE;
2552 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2560 int ssl_prepare_clienthello_tlsext(SSL *s)
2562 s->s3->alpn_sent = 0;
2566 int ssl_prepare_serverhello_tlsext(SSL *s)
2571 static int ssl_check_clienthello_tlsext_early(SSL *s)
2573 int ret = SSL_TLSEXT_ERR_NOACK;
2574 int al = SSL_AD_UNRECOGNIZED_NAME;
2576 #ifndef OPENSSL_NO_EC
2578 * The handling of the ECPointFormats extension is done elsewhere, namely
2579 * in ssl3_choose_cipher in s3_lib.c.
2582 * The handling of the EllipticCurves extension is done elsewhere, namely
2583 * in ssl3_choose_cipher in s3_lib.c.
2587 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2589 s->ctx->tlsext_servername_callback(s, &al,
2590 s->ctx->tlsext_servername_arg);
2591 else if (s->initial_ctx != NULL
2592 && s->initial_ctx->tlsext_servername_callback != 0)
2594 s->initial_ctx->tlsext_servername_callback(s, &al,
2596 initial_ctx->tlsext_servername_arg);
2599 case SSL_TLSEXT_ERR_ALERT_FATAL:
2600 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2603 case SSL_TLSEXT_ERR_ALERT_WARNING:
2604 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2607 case SSL_TLSEXT_ERR_NOACK:
2608 s->servername_done = 0;
2613 /* Initialise digests to default values */
2614 void ssl_set_default_md(SSL *s)
2616 const EVP_MD **pmd = s->s3->tmp.md;
2617 #ifndef OPENSSL_NO_DSA
2618 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2620 #ifndef OPENSSL_NO_RSA
2621 if (SSL_USE_SIGALGS(s))
2622 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2624 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2625 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2627 #ifndef OPENSSL_NO_EC
2628 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2630 #ifndef OPENSSL_NO_GOST
2631 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2632 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2633 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2637 int tls1_set_server_sigalgs(SSL *s)
2642 /* Clear any shared signature algorithms */
2643 OPENSSL_free(s->cert->shared_sigalgs);
2644 s->cert->shared_sigalgs = NULL;
2645 s->cert->shared_sigalgslen = 0;
2646 /* Clear certificate digests and validity flags */
2647 for (i = 0; i < SSL_PKEY_NUM; i++) {
2648 s->s3->tmp.md[i] = NULL;
2649 s->s3->tmp.valid_flags[i] = 0;
2652 /* If sigalgs received process it. */
2653 if (s->s3->tmp.peer_sigalgs) {
2654 if (!tls1_process_sigalgs(s)) {
2655 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2656 al = SSL_AD_INTERNAL_ERROR;
2659 /* Fatal error is no shared signature algorithms */
2660 if (!s->cert->shared_sigalgs) {
2661 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2662 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2663 al = SSL_AD_ILLEGAL_PARAMETER;
2667 ssl_set_default_md(s);
2671 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2675 int ssl_check_clienthello_tlsext_late(SSL *s)
2677 int ret = SSL_TLSEXT_ERR_OK;
2678 int al = SSL_AD_INTERNAL_ERROR;
2681 * If status request then ask callback what to do. Note: this must be
2682 * called after servername callbacks in case the certificate has changed,
2683 * and must be called after the cipher has been chosen because this may
2684 * influence which certificate is sent
2686 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2688 CERT_PKEY *certpkey;
2689 certpkey = ssl_get_server_send_pkey(s);
2690 /* If no certificate can't return certificate status */
2691 if (certpkey == NULL) {
2692 s->tlsext_status_expected = 0;
2696 * Set current certificate to one we will use so SSL_get_certificate
2697 * et al can pick it up.
2699 s->cert->key = certpkey;
2700 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2702 /* We don't want to send a status request response */
2703 case SSL_TLSEXT_ERR_NOACK:
2704 s->tlsext_status_expected = 0;
2706 /* status request response should be sent */
2707 case SSL_TLSEXT_ERR_OK:
2708 if (s->tlsext_ocsp_resp)
2709 s->tlsext_status_expected = 1;
2711 s->tlsext_status_expected = 0;
2713 /* something bad happened */
2714 case SSL_TLSEXT_ERR_ALERT_FATAL:
2715 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2716 al = SSL_AD_INTERNAL_ERROR;
2720 s->tlsext_status_expected = 0;
2722 if (!tls1_alpn_handle_client_hello_late(s, &ret, &al)) {
2728 case SSL_TLSEXT_ERR_ALERT_FATAL:
2729 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2732 case SSL_TLSEXT_ERR_ALERT_WARNING:
2733 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2741 int ssl_check_serverhello_tlsext(SSL *s)
2743 int ret = SSL_TLSEXT_ERR_NOACK;
2744 int al = SSL_AD_UNRECOGNIZED_NAME;
2746 #ifndef OPENSSL_NO_EC
2748 * If we are client and using an elliptic curve cryptography cipher
2749 * suite, then if server returns an EC point formats lists extension it
2750 * must contain uncompressed.
2752 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2753 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2754 if ((s->tlsext_ecpointformatlist != NULL)
2755 && (s->tlsext_ecpointformatlist_length > 0)
2756 && (s->session->tlsext_ecpointformatlist != NULL)
2757 && (s->session->tlsext_ecpointformatlist_length > 0)
2758 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2759 /* we are using an ECC cipher */
2761 unsigned char *list;
2762 int found_uncompressed = 0;
2763 list = s->session->tlsext_ecpointformatlist;
2764 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2765 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2766 found_uncompressed = 1;
2770 if (!found_uncompressed) {
2771 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2772 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2776 ret = SSL_TLSEXT_ERR_OK;
2777 #endif /* OPENSSL_NO_EC */
2779 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2781 s->ctx->tlsext_servername_callback(s, &al,
2782 s->ctx->tlsext_servername_arg);
2783 else if (s->initial_ctx != NULL
2784 && s->initial_ctx->tlsext_servername_callback != 0)
2786 s->initial_ctx->tlsext_servername_callback(s, &al,
2788 initial_ctx->tlsext_servername_arg);
2791 * Ensure we get sensible values passed to tlsext_status_cb in the event
2792 * that we don't receive a status message
2794 OPENSSL_free(s->tlsext_ocsp_resp);
2795 s->tlsext_ocsp_resp = NULL;
2796 s->tlsext_ocsp_resplen = -1;
2799 case SSL_TLSEXT_ERR_ALERT_FATAL:
2800 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2803 case SSL_TLSEXT_ERR_ALERT_WARNING:
2804 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2807 case SSL_TLSEXT_ERR_NOACK:
2808 s->servername_done = 0;
2814 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2817 if (s->version < SSL3_VERSION)
2819 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2820 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2824 if (ssl_check_serverhello_tlsext(s) <= 0) {
2825 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2832 * Since the server cache lookup is done early on in the processing of the
2833 * ClientHello and other operations depend on the result some extensions
2834 * need to be handled at the same time.
2836 * Two extensions are currently handled, session ticket and extended master
2839 * session_id: ClientHello session ID.
2840 * ext: ClientHello extensions (including length prefix)
2841 * ret: (output) on return, if a ticket was decrypted, then this is set to
2842 * point to the resulting session.
2844 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2845 * ciphersuite, in which case we have no use for session tickets and one will
2846 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2849 * -1: fatal error, either from parsing or decrypting the ticket.
2850 * 0: no ticket was found (or was ignored, based on settings).
2851 * 1: a zero length extension was found, indicating that the client supports
2852 * session tickets but doesn't currently have one to offer.
2853 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2854 * couldn't be decrypted because of a non-fatal error.
2855 * 3: a ticket was successfully decrypted and *ret was set.
2858 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2859 * a new session ticket to the client because the client indicated support
2860 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2861 * a session ticket or we couldn't use the one it gave us, or if
2862 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2863 * Otherwise, s->tlsext_ticket_expected is set to 0.
2865 * For extended master secret flag is set if the extension is present.
2868 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2869 const PACKET *session_id,
2873 PACKET local_ext = *ext;
2876 int have_ticket = 0;
2877 int use_ticket = tls_use_ticket(s);
2880 s->tlsext_ticket_expected = 0;
2881 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2884 * If tickets disabled behave as if no ticket present to permit stateful
2887 if ((s->version <= SSL3_VERSION))
2890 if (!PACKET_get_net_2(&local_ext, &i)) {
2894 while (PACKET_remaining(&local_ext) >= 4) {
2895 unsigned int type, size;
2897 if (!PACKET_get_net_2(&local_ext, &type)
2898 || !PACKET_get_net_2(&local_ext, &size)) {
2899 /* Shouldn't ever happen */
2903 if (PACKET_remaining(&local_ext) < size) {
2907 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
2909 const unsigned char *etick;
2911 /* Duplicate extension */
2912 if (have_ticket != 0) {
2920 * The client will accept a ticket but doesn't currently have
2923 s->tlsext_ticket_expected = 1;
2927 if (s->tls_session_secret_cb) {
2929 * Indicate that the ticket couldn't be decrypted rather than
2930 * generating the session from ticket now, trigger
2931 * abbreviated handshake based on external mechanism to
2932 * calculate the master secret later.
2937 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
2938 /* Shouldn't ever happen */
2942 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
2943 PACKET_remaining(session_id), ret);
2945 case 2: /* ticket couldn't be decrypted */
2946 s->tlsext_ticket_expected = 1;
2949 case 3: /* ticket was decrypted */
2952 case 4: /* ticket decrypted but need to renew */
2953 s->tlsext_ticket_expected = 1;
2956 default: /* fatal error */
2962 if (type == TLSEXT_TYPE_extended_master_secret)
2963 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2964 if (!PACKET_forward(&local_ext, size)) {
2970 if (have_ticket == 0)
2977 * tls_decrypt_ticket attempts to decrypt a session ticket.
2979 * etick: points to the body of the session ticket extension.
2980 * eticklen: the length of the session tickets extension.
2981 * sess_id: points at the session ID.
2982 * sesslen: the length of the session ID.
2983 * psess: (output) on return, if a ticket was decrypted, then this is set to
2984 * point to the resulting session.
2987 * -2: fatal error, malloc failure.
2988 * -1: fatal error, either from parsing or decrypting the ticket.
2989 * 2: the ticket couldn't be decrypted.
2990 * 3: a ticket was successfully decrypted and *psess was set.
2991 * 4: same as 3, but the ticket needs to be renewed.
2993 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2994 int eticklen, const unsigned char *sess_id,
2995 int sesslen, SSL_SESSION **psess)
2998 unsigned char *sdec;
2999 const unsigned char *p;
3000 int slen, mlen, renew_ticket = 0, ret = -1;
3001 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3002 HMAC_CTX *hctx = NULL;
3003 EVP_CIPHER_CTX *ctx;
3004 SSL_CTX *tctx = s->initial_ctx;
3005 /* Need at least keyname + iv + some encrypted data */
3008 /* Initialize session ticket encryption and HMAC contexts */
3009 hctx = HMAC_CTX_new();
3012 ctx = EVP_CIPHER_CTX_new();
3017 if (tctx->tlsext_ticket_key_cb) {
3018 unsigned char *nctick = (unsigned char *)etick;
3019 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3030 /* Check key name matches */
3031 if (memcmp(etick, tctx->tlsext_tick_key_name,
3032 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3036 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3037 sizeof(tctx->tlsext_tick_hmac_key),
3038 EVP_sha256(), NULL) <= 0
3039 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3040 tctx->tlsext_tick_aes_key,
3041 etick + sizeof(tctx->tlsext_tick_key_name)) <= 0) {
3046 * Attempt to process session ticket, first conduct sanity and integrity
3049 mlen = HMAC_size(hctx);
3054 /* Check HMAC of encrypted ticket */
3055 if (HMAC_Update(hctx, etick, eticklen) <= 0
3056 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3059 HMAC_CTX_free(hctx);
3060 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3061 EVP_CIPHER_CTX_free(ctx);
3064 /* Attempt to decrypt session data */
3065 /* Move p after IV to start of encrypted ticket, update length */
3066 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3067 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3068 sdec = OPENSSL_malloc(eticklen);
3070 || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3071 EVP_CIPHER_CTX_free(ctx);
3075 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3076 EVP_CIPHER_CTX_free(ctx);
3081 EVP_CIPHER_CTX_free(ctx);
3085 sess = d2i_SSL_SESSION(NULL, &p, slen);
3089 * The session ID, if non-empty, is used by some clients to detect
3090 * that the ticket has been accepted. So we copy it to the session
3091 * structure. If it is empty set length to zero as required by
3095 memcpy(sess->session_id, sess_id, sesslen);
3096 sess->session_id_length = sesslen;
3105 * For session parse failure, indicate that we need to send a new ticket.
3109 EVP_CIPHER_CTX_free(ctx);
3110 HMAC_CTX_free(hctx);
3114 /* Tables to translate from NIDs to TLS v1.2 ids */
3121 static const tls12_lookup tls12_md[] = {
3122 {NID_md5, TLSEXT_hash_md5},
3123 {NID_sha1, TLSEXT_hash_sha1},
3124 {NID_sha224, TLSEXT_hash_sha224},
3125 {NID_sha256, TLSEXT_hash_sha256},
3126 {NID_sha384, TLSEXT_hash_sha384},
3127 {NID_sha512, TLSEXT_hash_sha512},
3128 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3129 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3130 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3133 static const tls12_lookup tls12_sig[] = {
3134 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3135 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3136 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3137 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3138 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3139 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3142 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3145 for (i = 0; i < tlen; i++) {
3146 if (table[i].nid == nid)
3152 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3155 for (i = 0; i < tlen; i++) {
3156 if ((table[i].id) == id)
3157 return table[i].nid;
3162 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3168 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3171 sig_id = tls12_get_sigid(pk);
3174 p[0] = (unsigned char)md_id;
3175 p[1] = (unsigned char)sig_id;
3179 int tls12_get_sigid(const EVP_PKEY *pk)
3181 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3188 unsigned char tlsext_hash;
3191 static const tls12_hash_info tls12_md_info[] = {
3192 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3193 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3194 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3195 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3196 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3197 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3198 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3199 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3200 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3203 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3209 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3211 if (tls12_md_info[i].tlsext_hash == hash_alg)
3212 return tls12_md_info + i;
3218 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3220 const tls12_hash_info *inf;
3221 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3223 inf = tls12_get_hash_info(hash_alg);
3226 return ssl_md(inf->md_idx);
3229 static int tls12_get_pkey_idx(unsigned char sig_alg)
3232 #ifndef OPENSSL_NO_RSA
3233 case TLSEXT_signature_rsa:
3234 return SSL_PKEY_RSA_SIGN;
3236 #ifndef OPENSSL_NO_DSA
3237 case TLSEXT_signature_dsa:
3238 return SSL_PKEY_DSA_SIGN;
3240 #ifndef OPENSSL_NO_EC
3241 case TLSEXT_signature_ecdsa:
3242 return SSL_PKEY_ECC;
3244 # ifndef OPENSSL_NO_GOST
3245 case TLSEXT_signature_gostr34102001:
3246 return SSL_PKEY_GOST01;
3248 case TLSEXT_signature_gostr34102012_256:
3249 return SSL_PKEY_GOST12_256;
3251 case TLSEXT_signature_gostr34102012_512:
3252 return SSL_PKEY_GOST12_512;
3258 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3259 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3260 int *psignhash_nid, const unsigned char *data)
3262 int sign_nid = NID_undef, hash_nid = NID_undef;
3263 if (!phash_nid && !psign_nid && !psignhash_nid)
3265 if (phash_nid || psignhash_nid) {
3266 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3268 *phash_nid = hash_nid;
3270 if (psign_nid || psignhash_nid) {
3271 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3273 *psign_nid = sign_nid;
3275 if (psignhash_nid) {
3276 if (sign_nid == NID_undef || hash_nid == NID_undef
3277 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3279 *psignhash_nid = NID_undef;
3283 /* Check to see if a signature algorithm is allowed */
3284 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3286 /* See if we have an entry in the hash table and it is enabled */
3287 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3288 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3290 /* See if public key algorithm allowed */
3291 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3293 /* Finally see if security callback allows it */
3294 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3298 * Get a mask of disabled public key algorithms based on supported signature
3299 * algorithms. For example if no signature algorithm supports RSA then RSA is
3303 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3305 const unsigned char *sigalgs;
3306 size_t i, sigalgslen;
3307 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3309 * Now go through all signature algorithms seeing if we support any for
3310 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3311 * down calls to security callback only check if we have to.
3313 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3314 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3315 switch (sigalgs[1]) {
3316 #ifndef OPENSSL_NO_RSA
3317 case TLSEXT_signature_rsa:
3318 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3322 #ifndef OPENSSL_NO_DSA
3323 case TLSEXT_signature_dsa:
3324 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3328 #ifndef OPENSSL_NO_EC
3329 case TLSEXT_signature_ecdsa:
3330 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3337 *pmask_a |= SSL_aRSA;
3339 *pmask_a |= SSL_aDSS;
3341 *pmask_a |= SSL_aECDSA;
3344 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3345 const unsigned char *psig, size_t psiglen)
3347 unsigned char *tmpout = out;
3349 for (i = 0; i < psiglen; i += 2, psig += 2) {
3350 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3351 *tmpout++ = psig[0];
3352 *tmpout++ = psig[1];
3355 return tmpout - out;
3358 /* Given preference and allowed sigalgs set shared sigalgs */
3359 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3360 const unsigned char *pref, size_t preflen,
3361 const unsigned char *allow, size_t allowlen)
3363 const unsigned char *ptmp, *atmp;
3364 size_t i, j, nmatch = 0;
3365 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3366 /* Skip disabled hashes or signature algorithms */
3367 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3369 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3370 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3373 shsig->rhash = ptmp[0];
3374 shsig->rsign = ptmp[1];
3375 tls1_lookup_sigalg(&shsig->hash_nid,
3377 &shsig->signandhash_nid, ptmp);
3387 /* Set shared signature algorithms for SSL structures */
3388 static int tls1_set_shared_sigalgs(SSL *s)
3390 const unsigned char *pref, *allow, *conf;
3391 size_t preflen, allowlen, conflen;
3393 TLS_SIGALGS *salgs = NULL;
3395 unsigned int is_suiteb = tls1_suiteb(s);
3397 OPENSSL_free(c->shared_sigalgs);
3398 c->shared_sigalgs = NULL;
3399 c->shared_sigalgslen = 0;
3400 /* If client use client signature algorithms if not NULL */
3401 if (!s->server && c->client_sigalgs && !is_suiteb) {
3402 conf = c->client_sigalgs;
3403 conflen = c->client_sigalgslen;
3404 } else if (c->conf_sigalgs && !is_suiteb) {
3405 conf = c->conf_sigalgs;
3406 conflen = c->conf_sigalgslen;
3408 conflen = tls12_get_psigalgs(s, &conf);
3409 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3412 allow = s->s3->tmp.peer_sigalgs;
3413 allowlen = s->s3->tmp.peer_sigalgslen;
3417 pref = s->s3->tmp.peer_sigalgs;
3418 preflen = s->s3->tmp.peer_sigalgslen;
3420 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3422 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3425 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3429 c->shared_sigalgs = salgs;
3430 c->shared_sigalgslen = nmatch;
3434 /* Set preferred digest for each key type */
3436 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3439 /* Extension ignored for inappropriate versions */
3440 if (!SSL_USE_SIGALGS(s))
3442 /* Should never happen */
3446 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3447 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3448 if (s->s3->tmp.peer_sigalgs == NULL)
3450 s->s3->tmp.peer_sigalgslen = dsize;
3451 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3455 int tls1_process_sigalgs(SSL *s)
3460 const EVP_MD **pmd = s->s3->tmp.md;
3461 uint32_t *pvalid = s->s3->tmp.valid_flags;
3463 TLS_SIGALGS *sigptr;
3464 if (!tls1_set_shared_sigalgs(s))
3467 for (i = 0, sigptr = c->shared_sigalgs;
3468 i < c->shared_sigalgslen; i++, sigptr++) {
3469 idx = tls12_get_pkey_idx(sigptr->rsign);
3470 if (idx > 0 && pmd[idx] == NULL) {
3471 md = tls12_get_hash(sigptr->rhash);
3473 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3474 if (idx == SSL_PKEY_RSA_SIGN) {
3475 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3476 pmd[SSL_PKEY_RSA_ENC] = md;
3482 * In strict mode leave unset digests as NULL to indicate we can't use
3483 * the certificate for signing.
3485 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3487 * Set any remaining keys to default values. NOTE: if alg is not
3488 * supported it stays as NULL.
3490 #ifndef OPENSSL_NO_DSA
3491 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3492 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3494 #ifndef OPENSSL_NO_RSA
3495 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3496 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3497 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3500 #ifndef OPENSSL_NO_EC
3501 if (pmd[SSL_PKEY_ECC] == NULL)
3502 pmd[SSL_PKEY_ECC] = EVP_sha1();
3504 # ifndef OPENSSL_NO_GOST
3505 if (pmd[SSL_PKEY_GOST01] == NULL)
3506 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3507 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3508 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3509 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3510 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3516 int SSL_get_sigalgs(SSL *s, int idx,
3517 int *psign, int *phash, int *psignhash,
3518 unsigned char *rsig, unsigned char *rhash)
3520 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3525 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3532 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3534 return s->s3->tmp.peer_sigalgslen / 2;
3537 int SSL_get_shared_sigalgs(SSL *s, int idx,
3538 int *psign, int *phash, int *psignhash,
3539 unsigned char *rsig, unsigned char *rhash)
3541 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3542 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3546 *phash = shsigalgs->hash_nid;
3548 *psign = shsigalgs->sign_nid;
3550 *psignhash = shsigalgs->signandhash_nid;
3552 *rsig = shsigalgs->rsign;
3554 *rhash = shsigalgs->rhash;
3555 return s->cert->shared_sigalgslen;
3558 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3562 int sigalgs[MAX_SIGALGLEN];
3565 static void get_sigorhash(int *psig, int *phash, const char *str)
3567 if (strcmp(str, "RSA") == 0) {
3568 *psig = EVP_PKEY_RSA;
3569 } else if (strcmp(str, "DSA") == 0) {
3570 *psig = EVP_PKEY_DSA;
3571 } else if (strcmp(str, "ECDSA") == 0) {
3572 *psig = EVP_PKEY_EC;
3574 *phash = OBJ_sn2nid(str);
3575 if (*phash == NID_undef)
3576 *phash = OBJ_ln2nid(str);
3580 static int sig_cb(const char *elem, int len, void *arg)
3582 sig_cb_st *sarg = arg;
3585 int sig_alg = NID_undef, hash_alg = NID_undef;
3588 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3590 if (len > (int)(sizeof(etmp) - 1))
3592 memcpy(etmp, elem, len);
3594 p = strchr(etmp, '+');
3602 get_sigorhash(&sig_alg, &hash_alg, etmp);
3603 get_sigorhash(&sig_alg, &hash_alg, p);
3605 if (sig_alg == NID_undef || hash_alg == NID_undef)
3608 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3609 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3612 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3613 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3618 * Set suppored signature algorithms based on a colon separated list of the
3619 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3621 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3625 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3629 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3632 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3635 unsigned char *sigalgs, *sptr;
3640 sigalgs = OPENSSL_malloc(salglen);
3641 if (sigalgs == NULL)
3643 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3644 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3645 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3647 if (rhash == -1 || rsign == -1)
3654 OPENSSL_free(c->client_sigalgs);
3655 c->client_sigalgs = sigalgs;
3656 c->client_sigalgslen = salglen;
3658 OPENSSL_free(c->conf_sigalgs);
3659 c->conf_sigalgs = sigalgs;
3660 c->conf_sigalgslen = salglen;
3666 OPENSSL_free(sigalgs);
3670 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3674 if (default_nid == -1)
3676 sig_nid = X509_get_signature_nid(x);
3678 return sig_nid == default_nid ? 1 : 0;
3679 for (i = 0; i < c->shared_sigalgslen; i++)
3680 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3685 /* Check to see if a certificate issuer name matches list of CA names */
3686 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3690 nm = X509_get_issuer_name(x);
3691 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3692 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3699 * Check certificate chain is consistent with TLS extensions and is usable by
3700 * server. This servers two purposes: it allows users to check chains before
3701 * passing them to the server and it allows the server to check chains before
3702 * attempting to use them.
3705 /* Flags which need to be set for a certificate when stict mode not set */
3707 #define CERT_PKEY_VALID_FLAGS \
3708 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3709 /* Strict mode flags */
3710 #define CERT_PKEY_STRICT_FLAGS \
3711 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3712 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3714 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3719 int check_flags = 0, strict_mode;
3720 CERT_PKEY *cpk = NULL;
3723 unsigned int suiteb_flags = tls1_suiteb(s);
3724 /* idx == -1 means checking server chains */
3726 /* idx == -2 means checking client certificate chains */
3729 idx = cpk - c->pkeys;
3731 cpk = c->pkeys + idx;
3732 pvalid = s->s3->tmp.valid_flags + idx;
3734 pk = cpk->privatekey;
3736 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3737 /* If no cert or key, forget it */
3743 idx = ssl_cert_type(x, pk);
3746 pvalid = s->s3->tmp.valid_flags + idx;
3748 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3749 check_flags = CERT_PKEY_STRICT_FLAGS;
3751 check_flags = CERT_PKEY_VALID_FLAGS;
3758 check_flags |= CERT_PKEY_SUITEB;
3759 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3760 if (ok == X509_V_OK)
3761 rv |= CERT_PKEY_SUITEB;
3762 else if (!check_flags)
3767 * Check all signature algorithms are consistent with signature
3768 * algorithms extension if TLS 1.2 or later and strict mode.
3770 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3772 unsigned char rsign = 0;
3773 if (s->s3->tmp.peer_sigalgs)
3775 /* If no sigalgs extension use defaults from RFC5246 */
3778 case SSL_PKEY_RSA_ENC:
3779 case SSL_PKEY_RSA_SIGN:
3780 rsign = TLSEXT_signature_rsa;
3781 default_nid = NID_sha1WithRSAEncryption;
3784 case SSL_PKEY_DSA_SIGN:
3785 rsign = TLSEXT_signature_dsa;
3786 default_nid = NID_dsaWithSHA1;
3790 rsign = TLSEXT_signature_ecdsa;
3791 default_nid = NID_ecdsa_with_SHA1;
3794 case SSL_PKEY_GOST01:
3795 rsign = TLSEXT_signature_gostr34102001;
3796 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3799 case SSL_PKEY_GOST12_256:
3800 rsign = TLSEXT_signature_gostr34102012_256;
3801 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3804 case SSL_PKEY_GOST12_512:
3805 rsign = TLSEXT_signature_gostr34102012_512;
3806 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3815 * If peer sent no signature algorithms extension and we have set
3816 * preferred signature algorithms check we support sha1.
3818 if (default_nid > 0 && c->conf_sigalgs) {
3820 const unsigned char *p = c->conf_sigalgs;
3821 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3822 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3825 if (j == c->conf_sigalgslen) {
3832 /* Check signature algorithm of each cert in chain */
3833 if (!tls1_check_sig_alg(c, x, default_nid)) {
3837 rv |= CERT_PKEY_EE_SIGNATURE;
3838 rv |= CERT_PKEY_CA_SIGNATURE;
3839 for (i = 0; i < sk_X509_num(chain); i++) {
3840 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3842 rv &= ~CERT_PKEY_CA_SIGNATURE;
3849 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3850 else if (check_flags)
3851 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3853 /* Check cert parameters are consistent */
3854 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3855 rv |= CERT_PKEY_EE_PARAM;
3856 else if (!check_flags)
3859 rv |= CERT_PKEY_CA_PARAM;
3860 /* In strict mode check rest of chain too */
3861 else if (strict_mode) {
3862 rv |= CERT_PKEY_CA_PARAM;
3863 for (i = 0; i < sk_X509_num(chain); i++) {
3864 X509 *ca = sk_X509_value(chain, i);
3865 if (!tls1_check_cert_param(s, ca, 0)) {
3867 rv &= ~CERT_PKEY_CA_PARAM;
3874 if (!s->server && strict_mode) {
3875 STACK_OF(X509_NAME) *ca_dn;
3877 switch (EVP_PKEY_id(pk)) {
3879 check_type = TLS_CT_RSA_SIGN;
3882 check_type = TLS_CT_DSS_SIGN;
3885 check_type = TLS_CT_ECDSA_SIGN;
3889 const unsigned char *ctypes;
3893 ctypelen = (int)c->ctype_num;
3895 ctypes = (unsigned char *)s->s3->tmp.ctype;
3896 ctypelen = s->s3->tmp.ctype_num;
3898 for (i = 0; i < ctypelen; i++) {
3899 if (ctypes[i] == check_type) {
3900 rv |= CERT_PKEY_CERT_TYPE;
3904 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3907 rv |= CERT_PKEY_CERT_TYPE;
3909 ca_dn = s->s3->tmp.ca_names;
3911 if (!sk_X509_NAME_num(ca_dn))
3912 rv |= CERT_PKEY_ISSUER_NAME;
3914 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3915 if (ssl_check_ca_name(ca_dn, x))
3916 rv |= CERT_PKEY_ISSUER_NAME;
3918 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3919 for (i = 0; i < sk_X509_num(chain); i++) {
3920 X509 *xtmp = sk_X509_value(chain, i);
3921 if (ssl_check_ca_name(ca_dn, xtmp)) {
3922 rv |= CERT_PKEY_ISSUER_NAME;
3927 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3930 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3932 if (!check_flags || (rv & check_flags) == check_flags)
3933 rv |= CERT_PKEY_VALID;
3937 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3938 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3939 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3940 else if (s->s3->tmp.md[idx] != NULL)
3941 rv |= CERT_PKEY_SIGN;
3943 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3946 * When checking a CERT_PKEY structure all flags are irrelevant if the
3950 if (rv & CERT_PKEY_VALID)
3953 /* Preserve explicit sign flag, clear rest */
3954 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3961 /* Set validity of certificates in an SSL structure */
3962 void tls1_set_cert_validity(SSL *s)
3964 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3965 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3966 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3967 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3968 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
3969 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
3970 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
3973 /* User level utiity function to check a chain is suitable */
3974 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3976 return tls1_check_chain(s, x, pk, chain, -1);
3980 #ifndef OPENSSL_NO_DH
3981 DH *ssl_get_auto_dh(SSL *s)
3983 int dh_secbits = 80;
3984 if (s->cert->dh_tmp_auto == 2)
3985 return DH_get_1024_160();
3986 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
3987 if (s->s3->tmp.new_cipher->strength_bits == 256)
3992 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
3993 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
3996 if (dh_secbits >= 128) {
4004 if (dh_secbits >= 192)
4005 p = BN_get_rfc3526_prime_8192(NULL);
4007 p = BN_get_rfc3526_prime_3072(NULL);
4008 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4016 if (dh_secbits >= 112)
4017 return DH_get_2048_224();
4018 return DH_get_1024_160();
4022 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4025 EVP_PKEY *pkey = X509_get0_pubkey(x);
4028 * If no parameters this will return -1 and fail using the default
4029 * security callback for any non-zero security level. This will
4030 * reject keys which omit parameters but this only affects DSA and
4031 * omission of parameters is never (?) done in practice.
4033 secbits = EVP_PKEY_security_bits(pkey);
4036 return ssl_security(s, op, secbits, 0, x);
4038 return ssl_ctx_security(ctx, op, secbits, 0, x);
4041 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4043 /* Lookup signature algorithm digest */
4044 int secbits = -1, md_nid = NID_undef, sig_nid;
4045 /* Don't check signature if self signed */
4046 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4048 sig_nid = X509_get_signature_nid(x);
4049 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4051 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4052 secbits = EVP_MD_size(md) * 4;
4055 return ssl_security(s, op, secbits, md_nid, x);
4057 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4060 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4063 vfy = SSL_SECOP_PEER;
4065 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4066 return SSL_R_EE_KEY_TOO_SMALL;
4068 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4069 return SSL_R_CA_KEY_TOO_SMALL;
4071 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4072 return SSL_R_CA_MD_TOO_WEAK;
4077 * Check security of a chain, if sk includes the end entity certificate then
4078 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4079 * one to the peer. Return values: 1 if ok otherwise error code to use
4082 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4084 int rv, start_idx, i;
4086 x = sk_X509_value(sk, 0);
4091 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4095 for (i = start_idx; i < sk_X509_num(sk); i++) {
4096 x = sk_X509_value(sk, i);
4097 rv = ssl_security_cert(s, NULL, x, vfy, 0);