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 #include <openssl/dh.h>
119 #include <openssl/bn.h>
120 #include "ssl_locl.h"
121 #include <openssl/ct.h>
123 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
124 const unsigned char *sess_id, int sesslen,
125 SSL_SESSION **psess);
126 static int ssl_check_clienthello_tlsext_early(SSL *s);
127 static int ssl_check_serverhello_tlsext(SSL *s);
129 SSL3_ENC_METHOD const TLSv1_enc_data = {
132 tls1_setup_key_block,
133 tls1_generate_master_secret,
134 tls1_change_cipher_state,
135 tls1_final_finish_mac,
136 TLS1_FINISH_MAC_LENGTH,
137 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
138 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
140 tls1_export_keying_material,
142 SSL3_HM_HEADER_LENGTH,
143 ssl3_set_handshake_header,
147 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
150 tls1_setup_key_block,
151 tls1_generate_master_secret,
152 tls1_change_cipher_state,
153 tls1_final_finish_mac,
154 TLS1_FINISH_MAC_LENGTH,
155 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
156 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
158 tls1_export_keying_material,
159 SSL_ENC_FLAG_EXPLICIT_IV,
160 SSL3_HM_HEADER_LENGTH,
161 ssl3_set_handshake_header,
165 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
168 tls1_setup_key_block,
169 tls1_generate_master_secret,
170 tls1_change_cipher_state,
171 tls1_final_finish_mac,
172 TLS1_FINISH_MAC_LENGTH,
173 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
174 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
176 tls1_export_keying_material,
177 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
178 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
179 SSL3_HM_HEADER_LENGTH,
180 ssl3_set_handshake_header,
184 long tls1_default_timeout(void)
187 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
188 * http, the cache would over fill
190 return (60 * 60 * 2);
197 s->method->ssl_clear(s);
201 void tls1_free(SSL *s)
203 OPENSSL_free(s->tlsext_session_ticket);
207 void tls1_clear(SSL *s)
210 if (s->method->version == TLS_ANY_VERSION)
211 s->version = TLS_MAX_VERSION;
213 s->version = s->method->version;
216 #ifndef OPENSSL_NO_EC
219 int nid; /* Curve NID */
220 int secbits; /* Bits of security (from SP800-57) */
221 unsigned int flags; /* Flags: currently just field type */
224 /* Mask for curve type */
225 # define TLS_CURVE_TYPE 0x3
226 # define TLS_CURVE_PRIME 0x0
227 # define TLS_CURVE_CHAR2 0x1
228 # define TLS_CURVE_CUSTOM 0x2
231 * Table of curve information.
232 * Do not delete entries or reorder this array! It is used as a lookup
233 * table: the index of each entry is one less than the TLS curve id.
235 static const tls_curve_info nid_list[] = {
236 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
237 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
238 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
239 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
240 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
241 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
242 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
243 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
244 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
245 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
246 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
247 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
248 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
249 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
250 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
251 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
252 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
253 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
254 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
255 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
256 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
257 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
258 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
259 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
260 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
261 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
262 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
263 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
265 {NID_X25519, 128, TLS_CURVE_CUSTOM},
268 static const unsigned char ecformats_default[] = {
269 TLSEXT_ECPOINTFORMAT_uncompressed,
270 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
271 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
274 /* The default curves */
275 static const unsigned char eccurves_default[] = {
276 0, 29, /* X25519 (29) */
277 0, 23, /* secp256r1 (23) */
278 0, 25, /* secp521r1 (25) */
279 0, 24, /* secp384r1 (24) */
282 static const unsigned char eccurves_all[] = {
283 0, 29, /* X25519 (29) */
284 0, 23, /* secp256r1 (23) */
285 0, 25, /* secp521r1 (25) */
286 0, 24, /* secp384r1 (24) */
287 0, 26, /* brainpoolP256r1 (26) */
288 0, 27, /* brainpoolP384r1 (27) */
289 0, 28, /* brainpool512r1 (28) */
292 * Remaining curves disabled by default but still permitted if set
293 * via an explicit callback or parameters.
295 0, 22, /* secp256k1 (22) */
296 0, 14, /* sect571r1 (14) */
297 0, 13, /* sect571k1 (13) */
298 0, 11, /* sect409k1 (11) */
299 0, 12, /* sect409r1 (12) */
300 0, 9, /* sect283k1 (9) */
301 0, 10, /* sect283r1 (10) */
302 0, 20, /* secp224k1 (20) */
303 0, 21, /* secp224r1 (21) */
304 0, 18, /* secp192k1 (18) */
305 0, 19, /* secp192r1 (19) */
306 0, 15, /* secp160k1 (15) */
307 0, 16, /* secp160r1 (16) */
308 0, 17, /* secp160r2 (17) */
309 0, 8, /* sect239k1 (8) */
310 0, 6, /* sect233k1 (6) */
311 0, 7, /* sect233r1 (7) */
312 0, 4, /* sect193r1 (4) */
313 0, 5, /* sect193r2 (5) */
314 0, 1, /* sect163k1 (1) */
315 0, 2, /* sect163r1 (2) */
316 0, 3, /* sect163r2 (3) */
320 static const unsigned char suiteb_curves[] = {
321 0, TLSEXT_curve_P_256,
322 0, TLSEXT_curve_P_384
325 int tls1_ec_curve_id2nid(int curve_id)
327 /* ECC curves from RFC 4492 and RFC 7027 */
328 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
330 return nid_list[curve_id - 1].nid;
333 int tls1_ec_nid2curve_id(int nid)
336 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
337 if (nid_list[i].nid == nid)
344 * Get curves list, if "sess" is set return client curves otherwise
346 * Sets |num_curves| to the number of curves in the list, i.e.,
347 * the length of |pcurves| is 2 * num_curves.
348 * Returns 1 on success and 0 if the client curves list has invalid format.
349 * The latter indicates an internal error: we should not be accepting such
350 * lists in the first place.
351 * TODO(emilia): we should really be storing the curves list in explicitly
352 * parsed form instead. (However, this would affect binary compatibility
353 * so cannot happen in the 1.0.x series.)
355 static int tls1_get_curvelist(SSL *s, int sess,
356 const unsigned char **pcurves,
359 size_t pcurveslen = 0;
361 *pcurves = s->session->tlsext_ellipticcurvelist;
362 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
364 /* For Suite B mode only include P-256, P-384 */
365 switch (tls1_suiteb(s)) {
366 case SSL_CERT_FLAG_SUITEB_128_LOS:
367 *pcurves = suiteb_curves;
368 pcurveslen = sizeof(suiteb_curves);
371 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
372 *pcurves = suiteb_curves;
376 case SSL_CERT_FLAG_SUITEB_192_LOS:
377 *pcurves = suiteb_curves + 2;
381 *pcurves = s->tlsext_ellipticcurvelist;
382 pcurveslen = s->tlsext_ellipticcurvelist_length;
385 *pcurves = eccurves_default;
386 pcurveslen = sizeof(eccurves_default);
390 /* We do not allow odd length arrays to enter the system. */
391 if (pcurveslen & 1) {
392 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
396 *num_curves = pcurveslen / 2;
401 /* See if curve is allowed by security callback */
402 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
404 const tls_curve_info *cinfo;
407 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
409 cinfo = &nid_list[curve[1] - 1];
410 # ifdef OPENSSL_NO_EC2M
411 if (cinfo->flags & TLS_CURVE_CHAR2)
414 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
417 /* Check a curve is one of our preferences */
418 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
420 const unsigned char *curves;
421 size_t num_curves, i;
422 unsigned int suiteb_flags = tls1_suiteb(s);
423 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
425 /* Check curve matches Suite B preferences */
427 unsigned long cid = s->s3->tmp.new_cipher->id;
430 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
431 if (p[2] != TLSEXT_curve_P_256)
433 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
434 if (p[2] != TLSEXT_curve_P_384)
436 } else /* Should never happen */
439 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
441 for (i = 0; i < num_curves; i++, curves += 2) {
442 if (p[1] == curves[0] && p[2] == curves[1])
443 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
449 * For nmatch >= 0, return the NID of the |nmatch|th shared curve or NID_undef
450 * if there is no match.
451 * For nmatch == -1, return number of matches
452 * For nmatch == -2, return the NID of the curve to use for
453 * an EC tmp key, or NID_undef if there is no match.
455 int tls1_shared_curve(SSL *s, int nmatch)
457 const unsigned char *pref, *supp;
458 size_t num_pref, num_supp, i, j;
460 /* Can't do anything on client side */
464 if (tls1_suiteb(s)) {
466 * For Suite B ciphersuite determines curve: we already know
467 * these are acceptable due to previous checks.
469 unsigned long cid = s->s3->tmp.new_cipher->id;
470 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
471 return NID_X9_62_prime256v1; /* P-256 */
472 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
473 return NID_secp384r1; /* P-384 */
474 /* Should never happen */
477 /* If not Suite B just return first preference shared curve */
481 * Avoid truncation. tls1_get_curvelist takes an int
482 * but s->options is a long...
484 if (!tls1_get_curvelist
485 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
487 /* In practice, NID_undef == 0 but let's be precise. */
488 return nmatch == -1 ? 0 : NID_undef;
489 if (!tls1_get_curvelist
490 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
492 return nmatch == -1 ? 0 : NID_undef;
495 * If the client didn't send the elliptic_curves extension all of them
498 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
500 num_supp = sizeof(eccurves_all) / 2;
501 } else if (num_pref == 0 &&
502 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
504 num_pref = sizeof(eccurves_all) / 2;
508 for (i = 0; i < num_pref; i++, pref += 2) {
509 const unsigned char *tsupp = supp;
510 for (j = 0; j < num_supp; j++, tsupp += 2) {
511 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
512 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
515 int id = (pref[0] << 8) | pref[1];
516 return tls1_ec_curve_id2nid(id);
524 /* Out of range (nmatch > k). */
528 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
529 int *curves, size_t ncurves)
531 unsigned char *clist, *p;
534 * Bitmap of curves included to detect duplicates: only works while curve
537 unsigned long dup_list = 0;
538 clist = OPENSSL_malloc(ncurves * 2);
541 for (i = 0, p = clist; i < ncurves; i++) {
542 unsigned long idmask;
544 id = tls1_ec_nid2curve_id(curves[i]);
546 if (!id || (dup_list & idmask)) {
555 *pextlen = ncurves * 2;
559 # define MAX_CURVELIST 28
563 int nid_arr[MAX_CURVELIST];
566 static int nid_cb(const char *elem, int len, void *arg)
568 nid_cb_st *narg = arg;
574 if (narg->nidcnt == MAX_CURVELIST)
576 if (len > (int)(sizeof(etmp) - 1))
578 memcpy(etmp, elem, len);
580 nid = EC_curve_nist2nid(etmp);
581 if (nid == NID_undef)
582 nid = OBJ_sn2nid(etmp);
583 if (nid == NID_undef)
584 nid = OBJ_ln2nid(etmp);
585 if (nid == NID_undef)
587 for (i = 0; i < narg->nidcnt; i++)
588 if (narg->nid_arr[i] == nid)
590 narg->nid_arr[narg->nidcnt++] = nid;
594 /* Set curves based on a colon separate list */
595 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
600 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
604 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
607 /* For an EC key set TLS id and required compression based on parameters */
608 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
615 /* Determine if it is a prime field */
616 grp = EC_KEY_get0_group(ec);
619 /* Determine curve ID */
620 id = EC_GROUP_get_curve_name(grp);
621 id = tls1_ec_nid2curve_id(id);
622 /* If no id return error: we don't support arbitrary explicit curves */
626 curve_id[1] = (unsigned char)id;
628 if (EC_KEY_get0_public_key(ec) == NULL)
630 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
631 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
633 if ((nid_list[id - 1].flags & TLS_CURVE_TYPE) == TLS_CURVE_PRIME)
634 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
636 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
642 /* Check an EC key is compatible with extensions */
643 static int tls1_check_ec_key(SSL *s,
644 unsigned char *curve_id, unsigned char *comp_id)
646 const unsigned char *pformats, *pcurves;
647 size_t num_formats, num_curves, i;
650 * If point formats extension present check it, otherwise everything is
651 * supported (see RFC4492).
653 if (comp_id && s->session->tlsext_ecpointformatlist) {
654 pformats = s->session->tlsext_ecpointformatlist;
655 num_formats = s->session->tlsext_ecpointformatlist_length;
656 for (i = 0; i < num_formats; i++, pformats++) {
657 if (*comp_id == *pformats)
660 if (i == num_formats)
665 /* Check curve is consistent with client and server preferences */
666 for (j = 0; j <= 1; j++) {
667 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
669 if (j == 1 && num_curves == 0) {
671 * If we've not received any curves then skip this check.
672 * RFC 4492 does not require the supported elliptic curves extension
673 * so if it is not sent we can just choose any curve.
674 * It is invalid to send an empty list in the elliptic curves
675 * extension, so num_curves == 0 always means no extension.
679 for (i = 0; i < num_curves; i++, pcurves += 2) {
680 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
685 /* For clients can only check sent curve list */
692 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
696 * If we have a custom point format list use it otherwise use default
698 if (s->tlsext_ecpointformatlist) {
699 *pformats = s->tlsext_ecpointformatlist;
700 *num_formats = s->tlsext_ecpointformatlist_length;
702 *pformats = ecformats_default;
703 /* For Suite B we don't support char2 fields */
705 *num_formats = sizeof(ecformats_default) - 1;
707 *num_formats = sizeof(ecformats_default);
712 * Check cert parameters compatible with extensions: currently just checks EC
713 * certificates have compatible curves and compression.
715 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
717 unsigned char comp_id, curve_id[2];
720 pkey = X509_get0_pubkey(x);
723 /* If not EC nothing to do */
724 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
726 rv = tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey));
730 * Can't check curve_id for client certs as we don't have a supported
733 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
737 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
738 * SHA384+P-384, adjust digest if necessary.
740 if (set_ee_md && tls1_suiteb(s)) {
746 /* Check to see we have necessary signing algorithm */
747 if (curve_id[1] == TLSEXT_curve_P_256)
748 check_md = NID_ecdsa_with_SHA256;
749 else if (curve_id[1] == TLSEXT_curve_P_384)
750 check_md = NID_ecdsa_with_SHA384;
752 return 0; /* Should never happen */
753 for (i = 0; i < c->shared_sigalgslen; i++)
754 if (check_md == c->shared_sigalgs[i].signandhash_nid)
756 if (i == c->shared_sigalgslen)
758 if (set_ee_md == 2) {
759 if (check_md == NID_ecdsa_with_SHA256)
760 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
762 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
768 # ifndef OPENSSL_NO_EC
770 * tls1_check_ec_tmp_key - Check EC temporary key compatiblity
772 * @cid: Cipher ID we're considering using
774 * Checks that the kECDHE cipher suite we're considering using
775 * is compatible with the client extensions.
777 * Returns 0 when the cipher can't be used or 1 when it can.
779 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
782 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
785 if (tls1_suiteb(s)) {
786 unsigned char curve_id[2];
787 /* Curve to check determined by ciphersuite */
788 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
789 curve_id[1] = TLSEXT_curve_P_256;
790 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
791 curve_id[1] = TLSEXT_curve_P_384;
795 /* Check this curve is acceptable */
796 if (!tls1_check_ec_key(s, curve_id, NULL))
800 /* Need a shared curve */
801 if (tls1_shared_curve(s, 0))
805 # endif /* OPENSSL_NO_EC */
809 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
814 #endif /* OPENSSL_NO_EC */
817 * List of supported signature algorithms and hashes. Should make this
818 * customisable at some point, for now include everything we support.
821 #ifdef OPENSSL_NO_RSA
822 # define tlsext_sigalg_rsa(md) /* */
824 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
827 #ifdef OPENSSL_NO_DSA
828 # define tlsext_sigalg_dsa(md) /* */
830 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
834 # define tlsext_sigalg_ecdsa(md) /* */
836 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
839 #define tlsext_sigalg(md) \
840 tlsext_sigalg_rsa(md) \
841 tlsext_sigalg_dsa(md) \
842 tlsext_sigalg_ecdsa(md)
844 static const unsigned char tls12_sigalgs[] = {
845 tlsext_sigalg(TLSEXT_hash_sha512)
846 tlsext_sigalg(TLSEXT_hash_sha384)
847 tlsext_sigalg(TLSEXT_hash_sha256)
848 tlsext_sigalg(TLSEXT_hash_sha224)
849 tlsext_sigalg(TLSEXT_hash_sha1)
850 #ifndef OPENSSL_NO_GOST
851 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
852 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
853 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
857 #ifndef OPENSSL_NO_EC
858 static const unsigned char suiteb_sigalgs[] = {
859 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
860 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
863 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
866 * If Suite B mode use Suite B sigalgs only, ignore any other
869 #ifndef OPENSSL_NO_EC
870 switch (tls1_suiteb(s)) {
871 case SSL_CERT_FLAG_SUITEB_128_LOS:
872 *psigs = suiteb_sigalgs;
873 return sizeof(suiteb_sigalgs);
875 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
876 *psigs = suiteb_sigalgs;
879 case SSL_CERT_FLAG_SUITEB_192_LOS:
880 *psigs = suiteb_sigalgs + 2;
884 /* If server use client authentication sigalgs if not NULL */
885 if (s->server && s->cert->client_sigalgs) {
886 *psigs = s->cert->client_sigalgs;
887 return s->cert->client_sigalgslen;
888 } else if (s->cert->conf_sigalgs) {
889 *psigs = s->cert->conf_sigalgs;
890 return s->cert->conf_sigalgslen;
892 *psigs = tls12_sigalgs;
893 return sizeof(tls12_sigalgs);
898 * Check signature algorithm is consistent with sent supported signature
899 * algorithms and if so return relevant digest.
901 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
902 const unsigned char *sig, EVP_PKEY *pkey)
904 const unsigned char *sent_sigs;
905 size_t sent_sigslen, i;
906 int sigalg = tls12_get_sigid(pkey);
907 /* Should never happen */
910 /* Check key type is consistent with signature */
911 if (sigalg != (int)sig[1]) {
912 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
915 #ifndef OPENSSL_NO_EC
916 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
917 unsigned char curve_id[2], comp_id;
918 /* Check compression and curve matches extensions */
919 if (!tls1_set_ec_id(curve_id, &comp_id, EVP_PKEY_get0_EC_KEY(pkey)))
921 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
922 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
925 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
926 if (tls1_suiteb(s)) {
929 if (curve_id[1] == TLSEXT_curve_P_256) {
930 if (sig[0] != TLSEXT_hash_sha256) {
931 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
932 SSL_R_ILLEGAL_SUITEB_DIGEST);
935 } else if (curve_id[1] == TLSEXT_curve_P_384) {
936 if (sig[0] != TLSEXT_hash_sha384) {
937 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
938 SSL_R_ILLEGAL_SUITEB_DIGEST);
944 } else if (tls1_suiteb(s))
948 /* Check signature matches a type we sent */
949 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
950 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
951 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
954 /* Allow fallback to SHA1 if not strict mode */
955 if (i == sent_sigslen
956 && (sig[0] != TLSEXT_hash_sha1
957 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
958 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
961 *pmd = tls12_get_hash(sig[0]);
963 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
966 /* Make sure security callback allows algorithm */
967 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
968 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
970 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
974 * Store the digest used so applications can retrieve it if they wish.
976 s->s3->tmp.peer_md = *pmd;
981 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
982 * supported, doesn't appear in supported signature algorithms, isn't supported
983 * by the enabled protocol versions or by the security level.
985 * This function should only be used for checking which ciphers are supported
988 * Call ssl_cipher_disabled() to check that it's enabled or not.
990 void ssl_set_client_disabled(SSL *s)
992 s->s3->tmp.mask_a = 0;
993 s->s3->tmp.mask_k = 0;
994 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
995 ssl_get_client_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
996 # ifndef OPENSSL_NO_PSK
997 /* with PSK there must be client callback set */
998 if (!s->psk_client_callback) {
999 s->s3->tmp.mask_a |= SSL_aPSK;
1000 s->s3->tmp.mask_k |= SSL_PSK;
1002 #endif /* OPENSSL_NO_PSK */
1003 #ifndef OPENSSL_NO_SRP
1004 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1005 s->s3->tmp.mask_a |= SSL_aSRP;
1006 s->s3->tmp.mask_k |= SSL_kSRP;
1012 * ssl_cipher_disabled - check that a cipher is disabled or not
1013 * @s: SSL connection that you want to use the cipher on
1014 * @c: cipher to check
1015 * @op: Security check that you want to do
1017 * Returns 1 when it's disabled, 0 when enabled.
1019 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1021 if (c->algorithm_mkey & s->s3->tmp.mask_k
1022 || c->algorithm_auth & s->s3->tmp.mask_a)
1024 if (s->s3->tmp.max_ver == 0)
1026 if (!SSL_IS_DTLS(s) && ((c->min_tls > s->s3->tmp.max_ver)
1027 || (c->max_tls < s->s3->tmp.min_ver)))
1029 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
1030 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
1033 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1036 static int tls_use_ticket(SSL *s)
1038 if (s->options & SSL_OP_NO_TICKET)
1040 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1043 static int compare_uint(const void *p1, const void *p2) {
1044 unsigned int u1 = *((const unsigned int *)p1);
1045 unsigned int u2 = *((const unsigned int *)p2);
1055 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
1056 * more than one extension of the same type in a ClientHello or ServerHello.
1057 * This function does an initial scan over the extensions block to filter those
1058 * out. It returns 1 if all extensions are unique, and 0 if the extensions
1059 * contain duplicates, could not be successfully parsed, or an internal error
1062 static int tls1_check_duplicate_extensions(const PACKET *packet) {
1063 PACKET extensions = *packet;
1064 size_t num_extensions = 0, i = 0;
1065 unsigned int *extension_types = NULL;
1068 /* First pass: count the extensions. */
1069 while (PACKET_remaining(&extensions) > 0) {
1072 if (!PACKET_get_net_2(&extensions, &type) ||
1073 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1079 if (num_extensions <= 1)
1082 extension_types = OPENSSL_malloc(sizeof(unsigned int) * num_extensions);
1083 if (extension_types == NULL) {
1084 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_MALLOC_FAILURE);
1088 /* Second pass: gather the extension types. */
1089 extensions = *packet;
1090 for (i = 0; i < num_extensions; i++) {
1092 if (!PACKET_get_net_2(&extensions, &extension_types[i]) ||
1093 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
1094 /* This should not happen. */
1095 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1100 if (PACKET_remaining(&extensions) != 0) {
1101 SSLerr(SSL_F_TLS1_CHECK_DUPLICATE_EXTENSIONS, ERR_R_INTERNAL_ERROR);
1104 /* Sort the extensions and make sure there are no duplicates. */
1105 qsort(extension_types, num_extensions, sizeof(unsigned int), compare_uint);
1106 for (i = 1; i < num_extensions; i++) {
1107 if (extension_types[i - 1] == extension_types[i])
1112 OPENSSL_free(extension_types);
1116 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1117 unsigned char *limit, int *al)
1120 unsigned char *orig = buf;
1121 unsigned char *ret = buf;
1122 #ifndef OPENSSL_NO_EC
1123 /* See if we support any ECC ciphersuites */
1125 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1127 unsigned long alg_k, alg_a;
1128 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1130 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1131 const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1133 alg_k = c->algorithm_mkey;
1134 alg_a = c->algorithm_auth;
1135 if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
1136 || (alg_a & SSL_aECDSA)) {
1147 return NULL; /* this really never occurs, but ... */
1149 /* Add RI if renegotiating */
1150 if (s->renegotiate) {
1153 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1154 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1158 if ((limit - ret - 4 - el) < 0)
1161 s2n(TLSEXT_TYPE_renegotiate, ret);
1164 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1165 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1171 /* Only add RI for SSLv3 */
1172 if (s->client_version == SSL3_VERSION)
1175 if (s->tlsext_hostname != NULL) {
1176 /* Add TLS extension servername to the Client Hello message */
1177 unsigned long size_str;
1181 * check for enough space.
1182 * 4 for the servername type and entension length
1183 * 2 for servernamelist length
1184 * 1 for the hostname type
1185 * 2 for hostname length
1189 if ((lenmax = limit - ret - 9) < 0
1191 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1194 /* extension type and length */
1195 s2n(TLSEXT_TYPE_server_name, ret);
1196 s2n(size_str + 5, ret);
1198 /* length of servername list */
1199 s2n(size_str + 3, ret);
1201 /* hostname type, length and hostname */
1202 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1204 memcpy(ret, s->tlsext_hostname, size_str);
1207 #ifndef OPENSSL_NO_SRP
1208 /* Add SRP username if there is one */
1209 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1210 * Client Hello message */
1212 int login_len = strlen(s->srp_ctx.login);
1213 if (login_len > 255 || login_len == 0) {
1214 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1219 * check for enough space.
1220 * 4 for the srp type type and entension length
1221 * 1 for the srp user identity
1222 * + srp user identity length
1224 if ((limit - ret - 5 - login_len) < 0)
1227 /* fill in the extension */
1228 s2n(TLSEXT_TYPE_srp, ret);
1229 s2n(login_len + 1, ret);
1230 (*ret++) = (unsigned char)login_len;
1231 memcpy(ret, s->srp_ctx.login, login_len);
1236 #ifndef OPENSSL_NO_EC
1239 * Add TLS extension ECPointFormats to the ClientHello message
1242 const unsigned char *pcurves, *pformats;
1243 size_t num_curves, num_formats, curves_list_len;
1245 unsigned char *etmp;
1247 tls1_get_formatlist(s, &pformats, &num_formats);
1249 if ((lenmax = limit - ret - 5) < 0)
1251 if (num_formats > (size_t)lenmax)
1253 if (num_formats > 255) {
1254 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1258 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1259 /* The point format list has 1-byte length. */
1260 s2n(num_formats + 1, ret);
1261 *(ret++) = (unsigned char)num_formats;
1262 memcpy(ret, pformats, num_formats);
1266 * Add TLS extension EllipticCurves to the ClientHello message
1268 pcurves = s->tlsext_ellipticcurvelist;
1269 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1272 if ((lenmax = limit - ret - 6) < 0)
1274 if (num_curves > (size_t)lenmax / 2)
1276 if (num_curves > 65532 / 2) {
1277 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1281 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1283 /* Copy curve ID if supported */
1284 for (i = 0; i < num_curves; i++, pcurves += 2) {
1285 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1286 *etmp++ = pcurves[0];
1287 *etmp++ = pcurves[1];
1291 curves_list_len = etmp - ret - 4;
1293 s2n(curves_list_len + 2, ret);
1294 s2n(curves_list_len, ret);
1295 ret += curves_list_len;
1297 #endif /* OPENSSL_NO_EC */
1299 if (tls_use_ticket(s)) {
1301 if (!s->new_session && s->session && s->session->tlsext_tick)
1302 ticklen = s->session->tlsext_ticklen;
1303 else if (s->session && s->tlsext_session_ticket &&
1304 s->tlsext_session_ticket->data) {
1305 ticklen = s->tlsext_session_ticket->length;
1306 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1307 if (s->session->tlsext_tick == NULL)
1309 memcpy(s->session->tlsext_tick,
1310 s->tlsext_session_ticket->data, ticklen);
1311 s->session->tlsext_ticklen = ticklen;
1314 if (ticklen == 0 && s->tlsext_session_ticket &&
1315 s->tlsext_session_ticket->data == NULL)
1318 * Check for enough room 2 for extension type, 2 for len rest for
1321 if ((long)(limit - ret - 4 - ticklen) < 0)
1323 s2n(TLSEXT_TYPE_session_ticket, ret);
1326 memcpy(ret, s->session->tlsext_tick, ticklen);
1332 if (SSL_USE_SIGALGS(s)) {
1334 const unsigned char *salg;
1335 unsigned char *etmp;
1336 salglen = tls12_get_psigalgs(s, &salg);
1337 if ((size_t)(limit - ret) < salglen + 6)
1339 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1341 /* Skip over lengths for now */
1343 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1344 /* Fill in lengths */
1345 s2n(salglen + 2, etmp);
1350 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1352 long extlen, idlen, itmp;
1356 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1357 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1358 itmp = i2d_OCSP_RESPID(id, NULL);
1364 if (s->tlsext_ocsp_exts) {
1365 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1371 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1373 s2n(TLSEXT_TYPE_status_request, ret);
1374 if (extlen + idlen > 0xFFF0)
1376 s2n(extlen + idlen + 5, ret);
1377 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1379 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1380 /* save position of id len */
1381 unsigned char *q = ret;
1382 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1383 /* skip over id len */
1385 itmp = i2d_OCSP_RESPID(id, &ret);
1391 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1393 #ifndef OPENSSL_NO_HEARTBEATS
1394 if (SSL_IS_DTLS(s)) {
1395 /* Add Heartbeat extension */
1396 if ((limit - ret - 4 - 1) < 0)
1398 s2n(TLSEXT_TYPE_heartbeat, ret);
1402 * 1: peer may send requests
1403 * 2: peer not allowed to send requests
1405 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1406 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1408 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1412 #ifndef OPENSSL_NO_NEXTPROTONEG
1413 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1415 * The client advertises an emtpy extension to indicate its support
1416 * for Next Protocol Negotiation
1418 if (limit - ret - 4 < 0)
1420 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1426 * finish_md_len is non-zero during a renegotiation, so
1427 * this avoids sending ALPN during the renegotiation
1428 * (see longer comment below)
1430 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1431 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1433 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1434 s2n(2 + s->alpn_client_proto_list_len, ret);
1435 s2n(s->alpn_client_proto_list_len, ret);
1436 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1437 ret += s->alpn_client_proto_list_len;
1438 s->s3->alpn_sent = 1;
1440 #ifndef OPENSSL_NO_SRTP
1441 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1444 /* Returns 0 on success!! */
1445 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1446 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1450 if ((limit - ret - 4 - el) < 0)
1453 s2n(TLSEXT_TYPE_use_srtp, ret);
1456 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1457 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1463 custom_ext_init(&s->cert->cli_ext);
1464 /* Add custom TLS Extensions to ClientHello */
1465 if (!custom_ext_add(s, 0, &ret, limit, al))
1467 #ifdef TLSEXT_TYPE_encrypt_then_mac
1468 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1471 #ifndef OPENSSL_NO_CT
1472 if (s->ct_validation_callback != NULL) {
1473 s2n(TLSEXT_TYPE_signed_certificate_timestamp, ret);
1477 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1481 * Add padding to workaround bugs in F5 terminators. See
1482 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1483 * code works out the length of all existing extensions it MUST always
1486 if (s->options & SSL_OP_TLSEXT_PADDING) {
1487 int hlen = ret - (unsigned char *)s->init_buf->data;
1489 if (hlen > 0xff && hlen < 0x200) {
1490 hlen = 0x200 - hlen;
1496 s2n(TLSEXT_TYPE_padding, ret);
1498 memset(ret, 0, hlen);
1505 if ((extdatalen = ret - orig - 2) == 0)
1508 s2n(extdatalen, orig);
1512 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1513 unsigned char *limit, int *al)
1516 unsigned char *orig = buf;
1517 unsigned char *ret = buf;
1518 #ifndef OPENSSL_NO_NEXTPROTONEG
1519 int next_proto_neg_seen;
1521 #ifndef OPENSSL_NO_EC
1522 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1523 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1524 int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
1525 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1530 return NULL; /* this really never occurs, but ... */
1532 if (s->s3->send_connection_binding) {
1535 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1536 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1540 if ((limit - ret - 4 - el) < 0)
1543 s2n(TLSEXT_TYPE_renegotiate, ret);
1546 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1547 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1554 /* Only add RI for SSLv3 */
1555 if (s->version == SSL3_VERSION)
1558 if (!s->hit && s->servername_done == 1
1559 && s->session->tlsext_hostname != NULL) {
1560 if ((long)(limit - ret - 4) < 0)
1563 s2n(TLSEXT_TYPE_server_name, ret);
1566 #ifndef OPENSSL_NO_EC
1568 const unsigned char *plist;
1571 * Add TLS extension ECPointFormats to the ServerHello message
1575 tls1_get_formatlist(s, &plist, &plistlen);
1577 if ((lenmax = limit - ret - 5) < 0)
1579 if (plistlen > (size_t)lenmax)
1581 if (plistlen > 255) {
1582 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1586 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1587 s2n(plistlen + 1, ret);
1588 *(ret++) = (unsigned char)plistlen;
1589 memcpy(ret, plist, plistlen);
1594 * Currently the server should not respond with a SupportedCurves
1597 #endif /* OPENSSL_NO_EC */
1599 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1600 if ((long)(limit - ret - 4) < 0)
1602 s2n(TLSEXT_TYPE_session_ticket, ret);
1606 if (s->tlsext_status_expected) {
1607 if ((long)(limit - ret - 4) < 0)
1609 s2n(TLSEXT_TYPE_status_request, ret);
1613 #ifndef OPENSSL_NO_SRTP
1614 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1617 /* Returns 0 on success!! */
1618 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1619 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1622 if ((limit - ret - 4 - el) < 0)
1625 s2n(TLSEXT_TYPE_use_srtp, ret);
1628 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1629 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1636 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1637 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1638 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1639 const unsigned char cryptopro_ext[36] = {
1640 0xfd, 0xe8, /* 65000 */
1641 0x00, 0x20, /* 32 bytes length */
1642 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1643 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1644 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1645 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1647 if (limit - ret < 36)
1649 memcpy(ret, cryptopro_ext, 36);
1653 #ifndef OPENSSL_NO_HEARTBEATS
1654 /* Add Heartbeat extension if we've received one */
1655 if (SSL_IS_DTLS(s) && (s->tlsext_heartbeat & SSL_DTLSEXT_HB_ENABLED)) {
1656 if ((limit - ret - 4 - 1) < 0)
1658 s2n(TLSEXT_TYPE_heartbeat, ret);
1662 * 1: peer may send requests
1663 * 2: peer not allowed to send requests
1665 if (s->tlsext_heartbeat & SSL_DTLSEXT_HB_DONT_RECV_REQUESTS)
1666 *(ret++) = SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
1668 *(ret++) = SSL_DTLSEXT_HB_ENABLED;
1673 #ifndef OPENSSL_NO_NEXTPROTONEG
1674 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1675 s->s3->next_proto_neg_seen = 0;
1676 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1677 const unsigned char *npa;
1678 unsigned int npalen;
1681 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1683 ctx->next_protos_advertised_cb_arg);
1684 if (r == SSL_TLSEXT_ERR_OK) {
1685 if ((long)(limit - ret - 4 - npalen) < 0)
1687 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1689 memcpy(ret, npa, npalen);
1691 s->s3->next_proto_neg_seen = 1;
1695 if (!custom_ext_add(s, 1, &ret, limit, al))
1697 #ifdef TLSEXT_TYPE_encrypt_then_mac
1698 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1700 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1701 * for other cases too.
1703 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1704 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1705 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1706 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1707 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1709 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1714 if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1715 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1719 if (s->s3->alpn_selected != NULL) {
1720 const unsigned char *selected = s->s3->alpn_selected;
1721 unsigned int len = s->s3->alpn_selected_len;
1723 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1725 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1729 memcpy(ret, selected, len);
1735 if ((extdatalen = ret - orig - 2) == 0)
1738 s2n(extdatalen, orig);
1743 * Save the ALPN extension in a ClientHello.
1744 * pkt: the contents of the ALPN extension, not including type and length.
1745 * al: a pointer to the alert value to send in the event of a failure.
1746 * returns: 1 on success, 0 on error.
1748 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1750 PACKET protocol_list, save_protocol_list, protocol;
1752 *al = SSL_AD_DECODE_ERROR;
1754 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
1755 || PACKET_remaining(&protocol_list) < 2) {
1759 save_protocol_list = protocol_list;
1761 /* Protocol names can't be empty. */
1762 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
1763 || PACKET_remaining(&protocol) == 0) {
1766 } while (PACKET_remaining(&protocol_list) != 0);
1768 if (!PACKET_memdup(&save_protocol_list,
1769 &s->s3->alpn_proposed,
1770 &s->s3->alpn_proposed_len)) {
1771 *al = TLS1_AD_INTERNAL_ERROR;
1779 * Process the ALPN extension in a ClientHello.
1780 * ret: a pointer to the TLSEXT return value: SSL_TLSEXT_ERR_*
1781 * al: a pointer to the alert value to send in the event of a failure.
1782 * returns 1 on success, 0
1784 static int tls1_alpn_handle_client_hello_late(SSL *s, int *ret, int *al)
1786 const unsigned char *selected = NULL;
1787 unsigned char selected_len = 0;
1789 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
1790 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
1791 s->s3->alpn_proposed,
1792 s->s3->alpn_proposed_len,
1793 s->ctx->alpn_select_cb_arg);
1795 if (r == SSL_TLSEXT_ERR_OK) {
1796 OPENSSL_free(s->s3->alpn_selected);
1797 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
1798 if (s->s3->alpn_selected == NULL) {
1799 *al = SSL_AD_INTERNAL_ERROR;
1800 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1803 s->s3->alpn_selected_len = selected_len;
1804 #ifndef OPENSSL_NO_NEXTPROTONEG
1805 /* ALPN takes precedence over NPN. */
1806 s->s3->next_proto_neg_seen = 0;
1809 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
1810 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1818 #ifndef OPENSSL_NO_EC
1820 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1821 * SecureTransport using the TLS extension block in |pkt|.
1822 * Safari, since 10.6, sends exactly these extensions, in this order:
1827 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1828 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1829 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1830 * 10.8..10.8.3 (which don't work).
1832 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1838 static const unsigned char kSafariExtensionsBlock[] = {
1839 0x00, 0x0a, /* elliptic_curves extension */
1840 0x00, 0x08, /* 8 bytes */
1841 0x00, 0x06, /* 6 bytes of curve ids */
1842 0x00, 0x17, /* P-256 */
1843 0x00, 0x18, /* P-384 */
1844 0x00, 0x19, /* P-521 */
1846 0x00, 0x0b, /* ec_point_formats */
1847 0x00, 0x02, /* 2 bytes */
1848 0x01, /* 1 point format */
1849 0x00, /* uncompressed */
1850 /* The following is only present in TLS 1.2 */
1851 0x00, 0x0d, /* signature_algorithms */
1852 0x00, 0x0c, /* 12 bytes */
1853 0x00, 0x0a, /* 10 bytes */
1854 0x05, 0x01, /* SHA-384/RSA */
1855 0x04, 0x01, /* SHA-256/RSA */
1856 0x02, 0x01, /* SHA-1/RSA */
1857 0x04, 0x03, /* SHA-256/ECDSA */
1858 0x02, 0x03, /* SHA-1/ECDSA */
1861 /* Length of the common prefix (first two extensions). */
1862 static const size_t kSafariCommonExtensionsLength = 18;
1866 if (!PACKET_forward(&tmppkt, 2)
1867 || !PACKET_get_net_2(&tmppkt, &type)
1868 || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1872 if (type != TLSEXT_TYPE_server_name)
1875 ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1876 sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1878 s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1881 #endif /* !OPENSSL_NO_EC */
1884 * Parse ClientHello extensions and stash extension info in various parts of
1885 * the SSL object. Verify that there are no duplicate extensions.
1887 * Behaviour upon resumption is extension-specific. If the extension has no
1888 * effect during resumption, it is parsed (to verify its format) but otherwise
1891 * Consumes the entire packet in |pkt|. Returns 1 on success and 0 on failure.
1892 * Upon failure, sets |al| to the appropriate alert.
1894 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1897 int renegotiate_seen = 0;
1900 *al = SSL_AD_DECODE_ERROR;
1901 s->servername_done = 0;
1902 s->tlsext_status_type = -1;
1903 #ifndef OPENSSL_NO_NEXTPROTONEG
1904 s->s3->next_proto_neg_seen = 0;
1907 OPENSSL_free(s->s3->alpn_selected);
1908 s->s3->alpn_selected = NULL;
1909 s->s3->alpn_selected_len = 0;
1910 OPENSSL_free(s->s3->alpn_proposed);
1911 s->s3->alpn_proposed = NULL;
1912 s->s3->alpn_proposed_len = 0;
1913 #ifndef OPENSSL_NO_HEARTBEATS
1914 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
1915 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
1918 #ifndef OPENSSL_NO_EC
1919 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1920 ssl_check_for_safari(s, pkt);
1921 # endif /* !OPENSSL_NO_EC */
1923 /* Clear any signature algorithms extension received */
1924 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1925 s->s3->tmp.peer_sigalgs = NULL;
1926 #ifdef TLSEXT_TYPE_encrypt_then_mac
1927 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1930 #ifndef OPENSSL_NO_SRP
1931 OPENSSL_free(s->srp_ctx.login);
1932 s->srp_ctx.login = NULL;
1935 s->srtp_profile = NULL;
1937 if (PACKET_remaining(pkt) == 0)
1940 if (!PACKET_as_length_prefixed_2(pkt, &extensions))
1943 if (!tls1_check_duplicate_extensions(&extensions))
1947 * We parse all extensions to ensure the ClientHello is well-formed but,
1948 * unless an extension specifies otherwise, we ignore extensions upon
1951 while (PACKET_get_net_2(&extensions, &type)) {
1953 if (!PACKET_get_length_prefixed_2(&extensions, &extension))
1956 if (s->tlsext_debug_cb)
1957 s->tlsext_debug_cb(s, 0, type, PACKET_data(&extension),
1958 PACKET_remaining(&extension),
1959 s->tlsext_debug_arg);
1961 if (type == TLSEXT_TYPE_renegotiate) {
1962 if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, al))
1964 renegotiate_seen = 1;
1965 } else if (s->version == SSL3_VERSION) {
1968 * The servername extension is treated as follows:
1970 * - Only the hostname type is supported with a maximum length of 255.
1971 * - The servername is rejected if too long or if it contains zeros,
1972 * in which case an fatal alert is generated.
1973 * - The servername field is maintained together with the session cache.
1974 * - When a session is resumed, the servername call back invoked in order
1975 * to allow the application to position itself to the right context.
1976 * - The servername is acknowledged if it is new for a session or when
1977 * it is identical to a previously used for the same session.
1978 * Applications can control the behaviour. They can at any time
1979 * set a 'desirable' servername for a new SSL object. This can be the
1980 * case for example with HTTPS when a Host: header field is received and
1981 * a renegotiation is requested. In this case, a possible servername
1982 * presented in the new client hello is only acknowledged if it matches
1983 * the value of the Host: field.
1984 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1985 * if they provide for changing an explicit servername context for the
1986 * session, i.e. when the session has been established with a servername
1988 * - On session reconnect, the servername extension may be absent.
1992 else if (type == TLSEXT_TYPE_server_name) {
1993 unsigned int servname_type;
1994 PACKET sni, hostname;
1996 if (!PACKET_as_length_prefixed_2(&extension, &sni)
1997 /* ServerNameList must be at least 1 byte long. */
1998 || PACKET_remaining(&sni) == 0) {
2003 * Although the server_name extension was intended to be
2004 * extensible to new name types, RFC 4366 defined the
2005 * syntax inextensibly and OpenSSL 1.0.x parses it as
2007 * RFC 6066 corrected the mistake but adding new name types
2008 * is nevertheless no longer feasible, so act as if no other
2009 * SNI types can exist, to simplify parsing.
2011 * Also note that the RFC permits only one SNI value per type,
2012 * i.e., we can only have a single hostname.
2014 if (!PACKET_get_1(&sni, &servname_type)
2015 || servname_type != TLSEXT_NAMETYPE_host_name
2016 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
2021 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
2022 *al = TLS1_AD_UNRECOGNIZED_NAME;
2026 if (PACKET_contains_zero_byte(&hostname)) {
2027 *al = TLS1_AD_UNRECOGNIZED_NAME;
2031 if (!PACKET_strndup(&hostname, &s->session->tlsext_hostname)) {
2032 *al = TLS1_AD_INTERNAL_ERROR;
2036 s->servername_done = 1;
2039 * TODO(openssl-team): if the SNI doesn't match, we MUST
2040 * fall back to a full handshake.
2042 s->servername_done = s->session->tlsext_hostname
2043 && PACKET_equal(&hostname, s->session->tlsext_hostname,
2044 strlen(s->session->tlsext_hostname));
2047 #ifndef OPENSSL_NO_SRP
2048 else if (type == TLSEXT_TYPE_srp) {
2051 if (!PACKET_as_length_prefixed_1(&extension, &srp_I))
2054 if (PACKET_contains_zero_byte(&srp_I))
2058 * TODO(openssl-team): currently, we re-authenticate the user
2059 * upon resumption. Instead, we MUST ignore the login.
2061 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
2062 *al = TLS1_AD_INTERNAL_ERROR;
2068 #ifndef OPENSSL_NO_EC
2069 else if (type == TLSEXT_TYPE_ec_point_formats) {
2070 PACKET ec_point_format_list;
2072 if (!PACKET_as_length_prefixed_1(&extension,
2073 &ec_point_format_list)
2074 || PACKET_remaining(&ec_point_format_list) == 0) {
2079 if (!PACKET_memdup(&ec_point_format_list,
2080 &s->session->tlsext_ecpointformatlist,
2081 &s->session->tlsext_ecpointformatlist_length)) {
2082 *al = TLS1_AD_INTERNAL_ERROR;
2086 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2087 PACKET elliptic_curve_list;
2089 /* Each NamedCurve is 2 bytes and we must have at least 1. */
2090 if (!PACKET_as_length_prefixed_2(&extension,
2091 &elliptic_curve_list)
2092 || PACKET_remaining(&elliptic_curve_list) == 0
2093 || (PACKET_remaining(&elliptic_curve_list) % 2) != 0) {
2098 if (!PACKET_memdup(&elliptic_curve_list,
2099 &s->session->tlsext_ellipticcurvelist,
2100 &s->session->tlsext_ellipticcurvelist_length)) {
2101 *al = TLS1_AD_INTERNAL_ERROR;
2106 #endif /* OPENSSL_NO_EC */
2107 else if (type == TLSEXT_TYPE_session_ticket) {
2108 if (s->tls_session_ticket_ext_cb &&
2109 !s->tls_session_ticket_ext_cb(s, PACKET_data(&extension),
2110 PACKET_remaining(&extension),
2111 s->tls_session_ticket_ext_cb_arg)) {
2112 *al = TLS1_AD_INTERNAL_ERROR;
2115 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2116 PACKET supported_sig_algs;
2118 if (!PACKET_as_length_prefixed_2(&extension, &supported_sig_algs)
2119 || (PACKET_remaining(&supported_sig_algs) % 2) != 0
2120 || PACKET_remaining(&supported_sig_algs) == 0) {
2125 if (!tls1_save_sigalgs(s, PACKET_data(&supported_sig_algs),
2126 PACKET_remaining(&supported_sig_algs))) {
2130 } else if (type == TLSEXT_TYPE_status_request) {
2131 const unsigned char *ext_data;
2133 if (!PACKET_get_1(&extension,
2134 (unsigned int *)&s->tlsext_status_type)) {
2138 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2139 PACKET responder_id_list, exts;
2140 if (!PACKET_get_length_prefixed_2(&extension, &responder_id_list))
2143 while (PACKET_remaining(&responder_id_list) > 0) {
2145 PACKET responder_id;
2146 const unsigned char *id_data;
2148 if (!PACKET_get_length_prefixed_2(&responder_id_list,
2150 || PACKET_remaining(&responder_id) == 0) {
2154 if (s->tlsext_ocsp_ids == NULL
2155 && (s->tlsext_ocsp_ids =
2156 sk_OCSP_RESPID_new_null()) == NULL) {
2157 *al = SSL_AD_INTERNAL_ERROR;
2161 id_data = PACKET_data(&responder_id);
2162 id = d2i_OCSP_RESPID(NULL, &id_data,
2163 PACKET_remaining(&responder_id));
2167 if (id_data != PACKET_end(&responder_id)) {
2168 OCSP_RESPID_free(id);
2172 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2173 OCSP_RESPID_free(id);
2174 *al = SSL_AD_INTERNAL_ERROR;
2179 /* Read in request_extensions */
2180 if (!PACKET_as_length_prefixed_2(&extension, &exts))
2183 if (PACKET_remaining(&exts) > 0) {
2184 ext_data = PACKET_data(&exts);
2185 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2186 X509_EXTENSION_free);
2187 s->tlsext_ocsp_exts =
2188 d2i_X509_EXTENSIONS(NULL, &ext_data,
2189 PACKET_remaining(&exts));
2190 if (s->tlsext_ocsp_exts == NULL
2191 || ext_data != PACKET_end(&exts)) {
2196 * We don't know what to do with any other type * so ignore it.
2199 s->tlsext_status_type = -1;
2202 #ifndef OPENSSL_NO_HEARTBEATS
2203 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2204 unsigned int hbtype;
2206 if (!PACKET_get_1(&extension, &hbtype)
2207 || PACKET_remaining(&extension)) {
2208 *al = SSL_AD_DECODE_ERROR;
2212 case 0x01: /* Client allows us to send HB requests */
2213 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2215 case 0x02: /* Client doesn't accept HB requests */
2216 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2217 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2220 *al = SSL_AD_ILLEGAL_PARAMETER;
2225 #ifndef OPENSSL_NO_NEXTPROTONEG
2226 else if (type == TLSEXT_TYPE_next_proto_neg &&
2227 s->s3->tmp.finish_md_len == 0) {
2229 * We shouldn't accept this extension on a
2232 * s->new_session will be set on renegotiation, but we
2233 * probably shouldn't rely that it couldn't be set on
2234 * the initial renegotation too in certain cases (when
2235 * there's some other reason to disallow resuming an
2236 * earlier session -- the current code won't be doing
2237 * anything like that, but this might change).
2239 * A valid sign that there's been a previous handshake
2240 * in this connection is if s->s3->tmp.finish_md_len >
2241 * 0. (We are talking about a check that will happen
2242 * in the Hello protocol round, well before a new
2243 * Finished message could have been computed.)
2245 s->s3->next_proto_neg_seen = 1;
2249 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2250 s->s3->tmp.finish_md_len == 0) {
2251 if (!tls1_alpn_handle_client_hello(s, &extension, al))
2255 /* session ticket processed earlier */
2256 #ifndef OPENSSL_NO_SRTP
2257 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2258 && type == TLSEXT_TYPE_use_srtp) {
2259 if (ssl_parse_clienthello_use_srtp_ext(s, &extension, al))
2263 #ifdef TLSEXT_TYPE_encrypt_then_mac
2264 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2265 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2268 * Note: extended master secret extension handled in
2269 * tls_check_serverhello_tlsext_early()
2273 * If this ClientHello extension was unhandled and this is a
2274 * nonresumed connection, check whether the extension is a custom
2275 * TLS Extension (has a custom_srv_ext_record), and if so call the
2276 * callback and record the extension number so that an appropriate
2277 * ServerHello may be later returned.
2280 if (custom_ext_parse(s, 1, type, PACKET_data(&extension),
2281 PACKET_remaining(&extension), al) <= 0)
2286 if (PACKET_remaining(pkt) != 0) {
2287 /* tls1_check_duplicate_extensions should ensure this never happens. */
2288 *al = SSL_AD_INTERNAL_ERROR;
2294 /* Need RI if renegotiating */
2296 if (!renegotiate_seen && s->renegotiate &&
2297 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2298 *al = SSL_AD_HANDSHAKE_FAILURE;
2299 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2300 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2305 * This function currently has no state to clean up, so it returns directly.
2306 * If parsing fails at any point, the function returns early.
2307 * The SSL object may be left with partial data from extensions, but it must
2308 * then no longer be used, and clearing it up will free the leftovers.
2313 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2316 custom_ext_init(&s->cert->srv_ext);
2317 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2318 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2321 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2322 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2328 #ifndef OPENSSL_NO_NEXTPROTONEG
2330 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2331 * elements of zero length are allowed and the set of elements must exactly
2332 * fill the length of the block.
2334 static char ssl_next_proto_validate(PACKET *pkt)
2338 while (PACKET_remaining(pkt)) {
2339 if (!PACKET_get_1(pkt, &len)
2340 || !PACKET_forward(pkt, len))
2348 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2350 unsigned int length, type, size;
2351 int tlsext_servername = 0;
2352 int renegotiate_seen = 0;
2354 #ifndef OPENSSL_NO_NEXTPROTONEG
2355 s->s3->next_proto_neg_seen = 0;
2357 s->tlsext_ticket_expected = 0;
2359 OPENSSL_free(s->s3->alpn_selected);
2360 s->s3->alpn_selected = NULL;
2361 #ifndef OPENSSL_NO_HEARTBEATS
2362 s->tlsext_heartbeat &= ~(SSL_DTLSEXT_HB_ENABLED |
2363 SSL_DTLSEXT_HB_DONT_SEND_REQUESTS);
2366 #ifdef TLSEXT_TYPE_encrypt_then_mac
2367 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2370 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2372 if (!PACKET_get_net_2(pkt, &length))
2375 if (PACKET_remaining(pkt) != length) {
2376 *al = SSL_AD_DECODE_ERROR;
2380 if (!tls1_check_duplicate_extensions(pkt)) {
2381 *al = SSL_AD_DECODE_ERROR;
2385 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2386 const unsigned char *data;
2389 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2390 || !PACKET_peek_bytes(&spkt, &data, size))
2393 if (s->tlsext_debug_cb)
2394 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2396 if (type == TLSEXT_TYPE_renegotiate) {
2397 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2399 renegotiate_seen = 1;
2400 } else if (s->version == SSL3_VERSION) {
2401 } else if (type == TLSEXT_TYPE_server_name) {
2402 if (s->tlsext_hostname == NULL || size > 0) {
2403 *al = TLS1_AD_UNRECOGNIZED_NAME;
2406 tlsext_servername = 1;
2408 #ifndef OPENSSL_NO_EC
2409 else if (type == TLSEXT_TYPE_ec_point_formats) {
2410 unsigned int ecpointformatlist_length;
2411 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2412 || ecpointformatlist_length != size - 1) {
2413 *al = TLS1_AD_DECODE_ERROR;
2417 s->session->tlsext_ecpointformatlist_length = 0;
2418 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2419 if ((s->session->tlsext_ecpointformatlist =
2420 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2421 *al = TLS1_AD_INTERNAL_ERROR;
2424 s->session->tlsext_ecpointformatlist_length =
2425 ecpointformatlist_length;
2426 if (!PACKET_copy_bytes(&spkt,
2427 s->session->tlsext_ecpointformatlist,
2428 ecpointformatlist_length)) {
2429 *al = TLS1_AD_DECODE_ERROR;
2435 #endif /* OPENSSL_NO_EC */
2437 else if (type == TLSEXT_TYPE_session_ticket) {
2438 if (s->tls_session_ticket_ext_cb &&
2439 !s->tls_session_ticket_ext_cb(s, data, size,
2440 s->tls_session_ticket_ext_cb_arg))
2442 *al = TLS1_AD_INTERNAL_ERROR;
2445 if (!tls_use_ticket(s) || (size > 0)) {
2446 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2449 s->tlsext_ticket_expected = 1;
2451 else if (type == TLSEXT_TYPE_status_request) {
2453 * MUST be empty and only sent if we've requested a status
2456 if ((s->tlsext_status_type == -1) || (size > 0)) {
2457 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2460 /* Set flag to expect CertificateStatus message */
2461 s->tlsext_status_expected = 1;
2463 #ifndef OPENSSL_NO_CT
2465 * Only take it if we asked for it - i.e if there is no CT validation
2466 * callback set, then a custom extension MAY be processing it, so we
2467 * need to let control continue to flow to that.
2469 else if (type == TLSEXT_TYPE_signed_certificate_timestamp &&
2470 s->ct_validation_callback != NULL) {
2471 /* Simply copy it off for later processing */
2472 if (s->tlsext_scts != NULL) {
2473 OPENSSL_free(s->tlsext_scts);
2474 s->tlsext_scts = NULL;
2476 s->tlsext_scts_len = size;
2478 s->tlsext_scts = OPENSSL_malloc(size);
2479 if (s->tlsext_scts == NULL) {
2480 *al = TLS1_AD_INTERNAL_ERROR;
2483 memcpy(s->tlsext_scts, data, size);
2487 #ifndef OPENSSL_NO_NEXTPROTONEG
2488 else if (type == TLSEXT_TYPE_next_proto_neg &&
2489 s->s3->tmp.finish_md_len == 0) {
2490 unsigned char *selected;
2491 unsigned char selected_len;
2492 /* We must have requested it. */
2493 if (s->ctx->next_proto_select_cb == NULL) {
2494 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2497 /* The data must be valid */
2498 if (!ssl_next_proto_validate(&spkt)) {
2499 *al = TLS1_AD_DECODE_ERROR;
2503 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2505 s->ctx->next_proto_select_cb_arg) !=
2506 SSL_TLSEXT_ERR_OK) {
2507 *al = TLS1_AD_INTERNAL_ERROR;
2510 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2511 if (s->next_proto_negotiated == NULL) {
2512 *al = TLS1_AD_INTERNAL_ERROR;
2515 memcpy(s->next_proto_negotiated, selected, selected_len);
2516 s->next_proto_negotiated_len = selected_len;
2517 s->s3->next_proto_neg_seen = 1;
2521 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2523 /* We must have requested it. */
2524 if (!s->s3->alpn_sent) {
2525 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2529 * The extension data consists of:
2530 * uint16 list_length
2531 * uint8 proto_length;
2532 * uint8 proto[proto_length];
2534 if (!PACKET_get_net_2(&spkt, &len)
2535 || PACKET_remaining(&spkt) != len
2536 || !PACKET_get_1(&spkt, &len)
2537 || PACKET_remaining(&spkt) != len) {
2538 *al = TLS1_AD_DECODE_ERROR;
2541 OPENSSL_free(s->s3->alpn_selected);
2542 s->s3->alpn_selected = OPENSSL_malloc(len);
2543 if (s->s3->alpn_selected == NULL) {
2544 *al = TLS1_AD_INTERNAL_ERROR;
2547 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2548 *al = TLS1_AD_DECODE_ERROR;
2551 s->s3->alpn_selected_len = len;
2553 #ifndef OPENSSL_NO_HEARTBEATS
2554 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_heartbeat) {
2555 unsigned int hbtype;
2556 if (!PACKET_get_1(&spkt, &hbtype)) {
2557 *al = SSL_AD_DECODE_ERROR;
2561 case 0x01: /* Server allows us to send HB requests */
2562 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2564 case 0x02: /* Server doesn't accept HB requests */
2565 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_ENABLED;
2566 s->tlsext_heartbeat |= SSL_DTLSEXT_HB_DONT_SEND_REQUESTS;
2569 *al = SSL_AD_ILLEGAL_PARAMETER;
2574 #ifndef OPENSSL_NO_SRTP
2575 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2576 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2580 #ifdef TLSEXT_TYPE_encrypt_then_mac
2581 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2582 /* Ignore if inappropriate ciphersuite */
2583 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2584 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2585 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2588 else if (type == TLSEXT_TYPE_extended_master_secret) {
2589 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2591 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2594 * If this extension type was not otherwise handled, but matches a
2595 * custom_cli_ext_record, then send it to the c callback
2597 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2601 if (PACKET_remaining(pkt) != 0) {
2602 *al = SSL_AD_DECODE_ERROR;
2606 if (!s->hit && tlsext_servername == 1) {
2607 if (s->tlsext_hostname) {
2608 if (s->session->tlsext_hostname == NULL) {
2609 s->session->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
2610 if (!s->session->tlsext_hostname) {
2611 *al = SSL_AD_UNRECOGNIZED_NAME;
2615 *al = SSL_AD_DECODE_ERROR;
2624 * Determine if we need to see RI. Strictly speaking if we want to avoid
2625 * an attack we should *always* see RI even on initial server hello
2626 * because the client doesn't see any renegotiation during an attack.
2627 * However this would mean we could not connect to any server which
2628 * doesn't support RI so for the immediate future tolerate RI absence on
2629 * initial connect only.
2631 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2632 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2633 *al = SSL_AD_HANDSHAKE_FAILURE;
2634 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2635 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2641 * Check extended master secret extension is consistent with
2644 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
2645 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
2646 *al = SSL_AD_HANDSHAKE_FAILURE;
2647 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT, SSL_R_INCONSISTENT_EXTMS);
2655 int ssl_prepare_clienthello_tlsext(SSL *s)
2657 s->s3->alpn_sent = 0;
2661 int ssl_prepare_serverhello_tlsext(SSL *s)
2666 static int ssl_check_clienthello_tlsext_early(SSL *s)
2668 int ret = SSL_TLSEXT_ERR_NOACK;
2669 int al = SSL_AD_UNRECOGNIZED_NAME;
2671 #ifndef OPENSSL_NO_EC
2673 * The handling of the ECPointFormats extension is done elsewhere, namely
2674 * in ssl3_choose_cipher in s3_lib.c.
2677 * The handling of the EllipticCurves extension is done elsewhere, namely
2678 * in ssl3_choose_cipher in s3_lib.c.
2682 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2684 s->ctx->tlsext_servername_callback(s, &al,
2685 s->ctx->tlsext_servername_arg);
2686 else if (s->initial_ctx != NULL
2687 && s->initial_ctx->tlsext_servername_callback != 0)
2689 s->initial_ctx->tlsext_servername_callback(s, &al,
2691 initial_ctx->tlsext_servername_arg);
2694 case SSL_TLSEXT_ERR_ALERT_FATAL:
2695 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2698 case SSL_TLSEXT_ERR_ALERT_WARNING:
2699 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2702 case SSL_TLSEXT_ERR_NOACK:
2703 s->servername_done = 0;
2708 /* Initialise digests to default values */
2709 void ssl_set_default_md(SSL *s)
2711 const EVP_MD **pmd = s->s3->tmp.md;
2712 #ifndef OPENSSL_NO_DSA
2713 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2715 #ifndef OPENSSL_NO_RSA
2716 if (SSL_USE_SIGALGS(s))
2717 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2719 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2720 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2722 #ifndef OPENSSL_NO_EC
2723 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2725 #ifndef OPENSSL_NO_GOST
2726 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2727 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2728 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2732 int tls1_set_server_sigalgs(SSL *s)
2736 /* Clear any shared sigtnature algorithms */
2737 OPENSSL_free(s->cert->shared_sigalgs);
2738 s->cert->shared_sigalgs = NULL;
2739 s->cert->shared_sigalgslen = 0;
2740 /* Clear certificate digests and validity flags */
2741 for (i = 0; i < SSL_PKEY_NUM; i++) {
2742 s->s3->tmp.md[i] = NULL;
2743 s->s3->tmp.valid_flags[i] = 0;
2746 /* If sigalgs received process it. */
2747 if (s->s3->tmp.peer_sigalgs) {
2748 if (!tls1_process_sigalgs(s)) {
2749 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2750 al = SSL_AD_INTERNAL_ERROR;
2753 /* Fatal error is no shared signature algorithms */
2754 if (!s->cert->shared_sigalgs) {
2755 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2756 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2757 al = SSL_AD_ILLEGAL_PARAMETER;
2761 ssl_set_default_md(s);
2765 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2769 int ssl_check_clienthello_tlsext_late(SSL *s)
2771 int ret = SSL_TLSEXT_ERR_OK;
2772 int al = SSL_AD_INTERNAL_ERROR;
2775 * If status request then ask callback what to do. Note: this must be
2776 * called after servername callbacks in case the certificate has changed,
2777 * and must be called after the cipher has been chosen because this may
2778 * influence which certificate is sent
2780 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2782 CERT_PKEY *certpkey;
2783 certpkey = ssl_get_server_send_pkey(s);
2784 /* If no certificate can't return certificate status */
2785 if (certpkey == NULL) {
2786 s->tlsext_status_expected = 0;
2790 * Set current certificate to one we will use so SSL_get_certificate
2791 * et al can pick it up.
2793 s->cert->key = certpkey;
2794 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2796 /* We don't want to send a status request response */
2797 case SSL_TLSEXT_ERR_NOACK:
2798 s->tlsext_status_expected = 0;
2800 /* status request response should be sent */
2801 case SSL_TLSEXT_ERR_OK:
2802 if (s->tlsext_ocsp_resp)
2803 s->tlsext_status_expected = 1;
2805 s->tlsext_status_expected = 0;
2807 /* something bad happened */
2808 case SSL_TLSEXT_ERR_ALERT_FATAL:
2809 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2810 al = SSL_AD_INTERNAL_ERROR;
2814 s->tlsext_status_expected = 0;
2816 if (!tls1_alpn_handle_client_hello_late(s, &ret, &al)) {
2822 case SSL_TLSEXT_ERR_ALERT_FATAL:
2823 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2826 case SSL_TLSEXT_ERR_ALERT_WARNING:
2827 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2835 int ssl_check_serverhello_tlsext(SSL *s)
2837 int ret = SSL_TLSEXT_ERR_NOACK;
2838 int al = SSL_AD_UNRECOGNIZED_NAME;
2840 #ifndef OPENSSL_NO_EC
2842 * If we are client and using an elliptic curve cryptography cipher
2843 * suite, then if server returns an EC point formats lists extension it
2844 * must contain uncompressed.
2846 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2847 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2848 if ((s->tlsext_ecpointformatlist != NULL)
2849 && (s->tlsext_ecpointformatlist_length > 0)
2850 && (s->session->tlsext_ecpointformatlist != NULL)
2851 && (s->session->tlsext_ecpointformatlist_length > 0)
2852 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
2853 /* we are using an ECC cipher */
2855 unsigned char *list;
2856 int found_uncompressed = 0;
2857 list = s->session->tlsext_ecpointformatlist;
2858 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2859 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2860 found_uncompressed = 1;
2864 if (!found_uncompressed) {
2865 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2866 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2870 ret = SSL_TLSEXT_ERR_OK;
2871 #endif /* OPENSSL_NO_EC */
2873 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2875 s->ctx->tlsext_servername_callback(s, &al,
2876 s->ctx->tlsext_servername_arg);
2877 else if (s->initial_ctx != NULL
2878 && s->initial_ctx->tlsext_servername_callback != 0)
2880 s->initial_ctx->tlsext_servername_callback(s, &al,
2882 initial_ctx->tlsext_servername_arg);
2885 * Ensure we get sensible values passed to tlsext_status_cb in the event
2886 * that we don't receive a status message
2888 OPENSSL_free(s->tlsext_ocsp_resp);
2889 s->tlsext_ocsp_resp = NULL;
2890 s->tlsext_ocsp_resplen = -1;
2893 case SSL_TLSEXT_ERR_ALERT_FATAL:
2894 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2897 case SSL_TLSEXT_ERR_ALERT_WARNING:
2898 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2901 case SSL_TLSEXT_ERR_NOACK:
2902 s->servername_done = 0;
2908 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2911 if (s->version < SSL3_VERSION)
2913 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2914 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2918 if (ssl_check_serverhello_tlsext(s) <= 0) {
2919 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2926 * Since the server cache lookup is done early on in the processing of the
2927 * ClientHello and other operations depend on the result some extensions
2928 * need to be handled at the same time.
2930 * Two extensions are currently handled, session ticket and extended master
2933 * session_id: ClientHello session ID.
2934 * ext: ClientHello extensions (including length prefix)
2935 * ret: (output) on return, if a ticket was decrypted, then this is set to
2936 * point to the resulting session.
2938 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2939 * ciphersuite, in which case we have no use for session tickets and one will
2940 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2943 * -1: fatal error, either from parsing or decrypting the ticket.
2944 * 0: no ticket was found (or was ignored, based on settings).
2945 * 1: a zero length extension was found, indicating that the client supports
2946 * session tickets but doesn't currently have one to offer.
2947 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2948 * couldn't be decrypted because of a non-fatal error.
2949 * 3: a ticket was successfully decrypted and *ret was set.
2952 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2953 * a new session ticket to the client because the client indicated support
2954 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2955 * a session ticket or we couldn't use the one it gave us, or if
2956 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2957 * Otherwise, s->tlsext_ticket_expected is set to 0.
2959 * For extended master secret flag is set if the extension is present.
2962 int tls_check_serverhello_tlsext_early(SSL *s, const PACKET *ext,
2963 const PACKET *session_id,
2967 PACKET local_ext = *ext;
2970 int have_ticket = 0;
2971 int use_ticket = tls_use_ticket(s);
2974 s->tlsext_ticket_expected = 0;
2975 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
2978 * If tickets disabled behave as if no ticket present to permit stateful
2981 if ((s->version <= SSL3_VERSION))
2984 if (!PACKET_get_net_2(&local_ext, &i)) {
2988 while (PACKET_remaining(&local_ext) >= 4) {
2989 unsigned int type, size;
2991 if (!PACKET_get_net_2(&local_ext, &type)
2992 || !PACKET_get_net_2(&local_ext, &size)) {
2993 /* Shouldn't ever happen */
2997 if (PACKET_remaining(&local_ext) < size) {
3001 if (type == TLSEXT_TYPE_session_ticket && use_ticket) {
3003 const unsigned char *etick;
3005 /* Duplicate extension */
3006 if (have_ticket != 0) {
3014 * The client will accept a ticket but doesn't currently have
3017 s->tlsext_ticket_expected = 1;
3021 if (s->tls_session_secret_cb) {
3023 * Indicate that the ticket couldn't be decrypted rather than
3024 * generating the session from ticket now, trigger
3025 * abbreviated handshake based on external mechanism to
3026 * calculate the master secret later.
3031 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
3032 /* Shouldn't ever happen */
3036 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
3037 PACKET_remaining(session_id), ret);
3039 case 2: /* ticket couldn't be decrypted */
3040 s->tlsext_ticket_expected = 1;
3043 case 3: /* ticket was decrypted */
3046 case 4: /* ticket decrypted but need to renew */
3047 s->tlsext_ticket_expected = 1;
3050 default: /* fatal error */
3056 if (type == TLSEXT_TYPE_extended_master_secret)
3057 s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
3058 if (!PACKET_forward(&local_ext, size)) {
3064 if (have_ticket == 0)
3071 * tls_decrypt_ticket attempts to decrypt a session ticket.
3073 * etick: points to the body of the session ticket extension.
3074 * eticklen: the length of the session tickets extenion.
3075 * sess_id: points at the session ID.
3076 * sesslen: the length of the session ID.
3077 * psess: (output) on return, if a ticket was decrypted, then this is set to
3078 * point to the resulting session.
3081 * -2: fatal error, malloc failure.
3082 * -1: fatal error, either from parsing or decrypting the ticket.
3083 * 2: the ticket couldn't be decrypted.
3084 * 3: a ticket was successfully decrypted and *psess was set.
3085 * 4: same as 3, but the ticket needs to be renewed.
3087 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3088 int eticklen, const unsigned char *sess_id,
3089 int sesslen, SSL_SESSION **psess)
3092 unsigned char *sdec;
3093 const unsigned char *p;
3094 int slen, mlen, renew_ticket = 0, ret = -1;
3095 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3096 HMAC_CTX *hctx = NULL;
3097 EVP_CIPHER_CTX *ctx;
3098 SSL_CTX *tctx = s->initial_ctx;
3099 /* Need at least keyname + iv + some encrypted data */
3102 /* Initialize session ticket encryption and HMAC contexts */
3103 hctx = HMAC_CTX_new();
3106 ctx = EVP_CIPHER_CTX_new();
3111 if (tctx->tlsext_ticket_key_cb) {
3112 unsigned char *nctick = (unsigned char *)etick;
3113 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3124 /* Check key name matches */
3125 if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
3129 if (HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 16,
3130 EVP_sha256(), NULL) <= 0
3131 || EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL,
3132 tctx->tlsext_tick_aes_key,
3138 * Attempt to process session ticket, first conduct sanity and integrity
3141 mlen = HMAC_size(hctx);
3146 /* Check HMAC of encrypted ticket */
3147 if (HMAC_Update(hctx, etick, eticklen) <= 0
3148 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
3151 HMAC_CTX_free(hctx);
3152 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3153 EVP_CIPHER_CTX_free(ctx);
3156 /* Attempt to decrypt session data */
3157 /* Move p after IV to start of encrypted ticket, update length */
3158 p = etick + 16 + EVP_CIPHER_CTX_iv_length(ctx);
3159 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(ctx);
3160 sdec = OPENSSL_malloc(eticklen);
3162 || EVP_DecryptUpdate(ctx, sdec, &slen, p, eticklen) <= 0) {
3163 EVP_CIPHER_CTX_free(ctx);
3167 if (EVP_DecryptFinal(ctx, sdec + slen, &mlen) <= 0) {
3168 EVP_CIPHER_CTX_free(ctx);
3173 EVP_CIPHER_CTX_free(ctx);
3177 sess = d2i_SSL_SESSION(NULL, &p, slen);
3181 * The session ID, if non-empty, is used by some clients to detect
3182 * that the ticket has been accepted. So we copy it to the session
3183 * structure. If it is empty set length to zero as required by
3187 memcpy(sess->session_id, sess_id, sesslen);
3188 sess->session_id_length = sesslen;
3197 * For session parse failure, indicate that we need to send a new ticket.
3201 EVP_CIPHER_CTX_free(ctx);
3202 HMAC_CTX_free(hctx);
3206 /* Tables to translate from NIDs to TLS v1.2 ids */
3213 static const tls12_lookup tls12_md[] = {
3214 {NID_md5, TLSEXT_hash_md5},
3215 {NID_sha1, TLSEXT_hash_sha1},
3216 {NID_sha224, TLSEXT_hash_sha224},
3217 {NID_sha256, TLSEXT_hash_sha256},
3218 {NID_sha384, TLSEXT_hash_sha384},
3219 {NID_sha512, TLSEXT_hash_sha512},
3220 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3221 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3222 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3225 static const tls12_lookup tls12_sig[] = {
3226 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3227 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3228 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3229 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3230 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3231 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3234 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3237 for (i = 0; i < tlen; i++) {
3238 if (table[i].nid == nid)
3244 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3247 for (i = 0; i < tlen; i++) {
3248 if ((table[i].id) == id)
3249 return table[i].nid;
3254 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3260 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3263 sig_id = tls12_get_sigid(pk);
3266 p[0] = (unsigned char)md_id;
3267 p[1] = (unsigned char)sig_id;
3271 int tls12_get_sigid(const EVP_PKEY *pk)
3273 return tls12_find_id(EVP_PKEY_id(pk), tls12_sig, OSSL_NELEM(tls12_sig));
3280 unsigned char tlsext_hash;
3283 static const tls12_hash_info tls12_md_info[] = {
3284 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3285 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3286 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3287 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3288 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3289 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3290 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3291 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3292 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3295 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3301 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3303 if (tls12_md_info[i].tlsext_hash == hash_alg)
3304 return tls12_md_info + i;
3310 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3312 const tls12_hash_info *inf;
3313 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3315 inf = tls12_get_hash_info(hash_alg);
3318 return ssl_md(inf->md_idx);
3321 static int tls12_get_pkey_idx(unsigned char sig_alg)
3324 #ifndef OPENSSL_NO_RSA
3325 case TLSEXT_signature_rsa:
3326 return SSL_PKEY_RSA_SIGN;
3328 #ifndef OPENSSL_NO_DSA
3329 case TLSEXT_signature_dsa:
3330 return SSL_PKEY_DSA_SIGN;
3332 #ifndef OPENSSL_NO_EC
3333 case TLSEXT_signature_ecdsa:
3334 return SSL_PKEY_ECC;
3336 # ifndef OPENSSL_NO_GOST
3337 case TLSEXT_signature_gostr34102001:
3338 return SSL_PKEY_GOST01;
3340 case TLSEXT_signature_gostr34102012_256:
3341 return SSL_PKEY_GOST12_256;
3343 case TLSEXT_signature_gostr34102012_512:
3344 return SSL_PKEY_GOST12_512;
3350 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3351 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3352 int *psignhash_nid, const unsigned char *data)
3354 int sign_nid = NID_undef, hash_nid = NID_undef;
3355 if (!phash_nid && !psign_nid && !psignhash_nid)
3357 if (phash_nid || psignhash_nid) {
3358 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3360 *phash_nid = hash_nid;
3362 if (psign_nid || psignhash_nid) {
3363 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3365 *psign_nid = sign_nid;
3367 if (psignhash_nid) {
3368 if (sign_nid == NID_undef || hash_nid == NID_undef
3369 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3371 *psignhash_nid = NID_undef;
3375 /* Check to see if a signature algorithm is allowed */
3376 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3378 /* See if we have an entry in the hash table and it is enabled */
3379 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3380 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3382 /* See if public key algorithm allowed */
3383 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3385 /* Finally see if security callback allows it */
3386 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3390 * Get a mask of disabled public key algorithms based on supported signature
3391 * algorithms. For example if no signature algorithm supports RSA then RSA is
3395 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3397 const unsigned char *sigalgs;
3398 size_t i, sigalgslen;
3399 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3401 * Now go through all signature algorithms seeing if we support any for
3402 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3403 * down calls to security callback only check if we have to.
3405 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3406 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3407 switch (sigalgs[1]) {
3408 #ifndef OPENSSL_NO_RSA
3409 case TLSEXT_signature_rsa:
3410 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3414 #ifndef OPENSSL_NO_DSA
3415 case TLSEXT_signature_dsa:
3416 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3420 #ifndef OPENSSL_NO_EC
3421 case TLSEXT_signature_ecdsa:
3422 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3429 *pmask_a |= SSL_aRSA;
3431 *pmask_a |= SSL_aDSS;
3433 *pmask_a |= SSL_aECDSA;
3436 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3437 const unsigned char *psig, size_t psiglen)
3439 unsigned char *tmpout = out;
3441 for (i = 0; i < psiglen; i += 2, psig += 2) {
3442 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3443 *tmpout++ = psig[0];
3444 *tmpout++ = psig[1];
3447 return tmpout - out;
3450 /* Given preference and allowed sigalgs set shared sigalgs */
3451 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3452 const unsigned char *pref, size_t preflen,
3453 const unsigned char *allow, size_t allowlen)
3455 const unsigned char *ptmp, *atmp;
3456 size_t i, j, nmatch = 0;
3457 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3458 /* Skip disabled hashes or signature algorithms */
3459 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3461 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3462 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3465 shsig->rhash = ptmp[0];
3466 shsig->rsign = ptmp[1];
3467 tls1_lookup_sigalg(&shsig->hash_nid,
3469 &shsig->signandhash_nid, ptmp);
3479 /* Set shared signature algorithms for SSL structures */
3480 static int tls1_set_shared_sigalgs(SSL *s)
3482 const unsigned char *pref, *allow, *conf;
3483 size_t preflen, allowlen, conflen;
3485 TLS_SIGALGS *salgs = NULL;
3487 unsigned int is_suiteb = tls1_suiteb(s);
3489 OPENSSL_free(c->shared_sigalgs);
3490 c->shared_sigalgs = NULL;
3491 c->shared_sigalgslen = 0;
3492 /* If client use client signature algorithms if not NULL */
3493 if (!s->server && c->client_sigalgs && !is_suiteb) {
3494 conf = c->client_sigalgs;
3495 conflen = c->client_sigalgslen;
3496 } else if (c->conf_sigalgs && !is_suiteb) {
3497 conf = c->conf_sigalgs;
3498 conflen = c->conf_sigalgslen;
3500 conflen = tls12_get_psigalgs(s, &conf);
3501 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3504 allow = s->s3->tmp.peer_sigalgs;
3505 allowlen = s->s3->tmp.peer_sigalgslen;
3509 pref = s->s3->tmp.peer_sigalgs;
3510 preflen = s->s3->tmp.peer_sigalgslen;
3512 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3514 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3517 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3521 c->shared_sigalgs = salgs;
3522 c->shared_sigalgslen = nmatch;
3526 /* Set preferred digest for each key type */
3528 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3531 /* Extension ignored for inappropriate versions */
3532 if (!SSL_USE_SIGALGS(s))
3534 /* Should never happen */
3538 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3539 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3540 if (s->s3->tmp.peer_sigalgs == NULL)
3542 s->s3->tmp.peer_sigalgslen = dsize;
3543 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3547 int tls1_process_sigalgs(SSL *s)
3552 const EVP_MD **pmd = s->s3->tmp.md;
3553 uint32_t *pvalid = s->s3->tmp.valid_flags;
3555 TLS_SIGALGS *sigptr;
3556 if (!tls1_set_shared_sigalgs(s))
3559 for (i = 0, sigptr = c->shared_sigalgs;
3560 i < c->shared_sigalgslen; i++, sigptr++) {
3561 idx = tls12_get_pkey_idx(sigptr->rsign);
3562 if (idx > 0 && pmd[idx] == NULL) {
3563 md = tls12_get_hash(sigptr->rhash);
3565 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3566 if (idx == SSL_PKEY_RSA_SIGN) {
3567 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3568 pmd[SSL_PKEY_RSA_ENC] = md;
3574 * In strict mode leave unset digests as NULL to indicate we can't use
3575 * the certificate for signing.
3577 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3579 * Set any remaining keys to default values. NOTE: if alg is not
3580 * supported it stays as NULL.
3582 #ifndef OPENSSL_NO_DSA
3583 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3584 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3586 #ifndef OPENSSL_NO_RSA
3587 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3588 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3589 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3592 #ifndef OPENSSL_NO_EC
3593 if (pmd[SSL_PKEY_ECC] == NULL)
3594 pmd[SSL_PKEY_ECC] = EVP_sha1();
3596 # ifndef OPENSSL_NO_GOST
3597 if (pmd[SSL_PKEY_GOST01] == NULL)
3598 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3599 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3600 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3601 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3602 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3608 int SSL_get_sigalgs(SSL *s, int idx,
3609 int *psign, int *phash, int *psignhash,
3610 unsigned char *rsig, unsigned char *rhash)
3612 const unsigned char *psig = s->s3->tmp.peer_sigalgs;