1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include <openssl/ocsp.h>
117 #include <openssl/rand.h>
118 #ifndef OPENSSL_NO_DH
119 # include <openssl/dh.h>
120 # include <openssl/bn.h>
122 #include "ssl_locl.h"
124 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
125 const unsigned char *sess_id, int sesslen,
126 SSL_SESSION **psess);
127 static int ssl_check_clienthello_tlsext_early(SSL *s);
128 static int ssl_check_serverhello_tlsext(SSL *s);
130 SSL3_ENC_METHOD const TLSv1_enc_data = {
133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
137 TLS1_FINISH_MAC_LENGTH,
138 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_export_keying_material,
143 SSL3_HM_HEADER_LENGTH,
144 ssl3_set_handshake_header,
148 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
151 tls1_setup_key_block,
152 tls1_generate_master_secret,
153 tls1_change_cipher_state,
154 tls1_final_finish_mac,
155 TLS1_FINISH_MAC_LENGTH,
156 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
166 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
169 tls1_setup_key_block,
170 tls1_generate_master_secret,
171 tls1_change_cipher_state,
172 tls1_final_finish_mac,
173 TLS1_FINISH_MAC_LENGTH,
174 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
175 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
177 tls1_export_keying_material,
178 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
179 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
180 SSL3_HM_HEADER_LENGTH,
181 ssl3_set_handshake_header,
185 long tls1_default_timeout(void)
188 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
189 * http, the cache would over fill
191 return (60 * 60 * 2);
198 s->method->ssl_clear(s);
202 void tls1_free(SSL *s)
204 OPENSSL_free(s->tlsext_session_ticket);
208 void tls1_clear(SSL *s)
211 if (s->method->version == TLS_ANY_VERSION)
212 s->version = TLS_MAX_VERSION;
214 s->version = s->method->version;
217 #ifndef OPENSSL_NO_EC
220 int nid; /* Curve NID */
221 int secbits; /* Bits of security (from SP800-57) */
222 unsigned int flags; /* Flags: currently just field type */
225 /* Mask for curve type */
226 # define TLS_CURVE_TYPE 0x3
227 # define TLS_CURVE_PRIME 0x0
228 # define TLS_CURVE_CHAR2 0x1
229 # define TLS_CURVE_CUSTOM 0x2
232 * Table of curve information.
233 * NB: do not delete entries or reorder this array. It is used as a lookup
234 * table: the index of each entry is one less than the TLS curve id.
237 static const tls_curve_info nid_list[] = {
238 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
239 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
240 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
241 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
242 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
243 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
244 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
245 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
246 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
247 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
248 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
249 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
250 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
251 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
252 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
253 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
254 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
255 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
256 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
257 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
258 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
259 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
260 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
261 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
262 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
263 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
264 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
265 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
267 {NID_X25519, 128, TLS_CURVE_CUSTOM},
270 static const unsigned char ecformats_default[] = {
271 TLSEXT_ECPOINTFORMAT_uncompressed,
272 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
273 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
276 /* The default curves */
277 static const unsigned char eccurves_default[] = {
278 0, 29, /* X25519 (29) */
279 /* Prefer P-256 which has the fastest and most secure implementations. */
280 0, 23, /* secp256r1 (23) */
281 /* Other >= 256-bit prime curves. */
282 0, 25, /* secp521r1 (25) */
283 0, 28, /* brainpool512r1 (28) */
284 0, 27, /* brainpoolP384r1 (27) */
285 0, 24, /* secp384r1 (24) */
286 0, 26, /* brainpoolP256r1 (26) */
287 0, 22, /* secp256k1 (22) */
288 /* >= 256-bit binary curves. */
289 0, 14, /* sect571r1 (14) */
290 0, 13, /* sect571k1 (13) */
291 0, 11, /* sect409k1 (11) */
292 0, 12, /* sect409r1 (12) */
293 0, 9, /* sect283k1 (9) */
294 0, 10, /* sect283r1 (10) */
297 static const unsigned char eccurves_all[] = {
298 0, 29, /* X25519 (29) */
299 /* Prefer P-256 which has the fastest and most secure implementations. */
300 0, 23, /* secp256r1 (23) */
301 /* Other >= 256-bit prime curves. */
302 0, 25, /* secp521r1 (25) */
303 0, 28, /* brainpool512r1 (28) */
304 0, 27, /* brainpoolP384r1 (27) */
305 0, 24, /* secp384r1 (24) */
306 0, 26, /* brainpoolP256r1 (26) */
307 0, 22, /* secp256k1 (22) */
308 /* >= 256-bit binary curves. */
309 0, 14, /* sect571r1 (14) */
310 0, 13, /* sect571k1 (13) */
311 0, 11, /* sect409k1 (11) */
312 0, 12, /* sect409r1 (12) */
313 0, 9, /* sect283k1 (9) */
314 0, 10, /* sect283r1 (10) */
316 * Remaining curves disabled by default but still permitted if set
317 * via an explicit callback or parameters.
319 0, 20, /* secp224k1 (20) */
320 0, 21, /* secp224r1 (21) */
321 0, 18, /* secp192k1 (18) */
322 0, 19, /* secp192r1 (19) */
323 0, 15, /* secp160k1 (15) */
324 0, 16, /* secp160r1 (16) */
325 0, 17, /* secp160r2 (17) */
326 0, 8, /* sect239k1 (8) */
327 0, 6, /* sect233k1 (6) */
328 0, 7, /* sect233r1 (7) */
329 0, 4, /* sect193r1 (4) */
330 0, 5, /* sect193r2 (5) */
331 0, 1, /* sect163k1 (1) */
332 0, 2, /* sect163r1 (2) */
333 0, 3, /* sect163r2 (3) */
337 static const unsigned char suiteb_curves[] = {
338 0, TLSEXT_curve_P_256,
339 0, TLSEXT_curve_P_384
342 int tls1_ec_curve_id2nid(int curve_id)
344 /* ECC curves from RFC 4492 and RFC 7027 */
345 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
347 return nid_list[curve_id - 1].nid;
350 int tls1_ec_nid2curve_id(int nid)
353 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
354 if (nid_list[i].nid == nid)
361 * Get curves list, if "sess" is set return client curves otherwise
363 * Sets |num_curves| to the number of curves in the list, i.e.,
364 * the length of |pcurves| is 2 * num_curves.
365 * Returns 1 on success and 0 if the client curves list has invalid format.
366 * The latter indicates an internal error: we should not be accepting such
367 * lists in the first place.
368 * TODO(emilia): we should really be storing the curves list in explicitly
369 * parsed form instead. (However, this would affect binary compatibility
370 * so cannot happen in the 1.0.x series.)
372 static int tls1_get_curvelist(SSL *s, int sess,
373 const unsigned char **pcurves,
376 size_t pcurveslen = 0;
378 *pcurves = s->session->tlsext_ellipticcurvelist;
379 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
381 /* For Suite B mode only include P-256, P-384 */
382 switch (tls1_suiteb(s)) {
383 case SSL_CERT_FLAG_SUITEB_128_LOS:
384 *pcurves = suiteb_curves;
385 pcurveslen = sizeof(suiteb_curves);
388 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
389 *pcurves = suiteb_curves;
393 case SSL_CERT_FLAG_SUITEB_192_LOS:
394 *pcurves = suiteb_curves + 2;
398 *pcurves = s->tlsext_ellipticcurvelist;
399 pcurveslen = s->tlsext_ellipticcurvelist_length;
402 *pcurves = eccurves_default;
403 pcurveslen = sizeof(eccurves_default);
407 /* We do not allow odd length arrays to enter the system. */
408 if (pcurveslen & 1) {
409 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
413 *num_curves = pcurveslen / 2;
418 /* See if curve is allowed by security callback */
419 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
421 const tls_curve_info *cinfo;
424 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
426 cinfo = &nid_list[curve[1] - 1];
427 # ifdef OPENSSL_NO_EC2M
428 if (cinfo->flags & TLS_CURVE_CHAR2)
431 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
434 /* Check a curve is one of our preferences */
435 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
437 const unsigned char *curves;
438 size_t num_curves, i;
439 unsigned int suiteb_flags = tls1_suiteb(s);
440 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
442 /* Check curve matches Suite B preferences */
444 unsigned long cid = s->s3->tmp.new_cipher->id;
447 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
448 if (p[2] != TLSEXT_curve_P_256)
450 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
451 if (p[2] != TLSEXT_curve_P_384)
453 } else /* Should never happen */
456 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
458 for (i = 0; i < num_curves; i++, curves += 2) {
459 if (p[1] == curves[0] && p[2] == curves[1])
460 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
466 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
467 * if there is no match.
468 * For nmatch == -1, return number of matches
469 * For nmatch == -2, return the NID of the curve to use for
470 * an EC tmp key, or NID_undef if there is no match.
472 int tls1_shared_curve(SSL *s, int nmatch)
474 const unsigned char *pref, *supp;
475 size_t num_pref, num_supp, i, j;
477 /* Can't do anything on client side */
481 if (tls1_suiteb(s)) {
483 * For Suite B ciphersuite determines curve: we already know
484 * these are acceptable due to previous checks.
486 unsigned long cid = s->s3->tmp.new_cipher->id;
487 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
488 return NID_X9_62_prime256v1; /* P-256 */
489 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
490 return NID_secp384r1; /* P-384 */
491 /* Should never happen */
494 /* If not Suite B just return first preference shared curve */
498 * Avoid truncation. tls1_get_curvelist takes an int
499 * but s->options is a long...
501 if (!tls1_get_curvelist
502 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
504 /* In practice, NID_undef == 0 but let's be precise. */
505 return nmatch == -1 ? 0 : NID_undef;
506 if (!tls1_get_curvelist
507 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
509 return nmatch == -1 ? 0 : NID_undef;
512 * If the client didn't send the elliptic_curves extension all of them
515 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
517 num_supp = sizeof(eccurves_all) / 2;
518 } else if (num_pref == 0 &&
519 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
521 num_pref = sizeof(eccurves_all) / 2;
525 for (i = 0; i < num_pref; i++, pref += 2) {
526 const unsigned char *tsupp = supp;
527 for (j = 0; j < num_supp; j++, tsupp += 2) {
528 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
529 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
532 int id = (pref[0] << 8) | pref[1];
533 return tls1_ec_curve_id2nid(id);
541 /* Out of range (nmatch > k). */
545 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
546 int *curves, size_t ncurves)
548 unsigned char *clist, *p;
551 * Bitmap of curves included to detect duplicates: only works while curve
554 unsigned long dup_list = 0;
555 clist = OPENSSL_malloc(ncurves * 2);
558 for (i = 0, p = clist; i < ncurves; i++) {
559 unsigned long idmask;
561 id = tls1_ec_nid2curve_id(curves[i]);
563 if (!id || (dup_list & idmask)) {
572 *pextlen = ncurves * 2;
576 # define MAX_CURVELIST 28
580 int nid_arr[MAX_CURVELIST];
583 static int nid_cb(const char *elem, int len, void *arg)
585 nid_cb_st *narg = arg;
591 if (narg->nidcnt == MAX_CURVELIST)
593 if (len > (int)(sizeof(etmp) - 1))
595 memcpy(etmp, elem, len);
597 nid = EC_curve_nist2nid(etmp);
598 if (nid == NID_undef)
599 nid = OBJ_sn2nid(etmp);
600 if (nid == NID_undef)
601 nid = OBJ_ln2nid(etmp);
602 if (nid == NID_undef)
604 for (i = 0; i < narg->nidcnt; i++)
605 if (narg->nid_arr[i] == nid)
607 narg->nid_arr[narg->nidcnt++] = nid;
611 /* Set curves based on a colon separate list */
612 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
617 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
621 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
624 /* For an EC key set TLS id and required compression based on parameters */
625 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
632 /* Determine if it is a prime field */
633 grp = EC_KEY_get0_group(ec);
636 /* Determine curve ID */
637 id = EC_GROUP_get_curve_name(grp);
638 id = tls1_ec_nid2curve_id(id);
639 /* If no id return error: we don't support arbitrary explicit curves */
643 curve_id[1] = (unsigned char)id;
645 if (EC_KEY_get0_public_key(ec) == NULL)
647 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
648 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
650 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
651 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
653 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
659 /* Check an EC key is compatible with extensions */
660 static int tls1_check_ec_key(SSL *s,
661 unsigned char *curve_id, unsigned char *comp_id)
663 const unsigned char *pformats, *pcurves;
664 size_t num_formats, num_curves, i;
667 * If point formats extension present check it, otherwise everything is
668 * supported (see RFC4492).
670 if (comp_id && s->session->tlsext_ecpointformatlist) {
671 pformats = s->session->tlsext_ecpointformatlist;
672 num_formats = s->session->tlsext_ecpointformatlist_length;
673 for (i = 0; i < num_formats; i++, pformats++) {
674 if (*comp_id == *pformats)
677 if (i == num_formats)
682 /* Check curve is consistent with client and server preferences */
683 for (j = 0; j <= 1; j++) {
684 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
686 if (j == 1 && num_curves == 0) {
688 * If we've not received any curves then skip this check.
689 * RFC 4492 does not require the supported elliptic curves extension
690 * so if it is not sent we can just choose any curve.
691 * It is invalid to send an empty list in the elliptic curves
692 * extension, so num_curves == 0 always means no extension.
696 for (i = 0; i < num_curves; i++, pcurves += 2) {
697 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
702 /* For clients can only check sent curve list */
709 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
713 * If we have a custom point format list use it otherwise use default
715 if (s->tlsext_ecpointformatlist) {
716 *pformats = s->tlsext_ecpointformatlist;
717 *num_formats = s->tlsext_ecpointformatlist_length;
719 *pformats = ecformats_default;
720 /* For Suite B we don't support char2 fields */
722 *num_formats = sizeof(ecformats_default) - 1;
724 *num_formats = sizeof(ecformats_default);
729 * Check cert parameters compatible with extensions: currently just checks EC
730 * certificates have compatible curves and compression.
732 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
734 unsigned char comp_id, curve_id[2];
737 pkey = X509_get0_pubkey(x);
740 /* If not EC nothing to do */
741 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
743 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
747 * Can't check curve_id for client certs as we don't have a supported
750 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
754 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
755 * SHA384+P-384, adjust digest if necessary.
757 if (set_ee_md && tls1_suiteb(s)) {
763 /* Check to see we have necessary signing algorithm */
764 if (curve_id[1] == TLSEXT_curve_P_256)
765 check_md = NID_ecdsa_with_SHA256;
766 else if (curve_id[1] == TLSEXT_curve_P_384)
767 check_md = NID_ecdsa_with_SHA384;
769 return 0; /* Should never happen */
770 for (i = 0; i < c->shared_sigalgslen; i++)
771 if (check_md == c->shared_sigalgs[i].signandhash_nid)
773 if (i == c->shared_sigalgslen)
775 if (set_ee_md == 2) {
776 if (check_md == NID_ecdsa_with_SHA256)
777 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
779 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
785 # ifndef OPENSSL_NO_EC
787 * tls1_check_ec_tmp_key - Check EC temporary key compatiblity
789 * @cid: Cipher ID we're considering using
791 * Checks that the kECDHE cipher suite we're considering using
792 * is compatible with the client extensions.
794 * Returns 0 when the cipher can't be used or 1 when it can.
796 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
799 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
802 if (tls1_suiteb(s)) {
803 unsigned char curve_id[2];
804 /* Curve to check determined by ciphersuite */
805 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
806 curve_id[1] = TLSEXT_curve_P_256;
807 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
808 curve_id[1] = TLSEXT_curve_P_384;
812 /* Check this curve is acceptable */
813 if (!tls1_check_ec_key(s, curve_id, NULL))
817 /* Need a shared curve */
818 if (tls1_shared_curve(s, 0))
822 # endif /* OPENSSL_NO_EC */
826 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
831 #endif /* OPENSSL_NO_EC */
834 * List of supported signature algorithms and hashes. Should make this
835 * customisable at some point, for now include everything we support.
838 #ifdef OPENSSL_NO_RSA
839 # define tlsext_sigalg_rsa(md) /* */
841 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
844 #ifdef OPENSSL_NO_DSA
845 # define tlsext_sigalg_dsa(md) /* */
847 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
851 # define tlsext_sigalg_ecdsa(md) /* */
853 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
856 #define tlsext_sigalg(md) \
857 tlsext_sigalg_rsa(md) \
858 tlsext_sigalg_dsa(md) \
859 tlsext_sigalg_ecdsa(md)
861 static const unsigned char tls12_sigalgs[] = {
862 tlsext_sigalg(TLSEXT_hash_sha512)
863 tlsext_sigalg(TLSEXT_hash_sha384)
864 tlsext_sigalg(TLSEXT_hash_sha256)
865 tlsext_sigalg(TLSEXT_hash_sha224)
866 tlsext_sigalg(TLSEXT_hash_sha1)
867 #ifndef OPENSSL_NO_GOST
868 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
869 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
870 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
874 #ifndef OPENSSL_NO_EC
875 static const unsigned char suiteb_sigalgs[] = {
876 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
877 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
880 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
883 * If Suite B mode use Suite B sigalgs only, ignore any other
886 #ifndef OPENSSL_NO_EC
887 switch (tls1_suiteb(s)) {
888 case SSL_CERT_FLAG_SUITEB_128_LOS:
889 *psigs = suiteb_sigalgs;
890 return sizeof(suiteb_sigalgs);
892 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
893 *psigs = suiteb_sigalgs;
896 case SSL_CERT_FLAG_SUITEB_192_LOS:
897 *psigs = suiteb_sigalgs + 2;
901 /* If server use client authentication sigalgs if not NULL */
902 if (s->server && s->cert->client_sigalgs) {
903 *psigs = s->cert->client_sigalgs;
904 return s->cert->client_sigalgslen;
905 } else if (s->cert->conf_sigalgs) {
906 *psigs = s->cert->conf_sigalgs;
907 return s->cert->conf_sigalgslen;
909 *psigs = tls12_sigalgs;
910 return sizeof(tls12_sigalgs);
915 * Check signature algorithm is consistent with sent supported signature
916 * algorithms and if so return relevant digest.
918 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
919 const unsigned char *sig, EVP_PKEY *pkey)
921 const unsigned char *sent_sigs;
922 size_t sent_sigslen, i;
923 int sigalg = tls12_get_sigid(pkey);
924 /* Should never happen */
927 /* Check key type is consistent with signature */
928 if (sigalg != (int)sig[1]) {
929 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
932 #ifndef OPENSSL_NO_EC
933 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
934 unsigned char curve_id[2], comp_id;
935 /* Check compression and curve matches extensions */
936 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
938 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
939 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
942 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
943 if (tls1_suiteb(s)) {
946 if (curve_id[1] == TLSEXT_curve_P_256) {
947 if (sig[0] != TLSEXT_hash_sha256) {
948 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
949 SSL_R_ILLEGAL_SUITEB_DIGEST);
952 } else if (curve_id[1] == TLSEXT_curve_P_384) {
953 if (sig[0] != TLSEXT_hash_sha384) {
954 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
955 SSL_R_ILLEGAL_SUITEB_DIGEST);
961 } else if (tls1_suiteb(s))
965 /* Check signature matches a type we sent */
966 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
967 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
968 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
971 /* Allow fallback to SHA1 if not strict mode */
972 if (i == sent_sigslen
973 && (sig[0] != TLSEXT_hash_sha1
974 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
975 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
978 *pmd = tls12_get_hash(sig[0]);
980 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
983 /* Make sure security callback allows algorithm */
984 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
985 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
987 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
991 * Store the digest used so applications can retrieve it if they wish.
993 s->s3->tmp.peer_md = *pmd;
998 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
999 * supported or doesn't appear in supported signature algorithms. Unlike
1000 * ssl_cipher_get_disabled this applies to a specific session and not global
1003 void ssl_set_client_disabled(SSL *s)
1005 s->s3->tmp.mask_a = 0;
1006 s->s3->tmp.mask_k = 0;
1007 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1008 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1009 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1011 s->s3->tmp.mask_ssl = 0;
1012 /* Disable TLS 1.0 ciphers if using SSL v3 */
1013 if (s->client_version == SSL3_VERSION)
1014 s->s3->tmp.mask_ssl |= SSL_TLSV1;
1015 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1016 # ifndef OPENSSL_NO_PSK
1017 /* with PSK there must be client callback set */
1018 if (!s->psk_client_callback) {
1019 s->s3->tmp.mask_a |= SSL_aPSK;
1020 s->s3->tmp.mask_k |= SSL_PSK;
1022 #endif /* OPENSSL_NO_PSK */
1023 #ifndef OPENSSL_NO_SRP
1024 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1025 s->s3->tmp.mask_a |= SSL_aSRP;
1026 s->s3->tmp.mask_k |= SSL_kSRP;
1031 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1033 if (c->algorithm_ssl & s->s3->tmp.mask_ssl
1034 || c->algorithm_mkey & s->s3->tmp.mask_k
1035 || c->algorithm_auth & s->s3->tmp.mask_a)
1037 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1040 static int tls_use_ticket(SSL *s)
1042 if (s->options & SSL_OP_NO_TICKET)
1044 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1047 static int compare_uint(const void *p1, const void *p2) {
1048 unsigned int u1 = *((const unsigned int *)p1);
1049 unsigned int u2 = *((const unsigned int *)p2);
1059 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
1060 * more than one extension of the same type in a ClientHello or ServerHello.
1061 * This function does an initial scan over the extensions block to filter those
1062 * out. It returns 1 if all extensions are unique, and 0 if the extensions
1063 * contain duplicates, could not be successfully parsed, or an internal error
1066 static int tls1_check_duplicate_extensions(const PACKET *packet) {
1067 PACKET extensions = *packet;
1068 size_t num_extensions = 0, i = 0;
1069 unsigned int *extension_types = NULL;
1072 /* First pass: count the extensions. */
1073 while (PACKET_remaining(&extensions) > 0) {
1076 if (!PACKET_get_net_2(&extensions, &type) ||
1077 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1083 if (num_extensions <= 1)
1086 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
1087 if (extension_types == NULL) {
1088 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
1092 /* Second pass: gather the extension types. */
1093 extensions = *packet;
1094 for (i = 0; i < num_extensions; i++) {
1096 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
1097 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1098 /* This should not happen. */
1099 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1104 if (PACKET_remaining(&extensions) != 0) {
1105 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1108 /* Sort the extensions and make sure there are no duplicates. */
1109 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1110 for (i = 1; i < num_extensions; i++) {
1111 if (extension_types[i - 1] == extension_types[i])
1116 OPENSSL_free(extension_types);
1120 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1121 unsigned char *limit, int *al)
1124 unsigned char *orig = buf;
1125 unsigned char *ret = buf;
1126 #ifndef OPENSSL_NO_EC
1127 /* See if we support any ECC ciphersuites */
1129 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1131 unsigned long alg_k, alg_a;
1132 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1134 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1135 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1137 alg_k = c->algorithm_mkey;
1138 alg_a = c->algorithm_auth;
1139 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1140 || (alg_a & SSL_aECDSA)) {
1151 return NULL; /* this really never occurs, but ... */
1153 /* Add RI if renegotiating */
1154 if (s->renegotiate) {
1157 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1158 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1162 if ((limit - ret - 4 - el) < 0)
1165 s2n(TLSEXT_TYPE_renegotiate, ret);
1168 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1169 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1175 /* Only add RI for SSLv3 */
1176 if (s->client_version == SSL3_VERSION)
1179 if (s->tlsext_hostname != NULL) {
1180 /* Add TLS extension servername to the Client Hello message */
1181 unsigned long size_str;
1185 * check for enough space.
1186 * 4 for the servername type and entension length
1187 * 2 for servernamelist length
1188 * 1 for the hostname type
1189 * 2 for hostname length
1193 if ((lenmax = limit - ret - 9) < 0
1195 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1198 /* extension type and length */
1199 s2n(TLSEXT_TYPE_server_name, ret);
1200 s2n(size_str + 5, ret);
1202 /* length of servername list */
1203 s2n(size_str + 3, ret);
1205 /* hostname type, length and hostname */
1206 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1208 memcpy(ret, s->tlsext_hostname, size_str);
1211 #ifndef OPENSSL_NO_SRP
1212 /* Add SRP username if there is one */
1213 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1214 * Client Hello message */
1216 int login_len = strlen(s->srp_ctx.login);
1217 if (login_len > 255 || login_len == 0) {
1218 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1223 * check for enough space.
1224 * 4 for the srp type type and entension length
1225 * 1 for the srp user identity
1226 * + srp user identity length
1228 if ((limit - ret - 5 - login_len) < 0)
1231 /* fill in the extension */
1232 s2n(TLSEXT_TYPE_srp, ret);
1233 s2n(login_len + 1, ret);
1234 (*ret++) = (unsigned char)login_len;
1235 memcpy(ret, s->srp_ctx.login, login_len);
1240 #ifndef OPENSSL_NO_EC
1243 * Add TLS extension ECPointFormats to the ClientHello message
1246 const unsigned char *pcurves, *pformats;
1247 size_t num_curves, num_formats, curves_list_len;
1249 unsigned char *etmp;
1251 tls1_get_formatlist(s, &pformats, &num_formats);
1253 if ((lenmax = limit - ret - 5) < 0)
1255 if (num_formats > (size_t)lenmax)
1257 if (num_formats > 255) {
1258 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1262 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1263 /* The point format list has 1-byte length. */
1264 s2n(num_formats + 1, ret);
1265 *(ret++) = (unsigned char)num_formats;
1266 memcpy(ret, pformats, num_formats);
1270 * Add TLS extension EllipticCurves to the ClientHello message
1272 pcurves = s->tlsext_ellipticcurvelist;
1273 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1276 if ((lenmax = limit - ret - 6) < 0)
1278 if (num_curves > (size_t)lenmax / 2)
1280 if (num_curves > 65532 / 2) {
1281 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1285 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1287 /* Copy curve ID if supported */
1288 for (i = 0; i < num_curves; i++, pcurves += 2) {
1289 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1290 *etmp++ = pcurves[0];
1291 *etmp++ = pcurves[1];
1295 curves_list_len = etmp - ret - 4;
1297 s2n(curves_list_len + 2, ret);
1298 s2n(curves_list_len, ret);
1299 ret += curves_list_len;
1301 #endif /* OPENSSL_NO_EC */
1303 if (tls_use_ticket(s)) {
1305 if (!s->new_session && s->session && s->session->tlsext_tick)
1306 ticklen = s->session->tlsext_ticklen;
1307 else if (s->session && s->tlsext_session_ticket &&
1308 s->tlsext_session_ticket->data) {
1309 ticklen = s->tlsext_session_ticket->length;
1310 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1311 if (s->session->tlsext_tick == NULL)
1313 memcpy(s->session->tlsext_tick,
1314 s->tlsext_session_ticket->data, ticklen);
1315 s->session->tlsext_ticklen = ticklen;
1318 if (ticklen == 0 && s->tlsext_session_ticket &&
1319 s->tlsext_session_ticket->data == NULL)
1322 * Check for enough room 2 for extension type, 2 for len rest for
1325 if ((long)(limit - ret - 4 - ticklen) < 0)
1327 s2n(TLSEXT_TYPE_session_ticket, ret);
1330 memcpy(ret, s->session->tlsext_tick, ticklen);
1336 if (SSL_USE_SIGALGS(s)) {
1338 const unsigned char *salg;
1339 unsigned char *etmp;
1340 salglen = tls12_get_psigalgs(s, &salg);
1341 if ((size_t)(limit - ret) < salglen + 6)
1343 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1345 /* Skip over lengths for now */
1347 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1348 /* Fill in lengths */
1349 s2n(salglen + 2, etmp);
1354 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1356 long extlen, idlen, itmp;
1360 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1361 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1362 itmp = i2d_OCSP_RESPID(id, NULL);
1368 if (s->tlsext_ocsp_exts) {
1369 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1375 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1377 s2n(TLSEXT_TYPE_status_request, ret);
1378 if (extlen + idlen > 0xFFF0)
1380 s2n(extlen + idlen + 5, ret);
1381 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1383 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1384 /* save position of id len */
1385 unsigned char *q = ret;
1386 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1387 /* skip over id len */
1389 itmp = i2d_OCSP_RESPID(id, &ret);
1395 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1397 #ifndef OPENSSL_NO_HEARTBEATS
1398 if (SSL_IS_DTLS(s)) {
1399 /* Add Heartbeat extension */
1400 if ((limit - ret - 4 - 1) < 0)
1402 s2n(TLSEXT_TYPE_heartbeat, ret);
1406 * 1: peer may send requests
1407 * 2: peer not allowed to send requests
1409 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1410 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1412 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1416 #ifndef OPENSSL_NO_NEXTPROTONEG
1417 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1419 * The client advertises an emtpy extension to indicate its support
1420 * for Next Protocol Negotiation
1422 if (limit - ret - 4 < 0)
1424 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1429 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1430 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1432 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1433 s2n(2 + s->alpn_client_proto_list_len, ret);
1434 s2n(s->alpn_client_proto_list_len, ret);
1435 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1436 ret += s->alpn_client_proto_list_len;
1438 #ifndef OPENSSL_NO_SRTP
1439 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1442 /* Returns 0 on success!! */
1443 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1444 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1448 if ((limit - ret - 4 - el) < 0)
1451 s2n(TLSEXT_TYPE_use_srtp, ret);
1454 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1455 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1461 custom_ext_init(&s->cert->cli_ext);
1462 /* Add custom TLS Extensions to ClientHello */
1463 if (!custom_ext_add(s, 0, &ret, limit, al))
1465 #ifdef TLSEXT_TYPE_encrypt_then_mac
1466 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1469 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1473 * Add padding to workaround bugs in F5 terminators. See
1474 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1475 * code works out the length of all existing extensions it MUST always
1478 if (s->options & SSL_OP_TLSEXT_PADDING) {
1479 int hlen = ret - (unsigned char *)s->init_buf->data;
1481 if (hlen > 0xff && hlen < 0x200) {
1482 hlen = 0x200 - hlen;
1488 s2n(TLSEXT_TYPE_padding, ret);
1490 memset(ret, 0, hlen);
1497 if ((extdatalen = ret - orig - 2) == 0)
1500 s2n(extdatalen, orig);
1504 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1505 unsigned char *limit, int *al)
1508 unsigned char *orig = buf;
1509 unsigned char *ret = buf;
1510 #ifndef OPENSSL_NO_NEXTPROTONEG
1511 int next_proto_neg_seen;
1513 #ifndef OPENSSL_NO_EC
1514 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1515 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1516 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1517 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1522 return NULL; /* this really never occurs, but ... */
1524 if (s->s3->send_connection_binding) {
1527 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1528 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1532 if ((limit - ret - 4 - el) < 0)
1535 s2n(TLSEXT_TYPE_renegotiate, ret);
1538 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1539 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1546 /* Only add RI for SSLv3 */
1547 if (s->version == SSL3_VERSION)
1550 if (!s->hit && s->servername_done == 1
1551 && s->session->tlsext_hostname != NULL) {
1552 if ((long)(limit - ret - 4) < 0)
1555 s2n(TLSEXT_TYPE_server_name, ret);
1558 #ifndef OPENSSL_NO_EC
1560 const unsigned char *plist;
1563 * Add TLS extension ECPointFormats to the ServerHello message
1567 tls1_get_formatlist(s, &plist, &plistlen);
1569 if ((lenmax = limit - ret - 5) < 0)
1571 if (plistlen > (size_t)lenmax)
1573 if (plistlen > 255) {
1574 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1578 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1579 s2n(plistlen + 1, ret);
1580 *(ret++) = (unsigned char)plistlen;
1581 memcpy(ret, plist, plistlen);
1586 * Currently the server should not respond with a SupportedCurves
1589 #endif /* OPENSSL_NO_EC */
1591 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1592 if ((long)(limit - ret - 4) < 0)
1594 s2n(TLSEXT_TYPE_session_ticket, ret);
1598 if (s->tlsext_status_expected) {
1599 if ((long)(limit - ret - 4) < 0)
1601 s2n(TLSEXT_TYPE_status_request, ret);
1605 #ifndef OPENSSL_NO_SRTP
1606 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1609 /* Returns 0 on success!! */
1610 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1611 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1614 if ((limit - ret - 4 - el) < 0)
1617 s2n(TLSEXT_TYPE_use_srtp, ret);
1620 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1621 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1628 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1629 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1630 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1631 const unsigned char cryptopro_ext[36] = {
1632 0xfd, 0xe8, /* 65000 */
1633 0x00, 0x20, /* 32 bytes length */
1634 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1635 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1636 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1637 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1639 if (limit - ret < 36)
1641 memcpy(ret, cryptopro_ext, 36);
1645 #ifndef OPENSSL_NO_HEARTBEATS
1646 /* Add Heartbeat extension if we've received one */
1647 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1648 if ((limit - ret - 4 - 1) < 0)
1650 s2n(TLSEXT_TYPE_heartbeat, ret);
1654 * 1: peer may send requests
1655 * 2: peer not allowed to send requests
1657 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1658 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1660 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1665 #ifndef OPENSSL_NO_NEXTPROTONEG
1666 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1667 s->s3->next_proto_neg_seen = 0;
1668 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1669 const unsigned char *npa;
1670 unsigned int npalen;
1673 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1675 ctx->next_protos_advertised_cb_arg);
1676 if (r == SSL_TLSEXT_ERR_OK) {
1677 if ((long)(limit - ret - 4 - npalen) < 0)
1679 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1681 memcpy(ret, npa, npalen);
1683 s->s3->next_proto_neg_seen = 1;
1687 if (!custom_ext_add(s, 1, &ret, limit, al))
1689 #ifdef TLSEXT_TYPE_encrypt_then_mac
1690 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1692 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1693 * for other cases too.
1695 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1696 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1697 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1698 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1699 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1701 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1706 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1707 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1711 if (s->s3->alpn_selected) {
1712 const unsigned char *selected = s->s3->alpn_selected;
1713 unsigned len = s->s3->alpn_selected_len;
1715 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1717 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1721 memcpy(ret, selected, len);
1727 if ((extdatalen = ret - orig - 2) == 0)
1730 s2n(extdatalen, orig);
1735 * tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1736 * ClientHello. data: the contents of the extension, not including the type
1737 * and length. data_len: the number of bytes in |data| al: a pointer to the
1738 * alert value to send in the event of a non-zero return. returns: 0 on
1741 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1743 unsigned int data_len;
1744 unsigned int proto_len;
1745 const unsigned char *selected;
1746 const unsigned char *data;
1747 unsigned char selected_len;
1750 if (s->ctx->alpn_select_cb == NULL)
1754 * data should contain a uint16 length followed by a series of 8-bit,
1755 * length-prefixed strings.
1757 if (!PACKET_get_net_2(pkt, &data_len)
1758 || PACKET_remaining(pkt) != data_len
1759 || !PACKET_peek_bytes(pkt, &data, data_len))
1763 if (!PACKET_get_1(pkt, &proto_len)
1765 || !PACKET_forward(pkt, proto_len))
1767 } while (PACKET_remaining(pkt));
1769 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1770 s->ctx->alpn_select_cb_arg);
1771 if (r == SSL_TLSEXT_ERR_OK) {
1772 OPENSSL_free(s->s3->alpn_selected);
1773 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1774 if (s->s3->alpn_selected == NULL) {
1775 *al = SSL_AD_INTERNAL_ERROR;
1778 memcpy(s->s3->alpn_selected, selected, selected_len);
1779 s->s3->alpn_selected_len = selected_len;
1784 *al = SSL_AD_DECODE_ERROR;
1788 #ifndef OPENSSL_NO_EC
1790 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1791 * SecureTransport using the TLS extension block in |d|, of length |n|.
1792 * Safari, since 10.6, sends exactly these extensions, in this order:
1797 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1798 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1799 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1800 * 10.8..10.8.3 (which don't work).
1802 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1804 unsigned int type, size;
1805 const unsigned char *eblock1, *eblock2;
1808 static const unsigned char kSafariExtensionsBlock[] = {
1809 0x00, 0x0a, /* elliptic_curves extension */
1810 0x00, 0x08, /* 8 bytes */
1811 0x00, 0x06, /* 6 bytes of curve ids */
1812 0x00, 0x17, /* P-256 */
1813 0x00, 0x18, /* P-384 */
1814 0x00, 0x19, /* P-521 */
1816 0x00, 0x0b, /* ec_point_formats */
1817 0x00, 0x02, /* 2 bytes */
1818 0x01, /* 1 point format */
1819 0x00, /* uncompressed */
1822 /* The following is only present in TLS 1.2 */
1823 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1824 0x00, 0x0d, /* signature_algorithms */
1825 0x00, 0x0c, /* 12 bytes */
1826 0x00, 0x0a, /* 10 bytes */
1827 0x05, 0x01, /* SHA-384/RSA */
1828 0x04, 0x01, /* SHA-256/RSA */
1829 0x02, 0x01, /* SHA-1/RSA */
1830 0x04, 0x03, /* SHA-256/ECDSA */
1831 0x02, 0x03, /* SHA-1/ECDSA */
1836 if (!PACKET_forward(&tmppkt, 2)
1837 || !PACKET_get_net_2(&tmppkt, &type)
1838 || !PACKET_get_net_2(&tmppkt, &size)
1839 || !PACKET_forward(&tmppkt, size))
1842 if (type != TLSEXT_TYPE_server_name)
1845 if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
1846 const size_t len1 = sizeof(kSafariExtensionsBlock);
1847 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1849 if (!PACKET_get_bytes(&tmppkt, &eblock1, len1)
1850 || !PACKET_get_bytes(&tmppkt, &eblock2, len2)
1851 || PACKET_remaining(&tmppkt))
1853 if (memcmp(eblock1, kSafariExtensionsBlock, len1) != 0)
1855 if (memcmp(eblock2, kSafariTLS12ExtensionsBlock, len2) != 0)
1858 const size_t len = sizeof(kSafariExtensionsBlock);
1860 if (!PACKET_get_bytes(&tmppkt, &eblock1, len)
1861 || PACKET_remaining(&tmppkt))
1863 if (memcmp(eblock1, kSafariExtensionsBlock, len) != 0)
1867 s->s3->is_probably_safari = 1;
1869 #endif /* !OPENSSL_NO_EC */
1871 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1876 const unsigned char *data;
1877 int renegotiate_seen = 0;
1879 s->servername_done = 0;
1880 s->tlsext_status_type = -1;
1881 #ifndef OPENSSL_NO_NEXTPROTONEG
1882 s->s3->next_proto_neg_seen = 0;
1885 OPENSSL_free(s->s3->alpn_selected);
1886 s->s3->alpn_selected = NULL;
1887 #ifndef OPENSSL_NO_HEARTBEATS
1888 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1889 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1892 #ifndef OPENSSL_NO_EC
1893 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1894 ssl_check_for_safari(s, pkt);
1895 # endif /* !OPENSSL_NO_EC */
1897 /* Clear any signature algorithms extension received */
1898 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1899 s->s3->tmp.peer_sigalgs = NULL;
1900 #ifdef TLSEXT_TYPE_encrypt_then_mac
1901 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1904 #ifndef OPENSSL_NO_SRP
1905 OPENSSL_free(s->srp_ctx.login);
1906 s->srp_ctx.login = NULL;
1909 s->srtp_profile = NULL;
1911 if (PACKET_remaining(pkt) == 0)
1914 if (!PACKET_get_net_2(pkt, &len))
1917 if (PACKET_remaining(pkt) != len)
1920 if (!tls1_check_duplicate_extensions(pkt))
1923 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
1926 if (!PACKET_peek_bytes(pkt, &data, size))
1929 if (s->tlsext_debug_cb)
1930 s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
1932 if (!PACKET_get_sub_packet(pkt, &subpkt, size))
1935 if (type == TLSEXT_TYPE_renegotiate) {
1936 if (!ssl_parse_clienthello_renegotiate_ext(s, &subpkt, al))
1938 renegotiate_seen = 1;
1939 } else if (s->version == SSL3_VERSION) {
1942 * The servername extension is treated as follows:
1944 * - Only the hostname type is supported with a maximum length of 255.
1945 * - The servername is rejected if too long or if it contains zeros,
1946 * in which case an fatal alert is generated.
1947 * - The servername field is maintained together with the session cache.
1948 * - When a session is resumed, the servername call back invoked in order
1949 * to allow the application to position itself to the right context.
1950 * - The servername is acknowledged if it is new for a session or when
1951 * it is identical to a previously used for the same session.
1952 * Applications can control the behaviour. They can at any time
1953 * set a 'desirable' servername for a new SSL object. This can be the
1954 * case for example with HTTPS when a Host: header field is received and
1955 * a renegotiation is requested. In this case, a possible servername
1956 * presented in the new client hello is only acknowledged if it matches
1957 * the value of the Host: field.
1958 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1959 * if they provide for changing an explicit servername context for the
1960 * session, i.e. when the session has been established with a servername
1962 * - On session reconnect, the servername extension may be absent.
1966 else if (type == TLSEXT_TYPE_server_name) {
1967 const unsigned char *sdata;
1968 unsigned int servname_type;
1972 if (!PACKET_get_net_2(&subpkt, &dsize)
1973 || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))
1976 while (PACKET_remaining(&ssubpkt) > 3) {
1977 if (!PACKET_get_1(&ssubpkt, &servname_type)
1978 || !PACKET_get_net_2(&ssubpkt, &len)
1979 || PACKET_remaining(&ssubpkt) < len)
1982 if (s->servername_done == 0)
1983 switch (servname_type) {
1984 case TLSEXT_NAMETYPE_host_name:
1986 if (s->session->tlsext_hostname)
1989 if (len > TLSEXT_MAXLEN_host_name) {
1990 *al = TLS1_AD_UNRECOGNIZED_NAME;
1993 if ((s->session->tlsext_hostname =
1994 OPENSSL_malloc(len + 1)) == NULL) {
1995 *al = TLS1_AD_INTERNAL_ERROR;
1998 if (!PACKET_copy_bytes(&ssubpkt,
1999 (unsigned char *)s->session
2002 *al = SSL_AD_DECODE_ERROR;
2005 s->session->tlsext_hostname[len] = '\0';
2006 if (strlen(s->session->tlsext_hostname) != len) {
2007 OPENSSL_free(s->session->tlsext_hostname);
2008 s->session->tlsext_hostname = NULL;
2009 *al = TLS1_AD_UNRECOGNIZED_NAME;
2012 s->servername_done = 1;
2015 if (!PACKET_get_bytes(&ssubpkt, &sdata, len)) {
2016 *al = SSL_AD_DECODE_ERROR;
2019 s->servername_done = s->session->tlsext_hostname
2020 && strlen(s->session->tlsext_hostname) == len
2021 && strncmp(s->session->tlsext_hostname,
2022 (char *)sdata, len) == 0;
2031 /* We shouldn't have any bytes left */
2032 if (PACKET_remaining(&ssubpkt) != 0)
2036 #ifndef OPENSSL_NO_SRP
2037 else if (type == TLSEXT_TYPE_srp) {
2038 if (!PACKET_get_1(&subpkt, &len)
2039 || s->srp_ctx.login != NULL)
2042 if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
2044 if (!PACKET_copy_bytes(&subpkt, (unsigned char *)s->srp_ctx.login,
2047 s->srp_ctx.login[len] = '\0';
2049 if (strlen(s->srp_ctx.login) != len
2050 || PACKET_remaining(&subpkt))
2055 #ifndef OPENSSL_NO_EC
2056 else if (type == TLSEXT_TYPE_ec_point_formats) {
2057 unsigned int ecpointformatlist_length;
2059 if (!PACKET_get_1(&subpkt, &ecpointformatlist_length)
2060 || ecpointformatlist_length == 0)
2064 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2065 s->session->tlsext_ecpointformatlist = NULL;
2066 s->session->tlsext_ecpointformatlist_length = 0;
2067 if ((s->session->tlsext_ecpointformatlist =
2068 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2069 *al = TLS1_AD_INTERNAL_ERROR;
2072 s->session->tlsext_ecpointformatlist_length =
2073 ecpointformatlist_length;
2074 if (!PACKET_copy_bytes(&subpkt,
2075 s->session->tlsext_ecpointformatlist,
2076 ecpointformatlist_length))
2078 } else if (!PACKET_forward(&subpkt, ecpointformatlist_length)) {
2081 /* We should have consumed all the bytes by now */
2082 if (PACKET_remaining(&subpkt)) {
2083 *al = TLS1_AD_DECODE_ERROR;
2086 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2087 unsigned int ellipticcurvelist_length;
2089 /* Each NamedCurve is 2 bytes and we must have at least 1 */
2090 if (!PACKET_get_net_2(&subpkt, &ellipticcurvelist_length)
2091 || ellipticcurvelist_length == 0
2092 || (ellipticcurvelist_length & 1) != 0)
2096 if (s->session->tlsext_ellipticcurvelist)
2099 s->session->tlsext_ellipticcurvelist_length = 0;
2100 if ((s->session->tlsext_ellipticcurvelist =
2101 OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
2102 *al = TLS1_AD_INTERNAL_ERROR;
2105 s->session->tlsext_ellipticcurvelist_length =
2106 ellipticcurvelist_length;
2107 if (!PACKET_copy_bytes(&subpkt,
2108 s->session->tlsext_ellipticcurvelist,
2109 ellipticcurvelist_length))
2111 } else if (!PACKET_forward(&subpkt, ellipticcurvelist_length)) {
2114 /* We should have consumed all the bytes by now */
2115 if (PACKET_remaining(&subpkt)) {
2119 #endif /* OPENSSL_NO_EC */
2120 else if (type == TLSEXT_TYPE_session_ticket) {
2121 if (!PACKET_forward(&subpkt, size)
2122 || (s->tls_session_ticket_ext_cb &&
2123 !s->tls_session_ticket_ext_cb(s, data, size,
2124 s->tls_session_ticket_ext_cb_arg))) {
2125 *al = TLS1_AD_INTERNAL_ERROR;
2128 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2131 if (s->s3->tmp.peer_sigalgs
2132 || !PACKET_get_net_2(&subpkt, &dsize)
2135 || !PACKET_get_bytes(&subpkt, &data, dsize)
2136 || PACKET_remaining(&subpkt) != 0
2137 || !tls1_save_sigalgs(s, data, dsize)) {
2140 } else if (type == TLSEXT_TYPE_status_request) {
2143 if (!PACKET_get_1(&subpkt,
2144 (unsigned int *)&s->tlsext_status_type))
2147 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2148 const unsigned char *sdata;
2150 /* Read in responder_id_list */
2151 if (!PACKET_get_net_2(&subpkt, &dsize)
2152 || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))
2155 while (PACKET_remaining(&ssubpkt)) {
2157 unsigned int idsize;
2159 if (PACKET_remaining(&ssubpkt) < 4
2160 || !PACKET_get_net_2(&ssubpkt, &idsize)
2161 || !PACKET_get_bytes(&ssubpkt, &data, idsize)) {
2166 id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
2169 if (data != sdata) {
2170 OCSP_RESPID_free(id);
2173 if (!s->tlsext_ocsp_ids
2174 && !(s->tlsext_ocsp_ids =
2175 sk_OCSP_RESPID_new_null())) {
2176 OCSP_RESPID_free(id);
2177 *al = SSL_AD_INTERNAL_ERROR;
2180 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2181 OCSP_RESPID_free(id);
2182 *al = SSL_AD_INTERNAL_ERROR;
2187 /* Read in request_extensions */
2188 if (!PACKET_get_net_2(&subpkt, &dsize)
2189 || !PACKET_get_bytes(&subpkt, &data, dsize)
2190 || PACKET_remaining(&subpkt)) {
2195 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2196 X509_EXTENSION_free);
2197 s->tlsext_ocsp_exts =
2198 d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
2199 if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
2204 * We don't know what to do with any other type * so ignore it.
2207 s->tlsext_status_type = -1;
2209 #ifndef OPENSSL_NO_HEARTBEATS
2210 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2211 unsigned int hbtype;
2213 if (!PACKET_get_1(&subpkt, &hbtype)
2214 || PACKET_remaining(&subpkt)) {
2215 *al = SSL_AD_DECODE_ERROR;
2219 case 0x01: /* Client allows us to send HB requests */
2220 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2222 case 0x02: /* Client doesn't accept HB requests */
2223 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2224 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2227 *al = SSL_AD_ILLEGAL_PARAMETER;
2232 #ifndef OPENSSL_NO_NEXTPROTONEG
2233 else if (type == TLSEXT_TYPE_next_proto_neg &&
2234 s->s3->tmp.finish_md_len == 0 &&
2235 s->s3->alpn_selected == NULL) {
2237 * We shouldn't accept this extension on a
2240 * s->new_session will be set on renegotiation, but we
2241 * probably shouldn't rely that it couldn't be set on
2242 * the initial renegotation too in certain cases (when
2243 * there's some other reason to disallow resuming an
2244 * earlier session -- the current code won't be doing
2245 * anything like that, but this might change).
2247 * A valid sign that there's been a previous handshake
2248 * in this connection is if s->s3->tmp.finish_md_len >
2249 * 0. (We are talking about a check that will happen
2250 * in the Hello protocol round, well before a new
2251 * Finished message could have been computed.)
2253 s->s3->next_proto_neg_seen = 1;
2257 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2258 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
2259 if (tls1_alpn_handle_client_hello(s, &subpkt, al) != 0)
2261 #ifndef OPENSSL_NO_NEXTPROTONEG
2262 /* ALPN takes precedence over NPN. */
2263 s->s3->next_proto_neg_seen = 0;
2267 /* session ticket processed earlier */
2268 #ifndef OPENSSL_NO_SRTP
2269 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2270 && type == TLSEXT_TYPE_use_srtp) {
2271 if (ssl_parse_clienthello_use_srtp_ext(s, &subpkt, al))
2275 #ifdef TLSEXT_TYPE_encrypt_then_mac
2276 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2277 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2280 * Note: extended master secret extension handled in
2281 * tls_check_serverhello_tlsext_early()
2285 * If this ClientHello extension was unhandled and this is a
2286 * nonresumed connection, check whether the extension is a custom
2287 * TLS Extension (has a custom_srv_ext_record), and if so call the
2288 * callback and record the extension number so that an appropriate
2289 * ServerHello may be later returned.
2292 if (custom_ext_parse(s, 1, type, data, size, al) <= 0)
2297 /* Spurious data on the end */
2298 if (PACKET_remaining(pkt) != 0)
2303 /* Need RI if renegotiating */
2305 if (!renegotiate_seen && s->renegotiate &&
2306 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2307 *al = SSL_AD_HANDSHAKE_FAILURE;
2308 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2309 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2315 *al = SSL_AD_DECODE_ERROR;
2319 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2322 custom_ext_init(&s->cert->srv_ext);
2323 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2324 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2328 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2329 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2335 #ifndef OPENSSL_NO_NEXTPROTONEG
2337 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2338 * elements of zero length are allowed and the set of elements must exactly
2339 * fill the length of the block.
2341 static char ssl_next_proto_validate(PACKET *pkt)
2345 while (PACKET_remaining(pkt)) {
2346 if (!PACKET_get_1(pkt, &len)
2347 || !PACKET_forward(pkt, len))
2355 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2357 unsigned int length, type, size;
2358 int tlsext_servername = 0;
2359 int renegotiate_seen = 0;
2361 #ifndef OPENSSL_NO_NEXTPROTONEG
2362 s->s3->next_proto_neg_seen = 0;
2364 s->tlsext_ticket_expected = 0;
2366 OPENSSL_free(s->s3->alpn_selected);
2367 s->s3->alpn_selected = NULL;
2368 #ifndef OPENSSL_NO_HEARTBEATS
2369 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2370 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2373 #ifdef TLSEXT_TYPE_encrypt_then_mac
2374 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2377 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2379 if (!PACKET_get_net_2(pkt, &length))
2382 if (PACKET_remaining(pkt) != length) {
2383 *al = SSL_AD_DECODE_ERROR;
2387 if (!tls1_check_duplicate_extensions(pkt)) {
2388 *al = SSL_AD_DECODE_ERROR;
2392 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2393 const unsigned char *data;
2396 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2397 || !PACKET_peek_bytes(&spkt, &data, size))
2400 if (s->tlsext_debug_cb)
2401 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2403 if (type == TLSEXT_TYPE_renegotiate) {
2404 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2406 renegotiate_seen = 1;
2407 } else if (s->version == SSL3_VERSION) {
2408 } else if (type == TLSEXT_TYPE_server_name) {
2409 if (s->tlsext_hostname == NULL || size > 0) {
2410 *al = TLS1_AD_UNRECOGNIZED_NAME;
2413 tlsext_servername = 1;
2415 #ifndef OPENSSL_NO_EC
2416 else if (type == TLSEXT_TYPE_ec_point_formats) {
2417 unsigned int ecpointformatlist_length;
2418 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2419 || ecpointformatlist_length != size - 1) {
2420 *al = TLS1_AD_DECODE_ERROR;
2424 s->session->tlsext_ecpointformatlist_length = 0;
2425 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2426 if ((s->session->tlsext_ecpointformatlist =
2427 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2428 *al = TLS1_AD_INTERNAL_ERROR;
2431 s->session->tlsext_ecpointformatlist_length =
2432 ecpointformatlist_length;
2433 if (!PACKET_copy_bytes(&spkt,
2434 s->session->tlsext_ecpointformatlist,
2435 ecpointformatlist_length)) {
2436 *al = TLS1_AD_DECODE_ERROR;
2442 #endif /* OPENSSL_NO_EC */
2444 else if (type == TLSEXT_TYPE_session_ticket) {
2445 if (s->tls_session_ticket_ext_cb &&
2446 !s->tls_session_ticket_ext_cb(s, data, size,
2447 s->tls_session_ticket_ext_cb_arg))
2449 *al = TLS1_AD_INTERNAL_ERROR;
2452 if (!tls_use_ticket(s) || (size > 0)) {
2453 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2456 s->tlsext_ticket_expected = 1;
2458 else if (type == TLSEXT_TYPE_status_request) {
2460 * MUST be empty and only sent if we've requested a status
2463 if ((s->tlsext_status_type == -1) || (size > 0)) {
2464 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2467 /* Set flag to expect CertificateStatus message */
2468 s->tlsext_status_expected = 1;
2470 #ifndef OPENSSL_NO_NEXTPROTONEG
2471 else if (type == TLSEXT_TYPE_next_proto_neg &&
2472 s->s3->tmp.finish_md_len == 0) {
2473 unsigned char *selected;
2474 unsigned char selected_len;
2475 /* We must have requested it. */
2476 if (s->ctx->next_proto_select_cb == NULL) {
2477 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2480 /* The data must be valid */
2481 if (!ssl_next_proto_validate(&spkt)) {
2482 *al = TLS1_AD_DECODE_ERROR;
2486 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2488 s->ctx->next_proto_select_cb_arg) !=
2489 SSL_TLSEXT_ERR_OK) {
2490 *al = TLS1_AD_INTERNAL_ERROR;
2493 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2494 if (s->next_proto_negotiated == NULL) {
2495 *al = TLS1_AD_INTERNAL_ERROR;
2498 memcpy(s->next_proto_negotiated, selected, selected_len);
2499 s->next_proto_negotiated_len = selected_len;
2500 s->s3->next_proto_neg_seen = 1;
2504 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2506 /* We must have requested it. */
2507 if (s->alpn_client_proto_list == NULL) {
2508 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2512 * The extension data consists of:
2513 * uint16 list_length
2514 * uint8 proto_length;
2515 * uint8 proto[proto_length];
2517 if (!PACKET_get_net_2(&spkt, &len)
2518 || PACKET_remaining(&spkt) != len
2519 || !PACKET_get_1(&spkt, &len)
2520 || PACKET_remaining(&spkt) != len) {
2521 *al = TLS1_AD_DECODE_ERROR;
2524 OPENSSL_free(s->s3->alpn_selected);
2525 s->s3->alpn_selected = OPENSSL_malloc(len);
2526 if (s->s3->alpn_selected == NULL) {
2527 *al = TLS1_AD_INTERNAL_ERROR;
2530 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2531 *al = TLS1_AD_DECODE_ERROR;
2534 s->s3->alpn_selected_len = len;
2536 #ifndef OPENSSL_NO_HEARTBEATS
2537 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2538 unsigned int hbtype;
2539 if (!PACKET_get_1(&spkt, &hbtype)) {
2540 *al = SSL_AD_DECODE_ERROR;
2544 case 0x01: /* Server allows us to send HB requests */
2545 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2547 case 0x02: /* Server doesn't accept HB requests */
2548 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2549 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2552 *al = SSL_AD_ILLEGAL_PARAMETER;
2557 #ifndef OPENSSL_NO_SRTP
2558 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2559 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2563 #ifdef TLSEXT_TYPE_encrypt_then_mac
2564 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2565 /* Ignore if inappropriate ciphersuite */
2566 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2567 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2568 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2571 else if (type == TLSEXT_TYPE_extended_master_secret) {
2572 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2574 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2577 * If this extension type was not otherwise handled, but matches a
2578 * custom_cli_ext_record, then send it to the c callback
2580 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2584 if (PACKET_remaining(pkt) != 0) {
2585 *al = SSL_AD_DECODE_ERROR;
2589 if (!s->hit && tlsext_servername == 1) {
2590 if (s->tlsext_hostname) {
2591 if (s->session->tlsext_hostname == NULL) {
2592 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
2593 if (!s->session->tlsext_hostname) {
2594 *al = SSL_AD_UNRECOGNIZED_NAME;
2598 *al = SSL_AD_DECODE_ERROR;
2607 * Determine if we need to see RI. Strictly speaking if we want to avoid
2608 * an attack we should *always* see RI even on initial server hello
2609 * because the client doesn't see any renegotiation during an attack.
2610 * However this would mean we could not connect to any server which
2611 * doesn't support RI so for the immediate future tolerate RI absence on
2612 * initial connect only.
2614 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2615 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2616 *al = SSL_AD_HANDSHAKE_FAILURE;
2617 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2618 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2624 * Check extended master secret extension is consistent with
2627 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2628 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2629 *al = SSL_AD_HANDSHAKE_FAILURE;
2630 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2638 int ssl_prepare_clienthello_tlsext(SSL *s)
2644 int ssl_prepare_serverhello_tlsext(SSL *s)
2649 static int ssl_check_clienthello_tlsext_early(SSL *s)
2651 int ret = SSL_TLSEXT_ERR_NOACK;
2652 int al = SSL_AD_UNRECOGNIZED_NAME;
2654 #ifndef OPENSSL_NO_EC
2656 * The handling of the ECPointFormats extension is done elsewhere, namely
2657 * in ssl3_choose_cipher in s3_lib.c.
2660 * The handling of the EllipticCurves extension is done elsewhere, namely
2661 * in ssl3_choose_cipher in s3_lib.c.
2665 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2667 s->ctx->tlsext_servername_callback(s, &al,
2668 s->ctx->tlsext_servername_arg);
2669 else if (s->initial_ctx != NULL
2670 && s->initial_ctx->tlsext_servername_callback != 0)
2672 s->initial_ctx->tlsext_servername_callback(s, &al,
2674 initial_ctx->tlsext_servername_arg);
2677 case SSL_TLSEXT_ERR_ALERT_FATAL:
2678 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2681 case SSL_TLSEXT_ERR_ALERT_WARNING:
2682 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2685 case SSL_TLSEXT_ERR_NOACK:
2686 s->servername_done = 0;
2691 /* Initialise digests to default values */
2692 void ssl_set_default_md(SSL *s)
2694 const EVP_MD **pmd = s->s3->tmp.md;
2695 #ifndef OPENSSL_NO_DSA
2696 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2698 #ifndef OPENSSL_NO_RSA
2699 if (SSL_USE_SIGALGS(s))
2700 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2702 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2703 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2705 #ifndef OPENSSL_NO_EC
2706 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2708 #ifndef OPENSSL_NO_GOST
2709 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2710 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2711 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2715 int tls1_set_server_sigalgs(SSL *s)
2719 /* Clear any shared sigtnature algorithms */
2720 OPENSSL_free(s->cert->shared_sigalgs);
2721 s->cert->shared_sigalgs = NULL;
2722 s->cert->shared_sigalgslen = 0;
2723 /* Clear certificate digests and validity flags */
2724 for (i = 0; i < SSL_PKEY_NUM; i++) {
2725 s->s3->tmp.md[i] = NULL;
2726 s->s3->tmp.valid_flags[i] = 0;
2729 /* If sigalgs received process it. */
2730 if (s->s3->tmp.peer_sigalgs) {
2731 if (!tls1_process_sigalgs(s)) {
2732 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2733 al = SSL_AD_INTERNAL_ERROR;
2736 /* Fatal error is no shared signature algorithms */
2737 if (!s->cert->shared_sigalgs) {
2738 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2739 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2740 al = SSL_AD_ILLEGAL_PARAMETER;
2744 ssl_set_default_md(s);
2748 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2752 int ssl_check_clienthello_tlsext_late(SSL *s)
2754 int ret = SSL_TLSEXT_ERR_OK;
2755 int al = SSL_AD_INTERNAL_ERROR;
2758 * If status request then ask callback what to do. Note: this must be
2759 * called after servername callbacks in case the certificate has changed,
2760 * and must be called after the cipher has been chosen because this may
2761 * influence which certificate is sent
2763 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2765 CERT_PKEY *certpkey;
2766 certpkey = ssl_get_server_send_pkey(s);
2767 /* If no certificate can't return certificate status */
2768 if (certpkey == NULL) {
2769 s->tlsext_status_expected = 0;
2773 * Set current certificate to one we will use so SSL_get_certificate
2774 * et al can pick it up.
2776 s->cert->key = certpkey;
2777 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2779 /* We don't want to send a status request response */
2780 case SSL_TLSEXT_ERR_NOACK:
2781 s->tlsext_status_expected = 0;
2783 /* status request response should be sent */
2784 case SSL_TLSEXT_ERR_OK:
2785 if (s->tlsext_ocsp_resp)
2786 s->tlsext_status_expected = 1;
2788 s->tlsext_status_expected = 0;
2790 /* something bad happened */
2791 case SSL_TLSEXT_ERR_ALERT_FATAL:
2792 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2793 al = SSL_AD_INTERNAL_ERROR;
2797 s->tlsext_status_expected = 0;
2801 case SSL_TLSEXT_ERR_ALERT_FATAL:
2802 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2805 case SSL_TLSEXT_ERR_ALERT_WARNING:
2806 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2814 int ssl_check_serverhello_tlsext(SSL *s)
2816 int ret = SSL_TLSEXT_ERR_NOACK;
2817 int al = SSL_AD_UNRECOGNIZED_NAME;
2819 #ifndef OPENSSL_NO_EC
2821 * If we are client and using an elliptic curve cryptography cipher
2822 * suite, then if server returns an EC point formats lists extension it
2823 * must contain uncompressed.
2825 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2826 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2827 if ((s->tlsext_ecpointformatlist != NULL)
2828 && (s->tlsext_ecpointformatlist_length > 0)
2829 && (s->session->tlsext_ecpointformatlist != NULL)
2830 && (s->session->tlsext_ecpointformatlist_length > 0)
2831 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2832 /* we are using an ECC cipher */
2834 unsigned char *list;
2835 int found_uncompressed = 0;
2836 list = s->session->tlsext_ecpointformatlist;
2837 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2838 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2839 found_uncompressed = 1;
2843 if (!found_uncompressed) {
2844 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2845 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2849 ret = SSL_TLSEXT_ERR_OK;
2850 #endif /* OPENSSL_NO_EC */
2852 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2854 s->ctx->tlsext_servername_callback(s, &al,
2855 s->ctx->tlsext_servername_arg);
2856 else if (s->initial_ctx != NULL
2857 && s->initial_ctx->tlsext_servername_callback != 0)
2859 s->initial_ctx->tlsext_servername_callback(s, &al,
2861 initial_ctx->tlsext_servername_arg);
2864 * Ensure we get sensible values passed to tlsext_status_cb in the event
2865 * that we don't receive a status message
2867 OPENSSL_free(s->tlsext_ocsp_resp);
2868 s->tlsext_ocsp_resp = NULL;
2869 s->tlsext_ocsp_resplen = -1;
2872 case SSL_TLSEXT_ERR_ALERT_FATAL:
2873 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2876 case SSL_TLSEXT_ERR_ALERT_WARNING:
2877 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2880 case SSL_TLSEXT_ERR_NOACK:
2881 s->servername_done = 0;
2887 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2890 if (s->version < SSL3_VERSION)
2892 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2893 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2897 if (ssl_check_serverhello_tlsext(s) <= 0) {
2898 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2905 * Since the server cache lookup is done early on in the processing of the
2906 * ClientHello and other operations depend on the result some extensions
2907 * need to be handled at the same time.
2909 * Two extensions are currently handled, session ticket and extended master
2912 * session_id: ClientHello session ID.
2913 * ext: ClientHello extensions (including length prefix)
2914 * ret: (output) on return, if a ticket was decrypted, then this is set to
2915 * point to the resulting session.
2917 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2918 * ciphersuite, in which case we have no use for session tickets and one will
2919 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2922 * -1: fatal error, either from parsing or decrypting the ticket.
2923 * 0: no ticket was found (or was ignored, based on settings).
2924 * 1: a zero length extension was found, indicating that the client supports
2925 * session tickets but doesn't currently have one to offer.
2926 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2927 * couldn't be decrypted because of a non-fatal error.
2928 * 3: a ticket was successfully decrypted and *ret was set.
2931 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2932 * a new session ticket to the client because the client indicated support
2933 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2934 * a session ticket or we couldn't use the one it gave us, or if
2935 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2936 * Otherwise, s->tlsext_ticket_expected is set to 0.
2938 * For extended master secret flag is set if the extension is present.
2941 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2942 const PACKET *session_id,
2946 PACKET local_ext = *ext;
2949 int have_ticket = 0;
2950 int use_ticket = tls_use_ticket(s);
2953 s->tlsext_ticket_expected = 0;
2954 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2957 * If tickets disabled behave as if no ticket present to permit stateful
2960 if ((s->version <= SSL3_VERSION))
2963 if (!PACKET_get_net_2(&local_ext, &i)) {
2967 while (PACKET_remaining(&local_ext) >= 4) {
2968 unsigned int type, size;
2970 if (!PACKET_get_net_2(&local_ext, &type)
2971 || !PACKET_get_net_2(&local_ext, &size)) {
2972 /* Shouldn't ever happen */
2976 if (PACKET_remaining(&local_ext) < size) {
2980 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
2982 const unsigned char *etick;
2984 /* Duplicate extension */
2985 if (have_ticket != 0) {
2993 * The client will accept a ticket but doesn't currently have
2996 s->tlsext_ticket_expected = 1;
3000 if (s->tls_session_secret_cb) {
3002 * Indicate that the ticket couldn't be decrypted rather than
3003 * generating the session from ticket now, trigger
3004 * abbreviated handshake based on external mechanism to
3005 * calculate the master secret later.
3010 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
3011 /* Shouldn't ever happen */
3015 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
3016 PACKET_remaining(session_id), ret);
3018 case 2: /* ticket couldn't be decrypted */
3019 s->tlsext_ticket_expected = 1;
3022 case 3: /* ticket was decrypted */
3025 case 4: /* ticket decrypted but need to renew */
3026 s->tlsext_ticket_expected = 1;
3029 default: /* fatal error */
3035 if (type == TLSEXT_TYPE_extended_master_secret)
3036 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
3037 if (!PACKET_forward(&local_ext, size)) {
3043 if (have_ticket == 0)
3050 * tls_decrypt_ticket attempts to decrypt a session ticket.
3052 * etick: points to the body of the session ticket extension.
3053 * eticklen: the length of the session tickets extenion.
3054 * sess_id: points at the session ID.
3055 * sesslen: the length of the session ID.
3056 * psess: (output) on return, if a ticket was decrypted, then this is set to
3057 * point to the resulting session.
3060 * -2: fatal error, malloc failure.
3061 * -1: fatal error, either from parsing or decrypting the ticket.
3062 * 2: the ticket couldn't be decrypted.
3063 * 3: a ticket was successfully decrypted and *psess was set.
3064 * 4: same as 3, but the ticket needs to be renewed.
3066 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3067 int eticklen, const unsigned char *sess_id,
3068 int sesslen, SSL_SESSION **psess)
3071 unsigned char *sdec;
3072 const unsigned char *p;
3073 int slen, mlen, renew_ticket = 0, ret = -1;
3074 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3075 HMAC_CTX *hctx = NULL;
3076 EVP_CIPHER_CTX *ctx;
3077 SSL_CTX *tctx = s->initial_ctx;
3078 /* Need at least keyname + iv + some encrypted data */
3081 /* Initialize session ticket encryption and HMAC contexts */
3082 hctx = HMAC_CTX_new();
3085 ctx = EVP_CIPHER_CTX_new();
3090 if (tctx->tlsext_ticket_key_cb) {
3091 unsigned char *nctick = (unsigned char *)etick;
3092 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3103 /* Check key name matches */
3104 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
3108 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16,
3109 EVP_sha256(), NULL) <= 0
3110 || EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL,
3111 tctx->tlsext_tick_aes_key,
3117 * Attempt to process session ticket, first conduct sanity and integrity
3120 mlen = HMAC_size(hctx);
3125 /* Check HMAC of encrypted ticket */
3126 if (HMAC_Update(hctx, etick, eticklen) <= 0
3127 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3130 HMAC_CTX_free(hctx);
3131 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3132 EVP_CIPHER_CTX_free(ctx);
3135 /* Attempt to decrypt session data */
3136 /* Move p after IV to start of encrypted ticket, update length */
3137 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3138 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3139 sdec = OPENSSL_malloc(eticklen);
3141 || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3142 EVP_CIPHER_CTX_free(ctx);
3145 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3146 EVP_CIPHER_CTX_free(ctx);
3151 EVP_CIPHER_CTX_free(ctx);
3155 sess = d2i_SSL_SESSION(NULL, &p, slen);
3159 * The session ID, if non-empty, is used by some clients to detect
3160 * that the ticket has been accepted. So we copy it to the session
3161 * structure. If it is empty set length to zero as required by
3165 memcpy(sess->session_id, sess_id, sesslen);
3166 sess->session_id_length = sesslen;
3175 * For session parse failure, indicate that we need to send a new ticket.
3179 EVP_CIPHER_CTX_free(ctx);
3180 HMAC_CTX_free(hctx);
3184 /* Tables to translate from NIDs to TLS v1.2 ids */
3191 static const tls12_lookup tls12_md[] = {
3192 {NID_md5, TLSEXT_hash_md5},
3193 {NID_sha1, TLSEXT_hash_sha1},
3194 {NID_sha224, TLSEXT_hash_sha224},
3195 {NID_sha256, TLSEXT_hash_sha256},
3196 {NID_sha384, TLSEXT_hash_sha384},
3197 {NID_sha512, TLSEXT_hash_sha512},
3198 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3199 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3200 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3203 static const tls12_lookup tls12_sig[] = {
3204 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3205 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3206 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3207 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3208 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3209 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3212 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3215 for (i = 0; i < tlen; i++) {
3216 if (table[i].nid == nid)
3222 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3225 for (i = 0; i < tlen; i++) {
3226 if ((table[i].id) == id)
3227 return table[i].nid;
3232 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3238 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3241 sig_id = tls12_get_sigid(pk);
3244 p[0] = (unsigned char)md_id;
3245 p[1] = (unsigned char)sig_id;
3249 int tls12_get_sigid(const EVP_PKEY *pk)
3251 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3258 unsigned char tlsext_hash;
3261 static const tls12_hash_info tls12_md_info[] = {
3262 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3263 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3264 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3265 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3266 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3267 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3268 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3269 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3270 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3273 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3279 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3281 if (tls12_md_info[i].tlsext_hash == hash_alg)
3282 return tls12_md_info + i;
3288 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3290 const tls12_hash_info *inf;
3291 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3293 inf = tls12_get_hash_info(hash_alg);
3296 return ssl_md(inf->md_idx);
3299 static int tls12_get_pkey_idx(unsigned char sig_alg)
3302 #ifndef OPENSSL_NO_RSA
3303 case TLSEXT_signature_rsa:
3304 return SSL_PKEY_RSA_SIGN;
3306 #ifndef OPENSSL_NO_DSA
3307 case TLSEXT_signature_dsa:
3308 return SSL_PKEY_DSA_SIGN;
3310 #ifndef OPENSSL_NO_EC
3311 case TLSEXT_signature_ecdsa:
3312 return SSL_PKEY_ECC;
3314 # ifndef OPENSSL_NO_GOST
3315 case TLSEXT_signature_gostr34102001:
3316 return SSL_PKEY_GOST01;
3318 case TLSEXT_signature_gostr34102012_256:
3319 return SSL_PKEY_GOST12_256;
3321 case TLSEXT_signature_gostr34102012_512:
3322 return SSL_PKEY_GOST12_512;
3328 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3329 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3330 int *psignhash_nid, const unsigned char *data)
3332 int sign_nid = NID_undef, hash_nid = NID_undef;
3333 if (!phash_nid && !psign_nid && !psignhash_nid)
3335 if (phash_nid || psignhash_nid) {
3336 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3338 *phash_nid = hash_nid;
3340 if (psign_nid || psignhash_nid) {
3341 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3343 *psign_nid = sign_nid;
3345 if (psignhash_nid) {
3346 if (sign_nid == NID_undef || hash_nid == NID_undef
3347 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3349 *psignhash_nid = NID_undef;
3353 /* Check to see if a signature algorithm is allowed */
3354 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3356 /* See if we have an entry in the hash table and it is enabled */
3357 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3358 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3360 /* See if public key algorithm allowed */
3361 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3363 /* Finally see if security callback allows it */
3364 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3368 * Get a mask of disabled public key algorithms based on supported signature
3369 * algorithms. For example if no signature algorithm supports RSA then RSA is
3373 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3375 const unsigned char *sigalgs;
3376 size_t i, sigalgslen;
3377 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3379 * Now go through all signature algorithms seeing if we support any for
3380 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3381 * down calls to security callback only check if we have to.
3383 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3384 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3385 switch (sigalgs[1]) {
3386 #ifndef OPENSSL_NO_RSA
3387 case TLSEXT_signature_rsa:
3388 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3392 #ifndef OPENSSL_NO_DSA
3393 case TLSEXT_signature_dsa:
3394 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3398 #ifndef OPENSSL_NO_EC
3399 case TLSEXT_signature_ecdsa:
3400 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3407 *pmask_a |= SSL_aRSA;
3409 *pmask_a |= SSL_aDSS;
3411 *pmask_a |= SSL_aECDSA;
3414 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3415 const unsigned char *psig, size_t psiglen)
3417 unsigned char *tmpout = out;
3419 for (i = 0; i < psiglen; i += 2, psig += 2) {
3420 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3421 *tmpout++ = psig[0];
3422 *tmpout++ = psig[1];
3425 return tmpout - out;
3428 /* Given preference and allowed sigalgs set shared sigalgs */
3429 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3430 const unsigned char *pref, size_t preflen,
3431 const unsigned char *allow, size_t allowlen)
3433 const unsigned char *ptmp, *atmp;
3434 size_t i, j, nmatch = 0;
3435 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3436 /* Skip disabled hashes or signature algorithms */
3437 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3439 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3440 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3443 shsig->rhash = ptmp[0];
3444 shsig->rsign = ptmp[1];
3445 tls1_lookup_sigalg(&shsig->hash_nid,
3447 &shsig->signandhash_nid, ptmp);
3457 /* Set shared signature algorithms for SSL structures */
3458 static int tls1_set_shared_sigalgs(SSL *s)
3460 const unsigned char *pref, *allow, *conf;
3461 size_t preflen, allowlen, conflen;
3463 TLS_SIGALGS *salgs = NULL;
3465 unsigned int is_suiteb = tls1_suiteb(s);
3467 OPENSSL_free(c->shared_sigalgs);
3468 c->shared_sigalgs = NULL;
3469 c->shared_sigalgslen = 0;
3470 /* If client use client signature algorithms if not NULL */
3471 if (!s->server && c->client_sigalgs && !is_suiteb) {
3472 conf = c->client_sigalgs;
3473 conflen = c->client_sigalgslen;
3474 } else if (c->conf_sigalgs && !is_suiteb) {
3475 conf = c->conf_sigalgs;
3476 conflen = c->conf_sigalgslen;
3478 conflen = tls12_get_psigalgs(s, &conf);
3479 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3482 allow = s->s3->tmp.peer_sigalgs;
3483 allowlen = s->s3->tmp.peer_sigalgslen;
3487 pref = s->s3->tmp.peer_sigalgs;
3488 preflen = s->s3->tmp.peer_sigalgslen;
3490 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3492 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3495 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3499 c->shared_sigalgs = salgs;
3500 c->shared_sigalgslen = nmatch;
3504 /* Set preferred digest for each key type */
3506 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3509 /* Extension ignored for inappropriate versions */
3510 if (!SSL_USE_SIGALGS(s))
3512 /* Should never happen */
3516 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3517 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3518 if (s->s3->tmp.peer_sigalgs == NULL)
3520 s->s3->tmp.peer_sigalgslen = dsize;
3521 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3525 int tls1_process_sigalgs(SSL *s)
3530 const EVP_MD **pmd = s->s3->tmp.md;
3531 uint32_t *pvalid = s->s3->tmp.valid_flags;
3533 TLS_SIGALGS *sigptr;
3534 if (!tls1_set_shared_sigalgs(s))
3537 for (i = 0, sigptr = c->shared_sigalgs;
3538 i < c->shared_sigalgslen; i++, sigptr++) {
3539 idx = tls12_get_pkey_idx(sigptr->rsign);
3540 if (idx > 0 && pmd[idx] == NULL) {
3541 md = tls12_get_hash(sigptr->rhash);
3543 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3544 if (idx == SSL_PKEY_RSA_SIGN) {
3545 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3546 pmd[SSL_PKEY_RSA_ENC] = md;
3552 * In strict mode leave unset digests as NULL to indicate we can't use
3553 * the certificate for signing.
3555 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3557 * Set any remaining keys to default values. NOTE: if alg is not
3558 * supported it stays as NULL.
3560 #ifndef OPENSSL_NO_DSA
3561 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3562 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3564 #ifndef OPENSSL_NO_RSA
3565 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3566 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3567 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3570 #ifndef OPENSSL_NO_EC
3571 if (pmd[SSL_PKEY_ECC] == NULL)
3572 pmd[SSL_PKEY_ECC] = EVP_sha1();
3574 # ifndef OPENSSL_NO_GOST
3575 if (pmd[SSL_PKEY_GOST01] == NULL)
3576 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3577 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3578 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3579 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3580 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3586 int SSL_get_sigalgs(SSL *s, int idx,
3587 int *psign, int *phash, int *psignhash,
3588 unsigned char *rsig, unsigned char *rhash)
3590 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3595 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3602 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3604 return s->s3->tmp.peer_sigalgslen / 2;
3607 int SSL_get_shared_sigalgs(SSL *s, int idx,
3608 int *psign, int *phash, int *psignhash,
3609 unsigned char *rsig, unsigned char *rhash)
3611 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3612 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3616 *phash = shsigalgs->hash_nid;
3618 *psign = shsigalgs->sign_nid;
3620 *psignhash = shsigalgs->signandhash_nid;
3622 *rsig = shsigalgs->rsign;
3624 *rhash = shsigalgs->rhash;
3625 return s->cert->shared_sigalgslen;
3628 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3632 int sigalgs[MAX_SIGALGLEN];
3635 static void get_sigorhash(int *psig, int *phash, const char *str)
3637 if (strcmp(str, "RSA") == 0) {
3638 *psig = EVP_PKEY_RSA;
3639 } else if (strcmp(str, "DSA") == 0) {
3640 *psig = EVP_PKEY_DSA;
3641 } else if (strcmp(str, "ECDSA") == 0) {
3642 *psig = EVP_PKEY_EC;
3644 *phash = OBJ_sn2nid(str);
3645 if (*phash == NID_undef)
3646 *phash = OBJ_ln2nid(str);
3650 static int sig_cb(const char *elem, int len, void *arg)
3652 sig_cb_st *sarg = arg;
3655 int sig_alg = NID_undef, hash_alg = NID_undef;
3658 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3660 if (len > (int)(sizeof(etmp) - 1))
3662 memcpy(etmp, elem, len);
3664 p = strchr(etmp, '+');
3672 get_sigorhash(&sig_alg, &hash_alg, etmp);
3673 get_sigorhash(&sig_alg, &hash_alg, p);
3675 if (sig_alg == NID_undef || hash_alg == NID_undef)
3678 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3679 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3682 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3683 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3688 * Set suppored signature algorithms based on a colon separated list of the
3689 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3691 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3695 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3699 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3702 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3705 unsigned char *sigalgs, *sptr;
3710 sigalgs = OPENSSL_malloc(salglen);
3711 if (sigalgs == NULL)
3713 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3714 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3715 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3717 if (rhash == -1 || rsign == -1)
3724 OPENSSL_free(c->client_sigalgs);
3725 c->client_sigalgs = sigalgs;
3726 c->client_sigalgslen = salglen;
3728 OPENSSL_free(c->conf_sigalgs);
3729 c->conf_sigalgs = sigalgs;
3730 c->conf_sigalgslen = salglen;
3736 OPENSSL_free(sigalgs);
3740 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3744 if (default_nid == -1)
3746 sig_nid = X509_get_signature_nid(x);
3748 return sig_nid == default_nid ? 1 : 0;
3749 for (i = 0; i < c->shared_sigalgslen; i++)
3750 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3755 /* Check to see if a certificate issuer name matches list of CA names */
3756 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3760 nm = X509_get_issuer_name(x);
3761 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3762 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3769 * Check certificate chain is consistent with TLS extensions and is usable by
3770 * server. This servers two purposes: it allows users to check chains before
3771 * passing them to the server and it allows the server to check chains before
3772 * attempting to use them.
3775 /* Flags which need to be set for a certificate when stict mode not set */
3777 #define CERT_PKEY_VALID_FLAGS \
3778 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3779 /* Strict mode flags */
3780 #define CERT_PKEY_STRICT_FLAGS \
3781 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3782 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3784 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3789 int check_flags = 0, strict_mode;
3790 CERT_PKEY *cpk = NULL;
3793 unsigned int suiteb_flags = tls1_suiteb(s);
3794 /* idx == -1 means checking server chains */
3796 /* idx == -2 means checking client certificate chains */
3799 idx = cpk - c->pkeys;
3801 cpk = c->pkeys + idx;
3802 pvalid = s->s3->tmp.valid_flags + idx;
3804 pk = cpk->privatekey;
3806 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3807 /* If no cert or key, forget it */
3813 idx = ssl_cert_type(x, pk);
3816 pvalid = s->s3->tmp.valid_flags + idx;
3818 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3819 check_flags = CERT_PKEY_STRICT_FLAGS;
3821 check_flags = CERT_PKEY_VALID_FLAGS;
3828 check_flags |= CERT_PKEY_SUITEB;
3829 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3830 if (ok == X509_V_OK)
3831 rv |= CERT_PKEY_SUITEB;
3832 else if (!check_flags)
3837 * Check all signature algorithms are consistent with signature
3838 * algorithms extension if TLS 1.2 or later and strict mode.
3840 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3842 unsigned char rsign = 0;
3843 if (s->s3->tmp.peer_sigalgs)
3845 /* If no sigalgs extension use defaults from RFC5246 */
3848 case SSL_PKEY_RSA_ENC:
3849 case SSL_PKEY_RSA_SIGN:
3850 rsign = TLSEXT_signature_rsa;
3851 default_nid = NID_sha1WithRSAEncryption;
3854 case SSL_PKEY_DSA_SIGN:
3855 rsign = TLSEXT_signature_dsa;
3856 default_nid = NID_dsaWithSHA1;
3860 rsign = TLSEXT_signature_ecdsa;
3861 default_nid = NID_ecdsa_with_SHA1;
3864 case SSL_PKEY_GOST01:
3865 rsign = TLSEXT_signature_gostr34102001;
3866 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3869 case SSL_PKEY_GOST12_256:
3870 rsign = TLSEXT_signature_gostr34102012_256;
3871 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3874 case SSL_PKEY_GOST12_512:
3875 rsign = TLSEXT_signature_gostr34102012_512;
3876 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3885 * If peer sent no signature algorithms extension and we have set
3886 * preferred signature algorithms check we support sha1.
3888 if (default_nid > 0 && c->conf_sigalgs) {
3890 const unsigned char *p = c->conf_sigalgs;
3891 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3892 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3895 if (j == c->conf_sigalgslen) {
3902 /* Check signature algorithm of each cert in chain */
3903 if (!tls1_check_sig_alg(c, x, default_nid)) {
3907 rv |= CERT_PKEY_EE_SIGNATURE;
3908 rv |= CERT_PKEY_CA_SIGNATURE;
3909 for (i = 0; i < sk_X509_num(chain); i++) {
3910 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3912 rv &= ~CERT_PKEY_CA_SIGNATURE;
3919 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3920 else if (check_flags)
3921 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3923 /* Check cert parameters are consistent */
3924 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3925 rv |= CERT_PKEY_EE_PARAM;
3926 else if (!check_flags)
3929 rv |= CERT_PKEY_CA_PARAM;
3930 /* In strict mode check rest of chain too */
3931 else if (strict_mode) {
3932 rv |= CERT_PKEY_CA_PARAM;
3933 for (i = 0; i < sk_X509_num(chain); i++) {
3934 X509 *ca = sk_X509_value(chain, i);
3935 if (!tls1_check_cert_param(s, ca, 0)) {
3937 rv &= ~CERT_PKEY_CA_PARAM;
3944 if (!s->server && strict_mode) {
3945 STACK_OF(X509_NAME) *ca_dn;
3947 switch (EVP_PKEY_id(pk)) {
3949 check_type = TLS_CT_RSA_SIGN;
3952 check_type = TLS_CT_DSS_SIGN;
3955 check_type = TLS_CT_ECDSA_SIGN;
3959 const unsigned char *ctypes;
3963 ctypelen = (int)c->ctype_num;
3965 ctypes = (unsigned char *)s->s3->tmp.ctype;
3966 ctypelen = s->s3->tmp.ctype_num;
3968 for (i = 0; i < ctypelen; i++) {
3969 if (ctypes[i] == check_type) {
3970 rv |= CERT_PKEY_CERT_TYPE;
3974 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3977 rv |= CERT_PKEY_CERT_TYPE;
3979 ca_dn = s->s3->tmp.ca_names;
3981 if (!sk_X509_NAME_num(ca_dn))
3982 rv |= CERT_PKEY_ISSUER_NAME;
3984 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3985 if (ssl_check_ca_name(ca_dn, x))
3986 rv |= CERT_PKEY_ISSUER_NAME;
3988 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3989 for (i = 0; i < sk_X509_num(chain); i++) {
3990 X509 *xtmp = sk_X509_value(chain, i);
3991 if (ssl_check_ca_name(ca_dn, xtmp)) {
3992 rv |= CERT_PKEY_ISSUER_NAME;
3997 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4000 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4002 if (!check_flags || (rv & check_flags) == check_flags)
4003 rv |= CERT_PKEY_VALID;
4007 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4008 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
4009 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4010 else if (s->s3->tmp.md[idx] != NULL)
4011 rv |= CERT_PKEY_SIGN;
4013 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4016 * When checking a CERT_PKEY structure all flags are irrelevant if the
4020 if (rv & CERT_PKEY_VALID)
4023 /* Preserve explicit sign flag, clear rest */
4024 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
4031 /* Set validity of certificates in an SSL structure */
4032 void tls1_set_cert_validity(SSL *s)
4034 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4035 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4036 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4037 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4038 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
4039 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
4040 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
4043 /* User level utiity function to check a chain is suitable */
4044 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4046 return tls1_check_chain(s, x, pk, chain, -1);
4050 #ifndef OPENSSL_NO_DH
4051 DH *ssl_get_auto_dh(SSL *s)
4053 int dh_secbits = 80;
4054 if (s->cert->dh_tmp_auto == 2)
4055 return DH_get_1024_160();
4056 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
4057 if (s->s3->tmp.new_cipher->strength_bits == 256)
4062 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4063 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4066 if (dh_secbits >= 128) {
4072 BN_set_word(dhp->g, 2);
4073 if (dh_secbits >= 192)
4074 dhp->p = get_rfc3526_prime_8192(NULL);
4076 dhp->p = get_rfc3526_prime_3072(NULL);
4077 if (dhp->p == NULL || dhp->g == NULL) {
4083 if (dh_secbits >= 112)
4084 return DH_get_2048_224();
4085 return DH_get_1024_160();
4089 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4092 EVP_PKEY *pkey = X509_get0_pubkey(x);
4095 * If no parameters this will return -1 and fail using the default
4096 * security callback for any non-zero security level. This will
4097 * reject keys which omit parameters but this only affects DSA and
4098 * omission of parameters is never (?) done in practice.
4100 secbits = EVP_PKEY_security_bits(pkey);
4103 return ssl_security(s, op, secbits, 0, x);
4105 return ssl_ctx_security(ctx, op, secbits, 0, x);
4108 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4110 /* Lookup signature algorithm digest */
4111 int secbits = -1, md_nid = NID_undef, sig_nid;
4112 /* Don't check signature if self signed */
4113 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4115 sig_nid = X509_get_signature_nid(x);
4116 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4118 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4119 secbits = EVP_MD_size(md) * 4;
4122 return ssl_security(s, op, secbits, md_nid, x);
4124 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4127 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4130 vfy = SSL_SECOP_PEER;
4132 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4133 return SSL_R_EE_KEY_TOO_SMALL;
4135 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4136 return SSL_R_CA_KEY_TOO_SMALL;
4138 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4139 return SSL_R_CA_MD_TOO_WEAK;
4144 * Check security of a chain, if sk includes the end entity certificate then
4145 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4146 * one to the peer. Return values: 1 if ok otherwise error code to use
4149 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4151 int rv, start_idx, i;
4153 x = sk_X509_value(sk, 0);
4158 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4162 for (i = start_idx; i < sk_X509_num(sk); i++) {
4163 x = sk_X509_value(sk, i);
4164 rv = ssl_security_cert(s, NULL, x, vfy, 0);