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 #ifndef OPENSSL_NO_TLSEXT
121 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
126 SSL3_ENC_METHOD TLSv1_enc_data={
129 tls1_setup_key_block,
130 tls1_generate_master_secret,
131 tls1_change_cipher_state,
132 tls1_final_finish_mac,
133 TLS1_FINISH_MAC_LENGTH,
134 tls1_cert_verify_mac,
135 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
136 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
140 long tls1_default_timeout(void)
142 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
143 * is way too long for http, the cache would over fill */
149 if (!ssl3_new(s)) return(0);
150 s->method->ssl_clear(s);
154 void tls1_free(SSL *s)
159 void tls1_clear(SSL *s)
162 s->version=TLS1_VERSION;
166 #ifndef OPENSSL_NO_TLSEXT
167 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
170 unsigned char *ret = p;
174 if (ret>=limit) return NULL; /* this really never occurs, but ... */
176 if (s->tlsext_hostname != NULL)
178 /* Add TLS extension servername to the Client Hello message */
179 unsigned long size_str;
182 /* check for enough space.
183 4 for the servername type and entension length
184 2 for servernamelist length
185 1 for the hostname type
186 2 for hostname length
190 if ((lenmax = limit - p - 9) < 0
191 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
194 /* extension type and length */
195 s2n(TLSEXT_TYPE_server_name,ret);
198 /* length of servername list */
201 /* hostname type, length and hostname */
202 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
204 memcpy(ret, s->tlsext_hostname, size_str);
208 #ifndef OPENSSL_NO_EC
209 if (s->tlsext_ecpointformatlist != NULL)
211 /* Add TLS extension ECPointFormats to the ClientHello message */
214 if ((lenmax = limit - p - 5) < 0) return NULL;
215 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
216 if (s->tlsext_ecpointformatlist_length > 255)
218 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
222 s2n(TLSEXT_TYPE_ec_point_formats,ret);
223 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
224 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
225 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
226 ret+=s->tlsext_ecpointformatlist_length;
228 if (s->tlsext_ellipticcurvelist != NULL)
230 /* Add TLS extension EllipticCurves to the ClientHello message */
233 if ((lenmax = limit - p - 6) < 0) return NULL;
234 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
235 if (s->tlsext_ellipticcurvelist_length > 65532)
237 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
241 s2n(TLSEXT_TYPE_elliptic_curves,ret);
242 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
244 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
245 * elliptic_curve_list, but the examples use two bytes.
246 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
247 * resolves this to two bytes.
249 s2n(s->tlsext_ellipticcurvelist_length, ret);
250 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
251 ret+=s->tlsext_ellipticcurvelist_length;
253 #endif /* OPENSSL_NO_EC */
255 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
258 if (s->session && s->session->tlsext_tick)
259 ticklen = s->session->tlsext_ticklen;
262 /* Check for enough room 2 for extension type, 2 for len
265 if (limit - p - 4 - ticklen < 0)
267 s2n(TLSEXT_TYPE_session_ticket,ret);
271 memcpy(ret, s->session->tlsext_tick, ticklen);
276 if ((extdatalen = ret-p-2)== 0)
283 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
286 unsigned char *ret = p;
289 if (ret>=limit) return NULL; /* this really never occurs, but ... */
291 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
293 if (limit - p - 4 < 0) return NULL;
295 s2n(TLSEXT_TYPE_server_name,ret);
298 #ifndef OPENSSL_NO_EC
299 if (s->tlsext_ecpointformatlist != NULL)
301 /* Add TLS extension ECPointFormats to the ServerHello message */
304 if ((lenmax = limit - p - 5) < 0) return NULL;
305 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
306 if (s->tlsext_ecpointformatlist_length > 255)
308 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
312 s2n(TLSEXT_TYPE_ec_point_formats,ret);
313 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
314 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
315 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
316 ret+=s->tlsext_ecpointformatlist_length;
319 /* Currently the server should not respond with a SupportedCurves extension */
320 #endif /* OPENSSL_NO_EC */
322 if (s->tlsext_ticket_expected
323 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
325 if (limit - p - 4 < 0) return NULL;
326 s2n(TLSEXT_TYPE_session_ticket,ret);
330 if ((extdatalen = ret-p-2)== 0)
337 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
342 unsigned char *data = *p;
343 s->servername_done = 0;
349 if (data > (d+n-len))
352 while (data <= (d+n-4))
357 if (data+size > (d+n))
360 if (s->tlsext_debug_cb)
361 s->tlsext_debug_cb(s, 0, type, data, size,
362 s->tlsext_debug_arg);
363 /* The servername extension is treated as follows:
365 - Only the hostname type is supported with a maximum length of 255.
366 - The servername is rejected if too long or if it contains zeros,
367 in which case an fatal alert is generated.
368 - The servername field is maintained together with the session cache.
369 - When a session is resumed, the servername call back invoked in order
370 to allow the application to position itself to the right context.
371 - The servername is acknowledged if it is new for a session or when
372 it is identical to a previously used for the same session.
373 Applications can control the behaviour. They can at any time
374 set a 'desirable' servername for a new SSL object. This can be the
375 case for example with HTTPS when a Host: header field is received and
376 a renegotiation is requested. In this case, a possible servername
377 presented in the new client hello is only acknowledged if it matches
378 the value of the Host: field.
379 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
380 if they provide for changing an explicit servername context for the session,
381 i.e. when the session has been established with a servername extension.
382 - On session reconnect, the servername extension may be absent.
386 if (type == TLSEXT_TYPE_server_name)
388 unsigned char *sdata;
394 *al = SSL_AD_DECODE_ERROR;
401 *al = SSL_AD_DECODE_ERROR;
408 servname_type = *(sdata++);
414 *al = SSL_AD_DECODE_ERROR;
417 if (s->servername_done == 0)
418 switch (servname_type)
420 case TLSEXT_NAMETYPE_host_name:
421 if (s->session->tlsext_hostname == NULL)
423 if (len > TLSEXT_MAXLEN_host_name ||
424 ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
426 *al = TLS1_AD_UNRECOGNIZED_NAME;
429 memcpy(s->session->tlsext_hostname, sdata, len);
430 s->session->tlsext_hostname[len]='\0';
431 if (strlen(s->session->tlsext_hostname) != len) {
432 OPENSSL_free(s->session->tlsext_hostname);
433 *al = TLS1_AD_UNRECOGNIZED_NAME;
436 s->servername_done = 1;
440 s->servername_done = strlen(s->session->tlsext_hostname) == len
441 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
453 *al = SSL_AD_DECODE_ERROR;
459 #ifndef OPENSSL_NO_EC
460 else if (type == TLSEXT_TYPE_ec_point_formats)
462 unsigned char *sdata = data;
463 int ecpointformatlist_length = *(sdata++);
465 if (ecpointformatlist_length != size - 1)
467 *al = TLS1_AD_DECODE_ERROR;
470 s->session->tlsext_ecpointformatlist_length = 0;
471 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
472 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
474 *al = TLS1_AD_INTERNAL_ERROR;
477 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
478 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
480 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
481 sdata = s->session->tlsext_ecpointformatlist;
482 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
483 fprintf(stderr,"%i ",*(sdata++));
484 fprintf(stderr,"\n");
487 else if (type == TLSEXT_TYPE_elliptic_curves)
489 unsigned char *sdata = data;
490 int ellipticcurvelist_length = (*(sdata++) << 8);
491 ellipticcurvelist_length += (*(sdata++));
493 if (ellipticcurvelist_length != size - 2)
495 *al = TLS1_AD_DECODE_ERROR;
498 s->session->tlsext_ellipticcurvelist_length = 0;
499 if (s->session->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->session->tlsext_ellipticcurvelist);
500 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
502 *al = TLS1_AD_INTERNAL_ERROR;
505 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
506 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
508 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
509 sdata = s->session->tlsext_ellipticcurvelist;
510 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
511 fprintf(stderr,"%i ",*(sdata++));
512 fprintf(stderr,"\n");
515 #endif /* OPENSSL_NO_EC */
516 /* session ticket processed earlier */
524 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
529 unsigned char *data = *p;
531 int tlsext_servername = 0;
538 while(data <= (d+n-4))
543 if (data+size > (d+n))
546 if (s->tlsext_debug_cb)
547 s->tlsext_debug_cb(s, 1, type, data, size,
548 s->tlsext_debug_arg);
550 if (type == TLSEXT_TYPE_server_name)
552 if (s->tlsext_hostname == NULL || size > 0)
554 *al = TLS1_AD_UNRECOGNIZED_NAME;
557 tlsext_servername = 1;
560 #ifndef OPENSSL_NO_EC
561 else if (type == TLSEXT_TYPE_ec_point_formats)
563 unsigned char *sdata = data;
564 int ecpointformatlist_length = *(sdata++);
566 if (ecpointformatlist_length != size - 1)
568 *al = TLS1_AD_DECODE_ERROR;
571 s->session->tlsext_ecpointformatlist_length = 0;
572 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
573 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
575 *al = TLS1_AD_INTERNAL_ERROR;
578 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
579 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
581 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
582 sdata = s->session->tlsext_ecpointformatlist;
583 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
584 fprintf(stderr,"%i ",*(sdata++));
585 fprintf(stderr,"\n");
588 #endif /* OPENSSL_NO_EC */
590 else if (type == TLSEXT_TYPE_session_ticket)
592 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
595 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
598 s->tlsext_ticket_expected = 1;
605 *al = SSL_AD_DECODE_ERROR;
609 if (!s->hit && tlsext_servername == 1)
611 if (s->tlsext_hostname)
613 if (s->session->tlsext_hostname == NULL)
615 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
616 if (!s->session->tlsext_hostname)
618 *al = SSL_AD_UNRECOGNIZED_NAME;
624 *al = SSL_AD_DECODE_ERROR;
635 int ssl_prepare_clienthello_tlsext(SSL *s)
637 #ifndef OPENSSL_NO_EC
638 /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
639 * and elliptic curves we support.
644 unsigned long alg_k, alg_a;
645 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
647 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
649 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
651 alg_k = c->algorithm_mkey;
652 alg_a = c->algorithm_auth;
653 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
659 using_ecc = using_ecc && (s->version == TLS1_VERSION);
662 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
663 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
665 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
668 s->tlsext_ecpointformatlist_length = 3;
669 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
670 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
671 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
673 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
674 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
675 s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
676 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
678 s->tlsext_ellipticcurvelist_length = 0;
679 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
682 for (i = 1, j = s->tlsext_ellipticcurvelist; i <= sizeof(nid_list)/sizeof(nid_list[0]); i++)
685 #endif /* OPENSSL_NO_EC */
689 int ssl_prepare_serverhello_tlsext(SSL *s)
691 #ifndef OPENSSL_NO_EC
692 /* If we are server and using an ECC cipher suite, send the point formats we support
693 * if the client sent us an ECPointsFormat extension. Note that the server is not
694 * supposed to send an EllipticCurves extension.
697 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
698 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
699 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
700 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
704 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
705 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
707 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
710 s->tlsext_ecpointformatlist_length = 3;
711 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
712 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
713 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
715 #endif /* OPENSSL_NO_EC */
719 int ssl_check_clienthello_tlsext(SSL *s)
721 int ret=SSL_TLSEXT_ERR_NOACK;
722 int al = SSL_AD_UNRECOGNIZED_NAME;
724 #ifndef OPENSSL_NO_EC
725 /* The handling of the ECPointFormats extension is done elsewhere, namely in
726 * ssl3_choose_cipher in s3_lib.c.
728 /* The handling of the EllipticCurves extension is done elsewhere, namely in
729 * ssl3_choose_cipher in s3_lib.c.
733 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
734 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
735 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
736 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
740 case SSL_TLSEXT_ERR_ALERT_FATAL:
741 ssl3_send_alert(s,SSL3_AL_FATAL,al);
744 case SSL_TLSEXT_ERR_ALERT_WARNING:
745 ssl3_send_alert(s,SSL3_AL_WARNING,al);
748 case SSL_TLSEXT_ERR_NOACK:
749 s->servername_done=0;
755 int ssl_check_serverhello_tlsext(SSL *s)
757 int ret=SSL_TLSEXT_ERR_NOACK;
758 int al = SSL_AD_UNRECOGNIZED_NAME;
760 #ifndef OPENSSL_NO_EC
761 /* If we are client and using an elliptic curve cryptography cipher suite, then server
762 * must return a an EC point formats lists containing uncompressed.
764 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
765 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
766 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
767 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
769 /* we are using an ECC cipher */
772 int found_uncompressed = 0;
773 if ((s->session->tlsext_ecpointformatlist == NULL) || (s->session->tlsext_ecpointformatlist_length == 0))
775 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
778 list = s->session->tlsext_ecpointformatlist;
779 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
781 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
783 found_uncompressed = 1;
787 if (!found_uncompressed)
789 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
793 ret = SSL_TLSEXT_ERR_OK;
794 #endif /* OPENSSL_NO_EC */
796 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
797 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
798 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
799 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
803 case SSL_TLSEXT_ERR_ALERT_FATAL:
804 ssl3_send_alert(s,SSL3_AL_FATAL,al);
807 case SSL_TLSEXT_ERR_ALERT_WARNING:
808 ssl3_send_alert(s,SSL3_AL_WARNING,al);
811 case SSL_TLSEXT_ERR_NOACK:
812 s->servername_done=0;
818 /* Since the server cache lookup is done early on in the processing of client
819 * hello and other operations depend on the result we need to handle any TLS
820 * session ticket extension at the same time.
823 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
824 const unsigned char *limit, SSL_SESSION **ret)
826 /* Point after session ID in client hello */
827 const unsigned char *p = session_id + len;
829 if ((s->version <= SSL3_VERSION) || !limit)
833 /* Skip past cipher list */
838 /* Skip past compression algorithm list */
843 /* Now at start of extensions */
844 if ((p + 2) >= limit)
847 while ((p + 4) <= limit)
849 unsigned short type, size;
852 if (p + size > limit)
854 if (type == TLSEXT_TYPE_session_ticket)
856 /* If tickets disabled indicate cache miss which will
857 * trigger a full handshake
859 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
861 /* If zero length not client will accept a ticket
862 * and indicate cache miss to trigger full handshake
866 s->tlsext_ticket_expected = 1;
867 return 0; /* Cache miss */
869 return tls_decrypt_ticket(s, p, size, session_id, len,
877 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
878 const unsigned char *sess_id, int sesslen,
883 const unsigned char *p;
885 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
888 /* Attempt to process session ticket, first conduct sanity and
889 * integrity checks on ticket.
891 mlen = EVP_MD_size(EVP_sha1());
893 /* Need at least keyname + iv + some encrypted data */
896 /* Check key name matches */
897 if (memcmp(etick, s->ctx->tlsext_tick_key_name, 16))
899 /* Check HMAC of encrypted ticket */
900 HMAC_CTX_init(&hctx);
901 HMAC_Init_ex(&hctx, s->ctx->tlsext_tick_hmac_key, 16,
903 HMAC_Update(&hctx, etick, eticklen);
904 HMAC_Final(&hctx, tick_hmac, NULL);
905 HMAC_CTX_cleanup(&hctx);
906 if (memcmp(tick_hmac, etick + eticklen, mlen))
908 /* Set p to start of IV */
910 EVP_CIPHER_CTX_init(&ctx);
911 /* Attempt to decrypt session data */
912 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
913 s->ctx->tlsext_tick_aes_key, p);
914 /* Move p after IV to start of encrypted ticket, update length */
917 sdec = OPENSSL_malloc(eticklen);
920 EVP_CIPHER_CTX_cleanup(&ctx);
923 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
924 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
927 EVP_CIPHER_CTX_cleanup(&ctx);
930 sess = d2i_SSL_SESSION(NULL, &p, slen);
934 /* The session ID if non-empty is used by some clients to
935 * detect that the ticket has been accepted. So we copy it to
936 * the session structure. If it is empty set length to zero
937 * as required by standard.
940 memcpy(sess->session_id, sess_id, sesslen);
941 sess->session_id_length = sesslen;
945 /* If session decrypt failure indicate a cache miss and set state to
949 s->tlsext_ticket_expected = 1;
954 #ifndef OPENSSL_NO_EC
955 static int nid_list[] =
957 NID_sect163k1, /* sect163k1 (1) */
958 NID_sect163r1, /* sect163r1 (2) */
959 NID_sect163r2, /* sect163r2 (3) */
960 NID_sect193r1, /* sect193r1 (4) */
961 NID_sect193r2, /* sect193r2 (5) */
962 NID_sect233k1, /* sect233k1 (6) */
963 NID_sect233r1, /* sect233r1 (7) */
964 NID_sect239k1, /* sect239k1 (8) */
965 NID_sect283k1, /* sect283k1 (9) */
966 NID_sect283r1, /* sect283r1 (10) */
967 NID_sect409k1, /* sect409k1 (11) */
968 NID_sect409r1, /* sect409r1 (12) */
969 NID_sect571k1, /* sect571k1 (13) */
970 NID_sect571r1, /* sect571r1 (14) */
971 NID_secp160k1, /* secp160k1 (15) */
972 NID_secp160r1, /* secp160r1 (16) */
973 NID_secp160r2, /* secp160r2 (17) */
974 NID_secp192k1, /* secp192k1 (18) */
975 NID_X9_62_prime192v1, /* secp192r1 (19) */
976 NID_secp224k1, /* secp224k1 (20) */
977 NID_secp224r1, /* secp224r1 (21) */
978 NID_secp256k1, /* secp256k1 (22) */
979 NID_X9_62_prime256v1, /* secp256r1 (23) */
980 NID_secp384r1, /* secp384r1 (24) */
981 NID_secp521r1 /* secp521r1 (25) */
984 int tls1_ec_curve_id2nid(int curve_id)
986 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
987 if ((curve_id < 1) || (curve_id > sizeof(nid_list)/sizeof(nid_list[0]))) return 0;
988 return nid_list[curve_id-1];
991 int tls1_ec_nid2curve_id(int nid)
993 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
996 case NID_sect163k1: /* sect163k1 (1) */
998 case NID_sect163r1: /* sect163r1 (2) */
1000 case NID_sect163r2: /* sect163r2 (3) */
1002 case NID_sect193r1: /* sect193r1 (4) */
1004 case NID_sect193r2: /* sect193r2 (5) */
1006 case NID_sect233k1: /* sect233k1 (6) */
1008 case NID_sect233r1: /* sect233r1 (7) */
1010 case NID_sect239k1: /* sect239k1 (8) */
1012 case NID_sect283k1: /* sect283k1 (9) */
1014 case NID_sect283r1: /* sect283r1 (10) */
1016 case NID_sect409k1: /* sect409k1 (11) */
1018 case NID_sect409r1: /* sect409r1 (12) */
1020 case NID_sect571k1: /* sect571k1 (13) */
1022 case NID_sect571r1: /* sect571r1 (14) */
1024 case NID_secp160k1: /* secp160k1 (15) */
1026 case NID_secp160r1: /* secp160r1 (16) */
1028 case NID_secp160r2: /* secp160r2 (17) */
1030 case NID_secp192k1: /* secp192k1 (18) */
1032 case NID_X9_62_prime192v1: /* secp192r1 (19) */
1034 case NID_secp224k1: /* secp224k1 (20) */
1036 case NID_secp224r1: /* secp224r1 (21) */
1038 case NID_secp256k1: /* secp256k1 (22) */
1040 case NID_X9_62_prime256v1: /* secp256r1 (23) */
1042 case NID_secp384r1: /* secp384r1 (24) */
1044 case NID_secp521r1: /* secp521r1 (25) */
1050 #endif /* OPENSSL_NO_EC */