2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * 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 const char tls1_version_str
[] = "TLSv1" OPENSSL_VERSION_PTEXT
;
126 #ifndef OPENSSL_NO_TLSEXT
127 static int tls_decrypt_ticket(SSL
*s
, const unsigned char *tick
, int ticklen
,
128 const unsigned char *sess_id
, int sesslen
,
129 SSL_SESSION
**psess
);
130 static int ssl_check_clienthello_tlsext_early(SSL
*s
);
131 int ssl_check_serverhello_tlsext(SSL
*s
);
134 SSL3_ENC_METHOD
const TLSv1_enc_data
= {
137 tls1_setup_key_block
,
138 tls1_generate_master_secret
,
139 tls1_change_cipher_state
,
140 tls1_final_finish_mac
,
141 TLS1_FINISH_MAC_LENGTH
,
142 tls1_cert_verify_mac
,
143 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
144 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
146 tls1_export_keying_material
,
148 SSL3_HM_HEADER_LENGTH
,
149 ssl3_set_handshake_header
,
153 SSL3_ENC_METHOD
const TLSv1_1_enc_data
= {
156 tls1_setup_key_block
,
157 tls1_generate_master_secret
,
158 tls1_change_cipher_state
,
159 tls1_final_finish_mac
,
160 TLS1_FINISH_MAC_LENGTH
,
161 tls1_cert_verify_mac
,
162 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
163 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
165 tls1_export_keying_material
,
166 SSL_ENC_FLAG_EXPLICIT_IV
,
167 SSL3_HM_HEADER_LENGTH
,
168 ssl3_set_handshake_header
,
172 SSL3_ENC_METHOD
const TLSv1_2_enc_data
= {
175 tls1_setup_key_block
,
176 tls1_generate_master_secret
,
177 tls1_change_cipher_state
,
178 tls1_final_finish_mac
,
179 TLS1_FINISH_MAC_LENGTH
,
180 tls1_cert_verify_mac
,
181 TLS_MD_CLIENT_FINISH_CONST
, TLS_MD_CLIENT_FINISH_CONST_SIZE
,
182 TLS_MD_SERVER_FINISH_CONST
, TLS_MD_SERVER_FINISH_CONST_SIZE
,
184 tls1_export_keying_material
,
185 SSL_ENC_FLAG_EXPLICIT_IV
| SSL_ENC_FLAG_SIGALGS
| SSL_ENC_FLAG_SHA256_PRF
186 | SSL_ENC_FLAG_TLS1_2_CIPHERS
,
187 SSL3_HM_HEADER_LENGTH
,
188 ssl3_set_handshake_header
,
192 long tls1_default_timeout(void)
195 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
196 * http, the cache would over fill
198 return (60 * 60 * 2);
205 s
->method
->ssl_clear(s
);
209 void tls1_free(SSL
*s
)
211 #ifndef OPENSSL_NO_TLSEXT
212 OPENSSL_free(s
->tlsext_session_ticket
);
213 #endif /* OPENSSL_NO_TLSEXT */
217 void tls1_clear(SSL
*s
)
220 s
->version
= s
->method
->version
;
223 #ifndef OPENSSL_NO_EC
226 int nid
; /* Curve NID */
227 int secbits
; /* Bits of security (from SP800-57) */
228 unsigned int flags
; /* Flags: currently just field type */
231 # define TLS_CURVE_CHAR2 0x1
232 # define TLS_CURVE_PRIME 0x0
234 static const tls_curve_info nid_list
[] = {
235 {NID_sect163k1
, 80, TLS_CURVE_CHAR2
}, /* sect163k1 (1) */
236 {NID_sect163r1
, 80, TLS_CURVE_CHAR2
}, /* sect163r1 (2) */
237 {NID_sect163r2
, 80, TLS_CURVE_CHAR2
}, /* sect163r2 (3) */
238 {NID_sect193r1
, 80, TLS_CURVE_CHAR2
}, /* sect193r1 (4) */
239 {NID_sect193r2
, 80, TLS_CURVE_CHAR2
}, /* sect193r2 (5) */
240 {NID_sect233k1
, 112, TLS_CURVE_CHAR2
}, /* sect233k1 (6) */
241 {NID_sect233r1
, 112, TLS_CURVE_CHAR2
}, /* sect233r1 (7) */
242 {NID_sect239k1
, 112, TLS_CURVE_CHAR2
}, /* sect239k1 (8) */
243 {NID_sect283k1
, 128, TLS_CURVE_CHAR2
}, /* sect283k1 (9) */
244 {NID_sect283r1
, 128, TLS_CURVE_CHAR2
}, /* sect283r1 (10) */
245 {NID_sect409k1
, 192, TLS_CURVE_CHAR2
}, /* sect409k1 (11) */
246 {NID_sect409r1
, 192, TLS_CURVE_CHAR2
}, /* sect409r1 (12) */
247 {NID_sect571k1
, 256, TLS_CURVE_CHAR2
}, /* sect571k1 (13) */
248 {NID_sect571r1
, 256, TLS_CURVE_CHAR2
}, /* sect571r1 (14) */
249 {NID_secp160k1
, 80, TLS_CURVE_PRIME
}, /* secp160k1 (15) */
250 {NID_secp160r1
, 80, TLS_CURVE_PRIME
}, /* secp160r1 (16) */
251 {NID_secp160r2
, 80, TLS_CURVE_PRIME
}, /* secp160r2 (17) */
252 {NID_secp192k1
, 80, TLS_CURVE_PRIME
}, /* secp192k1 (18) */
253 {NID_X9_62_prime192v1
, 80, TLS_CURVE_PRIME
}, /* secp192r1 (19) */
254 {NID_secp224k1
, 112, TLS_CURVE_PRIME
}, /* secp224k1 (20) */
255 {NID_secp224r1
, 112, TLS_CURVE_PRIME
}, /* secp224r1 (21) */
256 {NID_secp256k1
, 128, TLS_CURVE_PRIME
}, /* secp256k1 (22) */
257 {NID_X9_62_prime256v1
, 128, TLS_CURVE_PRIME
}, /* secp256r1 (23) */
258 {NID_secp384r1
, 192, TLS_CURVE_PRIME
}, /* secp384r1 (24) */
259 {NID_secp521r1
, 256, TLS_CURVE_PRIME
}, /* secp521r1 (25) */
260 {NID_brainpoolP256r1
, 128, TLS_CURVE_PRIME
}, /* brainpoolP256r1 (26) */
261 {NID_brainpoolP384r1
, 192, TLS_CURVE_PRIME
}, /* brainpoolP384r1 (27) */
262 {NID_brainpoolP512r1
, 256, TLS_CURVE_PRIME
}, /* brainpool512r1 (28) */
265 static const unsigned char ecformats_default
[] = {
266 TLSEXT_ECPOINTFORMAT_uncompressed
,
267 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime
,
268 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
271 static const unsigned char eccurves_default
[] = {
272 0, 14, /* sect571r1 (14) */
273 0, 13, /* sect571k1 (13) */
274 0, 25, /* secp521r1 (25) */
275 0, 28, /* brainpool512r1 (28) */
276 0, 11, /* sect409k1 (11) */
277 0, 12, /* sect409r1 (12) */
278 0, 27, /* brainpoolP384r1 (27) */
279 0, 24, /* secp384r1 (24) */
280 0, 9, /* sect283k1 (9) */
281 0, 10, /* sect283r1 (10) */
282 0, 26, /* brainpoolP256r1 (26) */
283 0, 22, /* secp256k1 (22) */
284 0, 23, /* secp256r1 (23) */
285 0, 8, /* sect239k1 (8) */
286 0, 6, /* sect233k1 (6) */
287 0, 7, /* sect233r1 (7) */
288 0, 20, /* secp224k1 (20) */
289 0, 21, /* secp224r1 (21) */
290 0, 4, /* sect193r1 (4) */
291 0, 5, /* sect193r2 (5) */
292 0, 18, /* secp192k1 (18) */
293 0, 19, /* secp192r1 (19) */
294 0, 1, /* sect163k1 (1) */
295 0, 2, /* sect163r1 (2) */
296 0, 3, /* sect163r2 (3) */
297 0, 15, /* secp160k1 (15) */
298 0, 16, /* secp160r1 (16) */
299 0, 17, /* secp160r2 (17) */
302 static const unsigned char suiteb_curves
[] = {
303 0, TLSEXT_curve_P_256
,
304 0, TLSEXT_curve_P_384
307 int tls1_ec_curve_id2nid(int curve_id
)
309 /* ECC curves from RFC 4492 and RFC 7027 */
310 if ((curve_id
< 1) || ((unsigned int)curve_id
> OSSL_NELEM(nid_list
)))
312 return nid_list
[curve_id
- 1].nid
;
315 int tls1_ec_nid2curve_id(int nid
)
317 /* ECC curves from RFC 4492 and RFC 7027 */
319 case NID_sect163k1
: /* sect163k1 (1) */
321 case NID_sect163r1
: /* sect163r1 (2) */
323 case NID_sect163r2
: /* sect163r2 (3) */
325 case NID_sect193r1
: /* sect193r1 (4) */
327 case NID_sect193r2
: /* sect193r2 (5) */
329 case NID_sect233k1
: /* sect233k1 (6) */
331 case NID_sect233r1
: /* sect233r1 (7) */
333 case NID_sect239k1
: /* sect239k1 (8) */
335 case NID_sect283k1
: /* sect283k1 (9) */
337 case NID_sect283r1
: /* sect283r1 (10) */
339 case NID_sect409k1
: /* sect409k1 (11) */
341 case NID_sect409r1
: /* sect409r1 (12) */
343 case NID_sect571k1
: /* sect571k1 (13) */
345 case NID_sect571r1
: /* sect571r1 (14) */
347 case NID_secp160k1
: /* secp160k1 (15) */
349 case NID_secp160r1
: /* secp160r1 (16) */
351 case NID_secp160r2
: /* secp160r2 (17) */
353 case NID_secp192k1
: /* secp192k1 (18) */
355 case NID_X9_62_prime192v1
: /* secp192r1 (19) */
357 case NID_secp224k1
: /* secp224k1 (20) */
359 case NID_secp224r1
: /* secp224r1 (21) */
361 case NID_secp256k1
: /* secp256k1 (22) */
363 case NID_X9_62_prime256v1
: /* secp256r1 (23) */
365 case NID_secp384r1
: /* secp384r1 (24) */
367 case NID_secp521r1
: /* secp521r1 (25) */
369 case NID_brainpoolP256r1
: /* brainpoolP256r1 (26) */
371 case NID_brainpoolP384r1
: /* brainpoolP384r1 (27) */
373 case NID_brainpoolP512r1
: /* brainpool512r1 (28) */
381 * Get curves list, if "sess" is set return client curves otherwise
383 * Sets |num_curves| to the number of curves in the list, i.e.,
384 * the length of |pcurves| is 2 * num_curves.
385 * Returns 1 on success and 0 if the client curves list has invalid format.
386 * The latter indicates an internal error: we should not be accepting such
387 * lists in the first place.
388 * TODO(emilia): we should really be storing the curves list in explicitly
389 * parsed form instead. (However, this would affect binary compatibility
390 * so cannot happen in the 1.0.x series.)
392 static int tls1_get_curvelist(SSL
*s
, int sess
,
393 const unsigned char **pcurves
,
396 size_t pcurveslen
= 0;
398 *pcurves
= s
->session
->tlsext_ellipticcurvelist
;
399 pcurveslen
= s
->session
->tlsext_ellipticcurvelist_length
;
401 /* For Suite B mode only include P-256, P-384 */
402 switch (tls1_suiteb(s
)) {
403 case SSL_CERT_FLAG_SUITEB_128_LOS
:
404 *pcurves
= suiteb_curves
;
405 pcurveslen
= sizeof(suiteb_curves
);
408 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY
:
409 *pcurves
= suiteb_curves
;
413 case SSL_CERT_FLAG_SUITEB_192_LOS
:
414 *pcurves
= suiteb_curves
+ 2;
418 *pcurves
= s
->tlsext_ellipticcurvelist
;
419 pcurveslen
= s
->tlsext_ellipticcurvelist_length
;
422 *pcurves
= eccurves_default
;
423 pcurveslen
= sizeof(eccurves_default
);
427 /* We do not allow odd length arrays to enter the system. */
428 if (pcurveslen
& 1) {
429 SSLerr(SSL_F_TLS1_GET_CURVELIST
, ERR_R_INTERNAL_ERROR
);
433 *num_curves
= pcurveslen
/ 2;
438 /* See if curve is allowed by security callback */
439 static int tls_curve_allowed(SSL
*s
, const unsigned char *curve
, int op
)
441 const tls_curve_info
*cinfo
;
444 if ((curve
[1] < 1) || ((size_t)curve
[1] > OSSL_NELEM(nid_list
)))
446 cinfo
= &nid_list
[curve
[1] - 1];
447 # ifdef OPENSSL_NO_EC2M
448 if (cinfo
->flags
& TLS_CURVE_CHAR2
)
451 return ssl_security(s
, op
, cinfo
->secbits
, cinfo
->nid
, (void *)curve
);
454 /* Check a curve is one of our preferences */
455 int tls1_check_curve(SSL
*s
, const unsigned char *p
, size_t len
)
457 const unsigned char *curves
;
458 size_t num_curves
, i
;
459 unsigned int suiteb_flags
= tls1_suiteb(s
);
460 if (len
!= 3 || p
[0] != NAMED_CURVE_TYPE
)
462 /* Check curve matches Suite B preferences */
464 unsigned long cid
= s
->s3
->tmp
.new_cipher
->id
;
467 if (cid
== TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
) {
468 if (p
[2] != TLSEXT_curve_P_256
)
470 } else if (cid
== TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
) {
471 if (p
[2] != TLSEXT_curve_P_384
)
473 } else /* Should never happen */
476 if (!tls1_get_curvelist(s
, 0, &curves
, &num_curves
))
478 for (i
= 0; i
< num_curves
; i
++, curves
+= 2) {
479 if (p
[1] == curves
[0] && p
[2] == curves
[1])
480 return tls_curve_allowed(s
, p
+ 1, SSL_SECOP_CURVE_CHECK
);
486 * Return |nmatch|th shared curve or NID_undef if there is no match.
487 * For nmatch == -1, return number of matches
488 * For nmatch == -2, return the NID of the curve to use for
489 * an EC tmp key, or NID_undef if there is no match.
491 int tls1_shared_curve(SSL
*s
, int nmatch
)
493 const unsigned char *pref
, *supp
;
494 size_t num_pref
, num_supp
, i
, j
;
496 /* Can't do anything on client side */
500 if (tls1_suiteb(s
)) {
502 * For Suite B ciphersuite determines curve: we already know
503 * these are acceptable due to previous checks.
505 unsigned long cid
= s
->s3
->tmp
.new_cipher
->id
;
506 if (cid
== TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
)
507 return NID_X9_62_prime256v1
; /* P-256 */
508 if (cid
== TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
)
509 return NID_secp384r1
; /* P-384 */
510 /* Should never happen */
513 /* If not Suite B just return first preference shared curve */
517 * Avoid truncation. tls1_get_curvelist takes an int
518 * but s->options is a long...
520 if (!tls1_get_curvelist
521 (s
, (s
->options
& SSL_OP_CIPHER_SERVER_PREFERENCE
) != 0, &supp
,
523 /* In practice, NID_undef == 0 but let's be precise. */
524 return nmatch
== -1 ? 0 : NID_undef
;
525 if (!tls1_get_curvelist
526 (s
, !(s
->options
& SSL_OP_CIPHER_SERVER_PREFERENCE
), &pref
,
528 return nmatch
== -1 ? 0 : NID_undef
;
530 for (i
= 0; i
< num_pref
; i
++, pref
+= 2) {
531 const unsigned char *tsupp
= supp
;
532 for (j
= 0; j
< num_supp
; j
++, tsupp
+= 2) {
533 if (pref
[0] == tsupp
[0] && pref
[1] == tsupp
[1]) {
534 if (!tls_curve_allowed(s
, pref
, SSL_SECOP_CURVE_SHARED
))
537 int id
= (pref
[0] << 8) | pref
[1];
538 return tls1_ec_curve_id2nid(id
);
546 /* Out of range (nmatch > k). */
550 int tls1_set_curves(unsigned char **pext
, size_t *pextlen
,
551 int *curves
, size_t ncurves
)
553 unsigned char *clist
, *p
;
556 * Bitmap of curves included to detect duplicates: only works while curve
559 unsigned long dup_list
= 0;
560 clist
= OPENSSL_malloc(ncurves
* 2);
563 for (i
= 0, p
= clist
; i
< ncurves
; i
++) {
564 unsigned long idmask
;
566 id
= tls1_ec_nid2curve_id(curves
[i
]);
568 if (!id
|| (dup_list
& idmask
)) {
577 *pextlen
= ncurves
* 2;
581 # define MAX_CURVELIST 28
585 int nid_arr
[MAX_CURVELIST
];
588 static int nid_cb(const char *elem
, int len
, void *arg
)
590 nid_cb_st
*narg
= arg
;
596 if (narg
->nidcnt
== MAX_CURVELIST
)
598 if (len
> (int)(sizeof(etmp
) - 1))
600 memcpy(etmp
, elem
, len
);
602 nid
= EC_curve_nist2nid(etmp
);
603 if (nid
== NID_undef
)
604 nid
= OBJ_sn2nid(etmp
);
605 if (nid
== NID_undef
)
606 nid
= OBJ_ln2nid(etmp
);
607 if (nid
== NID_undef
)
609 for (i
= 0; i
< narg
->nidcnt
; i
++)
610 if (narg
->nid_arr
[i
] == nid
)
612 narg
->nid_arr
[narg
->nidcnt
++] = nid
;
616 /* Set curves based on a colon separate list */
617 int tls1_set_curves_list(unsigned char **pext
, size_t *pextlen
,
622 if (!CONF_parse_list(str
, ':', 1, nid_cb
, &ncb
))
626 return tls1_set_curves(pext
, pextlen
, ncb
.nid_arr
, ncb
.nidcnt
);
629 /* For an EC key set TLS id and required compression based on parameters */
630 static int tls1_set_ec_id(unsigned char *curve_id
, unsigned char *comp_id
,
635 const EC_METHOD
*meth
;
638 /* Determine if it is a prime field */
639 grp
= EC_KEY_get0_group(ec
);
642 meth
= EC_GROUP_method_of(grp
);
645 if (EC_METHOD_get_field_type(meth
) == NID_X9_62_prime_field
)
649 /* Determine curve ID */
650 id
= EC_GROUP_get_curve_name(grp
);
651 id
= tls1_ec_nid2curve_id(id
);
652 /* If we have an ID set it, otherwise set arbitrary explicit curve */
655 curve_id
[1] = (unsigned char)id
;
664 if (EC_KEY_get0_public_key(ec
) == NULL
)
666 if (EC_KEY_get_conv_form(ec
) == POINT_CONVERSION_COMPRESSED
) {
668 *comp_id
= TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime
;
670 *comp_id
= TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
;
672 *comp_id
= TLSEXT_ECPOINTFORMAT_uncompressed
;
677 /* Check an EC key is compatible with extensions */
678 static int tls1_check_ec_key(SSL
*s
,
679 unsigned char *curve_id
, unsigned char *comp_id
)
681 const unsigned char *pformats
, *pcurves
;
682 size_t num_formats
, num_curves
, i
;
685 * If point formats extension present check it, otherwise everything is
686 * supported (see RFC4492).
688 if (comp_id
&& s
->session
->tlsext_ecpointformatlist
) {
689 pformats
= s
->session
->tlsext_ecpointformatlist
;
690 num_formats
= s
->session
->tlsext_ecpointformatlist_length
;
691 for (i
= 0; i
< num_formats
; i
++, pformats
++) {
692 if (*comp_id
== *pformats
)
695 if (i
== num_formats
)
700 /* Check curve is consistent with client and server preferences */
701 for (j
= 0; j
<= 1; j
++) {
702 if (!tls1_get_curvelist(s
, j
, &pcurves
, &num_curves
))
704 if (j
== 1 && num_curves
== 0) {
706 * If we've not received any curves then skip this check.
707 * RFC 4492 does not require the supported elliptic curves extension
708 * so if it is not sent we can just choose any curve.
709 * It is invalid to send an empty list in the elliptic curves
710 * extension, so num_curves == 0 always means no extension.
714 for (i
= 0; i
< num_curves
; i
++, pcurves
+= 2) {
715 if (pcurves
[0] == curve_id
[0] && pcurves
[1] == curve_id
[1])
720 /* For clients can only check sent curve list */
727 static void tls1_get_formatlist(SSL
*s
, const unsigned char **pformats
,
731 * If we have a custom point format list use it otherwise use default
733 if (s
->tlsext_ecpointformatlist
) {
734 *pformats
= s
->tlsext_ecpointformatlist
;
735 *num_formats
= s
->tlsext_ecpointformatlist_length
;
737 *pformats
= ecformats_default
;
738 /* For Suite B we don't support char2 fields */
740 *num_formats
= sizeof(ecformats_default
) - 1;
742 *num_formats
= sizeof(ecformats_default
);
747 * Check cert parameters compatible with extensions: currently just checks EC
748 * certificates have compatible curves and compression.
750 static int tls1_check_cert_param(SSL
*s
, X509
*x
, int set_ee_md
)
752 unsigned char comp_id
, curve_id
[2];
755 pkey
= X509_get_pubkey(x
);
758 /* If not EC nothing to do */
759 if (pkey
->type
!= EVP_PKEY_EC
) {
763 rv
= tls1_set_ec_id(curve_id
, &comp_id
, pkey
->pkey
.ec
);
768 * Can't check curve_id for client certs as we don't have a supported
771 rv
= tls1_check_ec_key(s
, s
->server
? curve_id
: NULL
, &comp_id
);
775 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
776 * SHA384+P-384, adjust digest if necessary.
778 if (set_ee_md
&& tls1_suiteb(s
)) {
784 /* Check to see we have necessary signing algorithm */
785 if (curve_id
[1] == TLSEXT_curve_P_256
)
786 check_md
= NID_ecdsa_with_SHA256
;
787 else if (curve_id
[1] == TLSEXT_curve_P_384
)
788 check_md
= NID_ecdsa_with_SHA384
;
790 return 0; /* Should never happen */
791 for (i
= 0; i
< c
->shared_sigalgslen
; i
++)
792 if (check_md
== c
->shared_sigalgs
[i
].signandhash_nid
)
794 if (i
== c
->shared_sigalgslen
)
796 if (set_ee_md
== 2) {
797 if (check_md
== NID_ecdsa_with_SHA256
)
798 c
->pkeys
[SSL_PKEY_ECC
].digest
= EVP_sha256();
800 c
->pkeys
[SSL_PKEY_ECC
].digest
= EVP_sha384();
806 # ifndef OPENSSL_NO_EC
807 /* Check EC temporary key is compatible with client extensions */
808 int tls1_check_ec_tmp_key(SSL
*s
, unsigned long cid
)
810 unsigned char curve_id
[2];
811 EC_KEY
*ec
= s
->cert
->ecdh_tmp
;
812 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
813 /* Allow any curve: not just those peer supports */
814 if (s
->cert
->cert_flags
& SSL_CERT_FLAG_BROKEN_PROTOCOL
)
818 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
821 if (tls1_suiteb(s
)) {
822 /* Curve to check determined by ciphersuite */
823 if (cid
== TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
)
824 curve_id
[1] = TLSEXT_curve_P_256
;
825 else if (cid
== TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
)
826 curve_id
[1] = TLSEXT_curve_P_384
;
830 /* Check this curve is acceptable */
831 if (!tls1_check_ec_key(s
, curve_id
, NULL
))
833 /* If auto or setting curve from callback assume OK */
834 if (s
->cert
->ecdh_tmp_auto
|| s
->cert
->ecdh_tmp_cb
)
836 /* Otherwise check curve is acceptable */
838 unsigned char curve_tmp
[2];
841 if (!tls1_set_ec_id(curve_tmp
, NULL
, ec
))
843 if (!curve_tmp
[0] || curve_tmp
[1] == curve_id
[1])
849 if (s
->cert
->ecdh_tmp_auto
) {
850 /* Need a shared curve */
851 if (tls1_shared_curve(s
, 0))
857 if (s
->cert
->ecdh_tmp_cb
)
862 if (!tls1_set_ec_id(curve_id
, NULL
, ec
))
864 /* Set this to allow use of invalid curves for testing */
868 return tls1_check_ec_key(s
, curve_id
, NULL
);
871 # endif /* OPENSSL_NO_EC */
875 static int tls1_check_cert_param(SSL
*s
, X509
*x
, int set_ee_md
)
880 #endif /* OPENSSL_NO_EC */
882 #ifndef OPENSSL_NO_TLSEXT
885 * List of supported signature algorithms and hashes. Should make this
886 * customisable at some point, for now include everything we support.
889 # ifdef OPENSSL_NO_RSA
890 # define tlsext_sigalg_rsa(md) /* */
892 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
895 # ifdef OPENSSL_NO_DSA
896 # define tlsext_sigalg_dsa(md) /* */
898 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
901 # ifdef OPENSSL_NO_EC
902 # define tlsext_sigalg_ecdsa(md) /* */
904 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
907 # define tlsext_sigalg(md) \
908 tlsext_sigalg_rsa(md) \
909 tlsext_sigalg_dsa(md) \
910 tlsext_sigalg_ecdsa(md)
912 static const unsigned char tls12_sigalgs
[] = {
913 tlsext_sigalg(TLSEXT_hash_sha512
)
914 tlsext_sigalg(TLSEXT_hash_sha384
)
915 tlsext_sigalg(TLSEXT_hash_sha256
)
916 tlsext_sigalg(TLSEXT_hash_sha224
)
917 tlsext_sigalg(TLSEXT_hash_sha1
)
920 # ifndef OPENSSL_NO_EC
921 static const unsigned char suiteb_sigalgs
[] = {
922 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256
)
923 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384
)
926 size_t tls12_get_psigalgs(SSL
*s
, const unsigned char **psigs
)
929 * If Suite B mode use Suite B sigalgs only, ignore any other
932 # ifndef OPENSSL_NO_EC
933 switch (tls1_suiteb(s
)) {
934 case SSL_CERT_FLAG_SUITEB_128_LOS
:
935 *psigs
= suiteb_sigalgs
;
936 return sizeof(suiteb_sigalgs
);
938 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY
:
939 *psigs
= suiteb_sigalgs
;
942 case SSL_CERT_FLAG_SUITEB_192_LOS
:
943 *psigs
= suiteb_sigalgs
+ 2;
947 /* If server use client authentication sigalgs if not NULL */
948 if (s
->server
&& s
->cert
->client_sigalgs
) {
949 *psigs
= s
->cert
->client_sigalgs
;
950 return s
->cert
->client_sigalgslen
;
951 } else if (s
->cert
->conf_sigalgs
) {
952 *psigs
= s
->cert
->conf_sigalgs
;
953 return s
->cert
->conf_sigalgslen
;
955 *psigs
= tls12_sigalgs
;
956 return sizeof(tls12_sigalgs
);
961 * Check signature algorithm is consistent with sent supported signature
962 * algorithms and if so return relevant digest.
964 int tls12_check_peer_sigalg(const EVP_MD
**pmd
, SSL
*s
,
965 const unsigned char *sig
, EVP_PKEY
*pkey
)
967 const unsigned char *sent_sigs
;
968 size_t sent_sigslen
, i
;
969 int sigalg
= tls12_get_sigid(pkey
);
970 /* Should never happen */
973 /* Check key type is consistent with signature */
974 if (sigalg
!= (int)sig
[1]) {
975 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_WRONG_SIGNATURE_TYPE
);
978 # ifndef OPENSSL_NO_EC
979 if (pkey
->type
== EVP_PKEY_EC
) {
980 unsigned char curve_id
[2], comp_id
;
981 /* Check compression and curve matches extensions */
982 if (!tls1_set_ec_id(curve_id
, &comp_id
, pkey
->pkey
.ec
))
984 if (!s
->server
&& !tls1_check_ec_key(s
, curve_id
, &comp_id
)) {
985 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_WRONG_CURVE
);
988 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
989 if (tls1_suiteb(s
)) {
992 if (curve_id
[1] == TLSEXT_curve_P_256
) {
993 if (sig
[0] != TLSEXT_hash_sha256
) {
994 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
,
995 SSL_R_ILLEGAL_SUITEB_DIGEST
);
998 } else if (curve_id
[1] == TLSEXT_curve_P_384
) {
999 if (sig
[0] != TLSEXT_hash_sha384
) {
1000 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
,
1001 SSL_R_ILLEGAL_SUITEB_DIGEST
);
1007 } else if (tls1_suiteb(s
))
1011 /* Check signature matches a type we sent */
1012 sent_sigslen
= tls12_get_psigalgs(s
, &sent_sigs
);
1013 for (i
= 0; i
< sent_sigslen
; i
+= 2, sent_sigs
+= 2) {
1014 if (sig
[0] == sent_sigs
[0] && sig
[1] == sent_sigs
[1])
1017 /* Allow fallback to SHA1 if not strict mode */
1018 if (i
== sent_sigslen
1019 && (sig
[0] != TLSEXT_hash_sha1
1020 || s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
)) {
1021 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_WRONG_SIGNATURE_TYPE
);
1024 *pmd
= tls12_get_hash(sig
[0]);
1026 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_UNKNOWN_DIGEST
);
1029 /* Make sure security callback allows algorithm */
1030 if (!ssl_security(s
, SSL_SECOP_SIGALG_CHECK
,
1031 EVP_MD_size(*pmd
) * 4, EVP_MD_type(*pmd
),
1033 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG
, SSL_R_WRONG_SIGNATURE_TYPE
);
1037 * Store the digest used so applications can retrieve it if they wish.
1039 if (s
->session
&& s
->session
->sess_cert
)
1040 s
->session
->sess_cert
->peer_key
->digest
= *pmd
;
1045 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
1046 * supported or doesn't appear in supported signature algorithms. Unlike
1047 * ssl_cipher_get_disabled this applies to a specific session and not global
1050 void ssl_set_client_disabled(SSL
*s
)
1055 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1056 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s
))
1057 c
->mask_ssl
= SSL_TLSV1_2
;
1060 ssl_set_sig_mask(&c
->mask_a
, s
, SSL_SECOP_SIGALG_MASK
);
1062 * Disable static DH if we don't include any appropriate signature
1065 if (c
->mask_a
& SSL_aRSA
)
1066 c
->mask_k
|= SSL_kDHr
| SSL_kECDHr
;
1067 if (c
->mask_a
& SSL_aDSS
)
1068 c
->mask_k
|= SSL_kDHd
;
1069 if (c
->mask_a
& SSL_aECDSA
)
1070 c
->mask_k
|= SSL_kECDHe
;
1071 # ifndef OPENSSL_NO_KRB5
1072 if (!kssl_tgt_is_available(s
->kssl_ctx
)) {
1073 c
->mask_a
|= SSL_aKRB5
;
1074 c
->mask_k
|= SSL_kKRB5
;
1077 # ifndef OPENSSL_NO_PSK
1078 /* with PSK there must be client callback set */
1079 if (!s
->psk_client_callback
) {
1080 c
->mask_a
|= SSL_aPSK
;
1081 c
->mask_k
|= SSL_kPSK
;
1083 # endif /* OPENSSL_NO_PSK */
1084 # ifndef OPENSSL_NO_SRP
1085 if (!(s
->srp_ctx
.srp_Mask
& SSL_kSRP
)) {
1086 c
->mask_a
|= SSL_aSRP
;
1087 c
->mask_k
|= SSL_kSRP
;
1093 int ssl_cipher_disabled(SSL
*s
, const SSL_CIPHER
*c
, int op
)
1096 if (c
->algorithm_ssl
& ct
->mask_ssl
|| c
->algorithm_mkey
& ct
->mask_k
1097 || c
->algorithm_auth
& ct
->mask_a
)
1099 return !ssl_security(s
, op
, c
->strength_bits
, 0, (void *)c
);
1102 static int tls_use_ticket(SSL
*s
)
1104 if (s
->options
& SSL_OP_NO_TICKET
)
1106 return ssl_security(s
, SSL_SECOP_TICKET
, 0, 0, NULL
);
1109 unsigned char *ssl_add_clienthello_tlsext(SSL
*s
, unsigned char *buf
,
1110 unsigned char *limit
, int *al
)
1113 unsigned char *orig
= buf
;
1114 unsigned char *ret
= buf
;
1115 # ifndef OPENSSL_NO_EC
1116 /* See if we support any ECC ciphersuites */
1118 if (s
->version
>= TLS1_VERSION
|| SSL_IS_DTLS(s
)) {
1120 unsigned long alg_k
, alg_a
;
1121 STACK_OF(SSL_CIPHER
) *cipher_stack
= SSL_get_ciphers(s
);
1123 for (i
= 0; i
< sk_SSL_CIPHER_num(cipher_stack
); i
++) {
1124 SSL_CIPHER
*c
= sk_SSL_CIPHER_value(cipher_stack
, i
);
1126 alg_k
= c
->algorithm_mkey
;
1127 alg_a
= c
->algorithm_auth
;
1128 if ((alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
)
1129 || (alg_a
& SSL_aECDSA
))) {
1140 return NULL
; /* this really never occurs, but ... */
1142 /* Add RI if renegotiating */
1143 if (s
->renegotiate
) {
1146 if (!ssl_add_clienthello_renegotiate_ext(s
, 0, &el
, 0)) {
1147 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1151 if ((limit
- ret
- 4 - el
) < 0)
1154 s2n(TLSEXT_TYPE_renegotiate
, ret
);
1157 if (!ssl_add_clienthello_renegotiate_ext(s
, ret
, &el
, el
)) {
1158 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1164 /* Only add RI for SSLv3 */
1165 if (s
->client_version
== SSL3_VERSION
)
1168 if (s
->tlsext_hostname
!= NULL
) {
1169 /* Add TLS extension servername to the Client Hello message */
1170 unsigned long size_str
;
1174 * check for enough space.
1175 * 4 for the servername type and entension length
1176 * 2 for servernamelist length
1177 * 1 for the hostname type
1178 * 2 for hostname length
1182 if ((lenmax
= limit
- ret
- 9) < 0
1184 strlen(s
->tlsext_hostname
)) > (unsigned long)lenmax
)
1187 /* extension type and length */
1188 s2n(TLSEXT_TYPE_server_name
, ret
);
1189 s2n(size_str
+ 5, ret
);
1191 /* length of servername list */
1192 s2n(size_str
+ 3, ret
);
1194 /* hostname type, length and hostname */
1195 *(ret
++) = (unsigned char)TLSEXT_NAMETYPE_host_name
;
1197 memcpy(ret
, s
->tlsext_hostname
, size_str
);
1200 # ifndef OPENSSL_NO_SRP
1201 /* Add SRP username if there is one */
1202 if (s
->srp_ctx
.login
!= NULL
) { /* Add TLS extension SRP username to the
1203 * Client Hello message */
1205 int login_len
= strlen(s
->srp_ctx
.login
);
1206 if (login_len
> 255 || login_len
== 0) {
1207 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1212 * check for enough space.
1213 * 4 for the srp type type and entension length
1214 * 1 for the srp user identity
1215 * + srp user identity length
1217 if ((limit
- ret
- 5 - login_len
) < 0)
1220 /* fill in the extension */
1221 s2n(TLSEXT_TYPE_srp
, ret
);
1222 s2n(login_len
+ 1, ret
);
1223 (*ret
++) = (unsigned char)login_len
;
1224 memcpy(ret
, s
->srp_ctx
.login
, login_len
);
1229 # ifndef OPENSSL_NO_EC
1232 * Add TLS extension ECPointFormats to the ClientHello message
1235 const unsigned char *pcurves
, *pformats
;
1236 size_t num_curves
, num_formats
, curves_list_len
;
1238 unsigned char *etmp
;
1240 tls1_get_formatlist(s
, &pformats
, &num_formats
);
1242 if ((lenmax
= limit
- ret
- 5) < 0)
1244 if (num_formats
> (size_t)lenmax
)
1246 if (num_formats
> 255) {
1247 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1251 s2n(TLSEXT_TYPE_ec_point_formats
, ret
);
1252 /* The point format list has 1-byte length. */
1253 s2n(num_formats
+ 1, ret
);
1254 *(ret
++) = (unsigned char)num_formats
;
1255 memcpy(ret
, pformats
, num_formats
);
1259 * Add TLS extension EllipticCurves to the ClientHello message
1261 pcurves
= s
->tlsext_ellipticcurvelist
;
1262 if (!tls1_get_curvelist(s
, 0, &pcurves
, &num_curves
))
1265 if ((lenmax
= limit
- ret
- 6) < 0)
1267 if (num_curves
> (size_t)lenmax
/ 2)
1269 if (num_curves
> 65532 / 2) {
1270 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1274 s2n(TLSEXT_TYPE_elliptic_curves
, ret
);
1276 /* Copy curve ID if supported */
1277 for (i
= 0; i
< num_curves
; i
++, pcurves
+= 2) {
1278 if (tls_curve_allowed(s
, pcurves
, SSL_SECOP_CURVE_SUPPORTED
)) {
1279 *etmp
++ = pcurves
[0];
1280 *etmp
++ = pcurves
[1];
1284 curves_list_len
= etmp
- ret
- 4;
1286 s2n(curves_list_len
+ 2, ret
);
1287 s2n(curves_list_len
, ret
);
1288 ret
+= curves_list_len
;
1290 # endif /* OPENSSL_NO_EC */
1292 if (tls_use_ticket(s
)) {
1294 if (!s
->new_session
&& s
->session
&& s
->session
->tlsext_tick
)
1295 ticklen
= s
->session
->tlsext_ticklen
;
1296 else if (s
->session
&& s
->tlsext_session_ticket
&&
1297 s
->tlsext_session_ticket
->data
) {
1298 ticklen
= s
->tlsext_session_ticket
->length
;
1299 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
1300 if (!s
->session
->tlsext_tick
)
1302 memcpy(s
->session
->tlsext_tick
,
1303 s
->tlsext_session_ticket
->data
, ticklen
);
1304 s
->session
->tlsext_ticklen
= ticklen
;
1307 if (ticklen
== 0 && s
->tlsext_session_ticket
&&
1308 s
->tlsext_session_ticket
->data
== NULL
)
1311 * Check for enough room 2 for extension type, 2 for len rest for
1314 if ((long)(limit
- ret
- 4 - ticklen
) < 0)
1316 s2n(TLSEXT_TYPE_session_ticket
, ret
);
1319 memcpy(ret
, s
->session
->tlsext_tick
, ticklen
);
1325 if (SSL_USE_SIGALGS(s
)) {
1327 const unsigned char *salg
;
1328 unsigned char *etmp
;
1329 salglen
= tls12_get_psigalgs(s
, &salg
);
1330 if ((size_t)(limit
- ret
) < salglen
+ 6)
1332 s2n(TLSEXT_TYPE_signature_algorithms
, ret
);
1334 /* Skip over lengths for now */
1336 salglen
= tls12_copy_sigalgs(s
, ret
, salg
, salglen
);
1337 /* Fill in lengths */
1338 s2n(salglen
+ 2, etmp
);
1343 if (s
->tlsext_status_type
== TLSEXT_STATUSTYPE_ocsp
) {
1345 long extlen
, idlen
, itmp
;
1349 for (i
= 0; i
< sk_OCSP_RESPID_num(s
->tlsext_ocsp_ids
); i
++) {
1350 id
= sk_OCSP_RESPID_value(s
->tlsext_ocsp_ids
, i
);
1351 itmp
= i2d_OCSP_RESPID(id
, NULL
);
1357 if (s
->tlsext_ocsp_exts
) {
1358 extlen
= i2d_X509_EXTENSIONS(s
->tlsext_ocsp_exts
, NULL
);
1364 if ((long)(limit
- ret
- 7 - extlen
- idlen
) < 0)
1366 s2n(TLSEXT_TYPE_status_request
, ret
);
1367 if (extlen
+ idlen
> 0xFFF0)
1369 s2n(extlen
+ idlen
+ 5, ret
);
1370 *(ret
++) = TLSEXT_STATUSTYPE_ocsp
;
1372 for (i
= 0; i
< sk_OCSP_RESPID_num(s
->tlsext_ocsp_ids
); i
++) {
1373 /* save position of id len */
1374 unsigned char *q
= ret
;
1375 id
= sk_OCSP_RESPID_value(s
->tlsext_ocsp_ids
, i
);
1376 /* skip over id len */
1378 itmp
= i2d_OCSP_RESPID(id
, &ret
);
1384 i2d_X509_EXTENSIONS(s
->tlsext_ocsp_exts
, &ret
);
1386 # ifndef OPENSSL_NO_HEARTBEATS
1387 /* Add Heartbeat extension */
1388 if ((limit
- ret
- 4 - 1) < 0)
1390 s2n(TLSEXT_TYPE_heartbeat
, ret
);
1394 * 1: peer may send requests
1395 * 2: peer not allowed to send requests
1397 if (s
->tlsext_heartbeat
& SSL_TLSEXT_HB_DONT_RECV_REQUESTS
)
1398 *(ret
++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS
;
1400 *(ret
++) = SSL_TLSEXT_HB_ENABLED
;
1403 # ifndef OPENSSL_NO_NEXTPROTONEG
1404 if (s
->ctx
->next_proto_select_cb
&& !s
->s3
->tmp
.finish_md_len
) {
1406 * The client advertises an emtpy extension to indicate its support
1407 * for Next Protocol Negotiation
1409 if (limit
- ret
- 4 < 0)
1411 s2n(TLSEXT_TYPE_next_proto_neg
, ret
);
1416 if (s
->alpn_client_proto_list
&& !s
->s3
->tmp
.finish_md_len
) {
1417 if ((size_t)(limit
- ret
) < 6 + s
->alpn_client_proto_list_len
)
1419 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation
, ret
);
1420 s2n(2 + s
->alpn_client_proto_list_len
, ret
);
1421 s2n(s
->alpn_client_proto_list_len
, ret
);
1422 memcpy(ret
, s
->alpn_client_proto_list
, s
->alpn_client_proto_list_len
);
1423 ret
+= s
->alpn_client_proto_list_len
;
1425 # ifndef OPENSSL_NO_SRTP
1426 if (SSL_IS_DTLS(s
) && SSL_get_srtp_profiles(s
)) {
1429 /* Returns 0 on success!! */
1430 if (ssl_add_clienthello_use_srtp_ext(s
, 0, &el
, 0)) {
1431 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1435 if ((limit
- ret
- 4 - el
) < 0)
1438 s2n(TLSEXT_TYPE_use_srtp
, ret
);
1441 if (ssl_add_clienthello_use_srtp_ext(s
, ret
, &el
, el
)) {
1442 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1448 custom_ext_init(&s
->cert
->cli_ext
);
1449 /* Add custom TLS Extensions to ClientHello */
1450 if (!custom_ext_add(s
, 0, &ret
, limit
, al
))
1452 # ifdef TLSEXT_TYPE_encrypt_then_mac
1453 s2n(TLSEXT_TYPE_encrypt_then_mac
, ret
);
1456 s2n(TLSEXT_TYPE_extended_master_secret
, ret
);
1460 * Add padding to workaround bugs in F5 terminators. See
1461 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1462 * code works out the length of all existing extensions it MUST always
1465 if (s
->options
& SSL_OP_TLSEXT_PADDING
) {
1466 int hlen
= ret
- (unsigned char *)s
->init_buf
->data
;
1468 * The code in s23_clnt.c to build ClientHello messages includes the
1469 * 5-byte record header in the buffer, while the code in s3_clnt.c
1472 if (s
->state
== SSL23_ST_CW_CLNT_HELLO_A
)
1474 if (hlen
> 0xff && hlen
< 0x200) {
1475 hlen
= 0x200 - hlen
;
1481 s2n(TLSEXT_TYPE_padding
, ret
);
1483 memset(ret
, 0, hlen
);
1490 if ((extdatalen
= ret
- orig
- 2) == 0)
1493 s2n(extdatalen
, orig
);
1497 unsigned char *ssl_add_serverhello_tlsext(SSL
*s
, unsigned char *buf
,
1498 unsigned char *limit
, int *al
)
1501 unsigned char *orig
= buf
;
1502 unsigned char *ret
= buf
;
1503 # ifndef OPENSSL_NO_NEXTPROTONEG
1504 int next_proto_neg_seen
;
1506 # ifndef OPENSSL_NO_EC
1507 unsigned long alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1508 unsigned long alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1509 int using_ecc
= (alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
))
1510 || (alg_a
& SSL_aECDSA
);
1511 using_ecc
= using_ecc
&& (s
->session
->tlsext_ecpointformatlist
!= NULL
);
1516 return NULL
; /* this really never occurs, but ... */
1518 if (s
->s3
->send_connection_binding
) {
1521 if (!ssl_add_serverhello_renegotiate_ext(s
, 0, &el
, 0)) {
1522 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1526 if ((limit
- ret
- 4 - el
) < 0)
1529 s2n(TLSEXT_TYPE_renegotiate
, ret
);
1532 if (!ssl_add_serverhello_renegotiate_ext(s
, ret
, &el
, el
)) {
1533 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1540 /* Only add RI for SSLv3 */
1541 if (s
->version
== SSL3_VERSION
)
1544 if (!s
->hit
&& s
->servername_done
== 1
1545 && s
->session
->tlsext_hostname
!= NULL
) {
1546 if ((long)(limit
- ret
- 4) < 0)
1549 s2n(TLSEXT_TYPE_server_name
, ret
);
1552 # ifndef OPENSSL_NO_EC
1554 const unsigned char *plist
;
1557 * Add TLS extension ECPointFormats to the ServerHello message
1561 tls1_get_formatlist(s
, &plist
, &plistlen
);
1563 if ((lenmax
= limit
- ret
- 5) < 0)
1565 if (plistlen
> (size_t)lenmax
)
1567 if (plistlen
> 255) {
1568 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1572 s2n(TLSEXT_TYPE_ec_point_formats
, ret
);
1573 s2n(plistlen
+ 1, ret
);
1574 *(ret
++) = (unsigned char)plistlen
;
1575 memcpy(ret
, plist
, plistlen
);
1580 * Currently the server should not respond with a SupportedCurves
1583 # endif /* OPENSSL_NO_EC */
1585 if (s
->tlsext_ticket_expected
&& tls_use_ticket(s
)) {
1586 if ((long)(limit
- ret
- 4) < 0)
1588 s2n(TLSEXT_TYPE_session_ticket
, ret
);
1592 if (s
->tlsext_status_expected
) {
1593 if ((long)(limit
- ret
- 4) < 0)
1595 s2n(TLSEXT_TYPE_status_request
, ret
);
1599 # ifndef OPENSSL_NO_SRTP
1600 if (SSL_IS_DTLS(s
) && s
->srtp_profile
) {
1603 /* Returns 0 on success!! */
1604 if (ssl_add_serverhello_use_srtp_ext(s
, 0, &el
, 0)) {
1605 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1608 if ((limit
- ret
- 4 - el
) < 0)
1611 s2n(TLSEXT_TYPE_use_srtp
, ret
);
1614 if (ssl_add_serverhello_use_srtp_ext(s
, ret
, &el
, el
)) {
1615 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT
, ERR_R_INTERNAL_ERROR
);
1622 if (((s
->s3
->tmp
.new_cipher
->id
& 0xFFFF) == 0x80
1623 || (s
->s3
->tmp
.new_cipher
->id
& 0xFFFF) == 0x81)
1624 && (SSL_get_options(s
) & SSL_OP_CRYPTOPRO_TLSEXT_BUG
)) {
1625 const unsigned char cryptopro_ext
[36] = {
1626 0xfd, 0xe8, /* 65000 */
1627 0x00, 0x20, /* 32 bytes length */
1628 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1629 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1630 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1631 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1633 if (limit
- ret
< 36)
1635 memcpy(ret
, cryptopro_ext
, 36);
1639 # ifndef OPENSSL_NO_HEARTBEATS
1640 /* Add Heartbeat extension if we've received one */
1641 if (s
->tlsext_heartbeat
& SSL_TLSEXT_HB_ENABLED
) {
1642 if ((limit
- ret
- 4 - 1) < 0)
1644 s2n(TLSEXT_TYPE_heartbeat
, ret
);
1648 * 1: peer may send requests
1649 * 2: peer not allowed to send requests
1651 if (s
->tlsext_heartbeat
& SSL_TLSEXT_HB_DONT_RECV_REQUESTS
)
1652 *(ret
++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS
;
1654 *(ret
++) = SSL_TLSEXT_HB_ENABLED
;
1659 # ifndef OPENSSL_NO_NEXTPROTONEG
1660 next_proto_neg_seen
= s
->s3
->next_proto_neg_seen
;
1661 s
->s3
->next_proto_neg_seen
= 0;
1662 if (next_proto_neg_seen
&& s
->ctx
->next_protos_advertised_cb
) {
1663 const unsigned char *npa
;
1664 unsigned int npalen
;
1667 r
= s
->ctx
->next_protos_advertised_cb(s
, &npa
, &npalen
,
1669 ctx
->next_protos_advertised_cb_arg
);
1670 if (r
== SSL_TLSEXT_ERR_OK
) {
1671 if ((long)(limit
- ret
- 4 - npalen
) < 0)
1673 s2n(TLSEXT_TYPE_next_proto_neg
, ret
);
1675 memcpy(ret
, npa
, npalen
);
1677 s
->s3
->next_proto_neg_seen
= 1;
1681 if (!custom_ext_add(s
, 1, &ret
, limit
, al
))
1683 # ifdef TLSEXT_TYPE_encrypt_then_mac
1684 if (s
->s3
->flags
& TLS1_FLAGS_ENCRYPT_THEN_MAC
) {
1686 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1687 * for other cases too.
1689 if (s
->s3
->tmp
.new_cipher
->algorithm_mac
== SSL_AEAD
1690 || s
->s3
->tmp
.new_cipher
->algorithm_enc
== SSL_RC4
)
1691 s
->s3
->flags
&= ~TLS1_FLAGS_ENCRYPT_THEN_MAC
;
1693 s2n(TLSEXT_TYPE_encrypt_then_mac
, ret
);
1698 if (!s
->hit
&& s
->session
->flags
& SSL_SESS_FLAG_EXTMS
) {
1699 s2n(TLSEXT_TYPE_extended_master_secret
, ret
);
1703 if (s
->s3
->alpn_selected
) {
1704 const unsigned char *selected
= s
->s3
->alpn_selected
;
1705 unsigned len
= s
->s3
->alpn_selected_len
;
1707 if ((long)(limit
- ret
- 4 - 2 - 1 - len
) < 0)
1709 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation
, ret
);
1713 memcpy(ret
, selected
, len
);
1719 if ((extdatalen
= ret
- orig
- 2) == 0)
1722 s2n(extdatalen
, orig
);
1727 * tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1728 * ClientHello. data: the contents of the extension, not including the type
1729 * and length. data_len: the number of bytes in |data| al: a pointer to the
1730 * alert value to send in the event of a non-zero return. returns: 0 on
1733 static int tls1_alpn_handle_client_hello(SSL
*s
, const unsigned char *data
,
1734 unsigned data_len
, int *al
)
1738 const unsigned char *selected
;
1739 unsigned char selected_len
;
1742 if (s
->ctx
->alpn_select_cb
== NULL
)
1749 * data should contain a uint16 length followed by a series of 8-bit,
1750 * length-prefixed strings.
1752 i
= ((unsigned)data
[0]) << 8 | ((unsigned)data
[1]);
1761 for (i
= 0; i
< data_len
;) {
1762 proto_len
= data
[i
];
1768 if (i
+ proto_len
< i
|| i
+ proto_len
> data_len
)
1774 r
= s
->ctx
->alpn_select_cb(s
, &selected
, &selected_len
, data
, data_len
,
1775 s
->ctx
->alpn_select_cb_arg
);
1776 if (r
== SSL_TLSEXT_ERR_OK
) {
1777 OPENSSL_free(s
->s3
->alpn_selected
);
1778 s
->s3
->alpn_selected
= OPENSSL_malloc(selected_len
);
1779 if (!s
->s3
->alpn_selected
) {
1780 *al
= SSL_AD_INTERNAL_ERROR
;
1783 memcpy(s
->s3
->alpn_selected
, selected
, selected_len
);
1784 s
->s3
->alpn_selected_len
= selected_len
;
1789 *al
= SSL_AD_DECODE_ERROR
;
1793 # ifndef OPENSSL_NO_EC
1795 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1796 * SecureTransport using the TLS extension block in |d|, of length |n|.
1797 * Safari, since 10.6, sends exactly these extensions, in this order:
1802 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1803 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1804 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1805 * 10.8..10.8.3 (which don't work).
1807 static void ssl_check_for_safari(SSL
*s
, const unsigned char *data
,
1808 const unsigned char *d
, int n
)
1810 unsigned short type
, size
;
1811 static const unsigned char kSafariExtensionsBlock
[] = {
1812 0x00, 0x0a, /* elliptic_curves extension */
1813 0x00, 0x08, /* 8 bytes */
1814 0x00, 0x06, /* 6 bytes of curve ids */
1815 0x00, 0x17, /* P-256 */
1816 0x00, 0x18, /* P-384 */
1817 0x00, 0x19, /* P-521 */
1819 0x00, 0x0b, /* ec_point_formats */
1820 0x00, 0x02, /* 2 bytes */
1821 0x01, /* 1 point format */
1822 0x00, /* uncompressed */
1825 /* The following is only present in TLS 1.2 */
1826 static const unsigned char kSafariTLS12ExtensionsBlock
[] = {
1827 0x00, 0x0d, /* signature_algorithms */
1828 0x00, 0x0c, /* 12 bytes */
1829 0x00, 0x0a, /* 10 bytes */
1830 0x05, 0x01, /* SHA-384/RSA */
1831 0x04, 0x01, /* SHA-256/RSA */
1832 0x02, 0x01, /* SHA-1/RSA */
1833 0x04, 0x03, /* SHA-256/ECDSA */
1834 0x02, 0x03, /* SHA-1/ECDSA */
1837 if (data
>= (d
+ n
- 2))
1841 if (data
> (d
+ n
- 4))
1846 if (type
!= TLSEXT_TYPE_server_name
)
1849 if (data
+ size
> d
+ n
)
1853 if (TLS1_get_client_version(s
) >= TLS1_2_VERSION
) {
1854 const size_t len1
= sizeof(kSafariExtensionsBlock
);
1855 const size_t len2
= sizeof(kSafariTLS12ExtensionsBlock
);
1857 if (data
+ len1
+ len2
!= d
+ n
)
1859 if (memcmp(data
, kSafariExtensionsBlock
, len1
) != 0)
1861 if (memcmp(data
+ len1
, kSafariTLS12ExtensionsBlock
, len2
) != 0)
1864 const size_t len
= sizeof(kSafariExtensionsBlock
);
1866 if (data
+ len
!= d
+ n
)
1868 if (memcmp(data
, kSafariExtensionsBlock
, len
) != 0)
1872 s
->s3
->is_probably_safari
= 1;
1874 # endif /* !OPENSSL_NO_EC */
1876 static int ssl_scan_clienthello_tlsext(SSL
*s
, unsigned char **p
,
1877 unsigned char *d
, int n
, int *al
)
1879 unsigned short type
;
1880 unsigned short size
;
1882 unsigned char *data
= *p
;
1883 int renegotiate_seen
= 0;
1885 s
->servername_done
= 0;
1886 s
->tlsext_status_type
= -1;
1887 # ifndef OPENSSL_NO_NEXTPROTONEG
1888 s
->s3
->next_proto_neg_seen
= 0;
1891 OPENSSL_free(s
->s3
->alpn_selected
);
1892 s
->s3
->alpn_selected
= NULL
;
1893 # ifndef OPENSSL_NO_HEARTBEATS
1894 s
->tlsext_heartbeat
&= ~(SSL_TLSEXT_HB_ENABLED
|
1895 SSL_TLSEXT_HB_DONT_SEND_REQUESTS
);
1898 # ifndef OPENSSL_NO_EC
1899 if (s
->options
& SSL_OP_SAFARI_ECDHE_ECDSA_BUG
)
1900 ssl_check_for_safari(s
, data
, d
, n
);
1901 # endif /* !OPENSSL_NO_EC */
1903 /* Clear any signature algorithms extension received */
1904 OPENSSL_free(s
->cert
->peer_sigalgs
);
1905 s
->cert
->peer_sigalgs
= NULL
;
1906 # ifdef TLSEXT_TYPE_encrypt_then_mac
1907 s
->s3
->flags
&= ~TLS1_FLAGS_ENCRYPT_THEN_MAC
;
1910 # ifndef OPENSSL_NO_SRP
1911 OPENSSL_free(s
->srp_ctx
.login
);
1912 s
->srp_ctx
.login
= NULL
;
1915 s
->srtp_profile
= NULL
;
1917 if (data
>= (d
+ n
- 2))
1921 if (data
> (d
+ n
- len
))
1924 while (data
<= (d
+ n
- 4)) {
1928 if (data
+ size
> (d
+ n
))
1930 if (s
->tlsext_debug_cb
)
1931 s
->tlsext_debug_cb(s
, 0, type
, data
, size
, s
->tlsext_debug_arg
);
1932 if (type
== TLSEXT_TYPE_renegotiate
) {
1933 if (!ssl_parse_clienthello_renegotiate_ext(s
, data
, size
, al
))
1935 renegotiate_seen
= 1;
1936 } else if (s
->version
== SSL3_VERSION
) {
1939 * The servername extension is treated as follows:
1941 * - Only the hostname type is supported with a maximum length of 255.
1942 * - The servername is rejected if too long or if it contains zeros,
1943 * in which case an fatal alert is generated.
1944 * - The servername field is maintained together with the session cache.
1945 * - When a session is resumed, the servername call back invoked in order
1946 * to allow the application to position itself to the right context.
1947 * - The servername is acknowledged if it is new for a session or when
1948 * it is identical to a previously used for the same session.
1949 * Applications can control the behaviour. They can at any time
1950 * set a 'desirable' servername for a new SSL object. This can be the
1951 * case for example with HTTPS when a Host: header field is received and
1952 * a renegotiation is requested. In this case, a possible servername
1953 * presented in the new client hello is only acknowledged if it matches
1954 * the value of the Host: field.
1955 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1956 * if they provide for changing an explicit servername context for the
1957 * session, i.e. when the session has been established with a servername
1959 * - On session reconnect, the servername extension may be absent.
1963 else if (type
== TLSEXT_TYPE_server_name
) {
1964 unsigned char *sdata
;
1969 *al
= SSL_AD_DECODE_ERROR
;
1975 *al
= SSL_AD_DECODE_ERROR
;
1981 servname_type
= *(sdata
++);
1986 *al
= SSL_AD_DECODE_ERROR
;
1989 if (s
->servername_done
== 0)
1990 switch (servname_type
) {
1991 case TLSEXT_NAMETYPE_host_name
:
1993 if (s
->session
->tlsext_hostname
) {
1994 *al
= SSL_AD_DECODE_ERROR
;
1997 if (len
> TLSEXT_MAXLEN_host_name
) {
1998 *al
= TLS1_AD_UNRECOGNIZED_NAME
;
2001 if ((s
->session
->tlsext_hostname
=
2002 OPENSSL_malloc(len
+ 1)) == NULL
) {
2003 *al
= TLS1_AD_INTERNAL_ERROR
;
2006 memcpy(s
->session
->tlsext_hostname
, sdata
, len
);
2007 s
->session
->tlsext_hostname
[len
] = '\0';
2008 if (strlen(s
->session
->tlsext_hostname
) != len
) {
2009 OPENSSL_free(s
->session
->tlsext_hostname
);
2010 s
->session
->tlsext_hostname
= NULL
;
2011 *al
= TLS1_AD_UNRECOGNIZED_NAME
;
2014 s
->servername_done
= 1;
2017 s
->servername_done
= s
->session
->tlsext_hostname
2018 && strlen(s
->session
->tlsext_hostname
) == len
2019 && strncmp(s
->session
->tlsext_hostname
,
2020 (char *)sdata
, len
) == 0;
2031 *al
= SSL_AD_DECODE_ERROR
;
2036 # ifndef OPENSSL_NO_SRP
2037 else if (type
== TLSEXT_TYPE_srp
) {
2038 if (size
<= 0 || ((len
= data
[0])) != (size
- 1)) {
2039 *al
= SSL_AD_DECODE_ERROR
;
2042 if (s
->srp_ctx
.login
!= NULL
) {
2043 *al
= SSL_AD_DECODE_ERROR
;
2046 if ((s
->srp_ctx
.login
= OPENSSL_malloc(len
+ 1)) == NULL
)
2048 memcpy(s
->srp_ctx
.login
, &data
[1], len
);
2049 s
->srp_ctx
.login
[len
] = '\0';
2051 if (strlen(s
->srp_ctx
.login
) != len
) {
2052 *al
= SSL_AD_DECODE_ERROR
;
2058 # ifndef OPENSSL_NO_EC
2059 else if (type
== TLSEXT_TYPE_ec_point_formats
) {
2060 unsigned char *sdata
= data
;
2061 int ecpointformatlist_length
= *(sdata
++);
2063 if (ecpointformatlist_length
!= size
- 1 ||
2064 ecpointformatlist_length
< 1) {
2065 *al
= TLS1_AD_DECODE_ERROR
;
2069 OPENSSL_free(s
->session
->tlsext_ecpointformatlist
);
2070 s
->session
->tlsext_ecpointformatlist
= NULL
;
2071 s
->session
->tlsext_ecpointformatlist_length
= 0;
2072 if ((s
->session
->tlsext_ecpointformatlist
=
2073 OPENSSL_malloc(ecpointformatlist_length
)) == NULL
) {
2074 *al
= TLS1_AD_INTERNAL_ERROR
;
2077 s
->session
->tlsext_ecpointformatlist_length
=
2078 ecpointformatlist_length
;
2079 memcpy(s
->session
->tlsext_ecpointformatlist
, sdata
,
2080 ecpointformatlist_length
);
2082 } else if (type
== TLSEXT_TYPE_elliptic_curves
) {
2083 unsigned char *sdata
= data
;
2084 int ellipticcurvelist_length
= (*(sdata
++) << 8);
2085 ellipticcurvelist_length
+= (*(sdata
++));
2087 if (ellipticcurvelist_length
!= size
- 2 ||
2088 ellipticcurvelist_length
< 1 ||
2089 /* Each NamedCurve is 2 bytes. */
2090 ellipticcurvelist_length
& 1) {
2091 *al
= TLS1_AD_DECODE_ERROR
;
2095 if (s
->session
->tlsext_ellipticcurvelist
) {
2096 *al
= TLS1_AD_DECODE_ERROR
;
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 memcpy(s
->session
->tlsext_ellipticcurvelist
, sdata
,
2108 ellipticcurvelist_length
);
2111 # endif /* OPENSSL_NO_EC */
2112 else if (type
== TLSEXT_TYPE_session_ticket
) {
2113 if (s
->tls_session_ticket_ext_cb
&&
2114 !s
->tls_session_ticket_ext_cb(s
, data
, size
,
2115 s
->tls_session_ticket_ext_cb_arg
))
2117 *al
= TLS1_AD_INTERNAL_ERROR
;
2120 } else if (type
== TLSEXT_TYPE_signature_algorithms
) {
2122 if (s
->cert
->peer_sigalgs
|| size
< 2) {
2123 *al
= SSL_AD_DECODE_ERROR
;
2128 if (dsize
!= size
|| dsize
& 1 || !dsize
) {
2129 *al
= SSL_AD_DECODE_ERROR
;
2132 if (!tls1_save_sigalgs(s
, data
, dsize
)) {
2133 *al
= SSL_AD_DECODE_ERROR
;
2136 } else if (type
== TLSEXT_TYPE_status_request
) {
2139 *al
= SSL_AD_DECODE_ERROR
;
2143 s
->tlsext_status_type
= *data
++;
2145 if (s
->tlsext_status_type
== TLSEXT_STATUSTYPE_ocsp
) {
2146 const unsigned char *sdata
;
2148 /* Read in responder_id_list */
2152 *al
= SSL_AD_DECODE_ERROR
;
2159 *al
= SSL_AD_DECODE_ERROR
;
2163 dsize
-= 2 + idsize
;
2166 *al
= SSL_AD_DECODE_ERROR
;
2171 id
= d2i_OCSP_RESPID(NULL
, &sdata
, idsize
);
2173 *al
= SSL_AD_DECODE_ERROR
;
2176 if (data
!= sdata
) {
2177 OCSP_RESPID_free(id
);
2178 *al
= SSL_AD_DECODE_ERROR
;
2181 if (!s
->tlsext_ocsp_ids
2182 && !(s
->tlsext_ocsp_ids
=
2183 sk_OCSP_RESPID_new_null())) {
2184 OCSP_RESPID_free(id
);
2185 *al
= SSL_AD_INTERNAL_ERROR
;
2188 if (!sk_OCSP_RESPID_push(s
->tlsext_ocsp_ids
, id
)) {
2189 OCSP_RESPID_free(id
);
2190 *al
= SSL_AD_INTERNAL_ERROR
;
2195 /* Read in request_extensions */
2197 *al
= SSL_AD_DECODE_ERROR
;
2202 if (dsize
!= size
) {
2203 *al
= SSL_AD_DECODE_ERROR
;
2208 sk_X509_EXTENSION_pop_free(s
->tlsext_ocsp_exts
,
2209 X509_EXTENSION_free
);
2210 s
->tlsext_ocsp_exts
=
2211 d2i_X509_EXTENSIONS(NULL
, &sdata
, dsize
);
2212 if (!s
->tlsext_ocsp_exts
|| (data
+ dsize
!= sdata
)) {
2213 *al
= SSL_AD_DECODE_ERROR
;
2219 * We don't know what to do with any other type * so ignore it.
2222 s
->tlsext_status_type
= -1;
2224 # ifndef OPENSSL_NO_HEARTBEATS
2225 else if (type
== TLSEXT_TYPE_heartbeat
) {
2227 case 0x01: /* Client allows us to send HB requests */
2228 s
->tlsext_heartbeat
|= SSL_TLSEXT_HB_ENABLED
;
2230 case 0x02: /* Client doesn't accept HB requests */
2231 s
->tlsext_heartbeat
|= SSL_TLSEXT_HB_ENABLED
;
2232 s
->tlsext_heartbeat
|= SSL_TLSEXT_HB_DONT_SEND_REQUESTS
;
2235 *al
= SSL_AD_ILLEGAL_PARAMETER
;
2240 # ifndef OPENSSL_NO_NEXTPROTONEG
2241 else if (type
== TLSEXT_TYPE_next_proto_neg
&&
2242 s
->s3
->tmp
.finish_md_len
== 0 &&
2243 s
->s3
->alpn_selected
== NULL
) {
2245 * We shouldn't accept this extension on a
2248 * s->new_session will be set on renegotiation, but we
2249 * probably shouldn't rely that it couldn't be set on
2250 * the initial renegotation too in certain cases (when
2251 * there's some other reason to disallow resuming an
2252 * earlier session -- the current code won't be doing
2253 * anything like that, but this might change).
2255 * A valid sign that there's been a previous handshake
2256 * in this connection is if s->s3->tmp.finish_md_len >
2257 * 0. (We are talking about a check that will happen
2258 * in the Hello protocol round, well before a new
2259 * Finished message could have been computed.)
2261 s
->s3
->next_proto_neg_seen
= 1;
2265 else if (type
== TLSEXT_TYPE_application_layer_protocol_negotiation
&&
2266 s
->ctx
->alpn_select_cb
&& s
->s3
->tmp
.finish_md_len
== 0) {
2267 if (tls1_alpn_handle_client_hello(s
, data
, size
, al
) != 0)
2269 # ifndef OPENSSL_NO_NEXTPROTONEG
2270 /* ALPN takes precedence over NPN. */
2271 s
->s3
->next_proto_neg_seen
= 0;
2275 /* session ticket processed earlier */
2276 # ifndef OPENSSL_NO_SRTP
2277 else if (SSL_IS_DTLS(s
) && SSL_get_srtp_profiles(s
)
2278 && type
== TLSEXT_TYPE_use_srtp
) {
2279 if (ssl_parse_clienthello_use_srtp_ext(s
, data
, size
, al
))
2283 # ifdef TLSEXT_TYPE_encrypt_then_mac
2284 else if (type
== TLSEXT_TYPE_encrypt_then_mac
)
2285 s
->s3
->flags
|= TLS1_FLAGS_ENCRYPT_THEN_MAC
;
2287 else if (type
== TLSEXT_TYPE_extended_master_secret
) {
2289 s
->session
->flags
|= SSL_SESS_FLAG_EXTMS
;
2292 * If this ClientHello extension was unhandled and this is a
2293 * nonresumed connection, check whether the extension is a custom
2294 * TLS Extension (has a custom_srv_ext_record), and if so call the
2295 * callback and record the extension number so that an appropriate
2296 * ServerHello may be later returned.
2299 if (custom_ext_parse(s
, 1, type
, data
, size
, al
) <= 0)
2310 /* Need RI if renegotiating */
2312 if (!renegotiate_seen
&& s
->renegotiate
&&
2313 !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)) {
2314 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2315 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT
,
2316 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
2323 int ssl_parse_clienthello_tlsext(SSL
*s
, unsigned char **p
, unsigned char *d
,
2327 custom_ext_init(&s
->cert
->srv_ext
);
2328 if (ssl_scan_clienthello_tlsext(s
, p
, d
, n
, &al
) <= 0) {
2329 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2333 if (ssl_check_clienthello_tlsext_early(s
) <= 0) {
2334 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT
, SSL_R_CLIENTHELLO_TLSEXT
);
2340 # ifndef OPENSSL_NO_NEXTPROTONEG
2342 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2343 * elements of zero length are allowed and the set of elements must exactly
2344 * fill the length of the block.
2346 static char ssl_next_proto_validate(unsigned char *d
, unsigned len
)
2348 unsigned int off
= 0;
2361 static int ssl_scan_serverhello_tlsext(SSL
*s
, unsigned char **p
,
2362 unsigned char *d
, int n
, int *al
)
2364 unsigned short length
;
2365 unsigned short type
;
2366 unsigned short size
;
2367 unsigned char *data
= *p
;
2368 int tlsext_servername
= 0;
2369 int renegotiate_seen
= 0;
2371 # ifndef OPENSSL_NO_NEXTPROTONEG
2372 s
->s3
->next_proto_neg_seen
= 0;
2374 s
->tlsext_ticket_expected
= 0;
2376 OPENSSL_free(s
->s3
->alpn_selected
);
2377 s
->s3
->alpn_selected
= NULL
;
2378 # ifndef OPENSSL_NO_HEARTBEATS
2379 s
->tlsext_heartbeat
&= ~(SSL_TLSEXT_HB_ENABLED
|
2380 SSL_TLSEXT_HB_DONT_SEND_REQUESTS
);
2383 # ifdef TLSEXT_TYPE_encrypt_then_mac
2384 s
->s3
->flags
&= ~TLS1_FLAGS_ENCRYPT_THEN_MAC
;
2387 if (data
>= (d
+ n
- 2))
2391 if (data
+ length
!= d
+ n
) {
2392 *al
= SSL_AD_DECODE_ERROR
;
2396 while (data
<= (d
+ n
- 4)) {
2400 if (data
+ size
> (d
+ n
))
2403 if (s
->tlsext_debug_cb
)
2404 s
->tlsext_debug_cb(s
, 1, type
, data
, size
, s
->tlsext_debug_arg
);
2406 if (type
== TLSEXT_TYPE_renegotiate
) {
2407 if (!ssl_parse_serverhello_renegotiate_ext(s
, data
, size
, al
))
2409 renegotiate_seen
= 1;
2410 } else if (s
->version
== SSL3_VERSION
) {
2411 } else if (type
== TLSEXT_TYPE_server_name
) {
2412 if (s
->tlsext_hostname
== NULL
|| size
> 0) {
2413 *al
= TLS1_AD_UNRECOGNIZED_NAME
;
2416 tlsext_servername
= 1;
2418 # ifndef OPENSSL_NO_EC
2419 else if (type
== TLSEXT_TYPE_ec_point_formats
) {
2420 unsigned char *sdata
= data
;
2421 int ecpointformatlist_length
= *(sdata
++);
2423 if (ecpointformatlist_length
!= size
- 1) {
2424 *al
= TLS1_AD_DECODE_ERROR
;
2428 s
->session
->tlsext_ecpointformatlist_length
= 0;
2429 OPENSSL_free(s
->session
->tlsext_ecpointformatlist
);
2430 if ((s
->session
->tlsext_ecpointformatlist
=
2431 OPENSSL_malloc(ecpointformatlist_length
)) == NULL
) {
2432 *al
= TLS1_AD_INTERNAL_ERROR
;
2435 s
->session
->tlsext_ecpointformatlist_length
=
2436 ecpointformatlist_length
;
2437 memcpy(s
->session
->tlsext_ecpointformatlist
, sdata
,
2438 ecpointformatlist_length
);
2441 # endif /* OPENSSL_NO_EC */
2443 else if (type
== TLSEXT_TYPE_session_ticket
) {
2444 if (s
->tls_session_ticket_ext_cb
&&
2445 !s
->tls_session_ticket_ext_cb(s
, data
, size
,
2446 s
->tls_session_ticket_ext_cb_arg
))
2448 *al
= TLS1_AD_INTERNAL_ERROR
;
2451 if (!tls_use_ticket(s
) || (size
> 0)) {
2452 *al
= TLS1_AD_UNSUPPORTED_EXTENSION
;
2455 s
->tlsext_ticket_expected
= 1;
2457 else if (type
== TLSEXT_TYPE_status_request
) {
2459 * MUST be empty and only sent if we've requested a status
2462 if ((s
->tlsext_status_type
== -1) || (size
> 0)) {
2463 *al
= TLS1_AD_UNSUPPORTED_EXTENSION
;
2466 /* Set flag to expect CertificateStatus message */
2467 s
->tlsext_status_expected
= 1;
2469 # ifndef OPENSSL_NO_NEXTPROTONEG
2470 else if (type
== TLSEXT_TYPE_next_proto_neg
&&
2471 s
->s3
->tmp
.finish_md_len
== 0) {
2472 unsigned char *selected
;
2473 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(data
, size
)) {
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
) {
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
) {
2507 /* We must have requested it. */
2508 if (s
->alpn_client_proto_list
== NULL
) {
2509 *al
= TLS1_AD_UNSUPPORTED_EXTENSION
;
2513 *al
= TLS1_AD_DECODE_ERROR
;
2517 * The extension data consists of:
2518 * uint16 list_length
2519 * uint8 proto_length;
2520 * uint8 proto[proto_length];
2525 if (len
!= (unsigned)size
- 2) {
2526 *al
= TLS1_AD_DECODE_ERROR
;
2530 if (len
!= (unsigned)size
- 3) {
2531 *al
= TLS1_AD_DECODE_ERROR
;
2534 OPENSSL_free(s
->s3
->alpn_selected
);
2535 s
->s3
->alpn_selected
= OPENSSL_malloc(len
);
2536 if (!s
->s3
->alpn_selected
) {
2537 *al
= TLS1_AD_INTERNAL_ERROR
;
2540 memcpy(s
->s3
->alpn_selected
, data
+ 3, len
);
2541 s
->s3
->alpn_selected_len
= len
;
2543 # ifndef OPENSSL_NO_HEARTBEATS
2544 else if (type
== TLSEXT_TYPE_heartbeat
) {
2546 case 0x01: /* Server allows us to send HB requests */
2547 s
->tlsext_heartbeat
|= SSL_TLSEXT_HB_ENABLED
;
2549 case 0x02: /* Server doesn't accept HB requests */
2550 s
->tlsext_heartbeat
|= SSL_TLSEXT_HB_ENABLED
;
2551 s
->tlsext_heartbeat
|= SSL_TLSEXT_HB_DONT_SEND_REQUESTS
;
2554 *al
= SSL_AD_ILLEGAL_PARAMETER
;
2559 # ifndef OPENSSL_NO_SRTP
2560 else if (SSL_IS_DTLS(s
) && type
== TLSEXT_TYPE_use_srtp
) {
2561 if (ssl_parse_serverhello_use_srtp_ext(s
, data
, size
, al
))
2565 # ifdef TLSEXT_TYPE_encrypt_then_mac
2566 else if (type
== TLSEXT_TYPE_encrypt_then_mac
) {
2567 /* Ignore if inappropriate ciphersuite */
2568 if (s
->s3
->tmp
.new_cipher
->algorithm_mac
!= SSL_AEAD
2569 && s
->s3
->tmp
.new_cipher
->algorithm_enc
!= SSL_RC4
)
2570 s
->s3
->flags
|= TLS1_FLAGS_ENCRYPT_THEN_MAC
;
2573 else if (type
== TLSEXT_TYPE_extended_master_secret
) {
2575 s
->session
->flags
|= SSL_SESS_FLAG_EXTMS
;
2578 * If this extension type was not otherwise handled, but matches a
2579 * custom_cli_ext_record, then send it to the c callback
2581 else if (custom_ext_parse(s
, 0, type
, data
, size
, al
) <= 0)
2587 if (data
!= d
+ n
) {
2588 *al
= SSL_AD_DECODE_ERROR
;
2592 if (!s
->hit
&& tlsext_servername
== 1) {
2593 if (s
->tlsext_hostname
) {
2594 if (s
->session
->tlsext_hostname
== NULL
) {
2595 s
->session
->tlsext_hostname
= BUF_strdup(s
->tlsext_hostname
);
2596 if (!s
->session
->tlsext_hostname
) {
2597 *al
= SSL_AD_UNRECOGNIZED_NAME
;
2601 *al
= SSL_AD_DECODE_ERROR
;
2612 * Determine if we need to see RI. Strictly speaking if we want to avoid
2613 * an attack we should *always* see RI even on initial server hello
2614 * because the client doesn't see any renegotiation during an attack.
2615 * However this would mean we could not connect to any server which
2616 * doesn't support RI so for the immediate future tolerate RI absence on
2617 * initial connect only.
2619 if (!renegotiate_seen
&& !(s
->options
& SSL_OP_LEGACY_SERVER_CONNECT
)
2620 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)) {
2621 *al
= SSL_AD_HANDSHAKE_FAILURE
;
2622 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT
,
2623 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
2630 int ssl_prepare_clienthello_tlsext(SSL
*s
)
2636 int ssl_prepare_serverhello_tlsext(SSL
*s
)
2641 static int ssl_check_clienthello_tlsext_early(SSL
*s
)
2643 int ret
= SSL_TLSEXT_ERR_NOACK
;
2644 int al
= SSL_AD_UNRECOGNIZED_NAME
;
2646 # ifndef OPENSSL_NO_EC
2648 * The handling of the ECPointFormats extension is done elsewhere, namely
2649 * in ssl3_choose_cipher in s3_lib.c.
2652 * The handling of the EllipticCurves extension is done elsewhere, namely
2653 * in ssl3_choose_cipher in s3_lib.c.
2657 if (s
->ctx
!= NULL
&& s
->ctx
->tlsext_servername_callback
!= 0)
2659 s
->ctx
->tlsext_servername_callback(s
, &al
,
2660 s
->ctx
->tlsext_servername_arg
);
2661 else if (s
->initial_ctx
!= NULL
2662 && s
->initial_ctx
->tlsext_servername_callback
!= 0)
2664 s
->initial_ctx
->tlsext_servername_callback(s
, &al
,
2666 initial_ctx
->tlsext_servername_arg
);
2669 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2670 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2673 case SSL_TLSEXT_ERR_ALERT_WARNING
:
2674 ssl3_send_alert(s
, SSL3_AL_WARNING
, al
);
2677 case SSL_TLSEXT_ERR_NOACK
:
2678 s
->servername_done
= 0;
2684 int tls1_set_server_sigalgs(SSL
*s
)
2688 /* Clear any shared sigtnature algorithms */
2689 OPENSSL_free(s
->cert
->shared_sigalgs
);
2690 s
->cert
->shared_sigalgs
= NULL
;
2691 s
->cert
->shared_sigalgslen
= 0;
2692 /* Clear certificate digests and validity flags */
2693 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
2694 s
->cert
->pkeys
[i
].digest
= NULL
;
2695 s
->cert
->pkeys
[i
].valid_flags
= 0;
2698 /* If sigalgs received process it. */
2699 if (s
->cert
->peer_sigalgs
) {
2700 if (!tls1_process_sigalgs(s
)) {
2701 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS
, ERR_R_MALLOC_FAILURE
);
2702 al
= SSL_AD_INTERNAL_ERROR
;
2705 /* Fatal error is no shared signature algorithms */
2706 if (!s
->cert
->shared_sigalgs
) {
2707 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS
,
2708 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS
);
2709 al
= SSL_AD_ILLEGAL_PARAMETER
;
2713 ssl_cert_set_default_md(s
->cert
);
2716 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2720 int ssl_check_clienthello_tlsext_late(SSL
*s
)
2722 int ret
= SSL_TLSEXT_ERR_OK
;
2723 int al
= SSL_AD_INTERNAL_ERROR
;
2726 * If status request then ask callback what to do. Note: this must be
2727 * called after servername callbacks in case the certificate has changed,
2728 * and must be called after the cipher has been chosen because this may
2729 * influence which certificate is sent
2731 if ((s
->tlsext_status_type
!= -1) && s
->ctx
&& s
->ctx
->tlsext_status_cb
) {
2733 CERT_PKEY
*certpkey
;
2734 certpkey
= ssl_get_server_send_pkey(s
);
2735 /* If no certificate can't return certificate status */
2736 if (certpkey
== NULL
) {
2737 s
->tlsext_status_expected
= 0;
2741 * Set current certificate to one we will use so SSL_get_certificate
2742 * et al can pick it up.
2744 s
->cert
->key
= certpkey
;
2745 r
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2747 /* We don't want to send a status request response */
2748 case SSL_TLSEXT_ERR_NOACK
:
2749 s
->tlsext_status_expected
= 0;
2751 /* status request response should be sent */
2752 case SSL_TLSEXT_ERR_OK
:
2753 if (s
->tlsext_ocsp_resp
)
2754 s
->tlsext_status_expected
= 1;
2756 s
->tlsext_status_expected
= 0;
2758 /* something bad happened */
2759 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2760 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
2761 al
= SSL_AD_INTERNAL_ERROR
;
2765 s
->tlsext_status_expected
= 0;
2769 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2770 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2773 case SSL_TLSEXT_ERR_ALERT_WARNING
:
2774 ssl3_send_alert(s
, SSL3_AL_WARNING
, al
);
2782 int ssl_check_serverhello_tlsext(SSL
*s
)
2784 int ret
= SSL_TLSEXT_ERR_NOACK
;
2785 int al
= SSL_AD_UNRECOGNIZED_NAME
;
2787 # ifndef OPENSSL_NO_EC
2789 * If we are client and using an elliptic curve cryptography cipher
2790 * suite, then if server returns an EC point formats lists extension it
2791 * must contain uncompressed.
2793 unsigned long alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2794 unsigned long alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2795 if ((s
->tlsext_ecpointformatlist
!= NULL
)
2796 && (s
->tlsext_ecpointformatlist_length
> 0)
2797 && (s
->session
->tlsext_ecpointformatlist
!= NULL
)
2798 && (s
->session
->tlsext_ecpointformatlist_length
> 0)
2799 && ((alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
))
2800 || (alg_a
& SSL_aECDSA
))) {
2801 /* we are using an ECC cipher */
2803 unsigned char *list
;
2804 int found_uncompressed
= 0;
2805 list
= s
->session
->tlsext_ecpointformatlist
;
2806 for (i
= 0; i
< s
->session
->tlsext_ecpointformatlist_length
; i
++) {
2807 if (*(list
++) == TLSEXT_ECPOINTFORMAT_uncompressed
) {
2808 found_uncompressed
= 1;
2812 if (!found_uncompressed
) {
2813 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT
,
2814 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST
);
2818 ret
= SSL_TLSEXT_ERR_OK
;
2819 # endif /* OPENSSL_NO_EC */
2821 if (s
->ctx
!= NULL
&& s
->ctx
->tlsext_servername_callback
!= 0)
2823 s
->ctx
->tlsext_servername_callback(s
, &al
,
2824 s
->ctx
->tlsext_servername_arg
);
2825 else if (s
->initial_ctx
!= NULL
2826 && s
->initial_ctx
->tlsext_servername_callback
!= 0)
2828 s
->initial_ctx
->tlsext_servername_callback(s
, &al
,
2830 initial_ctx
->tlsext_servername_arg
);
2833 * If we've requested certificate status and we wont get one tell the
2836 if ((s
->tlsext_status_type
!= -1) && !(s
->tlsext_status_expected
)
2837 && s
->ctx
&& s
->ctx
->tlsext_status_cb
) {
2840 * Set resp to NULL, resplen to -1 so callback knows there is no
2843 OPENSSL_free(s
->tlsext_ocsp_resp
);
2844 s
->tlsext_ocsp_resp
= NULL
;
2845 s
->tlsext_ocsp_resplen
= -1;
2846 r
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2848 al
= SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
2849 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
2852 al
= SSL_AD_INTERNAL_ERROR
;
2853 ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
2858 case SSL_TLSEXT_ERR_ALERT_FATAL
:
2859 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2862 case SSL_TLSEXT_ERR_ALERT_WARNING
:
2863 ssl3_send_alert(s
, SSL3_AL_WARNING
, al
);
2866 case SSL_TLSEXT_ERR_NOACK
:
2867 s
->servername_done
= 0;
2873 int ssl_parse_serverhello_tlsext(SSL
*s
, unsigned char **p
, unsigned char *d
,
2877 if (s
->version
< SSL3_VERSION
)
2879 if (ssl_scan_serverhello_tlsext(s
, p
, d
, n
, &al
) <= 0) {
2880 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2884 if (ssl_check_serverhello_tlsext(s
) <= 0) {
2885 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT
, SSL_R_SERVERHELLO_TLSEXT
);
2892 * Since the server cache lookup is done early on in the processing of the
2893 * ClientHello, and other operations depend on the result, we need to handle
2894 * any TLS session ticket extension at the same time.
2896 * session_id: points at the session ID in the ClientHello. This code will
2897 * read past the end of this in order to parse out the session ticket
2898 * extension, if any.
2899 * len: the length of the session ID.
2900 * limit: a pointer to the first byte after the ClientHello.
2901 * ret: (output) on return, if a ticket was decrypted, then this is set to
2902 * point to the resulting session.
2904 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2905 * ciphersuite, in which case we have no use for session tickets and one will
2906 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2909 * -1: fatal error, either from parsing or decrypting the ticket.
2910 * 0: no ticket was found (or was ignored, based on settings).
2911 * 1: a zero length extension was found, indicating that the client supports
2912 * session tickets but doesn't currently have one to offer.
2913 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2914 * couldn't be decrypted because of a non-fatal error.
2915 * 3: a ticket was successfully decrypted and *ret was set.
2918 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2919 * a new session ticket to the client because the client indicated support
2920 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2921 * a session ticket or we couldn't use the one it gave us, or if
2922 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2923 * Otherwise, s->tlsext_ticket_expected is set to 0.
2925 int tls1_process_ticket(SSL
*s
, unsigned char *session_id
, int len
,
2926 const unsigned char *limit
, SSL_SESSION
**ret
)
2928 /* Point after session ID in client hello */
2929 const unsigned char *p
= session_id
+ len
;
2933 s
->tlsext_ticket_expected
= 0;
2936 * If tickets disabled behave as if no ticket present to permit stateful
2939 if (!tls_use_ticket(s
))
2941 if ((s
->version
<= SSL3_VERSION
) || !limit
)
2945 /* Skip past DTLS cookie */
2946 if (SSL_IS_DTLS(s
)) {
2952 /* Skip past cipher list */
2957 /* Skip past compression algorithm list */
2962 /* Now at start of extensions */
2963 if ((p
+ 2) >= limit
)
2966 while ((p
+ 4) <= limit
) {
2967 unsigned short type
, size
;
2970 if (p
+ size
> limit
)
2972 if (type
== TLSEXT_TYPE_session_ticket
) {
2976 * The client will accept a ticket but doesn't currently have
2979 s
->tlsext_ticket_expected
= 1;
2982 if (s
->tls_session_secret_cb
) {
2984 * Indicate that the ticket couldn't be decrypted rather than
2985 * generating the session from ticket now, trigger
2986 * abbreviated handshake based on external mechanism to
2987 * calculate the master secret later.
2991 r
= tls_decrypt_ticket(s
, p
, size
, session_id
, len
, ret
);
2993 case 2: /* ticket couldn't be decrypted */
2994 s
->tlsext_ticket_expected
= 1;
2996 case 3: /* ticket was decrypted */
2998 case 4: /* ticket decrypted but need to renew */
2999 s
->tlsext_ticket_expected
= 1;
3001 default: /* fatal error */
3011 * tls_decrypt_ticket attempts to decrypt a session ticket.
3013 * etick: points to the body of the session ticket extension.
3014 * eticklen: the length of the session tickets extenion.
3015 * sess_id: points at the session ID.
3016 * sesslen: the length of the session ID.
3017 * psess: (output) on return, if a ticket was decrypted, then this is set to
3018 * point to the resulting session.
3021 * -1: fatal error, either from parsing or decrypting the ticket.
3022 * 2: the ticket couldn't be decrypted.
3023 * 3: a ticket was successfully decrypted and *psess was set.
3024 * 4: same as 3, but the ticket needs to be renewed.
3026 static int tls_decrypt_ticket(SSL
*s
, const unsigned char *etick
,
3027 int eticklen
, const unsigned char *sess_id
,
3028 int sesslen
, SSL_SESSION
**psess
)
3031 unsigned char *sdec
;
3032 const unsigned char *p
;
3033 int slen
, mlen
, renew_ticket
= 0;
3034 unsigned char tick_hmac
[EVP_MAX_MD_SIZE
];
3037 SSL_CTX
*tctx
= s
->initial_ctx
;
3038 /* Need at least keyname + iv + some encrypted data */
3041 /* Initialize session ticket encryption and HMAC contexts */
3042 HMAC_CTX_init(&hctx
);
3043 EVP_CIPHER_CTX_init(&ctx
);
3044 if (tctx
->tlsext_ticket_key_cb
) {
3045 unsigned char *nctick
= (unsigned char *)etick
;
3046 int rv
= tctx
->tlsext_ticket_key_cb(s
, nctick
, nctick
+ 16,
3055 /* Check key name matches */
3056 if (memcmp(etick
, tctx
->tlsext_tick_key_name
, 16))
3058 HMAC_Init_ex(&hctx
, tctx
->tlsext_tick_hmac_key
, 16,
3059 EVP_sha256(), NULL
);
3060 EVP_DecryptInit_ex(&ctx
, EVP_aes_128_cbc(), NULL
,
3061 tctx
->tlsext_tick_aes_key
, etick
+ 16);
3064 * Attempt to process session ticket, first conduct sanity and integrity
3067 mlen
= HMAC_size(&hctx
);
3069 EVP_CIPHER_CTX_cleanup(&ctx
);
3073 /* Check HMAC of encrypted ticket */
3074 HMAC_Update(&hctx
, etick
, eticklen
);
3075 HMAC_Final(&hctx
, tick_hmac
, NULL
);
3076 HMAC_CTX_cleanup(&hctx
);
3077 if (CRYPTO_memcmp(tick_hmac
, etick
+ eticklen
, mlen
)) {
3078 EVP_CIPHER_CTX_cleanup(&ctx
);
3081 /* Attempt to decrypt session data */
3082 /* Move p after IV to start of encrypted ticket, update length */
3083 p
= etick
+ 16 + EVP_CIPHER_CTX_iv_length(&ctx
);
3084 eticklen
-= 16 + EVP_CIPHER_CTX_iv_length(&ctx
);
3085 sdec
= OPENSSL_malloc(eticklen
);
3087 EVP_CIPHER_CTX_cleanup(&ctx
);
3090 EVP_DecryptUpdate(&ctx
, sdec
, &slen
, p
, eticklen
);
3091 if (EVP_DecryptFinal(&ctx
, sdec
+ slen
, &mlen
) <= 0) {
3092 EVP_CIPHER_CTX_cleanup(&ctx
);
3097 EVP_CIPHER_CTX_cleanup(&ctx
);
3100 sess
= d2i_SSL_SESSION(NULL
, &p
, slen
);
3104 * The session ID, if non-empty, is used by some clients to detect
3105 * that the ticket has been accepted. So we copy it to the session
3106 * structure. If it is empty set length to zero as required by
3110 memcpy(sess
->session_id
, sess_id
, sesslen
);
3111 sess
->session_id_length
= sesslen
;
3120 * For session parse failure, indicate that we need to send a new ticket.
3125 /* Tables to translate from NIDs to TLS v1.2 ids */
3132 static const tls12_lookup tls12_md
[] = {
3133 {NID_md5
, TLSEXT_hash_md5
},
3134 {NID_sha1
, TLSEXT_hash_sha1
},
3135 {NID_sha224
, TLSEXT_hash_sha224
},
3136 {NID_sha256
, TLSEXT_hash_sha256
},
3137 {NID_sha384
, TLSEXT_hash_sha384
},
3138 {NID_sha512
, TLSEXT_hash_sha512
}
3141 static const tls12_lookup tls12_sig
[] = {
3142 {EVP_PKEY_RSA
, TLSEXT_signature_rsa
},
3143 {EVP_PKEY_DSA
, TLSEXT_signature_dsa
},
3144 {EVP_PKEY_EC
, TLSEXT_signature_ecdsa
}
3147 static int tls12_find_id(int nid
, const tls12_lookup
*table
, size_t tlen
)
3150 for (i
= 0; i
< tlen
; i
++) {
3151 if (table
[i
].nid
== nid
)
3157 static int tls12_find_nid(int id
, const tls12_lookup
*table
, size_t tlen
)
3160 for (i
= 0; i
< tlen
; i
++) {
3161 if ((table
[i
].id
) == id
)
3162 return table
[i
].nid
;
3167 int tls12_get_sigandhash(unsigned char *p
, const EVP_PKEY
*pk
,
3173 md_id
= tls12_find_id(EVP_MD_type(md
), tls12_md
, OSSL_NELEM(tls12_md
));
3176 sig_id
= tls12_get_sigid(pk
);
3179 p
[0] = (unsigned char)md_id
;
3180 p
[1] = (unsigned char)sig_id
;
3184 int tls12_get_sigid(const EVP_PKEY
*pk
)
3186 return tls12_find_id(pk
->type
, tls12_sig
, OSSL_NELEM(tls12_sig
));
3192 const EVP_MD
*(*mfunc
) (void);
3195 static const tls12_hash_info tls12_md_info
[] = {
3196 # ifdef OPENSSL_NO_MD5
3199 {NID_md5
, 64, EVP_md5
},
3201 {NID_sha1
, 80, EVP_sha1
},
3202 {NID_sha224
, 112, EVP_sha224
},
3203 {NID_sha256
, 128, EVP_sha256
},
3204 {NID_sha384
, 192, EVP_sha384
},
3205 {NID_sha512
, 256, EVP_sha512
}
3208 static const tls12_hash_info
*tls12_get_hash_info(unsigned char hash_alg
)
3212 if (hash_alg
> OSSL_NELEM(tls12_md_info
))
3214 return tls12_md_info
+ hash_alg
- 1;
3217 const EVP_MD
*tls12_get_hash(unsigned char hash_alg
)
3219 const tls12_hash_info
*inf
;
3220 if (hash_alg
== TLSEXT_hash_md5
&& FIPS_mode())
3222 inf
= tls12_get_hash_info(hash_alg
);
3223 if (!inf
|| !inf
->mfunc
)
3225 return inf
->mfunc();
3228 static int tls12_get_pkey_idx(unsigned char sig_alg
)
3231 # ifndef OPENSSL_NO_RSA
3232 case TLSEXT_signature_rsa
:
3233 return SSL_PKEY_RSA_SIGN
;
3235 # ifndef OPENSSL_NO_DSA
3236 case TLSEXT_signature_dsa
:
3237 return SSL_PKEY_DSA_SIGN
;
3239 # ifndef OPENSSL_NO_EC
3240 case TLSEXT_signature_ecdsa
:
3241 return SSL_PKEY_ECC
;
3247 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3248 static void tls1_lookup_sigalg(int *phash_nid
, int *psign_nid
,
3249 int *psignhash_nid
, const unsigned char *data
)
3251 int sign_nid
= 0, hash_nid
= 0;
3252 if (!phash_nid
&& !psign_nid
&& !psignhash_nid
)
3254 if (phash_nid
|| psignhash_nid
) {
3255 hash_nid
= tls12_find_nid(data
[0], tls12_md
, OSSL_NELEM(tls12_md
));
3257 *phash_nid
= hash_nid
;
3259 if (psign_nid
|| psignhash_nid
) {
3260 sign_nid
= tls12_find_nid(data
[1], tls12_sig
, OSSL_NELEM(tls12_sig
));
3262 *psign_nid
= sign_nid
;
3264 if (psignhash_nid
) {
3265 if (sign_nid
&& hash_nid
)
3266 OBJ_find_sigid_by_algs(psignhash_nid
, hash_nid
, sign_nid
);
3268 *psignhash_nid
= NID_undef
;
3272 /* Check to see if a signature algorithm is allowed */
3273 static int tls12_sigalg_allowed(SSL
*s
, int op
, const unsigned char *ptmp
)
3275 /* See if we have an entry in the hash table and it is enabled */
3276 const tls12_hash_info
*hinf
= tls12_get_hash_info(ptmp
[0]);
3277 if (!hinf
|| !hinf
->mfunc
)
3279 /* See if public key algorithm allowed */
3280 if (tls12_get_pkey_idx(ptmp
[1]) == -1)
3282 /* Finally see if security callback allows it */
3283 return ssl_security(s
, op
, hinf
->secbits
, hinf
->nid
, (void *)ptmp
);
3287 * Get a mask of disabled public key algorithms based on supported signature
3288 * algorithms. For example if no signature algorithm supports RSA then RSA is
3292 void ssl_set_sig_mask(unsigned long *pmask_a
, SSL
*s
, int op
)
3294 const unsigned char *sigalgs
;
3295 size_t i
, sigalgslen
;
3296 int have_rsa
= 0, have_dsa
= 0, have_ecdsa
= 0;
3298 * Now go through all signature algorithms seeing if we support any for
3299 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3300 * down calls to security callback only check if we have to.
3302 sigalgslen
= tls12_get_psigalgs(s
, &sigalgs
);
3303 for (i
= 0; i
< sigalgslen
; i
+= 2, sigalgs
+= 2) {
3304 switch (sigalgs
[1]) {
3305 # ifndef OPENSSL_NO_RSA
3306 case TLSEXT_signature_rsa
:
3307 if (!have_rsa
&& tls12_sigalg_allowed(s
, op
, sigalgs
))
3311 # ifndef OPENSSL_NO_DSA
3312 case TLSEXT_signature_dsa
:
3313 if (!have_dsa
&& tls12_sigalg_allowed(s
, op
, sigalgs
))
3317 # ifndef OPENSSL_NO_EC
3318 case TLSEXT_signature_ecdsa
:
3319 if (!have_ecdsa
&& tls12_sigalg_allowed(s
, op
, sigalgs
))
3326 *pmask_a
|= SSL_aRSA
;
3328 *pmask_a
|= SSL_aDSS
;
3330 *pmask_a
|= SSL_aECDSA
;
3333 size_t tls12_copy_sigalgs(SSL
*s
, unsigned char *out
,
3334 const unsigned char *psig
, size_t psiglen
)
3336 unsigned char *tmpout
= out
;
3338 for (i
= 0; i
< psiglen
; i
+= 2, psig
+= 2) {
3339 if (tls12_sigalg_allowed(s
, SSL_SECOP_SIGALG_SUPPORTED
, psig
)) {
3340 *tmpout
++ = psig
[0];
3341 *tmpout
++ = psig
[1];
3344 return tmpout
- out
;
3347 /* Given preference and allowed sigalgs set shared sigalgs */
3348 static int tls12_shared_sigalgs(SSL
*s
, TLS_SIGALGS
*shsig
,
3349 const unsigned char *pref
, size_t preflen
,
3350 const unsigned char *allow
, size_t allowlen
)
3352 const unsigned char *ptmp
, *atmp
;
3353 size_t i
, j
, nmatch
= 0;
3354 for (i
= 0, ptmp
= pref
; i
< preflen
; i
+= 2, ptmp
+= 2) {
3355 /* Skip disabled hashes or signature algorithms */
3356 if (!tls12_sigalg_allowed(s
, SSL_SECOP_SIGALG_SHARED
, ptmp
))
3358 for (j
= 0, atmp
= allow
; j
< allowlen
; j
+= 2, atmp
+= 2) {
3359 if (ptmp
[0] == atmp
[0] && ptmp
[1] == atmp
[1]) {
3362 shsig
->rhash
= ptmp
[0];
3363 shsig
->rsign
= ptmp
[1];
3364 tls1_lookup_sigalg(&shsig
->hash_nid
,
3366 &shsig
->signandhash_nid
, ptmp
);
3376 /* Set shared signature algorithms for SSL structures */
3377 static int tls1_set_shared_sigalgs(SSL
*s
)
3379 const unsigned char *pref
, *allow
, *conf
;
3380 size_t preflen
, allowlen
, conflen
;
3382 TLS_SIGALGS
*salgs
= NULL
;
3384 unsigned int is_suiteb
= tls1_suiteb(s
);
3386 OPENSSL_free(c
->shared_sigalgs
);
3387 c
->shared_sigalgs
= NULL
;
3388 c
->shared_sigalgslen
= 0;
3389 /* If client use client signature algorithms if not NULL */
3390 if (!s
->server
&& c
->client_sigalgs
&& !is_suiteb
) {
3391 conf
= c
->client_sigalgs
;
3392 conflen
= c
->client_sigalgslen
;
3393 } else if (c
->conf_sigalgs
&& !is_suiteb
) {
3394 conf
= c
->conf_sigalgs
;
3395 conflen
= c
->conf_sigalgslen
;
3397 conflen
= tls12_get_psigalgs(s
, &conf
);
3398 if (s
->options
& SSL_OP_CIPHER_SERVER_PREFERENCE
|| is_suiteb
) {
3401 allow
= c
->peer_sigalgs
;
3402 allowlen
= c
->peer_sigalgslen
;
3406 pref
= c
->peer_sigalgs
;
3407 preflen
= c
->peer_sigalgslen
;
3409 nmatch
= tls12_shared_sigalgs(s
, NULL
, pref
, preflen
, allow
, allowlen
);
3411 salgs
= OPENSSL_malloc(nmatch
* sizeof(TLS_SIGALGS
));
3414 nmatch
= tls12_shared_sigalgs(s
, salgs
, pref
, preflen
, allow
, allowlen
);
3418 c
->shared_sigalgs
= salgs
;
3419 c
->shared_sigalgslen
= nmatch
;
3423 /* Set preferred digest for each key type */
3425 int tls1_save_sigalgs(SSL
*s
, const unsigned char *data
, int dsize
)
3428 /* Extension ignored for inappropriate versions */
3429 if (!SSL_USE_SIGALGS(s
))
3431 /* Should never happen */
3435 OPENSSL_free(c
->peer_sigalgs
);
3436 c
->peer_sigalgs
= OPENSSL_malloc(dsize
);
3437 if (!c
->peer_sigalgs
)
3439 c
->peer_sigalgslen
= dsize
;
3440 memcpy(c
->peer_sigalgs
, data
, dsize
);
3444 int tls1_process_sigalgs(SSL
*s
)
3450 TLS_SIGALGS
*sigptr
;
3451 if (!tls1_set_shared_sigalgs(s
))
3454 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3455 if (s
->cert
->cert_flags
& SSL_CERT_FLAG_BROKEN_PROTOCOL
) {
3457 * Use first set signature preference to force message digest,
3458 * ignoring any peer preferences.
3460 const unsigned char *sigs
= NULL
;
3462 sigs
= c
->conf_sigalgs
;
3464 sigs
= c
->client_sigalgs
;
3466 idx
= tls12_get_pkey_idx(sigs
[1]);
3467 md
= tls12_get_hash(sigs
[0]);
3468 c
->pkeys
[idx
].digest
= md
;
3469 c
->pkeys
[idx
].valid_flags
= CERT_PKEY_EXPLICIT_SIGN
;
3470 if (idx
== SSL_PKEY_RSA_SIGN
) {
3471 c
->pkeys
[SSL_PKEY_RSA_ENC
].valid_flags
=
3472 CERT_PKEY_EXPLICIT_SIGN
;
3473 c
->pkeys
[SSL_PKEY_RSA_ENC
].digest
= md
;
3479 for (i
= 0, sigptr
= c
->shared_sigalgs
;
3480 i
< c
->shared_sigalgslen
; i
++, sigptr
++) {
3481 idx
= tls12_get_pkey_idx(sigptr
->rsign
);
3482 if (idx
> 0 && c
->pkeys
[idx
].digest
== NULL
) {
3483 md
= tls12_get_hash(sigptr
->rhash
);
3484 c
->pkeys
[idx
].digest
= md
;
3485 c
->pkeys
[idx
].valid_flags
= CERT_PKEY_EXPLICIT_SIGN
;
3486 if (idx
== SSL_PKEY_RSA_SIGN
) {
3487 c
->pkeys
[SSL_PKEY_RSA_ENC
].valid_flags
=
3488 CERT_PKEY_EXPLICIT_SIGN
;
3489 c
->pkeys
[SSL_PKEY_RSA_ENC
].digest
= md
;
3495 * In strict mode leave unset digests as NULL to indicate we can't use
3496 * the certificate for signing.
3498 if (!(s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
)) {
3500 * Set any remaining keys to default values. NOTE: if alg is not
3501 * supported it stays as NULL.
3503 # ifndef OPENSSL_NO_DSA
3504 if (!c
->pkeys
[SSL_PKEY_DSA_SIGN
].digest
)
3505 c
->pkeys
[SSL_PKEY_DSA_SIGN
].digest
= EVP_sha1();
3507 # ifndef OPENSSL_NO_RSA
3508 if (!c
->pkeys
[SSL_PKEY_RSA_SIGN
].digest
) {
3509 c
->pkeys
[SSL_PKEY_RSA_SIGN
].digest
= EVP_sha1();
3510 c
->pkeys
[SSL_PKEY_RSA_ENC
].digest
= EVP_sha1();
3513 # ifndef OPENSSL_NO_EC
3514 if (!c
->pkeys
[SSL_PKEY_ECC
].digest
)
3515 c
->pkeys
[SSL_PKEY_ECC
].digest
= EVP_sha1();
3521 int SSL_get_sigalgs(SSL
*s
, int idx
,
3522 int *psign
, int *phash
, int *psignhash
,
3523 unsigned char *rsig
, unsigned char *rhash
)
3525 const unsigned char *psig
= s
->cert
->peer_sigalgs
;
3530 if (idx
>= (int)s
->cert
->peer_sigalgslen
)
3537 tls1_lookup_sigalg(phash
, psign
, psignhash
, psig
);
3539 return s
->cert
->peer_sigalgslen
/ 2;
3542 int SSL_get_shared_sigalgs(SSL
*s
, int idx
,
3543 int *psign
, int *phash
, int *psignhash
,
3544 unsigned char *rsig
, unsigned char *rhash
)
3546 TLS_SIGALGS
*shsigalgs
= s
->cert
->shared_sigalgs
;
3547 if (!shsigalgs
|| idx
>= (int)s
->cert
->shared_sigalgslen
)
3551 *phash
= shsigalgs
->hash_nid
;
3553 *psign
= shsigalgs
->sign_nid
;
3555 *psignhash
= shsigalgs
->signandhash_nid
;
3557 *rsig
= shsigalgs
->rsign
;
3559 *rhash
= shsigalgs
->rhash
;
3560 return s
->cert
->shared_sigalgslen
;
3563 # ifndef OPENSSL_NO_HEARTBEATS
3564 int tls1_process_heartbeat(SSL
*s
, unsigned char *p
, unsigned int length
)
3567 unsigned short hbtype
;
3568 unsigned int payload
;
3569 unsigned int padding
= 16; /* Use minimum padding */
3571 if (s
->msg_callback
)
3572 s
->msg_callback(0, s
->version
, TLS1_RT_HEARTBEAT
,
3574 s
, s
->msg_callback_arg
);
3576 /* Read type and payload length first */
3577 if (1 + 2 + 16 > length
)
3578 return 0; /* silently discard */
3581 if (1 + 2 + payload
+ 16 > length
)
3582 return 0; /* silently discard per RFC 6520 sec. 4 */
3585 if (hbtype
== TLS1_HB_REQUEST
) {
3586 unsigned char *buffer
, *bp
;
3590 * Allocate memory for the response, size is 1 bytes message type,
3591 * plus 2 bytes payload length, plus payload, plus padding
3593 buffer
= OPENSSL_malloc(1 + 2 + payload
+ padding
);
3594 if (buffer
== NULL
) {
3595 SSLerr(SSL_F_TLS1_PROCESS_HEARTBEAT
, ERR_R_MALLOC_FAILURE
);
3600 /* Enter response type, length and copy payload */
3601 *bp
++ = TLS1_HB_RESPONSE
;
3603 memcpy(bp
, pl
, payload
);
3605 /* Random padding */
3606 if (RAND_bytes(bp
, padding
) <= 0) {
3607 OPENSSL_free(buffer
);
3611 r
= ssl3_write_bytes(s
, TLS1_RT_HEARTBEAT
, buffer
,
3612 3 + payload
+ padding
);
3614 if (r
>= 0 && s
->msg_callback
)
3615 s
->msg_callback(1, s
->version
, TLS1_RT_HEARTBEAT
,
3616 buffer
, 3 + payload
+ padding
,
3617 s
, s
->msg_callback_arg
);
3619 OPENSSL_free(buffer
);
3623 } else if (hbtype
== TLS1_HB_RESPONSE
) {
3627 * We only send sequence numbers (2 bytes unsigned int), and 16
3628 * random bytes, so we just try to read the sequence number
3632 if (payload
== 18 && seq
== s
->tlsext_hb_seq
) {
3634 s
->tlsext_hb_pending
= 0;
3641 int tls1_heartbeat(SSL
*s
)
3643 unsigned char *buf
, *p
;
3645 unsigned int payload
= 18; /* Sequence number + random bytes */
3646 unsigned int padding
= 16; /* Use minimum padding */
3648 /* Only send if peer supports and accepts HB requests... */
3649 if (!(s
->tlsext_heartbeat
& SSL_TLSEXT_HB_ENABLED
) ||
3650 s
->tlsext_heartbeat
& SSL_TLSEXT_HB_DONT_SEND_REQUESTS
) {
3651 SSLerr(SSL_F_TLS1_HEARTBEAT
, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT
);
3655 /* ...and there is none in flight yet... */
3656 if (s
->tlsext_hb_pending
) {
3657 SSLerr(SSL_F_TLS1_HEARTBEAT
, SSL_R_TLS_HEARTBEAT_PENDING
);
3661 /* ...and no handshake in progress. */
3662 if (SSL_in_init(s
) || s
->in_handshake
) {
3663 SSLerr(SSL_F_TLS1_HEARTBEAT
, SSL_R_UNEXPECTED_MESSAGE
);
3668 * Check if padding is too long, payload and padding must not exceed 2^14
3669 * - 3 = 16381 bytes in total.
3671 OPENSSL_assert(payload
+ padding
<= 16381);
3674 * Create HeartBeat message, we just use a sequence number
3675 * as payload to distuingish different messages and add
3676 * some random stuff.
3677 * - Message Type, 1 byte
3678 * - Payload Length, 2 bytes (unsigned int)
3679 * - Payload, the sequence number (2 bytes uint)
3680 * - Payload, random bytes (16 bytes uint)
3683 buf
= OPENSSL_malloc(1 + 2 + payload
+ padding
);
3685 SSLerr(SSL_F_TLS1_HEARTBEAT
, ERR_R_MALLOC_FAILURE
);
3690 *p
++ = TLS1_HB_REQUEST
;
3691 /* Payload length (18 bytes here) */
3693 /* Sequence number */
3694 s2n(s
->tlsext_hb_seq
, p
);
3695 /* 16 random bytes */
3696 if (RAND_bytes(p
, 16) <= 0) {
3697 SSLerr(SSL_F_TLS1_HEARTBEAT
, ERR_R_INTERNAL_ERROR
);
3701 /* Random padding */
3702 if (RAND_bytes(p
, padding
) <= 0) {
3703 SSLerr(SSL_F_TLS1_HEARTBEAT
, ERR_R_INTERNAL_ERROR
);
3707 ret
= ssl3_write_bytes(s
, TLS1_RT_HEARTBEAT
, buf
, 3 + payload
+ padding
);
3709 if (s
->msg_callback
)
3710 s
->msg_callback(1, s
->version
, TLS1_RT_HEARTBEAT
,
3711 buf
, 3 + payload
+ padding
,
3712 s
, s
->msg_callback_arg
);
3714 s
->tlsext_hb_pending
= 1;
3723 # define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3727 int sigalgs
[MAX_SIGALGLEN
];
3730 static int sig_cb(const char *elem
, int len
, void *arg
)
3732 sig_cb_st
*sarg
= arg
;
3735 int sig_alg
, hash_alg
;
3738 if (sarg
->sigalgcnt
== MAX_SIGALGLEN
)
3740 if (len
> (int)(sizeof(etmp
) - 1))
3742 memcpy(etmp
, elem
, len
);
3744 p
= strchr(etmp
, '+');
3752 if (!strcmp(etmp
, "RSA"))
3753 sig_alg
= EVP_PKEY_RSA
;
3754 else if (!strcmp(etmp
, "DSA"))
3755 sig_alg
= EVP_PKEY_DSA
;
3756 else if (!strcmp(etmp
, "ECDSA"))
3757 sig_alg
= EVP_PKEY_EC
;
3761 hash_alg
= OBJ_sn2nid(p
);
3762 if (hash_alg
== NID_undef
)
3763 hash_alg
= OBJ_ln2nid(p
);
3764 if (hash_alg
== NID_undef
)
3767 for (i
= 0; i
< sarg
->sigalgcnt
; i
+= 2) {
3768 if (sarg
->sigalgs
[i
] == sig_alg
&& sarg
->sigalgs
[i
+ 1] == hash_alg
)
3771 sarg
->sigalgs
[sarg
->sigalgcnt
++] = hash_alg
;
3772 sarg
->sigalgs
[sarg
->sigalgcnt
++] = sig_alg
;
3777 * Set suppored signature algorithms based on a colon separated list of the
3778 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3780 int tls1_set_sigalgs_list(CERT
*c
, const char *str
, int client
)
3784 if (!CONF_parse_list(str
, ':', 1, sig_cb
, &sig
))
3788 return tls1_set_sigalgs(c
, sig
.sigalgs
, sig
.sigalgcnt
, client
);
3791 int tls1_set_sigalgs(CERT
*c
, const int *psig_nids
, size_t salglen
,
3794 unsigned char *sigalgs
, *sptr
;
3799 sigalgs
= OPENSSL_malloc(salglen
);
3800 if (sigalgs
== NULL
)
3802 for (i
= 0, sptr
= sigalgs
; i
< salglen
; i
+= 2) {
3803 rhash
= tls12_find_id(*psig_nids
++, tls12_md
, OSSL_NELEM(tls12_md
));
3804 rsign
= tls12_find_id(*psig_nids
++, tls12_sig
, OSSL_NELEM(tls12_sig
));
3806 if (rhash
== -1 || rsign
== -1)
3813 OPENSSL_free(c
->client_sigalgs
);
3814 c
->client_sigalgs
= sigalgs
;
3815 c
->client_sigalgslen
= salglen
;
3817 OPENSSL_free(c
->conf_sigalgs
);
3818 c
->conf_sigalgs
= sigalgs
;
3819 c
->conf_sigalgslen
= salglen
;
3825 OPENSSL_free(sigalgs
);
3829 static int tls1_check_sig_alg(CERT
*c
, X509
*x
, int default_nid
)
3833 if (default_nid
== -1)
3835 sig_nid
= X509_get_signature_nid(x
);
3837 return sig_nid
== default_nid
? 1 : 0;
3838 for (i
= 0; i
< c
->shared_sigalgslen
; i
++)
3839 if (sig_nid
== c
->shared_sigalgs
[i
].signandhash_nid
)
3844 /* Check to see if a certificate issuer name matches list of CA names */
3845 static int ssl_check_ca_name(STACK_OF(X509_NAME
) *names
, X509
*x
)
3849 nm
= X509_get_issuer_name(x
);
3850 for (i
= 0; i
< sk_X509_NAME_num(names
); i
++) {
3851 if (!X509_NAME_cmp(nm
, sk_X509_NAME_value(names
, i
)))
3858 * Check certificate chain is consistent with TLS extensions and is usable by
3859 * server. This servers two purposes: it allows users to check chains before
3860 * passing them to the server and it allows the server to check chains before
3861 * attempting to use them.
3864 /* Flags which need to be set for a certificate when stict mode not set */
3866 # define CERT_PKEY_VALID_FLAGS \
3867 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3868 /* Strict mode flags */
3869 # define CERT_PKEY_STRICT_FLAGS \
3870 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3871 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3873 int tls1_check_chain(SSL
*s
, X509
*x
, EVP_PKEY
*pk
, STACK_OF(X509
) *chain
,
3878 int check_flags
= 0, strict_mode
;
3879 CERT_PKEY
*cpk
= NULL
;
3881 unsigned int suiteb_flags
= tls1_suiteb(s
);
3882 /* idx == -1 means checking server chains */
3884 /* idx == -2 means checking client certificate chains */
3887 idx
= cpk
- c
->pkeys
;
3889 cpk
= c
->pkeys
+ idx
;
3891 pk
= cpk
->privatekey
;
3893 strict_mode
= c
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
;
3894 /* If no cert or key, forget it */
3897 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3898 /* Allow any certificate to pass test */
3899 if (s
->cert
->cert_flags
& SSL_CERT_FLAG_BROKEN_PROTOCOL
) {
3900 rv
= CERT_PKEY_STRICT_FLAGS
| CERT_PKEY_EXPLICIT_SIGN
|
3901 CERT_PKEY_VALID
| CERT_PKEY_SIGN
;
3902 cpk
->valid_flags
= rv
;
3909 idx
= ssl_cert_type(x
, pk
);
3912 cpk
= c
->pkeys
+ idx
;
3913 if (c
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
)
3914 check_flags
= CERT_PKEY_STRICT_FLAGS
;
3916 check_flags
= CERT_PKEY_VALID_FLAGS
;
3923 check_flags
|= CERT_PKEY_SUITEB
;
3924 ok
= X509_chain_check_suiteb(NULL
, x
, chain
, suiteb_flags
);
3925 if (ok
== X509_V_OK
)
3926 rv
|= CERT_PKEY_SUITEB
;
3927 else if (!check_flags
)
3932 * Check all signature algorithms are consistent with signature
3933 * algorithms extension if TLS 1.2 or later and strict mode.
3935 if (TLS1_get_version(s
) >= TLS1_2_VERSION
&& strict_mode
) {
3937 unsigned char rsign
= 0;
3938 if (c
->peer_sigalgs
)
3940 /* If no sigalgs extension use defaults from RFC5246 */
3943 case SSL_PKEY_RSA_ENC
:
3944 case SSL_PKEY_RSA_SIGN
:
3945 case SSL_PKEY_DH_RSA
:
3946 rsign
= TLSEXT_signature_rsa
;
3947 default_nid
= NID_sha1WithRSAEncryption
;
3950 case SSL_PKEY_DSA_SIGN
:
3951 case SSL_PKEY_DH_DSA
:
3952 rsign
= TLSEXT_signature_dsa
;
3953 default_nid
= NID_dsaWithSHA1
;
3957 rsign
= TLSEXT_signature_ecdsa
;
3958 default_nid
= NID_ecdsa_with_SHA1
;
3967 * If peer sent no signature algorithms extension and we have set
3968 * preferred signature algorithms check we support sha1.
3970 if (default_nid
> 0 && c
->conf_sigalgs
) {
3972 const unsigned char *p
= c
->conf_sigalgs
;
3973 for (j
= 0; j
< c
->conf_sigalgslen
; j
+= 2, p
+= 2) {
3974 if (p
[0] == TLSEXT_hash_sha1
&& p
[1] == rsign
)
3977 if (j
== c
->conf_sigalgslen
) {
3984 /* Check signature algorithm of each cert in chain */
3985 if (!tls1_check_sig_alg(c
, x
, default_nid
)) {
3989 rv
|= CERT_PKEY_EE_SIGNATURE
;
3990 rv
|= CERT_PKEY_CA_SIGNATURE
;
3991 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
3992 if (!tls1_check_sig_alg(c
, sk_X509_value(chain
, i
), default_nid
)) {
3994 rv
&= ~CERT_PKEY_CA_SIGNATURE
;
4001 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4002 else if (check_flags
)
4003 rv
|= CERT_PKEY_EE_SIGNATURE
| CERT_PKEY_CA_SIGNATURE
;
4005 /* Check cert parameters are consistent */
4006 if (tls1_check_cert_param(s
, x
, check_flags
? 1 : 2))
4007 rv
|= CERT_PKEY_EE_PARAM
;
4008 else if (!check_flags
)
4011 rv
|= CERT_PKEY_CA_PARAM
;
4012 /* In strict mode check rest of chain too */
4013 else if (strict_mode
) {
4014 rv
|= CERT_PKEY_CA_PARAM
;
4015 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
4016 X509
*ca
= sk_X509_value(chain
, i
);
4017 if (!tls1_check_cert_param(s
, ca
, 0)) {
4019 rv
&= ~CERT_PKEY_CA_PARAM
;
4026 if (!s
->server
&& strict_mode
) {
4027 STACK_OF(X509_NAME
) *ca_dn
;
4031 check_type
= TLS_CT_RSA_SIGN
;
4034 check_type
= TLS_CT_DSS_SIGN
;
4037 check_type
= TLS_CT_ECDSA_SIGN
;
4042 int cert_type
= X509_certificate_type(x
, pk
);
4043 if (cert_type
& EVP_PKS_RSA
)
4044 check_type
= TLS_CT_RSA_FIXED_DH
;
4045 if (cert_type
& EVP_PKS_DSA
)
4046 check_type
= TLS_CT_DSS_FIXED_DH
;
4050 const unsigned char *ctypes
;
4054 ctypelen
= (int)c
->ctype_num
;
4056 ctypes
= (unsigned char *)s
->s3
->tmp
.ctype
;
4057 ctypelen
= s
->s3
->tmp
.ctype_num
;
4059 for (i
= 0; i
< ctypelen
; i
++) {
4060 if (ctypes
[i
] == check_type
) {
4061 rv
|= CERT_PKEY_CERT_TYPE
;
4065 if (!(rv
& CERT_PKEY_CERT_TYPE
) && !check_flags
)
4068 rv
|= CERT_PKEY_CERT_TYPE
;
4070 ca_dn
= s
->s3
->tmp
.ca_names
;
4072 if (!sk_X509_NAME_num(ca_dn
))
4073 rv
|= CERT_PKEY_ISSUER_NAME
;
4075 if (!(rv
& CERT_PKEY_ISSUER_NAME
)) {
4076 if (ssl_check_ca_name(ca_dn
, x
))
4077 rv
|= CERT_PKEY_ISSUER_NAME
;
4079 if (!(rv
& CERT_PKEY_ISSUER_NAME
)) {
4080 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
4081 X509
*xtmp
= sk_X509_value(chain
, i
);
4082 if (ssl_check_ca_name(ca_dn
, xtmp
)) {
4083 rv
|= CERT_PKEY_ISSUER_NAME
;
4088 if (!check_flags
&& !(rv
& CERT_PKEY_ISSUER_NAME
))
4091 rv
|= CERT_PKEY_ISSUER_NAME
| CERT_PKEY_CERT_TYPE
;
4093 if (!check_flags
|| (rv
& check_flags
) == check_flags
)
4094 rv
|= CERT_PKEY_VALID
;
4098 if (TLS1_get_version(s
) >= TLS1_2_VERSION
) {
4099 if (cpk
->valid_flags
& CERT_PKEY_EXPLICIT_SIGN
)
4100 rv
|= CERT_PKEY_EXPLICIT_SIGN
| CERT_PKEY_SIGN
;
4101 else if (cpk
->digest
)
4102 rv
|= CERT_PKEY_SIGN
;
4104 rv
|= CERT_PKEY_SIGN
| CERT_PKEY_EXPLICIT_SIGN
;
4107 * When checking a CERT_PKEY structure all flags are irrelevant if the
4111 if (rv
& CERT_PKEY_VALID
)
4112 cpk
->valid_flags
= rv
;
4114 /* Preserve explicit sign flag, clear rest */
4115 cpk
->valid_flags
&= CERT_PKEY_EXPLICIT_SIGN
;
4122 /* Set validity of certificates in an SSL structure */
4123 void tls1_set_cert_validity(SSL
*s
)
4125 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_RSA_ENC
);
4126 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_RSA_SIGN
);
4127 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_DSA_SIGN
);
4128 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_DH_RSA
);
4129 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_DH_DSA
);
4130 tls1_check_chain(s
, NULL
, NULL
, NULL
, SSL_PKEY_ECC
);
4133 /* User level utiity function to check a chain is suitable */
4134 int SSL_check_chain(SSL
*s
, X509
*x
, EVP_PKEY
*pk
, STACK_OF(X509
) *chain
)
4136 return tls1_check_chain(s
, x
, pk
, chain
, -1);
4141 #ifndef OPENSSL_NO_DH
4142 DH
*ssl_get_auto_dh(SSL
*s
)
4144 int dh_secbits
= 80;
4145 if (s
->cert
->dh_tmp_auto
== 2)
4146 return DH_get_1024_160();
4147 if (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
) {
4148 if (s
->s3
->tmp
.new_cipher
->strength_bits
== 256)
4153 CERT_PKEY
*cpk
= ssl_get_server_send_pkey(s
);
4154 dh_secbits
= EVP_PKEY_security_bits(cpk
->privatekey
);
4157 if (dh_secbits
>= 128) {
4163 BN_set_word(dhp
->g
, 2);
4164 if (dh_secbits
>= 192)
4165 dhp
->p
= get_rfc3526_prime_8192(NULL
);
4167 dhp
->p
= get_rfc3526_prime_3072(NULL
);
4168 if (!dhp
->p
|| !dhp
->g
) {
4174 if (dh_secbits
>= 112)
4175 return DH_get_2048_224();
4176 return DH_get_1024_160();
4180 static int ssl_security_cert_key(SSL
*s
, SSL_CTX
*ctx
, X509
*x
, int op
)
4183 EVP_PKEY
*pkey
= X509_get_pubkey(x
);
4185 secbits
= EVP_PKEY_security_bits(pkey
);
4186 EVP_PKEY_free(pkey
);
4190 return ssl_security(s
, op
, secbits
, 0, x
);
4192 return ssl_ctx_security(ctx
, op
, secbits
, 0, x
);
4195 static int ssl_security_cert_sig(SSL
*s
, SSL_CTX
*ctx
, X509
*x
, int op
)
4197 /* Lookup signature algorithm digest */
4198 int secbits
= -1, md_nid
= NID_undef
, sig_nid
;
4199 sig_nid
= X509_get_signature_nid(x
);
4200 if (sig_nid
&& OBJ_find_sigid_algs(sig_nid
, &md_nid
, NULL
)) {
4202 if (md_nid
&& (md
= EVP_get_digestbynid(md_nid
)))
4203 secbits
= EVP_MD_size(md
) * 4;
4206 return ssl_security(s
, op
, secbits
, md_nid
, x
);
4208 return ssl_ctx_security(ctx
, op
, secbits
, md_nid
, x
);
4211 int ssl_security_cert(SSL
*s
, SSL_CTX
*ctx
, X509
*x
, int vfy
, int is_ee
)
4214 vfy
= SSL_SECOP_PEER
;
4216 if (!ssl_security_cert_key(s
, ctx
, x
, SSL_SECOP_EE_KEY
| vfy
))
4217 return SSL_R_EE_KEY_TOO_SMALL
;
4219 if (!ssl_security_cert_key(s
, ctx
, x
, SSL_SECOP_CA_KEY
| vfy
))
4220 return SSL_R_CA_KEY_TOO_SMALL
;
4222 if (!ssl_security_cert_sig(s
, ctx
, x
, SSL_SECOP_CA_MD
| vfy
))
4223 return SSL_R_CA_MD_TOO_WEAK
;
4228 * Check security of a chain, if sk includes the end entity certificate then
4229 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4230 * one to the peer. Return values: 1 if ok otherwise error code to use
4233 int ssl_security_cert_chain(SSL
*s
, STACK_OF(X509
) *sk
, X509
*x
, int vfy
)
4235 int rv
, start_idx
, i
;
4237 x
= sk_X509_value(sk
, 0);
4242 rv
= ssl_security_cert(s
, NULL
, x
, vfy
, 1);
4246 for (i
= start_idx
; i
< sk_X509_num(sk
); i
++) {
4247 x
= sk_X509_value(sk
, i
);
4248 rv
= ssl_security_cert(s
, NULL
, x
, vfy
, 0);