2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
113 #include <openssl/objects.h>
114 #include <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include "ssl_locl.h"
118 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
120 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
121 const unsigned char *sess_id, int sesslen,
122 SSL_SESSION **psess);
124 SSL3_ENC_METHOD TLSv1_enc_data={
127 tls1_setup_key_block,
128 tls1_generate_master_secret,
129 tls1_change_cipher_state,
130 tls1_final_finish_mac,
131 TLS1_FINISH_MAC_LENGTH,
132 tls1_cert_verify_mac,
133 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
134 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
138 long tls1_default_timeout(void)
140 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
141 * is way too long for http, the cache would over fill */
147 if (!ssl3_new(s)) return(0);
148 s->method->ssl_clear(s);
152 void tls1_free(SSL *s)
157 void tls1_clear(SSL *s)
160 s->version=TLS1_VERSION;
164 #ifndef OPENSSL_NO_TLSEXT
165 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
168 unsigned char *ret = p;
172 if (ret>=limit) return NULL; /* this really never occurs, but ... */
174 if (s->tlsext_hostname != NULL)
176 /* Add TLS extension servername to the Client Hello message */
177 unsigned long size_str;
180 /* check for enough space.
181 4 for the servername type and entension length
182 2 for servernamelist length
183 1 for the hostname type
184 2 for hostname length
188 if ((lenmax = limit - p - 9) < 0
189 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
192 /* extension type and length */
193 s2n(TLSEXT_TYPE_server_name,ret);
196 /* length of servername list */
199 /* hostname type, length and hostname */
200 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
202 memcpy(ret, s->tlsext_hostname, size_str);
206 #ifndef OPENSSL_NO_EC
207 if (s->tlsext_ecpointformatlist != NULL)
209 /* Add TLS extension ECPointFormats to the ClientHello message */
212 if ((lenmax = limit - p - 5) < 0) return NULL;
213 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
214 if (s->tlsext_ecpointformatlist_length > 255)
216 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
220 s2n(TLSEXT_TYPE_ec_point_formats,ret);
221 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
222 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
223 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
224 ret+=s->tlsext_ecpointformatlist_length;
226 if (s->tlsext_ellipticcurvelist != NULL)
228 /* Add TLS extension EllipticCurves to the ClientHello message */
231 if ((lenmax = limit - p - 6) < 0) return NULL;
232 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
233 if (s->tlsext_ellipticcurvelist_length > 65532)
235 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
239 s2n(TLSEXT_TYPE_elliptic_curves,ret);
240 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
242 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
243 * elliptic_curve_list, but the examples use two bytes.
244 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
245 * resolves this to two bytes.
247 s2n(s->tlsext_ellipticcurvelist_length, ret);
248 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
249 ret+=s->tlsext_ellipticcurvelist_length;
251 #endif /* OPENSSL_NO_EC */
253 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
256 if (s->session && s->session->tlsext_tick)
257 ticklen = s->session->tlsext_ticklen;
260 /* Check for enough room 2 for extension type, 2 for len
263 if (limit - p - 4 - ticklen < 0)
265 s2n(TLSEXT_TYPE_session_ticket,ret);
269 memcpy(ret, s->session->tlsext_tick, ticklen);
274 if ((extdatalen = ret-p-2)== 0)
281 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
284 unsigned char *ret = p;
287 if (ret>=limit) return NULL; /* this really never occurs, but ... */
289 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
291 if (limit - p - 4 < 0) return NULL;
293 s2n(TLSEXT_TYPE_server_name,ret);
296 #ifndef OPENSSL_NO_EC
297 if (s->tlsext_ecpointformatlist != NULL)
299 /* Add TLS extension ECPointFormats to the ServerHello message */
302 if ((lenmax = limit - p - 5) < 0) return NULL;
303 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
304 if (s->tlsext_ecpointformatlist_length > 255)
306 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
310 s2n(TLSEXT_TYPE_ec_point_formats,ret);
311 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
312 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
313 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
314 ret+=s->tlsext_ecpointformatlist_length;
317 /* Currently the server should not respond with a SupportedCurves extension */
318 #endif /* OPENSSL_NO_EC */
320 if (s->tlsext_ticket_expected
321 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
323 if (limit - p - 4 < 0) return NULL;
324 s2n(TLSEXT_TYPE_session_ticket,ret);
328 if ((extdatalen = ret-p-2)== 0)
335 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
340 unsigned char *data = *p;
341 s->servername_done = 0;
347 if (data > (d+n-len))
350 while (data <= (d+n-4))
355 if (data+size > (d+n))
358 if (s->tlsext_debug_cb)
359 s->tlsext_debug_cb(s, 0, type, data, size,
360 s->tlsext_debug_arg);
361 /* The servername extension is treated as follows:
363 - Only the hostname type is supported with a maximum length of 255.
364 - The servername is rejected if too long or if it contains zeros,
365 in which case an fatal alert is generated.
366 - The servername field is maintained together with the session cache.
367 - When a session is resumed, the servername call back invoked in order
368 to allow the application to position itself to the right context.
369 - The servername is acknowledged if it is new for a session or when
370 it is identical to a previously used for the same session.
371 Applications can control the behaviour. They can at any time
372 set a 'desirable' servername for a new SSL object. This can be the
373 case for example with HTTPS when a Host: header field is received and
374 a renegotiation is requested. In this case, a possible servername
375 presented in the new client hello is only acknowledged if it matches
376 the value of the Host: field.
377 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
378 if they provide for changing an explicit servername context for the session,
379 i.e. when the session has been established with a servername extension.
380 - On session reconnect, the servername extension may be absent.
384 if (type == TLSEXT_TYPE_server_name)
386 unsigned char *sdata;
392 *al = SSL_AD_DECODE_ERROR;
399 *al = SSL_AD_DECODE_ERROR;
406 servname_type = *(sdata++);
412 *al = SSL_AD_DECODE_ERROR;
415 if (s->servername_done == 0)
416 switch (servname_type)
418 case TLSEXT_NAMETYPE_host_name:
419 if (s->session->tlsext_hostname == NULL)
421 if (len > TLSEXT_MAXLEN_host_name ||
422 ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
424 *al = TLS1_AD_UNRECOGNIZED_NAME;
427 memcpy(s->session->tlsext_hostname, sdata, len);
428 s->session->tlsext_hostname[len]='\0';
429 if (strlen(s->session->tlsext_hostname) != len) {
430 OPENSSL_free(s->session->tlsext_hostname);
431 *al = TLS1_AD_UNRECOGNIZED_NAME;
434 s->servername_done = 1;
438 s->servername_done = strlen(s->session->tlsext_hostname) == len
439 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
451 *al = SSL_AD_DECODE_ERROR;
457 #ifndef OPENSSL_NO_EC
458 else if (type == TLSEXT_TYPE_ec_point_formats)
460 unsigned char *sdata = data;
461 int ecpointformatlist_length = *(sdata++);
463 if (ecpointformatlist_length != size - 1)
465 *al = TLS1_AD_DECODE_ERROR;
468 s->session->tlsext_ecpointformatlist_length = 0;
469 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
470 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
472 *al = TLS1_AD_INTERNAL_ERROR;
475 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
476 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
478 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
479 sdata = s->session->tlsext_ecpointformatlist;
480 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
481 fprintf(stderr,"%i ",*(sdata++));
482 fprintf(stderr,"\n");
485 else if (type == TLSEXT_TYPE_elliptic_curves)
487 unsigned char *sdata = data;
488 int ellipticcurvelist_length = (*(sdata++) << 8);
489 ellipticcurvelist_length += (*(sdata++));
491 if (ellipticcurvelist_length != size - 2)
493 *al = TLS1_AD_DECODE_ERROR;
496 s->session->tlsext_ellipticcurvelist_length = 0;
497 if (s->session->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->session->tlsext_ellipticcurvelist);
498 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
500 *al = TLS1_AD_INTERNAL_ERROR;
503 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
504 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
506 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
507 sdata = s->session->tlsext_ellipticcurvelist;
508 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
509 fprintf(stderr,"%i ",*(sdata++));
510 fprintf(stderr,"\n");
513 #endif /* OPENSSL_NO_EC */
514 /* session ticket processed earlier */
522 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
527 unsigned char *data = *p;
529 int tlsext_servername = 0;
536 while(data <= (d+n-4))
541 if (data+size > (d+n))
544 if (s->tlsext_debug_cb)
545 s->tlsext_debug_cb(s, 1, type, data, size,
546 s->tlsext_debug_arg);
548 if (type == TLSEXT_TYPE_server_name)
550 if (s->tlsext_hostname == NULL || size > 0)
552 *al = TLS1_AD_UNRECOGNIZED_NAME;
555 tlsext_servername = 1;
558 #ifndef OPENSSL_NO_EC
559 else if (type == TLSEXT_TYPE_ec_point_formats)
561 unsigned char *sdata = data;
562 int ecpointformatlist_length = *(sdata++);
564 if (ecpointformatlist_length != size - 1)
566 *al = TLS1_AD_DECODE_ERROR;
569 s->session->tlsext_ecpointformatlist_length = 0;
570 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
571 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
573 *al = TLS1_AD_INTERNAL_ERROR;
576 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
577 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
579 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
580 sdata = s->session->tlsext_ecpointformatlist;
581 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
582 fprintf(stderr,"%i ",*(sdata++));
583 fprintf(stderr,"\n");
586 #endif /* OPENSSL_NO_EC */
588 else if (type == TLSEXT_TYPE_session_ticket)
590 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
593 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
596 s->tlsext_ticket_expected = 1;
603 *al = SSL_AD_DECODE_ERROR;
607 if (!s->hit && tlsext_servername == 1)
609 if (s->tlsext_hostname)
611 if (s->session->tlsext_hostname == NULL)
613 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
614 if (!s->session->tlsext_hostname)
616 *al = SSL_AD_UNRECOGNIZED_NAME;
622 *al = SSL_AD_DECODE_ERROR;
632 #ifndef OPENSSL_NO_EC
633 static int nid_list[] =
635 NID_sect163k1, /* sect163k1 (1) */
636 NID_sect163r1, /* sect163r1 (2) */
637 NID_sect163r2, /* sect163r2 (3) */
638 NID_sect193r1, /* sect193r1 (4) */
639 NID_sect193r2, /* sect193r2 (5) */
640 NID_sect233k1, /* sect233k1 (6) */
641 NID_sect233r1, /* sect233r1 (7) */
642 NID_sect239k1, /* sect239k1 (8) */
643 NID_sect283k1, /* sect283k1 (9) */
644 NID_sect283r1, /* sect283r1 (10) */
645 NID_sect409k1, /* sect409k1 (11) */
646 NID_sect409r1, /* sect409r1 (12) */
647 NID_sect571k1, /* sect571k1 (13) */
648 NID_sect571r1, /* sect571r1 (14) */
649 NID_secp160k1, /* secp160k1 (15) */
650 NID_secp160r1, /* secp160r1 (16) */
651 NID_secp160r2, /* secp160r2 (17) */
652 NID_secp192k1, /* secp192k1 (18) */
653 NID_X9_62_prime192v1, /* secp192r1 (19) */
654 NID_secp224k1, /* secp224k1 (20) */
655 NID_secp224r1, /* secp224r1 (21) */
656 NID_secp256k1, /* secp256k1 (22) */
657 NID_X9_62_prime256v1, /* secp256r1 (23) */
658 NID_secp384r1, /* secp384r1 (24) */
659 NID_secp521r1 /* secp521r1 (25) */
662 int tls1_ec_curve_id2nid(int curve_id)
664 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
665 if ((curve_id < 1) || (curve_id > sizeof(nid_list)/sizeof(nid_list[0]))) return 0;
666 return nid_list[curve_id-1];
669 int tls1_ec_nid2curve_id(int nid)
671 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
674 case NID_sect163k1: /* sect163k1 (1) */
676 case NID_sect163r1: /* sect163r1 (2) */
678 case NID_sect163r2: /* sect163r2 (3) */
680 case NID_sect193r1: /* sect193r1 (4) */
682 case NID_sect193r2: /* sect193r2 (5) */
684 case NID_sect233k1: /* sect233k1 (6) */
686 case NID_sect233r1: /* sect233r1 (7) */
688 case NID_sect239k1: /* sect239k1 (8) */
690 case NID_sect283k1: /* sect283k1 (9) */
692 case NID_sect283r1: /* sect283r1 (10) */
694 case NID_sect409k1: /* sect409k1 (11) */
696 case NID_sect409r1: /* sect409r1 (12) */
698 case NID_sect571k1: /* sect571k1 (13) */
700 case NID_sect571r1: /* sect571r1 (14) */
702 case NID_secp160k1: /* secp160k1 (15) */
704 case NID_secp160r1: /* secp160r1 (16) */
706 case NID_secp160r2: /* secp160r2 (17) */
708 case NID_secp192k1: /* secp192k1 (18) */
710 case NID_X9_62_prime192v1: /* secp192r1 (19) */
712 case NID_secp224k1: /* secp224k1 (20) */
714 case NID_secp224r1: /* secp224r1 (21) */
716 case NID_secp256k1: /* secp256k1 (22) */
718 case NID_X9_62_prime256v1: /* secp256r1 (23) */
720 case NID_secp384r1: /* secp384r1 (24) */
722 case NID_secp521r1: /* secp521r1 (25) */
728 #endif /* OPENSSL_NO_EC */
730 int ssl_prepare_clienthello_tlsext(SSL *s)
732 #ifndef OPENSSL_NO_EC
733 /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
734 * and elliptic curves we support.
739 unsigned long alg_k, alg_a;
740 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
742 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
744 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
746 alg_k = c->algorithm_mkey;
747 alg_a = c->algorithm_auth;
748 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
754 using_ecc = using_ecc && (s->version == TLS1_VERSION);
757 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
758 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
760 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
763 s->tlsext_ecpointformatlist_length = 3;
764 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
765 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
766 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
768 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
769 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
770 s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
771 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
773 s->tlsext_ellipticcurvelist_length = 0;
774 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
777 for (i = 1, j = s->tlsext_ellipticcurvelist; i <= sizeof(nid_list)/sizeof(nid_list[0]); i++)
780 #endif /* OPENSSL_NO_EC */
784 int ssl_prepare_serverhello_tlsext(SSL *s)
786 #ifndef OPENSSL_NO_EC
787 /* If we are server and using an ECC cipher suite, send the point formats we support
788 * if the client sent us an ECPointsFormat extension. Note that the server is not
789 * supposed to send an EllipticCurves extension.
792 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
793 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
794 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
795 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
799 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
800 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
802 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
805 s->tlsext_ecpointformatlist_length = 3;
806 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
807 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
808 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
810 #endif /* OPENSSL_NO_EC */
814 int ssl_check_clienthello_tlsext(SSL *s)
816 int ret=SSL_TLSEXT_ERR_NOACK;
817 int al = SSL_AD_UNRECOGNIZED_NAME;
819 #ifndef OPENSSL_NO_EC
820 /* The handling of the ECPointFormats extension is done elsewhere, namely in
821 * ssl3_choose_cipher in s3_lib.c.
823 /* The handling of the EllipticCurves extension is done elsewhere, namely in
824 * ssl3_choose_cipher in s3_lib.c.
828 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
829 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
830 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
831 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
835 case SSL_TLSEXT_ERR_ALERT_FATAL:
836 ssl3_send_alert(s,SSL3_AL_FATAL,al);
839 case SSL_TLSEXT_ERR_ALERT_WARNING:
840 ssl3_send_alert(s,SSL3_AL_WARNING,al);
843 case SSL_TLSEXT_ERR_NOACK:
844 s->servername_done=0;
850 int ssl_check_serverhello_tlsext(SSL *s)
852 int ret=SSL_TLSEXT_ERR_NOACK;
853 int al = SSL_AD_UNRECOGNIZED_NAME;
855 #ifndef OPENSSL_NO_EC
856 /* If we are client and using an elliptic curve cryptography cipher suite, then server
857 * must return a an EC point formats lists containing uncompressed.
859 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
860 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
861 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
862 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
864 /* we are using an ECC cipher */
867 int found_uncompressed = 0;
868 if ((s->session->tlsext_ecpointformatlist == NULL) || (s->session->tlsext_ecpointformatlist_length == 0))
870 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
873 list = s->session->tlsext_ecpointformatlist;
874 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
876 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
878 found_uncompressed = 1;
882 if (!found_uncompressed)
884 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
888 ret = SSL_TLSEXT_ERR_OK;
889 #endif /* OPENSSL_NO_EC */
891 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
892 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
893 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
894 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
898 case SSL_TLSEXT_ERR_ALERT_FATAL:
899 ssl3_send_alert(s,SSL3_AL_FATAL,al);
902 case SSL_TLSEXT_ERR_ALERT_WARNING:
903 ssl3_send_alert(s,SSL3_AL_WARNING,al);
906 case SSL_TLSEXT_ERR_NOACK:
907 s->servername_done=0;
913 /* Since the server cache lookup is done early on in the processing of client
914 * hello and other operations depend on the result we need to handle any TLS
915 * session ticket extension at the same time.
918 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
919 const unsigned char *limit, SSL_SESSION **ret)
921 /* Point after session ID in client hello */
922 const unsigned char *p = session_id + len;
924 if ((s->version <= SSL3_VERSION) || !limit)
928 /* Skip past cipher list */
933 /* Skip past compression algorithm list */
938 /* Now at start of extensions */
939 if ((p + 2) >= limit)
942 while ((p + 4) <= limit)
944 unsigned short type, size;
947 if (p + size > limit)
949 if (type == TLSEXT_TYPE_session_ticket)
951 /* If tickets disabled indicate cache miss which will
952 * trigger a full handshake
954 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
956 /* If zero length not client will accept a ticket
957 * and indicate cache miss to trigger full handshake
961 s->tlsext_ticket_expected = 1;
962 return 0; /* Cache miss */
964 return tls_decrypt_ticket(s, p, size, session_id, len,
972 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
973 const unsigned char *sess_id, int sesslen,
978 const unsigned char *p;
980 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
983 /* Attempt to process session ticket, first conduct sanity and
984 * integrity checks on ticket.
986 mlen = EVP_MD_size(EVP_sha1());
988 /* Need at least keyname + iv + some encrypted data */
991 /* Check key name matches */
992 if (memcmp(etick, s->ctx->tlsext_tick_key_name, 16))
994 /* Check HMAC of encrypted ticket */
995 HMAC_CTX_init(&hctx);
996 HMAC_Init_ex(&hctx, s->ctx->tlsext_tick_hmac_key, 16,
998 HMAC_Update(&hctx, etick, eticklen);
999 HMAC_Final(&hctx, tick_hmac, NULL);
1000 HMAC_CTX_cleanup(&hctx);
1001 if (memcmp(tick_hmac, etick + eticklen, mlen))
1003 /* Set p to start of IV */
1005 EVP_CIPHER_CTX_init(&ctx);
1006 /* Attempt to decrypt session data */
1007 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1008 s->ctx->tlsext_tick_aes_key, p);
1009 /* Move p after IV to start of encrypted ticket, update length */
1012 sdec = OPENSSL_malloc(eticklen);
1015 EVP_CIPHER_CTX_cleanup(&ctx);
1018 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
1019 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
1022 EVP_CIPHER_CTX_cleanup(&ctx);
1025 sess = d2i_SSL_SESSION(NULL, &p, slen);
1029 /* The session ID if non-empty is used by some clients to
1030 * detect that the ticket has been accepted. So we copy it to
1031 * the session structure. If it is empty set length to zero
1032 * as required by standard.
1035 memcpy(sess->session_id, sess_id, sesslen);
1036 sess->session_id_length = sesslen;
1040 /* If session decrypt failure indicate a cache miss and set state to
1044 s->tlsext_ticket_expected = 1;