35d9e6d38260454ca84d8b9633aa6a35ccfc518b
[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
234                 s2n(s->tlsext_ellipticcurvelist_length + 2,ret);
235                 *(ret++) = (unsigned char) ((s->tlsext_ellipticcurvelist_length >> 8) & 0xFF);
236                 *(ret++) = (unsigned char) (s->tlsext_ellipticcurvelist_length & 0xFF);
237                 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
238                 ret+=s->tlsext_ellipticcurvelist_length;
239                 }
240 #endif /* OPENSSL_NO_EC */
241
242         if ((extdatalen = ret-p-2)== 0) 
243                 return p;
244
245         s2n(extdatalen,p);
246         return ret;
247 }
248
249 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
250         {
251         int extdatalen=0;
252         unsigned char *ret = p;
253
254         ret+=2;
255         if (ret>=limit) return NULL; /* this really never occurs, but ... */
256
257         if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
258                 { 
259                 if (limit - p - 4 < 0) return NULL; 
260
261                 s2n(TLSEXT_TYPE_server_name,ret);
262                 s2n(0,ret);
263                 }
264 #ifndef OPENSSL_NO_EC
265         if (s->tlsext_ecpointformatlist != NULL)
266                 {
267                 /* Add TLS extension ECPointFormats to the ServerHello message */
268                 long lenmax; 
269
270                 if ((lenmax = limit - p - 5) < 0) return NULL; 
271                 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
272                 if (s->tlsext_ecpointformatlist_length > 255)
273                         {
274                         SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
275                         return NULL;
276                         }
277                 
278                 s2n(TLSEXT_TYPE_ec_point_formats,ret);
279                 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
280                 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
281                 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
282                 ret+=s->tlsext_ecpointformatlist_length;
283
284                 }
285         /* Currently the server should not respond with a SupportedCurves extension */
286 #endif /* OPENSSL_NO_EC */
287         
288         if ((extdatalen = ret-p-2)== 0) 
289                 return p;
290
291         s2n(extdatalen,p);
292         return ret;
293 }
294
295 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
296         {
297         unsigned short type;
298         unsigned short size;
299         unsigned short len;
300         unsigned char *data = *p;
301         s->servername_done = 0;
302
303         if (data >= (d+n-2))
304                 return 1;
305         n2s(data,len);
306
307         if (data > (d+n-len)) 
308                 return 1;
309
310         while (data <= (d+n-4))
311                 {
312                 n2s(data,type);
313                 n2s(data,size);
314
315                 if (data+size > (d+n))
316                         return 1;
317                 
318 /* The servername extension is treated as follows:
319
320    - Only the hostname type is supported with a maximum length of 255.
321    - The servername is rejected if too long or if it contains zeros,
322      in which case an fatal alert is generated.
323    - The servername field is maintained together with the session cache.
324    - When a session is resumed, the servername call back invoked in order
325      to allow the application to position itself to the right context. 
326    - The servername is acknowledged if it is new for a session or when 
327      it is identical to a previously used for the same session. 
328      Applications can control the behaviour.  They can at any time
329      set a 'desirable' servername for a new SSL object. This can be the
330      case for example with HTTPS when a Host: header field is received and
331      a renegotiation is requested. In this case, a possible servername
332      presented in the new client hello is only acknowledged if it matches
333      the value of the Host: field. 
334    - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
335      if they provide for changing an explicit servername context for the session,
336      i.e. when the session has been established with a servername extension. 
337    - On session reconnect, the servername extension may be absent. 
338
339 */      
340
341                 if (type == TLSEXT_TYPE_server_name)
342                         {
343                         unsigned char *sdata;
344                         int servname_type;
345                         int dsize; 
346                 
347                         if (size < 2) 
348                                 {
349                                 *al = SSL_AD_DECODE_ERROR;
350                                 return 0;
351                                 }
352                         n2s(data,dsize);  
353                         size -= 2;                    
354                         if (dsize > size  ) 
355                                 {
356                                 *al = SSL_AD_DECODE_ERROR;
357                                 return 0;
358                                 } 
359
360                         sdata = data;
361                         while (dsize > 3) 
362                                 {
363                                 servname_type = *(sdata++); 
364                                 n2s(sdata,len);
365                                 dsize -= 3;
366
367                                 if (len > dsize) 
368                                         {
369                                         *al = SSL_AD_DECODE_ERROR;
370                                         return 0;
371                                         }
372                                 if (s->servername_done == 0)
373                                 switch (servname_type)
374                                         {
375                                 case TLSEXT_NAMETYPE_host_name:
376                                         if (s->session->tlsext_hostname == NULL)
377                                                 {
378                                                 if (len > TLSEXT_MAXLEN_host_name || 
379                                                         ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
380                                                         {
381                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
382                                                         return 0;
383                                                         }
384                                                 memcpy(s->session->tlsext_hostname, sdata, len);
385                                                 s->session->tlsext_hostname[len]='\0';
386                                                 if (strlen(s->session->tlsext_hostname) != len) {
387                                                         OPENSSL_free(s->session->tlsext_hostname);
388                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
389                                                         return 0;
390                                                 }
391                                                 s->servername_done = 1; 
392
393                                                 }
394                                         else 
395                                                 s->servername_done = strlen(s->session->tlsext_hostname) == len 
396                                                         && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
397                                         
398                                         break;
399
400                                 default:
401                                         break;
402                                         }
403                                  
404                                 dsize -= len;
405                                 }
406                         if (dsize != 0) 
407                                 {
408                                 *al = SSL_AD_DECODE_ERROR;
409                                 return 0;
410                                 }
411
412                         }
413
414 #ifndef OPENSSL_NO_EC
415                 else if (type == TLSEXT_TYPE_ec_point_formats)
416                         {
417                         unsigned char *sdata = data;
418                         int ecpointformatlist_length = *(sdata++);
419
420                         if (ecpointformatlist_length != size - 1)
421                                 {
422                                 *al = TLS1_AD_DECODE_ERROR;
423                                 return 0;
424                                 }
425                         s->session->tlsext_ecpointformatlist_length = 0;
426                         if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
427                         if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
428                                 {
429                                 *al = TLS1_AD_INTERNAL_ERROR;
430                                 return 0;
431                                 }
432                         s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
433                         memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
434 #if 0
435                         fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
436                         sdata = s->session->tlsext_ecpointformatlist;
437                         for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
438                                 fprintf(stderr,"%i ",*(sdata++));
439                         fprintf(stderr,"\n");
440 #endif
441                         }
442                 else if (type == TLSEXT_TYPE_elliptic_curves)
443                         {
444                         unsigned char *sdata = data;
445                         int ellipticcurvelist_length = (*(sdata++) << 8);
446                         ellipticcurvelist_length += (*(sdata++));
447
448                         if (ellipticcurvelist_length != size - 2)
449                                 {
450                                 *al = TLS1_AD_DECODE_ERROR;
451                                 return 0;
452                                 }
453                         s->session->tlsext_ellipticcurvelist_length = 0;
454                         if (s->session->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->session->tlsext_ellipticcurvelist);
455                         if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
456                                 {
457                                 *al = TLS1_AD_INTERNAL_ERROR;
458                                 return 0;
459                                 }
460                         s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
461                         memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
462 #if 0
463                         fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
464                         sdata = s->session->tlsext_ellipticcurvelist;
465                         for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
466                                 fprintf(stderr,"%i ",*(sdata++));
467                         fprintf(stderr,"\n");
468 #endif
469                         }
470 #endif /* OPENSSL_NO_EC */
471                 data+=size;             
472                 }
473
474         *p = data;
475         return 1;
476 }
477
478 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
479         {
480         unsigned short type;
481         unsigned short size;
482         unsigned short len;  
483         unsigned char *data = *p;
484
485         int tlsext_servername = 0;
486
487         if (data >= (d+n-2))
488                 return 1;
489
490         n2s(data,len);
491
492         while(data <= (d+n-4))
493                 {
494                 n2s(data,type);
495                 n2s(data,size);
496
497                 if (data+size > (d+n))
498                         return 1;
499
500                 if (type == TLSEXT_TYPE_server_name)
501                         {
502                         if (s->tlsext_hostname == NULL || size > 0)
503                                 {
504                                 *al = TLS1_AD_UNRECOGNIZED_NAME;
505                                 return 0;
506                                 }
507                         tlsext_servername = 1;   
508                         }
509
510 #ifndef OPENSSL_NO_EC
511                 else if (type == TLSEXT_TYPE_ec_point_formats)
512                         {
513                         unsigned char *sdata = data;
514                         int ecpointformatlist_length = *(sdata++);
515
516                         if (ecpointformatlist_length != size - 1)
517                                 {
518                                 *al = TLS1_AD_DECODE_ERROR;
519                                 return 0;
520                                 }
521                         s->session->tlsext_ecpointformatlist_length = 0;
522                         if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
523                         if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
524                                 {
525                                 *al = TLS1_AD_INTERNAL_ERROR;
526                                 return 0;
527                                 }
528                         s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
529                         memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
530 #if 0
531                         fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
532                         sdata = s->session->tlsext_ecpointformatlist;
533                         for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
534                                 fprintf(stderr,"%i ",*(sdata++));
535                         fprintf(stderr,"\n");
536 #endif
537                         }
538 #endif /* OPENSSL_NO_EC */
539                 data+=size;             
540                 }
541
542         if (data != d+n)
543                 {
544                 *al = SSL_AD_DECODE_ERROR;
545                 return 0;
546                 }
547
548         if (!s->hit && tlsext_servername == 1)
549                 {
550                 if (s->tlsext_hostname)
551                         {
552                         if (s->session->tlsext_hostname == NULL)
553                                 {
554                                 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);   
555                                 if (!s->session->tlsext_hostname)
556                                         {
557                                         *al = SSL_AD_UNRECOGNIZED_NAME;
558                                         return 0;
559                                         }
560                                 }
561                         else 
562                                 {
563                                 *al = SSL_AD_DECODE_ERROR;
564                                 return 0;
565                                 }
566                         }
567                 }
568
569         *p = data;
570         return 1;
571 }
572
573 #ifndef OPENSSL_NO_EC
574 static int nid_list[] =
575         {
576                 NID_sect163k1, /* sect163k1 (1) */
577                 NID_sect163r1, /* sect163r1 (2) */
578                 NID_sect163r2, /* sect163r2 (3) */
579                 NID_sect193r1, /* sect193r1 (4) */ 
580                 NID_sect193r2, /* sect193r2 (5) */ 
581                 NID_sect233k1, /* sect233k1 (6) */
582                 NID_sect233r1, /* sect233r1 (7) */ 
583                 NID_sect239k1, /* sect239k1 (8) */ 
584                 NID_sect283k1, /* sect283k1 (9) */
585                 NID_sect283r1, /* sect283r1 (10) */ 
586                 NID_sect409k1, /* sect409k1 (11) */ 
587                 NID_sect409r1, /* sect409r1 (12) */
588                 NID_sect571k1, /* sect571k1 (13) */ 
589                 NID_sect571r1, /* sect571r1 (14) */ 
590                 NID_secp160k1, /* secp160k1 (15) */
591                 NID_secp160r1, /* secp160r1 (16) */ 
592                 NID_secp160r2, /* secp160r2 (17) */ 
593                 NID_secp192k1, /* secp192k1 (18) */
594                 NID_X9_62_prime192v1, /* secp192r1 (19) */ 
595                 NID_secp224k1, /* secp224k1 (20) */ 
596                 NID_secp224r1, /* secp224r1 (21) */
597                 NID_secp256k1, /* secp256k1 (22) */ 
598                 NID_X9_62_prime256v1, /* secp256r1 (23) */ 
599                 NID_secp384r1, /* secp384r1 (24) */
600                 NID_secp521r1  /* secp521r1 (25) */     
601         };
602         
603 int tls1_ec_curve_id2nid(int curve_id)
604         {
605         /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
606         if ((curve_id < 1) || (curve_id > sizeof(nid_list)/sizeof(nid_list[0]))) return 0;
607         return nid_list[curve_id-1];
608         }
609
610 int tls1_ec_nid2curve_id(int nid)
611         {
612         /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
613         switch (nid)
614                 {
615         case NID_sect163k1: /* sect163k1 (1) */
616                 return 1;
617         case NID_sect163r1: /* sect163r1 (2) */
618                 return 2;
619         case NID_sect163r2: /* sect163r2 (3) */
620                 return 3;
621         case NID_sect193r1: /* sect193r1 (4) */ 
622                 return 4;
623         case NID_sect193r2: /* sect193r2 (5) */ 
624                 return 5;
625         case NID_sect233k1: /* sect233k1 (6) */
626                 return 6;
627         case NID_sect233r1: /* sect233r1 (7) */ 
628                 return 7;
629         case NID_sect239k1: /* sect239k1 (8) */ 
630                 return 8;
631         case NID_sect283k1: /* sect283k1 (9) */
632                 return 9;
633         case NID_sect283r1: /* sect283r1 (10) */ 
634                 return 10;
635         case NID_sect409k1: /* sect409k1 (11) */ 
636                 return 11;
637         case NID_sect409r1: /* sect409r1 (12) */
638                 return 12;
639         case NID_sect571k1: /* sect571k1 (13) */ 
640                 return 13;
641         case NID_sect571r1: /* sect571r1 (14) */ 
642                 return 14;
643         case NID_secp160k1: /* secp160k1 (15) */
644                 return 15;
645         case NID_secp160r1: /* secp160r1 (16) */ 
646                 return 16;
647         case NID_secp160r2: /* secp160r2 (17) */ 
648                 return 17;
649         case NID_secp192k1: /* secp192k1 (18) */
650                 return 18;
651         case NID_X9_62_prime192v1: /* secp192r1 (19) */ 
652                 return 19;
653         case NID_secp224k1: /* secp224k1 (20) */ 
654                 return 20;
655         case NID_secp224r1: /* secp224r1 (21) */
656                 return 21;
657         case NID_secp256k1: /* secp256k1 (22) */ 
658                 return 22;
659         case NID_X9_62_prime256v1: /* secp256r1 (23) */ 
660                 return 23;
661         case NID_secp384r1: /* secp384r1 (24) */
662                 return 24;
663         case NID_secp521r1:  /* secp521r1 (25) */       
664                 return 25;
665         default:
666                 return 0;
667                 }
668         }
669 #endif /* OPENSSL_NO_EC */
670
671 int ssl_prepare_clienthello_tlsext(SSL *s)
672         {
673 #ifndef OPENSSL_NO_EC
674         /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats 
675          * and elliptic curves we support.
676          */
677         int using_ecc = 0;
678         int i;
679         unsigned char *j;
680         int algs;
681         STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
682         for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
683                 {
684                 algs = (sk_SSL_CIPHER_value(cipher_stack, i))->algorithms;
685                 if ((algs & SSL_kECDH) || (algs & SSL_kECDHE) || (algs & SSL_aECDSA)) 
686                         {
687                         using_ecc = 1;
688                         break;
689                         }
690
691                 }
692         using_ecc = using_ecc && (s->version == TLS1_VERSION);
693         if (using_ecc)
694                 {
695                 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
696                 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
697                         {
698                         SSLerr(SSL_F_TLS1_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
699                         return -1;
700                         }
701                 s->tlsext_ecpointformatlist_length = 3;
702                 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
703                 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
704                 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
705
706                 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
707                 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
708                 s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
709                 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
710                         {
711                         s->tlsext_ellipticcurvelist_length = 0;
712                         SSLerr(SSL_F_TLS1_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
713                         return -1;
714                         }
715                 for (i = 1, j = s->tlsext_ellipticcurvelist; i <= sizeof(nid_list)/sizeof(nid_list[0]); i++)
716                         s2n(i,j);
717                 }
718 #endif /* OPENSSL_NO_EC */
719         return 1;
720 }
721
722 int ssl_prepare_serverhello_tlsext(SSL *s)
723         {
724 #ifndef OPENSSL_NO_EC
725         /* If we are server and using an ECC cipher suite, send the point formats we support 
726          * if the client sent us an ECPointsFormat extension.  Note that the server is not
727          * supposed to send an EllipticCurves extension.
728          */
729         int algs = s->s3->tmp.new_cipher->algorithms;
730         int using_ecc = (algs & SSL_kECDH) || (algs & SSL_kECDHE) || (algs & SSL_aECDSA);
731         using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
732
733         if (using_ecc)
734                 {
735                 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
736                 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
737                         {
738                         SSLerr(SSL_F_TLS1_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
739                         return -1;
740                         }
741                 s->tlsext_ecpointformatlist_length = 3;
742                 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
743                 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
744                 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
745                 }
746 #endif /* OPENSSL_NO_EC */
747         return 1;
748 }
749
750 int ssl_check_clienthello_tlsext(SSL *s)
751         {
752         int ret=SSL_TLSEXT_ERR_NOACK;
753         int al = SSL_AD_UNRECOGNIZED_NAME;
754
755 #ifndef OPENSSL_NO_EC
756         /* The handling of the ECPointFormats extension is done elsewhere, namely in 
757          * ssl3_choose_cipher in s3_lib.c.
758          */
759         /* The handling of the EllipticCurves extension is done elsewhere, namely in 
760          * ssl3_choose_cipher in s3_lib.c.
761          */
762 #endif
763
764         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
765                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
766         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
767                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
768
769         switch (ret) {
770                 case SSL_TLSEXT_ERR_ALERT_FATAL:
771                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
772                         return -1;
773
774                 case SSL_TLSEXT_ERR_ALERT_WARNING:
775                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
776                         return 1; 
777                                         
778                 case SSL_TLSEXT_ERR_NOACK:
779                         s->servername_done=0;
780                         default:
781                 return 1;
782         }
783 }
784
785 int ssl_check_serverhello_tlsext(SSL *s)
786         {
787         int ret=SSL_TLSEXT_ERR_NOACK;
788         int al = SSL_AD_UNRECOGNIZED_NAME;
789
790 #ifndef OPENSSL_NO_EC
791         /* If we are client and using an elliptic curve cryptography cipher suite, then server
792          * must return a an EC point formats lists containing uncompressed.
793          */
794         int algs = s->s3->tmp.new_cipher->algorithms;
795         if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) && 
796             ((algs & SSL_kECDH) || (algs & SSL_kECDHE) || (algs & SSL_aECDSA))) 
797                 {
798                 /* we are using an ECC cipher */
799                 size_t i;
800                 unsigned char *list;
801                 int found_uncompressed = 0;
802                 if ((s->session->tlsext_ecpointformatlist == NULL) || (s->session->tlsext_ecpointformatlist_length == 0))
803                         {
804                         SSLerr(SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
805                         return -1;
806                         }
807                 list = s->session->tlsext_ecpointformatlist;
808                 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
809                         {
810                         if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
811                                 {
812                                 found_uncompressed = 1;
813                                 break;
814                                 }
815                         }
816                 if (!found_uncompressed)
817                         {
818                         SSLerr(SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
819                         return -1;
820                         }
821                 }
822         ret = SSL_TLSEXT_ERR_OK;
823 #endif /* OPENSSL_NO_EC */
824
825         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
826                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
827         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
828                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
829
830         switch (ret) {
831                 case SSL_TLSEXT_ERR_ALERT_FATAL:
832                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
833                         return -1;
834
835                 case SSL_TLSEXT_ERR_ALERT_WARNING:
836                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
837                         return 1; 
838                                         
839                 case SSL_TLSEXT_ERR_NOACK:
840                         s->servername_done=0;
841                         default:
842                 return 1;
843         }
844 }
845 #endif
846