2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/hmac.h>
15 #include <openssl/ocsp.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/dh.h>
19 #include <openssl/bn.h>
21 #include <openssl/ct.h>
23 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, size_t ticklen,
24 const unsigned char *sess_id, size_t sesslen,
26 static int ssl_check_clienthello_tlsext_early(SSL *s);
27 static int ssl_check_serverhello_tlsext(SSL *s);
29 SSL3_ENC_METHOD const TLSv1_enc_data = {
33 tls1_generate_master_secret,
34 tls1_change_cipher_state,
35 tls1_final_finish_mac,
36 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
37 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
39 tls1_export_keying_material,
41 ssl3_set_handshake_header,
42 tls_close_construct_packet,
46 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
50 tls1_generate_master_secret,
51 tls1_change_cipher_state,
52 tls1_final_finish_mac,
53 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
54 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
56 tls1_export_keying_material,
57 SSL_ENC_FLAG_EXPLICIT_IV,
58 ssl3_set_handshake_header,
59 tls_close_construct_packet,
63 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
67 tls1_generate_master_secret,
68 tls1_change_cipher_state,
69 tls1_final_finish_mac,
70 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
71 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
73 tls1_export_keying_material,
74 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
75 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
76 ssl3_set_handshake_header,
77 tls_close_construct_packet,
81 SSL3_ENC_METHOD const TLSv1_3_enc_data = {
85 tls1_generate_master_secret,
86 tls1_change_cipher_state,
87 tls1_final_finish_mac,
88 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
89 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
91 tls1_export_keying_material,
92 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
93 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
94 ssl3_set_handshake_header,
95 tls_close_construct_packet,
99 long tls1_default_timeout(void)
102 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
103 * http, the cache would over fill
105 return (60 * 60 * 2);
112 s->method->ssl_clear(s);
116 void tls1_free(SSL *s)
118 OPENSSL_free(s->tlsext_session_ticket);
122 void tls1_clear(SSL *s)
125 if (s->method->version == TLS_ANY_VERSION)
126 s->version = TLS_MAX_VERSION;
128 s->version = s->method->version;
131 #ifndef OPENSSL_NO_EC
134 int nid; /* Curve NID */
135 int secbits; /* Bits of security (from SP800-57) */
136 unsigned int flags; /* Flags: currently just field type */
140 * Table of curve information.
141 * Do not delete entries or reorder this array! It is used as a lookup
142 * table: the index of each entry is one less than the TLS curve id.
144 static const tls_curve_info nid_list[] = {
145 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
146 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
147 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
148 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
149 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
150 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
151 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
152 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
153 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
154 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
155 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
156 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
157 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
158 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
159 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
160 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
161 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
162 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
163 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
164 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
165 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
166 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
167 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
168 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
169 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
170 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
171 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
172 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
173 {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
176 static const unsigned char ecformats_default[] = {
177 TLSEXT_ECPOINTFORMAT_uncompressed,
178 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
179 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
182 /* The default curves */
183 static const unsigned char eccurves_default[] = {
184 0, 29, /* X25519 (29) */
185 0, 23, /* secp256r1 (23) */
186 0, 25, /* secp521r1 (25) */
187 0, 24, /* secp384r1 (24) */
190 static const unsigned char eccurves_all[] = {
191 0, 29, /* X25519 (29) */
192 0, 23, /* secp256r1 (23) */
193 0, 25, /* secp521r1 (25) */
194 0, 24, /* secp384r1 (24) */
195 0, 26, /* brainpoolP256r1 (26) */
196 0, 27, /* brainpoolP384r1 (27) */
197 0, 28, /* brainpool512r1 (28) */
200 * Remaining curves disabled by default but still permitted if set
201 * via an explicit callback or parameters.
203 0, 22, /* secp256k1 (22) */
204 0, 14, /* sect571r1 (14) */
205 0, 13, /* sect571k1 (13) */
206 0, 11, /* sect409k1 (11) */
207 0, 12, /* sect409r1 (12) */
208 0, 9, /* sect283k1 (9) */
209 0, 10, /* sect283r1 (10) */
210 0, 20, /* secp224k1 (20) */
211 0, 21, /* secp224r1 (21) */
212 0, 18, /* secp192k1 (18) */
213 0, 19, /* secp192r1 (19) */
214 0, 15, /* secp160k1 (15) */
215 0, 16, /* secp160r1 (16) */
216 0, 17, /* secp160r2 (17) */
217 0, 8, /* sect239k1 (8) */
218 0, 6, /* sect233k1 (6) */
219 0, 7, /* sect233r1 (7) */
220 0, 4, /* sect193r1 (4) */
221 0, 5, /* sect193r2 (5) */
222 0, 1, /* sect163k1 (1) */
223 0, 2, /* sect163r1 (2) */
224 0, 3, /* sect163r2 (3) */
227 static const unsigned char suiteb_curves[] = {
228 0, TLSEXT_curve_P_256,
229 0, TLSEXT_curve_P_384
232 int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags)
234 const tls_curve_info *cinfo;
235 /* ECC curves from RFC 4492 and RFC 7027 */
236 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
238 cinfo = nid_list + curve_id - 1;
240 *pflags = cinfo->flags;
244 int tls1_ec_nid2curve_id(int nid)
247 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
248 if (nid_list[i].nid == nid)
255 * Get curves list, if "sess" is set return client curves otherwise
257 * Sets |num_curves| to the number of curves in the list, i.e.,
258 * the length of |pcurves| is 2 * num_curves.
259 * Returns 1 on success and 0 if the client curves list has invalid format.
260 * The latter indicates an internal error: we should not be accepting such
261 * lists in the first place.
262 * TODO(emilia): we should really be storing the curves list in explicitly
263 * parsed form instead. (However, this would affect binary compatibility
264 * so cannot happen in the 1.0.x series.)
266 static int tls1_get_curvelist(SSL *s, int sess,
267 const unsigned char **pcurves, size_t *num_curves)
269 size_t pcurveslen = 0;
271 *pcurves = s->session->tlsext_ellipticcurvelist;
272 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
274 /* For Suite B mode only include P-256, P-384 */
275 switch (tls1_suiteb(s)) {
276 case SSL_CERT_FLAG_SUITEB_128_LOS:
277 *pcurves = suiteb_curves;
278 pcurveslen = sizeof(suiteb_curves);
281 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
282 *pcurves = suiteb_curves;
286 case SSL_CERT_FLAG_SUITEB_192_LOS:
287 *pcurves = suiteb_curves + 2;
291 *pcurves = s->tlsext_ellipticcurvelist;
292 pcurveslen = s->tlsext_ellipticcurvelist_length;
295 *pcurves = eccurves_default;
296 pcurveslen = sizeof(eccurves_default);
300 /* We do not allow odd length arrays to enter the system. */
301 if (pcurveslen & 1) {
302 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
306 *num_curves = pcurveslen / 2;
311 /* See if curve is allowed by security callback */
312 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
314 const tls_curve_info *cinfo;
317 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
319 cinfo = &nid_list[curve[1] - 1];
320 # ifdef OPENSSL_NO_EC2M
321 if (cinfo->flags & TLS_CURVE_CHAR2)
324 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
327 /* Check a curve is one of our preferences */
328 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
330 const unsigned char *curves;
331 size_t num_curves, i;
332 unsigned int suiteb_flags = tls1_suiteb(s);
333 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
335 /* Check curve matches Suite B preferences */
337 unsigned long cid = s->s3->tmp.new_cipher->id;
340 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
341 if (p[2] != TLSEXT_curve_P_256)
343 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
344 if (p[2] != TLSEXT_curve_P_384)
346 } else /* Should never happen */
349 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
351 for (i = 0; i < num_curves; i++, curves += 2) {
352 if (p[1] == curves[0] && p[2] == curves[1])
353 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
359 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
360 * if there is no match.
361 * For nmatch == -1, return number of matches
362 * For nmatch == -2, return the NID of the curve to use for
363 * an EC tmp key, or NID_undef if there is no match.
365 int tls1_shared_curve(SSL *s, int nmatch)
367 const unsigned char *pref, *supp;
368 size_t num_pref, num_supp, i, j;
370 /* Can't do anything on client side */
374 if (tls1_suiteb(s)) {
376 * For Suite B ciphersuite determines curve: we already know
377 * these are acceptable due to previous checks.
379 unsigned long cid = s->s3->tmp.new_cipher->id;
380 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
381 return NID_X9_62_prime256v1; /* P-256 */
382 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
383 return NID_secp384r1; /* P-384 */
384 /* Should never happen */
387 /* If not Suite B just return first preference shared curve */
391 * Avoid truncation. tls1_get_curvelist takes an int
392 * but s->options is a long...
394 if (!tls1_get_curvelist
395 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
397 /* In practice, NID_undef == 0 but let's be precise. */
398 return nmatch == -1 ? 0 : NID_undef;
399 if (!tls1_get_curvelist
400 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, &num_pref))
401 return nmatch == -1 ? 0 : NID_undef;
404 * If the client didn't send the elliptic_curves extension all of them
407 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
409 num_supp = sizeof(eccurves_all) / 2;
410 } else if (num_pref == 0 &&
411 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
413 num_pref = sizeof(eccurves_all) / 2;
417 for (i = 0; i < num_pref; i++, pref += 2) {
418 const unsigned char *tsupp = supp;
419 for (j = 0; j < num_supp; j++, tsupp += 2) {
420 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
421 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
424 int id = (pref[0] << 8) | pref[1];
425 return tls1_ec_curve_id2nid(id, NULL);
433 /* Out of range (nmatch > k). */
437 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
438 int *curves, size_t ncurves)
440 unsigned char *clist, *p;
443 * Bitmap of curves included to detect duplicates: only works while curve
446 unsigned long dup_list = 0;
447 clist = OPENSSL_malloc(ncurves * 2);
450 for (i = 0, p = clist; i < ncurves; i++) {
451 unsigned long idmask;
453 id = tls1_ec_nid2curve_id(curves[i]);
455 if (!id || (dup_list & idmask)) {
464 *pextlen = ncurves * 2;
468 # define MAX_CURVELIST 28
472 int nid_arr[MAX_CURVELIST];
475 static int nid_cb(const char *elem, int len, void *arg)
477 nid_cb_st *narg = arg;
483 if (narg->nidcnt == MAX_CURVELIST)
485 if (len > (int)(sizeof(etmp) - 1))
487 memcpy(etmp, elem, len);
489 nid = EC_curve_nist2nid(etmp);
490 if (nid == NID_undef)
491 nid = OBJ_sn2nid(etmp);
492 if (nid == NID_undef)
493 nid = OBJ_ln2nid(etmp);
494 if (nid == NID_undef)
496 for (i = 0; i < narg->nidcnt; i++)
497 if (narg->nid_arr[i] == nid)
499 narg->nid_arr[narg->nidcnt++] = nid;
503 /* Set curves based on a colon separate list */
504 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen, const char *str)
508 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
512 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
515 /* For an EC key set TLS id and required compression based on parameters */
516 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
523 /* Determine if it is a prime field */
524 grp = EC_KEY_get0_group(ec);
527 /* Determine curve ID */
528 id = EC_GROUP_get_curve_name(grp);
529 id = tls1_ec_nid2curve_id(id);
530 /* If no id return error: we don't support arbitrary explicit curves */
534 curve_id[1] = (unsigned char)id;
536 if (EC_KEY_get0_public_key(ec) == NULL)
538 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
539 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
541 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
542 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
544 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
550 /* Check an EC key is compatible with extensions */
551 static int tls1_check_ec_key(SSL *s,
552 unsigned char *curve_id, unsigned char *comp_id)
554 const unsigned char *pformats, *pcurves;
555 size_t num_formats, num_curves, i;
558 * If point formats extension present check it, otherwise everything is
559 * supported (see RFC4492).
561 if (comp_id && s->session->tlsext_ecpointformatlist) {
562 pformats = s->session->tlsext_ecpointformatlist;
563 num_formats = s->session->tlsext_ecpointformatlist_length;
564 for (i = 0; i < num_formats; i++, pformats++) {
565 if (*comp_id == *pformats)
568 if (i == num_formats)
573 /* Check curve is consistent with client and server preferences */
574 for (j = 0; j <= 1; j++) {
575 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
577 if (j == 1 && num_curves == 0) {
579 * If we've not received any curves then skip this check.
580 * RFC 4492 does not require the supported elliptic curves extension
581 * so if it is not sent we can just choose any curve.
582 * It is invalid to send an empty list in the elliptic curves
583 * extension, so num_curves == 0 always means no extension.
587 for (i = 0; i < num_curves; i++, pcurves += 2) {
588 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
593 /* For clients can only check sent curve list */
600 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
604 * If we have a custom point format list use it otherwise use default
606 if (s->tlsext_ecpointformatlist) {
607 *pformats = s->tlsext_ecpointformatlist;
608 *num_formats = s->tlsext_ecpointformatlist_length;
610 *pformats = ecformats_default;
611 /* For Suite B we don't support char2 fields */
613 *num_formats = sizeof(ecformats_default) - 1;
615 *num_formats = sizeof(ecformats_default);
620 * Check cert parameters compatible with extensions: currently just checks EC
621 * certificates have compatible curves and compression.
623 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
625 unsigned char comp_id, curve_id[2];
628 pkey = X509_get0_pubkey(x);
631 /* If not EC nothing to do */
632 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
634 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
638 * Can't check curve_id for client certs as we don't have a supported
641 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
645 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
646 * SHA384+P-384, adjust digest if necessary.
648 if (set_ee_md && tls1_suiteb(s)) {
654 /* Check to see we have necessary signing algorithm */
655 if (curve_id[1] == TLSEXT_curve_P_256)
656 check_md = NID_ecdsa_with_SHA256;
657 else if (curve_id[1] == TLSEXT_curve_P_384)
658 check_md = NID_ecdsa_with_SHA384;
660 return 0; /* Should never happen */
661 for (i = 0; i < c->shared_sigalgslen; i++)
662 if (check_md == c->shared_sigalgs[i].signandhash_nid)
664 if (i == c->shared_sigalgslen)
666 if (set_ee_md == 2) {
667 if (check_md == NID_ecdsa_with_SHA256)
668 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
670 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
676 # ifndef OPENSSL_NO_EC
678 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
680 * @cid: Cipher ID we're considering using
682 * Checks that the kECDHE cipher suite we're considering using
683 * is compatible with the client extensions.
685 * Returns 0 when the cipher can't be used or 1 when it can.
687 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
690 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
693 if (tls1_suiteb(s)) {
694 unsigned char curve_id[2];
695 /* Curve to check determined by ciphersuite */
696 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
697 curve_id[1] = TLSEXT_curve_P_256;
698 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
699 curve_id[1] = TLSEXT_curve_P_384;
703 /* Check this curve is acceptable */
704 if (!tls1_check_ec_key(s, curve_id, NULL))
708 /* Need a shared curve */
709 if (tls1_shared_curve(s, 0))
713 # endif /* OPENSSL_NO_EC */
717 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
722 #endif /* OPENSSL_NO_EC */
725 * List of supported signature algorithms and hashes. Should make this
726 * customisable at some point, for now include everything we support.
729 #ifdef OPENSSL_NO_RSA
730 # define tlsext_sigalg_rsa(md) /* */
732 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
735 #ifdef OPENSSL_NO_DSA
736 # define tlsext_sigalg_dsa(md) /* */
738 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
742 # define tlsext_sigalg_ecdsa(md)/* */
744 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
747 #define tlsext_sigalg(md) \
748 tlsext_sigalg_rsa(md) \
749 tlsext_sigalg_dsa(md) \
750 tlsext_sigalg_ecdsa(md)
752 static const unsigned char tls12_sigalgs[] = {
753 tlsext_sigalg(TLSEXT_hash_sha512)
754 tlsext_sigalg(TLSEXT_hash_sha384)
755 tlsext_sigalg(TLSEXT_hash_sha256)
756 tlsext_sigalg(TLSEXT_hash_sha224)
757 tlsext_sigalg(TLSEXT_hash_sha1)
758 #ifndef OPENSSL_NO_GOST
759 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
760 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
761 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
765 #ifndef OPENSSL_NO_EC
766 static const unsigned char suiteb_sigalgs[] = {
767 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
768 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
771 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
774 * If Suite B mode use Suite B sigalgs only, ignore any other
777 #ifndef OPENSSL_NO_EC
778 switch (tls1_suiteb(s)) {
779 case SSL_CERT_FLAG_SUITEB_128_LOS:
780 *psigs = suiteb_sigalgs;
781 return sizeof(suiteb_sigalgs);
783 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
784 *psigs = suiteb_sigalgs;
787 case SSL_CERT_FLAG_SUITEB_192_LOS:
788 *psigs = suiteb_sigalgs + 2;
792 /* If server use client authentication sigalgs if not NULL */
793 if (s->server && s->cert->client_sigalgs) {
794 *psigs = s->cert->client_sigalgs;
795 return s->cert->client_sigalgslen;
796 } else if (s->cert->conf_sigalgs) {
797 *psigs = s->cert->conf_sigalgs;
798 return s->cert->conf_sigalgslen;
800 *psigs = tls12_sigalgs;
801 return sizeof(tls12_sigalgs);
806 * Check signature algorithm is consistent with sent supported signature
807 * algorithms and if so return relevant digest.
809 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
810 const unsigned char *sig, EVP_PKEY *pkey)
812 const unsigned char *sent_sigs;
813 size_t sent_sigslen, i;
814 int sigalg = tls12_get_sigid(pkey);
815 /* Should never happen */
818 /* Check key type is consistent with signature */
819 if (sigalg != (int)sig[1]) {
820 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
823 #ifndef OPENSSL_NO_EC
824 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
825 unsigned char curve_id[2], comp_id;
826 /* Check compression and curve matches extensions */
827 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
829 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
830 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
833 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
834 if (tls1_suiteb(s)) {
837 if (curve_id[1] == TLSEXT_curve_P_256) {
838 if (sig[0] != TLSEXT_hash_sha256) {
839 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
840 SSL_R_ILLEGAL_SUITEB_DIGEST);
843 } else if (curve_id[1] == TLSEXT_curve_P_384) {
844 if (sig[0] != TLSEXT_hash_sha384) {
845 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
846 SSL_R_ILLEGAL_SUITEB_DIGEST);
852 } else if (tls1_suiteb(s))
856 /* Check signature matches a type we sent */
857 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
858 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
859 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
862 /* Allow fallback to SHA1 if not strict mode */
863 if (i == sent_sigslen
864 && (sig[0] != TLSEXT_hash_sha1
865 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
866 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
869 *pmd = tls12_get_hash(sig[0]);
871 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
874 /* Make sure security callback allows algorithm */
875 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
876 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) {
877 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
881 * Store the digest used so applications can retrieve it if they wish.
883 s->s3->tmp.peer_md = *pmd;
888 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
889 * supported, doesn't appear in supported signature algorithms, isn't supported
890 * by the enabled protocol versions or by the security level.
892 * This function should only be used for checking which ciphers are supported
895 * Call ssl_cipher_disabled() to check that it's enabled or not.
897 void ssl_set_client_disabled(SSL *s)
899 s->s3->tmp.mask_a = 0;
900 s->s3->tmp.mask_k = 0;
901 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
902 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
903 #ifndef OPENSSL_NO_PSK
904 /* with PSK there must be client callback set */
905 if (!s->psk_client_callback) {
906 s->s3->tmp.mask_a |= SSL_aPSK;
907 s->s3->tmp.mask_k |= SSL_PSK;
909 #endif /* OPENSSL_NO_PSK */
910 #ifndef OPENSSL_NO_SRP
911 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
912 s->s3->tmp.mask_a |= SSL_aSRP;
913 s->s3->tmp.mask_k |= SSL_kSRP;
919 * ssl_cipher_disabled - check that a cipher is disabled or not
920 * @s: SSL connection that you want to use the cipher on
921 * @c: cipher to check
922 * @op: Security check that you want to do
924 * Returns 1 when it's disabled, 0 when enabled.
926 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
928 if (c->algorithm_mkey & s->s3->tmp.mask_k
929 || c->algorithm_auth & s->s3->tmp.mask_a)
931 if (s->s3->tmp.max_ver == 0)
933 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
934 || (c->max_tls < s->s3->tmp.min_ver)))
936 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
937 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
940 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
943 static int tls_use_ticket(SSL *s)
945 if (s->options & SSL_OP_NO_TICKET)
947 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
950 static int compare_uint(const void *p1, const void *p2)
952 unsigned int u1 = *((const unsigned int *)p1);
953 unsigned int u2 = *((const unsigned int *)p2);
963 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
964 * more than one extension of the same type in a ClientHello or ServerHello.
965 * This function does an initial scan over the extensions block to filter those
966 * out. It returns 1 if all extensions are unique, and 0 if the extensions
967 * contain duplicates, could not be successfully parsed, or an internal error
970 static int tls1_check_duplicate_extensions(const PACKET *packet)
972 PACKET extensions = *packet;
973 size_t num_extensions = 0, i = 0;
974 unsigned int *extension_types = NULL;
977 /* First pass: count the extensions. */
978 while (PACKET_remaining(&extensions) > 0) {
981 if (!PACKET_get_net_2(&extensions, &type) ||
982 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
988 if (num_extensions <= 1)
991 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
992 if (extension_types == NULL) {
993 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
997 /* Second pass: gather the extension types. */
998 extensions = *packet;
999 for (i = 0; i < num_extensions; i++) {
1001 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
1002 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1003 /* This should not happen. */
1004 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1009 if (PACKET_remaining(&extensions) != 0) {
1010 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1013 /* Sort the extensions and make sure there are no duplicates. */
1014 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1015 for (i = 1; i < num_extensions; i++) {
1016 if (extension_types[i - 1] == extension_types[i])
1021 OPENSSL_free(extension_types);
1025 int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al)
1027 #ifndef OPENSSL_NO_EC
1028 /* See if we support any ECC ciphersuites */
1030 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1032 unsigned long alg_k, alg_a;
1033 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1035 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1036 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1038 alg_k = c->algorithm_mkey;
1039 alg_a = c->algorithm_auth;
1040 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1041 || (alg_a & SSL_aECDSA)) {
1049 /* Add RI if renegotiating */
1050 if (s->renegotiate) {
1051 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1052 || !WPACKET_start_sub_packet_u16(pkt)
1053 || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
1054 s->s3->previous_client_finished_len)
1055 || !WPACKET_close(pkt)) {
1056 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1060 /* Only add RI for SSLv3 */
1061 if (s->client_version == SSL3_VERSION)
1064 if (s->tlsext_hostname != NULL) {
1065 /* Add TLS extension servername to the Client Hello message */
1066 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1067 /* Sub-packet for server_name extension */
1068 || !WPACKET_start_sub_packet_u16(pkt)
1069 /* Sub-packet for servername list (always 1 hostname)*/
1070 || !WPACKET_start_sub_packet_u16(pkt)
1071 || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
1072 || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,
1073 strlen(s->tlsext_hostname))
1074 || !WPACKET_close(pkt)
1075 || !WPACKET_close(pkt)) {
1076 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1080 #ifndef OPENSSL_NO_SRP
1081 /* Add SRP username if there is one */
1082 if (s->srp_ctx.login != NULL) {
1083 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
1084 /* Sub-packet for SRP extension */
1085 || !WPACKET_start_sub_packet_u16(pkt)
1086 || !WPACKET_start_sub_packet_u8(pkt)
1087 /* login must not be zero...internal error if so */
1088 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
1089 || !WPACKET_memcpy(pkt, s->srp_ctx.login,
1090 strlen(s->srp_ctx.login))
1091 || !WPACKET_close(pkt)
1092 || !WPACKET_close(pkt)) {
1093 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1099 #ifndef OPENSSL_NO_EC
1102 * Add TLS extension ECPointFormats to the ClientHello message
1104 const unsigned char *pcurves, *pformats;
1105 size_t num_curves, num_formats;
1108 tls1_get_formatlist(s, &pformats, &num_formats);
1110 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1111 /* Sub-packet for formats extension */
1112 || !WPACKET_start_sub_packet_u16(pkt)
1113 || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
1114 || !WPACKET_close(pkt)) {
1115 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1120 * Add TLS extension EllipticCurves to the ClientHello message
1122 pcurves = s->tlsext_ellipticcurvelist;
1123 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
1124 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1128 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_elliptic_curves)
1129 /* Sub-packet for curves extension */
1130 || !WPACKET_start_sub_packet_u16(pkt)
1131 || !WPACKET_start_sub_packet_u16(pkt)) {
1132 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1135 /* Copy curve ID if supported */
1136 for (i = 0; i < num_curves; i++, pcurves += 2) {
1137 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1138 if (!WPACKET_put_bytes_u8(pkt, pcurves[0])
1139 || !WPACKET_put_bytes_u8(pkt, pcurves[1])) {
1140 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,
1141 ERR_R_INTERNAL_ERROR);
1146 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1147 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1151 #endif /* OPENSSL_NO_EC */
1153 if (tls_use_ticket(s)) {
1155 if (!s->new_session && s->session && s->session->tlsext_tick)
1156 ticklen = s->session->tlsext_ticklen;
1157 else if (s->session && s->tlsext_session_ticket &&
1158 s->tlsext_session_ticket->data) {
1159 ticklen = s->tlsext_session_ticket->length;
1160 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1161 if (s->session->tlsext_tick == NULL) {
1162 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1165 memcpy(s->session->tlsext_tick,
1166 s->tlsext_session_ticket->data, ticklen);
1167 s->session->tlsext_ticklen = ticklen;
1170 if (ticklen == 0 && s->tlsext_session_ticket &&
1171 s->tlsext_session_ticket->data == NULL)
1174 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1175 || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick,
1177 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1183 if (SSL_CLIENT_USE_SIGALGS(s)) {
1185 const unsigned char *salg;
1187 salglen = tls12_get_psigalgs(s, &salg);
1189 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
1190 /* Sub-packet for sig-algs extension */
1191 || !WPACKET_start_sub_packet_u16(pkt)
1192 /* Sub-packet for the actual list */
1193 || !WPACKET_start_sub_packet_u16(pkt)
1194 || !tls12_copy_sigalgs(s, pkt, salg, salglen)
1195 || !WPACKET_close(pkt)
1196 || !WPACKET_close(pkt)) {
1197 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1201 #ifndef OPENSSL_NO_OCSP
1202 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1205 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1206 /* Sub-packet for status request extension */
1207 || !WPACKET_start_sub_packet_u16(pkt)
1208 || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
1209 /* Sub-packet for the ids */
1210 || !WPACKET_start_sub_packet_u16(pkt)) {
1211 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1214 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1215 unsigned char *idbytes;
1219 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1220 idlen = i2d_OCSP_RESPID(id, NULL);
1222 /* Sub-packet for an individual id */
1223 || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
1224 || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
1225 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1229 if (!WPACKET_close(pkt)
1230 || !WPACKET_start_sub_packet_u16(pkt)) {
1231 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1234 if (s->tlsext_ocsp_exts) {
1235 unsigned char *extbytes;
1236 int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1239 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1242 if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
1243 || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)
1245 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1249 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1250 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1255 #ifndef OPENSSL_NO_HEARTBEATS
1256 if (SSL_IS_DTLS(s)) {
1261 * 1: peer may send requests
1262 * 2: peer not allowed to send requests
1264 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1265 mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1267 mode = SSL_DTLSEXT_HB_ENABLED;
1269 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)
1270 /* Sub-packet for Hearbeat extension */
1271 || !WPACKET_start_sub_packet_u16(pkt)
1272 || !WPACKET_put_bytes_u8(pkt, mode)
1273 || !WPACKET_close(pkt)) {
1274 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1280 #ifndef OPENSSL_NO_NEXTPROTONEG
1281 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1283 * The client advertises an empty extension to indicate its support
1284 * for Next Protocol Negotiation
1286 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1287 || !WPACKET_put_bytes_u16(pkt, 0)) {
1288 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1295 * finish_md_len is non-zero during a renegotiation, so
1296 * this avoids sending ALPN during the renegotiation
1297 * (see longer comment below)
1299 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1300 if (!WPACKET_put_bytes_u16(pkt,
1301 TLSEXT_TYPE_application_layer_protocol_negotiation)
1302 /* Sub-packet ALPN extension */
1303 || !WPACKET_start_sub_packet_u16(pkt)
1304 || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,
1305 s->alpn_client_proto_list_len)
1306 || !WPACKET_close(pkt)) {
1307 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1310 s->s3->alpn_sent = 1;
1312 #ifndef OPENSSL_NO_SRTP
1313 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1314 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
1315 SRTP_PROTECTION_PROFILE *prof;
1318 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1319 /* Sub-packet for SRTP extension */
1320 || !WPACKET_start_sub_packet_u16(pkt)
1321 /* Sub-packet for the protection profile list */
1322 || !WPACKET_start_sub_packet_u16(pkt)) {
1323 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1326 ct = sk_SRTP_PROTECTION_PROFILE_num(clnt);
1327 for (i = 0; i < ct; i++) {
1328 prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1329 if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
1330 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1334 if (!WPACKET_close(pkt)
1335 /* Add an empty use_mki value */
1336 || !WPACKET_put_bytes_u8(pkt, 0)
1337 || !WPACKET_close(pkt)) {
1338 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1343 custom_ext_init(&s->cert->cli_ext);
1344 /* Add custom TLS Extensions to ClientHello */
1345 if (!custom_ext_add(s, 0, pkt, al)) {
1346 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1350 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) {
1351 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1352 || !WPACKET_put_bytes_u16(pkt, 0)) {
1353 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1358 #ifndef OPENSSL_NO_CT
1359 if (s->ct_validation_callback != NULL) {
1360 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
1361 || !WPACKET_put_bytes_u16(pkt, 0)) {
1362 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1368 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1369 || !WPACKET_put_bytes_u16(pkt, 0)) {
1370 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1374 if (SSL_IS_TLS13(s)) {
1375 int min_version, max_version, reason, currv;
1376 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1377 || !WPACKET_start_sub_packet_u16(pkt)
1378 || !WPACKET_start_sub_packet_u8(pkt)) {
1379 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1382 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
1384 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, reason);
1388 * TODO(TLS1.3): There is some discussion on the TLS list as to wheter
1389 * we should include versions <TLS1.2. For the moment we do. To be
1392 for (currv = max_version; currv >= min_version; currv--) {
1393 /* TODO(TLS1.3): Remove this first if clause prior to release!! */
1394 if (currv == TLS1_3_VERSION) {
1395 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
1396 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,
1397 ERR_R_INTERNAL_ERROR);
1400 } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
1401 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1405 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1406 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1412 * Add padding to workaround bugs in F5 terminators. See
1413 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1414 * code works out the length of all existing extensions it MUST always
1417 if (s->options & SSL_OP_TLSEXT_PADDING) {
1418 unsigned char *padbytes;
1421 if (!WPACKET_get_total_written(pkt, &hlen)) {
1422 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1426 if (hlen > 0xff && hlen < 0x200) {
1427 hlen = 0x200 - hlen;
1433 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
1434 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
1435 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1438 memset(padbytes, 0, hlen);
1446 int ssl_add_serverhello_tlsext(SSL *s, WPACKET *pkt, int *al)
1448 #ifndef OPENSSL_NO_NEXTPROTONEG
1449 int next_proto_neg_seen;
1451 #ifndef OPENSSL_NO_EC
1452 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1453 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1454 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1455 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1458 if (!WPACKET_start_sub_packet_u16(pkt)
1459 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) {
1460 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1464 if (s->s3->send_connection_binding &&
1465 !ssl_add_serverhello_renegotiate_ext(s, pkt)) {
1466 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1470 /* Only add RI for SSLv3 */
1471 if (s->version == SSL3_VERSION)
1474 if (!s->hit && s->servername_done == 1
1475 && s->session->tlsext_hostname != NULL) {
1476 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1477 || !WPACKET_put_bytes_u16(pkt, 0)) {
1478 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1482 #ifndef OPENSSL_NO_EC
1484 const unsigned char *plist;
1487 * Add TLS extension ECPointFormats to the ServerHello message
1489 tls1_get_formatlist(s, &plist, &plistlen);
1491 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1492 || !WPACKET_start_sub_packet_u16(pkt)
1493 || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1494 || !WPACKET_close(pkt)) {
1495 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1500 * Currently the server should not respond with a SupportedCurves
1503 #endif /* OPENSSL_NO_EC */
1505 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1506 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1507 || !WPACKET_put_bytes_u16(pkt, 0)) {
1508 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1513 * if we don't add the above TLSEXT, we can't add a session ticket
1516 s->tlsext_ticket_expected = 0;
1519 if (s->tlsext_status_expected) {
1520 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1521 || !WPACKET_put_bytes_u16(pkt, 0)) {
1522 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1526 #ifndef OPENSSL_NO_SRTP
1527 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1528 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1529 || !WPACKET_start_sub_packet_u16(pkt)
1530 || !WPACKET_put_bytes_u16(pkt, 2)
1531 || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1532 || !WPACKET_put_bytes_u8(pkt, 0)
1533 || !WPACKET_close(pkt)) {
1534 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1540 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1541 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1542 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1543 const unsigned char cryptopro_ext[36] = {
1544 0xfd, 0xe8, /* 65000 */
1545 0x00, 0x20, /* 32 bytes length */
1546 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1547 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1548 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1549 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1551 if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
1552 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
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)) {
1562 * 1: peer may send requests
1563 * 2: peer not allowed to send requests
1565 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1566 mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1568 mode = SSL_DTLSEXT_HB_ENABLED;
1570 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)
1571 || !WPACKET_start_sub_packet_u16(pkt)
1572 || !WPACKET_put_bytes_u8(pkt, mode)
1573 || !WPACKET_close(pkt)) {
1574 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1581 #ifndef OPENSSL_NO_NEXTPROTONEG
1582 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1583 s->s3->next_proto_neg_seen = 0;
1584 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1585 const unsigned char *npa;
1586 unsigned int npalen;
1589 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1591 ctx->next_protos_advertised_cb_arg);
1592 if (r == SSL_TLSEXT_ERR_OK) {
1593 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1594 || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1595 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1598 s->s3->next_proto_neg_seen = 1;
1602 if (!custom_ext_add(s, 1, pkt, al)) {
1603 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1607 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1609 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1610 * for other cases too.
1612 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1613 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1614 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1615 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1616 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1618 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1619 || !WPACKET_put_bytes_u16(pkt, 0)) {
1620 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1625 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1626 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1627 || !WPACKET_put_bytes_u16(pkt, 0)) {
1628 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1633 if (s->s3->alpn_selected != NULL) {
1634 if (!WPACKET_put_bytes_u16(pkt,
1635 TLSEXT_TYPE_application_layer_protocol_negotiation)
1636 || !WPACKET_start_sub_packet_u16(pkt)
1637 || !WPACKET_start_sub_packet_u16(pkt)
1638 || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
1639 s->s3->alpn_selected_len)
1640 || !WPACKET_close(pkt)
1641 || !WPACKET_close(pkt)) {
1642 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1648 if (!WPACKET_close(pkt)) {
1649 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1656 * Save the ALPN extension in a ClientHello.
1657 * pkt: the contents of the ALPN extension, not including type and length.
1658 * al: a pointer to the alert value to send in the event of a failure.
1659 * returns: 1 on success, 0 on error.
1661 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1663 PACKET protocol_list, save_protocol_list, protocol;
1665 *al = SSL_AD_DECODE_ERROR;
1667 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1668 || PACKET_remaining(&protocol_list) < 2) {
1672 save_protocol_list = protocol_list;
1674 /* Protocol names can't be empty. */
1675 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1676 || PACKET_remaining(&protocol) == 0) {
1679 } while (PACKET_remaining(&protocol_list) != 0);
1681 if (!PACKET_memdup(&save_protocol_list,
1682 &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
1683 *al = TLS1_AD_INTERNAL_ERROR;
1691 * Process the ALPN extension in a ClientHello.
1692 * al: a pointer to the alert value to send in the event of a failure.
1693 * returns 1 on success, 0 on error.
1695 static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
1697 const unsigned char *selected = NULL;
1698 unsigned char selected_len = 0;
1700 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1701 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1702 s->s3->alpn_proposed,
1703 (unsigned int)s->s3->alpn_proposed_len,
1704 s->ctx->alpn_select_cb_arg);
1706 if (r == SSL_TLSEXT_ERR_OK) {
1707 OPENSSL_free(s->s3->alpn_selected);
1708 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1709 if (s->s3->alpn_selected == NULL) {
1710 *al = SSL_AD_INTERNAL_ERROR;
1713 s->s3->alpn_selected_len = selected_len;
1714 #ifndef OPENSSL_NO_NEXTPROTONEG
1715 /* ALPN takes precedence over NPN. */
1716 s->s3->next_proto_neg_seen = 0;
1719 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1727 #ifndef OPENSSL_NO_EC
1729 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1730 * SecureTransport using the TLS extension block in |hello|.
1731 * Safari, since 10.6, sends exactly these extensions, in this order:
1736 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1737 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1738 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1739 * 10.8..10.8.3 (which don't work).
1741 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1747 static const unsigned char kSafariExtensionsBlock[] = {
1748 0x00, 0x0a, /* elliptic_curves extension */
1749 0x00, 0x08, /* 8 bytes */
1750 0x00, 0x06, /* 6 bytes of curve ids */
1751 0x00, 0x17, /* P-256 */
1752 0x00, 0x18, /* P-384 */
1753 0x00, 0x19, /* P-521 */
1755 0x00, 0x0b, /* ec_point_formats */
1756 0x00, 0x02, /* 2 bytes */
1757 0x01, /* 1 point format */
1758 0x00, /* uncompressed */
1759 /* The following is only present in TLS 1.2 */
1760 0x00, 0x0d, /* signature_algorithms */
1761 0x00, 0x0c, /* 12 bytes */
1762 0x00, 0x0a, /* 10 bytes */
1763 0x05, 0x01, /* SHA-384/RSA */
1764 0x04, 0x01, /* SHA-256/RSA */
1765 0x02, 0x01, /* SHA-1/RSA */
1766 0x04, 0x03, /* SHA-256/ECDSA */
1767 0x02, 0x03, /* SHA-1/ECDSA */
1770 /* Length of the common prefix (first two extensions). */
1771 static const size_t kSafariCommonExtensionsLength = 18;
1773 tmppkt = hello->extensions;
1775 if (!PACKET_forward(&tmppkt, 2)
1776 || !PACKET_get_net_2(&tmppkt, &type)
1777 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1781 if (type != TLSEXT_TYPE_server_name)
1784 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1785 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1787 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1790 #endif /* !OPENSSL_NO_EC */
1793 * Loop through all remaining ClientHello extensions that we collected earlier
1794 * and haven't already processed. For each one parse it and update the SSL
1795 * object as required.
1797 * Behaviour upon resumption is extension-specific. If the extension has no
1798 * effect during resumption, it is parsed (to verify its format) but otherwise
1801 * Returns 1 on success and 0 on failure.
1802 * Upon failure, sets |al| to the appropriate alert.
1804 static int ssl_scan_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello, int *al)
1807 int renegotiate_seen = 0;
1809 *al = SSL_AD_DECODE_ERROR;
1810 s->servername_done = 0;
1811 s->tlsext_status_type = -1;
1812 #ifndef OPENSSL_NO_NEXTPROTONEG
1813 s->s3->next_proto_neg_seen = 0;
1816 OPENSSL_free(s->s3->alpn_selected);
1817 s->s3->alpn_selected = NULL;
1818 s->s3->alpn_selected_len = 0;
1819 OPENSSL_free(s->s3->alpn_proposed);
1820 s->s3->alpn_proposed = NULL;
1821 s->s3->alpn_proposed_len = 0;
1822 #ifndef OPENSSL_NO_HEARTBEATS
1823 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1824 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1827 #ifndef OPENSSL_NO_EC
1828 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1829 ssl_check_for_safari(s, hello);
1830 #endif /* !OPENSSL_NO_EC */
1832 /* Clear any signature algorithms extension received */
1833 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1834 s->s3->tmp.peer_sigalgs = NULL;
1835 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1837 #ifndef OPENSSL_NO_SRP
1838 OPENSSL_free(s->srp_ctx.login);
1839 s->srp_ctx.login = NULL;
1842 s->srtp_profile = NULL;
1845 * We parse all extensions to ensure the ClientHello is well-formed but,
1846 * unless an extension specifies otherwise, we ignore extensions upon
1849 for (loop = 0; loop < hello->num_extensions; loop++) {
1850 RAW_EXTENSION *currext = &hello->pre_proc_exts[loop];
1852 if (s->tlsext_debug_cb)
1853 s->tlsext_debug_cb(s, 0, currext->type,
1854 PACKET_data(&currext->data),
1855 PACKET_remaining(&currext->data),
1856 s->tlsext_debug_arg);
1858 if (currext->type == TLSEXT_TYPE_renegotiate) {
1859 if (!ssl_parse_clienthello_renegotiate_ext(s,
1860 &currext->data, al))
1862 renegotiate_seen = 1;
1863 } else if (s->version == SSL3_VERSION) {
1866 * The servername extension is treated as follows:
1868 * - Only the hostname type is supported with a maximum length of 255.
1869 * - The servername is rejected if too long or if it contains zeros,
1870 * in which case an fatal alert is generated.
1871 * - The servername field is maintained together with the session cache.
1872 * - When a session is resumed, the servername call back invoked in order
1873 * to allow the application to position itself to the right context.
1874 * - The servername is acknowledged if it is new for a session or when
1875 * it is identical to a previously used for the same session.
1876 * Applications can control the behaviour. They can at any time
1877 * set a 'desirable' servername for a new SSL object. This can be the
1878 * case for example with HTTPS when a Host: header field is received and
1879 * a renegotiation is requested. In this case, a possible servername
1880 * presented in the new client hello is only acknowledged if it matches
1881 * the value of the Host: field.
1882 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1883 * if they provide for changing an explicit servername context for the
1884 * session, i.e. when the session has been established with a servername
1886 * - On session reconnect, the servername extension may be absent.
1890 else if (currext->type == TLSEXT_TYPE_server_name) {
1891 unsigned int servname_type;
1892 PACKET sni, hostname;
1894 if (!PACKET_as_length_prefixed_2(&currext->data, &sni)
1895 /* ServerNameList must be at least 1 byte long. */
1896 || PACKET_remaining(&sni) == 0) {
1901 * Although the server_name extension was intended to be
1902 * extensible to new name types, RFC 4366 defined the
1903 * syntax inextensibility and OpenSSL 1.0.x parses it as
1905 * RFC 6066 corrected the mistake but adding new name types
1906 * is nevertheless no longer feasible, so act as if no other
1907 * SNI types can exist, to simplify parsing.
1909 * Also note that the RFC permits only one SNI value per type,
1910 * i.e., we can only have a single hostname.
1912 if (!PACKET_get_1(&sni, &servname_type)
1913 || servname_type != TLSEXT_NAMETYPE_host_name
1914 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1919 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1920 *al = TLS1_AD_UNRECOGNIZED_NAME;
1924 if (PACKET_contains_zero_byte(&hostname)) {
1925 *al = TLS1_AD_UNRECOGNIZED_NAME;
1929 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1930 *al = TLS1_AD_INTERNAL_ERROR;
1934 s->servername_done = 1;
1937 * TODO(openssl-team): if the SNI doesn't match, we MUST
1938 * fall back to a full handshake.
1940 s->servername_done = s->session->tlsext_hostname
1941 && PACKET_equal(&hostname, s->session->tlsext_hostname,
1942 strlen(s->session->tlsext_hostname));
1945 #ifndef OPENSSL_NO_SRP
1946 else if (currext->type == TLSEXT_TYPE_srp) {
1949 if (!PACKET_as_length_prefixed_1(&currext->data, &srp_I))
1952 if (PACKET_contains_zero_byte(&srp_I))
1956 * TODO(openssl-team): currently, we re-authenticate the user
1957 * upon resumption. Instead, we MUST ignore the login.
1959 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
1960 *al = TLS1_AD_INTERNAL_ERROR;
1966 #ifndef OPENSSL_NO_EC
1967 else if (currext->type == TLSEXT_TYPE_ec_point_formats) {
1968 PACKET ec_point_format_list;
1970 if (!PACKET_as_length_prefixed_1(&currext->data,
1971 &ec_point_format_list)
1972 || PACKET_remaining(&ec_point_format_list) == 0) {
1977 if (!PACKET_memdup(&ec_point_format_list,
1978 &s->session->tlsext_ecpointformatlist,
1980 session->tlsext_ecpointformatlist_length)) {
1981 *al = TLS1_AD_INTERNAL_ERROR;
1985 } else if (currext->type == TLSEXT_TYPE_elliptic_curves) {
1986 PACKET elliptic_curve_list;
1988 /* Each NamedCurve is 2 bytes and we must have at least 1. */
1989 if (!PACKET_as_length_prefixed_2(&currext->data,
1990 &elliptic_curve_list)
1991 || PACKET_remaining(&elliptic_curve_list) == 0
1992 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
1997 if (!PACKET_memdup(&elliptic_curve_list,
1998 &s->session->tlsext_ellipticcurvelist,
2000 session->tlsext_ellipticcurvelist_length)) {
2001 *al = TLS1_AD_INTERNAL_ERROR;
2006 #endif /* OPENSSL_NO_EC */
2007 else if (currext->type == TLSEXT_TYPE_session_ticket) {
2008 if (s->tls_session_ticket_ext_cb &&
2009 !s->tls_session_ticket_ext_cb(s,
2010 PACKET_data(&currext->data),
2011 PACKET_remaining(&currext->data),
2012 s->tls_session_ticket_ext_cb_arg)) {
2013 *al = TLS1_AD_INTERNAL_ERROR;
2016 } else if (currext->type == TLSEXT_TYPE_signature_algorithms) {
2017 PACKET supported_sig_algs;
2019 if (!PACKET_as_length_prefixed_2(&currext->data,
2020 &supported_sig_algs)
2021 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2022 || PACKET_remaining(&supported_sig_algs) == 0) {
2027 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2028 PACKET_remaining(&supported_sig_algs))) {
2032 } else if (currext->type == TLSEXT_TYPE_status_request) {
2033 if (!PACKET_get_1(&currext->data,
2034 (unsigned int *)&s->tlsext_status_type)) {
2037 #ifndef OPENSSL_NO_OCSP
2038 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2039 const unsigned char *ext_data;
2040 PACKET responder_id_list, exts;
2041 if (!PACKET_get_length_prefixed_2
2042 (&currext->data, &responder_id_list))
2046 * We remove any OCSP_RESPIDs from a previous handshake
2047 * to prevent unbounded memory growth - CVE-2016-6304
2049 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2051 if (PACKET_remaining(&responder_id_list) > 0) {
2052 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2053 if (s->tlsext_ocsp_ids == NULL) {
2054 *al = SSL_AD_INTERNAL_ERROR;
2058 s->tlsext_ocsp_ids = NULL;
2061 while (PACKET_remaining(&responder_id_list) > 0) {
2063 PACKET responder_id;
2064 const unsigned char *id_data;
2066 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2068 || PACKET_remaining(&responder_id) == 0) {
2072 id_data = PACKET_data(&responder_id);
2073 /* TODO(size_t): Convert d2i_* to size_t */
2074 id = d2i_OCSP_RESPID(NULL, &id_data,
2075 (int)PACKET_remaining(&responder_id));
2079 if (id_data != PACKET_end(&responder_id)) {
2080 OCSP_RESPID_free(id);
2084 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2085 OCSP_RESPID_free(id);
2086 *al = SSL_AD_INTERNAL_ERROR;
2091 /* Read in request_extensions */
2092 if (!PACKET_as_length_prefixed_2(
2093 &currext->data, &exts))
2096 if (PACKET_remaining(&exts) > 0) {
2097 ext_data = PACKET_data(&exts);
2098 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2099 X509_EXTENSION_free);
2100 s->tlsext_ocsp_exts =
2101 d2i_X509_EXTENSIONS(NULL, &ext_data,
2102 (int)PACKET_remaining(&exts));
2103 if (s->tlsext_ocsp_exts == NULL
2104 || ext_data != PACKET_end(&exts)) {
2112 * We don't know what to do with any other type so ignore it.
2114 s->tlsext_status_type = -1;
2117 #ifndef OPENSSL_NO_HEARTBEATS
2118 else if (SSL_IS_DTLS(s) && currext->type == TLSEXT_TYPE_heartbeat) {
2119 unsigned int hbtype;
2121 if (!PACKET_get_1(&currext->data, &hbtype)
2122 || PACKET_remaining(&currext->data)) {
2123 *al = SSL_AD_DECODE_ERROR;
2127 case 0x01: /* Client allows us to send HB requests */
2128 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2130 case 0x02: /* Client doesn't accept HB requests */
2131 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2132 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2135 *al = SSL_AD_ILLEGAL_PARAMETER;
2140 #ifndef OPENSSL_NO_NEXTPROTONEG
2141 else if (currext->type == TLSEXT_TYPE_next_proto_neg
2142 && s->s3->tmp.finish_md_len == 0) {
2144 * We shouldn't accept this extension on a
2147 * s->new_session will be set on renegotiation, but we
2148 * probably shouldn't rely that it couldn't be set on
2149 * the initial renegotiation too in certain cases (when
2150 * there's some other reason to disallow resuming an
2151 * earlier session -- the current code won't be doing
2152 * anything like that, but this might change).
2154 * A valid sign that there's been a previous handshake
2155 * in this connection is if s->s3->tmp.finish_md_len >
2156 * 0. (We are talking about a check that will happen
2157 * in the Hello protocol round, well before a new
2158 * Finished message could have been computed.)
2160 s->s3->next_proto_neg_seen = 1;
2164 else if (currext->type
2165 == TLSEXT_TYPE_application_layer_protocol_negotiation
2166 && s->s3->tmp.finish_md_len == 0) {
2167 if (!tls1_alpn_handle_client_hello(s,
2168 &currext->data, al))
2172 /* session ticket processed earlier */
2173 #ifndef OPENSSL_NO_SRTP
2174 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2175 && currext->type == TLSEXT_TYPE_use_srtp) {
2176 if (ssl_parse_clienthello_use_srtp_ext(s,
2177 &currext->data, al))
2181 else if (currext->type == TLSEXT_TYPE_encrypt_then_mac
2182 && !(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
2183 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2185 * Note: extended master secret extension handled in
2186 * tls_check_client_ems_support()
2190 * If this ClientHello extension was unhandled and this is a
2191 * nonresumed connection, check whether the extension is a custom
2192 * TLS Extension (has a custom_srv_ext_record), and if so call the
2193 * callback and record the extension number so that an appropriate
2194 * ServerHello may be later returned.
2197 if (custom_ext_parse(s, 1, currext->type,
2198 PACKET_data(&currext->data),
2199 PACKET_remaining(&currext->data), al) <= 0)
2204 /* Need RI if renegotiating */
2206 if (!renegotiate_seen && s->renegotiate &&
2207 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2208 *al = SSL_AD_HANDSHAKE_FAILURE;
2209 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2210 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2215 * This function currently has no state to clean up, so it returns directly.
2216 * If parsing fails at any point, the function returns early.
2217 * The SSL object may be left with partial data from extensions, but it must
2218 * then no longer be used, and clearing it up will free the leftovers.
2223 int ssl_parse_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello)
2226 custom_ext_init(&s->cert->srv_ext);
2227 if (ssl_scan_clienthello_tlsext(s, hello, &al) <= 0) {
2228 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2231 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2232 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2238 #ifndef OPENSSL_NO_NEXTPROTONEG
2240 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2241 * elements of zero length are allowed and the set of elements must exactly
2242 * fill the length of the block.
2244 static char ssl_next_proto_validate(PACKET *pkt)
2246 PACKET tmp_protocol;
2248 while (PACKET_remaining(pkt)) {
2249 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2250 || PACKET_remaining(&tmp_protocol) == 0)
2258 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2260 unsigned int length, type, size;
2261 int tlsext_servername = 0;
2262 int renegotiate_seen = 0;
2264 #ifndef OPENSSL_NO_NEXTPROTONEG
2265 s->s3->next_proto_neg_seen = 0;
2267 s->tlsext_ticket_expected = 0;
2269 OPENSSL_free(s->s3->alpn_selected);
2270 s->s3->alpn_selected = NULL;
2271 #ifndef OPENSSL_NO_HEARTBEATS
2272 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2273 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2276 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2278 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2280 if (!PACKET_get_net_2(pkt, &length))
2283 if (PACKET_remaining(pkt) != length) {
2284 *al = SSL_AD_DECODE_ERROR;
2288 if (!tls1_check_duplicate_extensions(pkt)) {
2289 *al = SSL_AD_DECODE_ERROR;
2293 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2294 const unsigned char *data;
2297 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2298 || !PACKET_peek_bytes(&spkt, &data, size))
2301 if (s->tlsext_debug_cb)
2302 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2304 if (type == TLSEXT_TYPE_renegotiate) {
2305 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2307 renegotiate_seen = 1;
2308 } else if (s->version == SSL3_VERSION) {
2309 } else if (type == TLSEXT_TYPE_server_name) {
2310 if (s->tlsext_hostname == NULL || size > 0) {
2311 *al = TLS1_AD_UNRECOGNIZED_NAME;
2314 tlsext_servername = 1;
2316 #ifndef OPENSSL_NO_EC
2317 else if (type == TLSEXT_TYPE_ec_point_formats) {
2318 unsigned int ecpointformatlist_length;
2319 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2320 || ecpointformatlist_length != size - 1) {
2321 *al = TLS1_AD_DECODE_ERROR;
2325 s->session->tlsext_ecpointformatlist_length = 0;
2326 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2327 if ((s->session->tlsext_ecpointformatlist =
2328 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2329 *al = TLS1_AD_INTERNAL_ERROR;
2332 s->session->tlsext_ecpointformatlist_length =
2333 ecpointformatlist_length;
2334 if (!PACKET_copy_bytes(&spkt,
2335 s->session->tlsext_ecpointformatlist,
2336 ecpointformatlist_length)) {
2337 *al = TLS1_AD_DECODE_ERROR;
2343 #endif /* OPENSSL_NO_EC */
2345 else if (type == TLSEXT_TYPE_session_ticket) {
2346 if (s->tls_session_ticket_ext_cb &&
2347 !s->tls_session_ticket_ext_cb(s, data, size,
2348 s->tls_session_ticket_ext_cb_arg))
2350 *al = TLS1_AD_INTERNAL_ERROR;
2353 if (!tls_use_ticket(s) || (size > 0)) {
2354 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2357 s->tlsext_ticket_expected = 1;
2358 } else if (type == TLSEXT_TYPE_status_request) {
2360 * MUST be empty and only sent if we've requested a status
2363 if ((s->tlsext_status_type == -1) || (size > 0)) {
2364 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2367 /* Set flag to expect CertificateStatus message */
2368 s->tlsext_status_expected = 1;
2370 #ifndef OPENSSL_NO_CT
2372 * Only take it if we asked for it - i.e if there is no CT validation
2373 * callback set, then a custom extension MAY be processing it, so we
2374 * need to let control continue to flow to that.
2376 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2377 s->ct_validation_callback != NULL) {
2378 /* Simply copy it off for later processing */
2379 if (s->tlsext_scts != NULL) {
2380 OPENSSL_free(s->tlsext_scts);
2381 s->tlsext_scts = NULL;
2383 s->tlsext_scts_len = size;
2385 s->tlsext_scts = OPENSSL_malloc(size);
2386 if (s->tlsext_scts == NULL) {
2387 *al = TLS1_AD_INTERNAL_ERROR;
2390 memcpy(s->tlsext_scts, data, size);
2394 #ifndef OPENSSL_NO_NEXTPROTONEG
2395 else if (type == TLSEXT_TYPE_next_proto_neg &&
2396 s->s3->tmp.finish_md_len == 0) {
2397 unsigned char *selected;
2398 unsigned char selected_len;
2399 /* We must have requested it. */
2400 if (s->ctx->next_proto_select_cb == NULL) {
2401 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2404 /* The data must be valid */
2405 if (!ssl_next_proto_validate(&spkt)) {
2406 *al = TLS1_AD_DECODE_ERROR;
2409 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2412 ctx->next_proto_select_cb_arg) !=
2413 SSL_TLSEXT_ERR_OK) {
2414 *al = TLS1_AD_INTERNAL_ERROR;
2418 * Could be non-NULL if server has sent multiple NPN extensions in
2419 * a single Serverhello
2421 OPENSSL_free(s->next_proto_negotiated);
2422 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2423 if (s->next_proto_negotiated == NULL) {
2424 *al = TLS1_AD_INTERNAL_ERROR;
2427 memcpy(s->next_proto_negotiated, selected, selected_len);
2428 s->next_proto_negotiated_len = selected_len;
2429 s->s3->next_proto_neg_seen = 1;
2433 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2435 /* We must have requested it. */
2436 if (!s->s3->alpn_sent) {
2437 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2441 * The extension data consists of:
2442 * uint16 list_length
2443 * uint8 proto_length;
2444 * uint8 proto[proto_length];
2446 if (!PACKET_get_net_2(&spkt, &len)
2447 || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len)
2448 || PACKET_remaining(&spkt) != len) {
2449 *al = TLS1_AD_DECODE_ERROR;
2452 OPENSSL_free(s->s3->alpn_selected);
2453 s->s3->alpn_selected = OPENSSL_malloc(len);
2454 if (s->s3->alpn_selected == NULL) {
2455 *al = TLS1_AD_INTERNAL_ERROR;
2458 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2459 *al = TLS1_AD_DECODE_ERROR;
2462 s->s3->alpn_selected_len = len;
2464 #ifndef OPENSSL_NO_HEARTBEATS
2465 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2466 unsigned int hbtype;
2467 if (!PACKET_get_1(&spkt, &hbtype)) {
2468 *al = SSL_AD_DECODE_ERROR;
2472 case 0x01: /* Server allows us to send HB requests */
2473 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2475 case 0x02: /* Server doesn't accept HB requests */
2476 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2477 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2480 *al = SSL_AD_ILLEGAL_PARAMETER;
2485 #ifndef OPENSSL_NO_SRTP
2486 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2487 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2491 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2492 /* Ignore if inappropriate ciphersuite */
2493 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) &&
2494 s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2495 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2496 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2497 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2498 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2500 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2503 * If this extension type was not otherwise handled, but matches a
2504 * custom_cli_ext_record, then send it to the c callback
2506 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2510 if (PACKET_remaining(pkt) != 0) {
2511 *al = SSL_AD_DECODE_ERROR;
2515 if (!s->hit && tlsext_servername == 1) {
2516 if (s->tlsext_hostname) {
2517 if (s->session->tlsext_hostname == NULL) {
2518 s->session->tlsext_hostname =
2519 OPENSSL_strdup(s->tlsext_hostname);
2520 if (!s->session->tlsext_hostname) {
2521 *al = SSL_AD_UNRECOGNIZED_NAME;
2525 *al = SSL_AD_DECODE_ERROR;
2534 * Determine if we need to see RI. Strictly speaking if we want to avoid
2535 * an attack we should *always* see RI even on initial server hello
2536 * because the client doesn't see any renegotiation during an attack.
2537 * However this would mean we could not connect to any server which
2538 * doesn't support RI so for the immediate future tolerate RI absence
2540 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2541 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2542 *al = SSL_AD_HANDSHAKE_FAILURE;
2543 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2544 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2550 * Check extended master secret extension is consistent with
2553 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2554 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2555 *al = SSL_AD_HANDSHAKE_FAILURE;
2556 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2564 int ssl_prepare_clienthello_tlsext(SSL *s)
2566 s->s3->alpn_sent = 0;
2570 int ssl_prepare_serverhello_tlsext(SSL *s)
2575 static int ssl_check_clienthello_tlsext_early(SSL *s)
2577 int ret = SSL_TLSEXT_ERR_NOACK;
2578 int al = SSL_AD_UNRECOGNIZED_NAME;
2580 #ifndef OPENSSL_NO_EC
2582 * The handling of the ECPointFormats extension is done elsewhere, namely
2583 * in ssl3_choose_cipher in s3_lib.c.
2586 * The handling of the EllipticCurves extension is done elsewhere, namely
2587 * in ssl3_choose_cipher in s3_lib.c.
2591 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2593 s->ctx->tlsext_servername_callback(s, &al,
2594 s->ctx->tlsext_servername_arg);
2595 else if (s->initial_ctx != NULL
2596 && s->initial_ctx->tlsext_servername_callback != 0)
2598 s->initial_ctx->tlsext_servername_callback(s, &al,
2600 initial_ctx->tlsext_servername_arg);
2603 case SSL_TLSEXT_ERR_ALERT_FATAL:
2604 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2607 case SSL_TLSEXT_ERR_ALERT_WARNING:
2608 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2611 case SSL_TLSEXT_ERR_NOACK:
2612 s->servername_done = 0;
2618 /* Initialise digests to default values */
2619 void ssl_set_default_md(SSL *s)
2621 const EVP_MD **pmd = s->s3->tmp.md;
2622 #ifndef OPENSSL_NO_DSA
2623 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2625 #ifndef OPENSSL_NO_RSA
2626 if (SSL_USE_SIGALGS(s))
2627 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2629 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2630 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2632 #ifndef OPENSSL_NO_EC
2633 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2635 #ifndef OPENSSL_NO_GOST
2636 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2637 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2638 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2642 int tls1_set_server_sigalgs(SSL *s)
2647 /* Clear any shared signature algorithms */
2648 OPENSSL_free(s->cert->shared_sigalgs);
2649 s->cert->shared_sigalgs = NULL;
2650 s->cert->shared_sigalgslen = 0;
2651 /* Clear certificate digests and validity flags */
2652 for (i = 0; i < SSL_PKEY_NUM; i++) {
2653 s->s3->tmp.md[i] = NULL;
2654 s->s3->tmp.valid_flags[i] = 0;
2657 /* If sigalgs received process it. */
2658 if (s->s3->tmp.peer_sigalgs) {
2659 if (!tls1_process_sigalgs(s)) {
2660 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2661 al = SSL_AD_INTERNAL_ERROR;
2664 /* Fatal error is no shared signature algorithms */
2665 if (!s->cert->shared_sigalgs) {
2666 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2667 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
2668 al = SSL_AD_ILLEGAL_PARAMETER;
2672 ssl_set_default_md(s);
2676 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2681 * Upon success, returns 1.
2682 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
2684 int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
2686 s->tlsext_status_expected = 0;
2689 * If status request then ask callback what to do. Note: this must be
2690 * called after servername callbacks in case the certificate has changed,
2691 * and must be called after the cipher has been chosen because this may
2692 * influence which certificate is sent
2694 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2696 CERT_PKEY *certpkey;
2697 certpkey = ssl_get_server_send_pkey(s);
2698 /* If no certificate can't return certificate status */
2699 if (certpkey != NULL) {
2701 * Set current certificate to one we will use so SSL_get_certificate
2702 * et al can pick it up.
2704 s->cert->key = certpkey;
2705 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2707 /* We don't want to send a status request response */
2708 case SSL_TLSEXT_ERR_NOACK:
2709 s->tlsext_status_expected = 0;
2711 /* status request response should be sent */
2712 case SSL_TLSEXT_ERR_OK:
2713 if (s->tlsext_ocsp_resp)
2714 s->tlsext_status_expected = 1;
2716 /* something bad happened */
2717 case SSL_TLSEXT_ERR_ALERT_FATAL:
2719 *al = SSL_AD_INTERNAL_ERROR;
2725 if (!tls1_alpn_handle_client_hello_late(s, al)) {
2732 int ssl_check_serverhello_tlsext(SSL *s)
2734 int ret = SSL_TLSEXT_ERR_NOACK;
2735 int al = SSL_AD_UNRECOGNIZED_NAME;
2737 #ifndef OPENSSL_NO_EC
2739 * If we are client and using an elliptic curve cryptography cipher
2740 * suite, then if server returns an EC point formats lists extension it
2741 * must contain uncompressed.
2743 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2744 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2745 if ((s->tlsext_ecpointformatlist != NULL)
2746 && (s->tlsext_ecpointformatlist_length > 0)
2747 && (s->session->tlsext_ecpointformatlist != NULL)
2748 && (s->session->tlsext_ecpointformatlist_length > 0)
2749 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2750 /* we are using an ECC cipher */
2752 unsigned char *list;
2753 int found_uncompressed = 0;
2754 list = s->session->tlsext_ecpointformatlist;
2755 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2756 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2757 found_uncompressed = 1;
2761 if (!found_uncompressed) {
2762 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2763 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2767 ret = SSL_TLSEXT_ERR_OK;
2768 #endif /* OPENSSL_NO_EC */
2770 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2772 s->ctx->tlsext_servername_callback(s, &al,
2773 s->ctx->tlsext_servername_arg);
2774 else if (s->initial_ctx != NULL
2775 && s->initial_ctx->tlsext_servername_callback != 0)
2777 s->initial_ctx->tlsext_servername_callback(s, &al,
2779 initial_ctx->tlsext_servername_arg);
2782 * Ensure we get sensible values passed to tlsext_status_cb in the event
2783 * that we don't receive a status message
2785 OPENSSL_free(s->tlsext_ocsp_resp);
2786 s->tlsext_ocsp_resp = NULL;
2787 s->tlsext_ocsp_resplen = 0;
2790 case SSL_TLSEXT_ERR_ALERT_FATAL:
2791 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2794 case SSL_TLSEXT_ERR_ALERT_WARNING:
2795 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2798 case SSL_TLSEXT_ERR_NOACK:
2799 s->servername_done = 0;
2805 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2808 if (s->version < SSL3_VERSION)
2810 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2811 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2815 if (ssl_check_serverhello_tlsext(s) <= 0) {
2816 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2823 * Given a list of extensions that we collected earlier, find one of a given
2824 * type and return it.
2826 * |exts| is the set of extensions previously collected.
2827 * |numexts| is the number of extensions that we have.
2828 * |type| the type of the extension that we are looking for.
2830 * Returns a pointer to the found RAW_EXTENSION data, or NULL if not found.
2832 RAW_EXTENSION *tls_get_extension_by_type(RAW_EXTENSION *exts, size_t numexts,
2837 for (loop = 0; loop < numexts; loop++) {
2838 if (exts[loop].type == type)
2846 * Gets the ticket information supplied by the client if any.
2848 * hello: The parsed ClientHello data
2849 * ret: (output) on return, if a ticket was decrypted, then this is set to
2850 * point to the resulting session.
2852 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2853 * ciphersuite, in which case we have no use for session tickets and one will
2854 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2857 * -1: fatal error, either from parsing or decrypting the ticket.
2858 * 0: no ticket was found (or was ignored, based on settings).
2859 * 1: a zero length extension was found, indicating that the client supports
2860 * session tickets but doesn't currently have one to offer.
2861 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2862 * couldn't be decrypted because of a non-fatal error.
2863 * 3: a ticket was successfully decrypted and *ret was set.
2866 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2867 * a new session ticket to the client because the client indicated support
2868 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2869 * a session ticket or we couldn't use the one it gave us, or if
2870 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2871 * Otherwise, s->tlsext_ticket_expected is set to 0.
2873 int tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
2877 const unsigned char *etick;
2879 RAW_EXTENSION *ticketext;
2882 s->tlsext_ticket_expected = 0;
2885 * If tickets disabled behave as if no ticket present to permit stateful
2888 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
2891 ticketext = tls_get_extension_by_type(hello->pre_proc_exts,
2892 hello->num_extensions,
2893 TLSEXT_TYPE_session_ticket);
2894 if (ticketext == NULL)
2897 size = PACKET_remaining(&ticketext->data);
2900 * The client will accept a ticket but doesn't currently have
2903 s->tlsext_ticket_expected = 1;
2906 if (s->tls_session_secret_cb) {
2908 * Indicate that the ticket couldn't be decrypted rather than
2909 * generating the session from ticket now, trigger
2910 * abbreviated handshake based on external mechanism to
2911 * calculate the master secret later.
2915 if (!PACKET_get_bytes(&ticketext->data, &etick, size)) {
2916 /* Shouldn't ever happen */
2919 retv = tls_decrypt_ticket(s, etick, size, hello->session_id,
2920 hello->session_id_len, ret);
2922 case 2: /* ticket couldn't be decrypted */
2923 s->tlsext_ticket_expected = 1;
2926 case 3: /* ticket was decrypted */
2929 case 4: /* ticket decrypted but need to renew */
2930 s->tlsext_ticket_expected = 1;
2933 default: /* fatal error */
2939 * Sets the extended master secret flag if the extension is present in the
2945 int tls_check_client_ems_support(SSL *s, const CLIENTHELLO_MSG *hello)
2947 RAW_EXTENSION *emsext;
2949 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2951 if (s->version <= SSL3_VERSION)
2954 emsext = tls_get_extension_by_type(hello->pre_proc_exts,
2955 hello->num_extensions,
2956 TLSEXT_TYPE_extended_master_secret);
2959 * No extensions is a success - we have successfully discovered that the
2960 * client doesn't support EMS.
2965 /* The extensions must always be empty */
2966 if (PACKET_remaining(&emsext->data) != 0)
2969 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2975 * tls_decrypt_ticket attempts to decrypt a session ticket.
2977 * etick: points to the body of the session ticket extension.
2978 * eticklen: the length of the session tickets extension.
2979 * sess_id: points at the session ID.
2980 * sesslen: the length of the session ID.
2981 * psess: (output) on return, if a ticket was decrypted, then this is set to
2982 * point to the resulting session.
2985 * -2: fatal error, malloc failure.
2986 * -1: fatal error, either from parsing or decrypting the ticket.
2987 * 2: the ticket couldn't be decrypted.
2988 * 3: a ticket was successfully decrypted and *psess was set.
2989 * 4: same as 3, but the ticket needs to be renewed.
2991 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2992 size_t eticklen, const unsigned char *sess_id,
2993 size_t sesslen, SSL_SESSION **psess)
2996 unsigned char *sdec;
2997 const unsigned char *p;
2998 int slen, renew_ticket = 0, ret = -1, declen;
3000 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3001 HMAC_CTX *hctx = NULL;
3002 EVP_CIPHER_CTX *ctx;
3003 SSL_CTX *tctx = s->initial_ctx;
3005 /* Initialize session ticket encryption and HMAC contexts */
3006 hctx = HMAC_CTX_new();
3009 ctx = EVP_CIPHER_CTX_new();
3014 if (tctx->tlsext_ticket_key_cb) {
3015 unsigned char *nctick = (unsigned char *)etick;
3016 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3027 /* Check key name matches */
3028 if (memcmp(etick, tctx->tlsext_tick_key_name,
3029 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3033 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3034 sizeof(tctx->tlsext_tick_hmac_key),
3035 EVP_sha256(), NULL) <= 0
3036 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3037 tctx->tlsext_tick_aes_key,
3038 etick + sizeof(tctx->tlsext_tick_key_name)) <=
3044 * Attempt to process session ticket, first conduct sanity and integrity
3047 mlen = HMAC_size(hctx);
3051 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3053 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
3058 /* Check HMAC of encrypted ticket */
3059 if (HMAC_Update(hctx, etick, eticklen) <= 0
3060 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3063 HMAC_CTX_free(hctx);
3064 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3065 EVP_CIPHER_CTX_free(ctx);
3068 /* Attempt to decrypt session data */
3069 /* Move p after IV to start of encrypted ticket, update length */
3070 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3071 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3072 sdec = OPENSSL_malloc(eticklen);
3073 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
3074 (int)eticklen) <= 0) {
3075 EVP_CIPHER_CTX_free(ctx);
3079 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
3080 EVP_CIPHER_CTX_free(ctx);
3085 EVP_CIPHER_CTX_free(ctx);
3089 sess = d2i_SSL_SESSION(NULL, &p, slen);
3093 * The session ID, if non-empty, is used by some clients to detect
3094 * that the ticket has been accepted. So we copy it to the session
3095 * structure. If it is empty set length to zero as required by
3099 memcpy(sess->session_id, sess_id, sesslen);
3100 sess->session_id_length = sesslen;
3109 * For session parse failure, indicate that we need to send a new ticket.
3113 EVP_CIPHER_CTX_free(ctx);
3114 HMAC_CTX_free(hctx);
3118 /* Tables to translate from NIDs to TLS v1.2 ids */
3125 static const tls12_lookup tls12_md[] = {
3126 {NID_md5, TLSEXT_hash_md5},
3127 {NID_sha1, TLSEXT_hash_sha1},
3128 {NID_sha224, TLSEXT_hash_sha224},
3129 {NID_sha256, TLSEXT_hash_sha256},
3130 {NID_sha384, TLSEXT_hash_sha384},
3131 {NID_sha512, TLSEXT_hash_sha512},
3132 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3133 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3134 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3137 static const tls12_lookup tls12_sig[] = {
3138 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3139 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3140 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3141 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3142 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3143 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3146 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3149 for (i = 0; i < tlen; i++) {
3150 if (table[i].nid == nid)
3156 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3159 for (i = 0; i < tlen; i++) {
3160 if ((table[i].id) == id)
3161 return table[i].nid;
3166 int tls12_get_sigandhash(WPACKET *pkt, const EVP_PKEY *pk, const EVP_MD *md)
3172 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3175 sig_id = tls12_get_sigid(pk);
3178 if (!WPACKET_put_bytes_u8(pkt, md_id) || !WPACKET_put_bytes_u8(pkt, sig_id))
3184 int tls12_get_sigid(const EVP_PKEY *pk)
3186 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3193 unsigned char tlsext_hash;
3196 static const tls12_hash_info tls12_md_info[] = {
3197 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3198 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3199 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3200 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3201 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3202 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3203 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3204 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX,
3205 TLSEXT_hash_gostr34112012_256},
3206 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX,
3207 TLSEXT_hash_gostr34112012_512},
3210 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3216 for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) {
3217 if (tls12_md_info[i].tlsext_hash == hash_alg)
3218 return tls12_md_info + i;
3224 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3226 const tls12_hash_info *inf;
3227 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3229 inf = tls12_get_hash_info(hash_alg);
3232 return ssl_md(inf->md_idx);
3235 static int tls12_get_pkey_idx(unsigned char sig_alg)
3238 #ifndef OPENSSL_NO_RSA
3239 case TLSEXT_signature_rsa:
3240 return SSL_PKEY_RSA_SIGN;
3242 #ifndef OPENSSL_NO_DSA
3243 case TLSEXT_signature_dsa:
3244 return SSL_PKEY_DSA_SIGN;
3246 #ifndef OPENSSL_NO_EC
3247 case TLSEXT_signature_ecdsa:
3248 return SSL_PKEY_ECC;
3250 #ifndef OPENSSL_NO_GOST
3251 case TLSEXT_signature_gostr34102001:
3252 return SSL_PKEY_GOST01;
3254 case TLSEXT_signature_gostr34102012_256:
3255 return SSL_PKEY_GOST12_256;
3257 case TLSEXT_signature_gostr34102012_512:
3258 return SSL_PKEY_GOST12_512;
3264 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3265 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3266 int *psignhash_nid, const unsigned char *data)
3268 int sign_nid = NID_undef, hash_nid = NID_undef;
3269 if (!phash_nid && !psign_nid && !psignhash_nid)
3271 if (phash_nid || psignhash_nid) {
3272 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3274 *phash_nid = hash_nid;
3276 if (psign_nid || psignhash_nid) {
3277 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3279 *psign_nid = sign_nid;
3281 if (psignhash_nid) {
3282 if (sign_nid == NID_undef || hash_nid == NID_undef
3283 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0)
3284 *psignhash_nid = NID_undef;
3288 /* Check to see if a signature algorithm is allowed */
3289 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3291 /* See if we have an entry in the hash table and it is enabled */
3292 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3293 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3295 /* See if public key algorithm allowed */
3296 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3298 /* Finally see if security callback allows it */
3299 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3303 * Get a mask of disabled public key algorithms based on supported signature
3304 * algorithms. For example if no signature algorithm supports RSA then RSA is
3308 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3310 const unsigned char *sigalgs;
3311 size_t i, sigalgslen;
3312 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3314 * Now go through all signature algorithms seeing if we support any for
3315 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3316 * down calls to security callback only check if we have to.
3318 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3319 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3320 switch (sigalgs[1]) {
3321 #ifndef OPENSSL_NO_RSA
3322 case TLSEXT_signature_rsa:
3323 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3327 #ifndef OPENSSL_NO_DSA
3328 case TLSEXT_signature_dsa:
3329 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3333 #ifndef OPENSSL_NO_EC
3334 case TLSEXT_signature_ecdsa:
3335 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3342 *pmask_a |= SSL_aRSA;
3344 *pmask_a |= SSL_aDSS;
3346 *pmask_a |= SSL_aECDSA;
3349 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
3350 const unsigned char *psig, size_t psiglen)
3354 for (i = 0; i < psiglen; i += 2, psig += 2) {
3355 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3356 if (!WPACKET_put_bytes_u8(pkt, psig[0])
3357 || !WPACKET_put_bytes_u8(pkt, psig[1]))
3364 /* Given preference and allowed sigalgs set shared sigalgs */
3365 static size_t tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3366 const unsigned char *pref, size_t preflen,
3367 const unsigned char *allow, size_t allowlen)
3369 const unsigned char *ptmp, *atmp;
3370 size_t i, j, nmatch = 0;
3371 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3372 /* Skip disabled hashes or signature algorithms */
3373 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3375 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3376 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3379 shsig->rhash = ptmp[0];
3380 shsig->rsign = ptmp[1];
3381 tls1_lookup_sigalg(&shsig->hash_nid,
3383 &shsig->signandhash_nid, ptmp);
3393 /* Set shared signature algorithms for SSL structures */
3394 static int tls1_set_shared_sigalgs(SSL *s)
3396 const unsigned char *pref, *allow, *conf;
3397 size_t preflen, allowlen, conflen;
3399 TLS_SIGALGS *salgs = NULL;
3401 unsigned int is_suiteb = tls1_suiteb(s);
3403 OPENSSL_free(c->shared_sigalgs);
3404 c->shared_sigalgs = NULL;
3405 c->shared_sigalgslen = 0;
3406 /* If client use client signature algorithms if not NULL */
3407 if (!s->server && c->client_sigalgs && !is_suiteb) {
3408 conf = c->client_sigalgs;
3409 conflen = c->client_sigalgslen;
3410 } else if (c->conf_sigalgs && !is_suiteb) {
3411 conf = c->conf_sigalgs;
3412 conflen = c->conf_sigalgslen;
3414 conflen = tls12_get_psigalgs(s, &conf);
3415 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3418 allow = s->s3->tmp.peer_sigalgs;
3419 allowlen = s->s3->tmp.peer_sigalgslen;
3423 pref = s->s3->tmp.peer_sigalgs;
3424 preflen = s->s3->tmp.peer_sigalgslen;
3426 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3428 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3431 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3435 c->shared_sigalgs = salgs;
3436 c->shared_sigalgslen = nmatch;
3440 /* Set preferred digest for each key type */
3442 int tls1_save_sigalgs(SSL *s, const unsigned char *data, size_t dsize)
3445 /* Extension ignored for inappropriate versions */
3446 if (!SSL_USE_SIGALGS(s))
3448 /* Should never happen */
3452 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3453 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3454 if (s->s3->tmp.peer_sigalgs == NULL)
3456 s->s3->tmp.peer_sigalgslen = dsize;
3457 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3461 int tls1_process_sigalgs(SSL *s)
3466 const EVP_MD **pmd = s->s3->tmp.md;
3467 uint32_t *pvalid = s->s3->tmp.valid_flags;
3469 TLS_SIGALGS *sigptr;
3470 if (!tls1_set_shared_sigalgs(s))
3473 for (i = 0, sigptr = c->shared_sigalgs;
3474 i < c->shared_sigalgslen; i++, sigptr++) {
3475 idx = tls12_get_pkey_idx(sigptr->rsign);
3476 if (idx > 0 && pmd[idx] == NULL) {
3477 md = tls12_get_hash(sigptr->rhash);
3479 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3480 if (idx == SSL_PKEY_RSA_SIGN) {
3481 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3482 pmd[SSL_PKEY_RSA_ENC] = md;
3488 * In strict mode leave unset digests as NULL to indicate we can't use
3489 * the certificate for signing.
3491 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3493 * Set any remaining keys to default values. NOTE: if alg is not
3494 * supported it stays as NULL.
3496 #ifndef OPENSSL_NO_DSA
3497 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3498 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3500 #ifndef OPENSSL_NO_RSA
3501 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3502 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3503 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3506 #ifndef OPENSSL_NO_EC
3507 if (pmd[SSL_PKEY_ECC] == NULL)
3508 pmd[SSL_PKEY_ECC] = EVP_sha1();
3510 #ifndef OPENSSL_NO_GOST
3511 if (pmd[SSL_PKEY_GOST01] == NULL)
3512 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3513 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3514 pmd[SSL_PKEY_GOST12_256] =
3515 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3516 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3517 pmd[SSL_PKEY_GOST12_512] =
3518 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3524 int SSL_get_sigalgs(SSL *s, int idx,
3525 int *psign, int *phash, int *psignhash,
3526 unsigned char *rsig, unsigned char *rhash)
3528 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3529 size_t numsigalgs = s->s3->tmp.peer_sigalgslen / 2;
3530 if (psig == NULL || numsigalgs > INT_MAX)
3534 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3541 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3543 return (int)numsigalgs;
3546 int SSL_get_shared_sigalgs(SSL *s, int idx,
3547 int *psign, int *phash, int *psignhash,
3548 unsigned char *rsig, unsigned char *rhash)
3550 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3551 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen
3552 || s->cert->shared_sigalgslen > INT_MAX)
3556 *phash = shsigalgs->hash_nid;
3558 *psign = shsigalgs->sign_nid;
3560 *psignhash = shsigalgs->signandhash_nid;
3562 *rsig = shsigalgs->rsign;
3564 *rhash = shsigalgs->rhash;
3565 return (int)s->cert->shared_sigalgslen;
3568 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3572 int sigalgs[MAX_SIGALGLEN];
3575 static void get_sigorhash(int *psig, int *phash, const char *str)
3577 if (strcmp(str, "RSA") == 0) {
3578 *psig = EVP_PKEY_RSA;
3579 } else if (strcmp(str, "DSA") == 0) {
3580 *psig = EVP_PKEY_DSA;
3581 } else if (strcmp(str, "ECDSA") == 0) {
3582 *psig = EVP_PKEY_EC;
3584 *phash = OBJ_sn2nid(str);
3585 if (*phash == NID_undef)
3586 *phash = OBJ_ln2nid(str);
3590 static int sig_cb(const char *elem, int len, void *arg)
3592 sig_cb_st *sarg = arg;
3595 int sig_alg = NID_undef, hash_alg = NID_undef;
3598 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3600 if (len > (int)(sizeof(etmp) - 1))
3602 memcpy(etmp, elem, len);
3604 p = strchr(etmp, '+');
3612 get_sigorhash(&sig_alg, &hash_alg, etmp);
3613 get_sigorhash(&sig_alg, &hash_alg, p);
3615 if (sig_alg == NID_undef || hash_alg == NID_undef)
3618 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3619 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3622 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3623 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3628 * Set supported signature algorithms based on a colon separated list of the
3629 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3631 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3635 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3639 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3642 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3644 unsigned char *sigalgs, *sptr;
3649 sigalgs = OPENSSL_malloc(salglen);
3650 if (sigalgs == NULL)
3652 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3653 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3654 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3656 if (rhash == -1 || rsign == -1)
3663 OPENSSL_free(c->client_sigalgs);
3664 c->client_sigalgs = sigalgs;
3665 c->client_sigalgslen = salglen;
3667 OPENSSL_free(c->conf_sigalgs);
3668 c->conf_sigalgs = sigalgs;
3669 c->conf_sigalgslen = salglen;
3675 OPENSSL_free(sigalgs);
3679 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3683 if (default_nid == -1)
3685 sig_nid = X509_get_signature_nid(x);
3687 return sig_nid == default_nid ? 1 : 0;
3688 for (i = 0; i < c->shared_sigalgslen; i++)
3689 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3694 /* Check to see if a certificate issuer name matches list of CA names */
3695 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3699 nm = X509_get_issuer_name(x);
3700 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3701 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3708 * Check certificate chain is consistent with TLS extensions and is usable by
3709 * server. This servers two purposes: it allows users to check chains before
3710 * passing them to the server and it allows the server to check chains before
3711 * attempting to use them.
3714 /* Flags which need to be set for a certificate when stict mode not set */
3716 #define CERT_PKEY_VALID_FLAGS \
3717 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3718 /* Strict mode flags */
3719 #define CERT_PKEY_STRICT_FLAGS \
3720 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3721 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3723 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3728 int check_flags = 0, strict_mode;
3729 CERT_PKEY *cpk = NULL;
3732 unsigned int suiteb_flags = tls1_suiteb(s);
3733 /* idx == -1 means checking server chains */
3735 /* idx == -2 means checking client certificate chains */
3738 idx = (int)(cpk - c->pkeys);
3740 cpk = c->pkeys + idx;
3741 pvalid = s->s3->tmp.valid_flags + idx;
3743 pk = cpk->privatekey;
3745 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3746 /* If no cert or key, forget it */
3752 idx = ssl_cert_type(x, pk);
3755 pvalid = s->s3->tmp.valid_flags + idx;
3757 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3758 check_flags = CERT_PKEY_STRICT_FLAGS;
3760 check_flags = CERT_PKEY_VALID_FLAGS;
3767 check_flags |= CERT_PKEY_SUITEB;
3768 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3769 if (ok == X509_V_OK)
3770 rv |= CERT_PKEY_SUITEB;
3771 else if (!check_flags)
3776 * Check all signature algorithms are consistent with signature
3777 * algorithms extension if TLS 1.2 or later and strict mode.
3779 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3781 unsigned char rsign = 0;
3782 if (s->s3->tmp.peer_sigalgs)
3784 /* If no sigalgs extension use defaults from RFC5246 */
3787 case SSL_PKEY_RSA_ENC:
3788 case SSL_PKEY_RSA_SIGN:
3789 rsign = TLSEXT_signature_rsa;
3790 default_nid = NID_sha1WithRSAEncryption;
3793 case SSL_PKEY_DSA_SIGN:
3794 rsign = TLSEXT_signature_dsa;
3795 default_nid = NID_dsaWithSHA1;
3799 rsign = TLSEXT_signature_ecdsa;
3800 default_nid = NID_ecdsa_with_SHA1;
3803 case SSL_PKEY_GOST01:
3804 rsign = TLSEXT_signature_gostr34102001;
3805 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3808 case SSL_PKEY_GOST12_256:
3809 rsign = TLSEXT_signature_gostr34102012_256;
3810 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3813 case SSL_PKEY_GOST12_512:
3814 rsign = TLSEXT_signature_gostr34102012_512;
3815 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3824 * If peer sent no signature algorithms extension and we have set
3825 * preferred signature algorithms check we support sha1.
3827 if (default_nid > 0 && c->conf_sigalgs) {
3829 const unsigned char *p = c->conf_sigalgs;
3830 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3831 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3834 if (j == c->conf_sigalgslen) {
3841 /* Check signature algorithm of each cert in chain */
3842 if (!tls1_check_sig_alg(c, x, default_nid)) {
3846 rv |= CERT_PKEY_EE_SIGNATURE;
3847 rv |= CERT_PKEY_CA_SIGNATURE;
3848 for (i = 0; i < sk_X509_num(chain); i++) {
3849 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3851 rv &= ~CERT_PKEY_CA_SIGNATURE;
3858 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3859 else if (check_flags)
3860 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3862 /* Check cert parameters are consistent */
3863 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3864 rv |= CERT_PKEY_EE_PARAM;
3865 else if (!check_flags)
3868 rv |= CERT_PKEY_CA_PARAM;
3869 /* In strict mode check rest of chain too */
3870 else if (strict_mode) {
3871 rv |= CERT_PKEY_CA_PARAM;
3872 for (i = 0; i < sk_X509_num(chain); i++) {
3873 X509 *ca = sk_X509_value(chain, i);
3874 if (!tls1_check_cert_param(s, ca, 0)) {
3876 rv &= ~CERT_PKEY_CA_PARAM;
3883 if (!s->server && strict_mode) {
3884 STACK_OF(X509_NAME) *ca_dn;
3886 switch (EVP_PKEY_id(pk)) {
3888 check_type = TLS_CT_RSA_SIGN;
3891 check_type = TLS_CT_DSS_SIGN;
3894 check_type = TLS_CT_ECDSA_SIGN;
3898 const unsigned char *ctypes;
3902 ctypelen = (int)c->ctype_num;
3904 ctypes = (unsigned char *)s->s3->tmp.ctype;
3905 ctypelen = s->s3->tmp.ctype_num;
3907 for (i = 0; i < ctypelen; i++) {
3908 if (ctypes[i] == check_type) {
3909 rv |= CERT_PKEY_CERT_TYPE;
3913 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3916 rv |= CERT_PKEY_CERT_TYPE;
3918 ca_dn = s->s3->tmp.ca_names;
3920 if (!sk_X509_NAME_num(ca_dn))
3921 rv |= CERT_PKEY_ISSUER_NAME;
3923 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3924 if (ssl_check_ca_name(ca_dn, x))
3925 rv |= CERT_PKEY_ISSUER_NAME;
3927 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3928 for (i = 0; i < sk_X509_num(chain); i++) {
3929 X509 *xtmp = sk_X509_value(chain, i);
3930 if (ssl_check_ca_name(ca_dn, xtmp)) {
3931 rv |= CERT_PKEY_ISSUER_NAME;
3936 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3939 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3941 if (!check_flags || (rv & check_flags) == check_flags)
3942 rv |= CERT_PKEY_VALID;
3946 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3947 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3948 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3949 else if (s->s3->tmp.md[idx] != NULL)
3950 rv |= CERT_PKEY_SIGN;
3952 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3955 * When checking a CERT_PKEY structure all flags are irrelevant if the
3959 if (rv & CERT_PKEY_VALID)
3962 /* Preserve explicit sign flag, clear rest */
3963 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3970 /* Set validity of certificates in an SSL structure */
3971 void tls1_set_cert_validity(SSL *s)
3973 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3974 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3975 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3976 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3977 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
3978 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
3979 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
3982 /* User level utiity function to check a chain is suitable */
3983 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3985 return tls1_check_chain(s, x, pk, chain, -1);
3988 #ifndef OPENSSL_NO_DH
3989 DH *ssl_get_auto_dh(SSL *s)
3991 int dh_secbits = 80;
3992 if (s->cert->dh_tmp_auto == 2)
3993 return DH_get_1024_160();
3994 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
3995 if (s->s3->tmp.new_cipher->strength_bits == 256)
4000 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4001 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4004 if (dh_secbits >= 128) {
4012 if (dh_secbits >= 192)
4013 p = BN_get_rfc3526_prime_8192(NULL);
4015 p = BN_get_rfc3526_prime_3072(NULL);
4016 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4024 if (dh_secbits >= 112)
4025 return DH_get_2048_224();
4026 return DH_get_1024_160();
4030 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4033 EVP_PKEY *pkey = X509_get0_pubkey(x);
4036 * If no parameters this will return -1 and fail using the default
4037 * security callback for any non-zero security level. This will
4038 * reject keys which omit parameters but this only affects DSA and
4039 * omission of parameters is never (?) done in practice.
4041 secbits = EVP_PKEY_security_bits(pkey);
4044 return ssl_security(s, op, secbits, 0, x);
4046 return ssl_ctx_security(ctx, op, secbits, 0, x);
4049 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4051 /* Lookup signature algorithm digest */
4052 int secbits = -1, md_nid = NID_undef, sig_nid;
4053 /* Don't check signature if self signed */
4054 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4056 sig_nid = X509_get_signature_nid(x);
4057 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4059 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4060 secbits = EVP_MD_size(md) * 4;
4063 return ssl_security(s, op, secbits, md_nid, x);
4065 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4068 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4071 vfy = SSL_SECOP_PEER;
4073 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4074 return SSL_R_EE_KEY_TOO_SMALL;
4076 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4077 return SSL_R_CA_KEY_TOO_SMALL;
4079 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4080 return SSL_R_CA_MD_TOO_WEAK;
4085 * Check security of a chain, if sk includes the end entity certificate then
4086 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4087 * one to the peer. Return values: 1 if ok otherwise error code to use
4090 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4092 int rv, start_idx, i;
4094 x = sk_X509_value(sk, 0);
4099 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4103 for (i = start_idx; i < sk_X509_num(sk); i++) {
4104 x = sk_X509_value(sk, i);
4105 rv = ssl_security_cert(s, NULL, x, vfy, 0);