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);
1375 /* TODO(TLS1.3): Should we add this extension for versions < TLS1.3? */
1376 if (!SSL_IS_DTLS(s) && s->version >= TLS1_3_VERSION) {
1377 int min_version, max_version, reason, currv;
1378 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1379 || !WPACKET_start_sub_packet_u16(pkt)
1380 || !WPACKET_start_sub_packet_u8(pkt)) {
1381 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1384 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
1386 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, reason);
1389 for (currv = max_version; currv >= min_version; currv--) {
1390 /* TODO(TLS1.3): Remove this first if clause prior to release!! */
1391 if (currv == TLS1_3_VERSION) {
1392 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
1393 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,
1394 ERR_R_INTERNAL_ERROR);
1397 } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
1398 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1402 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1403 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1409 * Add padding to workaround bugs in F5 terminators. See
1410 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1411 * code works out the length of all existing extensions it MUST always
1414 if (s->options & SSL_OP_TLSEXT_PADDING) {
1415 unsigned char *padbytes;
1418 if (!WPACKET_get_total_written(pkt, &hlen)) {
1419 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1423 if (hlen > 0xff && hlen < 0x200) {
1424 hlen = 0x200 - hlen;
1430 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
1431 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
1432 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1435 memset(padbytes, 0, hlen);
1443 int ssl_add_serverhello_tlsext(SSL *s, WPACKET *pkt, int *al)
1445 #ifndef OPENSSL_NO_NEXTPROTONEG
1446 int next_proto_neg_seen;
1448 #ifndef OPENSSL_NO_EC
1449 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1450 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1451 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1452 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1455 if (!WPACKET_start_sub_packet_u16(pkt)
1456 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) {
1457 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1461 if (s->s3->send_connection_binding &&
1462 !ssl_add_serverhello_renegotiate_ext(s, pkt)) {
1463 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1467 /* Only add RI for SSLv3 */
1468 if (s->version == SSL3_VERSION)
1471 if (!s->hit && s->servername_done == 1
1472 && s->session->tlsext_hostname != NULL) {
1473 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1474 || !WPACKET_put_bytes_u16(pkt, 0)) {
1475 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1479 #ifndef OPENSSL_NO_EC
1481 const unsigned char *plist;
1484 * Add TLS extension ECPointFormats to the ServerHello message
1486 tls1_get_formatlist(s, &plist, &plistlen);
1488 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1489 || !WPACKET_start_sub_packet_u16(pkt)
1490 || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1491 || !WPACKET_close(pkt)) {
1492 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1497 * Currently the server should not respond with a SupportedCurves
1500 #endif /* OPENSSL_NO_EC */
1502 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1503 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1504 || !WPACKET_put_bytes_u16(pkt, 0)) {
1505 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1510 * if we don't add the above TLSEXT, we can't add a session ticket
1513 s->tlsext_ticket_expected = 0;
1516 if (s->tlsext_status_expected) {
1517 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1518 || !WPACKET_put_bytes_u16(pkt, 0)) {
1519 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1523 #ifndef OPENSSL_NO_SRTP
1524 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1525 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1526 || !WPACKET_start_sub_packet_u16(pkt)
1527 || !WPACKET_put_bytes_u16(pkt, 2)
1528 || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1529 || !WPACKET_put_bytes_u8(pkt, 0)
1530 || !WPACKET_close(pkt)) {
1531 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1537 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1538 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1539 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1540 const unsigned char cryptopro_ext[36] = {
1541 0xfd, 0xe8, /* 65000 */
1542 0x00, 0x20, /* 32 bytes length */
1543 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1544 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1545 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1546 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1548 if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
1549 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1553 #ifndef OPENSSL_NO_HEARTBEATS
1554 /* Add Heartbeat extension if we've received one */
1555 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1559 * 1: peer may send requests
1560 * 2: peer not allowed to send requests
1562 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1563 mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1565 mode = SSL_DTLSEXT_HB_ENABLED;
1567 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)
1568 || !WPACKET_start_sub_packet_u16(pkt)
1569 || !WPACKET_put_bytes_u8(pkt, mode)
1570 || !WPACKET_close(pkt)) {
1571 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1578 #ifndef OPENSSL_NO_NEXTPROTONEG
1579 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1580 s->s3->next_proto_neg_seen = 0;
1581 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1582 const unsigned char *npa;
1583 unsigned int npalen;
1586 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1588 ctx->next_protos_advertised_cb_arg);
1589 if (r == SSL_TLSEXT_ERR_OK) {
1590 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1591 || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1592 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1595 s->s3->next_proto_neg_seen = 1;
1599 if (!custom_ext_add(s, 1, pkt, al)) {
1600 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1604 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1606 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1607 * for other cases too.
1609 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1610 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1611 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1612 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1613 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1615 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1616 || !WPACKET_put_bytes_u16(pkt, 0)) {
1617 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1622 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1623 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1624 || !WPACKET_put_bytes_u16(pkt, 0)) {
1625 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1630 if (s->s3->alpn_selected != NULL) {
1631 if (!WPACKET_put_bytes_u16(pkt,
1632 TLSEXT_TYPE_application_layer_protocol_negotiation)
1633 || !WPACKET_start_sub_packet_u16(pkt)
1634 || !WPACKET_start_sub_packet_u16(pkt)
1635 || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
1636 s->s3->alpn_selected_len)
1637 || !WPACKET_close(pkt)
1638 || !WPACKET_close(pkt)) {
1639 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1645 if (!WPACKET_close(pkt)) {
1646 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1653 * Save the ALPN extension in a ClientHello.
1654 * pkt: the contents of the ALPN extension, not including type and length.
1655 * al: a pointer to the alert value to send in the event of a failure.
1656 * returns: 1 on success, 0 on error.
1658 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1660 PACKET protocol_list, save_protocol_list, protocol;
1662 *al = SSL_AD_DECODE_ERROR;
1664 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1665 || PACKET_remaining(&protocol_list) < 2) {
1669 save_protocol_list = protocol_list;
1671 /* Protocol names can't be empty. */
1672 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1673 || PACKET_remaining(&protocol) == 0) {
1676 } while (PACKET_remaining(&protocol_list) != 0);
1678 if (!PACKET_memdup(&save_protocol_list,
1679 &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
1680 *al = TLS1_AD_INTERNAL_ERROR;
1688 * Process the ALPN extension in a ClientHello.
1689 * al: a pointer to the alert value to send in the event of a failure.
1690 * returns 1 on success, 0 on error.
1692 static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
1694 const unsigned char *selected = NULL;
1695 unsigned char selected_len = 0;
1697 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1698 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1699 s->s3->alpn_proposed,
1700 (unsigned int)s->s3->alpn_proposed_len,
1701 s->ctx->alpn_select_cb_arg);
1703 if (r == SSL_TLSEXT_ERR_OK) {
1704 OPENSSL_free(s->s3->alpn_selected);
1705 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1706 if (s->s3->alpn_selected == NULL) {
1707 *al = SSL_AD_INTERNAL_ERROR;
1710 s->s3->alpn_selected_len = selected_len;
1711 #ifndef OPENSSL_NO_NEXTPROTONEG
1712 /* ALPN takes precedence over NPN. */
1713 s->s3->next_proto_neg_seen = 0;
1716 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1724 #ifndef OPENSSL_NO_EC
1726 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1727 * SecureTransport using the TLS extension block in |hello|.
1728 * Safari, since 10.6, sends exactly these extensions, in this order:
1733 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1734 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1735 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1736 * 10.8..10.8.3 (which don't work).
1738 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1744 static const unsigned char kSafariExtensionsBlock[] = {
1745 0x00, 0x0a, /* elliptic_curves extension */
1746 0x00, 0x08, /* 8 bytes */
1747 0x00, 0x06, /* 6 bytes of curve ids */
1748 0x00, 0x17, /* P-256 */
1749 0x00, 0x18, /* P-384 */
1750 0x00, 0x19, /* P-521 */
1752 0x00, 0x0b, /* ec_point_formats */
1753 0x00, 0x02, /* 2 bytes */
1754 0x01, /* 1 point format */
1755 0x00, /* uncompressed */
1756 /* The following is only present in TLS 1.2 */
1757 0x00, 0x0d, /* signature_algorithms */
1758 0x00, 0x0c, /* 12 bytes */
1759 0x00, 0x0a, /* 10 bytes */
1760 0x05, 0x01, /* SHA-384/RSA */
1761 0x04, 0x01, /* SHA-256/RSA */
1762 0x02, 0x01, /* SHA-1/RSA */
1763 0x04, 0x03, /* SHA-256/ECDSA */
1764 0x02, 0x03, /* SHA-1/ECDSA */
1767 /* Length of the common prefix (first two extensions). */
1768 static const size_t kSafariCommonExtensionsLength = 18;
1770 tmppkt = hello->extensions;
1772 if (!PACKET_forward(&tmppkt, 2)
1773 || !PACKET_get_net_2(&tmppkt, &type)
1774 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1778 if (type != TLSEXT_TYPE_server_name)
1781 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1782 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1784 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1787 #endif /* !OPENSSL_NO_EC */
1790 * Loop through all remaining ClientHello extensions that we collected earlier
1791 * and haven't already processed. For each one parse it and update the SSL
1792 * object as required.
1794 * Behaviour upon resumption is extension-specific. If the extension has no
1795 * effect during resumption, it is parsed (to verify its format) but otherwise
1798 * Returns 1 on success and 0 on failure.
1799 * Upon failure, sets |al| to the appropriate alert.
1801 static int ssl_scan_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello, int *al)
1804 int renegotiate_seen = 0;
1806 *al = SSL_AD_DECODE_ERROR;
1807 s->servername_done = 0;
1808 s->tlsext_status_type = -1;
1809 #ifndef OPENSSL_NO_NEXTPROTONEG
1810 s->s3->next_proto_neg_seen = 0;
1813 OPENSSL_free(s->s3->alpn_selected);
1814 s->s3->alpn_selected = NULL;
1815 s->s3->alpn_selected_len = 0;
1816 OPENSSL_free(s->s3->alpn_proposed);
1817 s->s3->alpn_proposed = NULL;
1818 s->s3->alpn_proposed_len = 0;
1819 #ifndef OPENSSL_NO_HEARTBEATS
1820 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1821 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1824 #ifndef OPENSSL_NO_EC
1825 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1826 ssl_check_for_safari(s, hello);
1827 #endif /* !OPENSSL_NO_EC */
1829 /* Clear any signature algorithms extension received */
1830 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1831 s->s3->tmp.peer_sigalgs = NULL;
1832 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1834 #ifndef OPENSSL_NO_SRP
1835 OPENSSL_free(s->srp_ctx.login);
1836 s->srp_ctx.login = NULL;
1839 s->srtp_profile = NULL;
1842 * We parse all extensions to ensure the ClientHello is well-formed but,
1843 * unless an extension specifies otherwise, we ignore extensions upon
1846 for (loop = 0; loop < hello->num_extensions; loop++) {
1847 RAW_EXTENSION *currext = &hello->pre_proc_exts[loop];
1849 if (s->tlsext_debug_cb)
1850 s->tlsext_debug_cb(s, 0, currext->type,
1851 PACKET_data(&currext->data),
1852 PACKET_remaining(&currext->data),
1853 s->tlsext_debug_arg);
1855 if (currext->type == TLSEXT_TYPE_renegotiate) {
1856 if (!ssl_parse_clienthello_renegotiate_ext(s,
1857 &currext->data, al))
1859 renegotiate_seen = 1;
1860 } else if (s->version == SSL3_VERSION) {
1863 * The servername extension is treated as follows:
1865 * - Only the hostname type is supported with a maximum length of 255.
1866 * - The servername is rejected if too long or if it contains zeros,
1867 * in which case an fatal alert is generated.
1868 * - The servername field is maintained together with the session cache.
1869 * - When a session is resumed, the servername call back invoked in order
1870 * to allow the application to position itself to the right context.
1871 * - The servername is acknowledged if it is new for a session or when
1872 * it is identical to a previously used for the same session.
1873 * Applications can control the behaviour. They can at any time
1874 * set a 'desirable' servername for a new SSL object. This can be the
1875 * case for example with HTTPS when a Host: header field is received and
1876 * a renegotiation is requested. In this case, a possible servername
1877 * presented in the new client hello is only acknowledged if it matches
1878 * the value of the Host: field.
1879 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1880 * if they provide for changing an explicit servername context for the
1881 * session, i.e. when the session has been established with a servername
1883 * - On session reconnect, the servername extension may be absent.
1887 else if (currext->type == TLSEXT_TYPE_server_name) {
1888 unsigned int servname_type;
1889 PACKET sni, hostname;
1891 if (!PACKET_as_length_prefixed_2(&currext->data, &sni)
1892 /* ServerNameList must be at least 1 byte long. */
1893 || PACKET_remaining(&sni) == 0) {
1898 * Although the server_name extension was intended to be
1899 * extensible to new name types, RFC 4366 defined the
1900 * syntax inextensibility and OpenSSL 1.0.x parses it as
1902 * RFC 6066 corrected the mistake but adding new name types
1903 * is nevertheless no longer feasible, so act as if no other
1904 * SNI types can exist, to simplify parsing.
1906 * Also note that the RFC permits only one SNI value per type,
1907 * i.e., we can only have a single hostname.
1909 if (!PACKET_get_1(&sni, &servname_type)
1910 || servname_type != TLSEXT_NAMETYPE_host_name
1911 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1916 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1917 *al = TLS1_AD_UNRECOGNIZED_NAME;
1921 if (PACKET_contains_zero_byte(&hostname)) {
1922 *al = TLS1_AD_UNRECOGNIZED_NAME;
1926 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1927 *al = TLS1_AD_INTERNAL_ERROR;
1931 s->servername_done = 1;
1934 * TODO(openssl-team): if the SNI doesn't match, we MUST
1935 * fall back to a full handshake.
1937 s->servername_done = s->session->tlsext_hostname
1938 && PACKET_equal(&hostname, s->session->tlsext_hostname,
1939 strlen(s->session->tlsext_hostname));
1942 #ifndef OPENSSL_NO_SRP
1943 else if (currext->type == TLSEXT_TYPE_srp) {
1946 if (!PACKET_as_length_prefixed_1(&currext->data, &srp_I))
1949 if (PACKET_contains_zero_byte(&srp_I))
1953 * TODO(openssl-team): currently, we re-authenticate the user
1954 * upon resumption. Instead, we MUST ignore the login.
1956 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
1957 *al = TLS1_AD_INTERNAL_ERROR;
1963 #ifndef OPENSSL_NO_EC
1964 else if (currext->type == TLSEXT_TYPE_ec_point_formats) {
1965 PACKET ec_point_format_list;
1967 if (!PACKET_as_length_prefixed_1(&currext->data,
1968 &ec_point_format_list)
1969 || PACKET_remaining(&ec_point_format_list) == 0) {
1974 if (!PACKET_memdup(&ec_point_format_list,
1975 &s->session->tlsext_ecpointformatlist,
1977 session->tlsext_ecpointformatlist_length)) {
1978 *al = TLS1_AD_INTERNAL_ERROR;
1982 } else if (currext->type == TLSEXT_TYPE_elliptic_curves) {
1983 PACKET elliptic_curve_list;
1985 /* Each NamedCurve is 2 bytes and we must have at least 1. */
1986 if (!PACKET_as_length_prefixed_2(&currext->data,
1987 &elliptic_curve_list)
1988 || PACKET_remaining(&elliptic_curve_list) == 0
1989 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
1994 if (!PACKET_memdup(&elliptic_curve_list,
1995 &s->session->tlsext_ellipticcurvelist,
1997 session->tlsext_ellipticcurvelist_length)) {
1998 *al = TLS1_AD_INTERNAL_ERROR;
2003 #endif /* OPENSSL_NO_EC */
2004 else if (currext->type == TLSEXT_TYPE_session_ticket) {
2005 if (s->tls_session_ticket_ext_cb &&
2006 !s->tls_session_ticket_ext_cb(s,
2007 PACKET_data(&currext->data),
2008 PACKET_remaining(&currext->data),
2009 s->tls_session_ticket_ext_cb_arg)) {
2010 *al = TLS1_AD_INTERNAL_ERROR;
2013 } else if (currext->type == TLSEXT_TYPE_signature_algorithms) {
2014 PACKET supported_sig_algs;
2016 if (!PACKET_as_length_prefixed_2(&currext->data,
2017 &supported_sig_algs)
2018 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2019 || PACKET_remaining(&supported_sig_algs) == 0) {
2024 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2025 PACKET_remaining(&supported_sig_algs))) {
2029 } else if (currext->type == TLSEXT_TYPE_status_request) {
2030 if (!PACKET_get_1(&currext->data,
2031 (unsigned int *)&s->tlsext_status_type)) {
2034 #ifndef OPENSSL_NO_OCSP
2035 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2036 const unsigned char *ext_data;
2037 PACKET responder_id_list, exts;
2038 if (!PACKET_get_length_prefixed_2
2039 (&currext->data, &responder_id_list))
2043 * We remove any OCSP_RESPIDs from a previous handshake
2044 * to prevent unbounded memory growth - CVE-2016-6304
2046 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2048 if (PACKET_remaining(&responder_id_list) > 0) {
2049 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2050 if (s->tlsext_ocsp_ids == NULL) {
2051 *al = SSL_AD_INTERNAL_ERROR;
2055 s->tlsext_ocsp_ids = NULL;
2058 while (PACKET_remaining(&responder_id_list) > 0) {
2060 PACKET responder_id;
2061 const unsigned char *id_data;
2063 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2065 || PACKET_remaining(&responder_id) == 0) {
2069 id_data = PACKET_data(&responder_id);
2070 /* TODO(size_t): Convert d2i_* to size_t */
2071 id = d2i_OCSP_RESPID(NULL, &id_data,
2072 (int)PACKET_remaining(&responder_id));
2076 if (id_data != PACKET_end(&responder_id)) {
2077 OCSP_RESPID_free(id);
2081 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2082 OCSP_RESPID_free(id);
2083 *al = SSL_AD_INTERNAL_ERROR;
2088 /* Read in request_extensions */
2089 if (!PACKET_as_length_prefixed_2(
2090 &currext->data, &exts))
2093 if (PACKET_remaining(&exts) > 0) {
2094 ext_data = PACKET_data(&exts);
2095 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2096 X509_EXTENSION_free);
2097 s->tlsext_ocsp_exts =
2098 d2i_X509_EXTENSIONS(NULL, &ext_data,
2099 (int)PACKET_remaining(&exts));
2100 if (s->tlsext_ocsp_exts == NULL
2101 || ext_data != PACKET_end(&exts)) {
2109 * We don't know what to do with any other type so ignore it.
2111 s->tlsext_status_type = -1;
2114 #ifndef OPENSSL_NO_HEARTBEATS
2115 else if (SSL_IS_DTLS(s) && currext->type == TLSEXT_TYPE_heartbeat) {
2116 unsigned int hbtype;
2118 if (!PACKET_get_1(&currext->data, &hbtype)
2119 || PACKET_remaining(&currext->data)) {
2120 *al = SSL_AD_DECODE_ERROR;
2124 case 0x01: /* Client allows us to send HB requests */
2125 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2127 case 0x02: /* Client doesn't accept HB requests */
2128 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2129 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2132 *al = SSL_AD_ILLEGAL_PARAMETER;
2137 #ifndef OPENSSL_NO_NEXTPROTONEG
2138 else if (currext->type == TLSEXT_TYPE_next_proto_neg
2139 && s->s3->tmp.finish_md_len == 0) {
2141 * We shouldn't accept this extension on a
2144 * s->new_session will be set on renegotiation, but we
2145 * probably shouldn't rely that it couldn't be set on
2146 * the initial renegotiation too in certain cases (when
2147 * there's some other reason to disallow resuming an
2148 * earlier session -- the current code won't be doing
2149 * anything like that, but this might change).
2151 * A valid sign that there's been a previous handshake
2152 * in this connection is if s->s3->tmp.finish_md_len >
2153 * 0. (We are talking about a check that will happen
2154 * in the Hello protocol round, well before a new
2155 * Finished message could have been computed.)
2157 s->s3->next_proto_neg_seen = 1;
2161 else if (currext->type
2162 == TLSEXT_TYPE_application_layer_protocol_negotiation
2163 && s->s3->tmp.finish_md_len == 0) {
2164 if (!tls1_alpn_handle_client_hello(s,
2165 &currext->data, al))
2169 /* session ticket processed earlier */
2170 #ifndef OPENSSL_NO_SRTP
2171 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2172 && currext->type == TLSEXT_TYPE_use_srtp) {
2173 if (ssl_parse_clienthello_use_srtp_ext(s,
2174 &currext->data, al))
2178 else if (currext->type == TLSEXT_TYPE_encrypt_then_mac
2179 && !(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
2180 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2182 * Note: extended master secret extension handled in
2183 * tls_check_client_ems_support()
2187 * If this ClientHello extension was unhandled and this is a
2188 * nonresumed connection, check whether the extension is a custom
2189 * TLS Extension (has a custom_srv_ext_record), and if so call the
2190 * callback and record the extension number so that an appropriate
2191 * ServerHello may be later returned.
2194 if (custom_ext_parse(s, 1, currext->type,
2195 PACKET_data(&currext->data),
2196 PACKET_remaining(&currext->data), al) <= 0)
2201 /* Need RI if renegotiating */
2203 if (!renegotiate_seen && s->renegotiate &&
2204 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2205 *al = SSL_AD_HANDSHAKE_FAILURE;
2206 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2207 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2212 * This function currently has no state to clean up, so it returns directly.
2213 * If parsing fails at any point, the function returns early.
2214 * The SSL object may be left with partial data from extensions, but it must
2215 * then no longer be used, and clearing it up will free the leftovers.
2220 int ssl_parse_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello)
2223 custom_ext_init(&s->cert->srv_ext);
2224 if (ssl_scan_clienthello_tlsext(s, hello, &al) <= 0) {
2225 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2228 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2229 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2235 #ifndef OPENSSL_NO_NEXTPROTONEG
2237 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2238 * elements of zero length are allowed and the set of elements must exactly
2239 * fill the length of the block.
2241 static char ssl_next_proto_validate(PACKET *pkt)
2243 PACKET tmp_protocol;
2245 while (PACKET_remaining(pkt)) {
2246 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2247 || PACKET_remaining(&tmp_protocol) == 0)
2255 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2257 unsigned int length, type, size;
2258 int tlsext_servername = 0;
2259 int renegotiate_seen = 0;
2261 #ifndef OPENSSL_NO_NEXTPROTONEG
2262 s->s3->next_proto_neg_seen = 0;
2264 s->tlsext_ticket_expected = 0;
2266 OPENSSL_free(s->s3->alpn_selected);
2267 s->s3->alpn_selected = NULL;
2268 #ifndef OPENSSL_NO_HEARTBEATS
2269 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2270 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2273 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2275 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2277 if (!PACKET_get_net_2(pkt, &length))
2280 if (PACKET_remaining(pkt) != length) {
2281 *al = SSL_AD_DECODE_ERROR;
2285 if (!tls1_check_duplicate_extensions(pkt)) {
2286 *al = SSL_AD_DECODE_ERROR;
2290 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2291 const unsigned char *data;
2294 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2295 || !PACKET_peek_bytes(&spkt, &data, size))
2298 if (s->tlsext_debug_cb)
2299 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2301 if (type == TLSEXT_TYPE_renegotiate) {
2302 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2304 renegotiate_seen = 1;
2305 } else if (s->version == SSL3_VERSION) {
2306 } else if (type == TLSEXT_TYPE_server_name) {
2307 if (s->tlsext_hostname == NULL || size > 0) {
2308 *al = TLS1_AD_UNRECOGNIZED_NAME;
2311 tlsext_servername = 1;
2313 #ifndef OPENSSL_NO_EC
2314 else if (type == TLSEXT_TYPE_ec_point_formats) {
2315 unsigned int ecpointformatlist_length;
2316 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2317 || ecpointformatlist_length != size - 1) {
2318 *al = TLS1_AD_DECODE_ERROR;
2322 s->session->tlsext_ecpointformatlist_length = 0;
2323 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2324 if ((s->session->tlsext_ecpointformatlist =
2325 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2326 *al = TLS1_AD_INTERNAL_ERROR;
2329 s->session->tlsext_ecpointformatlist_length =
2330 ecpointformatlist_length;
2331 if (!PACKET_copy_bytes(&spkt,
2332 s->session->tlsext_ecpointformatlist,
2333 ecpointformatlist_length)) {
2334 *al = TLS1_AD_DECODE_ERROR;
2340 #endif /* OPENSSL_NO_EC */
2342 else if (type == TLSEXT_TYPE_session_ticket) {
2343 if (s->tls_session_ticket_ext_cb &&
2344 !s->tls_session_ticket_ext_cb(s, data, size,
2345 s->tls_session_ticket_ext_cb_arg))
2347 *al = TLS1_AD_INTERNAL_ERROR;
2350 if (!tls_use_ticket(s) || (size > 0)) {
2351 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2354 s->tlsext_ticket_expected = 1;
2355 } else if (type == TLSEXT_TYPE_status_request) {
2357 * MUST be empty and only sent if we've requested a status
2360 if ((s->tlsext_status_type == -1) || (size > 0)) {
2361 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2364 /* Set flag to expect CertificateStatus message */
2365 s->tlsext_status_expected = 1;
2367 #ifndef OPENSSL_NO_CT
2369 * Only take it if we asked for it - i.e if there is no CT validation
2370 * callback set, then a custom extension MAY be processing it, so we
2371 * need to let control continue to flow to that.
2373 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2374 s->ct_validation_callback != NULL) {
2375 /* Simply copy it off for later processing */
2376 if (s->tlsext_scts != NULL) {
2377 OPENSSL_free(s->tlsext_scts);
2378 s->tlsext_scts = NULL;
2380 s->tlsext_scts_len = size;
2382 s->tlsext_scts = OPENSSL_malloc(size);
2383 if (s->tlsext_scts == NULL) {
2384 *al = TLS1_AD_INTERNAL_ERROR;
2387 memcpy(s->tlsext_scts, data, size);
2391 #ifndef OPENSSL_NO_NEXTPROTONEG
2392 else if (type == TLSEXT_TYPE_next_proto_neg &&
2393 s->s3->tmp.finish_md_len == 0) {
2394 unsigned char *selected;
2395 unsigned char selected_len;
2396 /* We must have requested it. */
2397 if (s->ctx->next_proto_select_cb == NULL) {
2398 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2401 /* The data must be valid */
2402 if (!ssl_next_proto_validate(&spkt)) {
2403 *al = TLS1_AD_DECODE_ERROR;
2406 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2409 ctx->next_proto_select_cb_arg) !=
2410 SSL_TLSEXT_ERR_OK) {
2411 *al = TLS1_AD_INTERNAL_ERROR;
2415 * Could be non-NULL if server has sent multiple NPN extensions in
2416 * a single Serverhello
2418 OPENSSL_free(s->next_proto_negotiated);
2419 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2420 if (s->next_proto_negotiated == NULL) {
2421 *al = TLS1_AD_INTERNAL_ERROR;
2424 memcpy(s->next_proto_negotiated, selected, selected_len);
2425 s->next_proto_negotiated_len = selected_len;
2426 s->s3->next_proto_neg_seen = 1;
2430 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2432 /* We must have requested it. */
2433 if (!s->s3->alpn_sent) {
2434 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2438 * The extension data consists of:
2439 * uint16 list_length
2440 * uint8 proto_length;
2441 * uint8 proto[proto_length];
2443 if (!PACKET_get_net_2(&spkt, &len)
2444 || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len)
2445 || PACKET_remaining(&spkt) != len) {
2446 *al = TLS1_AD_DECODE_ERROR;
2449 OPENSSL_free(s->s3->alpn_selected);
2450 s->s3->alpn_selected = OPENSSL_malloc(len);
2451 if (s->s3->alpn_selected == NULL) {
2452 *al = TLS1_AD_INTERNAL_ERROR;
2455 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2456 *al = TLS1_AD_DECODE_ERROR;
2459 s->s3->alpn_selected_len = len;
2461 #ifndef OPENSSL_NO_HEARTBEATS
2462 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2463 unsigned int hbtype;
2464 if (!PACKET_get_1(&spkt, &hbtype)) {
2465 *al = SSL_AD_DECODE_ERROR;
2469 case 0x01: /* Server allows us to send HB requests */
2470 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2472 case 0x02: /* Server doesn't accept HB requests */
2473 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2474 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2477 *al = SSL_AD_ILLEGAL_PARAMETER;
2482 #ifndef OPENSSL_NO_SRTP
2483 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2484 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2488 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2489 /* Ignore if inappropriate ciphersuite */
2490 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) &&
2491 s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2492 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2493 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2494 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2495 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2497 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2500 * If this extension type was not otherwise handled, but matches a
2501 * custom_cli_ext_record, then send it to the c callback
2503 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2507 if (PACKET_remaining(pkt) != 0) {
2508 *al = SSL_AD_DECODE_ERROR;
2512 if (!s->hit && tlsext_servername == 1) {
2513 if (s->tlsext_hostname) {
2514 if (s->session->tlsext_hostname == NULL) {
2515 s->session->tlsext_hostname =
2516 OPENSSL_strdup(s->tlsext_hostname);
2517 if (!s->session->tlsext_hostname) {
2518 *al = SSL_AD_UNRECOGNIZED_NAME;
2522 *al = SSL_AD_DECODE_ERROR;
2531 * Determine if we need to see RI. Strictly speaking if we want to avoid
2532 * an attack we should *always* see RI even on initial server hello
2533 * because the client doesn't see any renegotiation during an attack.
2534 * However this would mean we could not connect to any server which
2535 * doesn't support RI so for the immediate future tolerate RI absence
2537 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2538 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2539 *al = SSL_AD_HANDSHAKE_FAILURE;
2540 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2541 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2547 * Check extended master secret extension is consistent with
2550 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2551 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2552 *al = SSL_AD_HANDSHAKE_FAILURE;
2553 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2561 int ssl_prepare_clienthello_tlsext(SSL *s)
2563 s->s3->alpn_sent = 0;
2567 int ssl_prepare_serverhello_tlsext(SSL *s)
2572 static int ssl_check_clienthello_tlsext_early(SSL *s)
2574 int ret = SSL_TLSEXT_ERR_NOACK;
2575 int al = SSL_AD_UNRECOGNIZED_NAME;
2577 #ifndef OPENSSL_NO_EC
2579 * The handling of the ECPointFormats extension is done elsewhere, namely
2580 * in ssl3_choose_cipher in s3_lib.c.
2583 * The handling of the EllipticCurves extension is done elsewhere, namely
2584 * in ssl3_choose_cipher in s3_lib.c.
2588 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2590 s->ctx->tlsext_servername_callback(s, &al,
2591 s->ctx->tlsext_servername_arg);
2592 else if (s->initial_ctx != NULL
2593 && s->initial_ctx->tlsext_servername_callback != 0)
2595 s->initial_ctx->tlsext_servername_callback(s, &al,
2597 initial_ctx->tlsext_servername_arg);
2600 case SSL_TLSEXT_ERR_ALERT_FATAL:
2601 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2604 case SSL_TLSEXT_ERR_ALERT_WARNING:
2605 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2608 case SSL_TLSEXT_ERR_NOACK:
2609 s->servername_done = 0;
2615 /* Initialise digests to default values */
2616 void ssl_set_default_md(SSL *s)
2618 const EVP_MD **pmd = s->s3->tmp.md;
2619 #ifndef OPENSSL_NO_DSA
2620 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2622 #ifndef OPENSSL_NO_RSA
2623 if (SSL_USE_SIGALGS(s))
2624 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2626 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2627 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2629 #ifndef OPENSSL_NO_EC
2630 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2632 #ifndef OPENSSL_NO_GOST
2633 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2634 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2635 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2639 int tls1_set_server_sigalgs(SSL *s)
2644 /* Clear any shared signature algorithms */
2645 OPENSSL_free(s->cert->shared_sigalgs);
2646 s->cert->shared_sigalgs = NULL;
2647 s->cert->shared_sigalgslen = 0;
2648 /* Clear certificate digests and validity flags */
2649 for (i = 0; i < SSL_PKEY_NUM; i++) {
2650 s->s3->tmp.md[i] = NULL;
2651 s->s3->tmp.valid_flags[i] = 0;
2654 /* If sigalgs received process it. */
2655 if (s->s3->tmp.peer_sigalgs) {
2656 if (!tls1_process_sigalgs(s)) {
2657 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2658 al = SSL_AD_INTERNAL_ERROR;
2661 /* Fatal error is no shared signature algorithms */
2662 if (!s->cert->shared_sigalgs) {
2663 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2664 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
2665 al = SSL_AD_ILLEGAL_PARAMETER;
2669 ssl_set_default_md(s);
2673 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2678 * Upon success, returns 1.
2679 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
2681 int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
2683 s->tlsext_status_expected = 0;
2686 * If status request then ask callback what to do. Note: this must be
2687 * called after servername callbacks in case the certificate has changed,
2688 * and must be called after the cipher has been chosen because this may
2689 * influence which certificate is sent
2691 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2693 CERT_PKEY *certpkey;
2694 certpkey = ssl_get_server_send_pkey(s);
2695 /* If no certificate can't return certificate status */
2696 if (certpkey != NULL) {
2698 * Set current certificate to one we will use so SSL_get_certificate
2699 * et al can pick it up.
2701 s->cert->key = certpkey;
2702 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2704 /* We don't want to send a status request response */
2705 case SSL_TLSEXT_ERR_NOACK:
2706 s->tlsext_status_expected = 0;
2708 /* status request response should be sent */
2709 case SSL_TLSEXT_ERR_OK:
2710 if (s->tlsext_ocsp_resp)
2711 s->tlsext_status_expected = 1;
2713 /* something bad happened */
2714 case SSL_TLSEXT_ERR_ALERT_FATAL:
2716 *al = SSL_AD_INTERNAL_ERROR;
2722 if (!tls1_alpn_handle_client_hello_late(s, al)) {
2729 int ssl_check_serverhello_tlsext(SSL *s)
2731 int ret = SSL_TLSEXT_ERR_NOACK;
2732 int al = SSL_AD_UNRECOGNIZED_NAME;
2734 #ifndef OPENSSL_NO_EC
2736 * If we are client and using an elliptic curve cryptography cipher
2737 * suite, then if server returns an EC point formats lists extension it
2738 * must contain uncompressed.
2740 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2741 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2742 if ((s->tlsext_ecpointformatlist != NULL)
2743 && (s->tlsext_ecpointformatlist_length > 0)
2744 && (s->session->tlsext_ecpointformatlist != NULL)
2745 && (s->session->tlsext_ecpointformatlist_length > 0)
2746 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2747 /* we are using an ECC cipher */
2749 unsigned char *list;
2750 int found_uncompressed = 0;
2751 list = s->session->tlsext_ecpointformatlist;
2752 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2753 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2754 found_uncompressed = 1;
2758 if (!found_uncompressed) {
2759 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2760 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2764 ret = SSL_TLSEXT_ERR_OK;
2765 #endif /* OPENSSL_NO_EC */
2767 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2769 s->ctx->tlsext_servername_callback(s, &al,
2770 s->ctx->tlsext_servername_arg);
2771 else if (s->initial_ctx != NULL
2772 && s->initial_ctx->tlsext_servername_callback != 0)
2774 s->initial_ctx->tlsext_servername_callback(s, &al,
2776 initial_ctx->tlsext_servername_arg);
2779 * Ensure we get sensible values passed to tlsext_status_cb in the event
2780 * that we don't receive a status message
2782 OPENSSL_free(s->tlsext_ocsp_resp);
2783 s->tlsext_ocsp_resp = NULL;
2784 s->tlsext_ocsp_resplen = 0;
2787 case SSL_TLSEXT_ERR_ALERT_FATAL:
2788 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2791 case SSL_TLSEXT_ERR_ALERT_WARNING:
2792 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2795 case SSL_TLSEXT_ERR_NOACK:
2796 s->servername_done = 0;
2802 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2805 if (s->version < SSL3_VERSION)
2807 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2808 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2812 if (ssl_check_serverhello_tlsext(s) <= 0) {
2813 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2820 * Given a list of extensions that we collected earlier, find one of a given
2821 * type and return it.
2823 * |exts| is the set of extensions previously collected.
2824 * |numexts| is the number of extensions that we have.
2825 * |type| the type of the extension that we are looking for.
2827 * Returns a pointer to the found RAW_EXTENSION data, or NULL if not found.
2829 RAW_EXTENSION *tls_get_extension_by_type(RAW_EXTENSION *exts, size_t numexts,
2834 for (loop = 0; loop < numexts; loop++) {
2835 if (exts[loop].type == type)
2843 * Gets the ticket information supplied by the client if any.
2845 * hello: The parsed ClientHello data
2846 * ret: (output) on return, if a ticket was decrypted, then this is set to
2847 * point to the resulting session.
2849 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2850 * ciphersuite, in which case we have no use for session tickets and one will
2851 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2854 * -1: fatal error, either from parsing or decrypting the ticket.
2855 * 0: no ticket was found (or was ignored, based on settings).
2856 * 1: a zero length extension was found, indicating that the client supports
2857 * session tickets but doesn't currently have one to offer.
2858 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2859 * couldn't be decrypted because of a non-fatal error.
2860 * 3: a ticket was successfully decrypted and *ret was set.
2863 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2864 * a new session ticket to the client because the client indicated support
2865 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2866 * a session ticket or we couldn't use the one it gave us, or if
2867 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2868 * Otherwise, s->tlsext_ticket_expected is set to 0.
2870 int tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
2874 const unsigned char *etick;
2876 RAW_EXTENSION *ticketext;
2879 s->tlsext_ticket_expected = 0;
2882 * If tickets disabled behave as if no ticket present to permit stateful
2885 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
2888 ticketext = tls_get_extension_by_type(hello->pre_proc_exts,
2889 hello->num_extensions,
2890 TLSEXT_TYPE_session_ticket);
2891 if (ticketext == NULL)
2894 size = PACKET_remaining(&ticketext->data);
2897 * The client will accept a ticket but doesn't currently have
2900 s->tlsext_ticket_expected = 1;
2903 if (s->tls_session_secret_cb) {
2905 * Indicate that the ticket couldn't be decrypted rather than
2906 * generating the session from ticket now, trigger
2907 * abbreviated handshake based on external mechanism to
2908 * calculate the master secret later.
2912 if (!PACKET_get_bytes(&ticketext->data, &etick, size)) {
2913 /* Shouldn't ever happen */
2916 retv = tls_decrypt_ticket(s, etick, size, hello->session_id,
2917 hello->session_id_len, ret);
2919 case 2: /* ticket couldn't be decrypted */
2920 s->tlsext_ticket_expected = 1;
2923 case 3: /* ticket was decrypted */
2926 case 4: /* ticket decrypted but need to renew */
2927 s->tlsext_ticket_expected = 1;
2930 default: /* fatal error */
2936 * Sets the extended master secret flag if the extension is present in the
2942 int tls_check_client_ems_support(SSL *s, const CLIENTHELLO_MSG *hello)
2944 RAW_EXTENSION *emsext;
2946 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2948 if (s->version <= SSL3_VERSION)
2951 emsext = tls_get_extension_by_type(hello->pre_proc_exts,
2952 hello->num_extensions,
2953 TLSEXT_TYPE_extended_master_secret);
2956 * No extensions is a success - we have successfully discovered that the
2957 * client doesn't support EMS.
2962 /* The extensions must always be empty */
2963 if (PACKET_remaining(&emsext->data) != 0)
2966 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2972 * tls_decrypt_ticket attempts to decrypt a session ticket.
2974 * etick: points to the body of the session ticket extension.
2975 * eticklen: the length of the session tickets extension.
2976 * sess_id: points at the session ID.
2977 * sesslen: the length of the session ID.
2978 * psess: (output) on return, if a ticket was decrypted, then this is set to
2979 * point to the resulting session.
2982 * -2: fatal error, malloc failure.
2983 * -1: fatal error, either from parsing or decrypting the ticket.
2984 * 2: the ticket couldn't be decrypted.
2985 * 3: a ticket was successfully decrypted and *psess was set.
2986 * 4: same as 3, but the ticket needs to be renewed.
2988 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2989 size_t eticklen, const unsigned char *sess_id,
2990 size_t sesslen, SSL_SESSION **psess)
2993 unsigned char *sdec;
2994 const unsigned char *p;
2995 int slen, renew_ticket = 0, ret = -1, declen;
2997 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2998 HMAC_CTX *hctx = NULL;
2999 EVP_CIPHER_CTX *ctx;
3000 SSL_CTX *tctx = s->initial_ctx;
3002 /* Initialize session ticket encryption and HMAC contexts */
3003 hctx = HMAC_CTX_new();
3006 ctx = EVP_CIPHER_CTX_new();
3011 if (tctx->tlsext_ticket_key_cb) {
3012 unsigned char *nctick = (unsigned char *)etick;
3013 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3024 /* Check key name matches */
3025 if (memcmp(etick, tctx->tlsext_tick_key_name,
3026 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3030 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3031 sizeof(tctx->tlsext_tick_hmac_key),
3032 EVP_sha256(), NULL) <= 0
3033 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3034 tctx->tlsext_tick_aes_key,
3035 etick + sizeof(tctx->tlsext_tick_key_name)) <=
3041 * Attempt to process session ticket, first conduct sanity and integrity
3044 mlen = HMAC_size(hctx);
3048 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3050 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
3055 /* Check HMAC of encrypted ticket */
3056 if (HMAC_Update(hctx, etick, eticklen) <= 0
3057 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3060 HMAC_CTX_free(hctx);
3061 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3062 EVP_CIPHER_CTX_free(ctx);
3065 /* Attempt to decrypt session data */
3066 /* Move p after IV to start of encrypted ticket, update length */
3067 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3068 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3069 sdec = OPENSSL_malloc(eticklen);
3070 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
3071 (int)eticklen) <= 0) {
3072 EVP_CIPHER_CTX_free(ctx);
3076 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
3077 EVP_CIPHER_CTX_free(ctx);
3082 EVP_CIPHER_CTX_free(ctx);
3086 sess = d2i_SSL_SESSION(NULL, &p, slen);
3090 * The session ID, if non-empty, is used by some clients to detect
3091 * that the ticket has been accepted. So we copy it to the session
3092 * structure. If it is empty set length to zero as required by
3096 memcpy(sess->session_id, sess_id, sesslen);
3097 sess->session_id_length = sesslen;
3106 * For session parse failure, indicate that we need to send a new ticket.
3110 EVP_CIPHER_CTX_free(ctx);
3111 HMAC_CTX_free(hctx);
3115 /* Tables to translate from NIDs to TLS v1.2 ids */
3122 static const tls12_lookup tls12_md[] = {
3123 {NID_md5, TLSEXT_hash_md5},
3124 {NID_sha1, TLSEXT_hash_sha1},
3125 {NID_sha224, TLSEXT_hash_sha224},
3126 {NID_sha256, TLSEXT_hash_sha256},
3127 {NID_sha384, TLSEXT_hash_sha384},
3128 {NID_sha512, TLSEXT_hash_sha512},
3129 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3130 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3131 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3134 static const tls12_lookup tls12_sig[] = {
3135 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3136 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3137 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3138 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3139 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3140 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3143 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3146 for (i = 0; i < tlen; i++) {
3147 if (table[i].nid == nid)
3153 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3156 for (i = 0; i < tlen; i++) {
3157 if ((table[i].id) == id)
3158 return table[i].nid;
3163 int tls12_get_sigandhash(WPACKET *pkt, const EVP_PKEY *pk, const EVP_MD *md)
3169 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3172 sig_id = tls12_get_sigid(pk);
3175 if (!WPACKET_put_bytes_u8(pkt, md_id) || !WPACKET_put_bytes_u8(pkt, sig_id))
3181 int tls12_get_sigid(const EVP_PKEY *pk)
3183 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3190 unsigned char tlsext_hash;
3193 static const tls12_hash_info tls12_md_info[] = {
3194 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3195 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3196 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3197 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3198 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3199 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3200 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3201 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX,
3202 TLSEXT_hash_gostr34112012_256},
3203 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX,
3204 TLSEXT_hash_gostr34112012_512},
3207 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3213 for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) {
3214 if (tls12_md_info[i].tlsext_hash == hash_alg)
3215 return tls12_md_info + i;
3221 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3223 const tls12_hash_info *inf;
3224 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3226 inf = tls12_get_hash_info(hash_alg);
3229 return ssl_md(inf->md_idx);
3232 static int tls12_get_pkey_idx(unsigned char sig_alg)
3235 #ifndef OPENSSL_NO_RSA
3236 case TLSEXT_signature_rsa:
3237 return SSL_PKEY_RSA_SIGN;
3239 #ifndef OPENSSL_NO_DSA
3240 case TLSEXT_signature_dsa:
3241 return SSL_PKEY_DSA_SIGN;
3243 #ifndef OPENSSL_NO_EC
3244 case TLSEXT_signature_ecdsa:
3245 return SSL_PKEY_ECC;
3247 #ifndef OPENSSL_NO_GOST
3248 case TLSEXT_signature_gostr34102001:
3249 return SSL_PKEY_GOST01;
3251 case TLSEXT_signature_gostr34102012_256:
3252 return SSL_PKEY_GOST12_256;
3254 case TLSEXT_signature_gostr34102012_512:
3255 return SSL_PKEY_GOST12_512;
3261 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3262 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3263 int *psignhash_nid, const unsigned char *data)
3265 int sign_nid = NID_undef, hash_nid = NID_undef;
3266 if (!phash_nid && !psign_nid && !psignhash_nid)
3268 if (phash_nid || psignhash_nid) {
3269 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3271 *phash_nid = hash_nid;
3273 if (psign_nid || psignhash_nid) {
3274 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3276 *psign_nid = sign_nid;
3278 if (psignhash_nid) {
3279 if (sign_nid == NID_undef || hash_nid == NID_undef
3280 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0)
3281 *psignhash_nid = NID_undef;
3285 /* Check to see if a signature algorithm is allowed */
3286 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3288 /* See if we have an entry in the hash table and it is enabled */
3289 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3290 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3292 /* See if public key algorithm allowed */
3293 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3295 /* Finally see if security callback allows it */
3296 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3300 * Get a mask of disabled public key algorithms based on supported signature
3301 * algorithms. For example if no signature algorithm supports RSA then RSA is
3305 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3307 const unsigned char *sigalgs;
3308 size_t i, sigalgslen;
3309 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3311 * Now go through all signature algorithms seeing if we support any for
3312 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3313 * down calls to security callback only check if we have to.
3315 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3316 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3317 switch (sigalgs[1]) {
3318 #ifndef OPENSSL_NO_RSA
3319 case TLSEXT_signature_rsa:
3320 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3324 #ifndef OPENSSL_NO_DSA
3325 case TLSEXT_signature_dsa:
3326 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3330 #ifndef OPENSSL_NO_EC
3331 case TLSEXT_signature_ecdsa:
3332 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3339 *pmask_a |= SSL_aRSA;
3341 *pmask_a |= SSL_aDSS;
3343 *pmask_a |= SSL_aECDSA;
3346 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
3347 const unsigned char *psig, size_t psiglen)
3351 for (i = 0; i < psiglen; i += 2, psig += 2) {
3352 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3353 if (!WPACKET_put_bytes_u8(pkt, psig[0])
3354 || !WPACKET_put_bytes_u8(pkt, psig[1]))
3361 /* Given preference and allowed sigalgs set shared sigalgs */
3362 static size_t tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3363 const unsigned char *pref, size_t preflen,
3364 const unsigned char *allow, size_t allowlen)
3366 const unsigned char *ptmp, *atmp;
3367 size_t i, j, nmatch = 0;
3368 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3369 /* Skip disabled hashes or signature algorithms */
3370 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3372 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3373 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3376 shsig->rhash = ptmp[0];
3377 shsig->rsign = ptmp[1];
3378 tls1_lookup_sigalg(&shsig->hash_nid,
3380 &shsig->signandhash_nid, ptmp);
3390 /* Set shared signature algorithms for SSL structures */
3391 static int tls1_set_shared_sigalgs(SSL *s)
3393 const unsigned char *pref, *allow, *conf;
3394 size_t preflen, allowlen, conflen;
3396 TLS_SIGALGS *salgs = NULL;
3398 unsigned int is_suiteb = tls1_suiteb(s);
3400 OPENSSL_free(c->shared_sigalgs);
3401 c->shared_sigalgs = NULL;
3402 c->shared_sigalgslen = 0;
3403 /* If client use client signature algorithms if not NULL */
3404 if (!s->server && c->client_sigalgs && !is_suiteb) {
3405 conf = c->client_sigalgs;
3406 conflen = c->client_sigalgslen;
3407 } else if (c->conf_sigalgs && !is_suiteb) {
3408 conf = c->conf_sigalgs;
3409 conflen = c->conf_sigalgslen;
3411 conflen = tls12_get_psigalgs(s, &conf);
3412 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3415 allow = s->s3->tmp.peer_sigalgs;
3416 allowlen = s->s3->tmp.peer_sigalgslen;
3420 pref = s->s3->tmp.peer_sigalgs;
3421 preflen = s->s3->tmp.peer_sigalgslen;
3423 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3425 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3428 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3432 c->shared_sigalgs = salgs;
3433 c->shared_sigalgslen = nmatch;
3437 /* Set preferred digest for each key type */
3439 int tls1_save_sigalgs(SSL *s, const unsigned char *data, size_t dsize)
3442 /* Extension ignored for inappropriate versions */
3443 if (!SSL_USE_SIGALGS(s))
3445 /* Should never happen */
3449 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3450 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3451 if (s->s3->tmp.peer_sigalgs == NULL)
3453 s->s3->tmp.peer_sigalgslen = dsize;
3454 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3458 int tls1_process_sigalgs(SSL *s)
3463 const EVP_MD **pmd = s->s3->tmp.md;
3464 uint32_t *pvalid = s->s3->tmp.valid_flags;
3466 TLS_SIGALGS *sigptr;
3467 if (!tls1_set_shared_sigalgs(s))
3470 for (i = 0, sigptr = c->shared_sigalgs;
3471 i < c->shared_sigalgslen; i++, sigptr++) {
3472 idx = tls12_get_pkey_idx(sigptr->rsign);
3473 if (idx > 0 && pmd[idx] == NULL) {
3474 md = tls12_get_hash(sigptr->rhash);
3476 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3477 if (idx == SSL_PKEY_RSA_SIGN) {
3478 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3479 pmd[SSL_PKEY_RSA_ENC] = md;
3485 * In strict mode leave unset digests as NULL to indicate we can't use
3486 * the certificate for signing.
3488 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3490 * Set any remaining keys to default values. NOTE: if alg is not
3491 * supported it stays as NULL.
3493 #ifndef OPENSSL_NO_DSA
3494 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3495 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3497 #ifndef OPENSSL_NO_RSA
3498 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3499 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3500 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3503 #ifndef OPENSSL_NO_EC
3504 if (pmd[SSL_PKEY_ECC] == NULL)
3505 pmd[SSL_PKEY_ECC] = EVP_sha1();
3507 #ifndef OPENSSL_NO_GOST
3508 if (pmd[SSL_PKEY_GOST01] == NULL)
3509 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3510 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3511 pmd[SSL_PKEY_GOST12_256] =
3512 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3513 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3514 pmd[SSL_PKEY_GOST12_512] =
3515 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3521 int SSL_get_sigalgs(SSL *s, int idx,
3522 int *psign, int *phash, int *psignhash,
3523 unsigned char *rsig, unsigned char *rhash)
3525 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3526 size_t numsigalgs = s->s3->tmp.peer_sigalgslen / 2;
3527 if (psig == NULL || numsigalgs > INT_MAX)
3531 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3538 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3540 return (int)numsigalgs;
3543 int SSL_get_shared_sigalgs(SSL *s, int idx,
3544 int *psign, int *phash, int *psignhash,
3545 unsigned char *rsig, unsigned char *rhash)
3547 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3548 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen
3549 || s->cert->shared_sigalgslen > INT_MAX)
3553 *phash = shsigalgs->hash_nid;
3555 *psign = shsigalgs->sign_nid;
3557 *psignhash = shsigalgs->signandhash_nid;
3559 *rsig = shsigalgs->rsign;
3561 *rhash = shsigalgs->rhash;
3562 return (int)s->cert->shared_sigalgslen;
3565 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3569 int sigalgs[MAX_SIGALGLEN];
3572 static void get_sigorhash(int *psig, int *phash, const char *str)
3574 if (strcmp(str, "RSA") == 0) {
3575 *psig = EVP_PKEY_RSA;
3576 } else if (strcmp(str, "DSA") == 0) {
3577 *psig = EVP_PKEY_DSA;
3578 } else if (strcmp(str, "ECDSA") == 0) {
3579 *psig = EVP_PKEY_EC;
3581 *phash = OBJ_sn2nid(str);
3582 if (*phash == NID_undef)
3583 *phash = OBJ_ln2nid(str);
3587 static int sig_cb(const char *elem, int len, void *arg)
3589 sig_cb_st *sarg = arg;
3592 int sig_alg = NID_undef, hash_alg = NID_undef;
3595 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3597 if (len > (int)(sizeof(etmp) - 1))
3599 memcpy(etmp, elem, len);
3601 p = strchr(etmp, '+');
3609 get_sigorhash(&sig_alg, &hash_alg, etmp);
3610 get_sigorhash(&sig_alg, &hash_alg, p);
3612 if (sig_alg == NID_undef || hash_alg == NID_undef)
3615 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3616 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3619 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3620 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3625 * Set supported signature algorithms based on a colon separated list of the
3626 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3628 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3632 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3636 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3639 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3641 unsigned char *sigalgs, *sptr;
3646 sigalgs = OPENSSL_malloc(salglen);
3647 if (sigalgs == NULL)
3649 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3650 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3651 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3653 if (rhash == -1 || rsign == -1)
3660 OPENSSL_free(c->client_sigalgs);
3661 c->client_sigalgs = sigalgs;
3662 c->client_sigalgslen = salglen;
3664 OPENSSL_free(c->conf_sigalgs);
3665 c->conf_sigalgs = sigalgs;
3666 c->conf_sigalgslen = salglen;
3672 OPENSSL_free(sigalgs);
3676 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3680 if (default_nid == -1)
3682 sig_nid = X509_get_signature_nid(x);
3684 return sig_nid == default_nid ? 1 : 0;
3685 for (i = 0; i < c->shared_sigalgslen; i++)
3686 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3691 /* Check to see if a certificate issuer name matches list of CA names */
3692 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3696 nm = X509_get_issuer_name(x);
3697 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3698 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3705 * Check certificate chain is consistent with TLS extensions and is usable by
3706 * server. This servers two purposes: it allows users to check chains before
3707 * passing them to the server and it allows the server to check chains before
3708 * attempting to use them.
3711 /* Flags which need to be set for a certificate when stict mode not set */
3713 #define CERT_PKEY_VALID_FLAGS \
3714 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3715 /* Strict mode flags */
3716 #define CERT_PKEY_STRICT_FLAGS \
3717 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3718 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3720 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3725 int check_flags = 0, strict_mode;
3726 CERT_PKEY *cpk = NULL;
3729 unsigned int suiteb_flags = tls1_suiteb(s);
3730 /* idx == -1 means checking server chains */
3732 /* idx == -2 means checking client certificate chains */
3735 idx = (int)(cpk - c->pkeys);
3737 cpk = c->pkeys + idx;
3738 pvalid = s->s3->tmp.valid_flags + idx;
3740 pk = cpk->privatekey;
3742 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3743 /* If no cert or key, forget it */
3749 idx = ssl_cert_type(x, pk);
3752 pvalid = s->s3->tmp.valid_flags + idx;
3754 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3755 check_flags = CERT_PKEY_STRICT_FLAGS;
3757 check_flags = CERT_PKEY_VALID_FLAGS;
3764 check_flags |= CERT_PKEY_SUITEB;
3765 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3766 if (ok == X509_V_OK)
3767 rv |= CERT_PKEY_SUITEB;
3768 else if (!check_flags)
3773 * Check all signature algorithms are consistent with signature
3774 * algorithms extension if TLS 1.2 or later and strict mode.
3776 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3778 unsigned char rsign = 0;
3779 if (s->s3->tmp.peer_sigalgs)
3781 /* If no sigalgs extension use defaults from RFC5246 */
3784 case SSL_PKEY_RSA_ENC:
3785 case SSL_PKEY_RSA_SIGN:
3786 rsign = TLSEXT_signature_rsa;
3787 default_nid = NID_sha1WithRSAEncryption;
3790 case SSL_PKEY_DSA_SIGN:
3791 rsign = TLSEXT_signature_dsa;
3792 default_nid = NID_dsaWithSHA1;
3796 rsign = TLSEXT_signature_ecdsa;
3797 default_nid = NID_ecdsa_with_SHA1;
3800 case SSL_PKEY_GOST01:
3801 rsign = TLSEXT_signature_gostr34102001;
3802 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3805 case SSL_PKEY_GOST12_256:
3806 rsign = TLSEXT_signature_gostr34102012_256;
3807 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3810 case SSL_PKEY_GOST12_512:
3811 rsign = TLSEXT_signature_gostr34102012_512;
3812 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3821 * If peer sent no signature algorithms extension and we have set
3822 * preferred signature algorithms check we support sha1.
3824 if (default_nid > 0 && c->conf_sigalgs) {
3826 const unsigned char *p = c->conf_sigalgs;
3827 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3828 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3831 if (j == c->conf_sigalgslen) {
3838 /* Check signature algorithm of each cert in chain */
3839 if (!tls1_check_sig_alg(c, x, default_nid)) {
3843 rv |= CERT_PKEY_EE_SIGNATURE;
3844 rv |= CERT_PKEY_CA_SIGNATURE;
3845 for (i = 0; i < sk_X509_num(chain); i++) {
3846 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3848 rv &= ~CERT_PKEY_CA_SIGNATURE;
3855 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3856 else if (check_flags)
3857 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3859 /* Check cert parameters are consistent */
3860 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3861 rv |= CERT_PKEY_EE_PARAM;
3862 else if (!check_flags)
3865 rv |= CERT_PKEY_CA_PARAM;
3866 /* In strict mode check rest of chain too */
3867 else if (strict_mode) {
3868 rv |= CERT_PKEY_CA_PARAM;
3869 for (i = 0; i < sk_X509_num(chain); i++) {
3870 X509 *ca = sk_X509_value(chain, i);
3871 if (!tls1_check_cert_param(s, ca, 0)) {
3873 rv &= ~CERT_PKEY_CA_PARAM;
3880 if (!s->server && strict_mode) {
3881 STACK_OF(X509_NAME) *ca_dn;
3883 switch (EVP_PKEY_id(pk)) {
3885 check_type = TLS_CT_RSA_SIGN;
3888 check_type = TLS_CT_DSS_SIGN;
3891 check_type = TLS_CT_ECDSA_SIGN;
3895 const unsigned char *ctypes;
3899 ctypelen = (int)c->ctype_num;
3901 ctypes = (unsigned char *)s->s3->tmp.ctype;
3902 ctypelen = s->s3->tmp.ctype_num;
3904 for (i = 0; i < ctypelen; i++) {
3905 if (ctypes[i] == check_type) {
3906 rv |= CERT_PKEY_CERT_TYPE;
3910 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3913 rv |= CERT_PKEY_CERT_TYPE;
3915 ca_dn = s->s3->tmp.ca_names;
3917 if (!sk_X509_NAME_num(ca_dn))
3918 rv |= CERT_PKEY_ISSUER_NAME;
3920 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3921 if (ssl_check_ca_name(ca_dn, x))
3922 rv |= CERT_PKEY_ISSUER_NAME;
3924 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3925 for (i = 0; i < sk_X509_num(chain); i++) {
3926 X509 *xtmp = sk_X509_value(chain, i);
3927 if (ssl_check_ca_name(ca_dn, xtmp)) {
3928 rv |= CERT_PKEY_ISSUER_NAME;
3933 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3936 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3938 if (!check_flags || (rv & check_flags) == check_flags)
3939 rv |= CERT_PKEY_VALID;
3943 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3944 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3945 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3946 else if (s->s3->tmp.md[idx] != NULL)
3947 rv |= CERT_PKEY_SIGN;
3949 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3952 * When checking a CERT_PKEY structure all flags are irrelevant if the
3956 if (rv & CERT_PKEY_VALID)
3959 /* Preserve explicit sign flag, clear rest */
3960 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3967 /* Set validity of certificates in an SSL structure */
3968 void tls1_set_cert_validity(SSL *s)
3970 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3971 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3972 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3973 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3974 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
3975 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
3976 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
3979 /* User level utiity function to check a chain is suitable */
3980 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3982 return tls1_check_chain(s, x, pk, chain, -1);
3985 #ifndef OPENSSL_NO_DH
3986 DH *ssl_get_auto_dh(SSL *s)
3988 int dh_secbits = 80;
3989 if (s->cert->dh_tmp_auto == 2)
3990 return DH_get_1024_160();
3991 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
3992 if (s->s3->tmp.new_cipher->strength_bits == 256)
3997 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
3998 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4001 if (dh_secbits >= 128) {
4009 if (dh_secbits >= 192)
4010 p = BN_get_rfc3526_prime_8192(NULL);
4012 p = BN_get_rfc3526_prime_3072(NULL);
4013 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4021 if (dh_secbits >= 112)
4022 return DH_get_2048_224();
4023 return DH_get_1024_160();
4027 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4030 EVP_PKEY *pkey = X509_get0_pubkey(x);
4033 * If no parameters this will return -1 and fail using the default
4034 * security callback for any non-zero security level. This will
4035 * reject keys which omit parameters but this only affects DSA and
4036 * omission of parameters is never (?) done in practice.
4038 secbits = EVP_PKEY_security_bits(pkey);
4041 return ssl_security(s, op, secbits, 0, x);
4043 return ssl_ctx_security(ctx, op, secbits, 0, x);
4046 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4048 /* Lookup signature algorithm digest */
4049 int secbits = -1, md_nid = NID_undef, sig_nid;
4050 /* Don't check signature if self signed */
4051 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4053 sig_nid = X509_get_signature_nid(x);
4054 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4056 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4057 secbits = EVP_MD_size(md) * 4;
4060 return ssl_security(s, op, secbits, md_nid, x);
4062 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4065 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4068 vfy = SSL_SECOP_PEER;
4070 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4071 return SSL_R_EE_KEY_TOO_SMALL;
4073 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4074 return SSL_R_CA_KEY_TOO_SMALL;
4076 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4077 return SSL_R_CA_MD_TOO_WEAK;
4082 * Check security of a chain, if sk includes the end entity certificate then
4083 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4084 * one to the peer. Return values: 1 if ok otherwise error code to use
4087 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4089 int rv, start_idx, i;
4091 x = sk_X509_value(sk, 0);
4096 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4100 for (i = start_idx; i < sk_X509_num(sk); i++) {
4101 x = sk_X509_value(sk, i);
4102 rv = ssl_security_cert(s, NULL, x, vfy, 0);