2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/hmac.h>
15 #include <openssl/ocsp.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/dh.h>
19 #include <openssl/bn.h>
21 #include <openssl/ct.h>
23 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, size_t ticklen,
24 const unsigned char *sess_id, size_t sesslen,
26 static int ssl_check_clienthello_tlsext_early(SSL *s);
27 static int ssl_check_serverhello_tlsext(SSL *s);
29 SSL3_ENC_METHOD const TLSv1_enc_data = {
33 tls1_generate_master_secret,
34 tls1_change_cipher_state,
35 tls1_final_finish_mac,
36 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
37 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
39 tls1_export_keying_material,
41 ssl3_set_handshake_header,
42 tls_close_construct_packet,
46 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
50 tls1_generate_master_secret,
51 tls1_change_cipher_state,
52 tls1_final_finish_mac,
53 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
54 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
56 tls1_export_keying_material,
57 SSL_ENC_FLAG_EXPLICIT_IV,
58 ssl3_set_handshake_header,
59 tls_close_construct_packet,
63 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
67 tls1_generate_master_secret,
68 tls1_change_cipher_state,
69 tls1_final_finish_mac,
70 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
71 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
73 tls1_export_keying_material,
74 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
75 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
76 ssl3_set_handshake_header,
77 tls_close_construct_packet,
81 SSL3_ENC_METHOD const TLSv1_3_enc_data = {
85 tls1_generate_master_secret,
86 tls1_change_cipher_state,
87 tls1_final_finish_mac,
88 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
89 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
91 tls1_export_keying_material,
92 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
93 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
94 ssl3_set_handshake_header,
95 tls_close_construct_packet,
99 long tls1_default_timeout(void)
102 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
103 * http, the cache would over fill
105 return (60 * 60 * 2);
112 s->method->ssl_clear(s);
116 void tls1_free(SSL *s)
118 OPENSSL_free(s->tlsext_session_ticket);
122 void tls1_clear(SSL *s)
125 if (s->method->version == TLS_ANY_VERSION)
126 s->version = TLS_MAX_VERSION;
128 s->version = s->method->version;
131 #ifndef OPENSSL_NO_EC
134 int nid; /* Curve NID */
135 int secbits; /* Bits of security (from SP800-57) */
136 unsigned int flags; /* Flags: currently just field type */
140 * Table of curve information.
141 * Do not delete entries or reorder this array! It is used as a lookup
142 * table: the index of each entry is one less than the TLS curve id.
144 static const tls_curve_info nid_list[] = {
145 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
146 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
147 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
148 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
149 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
150 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
151 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
152 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
153 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
154 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
155 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
156 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
157 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
158 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
159 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
160 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
161 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
162 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
163 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
164 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
165 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
166 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
167 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
168 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
169 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
170 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
171 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
172 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
173 {NID_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
176 static const unsigned char ecformats_default[] = {
177 TLSEXT_ECPOINTFORMAT_uncompressed,
178 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
179 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
182 /* The default curves */
183 static const unsigned char eccurves_default[] = {
184 0, 29, /* X25519 (29) */
185 0, 23, /* secp256r1 (23) */
186 0, 25, /* secp521r1 (25) */
187 0, 24, /* secp384r1 (24) */
190 static const unsigned char eccurves_all[] = {
191 0, 29, /* X25519 (29) */
192 0, 23, /* secp256r1 (23) */
193 0, 25, /* secp521r1 (25) */
194 0, 24, /* secp384r1 (24) */
195 0, 26, /* brainpoolP256r1 (26) */
196 0, 27, /* brainpoolP384r1 (27) */
197 0, 28, /* brainpool512r1 (28) */
200 * Remaining curves disabled by default but still permitted if set
201 * via an explicit callback or parameters.
203 0, 22, /* secp256k1 (22) */
204 0, 14, /* sect571r1 (14) */
205 0, 13, /* sect571k1 (13) */
206 0, 11, /* sect409k1 (11) */
207 0, 12, /* sect409r1 (12) */
208 0, 9, /* sect283k1 (9) */
209 0, 10, /* sect283r1 (10) */
210 0, 20, /* secp224k1 (20) */
211 0, 21, /* secp224r1 (21) */
212 0, 18, /* secp192k1 (18) */
213 0, 19, /* secp192r1 (19) */
214 0, 15, /* secp160k1 (15) */
215 0, 16, /* secp160r1 (16) */
216 0, 17, /* secp160r2 (17) */
217 0, 8, /* sect239k1 (8) */
218 0, 6, /* sect233k1 (6) */
219 0, 7, /* sect233r1 (7) */
220 0, 4, /* sect193r1 (4) */
221 0, 5, /* sect193r2 (5) */
222 0, 1, /* sect163k1 (1) */
223 0, 2, /* sect163r1 (2) */
224 0, 3, /* sect163r2 (3) */
227 static const unsigned char suiteb_curves[] = {
228 0, TLSEXT_curve_P_256,
229 0, TLSEXT_curve_P_384
232 int tls1_ec_curve_id2nid(int curve_id, unsigned int *pflags)
234 const tls_curve_info *cinfo;
235 /* ECC curves from RFC 4492 and RFC 7027 */
236 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
238 cinfo = nid_list + curve_id - 1;
240 *pflags = cinfo->flags;
244 int tls1_ec_nid2curve_id(int nid)
247 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
248 if (nid_list[i].nid == nid)
255 * Get curves list, if "sess" is set return client curves otherwise
257 * Sets |num_curves| to the number of curves in the list, i.e.,
258 * the length of |pcurves| is 2 * num_curves.
259 * Returns 1 on success and 0 if the client curves list has invalid format.
260 * The latter indicates an internal error: we should not be accepting such
261 * lists in the first place.
262 * TODO(emilia): we should really be storing the curves list in explicitly
263 * parsed form instead. (However, this would affect binary compatibility
264 * so cannot happen in the 1.0.x series.)
266 static int tls1_get_curvelist(SSL *s, int sess,
267 const unsigned char **pcurves, size_t *num_curves)
269 size_t pcurveslen = 0;
271 *pcurves = s->session->tlsext_ellipticcurvelist;
272 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
274 /* For Suite B mode only include P-256, P-384 */
275 switch (tls1_suiteb(s)) {
276 case SSL_CERT_FLAG_SUITEB_128_LOS:
277 *pcurves = suiteb_curves;
278 pcurveslen = sizeof(suiteb_curves);
281 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
282 *pcurves = suiteb_curves;
286 case SSL_CERT_FLAG_SUITEB_192_LOS:
287 *pcurves = suiteb_curves + 2;
291 *pcurves = s->tlsext_ellipticcurvelist;
292 pcurveslen = s->tlsext_ellipticcurvelist_length;
295 *pcurves = eccurves_default;
296 pcurveslen = sizeof(eccurves_default);
300 /* We do not allow odd length arrays to enter the system. */
301 if (pcurveslen & 1) {
302 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
306 *num_curves = pcurveslen / 2;
311 /* See if curve is allowed by security callback */
312 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
314 const tls_curve_info *cinfo;
317 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
319 cinfo = &nid_list[curve[1] - 1];
320 # ifdef OPENSSL_NO_EC2M
321 if (cinfo->flags & TLS_CURVE_CHAR2)
324 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
327 /* Check a curve is one of our preferences */
328 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
330 const unsigned char *curves;
331 size_t num_curves, i;
332 unsigned int suiteb_flags = tls1_suiteb(s);
333 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
335 /* Check curve matches Suite B preferences */
337 unsigned long cid = s->s3->tmp.new_cipher->id;
340 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
341 if (p[2] != TLSEXT_curve_P_256)
343 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
344 if (p[2] != TLSEXT_curve_P_384)
346 } else /* Should never happen */
349 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
351 for (i = 0; i < num_curves; i++, curves += 2) {
352 if (p[1] == curves[0] && p[2] == curves[1])
353 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
359 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
360 * if there is no match.
361 * For nmatch == -1, return number of matches
362 * For nmatch == -2, return the NID of the curve to use for
363 * an EC tmp key, or NID_undef if there is no match.
365 int tls1_shared_curve(SSL *s, int nmatch)
367 const unsigned char *pref, *supp;
368 size_t num_pref, num_supp, i, j;
370 /* Can't do anything on client side */
374 if (tls1_suiteb(s)) {
376 * For Suite B ciphersuite determines curve: we already know
377 * these are acceptable due to previous checks.
379 unsigned long cid = s->s3->tmp.new_cipher->id;
380 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
381 return NID_X9_62_prime256v1; /* P-256 */
382 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
383 return NID_secp384r1; /* P-384 */
384 /* Should never happen */
387 /* If not Suite B just return first preference shared curve */
391 * Avoid truncation. tls1_get_curvelist takes an int
392 * but s->options is a long...
394 if (!tls1_get_curvelist
395 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
397 /* In practice, NID_undef == 0 but let's be precise. */
398 return nmatch == -1 ? 0 : NID_undef;
399 if (!tls1_get_curvelist
400 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref, &num_pref))
401 return nmatch == -1 ? 0 : NID_undef;
404 * If the client didn't send the elliptic_curves extension all of them
407 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
409 num_supp = sizeof(eccurves_all) / 2;
410 } else if (num_pref == 0 &&
411 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
413 num_pref = sizeof(eccurves_all) / 2;
417 for (i = 0; i < num_pref; i++, pref += 2) {
418 const unsigned char *tsupp = supp;
419 for (j = 0; j < num_supp; j++, tsupp += 2) {
420 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
421 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
424 int id = (pref[0] << 8) | pref[1];
425 return tls1_ec_curve_id2nid(id, NULL);
433 /* Out of range (nmatch > k). */
437 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
438 int *curves, size_t ncurves)
440 unsigned char *clist, *p;
443 * Bitmap of curves included to detect duplicates: only works while curve
446 unsigned long dup_list = 0;
447 clist = OPENSSL_malloc(ncurves * 2);
450 for (i = 0, p = clist; i < ncurves; i++) {
451 unsigned long idmask;
453 id = tls1_ec_nid2curve_id(curves[i]);
455 if (!id || (dup_list & idmask)) {
464 *pextlen = ncurves * 2;
468 # define MAX_CURVELIST 28
472 int nid_arr[MAX_CURVELIST];
475 static int nid_cb(const char *elem, int len, void *arg)
477 nid_cb_st *narg = arg;
483 if (narg->nidcnt == MAX_CURVELIST)
485 if (len > (int)(sizeof(etmp) - 1))
487 memcpy(etmp, elem, len);
489 nid = EC_curve_nist2nid(etmp);
490 if (nid == NID_undef)
491 nid = OBJ_sn2nid(etmp);
492 if (nid == NID_undef)
493 nid = OBJ_ln2nid(etmp);
494 if (nid == NID_undef)
496 for (i = 0; i < narg->nidcnt; i++)
497 if (narg->nid_arr[i] == nid)
499 narg->nid_arr[narg->nidcnt++] = nid;
503 /* Set curves based on a colon separate list */
504 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen, const char *str)
508 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
512 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
515 /* For an EC key set TLS id and required compression based on parameters */
516 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
523 /* Determine if it is a prime field */
524 grp = EC_KEY_get0_group(ec);
527 /* Determine curve ID */
528 id = EC_GROUP_get_curve_name(grp);
529 id = tls1_ec_nid2curve_id(id);
530 /* If no id return error: we don't support arbitrary explicit curves */
534 curve_id[1] = (unsigned char)id;
536 if (EC_KEY_get0_public_key(ec) == NULL)
538 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
539 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
541 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
542 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
544 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
550 /* Check an EC key is compatible with extensions */
551 static int tls1_check_ec_key(SSL *s,
552 unsigned char *curve_id, unsigned char *comp_id)
554 const unsigned char *pformats, *pcurves;
555 size_t num_formats, num_curves, i;
558 * If point formats extension present check it, otherwise everything is
559 * supported (see RFC4492).
561 if (comp_id && s->session->tlsext_ecpointformatlist) {
562 pformats = s->session->tlsext_ecpointformatlist;
563 num_formats = s->session->tlsext_ecpointformatlist_length;
564 for (i = 0; i < num_formats; i++, pformats++) {
565 if (*comp_id == *pformats)
568 if (i == num_formats)
573 /* Check curve is consistent with client and server preferences */
574 for (j = 0; j <= 1; j++) {
575 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
577 if (j == 1 && num_curves == 0) {
579 * If we've not received any curves then skip this check.
580 * RFC 4492 does not require the supported elliptic curves extension
581 * so if it is not sent we can just choose any curve.
582 * It is invalid to send an empty list in the elliptic curves
583 * extension, so num_curves == 0 always means no extension.
587 for (i = 0; i < num_curves; i++, pcurves += 2) {
588 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
593 /* For clients can only check sent curve list */
600 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
604 * If we have a custom point format list use it otherwise use default
606 if (s->tlsext_ecpointformatlist) {
607 *pformats = s->tlsext_ecpointformatlist;
608 *num_formats = s->tlsext_ecpointformatlist_length;
610 *pformats = ecformats_default;
611 /* For Suite B we don't support char2 fields */
613 *num_formats = sizeof(ecformats_default) - 1;
615 *num_formats = sizeof(ecformats_default);
620 * Check cert parameters compatible with extensions: currently just checks EC
621 * certificates have compatible curves and compression.
623 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
625 unsigned char comp_id, curve_id[2];
628 pkey = X509_get0_pubkey(x);
631 /* If not EC nothing to do */
632 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
634 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
638 * Can't check curve_id for client certs as we don't have a supported
641 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
645 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
646 * SHA384+P-384, adjust digest if necessary.
648 if (set_ee_md && tls1_suiteb(s)) {
654 /* Check to see we have necessary signing algorithm */
655 if (curve_id[1] == TLSEXT_curve_P_256)
656 check_md = NID_ecdsa_with_SHA256;
657 else if (curve_id[1] == TLSEXT_curve_P_384)
658 check_md = NID_ecdsa_with_SHA384;
660 return 0; /* Should never happen */
661 for (i = 0; i < c->shared_sigalgslen; i++)
662 if (check_md == c->shared_sigalgs[i].signandhash_nid)
664 if (i == c->shared_sigalgslen)
666 if (set_ee_md == 2) {
667 if (check_md == NID_ecdsa_with_SHA256)
668 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
670 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
676 # ifndef OPENSSL_NO_EC
678 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
680 * @cid: Cipher ID we're considering using
682 * Checks that the kECDHE cipher suite we're considering using
683 * is compatible with the client extensions.
685 * Returns 0 when the cipher can't be used or 1 when it can.
687 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
690 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
693 if (tls1_suiteb(s)) {
694 unsigned char curve_id[2];
695 /* Curve to check determined by ciphersuite */
696 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
697 curve_id[1] = TLSEXT_curve_P_256;
698 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
699 curve_id[1] = TLSEXT_curve_P_384;
703 /* Check this curve is acceptable */
704 if (!tls1_check_ec_key(s, curve_id, NULL))
708 /* Need a shared curve */
709 if (tls1_shared_curve(s, 0))
713 # endif /* OPENSSL_NO_EC */
717 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
722 #endif /* OPENSSL_NO_EC */
725 * List of supported signature algorithms and hashes. Should make this
726 * customisable at some point, for now include everything we support.
729 #ifdef OPENSSL_NO_RSA
730 # define tlsext_sigalg_rsa(md) /* */
732 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
735 #ifdef OPENSSL_NO_DSA
736 # define tlsext_sigalg_dsa(md) /* */
738 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
742 # define tlsext_sigalg_ecdsa(md)/* */
744 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
747 #define tlsext_sigalg(md) \
748 tlsext_sigalg_rsa(md) \
749 tlsext_sigalg_dsa(md) \
750 tlsext_sigalg_ecdsa(md)
752 static const unsigned char tls12_sigalgs[] = {
753 tlsext_sigalg(TLSEXT_hash_sha512)
754 tlsext_sigalg(TLSEXT_hash_sha384)
755 tlsext_sigalg(TLSEXT_hash_sha256)
756 tlsext_sigalg(TLSEXT_hash_sha224)
757 tlsext_sigalg(TLSEXT_hash_sha1)
758 #ifndef OPENSSL_NO_GOST
759 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
760 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
761 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
765 #ifndef OPENSSL_NO_EC
766 static const unsigned char suiteb_sigalgs[] = {
767 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
768 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
771 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
774 * If Suite B mode use Suite B sigalgs only, ignore any other
777 #ifndef OPENSSL_NO_EC
778 switch (tls1_suiteb(s)) {
779 case SSL_CERT_FLAG_SUITEB_128_LOS:
780 *psigs = suiteb_sigalgs;
781 return sizeof(suiteb_sigalgs);
783 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
784 *psigs = suiteb_sigalgs;
787 case SSL_CERT_FLAG_SUITEB_192_LOS:
788 *psigs = suiteb_sigalgs + 2;
792 /* If server use client authentication sigalgs if not NULL */
793 if (s->server && s->cert->client_sigalgs) {
794 *psigs = s->cert->client_sigalgs;
795 return s->cert->client_sigalgslen;
796 } else if (s->cert->conf_sigalgs) {
797 *psigs = s->cert->conf_sigalgs;
798 return s->cert->conf_sigalgslen;
800 *psigs = tls12_sigalgs;
801 return sizeof(tls12_sigalgs);
806 * Check signature algorithm is consistent with sent supported signature
807 * algorithms and if so return relevant digest.
809 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
810 const unsigned char *sig, EVP_PKEY *pkey)
812 const unsigned char *sent_sigs;
813 size_t sent_sigslen, i;
814 int sigalg = tls12_get_sigid(pkey);
815 /* Should never happen */
818 /* Check key type is consistent with signature */
819 if (sigalg != (int)sig[1]) {
820 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
823 #ifndef OPENSSL_NO_EC
824 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
825 unsigned char curve_id[2], comp_id;
826 /* Check compression and curve matches extensions */
827 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
829 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
830 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
833 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
834 if (tls1_suiteb(s)) {
837 if (curve_id[1] == TLSEXT_curve_P_256) {
838 if (sig[0] != TLSEXT_hash_sha256) {
839 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
840 SSL_R_ILLEGAL_SUITEB_DIGEST);
843 } else if (curve_id[1] == TLSEXT_curve_P_384) {
844 if (sig[0] != TLSEXT_hash_sha384) {
845 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
846 SSL_R_ILLEGAL_SUITEB_DIGEST);
852 } else if (tls1_suiteb(s))
856 /* Check signature matches a type we sent */
857 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
858 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
859 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
862 /* Allow fallback to SHA1 if not strict mode */
863 if (i == sent_sigslen
864 && (sig[0] != TLSEXT_hash_sha1
865 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
866 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
869 *pmd = tls12_get_hash(sig[0]);
871 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
874 /* Make sure security callback allows algorithm */
875 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
876 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd), (void *)sig)) {
877 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
881 * Store the digest used so applications can retrieve it if they wish.
883 s->s3->tmp.peer_md = *pmd;
888 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
889 * supported, doesn't appear in supported signature algorithms, isn't supported
890 * by the enabled protocol versions or by the security level.
892 * This function should only be used for checking which ciphers are supported
895 * Call ssl_cipher_disabled() to check that it's enabled or not.
897 void ssl_set_client_disabled(SSL *s)
899 s->s3->tmp.mask_a = 0;
900 s->s3->tmp.mask_k = 0;
901 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
902 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
903 #ifndef OPENSSL_NO_PSK
904 /* with PSK there must be client callback set */
905 if (!s->psk_client_callback) {
906 s->s3->tmp.mask_a |= SSL_aPSK;
907 s->s3->tmp.mask_k |= SSL_PSK;
909 #endif /* OPENSSL_NO_PSK */
910 #ifndef OPENSSL_NO_SRP
911 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
912 s->s3->tmp.mask_a |= SSL_aSRP;
913 s->s3->tmp.mask_k |= SSL_kSRP;
919 * ssl_cipher_disabled - check that a cipher is disabled or not
920 * @s: SSL connection that you want to use the cipher on
921 * @c: cipher to check
922 * @op: Security check that you want to do
924 * Returns 1 when it's disabled, 0 when enabled.
926 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
928 if (c->algorithm_mkey & s->s3->tmp.mask_k
929 || c->algorithm_auth & s->s3->tmp.mask_a)
931 if (s->s3->tmp.max_ver == 0)
933 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
934 || (c->max_tls < s->s3->tmp.min_ver)))
936 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
937 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
940 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
943 static int tls_use_ticket(SSL *s)
945 if (s->options & SSL_OP_NO_TICKET)
947 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
950 static int compare_uint(const void *p1, const void *p2)
952 unsigned int u1 = *((const unsigned int *)p1);
953 unsigned int u2 = *((const unsigned int *)p2);
963 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
964 * more than one extension of the same type in a ClientHello or ServerHello.
965 * This function does an initial scan over the extensions block to filter those
966 * out. It returns 1 if all extensions are unique, and 0 if the extensions
967 * contain duplicates, could not be successfully parsed, or an internal error
970 static int tls1_check_duplicate_extensions(const PACKET *packet)
972 PACKET extensions = *packet;
973 size_t num_extensions = 0, i = 0;
974 unsigned int *extension_types = NULL;
977 /* First pass: count the extensions. */
978 while (PACKET_remaining(&extensions) > 0) {
981 if (!PACKET_get_net_2(&extensions, &type) ||
982 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
988 if (num_extensions <= 1)
991 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
992 if (extension_types == NULL) {
993 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
997 /* Second pass: gather the extension types. */
998 extensions = *packet;
999 for (i = 0; i < num_extensions; i++) {
1001 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
1002 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1003 /* This should not happen. */
1004 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1009 if (PACKET_remaining(&extensions) != 0) {
1010 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1013 /* Sort the extensions and make sure there are no duplicates. */
1014 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1015 for (i = 1; i < num_extensions; i++) {
1016 if (extension_types[i - 1] == extension_types[i])
1021 OPENSSL_free(extension_types);
1025 int ssl_add_clienthello_tlsext(SSL *s, WPACKET *pkt, int *al)
1027 #ifndef OPENSSL_NO_EC
1028 /* See if we support any ECC ciphersuites */
1030 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1032 unsigned long alg_k, alg_a;
1033 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1035 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1036 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1038 alg_k = c->algorithm_mkey;
1039 alg_a = c->algorithm_auth;
1040 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1041 || (alg_a & SSL_aECDSA)) {
1049 /* Add RI if renegotiating */
1050 if (s->renegotiate) {
1051 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1052 || !WPACKET_start_sub_packet_u16(pkt)
1053 || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
1054 s->s3->previous_client_finished_len)
1055 || !WPACKET_close(pkt)) {
1056 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1060 /* Only add RI for SSLv3 */
1061 if (s->client_version == SSL3_VERSION)
1064 if (s->tlsext_hostname != NULL) {
1065 /* Add TLS extension servername to the Client Hello message */
1066 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1067 /* Sub-packet for server_name extension */
1068 || !WPACKET_start_sub_packet_u16(pkt)
1069 /* Sub-packet for servername list (always 1 hostname)*/
1070 || !WPACKET_start_sub_packet_u16(pkt)
1071 || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
1072 || !WPACKET_sub_memcpy_u16(pkt, s->tlsext_hostname,
1073 strlen(s->tlsext_hostname))
1074 || !WPACKET_close(pkt)
1075 || !WPACKET_close(pkt)) {
1076 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1080 #ifndef OPENSSL_NO_SRP
1081 /* Add SRP username if there is one */
1082 if (s->srp_ctx.login != NULL) {
1083 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
1084 /* Sub-packet for SRP extension */
1085 || !WPACKET_start_sub_packet_u16(pkt)
1086 || !WPACKET_start_sub_packet_u8(pkt)
1087 /* login must not be zero...internal error if so */
1088 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
1089 || !WPACKET_memcpy(pkt, s->srp_ctx.login,
1090 strlen(s->srp_ctx.login))
1091 || !WPACKET_close(pkt)
1092 || !WPACKET_close(pkt)) {
1093 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1099 #ifndef OPENSSL_NO_EC
1102 * Add TLS extension ECPointFormats to the ClientHello message
1104 const unsigned char *pcurves, *pformats;
1105 size_t num_curves, num_formats;
1108 tls1_get_formatlist(s, &pformats, &num_formats);
1110 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1111 /* Sub-packet for formats extension */
1112 || !WPACKET_start_sub_packet_u16(pkt)
1113 || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
1114 || !WPACKET_close(pkt)) {
1115 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1120 * Add TLS extension EllipticCurves to the ClientHello message
1122 pcurves = s->tlsext_ellipticcurvelist;
1123 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {
1124 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1128 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_elliptic_curves)
1129 /* Sub-packet for curves extension */
1130 || !WPACKET_start_sub_packet_u16(pkt)
1131 || !WPACKET_start_sub_packet_u16(pkt)) {
1132 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1135 /* Copy curve ID if supported */
1136 for (i = 0; i < num_curves; i++, pcurves += 2) {
1137 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1138 if (!WPACKET_put_bytes_u8(pkt, pcurves[0])
1139 || !WPACKET_put_bytes_u8(pkt, pcurves[1])) {
1140 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,
1141 ERR_R_INTERNAL_ERROR);
1146 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1147 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1151 #endif /* OPENSSL_NO_EC */
1153 if (tls_use_ticket(s)) {
1155 if (!s->new_session && s->session && s->session->tlsext_tick)
1156 ticklen = s->session->tlsext_ticklen;
1157 else if (s->session && s->tlsext_session_ticket &&
1158 s->tlsext_session_ticket->data) {
1159 ticklen = s->tlsext_session_ticket->length;
1160 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1161 if (s->session->tlsext_tick == NULL) {
1162 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1165 memcpy(s->session->tlsext_tick,
1166 s->tlsext_session_ticket->data, ticklen);
1167 s->session->tlsext_ticklen = ticklen;
1170 if (ticklen == 0 && s->tlsext_session_ticket &&
1171 s->tlsext_session_ticket->data == NULL)
1174 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1175 || !WPACKET_sub_memcpy_u16(pkt, s->session->tlsext_tick,
1177 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1183 if (SSL_CLIENT_USE_SIGALGS(s)) {
1185 const unsigned char *salg;
1187 salglen = tls12_get_psigalgs(s, &salg);
1189 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
1190 /* Sub-packet for sig-algs extension */
1191 || !WPACKET_start_sub_packet_u16(pkt)
1192 /* Sub-packet for the actual list */
1193 || !WPACKET_start_sub_packet_u16(pkt)
1194 || !tls12_copy_sigalgs(s, pkt, salg, salglen)
1195 || !WPACKET_close(pkt)
1196 || !WPACKET_close(pkt)) {
1197 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1201 #ifndef OPENSSL_NO_OCSP
1202 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1205 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1206 /* Sub-packet for status request extension */
1207 || !WPACKET_start_sub_packet_u16(pkt)
1208 || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
1209 /* Sub-packet for the ids */
1210 || !WPACKET_start_sub_packet_u16(pkt)) {
1211 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1214 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1215 unsigned char *idbytes;
1219 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1220 idlen = i2d_OCSP_RESPID(id, NULL);
1222 /* Sub-packet for an individual id */
1223 || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
1224 || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
1225 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1229 if (!WPACKET_close(pkt)
1230 || !WPACKET_start_sub_packet_u16(pkt)) {
1231 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1234 if (s->tlsext_ocsp_exts) {
1235 unsigned char *extbytes;
1236 int extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1239 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1242 if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
1243 || i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &extbytes)
1245 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1249 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1250 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1255 #ifndef OPENSSL_NO_HEARTBEATS
1256 if (SSL_IS_DTLS(s)) {
1261 * 1: peer may send requests
1262 * 2: peer not allowed to send requests
1264 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1265 mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1267 mode = SSL_DTLSEXT_HB_ENABLED;
1269 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)
1270 /* Sub-packet for Hearbeat extension */
1271 || !WPACKET_start_sub_packet_u16(pkt)
1272 || !WPACKET_put_bytes_u8(pkt, mode)
1273 || !WPACKET_close(pkt)) {
1274 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1280 #ifndef OPENSSL_NO_NEXTPROTONEG
1281 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1283 * The client advertises an empty extension to indicate its support
1284 * for Next Protocol Negotiation
1286 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1287 || !WPACKET_put_bytes_u16(pkt, 0)) {
1288 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1295 * finish_md_len is non-zero during a renegotiation, so
1296 * this avoids sending ALPN during the renegotiation
1297 * (see longer comment below)
1299 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1300 if (!WPACKET_put_bytes_u16(pkt,
1301 TLSEXT_TYPE_application_layer_protocol_negotiation)
1302 /* Sub-packet ALPN extension */
1303 || !WPACKET_start_sub_packet_u16(pkt)
1304 || !WPACKET_sub_memcpy_u16(pkt, s->alpn_client_proto_list,
1305 s->alpn_client_proto_list_len)
1306 || !WPACKET_close(pkt)) {
1307 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1310 s->s3->alpn_sent = 1;
1312 #ifndef OPENSSL_NO_SRTP
1313 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1314 STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
1315 SRTP_PROTECTION_PROFILE *prof;
1318 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1319 /* Sub-packet for SRTP extension */
1320 || !WPACKET_start_sub_packet_u16(pkt)
1321 /* Sub-packet for the protection profile list */
1322 || !WPACKET_start_sub_packet_u16(pkt)) {
1323 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1326 ct = sk_SRTP_PROTECTION_PROFILE_num(clnt);
1327 for (i = 0; i < ct; i++) {
1328 prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1329 if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
1330 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1334 if (!WPACKET_close(pkt)
1335 /* Add an empty use_mki value */
1336 || !WPACKET_put_bytes_u8(pkt, 0)
1337 || !WPACKET_close(pkt)) {
1338 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1343 custom_ext_init(&s->cert->cli_ext);
1344 /* Add custom TLS Extensions to ClientHello */
1345 if (!custom_ext_add(s, 0, pkt, al)) {
1346 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1350 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) {
1351 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1352 || !WPACKET_put_bytes_u16(pkt, 0)) {
1353 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1358 #ifndef OPENSSL_NO_CT
1359 if (s->ct_validation_callback != NULL) {
1360 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
1361 || !WPACKET_put_bytes_u16(pkt, 0)) {
1362 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1368 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1369 || !WPACKET_put_bytes_u16(pkt, 0)) {
1370 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1374 if (!SSL_IS_DTLS(s) && s->version >= TLS1_3_VERSION) {
1375 int min_version, max_version, reason, currv;
1376 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1377 || !WPACKET_start_sub_packet_u16(pkt)
1378 || !WPACKET_start_sub_packet_u8(pkt)) {
1379 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1382 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
1384 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, reason);
1387 for (currv = max_version; currv >= min_version; currv--) {
1388 /* TODO(TLS1.3): Remove this first if clause prior to release!! */
1389 if (currv == TLS1_3_VERSION) {
1390 if (!WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION_DRAFT)) {
1391 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT,
1392 ERR_R_INTERNAL_ERROR);
1395 } else if (!WPACKET_put_bytes_u16(pkt, currv)) {
1396 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1400 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1401 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1407 * Add padding to workaround bugs in F5 terminators. See
1408 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1409 * code works out the length of all existing extensions it MUST always
1412 if (s->options & SSL_OP_TLSEXT_PADDING) {
1413 unsigned char *padbytes;
1416 if (!WPACKET_get_total_written(pkt, &hlen)) {
1417 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1421 if (hlen > 0xff && hlen < 0x200) {
1422 hlen = 0x200 - hlen;
1428 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
1429 || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
1430 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1433 memset(padbytes, 0, hlen);
1441 int ssl_add_serverhello_tlsext(SSL *s, WPACKET *pkt, int *al)
1443 #ifndef OPENSSL_NO_NEXTPROTONEG
1444 int next_proto_neg_seen;
1446 #ifndef OPENSSL_NO_EC
1447 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1448 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1449 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1450 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1453 if (!WPACKET_start_sub_packet_u16(pkt)
1454 || !WPACKET_set_flags(pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)) {
1455 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1459 if (s->s3->send_connection_binding &&
1460 !ssl_add_serverhello_renegotiate_ext(s, pkt)) {
1461 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1465 /* Only add RI for SSLv3 */
1466 if (s->version == SSL3_VERSION)
1469 if (!s->hit && s->servername_done == 1
1470 && s->session->tlsext_hostname != NULL) {
1471 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1472 || !WPACKET_put_bytes_u16(pkt, 0)) {
1473 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1477 #ifndef OPENSSL_NO_EC
1479 const unsigned char *plist;
1482 * Add TLS extension ECPointFormats to the ServerHello message
1484 tls1_get_formatlist(s, &plist, &plistlen);
1486 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1487 || !WPACKET_start_sub_packet_u16(pkt)
1488 || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1489 || !WPACKET_close(pkt)) {
1490 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1495 * Currently the server should not respond with a SupportedCurves
1498 #endif /* OPENSSL_NO_EC */
1500 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1501 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1502 || !WPACKET_put_bytes_u16(pkt, 0)) {
1503 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1508 * if we don't add the above TLSEXT, we can't add a session ticket
1511 s->tlsext_ticket_expected = 0;
1514 if (s->tlsext_status_expected) {
1515 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1516 || !WPACKET_put_bytes_u16(pkt, 0)) {
1517 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1521 #ifndef OPENSSL_NO_SRTP
1522 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1523 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1524 || !WPACKET_start_sub_packet_u16(pkt)
1525 || !WPACKET_put_bytes_u16(pkt, 2)
1526 || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1527 || !WPACKET_put_bytes_u8(pkt, 0)
1528 || !WPACKET_close(pkt)) {
1529 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1535 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1536 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1537 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1538 const unsigned char cryptopro_ext[36] = {
1539 0xfd, 0xe8, /* 65000 */
1540 0x00, 0x20, /* 32 bytes length */
1541 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1542 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1543 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1544 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1546 if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
1547 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1551 #ifndef OPENSSL_NO_HEARTBEATS
1552 /* Add Heartbeat extension if we've received one */
1553 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1557 * 1: peer may send requests
1558 * 2: peer not allowed to send requests
1560 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1561 mode = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1563 mode = SSL_DTLSEXT_HB_ENABLED;
1565 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_heartbeat)
1566 || !WPACKET_start_sub_packet_u16(pkt)
1567 || !WPACKET_put_bytes_u8(pkt, mode)
1568 || !WPACKET_close(pkt)) {
1569 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1576 #ifndef OPENSSL_NO_NEXTPROTONEG
1577 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1578 s->s3->next_proto_neg_seen = 0;
1579 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1580 const unsigned char *npa;
1581 unsigned int npalen;
1584 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1586 ctx->next_protos_advertised_cb_arg);
1587 if (r == SSL_TLSEXT_ERR_OK) {
1588 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1589 || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1590 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1593 s->s3->next_proto_neg_seen = 1;
1597 if (!custom_ext_add(s, 1, pkt, al)) {
1598 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1602 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1604 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1605 * for other cases too.
1607 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1608 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1609 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1610 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1611 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1613 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1614 || !WPACKET_put_bytes_u16(pkt, 0)) {
1615 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1620 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1621 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1622 || !WPACKET_put_bytes_u16(pkt, 0)) {
1623 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1628 if (s->s3->alpn_selected != NULL) {
1629 if (!WPACKET_put_bytes_u16(pkt,
1630 TLSEXT_TYPE_application_layer_protocol_negotiation)
1631 || !WPACKET_start_sub_packet_u16(pkt)
1632 || !WPACKET_start_sub_packet_u16(pkt)
1633 || !WPACKET_sub_memcpy_u8(pkt, s->s3->alpn_selected,
1634 s->s3->alpn_selected_len)
1635 || !WPACKET_close(pkt)
1636 || !WPACKET_close(pkt)) {
1637 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1643 if (!WPACKET_close(pkt)) {
1644 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1651 * Save the ALPN extension in a ClientHello.
1652 * pkt: the contents of the ALPN extension, not including type and length.
1653 * al: a pointer to the alert value to send in the event of a failure.
1654 * returns: 1 on success, 0 on error.
1656 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1658 PACKET protocol_list, save_protocol_list, protocol;
1660 *al = SSL_AD_DECODE_ERROR;
1662 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1663 || PACKET_remaining(&protocol_list) < 2) {
1667 save_protocol_list = protocol_list;
1669 /* Protocol names can't be empty. */
1670 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1671 || PACKET_remaining(&protocol) == 0) {
1674 } while (PACKET_remaining(&protocol_list) != 0);
1676 if (!PACKET_memdup(&save_protocol_list,
1677 &s->s3->alpn_proposed, &s->s3->alpn_proposed_len)) {
1678 *al = TLS1_AD_INTERNAL_ERROR;
1686 * Process the ALPN extension in a ClientHello.
1687 * al: a pointer to the alert value to send in the event of a failure.
1688 * returns 1 on success, 0 on error.
1690 static int tls1_alpn_handle_client_hello_late(SSL *s, int *al)
1692 const unsigned char *selected = NULL;
1693 unsigned char selected_len = 0;
1695 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1696 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1697 s->s3->alpn_proposed,
1698 (unsigned int)s->s3->alpn_proposed_len,
1699 s->ctx->alpn_select_cb_arg);
1701 if (r == SSL_TLSEXT_ERR_OK) {
1702 OPENSSL_free(s->s3->alpn_selected);
1703 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1704 if (s->s3->alpn_selected == NULL) {
1705 *al = SSL_AD_INTERNAL_ERROR;
1708 s->s3->alpn_selected_len = selected_len;
1709 #ifndef OPENSSL_NO_NEXTPROTONEG
1710 /* ALPN takes precedence over NPN. */
1711 s->s3->next_proto_neg_seen = 0;
1714 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1722 #ifndef OPENSSL_NO_EC
1724 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1725 * SecureTransport using the TLS extension block in |hello|.
1726 * Safari, since 10.6, sends exactly these extensions, in this order:
1731 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1732 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1733 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1734 * 10.8..10.8.3 (which don't work).
1736 static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1742 static const unsigned char kSafariExtensionsBlock[] = {
1743 0x00, 0x0a, /* elliptic_curves extension */
1744 0x00, 0x08, /* 8 bytes */
1745 0x00, 0x06, /* 6 bytes of curve ids */
1746 0x00, 0x17, /* P-256 */
1747 0x00, 0x18, /* P-384 */
1748 0x00, 0x19, /* P-521 */
1750 0x00, 0x0b, /* ec_point_formats */
1751 0x00, 0x02, /* 2 bytes */
1752 0x01, /* 1 point format */
1753 0x00, /* uncompressed */
1754 /* The following is only present in TLS 1.2 */
1755 0x00, 0x0d, /* signature_algorithms */
1756 0x00, 0x0c, /* 12 bytes */
1757 0x00, 0x0a, /* 10 bytes */
1758 0x05, 0x01, /* SHA-384/RSA */
1759 0x04, 0x01, /* SHA-256/RSA */
1760 0x02, 0x01, /* SHA-1/RSA */
1761 0x04, 0x03, /* SHA-256/ECDSA */
1762 0x02, 0x03, /* SHA-1/ECDSA */
1765 /* Length of the common prefix (first two extensions). */
1766 static const size_t kSafariCommonExtensionsLength = 18;
1768 tmppkt = hello->extensions;
1770 if (!PACKET_forward(&tmppkt, 2)
1771 || !PACKET_get_net_2(&tmppkt, &type)
1772 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1776 if (type != TLSEXT_TYPE_server_name)
1779 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1780 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1782 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1785 #endif /* !OPENSSL_NO_EC */
1788 * Loop through all remaining ClientHello extensions that we collected earlier
1789 * and haven't already processed. For each one parse it and update the SSL
1790 * object as required.
1792 * Behaviour upon resumption is extension-specific. If the extension has no
1793 * effect during resumption, it is parsed (to verify its format) but otherwise
1796 * Returns 1 on success and 0 on failure.
1797 * Upon failure, sets |al| to the appropriate alert.
1799 static int ssl_scan_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello, int *al)
1802 int renegotiate_seen = 0;
1804 *al = SSL_AD_DECODE_ERROR;
1805 s->servername_done = 0;
1806 s->tlsext_status_type = -1;
1807 #ifndef OPENSSL_NO_NEXTPROTONEG
1808 s->s3->next_proto_neg_seen = 0;
1811 OPENSSL_free(s->s3->alpn_selected);
1812 s->s3->alpn_selected = NULL;
1813 s->s3->alpn_selected_len = 0;
1814 OPENSSL_free(s->s3->alpn_proposed);
1815 s->s3->alpn_proposed = NULL;
1816 s->s3->alpn_proposed_len = 0;
1817 #ifndef OPENSSL_NO_HEARTBEATS
1818 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1819 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1822 #ifndef OPENSSL_NO_EC
1823 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1824 ssl_check_for_safari(s, hello);
1825 #endif /* !OPENSSL_NO_EC */
1827 /* Clear any signature algorithms extension received */
1828 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1829 s->s3->tmp.peer_sigalgs = NULL;
1830 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1832 #ifndef OPENSSL_NO_SRP
1833 OPENSSL_free(s->srp_ctx.login);
1834 s->srp_ctx.login = NULL;
1837 s->srtp_profile = NULL;
1840 * We parse all extensions to ensure the ClientHello is well-formed but,
1841 * unless an extension specifies otherwise, we ignore extensions upon
1844 for (loop = 0; loop < hello->num_extensions; loop++) {
1845 RAW_EXTENSION *currext = &hello->pre_proc_exts[loop];
1847 if (s->tlsext_debug_cb)
1848 s->tlsext_debug_cb(s, 0, currext->type,
1849 PACKET_data(&currext->data),
1850 PACKET_remaining(&currext->data),
1851 s->tlsext_debug_arg);
1853 if (currext->type == TLSEXT_TYPE_renegotiate) {
1854 if (!ssl_parse_clienthello_renegotiate_ext(s,
1855 &currext->data, al))
1857 renegotiate_seen = 1;
1858 } else if (s->version == SSL3_VERSION) {
1861 * The servername extension is treated as follows:
1863 * - Only the hostname type is supported with a maximum length of 255.
1864 * - The servername is rejected if too long or if it contains zeros,
1865 * in which case an fatal alert is generated.
1866 * - The servername field is maintained together with the session cache.
1867 * - When a session is resumed, the servername call back invoked in order
1868 * to allow the application to position itself to the right context.
1869 * - The servername is acknowledged if it is new for a session or when
1870 * it is identical to a previously used for the same session.
1871 * Applications can control the behaviour. They can at any time
1872 * set a 'desirable' servername for a new SSL object. This can be the
1873 * case for example with HTTPS when a Host: header field is received and
1874 * a renegotiation is requested. In this case, a possible servername
1875 * presented in the new client hello is only acknowledged if it matches
1876 * the value of the Host: field.
1877 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1878 * if they provide for changing an explicit servername context for the
1879 * session, i.e. when the session has been established with a servername
1881 * - On session reconnect, the servername extension may be absent.
1885 else if (currext->type == TLSEXT_TYPE_server_name) {
1886 unsigned int servname_type;
1887 PACKET sni, hostname;
1889 if (!PACKET_as_length_prefixed_2(&currext->data, &sni)
1890 /* ServerNameList must be at least 1 byte long. */
1891 || PACKET_remaining(&sni) == 0) {
1896 * Although the server_name extension was intended to be
1897 * extensible to new name types, RFC 4366 defined the
1898 * syntax inextensibility and OpenSSL 1.0.x parses it as
1900 * RFC 6066 corrected the mistake but adding new name types
1901 * is nevertheless no longer feasible, so act as if no other
1902 * SNI types can exist, to simplify parsing.
1904 * Also note that the RFC permits only one SNI value per type,
1905 * i.e., we can only have a single hostname.
1907 if (!PACKET_get_1(&sni, &servname_type)
1908 || servname_type != TLSEXT_NAMETYPE_host_name
1909 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1914 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1915 *al = TLS1_AD_UNRECOGNIZED_NAME;
1919 if (PACKET_contains_zero_byte(&hostname)) {
1920 *al = TLS1_AD_UNRECOGNIZED_NAME;
1924 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1925 *al = TLS1_AD_INTERNAL_ERROR;
1929 s->servername_done = 1;
1932 * TODO(openssl-team): if the SNI doesn't match, we MUST
1933 * fall back to a full handshake.
1935 s->servername_done = s->session->tlsext_hostname
1936 && PACKET_equal(&hostname, s->session->tlsext_hostname,
1937 strlen(s->session->tlsext_hostname));
1940 #ifndef OPENSSL_NO_SRP
1941 else if (currext->type == TLSEXT_TYPE_srp) {
1944 if (!PACKET_as_length_prefixed_1(&currext->data, &srp_I))
1947 if (PACKET_contains_zero_byte(&srp_I))
1951 * TODO(openssl-team): currently, we re-authenticate the user
1952 * upon resumption. Instead, we MUST ignore the login.
1954 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
1955 *al = TLS1_AD_INTERNAL_ERROR;
1961 #ifndef OPENSSL_NO_EC
1962 else if (currext->type == TLSEXT_TYPE_ec_point_formats) {
1963 PACKET ec_point_format_list;
1965 if (!PACKET_as_length_prefixed_1(&currext->data,
1966 &ec_point_format_list)
1967 || PACKET_remaining(&ec_point_format_list) == 0) {
1972 if (!PACKET_memdup(&ec_point_format_list,
1973 &s->session->tlsext_ecpointformatlist,
1975 session->tlsext_ecpointformatlist_length)) {
1976 *al = TLS1_AD_INTERNAL_ERROR;
1980 } else if (currext->type == TLSEXT_TYPE_elliptic_curves) {
1981 PACKET elliptic_curve_list;
1983 /* Each NamedCurve is 2 bytes and we must have at least 1. */
1984 if (!PACKET_as_length_prefixed_2(&currext->data,
1985 &elliptic_curve_list)
1986 || PACKET_remaining(&elliptic_curve_list) == 0
1987 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
1992 if (!PACKET_memdup(&elliptic_curve_list,
1993 &s->session->tlsext_ellipticcurvelist,
1995 session->tlsext_ellipticcurvelist_length)) {
1996 *al = TLS1_AD_INTERNAL_ERROR;
2001 #endif /* OPENSSL_NO_EC */
2002 else if (currext->type == TLSEXT_TYPE_session_ticket) {
2003 if (s->tls_session_ticket_ext_cb &&
2004 !s->tls_session_ticket_ext_cb(s,
2005 PACKET_data(&currext->data),
2006 PACKET_remaining(&currext->data),
2007 s->tls_session_ticket_ext_cb_arg)) {
2008 *al = TLS1_AD_INTERNAL_ERROR;
2011 } else if (currext->type == TLSEXT_TYPE_signature_algorithms) {
2012 PACKET supported_sig_algs;
2014 if (!PACKET_as_length_prefixed_2(&currext->data,
2015 &supported_sig_algs)
2016 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2017 || PACKET_remaining(&supported_sig_algs) == 0) {
2022 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2023 PACKET_remaining(&supported_sig_algs))) {
2027 } else if (currext->type == TLSEXT_TYPE_status_request) {
2028 if (!PACKET_get_1(&currext->data,
2029 (unsigned int *)&s->tlsext_status_type)) {
2032 #ifndef OPENSSL_NO_OCSP
2033 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2034 const unsigned char *ext_data;
2035 PACKET responder_id_list, exts;
2036 if (!PACKET_get_length_prefixed_2
2037 (&currext->data, &responder_id_list))
2041 * We remove any OCSP_RESPIDs from a previous handshake
2042 * to prevent unbounded memory growth - CVE-2016-6304
2044 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2046 if (PACKET_remaining(&responder_id_list) > 0) {
2047 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2048 if (s->tlsext_ocsp_ids == NULL) {
2049 *al = SSL_AD_INTERNAL_ERROR;
2053 s->tlsext_ocsp_ids = NULL;
2056 while (PACKET_remaining(&responder_id_list) > 0) {
2058 PACKET responder_id;
2059 const unsigned char *id_data;
2061 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2063 || PACKET_remaining(&responder_id) == 0) {
2067 id_data = PACKET_data(&responder_id);
2068 /* TODO(size_t): Convert d2i_* to size_t */
2069 id = d2i_OCSP_RESPID(NULL, &id_data,
2070 (int)PACKET_remaining(&responder_id));
2074 if (id_data != PACKET_end(&responder_id)) {
2075 OCSP_RESPID_free(id);
2079 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2080 OCSP_RESPID_free(id);
2081 *al = SSL_AD_INTERNAL_ERROR;
2086 /* Read in request_extensions */
2087 if (!PACKET_as_length_prefixed_2(
2088 &currext->data, &exts))
2091 if (PACKET_remaining(&exts) > 0) {
2092 ext_data = PACKET_data(&exts);
2093 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2094 X509_EXTENSION_free);
2095 s->tlsext_ocsp_exts =
2096 d2i_X509_EXTENSIONS(NULL, &ext_data,
2097 (int)PACKET_remaining(&exts));
2098 if (s->tlsext_ocsp_exts == NULL
2099 || ext_data != PACKET_end(&exts)) {
2107 * We don't know what to do with any other type so ignore it.
2109 s->tlsext_status_type = -1;
2112 #ifndef OPENSSL_NO_HEARTBEATS
2113 else if (SSL_IS_DTLS(s) && currext->type == TLSEXT_TYPE_heartbeat) {
2114 unsigned int hbtype;
2116 if (!PACKET_get_1(&currext->data, &hbtype)
2117 || PACKET_remaining(&currext->data)) {
2118 *al = SSL_AD_DECODE_ERROR;
2122 case 0x01: /* Client allows us to send HB requests */
2123 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2125 case 0x02: /* Client doesn't accept HB requests */
2126 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2127 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2130 *al = SSL_AD_ILLEGAL_PARAMETER;
2135 #ifndef OPENSSL_NO_NEXTPROTONEG
2136 else if (currext->type == TLSEXT_TYPE_next_proto_neg
2137 && s->s3->tmp.finish_md_len == 0) {
2139 * We shouldn't accept this extension on a
2142 * s->new_session will be set on renegotiation, but we
2143 * probably shouldn't rely that it couldn't be set on
2144 * the initial renegotiation too in certain cases (when
2145 * there's some other reason to disallow resuming an
2146 * earlier session -- the current code won't be doing
2147 * anything like that, but this might change).
2149 * A valid sign that there's been a previous handshake
2150 * in this connection is if s->s3->tmp.finish_md_len >
2151 * 0. (We are talking about a check that will happen
2152 * in the Hello protocol round, well before a new
2153 * Finished message could have been computed.)
2155 s->s3->next_proto_neg_seen = 1;
2159 else if (currext->type
2160 == TLSEXT_TYPE_application_layer_protocol_negotiation
2161 && s->s3->tmp.finish_md_len == 0) {
2162 if (!tls1_alpn_handle_client_hello(s,
2163 &currext->data, al))
2167 /* session ticket processed earlier */
2168 #ifndef OPENSSL_NO_SRTP
2169 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2170 && currext->type == TLSEXT_TYPE_use_srtp) {
2171 if (ssl_parse_clienthello_use_srtp_ext(s,
2172 &currext->data, al))
2176 else if (currext->type == TLSEXT_TYPE_encrypt_then_mac
2177 && !(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
2178 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2180 * Note: extended master secret extension handled in
2181 * tls_check_client_ems_support()
2185 * If this ClientHello extension was unhandled and this is a
2186 * nonresumed connection, check whether the extension is a custom
2187 * TLS Extension (has a custom_srv_ext_record), and if so call the
2188 * callback and record the extension number so that an appropriate
2189 * ServerHello may be later returned.
2192 if (custom_ext_parse(s, 1, currext->type,
2193 PACKET_data(&currext->data),
2194 PACKET_remaining(&currext->data), al) <= 0)
2199 /* Need RI if renegotiating */
2201 if (!renegotiate_seen && s->renegotiate &&
2202 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2203 *al = SSL_AD_HANDSHAKE_FAILURE;
2204 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2205 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2210 * This function currently has no state to clean up, so it returns directly.
2211 * If parsing fails at any point, the function returns early.
2212 * The SSL object may be left with partial data from extensions, but it must
2213 * then no longer be used, and clearing it up will free the leftovers.
2218 int ssl_parse_clienthello_tlsext(SSL *s, CLIENTHELLO_MSG *hello)
2221 custom_ext_init(&s->cert->srv_ext);
2222 if (ssl_scan_clienthello_tlsext(s, hello, &al) <= 0) {
2223 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2226 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2227 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2233 #ifndef OPENSSL_NO_NEXTPROTONEG
2235 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2236 * elements of zero length are allowed and the set of elements must exactly
2237 * fill the length of the block.
2239 static char ssl_next_proto_validate(PACKET *pkt)
2241 PACKET tmp_protocol;
2243 while (PACKET_remaining(pkt)) {
2244 if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2245 || PACKET_remaining(&tmp_protocol) == 0)
2253 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2255 unsigned int length, type, size;
2256 int tlsext_servername = 0;
2257 int renegotiate_seen = 0;
2259 #ifndef OPENSSL_NO_NEXTPROTONEG
2260 s->s3->next_proto_neg_seen = 0;
2262 s->tlsext_ticket_expected = 0;
2264 OPENSSL_free(s->s3->alpn_selected);
2265 s->s3->alpn_selected = NULL;
2266 #ifndef OPENSSL_NO_HEARTBEATS
2267 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2268 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2271 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2273 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2275 if (!PACKET_get_net_2(pkt, &length))
2278 if (PACKET_remaining(pkt) != length) {
2279 *al = SSL_AD_DECODE_ERROR;
2283 if (!tls1_check_duplicate_extensions(pkt)) {
2284 *al = SSL_AD_DECODE_ERROR;
2288 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2289 const unsigned char *data;
2292 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2293 || !PACKET_peek_bytes(&spkt, &data, size))
2296 if (s->tlsext_debug_cb)
2297 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2299 if (type == TLSEXT_TYPE_renegotiate) {
2300 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2302 renegotiate_seen = 1;
2303 } else if (s->version == SSL3_VERSION) {
2304 } else if (type == TLSEXT_TYPE_server_name) {
2305 if (s->tlsext_hostname == NULL || size > 0) {
2306 *al = TLS1_AD_UNRECOGNIZED_NAME;
2309 tlsext_servername = 1;
2311 #ifndef OPENSSL_NO_EC
2312 else if (type == TLSEXT_TYPE_ec_point_formats) {
2313 unsigned int ecpointformatlist_length;
2314 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2315 || ecpointformatlist_length != size - 1) {
2316 *al = TLS1_AD_DECODE_ERROR;
2320 s->session->tlsext_ecpointformatlist_length = 0;
2321 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2322 if ((s->session->tlsext_ecpointformatlist =
2323 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2324 *al = TLS1_AD_INTERNAL_ERROR;
2327 s->session->tlsext_ecpointformatlist_length =
2328 ecpointformatlist_length;
2329 if (!PACKET_copy_bytes(&spkt,
2330 s->session->tlsext_ecpointformatlist,
2331 ecpointformatlist_length)) {
2332 *al = TLS1_AD_DECODE_ERROR;
2338 #endif /* OPENSSL_NO_EC */
2340 else if (type == TLSEXT_TYPE_session_ticket) {
2341 if (s->tls_session_ticket_ext_cb &&
2342 !s->tls_session_ticket_ext_cb(s, data, size,
2343 s->tls_session_ticket_ext_cb_arg))
2345 *al = TLS1_AD_INTERNAL_ERROR;
2348 if (!tls_use_ticket(s) || (size > 0)) {
2349 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2352 s->tlsext_ticket_expected = 1;
2353 } else if (type == TLSEXT_TYPE_status_request) {
2355 * MUST be empty and only sent if we've requested a status
2358 if ((s->tlsext_status_type == -1) || (size > 0)) {
2359 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2362 /* Set flag to expect CertificateStatus message */
2363 s->tlsext_status_expected = 1;
2365 #ifndef OPENSSL_NO_CT
2367 * Only take it if we asked for it - i.e if there is no CT validation
2368 * callback set, then a custom extension MAY be processing it, so we
2369 * need to let control continue to flow to that.
2371 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2372 s->ct_validation_callback != NULL) {
2373 /* Simply copy it off for later processing */
2374 if (s->tlsext_scts != NULL) {
2375 OPENSSL_free(s->tlsext_scts);
2376 s->tlsext_scts = NULL;
2378 s->tlsext_scts_len = size;
2380 s->tlsext_scts = OPENSSL_malloc(size);
2381 if (s->tlsext_scts == NULL) {
2382 *al = TLS1_AD_INTERNAL_ERROR;
2385 memcpy(s->tlsext_scts, data, size);
2389 #ifndef OPENSSL_NO_NEXTPROTONEG
2390 else if (type == TLSEXT_TYPE_next_proto_neg &&
2391 s->s3->tmp.finish_md_len == 0) {
2392 unsigned char *selected;
2393 unsigned char selected_len;
2394 /* We must have requested it. */
2395 if (s->ctx->next_proto_select_cb == NULL) {
2396 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2399 /* The data must be valid */
2400 if (!ssl_next_proto_validate(&spkt)) {
2401 *al = TLS1_AD_DECODE_ERROR;
2404 if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2407 ctx->next_proto_select_cb_arg) !=
2408 SSL_TLSEXT_ERR_OK) {
2409 *al = TLS1_AD_INTERNAL_ERROR;
2413 * Could be non-NULL if server has sent multiple NPN extensions in
2414 * a single Serverhello
2416 OPENSSL_free(s->next_proto_negotiated);
2417 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2418 if (s->next_proto_negotiated == NULL) {
2419 *al = TLS1_AD_INTERNAL_ERROR;
2422 memcpy(s->next_proto_negotiated, selected, selected_len);
2423 s->next_proto_negotiated_len = selected_len;
2424 s->s3->next_proto_neg_seen = 1;
2428 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2430 /* We must have requested it. */
2431 if (!s->s3->alpn_sent) {
2432 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2436 * The extension data consists of:
2437 * uint16 list_length
2438 * uint8 proto_length;
2439 * uint8 proto[proto_length];
2441 if (!PACKET_get_net_2(&spkt, &len)
2442 || PACKET_remaining(&spkt) != len || !PACKET_get_1(&spkt, &len)
2443 || PACKET_remaining(&spkt) != len) {
2444 *al = TLS1_AD_DECODE_ERROR;
2447 OPENSSL_free(s->s3->alpn_selected);
2448 s->s3->alpn_selected = OPENSSL_malloc(len);
2449 if (s->s3->alpn_selected == NULL) {
2450 *al = TLS1_AD_INTERNAL_ERROR;
2453 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2454 *al = TLS1_AD_DECODE_ERROR;
2457 s->s3->alpn_selected_len = len;
2459 #ifndef OPENSSL_NO_HEARTBEATS
2460 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2461 unsigned int hbtype;
2462 if (!PACKET_get_1(&spkt, &hbtype)) {
2463 *al = SSL_AD_DECODE_ERROR;
2467 case 0x01: /* Server allows us to send HB requests */
2468 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2470 case 0x02: /* Server doesn't accept HB requests */
2471 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2472 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2475 *al = SSL_AD_ILLEGAL_PARAMETER;
2480 #ifndef OPENSSL_NO_SRTP
2481 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2482 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2486 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2487 /* Ignore if inappropriate ciphersuite */
2488 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) &&
2489 s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2490 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2491 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2492 } else if (type == TLSEXT_TYPE_extended_master_secret) {
2493 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2495 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2498 * If this extension type was not otherwise handled, but matches a
2499 * custom_cli_ext_record, then send it to the c callback
2501 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2505 if (PACKET_remaining(pkt) != 0) {
2506 *al = SSL_AD_DECODE_ERROR;
2510 if (!s->hit && tlsext_servername == 1) {
2511 if (s->tlsext_hostname) {
2512 if (s->session->tlsext_hostname == NULL) {
2513 s->session->tlsext_hostname =
2514 OPENSSL_strdup(s->tlsext_hostname);
2515 if (!s->session->tlsext_hostname) {
2516 *al = SSL_AD_UNRECOGNIZED_NAME;
2520 *al = SSL_AD_DECODE_ERROR;
2529 * Determine if we need to see RI. Strictly speaking if we want to avoid
2530 * an attack we should *always* see RI even on initial server hello
2531 * because the client doesn't see any renegotiation during an attack.
2532 * However this would mean we could not connect to any server which
2533 * doesn't support RI so for the immediate future tolerate RI absence
2535 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2536 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2537 *al = SSL_AD_HANDSHAKE_FAILURE;
2538 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2539 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2545 * Check extended master secret extension is consistent with
2548 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2549 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2550 *al = SSL_AD_HANDSHAKE_FAILURE;
2551 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2559 int ssl_prepare_clienthello_tlsext(SSL *s)
2561 s->s3->alpn_sent = 0;
2565 int ssl_prepare_serverhello_tlsext(SSL *s)
2570 static int ssl_check_clienthello_tlsext_early(SSL *s)
2572 int ret = SSL_TLSEXT_ERR_NOACK;
2573 int al = SSL_AD_UNRECOGNIZED_NAME;
2575 #ifndef OPENSSL_NO_EC
2577 * The handling of the ECPointFormats extension is done elsewhere, namely
2578 * in ssl3_choose_cipher in s3_lib.c.
2581 * The handling of the EllipticCurves extension is done elsewhere, namely
2582 * in ssl3_choose_cipher in s3_lib.c.
2586 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2588 s->ctx->tlsext_servername_callback(s, &al,
2589 s->ctx->tlsext_servername_arg);
2590 else if (s->initial_ctx != NULL
2591 && s->initial_ctx->tlsext_servername_callback != 0)
2593 s->initial_ctx->tlsext_servername_callback(s, &al,
2595 initial_ctx->tlsext_servername_arg);
2598 case SSL_TLSEXT_ERR_ALERT_FATAL:
2599 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2602 case SSL_TLSEXT_ERR_ALERT_WARNING:
2603 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2606 case SSL_TLSEXT_ERR_NOACK:
2607 s->servername_done = 0;
2613 /* Initialise digests to default values */
2614 void ssl_set_default_md(SSL *s)
2616 const EVP_MD **pmd = s->s3->tmp.md;
2617 #ifndef OPENSSL_NO_DSA
2618 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2620 #ifndef OPENSSL_NO_RSA
2621 if (SSL_USE_SIGALGS(s))
2622 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2624 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2625 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2627 #ifndef OPENSSL_NO_EC
2628 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2630 #ifndef OPENSSL_NO_GOST
2631 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2632 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2633 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2637 int tls1_set_server_sigalgs(SSL *s)
2642 /* Clear any shared signature algorithms */
2643 OPENSSL_free(s->cert->shared_sigalgs);
2644 s->cert->shared_sigalgs = NULL;
2645 s->cert->shared_sigalgslen = 0;
2646 /* Clear certificate digests and validity flags */
2647 for (i = 0; i < SSL_PKEY_NUM; i++) {
2648 s->s3->tmp.md[i] = NULL;
2649 s->s3->tmp.valid_flags[i] = 0;
2652 /* If sigalgs received process it. */
2653 if (s->s3->tmp.peer_sigalgs) {
2654 if (!tls1_process_sigalgs(s)) {
2655 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2656 al = SSL_AD_INTERNAL_ERROR;
2659 /* Fatal error is no shared signature algorithms */
2660 if (!s->cert->shared_sigalgs) {
2661 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2662 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
2663 al = SSL_AD_ILLEGAL_PARAMETER;
2667 ssl_set_default_md(s);
2671 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2676 * Upon success, returns 1.
2677 * Upon failure, returns 0 and sets |al| to the appropriate fatal alert.
2679 int ssl_check_clienthello_tlsext_late(SSL *s, int *al)
2681 s->tlsext_status_expected = 0;
2684 * If status request then ask callback what to do. Note: this must be
2685 * called after servername callbacks in case the certificate has changed,
2686 * and must be called after the cipher has been chosen because this may
2687 * influence which certificate is sent
2689 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2691 CERT_PKEY *certpkey;
2692 certpkey = ssl_get_server_send_pkey(s);
2693 /* If no certificate can't return certificate status */
2694 if (certpkey != NULL) {
2696 * Set current certificate to one we will use so SSL_get_certificate
2697 * et al can pick it up.
2699 s->cert->key = certpkey;
2700 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2702 /* We don't want to send a status request response */
2703 case SSL_TLSEXT_ERR_NOACK:
2704 s->tlsext_status_expected = 0;
2706 /* status request response should be sent */
2707 case SSL_TLSEXT_ERR_OK:
2708 if (s->tlsext_ocsp_resp)
2709 s->tlsext_status_expected = 1;
2711 /* something bad happened */
2712 case SSL_TLSEXT_ERR_ALERT_FATAL:
2714 *al = SSL_AD_INTERNAL_ERROR;
2720 if (!tls1_alpn_handle_client_hello_late(s, al)) {
2727 int ssl_check_serverhello_tlsext(SSL *s)
2729 int ret = SSL_TLSEXT_ERR_NOACK;
2730 int al = SSL_AD_UNRECOGNIZED_NAME;
2732 #ifndef OPENSSL_NO_EC
2734 * If we are client and using an elliptic curve cryptography cipher
2735 * suite, then if server returns an EC point formats lists extension it
2736 * must contain uncompressed.
2738 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2739 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2740 if ((s->tlsext_ecpointformatlist != NULL)
2741 && (s->tlsext_ecpointformatlist_length > 0)
2742 && (s->session->tlsext_ecpointformatlist != NULL)
2743 && (s->session->tlsext_ecpointformatlist_length > 0)
2744 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2745 /* we are using an ECC cipher */
2747 unsigned char *list;
2748 int found_uncompressed = 0;
2749 list = s->session->tlsext_ecpointformatlist;
2750 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2751 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2752 found_uncompressed = 1;
2756 if (!found_uncompressed) {
2757 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2758 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2762 ret = SSL_TLSEXT_ERR_OK;
2763 #endif /* OPENSSL_NO_EC */
2765 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2767 s->ctx->tlsext_servername_callback(s, &al,
2768 s->ctx->tlsext_servername_arg);
2769 else if (s->initial_ctx != NULL
2770 && s->initial_ctx->tlsext_servername_callback != 0)
2772 s->initial_ctx->tlsext_servername_callback(s, &al,
2774 initial_ctx->tlsext_servername_arg);
2777 * Ensure we get sensible values passed to tlsext_status_cb in the event
2778 * that we don't receive a status message
2780 OPENSSL_free(s->tlsext_ocsp_resp);
2781 s->tlsext_ocsp_resp = NULL;
2782 s->tlsext_ocsp_resplen = 0;
2785 case SSL_TLSEXT_ERR_ALERT_FATAL:
2786 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2789 case SSL_TLSEXT_ERR_ALERT_WARNING:
2790 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2793 case SSL_TLSEXT_ERR_NOACK:
2794 s->servername_done = 0;
2800 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2803 if (s->version < SSL3_VERSION)
2805 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2806 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2810 if (ssl_check_serverhello_tlsext(s) <= 0) {
2811 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2818 * Given a list of extensions that we collected earlier, find one of a given
2819 * type and return it.
2821 * |exts| is the set of extensions previously collected.
2822 * |numexts| is the number of extensions that we have.
2823 * |type| the type of the extension that we are looking for.
2825 * Returns a pointer to the found RAW_EXTENSION data, or NULL if not found.
2827 RAW_EXTENSION *tls_get_extension_by_type(RAW_EXTENSION *exts, size_t numexts,
2832 for (loop = 0; loop < numexts; loop++) {
2833 if (exts[loop].type == type)
2841 * Gets the ticket information supplied by the client if any.
2843 * hello: The parsed ClientHello data
2844 * ret: (output) on return, if a ticket was decrypted, then this is set to
2845 * point to the resulting session.
2847 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2848 * ciphersuite, in which case we have no use for session tickets and one will
2849 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2852 * -1: fatal error, either from parsing or decrypting the ticket.
2853 * 0: no ticket was found (or was ignored, based on settings).
2854 * 1: a zero length extension was found, indicating that the client supports
2855 * session tickets but doesn't currently have one to offer.
2856 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2857 * couldn't be decrypted because of a non-fatal error.
2858 * 3: a ticket was successfully decrypted and *ret was set.
2861 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2862 * a new session ticket to the client because the client indicated support
2863 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2864 * a session ticket or we couldn't use the one it gave us, or if
2865 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2866 * Otherwise, s->tlsext_ticket_expected is set to 0.
2868 int tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
2872 const unsigned char *etick;
2874 RAW_EXTENSION *ticketext;
2877 s->tlsext_ticket_expected = 0;
2880 * If tickets disabled behave as if no ticket present to permit stateful
2883 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
2886 ticketext = tls_get_extension_by_type(hello->pre_proc_exts,
2887 hello->num_extensions,
2888 TLSEXT_TYPE_session_ticket);
2889 if (ticketext == NULL)
2892 size = PACKET_remaining(&ticketext->data);
2895 * The client will accept a ticket but doesn't currently have
2898 s->tlsext_ticket_expected = 1;
2901 if (s->tls_session_secret_cb) {
2903 * Indicate that the ticket couldn't be decrypted rather than
2904 * generating the session from ticket now, trigger
2905 * abbreviated handshake based on external mechanism to
2906 * calculate the master secret later.
2910 if (!PACKET_get_bytes(&ticketext->data, &etick, size)) {
2911 /* Shouldn't ever happen */
2914 retv = tls_decrypt_ticket(s, etick, size, hello->session_id,
2915 hello->session_id_len, ret);
2917 case 2: /* ticket couldn't be decrypted */
2918 s->tlsext_ticket_expected = 1;
2921 case 3: /* ticket was decrypted */
2924 case 4: /* ticket decrypted but need to renew */
2925 s->tlsext_ticket_expected = 1;
2928 default: /* fatal error */
2934 * Sets the extended master secret flag if the extension is present in the
2940 int tls_check_client_ems_support(SSL *s, const CLIENTHELLO_MSG *hello)
2942 RAW_EXTENSION *emsext;
2944 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2946 if (s->version <= SSL3_VERSION)
2949 emsext = tls_get_extension_by_type(hello->pre_proc_exts,
2950 hello->num_extensions,
2951 TLSEXT_TYPE_extended_master_secret);
2954 * No extensions is a success - we have successfully discovered that the
2955 * client doesn't support EMS.
2960 /* The extensions must always be empty */
2961 if (PACKET_remaining(&emsext->data) != 0)
2964 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2970 * tls_decrypt_ticket attempts to decrypt a session ticket.
2972 * etick: points to the body of the session ticket extension.
2973 * eticklen: the length of the session tickets extension.
2974 * sess_id: points at the session ID.
2975 * sesslen: the length of the session ID.
2976 * psess: (output) on return, if a ticket was decrypted, then this is set to
2977 * point to the resulting session.
2980 * -2: fatal error, malloc failure.
2981 * -1: fatal error, either from parsing or decrypting the ticket.
2982 * 2: the ticket couldn't be decrypted.
2983 * 3: a ticket was successfully decrypted and *psess was set.
2984 * 4: same as 3, but the ticket needs to be renewed.
2986 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2987 size_t eticklen, const unsigned char *sess_id,
2988 size_t sesslen, SSL_SESSION **psess)
2991 unsigned char *sdec;
2992 const unsigned char *p;
2993 int slen, renew_ticket = 0, ret = -1, declen;
2995 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2996 HMAC_CTX *hctx = NULL;
2997 EVP_CIPHER_CTX *ctx;
2998 SSL_CTX *tctx = s->initial_ctx;
3000 /* Initialize session ticket encryption and HMAC contexts */
3001 hctx = HMAC_CTX_new();
3004 ctx = EVP_CIPHER_CTX_new();
3009 if (tctx->tlsext_ticket_key_cb) {
3010 unsigned char *nctick = (unsigned char *)etick;
3011 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3022 /* Check key name matches */
3023 if (memcmp(etick, tctx->tlsext_tick_key_name,
3024 sizeof(tctx->tlsext_tick_key_name)) != 0) {
3028 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
3029 sizeof(tctx->tlsext_tick_hmac_key),
3030 EVP_sha256(), NULL) <= 0
3031 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
3032 tctx->tlsext_tick_aes_key,
3033 etick + sizeof(tctx->tlsext_tick_key_name)) <=
3039 * Attempt to process session ticket, first conduct sanity and integrity
3042 mlen = HMAC_size(hctx);
3046 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3048 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
3053 /* Check HMAC of encrypted ticket */
3054 if (HMAC_Update(hctx, etick, eticklen) <= 0
3055 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3058 HMAC_CTX_free(hctx);
3059 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3060 EVP_CIPHER_CTX_free(ctx);
3063 /* Attempt to decrypt session data */
3064 /* Move p after IV to start of encrypted ticket, update length */
3065 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3066 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3067 sdec = OPENSSL_malloc(eticklen);
3068 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
3069 (int)eticklen) <= 0) {
3070 EVP_CIPHER_CTX_free(ctx);
3074 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
3075 EVP_CIPHER_CTX_free(ctx);
3080 EVP_CIPHER_CTX_free(ctx);
3084 sess = d2i_SSL_SESSION(NULL, &p, slen);
3088 * The session ID, if non-empty, is used by some clients to detect
3089 * that the ticket has been accepted. So we copy it to the session
3090 * structure. If it is empty set length to zero as required by
3094 memcpy(sess->session_id, sess_id, sesslen);
3095 sess->session_id_length = sesslen;
3104 * For session parse failure, indicate that we need to send a new ticket.
3108 EVP_CIPHER_CTX_free(ctx);
3109 HMAC_CTX_free(hctx);
3113 /* Tables to translate from NIDs to TLS v1.2 ids */
3120 static const tls12_lookup tls12_md[] = {
3121 {NID_md5, TLSEXT_hash_md5},
3122 {NID_sha1, TLSEXT_hash_sha1},
3123 {NID_sha224, TLSEXT_hash_sha224},
3124 {NID_sha256, TLSEXT_hash_sha256},
3125 {NID_sha384, TLSEXT_hash_sha384},
3126 {NID_sha512, TLSEXT_hash_sha512},
3127 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3128 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3129 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3132 static const tls12_lookup tls12_sig[] = {
3133 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3134 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3135 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3136 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3137 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3138 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3141 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3144 for (i = 0; i < tlen; i++) {
3145 if (table[i].nid == nid)
3151 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3154 for (i = 0; i < tlen; i++) {
3155 if ((table[i].id) == id)
3156 return table[i].nid;
3161 int tls12_get_sigandhash(WPACKET *pkt, const EVP_PKEY *pk, const EVP_MD *md)
3167 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3170 sig_id = tls12_get_sigid(pk);
3173 if (!WPACKET_put_bytes_u8(pkt, md_id) || !WPACKET_put_bytes_u8(pkt, sig_id))
3179 int tls12_get_sigid(const EVP_PKEY *pk)
3181 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3188 unsigned char tlsext_hash;
3191 static const tls12_hash_info tls12_md_info[] = {
3192 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3193 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3194 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3195 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3196 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3197 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3198 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3199 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX,
3200 TLSEXT_hash_gostr34112012_256},
3201 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX,
3202 TLSEXT_hash_gostr34112012_512},
3205 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3211 for (i = 0; i < OSSL_NELEM(tls12_md_info); i++) {
3212 if (tls12_md_info[i].tlsext_hash == hash_alg)
3213 return tls12_md_info + i;
3219 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3221 const tls12_hash_info *inf;
3222 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3224 inf = tls12_get_hash_info(hash_alg);
3227 return ssl_md(inf->md_idx);
3230 static int tls12_get_pkey_idx(unsigned char sig_alg)
3233 #ifndef OPENSSL_NO_RSA
3234 case TLSEXT_signature_rsa:
3235 return SSL_PKEY_RSA_SIGN;
3237 #ifndef OPENSSL_NO_DSA
3238 case TLSEXT_signature_dsa:
3239 return SSL_PKEY_DSA_SIGN;
3241 #ifndef OPENSSL_NO_EC
3242 case TLSEXT_signature_ecdsa:
3243 return SSL_PKEY_ECC;
3245 #ifndef OPENSSL_NO_GOST
3246 case TLSEXT_signature_gostr34102001:
3247 return SSL_PKEY_GOST01;
3249 case TLSEXT_signature_gostr34102012_256:
3250 return SSL_PKEY_GOST12_256;
3252 case TLSEXT_signature_gostr34102012_512:
3253 return SSL_PKEY_GOST12_512;
3259 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3260 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3261 int *psignhash_nid, const unsigned char *data)
3263 int sign_nid = NID_undef, hash_nid = NID_undef;
3264 if (!phash_nid && !psign_nid && !psignhash_nid)
3266 if (phash_nid || psignhash_nid) {
3267 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3269 *phash_nid = hash_nid;
3271 if (psign_nid || psignhash_nid) {
3272 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3274 *psign_nid = sign_nid;
3276 if (psignhash_nid) {
3277 if (sign_nid == NID_undef || hash_nid == NID_undef
3278 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid) <= 0)
3279 *psignhash_nid = NID_undef;
3283 /* Check to see if a signature algorithm is allowed */
3284 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3286 /* See if we have an entry in the hash table and it is enabled */
3287 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3288 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3290 /* See if public key algorithm allowed */
3291 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3293 /* Finally see if security callback allows it */
3294 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3298 * Get a mask of disabled public key algorithms based on supported signature
3299 * algorithms. For example if no signature algorithm supports RSA then RSA is
3303 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3305 const unsigned char *sigalgs;
3306 size_t i, sigalgslen;
3307 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3309 * Now go through all signature algorithms seeing if we support any for
3310 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3311 * down calls to security callback only check if we have to.
3313 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3314 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3315 switch (sigalgs[1]) {
3316 #ifndef OPENSSL_NO_RSA
3317 case TLSEXT_signature_rsa:
3318 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3322 #ifndef OPENSSL_NO_DSA
3323 case TLSEXT_signature_dsa:
3324 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3328 #ifndef OPENSSL_NO_EC
3329 case TLSEXT_signature_ecdsa:
3330 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3337 *pmask_a |= SSL_aRSA;
3339 *pmask_a |= SSL_aDSS;
3341 *pmask_a |= SSL_aECDSA;
3344 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
3345 const unsigned char *psig, size_t psiglen)
3349 for (i = 0; i < psiglen; i += 2, psig += 2) {
3350 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3351 if (!WPACKET_put_bytes_u8(pkt, psig[0])
3352 || !WPACKET_put_bytes_u8(pkt, psig[1]))
3359 /* Given preference and allowed sigalgs set shared sigalgs */
3360 static size_t tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3361 const unsigned char *pref, size_t preflen,
3362 const unsigned char *allow, size_t allowlen)
3364 const unsigned char *ptmp, *atmp;
3365 size_t i, j, nmatch = 0;
3366 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3367 /* Skip disabled hashes or signature algorithms */
3368 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3370 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3371 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3374 shsig->rhash = ptmp[0];
3375 shsig->rsign = ptmp[1];
3376 tls1_lookup_sigalg(&shsig->hash_nid,
3378 &shsig->signandhash_nid, ptmp);
3388 /* Set shared signature algorithms for SSL structures */
3389 static int tls1_set_shared_sigalgs(SSL *s)
3391 const unsigned char *pref, *allow, *conf;
3392 size_t preflen, allowlen, conflen;
3394 TLS_SIGALGS *salgs = NULL;
3396 unsigned int is_suiteb = tls1_suiteb(s);
3398 OPENSSL_free(c->shared_sigalgs);
3399 c->shared_sigalgs = NULL;
3400 c->shared_sigalgslen = 0;
3401 /* If client use client signature algorithms if not NULL */
3402 if (!s->server && c->client_sigalgs && !is_suiteb) {
3403 conf = c->client_sigalgs;
3404 conflen = c->client_sigalgslen;
3405 } else if (c->conf_sigalgs && !is_suiteb) {
3406 conf = c->conf_sigalgs;
3407 conflen = c->conf_sigalgslen;
3409 conflen = tls12_get_psigalgs(s, &conf);
3410 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3413 allow = s->s3->tmp.peer_sigalgs;
3414 allowlen = s->s3->tmp.peer_sigalgslen;
3418 pref = s->s3->tmp.peer_sigalgs;
3419 preflen = s->s3->tmp.peer_sigalgslen;
3421 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3423 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3426 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3430 c->shared_sigalgs = salgs;
3431 c->shared_sigalgslen = nmatch;
3435 /* Set preferred digest for each key type */
3437 int tls1_save_sigalgs(SSL *s, const unsigned char *data, size_t dsize)
3440 /* Extension ignored for inappropriate versions */
3441 if (!SSL_USE_SIGALGS(s))
3443 /* Should never happen */
3447 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3448 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3449 if (s->s3->tmp.peer_sigalgs == NULL)
3451 s->s3->tmp.peer_sigalgslen = dsize;
3452 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3456 int tls1_process_sigalgs(SSL *s)
3461 const EVP_MD **pmd = s->s3->tmp.md;
3462 uint32_t *pvalid = s->s3->tmp.valid_flags;
3464 TLS_SIGALGS *sigptr;
3465 if (!tls1_set_shared_sigalgs(s))
3468 for (i = 0, sigptr = c->shared_sigalgs;
3469 i < c->shared_sigalgslen; i++, sigptr++) {
3470 idx = tls12_get_pkey_idx(sigptr->rsign);
3471 if (idx > 0 && pmd[idx] == NULL) {
3472 md = tls12_get_hash(sigptr->rhash);
3474 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3475 if (idx == SSL_PKEY_RSA_SIGN) {
3476 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3477 pmd[SSL_PKEY_RSA_ENC] = md;
3483 * In strict mode leave unset digests as NULL to indicate we can't use
3484 * the certificate for signing.
3486 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3488 * Set any remaining keys to default values. NOTE: if alg is not
3489 * supported it stays as NULL.
3491 #ifndef OPENSSL_NO_DSA
3492 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3493 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3495 #ifndef OPENSSL_NO_RSA
3496 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3497 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3498 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3501 #ifndef OPENSSL_NO_EC
3502 if (pmd[SSL_PKEY_ECC] == NULL)
3503 pmd[SSL_PKEY_ECC] = EVP_sha1();
3505 #ifndef OPENSSL_NO_GOST
3506 if (pmd[SSL_PKEY_GOST01] == NULL)
3507 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3508 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3509 pmd[SSL_PKEY_GOST12_256] =
3510 EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3511 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3512 pmd[SSL_PKEY_GOST12_512] =
3513 EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3519 int SSL_get_sigalgs(SSL *s, int idx,
3520 int *psign, int *phash, int *psignhash,
3521 unsigned char *rsig, unsigned char *rhash)
3523 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3524 size_t numsigalgs = s->s3->tmp.peer_sigalgslen / 2;
3525 if (psig == NULL || numsigalgs > INT_MAX)
3529 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3536 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3538 return (int)numsigalgs;
3541 int SSL_get_shared_sigalgs(SSL *s, int idx,
3542 int *psign, int *phash, int *psignhash,
3543 unsigned char *rsig, unsigned char *rhash)
3545 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3546 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen
3547 || s->cert->shared_sigalgslen > INT_MAX)
3551 *phash = shsigalgs->hash_nid;
3553 *psign = shsigalgs->sign_nid;
3555 *psignhash = shsigalgs->signandhash_nid;
3557 *rsig = shsigalgs->rsign;
3559 *rhash = shsigalgs->rhash;
3560 return (int)s->cert->shared_sigalgslen;
3563 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3567 int sigalgs[MAX_SIGALGLEN];
3570 static void get_sigorhash(int *psig, int *phash, const char *str)
3572 if (strcmp(str, "RSA") == 0) {
3573 *psig = EVP_PKEY_RSA;
3574 } else if (strcmp(str, "DSA") == 0) {
3575 *psig = EVP_PKEY_DSA;
3576 } else if (strcmp(str, "ECDSA") == 0) {
3577 *psig = EVP_PKEY_EC;
3579 *phash = OBJ_sn2nid(str);
3580 if (*phash == NID_undef)
3581 *phash = OBJ_ln2nid(str);
3585 static int sig_cb(const char *elem, int len, void *arg)
3587 sig_cb_st *sarg = arg;
3590 int sig_alg = NID_undef, hash_alg = NID_undef;
3593 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3595 if (len > (int)(sizeof(etmp) - 1))
3597 memcpy(etmp, elem, len);
3599 p = strchr(etmp, '+');
3607 get_sigorhash(&sig_alg, &hash_alg, etmp);
3608 get_sigorhash(&sig_alg, &hash_alg, p);
3610 if (sig_alg == NID_undef || hash_alg == NID_undef)
3613 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3614 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3617 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3618 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3623 * Set supported signature algorithms based on a colon separated list of the
3624 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3626 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3630 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3634 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3637 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
3639 unsigned char *sigalgs, *sptr;
3644 sigalgs = OPENSSL_malloc(salglen);
3645 if (sigalgs == NULL)
3647 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3648 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3649 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3651 if (rhash == -1 || rsign == -1)
3658 OPENSSL_free(c->client_sigalgs);
3659 c->client_sigalgs = sigalgs;
3660 c->client_sigalgslen = salglen;
3662 OPENSSL_free(c->conf_sigalgs);
3663 c->conf_sigalgs = sigalgs;
3664 c->conf_sigalgslen = salglen;
3670 OPENSSL_free(sigalgs);
3674 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3678 if (default_nid == -1)
3680 sig_nid = X509_get_signature_nid(x);
3682 return sig_nid == default_nid ? 1 : 0;
3683 for (i = 0; i < c->shared_sigalgslen; i++)
3684 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3689 /* Check to see if a certificate issuer name matches list of CA names */
3690 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3694 nm = X509_get_issuer_name(x);
3695 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3696 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3703 * Check certificate chain is consistent with TLS extensions and is usable by
3704 * server. This servers two purposes: it allows users to check chains before
3705 * passing them to the server and it allows the server to check chains before
3706 * attempting to use them.
3709 /* Flags which need to be set for a certificate when stict mode not set */
3711 #define CERT_PKEY_VALID_FLAGS \
3712 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3713 /* Strict mode flags */
3714 #define CERT_PKEY_STRICT_FLAGS \
3715 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3716 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3718 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3723 int check_flags = 0, strict_mode;
3724 CERT_PKEY *cpk = NULL;
3727 unsigned int suiteb_flags = tls1_suiteb(s);
3728 /* idx == -1 means checking server chains */
3730 /* idx == -2 means checking client certificate chains */
3733 idx = (int)(cpk - c->pkeys);
3735 cpk = c->pkeys + idx;
3736 pvalid = s->s3->tmp.valid_flags + idx;
3738 pk = cpk->privatekey;
3740 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3741 /* If no cert or key, forget it */
3747 idx = ssl_cert_type(x, pk);
3750 pvalid = s->s3->tmp.valid_flags + idx;
3752 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3753 check_flags = CERT_PKEY_STRICT_FLAGS;
3755 check_flags = CERT_PKEY_VALID_FLAGS;
3762 check_flags |= CERT_PKEY_SUITEB;
3763 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3764 if (ok == X509_V_OK)
3765 rv |= CERT_PKEY_SUITEB;
3766 else if (!check_flags)
3771 * Check all signature algorithms are consistent with signature
3772 * algorithms extension if TLS 1.2 or later and strict mode.
3774 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3776 unsigned char rsign = 0;
3777 if (s->s3->tmp.peer_sigalgs)
3779 /* If no sigalgs extension use defaults from RFC5246 */
3782 case SSL_PKEY_RSA_ENC:
3783 case SSL_PKEY_RSA_SIGN:
3784 rsign = TLSEXT_signature_rsa;
3785 default_nid = NID_sha1WithRSAEncryption;
3788 case SSL_PKEY_DSA_SIGN:
3789 rsign = TLSEXT_signature_dsa;
3790 default_nid = NID_dsaWithSHA1;
3794 rsign = TLSEXT_signature_ecdsa;
3795 default_nid = NID_ecdsa_with_SHA1;
3798 case SSL_PKEY_GOST01:
3799 rsign = TLSEXT_signature_gostr34102001;
3800 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3803 case SSL_PKEY_GOST12_256:
3804 rsign = TLSEXT_signature_gostr34102012_256;
3805 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3808 case SSL_PKEY_GOST12_512:
3809 rsign = TLSEXT_signature_gostr34102012_512;
3810 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3819 * If peer sent no signature algorithms extension and we have set
3820 * preferred signature algorithms check we support sha1.
3822 if (default_nid > 0 && c->conf_sigalgs) {
3824 const unsigned char *p = c->conf_sigalgs;
3825 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3826 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3829 if (j == c->conf_sigalgslen) {
3836 /* Check signature algorithm of each cert in chain */
3837 if (!tls1_check_sig_alg(c, x, default_nid)) {
3841 rv |= CERT_PKEY_EE_SIGNATURE;
3842 rv |= CERT_PKEY_CA_SIGNATURE;
3843 for (i = 0; i < sk_X509_num(chain); i++) {
3844 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3846 rv &= ~CERT_PKEY_CA_SIGNATURE;
3853 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3854 else if (check_flags)
3855 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3857 /* Check cert parameters are consistent */
3858 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3859 rv |= CERT_PKEY_EE_PARAM;
3860 else if (!check_flags)
3863 rv |= CERT_PKEY_CA_PARAM;
3864 /* In strict mode check rest of chain too */
3865 else if (strict_mode) {
3866 rv |= CERT_PKEY_CA_PARAM;
3867 for (i = 0; i < sk_X509_num(chain); i++) {
3868 X509 *ca = sk_X509_value(chain, i);
3869 if (!tls1_check_cert_param(s, ca, 0)) {
3871 rv &= ~CERT_PKEY_CA_PARAM;
3878 if (!s->server && strict_mode) {
3879 STACK_OF(X509_NAME) *ca_dn;
3881 switch (EVP_PKEY_id(pk)) {
3883 check_type = TLS_CT_RSA_SIGN;
3886 check_type = TLS_CT_DSS_SIGN;
3889 check_type = TLS_CT_ECDSA_SIGN;
3893 const unsigned char *ctypes;
3897 ctypelen = (int)c->ctype_num;
3899 ctypes = (unsigned char *)s->s3->tmp.ctype;
3900 ctypelen = s->s3->tmp.ctype_num;
3902 for (i = 0; i < ctypelen; i++) {
3903 if (ctypes[i] == check_type) {
3904 rv |= CERT_PKEY_CERT_TYPE;
3908 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3911 rv |= CERT_PKEY_CERT_TYPE;
3913 ca_dn = s->s3->tmp.ca_names;
3915 if (!sk_X509_NAME_num(ca_dn))
3916 rv |= CERT_PKEY_ISSUER_NAME;
3918 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3919 if (ssl_check_ca_name(ca_dn, x))
3920 rv |= CERT_PKEY_ISSUER_NAME;
3922 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3923 for (i = 0; i < sk_X509_num(chain); i++) {
3924 X509 *xtmp = sk_X509_value(chain, i);
3925 if (ssl_check_ca_name(ca_dn, xtmp)) {
3926 rv |= CERT_PKEY_ISSUER_NAME;
3931 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3934 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3936 if (!check_flags || (rv & check_flags) == check_flags)
3937 rv |= CERT_PKEY_VALID;
3941 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3942 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3943 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3944 else if (s->s3->tmp.md[idx] != NULL)
3945 rv |= CERT_PKEY_SIGN;
3947 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3950 * When checking a CERT_PKEY structure all flags are irrelevant if the
3954 if (rv & CERT_PKEY_VALID)
3957 /* Preserve explicit sign flag, clear rest */
3958 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3965 /* Set validity of certificates in an SSL structure */
3966 void tls1_set_cert_validity(SSL *s)
3968 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3969 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3970 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
3971 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
3972 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
3973 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
3974 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
3977 /* User level utiity function to check a chain is suitable */
3978 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
3980 return tls1_check_chain(s, x, pk, chain, -1);
3983 #ifndef OPENSSL_NO_DH
3984 DH *ssl_get_auto_dh(SSL *s)
3986 int dh_secbits = 80;
3987 if (s->cert->dh_tmp_auto == 2)
3988 return DH_get_1024_160();
3989 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
3990 if (s->s3->tmp.new_cipher->strength_bits == 256)
3995 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
3996 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
3999 if (dh_secbits >= 128) {
4007 if (dh_secbits >= 192)
4008 p = BN_get_rfc3526_prime_8192(NULL);
4010 p = BN_get_rfc3526_prime_3072(NULL);
4011 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
4019 if (dh_secbits >= 112)
4020 return DH_get_2048_224();
4021 return DH_get_1024_160();
4025 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4028 EVP_PKEY *pkey = X509_get0_pubkey(x);
4031 * If no parameters this will return -1 and fail using the default
4032 * security callback for any non-zero security level. This will
4033 * reject keys which omit parameters but this only affects DSA and
4034 * omission of parameters is never (?) done in practice.
4036 secbits = EVP_PKEY_security_bits(pkey);
4039 return ssl_security(s, op, secbits, 0, x);
4041 return ssl_ctx_security(ctx, op, secbits, 0, x);
4044 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4046 /* Lookup signature algorithm digest */
4047 int secbits = -1, md_nid = NID_undef, sig_nid;
4048 /* Don't check signature if self signed */
4049 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4051 sig_nid = X509_get_signature_nid(x);
4052 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4054 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4055 secbits = EVP_MD_size(md) * 4;
4058 return ssl_security(s, op, secbits, md_nid, x);
4060 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4063 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4066 vfy = SSL_SECOP_PEER;
4068 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4069 return SSL_R_EE_KEY_TOO_SMALL;
4071 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4072 return SSL_R_CA_KEY_TOO_SMALL;
4074 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4075 return SSL_R_CA_MD_TOO_WEAK;
4080 * Check security of a chain, if sk includes the end entity certificate then
4081 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4082 * one to the peer. Return values: 1 if ok otherwise error code to use
4085 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4087 int rv, start_idx, i;
4089 x = sk_X509_value(sk, 0);
4094 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4098 for (i = start_idx; i < sk_X509_num(sk); i++) {
4099 x = sk_X509_value(sk, i);
4100 rv = ssl_security_cert(s, NULL, x, vfy, 0);