2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include <openssl/ocsp.h>
117 #include <openssl/rand.h>
118 #ifndef OPENSSL_NO_DH
119 # include <openssl/dh.h>
120 # include <openssl/bn.h>
122 #include "ssl_locl.h"
124 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
125 const unsigned char *sess_id, int sesslen,
126 SSL_SESSION **psess);
127 static int ssl_check_clienthello_tlsext_early(SSL *s);
128 int ssl_check_serverhello_tlsext(SSL *s);
130 SSL3_ENC_METHOD const TLSv1_enc_data = {
133 tls1_setup_key_block,
134 tls1_generate_master_secret,
135 tls1_change_cipher_state,
136 tls1_final_finish_mac,
137 TLS1_FINISH_MAC_LENGTH,
138 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
139 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
141 tls1_export_keying_material,
143 SSL3_HM_HEADER_LENGTH,
144 ssl3_set_handshake_header,
148 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
151 tls1_setup_key_block,
152 tls1_generate_master_secret,
153 tls1_change_cipher_state,
154 tls1_final_finish_mac,
155 TLS1_FINISH_MAC_LENGTH,
156 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
157 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
159 tls1_export_keying_material,
160 SSL_ENC_FLAG_EXPLICIT_IV,
161 SSL3_HM_HEADER_LENGTH,
162 ssl3_set_handshake_header,
166 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
169 tls1_setup_key_block,
170 tls1_generate_master_secret,
171 tls1_change_cipher_state,
172 tls1_final_finish_mac,
173 TLS1_FINISH_MAC_LENGTH,
174 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
175 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
177 tls1_export_keying_material,
178 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
179 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
180 SSL3_HM_HEADER_LENGTH,
181 ssl3_set_handshake_header,
185 long tls1_default_timeout(void)
188 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
189 * http, the cache would over fill
191 return (60 * 60 * 2);
198 s->method->ssl_clear(s);
202 void tls1_free(SSL *s)
204 OPENSSL_free(s->tlsext_session_ticket);
208 void tls1_clear(SSL *s)
211 s->version = s->method->version;
214 #ifndef OPENSSL_NO_EC
217 int nid; /* Curve NID */
218 int secbits; /* Bits of security (from SP800-57) */
219 unsigned int flags; /* Flags: currently just field type */
222 # define TLS_CURVE_CHAR2 0x1
223 # define TLS_CURVE_PRIME 0x0
225 static const tls_curve_info nid_list[] = {
226 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
227 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
228 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
229 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
230 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
231 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
232 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
233 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
234 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
235 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
236 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
237 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
238 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
239 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
240 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
241 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
242 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
243 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
244 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
245 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
246 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
247 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
248 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
249 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
250 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
251 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
252 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
253 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
256 static const unsigned char ecformats_default[] = {
257 TLSEXT_ECPOINTFORMAT_uncompressed,
258 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
259 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
262 /* The client's default curves / the server's 'auto' curves. */
263 static const unsigned char eccurves_auto[] = {
264 /* Prefer P-256 which has the fastest and most secure implementations. */
265 0, 23, /* secp256r1 (23) */
266 /* Other >= 256-bit prime curves. */
267 0, 25, /* secp521r1 (25) */
268 0, 28, /* brainpool512r1 (28) */
269 0, 27, /* brainpoolP384r1 (27) */
270 0, 24, /* secp384r1 (24) */
271 0, 26, /* brainpoolP256r1 (26) */
272 0, 22, /* secp256k1 (22) */
273 /* >= 256-bit binary curves. */
274 0, 14, /* sect571r1 (14) */
275 0, 13, /* sect571k1 (13) */
276 0, 11, /* sect409k1 (11) */
277 0, 12, /* sect409r1 (12) */
278 0, 9, /* sect283k1 (9) */
279 0, 10, /* sect283r1 (10) */
282 static const unsigned char eccurves_all[] = {
283 /* Prefer P-256 which has the fastest and most secure implementations. */
284 0, 23, /* secp256r1 (23) */
285 /* Other >= 256-bit prime curves. */
286 0, 25, /* secp521r1 (25) */
287 0, 28, /* brainpool512r1 (28) */
288 0, 27, /* brainpoolP384r1 (27) */
289 0, 24, /* secp384r1 (24) */
290 0, 26, /* brainpoolP256r1 (26) */
291 0, 22, /* secp256k1 (22) */
292 /* >= 256-bit binary curves. */
293 0, 14, /* sect571r1 (14) */
294 0, 13, /* sect571k1 (13) */
295 0, 11, /* sect409k1 (11) */
296 0, 12, /* sect409r1 (12) */
297 0, 9, /* sect283k1 (9) */
298 0, 10, /* sect283r1 (10) */
300 * Remaining curves disabled by default but still permitted if set
301 * via an explicit callback or parameters.
303 0, 20, /* secp224k1 (20) */
304 0, 21, /* secp224r1 (21) */
305 0, 18, /* secp192k1 (18) */
306 0, 19, /* secp192r1 (19) */
307 0, 15, /* secp160k1 (15) */
308 0, 16, /* secp160r1 (16) */
309 0, 17, /* secp160r2 (17) */
310 0, 8, /* sect239k1 (8) */
311 0, 6, /* sect233k1 (6) */
312 0, 7, /* sect233r1 (7) */
313 0, 4, /* sect193r1 (4) */
314 0, 5, /* sect193r2 (5) */
315 0, 1, /* sect163k1 (1) */
316 0, 2, /* sect163r1 (2) */
317 0, 3, /* sect163r2 (3) */
321 static const unsigned char suiteb_curves[] = {
322 0, TLSEXT_curve_P_256,
323 0, TLSEXT_curve_P_384
326 int tls1_ec_curve_id2nid(int curve_id)
328 /* ECC curves from RFC 4492 and RFC 7027 */
329 if ((curve_id < 1) || ((unsigned int)curve_id > OSSL_NELEM(nid_list)))
331 return nid_list[curve_id - 1].nid;
334 int tls1_ec_nid2curve_id(int nid)
336 /* ECC curves from RFC 4492 and RFC 7027 */
338 case NID_sect163k1: /* sect163k1 (1) */
340 case NID_sect163r1: /* sect163r1 (2) */
342 case NID_sect163r2: /* sect163r2 (3) */
344 case NID_sect193r1: /* sect193r1 (4) */
346 case NID_sect193r2: /* sect193r2 (5) */
348 case NID_sect233k1: /* sect233k1 (6) */
350 case NID_sect233r1: /* sect233r1 (7) */
352 case NID_sect239k1: /* sect239k1 (8) */
354 case NID_sect283k1: /* sect283k1 (9) */
356 case NID_sect283r1: /* sect283r1 (10) */
358 case NID_sect409k1: /* sect409k1 (11) */
360 case NID_sect409r1: /* sect409r1 (12) */
362 case NID_sect571k1: /* sect571k1 (13) */
364 case NID_sect571r1: /* sect571r1 (14) */
366 case NID_secp160k1: /* secp160k1 (15) */
368 case NID_secp160r1: /* secp160r1 (16) */
370 case NID_secp160r2: /* secp160r2 (17) */
372 case NID_secp192k1: /* secp192k1 (18) */
374 case NID_X9_62_prime192v1: /* secp192r1 (19) */
376 case NID_secp224k1: /* secp224k1 (20) */
378 case NID_secp224r1: /* secp224r1 (21) */
380 case NID_secp256k1: /* secp256k1 (22) */
382 case NID_X9_62_prime256v1: /* secp256r1 (23) */
384 case NID_secp384r1: /* secp384r1 (24) */
386 case NID_secp521r1: /* secp521r1 (25) */
388 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */
390 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */
392 case NID_brainpoolP512r1: /* brainpool512r1 (28) */
400 * Get curves list, if "sess" is set return client curves otherwise
402 * Sets |num_curves| to the number of curves in the list, i.e.,
403 * the length of |pcurves| is 2 * num_curves.
404 * Returns 1 on success and 0 if the client curves list has invalid format.
405 * The latter indicates an internal error: we should not be accepting such
406 * lists in the first place.
407 * TODO(emilia): we should really be storing the curves list in explicitly
408 * parsed form instead. (However, this would affect binary compatibility
409 * so cannot happen in the 1.0.x series.)
411 static int tls1_get_curvelist(SSL *s, int sess,
412 const unsigned char **pcurves,
415 size_t pcurveslen = 0;
417 *pcurves = s->session->tlsext_ellipticcurvelist;
418 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
420 /* For Suite B mode only include P-256, P-384 */
421 switch (tls1_suiteb(s)) {
422 case SSL_CERT_FLAG_SUITEB_128_LOS:
423 *pcurves = suiteb_curves;
424 pcurveslen = sizeof(suiteb_curves);
427 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
428 *pcurves = suiteb_curves;
432 case SSL_CERT_FLAG_SUITEB_192_LOS:
433 *pcurves = suiteb_curves + 2;
437 *pcurves = s->tlsext_ellipticcurvelist;
438 pcurveslen = s->tlsext_ellipticcurvelist_length;
441 if (!s->server || s->cert->ecdh_tmp_auto) {
442 *pcurves = eccurves_auto;
443 pcurveslen = sizeof(eccurves_auto);
445 *pcurves = eccurves_all;
446 pcurveslen = sizeof(eccurves_all);
451 /* We do not allow odd length arrays to enter the system. */
452 if (pcurveslen & 1) {
453 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
457 *num_curves = pcurveslen / 2;
462 /* See if curve is allowed by security callback */
463 static int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)
465 const tls_curve_info *cinfo;
468 if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))
470 cinfo = &nid_list[curve[1] - 1];
471 # ifdef OPENSSL_NO_EC2M
472 if (cinfo->flags & TLS_CURVE_CHAR2)
475 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);
478 /* Check a curve is one of our preferences */
479 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
481 const unsigned char *curves;
482 size_t num_curves, i;
483 unsigned int suiteb_flags = tls1_suiteb(s);
484 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
486 /* Check curve matches Suite B preferences */
488 unsigned long cid = s->s3->tmp.new_cipher->id;
491 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
492 if (p[2] != TLSEXT_curve_P_256)
494 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
495 if (p[2] != TLSEXT_curve_P_384)
497 } else /* Should never happen */
500 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
502 for (i = 0; i < num_curves; i++, curves += 2) {
503 if (p[1] == curves[0] && p[2] == curves[1])
504 return tls_curve_allowed(s, p + 1, SSL_SECOP_CURVE_CHECK);
510 * Return |nmatch|th shared curve or NID_undef if there is no match.
511 * For nmatch == -1, return number of matches
512 * For nmatch == -2, return the NID of the curve to use for
513 * an EC tmp key, or NID_undef if there is no match.
515 int tls1_shared_curve(SSL *s, int nmatch)
517 const unsigned char *pref, *supp;
518 size_t num_pref, num_supp, i, j;
520 /* Can't do anything on client side */
524 if (tls1_suiteb(s)) {
526 * For Suite B ciphersuite determines curve: we already know
527 * these are acceptable due to previous checks.
529 unsigned long cid = s->s3->tmp.new_cipher->id;
530 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
531 return NID_X9_62_prime256v1; /* P-256 */
532 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
533 return NID_secp384r1; /* P-384 */
534 /* Should never happen */
537 /* If not Suite B just return first preference shared curve */
541 * Avoid truncation. tls1_get_curvelist takes an int
542 * but s->options is a long...
544 if (!tls1_get_curvelist
545 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
547 /* In practice, NID_undef == 0 but let's be precise. */
548 return nmatch == -1 ? 0 : NID_undef;
549 if (!tls1_get_curvelist
550 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
552 return nmatch == -1 ? 0 : NID_undef;
555 * If the client didn't send the elliptic_curves extension all of them
558 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
560 num_supp = sizeof(eccurves_all) / 2;
561 } else if (num_pref == 0 &&
562 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
564 num_pref = sizeof(eccurves_all) / 2;
568 for (i = 0; i < num_pref; i++, pref += 2) {
569 const unsigned char *tsupp = supp;
570 for (j = 0; j < num_supp; j++, tsupp += 2) {
571 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
572 if (!tls_curve_allowed(s, pref, SSL_SECOP_CURVE_SHARED))
575 int id = (pref[0] << 8) | pref[1];
576 return tls1_ec_curve_id2nid(id);
584 /* Out of range (nmatch > k). */
588 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
589 int *curves, size_t ncurves)
591 unsigned char *clist, *p;
594 * Bitmap of curves included to detect duplicates: only works while curve
597 unsigned long dup_list = 0;
598 clist = OPENSSL_malloc(ncurves * 2);
601 for (i = 0, p = clist; i < ncurves; i++) {
602 unsigned long idmask;
604 id = tls1_ec_nid2curve_id(curves[i]);
606 if (!id || (dup_list & idmask)) {
615 *pextlen = ncurves * 2;
619 # define MAX_CURVELIST 28
623 int nid_arr[MAX_CURVELIST];
626 static int nid_cb(const char *elem, int len, void *arg)
628 nid_cb_st *narg = arg;
634 if (narg->nidcnt == MAX_CURVELIST)
636 if (len > (int)(sizeof(etmp) - 1))
638 memcpy(etmp, elem, len);
640 nid = EC_curve_nist2nid(etmp);
641 if (nid == NID_undef)
642 nid = OBJ_sn2nid(etmp);
643 if (nid == NID_undef)
644 nid = OBJ_ln2nid(etmp);
645 if (nid == NID_undef)
647 for (i = 0; i < narg->nidcnt; i++)
648 if (narg->nid_arr[i] == nid)
650 narg->nid_arr[narg->nidcnt++] = nid;
654 /* Set curves based on a colon separate list */
655 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
660 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
664 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
667 /* For an EC key set TLS id and required compression based on parameters */
668 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
673 const EC_METHOD *meth;
676 /* Determine if it is a prime field */
677 grp = EC_KEY_get0_group(ec);
680 meth = EC_GROUP_method_of(grp);
683 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
687 /* Determine curve ID */
688 id = EC_GROUP_get_curve_name(grp);
689 id = tls1_ec_nid2curve_id(id);
690 /* If we have an ID set it, otherwise set arbitrary explicit curve */
693 curve_id[1] = (unsigned char)id;
702 if (EC_KEY_get0_public_key(ec) == NULL)
704 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
706 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
708 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
710 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
715 /* Check an EC key is compatible with extensions */
716 static int tls1_check_ec_key(SSL *s,
717 unsigned char *curve_id, unsigned char *comp_id)
719 const unsigned char *pformats, *pcurves;
720 size_t num_formats, num_curves, i;
723 * If point formats extension present check it, otherwise everything is
724 * supported (see RFC4492).
726 if (comp_id && s->session->tlsext_ecpointformatlist) {
727 pformats = s->session->tlsext_ecpointformatlist;
728 num_formats = s->session->tlsext_ecpointformatlist_length;
729 for (i = 0; i < num_formats; i++, pformats++) {
730 if (*comp_id == *pformats)
733 if (i == num_formats)
738 /* Check curve is consistent with client and server preferences */
739 for (j = 0; j <= 1; j++) {
740 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
742 if (j == 1 && num_curves == 0) {
744 * If we've not received any curves then skip this check.
745 * RFC 4492 does not require the supported elliptic curves extension
746 * so if it is not sent we can just choose any curve.
747 * It is invalid to send an empty list in the elliptic curves
748 * extension, so num_curves == 0 always means no extension.
752 for (i = 0; i < num_curves; i++, pcurves += 2) {
753 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
758 /* For clients can only check sent curve list */
765 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
769 * If we have a custom point format list use it otherwise use default
771 if (s->tlsext_ecpointformatlist) {
772 *pformats = s->tlsext_ecpointformatlist;
773 *num_formats = s->tlsext_ecpointformatlist_length;
775 *pformats = ecformats_default;
776 /* For Suite B we don't support char2 fields */
778 *num_formats = sizeof(ecformats_default) - 1;
780 *num_formats = sizeof(ecformats_default);
785 * Check cert parameters compatible with extensions: currently just checks EC
786 * certificates have compatible curves and compression.
788 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
790 unsigned char comp_id, curve_id[2];
793 pkey = X509_get_pubkey(x);
796 /* If not EC nothing to do */
797 if (pkey->type != EVP_PKEY_EC) {
801 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
806 * Can't check curve_id for client certs as we don't have a supported
809 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
813 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
814 * SHA384+P-384, adjust digest if necessary.
816 if (set_ee_md && tls1_suiteb(s)) {
822 /* Check to see we have necessary signing algorithm */
823 if (curve_id[1] == TLSEXT_curve_P_256)
824 check_md = NID_ecdsa_with_SHA256;
825 else if (curve_id[1] == TLSEXT_curve_P_384)
826 check_md = NID_ecdsa_with_SHA384;
828 return 0; /* Should never happen */
829 for (i = 0; i < c->shared_sigalgslen; i++)
830 if (check_md == c->shared_sigalgs[i].signandhash_nid)
832 if (i == c->shared_sigalgslen)
834 if (set_ee_md == 2) {
835 if (check_md == NID_ecdsa_with_SHA256)
836 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha256();
838 s->s3->tmp.md[SSL_PKEY_ECC] = EVP_sha384();
844 # ifndef OPENSSL_NO_EC
845 /* Check EC temporary key is compatible with client extensions */
846 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
848 unsigned char curve_id[2];
849 EC_KEY *ec = s->cert->ecdh_tmp;
850 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
851 /* Allow any curve: not just those peer supports */
852 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
856 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
859 if (tls1_suiteb(s)) {
860 /* Curve to check determined by ciphersuite */
861 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
862 curve_id[1] = TLSEXT_curve_P_256;
863 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
864 curve_id[1] = TLSEXT_curve_P_384;
868 /* Check this curve is acceptable */
869 if (!tls1_check_ec_key(s, curve_id, NULL))
871 /* If auto or setting curve from callback assume OK */
872 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
874 /* Otherwise check curve is acceptable */
876 unsigned char curve_tmp[2];
879 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
881 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
887 if (s->cert->ecdh_tmp_auto) {
888 /* Need a shared curve */
889 if (tls1_shared_curve(s, 0))
895 if (s->cert->ecdh_tmp_cb)
900 if (!tls1_set_ec_id(curve_id, NULL, ec))
902 /* Set this to allow use of invalid curves for testing */
906 return tls1_check_ec_key(s, curve_id, NULL);
909 # endif /* OPENSSL_NO_EC */
913 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
918 #endif /* OPENSSL_NO_EC */
921 * List of supported signature algorithms and hashes. Should make this
922 * customisable at some point, for now include everything we support.
925 #ifdef OPENSSL_NO_RSA
926 # define tlsext_sigalg_rsa(md) /* */
928 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
931 #ifdef OPENSSL_NO_DSA
932 # define tlsext_sigalg_dsa(md) /* */
934 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
938 # define tlsext_sigalg_ecdsa(md) /* */
940 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
943 #define tlsext_sigalg(md) \
944 tlsext_sigalg_rsa(md) \
945 tlsext_sigalg_dsa(md) \
946 tlsext_sigalg_ecdsa(md)
948 static const unsigned char tls12_sigalgs[] = {
949 tlsext_sigalg(TLSEXT_hash_sha512)
950 tlsext_sigalg(TLSEXT_hash_sha384)
951 tlsext_sigalg(TLSEXT_hash_sha256)
952 tlsext_sigalg(TLSEXT_hash_sha224)
953 tlsext_sigalg(TLSEXT_hash_sha1)
954 #ifndef OPENSSL_NO_GOST
955 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
956 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
957 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
961 #ifndef OPENSSL_NO_EC
962 static const unsigned char suiteb_sigalgs[] = {
963 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
964 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
967 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
970 * If Suite B mode use Suite B sigalgs only, ignore any other
973 #ifndef OPENSSL_NO_EC
974 switch (tls1_suiteb(s)) {
975 case SSL_CERT_FLAG_SUITEB_128_LOS:
976 *psigs = suiteb_sigalgs;
977 return sizeof(suiteb_sigalgs);
979 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
980 *psigs = suiteb_sigalgs;
983 case SSL_CERT_FLAG_SUITEB_192_LOS:
984 *psigs = suiteb_sigalgs + 2;
988 /* If server use client authentication sigalgs if not NULL */
989 if (s->server && s->cert->client_sigalgs) {
990 *psigs = s->cert->client_sigalgs;
991 return s->cert->client_sigalgslen;
992 } else if (s->cert->conf_sigalgs) {
993 *psigs = s->cert->conf_sigalgs;
994 return s->cert->conf_sigalgslen;
996 *psigs = tls12_sigalgs;
997 return sizeof(tls12_sigalgs);
1002 * Check signature algorithm is consistent with sent supported signature
1003 * algorithms and if so return relevant digest.
1005 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
1006 const unsigned char *sig, EVP_PKEY *pkey)
1008 const unsigned char *sent_sigs;
1009 size_t sent_sigslen, i;
1010 int sigalg = tls12_get_sigid(pkey);
1011 /* Should never happen */
1014 /* Check key type is consistent with signature */
1015 if (sigalg != (int)sig[1]) {
1016 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1019 #ifndef OPENSSL_NO_EC
1020 if (pkey->type == EVP_PKEY_EC) {
1021 unsigned char curve_id[2], comp_id;
1022 /* Check compression and curve matches extensions */
1023 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
1025 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
1026 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1029 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
1030 if (tls1_suiteb(s)) {
1033 if (curve_id[1] == TLSEXT_curve_P_256) {
1034 if (sig[0] != TLSEXT_hash_sha256) {
1035 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1036 SSL_R_ILLEGAL_SUITEB_DIGEST);
1039 } else if (curve_id[1] == TLSEXT_curve_P_384) {
1040 if (sig[0] != TLSEXT_hash_sha384) {
1041 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1042 SSL_R_ILLEGAL_SUITEB_DIGEST);
1048 } else if (tls1_suiteb(s))
1052 /* Check signature matches a type we sent */
1053 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1054 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
1055 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1058 /* Allow fallback to SHA1 if not strict mode */
1059 if (i == sent_sigslen
1060 && (sig[0] != TLSEXT_hash_sha1
1061 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1062 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1065 *pmd = tls12_get_hash(sig[0]);
1067 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
1070 /* Make sure security callback allows algorithm */
1071 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1072 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
1074 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1078 * Store the digest used so applications can retrieve it if they wish.
1080 s->s3->tmp.peer_md = *pmd;
1085 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
1086 * supported or doesn't appear in supported signature algorithms. Unlike
1087 * ssl_cipher_get_disabled this applies to a specific session and not global
1090 void ssl_set_client_disabled(SSL *s)
1092 s->s3->tmp.mask_a = 0;
1093 s->s3->tmp.mask_k = 0;
1094 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1095 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1096 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1098 s->s3->tmp.mask_ssl = 0;
1099 /* Disable TLS 1.0 ciphers if using SSL v3 */
1100 if (s->client_version == SSL3_VERSION)
1101 s->s3->tmp.mask_ssl |= SSL_TLSV1;
1102 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1104 * Disable static DH if we don't include any appropriate signature
1107 if (s->s3->tmp.mask_a & SSL_aRSA)
1108 s->s3->tmp.mask_k |= SSL_kDHr | SSL_kECDHr;
1109 if (s->s3->tmp.mask_a & SSL_aDSS)
1110 s->s3->tmp.mask_k |= SSL_kDHd;
1111 if (s->s3->tmp.mask_a & SSL_aECDSA)
1112 s->s3->tmp.mask_k |= SSL_kECDHe;
1113 # ifndef OPENSSL_NO_PSK
1114 /* with PSK there must be client callback set */
1115 if (!s->psk_client_callback) {
1116 s->s3->tmp.mask_a |= SSL_aPSK;
1117 s->s3->tmp.mask_k |= SSL_PSK;
1119 #endif /* OPENSSL_NO_PSK */
1120 #ifndef OPENSSL_NO_SRP
1121 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1122 s->s3->tmp.mask_a |= SSL_aSRP;
1123 s->s3->tmp.mask_k |= SSL_kSRP;
1128 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1130 if (c->algorithm_ssl & s->s3->tmp.mask_ssl
1131 || c->algorithm_mkey & s->s3->tmp.mask_k
1132 || c->algorithm_auth & s->s3->tmp.mask_a)
1134 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1137 static int tls_use_ticket(SSL *s)
1139 if (s->options & SSL_OP_NO_TICKET)
1141 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1144 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1145 unsigned char *limit, int *al)
1148 unsigned char *orig = buf;
1149 unsigned char *ret = buf;
1150 #ifndef OPENSSL_NO_EC
1151 /* See if we support any ECC ciphersuites */
1153 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1155 unsigned long alg_k, alg_a;
1156 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1158 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1159 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1161 alg_k = c->algorithm_mkey;
1162 alg_a = c->algorithm_auth;
1163 if ((alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)
1164 || (alg_a & SSL_aECDSA))) {
1175 return NULL; /* this really never occurs, but ... */
1177 /* Add RI if renegotiating */
1178 if (s->renegotiate) {
1181 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1182 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1186 if ((limit - ret - 4 - el) < 0)
1189 s2n(TLSEXT_TYPE_renegotiate, ret);
1192 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1193 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1199 /* Only add RI for SSLv3 */
1200 if (s->client_version == SSL3_VERSION)
1203 if (s->tlsext_hostname != NULL) {
1204 /* Add TLS extension servername to the Client Hello message */
1205 unsigned long size_str;
1209 * check for enough space.
1210 * 4 for the servername type and entension length
1211 * 2 for servernamelist length
1212 * 1 for the hostname type
1213 * 2 for hostname length
1217 if ((lenmax = limit - ret - 9) < 0
1219 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1222 /* extension type and length */
1223 s2n(TLSEXT_TYPE_server_name, ret);
1224 s2n(size_str + 5, ret);
1226 /* length of servername list */
1227 s2n(size_str + 3, ret);
1229 /* hostname type, length and hostname */
1230 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1232 memcpy(ret, s->tlsext_hostname, size_str);
1235 #ifndef OPENSSL_NO_SRP
1236 /* Add SRP username if there is one */
1237 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1238 * Client Hello message */
1240 int login_len = strlen(s->srp_ctx.login);
1241 if (login_len > 255 || login_len == 0) {
1242 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1247 * check for enough space.
1248 * 4 for the srp type type and entension length
1249 * 1 for the srp user identity
1250 * + srp user identity length
1252 if ((limit - ret - 5 - login_len) < 0)
1255 /* fill in the extension */
1256 s2n(TLSEXT_TYPE_srp, ret);
1257 s2n(login_len + 1, ret);
1258 (*ret++) = (unsigned char)login_len;
1259 memcpy(ret, s->srp_ctx.login, login_len);
1264 #ifndef OPENSSL_NO_EC
1267 * Add TLS extension ECPointFormats to the ClientHello message
1270 const unsigned char *pcurves, *pformats;
1271 size_t num_curves, num_formats, curves_list_len;
1273 unsigned char *etmp;
1275 tls1_get_formatlist(s, &pformats, &num_formats);
1277 if ((lenmax = limit - ret - 5) < 0)
1279 if (num_formats > (size_t)lenmax)
1281 if (num_formats > 255) {
1282 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1286 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1287 /* The point format list has 1-byte length. */
1288 s2n(num_formats + 1, ret);
1289 *(ret++) = (unsigned char)num_formats;
1290 memcpy(ret, pformats, num_formats);
1294 * Add TLS extension EllipticCurves to the ClientHello message
1296 pcurves = s->tlsext_ellipticcurvelist;
1297 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1300 if ((lenmax = limit - ret - 6) < 0)
1302 if (num_curves > (size_t)lenmax / 2)
1304 if (num_curves > 65532 / 2) {
1305 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1309 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1311 /* Copy curve ID if supported */
1312 for (i = 0; i < num_curves; i++, pcurves += 2) {
1313 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1314 *etmp++ = pcurves[0];
1315 *etmp++ = pcurves[1];
1319 curves_list_len = etmp - ret - 4;
1321 s2n(curves_list_len + 2, ret);
1322 s2n(curves_list_len, ret);
1323 ret += curves_list_len;
1325 #endif /* OPENSSL_NO_EC */
1327 if (tls_use_ticket(s)) {
1329 if (!s->new_session && s->session && s->session->tlsext_tick)
1330 ticklen = s->session->tlsext_ticklen;
1331 else if (s->session && s->tlsext_session_ticket &&
1332 s->tlsext_session_ticket->data) {
1333 ticklen = s->tlsext_session_ticket->length;
1334 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1335 if (s->session->tlsext_tick == NULL)
1337 memcpy(s->session->tlsext_tick,
1338 s->tlsext_session_ticket->data, ticklen);
1339 s->session->tlsext_ticklen = ticklen;
1342 if (ticklen == 0 && s->tlsext_session_ticket &&
1343 s->tlsext_session_ticket->data == NULL)
1346 * Check for enough room 2 for extension type, 2 for len rest for
1349 if ((long)(limit - ret - 4 - ticklen) < 0)
1351 s2n(TLSEXT_TYPE_session_ticket, ret);
1354 memcpy(ret, s->session->tlsext_tick, ticklen);
1360 if (SSL_USE_SIGALGS(s)) {
1362 const unsigned char *salg;
1363 unsigned char *etmp;
1364 salglen = tls12_get_psigalgs(s, &salg);
1365 if ((size_t)(limit - ret) < salglen + 6)
1367 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1369 /* Skip over lengths for now */
1371 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1372 /* Fill in lengths */
1373 s2n(salglen + 2, etmp);
1378 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1380 long extlen, idlen, itmp;
1384 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1385 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1386 itmp = i2d_OCSP_RESPID(id, NULL);
1392 if (s->tlsext_ocsp_exts) {
1393 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1399 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1401 s2n(TLSEXT_TYPE_status_request, ret);
1402 if (extlen + idlen > 0xFFF0)
1404 s2n(extlen + idlen + 5, ret);
1405 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1407 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1408 /* save position of id len */
1409 unsigned char *q = ret;
1410 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1411 /* skip over id len */
1413 itmp = i2d_OCSP_RESPID(id, &ret);
1419 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1421 #ifndef OPENSSL_NO_HEARTBEATS
1422 /* Add Heartbeat extension */
1423 if ((limit - ret - 4 - 1) < 0)
1425 s2n(TLSEXT_TYPE_heartbeat, ret);
1429 * 1: peer may send requests
1430 * 2: peer not allowed to send requests
1432 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1433 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1435 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1438 #ifndef OPENSSL_NO_NEXTPROTONEG
1439 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1441 * The client advertises an emtpy extension to indicate its support
1442 * for Next Protocol Negotiation
1444 if (limit - ret - 4 < 0)
1446 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1451 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1452 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1454 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1455 s2n(2 + s->alpn_client_proto_list_len, ret);
1456 s2n(s->alpn_client_proto_list_len, ret);
1457 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1458 ret += s->alpn_client_proto_list_len;
1460 #ifndef OPENSSL_NO_SRTP
1461 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1464 /* Returns 0 on success!! */
1465 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1466 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1470 if ((limit - ret - 4 - el) < 0)
1473 s2n(TLSEXT_TYPE_use_srtp, ret);
1476 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1477 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1483 custom_ext_init(&s->cert->cli_ext);
1484 /* Add custom TLS Extensions to ClientHello */
1485 if (!custom_ext_add(s, 0, &ret, limit, al))
1487 #ifdef TLSEXT_TYPE_encrypt_then_mac
1488 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1491 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1495 * Add padding to workaround bugs in F5 terminators. See
1496 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1497 * code works out the length of all existing extensions it MUST always
1500 if (s->options & SSL_OP_TLSEXT_PADDING) {
1501 int hlen = ret - (unsigned char *)s->init_buf->data;
1503 if (hlen > 0xff && hlen < 0x200) {
1504 hlen = 0x200 - hlen;
1510 s2n(TLSEXT_TYPE_padding, ret);
1512 memset(ret, 0, hlen);
1519 if ((extdatalen = ret - orig - 2) == 0)
1522 s2n(extdatalen, orig);
1526 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1527 unsigned char *limit, int *al)
1530 unsigned char *orig = buf;
1531 unsigned char *ret = buf;
1532 #ifndef OPENSSL_NO_NEXTPROTONEG
1533 int next_proto_neg_seen;
1535 #ifndef OPENSSL_NO_EC
1536 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1537 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1538 int using_ecc = (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe))
1539 || (alg_a & SSL_aECDSA);
1540 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1545 return NULL; /* this really never occurs, but ... */
1547 if (s->s3->send_connection_binding) {
1550 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1551 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1555 if ((limit - ret - 4 - el) < 0)
1558 s2n(TLSEXT_TYPE_renegotiate, ret);
1561 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1562 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1569 /* Only add RI for SSLv3 */
1570 if (s->version == SSL3_VERSION)
1573 if (!s->hit && s->servername_done == 1
1574 && s->session->tlsext_hostname != NULL) {
1575 if ((long)(limit - ret - 4) < 0)
1578 s2n(TLSEXT_TYPE_server_name, ret);
1581 #ifndef OPENSSL_NO_EC
1583 const unsigned char *plist;
1586 * Add TLS extension ECPointFormats to the ServerHello message
1590 tls1_get_formatlist(s, &plist, &plistlen);
1592 if ((lenmax = limit - ret - 5) < 0)
1594 if (plistlen > (size_t)lenmax)
1596 if (plistlen > 255) {
1597 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1601 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1602 s2n(plistlen + 1, ret);
1603 *(ret++) = (unsigned char)plistlen;
1604 memcpy(ret, plist, plistlen);
1609 * Currently the server should not respond with a SupportedCurves
1612 #endif /* OPENSSL_NO_EC */
1614 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1615 if ((long)(limit - ret - 4) < 0)
1617 s2n(TLSEXT_TYPE_session_ticket, ret);
1621 if (s->tlsext_status_expected) {
1622 if ((long)(limit - ret - 4) < 0)
1624 s2n(TLSEXT_TYPE_status_request, ret);
1628 #ifndef OPENSSL_NO_SRTP
1629 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1632 /* Returns 0 on success!! */
1633 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1634 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1637 if ((limit - ret - 4 - el) < 0)
1640 s2n(TLSEXT_TYPE_use_srtp, ret);
1643 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1644 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1651 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1652 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1653 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1654 const unsigned char cryptopro_ext[36] = {
1655 0xfd, 0xe8, /* 65000 */
1656 0x00, 0x20, /* 32 bytes length */
1657 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1658 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1659 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1660 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1662 if (limit - ret < 36)
1664 memcpy(ret, cryptopro_ext, 36);
1668 #ifndef OPENSSL_NO_HEARTBEATS
1669 /* Add Heartbeat extension if we've received one */
1670 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) {
1671 if ((limit - ret - 4 - 1) < 0)
1673 s2n(TLSEXT_TYPE_heartbeat, ret);
1677 * 1: peer may send requests
1678 * 2: peer not allowed to send requests
1680 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1681 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1683 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1688 #ifndef OPENSSL_NO_NEXTPROTONEG
1689 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1690 s->s3->next_proto_neg_seen = 0;
1691 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1692 const unsigned char *npa;
1693 unsigned int npalen;
1696 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1698 ctx->next_protos_advertised_cb_arg);
1699 if (r == SSL_TLSEXT_ERR_OK) {
1700 if ((long)(limit - ret - 4 - npalen) < 0)
1702 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1704 memcpy(ret, npa, npalen);
1706 s->s3->next_proto_neg_seen = 1;
1710 if (!custom_ext_add(s, 1, &ret, limit, al))
1712 #ifdef TLSEXT_TYPE_encrypt_then_mac
1713 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1715 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1716 * for other cases too.
1718 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1719 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1720 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1721 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1722 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1724 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1729 if (!s->hit && s->session->flags & SSL_SESS_FLAG_EXTMS) {
1730 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1734 if (s->s3->alpn_selected) {
1735 const unsigned char *selected = s->s3->alpn_selected;
1736 unsigned len = s->s3->alpn_selected_len;
1738 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1740 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1744 memcpy(ret, selected, len);
1750 if ((extdatalen = ret - orig - 2) == 0)
1753 s2n(extdatalen, orig);
1758 * tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1759 * ClientHello. data: the contents of the extension, not including the type
1760 * and length. data_len: the number of bytes in |data| al: a pointer to the
1761 * alert value to send in the event of a non-zero return. returns: 0 on
1764 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1766 unsigned int data_len;
1767 unsigned int proto_len;
1768 const unsigned char *selected;
1769 unsigned char *data;
1770 unsigned char selected_len;
1773 if (s->ctx->alpn_select_cb == NULL)
1777 * data should contain a uint16 length followed by a series of 8-bit,
1778 * length-prefixed strings.
1780 if (!PACKET_get_net_2(pkt, &data_len)
1781 || PACKET_remaining(pkt) != data_len
1782 || !PACKET_peek_bytes(pkt, &data, data_len))
1786 if (!PACKET_get_1(pkt, &proto_len)
1788 || !PACKET_forward(pkt, proto_len))
1790 } while (PACKET_remaining(pkt));
1792 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1793 s->ctx->alpn_select_cb_arg);
1794 if (r == SSL_TLSEXT_ERR_OK) {
1795 OPENSSL_free(s->s3->alpn_selected);
1796 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1797 if (s->s3->alpn_selected == NULL) {
1798 *al = SSL_AD_INTERNAL_ERROR;
1801 memcpy(s->s3->alpn_selected, selected, selected_len);
1802 s->s3->alpn_selected_len = selected_len;
1807 *al = SSL_AD_DECODE_ERROR;
1811 #ifndef OPENSSL_NO_EC
1813 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1814 * SecureTransport using the TLS extension block in |d|, of length |n|.
1815 * Safari, since 10.6, sends exactly these extensions, in this order:
1820 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1821 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1822 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1823 * 10.8..10.8.3 (which don't work).
1825 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1827 unsigned int type, size;
1828 unsigned char *eblock1, *eblock2;
1831 static const unsigned char kSafariExtensionsBlock[] = {
1832 0x00, 0x0a, /* elliptic_curves extension */
1833 0x00, 0x08, /* 8 bytes */
1834 0x00, 0x06, /* 6 bytes of curve ids */
1835 0x00, 0x17, /* P-256 */
1836 0x00, 0x18, /* P-384 */
1837 0x00, 0x19, /* P-521 */
1839 0x00, 0x0b, /* ec_point_formats */
1840 0x00, 0x02, /* 2 bytes */
1841 0x01, /* 1 point format */
1842 0x00, /* uncompressed */
1845 /* The following is only present in TLS 1.2 */
1846 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1847 0x00, 0x0d, /* signature_algorithms */
1848 0x00, 0x0c, /* 12 bytes */
1849 0x00, 0x0a, /* 10 bytes */
1850 0x05, 0x01, /* SHA-384/RSA */
1851 0x04, 0x01, /* SHA-256/RSA */
1852 0x02, 0x01, /* SHA-1/RSA */
1853 0x04, 0x03, /* SHA-256/ECDSA */
1854 0x02, 0x03, /* SHA-1/ECDSA */
1859 if (!PACKET_forward(&tmppkt, 2)
1860 || !PACKET_get_net_2(&tmppkt, &type)
1861 || !PACKET_get_net_2(&tmppkt, &size)
1862 || !PACKET_forward(&tmppkt, size))
1865 if (type != TLSEXT_TYPE_server_name)
1868 if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
1869 const size_t len1 = sizeof(kSafariExtensionsBlock);
1870 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1872 if (!PACKET_get_bytes(&tmppkt, &eblock1, len1)
1873 || !PACKET_get_bytes(&tmppkt, &eblock2, len2)
1874 || PACKET_remaining(&tmppkt))
1876 if (memcmp(eblock1, kSafariExtensionsBlock, len1) != 0)
1878 if (memcmp(eblock2, kSafariTLS12ExtensionsBlock, len2) != 0)
1881 const size_t len = sizeof(kSafariExtensionsBlock);
1883 if (!PACKET_get_bytes(&tmppkt, &eblock1, len)
1884 || PACKET_remaining(&tmppkt))
1886 if (memcmp(eblock1, kSafariExtensionsBlock, len) != 0)
1890 s->s3->is_probably_safari = 1;
1892 #endif /* !OPENSSL_NO_EC */
1894 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1899 unsigned char *data;
1900 int renegotiate_seen = 0;
1902 s->servername_done = 0;
1903 s->tlsext_status_type = -1;
1904 #ifndef OPENSSL_NO_NEXTPROTONEG
1905 s->s3->next_proto_neg_seen = 0;
1908 OPENSSL_free(s->s3->alpn_selected);
1909 s->s3->alpn_selected = NULL;
1910 #ifndef OPENSSL_NO_HEARTBEATS
1911 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1912 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1915 #ifndef OPENSSL_NO_EC
1916 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1917 ssl_check_for_safari(s, pkt);
1918 # endif /* !OPENSSL_NO_EC */
1920 /* Clear any signature algorithms extension received */
1921 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1922 s->s3->tmp.peer_sigalgs = NULL;
1923 #ifdef TLSEXT_TYPE_encrypt_then_mac
1924 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1927 #ifndef OPENSSL_NO_SRP
1928 OPENSSL_free(s->srp_ctx.login);
1929 s->srp_ctx.login = NULL;
1932 s->srtp_profile = NULL;
1934 if (PACKET_remaining(pkt) == 0)
1937 if (!PACKET_get_net_2(pkt, &len))
1940 if (PACKET_remaining(pkt) != len)
1943 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
1946 if (!PACKET_peek_bytes(pkt, &data, size))
1949 if (s->tlsext_debug_cb)
1950 s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
1952 if (!PACKET_get_sub_packet(pkt, &subpkt, size))
1955 if (type == TLSEXT_TYPE_renegotiate) {
1956 if (!ssl_parse_clienthello_renegotiate_ext(s, &subpkt, al))
1958 renegotiate_seen = 1;
1959 } else if (s->version == SSL3_VERSION) {
1962 * The servername extension is treated as follows:
1964 * - Only the hostname type is supported with a maximum length of 255.
1965 * - The servername is rejected if too long or if it contains zeros,
1966 * in which case an fatal alert is generated.
1967 * - The servername field is maintained together with the session cache.
1968 * - When a session is resumed, the servername call back invoked in order
1969 * to allow the application to position itself to the right context.
1970 * - The servername is acknowledged if it is new for a session or when
1971 * it is identical to a previously used for the same session.
1972 * Applications can control the behaviour. They can at any time
1973 * set a 'desirable' servername for a new SSL object. This can be the
1974 * case for example with HTTPS when a Host: header field is received and
1975 * a renegotiation is requested. In this case, a possible servername
1976 * presented in the new client hello is only acknowledged if it matches
1977 * the value of the Host: field.
1978 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1979 * if they provide for changing an explicit servername context for the
1980 * session, i.e. when the session has been established with a servername
1982 * - On session reconnect, the servername extension may be absent.
1986 else if (type == TLSEXT_TYPE_server_name) {
1987 unsigned char *sdata;
1988 unsigned int servname_type;
1992 if (!PACKET_get_net_2(&subpkt, &dsize)
1993 || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))
1996 while (PACKET_remaining(&ssubpkt) > 3) {
1997 if (!PACKET_get_1(&ssubpkt, &servname_type)
1998 || !PACKET_get_net_2(&ssubpkt, &len)
1999 || PACKET_remaining(&ssubpkt) < len)
2002 if (s->servername_done == 0)
2003 switch (servname_type) {
2004 case TLSEXT_NAMETYPE_host_name:
2006 if (s->session->tlsext_hostname)
2009 if (len > TLSEXT_MAXLEN_host_name) {
2010 *al = TLS1_AD_UNRECOGNIZED_NAME;
2013 if ((s->session->tlsext_hostname =
2014 OPENSSL_malloc(len + 1)) == NULL) {
2015 *al = TLS1_AD_INTERNAL_ERROR;
2018 if (!PACKET_copy_bytes(&ssubpkt,
2019 (unsigned char *)s->session
2022 *al = SSL_AD_DECODE_ERROR;
2025 s->session->tlsext_hostname[len] = '\0';
2026 if (strlen(s->session->tlsext_hostname) != len) {
2027 OPENSSL_free(s->session->tlsext_hostname);
2028 s->session->tlsext_hostname = NULL;
2029 *al = TLS1_AD_UNRECOGNIZED_NAME;
2032 s->servername_done = 1;
2035 if (!PACKET_get_bytes(&ssubpkt, &sdata, len)) {
2036 *al = SSL_AD_DECODE_ERROR;
2039 s->servername_done = s->session->tlsext_hostname
2040 && strlen(s->session->tlsext_hostname) == len
2041 && strncmp(s->session->tlsext_hostname,
2042 (char *)sdata, len) == 0;
2051 /* We shouldn't have any bytes left */
2052 if (PACKET_remaining(&ssubpkt) != 0)
2056 #ifndef OPENSSL_NO_SRP
2057 else if (type == TLSEXT_TYPE_srp) {
2058 if (!PACKET_get_1(&subpkt, &len)
2059 || s->srp_ctx.login != NULL)
2062 if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
2064 if (!PACKET_copy_bytes(&subpkt, (unsigned char *)s->srp_ctx.login,
2067 s->srp_ctx.login[len] = '\0';
2069 if (strlen(s->srp_ctx.login) != len
2070 || PACKET_remaining(&subpkt))
2075 #ifndef OPENSSL_NO_EC
2076 else if (type == TLSEXT_TYPE_ec_point_formats) {
2077 unsigned int ecpointformatlist_length;
2079 if (!PACKET_get_1(&subpkt, &ecpointformatlist_length)
2080 || ecpointformatlist_length == 0)
2084 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2085 s->session->tlsext_ecpointformatlist = NULL;
2086 s->session->tlsext_ecpointformatlist_length = 0;
2087 if ((s->session->tlsext_ecpointformatlist =
2088 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2089 *al = TLS1_AD_INTERNAL_ERROR;
2092 s->session->tlsext_ecpointformatlist_length =
2093 ecpointformatlist_length;
2094 if (!PACKET_copy_bytes(&subpkt,
2095 s->session->tlsext_ecpointformatlist,
2096 ecpointformatlist_length))
2098 } else if (!PACKET_forward(&subpkt, ecpointformatlist_length)) {
2101 /* We should have consumed all the bytes by now */
2102 if (PACKET_remaining(&subpkt)) {
2103 *al = TLS1_AD_DECODE_ERROR;
2106 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2107 unsigned int ellipticcurvelist_length;
2109 /* Each NamedCurve is 2 bytes and we must have at least 1 */
2110 if (!PACKET_get_net_2(&subpkt, &ellipticcurvelist_length)
2111 || ellipticcurvelist_length == 0
2112 || (ellipticcurvelist_length & 1) != 0)
2116 if (s->session->tlsext_ellipticcurvelist)
2119 s->session->tlsext_ellipticcurvelist_length = 0;
2120 if ((s->session->tlsext_ellipticcurvelist =
2121 OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
2122 *al = TLS1_AD_INTERNAL_ERROR;
2125 s->session->tlsext_ellipticcurvelist_length =
2126 ellipticcurvelist_length;
2127 if (!PACKET_copy_bytes(&subpkt,
2128 s->session->tlsext_ellipticcurvelist,
2129 ellipticcurvelist_length))
2131 } else if (!PACKET_forward(&subpkt, ellipticcurvelist_length)) {
2134 /* We should have consumed all the bytes by now */
2135 if (PACKET_remaining(&subpkt)) {
2139 #endif /* OPENSSL_NO_EC */
2140 else if (type == TLSEXT_TYPE_session_ticket) {
2141 if (!PACKET_forward(&subpkt, size)
2142 || (s->tls_session_ticket_ext_cb &&
2143 !s->tls_session_ticket_ext_cb(s, data, size,
2144 s->tls_session_ticket_ext_cb_arg))) {
2145 *al = TLS1_AD_INTERNAL_ERROR;
2148 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2151 if (s->s3->tmp.peer_sigalgs
2152 || !PACKET_get_net_2(&subpkt, &dsize)
2155 || !PACKET_get_bytes(&subpkt, &data, dsize)
2156 || PACKET_remaining(&subpkt) != 0
2157 || !tls1_save_sigalgs(s, data, dsize)) {
2160 } else if (type == TLSEXT_TYPE_status_request) {
2163 if (!PACKET_get_1(&subpkt,
2164 (unsigned int *)&s->tlsext_status_type))
2167 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2168 const unsigned char *sdata;
2170 /* Read in responder_id_list */
2171 if (!PACKET_get_net_2(&subpkt, &dsize)
2172 || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))
2175 while (PACKET_remaining(&ssubpkt)) {
2177 unsigned int idsize;
2179 if (PACKET_remaining(&ssubpkt) < 4
2180 || !PACKET_get_net_2(&ssubpkt, &idsize)
2181 || !PACKET_get_bytes(&ssubpkt, &data, idsize)) {
2186 id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
2189 if (data != sdata) {
2190 OCSP_RESPID_free(id);
2193 if (!s->tlsext_ocsp_ids
2194 && !(s->tlsext_ocsp_ids =
2195 sk_OCSP_RESPID_new_null())) {
2196 OCSP_RESPID_free(id);
2197 *al = SSL_AD_INTERNAL_ERROR;
2200 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2201 OCSP_RESPID_free(id);
2202 *al = SSL_AD_INTERNAL_ERROR;
2207 /* Read in request_extensions */
2208 if (!PACKET_get_net_2(&subpkt, &dsize)
2209 || !PACKET_get_bytes(&subpkt, &data, dsize)
2210 || PACKET_remaining(&subpkt)) {
2215 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2216 X509_EXTENSION_free);
2217 s->tlsext_ocsp_exts =
2218 d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
2219 if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
2224 * We don't know what to do with any other type * so ignore it.
2227 s->tlsext_status_type = -1;
2229 #ifndef OPENSSL_NO_HEARTBEATS
2230 else if (type == TLSEXT_TYPE_heartbeat) {
2231 unsigned int hbtype;
2233 if (!PACKET_get_1(&subpkt, &hbtype)
2234 || PACKET_remaining(&subpkt)) {
2235 *al = SSL_AD_DECODE_ERROR;
2239 case 0x01: /* Client allows us to send HB requests */
2240 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2242 case 0x02: /* Client doesn't accept HB requests */
2243 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2244 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2247 *al = SSL_AD_ILLEGAL_PARAMETER;
2252 #ifndef OPENSSL_NO_NEXTPROTONEG
2253 else if (type == TLSEXT_TYPE_next_proto_neg &&
2254 s->s3->tmp.finish_md_len == 0 &&
2255 s->s3->alpn_selected == NULL) {
2257 * We shouldn't accept this extension on a
2260 * s->new_session will be set on renegotiation, but we
2261 * probably shouldn't rely that it couldn't be set on
2262 * the initial renegotation too in certain cases (when
2263 * there's some other reason to disallow resuming an
2264 * earlier session -- the current code won't be doing
2265 * anything like that, but this might change).
2267 * A valid sign that there's been a previous handshake
2268 * in this connection is if s->s3->tmp.finish_md_len >
2269 * 0. (We are talking about a check that will happen
2270 * in the Hello protocol round, well before a new
2271 * Finished message could have been computed.)
2273 s->s3->next_proto_neg_seen = 1;
2277 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2278 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
2279 if (tls1_alpn_handle_client_hello(s, &subpkt, al) != 0)
2281 #ifndef OPENSSL_NO_NEXTPROTONEG
2282 /* ALPN takes precedence over NPN. */
2283 s->s3->next_proto_neg_seen = 0;
2287 /* session ticket processed earlier */
2288 #ifndef OPENSSL_NO_SRTP
2289 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2290 && type == TLSEXT_TYPE_use_srtp) {
2291 if (ssl_parse_clienthello_use_srtp_ext(s, &subpkt, al))
2295 #ifdef TLSEXT_TYPE_encrypt_then_mac
2296 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2297 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2299 else if (type == TLSEXT_TYPE_extended_master_secret) {
2301 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2304 * If this ClientHello extension was unhandled and this is a
2305 * nonresumed connection, check whether the extension is a custom
2306 * TLS Extension (has a custom_srv_ext_record), and if so call the
2307 * callback and record the extension number so that an appropriate
2308 * ServerHello may be later returned.
2311 if (custom_ext_parse(s, 1, type, data, size, al) <= 0)
2316 /* Spurious data on the end */
2317 if (PACKET_remaining(pkt) != 0)
2322 /* Need RI if renegotiating */
2324 if (!renegotiate_seen && s->renegotiate &&
2325 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2326 *al = SSL_AD_HANDSHAKE_FAILURE;
2327 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2328 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2334 *al = SSL_AD_DECODE_ERROR;
2338 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2341 custom_ext_init(&s->cert->srv_ext);
2342 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2343 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2347 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2348 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2354 #ifndef OPENSSL_NO_NEXTPROTONEG
2356 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2357 * elements of zero length are allowed and the set of elements must exactly
2358 * fill the length of the block.
2360 static char ssl_next_proto_validate(PACKET *pkt)
2364 while (PACKET_remaining(pkt)) {
2365 if (!PACKET_get_1(pkt, &len)
2366 || !PACKET_forward(pkt, len))
2374 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2376 unsigned int length, type, size;
2377 int tlsext_servername = 0;
2378 int renegotiate_seen = 0;
2380 #ifndef OPENSSL_NO_NEXTPROTONEG
2381 s->s3->next_proto_neg_seen = 0;
2383 s->tlsext_ticket_expected = 0;
2385 OPENSSL_free(s->s3->alpn_selected);
2386 s->s3->alpn_selected = NULL;
2387 #ifndef OPENSSL_NO_HEARTBEATS
2388 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2389 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2392 #ifdef TLSEXT_TYPE_encrypt_then_mac
2393 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2396 if (!PACKET_get_net_2(pkt, &length))
2399 if (PACKET_remaining(pkt) != length) {
2400 *al = SSL_AD_DECODE_ERROR;
2404 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2405 unsigned char *data;
2408 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2409 || !PACKET_peek_bytes(&spkt, &data, size))
2412 if (s->tlsext_debug_cb)
2413 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2415 if (type == TLSEXT_TYPE_renegotiate) {
2416 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2418 renegotiate_seen = 1;
2419 } else if (s->version == SSL3_VERSION) {
2420 } else if (type == TLSEXT_TYPE_server_name) {
2421 if (s->tlsext_hostname == NULL || size > 0) {
2422 *al = TLS1_AD_UNRECOGNIZED_NAME;
2425 tlsext_servername = 1;
2427 #ifndef OPENSSL_NO_EC
2428 else if (type == TLSEXT_TYPE_ec_point_formats) {
2429 unsigned int ecpointformatlist_length;
2430 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2431 || ecpointformatlist_length != size - 1) {
2432 *al = TLS1_AD_DECODE_ERROR;
2436 s->session->tlsext_ecpointformatlist_length = 0;
2437 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2438 if ((s->session->tlsext_ecpointformatlist =
2439 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2440 *al = TLS1_AD_INTERNAL_ERROR;
2443 s->session->tlsext_ecpointformatlist_length =
2444 ecpointformatlist_length;
2445 if (!PACKET_copy_bytes(&spkt,
2446 s->session->tlsext_ecpointformatlist,
2447 ecpointformatlist_length)) {
2448 *al = TLS1_AD_DECODE_ERROR;
2454 #endif /* OPENSSL_NO_EC */
2456 else if (type == TLSEXT_TYPE_session_ticket) {
2457 if (s->tls_session_ticket_ext_cb &&
2458 !s->tls_session_ticket_ext_cb(s, data, size,
2459 s->tls_session_ticket_ext_cb_arg))
2461 *al = TLS1_AD_INTERNAL_ERROR;
2464 if (!tls_use_ticket(s) || (size > 0)) {
2465 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2468 s->tlsext_ticket_expected = 1;
2470 else if (type == TLSEXT_TYPE_status_request) {
2472 * MUST be empty and only sent if we've requested a status
2475 if ((s->tlsext_status_type == -1) || (size > 0)) {
2476 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2479 /* Set flag to expect CertificateStatus message */
2480 s->tlsext_status_expected = 1;
2482 #ifndef OPENSSL_NO_NEXTPROTONEG
2483 else if (type == TLSEXT_TYPE_next_proto_neg &&
2484 s->s3->tmp.finish_md_len == 0) {
2485 unsigned char *selected;
2486 unsigned char selected_len;
2487 /* We must have requested it. */
2488 if (s->ctx->next_proto_select_cb == NULL) {
2489 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2492 /* The data must be valid */
2493 if (!ssl_next_proto_validate(&spkt)) {
2494 *al = TLS1_AD_DECODE_ERROR;
2498 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2500 s->ctx->next_proto_select_cb_arg) !=
2501 SSL_TLSEXT_ERR_OK) {
2502 *al = TLS1_AD_INTERNAL_ERROR;
2505 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2506 if (s->next_proto_negotiated == NULL) {
2507 *al = TLS1_AD_INTERNAL_ERROR;
2510 memcpy(s->next_proto_negotiated, selected, selected_len);
2511 s->next_proto_negotiated_len = selected_len;
2512 s->s3->next_proto_neg_seen = 1;
2516 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2518 /* We must have requested it. */
2519 if (s->alpn_client_proto_list == NULL) {
2520 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2524 * The extension data consists of:
2525 * uint16 list_length
2526 * uint8 proto_length;
2527 * uint8 proto[proto_length];
2529 if (!PACKET_get_net_2(&spkt, &len)
2530 || PACKET_remaining(&spkt) != len
2531 || !PACKET_get_1(&spkt, &len)
2532 || PACKET_remaining(&spkt) != len) {
2533 *al = TLS1_AD_DECODE_ERROR;
2536 OPENSSL_free(s->s3->alpn_selected);
2537 s->s3->alpn_selected = OPENSSL_malloc(len);
2538 if (s->s3->alpn_selected == NULL) {
2539 *al = TLS1_AD_INTERNAL_ERROR;
2542 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2543 *al = TLS1_AD_DECODE_ERROR;
2546 s->s3->alpn_selected_len = len;
2548 #ifndef OPENSSL_NO_HEARTBEATS
2549 else if (type == TLSEXT_TYPE_heartbeat) {
2550 unsigned int hbtype;
2551 if (!PACKET_get_1(&spkt, &hbtype)) {
2552 *al = SSL_AD_DECODE_ERROR;
2556 case 0x01: /* Server allows us to send HB requests */
2557 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2559 case 0x02: /* Server doesn't accept HB requests */
2560 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2561 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2564 *al = SSL_AD_ILLEGAL_PARAMETER;
2569 #ifndef OPENSSL_NO_SRTP
2570 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2571 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2575 #ifdef TLSEXT_TYPE_encrypt_then_mac
2576 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2577 /* Ignore if inappropriate ciphersuite */
2578 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2579 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2580 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2583 else if (type == TLSEXT_TYPE_extended_master_secret) {
2585 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2588 * If this extension type was not otherwise handled, but matches a
2589 * custom_cli_ext_record, then send it to the c callback
2591 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2595 if (PACKET_remaining(pkt) != 0) {
2596 *al = SSL_AD_DECODE_ERROR;
2600 if (!s->hit && tlsext_servername == 1) {
2601 if (s->tlsext_hostname) {
2602 if (s->session->tlsext_hostname == NULL) {
2603 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2604 if (!s->session->tlsext_hostname) {
2605 *al = SSL_AD_UNRECOGNIZED_NAME;
2609 *al = SSL_AD_DECODE_ERROR;
2618 * Determine if we need to see RI. Strictly speaking if we want to avoid
2619 * an attack we should *always* see RI even on initial server hello
2620 * because the client doesn't see any renegotiation during an attack.
2621 * However this would mean we could not connect to any server which
2622 * doesn't support RI so for the immediate future tolerate RI absence on
2623 * initial connect only.
2625 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2626 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2627 *al = SSL_AD_HANDSHAKE_FAILURE;
2628 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2629 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2636 int ssl_prepare_clienthello_tlsext(SSL *s)
2642 int ssl_prepare_serverhello_tlsext(SSL *s)
2647 static int ssl_check_clienthello_tlsext_early(SSL *s)
2649 int ret = SSL_TLSEXT_ERR_NOACK;
2650 int al = SSL_AD_UNRECOGNIZED_NAME;
2652 #ifndef OPENSSL_NO_EC
2654 * The handling of the ECPointFormats extension is done elsewhere, namely
2655 * in ssl3_choose_cipher in s3_lib.c.
2658 * The handling of the EllipticCurves extension is done elsewhere, namely
2659 * in ssl3_choose_cipher in s3_lib.c.
2663 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2665 s->ctx->tlsext_servername_callback(s, &al,
2666 s->ctx->tlsext_servername_arg);
2667 else if (s->initial_ctx != NULL
2668 && s->initial_ctx->tlsext_servername_callback != 0)
2670 s->initial_ctx->tlsext_servername_callback(s, &al,
2672 initial_ctx->tlsext_servername_arg);
2675 case SSL_TLSEXT_ERR_ALERT_FATAL:
2676 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2679 case SSL_TLSEXT_ERR_ALERT_WARNING:
2680 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2683 case SSL_TLSEXT_ERR_NOACK:
2684 s->servername_done = 0;
2689 /* Initialise digests to default values */
2690 void ssl_set_default_md(SSL *s)
2692 const EVP_MD **pmd = s->s3->tmp.md;
2693 #ifndef OPENSSL_NO_DSA
2694 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2696 #ifndef OPENSSL_NO_RSA
2697 if (SSL_USE_SIGALGS(s))
2698 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2700 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2701 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2703 #ifndef OPENSSL_NO_EC
2704 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2706 #ifndef OPENSSL_NO_GOST
2707 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2708 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2709 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2713 int tls1_set_server_sigalgs(SSL *s)
2717 /* Clear any shared sigtnature algorithms */
2718 OPENSSL_free(s->cert->shared_sigalgs);
2719 s->cert->shared_sigalgs = NULL;
2720 s->cert->shared_sigalgslen = 0;
2721 /* Clear certificate digests and validity flags */
2722 for (i = 0; i < SSL_PKEY_NUM; i++) {
2723 s->s3->tmp.md[i] = NULL;
2724 s->s3->tmp.valid_flags[i] = 0;
2727 /* If sigalgs received process it. */
2728 if (s->s3->tmp.peer_sigalgs) {
2729 if (!tls1_process_sigalgs(s)) {
2730 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2731 al = SSL_AD_INTERNAL_ERROR;
2734 /* Fatal error is no shared signature algorithms */
2735 if (!s->cert->shared_sigalgs) {
2736 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2737 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2738 al = SSL_AD_ILLEGAL_PARAMETER;
2742 ssl_set_default_md(s);
2746 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2750 int ssl_check_clienthello_tlsext_late(SSL *s)
2752 int ret = SSL_TLSEXT_ERR_OK;
2753 int al = SSL_AD_INTERNAL_ERROR;
2756 * If status request then ask callback what to do. Note: this must be
2757 * called after servername callbacks in case the certificate has changed,
2758 * and must be called after the cipher has been chosen because this may
2759 * influence which certificate is sent
2761 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2763 CERT_PKEY *certpkey;
2764 certpkey = ssl_get_server_send_pkey(s);
2765 /* If no certificate can't return certificate status */
2766 if (certpkey == NULL) {
2767 s->tlsext_status_expected = 0;
2771 * Set current certificate to one we will use so SSL_get_certificate
2772 * et al can pick it up.
2774 s->cert->key = certpkey;
2775 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2777 /* We don't want to send a status request response */
2778 case SSL_TLSEXT_ERR_NOACK:
2779 s->tlsext_status_expected = 0;
2781 /* status request response should be sent */
2782 case SSL_TLSEXT_ERR_OK:
2783 if (s->tlsext_ocsp_resp)
2784 s->tlsext_status_expected = 1;
2786 s->tlsext_status_expected = 0;
2788 /* something bad happened */
2789 case SSL_TLSEXT_ERR_ALERT_FATAL:
2790 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2791 al = SSL_AD_INTERNAL_ERROR;
2795 s->tlsext_status_expected = 0;
2799 case SSL_TLSEXT_ERR_ALERT_FATAL:
2800 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2803 case SSL_TLSEXT_ERR_ALERT_WARNING:
2804 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2812 int ssl_check_serverhello_tlsext(SSL *s)
2814 int ret = SSL_TLSEXT_ERR_NOACK;
2815 int al = SSL_AD_UNRECOGNIZED_NAME;
2817 #ifndef OPENSSL_NO_EC
2819 * If we are client and using an elliptic curve cryptography cipher
2820 * suite, then if server returns an EC point formats lists extension it
2821 * must contain uncompressed.
2823 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2824 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2825 if ((s->tlsext_ecpointformatlist != NULL)
2826 && (s->tlsext_ecpointformatlist_length > 0)
2827 && (s->session->tlsext_ecpointformatlist != NULL)
2828 && (s->session->tlsext_ecpointformatlist_length > 0)
2829 && ((alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe))
2830 || (alg_a & SSL_aECDSA))) {
2831 /* we are using an ECC cipher */
2833 unsigned char *list;
2834 int found_uncompressed = 0;
2835 list = s->session->tlsext_ecpointformatlist;
2836 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2837 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2838 found_uncompressed = 1;
2842 if (!found_uncompressed) {
2843 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2844 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2848 ret = SSL_TLSEXT_ERR_OK;
2849 #endif /* OPENSSL_NO_EC */
2851 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2853 s->ctx->tlsext_servername_callback(s, &al,
2854 s->ctx->tlsext_servername_arg);
2855 else if (s->initial_ctx != NULL
2856 && s->initial_ctx->tlsext_servername_callback != 0)
2858 s->initial_ctx->tlsext_servername_callback(s, &al,
2860 initial_ctx->tlsext_servername_arg);
2863 * If we've requested certificate status and we wont get one tell the
2866 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2867 && s->ctx && s->ctx->tlsext_status_cb) {
2870 * Set resp to NULL, resplen to -1 so callback knows there is no
2873 OPENSSL_free(s->tlsext_ocsp_resp);
2874 s->tlsext_ocsp_resp = NULL;
2875 s->tlsext_ocsp_resplen = -1;
2876 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2878 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2879 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2882 al = SSL_AD_INTERNAL_ERROR;
2883 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2888 case SSL_TLSEXT_ERR_ALERT_FATAL:
2889 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2892 case SSL_TLSEXT_ERR_ALERT_WARNING:
2893 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2896 case SSL_TLSEXT_ERR_NOACK:
2897 s->servername_done = 0;
2903 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2906 if (s->version < SSL3_VERSION)
2908 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2909 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2913 if (ssl_check_serverhello_tlsext(s) <= 0) {
2914 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2921 * Since the server cache lookup is done early on in the processing of the
2922 * ClientHello, and other operations depend on the result, we need to handle
2923 * any TLS session ticket extension at the same time.
2925 * session_id: ClientHello session ID.
2926 * ext: ClientHello extensions (including length prefix)
2927 * ret: (output) on return, if a ticket was decrypted, then this is set to
2928 * point to the resulting session.
2930 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2931 * ciphersuite, in which case we have no use for session tickets and one will
2932 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2935 * -1: fatal error, either from parsing or decrypting the ticket.
2936 * 0: no ticket was found (or was ignored, based on settings).
2937 * 1: a zero length extension was found, indicating that the client supports
2938 * session tickets but doesn't currently have one to offer.
2939 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2940 * couldn't be decrypted because of a non-fatal error.
2941 * 3: a ticket was successfully decrypted and *ret was set.
2944 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2945 * a new session ticket to the client because the client indicated support
2946 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2947 * a session ticket or we couldn't use the one it gave us, or if
2948 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2949 * Otherwise, s->tlsext_ticket_expected is set to 0.
2951 int tls1_process_ticket(SSL *s, const PACKET *ext, const PACKET *session_id,
2955 PACKET local_ext = *ext;
2959 s->tlsext_ticket_expected = 0;
2962 * If tickets disabled behave as if no ticket present to permit stateful
2965 if (!tls_use_ticket(s))
2967 if ((s->version <= SSL3_VERSION))
2970 if (!PACKET_get_net_2(&local_ext, &i)) {
2974 while (PACKET_remaining(&local_ext) >= 4) {
2975 unsigned int type, size;
2977 if (!PACKET_get_net_2(&local_ext, &type)
2978 || !PACKET_get_net_2(&local_ext, &size)) {
2979 /* Shouldn't ever happen */
2983 if (PACKET_remaining(&local_ext) < size) {
2987 if (type == TLSEXT_TYPE_session_ticket) {
2989 unsigned char *etick;
2993 * The client will accept a ticket but doesn't currently have
2996 s->tlsext_ticket_expected = 1;
3000 if (s->tls_session_secret_cb) {
3002 * Indicate that the ticket couldn't be decrypted rather than
3003 * generating the session from ticket now, trigger
3004 * abbreviated handshake based on external mechanism to
3005 * calculate the master secret later.
3010 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
3011 /* Shouldn't ever happen */
3015 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
3016 PACKET_remaining(session_id), ret);
3018 case 2: /* ticket couldn't be decrypted */
3019 s->tlsext_ticket_expected = 1;
3022 case 3: /* ticket was decrypted */
3025 case 4: /* ticket decrypted but need to renew */
3026 s->tlsext_ticket_expected = 1;
3029 default: /* fatal error */
3035 if (!PACKET_forward(&local_ext, size)) {
3047 * tls_decrypt_ticket attempts to decrypt a session ticket.
3049 * etick: points to the body of the session ticket extension.
3050 * eticklen: the length of the session tickets extenion.
3051 * sess_id: points at the session ID.
3052 * sesslen: the length of the session ID.
3053 * psess: (output) on return, if a ticket was decrypted, then this is set to
3054 * point to the resulting session.
3057 * -1: fatal error, either from parsing or decrypting the ticket.
3058 * 2: the ticket couldn't be decrypted.
3059 * 3: a ticket was successfully decrypted and *psess was set.
3060 * 4: same as 3, but the ticket needs to be renewed.
3062 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3063 int eticklen, const unsigned char *sess_id,
3064 int sesslen, SSL_SESSION **psess)
3067 unsigned char *sdec;
3068 const unsigned char *p;
3069 int slen, mlen, renew_ticket = 0;
3070 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3073 SSL_CTX *tctx = s->initial_ctx;
3074 /* Need at least keyname + iv + some encrypted data */
3077 /* Initialize session ticket encryption and HMAC contexts */
3078 HMAC_CTX_init(&hctx);
3079 EVP_CIPHER_CTX_init(&ctx);
3080 if (tctx->tlsext_ticket_key_cb) {
3081 unsigned char *nctick = (unsigned char *)etick;
3082 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3091 /* Check key name matches */
3092 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3094 if (HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3095 EVP_sha256(), NULL) <= 0
3096 || EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3097 tctx->tlsext_tick_aes_key,
3103 * Attempt to process session ticket, first conduct sanity and integrity
3106 mlen = HMAC_size(&hctx);
3111 /* Check HMAC of encrypted ticket */
3112 if (HMAC_Update(&hctx, etick, eticklen) <= 0
3113 || HMAC_Final(&hctx, tick_hmac, NULL) <= 0) {
3116 HMAC_CTX_cleanup(&hctx);
3117 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3118 EVP_CIPHER_CTX_cleanup(&ctx);
3121 /* Attempt to decrypt session data */
3122 /* Move p after IV to start of encrypted ticket, update length */
3123 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3124 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3125 sdec = OPENSSL_malloc(eticklen);
3127 || EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen) <= 0) {
3128 EVP_CIPHER_CTX_cleanup(&ctx);
3131 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
3132 EVP_CIPHER_CTX_cleanup(&ctx);
3137 EVP_CIPHER_CTX_cleanup(&ctx);
3140 sess = d2i_SSL_SESSION(NULL, &p, slen);
3144 * The session ID, if non-empty, is used by some clients to detect
3145 * that the ticket has been accepted. So we copy it to the session
3146 * structure. If it is empty set length to zero as required by
3150 memcpy(sess->session_id, sess_id, sesslen);
3151 sess->session_id_length = sesslen;
3160 * For session parse failure, indicate that we need to send a new ticket.
3164 EVP_CIPHER_CTX_cleanup(&ctx);
3165 HMAC_CTX_cleanup(&hctx);
3169 /* Tables to translate from NIDs to TLS v1.2 ids */
3176 static const tls12_lookup tls12_md[] = {
3177 {NID_md5, TLSEXT_hash_md5},
3178 {NID_sha1, TLSEXT_hash_sha1},
3179 {NID_sha224, TLSEXT_hash_sha224},
3180 {NID_sha256, TLSEXT_hash_sha256},
3181 {NID_sha384, TLSEXT_hash_sha384},
3182 {NID_sha512, TLSEXT_hash_sha512},
3183 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3184 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3185 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3188 static const tls12_lookup tls12_sig[] = {
3189 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3190 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3191 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3192 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3193 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3194 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3197 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3200 for (i = 0; i < tlen; i++) {
3201 if (table[i].nid == nid)
3207 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3210 for (i = 0; i < tlen; i++) {
3211 if ((table[i].id) == id)
3212 return table[i].nid;
3217 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3223 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3226 sig_id = tls12_get_sigid(pk);
3229 p[0] = (unsigned char)md_id;
3230 p[1] = (unsigned char)sig_id;
3234 int tls12_get_sigid(const EVP_PKEY *pk)
3236 return tls12_find_id(pk->type, tls12_sig, OSSL_NELEM(tls12_sig));
3243 unsigned char tlsext_hash;
3246 static const tls12_hash_info tls12_md_info[] = {
3247 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3248 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3249 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3250 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3251 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3252 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3253 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3254 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3255 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3258 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3264 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3266 if (tls12_md_info[i].tlsext_hash == hash_alg)
3267 return tls12_md_info + i;
3273 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3275 const tls12_hash_info *inf;
3276 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3278 inf = tls12_get_hash_info(hash_alg);
3281 return ssl_md(inf->md_idx);
3284 static int tls12_get_pkey_idx(unsigned char sig_alg)
3287 #ifndef OPENSSL_NO_RSA
3288 case TLSEXT_signature_rsa:
3289 return SSL_PKEY_RSA_SIGN;
3291 #ifndef OPENSSL_NO_DSA
3292 case TLSEXT_signature_dsa:
3293 return SSL_PKEY_DSA_SIGN;
3295 #ifndef OPENSSL_NO_EC
3296 case TLSEXT_signature_ecdsa:
3297 return SSL_PKEY_ECC;
3299 # ifndef OPENSSL_NO_GOST
3300 case TLSEXT_signature_gostr34102001:
3301 return SSL_PKEY_GOST01;
3303 case TLSEXT_signature_gostr34102012_256:
3304 return SSL_PKEY_GOST12_256;
3306 case TLSEXT_signature_gostr34102012_512:
3307 return SSL_PKEY_GOST12_512;
3313 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3314 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3315 int *psignhash_nid, const unsigned char *data)
3317 int sign_nid = 0, hash_nid = 0;
3318 if (!phash_nid && !psign_nid && !psignhash_nid)
3320 if (phash_nid || psignhash_nid) {
3321 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3323 *phash_nid = hash_nid;
3325 if (psign_nid || psignhash_nid) {
3326 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3328 *psign_nid = sign_nid;
3330 if (psignhash_nid) {
3331 if (sign_nid && hash_nid)
3332 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
3334 *psignhash_nid = NID_undef;
3338 /* Check to see if a signature algorithm is allowed */
3339 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3341 /* See if we have an entry in the hash table and it is enabled */
3342 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3343 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3345 /* See if public key algorithm allowed */
3346 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3348 /* Finally see if security callback allows it */
3349 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3353 * Get a mask of disabled public key algorithms based on supported signature
3354 * algorithms. For example if no signature algorithm supports RSA then RSA is
3358 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3360 const unsigned char *sigalgs;
3361 size_t i, sigalgslen;
3362 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3364 * Now go through all signature algorithms seeing if we support any for
3365 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3366 * down calls to security callback only check if we have to.
3368 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3369 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3370 switch (sigalgs[1]) {
3371 #ifndef OPENSSL_NO_RSA
3372 case TLSEXT_signature_rsa:
3373 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3377 #ifndef OPENSSL_NO_DSA
3378 case TLSEXT_signature_dsa:
3379 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3383 #ifndef OPENSSL_NO_EC
3384 case TLSEXT_signature_ecdsa:
3385 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3392 *pmask_a |= SSL_aRSA;
3394 *pmask_a |= SSL_aDSS;
3396 *pmask_a |= SSL_aECDSA;
3399 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3400 const unsigned char *psig, size_t psiglen)
3402 unsigned char *tmpout = out;
3404 for (i = 0; i < psiglen; i += 2, psig += 2) {
3405 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3406 *tmpout++ = psig[0];
3407 *tmpout++ = psig[1];
3410 return tmpout - out;
3413 /* Given preference and allowed sigalgs set shared sigalgs */
3414 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3415 const unsigned char *pref, size_t preflen,
3416 const unsigned char *allow, size_t allowlen)
3418 const unsigned char *ptmp, *atmp;
3419 size_t i, j, nmatch = 0;
3420 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3421 /* Skip disabled hashes or signature algorithms */
3422 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3424 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3425 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3428 shsig->rhash = ptmp[0];
3429 shsig->rsign = ptmp[1];
3430 tls1_lookup_sigalg(&shsig->hash_nid,
3432 &shsig->signandhash_nid, ptmp);
3442 /* Set shared signature algorithms for SSL structures */
3443 static int tls1_set_shared_sigalgs(SSL *s)
3445 const unsigned char *pref, *allow, *conf;
3446 size_t preflen, allowlen, conflen;
3448 TLS_SIGALGS *salgs = NULL;
3450 unsigned int is_suiteb = tls1_suiteb(s);
3452 OPENSSL_free(c->shared_sigalgs);
3453 c->shared_sigalgs = NULL;
3454 c->shared_sigalgslen = 0;
3455 /* If client use client signature algorithms if not NULL */
3456 if (!s->server && c->client_sigalgs && !is_suiteb) {
3457 conf = c->client_sigalgs;
3458 conflen = c->client_sigalgslen;
3459 } else if (c->conf_sigalgs && !is_suiteb) {
3460 conf = c->conf_sigalgs;
3461 conflen = c->conf_sigalgslen;
3463 conflen = tls12_get_psigalgs(s, &conf);
3464 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3467 allow = s->s3->tmp.peer_sigalgs;
3468 allowlen = s->s3->tmp.peer_sigalgslen;
3472 pref = s->s3->tmp.peer_sigalgs;
3473 preflen = s->s3->tmp.peer_sigalgslen;
3475 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3477 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3480 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3484 c->shared_sigalgs = salgs;
3485 c->shared_sigalgslen = nmatch;
3489 /* Set preferred digest for each key type */
3491 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3494 /* Extension ignored for inappropriate versions */
3495 if (!SSL_USE_SIGALGS(s))
3497 /* Should never happen */
3501 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3502 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3503 if (s->s3->tmp.peer_sigalgs == NULL)
3505 s->s3->tmp.peer_sigalgslen = dsize;
3506 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3510 int tls1_process_sigalgs(SSL *s)
3515 const EVP_MD **pmd = s->s3->tmp.md;
3516 uint32_t *pvalid = s->s3->tmp.valid_flags;
3518 TLS_SIGALGS *sigptr;
3519 if (!tls1_set_shared_sigalgs(s))
3522 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3523 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
3525 * Use first set signature preference to force message digest,
3526 * ignoring any peer preferences.
3528 const unsigned char *sigs = NULL;
3530 sigs = c->conf_sigalgs;
3532 sigs = c->client_sigalgs;
3534 idx = tls12_get_pkey_idx(sigs[1]);
3535 md = tls12_get_hash(sigs[0]);
3537 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3538 if (idx == SSL_PKEY_RSA_SIGN) {
3539 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3540 pmd[SSL_PKEY_RSA_ENC] = md;
3546 for (i = 0, sigptr = c->shared_sigalgs;
3547 i < c->shared_sigalgslen; i++, sigptr++) {
3548 idx = tls12_get_pkey_idx(sigptr->rsign);
3549 if (idx > 0 && pmd[idx] == NULL) {
3550 md = tls12_get_hash(sigptr->rhash);
3552 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3553 if (idx == SSL_PKEY_RSA_SIGN) {
3554 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3555 pmd[SSL_PKEY_RSA_ENC] = md;
3561 * In strict mode leave unset digests as NULL to indicate we can't use
3562 * the certificate for signing.
3564 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3566 * Set any remaining keys to default values. NOTE: if alg is not
3567 * supported it stays as NULL.
3569 #ifndef OPENSSL_NO_DSA
3570 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3571 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3573 #ifndef OPENSSL_NO_RSA
3574 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3575 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3576 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3579 #ifndef OPENSSL_NO_EC
3580 if (pmd[SSL_PKEY_ECC] == NULL)
3581 pmd[SSL_PKEY_ECC] = EVP_sha1();
3583 # ifndef OPENSSL_NO_GOST
3584 if (pmd[SSL_PKEY_GOST01] == NULL)
3585 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3586 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3587 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3588 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3589 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3595 int SSL_get_sigalgs(SSL *s, int idx,
3596 int *psign, int *phash, int *psignhash,
3597 unsigned char *rsig, unsigned char *rhash)
3599 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3604 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3611 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3613 return s->s3->tmp.peer_sigalgslen / 2;
3616 int SSL_get_shared_sigalgs(SSL *s, int idx,
3617 int *psign, int *phash, int *psignhash,
3618 unsigned char *rsig, unsigned char *rhash)
3620 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3621 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3625 *phash = shsigalgs->hash_nid;
3627 *psign = shsigalgs->sign_nid;
3629 *psignhash = shsigalgs->signandhash_nid;
3631 *rsig = shsigalgs->rsign;
3633 *rhash = shsigalgs->rhash;
3634 return s->cert->shared_sigalgslen;
3637 #ifndef OPENSSL_NO_HEARTBEATS
3638 int tls1_process_heartbeat(SSL *s, unsigned char *p, unsigned int length)
3641 unsigned short hbtype;
3642 unsigned int payload;
3643 unsigned int padding = 16; /* Use minimum padding */
3645 if (s->msg_callback)
3646 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
3648 s, s->msg_callback_arg);
3650 /* Read type and payload length first */
3651 if (1 + 2 + 16 > length)
3652 return 0; /* silently discard */
3655 if (1 + 2 + payload + 16 > length)
3656 return 0; /* silently discard per RFC 6520 sec. 4 */
3659 if (hbtype == TLS1_HB_REQUEST) {
3660 unsigned char *buffer, *bp;
3664 * Allocate memory for the response, size is 1 bytes message type,
3665 * plus 2 bytes payload length, plus payload, plus padding
3667 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
3668 if (buffer == NULL) {
3669 SSLerr(SSL_F_TLS1_PROCESS_HEARTBEAT, ERR_R_MALLOC_FAILURE);
3674 /* Enter response type, length and copy payload */
3675 *bp++ = TLS1_HB_RESPONSE;
3677 memcpy(bp, pl, payload);
3679 /* Random padding */
3680 if (RAND_bytes(bp, padding) <= 0) {
3681 OPENSSL_free(buffer);
3685 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,
3686 3 + payload + padding);
3688 if (r >= 0 && s->msg_callback)
3689 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3690 buffer, 3 + payload + padding,
3691 s, s->msg_callback_arg);
3693 OPENSSL_free(buffer);
3697 } else if (hbtype == TLS1_HB_RESPONSE) {
3701 * We only send sequence numbers (2 bytes unsigned int), and 16
3702 * random bytes, so we just try to read the sequence number
3706 if (payload == 18 && seq == s->tlsext_hb_seq) {
3708 s->tlsext_hb_pending = 0;
3715 int tls1_heartbeat(SSL *s)
3717 unsigned char *buf, *p;
3719 unsigned int payload = 18; /* Sequence number + random bytes */
3720 unsigned int padding = 16; /* Use minimum padding */
3722 /* Only send if peer supports and accepts HB requests... */
3723 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
3724 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
3725 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
3729 /* ...and there is none in flight yet... */
3730 if (s->tlsext_hb_pending) {
3731 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
3735 /* ...and no handshake in progress. */
3736 if (SSL_in_init(s) || ossl_statem_get_in_handshake(s)) {
3737 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
3742 * Create HeartBeat message, we just use a sequence number
3743 * as payload to distuingish different messages and add
3744 * some random stuff.
3745 * - Message Type, 1 byte
3746 * - Payload Length, 2 bytes (unsigned int)
3747 * - Payload, the sequence number (2 bytes uint)
3748 * - Payload, random bytes (16 bytes uint)
3751 buf = OPENSSL_malloc(1 + 2 + payload + padding);
3753 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_MALLOC_FAILURE);
3758 *p++ = TLS1_HB_REQUEST;
3759 /* Payload length (18 bytes here) */
3761 /* Sequence number */
3762 s2n(s->tlsext_hb_seq, p);
3763 /* 16 random bytes */
3764 if (RAND_bytes(p, 16) <= 0) {
3765 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
3769 /* Random padding */
3770 if (RAND_bytes(p, padding) <= 0) {
3771 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
3775 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
3777 if (s->msg_callback)
3778 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3779 buf, 3 + payload + padding,
3780 s, s->msg_callback_arg);
3782 s->tlsext_hb_pending = 1;
3791 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3795 int sigalgs[MAX_SIGALGLEN];
3798 static void get_sigorhash(int *psig, int *phash, const char *str)
3800 if (strcmp(str, "RSA") == 0) {
3801 *psig = EVP_PKEY_RSA;
3802 } else if (strcmp(str, "DSA") == 0) {
3803 *psig = EVP_PKEY_DSA;
3804 } else if (strcmp(str, "ECDSA") == 0) {
3805 *psig = EVP_PKEY_EC;
3807 *phash = OBJ_sn2nid(str);
3808 if (*phash == NID_undef)
3809 *phash = OBJ_ln2nid(str);
3813 static int sig_cb(const char *elem, int len, void *arg)
3815 sig_cb_st *sarg = arg;
3818 int sig_alg = NID_undef, hash_alg = NID_undef;
3821 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3823 if (len > (int)(sizeof(etmp) - 1))
3825 memcpy(etmp, elem, len);
3827 p = strchr(etmp, '+');
3835 get_sigorhash(&sig_alg, &hash_alg, etmp);
3836 get_sigorhash(&sig_alg, &hash_alg, p);
3838 if (sig_alg == NID_undef || hash_alg == NID_undef)
3841 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3842 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3845 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3846 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3851 * Set suppored signature algorithms based on a colon separated list of the
3852 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3854 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3858 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3862 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3865 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3868 unsigned char *sigalgs, *sptr;
3873 sigalgs = OPENSSL_malloc(salglen);
3874 if (sigalgs == NULL)
3876 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3877 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3878 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3880 if (rhash == -1 || rsign == -1)
3887 OPENSSL_free(c->client_sigalgs);
3888 c->client_sigalgs = sigalgs;
3889 c->client_sigalgslen = salglen;
3891 OPENSSL_free(c->conf_sigalgs);
3892 c->conf_sigalgs = sigalgs;
3893 c->conf_sigalgslen = salglen;
3899 OPENSSL_free(sigalgs);
3903 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3907 if (default_nid == -1)
3909 sig_nid = X509_get_signature_nid(x);
3911 return sig_nid == default_nid ? 1 : 0;
3912 for (i = 0; i < c->shared_sigalgslen; i++)
3913 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3918 /* Check to see if a certificate issuer name matches list of CA names */
3919 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3923 nm = X509_get_issuer_name(x);
3924 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3925 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3932 * Check certificate chain is consistent with TLS extensions and is usable by
3933 * server. This servers two purposes: it allows users to check chains before
3934 * passing them to the server and it allows the server to check chains before
3935 * attempting to use them.
3938 /* Flags which need to be set for a certificate when stict mode not set */
3940 #define CERT_PKEY_VALID_FLAGS \
3941 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3942 /* Strict mode flags */
3943 #define CERT_PKEY_STRICT_FLAGS \
3944 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3945 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3947 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3952 int check_flags = 0, strict_mode;
3953 CERT_PKEY *cpk = NULL;
3956 unsigned int suiteb_flags = tls1_suiteb(s);
3957 /* idx == -1 means checking server chains */
3959 /* idx == -2 means checking client certificate chains */
3962 idx = cpk - c->pkeys;
3964 cpk = c->pkeys + idx;
3965 pvalid = s->s3->tmp.valid_flags + idx;
3967 pk = cpk->privatekey;
3969 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3970 /* If no cert or key, forget it */
3973 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3974 /* Allow any certificate to pass test */
3975 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
3976 rv = CERT_PKEY_STRICT_FLAGS | CERT_PKEY_EXPLICIT_SIGN |
3977 CERT_PKEY_VALID | CERT_PKEY_SIGN;
3985 idx = ssl_cert_type(x, pk);
3988 pvalid = s->s3->tmp.valid_flags + idx;
3990 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3991 check_flags = CERT_PKEY_STRICT_FLAGS;
3993 check_flags = CERT_PKEY_VALID_FLAGS;
4000 check_flags |= CERT_PKEY_SUITEB;
4001 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
4002 if (ok == X509_V_OK)
4003 rv |= CERT_PKEY_SUITEB;
4004 else if (!check_flags)
4009 * Check all signature algorithms are consistent with signature
4010 * algorithms extension if TLS 1.2 or later and strict mode.
4012 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
4014 unsigned char rsign = 0;
4015 if (s->s3->tmp.peer_sigalgs)
4017 /* If no sigalgs extension use defaults from RFC5246 */
4020 case SSL_PKEY_RSA_ENC:
4021 case SSL_PKEY_RSA_SIGN:
4022 case SSL_PKEY_DH_RSA:
4023 rsign = TLSEXT_signature_rsa;
4024 default_nid = NID_sha1WithRSAEncryption;
4027 case SSL_PKEY_DSA_SIGN:
4028 case SSL_PKEY_DH_DSA:
4029 rsign = TLSEXT_signature_dsa;
4030 default_nid = NID_dsaWithSHA1;
4034 rsign = TLSEXT_signature_ecdsa;
4035 default_nid = NID_ecdsa_with_SHA1;
4038 case SSL_PKEY_GOST01:
4039 rsign = TLSEXT_signature_gostr34102001;
4040 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
4043 case SSL_PKEY_GOST12_256:
4044 rsign = TLSEXT_signature_gostr34102012_256;
4045 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
4048 case SSL_PKEY_GOST12_512:
4049 rsign = TLSEXT_signature_gostr34102012_512;
4050 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
4059 * If peer sent no signature algorithms extension and we have set
4060 * preferred signature algorithms check we support sha1.
4062 if (default_nid > 0 && c->conf_sigalgs) {
4064 const unsigned char *p = c->conf_sigalgs;
4065 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
4066 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4069 if (j == c->conf_sigalgslen) {
4076 /* Check signature algorithm of each cert in chain */
4077 if (!tls1_check_sig_alg(c, x, default_nid)) {
4081 rv |= CERT_PKEY_EE_SIGNATURE;
4082 rv |= CERT_PKEY_CA_SIGNATURE;
4083 for (i = 0; i < sk_X509_num(chain); i++) {
4084 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
4086 rv &= ~CERT_PKEY_CA_SIGNATURE;
4093 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4094 else if (check_flags)
4095 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
4097 /* Check cert parameters are consistent */
4098 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4099 rv |= CERT_PKEY_EE_PARAM;
4100 else if (!check_flags)
4103 rv |= CERT_PKEY_CA_PARAM;
4104 /* In strict mode check rest of chain too */
4105 else if (strict_mode) {
4106 rv |= CERT_PKEY_CA_PARAM;
4107 for (i = 0; i < sk_X509_num(chain); i++) {
4108 X509 *ca = sk_X509_value(chain, i);
4109 if (!tls1_check_cert_param(s, ca, 0)) {
4111 rv &= ~CERT_PKEY_CA_PARAM;
4118 if (!s->server && strict_mode) {
4119 STACK_OF(X509_NAME) *ca_dn;
4123 check_type = TLS_CT_RSA_SIGN;
4126 check_type = TLS_CT_DSS_SIGN;
4129 check_type = TLS_CT_ECDSA_SIGN;
4134 int cert_type = X509_certificate_type(x, pk);
4135 if (cert_type & EVP_PKS_RSA)
4136 check_type = TLS_CT_RSA_FIXED_DH;
4137 if (cert_type & EVP_PKS_DSA)
4138 check_type = TLS_CT_DSS_FIXED_DH;
4142 const unsigned char *ctypes;
4146 ctypelen = (int)c->ctype_num;
4148 ctypes = (unsigned char *)s->s3->tmp.ctype;
4149 ctypelen = s->s3->tmp.ctype_num;
4151 for (i = 0; i < ctypelen; i++) {
4152 if (ctypes[i] == check_type) {
4153 rv |= CERT_PKEY_CERT_TYPE;
4157 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4160 rv |= CERT_PKEY_CERT_TYPE;
4162 ca_dn = s->s3->tmp.ca_names;
4164 if (!sk_X509_NAME_num(ca_dn))
4165 rv |= CERT_PKEY_ISSUER_NAME;
4167 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4168 if (ssl_check_ca_name(ca_dn, x))
4169 rv |= CERT_PKEY_ISSUER_NAME;
4171 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4172 for (i = 0; i < sk_X509_num(chain); i++) {
4173 X509 *xtmp = sk_X509_value(chain, i);
4174 if (ssl_check_ca_name(ca_dn, xtmp)) {
4175 rv |= CERT_PKEY_ISSUER_NAME;
4180 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4183 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4185 if (!check_flags || (rv & check_flags) == check_flags)
4186 rv |= CERT_PKEY_VALID;
4190 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4191 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
4192 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4193 else if (s->s3->tmp.md[idx] != NULL)
4194 rv |= CERT_PKEY_SIGN;
4196 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4199 * When checking a CERT_PKEY structure all flags are irrelevant if the
4203 if (rv & CERT_PKEY_VALID)
4206 /* Preserve explicit sign flag, clear rest */
4207 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
4214 /* Set validity of certificates in an SSL structure */
4215 void tls1_set_cert_validity(SSL *s)
4217 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4218 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4219 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4220 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4221 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4222 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4223 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
4224 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
4225 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
4228 /* User level utiity function to check a chain is suitable */
4229 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4231 return tls1_check_chain(s, x, pk, chain, -1);
4235 #ifndef OPENSSL_NO_DH
4236 DH *ssl_get_auto_dh(SSL *s)
4238 int dh_secbits = 80;
4239 if (s->cert->dh_tmp_auto == 2)
4240 return DH_get_1024_160();
4241 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
4242 if (s->s3->tmp.new_cipher->strength_bits == 256)
4247 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4248 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4251 if (dh_secbits >= 128) {
4257 BN_set_word(dhp->g, 2);
4258 if (dh_secbits >= 192)
4259 dhp->p = get_rfc3526_prime_8192(NULL);
4261 dhp->p = get_rfc3526_prime_3072(NULL);
4262 if (dhp->p == NULL || dhp->g == NULL) {
4268 if (dh_secbits >= 112)
4269 return DH_get_2048_224();
4270 return DH_get_1024_160();
4274 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4277 EVP_PKEY *pkey = X509_get_pubkey(x);
4279 secbits = EVP_PKEY_security_bits(pkey);
4280 EVP_PKEY_free(pkey);
4284 return ssl_security(s, op, secbits, 0, x);
4286 return ssl_ctx_security(ctx, op, secbits, 0, x);
4289 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4291 /* Lookup signature algorithm digest */
4292 int secbits = -1, md_nid = NID_undef, sig_nid;
4293 sig_nid = X509_get_signature_nid(x);
4294 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4296 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4297 secbits = EVP_MD_size(md) * 4;
4300 return ssl_security(s, op, secbits, md_nid, x);
4302 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4305 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4308 vfy = SSL_SECOP_PEER;
4310 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4311 return SSL_R_EE_KEY_TOO_SMALL;
4313 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4314 return SSL_R_CA_KEY_TOO_SMALL;
4316 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4317 return SSL_R_CA_MD_TOO_WEAK;
4322 * Check security of a chain, if sk includes the end entity certificate then
4323 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4324 * one to the peer. Return values: 1 if ok otherwise error code to use
4327 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4329 int rv, start_idx, i;
4331 x = sk_X509_value(sk, 0);
4336 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4340 for (i = start_idx; i < sk_X509_num(sk); i++) {
4341 x = sk_X509_value(sk, i);
4342 rv = ssl_security_cert(s, NULL, x, vfy, 0);