d75588676d22c06aa32c5da3622a2af17d43216a
[openssl.git] / ssl / t1_lib.c
1 /* ssl/t1_lib.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
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.
8  * 
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).
15  * 
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.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
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)"
40  * 
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
51  * SUCH DAMAGE.
52  * 
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.]
57  */
58 /* ====================================================================
59  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
60  *
61  * Redistribution and use in source and binary forms, with or without
62  * modification, are permitted provided that the following conditions
63  * are met:
64  *
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer. 
67  *
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
71  *    distribution.
72  *
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/)"
77  *
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.
82  *
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.
86  *
87  * 6. Redistributions of any form whatsoever must retain the following
88  *    acknowledgment:
89  *    "This product includes software developed by the OpenSSL Project
90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91  *
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  * ====================================================================
105  *
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).
109  *
110  */
111
112 #include <stdio.h>
113 #include <openssl/objects.h>
114 #include "ssl_locl.h"
115
116 const char *tls1_version_str="TLSv1" OPENSSL_VERSION_PTEXT;
117
118 SSL3_ENC_METHOD TLSv1_enc_data={
119         tls1_enc,
120         tls1_mac,
121         tls1_setup_key_block,
122         tls1_generate_master_secret,
123         tls1_change_cipher_state,
124         tls1_final_finish_mac,
125         TLS1_FINISH_MAC_LENGTH,
126         tls1_cert_verify_mac,
127         TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
128         TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
129         tls1_alert_code,
130         };
131
132 long tls1_default_timeout(void)
133         {
134         /* 2 hours, the 24 hours mentioned in the TLSv1 spec
135          * is way too long for http, the cache would over fill */
136         return(60*60*2);
137         }
138
139 int tls1_new(SSL *s)
140         {
141         if (!ssl3_new(s)) return(0);
142         s->method->ssl_clear(s);
143         return(1);
144         }
145
146 void tls1_free(SSL *s)
147         {
148         ssl3_free(s);
149         }
150
151 void tls1_clear(SSL *s)
152         {
153         ssl3_clear(s);
154         s->version=TLS1_VERSION;
155         }
156
157
158 #ifndef OPENSSL_NO_TLSEXT
159 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
160         {
161         int extdatalen=0;
162         unsigned char *ret = p;
163
164         ret+=2;
165
166         if (ret>=limit) return NULL; /* this really never occurs, but ... */
167         if (s->tlsext_hostname != NULL)
168                 { 
169                 /* Add TLS extension servername to the Client Hello message */
170                 unsigned long size_str;
171                 long lenmax; 
172
173                 /* check for enough space.
174                    4 for the servername type and entension length
175                    2 for servernamelist length
176                    1 for the hostname type
177                    2 for hostname length
178                    + hostname length 
179                 */
180                    
181                 if ((lenmax = limit - p - 9) < 0 
182                 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) 
183                         return NULL;
184                         
185                 /* extension type and length */
186                 s2n(TLSEXT_TYPE_server_name,ret); 
187                 s2n(size_str+5,ret);
188                 
189                 /* length of servername list */
190                 s2n(size_str+3,ret);
191         
192                 /* hostname type, length and hostname */
193                 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
194                 s2n(size_str,ret);
195                 memcpy(ret, s->tlsext_hostname, size_str);
196                 ret+=size_str;
197
198                 }
199 #ifndef OPENSSL_NO_EC
200         if (s->tlsext_ecpointformatlist != NULL)
201                 {
202                 /* Add TLS extension ECPointFormats to the ClientHello message */
203                 long lenmax; 
204
205                 if ((lenmax = limit - p - 5) < 0) return NULL; 
206                 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
207                 if (s->tlsext_ecpointformatlist_length > 255)
208                         {
209                         SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
210                         return NULL;
211                         }
212                 
213                 s2n(TLSEXT_TYPE_ec_point_formats,ret);
214                 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
215                 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
216                 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
217                 ret+=s->tlsext_ecpointformatlist_length;
218                 }
219         if (s->tlsext_ellipticcurvelist != NULL)
220                 {
221                 /* Add TLS extension EllipticCurves to the ClientHello message */
222                 long lenmax; 
223
224                 if ((lenmax = limit - p - 5) < 0) return NULL; 
225                 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
226                 if (s->tlsext_ellipticcurvelist_length > 255)
227                         {
228                         SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
229                         return NULL;
230                         }
231                 
232                 s2n(TLSEXT_TYPE_elliptic_curves,ret);
233                 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
234                 s2n(s->tlsext_ellipticcurvelist_length, ret);
235                 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
236                 ret+=s->tlsext_ellipticcurvelist_length;
237                 }
238 #endif /* OPENSSL_NO_EC */
239
240         if ((extdatalen = ret-p-2)== 0) 
241                 return p;
242
243         s2n(extdatalen,p);
244         return ret;
245 }
246
247 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
248         {
249         int extdatalen=0;
250         unsigned char *ret = p;
251
252         ret+=2;
253         if (ret>=limit) return NULL; /* this really never occurs, but ... */
254
255         if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
256                 { 
257                 if (limit - p - 4 < 0) return NULL; 
258
259                 s2n(TLSEXT_TYPE_server_name,ret);
260                 s2n(0,ret);
261                 }
262 #ifndef OPENSSL_NO_EC
263         if (s->tlsext_ecpointformatlist != NULL)
264                 {
265                 /* Add TLS extension ECPointFormats to the ServerHello message */
266                 long lenmax; 
267
268                 if ((lenmax = limit - p - 5) < 0) return NULL; 
269                 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
270                 if (s->tlsext_ecpointformatlist_length > 255)
271                         {
272                         SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
273                         return NULL;
274                         }
275                 
276                 s2n(TLSEXT_TYPE_ec_point_formats,ret);
277                 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
278                 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
279                 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
280                 ret+=s->tlsext_ecpointformatlist_length;
281
282                 }
283         /* Currently the server should not respond with a SupportedCurves extension */
284 #endif /* OPENSSL_NO_EC */
285         
286         if ((extdatalen = ret-p-2)== 0) 
287                 return p;
288
289         s2n(extdatalen,p);
290         return ret;
291 }
292
293 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
294         {
295         unsigned short type;
296         unsigned short size;
297         unsigned short len;
298         unsigned char *data = *p;
299         s->servername_done = 0;
300
301         if (data >= (d+n-2))
302                 return 1;
303         n2s(data,len);
304
305         if (data > (d+n-len)) 
306                 return 1;
307
308         while (data <= (d+n-4))
309                 {
310                 n2s(data,type);
311                 n2s(data,size);
312
313                 if (data+size > (d+n))
314                         return 1;
315                 
316 /* The servername extension is treated as follows:
317
318    - Only the hostname type is supported with a maximum length of 255.
319    - The servername is rejected if too long or if it contains zeros,
320      in which case an fatal alert is generated.
321    - The servername field is maintained together with the session cache.
322    - When a session is resumed, the servername call back invoked in order
323      to allow the application to position itself to the right context. 
324    - The servername is acknowledged if it is new for a session or when 
325      it is identical to a previously used for the same session. 
326      Applications can control the behaviour.  They can at any time
327      set a 'desirable' servername for a new SSL object. This can be the
328      case for example with HTTPS when a Host: header field is received and
329      a renegotiation is requested. In this case, a possible servername
330      presented in the new client hello is only acknowledged if it matches
331      the value of the Host: field. 
332    - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
333      if they provide for changing an explicit servername context for the session,
334      i.e. when the session has been established with a servername extension. 
335    - On session reconnect, the servername extension may be absent. 
336
337 */      
338
339                 if (type == TLSEXT_TYPE_server_name)
340                         {
341                         unsigned char *sdata;
342                         int servname_type;
343                         int dsize; 
344                 
345                         if (size < 2) 
346                                 {
347                                 *al = SSL_AD_DECODE_ERROR;
348                                 return 0;
349                                 }
350                         n2s(data,dsize);  
351                         size -= 2;                    
352                         if (dsize > size  ) 
353                                 {
354                                 *al = SSL_AD_DECODE_ERROR;
355                                 return 0;
356                                 } 
357
358                         sdata = data;
359                         while (dsize > 3) 
360                                 {
361                                 servname_type = *(sdata++); 
362                                 n2s(sdata,len);
363                                 dsize -= 3;
364
365                                 if (len > dsize) 
366                                         {
367                                         *al = SSL_AD_DECODE_ERROR;
368                                         return 0;
369                                         }
370                                 if (s->servername_done == 0)
371                                 switch (servname_type)
372                                         {
373                                 case TLSEXT_NAMETYPE_host_name:
374                                         if (s->session->tlsext_hostname == NULL)
375                                                 {
376                                                 if (len > TLSEXT_MAXLEN_host_name || 
377                                                         ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
378                                                         {
379                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
380                                                         return 0;
381                                                         }
382                                                 memcpy(s->session->tlsext_hostname, sdata, len);
383                                                 s->session->tlsext_hostname[len]='\0';
384                                                 if (strlen(s->session->tlsext_hostname) != len) {
385                                                         OPENSSL_free(s->session->tlsext_hostname);
386                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
387                                                         return 0;
388                                                 }
389                                                 s->servername_done = 1; 
390
391                                                 }
392                                         else 
393                                                 s->servername_done = strlen(s->session->tlsext_hostname) == len 
394                                                         && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
395                                         
396                                         break;
397
398                                 default:
399                                         break;
400                                         }
401                                  
402                                 dsize -= len;
403                                 }
404                         if (dsize != 0) 
405                                 {
406                                 *al = SSL_AD_DECODE_ERROR;
407                                 return 0;
408                                 }
409
410                         }
411
412 #ifndef OPENSSL_NO_EC
413                 else if (type == TLSEXT_TYPE_ec_point_formats)
414                         {
415                         unsigned char *sdata = data;
416                         int ecpointformatlist_length = *(sdata++);
417
418                         if (ecpointformatlist_length != size - 1)
419                                 {
420                                 *al = TLS1_AD_DECODE_ERROR;
421                                 return 0;
422                                 }
423                         s->session->tlsext_ecpointformatlist_length = 0;
424                         if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
425                         if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
426                                 {
427                                 *al = TLS1_AD_INTERNAL_ERROR;
428                                 return 0;
429                                 }
430                         s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
431                         memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
432 #if 0
433                         fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
434                         sdata = s->session->tlsext_ecpointformatlist;
435                         for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
436                                 fprintf(stderr,"%i ",*(sdata++));
437                         fprintf(stderr,"\n");
438 #endif
439                         }
440                 else if (type == TLSEXT_TYPE_elliptic_curves)
441                         {
442                         unsigned char *sdata = data;
443                         int ellipticcurvelist_length = (*(sdata++) << 8);
444                         ellipticcurvelist_length += (*(sdata++));
445
446                         if (ellipticcurvelist_length != size - 2)
447                                 {
448                                 *al = TLS1_AD_DECODE_ERROR;
449                                 return 0;
450                                 }
451                         s->session->tlsext_ellipticcurvelist_length = 0;
452                         if (s->session->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->session->tlsext_ellipticcurvelist);
453                         if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
454                                 {
455                                 *al = TLS1_AD_INTERNAL_ERROR;
456                                 return 0;
457                                 }
458                         s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
459                         memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
460 #if 0
461                         fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
462                         sdata = s->session->tlsext_ellipticcurvelist;
463                         for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
464                                 fprintf(stderr,"%i ",*(sdata++));
465                         fprintf(stderr,"\n");
466 #endif
467                         }
468 #endif /* OPENSSL_NO_EC */
469                 data+=size;             
470                 }
471
472         *p = data;
473         return 1;
474 }
475
476 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
477         {
478         unsigned short type;
479         unsigned short size;
480         unsigned short len;  
481         unsigned char *data = *p;
482
483         int tlsext_servername = 0;
484
485         if (data >= (d+n-2))
486                 return 1;
487
488         n2s(data,len);
489
490         while(data <= (d+n-4))
491                 {
492                 n2s(data,type);
493                 n2s(data,size);
494
495                 if (data+size > (d+n))
496                         return 1;
497
498                 if (type == TLSEXT_TYPE_server_name)
499                         {
500                         if (s->tlsext_hostname == NULL || size > 0)
501                                 {
502                                 *al = TLS1_AD_UNRECOGNIZED_NAME;
503                                 return 0;
504                                 }
505                         tlsext_servername = 1;   
506                         }
507
508 #ifndef OPENSSL_NO_EC
509                 else if (type == TLSEXT_TYPE_ec_point_formats)
510                         {
511                         unsigned char *sdata = data;
512                         int ecpointformatlist_length = *(sdata++);
513
514                         if (ecpointformatlist_length != size - 1)
515                                 {
516                                 *al = TLS1_AD_DECODE_ERROR;
517                                 return 0;
518                                 }
519                         s->session->tlsext_ecpointformatlist_length = 0;
520                         if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
521                         if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
522                                 {
523                                 *al = TLS1_AD_INTERNAL_ERROR;
524                                 return 0;
525                                 }
526                         s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
527                         memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
528 #if 0
529                         fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
530                         sdata = s->session->tlsext_ecpointformatlist;
531                         for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
532                                 fprintf(stderr,"%i ",*(sdata++));
533                         fprintf(stderr,"\n");
534 #endif
535                         }
536 #endif /* OPENSSL_NO_EC */
537                 data+=size;             
538                 }
539
540         if (data != d+n)
541                 {
542                 *al = SSL_AD_DECODE_ERROR;
543                 return 0;
544                 }
545
546         if (!s->hit && tlsext_servername == 1)
547                 {
548                 if (s->tlsext_hostname)
549                         {
550                         if (s->session->tlsext_hostname == NULL)
551                                 {
552                                 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);   
553                                 if (!s->session->tlsext_hostname)
554                                         {
555                                         *al = SSL_AD_UNRECOGNIZED_NAME;
556                                         return 0;
557                                         }
558                                 }
559                         else 
560                                 {
561                                 *al = SSL_AD_DECODE_ERROR;
562                                 return 0;
563                                 }
564                         }
565                 }
566
567         *p = data;
568         return 1;
569 }
570
571 #ifndef OPENSSL_NO_EC
572 static int nid_list[] =
573         {
574                 NID_sect163k1, /* sect163k1 (1) */
575                 NID_sect163r1, /* sect163r1 (2) */
576                 NID_sect163r2, /* sect163r2 (3) */
577                 NID_sect193r1, /* sect193r1 (4) */ 
578                 NID_sect193r2, /* sect193r2 (5) */ 
579                 NID_sect233k1, /* sect233k1 (6) */
580                 NID_sect233r1, /* sect233r1 (7) */ 
581                 NID_sect239k1, /* sect239k1 (8) */ 
582                 NID_sect283k1, /* sect283k1 (9) */
583                 NID_sect283r1, /* sect283r1 (10) */ 
584                 NID_sect409k1, /* sect409k1 (11) */ 
585                 NID_sect409r1, /* sect409r1 (12) */
586                 NID_sect571k1, /* sect571k1 (13) */ 
587                 NID_sect571r1, /* sect571r1 (14) */ 
588                 NID_secp160k1, /* secp160k1 (15) */
589                 NID_secp160r1, /* secp160r1 (16) */ 
590                 NID_secp160r2, /* secp160r2 (17) */ 
591                 NID_secp192k1, /* secp192k1 (18) */
592                 NID_X9_62_prime192v1, /* secp192r1 (19) */ 
593                 NID_secp224k1, /* secp224k1 (20) */ 
594                 NID_secp224r1, /* secp224r1 (21) */
595                 NID_secp256k1, /* secp256k1 (22) */ 
596                 NID_X9_62_prime256v1, /* secp256r1 (23) */ 
597                 NID_secp384r1, /* secp384r1 (24) */
598                 NID_secp521r1  /* secp521r1 (25) */     
599         };
600         
601 int tls1_ec_curve_id2nid(int curve_id)
602         {
603         /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
604         if ((curve_id < 1) || (curve_id > sizeof(nid_list)/sizeof(nid_list[0]))) return 0;
605         return nid_list[curve_id-1];
606         }
607
608 int tls1_ec_nid2curve_id(int nid)
609         {
610         /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
611         switch (nid)
612                 {
613         case NID_sect163k1: /* sect163k1 (1) */
614                 return 1;
615         case NID_sect163r1: /* sect163r1 (2) */
616                 return 2;
617         case NID_sect163r2: /* sect163r2 (3) */
618                 return 3;
619         case NID_sect193r1: /* sect193r1 (4) */ 
620                 return 4;
621         case NID_sect193r2: /* sect193r2 (5) */ 
622                 return 5;
623         case NID_sect233k1: /* sect233k1 (6) */
624                 return 6;
625         case NID_sect233r1: /* sect233r1 (7) */ 
626                 return 7;
627         case NID_sect239k1: /* sect239k1 (8) */ 
628                 return 8;
629         case NID_sect283k1: /* sect283k1 (9) */
630                 return 9;
631         case NID_sect283r1: /* sect283r1 (10) */ 
632                 return 10;
633         case NID_sect409k1: /* sect409k1 (11) */ 
634                 return 11;
635         case NID_sect409r1: /* sect409r1 (12) */
636                 return 12;
637         case NID_sect571k1: /* sect571k1 (13) */ 
638                 return 13;
639         case NID_sect571r1: /* sect571r1 (14) */ 
640                 return 14;
641         case NID_secp160k1: /* secp160k1 (15) */
642                 return 15;
643         case NID_secp160r1: /* secp160r1 (16) */ 
644                 return 16;
645         case NID_secp160r2: /* secp160r2 (17) */ 
646                 return 17;
647         case NID_secp192k1: /* secp192k1 (18) */
648                 return 18;
649         case NID_X9_62_prime192v1: /* secp192r1 (19) */ 
650                 return 19;
651         case NID_secp224k1: /* secp224k1 (20) */ 
652                 return 20;
653         case NID_secp224r1: /* secp224r1 (21) */
654                 return 21;
655         case NID_secp256k1: /* secp256k1 (22) */ 
656                 return 22;
657         case NID_X9_62_prime256v1: /* secp256r1 (23) */ 
658                 return 23;
659         case NID_secp384r1: /* secp384r1 (24) */
660                 return 24;
661         case NID_secp521r1:  /* secp521r1 (25) */       
662                 return 25;
663         default:
664                 return 0;
665                 }
666         }
667 #endif /* OPENSSL_NO_EC */
668
669 int ssl_prepare_clienthello_tlsext(SSL *s)
670         {
671 #ifndef OPENSSL_NO_EC
672         /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats 
673          * and elliptic curves we support.
674          */
675         int using_ecc = 0;
676         int i;
677         unsigned char *j;
678         int algs;
679         STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
680         for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
681                 {
682                 algs = (sk_SSL_CIPHER_value(cipher_stack, i))->algorithms;
683                 if ((algs & SSL_kECDH) || (algs & SSL_kECDHE) || (algs & SSL_aECDSA)) 
684                         {
685                         using_ecc = 1;
686                         break;
687                         }
688
689                 }
690         using_ecc = using_ecc && (s->version == TLS1_VERSION);
691         if (using_ecc)
692                 {
693                 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
694                 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
695                         {
696                         SSLerr(SSL_F_TLS1_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
697                         return -1;
698                         }
699                 s->tlsext_ecpointformatlist_length = 3;
700                 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
701                 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
702                 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
703
704                 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
705                 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
706                 s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
707                 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
708                         {
709                         s->tlsext_ellipticcurvelist_length = 0;
710                         SSLerr(SSL_F_TLS1_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
711                         return -1;
712                         }
713                 for (i = 1, j = s->tlsext_ellipticcurvelist; i <= sizeof(nid_list)/sizeof(nid_list[0]); i++)
714                         s2n(i,j);
715                 }
716 #endif /* OPENSSL_NO_EC */
717         return 1;
718 }
719
720 int ssl_prepare_serverhello_tlsext(SSL *s)
721         {
722 #ifndef OPENSSL_NO_EC
723         /* If we are server and using an ECC cipher suite, send the point formats we support 
724          * if the client sent us an ECPointsFormat extension.  Note that the server is not
725          * supposed to send an EllipticCurves extension.
726          */
727         int algs = s->s3->tmp.new_cipher->algorithms;
728         int using_ecc = (algs & SSL_kECDH) || (algs & SSL_kECDHE) || (algs & SSL_aECDSA);
729         using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
730
731         if (using_ecc)
732                 {
733                 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
734                 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
735                         {
736                         SSLerr(SSL_F_TLS1_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
737                         return -1;
738                         }
739                 s->tlsext_ecpointformatlist_length = 3;
740                 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
741                 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
742                 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
743                 }
744 #endif /* OPENSSL_NO_EC */
745         return 1;
746 }
747
748 int ssl_check_clienthello_tlsext(SSL *s)
749         {
750         int ret=SSL_TLSEXT_ERR_NOACK;
751         int al = SSL_AD_UNRECOGNIZED_NAME;
752
753 #ifndef OPENSSL_NO_EC
754         /* The handling of the ECPointFormats extension is done elsewhere, namely in 
755          * ssl3_choose_cipher in s3_lib.c.
756          */
757         /* The handling of the EllipticCurves extension is done elsewhere, namely in 
758          * ssl3_choose_cipher in s3_lib.c.
759          */
760 #endif
761
762         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
763                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
764         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
765                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
766
767         switch (ret) {
768                 case SSL_TLSEXT_ERR_ALERT_FATAL:
769                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
770                         return -1;
771
772                 case SSL_TLSEXT_ERR_ALERT_WARNING:
773                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
774                         return 1; 
775                                         
776                 case SSL_TLSEXT_ERR_NOACK:
777                         s->servername_done=0;
778                         default:
779                 return 1;
780         }
781 }
782
783 int ssl_check_serverhello_tlsext(SSL *s)
784         {
785         int ret=SSL_TLSEXT_ERR_NOACK;
786         int al = SSL_AD_UNRECOGNIZED_NAME;
787
788 #ifndef OPENSSL_NO_EC
789         /* If we are client and using an elliptic curve cryptography cipher suite, then server
790          * must return a an EC point formats lists containing uncompressed.
791          */
792         int algs = s->s3->tmp.new_cipher->algorithms;
793         if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) && 
794             ((algs & SSL_kECDH) || (algs & SSL_kECDHE) || (algs & SSL_aECDSA))) 
795                 {
796                 /* we are using an ECC cipher */
797                 size_t i;
798                 unsigned char *list;
799                 int found_uncompressed = 0;
800                 if ((s->session->tlsext_ecpointformatlist == NULL) || (s->session->tlsext_ecpointformatlist_length == 0))
801                         {
802                         SSLerr(SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
803                         return -1;
804                         }
805                 list = s->session->tlsext_ecpointformatlist;
806                 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
807                         {
808                         if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
809                                 {
810                                 found_uncompressed = 1;
811                                 break;
812                                 }
813                         }
814                 if (!found_uncompressed)
815                         {
816                         SSLerr(SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
817                         return -1;
818                         }
819                 }
820         ret = SSL_TLSEXT_ERR_OK;
821 #endif /* OPENSSL_NO_EC */
822
823         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
824                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
825         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
826                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
827
828         switch (ret) {
829                 case SSL_TLSEXT_ERR_ALERT_FATAL:
830                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
831                         return -1;
832
833                 case SSL_TLSEXT_ERR_ALERT_WARNING:
834                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
835                         return 1; 
836                                         
837                 case SSL_TLSEXT_ERR_NOACK:
838                         s->servername_done=0;
839                         default:
840                 return 1;
841         }
842 }
843 #endif
844