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 #ifndef OPENSSL_NO_EC
117 #ifdef OPENSSL_NO_EC2M
118 # include <openssl/ec.h>
121 #include <openssl/ocsp.h>
122 #include <openssl/rand.h>
123 #include "ssl_locl.h"
125 const char tls1_version_str[] = "TLSv1" OPENSSL_VERSION_PTEXT;
127 #ifndef OPENSSL_NO_TLSEXT
128 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
129 const unsigned char *sess_id, int sesslen,
130 SSL_SESSION **psess);
131 static int ssl_check_clienthello_tlsext_early(SSL *s);
132 int ssl_check_serverhello_tlsext(SSL *s);
135 #define CHECKLEN(curr, val, limit) \
136 (((curr) >= (limit)) || (size_t)((limit) - (curr)) < (size_t)(val))
138 SSL3_ENC_METHOD TLSv1_enc_data = {
141 tls1_setup_key_block,
142 tls1_generate_master_secret,
143 tls1_change_cipher_state,
144 tls1_final_finish_mac,
145 TLS1_FINISH_MAC_LENGTH,
146 tls1_cert_verify_mac,
147 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
148 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
150 tls1_export_keying_material,
152 SSL3_HM_HEADER_LENGTH,
153 ssl3_set_handshake_header,
157 SSL3_ENC_METHOD TLSv1_1_enc_data = {
160 tls1_setup_key_block,
161 tls1_generate_master_secret,
162 tls1_change_cipher_state,
163 tls1_final_finish_mac,
164 TLS1_FINISH_MAC_LENGTH,
165 tls1_cert_verify_mac,
166 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
167 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
169 tls1_export_keying_material,
170 SSL_ENC_FLAG_EXPLICIT_IV,
171 SSL3_HM_HEADER_LENGTH,
172 ssl3_set_handshake_header,
176 SSL3_ENC_METHOD TLSv1_2_enc_data = {
179 tls1_setup_key_block,
180 tls1_generate_master_secret,
181 tls1_change_cipher_state,
182 tls1_final_finish_mac,
183 TLS1_FINISH_MAC_LENGTH,
184 tls1_cert_verify_mac,
185 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
186 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
188 tls1_export_keying_material,
189 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
190 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
191 SSL3_HM_HEADER_LENGTH,
192 ssl3_set_handshake_header,
196 long tls1_default_timeout(void)
199 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
200 * http, the cache would over fill
202 return (60 * 60 * 2);
209 s->method->ssl_clear(s);
213 void tls1_free(SSL *s)
215 #ifndef OPENSSL_NO_TLSEXT
216 if (s->tlsext_session_ticket) {
217 OPENSSL_free(s->tlsext_session_ticket);
219 #endif /* OPENSSL_NO_TLSEXT */
223 void tls1_clear(SSL *s)
226 s->version = s->method->version;
229 #ifndef OPENSSL_NO_EC
231 static int nid_list[] = {
232 NID_sect163k1, /* sect163k1 (1) */
233 NID_sect163r1, /* sect163r1 (2) */
234 NID_sect163r2, /* sect163r2 (3) */
235 NID_sect193r1, /* sect193r1 (4) */
236 NID_sect193r2, /* sect193r2 (5) */
237 NID_sect233k1, /* sect233k1 (6) */
238 NID_sect233r1, /* sect233r1 (7) */
239 NID_sect239k1, /* sect239k1 (8) */
240 NID_sect283k1, /* sect283k1 (9) */
241 NID_sect283r1, /* sect283r1 (10) */
242 NID_sect409k1, /* sect409k1 (11) */
243 NID_sect409r1, /* sect409r1 (12) */
244 NID_sect571k1, /* sect571k1 (13) */
245 NID_sect571r1, /* sect571r1 (14) */
246 NID_secp160k1, /* secp160k1 (15) */
247 NID_secp160r1, /* secp160r1 (16) */
248 NID_secp160r2, /* secp160r2 (17) */
249 NID_secp192k1, /* secp192k1 (18) */
250 NID_X9_62_prime192v1, /* secp192r1 (19) */
251 NID_secp224k1, /* secp224k1 (20) */
252 NID_secp224r1, /* secp224r1 (21) */
253 NID_secp256k1, /* secp256k1 (22) */
254 NID_X9_62_prime256v1, /* secp256r1 (23) */
255 NID_secp384r1, /* secp384r1 (24) */
256 NID_secp521r1, /* secp521r1 (25) */
257 NID_brainpoolP256r1, /* brainpoolP256r1 (26) */
258 NID_brainpoolP384r1, /* brainpoolP384r1 (27) */
259 NID_brainpoolP512r1 /* brainpool512r1 (28) */
262 static const unsigned char ecformats_default[] = {
263 TLSEXT_ECPOINTFORMAT_uncompressed,
264 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
265 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
268 /* The client's default curves / the server's 'auto' curves. */
269 static const unsigned char eccurves_auto[] = {
270 /* Prefer P-256 which has the fastest and most secure implementations. */
271 0, 23, /* secp256r1 (23) */
272 /* Other >= 256-bit prime curves. */
273 0, 25, /* secp521r1 (25) */
274 0, 28, /* brainpool512r1 (28) */
275 0, 27, /* brainpoolP384r1 (27) */
276 0, 24, /* secp384r1 (24) */
277 0, 26, /* brainpoolP256r1 (26) */
278 0, 22, /* secp256k1 (22) */
279 # ifndef OPENSSL_NO_EC2M
280 /* >= 256-bit binary curves. */
281 0, 14, /* sect571r1 (14) */
282 0, 13, /* sect571k1 (13) */
283 0, 11, /* sect409k1 (11) */
284 0, 12, /* sect409r1 (12) */
285 0, 9, /* sect283k1 (9) */
286 0, 10, /* sect283r1 (10) */
290 static const unsigned char eccurves_all[] = {
291 /* Prefer P-256 which has the fastest and most secure implementations. */
292 0, 23, /* secp256r1 (23) */
293 /* Other >= 256-bit prime curves. */
294 0, 25, /* secp521r1 (25) */
295 0, 28, /* brainpool512r1 (28) */
296 0, 27, /* brainpoolP384r1 (27) */
297 0, 24, /* secp384r1 (24) */
298 0, 26, /* brainpoolP256r1 (26) */
299 0, 22, /* secp256k1 (22) */
300 # ifndef OPENSSL_NO_EC2M
301 /* >= 256-bit binary curves. */
302 0, 14, /* sect571r1 (14) */
303 0, 13, /* sect571k1 (13) */
304 0, 11, /* sect409k1 (11) */
305 0, 12, /* sect409r1 (12) */
306 0, 9, /* sect283k1 (9) */
307 0, 10, /* sect283r1 (10) */
310 * Remaining curves disabled by default but still permitted if set
311 * via an explicit callback or parameters.
313 0, 20, /* secp224k1 (20) */
314 0, 21, /* secp224r1 (21) */
315 0, 18, /* secp192k1 (18) */
316 0, 19, /* secp192r1 (19) */
317 0, 15, /* secp160k1 (15) */
318 0, 16, /* secp160r1 (16) */
319 0, 17, /* secp160r2 (17) */
320 # ifndef OPENSSL_NO_EC2M
321 0, 8, /* sect239k1 (8) */
322 0, 6, /* sect233k1 (6) */
323 0, 7, /* sect233r1 (7) */
324 0, 4, /* sect193r1 (4) */
325 0, 5, /* sect193r2 (5) */
326 0, 1, /* sect163k1 (1) */
327 0, 2, /* sect163r1 (2) */
328 0, 3, /* sect163r2 (3) */
332 static const unsigned char suiteb_curves[] = {
333 0, TLSEXT_curve_P_256,
334 0, TLSEXT_curve_P_384
338 /* Brainpool not allowed in FIPS mode */
339 static const unsigned char fips_curves_default[] = {
340 # ifndef OPENSSL_NO_EC2M
341 0, 14, /* sect571r1 (14) */
342 0, 13, /* sect571k1 (13) */
344 0, 25, /* secp521r1 (25) */
345 # ifndef OPENSSL_NO_EC2M
346 0, 11, /* sect409k1 (11) */
347 0, 12, /* sect409r1 (12) */
349 0, 24, /* secp384r1 (24) */
350 # ifndef OPENSSL_NO_EC2M
351 0, 9, /* sect283k1 (9) */
352 0, 10, /* sect283r1 (10) */
354 0, 22, /* secp256k1 (22) */
355 0, 23, /* secp256r1 (23) */
356 # ifndef OPENSSL_NO_EC2M
357 0, 8, /* sect239k1 (8) */
358 0, 6, /* sect233k1 (6) */
359 0, 7, /* sect233r1 (7) */
361 0, 20, /* secp224k1 (20) */
362 0, 21, /* secp224r1 (21) */
363 # ifndef OPENSSL_NO_EC2M
364 0, 4, /* sect193r1 (4) */
365 0, 5, /* sect193r2 (5) */
367 0, 18, /* secp192k1 (18) */
368 0, 19, /* secp192r1 (19) */
369 # ifndef OPENSSL_NO_EC2M
370 0, 1, /* sect163k1 (1) */
371 0, 2, /* sect163r1 (2) */
372 0, 3, /* sect163r2 (3) */
374 0, 15, /* secp160k1 (15) */
375 0, 16, /* secp160r1 (16) */
376 0, 17, /* secp160r2 (17) */
380 int tls1_ec_curve_id2nid(int curve_id)
382 /* ECC curves from RFC 4492 and RFC 7027 */
383 if ((curve_id < 1) || ((unsigned int)curve_id >
384 sizeof(nid_list) / sizeof(nid_list[0])))
386 return nid_list[curve_id - 1];
389 int tls1_ec_nid2curve_id(int nid)
391 /* ECC curves from RFC 4492 and RFC 7027 */
393 case NID_sect163k1: /* sect163k1 (1) */
395 case NID_sect163r1: /* sect163r1 (2) */
397 case NID_sect163r2: /* sect163r2 (3) */
399 case NID_sect193r1: /* sect193r1 (4) */
401 case NID_sect193r2: /* sect193r2 (5) */
403 case NID_sect233k1: /* sect233k1 (6) */
405 case NID_sect233r1: /* sect233r1 (7) */
407 case NID_sect239k1: /* sect239k1 (8) */
409 case NID_sect283k1: /* sect283k1 (9) */
411 case NID_sect283r1: /* sect283r1 (10) */
413 case NID_sect409k1: /* sect409k1 (11) */
415 case NID_sect409r1: /* sect409r1 (12) */
417 case NID_sect571k1: /* sect571k1 (13) */
419 case NID_sect571r1: /* sect571r1 (14) */
421 case NID_secp160k1: /* secp160k1 (15) */
423 case NID_secp160r1: /* secp160r1 (16) */
425 case NID_secp160r2: /* secp160r2 (17) */
427 case NID_secp192k1: /* secp192k1 (18) */
429 case NID_X9_62_prime192v1: /* secp192r1 (19) */
431 case NID_secp224k1: /* secp224k1 (20) */
433 case NID_secp224r1: /* secp224r1 (21) */
435 case NID_secp256k1: /* secp256k1 (22) */
437 case NID_X9_62_prime256v1: /* secp256r1 (23) */
439 case NID_secp384r1: /* secp384r1 (24) */
441 case NID_secp521r1: /* secp521r1 (25) */
443 case NID_brainpoolP256r1: /* brainpoolP256r1 (26) */
445 case NID_brainpoolP384r1: /* brainpoolP384r1 (27) */
447 case NID_brainpoolP512r1: /* brainpool512r1 (28) */
455 * Get curves list, if "sess" is set return client curves otherwise
457 * Sets |num_curves| to the number of curves in the list, i.e.,
458 * the length of |pcurves| is 2 * num_curves.
459 * Returns 1 on success and 0 if the client curves list has invalid format.
460 * The latter indicates an internal error: we should not be accepting such
461 * lists in the first place.
462 * TODO(emilia): we should really be storing the curves list in explicitly
463 * parsed form instead. (However, this would affect binary compatibility
464 * so cannot happen in the 1.0.x series.)
466 static int tls1_get_curvelist(SSL *s, int sess,
467 const unsigned char **pcurves,
470 size_t pcurveslen = 0;
472 *pcurves = s->session->tlsext_ellipticcurvelist;
473 pcurveslen = s->session->tlsext_ellipticcurvelist_length;
475 /* For Suite B mode only include P-256, P-384 */
476 switch (tls1_suiteb(s)) {
477 case SSL_CERT_FLAG_SUITEB_128_LOS:
478 *pcurves = suiteb_curves;
479 pcurveslen = sizeof(suiteb_curves);
482 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
483 *pcurves = suiteb_curves;
487 case SSL_CERT_FLAG_SUITEB_192_LOS:
488 *pcurves = suiteb_curves + 2;
492 *pcurves = s->tlsext_ellipticcurvelist;
493 pcurveslen = s->tlsext_ellipticcurvelist_length;
498 *pcurves = fips_curves_default;
499 pcurveslen = sizeof(fips_curves_default);
503 if (!s->server || s->cert->ecdh_tmp_auto) {
504 *pcurves = eccurves_auto;
505 pcurveslen = sizeof(eccurves_auto);
507 *pcurves = eccurves_all;
508 pcurveslen = sizeof(eccurves_all);
513 /* We do not allow odd length arrays to enter the system. */
514 if (pcurveslen & 1) {
515 SSLerr(SSL_F_TLS1_GET_CURVELIST, ERR_R_INTERNAL_ERROR);
519 *num_curves = pcurveslen / 2;
524 /* Check a curve is one of our preferences */
525 int tls1_check_curve(SSL *s, const unsigned char *p, size_t len)
527 const unsigned char *curves;
528 size_t num_curves, i;
529 unsigned int suiteb_flags = tls1_suiteb(s);
530 if (len != 3 || p[0] != NAMED_CURVE_TYPE)
532 /* Check curve matches Suite B preferences */
534 unsigned long cid = s->s3->tmp.new_cipher->id;
537 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
538 if (p[2] != TLSEXT_curve_P_256)
540 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
541 if (p[2] != TLSEXT_curve_P_384)
543 } else /* Should never happen */
546 if (!tls1_get_curvelist(s, 0, &curves, &num_curves))
548 for (i = 0; i < num_curves; i++, curves += 2) {
549 if (p[1] == curves[0] && p[2] == curves[1])
556 * Return |nmatch|th shared curve or NID_undef if there is no match.
557 * For nmatch == -1, return number of matches
558 * For nmatch == -2, return the NID of the curve to use for
559 * an EC tmp key, or NID_undef if there is no match.
561 int tls1_shared_curve(SSL *s, int nmatch)
563 const unsigned char *pref, *supp;
564 size_t num_pref, num_supp, i, j;
566 /* Can't do anything on client side */
570 if (tls1_suiteb(s)) {
572 * For Suite B ciphersuite determines curve: we already know
573 * these are acceptable due to previous checks.
575 unsigned long cid = s->s3->tmp.new_cipher->id;
576 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
577 return NID_X9_62_prime256v1; /* P-256 */
578 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
579 return NID_secp384r1; /* P-384 */
580 /* Should never happen */
583 /* If not Suite B just return first preference shared curve */
587 * Avoid truncation. tls1_get_curvelist takes an int
588 * but s->options is a long...
590 if (!tls1_get_curvelist
591 (s, (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0, &supp,
593 /* In practice, NID_undef == 0 but let's be precise. */
594 return nmatch == -1 ? 0 : NID_undef;
595 if (!tls1_get_curvelist
596 (s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), &pref,
598 return nmatch == -1 ? 0 : NID_undef;
601 * If the client didn't send the elliptic_curves extension all of them
604 if (num_supp == 0 && (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) != 0) {
606 num_supp = sizeof(eccurves_all) / 2;
607 } else if (num_pref == 0 &&
608 (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) == 0) {
610 num_pref = sizeof(eccurves_all) / 2;
614 for (i = 0; i < num_pref; i++, pref += 2) {
615 const unsigned char *tsupp = supp;
616 for (j = 0; j < num_supp; j++, tsupp += 2) {
617 if (pref[0] == tsupp[0] && pref[1] == tsupp[1]) {
619 int id = (pref[0] << 8) | pref[1];
620 return tls1_ec_curve_id2nid(id);
628 /* Out of range (nmatch > k). */
632 int tls1_set_curves(unsigned char **pext, size_t *pextlen,
633 int *curves, size_t ncurves)
635 unsigned char *clist, *p;
638 * Bitmap of curves included to detect duplicates: only works while curve
641 unsigned long dup_list = 0;
642 # ifdef OPENSSL_NO_EC2M
646 clist = OPENSSL_malloc(ncurves * 2);
649 for (i = 0, p = clist; i < ncurves; i++) {
650 unsigned long idmask;
652 id = tls1_ec_nid2curve_id(curves[i]);
654 /* NB: 25 is last curve ID supported by FIPS module */
655 if (FIPS_mode() && id > 25) {
660 # ifdef OPENSSL_NO_EC2M
661 curve = EC_GROUP_new_by_curve_name(curves[i]);
662 if (!curve || EC_METHOD_get_field_type(EC_GROUP_method_of(curve))
663 == NID_X9_62_characteristic_two_field) {
665 EC_GROUP_free(curve);
669 EC_GROUP_free(curve);
672 if (!id || (dup_list & idmask)) {
682 *pextlen = ncurves * 2;
686 # define MAX_CURVELIST 28
690 int nid_arr[MAX_CURVELIST];
693 static int nid_cb(const char *elem, int len, void *arg)
695 nid_cb_st *narg = arg;
701 if (narg->nidcnt == MAX_CURVELIST)
703 if (len > (int)(sizeof(etmp) - 1))
705 memcpy(etmp, elem, len);
707 nid = EC_curve_nist2nid(etmp);
708 if (nid == NID_undef)
709 nid = OBJ_sn2nid(etmp);
710 if (nid == NID_undef)
711 nid = OBJ_ln2nid(etmp);
712 if (nid == NID_undef)
714 for (i = 0; i < narg->nidcnt; i++)
715 if (narg->nid_arr[i] == nid)
717 narg->nid_arr[narg->nidcnt++] = nid;
721 /* Set curves based on a colon separate list */
722 int tls1_set_curves_list(unsigned char **pext, size_t *pextlen,
727 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
731 return tls1_set_curves(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
734 /* For an EC key set TLS id and required compression based on parameters */
735 static int tls1_set_ec_id(unsigned char *curve_id, unsigned char *comp_id,
740 const EC_METHOD *meth;
743 /* Determine if it is a prime field */
744 grp = EC_KEY_get0_group(ec);
747 meth = EC_GROUP_method_of(grp);
750 if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
754 /* Determine curve ID */
755 id = EC_GROUP_get_curve_name(grp);
756 id = tls1_ec_nid2curve_id(id);
757 /* If we have an ID set it, otherwise set arbitrary explicit curve */
760 curve_id[1] = (unsigned char)id;
769 if (EC_KEY_get0_public_key(ec) == NULL)
771 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
773 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
775 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
777 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
782 /* Check an EC key is compatible with extensions */
783 static int tls1_check_ec_key(SSL *s,
784 unsigned char *curve_id, unsigned char *comp_id)
786 const unsigned char *pformats, *pcurves;
787 size_t num_formats, num_curves, i;
790 * If point formats extension present check it, otherwise everything is
791 * supported (see RFC4492).
793 if (comp_id && s->session->tlsext_ecpointformatlist) {
794 pformats = s->session->tlsext_ecpointformatlist;
795 num_formats = s->session->tlsext_ecpointformatlist_length;
796 for (i = 0; i < num_formats; i++, pformats++) {
797 if (*comp_id == *pformats)
800 if (i == num_formats)
805 /* Check curve is consistent with client and server preferences */
806 for (j = 0; j <= 1; j++) {
807 if (!tls1_get_curvelist(s, j, &pcurves, &num_curves))
809 if (j == 1 && num_curves == 0) {
811 * If we've not received any curves then skip this check.
812 * RFC 4492 does not require the supported elliptic curves extension
813 * so if it is not sent we can just choose any curve.
814 * It is invalid to send an empty list in the elliptic curves
815 * extension, so num_curves == 0 always means no extension.
819 for (i = 0; i < num_curves; i++, pcurves += 2) {
820 if (pcurves[0] == curve_id[0] && pcurves[1] == curve_id[1])
825 /* For clients can only check sent curve list */
832 static void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
836 * If we have a custom point format list use it otherwise use default
838 if (s->tlsext_ecpointformatlist) {
839 *pformats = s->tlsext_ecpointformatlist;
840 *num_formats = s->tlsext_ecpointformatlist_length;
842 *pformats = ecformats_default;
843 /* For Suite B we don't support char2 fields */
845 *num_formats = sizeof(ecformats_default) - 1;
847 *num_formats = sizeof(ecformats_default);
852 * Check cert parameters compatible with extensions: currently just checks EC
853 * certificates have compatible curves and compression.
855 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
857 unsigned char comp_id, curve_id[2];
860 pkey = X509_get_pubkey(x);
863 /* If not EC nothing to do */
864 if (pkey->type != EVP_PKEY_EC) {
868 rv = tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec);
873 * Can't check curve_id for client certs as we don't have a supported
876 rv = tls1_check_ec_key(s, s->server ? curve_id : NULL, &comp_id);
880 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
881 * SHA384+P-384, adjust digest if necessary.
883 if (set_ee_md && tls1_suiteb(s)) {
889 /* Check to see we have necessary signing algorithm */
890 if (curve_id[1] == TLSEXT_curve_P_256)
891 check_md = NID_ecdsa_with_SHA256;
892 else if (curve_id[1] == TLSEXT_curve_P_384)
893 check_md = NID_ecdsa_with_SHA384;
895 return 0; /* Should never happen */
896 for (i = 0; i < c->shared_sigalgslen; i++)
897 if (check_md == c->shared_sigalgs[i].signandhash_nid)
899 if (i == c->shared_sigalgslen)
901 if (set_ee_md == 2) {
902 if (check_md == NID_ecdsa_with_SHA256)
903 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha256();
905 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha384();
911 # ifndef OPENSSL_NO_ECDH
912 /* Check EC temporary key is compatible with client extensions */
913 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
915 unsigned char curve_id[2];
916 EC_KEY *ec = s->cert->ecdh_tmp;
917 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
918 /* Allow any curve: not just those peer supports */
919 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
923 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
926 if (tls1_suiteb(s)) {
927 /* Curve to check determined by ciphersuite */
928 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
929 curve_id[1] = TLSEXT_curve_P_256;
930 else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
931 curve_id[1] = TLSEXT_curve_P_384;
935 /* Check this curve is acceptable */
936 if (!tls1_check_ec_key(s, curve_id, NULL))
938 /* If auto or setting curve from callback assume OK */
939 if (s->cert->ecdh_tmp_auto || s->cert->ecdh_tmp_cb)
941 /* Otherwise check curve is acceptable */
943 unsigned char curve_tmp[2];
946 if (!tls1_set_ec_id(curve_tmp, NULL, ec))
948 if (!curve_tmp[0] || curve_tmp[1] == curve_id[1])
954 if (s->cert->ecdh_tmp_auto) {
955 /* Need a shared curve */
956 if (tls1_shared_curve(s, 0))
962 if (s->cert->ecdh_tmp_cb)
967 if (!tls1_set_ec_id(curve_id, NULL, ec))
969 /* Set this to allow use of invalid curves for testing */
973 return tls1_check_ec_key(s, curve_id, NULL);
976 # endif /* OPENSSL_NO_ECDH */
980 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
985 #endif /* OPENSSL_NO_EC */
987 #ifndef OPENSSL_NO_TLSEXT
990 * List of supported signature algorithms and hashes. Should make this
991 * customisable at some point, for now include everything we support.
994 # ifdef OPENSSL_NO_RSA
995 # define tlsext_sigalg_rsa(md) /* */
997 # define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
1000 # ifdef OPENSSL_NO_DSA
1001 # define tlsext_sigalg_dsa(md) /* */
1003 # define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
1006 # ifdef OPENSSL_NO_ECDSA
1007 # define tlsext_sigalg_ecdsa(md)
1010 # define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
1013 # define tlsext_sigalg(md) \
1014 tlsext_sigalg_rsa(md) \
1015 tlsext_sigalg_dsa(md) \
1016 tlsext_sigalg_ecdsa(md)
1018 static unsigned char tls12_sigalgs[] = {
1019 # ifndef OPENSSL_NO_SHA512
1020 tlsext_sigalg(TLSEXT_hash_sha512)
1021 tlsext_sigalg(TLSEXT_hash_sha384)
1023 # ifndef OPENSSL_NO_SHA256
1024 tlsext_sigalg(TLSEXT_hash_sha256)
1025 tlsext_sigalg(TLSEXT_hash_sha224)
1027 # ifndef OPENSSL_NO_SHA
1028 tlsext_sigalg(TLSEXT_hash_sha1)
1032 # ifndef OPENSSL_NO_ECDSA
1033 static unsigned char suiteb_sigalgs[] = {
1034 tlsext_sigalg_ecdsa(TLSEXT_hash_sha256)
1035 tlsext_sigalg_ecdsa(TLSEXT_hash_sha384)
1038 size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs)
1041 * If Suite B mode use Suite B sigalgs only, ignore any other
1044 # ifndef OPENSSL_NO_EC
1045 switch (tls1_suiteb(s)) {
1046 case SSL_CERT_FLAG_SUITEB_128_LOS:
1047 *psigs = suiteb_sigalgs;
1048 return sizeof(suiteb_sigalgs);
1050 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
1051 *psigs = suiteb_sigalgs;
1054 case SSL_CERT_FLAG_SUITEB_192_LOS:
1055 *psigs = suiteb_sigalgs + 2;
1059 /* If server use client authentication sigalgs if not NULL */
1060 if (s->server && s->cert->client_sigalgs) {
1061 *psigs = s->cert->client_sigalgs;
1062 return s->cert->client_sigalgslen;
1063 } else if (s->cert->conf_sigalgs) {
1064 *psigs = s->cert->conf_sigalgs;
1065 return s->cert->conf_sigalgslen;
1067 *psigs = tls12_sigalgs;
1068 return sizeof(tls12_sigalgs);
1073 * Check signature algorithm is consistent with sent supported signature
1074 * algorithms and if so return relevant digest.
1076 int tls12_check_peer_sigalg(const EVP_MD **pmd, SSL *s,
1077 const unsigned char *sig, EVP_PKEY *pkey)
1079 const unsigned char *sent_sigs;
1080 size_t sent_sigslen, i;
1081 int sigalg = tls12_get_sigid(pkey);
1082 /* Should never happen */
1085 /* Check key type is consistent with signature */
1086 if (sigalg != (int)sig[1]) {
1087 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1090 # ifndef OPENSSL_NO_EC
1091 if (pkey->type == EVP_PKEY_EC) {
1092 unsigned char curve_id[2], comp_id;
1093 /* Check compression and curve matches extensions */
1094 if (!tls1_set_ec_id(curve_id, &comp_id, pkey->pkey.ec))
1096 if (!s->server && !tls1_check_ec_key(s, curve_id, &comp_id)) {
1097 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
1100 /* If Suite B only P-384+SHA384 or P-256+SHA-256 allowed */
1101 if (tls1_suiteb(s)) {
1104 if (curve_id[1] == TLSEXT_curve_P_256) {
1105 if (sig[0] != TLSEXT_hash_sha256) {
1106 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1107 SSL_R_ILLEGAL_SUITEB_DIGEST);
1110 } else if (curve_id[1] == TLSEXT_curve_P_384) {
1111 if (sig[0] != TLSEXT_hash_sha384) {
1112 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG,
1113 SSL_R_ILLEGAL_SUITEB_DIGEST);
1119 } else if (tls1_suiteb(s))
1123 /* Check signature matches a type we sent */
1124 sent_sigslen = tls12_get_psigalgs(s, &sent_sigs);
1125 for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) {
1126 if (sig[0] == sent_sigs[0] && sig[1] == sent_sigs[1])
1129 /* Allow fallback to SHA1 if not strict mode */
1130 if (i == sent_sigslen
1131 && (sig[0] != TLSEXT_hash_sha1
1132 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1133 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_SIGNATURE_TYPE);
1136 *pmd = tls12_get_hash(sig[0]);
1138 SSLerr(SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_UNKNOWN_DIGEST);
1142 * Store the digest used so applications can retrieve it if they wish.
1144 if (s->session && s->session->sess_cert)
1145 s->session->sess_cert->peer_key->digest = *pmd;
1150 * Get a mask of disabled algorithms: an algorithm is disabled if it isn't
1151 * supported or doesn't appear in supported signature algorithms. Unlike
1152 * ssl_cipher_get_disabled this applies to a specific session and not global
1155 void ssl_set_client_disabled(SSL *s)
1158 const unsigned char *sigalgs;
1159 size_t i, sigalgslen;
1160 int have_rsa = 0, have_dsa = 0, have_ecdsa = 0;
1163 /* Don't allow TLS 1.2 only ciphers if we don't suppport them */
1164 if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s))
1165 c->mask_ssl = SSL_TLSV1_2;
1169 * Now go through all signature algorithms seeing if we support any for
1170 * RSA, DSA, ECDSA. Do this for all versions not just TLS 1.2.
1172 sigalgslen = tls12_get_psigalgs(s, &sigalgs);
1173 for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) {
1174 switch (sigalgs[1]) {
1175 # ifndef OPENSSL_NO_RSA
1176 case TLSEXT_signature_rsa:
1180 # ifndef OPENSSL_NO_DSA
1181 case TLSEXT_signature_dsa:
1185 # ifndef OPENSSL_NO_ECDSA
1186 case TLSEXT_signature_ecdsa:
1193 * Disable auth and static DH if we don't include any appropriate
1194 * signature algorithms.
1197 c->mask_a |= SSL_aRSA;
1198 c->mask_k |= SSL_kDHr | SSL_kECDHr;
1201 c->mask_a |= SSL_aDSS;
1202 c->mask_k |= SSL_kDHd;
1205 c->mask_a |= SSL_aECDSA;
1206 c->mask_k |= SSL_kECDHe;
1208 # ifndef OPENSSL_NO_KRB5
1209 if (!kssl_tgt_is_available(s->kssl_ctx)) {
1210 c->mask_a |= SSL_aKRB5;
1211 c->mask_k |= SSL_kKRB5;
1214 # ifndef OPENSSL_NO_PSK
1215 /* with PSK there must be client callback set */
1216 if (!s->psk_client_callback) {
1217 c->mask_a |= SSL_aPSK;
1218 c->mask_k |= SSL_kPSK;
1220 # endif /* OPENSSL_NO_PSK */
1221 # ifndef OPENSSL_NO_SRP
1222 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1223 c->mask_a |= SSL_aSRP;
1224 c->mask_k |= SSL_kSRP;
1230 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
1231 unsigned char *limit, int *al)
1234 unsigned char *orig = buf;
1235 unsigned char *ret = buf;
1236 # ifndef OPENSSL_NO_EC
1237 /* See if we support any ECC ciphersuites */
1239 if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
1241 unsigned long alg_k, alg_a;
1242 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1244 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1245 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1247 alg_k = c->algorithm_mkey;
1248 alg_a = c->algorithm_auth;
1249 if ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)
1250 || (alg_a & SSL_aECDSA))) {
1258 /* don't add extensions for SSLv3 unless doing secure renegotiation */
1259 if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding)
1265 return NULL; /* this really never occurs, but ... */
1267 if (s->tlsext_hostname != NULL) {
1268 /* Add TLS extension servername to the Client Hello message */
1272 * check for enough space.
1273 * 4 for the servername type and entension length
1274 * 2 for servernamelist length
1275 * 1 for the hostname type
1276 * 2 for hostname length
1279 size_str = strlen(s->tlsext_hostname);
1280 if (CHECKLEN(ret, 9 + size_str, limit))
1283 /* extension type and length */
1284 s2n(TLSEXT_TYPE_server_name, ret);
1285 s2n(size_str + 5, ret);
1287 /* length of servername list */
1288 s2n(size_str + 3, ret);
1290 /* hostname type, length and hostname */
1291 *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
1293 memcpy(ret, s->tlsext_hostname, size_str);
1297 /* Add RI if renegotiating */
1298 if (s->renegotiate) {
1301 if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
1302 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1306 if ((limit - ret - 4 - el) < 0)
1309 s2n(TLSEXT_TYPE_renegotiate, ret);
1312 if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
1313 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1319 # ifndef OPENSSL_NO_SRP
1320 /* Add SRP username if there is one */
1321 if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
1322 * Client Hello message */
1324 size_t login_len = strlen(s->srp_ctx.login);
1325 if (login_len > 255 || login_len == 0) {
1326 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1331 * check for enough space.
1332 * 4 for the srp type type and entension length
1333 * 1 for the srp user identity
1334 * + srp user identity length
1336 if (CHECKLEN(ret, 5 + login_len, limit))
1339 /* fill in the extension */
1340 s2n(TLSEXT_TYPE_srp, ret);
1341 s2n(login_len + 1, ret);
1342 (*ret++) = (unsigned char)login_len;
1343 memcpy(ret, s->srp_ctx.login, login_len);
1348 # ifndef OPENSSL_NO_EC
1351 * Add TLS extension ECPointFormats to the ClientHello message
1353 const unsigned char *pcurves, *pformats;
1354 size_t num_curves, num_formats, curves_list_len;
1356 tls1_get_formatlist(s, &pformats, &num_formats);
1358 if (num_formats > 255) {
1359 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1363 * check for enough space.
1364 * 4 bytes for the ec point formats type and extension length
1365 * 1 byte for the length of the formats
1368 if (CHECKLEN(ret, 5 + num_formats, limit))
1371 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1372 /* The point format list has 1-byte length. */
1373 s2n(num_formats + 1, ret);
1374 *(ret++) = (unsigned char)num_formats;
1375 memcpy(ret, pformats, num_formats);
1379 * Add TLS extension EllipticCurves to the ClientHello message
1381 pcurves = s->tlsext_ellipticcurvelist;
1382 if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves))
1385 if (num_curves > 65532 / 2) {
1386 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1389 curves_list_len = 2 * num_curves;
1391 * check for enough space.
1392 * 4 bytes for the ec curves type and extension length
1393 * 2 bytes for the curve list length
1394 * + curve list length
1396 if (CHECKLEN(ret, 6 + curves_list_len, limit))
1399 s2n(TLSEXT_TYPE_elliptic_curves, ret);
1400 s2n(curves_list_len + 2, ret);
1401 s2n(curves_list_len, ret);
1402 memcpy(ret, pcurves, curves_list_len);
1403 ret += curves_list_len;
1405 # endif /* OPENSSL_NO_EC */
1407 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1409 if (!s->new_session && s->session && s->session->tlsext_tick)
1410 ticklen = s->session->tlsext_ticklen;
1411 else if (s->session && s->tlsext_session_ticket &&
1412 s->tlsext_session_ticket->data) {
1413 ticklen = s->tlsext_session_ticket->length;
1414 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
1415 if (!s->session->tlsext_tick)
1417 memcpy(s->session->tlsext_tick,
1418 s->tlsext_session_ticket->data, ticklen);
1419 s->session->tlsext_ticklen = ticklen;
1422 if (ticklen == 0 && s->tlsext_session_ticket &&
1423 s->tlsext_session_ticket->data == NULL)
1426 * Check for enough room 2 for extension type, 2 for len rest for
1429 if (CHECKLEN(ret, 4 + ticklen, limit))
1431 s2n(TLSEXT_TYPE_session_ticket, ret);
1434 memcpy(ret, s->session->tlsext_tick, ticklen);
1440 if (SSL_CLIENT_USE_SIGALGS(s)) {
1442 const unsigned char *salg;
1443 salglen = tls12_get_psigalgs(s, &salg);
1446 * check for enough space.
1447 * 4 bytes for the sigalgs type and extension length
1448 * 2 bytes for the sigalg list length
1449 * + sigalg list length
1451 if (CHECKLEN(ret, salglen + 6, limit))
1453 s2n(TLSEXT_TYPE_signature_algorithms, ret);
1454 s2n(salglen + 2, ret);
1456 memcpy(ret, salg, salglen);
1459 # ifdef TLSEXT_TYPE_opaque_prf_input
1460 if (s->s3->client_opaque_prf_input != NULL) {
1461 size_t col = s->s3->client_opaque_prf_input_len;
1463 if ((long)(limit - ret - 6 - col < 0))
1465 if (col > 0xFFFD) /* can't happen */
1468 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1471 memcpy(ret, s->s3->client_opaque_prf_input, col);
1476 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1478 size_t extlen, idlen;
1483 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1484 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1485 lentmp = i2d_OCSP_RESPID(id, NULL);
1488 idlen += (size_t)lentmp + 2;
1491 if (s->tlsext_ocsp_exts) {
1492 lentmp = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
1495 extlen = (size_t)lentmp;
1499 if (extlen + idlen > 0xFFF0)
1502 * 2 bytes for status request type
1503 * 2 bytes for status request len
1504 * 1 byte for OCSP request type
1505 * 2 bytes for length of ids
1506 * 2 bytes for length of extensions
1508 * + length of extensions
1510 if (CHECKLEN(ret, 9 + idlen + extlen, limit))
1513 s2n(TLSEXT_TYPE_status_request, ret);
1514 s2n(extlen + idlen + 5, ret);
1515 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
1517 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
1518 /* save position of id len */
1519 unsigned char *q = ret;
1520 id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
1521 /* skip over id len */
1523 lentmp = i2d_OCSP_RESPID(id, &ret);
1529 i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
1531 # ifndef OPENSSL_NO_HEARTBEATS
1532 /* Add Heartbeat extension */
1535 * check for enough space.
1536 * 4 bytes for the heartbeat ext type and extension length
1537 * 1 byte for the mode
1539 if (CHECKLEN(ret, 5, limit))
1542 s2n(TLSEXT_TYPE_heartbeat, ret);
1546 * 1: peer may send requests
1547 * 2: peer not allowed to send requests
1549 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1550 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1552 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1555 # ifndef OPENSSL_NO_NEXTPROTONEG
1556 if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
1558 * The client advertises an emtpy extension to indicate its support
1559 * for Next Protocol Negotiation
1563 * check for enough space.
1564 * 4 bytes for the NPN ext type and extension length
1566 if (CHECKLEN(ret, 4, limit))
1568 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1573 if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) {
1575 * check for enough space.
1576 * 4 bytes for the ALPN type and extension length
1577 * 2 bytes for the ALPN protocol list length
1578 * + ALPN protocol list length
1580 if (CHECKLEN(ret, 6 + s->alpn_client_proto_list_len, limit))
1582 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1583 s2n(2 + s->alpn_client_proto_list_len, ret);
1584 s2n(s->alpn_client_proto_list_len, ret);
1585 memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
1586 ret += s->alpn_client_proto_list_len;
1587 s->cert->alpn_sent = 1;
1589 # ifndef OPENSSL_NO_SRTP
1590 if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
1593 ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
1596 * check for enough space.
1597 * 4 bytes for the SRTP type and extension length
1598 * + SRTP profiles length
1600 if (CHECKLEN(ret, 4 + el, limit))
1603 s2n(TLSEXT_TYPE_use_srtp, ret);
1606 if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
1607 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1613 custom_ext_init(&s->cert->cli_ext);
1614 /* Add custom TLS Extensions to ClientHello */
1615 if (!custom_ext_add(s, 0, &ret, limit, al))
1619 * Add padding to workaround bugs in F5 terminators. See
1620 * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
1621 * code works out the length of all existing extensions it MUST always
1624 if (s->options & SSL_OP_TLSEXT_PADDING) {
1625 int hlen = ret - (unsigned char *)s->init_buf->data;
1627 * The code in s23_clnt.c to build ClientHello messages includes the
1628 * 5-byte record header in the buffer, while the code in s3_clnt.c
1631 if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
1633 if (hlen > 0xff && hlen < 0x200) {
1634 hlen = 0x200 - hlen;
1641 * check for enough space. Strictly speaking we know we've already
1642 * got enough space because to get here the message size is < 0x200,
1643 * but we know that we've allocated far more than that in the buffer
1644 * - but for consistency and robustness we're going to check anyway.
1646 * 4 bytes for the padding type and extension length
1649 if (CHECKLEN(ret, 4 + hlen, limit))
1651 s2n(TLSEXT_TYPE_padding, ret);
1653 memset(ret, 0, hlen);
1658 if ((extdatalen = ret - orig - 2) == 0)
1661 s2n(extdatalen, orig);
1665 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
1666 unsigned char *limit, int *al)
1669 unsigned char *orig = buf;
1670 unsigned char *ret = buf;
1671 # ifndef OPENSSL_NO_NEXTPROTONEG
1672 int next_proto_neg_seen;
1674 # ifndef OPENSSL_NO_EC
1675 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1676 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1677 int using_ecc = (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
1678 || (alg_a & SSL_aECDSA);
1679 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1682 * don't add extensions for SSLv3, unless doing secure renegotiation
1684 if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
1689 return NULL; /* this really never occurs, but ... */
1691 if (!s->hit && s->servername_done == 1
1692 && s->session->tlsext_hostname != NULL) {
1693 if ((long)(limit - ret - 4) < 0)
1696 s2n(TLSEXT_TYPE_server_name, ret);
1700 if (s->s3->send_connection_binding) {
1703 if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
1704 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1709 * check for enough space.
1710 * 4 bytes for the reneg type and extension length
1711 * + reneg data length
1713 if (CHECKLEN(ret, 4 + el, limit))
1716 s2n(TLSEXT_TYPE_renegotiate, ret);
1719 if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
1720 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1726 # ifndef OPENSSL_NO_EC
1728 const unsigned char *plist;
1731 * Add TLS extension ECPointFormats to the ServerHello message
1734 tls1_get_formatlist(s, &plist, &plistlen);
1736 if (plistlen > 255) {
1737 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1742 * check for enough space.
1743 * 4 bytes for the ec points format type and extension length
1744 * 1 byte for the points format list length
1745 * + length of points format list
1747 if (CHECKLEN(ret, 5 + plistlen, limit))
1750 s2n(TLSEXT_TYPE_ec_point_formats, ret);
1751 s2n(plistlen + 1, ret);
1752 *(ret++) = (unsigned char)plistlen;
1753 memcpy(ret, plist, plistlen);
1758 * Currently the server should not respond with a SupportedCurves
1761 # endif /* OPENSSL_NO_EC */
1763 if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
1765 * check for enough space.
1766 * 4 bytes for the Ticket type and extension length
1768 if (CHECKLEN(ret, 4, limit))
1770 s2n(TLSEXT_TYPE_session_ticket, ret);
1774 if (s->tlsext_status_expected) {
1776 * check for enough space.
1777 * 4 bytes for the Status request type and extension length
1779 if (CHECKLEN(ret, 4, limit))
1781 s2n(TLSEXT_TYPE_status_request, ret);
1784 # ifdef TLSEXT_TYPE_opaque_prf_input
1785 if (s->s3->server_opaque_prf_input != NULL) {
1786 size_t sol = s->s3->server_opaque_prf_input_len;
1788 if ((long)(limit - ret - 6 - sol) < 0)
1790 if (sol > 0xFFFD) /* can't happen */
1793 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
1796 memcpy(ret, s->s3->server_opaque_prf_input, sol);
1801 # ifndef OPENSSL_NO_SRTP
1802 if (SSL_IS_DTLS(s) && s->srtp_profile) {
1805 ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
1808 * check for enough space.
1809 * 4 bytes for the SRTP profiles type and extension length
1810 * + length of the SRTP profiles list
1812 if (CHECKLEN(ret, 4 + el, limit))
1815 s2n(TLSEXT_TYPE_use_srtp, ret);
1818 if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
1819 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
1826 if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
1827 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
1828 && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
1829 const unsigned char cryptopro_ext[36] = {
1830 0xfd, 0xe8, /* 65000 */
1831 0x00, 0x20, /* 32 bytes length */
1832 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1833 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1834 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1835 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1838 /* check for enough space. */
1839 if (CHECKLEN(ret, sizeof(cryptopro_ext), limit))
1841 memcpy(ret, cryptopro_ext, sizeof(cryptopro_ext));
1842 ret += sizeof(cryptopro_ext);
1845 # ifndef OPENSSL_NO_HEARTBEATS
1846 /* Add Heartbeat extension if we've received one */
1847 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) {
1849 * check for enough space.
1850 * 4 bytes for the Heartbeat type and extension length
1851 * 1 byte for the mode
1853 if (CHECKLEN(ret, 5, limit))
1855 s2n(TLSEXT_TYPE_heartbeat, ret);
1859 * 1: peer may send requests
1860 * 2: peer not allowed to send requests
1862 if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
1863 *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1865 *(ret++) = SSL_TLSEXT_HB_ENABLED;
1870 # ifndef OPENSSL_NO_NEXTPROTONEG
1871 next_proto_neg_seen = s->s3->next_proto_neg_seen;
1872 s->s3->next_proto_neg_seen = 0;
1873 if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
1874 const unsigned char *npa;
1875 unsigned int npalen;
1878 r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
1880 ctx->next_protos_advertised_cb_arg);
1881 if (r == SSL_TLSEXT_ERR_OK) {
1883 * check for enough space.
1884 * 4 bytes for the NPN type and extension length
1885 * + length of protocols list
1887 if (CHECKLEN(ret, 4 + npalen, limit))
1889 s2n(TLSEXT_TYPE_next_proto_neg, ret);
1891 memcpy(ret, npa, npalen);
1893 s->s3->next_proto_neg_seen = 1;
1897 if (!custom_ext_add(s, 1, &ret, limit, al))
1900 if (s->s3->alpn_selected) {
1901 const unsigned char *selected = s->s3->alpn_selected;
1902 size_t len = s->s3->alpn_selected_len;
1905 * check for enough space.
1906 * 4 bytes for the ALPN type and extension length
1907 * 2 bytes for ALPN data length
1908 * 1 byte for selected protocol length
1909 * + length of the selected protocol
1911 if (CHECKLEN(ret, 7 + len, limit))
1913 s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
1917 memcpy(ret, selected, len);
1921 if ((extdatalen = ret - orig - 2) == 0)
1924 s2n(extdatalen, orig);
1928 # ifndef OPENSSL_NO_EC
1930 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1931 * SecureTransport using the TLS extension block in |d|, of length |n|.
1932 * Safari, since 10.6, sends exactly these extensions, in this order:
1937 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1938 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1939 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1940 * 10.8..10.8.3 (which don't work).
1942 static void ssl_check_for_safari(SSL *s, const unsigned char *data,
1943 const unsigned char *limit)
1945 unsigned short type, size;
1946 static const unsigned char kSafariExtensionsBlock[] = {
1947 0x00, 0x0a, /* elliptic_curves extension */
1948 0x00, 0x08, /* 8 bytes */
1949 0x00, 0x06, /* 6 bytes of curve ids */
1950 0x00, 0x17, /* P-256 */
1951 0x00, 0x18, /* P-384 */
1952 0x00, 0x19, /* P-521 */
1954 0x00, 0x0b, /* ec_point_formats */
1955 0x00, 0x02, /* 2 bytes */
1956 0x01, /* 1 point format */
1957 0x00, /* uncompressed */
1960 /* The following is only present in TLS 1.2 */
1961 static const unsigned char kSafariTLS12ExtensionsBlock[] = {
1962 0x00, 0x0d, /* signature_algorithms */
1963 0x00, 0x0c, /* 12 bytes */
1964 0x00, 0x0a, /* 10 bytes */
1965 0x05, 0x01, /* SHA-384/RSA */
1966 0x04, 0x01, /* SHA-256/RSA */
1967 0x02, 0x01, /* SHA-1/RSA */
1968 0x04, 0x03, /* SHA-256/ECDSA */
1969 0x02, 0x03, /* SHA-1/ECDSA */
1972 if (limit - data <= 2)
1976 if (limit - data < 4)
1981 if (type != TLSEXT_TYPE_server_name)
1984 if (limit - data < size)
1988 if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
1989 const size_t len1 = sizeof(kSafariExtensionsBlock);
1990 const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
1992 if (limit - data != (int)(len1 + len2))
1994 if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
1996 if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
1999 const size_t len = sizeof(kSafariExtensionsBlock);
2001 if (limit - data != (int)(len))
2003 if (memcmp(data, kSafariExtensionsBlock, len) != 0)
2007 s->s3->is_probably_safari = 1;
2009 # endif /* !OPENSSL_NO_EC */
2012 * tls1_alpn_handle_client_hello is called to save the ALPN extension in a
2013 * ClientHello. data: the contents of the extension, not including the type
2014 * and length. data_len: the number of bytes in |data| al: a pointer to the
2015 * alert value to send in the event of a non-zero return. returns: 0 on
2018 static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data,
2019 unsigned data_len, int *al)
2028 * data should contain a uint16 length followed by a series of 8-bit,
2029 * length-prefixed strings.
2031 i = ((unsigned)data[0]) << 8 | ((unsigned)data[1]);
2040 for (i = 0; i < data_len;) {
2041 proto_len = data[i];
2047 if (i + proto_len < i || i + proto_len > data_len)
2053 if (s->cert->alpn_proposed != NULL)
2054 OPENSSL_free(s->cert->alpn_proposed);
2055 s->cert->alpn_proposed = OPENSSL_malloc(data_len);
2056 if (s->cert->alpn_proposed == NULL) {
2057 *al = SSL_AD_INTERNAL_ERROR;
2060 memcpy(s->cert->alpn_proposed, data, data_len);
2061 s->cert->alpn_proposed_len = data_len;
2065 *al = SSL_AD_DECODE_ERROR;
2070 * Process the ALPN extension in a ClientHello.
2071 * ret: a pointer to the TLSEXT return value: SSL_TLSEXT_ERR_*
2072 * al: a pointer to the alert value to send in the event of a failure.
2073 * returns 1 on success, 0 on failure: al/ret set only on failure
2075 static int tls1_alpn_handle_client_hello_late(SSL *s, int *ret, int *al)
2077 const unsigned char *selected = NULL;
2078 unsigned char selected_len = 0;
2080 if (s->ctx->alpn_select_cb != NULL && s->cert->alpn_proposed != NULL) {
2081 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
2082 s->cert->alpn_proposed,
2083 s->cert->alpn_proposed_len,
2084 s->ctx->alpn_select_cb_arg);
2086 if (r == SSL_TLSEXT_ERR_OK) {
2087 OPENSSL_free(s->s3->alpn_selected);
2088 s->s3->alpn_selected = OPENSSL_malloc(selected_len);
2089 if (s->s3->alpn_selected == NULL) {
2090 *al = SSL_AD_INTERNAL_ERROR;
2091 *ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2094 memcpy(s->s3->alpn_selected, selected, selected_len);
2095 s->s3->alpn_selected_len = selected_len;
2096 # ifndef OPENSSL_NO_NEXTPROTONEG
2097 /* ALPN takes precedence over NPN. */
2098 s->s3->next_proto_neg_seen = 0;
2106 static int ssl_scan_clienthello_tlsext(SSL *s, unsigned char **p,
2107 unsigned char *limit, int *al)
2109 unsigned short type;
2110 unsigned short size;
2112 unsigned char *data = *p;
2113 int renegotiate_seen = 0;
2115 s->servername_done = 0;
2116 s->tlsext_status_type = -1;
2117 # ifndef OPENSSL_NO_NEXTPROTONEG
2118 s->s3->next_proto_neg_seen = 0;
2121 if (s->s3->alpn_selected) {
2122 OPENSSL_free(s->s3->alpn_selected);
2123 s->s3->alpn_selected = NULL;
2125 s->s3->alpn_selected_len = 0;
2126 if (s->cert->alpn_proposed) {
2127 OPENSSL_free(s->cert->alpn_proposed);
2128 s->cert->alpn_proposed = NULL;
2130 s->cert->alpn_proposed_len = 0;
2131 # ifndef OPENSSL_NO_HEARTBEATS
2132 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2133 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2136 # ifndef OPENSSL_NO_EC
2137 if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
2138 ssl_check_for_safari(s, data, limit);
2139 # endif /* !OPENSSL_NO_EC */
2141 /* Clear any signature algorithms extension received */
2142 if (s->cert->peer_sigalgs) {
2143 OPENSSL_free(s->cert->peer_sigalgs);
2144 s->cert->peer_sigalgs = NULL;
2146 # ifndef OPENSSL_NO_SRP
2147 if (s->srp_ctx.login != NULL) {
2148 OPENSSL_free(s->srp_ctx.login);
2149 s->srp_ctx.login = NULL;
2153 s->srtp_profile = NULL;
2158 if (limit - data < 2)
2163 if (limit - data != len)
2166 while (limit - data >= 4) {
2170 if (limit - data < size)
2173 fprintf(stderr, "Received extension type %d size %d\n", type, size);
2175 if (s->tlsext_debug_cb)
2176 s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
2178 * The servername extension is treated as follows:
2180 * - Only the hostname type is supported with a maximum length of 255.
2181 * - The servername is rejected if too long or if it contains zeros,
2182 * in which case an fatal alert is generated.
2183 * - The servername field is maintained together with the session cache.
2184 * - When a session is resumed, the servername call back invoked in order
2185 * to allow the application to position itself to the right context.
2186 * - The servername is acknowledged if it is new for a session or when
2187 * it is identical to a previously used for the same session.
2188 * Applications can control the behaviour. They can at any time
2189 * set a 'desirable' servername for a new SSL object. This can be the
2190 * case for example with HTTPS when a Host: header field is received and
2191 * a renegotiation is requested. In this case, a possible servername
2192 * presented in the new client hello is only acknowledged if it matches
2193 * the value of the Host: field.
2194 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
2195 * if they provide for changing an explicit servername context for the
2196 * session, i.e. when the session has been established with a servername
2198 * - On session reconnect, the servername extension may be absent.
2202 if (type == TLSEXT_TYPE_server_name) {
2203 unsigned char *sdata;
2216 servname_type = *(sdata++);
2223 if (s->servername_done == 0)
2224 switch (servname_type) {
2225 case TLSEXT_NAMETYPE_host_name:
2227 if (s->session->tlsext_hostname)
2230 if (len > TLSEXT_MAXLEN_host_name) {
2231 *al = TLS1_AD_UNRECOGNIZED_NAME;
2234 if ((s->session->tlsext_hostname =
2235 OPENSSL_malloc(len + 1)) == NULL) {
2236 *al = TLS1_AD_INTERNAL_ERROR;
2239 memcpy(s->session->tlsext_hostname, sdata, len);
2240 s->session->tlsext_hostname[len] = '\0';
2241 if (strlen(s->session->tlsext_hostname) != len) {
2242 OPENSSL_free(s->session->tlsext_hostname);
2243 s->session->tlsext_hostname = NULL;
2244 *al = TLS1_AD_UNRECOGNIZED_NAME;
2247 s->servername_done = 1;
2250 s->servername_done = s->session->tlsext_hostname
2251 && strlen(s->session->tlsext_hostname) == len
2252 && strncmp(s->session->tlsext_hostname,
2253 (char *)sdata, len) == 0;
2267 # ifndef OPENSSL_NO_SRP
2268 else if (type == TLSEXT_TYPE_srp) {
2269 if (size == 0 || ((len = data[0])) != (size - 1))
2271 if (s->srp_ctx.login != NULL)
2273 if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
2275 memcpy(s->srp_ctx.login, &data[1], len);
2276 s->srp_ctx.login[len] = '\0';
2278 if (strlen(s->srp_ctx.login) != len)
2283 # ifndef OPENSSL_NO_EC
2284 else if (type == TLSEXT_TYPE_ec_point_formats) {
2285 unsigned char *sdata = data;
2286 int ecpointformatlist_length = *(sdata++);
2288 if (ecpointformatlist_length != size - 1 ||
2289 ecpointformatlist_length < 1)
2292 if (s->session->tlsext_ecpointformatlist) {
2293 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2294 s->session->tlsext_ecpointformatlist = NULL;
2296 s->session->tlsext_ecpointformatlist_length = 0;
2297 if ((s->session->tlsext_ecpointformatlist =
2298 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2299 *al = TLS1_AD_INTERNAL_ERROR;
2302 s->session->tlsext_ecpointformatlist_length =
2303 ecpointformatlist_length;
2304 memcpy(s->session->tlsext_ecpointformatlist, sdata,
2305 ecpointformatlist_length);
2309 "ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ",
2310 s->session->tlsext_ecpointformatlist_length);
2311 sdata = s->session->tlsext_ecpointformatlist;
2312 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2313 fprintf(stderr, "%i ", *(sdata++));
2314 fprintf(stderr, "\n");
2316 } else if (type == TLSEXT_TYPE_elliptic_curves) {
2317 unsigned char *sdata = data;
2318 int ellipticcurvelist_length = (*(sdata++) << 8);
2319 ellipticcurvelist_length += (*(sdata++));
2321 if (ellipticcurvelist_length != size - 2 ||
2322 ellipticcurvelist_length < 1 ||
2323 /* Each NamedCurve is 2 bytes. */
2324 ellipticcurvelist_length & 1)
2328 if (s->session->tlsext_ellipticcurvelist)
2331 s->session->tlsext_ellipticcurvelist_length = 0;
2332 if ((s->session->tlsext_ellipticcurvelist =
2333 OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
2334 *al = TLS1_AD_INTERNAL_ERROR;
2337 s->session->tlsext_ellipticcurvelist_length =
2338 ellipticcurvelist_length;
2339 memcpy(s->session->tlsext_ellipticcurvelist, sdata,
2340 ellipticcurvelist_length);
2344 "ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ",
2345 s->session->tlsext_ellipticcurvelist_length);
2346 sdata = s->session->tlsext_ellipticcurvelist;
2347 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
2348 fprintf(stderr, "%i ", *(sdata++));
2349 fprintf(stderr, "\n");
2352 # endif /* OPENSSL_NO_EC */
2353 # ifdef TLSEXT_TYPE_opaque_prf_input
2354 else if (type == TLSEXT_TYPE_opaque_prf_input) {
2355 unsigned char *sdata = data;
2358 *al = SSL_AD_DECODE_ERROR;
2361 n2s(sdata, s->s3->client_opaque_prf_input_len);
2362 if (s->s3->client_opaque_prf_input_len != size - 2) {
2363 *al = SSL_AD_DECODE_ERROR;
2367 if (s->s3->client_opaque_prf_input != NULL) {
2368 /* shouldn't really happen */
2369 OPENSSL_free(s->s3->client_opaque_prf_input);
2372 /* dummy byte just to get non-NULL */
2373 if (s->s3->client_opaque_prf_input_len == 0)
2374 s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
2376 s->s3->client_opaque_prf_input =
2377 BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
2378 if (s->s3->client_opaque_prf_input == NULL) {
2379 *al = TLS1_AD_INTERNAL_ERROR;
2384 else if (type == TLSEXT_TYPE_session_ticket) {
2385 if (s->tls_session_ticket_ext_cb &&
2386 !s->tls_session_ticket_ext_cb(s, data, size,
2387 s->tls_session_ticket_ext_cb_arg))
2389 *al = TLS1_AD_INTERNAL_ERROR;
2392 } else if (type == TLSEXT_TYPE_renegotiate) {
2393 if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
2395 renegotiate_seen = 1;
2396 } else if (type == TLSEXT_TYPE_signature_algorithms) {
2398 if (s->cert->peer_sigalgs || size < 2)
2402 if (dsize != size || dsize & 1 || !dsize)
2404 if (!tls1_save_sigalgs(s, data, dsize))
2406 } else if (type == TLSEXT_TYPE_status_request) {
2411 s->tlsext_status_type = *data++;
2413 if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
2414 const unsigned char *sdata;
2416 /* Read in responder_id_list */
2423 * We remove any OCSP_RESPIDs from a previous handshake
2424 * to prevent unbounded memory growth - CVE-2016-6304
2426 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids,
2429 s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
2430 if (s->tlsext_ocsp_ids == NULL) {
2431 *al = SSL_AD_INTERNAL_ERROR;
2435 s->tlsext_ocsp_ids = NULL;
2444 dsize -= 2 + idsize;
2450 id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
2453 if (data != sdata) {
2454 OCSP_RESPID_free(id);
2457 if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
2458 OCSP_RESPID_free(id);
2459 *al = SSL_AD_INTERNAL_ERROR;
2464 /* Read in request_extensions */
2473 if (s->tlsext_ocsp_exts) {
2474 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
2475 X509_EXTENSION_free);
2478 s->tlsext_ocsp_exts =
2479 d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
2480 if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
2485 * We don't know what to do with any other type * so ignore it.
2488 s->tlsext_status_type = -1;
2490 # ifndef OPENSSL_NO_HEARTBEATS
2491 else if (type == TLSEXT_TYPE_heartbeat) {
2493 case 0x01: /* Client allows us to send HB requests */
2494 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2496 case 0x02: /* Client doesn't accept HB requests */
2497 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2498 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2501 *al = SSL_AD_ILLEGAL_PARAMETER;
2506 # ifndef OPENSSL_NO_NEXTPROTONEG
2507 else if (type == TLSEXT_TYPE_next_proto_neg &&
2508 s->s3->tmp.finish_md_len == 0) {
2510 * We shouldn't accept this extension on a
2513 * s->new_session will be set on renegotiation, but we
2514 * probably shouldn't rely that it couldn't be set on
2515 * the initial renegotation too in certain cases (when
2516 * there's some other reason to disallow resuming an
2517 * earlier session -- the current code won't be doing
2518 * anything like that, but this might change).
2520 * A valid sign that there's been a previous handshake
2521 * in this connection is if s->s3->tmp.finish_md_len >
2522 * 0. (We are talking about a check that will happen
2523 * in the Hello protocol round, well before a new
2524 * Finished message could have been computed.)
2526 s->s3->next_proto_neg_seen = 1;
2530 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
2531 s->s3->tmp.finish_md_len == 0) {
2532 if (tls1_alpn_handle_client_hello(s, data, size, al) != 0)
2536 /* session ticket processed earlier */
2537 # ifndef OPENSSL_NO_SRTP
2538 else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
2539 && type == TLSEXT_TYPE_use_srtp) {
2540 if (ssl_parse_clienthello_use_srtp_ext(s, data, size, al))
2548 /* Spurious data on the end */
2556 /* Need RI if renegotiating */
2558 if (!renegotiate_seen && s->renegotiate &&
2559 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2560 *al = SSL_AD_HANDSHAKE_FAILURE;
2561 SSLerr(SSL_F_SSL_SCAN_CLIENTHELLO_TLSEXT,
2562 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2568 *al = SSL_AD_DECODE_ERROR;
2573 * Parse any custom extensions found. "data" is the start of the extension data
2574 * and "limit" is the end of the record. TODO: add strict syntax checking.
2577 static int ssl_scan_clienthello_custom_tlsext(SSL *s,
2578 const unsigned char *data,
2579 const unsigned char *limit,
2582 unsigned short type, size, len;
2583 /* If resumed session or no custom extensions nothing to do */
2584 if (s->hit || s->cert->srv_ext.meths_count == 0)
2587 if (limit - data <= 2)
2591 if (limit - data < len)
2594 while (limit - data >= 4) {
2598 if (limit - data < size)
2600 if (custom_ext_parse(s, 1 /* server */ , type, data, size, al) <= 0)
2609 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p,
2610 unsigned char *limit)
2613 unsigned char *ptmp = *p;
2615 * Internally supported extensions are parsed first so SNI can be handled
2616 * before custom extensions. An application processing SNI will typically
2617 * switch the parent context using SSL_set_SSL_CTX and custom extensions
2618 * need to be handled by the new SSL_CTX structure.
2620 if (ssl_scan_clienthello_tlsext(s, p, limit, &al) <= 0) {
2621 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2625 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
2626 SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, SSL_R_CLIENTHELLO_TLSEXT);
2630 custom_ext_init(&s->cert->srv_ext);
2631 if (ssl_scan_clienthello_custom_tlsext(s, ptmp, limit, &al) <= 0) {
2632 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2639 # ifndef OPENSSL_NO_NEXTPROTONEG
2641 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2642 * elements of zero length are allowed and the set of elements must exactly
2643 * fill the length of the block.
2645 static char ssl_next_proto_validate(unsigned char *d, unsigned len)
2647 unsigned int off = 0;
2660 static int ssl_scan_serverhello_tlsext(SSL *s, unsigned char **p,
2661 unsigned char *d, int n, int *al)
2663 unsigned short length;
2664 unsigned short type;
2665 unsigned short size;
2666 unsigned char *data = *p;
2667 int tlsext_servername = 0;
2668 int renegotiate_seen = 0;
2670 # ifndef OPENSSL_NO_NEXTPROTONEG
2671 s->s3->next_proto_neg_seen = 0;
2673 s->tlsext_ticket_expected = 0;
2675 if (s->s3->alpn_selected) {
2676 OPENSSL_free(s->s3->alpn_selected);
2677 s->s3->alpn_selected = NULL;
2679 # ifndef OPENSSL_NO_HEARTBEATS
2680 s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
2681 SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
2684 if ((d + n) - data <= 2)
2688 if ((d + n) - data != length) {
2689 *al = SSL_AD_DECODE_ERROR;
2693 while ((d + n) - data >= 4) {
2697 if ((d + n) - data < size)
2700 if (s->tlsext_debug_cb)
2701 s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
2703 if (type == TLSEXT_TYPE_server_name) {
2704 if (s->tlsext_hostname == NULL || size > 0) {
2705 *al = TLS1_AD_UNRECOGNIZED_NAME;
2708 tlsext_servername = 1;
2710 # ifndef OPENSSL_NO_EC
2711 else if (type == TLSEXT_TYPE_ec_point_formats) {
2712 unsigned char *sdata = data;
2713 int ecpointformatlist_length = *(sdata++);
2715 if (ecpointformatlist_length != size - 1) {
2716 *al = TLS1_AD_DECODE_ERROR;
2720 s->session->tlsext_ecpointformatlist_length = 0;
2721 if (s->session->tlsext_ecpointformatlist != NULL)
2722 OPENSSL_free(s->session->tlsext_ecpointformatlist);
2723 if ((s->session->tlsext_ecpointformatlist =
2724 OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
2725 *al = TLS1_AD_INTERNAL_ERROR;
2728 s->session->tlsext_ecpointformatlist_length =
2729 ecpointformatlist_length;
2730 memcpy(s->session->tlsext_ecpointformatlist, sdata,
2731 ecpointformatlist_length);
2735 "ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
2736 sdata = s->session->tlsext_ecpointformatlist;
2737 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
2738 fprintf(stderr, "%i ", *(sdata++));
2739 fprintf(stderr, "\n");
2742 # endif /* OPENSSL_NO_EC */
2744 else if (type == TLSEXT_TYPE_session_ticket) {
2745 if (s->tls_session_ticket_ext_cb &&
2746 !s->tls_session_ticket_ext_cb(s, data, size,
2747 s->tls_session_ticket_ext_cb_arg))
2749 *al = TLS1_AD_INTERNAL_ERROR;
2752 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
2754 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2757 s->tlsext_ticket_expected = 1;
2759 # ifdef TLSEXT_TYPE_opaque_prf_input
2760 else if (type == TLSEXT_TYPE_opaque_prf_input) {
2761 unsigned char *sdata = data;
2764 *al = SSL_AD_DECODE_ERROR;
2767 n2s(sdata, s->s3->server_opaque_prf_input_len);
2768 if (s->s3->server_opaque_prf_input_len != size - 2) {
2769 *al = SSL_AD_DECODE_ERROR;
2773 if (s->s3->server_opaque_prf_input != NULL) {
2774 /* shouldn't really happen */
2775 OPENSSL_free(s->s3->server_opaque_prf_input);
2777 if (s->s3->server_opaque_prf_input_len == 0) {
2778 /* dummy byte just to get non-NULL */
2779 s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
2781 s->s3->server_opaque_prf_input =
2782 BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
2785 if (s->s3->server_opaque_prf_input == NULL) {
2786 *al = TLS1_AD_INTERNAL_ERROR;
2791 else if (type == TLSEXT_TYPE_status_request) {
2793 * MUST be empty and only sent if we've requested a status
2796 if ((s->tlsext_status_type == -1) || (size > 0)) {
2797 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2800 /* Set flag to expect CertificateStatus message */
2801 s->tlsext_status_expected = 1;
2803 # ifndef OPENSSL_NO_NEXTPROTONEG
2804 else if (type == TLSEXT_TYPE_next_proto_neg &&
2805 s->s3->tmp.finish_md_len == 0) {
2806 unsigned char *selected;
2807 unsigned char selected_len;
2809 /* We must have requested it. */
2810 if (s->ctx->next_proto_select_cb == NULL) {
2811 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2814 /* The data must be valid */
2815 if (!ssl_next_proto_validate(data, size)) {
2816 *al = TLS1_AD_DECODE_ERROR;
2820 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
2822 s->ctx->next_proto_select_cb_arg) !=
2823 SSL_TLSEXT_ERR_OK) {
2824 *al = TLS1_AD_INTERNAL_ERROR;
2828 * Could be non-NULL if server has sent multiple NPN extensions in
2829 * a single Serverhello
2831 OPENSSL_free(s->next_proto_negotiated);
2832 s->next_proto_negotiated = OPENSSL_malloc(selected_len);
2833 if (!s->next_proto_negotiated) {
2834 *al = TLS1_AD_INTERNAL_ERROR;
2837 memcpy(s->next_proto_negotiated, selected, selected_len);
2838 s->next_proto_negotiated_len = selected_len;
2839 s->s3->next_proto_neg_seen = 1;
2843 else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) {
2846 /* We must have requested it. */
2847 if (!s->cert->alpn_sent) {
2848 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
2852 *al = TLS1_AD_DECODE_ERROR;
2856 * The extension data consists of:
2857 * uint16 list_length
2858 * uint8 proto_length;
2859 * uint8 proto[proto_length];
2864 if (len != (unsigned)size - 2) {
2865 *al = TLS1_AD_DECODE_ERROR;
2869 if (len != (unsigned)size - 3) {
2870 *al = TLS1_AD_DECODE_ERROR;
2873 if (s->s3->alpn_selected)
2874 OPENSSL_free(s->s3->alpn_selected);
2875 s->s3->alpn_selected = OPENSSL_malloc(len);
2876 if (!s->s3->alpn_selected) {
2877 *al = TLS1_AD_INTERNAL_ERROR;
2880 memcpy(s->s3->alpn_selected, data + 3, len);
2881 s->s3->alpn_selected_len = len;
2884 else if (type == TLSEXT_TYPE_renegotiate) {
2885 if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
2887 renegotiate_seen = 1;
2889 # ifndef OPENSSL_NO_HEARTBEATS
2890 else if (type == TLSEXT_TYPE_heartbeat) {
2892 case 0x01: /* Server allows us to send HB requests */
2893 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2895 case 0x02: /* Server doesn't accept HB requests */
2896 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
2897 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
2900 *al = SSL_AD_ILLEGAL_PARAMETER;
2905 # ifndef OPENSSL_NO_SRTP
2906 else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
2907 if (ssl_parse_serverhello_use_srtp_ext(s, data, size, al))
2912 * If this extension type was not otherwise handled, but matches a
2913 * custom_cli_ext_record, then send it to the c callback
2915 else if (custom_ext_parse(s, 0, type, data, size, al) <= 0)
2921 if (data != d + n) {
2922 *al = SSL_AD_DECODE_ERROR;
2926 if (!s->hit && tlsext_servername == 1) {
2927 if (s->tlsext_hostname) {
2928 if (s->session->tlsext_hostname == NULL) {
2929 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
2930 if (!s->session->tlsext_hostname) {
2931 *al = SSL_AD_UNRECOGNIZED_NAME;
2935 *al = SSL_AD_DECODE_ERROR;
2946 * Determine if we need to see RI. Strictly speaking if we want to avoid
2947 * an attack we should *always* see RI even on initial server hello
2948 * because the client doesn't see any renegotiation during an attack.
2949 * However this would mean we could not connect to any server which
2950 * doesn't support RI so for the immediate future tolerate RI absence on
2951 * initial connect only.
2953 if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
2954 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
2955 *al = SSL_AD_HANDSHAKE_FAILURE;
2956 SSLerr(SSL_F_SSL_SCAN_SERVERHELLO_TLSEXT,
2957 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
2964 int ssl_prepare_clienthello_tlsext(SSL *s)
2967 # ifdef TLSEXT_TYPE_opaque_prf_input
2971 if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
2972 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
2974 ctx->tlsext_opaque_prf_input_callback_arg);
2979 if (s->tlsext_opaque_prf_input != NULL) {
2980 if (s->s3->client_opaque_prf_input != NULL) {
2981 /* shouldn't really happen */
2982 OPENSSL_free(s->s3->client_opaque_prf_input);
2985 if (s->tlsext_opaque_prf_input_len == 0) {
2986 /* dummy byte just to get non-NULL */
2987 s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
2989 s->s3->client_opaque_prf_input =
2990 BUF_memdup(s->tlsext_opaque_prf_input,
2991 s->tlsext_opaque_prf_input_len);
2993 if (s->s3->client_opaque_prf_input == NULL) {
2994 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,
2995 ERR_R_MALLOC_FAILURE);
2998 s->s3->client_opaque_prf_input_len =
2999 s->tlsext_opaque_prf_input_len;
3004 * at callback's request, insist on receiving an appropriate
3005 * server opaque PRF input
3007 s->s3->server_opaque_prf_input_len =
3008 s->tlsext_opaque_prf_input_len;
3012 s->cert->alpn_sent = 0;
3016 int ssl_prepare_serverhello_tlsext(SSL *s)
3021 static int ssl_check_clienthello_tlsext_early(SSL *s)
3023 int ret = SSL_TLSEXT_ERR_NOACK;
3024 int al = SSL_AD_UNRECOGNIZED_NAME;
3026 # ifndef OPENSSL_NO_EC
3028 * The handling of the ECPointFormats extension is done elsewhere, namely
3029 * in ssl3_choose_cipher in s3_lib.c.
3032 * The handling of the EllipticCurves extension is done elsewhere, namely
3033 * in ssl3_choose_cipher in s3_lib.c.
3037 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3039 s->ctx->tlsext_servername_callback(s, &al,
3040 s->ctx->tlsext_servername_arg);
3041 else if (s->initial_ctx != NULL
3042 && s->initial_ctx->tlsext_servername_callback != 0)
3044 s->initial_ctx->tlsext_servername_callback(s, &al,
3046 initial_ctx->tlsext_servername_arg);
3048 # ifdef TLSEXT_TYPE_opaque_prf_input
3051 * This sort of belongs into ssl_prepare_serverhello_tlsext(), but we
3052 * might be sending an alert in response to the client hello, so this
3053 * has to happen here in ssl_check_clienthello_tlsext_early().
3058 if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
3059 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
3061 ctx->tlsext_opaque_prf_input_callback_arg);
3063 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3064 al = SSL_AD_INTERNAL_ERROR;
3069 if (s->s3->server_opaque_prf_input != NULL) {
3070 /* shouldn't really happen */
3071 OPENSSL_free(s->s3->server_opaque_prf_input);
3073 s->s3->server_opaque_prf_input = NULL;
3075 if (s->tlsext_opaque_prf_input != NULL) {
3076 if (s->s3->client_opaque_prf_input != NULL &&
3077 s->s3->client_opaque_prf_input_len ==
3078 s->tlsext_opaque_prf_input_len) {
3080 * can only use this extension if we have a server opaque PRF
3081 * input of the same length as the client opaque PRF input!
3084 if (s->tlsext_opaque_prf_input_len == 0) {
3085 /* dummy byte just to get non-NULL */
3086 s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
3088 s->s3->server_opaque_prf_input =
3089 BUF_memdup(s->tlsext_opaque_prf_input,
3090 s->tlsext_opaque_prf_input_len);
3092 if (s->s3->server_opaque_prf_input == NULL) {
3093 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3094 al = SSL_AD_INTERNAL_ERROR;
3097 s->s3->server_opaque_prf_input_len =
3098 s->tlsext_opaque_prf_input_len;
3102 if (r == 2 && s->s3->server_opaque_prf_input == NULL) {
3104 * The callback wants to enforce use of the extension, but we
3105 * can't do that with the client opaque PRF input; abort the
3108 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3109 al = SSL_AD_HANDSHAKE_FAILURE;
3116 case SSL_TLSEXT_ERR_ALERT_FATAL:
3117 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3120 case SSL_TLSEXT_ERR_ALERT_WARNING:
3121 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3124 case SSL_TLSEXT_ERR_NOACK:
3125 s->servername_done = 0;
3131 int tls1_set_server_sigalgs(SSL *s)
3135 /* Clear any shared sigtnature algorithms */
3136 if (s->cert->shared_sigalgs) {
3137 OPENSSL_free(s->cert->shared_sigalgs);
3138 s->cert->shared_sigalgs = NULL;
3139 s->cert->shared_sigalgslen = 0;
3141 /* Clear certificate digests and validity flags */
3142 for (i = 0; i < SSL_PKEY_NUM; i++) {
3143 s->cert->pkeys[i].digest = NULL;
3144 s->cert->pkeys[i].valid_flags = 0;
3147 /* If sigalgs received process it. */
3148 if (s->cert->peer_sigalgs) {
3149 if (!tls1_process_sigalgs(s)) {
3150 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_MALLOC_FAILURE);
3151 al = SSL_AD_INTERNAL_ERROR;
3154 /* Fatal error is no shared signature algorithms */
3155 if (!s->cert->shared_sigalgs) {
3156 SSLerr(SSL_F_TLS1_SET_SERVER_SIGALGS,
3157 SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
3158 al = SSL_AD_ILLEGAL_PARAMETER;
3162 ssl_cert_set_default_md(s->cert);
3165 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3169 int ssl_check_clienthello_tlsext_late(SSL *s)
3171 int ret = SSL_TLSEXT_ERR_OK;
3175 * If status request then ask callback what to do. Note: this must be
3176 * called after servername callbacks in case the certificate has changed,
3177 * and must be called after the cipher has been chosen because this may
3178 * influence which certificate is sent
3180 if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
3182 CERT_PKEY *certpkey;
3183 certpkey = ssl_get_server_send_pkey(s);
3184 /* If no certificate can't return certificate status */
3185 if (certpkey == NULL) {
3186 s->tlsext_status_expected = 0;
3190 * Set current certificate to one we will use so SSL_get_certificate
3191 * et al can pick it up.
3193 s->cert->key = certpkey;
3194 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3196 /* We don't want to send a status request response */
3197 case SSL_TLSEXT_ERR_NOACK:
3198 s->tlsext_status_expected = 0;
3200 /* status request response should be sent */
3201 case SSL_TLSEXT_ERR_OK:
3202 if (s->tlsext_ocsp_resp)
3203 s->tlsext_status_expected = 1;
3205 s->tlsext_status_expected = 0;
3207 /* something bad happened */
3208 case SSL_TLSEXT_ERR_ALERT_FATAL:
3209 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3210 al = SSL_AD_INTERNAL_ERROR;
3214 s->tlsext_status_expected = 0;
3216 if (!tls1_alpn_handle_client_hello_late(s, &ret, &al)) {
3222 case SSL_TLSEXT_ERR_ALERT_FATAL:
3223 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3226 case SSL_TLSEXT_ERR_ALERT_WARNING:
3227 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3235 int ssl_check_serverhello_tlsext(SSL *s)
3237 int ret = SSL_TLSEXT_ERR_NOACK;
3238 int al = SSL_AD_UNRECOGNIZED_NAME;
3240 # ifndef OPENSSL_NO_EC
3242 * If we are client and using an elliptic curve cryptography cipher
3243 * suite, then if server returns an EC point formats lists extension it
3244 * must contain uncompressed.
3246 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3247 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3248 if ((s->tlsext_ecpointformatlist != NULL)
3249 && (s->tlsext_ecpointformatlist_length > 0)
3250 && (s->session->tlsext_ecpointformatlist != NULL)
3251 && (s->session->tlsext_ecpointformatlist_length > 0)
3252 && ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
3253 || (alg_a & SSL_aECDSA))) {
3254 /* we are using an ECC cipher */
3256 unsigned char *list;
3257 int found_uncompressed = 0;
3258 list = s->session->tlsext_ecpointformatlist;
3259 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
3260 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
3261 found_uncompressed = 1;
3265 if (!found_uncompressed) {
3266 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
3267 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
3271 ret = SSL_TLSEXT_ERR_OK;
3272 # endif /* OPENSSL_NO_EC */
3274 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
3276 s->ctx->tlsext_servername_callback(s, &al,
3277 s->ctx->tlsext_servername_arg);
3278 else if (s->initial_ctx != NULL
3279 && s->initial_ctx->tlsext_servername_callback != 0)
3281 s->initial_ctx->tlsext_servername_callback(s, &al,
3283 initial_ctx->tlsext_servername_arg);
3285 # ifdef TLSEXT_TYPE_opaque_prf_input
3286 if (s->s3->server_opaque_prf_input_len > 0) {
3288 * This case may indicate that we, as a client, want to insist on
3289 * using opaque PRF inputs. So first verify that we really have a
3290 * value from the server too.
3293 if (s->s3->server_opaque_prf_input == NULL) {
3294 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3295 al = SSL_AD_HANDSHAKE_FAILURE;
3299 * Anytime the server *has* sent an opaque PRF input, we need to
3300 * check that we have a client opaque PRF input of the same size.
3302 if (s->s3->client_opaque_prf_input == NULL ||
3303 s->s3->client_opaque_prf_input_len !=
3304 s->s3->server_opaque_prf_input_len) {
3305 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3306 al = SSL_AD_ILLEGAL_PARAMETER;
3311 OPENSSL_free(s->tlsext_ocsp_resp);
3312 s->tlsext_ocsp_resp = NULL;
3313 s->tlsext_ocsp_resplen = -1;
3315 * If we've requested certificate status and we wont get one tell the
3318 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
3319 && !(s->hit) && s->ctx && s->ctx->tlsext_status_cb) {
3322 * Call callback with resp == NULL and resplen == -1 so callback
3323 * knows there is no response
3325 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
3327 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
3328 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3331 al = SSL_AD_INTERNAL_ERROR;
3332 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
3337 case SSL_TLSEXT_ERR_ALERT_FATAL:
3338 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3341 case SSL_TLSEXT_ERR_ALERT_WARNING:
3342 ssl3_send_alert(s, SSL3_AL_WARNING, al);
3345 case SSL_TLSEXT_ERR_NOACK:
3346 s->servername_done = 0;
3352 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
3356 if (s->version < SSL3_VERSION)
3358 if (ssl_scan_serverhello_tlsext(s, p, d, n, &al) <= 0) {
3359 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3363 if (ssl_check_serverhello_tlsext(s) <= 0) {
3364 SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, SSL_R_SERVERHELLO_TLSEXT);
3371 * Since the server cache lookup is done early on in the processing of the
3372 * ClientHello, and other operations depend on the result, we need to handle
3373 * any TLS session ticket extension at the same time.
3375 * session_id: points at the session ID in the ClientHello. This code will
3376 * read past the end of this in order to parse out the session ticket
3377 * extension, if any.
3378 * len: the length of the session ID.
3379 * limit: a pointer to the first byte after the ClientHello.
3380 * ret: (output) on return, if a ticket was decrypted, then this is set to
3381 * point to the resulting session.
3383 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
3384 * ciphersuite, in which case we have no use for session tickets and one will
3385 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
3388 * -1: fatal error, either from parsing or decrypting the ticket.
3389 * 0: no ticket was found (or was ignored, based on settings).
3390 * 1: a zero length extension was found, indicating that the client supports
3391 * session tickets but doesn't currently have one to offer.
3392 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
3393 * couldn't be decrypted because of a non-fatal error.
3394 * 3: a ticket was successfully decrypted and *ret was set.
3397 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
3398 * a new session ticket to the client because the client indicated support
3399 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
3400 * a session ticket or we couldn't use the one it gave us, or if
3401 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
3402 * Otherwise, s->tlsext_ticket_expected is set to 0.
3404 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
3405 const unsigned char *limit, SSL_SESSION **ret)
3407 /* Point after session ID in client hello */
3408 const unsigned char *p = session_id + len;
3412 s->tlsext_ticket_expected = 0;
3415 * If tickets disabled behave as if no ticket present to permit stateful
3418 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
3420 if ((s->version <= SSL3_VERSION) || !limit)
3424 /* Skip past DTLS cookie */
3425 if (SSL_IS_DTLS(s)) {
3433 /* Skip past cipher list */
3439 /* Skip past compression algorithm list */
3445 /* Now at start of extensions */
3449 while (limit - p >= 4) {
3450 unsigned short type, size;
3453 if (limit - p < size)
3455 if (type == TLSEXT_TYPE_session_ticket) {
3459 * The client will accept a ticket but doesn't currently have
3462 s->tlsext_ticket_expected = 1;
3465 if (s->tls_session_secret_cb) {
3467 * Indicate that the ticket couldn't be decrypted rather than
3468 * generating the session from ticket now, trigger
3469 * abbreviated handshake based on external mechanism to
3470 * calculate the master secret later.
3474 r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
3476 case 2: /* ticket couldn't be decrypted */
3477 s->tlsext_ticket_expected = 1;
3479 case 3: /* ticket was decrypted */
3481 case 4: /* ticket decrypted but need to renew */
3482 s->tlsext_ticket_expected = 1;
3484 default: /* fatal error */
3494 * tls_decrypt_ticket attempts to decrypt a session ticket.
3496 * etick: points to the body of the session ticket extension.
3497 * eticklen: the length of the session tickets extenion.
3498 * sess_id: points at the session ID.
3499 * sesslen: the length of the session ID.
3500 * psess: (output) on return, if a ticket was decrypted, then this is set to
3501 * point to the resulting session.
3504 * -1: fatal error, either from parsing or decrypting the ticket.
3505 * 2: the ticket couldn't be decrypted.
3506 * 3: a ticket was successfully decrypted and *psess was set.
3507 * 4: same as 3, but the ticket needs to be renewed.
3509 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
3510 int eticklen, const unsigned char *sess_id,
3511 int sesslen, SSL_SESSION **psess)
3514 unsigned char *sdec;
3515 const unsigned char *p;
3516 int slen, mlen, renew_ticket = 0;
3517 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
3520 SSL_CTX *tctx = s->initial_ctx;
3522 /* Initialize session ticket encryption and HMAC contexts */
3523 HMAC_CTX_init(&hctx);
3524 EVP_CIPHER_CTX_init(&ctx);
3525 if (tctx->tlsext_ticket_key_cb) {
3526 unsigned char *nctick = (unsigned char *)etick;
3527 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
3536 /* Check key name matches */
3537 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
3539 if (HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3540 tlsext_tick_md(), NULL) <= 0
3541 || EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3542 tctx->tlsext_tick_aes_key,
3548 * Attempt to process session ticket, first conduct sanity and integrity
3551 mlen = HMAC_size(&hctx);
3555 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
3556 if (eticklen <= 16 + EVP_CIPHER_CTX_iv_length(&ctx) + mlen) {
3557 HMAC_CTX_cleanup(&hctx);
3558 EVP_CIPHER_CTX_cleanup(&ctx);
3563 /* Check HMAC of encrypted ticket */
3564 if (HMAC_Update(&hctx, etick, eticklen) <= 0
3565 || HMAC_Final(&hctx, tick_hmac, NULL) <= 0) {
3568 HMAC_CTX_cleanup(&hctx);
3569 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
3570 EVP_CIPHER_CTX_cleanup(&ctx);
3573 /* Attempt to decrypt session data */
3574 /* Move p after IV to start of encrypted ticket, update length */
3575 p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3576 eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
3577 sdec = OPENSSL_malloc(eticklen);
3579 || EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen) <= 0) {
3580 EVP_CIPHER_CTX_cleanup(&ctx);
3584 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
3585 EVP_CIPHER_CTX_cleanup(&ctx);
3590 EVP_CIPHER_CTX_cleanup(&ctx);
3593 sess = d2i_SSL_SESSION(NULL, &p, slen);
3597 * The session ID, if non-empty, is used by some clients to detect
3598 * that the ticket has been accepted. So we copy it to the session
3599 * structure. If it is empty set length to zero as required by
3603 memcpy(sess->session_id, sess_id, sesslen);
3604 sess->session_id_length = sesslen;
3613 * For session parse failure, indicate that we need to send a new ticket.
3617 EVP_CIPHER_CTX_cleanup(&ctx);
3618 HMAC_CTX_cleanup(&hctx);
3622 /* Tables to translate from NIDs to TLS v1.2 ids */
3629 static tls12_lookup tls12_md[] = {
3630 {NID_md5, TLSEXT_hash_md5},
3631 {NID_sha1, TLSEXT_hash_sha1},
3632 {NID_sha224, TLSEXT_hash_sha224},
3633 {NID_sha256, TLSEXT_hash_sha256},
3634 {NID_sha384, TLSEXT_hash_sha384},
3635 {NID_sha512, TLSEXT_hash_sha512}
3638 static tls12_lookup tls12_sig[] = {
3639 {EVP_PKEY_RSA, TLSEXT_signature_rsa},
3640 {EVP_PKEY_DSA, TLSEXT_signature_dsa},
3641 {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
3644 static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
3647 for (i = 0; i < tlen; i++) {
3648 if (table[i].nid == nid)
3654 static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
3657 for (i = 0; i < tlen; i++) {
3658 if ((table[i].id) == id)
3659 return table[i].nid;
3664 int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
3670 md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
3671 sizeof(tls12_md) / sizeof(tls12_lookup));
3674 sig_id = tls12_get_sigid(pk);
3677 p[0] = (unsigned char)md_id;
3678 p[1] = (unsigned char)sig_id;
3682 int tls12_get_sigid(const EVP_PKEY *pk)
3684 return tls12_find_id(pk->type, tls12_sig,
3685 sizeof(tls12_sig) / sizeof(tls12_lookup));
3688 const EVP_MD *tls12_get_hash(unsigned char hash_alg)
3691 # ifndef OPENSSL_NO_MD5
3692 case TLSEXT_hash_md5:
3693 # ifdef OPENSSL_FIPS
3699 # ifndef OPENSSL_NO_SHA
3700 case TLSEXT_hash_sha1:
3703 # ifndef OPENSSL_NO_SHA256
3704 case TLSEXT_hash_sha224:
3705 return EVP_sha224();
3707 case TLSEXT_hash_sha256:
3708 return EVP_sha256();
3710 # ifndef OPENSSL_NO_SHA512
3711 case TLSEXT_hash_sha384:
3712 return EVP_sha384();
3714 case TLSEXT_hash_sha512:
3715 return EVP_sha512();
3723 static int tls12_get_pkey_idx(unsigned char sig_alg)
3726 # ifndef OPENSSL_NO_RSA
3727 case TLSEXT_signature_rsa:
3728 return SSL_PKEY_RSA_SIGN;
3730 # ifndef OPENSSL_NO_DSA
3731 case TLSEXT_signature_dsa:
3732 return SSL_PKEY_DSA_SIGN;
3734 # ifndef OPENSSL_NO_ECDSA
3735 case TLSEXT_signature_ecdsa:
3736 return SSL_PKEY_ECC;
3742 /* Convert TLS 1.2 signature algorithm extension values into NIDs */
3743 static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
3744 int *psignhash_nid, const unsigned char *data)
3746 int sign_nid = NID_undef, hash_nid = NID_undef;
3747 if (!phash_nid && !psign_nid && !psignhash_nid)
3749 if (phash_nid || psignhash_nid) {
3750 hash_nid = tls12_find_nid(data[0], tls12_md,
3751 sizeof(tls12_md) / sizeof(tls12_lookup));
3753 *phash_nid = hash_nid;
3755 if (psign_nid || psignhash_nid) {
3756 sign_nid = tls12_find_nid(data[1], tls12_sig,
3757 sizeof(tls12_sig) / sizeof(tls12_lookup));
3759 *psign_nid = sign_nid;
3761 if (psignhash_nid) {
3762 if (sign_nid == NID_undef || hash_nid == NID_undef
3763 || OBJ_find_sigid_by_algs(psignhash_nid, hash_nid,
3765 *psignhash_nid = NID_undef;
3769 /* Given preference and allowed sigalgs set shared sigalgs */
3770 static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig,
3771 const unsigned char *pref, size_t preflen,
3772 const unsigned char *allow,
3775 const unsigned char *ptmp, *atmp;
3776 size_t i, j, nmatch = 0;
3777 for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
3778 /* Skip disabled hashes or signature algorithms */
3779 if (tls12_get_hash(ptmp[0]) == NULL)
3781 if (tls12_get_pkey_idx(ptmp[1]) == -1)
3783 for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
3784 if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
3787 shsig->rhash = ptmp[0];
3788 shsig->rsign = ptmp[1];
3789 tls1_lookup_sigalg(&shsig->hash_nid,
3791 &shsig->signandhash_nid, ptmp);
3801 /* Set shared signature algorithms for SSL structures */
3802 static int tls1_set_shared_sigalgs(SSL *s)
3804 const unsigned char *pref, *allow, *conf;
3805 size_t preflen, allowlen, conflen;
3807 TLS_SIGALGS *salgs = NULL;
3809 unsigned int is_suiteb = tls1_suiteb(s);
3810 if (c->shared_sigalgs) {
3811 OPENSSL_free(c->shared_sigalgs);
3812 c->shared_sigalgs = NULL;
3813 c->shared_sigalgslen = 0;
3815 /* If client use client signature algorithms if not NULL */
3816 if (!s->server && c->client_sigalgs && !is_suiteb) {
3817 conf = c->client_sigalgs;
3818 conflen = c->client_sigalgslen;
3819 } else if (c->conf_sigalgs && !is_suiteb) {
3820 conf = c->conf_sigalgs;
3821 conflen = c->conf_sigalgslen;
3823 conflen = tls12_get_psigalgs(s, &conf);
3824 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
3827 allow = c->peer_sigalgs;
3828 allowlen = c->peer_sigalgslen;
3832 pref = c->peer_sigalgs;
3833 preflen = c->peer_sigalgslen;
3835 nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
3837 salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
3840 nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
3844 c->shared_sigalgs = salgs;
3845 c->shared_sigalgslen = nmatch;
3849 /* Set preferred digest for each key type */
3851 int tls1_save_sigalgs(SSL *s, const unsigned char *data, int dsize)
3854 /* Extension ignored for inappropriate versions */
3855 if (!SSL_USE_SIGALGS(s))
3857 /* Should never happen */
3861 if (c->peer_sigalgs)
3862 OPENSSL_free(c->peer_sigalgs);
3863 c->peer_sigalgs = OPENSSL_malloc(dsize);
3864 if (!c->peer_sigalgs)
3866 c->peer_sigalgslen = dsize;
3867 memcpy(c->peer_sigalgs, data, dsize);
3871 int tls1_process_sigalgs(SSL *s)
3877 TLS_SIGALGS *sigptr;
3878 if (!tls1_set_shared_sigalgs(s))
3881 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3882 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
3884 * Use first set signature preference to force message digest,
3885 * ignoring any peer preferences.
3887 const unsigned char *sigs = NULL;
3889 sigs = c->conf_sigalgs;
3891 sigs = c->client_sigalgs;
3893 idx = tls12_get_pkey_idx(sigs[1]);
3894 md = tls12_get_hash(sigs[0]);
3895 c->pkeys[idx].digest = md;
3896 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3897 if (idx == SSL_PKEY_RSA_SIGN) {
3898 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags =
3899 CERT_PKEY_EXPLICIT_SIGN;
3900 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3906 for (i = 0, sigptr = c->shared_sigalgs;
3907 i < c->shared_sigalgslen; i++, sigptr++) {
3908 idx = tls12_get_pkey_idx(sigptr->rsign);
3909 if (idx > 0 && c->pkeys[idx].digest == NULL) {
3910 md = tls12_get_hash(sigptr->rhash);
3911 c->pkeys[idx].digest = md;
3912 c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN;
3913 if (idx == SSL_PKEY_RSA_SIGN) {
3914 c->pkeys[SSL_PKEY_RSA_ENC].valid_flags =
3915 CERT_PKEY_EXPLICIT_SIGN;
3916 c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
3922 * In strict mode leave unset digests as NULL to indicate we can't use
3923 * the certificate for signing.
3925 if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
3927 * Set any remaining keys to default values. NOTE: if alg is not
3928 * supported it stays as NULL.
3930 # ifndef OPENSSL_NO_DSA
3931 if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
3932 c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
3934 # ifndef OPENSSL_NO_RSA
3935 if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) {
3936 c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
3937 c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
3940 # ifndef OPENSSL_NO_ECDSA
3941 if (!c->pkeys[SSL_PKEY_ECC].digest)
3942 c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
3948 int SSL_get_sigalgs(SSL *s, int idx,
3949 int *psign, int *phash, int *psignhash,
3950 unsigned char *rsig, unsigned char *rhash)
3952 const unsigned char *psig = s->cert->peer_sigalgs;
3957 if (idx >= (int)s->cert->peer_sigalgslen)
3964 tls1_lookup_sigalg(phash, psign, psignhash, psig);
3966 return s->cert->peer_sigalgslen / 2;
3969 int SSL_get_shared_sigalgs(SSL *s, int idx,
3970 int *psign, int *phash, int *psignhash,
3971 unsigned char *rsig, unsigned char *rhash)
3973 TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
3974 if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen)
3978 *phash = shsigalgs->hash_nid;
3980 *psign = shsigalgs->sign_nid;
3982 *psignhash = shsigalgs->signandhash_nid;
3984 *rsig = shsigalgs->rsign;
3986 *rhash = shsigalgs->rhash;
3987 return s->cert->shared_sigalgslen;
3990 # ifndef OPENSSL_NO_HEARTBEATS
3991 int tls1_process_heartbeat(SSL *s)
3993 unsigned char *p = &s->s3->rrec.data[0], *pl;
3994 unsigned short hbtype;
3995 unsigned int payload;
3996 unsigned int padding = 16; /* Use minimum padding */
3998 if (s->msg_callback)
3999 s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
4000 &s->s3->rrec.data[0], s->s3->rrec.length,
4001 s, s->msg_callback_arg);
4003 /* Read type and payload length first */
4004 if (1 + 2 + 16 > s->s3->rrec.length)
4005 return 0; /* silently discard */
4008 if (1 + 2 + payload + 16 > s->s3->rrec.length)
4009 return 0; /* silently discard per RFC 6520 sec. 4 */
4012 if (hbtype == TLS1_HB_REQUEST) {
4013 unsigned char *buffer, *bp;
4017 * Allocate memory for the response, size is 1 bytes message type,
4018 * plus 2 bytes payload length, plus payload, plus padding
4020 buffer = OPENSSL_malloc(1 + 2 + payload + padding);
4025 /* Enter response type, length and copy payload */
4026 *bp++ = TLS1_HB_RESPONSE;
4028 memcpy(bp, pl, payload);
4030 /* Random padding */
4031 if (RAND_bytes(bp, padding) <= 0) {
4032 OPENSSL_free(buffer);
4036 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,
4037 3 + payload + padding);
4039 if (r >= 0 && s->msg_callback)
4040 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4041 buffer, 3 + payload + padding,
4042 s, s->msg_callback_arg);
4044 OPENSSL_free(buffer);
4048 } else if (hbtype == TLS1_HB_RESPONSE) {
4052 * We only send sequence numbers (2 bytes unsigned int), and 16
4053 * random bytes, so we just try to read the sequence number
4057 if (payload == 18 && seq == s->tlsext_hb_seq) {
4059 s->tlsext_hb_pending = 0;
4066 int tls1_heartbeat(SSL *s)
4068 unsigned char *buf, *p;
4070 unsigned int payload = 18; /* Sequence number + random bytes */
4071 unsigned int padding = 16; /* Use minimum padding */
4073 /* Only send if peer supports and accepts HB requests... */
4074 if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
4075 s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
4076 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
4080 /* ...and there is none in flight yet... */
4081 if (s->tlsext_hb_pending) {
4082 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
4086 /* ...and no handshake in progress. */
4087 if (SSL_in_init(s) || s->in_handshake) {
4088 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
4093 * Check if padding is too long, payload and padding must not exceed 2^14
4094 * - 3 = 16381 bytes in total.
4096 OPENSSL_assert(payload + padding <= 16381);
4099 * Create HeartBeat message, we just use a sequence number
4100 * as payload to distuingish different messages and add
4101 * some random stuff.
4102 * - Message Type, 1 byte
4103 * - Payload Length, 2 bytes (unsigned int)
4104 * - Payload, the sequence number (2 bytes uint)
4105 * - Payload, random bytes (16 bytes uint)
4108 buf = OPENSSL_malloc(1 + 2 + payload + padding);
4113 *p++ = TLS1_HB_REQUEST;
4114 /* Payload length (18 bytes here) */
4116 /* Sequence number */
4117 s2n(s->tlsext_hb_seq, p);
4118 /* 16 random bytes */
4119 if (RAND_bytes(p, 16) <= 0) {
4120 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
4124 /* Random padding */
4125 if (RAND_bytes(p, padding) <= 0) {
4126 SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
4130 ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
4132 if (s->msg_callback)
4133 s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
4134 buf, 3 + payload + padding,
4135 s, s->msg_callback_arg);
4137 s->tlsext_hb_pending = 1;
4147 # define MAX_SIGALGLEN (TLSEXT_hash_num * TLSEXT_signature_num * 2)
4151 int sigalgs[MAX_SIGALGLEN];
4154 static int sig_cb(const char *elem, int len, void *arg)
4156 sig_cb_st *sarg = arg;
4159 int sig_alg, hash_alg;
4162 if (sarg->sigalgcnt == MAX_SIGALGLEN)
4164 if (len > (int)(sizeof(etmp) - 1))
4166 memcpy(etmp, elem, len);
4168 p = strchr(etmp, '+');
4176 if (!strcmp(etmp, "RSA"))
4177 sig_alg = EVP_PKEY_RSA;
4178 else if (!strcmp(etmp, "DSA"))
4179 sig_alg = EVP_PKEY_DSA;
4180 else if (!strcmp(etmp, "ECDSA"))
4181 sig_alg = EVP_PKEY_EC;
4185 hash_alg = OBJ_sn2nid(p);
4186 if (hash_alg == NID_undef)
4187 hash_alg = OBJ_ln2nid(p);
4188 if (hash_alg == NID_undef)
4191 for (i = 0; i < sarg->sigalgcnt; i += 2) {
4192 if (sarg->sigalgs[i] == sig_alg && sarg->sigalgs[i + 1] == hash_alg)
4195 sarg->sigalgs[sarg->sigalgcnt++] = hash_alg;
4196 sarg->sigalgs[sarg->sigalgcnt++] = sig_alg;
4201 * Set suppored signature algorithms based on a colon separated list of the
4202 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
4204 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
4208 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
4212 return tls1_set_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
4215 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
4218 unsigned char *sigalgs, *sptr;
4223 sigalgs = OPENSSL_malloc(salglen);
4224 if (sigalgs == NULL)
4226 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
4227 rhash = tls12_find_id(*psig_nids++, tls12_md,
4228 sizeof(tls12_md) / sizeof(tls12_lookup));
4229 rsign = tls12_find_id(*psig_nids++, tls12_sig,
4230 sizeof(tls12_sig) / sizeof(tls12_lookup));
4232 if (rhash == -1 || rsign == -1)
4239 if (c->client_sigalgs)
4240 OPENSSL_free(c->client_sigalgs);
4241 c->client_sigalgs = sigalgs;
4242 c->client_sigalgslen = salglen;
4244 if (c->conf_sigalgs)
4245 OPENSSL_free(c->conf_sigalgs);
4246 c->conf_sigalgs = sigalgs;
4247 c->conf_sigalgslen = salglen;
4253 OPENSSL_free(sigalgs);
4257 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
4261 if (default_nid == -1)
4263 sig_nid = X509_get_signature_nid(x);
4265 return sig_nid == default_nid ? 1 : 0;
4266 for (i = 0; i < c->shared_sigalgslen; i++)
4267 if (sig_nid == c->shared_sigalgs[i].signandhash_nid)
4272 /* Check to see if a certificate issuer name matches list of CA names */
4273 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
4277 nm = X509_get_issuer_name(x);
4278 for (i = 0; i < sk_X509_NAME_num(names); i++) {
4279 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
4286 * Check certificate chain is consistent with TLS extensions and is usable by
4287 * server. This servers two purposes: it allows users to check chains before
4288 * passing them to the server and it allows the server to check chains before
4289 * attempting to use them.
4292 /* Flags which need to be set for a certificate when stict mode not set */
4294 # define CERT_PKEY_VALID_FLAGS \
4295 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
4296 /* Strict mode flags */
4297 # define CERT_PKEY_STRICT_FLAGS \
4298 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
4299 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
4301 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
4306 int check_flags = 0, strict_mode;
4307 CERT_PKEY *cpk = NULL;
4309 unsigned int suiteb_flags = tls1_suiteb(s);
4310 /* idx == -1 means checking server chains */
4312 /* idx == -2 means checking client certificate chains */
4315 idx = cpk - c->pkeys;
4317 cpk = c->pkeys + idx;
4319 pk = cpk->privatekey;
4321 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
4322 /* If no cert or key, forget it */
4325 # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
4326 /* Allow any certificate to pass test */
4327 if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) {
4328 rv = CERT_PKEY_STRICT_FLAGS | CERT_PKEY_EXPLICIT_SIGN |
4329 CERT_PKEY_VALID | CERT_PKEY_SIGN;
4330 cpk->valid_flags = rv;
4337 idx = ssl_cert_type(x, pk);
4340 cpk = c->pkeys + idx;
4341 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
4342 check_flags = CERT_PKEY_STRICT_FLAGS;
4344 check_flags = CERT_PKEY_VALID_FLAGS;
4351 check_flags |= CERT_PKEY_SUITEB;
4352 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
4353 if (ok == X509_V_OK)
4354 rv |= CERT_PKEY_SUITEB;
4355 else if (!check_flags)
4360 * Check all signature algorithms are consistent with signature
4361 * algorithms extension if TLS 1.2 or later and strict mode.
4363 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
4365 unsigned char rsign = 0;
4366 if (c->peer_sigalgs)
4368 /* If no sigalgs extension use defaults from RFC5246 */
4371 case SSL_PKEY_RSA_ENC:
4372 case SSL_PKEY_RSA_SIGN:
4373 case SSL_PKEY_DH_RSA:
4374 rsign = TLSEXT_signature_rsa;
4375 default_nid = NID_sha1WithRSAEncryption;
4378 case SSL_PKEY_DSA_SIGN:
4379 case SSL_PKEY_DH_DSA:
4380 rsign = TLSEXT_signature_dsa;
4381 default_nid = NID_dsaWithSHA1;
4385 rsign = TLSEXT_signature_ecdsa;
4386 default_nid = NID_ecdsa_with_SHA1;
4395 * If peer sent no signature algorithms extension and we have set
4396 * preferred signature algorithms check we support sha1.
4398 if (default_nid > 0 && c->conf_sigalgs) {
4400 const unsigned char *p = c->conf_sigalgs;
4401 for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) {
4402 if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign)
4405 if (j == c->conf_sigalgslen) {
4412 /* Check signature algorithm of each cert in chain */
4413 if (!tls1_check_sig_alg(c, x, default_nid)) {
4417 rv |= CERT_PKEY_EE_SIGNATURE;
4418 rv |= CERT_PKEY_CA_SIGNATURE;
4419 for (i = 0; i < sk_X509_num(chain); i++) {
4420 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
4422 rv &= ~CERT_PKEY_CA_SIGNATURE;
4429 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
4430 else if (check_flags)
4431 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
4433 /* Check cert parameters are consistent */
4434 if (tls1_check_cert_param(s, x, check_flags ? 1 : 2))
4435 rv |= CERT_PKEY_EE_PARAM;
4436 else if (!check_flags)
4439 rv |= CERT_PKEY_CA_PARAM;
4440 /* In strict mode check rest of chain too */
4441 else if (strict_mode) {
4442 rv |= CERT_PKEY_CA_PARAM;
4443 for (i = 0; i < sk_X509_num(chain); i++) {
4444 X509 *ca = sk_X509_value(chain, i);
4445 if (!tls1_check_cert_param(s, ca, 0)) {
4447 rv &= ~CERT_PKEY_CA_PARAM;
4454 if (!s->server && strict_mode) {
4455 STACK_OF(X509_NAME) *ca_dn;
4459 check_type = TLS_CT_RSA_SIGN;
4462 check_type = TLS_CT_DSS_SIGN;
4465 check_type = TLS_CT_ECDSA_SIGN;
4470 int cert_type = X509_certificate_type(x, pk);
4471 if (cert_type & EVP_PKS_RSA)
4472 check_type = TLS_CT_RSA_FIXED_DH;
4473 if (cert_type & EVP_PKS_DSA)
4474 check_type = TLS_CT_DSS_FIXED_DH;
4478 const unsigned char *ctypes;
4482 ctypelen = (int)c->ctype_num;
4484 ctypes = (unsigned char *)s->s3->tmp.ctype;
4485 ctypelen = s->s3->tmp.ctype_num;
4487 for (i = 0; i < ctypelen; i++) {
4488 if (ctypes[i] == check_type) {
4489 rv |= CERT_PKEY_CERT_TYPE;
4493 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
4496 rv |= CERT_PKEY_CERT_TYPE;
4498 ca_dn = s->s3->tmp.ca_names;
4500 if (!sk_X509_NAME_num(ca_dn))
4501 rv |= CERT_PKEY_ISSUER_NAME;
4503 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4504 if (ssl_check_ca_name(ca_dn, x))
4505 rv |= CERT_PKEY_ISSUER_NAME;
4507 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
4508 for (i = 0; i < sk_X509_num(chain); i++) {
4509 X509 *xtmp = sk_X509_value(chain, i);
4510 if (ssl_check_ca_name(ca_dn, xtmp)) {
4511 rv |= CERT_PKEY_ISSUER_NAME;
4516 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
4519 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
4521 if (!check_flags || (rv & check_flags) == check_flags)
4522 rv |= CERT_PKEY_VALID;
4526 if (TLS1_get_version(s) >= TLS1_2_VERSION) {
4527 if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN)
4528 rv |= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
4529 else if (cpk->digest)
4530 rv |= CERT_PKEY_SIGN;
4532 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
4535 * When checking a CERT_PKEY structure all flags are irrelevant if the
4539 if (rv & CERT_PKEY_VALID)
4540 cpk->valid_flags = rv;
4542 /* Preserve explicit sign flag, clear rest */
4543 cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
4550 /* Set validity of certificates in an SSL structure */
4551 void tls1_set_cert_validity(SSL *s)
4553 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC);
4554 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN);
4555 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
4556 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_RSA);
4557 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DH_DSA);
4558 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
4561 /* User level utiity function to check a chain is suitable */
4562 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
4564 return tls1_check_chain(s, x, pk, chain, -1);