1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include <openssl/ocsp.h>
117 #include <openssl/rand.h>
118 #ifndef OPENSSL_NO_DH
119 # include <openssl/dh.h>
120 # include <openssl/bn.h>
122 #include "ssl_locl.h"
124 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
125 const unsigned char *sess_id, int sesslen,
126 SSL_SESSION **psess);
127 static int ssl_check_clienthello_tlsext_early(SSL *s);
128 static int ssl_check_serverhello_tlsext(SSL *s);
130 SSL3_ENC_METHOD const TLSv1_enc_data = {
133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
137 TLS1_FINISH_MAC_LENGTH,
138 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_export_keying_material,
143 SSL3_HM_HEADER_LENGTH,
144 ssl3_set_handshake_header,
148 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
151 tls1_setup_key_block,
152 tls1_generate_master_secret,
153 tls1_change_cipher_state,
154 tls1_final_finish_mac,
155 TLS1_FINISH_MAC_LENGTH,
156 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
166 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
169 tls1_setup_key_block,
170 tls1_generate_master_secret,
171 tls1_change_cipher_state,
172 tls1_final_finish_mac,
173 TLS1_FINISH_MAC_LENGTH,
174 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
175 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
177 tls1_export_keying_material,
178 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
179 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
180 SSL3_HM_HEADER_LENGTH,
181 ssl3_set_handshake_header,
185 long tls1_default_timeout(void)
188 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
189 * http, the cache would over fill
191 return (60 * 60 * 2);
198 s->method->ssl_clear(s);
202 void tls1_free(SSL *s)
204 OPENSSL_free(s->tlsext_session_ticket);
208 void tls1_clear(SSL *s)
211 if (s->method->version == TLS_ANY_VERSION)
212 s->version = TLS_MAX_VERSION;
214 s->version = s->method->version;
217 #ifndef OPENSSL_NO_EC
220 int nid; /* Curve NID */
221 int secbits; /* Bits of security (from SP800-57) */
222 unsigned int flags; /* Flags: currently just field type */
225 /* Mask for curve type */
226 # define TLS_CURVE_TYPE 0x3
227 # define TLS_CURVE_PRIME 0x0
228 # define TLS_CURVE_CHAR2 0x1
229 # define TLS_CURVE_CUSTOM 0x2
232 * Table of curve information.
233 * NB: do not delete entries or reorder this array. It is used as a lookup
234 * table: the index of each entry is one less than the TLS curve id.
237 static const tls_curve_info nid_list[] = {
238 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
239 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
240 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
241 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
242 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
243 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
244 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
245 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
246 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
247 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
248 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
249 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
250 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
251 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
252 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
253 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
254 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
255 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
256 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
257 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
258 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
259 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
260 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
261 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
262 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
263 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
264 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
265 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
267 {NID_X25519, 128, TLS_CURVE_CUSTOM},
270 static const unsigned char ecformats_default[] = {
271 TLSEXT_ECPOINTFORMAT_uncompressed,
272 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
273 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
276 /* The default curves */
277 static const unsigned char eccurves_default[] = {
278 0, 29, /* X25519 (29) */
279 /* Prefer P-256 which has the fastest and most secure implementations. */
280 0, 23, /* secp256r1 (23) */
281 /* Other >= 256-bit prime curves. */
282 0, 25, /* secp521r1 (25) */
283 0, 28, /* brainpool512r1 (28) */
284 0, 27, /* brainpoolP384r1 (27) */
285 0, 24, /* secp384r1 (24) */
286 0, 26, /* brainpoolP256r1 (26) */
287 0, 22, /* secp256k1 (22) */
288 /* >= 256-bit binary curves. */
289 0, 14, /* sect571r1 (14) */
290 0, 13, /* sect571k1 (13) */
291 0, 11, /* sect409k1 (11) */
292 0, 12, /* sect409r1 (12) */
293 0, 9, /* sect283k1 (9) */
294 0, 10, /* sect283r1 (10) */
297 static const unsigned char eccurves_all[] = {
298 0, 29, /* X25519 (29) */
299 /* Prefer P-256 which has the fastest and most secure implementations. */
300 0, 23, /* secp256r1 (23) */
301 /* Other >= 256-bit prime curves. */
302 0, 25, /* secp521r1 (25) */
303 0, 28, /* brainpool512r1 (28) */
304 0, 27, /* brainpoolP384r1 (27) */
305 0, 24, /* secp384r1 (24) */
306 0, 26, /* brainpoolP256r1 (26) */
307 0, 22, /* secp256k1 (22) */
308 /* >= 256-bit binary curves. */
309 0, 14, /* sect571r1 (14) */
310 0, 13, /* sect571k1 (13) */
311 0, 11, /* sect409k1 (11) */
312 0, 12, /* sect409r1 (12) */
313 0, 9, /* sect283k1 (9) */
314 0, 10, /* sect283r1 (10) */
316 * Remaining curves disabled by default but still permitted if set
317 * via an explicit callback or parameters.
319 0, 20, /* secp224k1 (20) */
320 0, 21, /* secp224r1 (21) */
321 0, 18, /* secp192k1 (18) */
322 0, 19, /* secp192r1 (19) */
323 0, 15, /* secp160k1 (15) */
324 0, 16, /* secp160r1 (16) */
325 0, 17, /* secp160r2 (17) */
326 0, 8, /* sect239k1 (8) */
327 0, 6, /* sect233k1 (6) */
328 0, 7, /* sect233r1 (7) */
329 0, 4, /* sect193r1 (4) */
330 0, 5, /* sect193r2 (5) */
331 0, 1, /* sect163k1 (1) */
332 0, 2, /* sect163r1 (2) */
333 0, 3, /* sect163r2 (3) */
337 static const unsigned char suiteb_curves[] = {
338 0, TLSEXT_curve_P_256,
339 0, TLSEXT_curve_P_384
342 int tls1_ec_curve_id2nid(int curve_id)
344 /* ECC curves from RFC 4492 and RFC 7027 */
345 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
347 return nid_list[curve_id - 1].nid;
350 int tls1_ec_nid2curve_id(int nid)
353 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
354 if (nid_list[i].nid == nid)
361 * Get curves list, if "sess" is set return client curves otherwise
363 * Sets |num_curves| to the number of curves in the list, i.e.,
364 * the length of |pcurves| is 2 * num_curves.
365 * Returns 1 on success and 0 if the client curves list has invalid format.
366 * The latter indicates an internal error: we should not be accepting such
367 * lists in the first place.
368 * TODO(emilia): we should really be storing the curves list in explicitly
369 * parsed form instead. (However, this would affect binary compatibility
370 * so cannot happen in the 1.0.x series.)
372 static int tls1_get_curvelist(SSL *s, int sess,
373 const unsigned char **pcurves,
376 size_t pcurveslen = 0;
378 *pcurves = s->session->tlsext_ellipticcurvelist;
379 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
381 /* For Suite B mode only include P-256, P-384 */
382 switch (tls1_suiteb(s)) {
383 case SSL_CERT_FLAG_SUITEB_128_LOS:
384 *pcurves = suiteb_curves;
385 pcurveslen = sizeof(suiteb_curves);
388 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
389 *pcurves = suiteb_curves;
393 case SSL_CERT_FLAG_SUITEB_192_LOS:
394 *pcurves = suiteb_curves + 2;
398 *pcurves = s->tlsext_ellipticcurvelist;
399 pcurveslen = s->tlsext_ellipticcurvelist_length;
402 *pcurves = eccurves_default;
403 pcurveslen = sizeof(eccurves_default);
407 /* We do not allow odd length arrays to enter the system. */
408 if (pcurveslen & 1) {
409 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
413 *num_curves = pcurveslen / 2;
418 /* See if curve is allowed by security callback */
419 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
421 const tls_curve_info *cinfo;
424 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
426 cinfo = &nid_list[curve[1] - 1];
427 # ifdef OPENSSL_NO_EC2M
428 if (cinfo->flags & TLS_CURVE_CHAR2)
431 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
434 /* Check a curve is one of our preferences */
435 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
437 const unsigned char *curves;
438 size_t num_curves, i;
439 unsigned int suiteb_flags = tls1_suiteb(s);
440 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
442 /* Check curve matches Suite B preferences */
444 unsigned long cid = s->s3->tmp.new_cipher->id;
447 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
448 if (p[2] != TLSEXT_curve_P_256)
450 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
451 if (p[2] != TLSEXT_curve_P_384)
453 } else /* Should never happen */
456 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
458 for (i = 0; i < num_curves; i++, curves += 2) {
459 if (p[1] == curves[0] && p[2] == curves[1])
460 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
466 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
467 * if there is no match.
468 * For nmatch == -1, return number of matches
469 * For nmatch == -2, return the NID of the curve to use for
470 * an EC tmp key, or NID_undef if there is no match.
472 int tls1_shared_curve(SSL *s, int nmatch)
474 const unsigned char *pref, *supp;
475 size_t num_pref, num_supp, i, j;
477 /* Can't do anything on client side */
481 if (tls1_suiteb(s)) {
483 * For Suite B ciphersuite determines curve: we already know
484 * these are acceptable due to previous checks.
486 unsigned long cid = s->s3->tmp.new_cipher->id;
487 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
488 return NID_X9_62_prime256v1; /* P-256 */
489 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
490 return NID_secp384r1; /* P-384 */
491 /* Should never happen */
494 /* If not Suite B just return first preference shared curve */
498 * Avoid truncation. tls1_get_curvelist takes an int
499 * but s->options is a long...
501 if (!tls1_get_curvelist
502 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
504 /* In practice, NID_undef == 0 but let's be precise. */
505 return nmatch == -1 ? 0 : NID_undef;
506 if (!tls1_get_curvelist
507 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
509 return nmatch == -1 ? 0 : NID_undef;
512 * If the client didn't send the elliptic_curves extension all of them
515 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
517 num_supp = sizeof(eccurves_all) / 2;
518 } else if (num_pref == 0 &&
519 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
521 num_pref = sizeof(eccurves_all) / 2;
525 for (i = 0; i < num_pref; i++, pref += 2) {
526 const unsigned char *tsupp = supp;
527 for (j = 0; j < num_supp; j++, tsupp += 2) {
528 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
529 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
532 int id = (pref[0] << 8) | pref[1];
533 return tls1_ec_curve_id2nid(id);
541 /* Out of range (nmatch > k). */
545 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
546 int *curves, size_t ncurves)
548 unsigned char *clist, *p;
551 * Bitmap of curves included to detect duplicates: only works while curve
554 unsigned long dup_list = 0;
555 clist = OPENSSL_malloc(ncurves * 2);
558 for (i = 0, p = clist; i < ncurves; i++) {
559 unsigned long idmask;
561 id = tls1_ec_nid2curve_id(curves[i]);
563 if (!id || (dup_list & idmask)) {
572 *pextlen = ncurves * 2;
576 # define MAX_CURVELIST 28
580 int nid_arr[MAX_CURVELIST];
583 static int nid_cb(const char *elem, int len, void *arg)
585 nid_cb_st *narg = arg;
591 if (narg->nidcnt == MAX_CURVELIST)
593 if (len > (int)(sizeof(etmp) - 1))
595 memcpy(etmp, elem, len);
597 nid = EC_curve_nist2nid(etmp);
598 if (nid == NID_undef)
599 nid = OBJ_sn2nid(etmp);
600 if (nid == NID_undef)
601 nid = OBJ_ln2nid(etmp);
602 if (nid == NID_undef)
604 for (i = 0; i < narg->nidcnt; i++)
605 if (narg->nid_arr[i] == nid)
607 narg->nid_arr[narg->nidcnt++] = nid;
611 /* Set curves based on a colon separate list */
612 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
617 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
621 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
624 /* For an EC key set TLS id and required compression based on parameters */
625 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
632 /* Determine if it is a prime field */
633 grp = EC_KEY_get0_group(ec);
636 /* Determine curve ID */
637 id = EC_GROUP_get_curve_name(grp);
638 id = tls1_ec_nid2curve_id(id);
639 /* If no id return error: we don't support arbitrary explicit curves */
643 curve_id[1] = (unsigned char)id;
645 if (EC_KEY_get0_public_key(ec) == NULL)
647 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
648 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
650 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
651 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
653 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
659 /* Check an EC key is compatible with extensions */
660 static int tls1_check_ec_key(SSL *s,
661 unsigned char *curve_id, unsigned char *comp_id)
663 const unsigned char *pformats, *pcurves;
664 size_t num_formats, num_curves, i;
667 * If point formats extension present check it, otherwise everything is
668 * supported (see RFC4492).
670 if (comp_id && s->session->tlsext_ecpointformatlist) {
671 pformats = s->session->tlsext_ecpointformatlist;
672 num_formats = s->session->tlsext_ecpointformatlist_length;
673 for (i = 0; i < num_formats; i++, pformats++) {
674 if (*comp_id == *pformats)
677 if (i == num_formats)
682 /* Check curve is consistent with client and server preferences */
683 for (j = 0; j <= 1; j++) {
684 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
686 if (j == 1 && num_curves == 0) {
688 * If we've not received any curves then skip this check.
689 * RFC 4492 does not require the supported elliptic curves extension
690 * so if it is not sent we can just choose any curve.
691 * It is invalid to send an empty list in the elliptic curves
692 * extension, so num_curves == 0 always means no extension.
696 for (i = 0; i < num_curves; i++, pcurves += 2) {
697 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
702 /* For clients can only check sent curve list */
709 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
713 * If we have a custom point format list use it otherwise use default
715 if (s->tlsext_ecpointformatlist) {
716 *pformats = s->tlsext_ecpointformatlist;
717 *num_formats = s->tlsext_ecpointformatlist_length;
719 *pformats = ecformats_default;
720 /* For Suite B we don't support char2 fields */
722 *num_formats = sizeof(ecformats_default) - 1;
724 *num_formats = sizeof(ecformats_default);
729 * Check cert parameters compatible with extensions: currently just checks EC
730 * certificates have compatible curves and compression.
732 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
734 unsigned char comp_id, curve_id[2];
737 pkey = X509_get0_pubkey(x);
740 /* If not EC nothing to do */
741 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
743 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
747 * Can't check curve_id for client certs as we don't have a supported
750 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
754 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
755 * SHA384+P-384, adjust digest if necessary.
757 if (set_ee_md && tls1_suiteb(s)) {
763 /* Check to see we have necessary signing algorithm */
764 if (curve_id[1] == TLSEXT_curve_P_256)
765 check_md = NID_ecdsa_with_SHA256;
766 else if (curve_id[1] == TLSEXT_curve_P_384)
767 check_md = NID_ecdsa_with_SHA384;
769 return 0; /* Should never happen */
770 for (i = 0; i < c->shared_sigalgslen; i++)
771 if (check_md == c->shared_sigalgs[i].signandhash_nid)
773 if (i == c->shared_sigalgslen)
775 if (set_ee_md == 2) {
776 if (check_md == NID_ecdsa_with_SHA256)
777 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
779 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
785 # ifndef OPENSSL_NO_EC
787 * tls1_check_ec_tmp_key - Check EC temporary key compatiblity
789 * @cid: Cipher ID we're considering using
791 * Checks that the kECDHE cipher suite we're considering using
792 * is compatible with the client extensions.
794 * Returns 0 when the cipher can't be used or 1 when it can.
796 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
799 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
802 if (tls1_suiteb(s)) {
803 unsigned char curve_id[2];
804 /* Curve to check determined by ciphersuite */
805 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
806 curve_id[1] = TLSEXT_curve_P_256;
807 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
808 curve_id[1] = TLSEXT_curve_P_384;
812 /* Check this curve is acceptable */
813 if (!tls1_check_ec_key(s, curve_id, NULL))
817 /* Need a shared curve */
818 if (tls1_shared_curve(s, 0))
822 # endif /* OPENSSL_NO_EC */
826 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
831 #endif /* OPENSSL_NO_EC */
834 * List of supported signature algorithms and hashes. Should make this
835 * customisable at some point, for now include everything we support.
838 #ifdef OPENSSL_NO_RSA
839 # define tlsext_sigalg_rsa(md) /* */
841 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
844 #ifdef OPENSSL_NO_DSA
845 # define tlsext_sigalg_dsa(md) /* */
847 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
851 # define tlsext_sigalg_ecdsa(md) /* */
853 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
856 #define tlsext_sigalg(md) \
857 tlsext_sigalg_rsa(md) \
858 tlsext_sigalg_dsa(md) \
859 tlsext_sigalg_ecdsa(md)
861 static const unsigned char tls12_sigalgs[] = {
862 tlsext_sigalg(TLSEXT_hash_sha512)
863 tlsext_sigalg(TLSEXT_hash_sha384)
864 tlsext_sigalg(TLSEXT_hash_sha256)
865 tlsext_sigalg(TLSEXT_hash_sha224)
866 tlsext_sigalg(TLSEXT_hash_sha1)
867 #ifndef OPENSSL_NO_GOST
868 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
869 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
870 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
874 #ifndef OPENSSL_NO_EC
875 static const unsigned char suiteb_sigalgs[] = {
876 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
877 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
880 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
883 * If Suite B mode use Suite B sigalgs only, ignore any other
886 #ifndef OPENSSL_NO_EC
887 switch (tls1_suiteb(s)) {
888 case SSL_CERT_FLAG_SUITEB_128_LOS:
889 *psigs = suiteb_sigalgs;
890 return sizeof(suiteb_sigalgs);
892 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
893 *psigs = suiteb_sigalgs;
896 case SSL_CERT_FLAG_SUITEB_192_LOS:
897 *psigs = suiteb_sigalgs + 2;
901 /* If server use client authentication sigalgs if not NULL */
902 if (s->server && s->cert->client_sigalgs) {
903 *psigs = s->cert->client_sigalgs;
904 return s->cert->client_sigalgslen;
905 } else if (s->cert->conf_sigalgs) {
906 *psigs = s->cert->conf_sigalgs;
907 return s->cert->conf_sigalgslen;
909 *psigs = tls12_sigalgs;
910 return sizeof(tls12_sigalgs);
915 * Check signature algorithm is consistent with sent supported signature
916 * algorithms and if so return relevant digest.
918 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
919 const unsigned char *sig, EVP_PKEY *pkey)
921 const unsigned char *sent_sigs;
922 size_t sent_sigslen, i;
923 int sigalg = tls12_get_sigid(pkey);
924 /* Should never happen */
927 /* Check key type is consistent with signature */
928 if (sigalg != (int)sig[1]) {
929 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
932 #ifndef OPENSSL_NO_EC
933 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
934 unsigned char curve_id[2], comp_id;
935 /* Check compression and curve matches extensions */
936 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
938 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
939 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
942 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
943 if (tls1_suiteb(s)) {
946 if (curve_id[1] == TLSEXT_curve_P_256) {
947 if (sig[0] != TLSEXT_hash_sha256) {
948 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
949 SSL_R_ILLEGAL_SUITEB_DIGEST);
952 } else if (curve_id[1] == TLSEXT_curve_P_384) {
953 if (sig[0] != TLSEXT_hash_sha384) {
954 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
955 SSL_R_ILLEGAL_SUITEB_DIGEST);
961 } else if (tls1_suiteb(s))
965 /* Check signature matches a type we sent */
966 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
967 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
968 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
971 /* Allow fallback to SHA1 if not strict mode */
972 if (i == sent_sigslen
973 && (sig[0] != TLSEXT_hash_sha1
974 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
975 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
978 *pmd = tls12_get_hash(sig[0]);
980 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
983 /* Make sure security callback allows algorithm */
984 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
985 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
987 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
991 * Store the digest used so applications can retrieve it if they wish.
993 s->s3->tmp.peer_md = *pmd;
998 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
999 * supported or doesn't appear in supported signature algorithms. Unlike
1000 * ssl_cipher_get_disabled this applies to a specific session and not global
1003 void ssl_set_client_disabled(SSL *s)
1005 s->s3->tmp.mask_a = 0;
1006 s->s3->tmp.mask_k = 0;
1007 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1008 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1009 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1011 s->s3->tmp.mask_ssl = 0;
1012 /* Disable TLS 1.0 ciphers if using SSL v3 */
1013 if (s->client_version == SSL3_VERSION)
1014 s->s3->tmp.mask_ssl |= SSL_TLSV1;
1015 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1016 # ifndef OPENSSL_NO_PSK
1017 /* with PSK there must be client callback set */
1018 if (!s->psk_client_callback) {
1019 s->s3->tmp.mask_a |= SSL_aPSK;
1020 s->s3->tmp.mask_k |= SSL_PSK;
1022 #endif /* OPENSSL_NO_PSK */
1023 #ifndef OPENSSL_NO_SRP
1024 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1025 s->s3->tmp.mask_a |= SSL_aSRP;
1026 s->s3->tmp.mask_k |= SSL_kSRP;
1031 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1033 if (c->algorithm_ssl & s->s3->tmp.mask_ssl
1034 || c->algorithm_mkey & s->s3->tmp.mask_k
1035 || c->algorithm_auth & s->s3->tmp.mask_a)
1037 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1040 static int tls_use_ticket(SSL *s)
1042 if (s->options & SSL_OP_NO_TICKET)
1044 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1047 static int compare_uint(const void *p1, const void *p2) {
1048 unsigned int u1 = *((const unsigned int *)p1);
1049 unsigned int u2 = *((const unsigned int *)p2);
1059 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
1060 * more than one extension of the same type in a ClientHello or ServerHello.
1061 * This function does an initial scan over the extensions block to filter those
1062 * out. It returns 1 if all extensions are unique, and 0 if the extensions
1063 * contain duplicates, could not be successfully parsed, or an internal error
1066 static int tls1_check_duplicate_extensions(const PACKET *packet) {
1067 PACKET extensions = *packet;
1068 size_t num_extensions = 0, i = 0;
1069 unsigned int *extension_types = NULL;
1072 /* First pass: count the extensions. */
1073 while (PACKET_remaining(&extensions) > 0) {
1076 if (!PACKET_get_net_2(&extensions, &type) ||
1077 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1083 if (num_extensions <= 1)
1086 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
1087 if (extension_types == NULL) {
1088 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
1092 /* Second pass: gather the extension types. */
1093 extensions = *packet;
1094 for (i = 0; i < num_extensions; i++) {
1096 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
1097 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1098 /* This should not happen. */
1099 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1104 if (PACKET_remaining(&extensions) != 0) {
1105 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1108 /* Sort the extensions and make sure there are no duplicates. */
1109 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1110 for (i = 1; i < num_extensions; i++) {
1111 if (extension_types[i - 1] == extension_types[i])
1116 OPENSSL_free(extension_types);
1120 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1121 unsigned char *limit, int *al)
1124 unsigned char *orig = buf;
1125 unsigned char *ret = buf;
1126 #ifndef OPENSSL_NO_EC
1127 /* See if we support any ECC ciphersuites */
1129 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1131 unsigned long alg_k, alg_a;
1132 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1134 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1135 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1137 alg_k = c->algorithm_mkey;
1138 alg_a = c->algorithm_auth;
1139 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1140 || (alg_a & SSL_aECDSA)) {
1151 return NULL; /* this really never occurs, but ... */
1153 /* Add RI if renegotiating */
1154 if (s->renegotiate) {
1157 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1158 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1162 if ((limit - ret - 4 - el) < 0)
1165 s2n(TLSEXT_TYPE_renegotiate, ret);
1168 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1169 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1175 /* Only add RI for SSLv3 */
1176 if (s->client_version == SSL3_VERSION)
1179 if (s->tlsext_hostname != NULL) {
1180 /* Add TLS extension servername to the Client Hello message */
1181 unsigned long size_str;
1185 * check for enough space.
1186 * 4 for the servername type and entension length
1187 * 2 for servernamelist length
1188 * 1 for the hostname type
1189 * 2 for hostname length
1193 if ((lenmax = limit - ret - 9) < 0
1195 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1198 /* extension type and length */
1199 s2n(TLSEXT_TYPE_server_name, ret);
1200 s2n(size_str + 5, ret);
1202 /* length of servername list */
1203 s2n(size_str + 3, ret);
1205 /* hostname type, length and hostname */
1206 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1208 memcpy(ret, s->tlsext_hostname, size_str);
1211 #ifndef OPENSSL_NO_SRP
1212 /* Add SRP username if there is one */
1213 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1214 * Client Hello message */
1216 int login_len = strlen(s->srp_ctx.login);
1217 if (login_len > 255 || login_len == 0) {
1218 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1223 * check for enough space.
1224 * 4 for the srp type type and entension length
1225 * 1 for the srp user identity
1226 * + srp user identity length
1228 if ((limit - ret - 5 - login_len) < 0)
1231 /* fill in the extension */
1232 s2n(TLSEXT_TYPE_srp, ret);
1233 s2n(login_len + 1, ret);
1234 (*ret++) = (unsigned char)login_len;
1235 memcpy(ret, s->srp_ctx.login, login_len);
1240 #ifndef OPENSSL_NO_EC
1243 * Add TLS extension ECPointFormats to the ClientHello message
1246 const unsigned char *pcurves, *pformats;
1247 size_t num_curves, num_formats, curves_list_len;
1249 unsigned char *etmp;
1251 tls1_get_formatlist(s, &pformats, &num_formats);
1253 if ((lenmax = limit - ret - 5) < 0)
1255 if (num_formats > (size_t)lenmax)
1257 if (num_formats > 255) {
1258 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1262 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1263 /* The point format list has 1-byte length. */
1264 s2n(num_formats + 1, ret);
1265 *(ret++) = (unsigned char)num_formats;
1266 memcpy(ret, pformats, num_formats);
1270 * Add TLS extension EllipticCurves to the ClientHello message
1272 pcurves = s->tlsext_ellipticcurvelist;
1273 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1276 if ((lenmax = limit - ret - 6) < 0)
1278 if (num_curves > (size_t)lenmax / 2)
1280 if (num_curves > 65532 / 2) {
1281 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1285 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1287 /* Copy curve ID if supported */
1288 for (i = 0; i < num_curves; i++, pcurves += 2) {
1289 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1290 *etmp++ = pcurves[0];
1291 *etmp++ = pcurves[1];
1295 curves_list_len = etmp - ret - 4;
1297 s2n(curves_list_len + 2, ret);
1298 s2n(curves_list_len, ret);
1299 ret += curves_list_len;
1301 #endif /* OPENSSL_NO_EC */
1303 if (tls_use_ticket(s)) {
1305 if (!s->new_session && s->session && s->session->tlsext_tick)
1306 ticklen = s->session->tlsext_ticklen;
1307 else if (s->session && s->tlsext_session_ticket &&
1308 s->tlsext_session_ticket->data) {
1309 ticklen = s->tlsext_session_ticket->length;
1310 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1311 if (s->session->tlsext_tick == NULL)
1313 memcpy(s->session->tlsext_tick,
1314 s->tlsext_session_ticket->data, ticklen);
1315 s->session->tlsext_ticklen = ticklen;
1318 if (ticklen == 0 && s->tlsext_session_ticket &&
1319 s->tlsext_session_ticket->data == NULL)
1322 * Check for enough room 2 for extension type, 2 for len rest for
1325 if ((long)(limit - ret - 4 - ticklen) < 0)
1327 s2n(TLSEXT_TYPE_session_ticket, ret);
1330 memcpy(ret, s->session->tlsext_tick, ticklen);
1336 if (SSL_USE_SIGALGS(s)) {
1338 const unsigned char *salg;
1339 unsigned char *etmp;
1340 salglen = tls12_get_psigalgs(s, &salg);
1341 if ((size_t)(limit - ret) < salglen + 6)
1343 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1345 /* Skip over lengths for now */
1347 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1348 /* Fill in lengths */
1349 s2n(salglen + 2, etmp);
1354 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1356 long extlen, idlen, itmp;
1360 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1361 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1362 itmp = i2d_OCSP_RESPID(id, NULL);
1368 if (s->tlsext_ocsp_exts) {
1369 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1375 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1377 s2n(TLSEXT_TYPE_status_request, ret);
1378 if (extlen + idlen > 0xFFF0)
1380 s2n(extlen + idlen + 5, ret);
1381 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1383 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1384 /* save position of id len */
1385 unsigned char *q = ret;
1386 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1387 /* skip over id len */
1389 itmp = i2d_OCSP_RESPID(id, &ret);
1395 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1397 #ifndef OPENSSL_NO_HEARTBEATS
1398 if (SSL_IS_DTLS(s)) {
1399 /* Add Heartbeat extension */
1400 if ((limit - ret - 4 - 1) < 0)
1402 s2n(TLSEXT_TYPE_heartbeat, ret);
1406 * 1: peer may send requests
1407 * 2: peer not allowed to send requests
1409 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1410 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1412 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1416 #ifndef OPENSSL_NO_NEXTPROTONEG
1417 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1419 * The client advertises an emtpy extension to indicate its support
1420 * for Next Protocol Negotiation
1422 if (limit - ret - 4 < 0)
1424 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1429 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1430 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1432 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1433 s2n(2 + s->alpn_client_proto_list_len, ret);
1434 s2n(s->alpn_client_proto_list_len, ret);
1435 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1436 ret += s->alpn_client_proto_list_len;
1438 #ifndef OPENSSL_NO_SRTP
1439 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1442 /* Returns 0 on success!! */
1443 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1444 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1448 if ((limit - ret - 4 - el) < 0)
1451 s2n(TLSEXT_TYPE_use_srtp, ret);
1454 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1455 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1461 custom_ext_init(&s->cert->cli_ext);
1462 /* Add custom TLS Extensions to ClientHello */
1463 if (!custom_ext_add(s, 0, &ret, limit, al))
1465 #ifdef TLSEXT_TYPE_encrypt_then_mac
1466 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1469 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1473 * Add padding to workaround bugs in F5 terminators. See
1474 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1475 * code works out the length of all existing extensions it MUST always
1478 if (s->options & SSL_OP_TLSEXT_PADDING) {
1479 int hlen = ret - (unsigned char *)s->init_buf->data;
1481 if (hlen > 0xff && hlen < 0x200) {
1482 hlen = 0x200 - hlen;
1488 s2n(TLSEXT_TYPE_padding, ret);
1490 memset(ret, 0, hlen);
1497 if ((extdatalen = ret - orig - 2) == 0)
1500 s2n(extdatalen, orig);
1504 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1505 unsigned char *limit, int *al)
1508 unsigned char *orig = buf;
1509 unsigned char *ret = buf;
1510 #ifndef OPENSSL_NO_NEXTPROTONEG
1511 int next_proto_neg_seen;
1513 #ifndef OPENSSL_NO_EC
1514 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1515 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1516 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1517 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1522 return NULL; /* this really never occurs, but ... */
1524 if (s->s3->send_connection_binding) {
1527 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1528 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1532 if ((limit - ret - 4 - el) < 0)
1535 s2n(TLSEXT_TYPE_renegotiate, ret);
1538 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1539 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1546 /* Only add RI for SSLv3 */
1547 if (s->version == SSL3_VERSION)
1550 if (!s->hit && s->servername_done == 1
1551 && s->session->tlsext_hostname != NULL) {
1552 if ((long)(limit - ret - 4) < 0)
1555 s2n(TLSEXT_TYPE_server_name, ret);
1558 #ifndef OPENSSL_NO_EC
1560 const unsigned char *plist;
1563 * Add TLS extension ECPointFormats to the ServerHello message
1567 tls1_get_formatlist(s, &plist, &plistlen);
1569 if ((lenmax = limit - ret - 5) < 0)
1571 if (plistlen > (size_t)lenmax)
1573 if (plistlen > 255) {
1574 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1578 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1579 s2n(plistlen + 1, ret);
1580 *(ret++) = (unsigned char)plistlen;
1581 memcpy(ret, plist, plistlen);
1586 * Currently the server should not respond with a SupportedCurves
1589 #endif /* OPENSSL_NO_EC */
1591 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1592 if ((long)(limit - ret - 4) < 0)
1594 s2n(TLSEXT_TYPE_session_ticket, ret);
1598 if (s->tlsext_status_expected) {
1599 if ((long)(limit - ret - 4) < 0)
1601 s2n(TLSEXT_TYPE_status_request, ret);
1605 #ifndef OPENSSL_NO_SRTP
1606 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1609 /* Returns 0 on success!! */
1610 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1611 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1614 if ((limit - ret - 4 - el) < 0)
1617 s2n(TLSEXT_TYPE_use_srtp, ret);
1620 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1621 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1628 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1629 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1630 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1631 const unsigned char cryptopro_ext[36] = {
1632 0xfd, 0xe8, /* 65000 */
1633 0x00, 0x20, /* 32 bytes length */
1634 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1635 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1636 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1637 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1639 if (limit - ret < 36)
1641 memcpy(ret, cryptopro_ext, 36);
1645 #ifndef OPENSSL_NO_HEARTBEATS
1646 /* Add Heartbeat extension if we've received one */
1647 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1648 if ((limit - ret - 4 - 1) < 0)
1650 s2n(TLSEXT_TYPE_heartbeat, ret);
1654 * 1: peer may send requests
1655 * 2: peer not allowed to send requests
1657 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1658 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1660 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1665 #ifndef OPENSSL_NO_NEXTPROTONEG
1666 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1667 s->s3->next_proto_neg_seen = 0;
1668 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1669 const unsigned char *npa;
1670 unsigned int npalen;
1673 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1675 ctx->next_protos_advertised_cb_arg);
1676 if (r == SSL_TLSEXT_ERR_OK) {
1677 if ((long)(limit - ret - 4 - npalen) < 0)
1679 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1681 memcpy(ret, npa, npalen);
1683 s->s3->next_proto_neg_seen = 1;
1687 if (!custom_ext_add(s, 1, &ret, limit, al))
1689 #ifdef TLSEXT_TYPE_encrypt_then_mac
1690 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1692 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1693 * for other cases too.
1695 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1696 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1697 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1698 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1699 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1701 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1706 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1707 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1711 if (s->s3->alpn_selected) {
1712 const unsigned char *selected = s->s3->alpn_selected;
1713 unsigned len = s->s3->alpn_selected_len;
1715 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1717 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1721 memcpy(ret, selected, len);
1727 if ((extdatalen = ret - orig - 2) == 0)
1730 s2n(extdatalen, orig);
1735 * Process the ALPN extension in a ClientHello.
1736 * pkt: the contents of the ALPN extension, not including type and length.
1737 * al: a pointer to the alert value to send in the event of a failure.
1738 * returns: 1 on success, 0 on error.
1740 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1742 const unsigned char *selected;
1743 unsigned char selected_len;
1745 PACKET protocol_list, save_protocol_list, protocol;
1747 *al = SSL_AD_DECODE_ERROR;
1749 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1750 || PACKET_remaining(&protocol_list) < 2) {
1754 save_protocol_list = protocol_list;
1756 /* Protocol names can't be empty. */
1757 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1758 || PACKET_remaining(&protocol) == 0) {
1761 } while (PACKET_remaining(&protocol_list) != 0);
1763 if (s->ctx->alpn_select_cb == NULL)
1766 r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1767 PACKET_data(&save_protocol_list),
1768 PACKET_remaining(&save_protocol_list),
1769 s->ctx->alpn_select_cb_arg);
1770 if (r == SSL_TLSEXT_ERR_OK) {
1771 OPENSSL_free(s->s3->alpn_selected);
1772 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1773 if (s->s3->alpn_selected == NULL) {
1774 *al = SSL_AD_INTERNAL_ERROR;
1777 memcpy(s->s3->alpn_selected, selected, selected_len);
1778 s->s3->alpn_selected_len = selected_len;
1780 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1787 #ifndef OPENSSL_NO_EC
1789 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1790 * SecureTransport using the TLS extension block in |pkt|.
1791 * Safari, since 10.6, sends exactly these extensions, in this order:
1796 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1797 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1798 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1799 * 10.8..10.8.3 (which don't work).
1801 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1807 static const unsigned char kSafariExtensionsBlock[] = {
1808 0x00, 0x0a, /* elliptic_curves extension */
1809 0x00, 0x08, /* 8 bytes */
1810 0x00, 0x06, /* 6 bytes of curve ids */
1811 0x00, 0x17, /* P-256 */
1812 0x00, 0x18, /* P-384 */
1813 0x00, 0x19, /* P-521 */
1815 0x00, 0x0b, /* ec_point_formats */
1816 0x00, 0x02, /* 2 bytes */
1817 0x01, /* 1 point format */
1818 0x00, /* uncompressed */
1819 /* The following is only present in TLS 1.2 */
1820 0x00, 0x0d, /* signature_algorithms */
1821 0x00, 0x0c, /* 12 bytes */
1822 0x00, 0x0a, /* 10 bytes */
1823 0x05, 0x01, /* SHA-384/RSA */
1824 0x04, 0x01, /* SHA-256/RSA */
1825 0x02, 0x01, /* SHA-1/RSA */
1826 0x04, 0x03, /* SHA-256/ECDSA */
1827 0x02, 0x03, /* SHA-1/ECDSA */
1830 /* Length of the common prefix (first two extensions). */
1831 static const size_t kSafariCommonExtensionsLength = 18;
1835 if (!PACKET_forward(&tmppkt, 2)
1836 || !PACKET_get_net_2(&tmppkt, &type)
1837 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1841 if (type != TLSEXT_TYPE_server_name)
1844 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1845 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1847 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1850 #endif /* !OPENSSL_NO_EC */
1853 * Parse ClientHello extensions and stash extension info in various parts of
1854 * the SSL object. Verify that there are no duplicate extensions.
1856 * Behaviour upon resumption is extension-specific. If the extension has no
1857 * effect during resumption, it is parsed (to verify its format) but otherwise
1860 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1861 * Upon failure, sets |al| to the appropriate alert.
1863 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1866 int renegotiate_seen = 0;
1869 *al = SSL_AD_DECODE_ERROR;
1870 s->servername_done = 0;
1871 s->tlsext_status_type = -1;
1872 #ifndef OPENSSL_NO_NEXTPROTONEG
1873 s->s3->next_proto_neg_seen = 0;
1876 OPENSSL_free(s->s3->alpn_selected);
1877 s->s3->alpn_selected = NULL;
1878 #ifndef OPENSSL_NO_HEARTBEATS
1879 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1880 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1883 #ifndef OPENSSL_NO_EC
1884 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1885 ssl_check_for_safari(s, pkt);
1886 # endif /* !OPENSSL_NO_EC */
1888 /* Clear any signature algorithms extension received */
1889 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1890 s->s3->tmp.peer_sigalgs = NULL;
1891 #ifdef TLSEXT_TYPE_encrypt_then_mac
1892 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1895 #ifndef OPENSSL_NO_SRP
1896 OPENSSL_free(s->srp_ctx.login);
1897 s->srp_ctx.login = NULL;
1900 s->srtp_profile = NULL;
1902 if (PACKET_remaining(pkt) == 0)
1905 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1908 if (!tls1_check_duplicate_extensions(&extensions))
1912 * We parse all extensions to ensure the ClientHello is well-formed but,
1913 * unless an extension specifies otherwise, we ignore extensions upon
1916 while (PACKET_get_net_2(&extensions, &type)) {
1918 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1921 if (s->tlsext_debug_cb)
1922 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1923 PACKET_remaining(&extension),
1924 s->tlsext_debug_arg);
1926 if (type == TLSEXT_TYPE_renegotiate) {
1927 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
1929 renegotiate_seen = 1;
1930 } else if (s->version == SSL3_VERSION) {
1933 * The servername extension is treated as follows:
1935 * - Only the hostname type is supported with a maximum length of 255.
1936 * - The servername is rejected if too long or if it contains zeros,
1937 * in which case an fatal alert is generated.
1938 * - The servername field is maintained together with the session cache.
1939 * - When a session is resumed, the servername call back invoked in order
1940 * to allow the application to position itself to the right context.
1941 * - The servername is acknowledged if it is new for a session or when
1942 * it is identical to a previously used for the same session.
1943 * Applications can control the behaviour. They can at any time
1944 * set a 'desirable' servername for a new SSL object. This can be the
1945 * case for example with HTTPS when a Host: header field is received and
1946 * a renegotiation is requested. In this case, a possible servername
1947 * presented in the new client hello is only acknowledged if it matches
1948 * the value of the Host: field.
1949 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1950 * if they provide for changing an explicit servername context for the
1951 * session, i.e. when the session has been established with a servername
1953 * - On session reconnect, the servername extension may be absent.
1957 else if (type == TLSEXT_TYPE_server_name) {
1958 unsigned int servname_type;
1959 PACKET sni, hostname;
1961 if (!PACKET_as_length_prefixed_2(&extension, &sni)
1962 /* ServerNameList must be at least 1 byte long. */
1963 || PACKET_remaining(&sni) == 0) {
1968 * Although the server_name extension was intended to be
1969 * extensible to new name types, RFC 4366 defined the
1970 * syntax inextensibly and OpenSSL 1.0.x parses it as
1972 * RFC 6066 corrected the mistake but adding new name types
1973 * is nevertheless no longer feasible, so act as if no other
1974 * SNI types can exist, to simplify parsing.
1976 * Also note that the RFC permits only one SNI value per type,
1977 * i.e., we can only have a single hostname.
1979 if (!PACKET_get_1(&sni, &servname_type)
1980 || servname_type != TLSEXT_NAMETYPE_host_name
1981 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
1986 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
1987 *al = TLS1_AD_UNRECOGNIZED_NAME;
1991 if (PACKET_contains_zero_byte(&hostname)) {
1992 *al = TLS1_AD_UNRECOGNIZED_NAME;
1996 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
1997 *al = TLS1_AD_INTERNAL_ERROR;
2001 s->servername_done = 1;
2004 * TODO(openssl-team): if the SNI doesn't match, we MUST
2005 * fall back to a full handshake.
2007 s->servername_done = s->session->tlsext_hostname
2008 && PACKET_equal(&hostname, s->session->tlsext_hostname,
2009 strlen(s->session->tlsext_hostname));
2012 #ifndef OPENSSL_NO_SRP
2013 else if (type == TLSEXT_TYPE_srp) {
2016 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
2019 if (PACKET_contains_zero_byte(&srp_I))
2023 * TODO(openssl-team): currently, we re-authenticate the user
2024 * upon resumption. Instead, we MUST ignore the login.
2026 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
2027 *al = TLS1_AD_INTERNAL_ERROR;
2033 #ifndef OPENSSL_NO_EC
2034 else if (type == TLSEXT_TYPE_ec_point_formats) {
2035 PACKET ec_point_format_list;
2037 if (!PACKET_as_length_prefixed_1(&extension,
2038 &ec_point_format_list)
2039 || PACKET_remaining(&ec_point_format_list) == 0) {
2044 if (!PACKET_memdup(&ec_point_format_list,
2045 &s->session->tlsext_ecpointformatlist,
2046 &s->session->tlsext_ecpointformatlist_length)) {
2047 *al = TLS1_AD_INTERNAL_ERROR;
2051 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2052 PACKET elliptic_curve_list;
2054 /* Each NamedCurve is 2 bytes and we must have at least 1. */
2055 if (!PACKET_as_length_prefixed_2(&extension,
2056 &elliptic_curve_list)
2057 || PACKET_remaining(&elliptic_curve_list) == 0
2058 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
2063 if (!PACKET_memdup(&elliptic_curve_list,
2064 &s->session->tlsext_ellipticcurvelist,
2065 &s->session->tlsext_ellipticcurvelist_length)) {
2066 *al = TLS1_AD_INTERNAL_ERROR;
2071 #endif /* OPENSSL_NO_EC */
2072 else if (type == TLSEXT_TYPE_session_ticket) {
2073 if (s->tls_session_ticket_ext_cb &&
2074 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
2075 PACKET_remaining(&extension),
2076 s->tls_session_ticket_ext_cb_arg)) {
2077 *al = TLS1_AD_INTERNAL_ERROR;
2080 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2081 PACKET supported_sig_algs;
2083 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2084 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2085 || PACKET_remaining(&supported_sig_algs) == 0) {
2090 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2091 PACKET_remaining(&supported_sig_algs))) {
2095 } else if (type == TLSEXT_TYPE_status_request) {
2096 const unsigned char *ext_data;
2098 if (!PACKET_get_1(&extension,
2099 (unsigned int *)&s->tlsext_status_type)) {
2103 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2104 PACKET responder_id_list, exts;
2105 if (!PACKET_get_length_prefixed_2(&extension, &responder_id_list))
2108 while (PACKET_remaining(&responder_id_list) > 0) {
2110 PACKET responder_id;
2111 const unsigned char *id_data;
2113 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2115 || PACKET_remaining(&responder_id) == 0) {
2119 if (s->tlsext_ocsp_ids == NULL
2120 && (s->tlsext_ocsp_ids =
2121 sk_OCSP_RESPID_new_null()) == NULL) {
2122 *al = SSL_AD_INTERNAL_ERROR;
2126 id_data = PACKET_data(&responder_id);
2127 id = d2i_OCSP_RESPID(NULL, &id_data,
2128 PACKET_remaining(&responder_id));
2132 if (id_data != PACKET_end(&responder_id)) {
2133 OCSP_RESPID_free(id);
2137 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2138 OCSP_RESPID_free(id);
2139 *al = SSL_AD_INTERNAL_ERROR;
2144 /* Read in request_extensions */
2145 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2148 if (PACKET_remaining(&exts) > 0) {
2149 ext_data = PACKET_data(&exts);
2150 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2151 X509_EXTENSION_free);
2152 s->tlsext_ocsp_exts =
2153 d2i_X509_EXTENSIONS(NULL, &ext_data,
2154 PACKET_remaining(&exts));
2155 if (s->tlsext_ocsp_exts == NULL
2156 || ext_data != PACKET_end(&exts)) {
2161 * We don't know what to do with any other type * so ignore it.
2164 s->tlsext_status_type = -1;
2167 #ifndef OPENSSL_NO_HEARTBEATS
2168 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2169 unsigned int hbtype;
2171 if (!PACKET_get_1(&extension, &hbtype)
2172 || PACKET_remaining(&extension)) {
2173 *al = SSL_AD_DECODE_ERROR;
2177 case 0x01: /* Client allows us to send HB requests */
2178 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2180 case 0x02: /* Client doesn't accept HB requests */
2181 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2182 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2185 *al = SSL_AD_ILLEGAL_PARAMETER;
2190 #ifndef OPENSSL_NO_NEXTPROTONEG
2191 else if (type == TLSEXT_TYPE_next_proto_neg &&
2192 s->s3->tmp.finish_md_len == 0 &&
2193 s->s3->alpn_selected == NULL) {
2195 * We shouldn't accept this extension on a
2198 * s->new_session will be set on renegotiation, but we
2199 * probably shouldn't rely that it couldn't be set on
2200 * the initial renegotation too in certain cases (when
2201 * there's some other reason to disallow resuming an
2202 * earlier session -- the current code won't be doing
2203 * anything like that, but this might change).
2205 * A valid sign that there's been a previous handshake
2206 * in this connection is if s->s3->tmp.finish_md_len >
2207 * 0. (We are talking about a check that will happen
2208 * in the Hello protocol round, well before a new
2209 * Finished message could have been computed.)
2211 s->s3->next_proto_neg_seen = 1;
2215 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2216 s->s3->tmp.finish_md_len == 0) {
2217 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2219 #ifndef OPENSSL_NO_NEXTPROTONEG
2220 /* ALPN takes precedence over NPN. */
2221 s->s3->next_proto_neg_seen = 0;
2225 /* session ticket processed earlier */
2226 #ifndef OPENSSL_NO_SRTP
2227 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2228 && type == TLSEXT_TYPE_use_srtp) {
2229 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2233 #ifdef TLSEXT_TYPE_encrypt_then_mac
2234 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2235 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2238 * Note: extended master secret extension handled in
2239 * tls_check_serverhello_tlsext_early()
2243 * If this ClientHello extension was unhandled and this is a
2244 * nonresumed connection, check whether the extension is a custom
2245 * TLS Extension (has a custom_srv_ext_record), and if so call the
2246 * callback and record the extension number so that an appropriate
2247 * ServerHello may be later returned.
2250 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2251 PACKET_remaining(&extension), al) <= 0)
2256 if (PACKET_remaining(pkt) != 0) {
2257 /* tls1_check_duplicate_extensions should ensure this never happens. */
2258 *al = SSL_AD_INTERNAL_ERROR;
2264 /* Need RI if renegotiating */
2266 if (!renegotiate_seen && s->renegotiate &&
2267 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2268 *al = SSL_AD_HANDSHAKE_FAILURE;
2269 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2270 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2275 * This function currently has no state to clean up, so it returns directly.
2276 * If parsing fails at any point, the function returns early.
2277 * The SSL object may be left with partial data from extensions, but it must
2278 * then no longer be used, and clearing it up will free the leftovers.
2283 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2286 custom_ext_init(&s->cert->srv_ext);
2287 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2288 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2291 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2292 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2298 #ifndef OPENSSL_NO_NEXTPROTONEG
2300 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2301 * elements of zero length are allowed and the set of elements must exactly
2302 * fill the length of the block.
2304 static char ssl_next_proto_validate(PACKET *pkt)
2308 while (PACKET_remaining(pkt)) {
2309 if (!PACKET_get_1(pkt, &len)
2310 || !PACKET_forward(pkt, len))
2318 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2320 unsigned int length, type, size;
2321 int tlsext_servername = 0;
2322 int renegotiate_seen = 0;
2324 #ifndef OPENSSL_NO_NEXTPROTONEG
2325 s->s3->next_proto_neg_seen = 0;
2327 s->tlsext_ticket_expected = 0;
2329 OPENSSL_free(s->s3->alpn_selected);
2330 s->s3->alpn_selected = NULL;
2331 #ifndef OPENSSL_NO_HEARTBEATS
2332 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2333 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2336 #ifdef TLSEXT_TYPE_encrypt_then_mac
2337 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2340 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2342 if (!PACKET_get_net_2(pkt, &length))
2345 if (PACKET_remaining(pkt) != length) {
2346 *al = SSL_AD_DECODE_ERROR;
2350 if (!tls1_check_duplicate_extensions(pkt)) {
2351 *al = SSL_AD_DECODE_ERROR;
2355 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2356 const unsigned char *data;
2359 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2360 || !PACKET_peek_bytes(&spkt, &data, size))
2363 if (s->tlsext_debug_cb)
2364 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2366 if (type == TLSEXT_TYPE_renegotiate) {
2367 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2369 renegotiate_seen = 1;
2370 } else if (s->version == SSL3_VERSION) {
2371 } else if (type == TLSEXT_TYPE_server_name) {
2372 if (s->tlsext_hostname == NULL || size > 0) {
2373 *al = TLS1_AD_UNRECOGNIZED_NAME;
2376 tlsext_servername = 1;
2378 #ifndef OPENSSL_NO_EC
2379 else if (type == TLSEXT_TYPE_ec_point_formats) {
2380 unsigned int ecpointformatlist_length;
2381 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2382 || ecpointformatlist_length != size - 1) {
2383 *al = TLS1_AD_DECODE_ERROR;
2387 s->session->tlsext_ecpointformatlist_length = 0;
2388 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2389 if ((s->session->tlsext_ecpointformatlist =
2390 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2391 *al = TLS1_AD_INTERNAL_ERROR;
2394 s->session->tlsext_ecpointformatlist_length =
2395 ecpointformatlist_length;
2396 if (!PACKET_copy_bytes(&spkt,
2397 s->session->tlsext_ecpointformatlist,
2398 ecpointformatlist_length)) {
2399 *al = TLS1_AD_DECODE_ERROR;
2405 #endif /* OPENSSL_NO_EC */
2407 else if (type == TLSEXT_TYPE_session_ticket) {
2408 if (s->tls_session_ticket_ext_cb &&
2409 !s->tls_session_ticket_ext_cb(s, data, size,
2410 s->tls_session_ticket_ext_cb_arg))
2412 *al = TLS1_AD_INTERNAL_ERROR;
2415 if (!tls_use_ticket(s) || (size > 0)) {
2416 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2419 s->tlsext_ticket_expected = 1;
2421 else if (type == TLSEXT_TYPE_status_request) {
2423 * MUST be empty and only sent if we've requested a status
2426 if ((s->tlsext_status_type == -1) || (size > 0)) {
2427 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2430 /* Set flag to expect CertificateStatus message */
2431 s->tlsext_status_expected = 1;
2433 #ifndef OPENSSL_NO_NEXTPROTONEG
2434 else if (type == TLSEXT_TYPE_next_proto_neg &&
2435 s->s3->tmp.finish_md_len == 0) {
2436 unsigned char *selected;
2437 unsigned char selected_len;
2438 /* We must have requested it. */
2439 if (s->ctx->next_proto_select_cb == NULL) {
2440 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2443 /* The data must be valid */
2444 if (!ssl_next_proto_validate(&spkt)) {
2445 *al = TLS1_AD_DECODE_ERROR;
2449 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2451 s->ctx->next_proto_select_cb_arg) !=
2452 SSL_TLSEXT_ERR_OK) {
2453 *al = TLS1_AD_INTERNAL_ERROR;
2456 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2457 if (s->next_proto_negotiated == NULL) {
2458 *al = TLS1_AD_INTERNAL_ERROR;
2461 memcpy(s->next_proto_negotiated, selected, selected_len);
2462 s->next_proto_negotiated_len = selected_len;
2463 s->s3->next_proto_neg_seen = 1;
2467 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2469 /* We must have requested it. */
2470 if (s->alpn_client_proto_list == NULL) {
2471 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2475 * The extension data consists of:
2476 * uint16 list_length
2477 * uint8 proto_length;
2478 * uint8 proto[proto_length];
2480 if (!PACKET_get_net_2(&spkt, &len)
2481 || PACKET_remaining(&spkt) != len
2482 || !PACKET_get_1(&spkt, &len)
2483 || PACKET_remaining(&spkt) != len) {
2484 *al = TLS1_AD_DECODE_ERROR;
2487 OPENSSL_free(s->s3->alpn_selected);
2488 s->s3->alpn_selected = OPENSSL_malloc(len);
2489 if (s->s3->alpn_selected == NULL) {
2490 *al = TLS1_AD_INTERNAL_ERROR;
2493 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2494 *al = TLS1_AD_DECODE_ERROR;
2497 s->s3->alpn_selected_len = len;
2499 #ifndef OPENSSL_NO_HEARTBEATS
2500 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2501 unsigned int hbtype;
2502 if (!PACKET_get_1(&spkt, &hbtype)) {
2503 *al = SSL_AD_DECODE_ERROR;
2507 case 0x01: /* Server allows us to send HB requests */
2508 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2510 case 0x02: /* Server doesn't accept HB requests */
2511 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2512 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2515 *al = SSL_AD_ILLEGAL_PARAMETER;
2520 #ifndef OPENSSL_NO_SRTP
2521 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2522 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2526 #ifdef TLSEXT_TYPE_encrypt_then_mac
2527 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2528 /* Ignore if inappropriate ciphersuite */
2529 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2530 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2531 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2534 else if (type == TLSEXT_TYPE_extended_master_secret) {
2535 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2537 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2540 * If this extension type was not otherwise handled, but matches a
2541 * custom_cli_ext_record, then send it to the c callback
2543 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2547 if (PACKET_remaining(pkt) != 0) {
2548 *al = SSL_AD_DECODE_ERROR;
2552 if (!s->hit && tlsext_servername == 1) {
2553 if (s->tlsext_hostname) {
2554 if (s->session->tlsext_hostname == NULL) {
2555 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
2556 if (!s->session->tlsext_hostname) {
2557 *al = SSL_AD_UNRECOGNIZED_NAME;
2561 *al = SSL_AD_DECODE_ERROR;
2570 * Determine if we need to see RI. Strictly speaking if we want to avoid
2571 * an attack we should *always* see RI even on initial server hello
2572 * because the client doesn't see any renegotiation during an attack.
2573 * However this would mean we could not connect to any server which
2574 * doesn't support RI so for the immediate future tolerate RI absence on
2575 * initial connect only.
2577 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2578 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2579 *al = SSL_AD_HANDSHAKE_FAILURE;
2580 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2581 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2587 * Check extended master secret extension is consistent with
2590 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2591 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2592 *al = SSL_AD_HANDSHAKE_FAILURE;
2593 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2601 int ssl_prepare_clienthello_tlsext(SSL *s)
2607 int ssl_prepare_serverhello_tlsext(SSL *s)
2612 static int ssl_check_clienthello_tlsext_early(SSL *s)
2614 int ret = SSL_TLSEXT_ERR_NOACK;
2615 int al = SSL_AD_UNRECOGNIZED_NAME;
2617 #ifndef OPENSSL_NO_EC
2619 * The handling of the ECPointFormats extension is done elsewhere, namely
2620 * in ssl3_choose_cipher in s3_lib.c.
2623 * The handling of the EllipticCurves extension is done elsewhere, namely
2624 * in ssl3_choose_cipher in s3_lib.c.
2628 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2630 s->ctx->tlsext_servername_callback(s, &al,
2631 s->ctx->tlsext_servername_arg);
2632 else if (s->initial_ctx != NULL
2633 && s->initial_ctx->tlsext_servername_callback != 0)
2635 s->initial_ctx->tlsext_servername_callback(s, &al,
2637 initial_ctx->tlsext_servername_arg);
2640 case SSL_TLSEXT_ERR_ALERT_FATAL:
2641 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2644 case SSL_TLSEXT_ERR_ALERT_WARNING:
2645 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2648 case SSL_TLSEXT_ERR_NOACK:
2649 s->servername_done = 0;
2654 /* Initialise digests to default values */
2655 void ssl_set_default_md(SSL *s)
2657 const EVP_MD **pmd = s->s3->tmp.md;
2658 #ifndef OPENSSL_NO_DSA
2659 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2661 #ifndef OPENSSL_NO_RSA
2662 if (SSL_USE_SIGALGS(s))
2663 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2665 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2666 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2668 #ifndef OPENSSL_NO_EC
2669 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2671 #ifndef OPENSSL_NO_GOST
2672 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2673 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2674 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2678 int tls1_set_server_sigalgs(SSL *s)
2682 /* Clear any shared sigtnature algorithms */
2683 OPENSSL_free(s->cert->shared_sigalgs);
2684 s->cert->shared_sigalgs = NULL;
2685 s->cert->shared_sigalgslen = 0;
2686 /* Clear certificate digests and validity flags */
2687 for (i = 0; i < SSL_PKEY_NUM; i++) {
2688 s->s3->tmp.md[i] = NULL;
2689 s->s3->tmp.valid_flags[i] = 0;
2692 /* If sigalgs received process it. */
2693 if (s->s3->tmp.peer_sigalgs) {
2694 if (!tls1_process_sigalgs(s)) {
2695 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2696 al = SSL_AD_INTERNAL_ERROR;
2699 /* Fatal error is no shared signature algorithms */
2700 if (!s->cert->shared_sigalgs) {
2701 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2702 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2703 al = SSL_AD_ILLEGAL_PARAMETER;
2707 ssl_set_default_md(s);
2711 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2715 int ssl_check_clienthello_tlsext_late(SSL *s)
2717 int ret = SSL_TLSEXT_ERR_OK;
2718 int al = SSL_AD_INTERNAL_ERROR;
2721 * If status request then ask callback what to do. Note: this must be
2722 * called after servername callbacks in case the certificate has changed,
2723 * and must be called after the cipher has been chosen because this may
2724 * influence which certificate is sent
2726 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2728 CERT_PKEY *certpkey;
2729 certpkey = ssl_get_server_send_pkey(s);
2730 /* If no certificate can't return certificate status */
2731 if (certpkey == NULL) {
2732 s->tlsext_status_expected = 0;
2736 * Set current certificate to one we will use so SSL_get_certificate
2737 * et al can pick it up.
2739 s->cert->key = certpkey;
2740 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2742 /* We don't want to send a status request response */
2743 case SSL_TLSEXT_ERR_NOACK:
2744 s->tlsext_status_expected = 0;
2746 /* status request response should be sent */
2747 case SSL_TLSEXT_ERR_OK:
2748 if (s->tlsext_ocsp_resp)
2749 s->tlsext_status_expected = 1;
2751 s->tlsext_status_expected = 0;
2753 /* something bad happened */
2754 case SSL_TLSEXT_ERR_ALERT_FATAL:
2755 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2756 al = SSL_AD_INTERNAL_ERROR;
2760 s->tlsext_status_expected = 0;
2764 case SSL_TLSEXT_ERR_ALERT_FATAL:
2765 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2768 case SSL_TLSEXT_ERR_ALERT_WARNING:
2769 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2777 int ssl_check_serverhello_tlsext(SSL *s)
2779 int ret = SSL_TLSEXT_ERR_NOACK;
2780 int al = SSL_AD_UNRECOGNIZED_NAME;
2782 #ifndef OPENSSL_NO_EC
2784 * If we are client and using an elliptic curve cryptography cipher
2785 * suite, then if server returns an EC point formats lists extension it
2786 * must contain uncompressed.
2788 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2789 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2790 if ((s->tlsext_ecpointformatlist != NULL)
2791 && (s->tlsext_ecpointformatlist_length > 0)
2792 && (s->session->tlsext_ecpointformatlist != NULL)
2793 && (s->session->tlsext_ecpointformatlist_length > 0)
2794 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2795 /* we are using an ECC cipher */
2797 unsigned char *list;
2798 int found_uncompressed = 0;
2799 list = s->session->tlsext_ecpointformatlist;
2800 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2801 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2802 found_uncompressed = 1;
2806 if (!found_uncompressed) {
2807 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2808 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2812 ret = SSL_TLSEXT_ERR_OK;
2813 #endif /* OPENSSL_NO_EC */
2815 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2817 s->ctx->tlsext_servername_callback(s, &al,
2818 s->ctx->tlsext_servername_arg);
2819 else if (s->initial_ctx != NULL
2820 && s->initial_ctx->tlsext_servername_callback != 0)
2822 s->initial_ctx->tlsext_servername_callback(s, &al,
2824 initial_ctx->tlsext_servername_arg);
2827 * Ensure we get sensible values passed to tlsext_status_cb in the event
2828 * that we don't receive a status message
2830 OPENSSL_free(s->tlsext_ocsp_resp);
2831 s->tlsext_ocsp_resp = NULL;
2832 s->tlsext_ocsp_resplen = -1;
2835 case SSL_TLSEXT_ERR_ALERT_FATAL:
2836 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2839 case SSL_TLSEXT_ERR_ALERT_WARNING:
2840 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2843 case SSL_TLSEXT_ERR_NOACK:
2844 s->servername_done = 0;
2850 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2853 if (s->version < SSL3_VERSION)
2855 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2856 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2860 if (ssl_check_serverhello_tlsext(s) <= 0) {
2861 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2868 * Since the server cache lookup is done early on in the processing of the
2869 * ClientHello and other operations depend on the result some extensions
2870 * need to be handled at the same time.
2872 * Two extensions are currently handled, session ticket and extended master
2875 * session_id: ClientHello session ID.
2876 * ext: ClientHello extensions (including length prefix)
2877 * ret: (output) on return, if a ticket was decrypted, then this is set to
2878 * point to the resulting session.
2880 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2881 * ciphersuite, in which case we have no use for session tickets and one will
2882 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2885 * -1: fatal error, either from parsing or decrypting the ticket.
2886 * 0: no ticket was found (or was ignored, based on settings).
2887 * 1: a zero length extension was found, indicating that the client supports
2888 * session tickets but doesn't currently have one to offer.
2889 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2890 * couldn't be decrypted because of a non-fatal error.
2891 * 3: a ticket was successfully decrypted and *ret was set.
2894 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2895 * a new session ticket to the client because the client indicated support
2896 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2897 * a session ticket or we couldn't use the one it gave us, or if
2898 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2899 * Otherwise, s->tlsext_ticket_expected is set to 0.
2901 * For extended master secret flag is set if the extension is present.
2904 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2905 const PACKET *session_id,
2909 PACKET local_ext = *ext;
2912 int have_ticket = 0;
2913 int use_ticket = tls_use_ticket(s);
2916 s->tlsext_ticket_expected = 0;
2917 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2920 * If tickets disabled behave as if no ticket present to permit stateful
2923 if ((s->version <= SSL3_VERSION))
2926 if (!PACKET_get_net_2(&local_ext, &i)) {
2930 while (PACKET_remaining(&local_ext) >= 4) {
2931 unsigned int type, size;
2933 if (!PACKET_get_net_2(&local_ext, &type)
2934 || !PACKET_get_net_2(&local_ext, &size)) {
2935 /* Shouldn't ever happen */
2939 if (PACKET_remaining(&local_ext) < size) {
2943 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
2945 const unsigned char *etick;
2947 /* Duplicate extension */
2948 if (have_ticket != 0) {
2956 * The client will accept a ticket but doesn't currently have
2959 s->tlsext_ticket_expected = 1;
2963 if (s->tls_session_secret_cb) {
2965 * Indicate that the ticket couldn't be decrypted rather than
2966 * generating the session from ticket now, trigger
2967 * abbreviated handshake based on external mechanism to
2968 * calculate the master secret later.
2973 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
2974 /* Shouldn't ever happen */
2978 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
2979 PACKET_remaining(session_id), ret);
2981 case 2: /* ticket couldn't be decrypted */
2982 s->tlsext_ticket_expected = 1;
2985 case 3: /* ticket was decrypted */
2988 case 4: /* ticket decrypted but need to renew */
2989 s->tlsext_ticket_expected = 1;
2992 default: /* fatal error */
2998 if (type == TLSEXT_TYPE_extended_master_secret)
2999 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
3000 if (!PACKET_forward(&local_ext, size)) {
3006 if (have_ticket == 0)
3013 * tls_decrypt_ticket attempts to decrypt a session ticket.
3015 * etick: points to the body of the session ticket extension.
3016 * eticklen: the length of the session tickets extenion.
3017 * sess_id: points at the session ID.
3018 * sesslen: the length of the session ID.
3019 * psess: (output) on return, if a ticket was decrypted, then this is set to
3020 * point to the resulting session.
3023 * -2: fatal error, malloc failure.
3024 * -1: fatal error, either from parsing or decrypting the ticket.
3025 * 2: the ticket couldn't be decrypted.
3026 * 3: a ticket was successfully decrypted and *psess was set.
3027 * 4: same as 3, but the ticket needs to be renewed.
3029 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3030 int eticklen, const unsigned char *sess_id,
3031 int sesslen, SSL_SESSION **psess)
3034 unsigned char *sdec;
3035 const unsigned char *p;
3036 int slen, mlen, renew_ticket = 0, ret = -1;
3037 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3038 HMAC_CTX *hctx = NULL;
3039 EVP_CIPHER_CTX *ctx;
3040 SSL_CTX *tctx = s->initial_ctx;
3041 /* Need at least keyname + iv + some encrypted data */
3044 /* Initialize session ticket encryption and HMAC contexts */
3045 hctx = HMAC_CTX_new();
3048 ctx = EVP_CIPHER_CTX_new();
3053 if (tctx->tlsext_ticket_key_cb) {
3054 unsigned char *nctick = (unsigned char *)etick;
3055 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3066 /* Check key name matches */
3067 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
3071 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16,
3072 EVP_sha256(), NULL) <= 0
3073 || EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL,
3074 tctx->tlsext_tick_aes_key,
3080 * Attempt to process session ticket, first conduct sanity and integrity
3083 mlen = HMAC_size(hctx);
3088 /* Check HMAC of encrypted ticket */
3089 if (HMAC_Update(hctx, etick, eticklen) <= 0
3090 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3093 HMAC_CTX_free(hctx);
3094 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3095 EVP_CIPHER_CTX_free(ctx);
3098 /* Attempt to decrypt session data */
3099 /* Move p after IV to start of encrypted ticket, update length */
3100 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3101 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3102 sdec = OPENSSL_malloc(eticklen);
3104 || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3105 EVP_CIPHER_CTX_free(ctx);
3108 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3109 EVP_CIPHER_CTX_free(ctx);
3114 EVP_CIPHER_CTX_free(ctx);
3118 sess = d2i_SSL_SESSION(NULL, &p, slen);
3122 * The session ID, if non-empty, is used by some clients to detect
3123 * that the ticket has been accepted. So we copy it to the session
3124 * structure. If it is empty set length to zero as required by
3128 memcpy(sess->session_id, sess_id, sesslen);
3129 sess->session_id_length = sesslen;
3138 * For session parse failure, indicate that we need to send a new ticket.
3142 EVP_CIPHER_CTX_free(ctx);
3143 HMAC_CTX_free(hctx);
3147 /* Tables to translate from NIDs to TLS v1.2 ids */
3154 static const tls12_lookup tls12_md[] = {
3155 {NID_md5, TLSEXT_hash_md5},
3156 {NID_sha1, TLSEXT_hash_sha1},
3157 {NID_sha224, TLSEXT_hash_sha224},
3158 {NID_sha256, TLSEXT_hash_sha256},
3159 {NID_sha384, TLSEXT_hash_sha384},
3160 {NID_sha512, TLSEXT_hash_sha512},
3161 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3162 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3163 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3166 static const tls12_lookup tls12_sig[] = {
3167 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3168 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3169 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3170 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3171 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3172 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3175 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3178 for (i = 0; i < tlen; i++) {
3179 if (table[i].nid == nid)
3185 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3188 for (i = 0; i < tlen; i++) {
3189 if ((table[i].id) == id)
3190 return table[i].nid;
3195 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3201 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3204 sig_id = tls12_get_sigid(pk);
3207 p[0] = (unsigned char)md_id;
3208 p[1] = (unsigned char)sig_id;
3212 int tls12_get_sigid(const EVP_PKEY *pk)
3214 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3221 unsigned char tlsext_hash;
3224 static const tls12_hash_info tls12_md_info[] = {
3225 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3226 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3227 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3228 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3229 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3230 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3231 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3232 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3233 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3236 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3242 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3244 if (tls12_md_info[i].tlsext_hash == hash_alg)
3245 return tls12_md_info + i;
3251 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3253 const tls12_hash_info *inf;
3254 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3256 inf = tls12_get_hash_info(hash_alg);
3259 return ssl_md(inf->md_idx);
3262 static int tls12_get_pkey_idx(unsigned char sig_alg)
3265 #ifndef OPENSSL_NO_RSA
3266 case TLSEXT_signature_rsa:
3267 return SSL_PKEY_RSA_SIGN;
3269 #ifndef OPENSSL_NO_DSA
3270 case TLSEXT_signature_dsa:
3271 return SSL_PKEY_DSA_SIGN;
3273 #ifndef OPENSSL_NO_EC
3274 case TLSEXT_signature_ecdsa:
3275 return SSL_PKEY_ECC;
3277 # ifndef OPENSSL_NO_GOST
3278 case TLSEXT_signature_gostr34102001:
3279 return SSL_PKEY_GOST01;
3281 case TLSEXT_signature_gostr34102012_256:
3282 return SSL_PKEY_GOST12_256;
3284 case TLSEXT_signature_gostr34102012_512:
3285 return SSL_PKEY_GOST12_512;
3291 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3292 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3293 int *psignhash_nid, const unsigned char *data)
3295 int sign_nid = NID_undef, hash_nid = NID_undef;
3296 if (!phash_nid && !psign_nid && !psignhash_nid)
3298 if (phash_nid || psignhash_nid) {
3299 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3301 *phash_nid = hash_nid;
3303 if (psign_nid || psignhash_nid) {
3304 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3306 *psign_nid = sign_nid;
3308 if (psignhash_nid) {
3309 if (sign_nid == NID_undef || hash_nid == NID_undef
3310 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3312 *psignhash_nid = NID_undef;
3316 /* Check to see if a signature algorithm is allowed */
3317 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3319 /* See if we have an entry in the hash table and it is enabled */
3320 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3321 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3323 /* See if public key algorithm allowed */
3324 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3326 /* Finally see if security callback allows it */
3327 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3331 * Get a mask of disabled public key algorithms based on supported signature
3332 * algorithms. For example if no signature algorithm supports RSA then RSA is
3336 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3338 const unsigned char *sigalgs;
3339 size_t i, sigalgslen;
3340 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3342 * Now go through all signature algorithms seeing if we support any for
3343 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3344 * down calls to security callback only check if we have to.
3346 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3347 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3348 switch (sigalgs[1]) {
3349 #ifndef OPENSSL_NO_RSA
3350 case TLSEXT_signature_rsa:
3351 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3355 #ifndef OPENSSL_NO_DSA
3356 case TLSEXT_signature_dsa:
3357 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3361 #ifndef OPENSSL_NO_EC
3362 case TLSEXT_signature_ecdsa:
3363 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3370 *pmask_a |= SSL_aRSA;
3372 *pmask_a |= SSL_aDSS;
3374 *pmask_a |= SSL_aECDSA;
3377 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3378 const unsigned char *psig, size_t psiglen)
3380 unsigned char *tmpout = out;
3382 for (i = 0; i < psiglen; i += 2, psig += 2) {
3383 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3384 *tmpout++ = psig[0];
3385 *tmpout++ = psig[1];
3388 return tmpout - out;
3391 /* Given preference and allowed sigalgs set shared sigalgs */
3392 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3393 const unsigned char *pref, size_t preflen,
3394 const unsigned char *allow, size_t allowlen)
3396 const unsigned char *ptmp, *atmp;
3397 size_t i, j, nmatch = 0;
3398 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3399 /* Skip disabled hashes or signature algorithms */
3400 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3402 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3403 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3406 shsig->rhash = ptmp[0];
3407 shsig->rsign = ptmp[1];
3408 tls1_lookup_sigalg(&shsig->hash_nid,
3410 &shsig->signandhash_nid, ptmp);
3420 /* Set shared signature algorithms for SSL structures */
3421 static int tls1_set_shared_sigalgs(SSL *s)
3423 const unsigned char *pref, *allow, *conf;
3424 size_t preflen, allowlen, conflen;
3426 TLS_SIGALGS *salgs = NULL;
3428 unsigned int is_suiteb = tls1_suiteb(s);
3430 OPENSSL_free(c->shared_sigalgs);
3431 c->shared_sigalgs = NULL;
3432 c->shared_sigalgslen = 0;
3433 /* If client use client signature algorithms if not NULL */
3434 if (!s->server && c->client_sigalgs && !is_suiteb) {
3435 conf = c->client_sigalgs;
3436 conflen = c->client_sigalgslen;
3437 } else if (c->conf_sigalgs && !is_suiteb) {
3438 conf = c->conf_sigalgs;
3439 conflen = c->conf_sigalgslen;
3441 conflen = tls12_get_psigalgs(s, &conf);
3442 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3445 allow = s->s3->tmp.peer_sigalgs;
3446 allowlen = s->s3->tmp.peer_sigalgslen;
3450 pref = s->s3->tmp.peer_sigalgs;
3451 preflen = s->s3->tmp.peer_sigalgslen;
3453 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3455 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3458 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3462 c->shared_sigalgs = salgs;
3463 c->shared_sigalgslen = nmatch;
3467 /* Set preferred digest for each key type */
3469 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3472 /* Extension ignored for inappropriate versions */
3473 if (!SSL_USE_SIGALGS(s))
3475 /* Should never happen */
3479 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3480 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3481 if (s->s3->tmp.peer_sigalgs == NULL)
3483 s->s3->tmp.peer_sigalgslen = dsize;
3484 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3488 int tls1_process_sigalgs(SSL *s)
3493 const EVP_MD **pmd = s->s3->tmp.md;
3494 uint32_t *pvalid = s->s3->tmp.valid_flags;
3496 TLS_SIGALGS *sigptr;
3497 if (!tls1_set_shared_sigalgs(s))
3500 for (i = 0, sigptr = c->shared_sigalgs;
3501 i < c->shared_sigalgslen; i++, sigptr++) {
3502 idx = tls12_get_pkey_idx(sigptr->rsign);
3503 if (idx > 0 && pmd[idx] == NULL) {
3504 md = tls12_get_hash(sigptr->rhash);
3506 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3507 if (idx == SSL_PKEY_RSA_SIGN) {
3508 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3509 pmd[SSL_PKEY_RSA_ENC] = md;
3515 * In strict mode leave unset digests as NULL to indicate we can't use
3516 * the certificate for signing.
3518 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3520 * Set any remaining keys to default values. NOTE: if alg is not
3521 * supported it stays as NULL.
3523 #ifndef OPENSSL_NO_DSA
3524 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3525 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3527 #ifndef OPENSSL_NO_RSA
3528 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3529 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3530 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3533 #ifndef OPENSSL_NO_EC
3534 if (pmd[SSL_PKEY_ECC] == NULL)
3535 pmd[SSL_PKEY_ECC] = EVP_sha1();
3537 # ifndef OPENSSL_NO_GOST
3538 if (pmd[SSL_PKEY_GOST01] == NULL)
3539 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3540 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3541 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3542 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3543 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3549 int SSL_get_sigalgs(SSL *s, int idx,
3550 int *psign, int *phash, int *psignhash,
3551 unsigned char *rsig, unsigned char *rhash)
3553 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3558 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3565 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3567 return s->s3->tmp.peer_sigalgslen / 2;
3570 int SSL_get_shared_sigalgs(SSL *s, int idx,
3571 int *psign, int *phash, int *psignhash,
3572 unsigned char *rsig, unsigned char *rhash)
3574 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3575 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3579 *phash = shsigalgs->hash_nid;
3581 *psign = shsigalgs->sign_nid;
3583 *psignhash = shsigalgs->signandhash_nid;
3585 *rsig = shsigalgs->rsign;
3587 *rhash = shsigalgs->rhash;
3588 return s->cert->shared_sigalgslen;
3591 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3595 int sigalgs[MAX_SIGALGLEN];
3598 static void get_sigorhash(int *psig, int *phash, const char *str)
3600 if (strcmp(str, "RSA") == 0) {
3601 *psig = EVP_PKEY_RSA;
3602 } else if (strcmp(str, "DSA") == 0) {
3603 *psig = EVP_PKEY_DSA;
3604 } else if (strcmp(str, "ECDSA") == 0) {
3605 *psig = EVP_PKEY_EC;
3607 *phash = OBJ_sn2nid(str);
3608 if (*phash == NID_undef)
3609 *phash = OBJ_ln2nid(str);
3613 static int sig_cb(const char *elem, int len, void *arg)
3615 sig_cb_st *sarg = arg;
3618 int sig_alg = NID_undef, hash_alg = NID_undef;
3621 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3623 if (len > (int)(sizeof(etmp) - 1))
3625 memcpy(etmp, elem, len);
3627 p = strchr(etmp, '+');
3635 get_sigorhash(&sig_alg, &hash_alg, etmp);
3636 get_sigorhash(&sig_alg, &hash_alg, p);
3638 if (sig_alg == NID_undef || hash_alg == NID_undef)
3641 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3642 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3645 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3646 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3651 * Set suppored signature algorithms based on a colon separated list of the
3652 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3654 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3658 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3662 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3665 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3668 unsigned char *sigalgs, *sptr;
3673 sigalgs = OPENSSL_malloc(salglen);
3674 if (sigalgs == NULL)
3676 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3677 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3678 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3680 if (rhash == -1 || rsign == -1)
3687 OPENSSL_free(c->client_sigalgs);
3688 c->client_sigalgs = sigalgs;
3689 c->client_sigalgslen = salglen;
3691 OPENSSL_free(c->conf_sigalgs);
3692 c->conf_sigalgs = sigalgs;
3693 c->conf_sigalgslen = salglen;
3699 OPENSSL_free(sigalgs);
3703 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3707 if (default_nid == -1)
3709 sig_nid = X509_get_signature_nid(x);
3711 return sig_nid == default_nid ? 1 : 0;
3712 for (i = 0; i < c->shared_sigalgslen; i++)
3713 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3718 /* Check to see if a certificate issuer name matches list of CA names */
3719 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3723 nm = X509_get_issuer_name(x);
3724 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3725 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3732 * Check certificate chain is consistent with TLS extensions and is usable by
3733 * server. This servers two purposes: it allows users to check chains before
3734 * passing them to the server and it allows the server to check chains before
3735 * attempting to use them.
3738 /* Flags which need to be set for a certificate when stict mode not set */
3740 #define CERT_PKEY_VALID_FLAGS \
3741 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3742 /* Strict mode flags */
3743 #define CERT_PKEY_STRICT_FLAGS \
3744 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3745 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3747 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3752 int check_flags = 0, strict_mode;
3753 CERT_PKEY *cpk = NULL;
3756 unsigned int suiteb_flags = tls1_suiteb(s);
3757 /* idx == -1 means checking server chains */
3759 /* idx == -2 means checking client certificate chains */
3762 idx = cpk - c->pkeys;
3764 cpk = c->pkeys + idx;
3765 pvalid = s->s3->tmp.valid_flags + idx;
3767 pk = cpk->privatekey;
3769 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3770 /* If no cert or key, forget it */
3776 idx = ssl_cert_type(x, pk);
3779 pvalid = s->s3->tmp.valid_flags + idx;
3781 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3782 check_flags = CERT_PKEY_STRICT_FLAGS;
3784 check_flags = CERT_PKEY_VALID_FLAGS;
3791 check_flags |= CERT_PKEY_SUITEB;
3792 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3793 if (ok == X509_V_OK)
3794 rv |= CERT_PKEY_SUITEB;
3795 else if (!check_flags)
3800 * Check all signature algorithms are consistent with signature
3801 * algorithms extension if TLS 1.2 or later and strict mode.
3803 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
3805 unsigned char rsign = 0;
3806 if (s->s3->tmp.peer_sigalgs)
3808 /* If no sigalgs extension use defaults from RFC5246 */
3811 case SSL_PKEY_RSA_ENC:
3812 case SSL_PKEY_RSA_SIGN:
3813 rsign = TLSEXT_signature_rsa;
3814 default_nid = NID_sha1WithRSAEncryption;
3817 case SSL_PKEY_DSA_SIGN:
3818 rsign = TLSEXT_signature_dsa;
3819 default_nid = NID_dsaWithSHA1;
3823 rsign = TLSEXT_signature_ecdsa;
3824 default_nid = NID_ecdsa_with_SHA1;
3827 case SSL_PKEY_GOST01:
3828 rsign = TLSEXT_signature_gostr34102001;
3829 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
3832 case SSL_PKEY_GOST12_256:
3833 rsign = TLSEXT_signature_gostr34102012_256;
3834 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
3837 case SSL_PKEY_GOST12_512:
3838 rsign = TLSEXT_signature_gostr34102012_512;
3839 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
3848 * If peer sent no signature algorithms extension and we have set
3849 * preferred signature algorithms check we support sha1.
3851 if (default_nid > 0 && c->conf_sigalgs) {
3853 const unsigned char *p = c->conf_sigalgs;
3854 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
3855 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
3858 if (j == c->conf_sigalgslen) {
3865 /* Check signature algorithm of each cert in chain */
3866 if (!tls1_check_sig_alg(c, x, default_nid)) {
3870 rv |= CERT_PKEY_EE_SIGNATURE;
3871 rv |= CERT_PKEY_CA_SIGNATURE;
3872 for (i = 0; i < sk_X509_num(chain); i++) {
3873 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
3875 rv &= ~CERT_PKEY_CA_SIGNATURE;
3882 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
3883 else if (check_flags)
3884 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
3886 /* Check cert parameters are consistent */
3887 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
3888 rv |= CERT_PKEY_EE_PARAM;
3889 else if (!check_flags)
3892 rv |= CERT_PKEY_CA_PARAM;
3893 /* In strict mode check rest of chain too */
3894 else if (strict_mode) {
3895 rv |= CERT_PKEY_CA_PARAM;
3896 for (i = 0; i < sk_X509_num(chain); i++) {
3897 X509 *ca = sk_X509_value(chain, i);
3898 if (!tls1_check_cert_param(s, ca, 0)) {
3900 rv &= ~CERT_PKEY_CA_PARAM;
3907 if (!s->server && strict_mode) {
3908 STACK_OF(X509_NAME) *ca_dn;
3910 switch (EVP_PKEY_id(pk)) {
3912 check_type = TLS_CT_RSA_SIGN;
3915 check_type = TLS_CT_DSS_SIGN;
3918 check_type = TLS_CT_ECDSA_SIGN;
3922 const unsigned char *ctypes;
3926 ctypelen = (int)c->ctype_num;
3928 ctypes = (unsigned char *)s->s3->tmp.ctype;
3929 ctypelen = s->s3->tmp.ctype_num;
3931 for (i = 0; i < ctypelen; i++) {
3932 if (ctypes[i] == check_type) {
3933 rv |= CERT_PKEY_CERT_TYPE;
3937 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
3940 rv |= CERT_PKEY_CERT_TYPE;
3942 ca_dn = s->s3->tmp.ca_names;
3944 if (!sk_X509_NAME_num(ca_dn))
3945 rv |= CERT_PKEY_ISSUER_NAME;
3947 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3948 if (ssl_check_ca_name(ca_dn, x))
3949 rv |= CERT_PKEY_ISSUER_NAME;
3951 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
3952 for (i = 0; i < sk_X509_num(chain); i++) {
3953 X509 *xtmp = sk_X509_value(chain, i);
3954 if (ssl_check_ca_name(ca_dn, xtmp)) {
3955 rv |= CERT_PKEY_ISSUER_NAME;
3960 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
3963 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
3965 if (!check_flags || (rv & check_flags) == check_flags)
3966 rv |= CERT_PKEY_VALID;
3970 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3971 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
3972 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
3973 else if (s->s3->tmp.md[idx] != NULL)
3974 rv |= CERT_PKEY_SIGN;
3976 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
3979 * When checking a CERT_PKEY structure all flags are irrelevant if the
3983 if (rv & CERT_PKEY_VALID)
3986 /* Preserve explicit sign flag, clear rest */
3987 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
3994 /* Set validity of certificates in an SSL structure */
3995 void tls1_set_cert_validity(SSL *s)
3997 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
3998 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
3999 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4000 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4001 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
4002 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
4003 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
4006 /* User level utiity function to check a chain is suitable */
4007 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4009 return tls1_check_chain(s, x, pk, chain, -1);
4013 #ifndef OPENSSL_NO_DH
4014 DH *ssl_get_auto_dh(SSL *s)
4016 int dh_secbits = 80;
4017 if (s->cert->dh_tmp_auto == 2)
4018 return DH_get_1024_160();
4019 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
4020 if (s->s3->tmp.new_cipher->strength_bits == 256)
4025 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4026 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4029 if (dh_secbits >= 128) {
4035 BN_set_word(dhp->g, 2);
4036 if (dh_secbits >= 192)
4037 dhp->p = get_rfc3526_prime_8192(NULL);
4039 dhp->p = get_rfc3526_prime_3072(NULL);
4040 if (dhp->p == NULL || dhp->g == NULL) {
4046 if (dh_secbits >= 112)
4047 return DH_get_2048_224();
4048 return DH_get_1024_160();
4052 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4055 EVP_PKEY *pkey = X509_get0_pubkey(x);
4058 * If no parameters this will return -1 and fail using the default
4059 * security callback for any non-zero security level. This will
4060 * reject keys which omit parameters but this only affects DSA and
4061 * omission of parameters is never (?) done in practice.
4063 secbits = EVP_PKEY_security_bits(pkey);
4066 return ssl_security(s, op, secbits, 0, x);
4068 return ssl_ctx_security(ctx, op, secbits, 0, x);
4071 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4073 /* Lookup signature algorithm digest */
4074 int secbits = -1, md_nid = NID_undef, sig_nid;
4075 /* Don't check signature if self signed */
4076 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
4078 sig_nid = X509_get_signature_nid(x);
4079 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4081 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4082 secbits = EVP_MD_size(md) * 4;
4085 return ssl_security(s, op, secbits, md_nid, x);
4087 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4090 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4093 vfy = SSL_SECOP_PEER;
4095 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4096 return SSL_R_EE_KEY_TOO_SMALL;
4098 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4099 return SSL_R_CA_KEY_TOO_SMALL;
4101 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4102 return SSL_R_CA_MD_TOO_WEAK;
4107 * Check security of a chain, if sk includes the end entity certificate then
4108 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4109 * one to the peer. Return values: 1 if ok otherwise error code to use
4112 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4114 int rv, start_idx, i;
4116 x = sk_X509_value(sk, 0);
4121 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4125 for (i = start_idx; i < sk_X509_num(sk); i++) {
4126 x = sk_X509_value(sk, i);
4127 rv = ssl_security_cert(s, NULL, x, vfy, 0);