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 assume OK */
872 if (s->cert->ecdh_tmp_auto)
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))
897 if (!tls1_set_ec_id(curve_id, NULL, ec))
899 /* Set this to allow use of invalid curves for testing */
903 return tls1_check_ec_key(s, curve_id, NULL);
906 # endif /* OPENSSL_NO_EC */
910 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
915 #endif /* OPENSSL_NO_EC */
918 * List of supported signature algorithms and hashes. Should make this
919 * customisable at some point, for now include everything we support.
922 #ifdef OPENSSL_NO_RSA
923 # define tlsext_sigalg_rsa(md) /* */
925 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
928 #ifdef OPENSSL_NO_DSA
929 # define tlsext_sigalg_dsa(md) /* */
931 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
935 # define tlsext_sigalg_ecdsa(md) /* */
937 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
940 #define tlsext_sigalg(md) \
941 tlsext_sigalg_rsa(md) \
942 tlsext_sigalg_dsa(md) \
943 tlsext_sigalg_ecdsa(md)
945 static const unsigned char tls12_sigalgs[] = {
946 tlsext_sigalg(TLSEXT_hash_sha512)
947 tlsext_sigalg(TLSEXT_hash_sha384)
948 tlsext_sigalg(TLSEXT_hash_sha256)
949 tlsext_sigalg(TLSEXT_hash_sha224)
950 tlsext_sigalg(TLSEXT_hash_sha1)
951 #ifndef OPENSSL_NO_GOST
952 TLSEXT_hash_gostr3411, TLSEXT_signature_gostr34102001,
953 TLSEXT_hash_gostr34112012_256, TLSEXT_signature_gostr34102012_256,
954 TLSEXT_hash_gostr34112012_512, TLSEXT_signature_gostr34102012_512
958 #ifndef OPENSSL_NO_EC
959 static const unsigned char suiteb_sigalgs[] = {
960 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
961 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
964 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
967 * If Suite B mode use Suite B sigalgs only, ignore any other
970 #ifndef OPENSSL_NO_EC
971 switch (tls1_suiteb(s)) {
972 case SSL_CERT_FLAG_SUITEB_128_LOS:
973 *psigs = suiteb_sigalgs;
974 return sizeof(suiteb_sigalgs);
976 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
977 *psigs = suiteb_sigalgs;
980 case SSL_CERT_FLAG_SUITEB_192_LOS:
981 *psigs = suiteb_sigalgs + 2;
985 /* If server use client authentication sigalgs if not NULL */
986 if (s->server && s->cert->client_sigalgs) {
987 *psigs = s->cert->client_sigalgs;
988 return s->cert->client_sigalgslen;
989 } else if (s->cert->conf_sigalgs) {
990 *psigs = s->cert->conf_sigalgs;
991 return s->cert->conf_sigalgslen;
993 *psigs = tls12_sigalgs;
994 return sizeof(tls12_sigalgs);
999 * Check signature algorithm is consistent with sent supported signature
1000 * algorithms and if so return relevant digest.
1002 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
1003 const unsigned char *sig, EVP_PKEY *pkey)
1005 const unsigned char *sent_sigs;
1006 size_t sent_sigslen, i;
1007 int sigalg = tls12_get_sigid(pkey);
1008 /* Should never happen */
1011 /* Check key type is consistent with signature */
1012 if (sigalg != (int)sig[1]) {
1013 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1016 #ifndef OPENSSL_NO_EC
1017 if (pkey->type == EVP_PKEY_EC) {
1018 unsigned char curve_id[2], comp_id;
1019 /* Check compression and curve matches extensions */
1020 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
1022 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
1023 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1026 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
1027 if (tls1_suiteb(s)) {
1030 if (curve_id[1] == TLSEXT_curve_P_256) {
1031 if (sig[0] != TLSEXT_hash_sha256) {
1032 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1033 SSL_R_ILLEGAL_SUITEB_DIGEST);
1036 } else if (curve_id[1] == TLSEXT_curve_P_384) {
1037 if (sig[0] != TLSEXT_hash_sha384) {
1038 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1039 SSL_R_ILLEGAL_SUITEB_DIGEST);
1045 } else if (tls1_suiteb(s))
1049 /* Check signature matches a type we sent */
1050 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1051 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
1052 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1055 /* Allow fallback to SHA1 if not strict mode */
1056 if (i == sent_sigslen
1057 && (sig[0] != TLSEXT_hash_sha1
1058 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1059 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1062 *pmd = tls12_get_hash(sig[0]);
1064 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
1067 /* Make sure security callback allows algorithm */
1068 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1069 EVP_MD_size(*pmd) * 4, EVP_MD_type(*pmd),
1071 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1075 * Store the digest used so applications can retrieve it if they wish.
1077 s->s3->tmp.peer_md = *pmd;
1082 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
1083 * supported or doesn't appear in supported signature algorithms. Unlike
1084 * ssl_cipher_get_disabled this applies to a specific session and not global
1087 void ssl_set_client_disabled(SSL *s)
1089 s->s3->tmp.mask_a = 0;
1090 s->s3->tmp.mask_k = 0;
1091 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1092 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1093 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1095 s->s3->tmp.mask_ssl = 0;
1096 /* Disable TLS 1.0 ciphers if using SSL v3 */
1097 if (s->client_version == SSL3_VERSION)
1098 s->s3->tmp.mask_ssl |= SSL_TLSV1;
1099 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1101 * Disable static DH if we don't include any appropriate signature
1104 if (s->s3->tmp.mask_a & SSL_aRSA)
1105 s->s3->tmp.mask_k |= SSL_kDHr | SSL_kECDHr;
1106 if (s->s3->tmp.mask_a & SSL_aDSS)
1107 s->s3->tmp.mask_k |= SSL_kDHd;
1108 if (s->s3->tmp.mask_a & SSL_aECDSA)
1109 s->s3->tmp.mask_k |= SSL_kECDHe;
1110 # ifndef OPENSSL_NO_PSK
1111 /* with PSK there must be client callback set */
1112 if (!s->psk_client_callback) {
1113 s->s3->tmp.mask_a |= SSL_aPSK;
1114 s->s3->tmp.mask_k |= SSL_PSK;
1116 #endif /* OPENSSL_NO_PSK */
1117 #ifndef OPENSSL_NO_SRP
1118 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1119 s->s3->tmp.mask_a |= SSL_aSRP;
1120 s->s3->tmp.mask_k |= SSL_kSRP;
1125 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op)
1127 if (c->algorithm_ssl & s->s3->tmp.mask_ssl
1128 || c->algorithm_mkey & s->s3->tmp.mask_k
1129 || c->algorithm_auth & s->s3->tmp.mask_a)
1131 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1134 static int tls_use_ticket(SSL *s)
1136 if (s->options & SSL_OP_NO_TICKET)
1138 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1141 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1142 unsigned char *limit, int *al)
1145 unsigned char *orig = buf;
1146 unsigned char *ret = buf;
1147 #ifndef OPENSSL_NO_EC
1148 /* See if we support any ECC ciphersuites */
1150 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1152 unsigned long alg_k, alg_a;
1153 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1155 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1156 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1158 alg_k = c->algorithm_mkey;
1159 alg_a = c->algorithm_auth;
1160 if ((alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)
1161 || (alg_a & SSL_aECDSA))) {
1172 return NULL; /* this really never occurs, but ... */
1174 /* Add RI if renegotiating */
1175 if (s->renegotiate) {
1178 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1179 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1183 if ((limit - ret - 4 - el) < 0)
1186 s2n(TLSEXT_TYPE_renegotiate, ret);
1189 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1190 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1196 /* Only add RI for SSLv3 */
1197 if (s->client_version == SSL3_VERSION)
1200 if (s->tlsext_hostname != NULL) {
1201 /* Add TLS extension servername to the Client Hello message */
1202 unsigned long size_str;
1206 * check for enough space.
1207 * 4 for the servername type and entension length
1208 * 2 for servernamelist length
1209 * 1 for the hostname type
1210 * 2 for hostname length
1214 if ((lenmax = limit - ret - 9) < 0
1216 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
1219 /* extension type and length */
1220 s2n(TLSEXT_TYPE_server_name, ret);
1221 s2n(size_str + 5, ret);
1223 /* length of servername list */
1224 s2n(size_str + 3, ret);
1226 /* hostname type, length and hostname */
1227 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1229 memcpy(ret, s->tlsext_hostname, size_str);
1232 #ifndef OPENSSL_NO_SRP
1233 /* Add SRP username if there is one */
1234 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1235 * Client Hello message */
1237 int login_len = strlen(s->srp_ctx.login);
1238 if (login_len > 255 || login_len == 0) {
1239 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1244 * check for enough space.
1245 * 4 for the srp type type and entension length
1246 * 1 for the srp user identity
1247 * + srp user identity length
1249 if ((limit - ret - 5 - login_len) < 0)
1252 /* fill in the extension */
1253 s2n(TLSEXT_TYPE_srp, ret);
1254 s2n(login_len + 1, ret);
1255 (*ret++) = (unsigned char)login_len;
1256 memcpy(ret, s->srp_ctx.login, login_len);
1261 #ifndef OPENSSL_NO_EC
1264 * Add TLS extension ECPointFormats to the ClientHello message
1267 const unsigned char *pcurves, *pformats;
1268 size_t num_curves, num_formats, curves_list_len;
1270 unsigned char *etmp;
1272 tls1_get_formatlist(s, &pformats, &num_formats);
1274 if ((lenmax = limit - ret - 5) < 0)
1276 if (num_formats > (size_t)lenmax)
1278 if (num_formats > 255) {
1279 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1283 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1284 /* The point format list has 1-byte length. */
1285 s2n(num_formats + 1, ret);
1286 *(ret++) = (unsigned char)num_formats;
1287 memcpy(ret, pformats, num_formats);
1291 * Add TLS extension EllipticCurves to the ClientHello message
1293 pcurves = s->tlsext_ellipticcurvelist;
1294 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1297 if ((lenmax = limit - ret - 6) < 0)
1299 if (num_curves > (size_t)lenmax / 2)
1301 if (num_curves > 65532 / 2) {
1302 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1306 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1308 /* Copy curve ID if supported */
1309 for (i = 0; i < num_curves; i++, pcurves += 2) {
1310 if (tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED)) {
1311 *etmp++ = pcurves[0];
1312 *etmp++ = pcurves[1];
1316 curves_list_len = etmp - ret - 4;
1318 s2n(curves_list_len + 2, ret);
1319 s2n(curves_list_len, ret);
1320 ret += curves_list_len;
1322 #endif /* OPENSSL_NO_EC */
1324 if (tls_use_ticket(s)) {
1326 if (!s->new_session && s->session && s->session->tlsext_tick)
1327 ticklen = s->session->tlsext_ticklen;
1328 else if (s->session && s->tlsext_session_ticket &&
1329 s->tlsext_session_ticket->data) {
1330 ticklen = s->tlsext_session_ticket->length;
1331 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1332 if (s->session->tlsext_tick == NULL)
1334 memcpy(s->session->tlsext_tick,
1335 s->tlsext_session_ticket->data, ticklen);
1336 s->session->tlsext_ticklen = ticklen;
1339 if (ticklen == 0 && s->tlsext_session_ticket &&
1340 s->tlsext_session_ticket->data == NULL)
1343 * Check for enough room 2 for extension type, 2 for len rest for
1346 if ((long)(limit - ret - 4 - ticklen) < 0)
1348 s2n(TLSEXT_TYPE_session_ticket, ret);
1351 memcpy(ret, s->session->tlsext_tick, ticklen);
1357 if (SSL_USE_SIGALGS(s)) {
1359 const unsigned char *salg;
1360 unsigned char *etmp;
1361 salglen = tls12_get_psigalgs(s, &salg);
1362 if ((size_t)(limit - ret) < salglen + 6)
1364 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1366 /* Skip over lengths for now */
1368 salglen = tls12_copy_sigalgs(s, ret, salg, salglen);
1369 /* Fill in lengths */
1370 s2n(salglen + 2, etmp);
1375 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1377 long extlen, idlen, itmp;
1381 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1382 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1383 itmp = i2d_OCSP_RESPID(id, NULL);
1389 if (s->tlsext_ocsp_exts) {
1390 extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1396 if ((long)(limit - ret - 7 - extlen - idlen) < 0)
1398 s2n(TLSEXT_TYPE_status_request, ret);
1399 if (extlen + idlen > 0xFFF0)
1401 s2n(extlen + idlen + 5, ret);
1402 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1404 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1405 /* save position of id len */
1406 unsigned char *q = ret;
1407 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1408 /* skip over id len */
1410 itmp = i2d_OCSP_RESPID(id, &ret);
1416 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1418 #ifndef OPENSSL_NO_HEARTBEATS
1419 /* Add Heartbeat extension */
1420 if ((limit - ret - 4 - 1) < 0)
1422 s2n(TLSEXT_TYPE_heartbeat, ret);
1426 * 1: peer may send requests
1427 * 2: peer not allowed to send requests
1429 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1430 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1432 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1435 #ifndef OPENSSL_NO_NEXTPROTONEG
1436 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1438 * The client advertises an emtpy extension to indicate its support
1439 * for Next Protocol Negotiation
1441 if (limit - ret - 4 < 0)
1443 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1448 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1449 if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len)
1451 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1452 s2n(2 + s->alpn_client_proto_list_len, ret);
1453 s2n(s->alpn_client_proto_list_len, ret);
1454 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1455 ret += s->alpn_client_proto_list_len;
1457 #ifndef OPENSSL_NO_SRTP
1458 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1461 /* Returns 0 on success!! */
1462 if (ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0)) {
1463 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1467 if ((limit - ret - 4 - el) < 0)
1470 s2n(TLSEXT_TYPE_use_srtp, ret);
1473 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1474 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1480 custom_ext_init(&s->cert->cli_ext);
1481 /* Add custom TLS Extensions to ClientHello */
1482 if (!custom_ext_add(s, 0, &ret, limit, al))
1484 #ifdef TLSEXT_TYPE_encrypt_then_mac
1485 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1488 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1492 * Add padding to workaround bugs in F5 terminators. See
1493 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1494 * code works out the length of all existing extensions it MUST always
1497 if (s->options & SSL_OP_TLSEXT_PADDING) {
1498 int hlen = ret - (unsigned char *)s->init_buf->data;
1500 if (hlen > 0xff && hlen < 0x200) {
1501 hlen = 0x200 - hlen;
1507 s2n(TLSEXT_TYPE_padding, ret);
1509 memset(ret, 0, hlen);
1516 if ((extdatalen = ret - orig - 2) == 0)
1519 s2n(extdatalen, orig);
1523 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1524 unsigned char *limit, int *al)
1527 unsigned char *orig = buf;
1528 unsigned char *ret = buf;
1529 #ifndef OPENSSL_NO_NEXTPROTONEG
1530 int next_proto_neg_seen;
1532 #ifndef OPENSSL_NO_EC
1533 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1534 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1535 int using_ecc = (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe))
1536 || (alg_a & SSL_aECDSA);
1537 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1542 return NULL; /* this really never occurs, but ... */
1544 if (s->s3->send_connection_binding) {
1547 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1548 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1552 if ((limit - ret - 4 - el) < 0)
1555 s2n(TLSEXT_TYPE_renegotiate, ret);
1558 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1559 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1566 /* Only add RI for SSLv3 */
1567 if (s->version == SSL3_VERSION)
1570 if (!s->hit && s->servername_done == 1
1571 && s->session->tlsext_hostname != NULL) {
1572 if ((long)(limit - ret - 4) < 0)
1575 s2n(TLSEXT_TYPE_server_name, ret);
1578 #ifndef OPENSSL_NO_EC
1580 const unsigned char *plist;
1583 * Add TLS extension ECPointFormats to the ServerHello message
1587 tls1_get_formatlist(s, &plist, &plistlen);
1589 if ((lenmax = limit - ret - 5) < 0)
1591 if (plistlen > (size_t)lenmax)
1593 if (plistlen > 255) {
1594 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1598 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1599 s2n(plistlen + 1, ret);
1600 *(ret++) = (unsigned char)plistlen;
1601 memcpy(ret, plist, plistlen);
1606 * Currently the server should not respond with a SupportedCurves
1609 #endif /* OPENSSL_NO_EC */
1611 if (s->tlsext_ticket_expected && tls_use_ticket(s)) {
1612 if ((long)(limit - ret - 4) < 0)
1614 s2n(TLSEXT_TYPE_session_ticket, ret);
1618 if (s->tlsext_status_expected) {
1619 if ((long)(limit - ret - 4) < 0)
1621 s2n(TLSEXT_TYPE_status_request, ret);
1625 #ifndef OPENSSL_NO_SRTP
1626 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1629 /* Returns 0 on success!! */
1630 if (ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0)) {
1631 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1634 if ((limit - ret - 4 - el) < 0)
1637 s2n(TLSEXT_TYPE_use_srtp, ret);
1640 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1641 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1648 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1649 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1650 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1651 const unsigned char cryptopro_ext[36] = {
1652 0xfd, 0xe8, /* 65000 */
1653 0x00, 0x20, /* 32 bytes length */
1654 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1655 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1656 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1657 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1659 if (limit - ret < 36)
1661 memcpy(ret, cryptopro_ext, 36);
1665 #ifndef OPENSSL_NO_HEARTBEATS
1666 /* Add Heartbeat extension if we've received one */
1667 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) {
1668 if ((limit - ret - 4 - 1) < 0)
1670 s2n(TLSEXT_TYPE_heartbeat, ret);
1674 * 1: peer may send requests
1675 * 2: peer not allowed to send requests
1677 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1678 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1680 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1685 #ifndef OPENSSL_NO_NEXTPROTONEG
1686 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1687 s->s3->next_proto_neg_seen = 0;
1688 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1689 const unsigned char *npa;
1690 unsigned int npalen;
1693 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1695 ctx->next_protos_advertised_cb_arg);
1696 if (r == SSL_TLSEXT_ERR_OK) {
1697 if ((long)(limit - ret - 4 - npalen) < 0)
1699 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1701 memcpy(ret, npa, npalen);
1703 s->s3->next_proto_neg_seen = 1;
1707 if (!custom_ext_add(s, 1, &ret, limit, al))
1709 #ifdef TLSEXT_TYPE_encrypt_then_mac
1710 if (s->s3->flags & TLS1_FLAGS_ENCRYPT_THEN_MAC) {
1712 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1713 * for other cases too.
1715 if (s->s3->tmp.new_cipher->algorithm_mac == SSL_AEAD
1716 || s->s3->tmp.new_cipher->algorithm_enc == SSL_RC4
1717 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1718 || s->s3->tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12)
1719 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1721 s2n(TLSEXT_TYPE_encrypt_then_mac, ret);
1726 if (!s->hit && s->session->flags & SSL_SESS_FLAG_EXTMS) {
1727 s2n(TLSEXT_TYPE_extended_master_secret, ret);
1731 if (s->s3->alpn_selected) {
1732 const unsigned char *selected = s->s3->alpn_selected;
1733 unsigned len = s->s3->alpn_selected_len;
1735 if ((long)(limit - ret - 4 - 2 - 1 - len) < 0)
1737 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1741 memcpy(ret, selected, len);
1747 if ((extdatalen = ret - orig - 2) == 0)
1750 s2n(extdatalen, orig);
1755 * tls1_alpn_handle_client_hello is called to process the ALPN extension in a
1756 * ClientHello. data: the contents of the extension, not including the type
1757 * and length. data_len: the number of bytes in |data| al: a pointer to the
1758 * alert value to send in the event of a non-zero return. returns: 0 on
1761 static int tls1_alpn_handle_client_hello(SSL *s, PACKET *pkt, int *al)
1763 unsigned int data_len;
1764 unsigned int proto_len;
1765 const unsigned char *selected;
1766 unsigned char *data;
1767 unsigned char selected_len;
1770 if (s->ctx->alpn_select_cb == NULL)
1774 * data should contain a uint16 length followed by a series of 8-bit,
1775 * length-prefixed strings.
1777 if (!PACKET_get_net_2(pkt, &data_len)
1778 || PACKET_remaining(pkt) != data_len
1779 || !PACKET_peek_bytes(pkt, &data, data_len))
1783 if (!PACKET_get_1(pkt, &proto_len)
1785 || !PACKET_forward(pkt, proto_len))
1787 } while (PACKET_remaining(pkt));
1789 r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len,
1790 s->ctx->alpn_select_cb_arg);
1791 if (r == SSL_TLSEXT_ERR_OK) {
1792 OPENSSL_free(s->s3->alpn_selected);
1793 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
1794 if (s->s3->alpn_selected == NULL) {
1795 *al = SSL_AD_INTERNAL_ERROR;
1798 memcpy(s->s3->alpn_selected, selected, selected_len);
1799 s->s3->alpn_selected_len = selected_len;
1804 *al = SSL_AD_DECODE_ERROR;
1808 #ifndef OPENSSL_NO_EC
1810 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1811 * SecureTransport using the TLS extension block in |d|, of length |n|.
1812 * Safari, since 10.6, sends exactly these extensions, in this order:
1817 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1818 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1819 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1820 * 10.8..10.8.3 (which don't work).
1822 static void ssl_check_for_safari(SSL *s, const PACKET *pkt)
1824 unsigned int type, size;
1825 unsigned char *eblock1, *eblock2;
1828 static const unsigned char kSafariExtensionsBlock[] = {
1829 0x00, 0x0a, /* elliptic_curves extension */
1830 0x00, 0x08, /* 8 bytes */
1831 0x00, 0x06, /* 6 bytes of curve ids */
1832 0x00, 0x17, /* P-256 */
1833 0x00, 0x18, /* P-384 */
1834 0x00, 0x19, /* P-521 */
1836 0x00, 0x0b, /* ec_point_formats */
1837 0x00, 0x02, /* 2 bytes */
1838 0x01, /* 1 point format */
1839 0x00, /* uncompressed */
1842 /* The following is only present in TLS 1.2 */
1843 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1844 0x00, 0x0d, /* signature_algorithms */
1845 0x00, 0x0c, /* 12 bytes */
1846 0x00, 0x0a, /* 10 bytes */
1847 0x05, 0x01, /* SHA-384/RSA */
1848 0x04, 0x01, /* SHA-256/RSA */
1849 0x02, 0x01, /* SHA-1/RSA */
1850 0x04, 0x03, /* SHA-256/ECDSA */
1851 0x02, 0x03, /* SHA-1/ECDSA */
1856 if (!PACKET_forward(&tmppkt, 2)
1857 || !PACKET_get_net_2(&tmppkt, &type)
1858 || !PACKET_get_net_2(&tmppkt, &size)
1859 || !PACKET_forward(&tmppkt, size))
1862 if (type != TLSEXT_TYPE_server_name)
1865 if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
1866 const size_t len1 = sizeof(kSafariExtensionsBlock);
1867 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1869 if (!PACKET_get_bytes(&tmppkt, &eblock1, len1)
1870 || !PACKET_get_bytes(&tmppkt, &eblock2, len2)
1871 || PACKET_remaining(&tmppkt))
1873 if (memcmp(eblock1, kSafariExtensionsBlock, len1) != 0)
1875 if (memcmp(eblock2, kSafariTLS12ExtensionsBlock, len2) != 0)
1878 const size_t len = sizeof(kSafariExtensionsBlock);
1880 if (!PACKET_get_bytes(&tmppkt, &eblock1, len)
1881 || PACKET_remaining(&tmppkt))
1883 if (memcmp(eblock1, kSafariExtensionsBlock, len) != 0)
1887 s->s3->is_probably_safari = 1;
1889 #endif /* !OPENSSL_NO_EC */
1891 static int ssl_scan_clienthello_tlsext(SSL *s, PACKET *pkt, int *al)
1896 unsigned char *data;
1897 int renegotiate_seen = 0;
1899 s->servername_done = 0;
1900 s->tlsext_status_type = -1;
1901 #ifndef OPENSSL_NO_NEXTPROTONEG
1902 s->s3->next_proto_neg_seen = 0;
1905 OPENSSL_free(s->s3->alpn_selected);
1906 s->s3->alpn_selected = NULL;
1907 #ifndef OPENSSL_NO_HEARTBEATS
1908 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1909 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1912 #ifndef OPENSSL_NO_EC
1913 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1914 ssl_check_for_safari(s, pkt);
1915 # endif /* !OPENSSL_NO_EC */
1917 /* Clear any signature algorithms extension received */
1918 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1919 s->s3->tmp.peer_sigalgs = NULL;
1920 #ifdef TLSEXT_TYPE_encrypt_then_mac
1921 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
1924 #ifndef OPENSSL_NO_SRP
1925 OPENSSL_free(s->srp_ctx.login);
1926 s->srp_ctx.login = NULL;
1929 s->srtp_profile = NULL;
1931 if (PACKET_remaining(pkt) == 0)
1934 if (!PACKET_get_net_2(pkt, &len))
1937 if (PACKET_remaining(pkt) != len)
1940 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
1943 if (!PACKET_peek_bytes(pkt, &data, size))
1946 if (s->tlsext_debug_cb)
1947 s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
1949 if (!PACKET_get_sub_packet(pkt, &subpkt, size))
1952 if (type == TLSEXT_TYPE_renegotiate) {
1953 if (!ssl_parse_clienthello_renegotiate_ext(s, &subpkt, al))
1955 renegotiate_seen = 1;
1956 } else if (s->version == SSL3_VERSION) {
1959 * The servername extension is treated as follows:
1961 * - Only the hostname type is supported with a maximum length of 255.
1962 * - The servername is rejected if too long or if it contains zeros,
1963 * in which case an fatal alert is generated.
1964 * - The servername field is maintained together with the session cache.
1965 * - When a session is resumed, the servername call back invoked in order
1966 * to allow the application to position itself to the right context.
1967 * - The servername is acknowledged if it is new for a session or when
1968 * it is identical to a previously used for the same session.
1969 * Applications can control the behaviour. They can at any time
1970 * set a 'desirable' servername for a new SSL object. This can be the
1971 * case for example with HTTPS when a Host: header field is received and
1972 * a renegotiation is requested. In this case, a possible servername
1973 * presented in the new client hello is only acknowledged if it matches
1974 * the value of the Host: field.
1975 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1976 * if they provide for changing an explicit servername context for the
1977 * session, i.e. when the session has been established with a servername
1979 * - On session reconnect, the servername extension may be absent.
1983 else if (type == TLSEXT_TYPE_server_name) {
1984 unsigned char *sdata;
1985 unsigned int servname_type;
1989 if (!PACKET_get_net_2(&subpkt, &dsize)
1990 || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))
1993 while (PACKET_remaining(&ssubpkt) > 3) {
1994 if (!PACKET_get_1(&ssubpkt, &servname_type)
1995 || !PACKET_get_net_2(&ssubpkt, &len)
1996 || PACKET_remaining(&ssubpkt) < len)
1999 if (s->servername_done == 0)
2000 switch (servname_type) {
2001 case TLSEXT_NAMETYPE_host_name:
2003 if (s->session->tlsext_hostname)
2006 if (len > TLSEXT_MAXLEN_host_name) {
2007 *al = TLS1_AD_UNRECOGNIZED_NAME;
2010 if ((s->session->tlsext_hostname =
2011 OPENSSL_malloc(len + 1)) == NULL) {
2012 *al = TLS1_AD_INTERNAL_ERROR;
2015 if (!PACKET_copy_bytes(&ssubpkt,
2016 (unsigned char *)s->session
2019 *al = SSL_AD_DECODE_ERROR;
2022 s->session->tlsext_hostname[len] = '\0';
2023 if (strlen(s->session->tlsext_hostname) != len) {
2024 OPENSSL_free(s->session->tlsext_hostname);
2025 s->session->tlsext_hostname = NULL;
2026 *al = TLS1_AD_UNRECOGNIZED_NAME;
2029 s->servername_done = 1;
2032 if (!PACKET_get_bytes(&ssubpkt, &sdata, len)) {
2033 *al = SSL_AD_DECODE_ERROR;
2036 s->servername_done = s->session->tlsext_hostname
2037 && strlen(s->session->tlsext_hostname) == len
2038 && strncmp(s->session->tlsext_hostname,
2039 (char *)sdata, len) == 0;
2048 /* We shouldn't have any bytes left */
2049 if (PACKET_remaining(&ssubpkt) != 0)
2053 #ifndef OPENSSL_NO_SRP
2054 else if (type == TLSEXT_TYPE_srp) {
2055 if (!PACKET_get_1(&subpkt, &len)
2056 || s->srp_ctx.login != NULL)
2059 if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
2061 if (!PACKET_copy_bytes(&subpkt, (unsigned char *)s->srp_ctx.login,
2064 s->srp_ctx.login[len] = '\0';
2066 if (strlen(s->srp_ctx.login) != len
2067 || PACKET_remaining(&subpkt))
2072 #ifndef OPENSSL_NO_EC
2073 else if (type == TLSEXT_TYPE_ec_point_formats) {
2074 unsigned int ecpointformatlist_length;
2076 if (!PACKET_get_1(&subpkt, &ecpointformatlist_length)
2077 || ecpointformatlist_length == 0)
2081 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2082 s->session->tlsext_ecpointformatlist = NULL;
2083 s->session->tlsext_ecpointformatlist_length = 0;
2084 if ((s->session->tlsext_ecpointformatlist =
2085 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2086 *al = TLS1_AD_INTERNAL_ERROR;
2089 s->session->tlsext_ecpointformatlist_length =
2090 ecpointformatlist_length;
2091 if (!PACKET_copy_bytes(&subpkt,
2092 s->session->tlsext_ecpointformatlist,
2093 ecpointformatlist_length))
2095 } else if (!PACKET_forward(&subpkt, ecpointformatlist_length)) {
2098 /* We should have consumed all the bytes by now */
2099 if (PACKET_remaining(&subpkt)) {
2100 *al = TLS1_AD_DECODE_ERROR;
2103 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2104 unsigned int ellipticcurvelist_length;
2106 /* Each NamedCurve is 2 bytes and we must have at least 1 */
2107 if (!PACKET_get_net_2(&subpkt, &ellipticcurvelist_length)
2108 || ellipticcurvelist_length == 0
2109 || (ellipticcurvelist_length & 1) != 0)
2113 if (s->session->tlsext_ellipticcurvelist)
2116 s->session->tlsext_ellipticcurvelist_length = 0;
2117 if ((s->session->tlsext_ellipticcurvelist =
2118 OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
2119 *al = TLS1_AD_INTERNAL_ERROR;
2122 s->session->tlsext_ellipticcurvelist_length =
2123 ellipticcurvelist_length;
2124 if (!PACKET_copy_bytes(&subpkt,
2125 s->session->tlsext_ellipticcurvelist,
2126 ellipticcurvelist_length))
2128 } else if (!PACKET_forward(&subpkt, ellipticcurvelist_length)) {
2131 /* We should have consumed all the bytes by now */
2132 if (PACKET_remaining(&subpkt)) {
2136 #endif /* OPENSSL_NO_EC */
2137 else if (type == TLSEXT_TYPE_session_ticket) {
2138 if (!PACKET_forward(&subpkt, size)
2139 || (s->tls_session_ticket_ext_cb &&
2140 !s->tls_session_ticket_ext_cb(s, data, size,
2141 s->tls_session_ticket_ext_cb_arg))) {
2142 *al = TLS1_AD_INTERNAL_ERROR;
2145 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2148 if (s->s3->tmp.peer_sigalgs
2149 || !PACKET_get_net_2(&subpkt, &dsize)
2152 || !PACKET_get_bytes(&subpkt, &data, dsize)
2153 || PACKET_remaining(&subpkt) != 0
2154 || !tls1_save_sigalgs(s, data, dsize)) {
2157 } else if (type == TLSEXT_TYPE_status_request) {
2160 if (!PACKET_get_1(&subpkt,
2161 (unsigned int *)&s->tlsext_status_type))
2164 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2165 const unsigned char *sdata;
2167 /* Read in responder_id_list */
2168 if (!PACKET_get_net_2(&subpkt, &dsize)
2169 || !PACKET_get_sub_packet(&subpkt, &ssubpkt, dsize))
2172 while (PACKET_remaining(&ssubpkt)) {
2174 unsigned int idsize;
2176 if (PACKET_remaining(&ssubpkt) < 4
2177 || !PACKET_get_net_2(&ssubpkt, &idsize)
2178 || !PACKET_get_bytes(&ssubpkt, &data, idsize)) {
2183 id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
2186 if (data != sdata) {
2187 OCSP_RESPID_free(id);
2190 if (!s->tlsext_ocsp_ids
2191 && !(s->tlsext_ocsp_ids =
2192 sk_OCSP_RESPID_new_null())) {
2193 OCSP_RESPID_free(id);
2194 *al = SSL_AD_INTERNAL_ERROR;
2197 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2198 OCSP_RESPID_free(id);
2199 *al = SSL_AD_INTERNAL_ERROR;
2204 /* Read in request_extensions */
2205 if (!PACKET_get_net_2(&subpkt, &dsize)
2206 || !PACKET_get_bytes(&subpkt, &data, dsize)
2207 || PACKET_remaining(&subpkt)) {
2212 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2213 X509_EXTENSION_free);
2214 s->tlsext_ocsp_exts =
2215 d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
2216 if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
2221 * We don't know what to do with any other type * so ignore it.
2224 s->tlsext_status_type = -1;
2226 #ifndef OPENSSL_NO_HEARTBEATS
2227 else if (type == TLSEXT_TYPE_heartbeat) {
2228 unsigned int hbtype;
2230 if (!PACKET_get_1(&subpkt, &hbtype)
2231 || PACKET_remaining(&subpkt)) {
2232 *al = SSL_AD_DECODE_ERROR;
2236 case 0x01: /* Client allows us to send HB requests */
2237 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2239 case 0x02: /* Client doesn't accept HB requests */
2240 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2241 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2244 *al = SSL_AD_ILLEGAL_PARAMETER;
2249 #ifndef OPENSSL_NO_NEXTPROTONEG
2250 else if (type == TLSEXT_TYPE_next_proto_neg &&
2251 s->s3->tmp.finish_md_len == 0 &&
2252 s->s3->alpn_selected == NULL) {
2254 * We shouldn't accept this extension on a
2257 * s->new_session will be set on renegotiation, but we
2258 * probably shouldn't rely that it couldn't be set on
2259 * the initial renegotation too in certain cases (when
2260 * there's some other reason to disallow resuming an
2261 * earlier session -- the current code won't be doing
2262 * anything like that, but this might change).
2264 * A valid sign that there's been a previous handshake
2265 * in this connection is if s->s3->tmp.finish_md_len >
2266 * 0. (We are talking about a check that will happen
2267 * in the Hello protocol round, well before a new
2268 * Finished message could have been computed.)
2270 s->s3->next_proto_neg_seen = 1;
2274 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2275 s->ctx->alpn_select_cb && s->s3->tmp.finish_md_len == 0) {
2276 if (tls1_alpn_handle_client_hello(s, &subpkt, al) != 0)
2278 #ifndef OPENSSL_NO_NEXTPROTONEG
2279 /* ALPN takes precedence over NPN. */
2280 s->s3->next_proto_neg_seen = 0;
2284 /* session ticket processed earlier */
2285 #ifndef OPENSSL_NO_SRTP
2286 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2287 && type == TLSEXT_TYPE_use_srtp) {
2288 if (ssl_parse_clienthello_use_srtp_ext(s, &subpkt, al))
2292 #ifdef TLSEXT_TYPE_encrypt_then_mac
2293 else if (type == TLSEXT_TYPE_encrypt_then_mac)
2294 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2296 else if (type == TLSEXT_TYPE_extended_master_secret) {
2298 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2301 * If this ClientHello extension was unhandled and this is a
2302 * nonresumed connection, check whether the extension is a custom
2303 * TLS Extension (has a custom_srv_ext_record), and if so call the
2304 * callback and record the extension number so that an appropriate
2305 * ServerHello may be later returned.
2308 if (custom_ext_parse(s, 1, type, data, size, al) <= 0)
2313 /* Spurious data on the end */
2314 if (PACKET_remaining(pkt) != 0)
2319 /* Need RI if renegotiating */
2321 if (!renegotiate_seen && s->renegotiate &&
2322 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2323 *al = SSL_AD_HANDSHAKE_FAILURE;
2324 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2325 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2331 *al = SSL_AD_DECODE_ERROR;
2335 int ssl_parse_clienthello_tlsext(SSL *s, PACKET *pkt)
2338 custom_ext_init(&s->cert->srv_ext);
2339 if (ssl_scan_clienthello_tlsext(s, pkt, &al) <= 0) {
2340 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2344 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2345 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2351 #ifndef OPENSSL_NO_NEXTPROTONEG
2353 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2354 * elements of zero length are allowed and the set of elements must exactly
2355 * fill the length of the block.
2357 static char ssl_next_proto_validate(PACKET *pkt)
2361 while (PACKET_remaining(pkt)) {
2362 if (!PACKET_get_1(pkt, &len)
2363 || !PACKET_forward(pkt, len))
2371 static int ssl_scan_serverhello_tlsext(SSL *s, PACKET *pkt, int *al)
2373 unsigned int length, type, size;
2374 int tlsext_servername = 0;
2375 int renegotiate_seen = 0;
2377 #ifndef OPENSSL_NO_NEXTPROTONEG
2378 s->s3->next_proto_neg_seen = 0;
2380 s->tlsext_ticket_expected = 0;
2382 OPENSSL_free(s->s3->alpn_selected);
2383 s->s3->alpn_selected = NULL;
2384 #ifndef OPENSSL_NO_HEARTBEATS
2385 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2386 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2389 #ifdef TLSEXT_TYPE_encrypt_then_mac
2390 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
2393 if (!PACKET_get_net_2(pkt, &length))
2396 if (PACKET_remaining(pkt) != length) {
2397 *al = SSL_AD_DECODE_ERROR;
2401 while (PACKET_get_net_2(pkt, &type) && PACKET_get_net_2(pkt, &size)) {
2402 unsigned char *data;
2405 if (!PACKET_get_sub_packet(pkt, &spkt, size)
2406 || !PACKET_peek_bytes(&spkt, &data, size))
2409 if (s->tlsext_debug_cb)
2410 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2412 if (type == TLSEXT_TYPE_renegotiate) {
2413 if (!ssl_parse_serverhello_renegotiate_ext(s, &spkt, al))
2415 renegotiate_seen = 1;
2416 } else if (s->version == SSL3_VERSION) {
2417 } else if (type == TLSEXT_TYPE_server_name) {
2418 if (s->tlsext_hostname == NULL || size > 0) {
2419 *al = TLS1_AD_UNRECOGNIZED_NAME;
2422 tlsext_servername = 1;
2424 #ifndef OPENSSL_NO_EC
2425 else if (type == TLSEXT_TYPE_ec_point_formats) {
2426 unsigned int ecpointformatlist_length;
2427 if (!PACKET_get_1(&spkt, &ecpointformatlist_length)
2428 || ecpointformatlist_length != size - 1) {
2429 *al = TLS1_AD_DECODE_ERROR;
2433 s->session->tlsext_ecpointformatlist_length = 0;
2434 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2435 if ((s->session->tlsext_ecpointformatlist =
2436 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2437 *al = TLS1_AD_INTERNAL_ERROR;
2440 s->session->tlsext_ecpointformatlist_length =
2441 ecpointformatlist_length;
2442 if (!PACKET_copy_bytes(&spkt,
2443 s->session->tlsext_ecpointformatlist,
2444 ecpointformatlist_length)) {
2445 *al = TLS1_AD_DECODE_ERROR;
2451 #endif /* OPENSSL_NO_EC */
2453 else if (type == TLSEXT_TYPE_session_ticket) {
2454 if (s->tls_session_ticket_ext_cb &&
2455 !s->tls_session_ticket_ext_cb(s, data, size,
2456 s->tls_session_ticket_ext_cb_arg))
2458 *al = TLS1_AD_INTERNAL_ERROR;
2461 if (!tls_use_ticket(s) || (size > 0)) {
2462 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2465 s->tlsext_ticket_expected = 1;
2467 else if (type == TLSEXT_TYPE_status_request) {
2469 * MUST be empty and only sent if we've requested a status
2472 if ((s->tlsext_status_type == -1) || (size > 0)) {
2473 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2476 /* Set flag to expect CertificateStatus message */
2477 s->tlsext_status_expected = 1;
2479 #ifndef OPENSSL_NO_NEXTPROTONEG
2480 else if (type == TLSEXT_TYPE_next_proto_neg &&
2481 s->s3->tmp.finish_md_len == 0) {
2482 unsigned char *selected;
2483 unsigned char selected_len;
2484 /* We must have requested it. */
2485 if (s->ctx->next_proto_select_cb == NULL) {
2486 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2489 /* The data must be valid */
2490 if (!ssl_next_proto_validate(&spkt)) {
2491 *al = TLS1_AD_DECODE_ERROR;
2495 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2497 s->ctx->next_proto_select_cb_arg) !=
2498 SSL_TLSEXT_ERR_OK) {
2499 *al = TLS1_AD_INTERNAL_ERROR;
2502 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2503 if (s->next_proto_negotiated == NULL) {
2504 *al = TLS1_AD_INTERNAL_ERROR;
2507 memcpy(s->next_proto_negotiated, selected, selected_len);
2508 s->next_proto_negotiated_len = selected_len;
2509 s->s3->next_proto_neg_seen = 1;
2513 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2515 /* We must have requested it. */
2516 if (s->alpn_client_proto_list == NULL) {
2517 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2521 * The extension data consists of:
2522 * uint16 list_length
2523 * uint8 proto_length;
2524 * uint8 proto[proto_length];
2526 if (!PACKET_get_net_2(&spkt, &len)
2527 || PACKET_remaining(&spkt) != len
2528 || !PACKET_get_1(&spkt, &len)
2529 || PACKET_remaining(&spkt) != len) {
2530 *al = TLS1_AD_DECODE_ERROR;
2533 OPENSSL_free(s->s3->alpn_selected);
2534 s->s3->alpn_selected = OPENSSL_malloc(len);
2535 if (s->s3->alpn_selected == NULL) {
2536 *al = TLS1_AD_INTERNAL_ERROR;
2539 if (!PACKET_copy_bytes(&spkt, s->s3->alpn_selected, len)) {
2540 *al = TLS1_AD_DECODE_ERROR;
2543 s->s3->alpn_selected_len = len;
2545 #ifndef OPENSSL_NO_HEARTBEATS
2546 else if (type == TLSEXT_TYPE_heartbeat) {
2547 unsigned int hbtype;
2548 if (!PACKET_get_1(&spkt, &hbtype)) {
2549 *al = SSL_AD_DECODE_ERROR;
2553 case 0x01: /* Server allows us to send HB requests */
2554 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2556 case 0x02: /* Server doesn't accept HB requests */
2557 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2558 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2561 *al = SSL_AD_ILLEGAL_PARAMETER;
2566 #ifndef OPENSSL_NO_SRTP
2567 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2568 if (ssl_parse_serverhello_use_srtp_ext(s, &spkt, al))
2572 #ifdef TLSEXT_TYPE_encrypt_then_mac
2573 else if (type == TLSEXT_TYPE_encrypt_then_mac) {
2574 /* Ignore if inappropriate ciphersuite */
2575 if (s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
2576 && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4)
2577 s->s3->flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC;
2580 else if (type == TLSEXT_TYPE_extended_master_secret) {
2582 s->session->flags |= SSL_SESS_FLAG_EXTMS;
2585 * If this extension type was not otherwise handled, but matches a
2586 * custom_cli_ext_record, then send it to the c callback
2588 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2592 if (PACKET_remaining(pkt) != 0) {
2593 *al = SSL_AD_DECODE_ERROR;
2597 if (!s->hit && tlsext_servername == 1) {
2598 if (s->tlsext_hostname) {
2599 if (s->session->tlsext_hostname == NULL) {
2600 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2601 if (!s->session->tlsext_hostname) {
2602 *al = SSL_AD_UNRECOGNIZED_NAME;
2606 *al = SSL_AD_DECODE_ERROR;
2615 * Determine if we need to see RI. Strictly speaking if we want to avoid
2616 * an attack we should *always* see RI even on initial server hello
2617 * because the client doesn't see any renegotiation during an attack.
2618 * However this would mean we could not connect to any server which
2619 * doesn't support RI so for the immediate future tolerate RI absence on
2620 * initial connect only.
2622 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2623 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2624 *al = SSL_AD_HANDSHAKE_FAILURE;
2625 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2626 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2633 int ssl_prepare_clienthello_tlsext(SSL *s)
2639 int ssl_prepare_serverhello_tlsext(SSL *s)
2644 static int ssl_check_clienthello_tlsext_early(SSL *s)
2646 int ret = SSL_TLSEXT_ERR_NOACK;
2647 int al = SSL_AD_UNRECOGNIZED_NAME;
2649 #ifndef OPENSSL_NO_EC
2651 * The handling of the ECPointFormats extension is done elsewhere, namely
2652 * in ssl3_choose_cipher in s3_lib.c.
2655 * The handling of the EllipticCurves extension is done elsewhere, namely
2656 * in ssl3_choose_cipher in s3_lib.c.
2660 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2662 s->ctx->tlsext_servername_callback(s, &al,
2663 s->ctx->tlsext_servername_arg);
2664 else if (s->initial_ctx != NULL
2665 && s->initial_ctx->tlsext_servername_callback != 0)
2667 s->initial_ctx->tlsext_servername_callback(s, &al,
2669 initial_ctx->tlsext_servername_arg);
2672 case SSL_TLSEXT_ERR_ALERT_FATAL:
2673 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2676 case SSL_TLSEXT_ERR_ALERT_WARNING:
2677 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2680 case SSL_TLSEXT_ERR_NOACK:
2681 s->servername_done = 0;
2686 /* Initialise digests to default values */
2687 void ssl_set_default_md(SSL *s)
2689 const EVP_MD **pmd = s->s3->tmp.md;
2690 #ifndef OPENSSL_NO_DSA
2691 pmd[SSL_PKEY_DSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2693 #ifndef OPENSSL_NO_RSA
2694 if (SSL_USE_SIGALGS(s))
2695 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_SHA1_IDX);
2697 pmd[SSL_PKEY_RSA_SIGN] = ssl_md(SSL_MD_MD5_SHA1_IDX);
2698 pmd[SSL_PKEY_RSA_ENC] = pmd[SSL_PKEY_RSA_SIGN];
2700 #ifndef OPENSSL_NO_EC
2701 pmd[SSL_PKEY_ECC] = ssl_md(SSL_MD_SHA1_IDX);
2703 #ifndef OPENSSL_NO_GOST
2704 pmd[SSL_PKEY_GOST01] = ssl_md(SSL_MD_GOST94_IDX);
2705 pmd[SSL_PKEY_GOST12_256] = ssl_md(SSL_MD_GOST12_256_IDX);
2706 pmd[SSL_PKEY_GOST12_512] = ssl_md(SSL_MD_GOST12_512_IDX);
2710 int tls1_set_server_sigalgs(SSL *s)
2714 /* Clear any shared sigtnature algorithms */
2715 OPENSSL_free(s->cert->shared_sigalgs);
2716 s->cert->shared_sigalgs = NULL;
2717 s->cert->shared_sigalgslen = 0;
2718 /* Clear certificate digests and validity flags */
2719 for (i = 0; i < SSL_PKEY_NUM; i++) {
2720 s->s3->tmp.md[i] = NULL;
2721 s->s3->tmp.valid_flags[i] = 0;
2724 /* If sigalgs received process it. */
2725 if (s->s3->tmp.peer_sigalgs) {
2726 if (!tls1_process_sigalgs(s)) {
2727 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
2728 al = SSL_AD_INTERNAL_ERROR;
2731 /* Fatal error is no shared signature algorithms */
2732 if (!s->cert->shared_sigalgs) {
2733 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
2734 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
2735 al = SSL_AD_ILLEGAL_PARAMETER;
2739 ssl_set_default_md(s);
2743 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2747 int ssl_check_clienthello_tlsext_late(SSL *s)
2749 int ret = SSL_TLSEXT_ERR_OK;
2750 int al = SSL_AD_INTERNAL_ERROR;
2753 * If status request then ask callback what to do. Note: this must be
2754 * called after servername callbacks in case the certificate has changed,
2755 * and must be called after the cipher has been chosen because this may
2756 * influence which certificate is sent
2758 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
2760 CERT_PKEY *certpkey;
2761 certpkey = ssl_get_server_send_pkey(s);
2762 /* If no certificate can't return certificate status */
2763 if (certpkey == NULL) {
2764 s->tlsext_status_expected = 0;
2768 * Set current certificate to one we will use so SSL_get_certificate
2769 * et al can pick it up.
2771 s->cert->key = certpkey;
2772 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2774 /* We don't want to send a status request response */
2775 case SSL_TLSEXT_ERR_NOACK:
2776 s->tlsext_status_expected = 0;
2778 /* status request response should be sent */
2779 case SSL_TLSEXT_ERR_OK:
2780 if (s->tlsext_ocsp_resp)
2781 s->tlsext_status_expected = 1;
2783 s->tlsext_status_expected = 0;
2785 /* something bad happened */
2786 case SSL_TLSEXT_ERR_ALERT_FATAL:
2787 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2788 al = SSL_AD_INTERNAL_ERROR;
2792 s->tlsext_status_expected = 0;
2796 case SSL_TLSEXT_ERR_ALERT_FATAL:
2797 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2800 case SSL_TLSEXT_ERR_ALERT_WARNING:
2801 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2809 int ssl_check_serverhello_tlsext(SSL *s)
2811 int ret = SSL_TLSEXT_ERR_NOACK;
2812 int al = SSL_AD_UNRECOGNIZED_NAME;
2814 #ifndef OPENSSL_NO_EC
2816 * If we are client and using an elliptic curve cryptography cipher
2817 * suite, then if server returns an EC point formats lists extension it
2818 * must contain uncompressed.
2820 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2821 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2822 if ((s->tlsext_ecpointformatlist != NULL)
2823 && (s->tlsext_ecpointformatlist_length > 0)
2824 && (s->session->tlsext_ecpointformatlist != NULL)
2825 && (s->session->tlsext_ecpointformatlist_length > 0)
2826 && ((alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe))
2827 || (alg_a & SSL_aECDSA))) {
2828 /* we are using an ECC cipher */
2830 unsigned char *list;
2831 int found_uncompressed = 0;
2832 list = s->session->tlsext_ecpointformatlist;
2833 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2834 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2835 found_uncompressed = 1;
2839 if (!found_uncompressed) {
2840 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2841 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2845 ret = SSL_TLSEXT_ERR_OK;
2846 #endif /* OPENSSL_NO_EC */
2848 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2850 s->ctx->tlsext_servername_callback(s, &al,
2851 s->ctx->tlsext_servername_arg);
2852 else if (s->initial_ctx != NULL
2853 && s->initial_ctx->tlsext_servername_callback != 0)
2855 s->initial_ctx->tlsext_servername_callback(s, &al,
2857 initial_ctx->tlsext_servername_arg);
2860 * If we've requested certificate status and we wont get one tell the
2863 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2864 && s->ctx && s->ctx->tlsext_status_cb) {
2867 * Set resp to NULL, resplen to -1 so callback knows there is no
2870 OPENSSL_free(s->tlsext_ocsp_resp);
2871 s->tlsext_ocsp_resp = NULL;
2872 s->tlsext_ocsp_resplen = -1;
2873 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2875 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2876 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2879 al = SSL_AD_INTERNAL_ERROR;
2880 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2885 case SSL_TLSEXT_ERR_ALERT_FATAL:
2886 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2889 case SSL_TLSEXT_ERR_ALERT_WARNING:
2890 ssl3_send_alert(s, SSL3_AL_WARNING, al);
2893 case SSL_TLSEXT_ERR_NOACK:
2894 s->servername_done = 0;
2900 int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt)
2903 if (s->version < SSL3_VERSION)
2905 if (ssl_scan_serverhello_tlsext(s, pkt, &al) <= 0) {
2906 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2910 if (ssl_check_serverhello_tlsext(s) <= 0) {
2911 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
2918 * Since the server cache lookup is done early on in the processing of the
2919 * ClientHello, and other operations depend on the result, we need to handle
2920 * any TLS session ticket extension at the same time.
2922 * session_id: ClientHello session ID.
2923 * ext: ClientHello extensions (including length prefix)
2924 * ret: (output) on return, if a ticket was decrypted, then this is set to
2925 * point to the resulting session.
2927 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2928 * ciphersuite, in which case we have no use for session tickets and one will
2929 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2932 * -1: fatal error, either from parsing or decrypting the ticket.
2933 * 0: no ticket was found (or was ignored, based on settings).
2934 * 1: a zero length extension was found, indicating that the client supports
2935 * session tickets but doesn't currently have one to offer.
2936 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
2937 * couldn't be decrypted because of a non-fatal error.
2938 * 3: a ticket was successfully decrypted and *ret was set.
2941 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2942 * a new session ticket to the client because the client indicated support
2943 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2944 * a session ticket or we couldn't use the one it gave us, or if
2945 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2946 * Otherwise, s->tlsext_ticket_expected is set to 0.
2948 int tls1_process_ticket(SSL *s, const PACKET *ext, const PACKET *session_id,
2952 PACKET local_ext = *ext;
2956 s->tlsext_ticket_expected = 0;
2959 * If tickets disabled behave as if no ticket present to permit stateful
2962 if (!tls_use_ticket(s))
2964 if ((s->version <= SSL3_VERSION))
2967 if (!PACKET_get_net_2(&local_ext, &i)) {
2971 while (PACKET_remaining(&local_ext) >= 4) {
2972 unsigned int type, size;
2974 if (!PACKET_get_net_2(&local_ext, &type)
2975 || !PACKET_get_net_2(&local_ext, &size)) {
2976 /* Shouldn't ever happen */
2980 if (PACKET_remaining(&local_ext) < size) {
2984 if (type == TLSEXT_TYPE_session_ticket) {
2986 unsigned char *etick;
2990 * The client will accept a ticket but doesn't currently have
2993 s->tlsext_ticket_expected = 1;
2997 if (s->tls_session_secret_cb) {
2999 * Indicate that the ticket couldn't be decrypted rather than
3000 * generating the session from ticket now, trigger
3001 * abbreviated handshake based on external mechanism to
3002 * calculate the master secret later.
3007 if (!PACKET_get_bytes(&local_ext, &etick, size)) {
3008 /* Shouldn't ever happen */
3012 r = tls_decrypt_ticket(s, etick, size, PACKET_data(session_id),
3013 PACKET_remaining(session_id), ret);
3015 case 2: /* ticket couldn't be decrypted */
3016 s->tlsext_ticket_expected = 1;
3019 case 3: /* ticket was decrypted */
3022 case 4: /* ticket decrypted but need to renew */
3023 s->tlsext_ticket_expected = 1;
3026 default: /* fatal error */
3032 if (!PACKET_forward(&local_ext, size)) {
3044 * tls_decrypt_ticket attempts to decrypt a session ticket.
3046 * etick: points to the body of the session ticket extension.
3047 * eticklen: the length of the session tickets extenion.
3048 * sess_id: points at the session ID.
3049 * sesslen: the length of the session ID.
3050 * psess: (output) on return, if a ticket was decrypted, then this is set to
3051 * point to the resulting session.
3054 * -1: fatal error, either from parsing or decrypting the ticket.
3055 * 2: the ticket couldn't be decrypted.
3056 * 3: a ticket was successfully decrypted and *psess was set.
3057 * 4: same as 3, but the ticket needs to be renewed.
3059 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3060 int eticklen, const unsigned char *sess_id,
3061 int sesslen, SSL_SESSION **psess)
3064 unsigned char *sdec;
3065 const unsigned char *p;
3066 int slen, mlen, renew_ticket = 0;
3067 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3070 SSL_CTX *tctx = s->initial_ctx;
3071 /* Need at least keyname + iv + some encrypted data */
3074 /* Initialize session ticket encryption and HMAC contexts */
3075 HMAC_CTX_init(&hctx);
3076 EVP_CIPHER_CTX_init(&ctx);
3077 if (tctx->tlsext_ticket_key_cb) {
3078 unsigned char *nctick = (unsigned char *)etick;
3079 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3088 /* Check key name matches */
3089 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3091 if (HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3092 EVP_sha256(), NULL) <= 0
3093 || EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3094 tctx->tlsext_tick_aes_key,
3100 * Attempt to process session ticket, first conduct sanity and integrity
3103 mlen = HMAC_size(&hctx);
3108 /* Check HMAC of encrypted ticket */
3109 if (HMAC_Update(&hctx, etick, eticklen) <= 0
3110 || HMAC_Final(&hctx, tick_hmac, NULL) <= 0) {
3113 HMAC_CTX_cleanup(&hctx);
3114 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3115 EVP_CIPHER_CTX_cleanup(&ctx);
3118 /* Attempt to decrypt session data */
3119 /* Move p after IV to start of encrypted ticket, update length */
3120 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3121 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3122 sdec = OPENSSL_malloc(eticklen);
3124 || EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen) <= 0) {
3125 EVP_CIPHER_CTX_cleanup(&ctx);
3128 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
3129 EVP_CIPHER_CTX_cleanup(&ctx);
3134 EVP_CIPHER_CTX_cleanup(&ctx);
3137 sess = d2i_SSL_SESSION(NULL, &p, slen);
3141 * The session ID, if non-empty, is used by some clients to detect
3142 * that the ticket has been accepted. So we copy it to the session
3143 * structure. If it is empty set length to zero as required by
3147 memcpy(sess->session_id, sess_id, sesslen);
3148 sess->session_id_length = sesslen;
3157 * For session parse failure, indicate that we need to send a new ticket.
3161 EVP_CIPHER_CTX_cleanup(&ctx);
3162 HMAC_CTX_cleanup(&hctx);
3166 /* Tables to translate from NIDs to TLS v1.2 ids */
3173 static const tls12_lookup tls12_md[] = {
3174 {NID_md5, TLSEXT_hash_md5},
3175 {NID_sha1, TLSEXT_hash_sha1},
3176 {NID_sha224, TLSEXT_hash_sha224},
3177 {NID_sha256, TLSEXT_hash_sha256},
3178 {NID_sha384, TLSEXT_hash_sha384},
3179 {NID_sha512, TLSEXT_hash_sha512},
3180 {NID_id_GostR3411_94, TLSEXT_hash_gostr3411},
3181 {NID_id_GostR3411_2012_256, TLSEXT_hash_gostr34112012_256},
3182 {NID_id_GostR3411_2012_512, TLSEXT_hash_gostr34112012_512},
3185 static const tls12_lookup tls12_sig[] = {
3186 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3187 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3188 {EVP_PKEY_EC, TLSEXT_signature_ecdsa},
3189 {NID_id_GostR3410_2001, TLSEXT_signature_gostr34102001},
3190 {NID_id_GostR3410_2012_256, TLSEXT_signature_gostr34102012_256},
3191 {NID_id_GostR3410_2012_512, TLSEXT_signature_gostr34102012_512}
3194 static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen)
3197 for (i = 0; i < tlen; i++) {
3198 if (table[i].nid == nid)
3204 static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen)
3207 for (i = 0; i < tlen; i++) {
3208 if ((table[i].id) == id)
3209 return table[i].nid;
3214 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3220 md_id = tls12_find_id(EVP_MD_type(md), tls12_md, OSSL_NELEM(tls12_md));
3223 sig_id = tls12_get_sigid(pk);
3226 p[0] = (unsigned char)md_id;
3227 p[1] = (unsigned char)sig_id;
3231 int tls12_get_sigid(const EVP_PKEY *pk)
3233 return tls12_find_id(pk->type, tls12_sig, OSSL_NELEM(tls12_sig));
3240 unsigned char tlsext_hash;
3243 static const tls12_hash_info tls12_md_info[] = {
3244 {NID_md5, 64, SSL_MD_MD5_IDX, TLSEXT_hash_md5},
3245 {NID_sha1, 80, SSL_MD_SHA1_IDX, TLSEXT_hash_sha1},
3246 {NID_sha224, 112, SSL_MD_SHA224_IDX, TLSEXT_hash_sha224},
3247 {NID_sha256, 128, SSL_MD_SHA256_IDX, TLSEXT_hash_sha256},
3248 {NID_sha384, 192, SSL_MD_SHA384_IDX, TLSEXT_hash_sha384},
3249 {NID_sha512, 256, SSL_MD_SHA512_IDX, TLSEXT_hash_sha512},
3250 {NID_id_GostR3411_94, 128, SSL_MD_GOST94_IDX, TLSEXT_hash_gostr3411},
3251 {NID_id_GostR3411_2012_256, 128, SSL_MD_GOST12_256_IDX, TLSEXT_hash_gostr34112012_256},
3252 {NID_id_GostR3411_2012_512, 256, SSL_MD_GOST12_512_IDX, TLSEXT_hash_gostr34112012_512},
3255 static const tls12_hash_info *tls12_get_hash_info(unsigned char hash_alg)
3261 for (i=0; i < OSSL_NELEM(tls12_md_info); i++)
3263 if (tls12_md_info[i].tlsext_hash == hash_alg)
3264 return tls12_md_info + i;
3270 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3272 const tls12_hash_info *inf;
3273 if (hash_alg == TLSEXT_hash_md5 && FIPS_mode())
3275 inf = tls12_get_hash_info(hash_alg);
3278 return ssl_md(inf->md_idx);
3281 static int tls12_get_pkey_idx(unsigned char sig_alg)
3284 #ifndef OPENSSL_NO_RSA
3285 case TLSEXT_signature_rsa:
3286 return SSL_PKEY_RSA_SIGN;
3288 #ifndef OPENSSL_NO_DSA
3289 case TLSEXT_signature_dsa:
3290 return SSL_PKEY_DSA_SIGN;
3292 #ifndef OPENSSL_NO_EC
3293 case TLSEXT_signature_ecdsa:
3294 return SSL_PKEY_ECC;
3296 # ifndef OPENSSL_NO_GOST
3297 case TLSEXT_signature_gostr34102001:
3298 return SSL_PKEY_GOST01;
3300 case TLSEXT_signature_gostr34102012_256:
3301 return SSL_PKEY_GOST12_256;
3303 case TLSEXT_signature_gostr34102012_512:
3304 return SSL_PKEY_GOST12_512;
3310 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3311 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3312 int *psignhash_nid, const unsigned char *data)
3314 int sign_nid = 0, hash_nid = 0;
3315 if (!phash_nid && !psign_nid && !psignhash_nid)
3317 if (phash_nid || psignhash_nid) {
3318 hash_nid = tls12_find_nid(data[0], tls12_md, OSSL_NELEM(tls12_md));
3320 *phash_nid = hash_nid;
3322 if (psign_nid || psignhash_nid) {
3323 sign_nid = tls12_find_nid(data[1], tls12_sig, OSSL_NELEM(tls12_sig));
3325 *psign_nid = sign_nid;
3327 if (psignhash_nid) {
3328 if (sign_nid && hash_nid)
3329 OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
3331 *psignhash_nid = NID_undef;
3335 /* Check to see if a signature algorithm is allowed */
3336 static int tls12_sigalg_allowed(SSL *s, int op, const unsigned char *ptmp)
3338 /* See if we have an entry in the hash table and it is enabled */
3339 const tls12_hash_info *hinf = tls12_get_hash_info(ptmp[0]);
3340 if (hinf == NULL || ssl_md(hinf->md_idx) == NULL)
3342 /* See if public key algorithm allowed */
3343 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3345 /* Finally see if security callback allows it */
3346 return ssl_security(s, op, hinf->secbits, hinf->nid, (void *)ptmp);
3350 * Get a mask of disabled public key algorithms based on supported signature
3351 * algorithms. For example if no signature algorithm supports RSA then RSA is
3355 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
3357 const unsigned char *sigalgs;
3358 size_t i, sigalgslen;
3359 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
3361 * Now go through all signature algorithms seeing if we support any for
3362 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2. To keep
3363 * down calls to security callback only check if we have to.
3365 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
3366 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
3367 switch (sigalgs[1]) {
3368 #ifndef OPENSSL_NO_RSA
3369 case TLSEXT_signature_rsa:
3370 if (!have_rsa && tls12_sigalg_allowed(s, op, sigalgs))
3374 #ifndef OPENSSL_NO_DSA
3375 case TLSEXT_signature_dsa:
3376 if (!have_dsa && tls12_sigalg_allowed(s, op, sigalgs))
3380 #ifndef OPENSSL_NO_EC
3381 case TLSEXT_signature_ecdsa:
3382 if (!have_ecdsa && tls12_sigalg_allowed(s, op, sigalgs))
3389 *pmask_a |= SSL_aRSA;
3391 *pmask_a |= SSL_aDSS;
3393 *pmask_a |= SSL_aECDSA;
3396 size_t tls12_copy_sigalgs(SSL *s, unsigned char *out,
3397 const unsigned char *psig, size_t psiglen)
3399 unsigned char *tmpout = out;
3401 for (i = 0; i < psiglen; i += 2, psig += 2) {
3402 if (tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, psig)) {
3403 *tmpout++ = psig[0];
3404 *tmpout++ = psig[1];
3407 return tmpout - out;
3410 /* Given preference and allowed sigalgs set shared sigalgs */
3411 static int tls12_shared_sigalgs(SSL *s, TLS_SIGALGS *shsig,
3412 const unsigned char *pref, size_t preflen,
3413 const unsigned char *allow, size_t allowlen)
3415 const unsigned char *ptmp, *atmp;
3416 size_t i, j, nmatch = 0;
3417 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3418 /* Skip disabled hashes or signature algorithms */
3419 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, ptmp))
3421 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3422 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3425 shsig->rhash = ptmp[0];
3426 shsig->rsign = ptmp[1];
3427 tls1_lookup_sigalg(&shsig->hash_nid,
3429 &shsig->signandhash_nid, ptmp);
3439 /* Set shared signature algorithms for SSL structures */
3440 static int tls1_set_shared_sigalgs(SSL *s)
3442 const unsigned char *pref, *allow, *conf;
3443 size_t preflen, allowlen, conflen;
3445 TLS_SIGALGS *salgs = NULL;
3447 unsigned int is_suiteb = tls1_suiteb(s);
3449 OPENSSL_free(c->shared_sigalgs);
3450 c->shared_sigalgs = NULL;
3451 c->shared_sigalgslen = 0;
3452 /* If client use client signature algorithms if not NULL */
3453 if (!s->server && c->client_sigalgs && !is_suiteb) {
3454 conf = c->client_sigalgs;
3455 conflen = c->client_sigalgslen;
3456 } else if (c->conf_sigalgs && !is_suiteb) {
3457 conf = c->conf_sigalgs;
3458 conflen = c->conf_sigalgslen;
3460 conflen = tls12_get_psigalgs(s, &conf);
3461 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3464 allow = s->s3->tmp.peer_sigalgs;
3465 allowlen = s->s3->tmp.peer_sigalgslen;
3469 pref = s->s3->tmp.peer_sigalgs;
3470 preflen = s->s3->tmp.peer_sigalgslen;
3472 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
3474 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3477 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
3481 c->shared_sigalgs = salgs;
3482 c->shared_sigalgslen = nmatch;
3486 /* Set preferred digest for each key type */
3488 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3491 /* Extension ignored for inappropriate versions */
3492 if (!SSL_USE_SIGALGS(s))
3494 /* Should never happen */
3498 OPENSSL_free(s->s3->tmp.peer_sigalgs);
3499 s->s3->tmp.peer_sigalgs = OPENSSL_malloc(dsize);
3500 if (s->s3->tmp.peer_sigalgs == NULL)
3502 s->s3->tmp.peer_sigalgslen = dsize;
3503 memcpy(s->s3->tmp.peer_sigalgs, data, dsize);
3507 int tls1_process_sigalgs(SSL *s)
3512 const EVP_MD **pmd = s->s3->tmp.md;
3513 uint32_t *pvalid = s->s3->tmp.valid_flags;
3515 TLS_SIGALGS *sigptr;
3516 if (!tls1_set_shared_sigalgs(s))
3519 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3520 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
3522 * Use first set signature preference to force message digest,
3523 * ignoring any peer preferences.
3525 const unsigned char *sigs = NULL;
3527 sigs = c->conf_sigalgs;
3529 sigs = c->client_sigalgs;
3531 idx = tls12_get_pkey_idx(sigs[1]);
3532 md = tls12_get_hash(sigs[0]);
3534 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3535 if (idx == SSL_PKEY_RSA_SIGN) {
3536 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3537 pmd[SSL_PKEY_RSA_ENC] = md;
3543 for (i = 0, sigptr = c->shared_sigalgs;
3544 i < c->shared_sigalgslen; i++, sigptr++) {
3545 idx = tls12_get_pkey_idx(sigptr->rsign);
3546 if (idx > 0 && pmd[idx] == NULL) {
3547 md = tls12_get_hash(sigptr->rhash);
3549 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN;
3550 if (idx == SSL_PKEY_RSA_SIGN) {
3551 pvalid[SSL_PKEY_RSA_ENC] = CERT_PKEY_EXPLICIT_SIGN;
3552 pmd[SSL_PKEY_RSA_ENC] = md;
3558 * In strict mode leave unset digests as NULL to indicate we can't use
3559 * the certificate for signing.
3561 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3563 * Set any remaining keys to default values. NOTE: if alg is not
3564 * supported it stays as NULL.
3566 #ifndef OPENSSL_NO_DSA
3567 if (pmd[SSL_PKEY_DSA_SIGN] == NULL)
3568 pmd[SSL_PKEY_DSA_SIGN] = EVP_sha1();
3570 #ifndef OPENSSL_NO_RSA
3571 if (pmd[SSL_PKEY_RSA_SIGN] == NULL) {
3572 pmd[SSL_PKEY_RSA_SIGN] = EVP_sha1();
3573 pmd[SSL_PKEY_RSA_ENC] = EVP_sha1();
3576 #ifndef OPENSSL_NO_EC
3577 if (pmd[SSL_PKEY_ECC] == NULL)
3578 pmd[SSL_PKEY_ECC] = EVP_sha1();
3580 # ifndef OPENSSL_NO_GOST
3581 if (pmd[SSL_PKEY_GOST01] == NULL)
3582 pmd[SSL_PKEY_GOST01] = EVP_get_digestbynid(NID_id_GostR3411_94);
3583 if (pmd[SSL_PKEY_GOST12_256] == NULL)
3584 pmd[SSL_PKEY_GOST12_256] = EVP_get_digestbynid(NID_id_GostR3411_2012_256);
3585 if (pmd[SSL_PKEY_GOST12_512] == NULL)
3586 pmd[SSL_PKEY_GOST12_512] = EVP_get_digestbynid(NID_id_GostR3411_2012_512);
3592 int SSL_get_sigalgs(SSL *s, int idx,
3593 int *psign, int *phash, int *psignhash,
3594 unsigned char *rsig, unsigned char *rhash)
3596 const unsigned char *psig = s->s3->tmp.peer_sigalgs;
3601 if (idx >= (int)s->s3->tmp.peer_sigalgslen)
3608 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3610 return s->s3->tmp.peer_sigalgslen / 2;
3613 int SSL_get_shared_sigalgs(SSL *s, int idx,
3614 int *psign, int *phash, int *psignhash,
3615 unsigned char *rsig, unsigned char *rhash)
3617 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3618 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3622 *phash = shsigalgs->hash_nid;
3624 *psign = shsigalgs->sign_nid;
3626 *psignhash = shsigalgs->signandhash_nid;
3628 *rsig = shsigalgs->rsign;
3630 *rhash = shsigalgs->rhash;
3631 return s->cert->shared_sigalgslen;
3634 #ifndef OPENSSL_NO_HEARTBEATS
3635 int tls1_process_heartbeat(SSL *s, unsigned char *p, unsigned int length)
3638 unsigned short hbtype;
3639 unsigned int payload;
3640 unsigned int padding = 16; /* Use minimum padding */
3642 if (s->msg_callback)
3643 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
3645 s, s->msg_callback_arg);
3647 /* Read type and payload length first */
3648 if (1 + 2 + 16 > length)
3649 return 0; /* silently discard */
3652 if (1 + 2 + payload + 16 > length)
3653 return 0; /* silently discard per RFC 6520 sec. 4 */
3656 if (hbtype == TLS1_HB_REQUEST) {
3657 unsigned char *buffer, *bp;
3661 * Allocate memory for the response, size is 1 bytes message type,
3662 * plus 2 bytes payload length, plus payload, plus padding
3664 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
3665 if (buffer == NULL) {
3666 SSLerr(SSL_F_TLS1_PROCESS_HEARTBEAT, ERR_R_MALLOC_FAILURE);
3671 /* Enter response type, length and copy payload */
3672 *bp++ = TLS1_HB_RESPONSE;
3674 memcpy(bp, pl, payload);
3676 /* Random padding */
3677 if (RAND_bytes(bp, padding) <= 0) {
3678 OPENSSL_free(buffer);
3682 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,
3683 3 + payload + padding);
3685 if (r >= 0 && s->msg_callback)
3686 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3687 buffer, 3 + payload + padding,
3688 s, s->msg_callback_arg);
3690 OPENSSL_free(buffer);
3694 } else if (hbtype == TLS1_HB_RESPONSE) {
3698 * We only send sequence numbers (2 bytes unsigned int), and 16
3699 * random bytes, so we just try to read the sequence number
3703 if (payload == 18 && seq == s->tlsext_hb_seq) {
3705 s->tlsext_hb_pending = 0;
3712 int tls1_heartbeat(SSL *s)
3714 unsigned char *buf, *p;
3716 unsigned int payload = 18; /* Sequence number + random bytes */
3717 unsigned int padding = 16; /* Use minimum padding */
3719 /* Only send if peer supports and accepts HB requests... */
3720 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
3721 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
3722 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
3726 /* ...and there is none in flight yet... */
3727 if (s->tlsext_hb_pending) {
3728 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
3732 /* ...and no handshake in progress. */
3733 if (SSL_in_init(s) || ossl_statem_get_in_handshake(s)) {
3734 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
3739 * Create HeartBeat message, we just use a sequence number
3740 * as payload to distuingish different messages and add
3741 * some random stuff.
3742 * - Message Type, 1 byte
3743 * - Payload Length, 2 bytes (unsigned int)
3744 * - Payload, the sequence number (2 bytes uint)
3745 * - Payload, random bytes (16 bytes uint)
3748 buf = OPENSSL_malloc(1 + 2 + payload + padding);
3750 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_MALLOC_FAILURE);
3755 *p++ = TLS1_HB_REQUEST;
3756 /* Payload length (18 bytes here) */
3758 /* Sequence number */
3759 s2n(s->tlsext_hb_seq, p);
3760 /* 16 random bytes */
3761 if (RAND_bytes(p, 16) <= 0) {
3762 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
3766 /* Random padding */
3767 if (RAND_bytes(p, padding) <= 0) {
3768 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
3772 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
3774 if (s->msg_callback)
3775 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
3776 buf, 3 + payload + padding,
3777 s, s->msg_callback_arg);
3779 s->tlsext_hb_pending = 1;
3788 #define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
3792 int sigalgs[MAX_SIGALGLEN];
3795 static void get_sigorhash(int *psig, int *phash, const char *str)
3797 if (strcmp(str, "RSA") == 0) {
3798 *psig = EVP_PKEY_RSA;
3799 } else if (strcmp(str, "DSA") == 0) {
3800 *psig = EVP_PKEY_DSA;
3801 } else if (strcmp(str, "ECDSA") == 0) {
3802 *psig = EVP_PKEY_EC;
3804 *phash = OBJ_sn2nid(str);
3805 if (*phash == NID_undef)
3806 *phash = OBJ_ln2nid(str);
3810 static int sig_cb(const char *elem, int len, void *arg)
3812 sig_cb_st *sarg = arg;
3815 int sig_alg = NID_undef, hash_alg = NID_undef;
3818 if (sarg->sigalgcnt == MAX_SIGALGLEN)
3820 if (len > (int)(sizeof(etmp) - 1))
3822 memcpy(etmp, elem, len);
3824 p = strchr(etmp, '+');
3832 get_sigorhash(&sig_alg, &hash_alg, etmp);
3833 get_sigorhash(&sig_alg, &hash_alg, p);
3835 if (sig_alg == NID_undef || hash_alg == NID_undef)
3838 for (i = 0; i < sarg->sigalgcnt; i += 2) {
3839 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
3842 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
3843 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
3848 * Set suppored signature algorithms based on a colon separated list of the
3849 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
3851 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
3855 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
3859 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
3862 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
3865 unsigned char *sigalgs, *sptr;
3870 sigalgs = OPENSSL_malloc(salglen);
3871 if (sigalgs == NULL)
3873 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
3874 rhash = tls12_find_id(*psig_nids++, tls12_md, OSSL_NELEM(tls12_md));
3875 rsign = tls12_find_id(*psig_nids++, tls12_sig, OSSL_NELEM(tls12_sig));
3877 if (rhash == -1 || rsign == -1)
3884 OPENSSL_free(c->client_sigalgs);
3885 c->client_sigalgs = sigalgs;
3886 c->client_sigalgslen = salglen;
3888 OPENSSL_free(c->conf_sigalgs);
3889 c->conf_sigalgs = sigalgs;
3890 c->conf_sigalgslen = salglen;
3896 OPENSSL_free(sigalgs);
3900 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
3904 if (default_nid == -1)
3906 sig_nid = X509_get_signature_nid(x);
3908 return sig_nid == default_nid ? 1 : 0;
3909 for (i = 0; i < c->shared_sigalgslen; i++)
3910 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
3915 /* Check to see if a certificate issuer name matches list of CA names */
3916 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
3920 nm = X509_get_issuer_name(x);
3921 for (i = 0; i < sk_X509_NAME_num(names); i++) {
3922 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
3929 * Check certificate chain is consistent with TLS extensions and is usable by
3930 * server. This servers two purposes: it allows users to check chains before
3931 * passing them to the server and it allows the server to check chains before
3932 * attempting to use them.
3935 /* Flags which need to be set for a certificate when stict mode not set */
3937 #define CERT_PKEY_VALID_FLAGS \
3938 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
3939 /* Strict mode flags */
3940 #define CERT_PKEY_STRICT_FLAGS \
3941 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
3942 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
3944 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
3949 int check_flags = 0, strict_mode;
3950 CERT_PKEY *cpk = NULL;
3953 unsigned int suiteb_flags = tls1_suiteb(s);
3954 /* idx == -1 means checking server chains */
3956 /* idx == -2 means checking client certificate chains */
3959 idx = cpk - c->pkeys;
3961 cpk = c->pkeys + idx;
3962 pvalid = s->s3->tmp.valid_flags + idx;
3964 pk = cpk->privatekey;
3966 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
3967 /* If no cert or key, forget it */
3970 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3971 /* Allow any certificate to pass test */
3972 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
3973 rv = CERT_PKEY_STRICT_FLAGS | CERT_PKEY_EXPLICIT_SIGN |
3974 CERT_PKEY_VALID | CERT_PKEY_SIGN;
3982 idx = ssl_cert_type(x, pk);
3985 pvalid = s->s3->tmp.valid_flags + idx;
3987 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
3988 check_flags = CERT_PKEY_STRICT_FLAGS;
3990 check_flags = CERT_PKEY_VALID_FLAGS;
3997 check_flags |= CERT_PKEY_SUITEB;
3998 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
3999 if (ok == X509_V_OK)
4000 rv |= CERT_PKEY_SUITEB;
4001 else if (!check_flags)
4006 * Check all signature algorithms are consistent with signature
4007 * algorithms extension if TLS 1.2 or later and strict mode.
4009 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
4011 unsigned char rsign = 0;
4012 if (s->s3->tmp.peer_sigalgs)
4014 /* If no sigalgs extension use defaults from RFC5246 */
4017 case SSL_PKEY_RSA_ENC:
4018 case SSL_PKEY_RSA_SIGN:
4019 case SSL_PKEY_DH_RSA:
4020 rsign = TLSEXT_signature_rsa;
4021 default_nid = NID_sha1WithRSAEncryption;
4024 case SSL_PKEY_DSA_SIGN:
4025 case SSL_PKEY_DH_DSA:
4026 rsign = TLSEXT_signature_dsa;
4027 default_nid = NID_dsaWithSHA1;
4031 rsign = TLSEXT_signature_ecdsa;
4032 default_nid = NID_ecdsa_with_SHA1;
4035 case SSL_PKEY_GOST01:
4036 rsign = TLSEXT_signature_gostr34102001;
4037 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
4040 case SSL_PKEY_GOST12_256:
4041 rsign = TLSEXT_signature_gostr34102012_256;
4042 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
4045 case SSL_PKEY_GOST12_512:
4046 rsign = TLSEXT_signature_gostr34102012_512;
4047 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
4056 * If peer sent no signature algorithms extension and we have set
4057 * preferred signature algorithms check we support sha1.
4059 if (default_nid > 0 && c->conf_sigalgs) {
4061 const unsigned char *p = c->conf_sigalgs;
4062 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
4063 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4066 if (j == c->conf_sigalgslen) {
4073 /* Check signature algorithm of each cert in chain */
4074 if (!tls1_check_sig_alg(c, x, default_nid)) {
4078 rv |= CERT_PKEY_EE_SIGNATURE;
4079 rv |= CERT_PKEY_CA_SIGNATURE;
4080 for (i = 0; i < sk_X509_num(chain); i++) {
4081 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
4083 rv &= ~CERT_PKEY_CA_SIGNATURE;
4090 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4091 else if (check_flags)
4092 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
4094 /* Check cert parameters are consistent */
4095 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4096 rv |= CERT_PKEY_EE_PARAM;
4097 else if (!check_flags)
4100 rv |= CERT_PKEY_CA_PARAM;
4101 /* In strict mode check rest of chain too */
4102 else if (strict_mode) {
4103 rv |= CERT_PKEY_CA_PARAM;
4104 for (i = 0; i < sk_X509_num(chain); i++) {
4105 X509 *ca = sk_X509_value(chain, i);
4106 if (!tls1_check_cert_param(s, ca, 0)) {
4108 rv &= ~CERT_PKEY_CA_PARAM;
4115 if (!s->server && strict_mode) {
4116 STACK_OF(X509_NAME) *ca_dn;
4120 check_type = TLS_CT_RSA_SIGN;
4123 check_type = TLS_CT_DSS_SIGN;
4126 check_type = TLS_CT_ECDSA_SIGN;
4131 int cert_type = X509_certificate_type(x, pk);
4132 if (cert_type & EVP_PKS_RSA)
4133 check_type = TLS_CT_RSA_FIXED_DH;
4134 if (cert_type & EVP_PKS_DSA)
4135 check_type = TLS_CT_DSS_FIXED_DH;
4139 const unsigned char *ctypes;
4143 ctypelen = (int)c->ctype_num;
4145 ctypes = (unsigned char *)s->s3->tmp.ctype;
4146 ctypelen = s->s3->tmp.ctype_num;
4148 for (i = 0; i < ctypelen; i++) {
4149 if (ctypes[i] == check_type) {
4150 rv |= CERT_PKEY_CERT_TYPE;
4154 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4157 rv |= CERT_PKEY_CERT_TYPE;
4159 ca_dn = s->s3->tmp.ca_names;
4161 if (!sk_X509_NAME_num(ca_dn))
4162 rv |= CERT_PKEY_ISSUER_NAME;
4164 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4165 if (ssl_check_ca_name(ca_dn, x))
4166 rv |= CERT_PKEY_ISSUER_NAME;
4168 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4169 for (i = 0; i < sk_X509_num(chain); i++) {
4170 X509 *xtmp = sk_X509_value(chain, i);
4171 if (ssl_check_ca_name(ca_dn, xtmp)) {
4172 rv |= CERT_PKEY_ISSUER_NAME;
4177 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4180 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4182 if (!check_flags || (rv & check_flags) == check_flags)
4183 rv |= CERT_PKEY_VALID;
4187 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4188 if (*pvalid & CERT_PKEY_EXPLICIT_SIGN)
4189 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4190 else if (s->s3->tmp.md[idx] != NULL)
4191 rv |= CERT_PKEY_SIGN;
4193 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4196 * When checking a CERT_PKEY structure all flags are irrelevant if the
4200 if (rv & CERT_PKEY_VALID)
4203 /* Preserve explicit sign flag, clear rest */
4204 *pvalid &= CERT_PKEY_EXPLICIT_SIGN;
4211 /* Set validity of certificates in an SSL structure */
4212 void tls1_set_cert_validity(SSL *s)
4214 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4215 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4216 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4217 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4218 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4219 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4220 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
4221 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
4222 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
4225 /* User level utiity function to check a chain is suitable */
4226 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4228 return tls1_check_chain(s, x, pk, chain, -1);
4232 #ifndef OPENSSL_NO_DH
4233 DH *ssl_get_auto_dh(SSL *s)
4235 int dh_secbits = 80;
4236 if (s->cert->dh_tmp_auto == 2)
4237 return DH_get_1024_160();
4238 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
4239 if (s->s3->tmp.new_cipher->strength_bits == 256)
4244 CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
4245 dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
4248 if (dh_secbits >= 128) {
4254 BN_set_word(dhp->g, 2);
4255 if (dh_secbits >= 192)
4256 dhp->p = get_rfc3526_prime_8192(NULL);
4258 dhp->p = get_rfc3526_prime_3072(NULL);
4259 if (dhp->p == NULL || dhp->g == NULL) {
4265 if (dh_secbits >= 112)
4266 return DH_get_2048_224();
4267 return DH_get_1024_160();
4271 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4274 EVP_PKEY *pkey = X509_get_pubkey(x);
4276 secbits = EVP_PKEY_security_bits(pkey);
4277 EVP_PKEY_free(pkey);
4281 return ssl_security(s, op, secbits, 0, x);
4283 return ssl_ctx_security(ctx, op, secbits, 0, x);
4286 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
4288 /* Lookup signature algorithm digest */
4289 int secbits = -1, md_nid = NID_undef, sig_nid;
4290 sig_nid = X509_get_signature_nid(x);
4291 if (sig_nid && OBJ_find_sigid_algs(sig_nid, &md_nid, NULL)) {
4293 if (md_nid && (md = EVP_get_digestbynid(md_nid)))
4294 secbits = EVP_MD_size(md) * 4;
4297 return ssl_security(s, op, secbits, md_nid, x);
4299 return ssl_ctx_security(ctx, op, secbits, md_nid, x);
4302 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
4305 vfy = SSL_SECOP_PEER;
4307 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
4308 return SSL_R_EE_KEY_TOO_SMALL;
4310 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
4311 return SSL_R_CA_KEY_TOO_SMALL;
4313 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
4314 return SSL_R_CA_MD_TOO_WEAK;
4319 * Check security of a chain, if sk includes the end entity certificate then
4320 * x is NULL. If vfy is 1 then we are verifying a peer chain and not sending
4321 * one to the peer. Return values: 1 if ok otherwise error code to use
4324 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
4326 int rv, start_idx, i;
4328 x = sk_X509_value(sk, 0);
4333 rv = ssl_security_cert(s, NULL, x, vfy, 1);
4337 for (i = start_idx; i < sk_X509_num(sk); i++) {
4338 x = sk_X509_value(sk, i);
4339 rv = ssl_security_cert(s, NULL, x, vfy, 0);