261e2eb81141aa7a25942624317901bbdd89f705
[oweals/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 #include <stdio.h>
60 #include <openssl/objects.h>
61 #include <openssl/evp.h>
62 #include <openssl/hmac.h>
63 #include <openssl/ocsp.h>
64 #include "ssl_locl.h"
65
66 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
67
68 #ifndef OPENSSL_NO_TLSEXT
69 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
70                                 const unsigned char *sess_id, int sesslen,
71                                 SSL_SESSION **psess);
72 #endif
73
74 SSL3_ENC_METHOD TLSv1_enc_data={
75         tls1_enc,
76         tls1_mac,
77         tls1_setup_key_block,
78         tls1_generate_master_secret,
79         tls1_change_cipher_state,
80         tls1_final_finish_mac,
81         TLS1_FINISH_MAC_LENGTH,
82         tls1_cert_verify_mac,
83         TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
84         TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
85         tls1_alert_code,
86         };
87
88 long tls1_default_timeout(void)
89         {
90         /* 2 hours, the 24 hours mentioned in the TLSv1 spec
91          * is way too long for http, the cache would over fill */
92         return(60*60*2);
93         }
94
95 IMPLEMENT_tls1_meth_func(tlsv1_base_method,
96                         ssl_undefined_function,
97                         ssl_undefined_function,
98                         ssl_bad_method)
99
100 int tls1_new(SSL *s)
101         {
102         if (!ssl3_new(s)) return(0);
103         s->method->ssl_clear(s);
104         return(1);
105         }
106
107 void tls1_free(SSL *s)
108         {
109         ssl3_free(s);
110         }
111
112 void tls1_clear(SSL *s)
113         {
114         ssl3_clear(s);
115         s->version=TLS1_VERSION;
116         }
117
118 #if 0
119 long tls1_ctrl(SSL *s, int cmd, long larg, char *parg)
120         {
121         return(0);
122         }
123
124 long tls1_callback_ctrl(SSL *s, int cmd, void *(*fp)())
125         {
126         return(0);
127         }
128 #endif
129
130 #ifndef OPENSSL_NO_TLSEXT
131 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
132         {
133         int extdatalen=0;
134         unsigned char *ret = p;
135
136         /* don't add extensions for SSLv3 */
137         if (s->client_version == SSL3_VERSION)
138                 return p;
139
140         ret+=2;
141
142         if (ret>=limit) return NULL; /* this really never occurs, but ... */
143
144         if (s->tlsext_hostname != NULL)
145                 { 
146                 /* Add TLS extension servername to the Client Hello message */
147                 unsigned long size_str;
148                 long lenmax; 
149
150                 /* check for enough space.
151                    4 for the servername type and entension length
152                    2 for servernamelist length
153                    1 for the hostname type
154                    2 for hostname length
155                    + hostname length 
156                 */
157                    
158                 if ((lenmax = limit - ret - 9) < 0 
159                 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) 
160                         return NULL;
161                         
162                 /* extension type and length */
163                 s2n(TLSEXT_TYPE_server_name,ret); 
164                 s2n(size_str+5,ret);
165                 
166                 /* length of servername list */
167                 s2n(size_str+3,ret);
168         
169                 /* hostname type, length and hostname */
170                 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
171                 s2n(size_str,ret);
172                 memcpy(ret, s->tlsext_hostname, size_str);
173                 ret+=size_str;
174
175                 }
176         
177         /* Add the renegotiation option: TODOEKR switch */
178         {
179           int el;
180           
181           if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0))
182               {
183               SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
184               return NULL;
185               }
186
187           if((limit - p - 4 - el) < 0) return NULL;
188           
189           s2n(TLSEXT_TYPE_renegotiate,ret);
190           s2n(el,ret);
191
192           if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el))
193               {
194               SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
195               return NULL;
196               }
197
198           ret += el;
199         }
200
201            
202         if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
203                 {
204                 int ticklen;
205                 if (!s->new_session && s->session && s->session->tlsext_tick)
206                         ticklen = s->session->tlsext_ticklen;
207                 else
208                         ticklen = 0;
209                 /* Check for enough room 2 for extension type, 2 for len
210                  * rest for ticket
211                  */
212                 if (limit - ret - 4 - ticklen < 0)
213                         return NULL;
214                 s2n(TLSEXT_TYPE_session_ticket,ret); 
215                 s2n(ticklen,ret);
216                 if (ticklen)
217                         {
218                         memcpy(ret, s->session->tlsext_tick, ticklen);
219                         ret += ticklen;
220                         }
221                 }
222
223         if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
224                 {
225                 int i;
226                 long extlen, idlen, itmp;
227                 OCSP_RESPID *id;
228
229                 idlen = 0;
230                 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
231                         {
232                         id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
233                         itmp = i2d_OCSP_RESPID(id, NULL);
234                         if (itmp <= 0)
235                                 return NULL;
236                         idlen += itmp + 2;
237                         }
238
239                 if (s->tlsext_ocsp_exts)
240                         {
241                         extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
242                         if (extlen < 0)
243                                 return NULL;
244                         }
245                 else
246                         extlen = 0;
247                         
248                 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
249                 s2n(TLSEXT_TYPE_status_request, ret);
250                 if (extlen + idlen > 0xFFF0)
251                         return NULL;
252                 s2n(extlen + idlen + 5, ret);
253                 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
254                 s2n(idlen, ret);
255                 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
256                         {
257                         /* save position of id len */
258                         unsigned char *q = ret;
259                         id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
260                         /* skip over id len */
261                         ret += 2;
262                         itmp = i2d_OCSP_RESPID(id, &ret);
263                         /* write id len */
264                         s2n(itmp, q);
265                         }
266                 s2n(extlen, ret);
267                 if (extlen > 0)
268                         i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
269                 }
270
271         if ((extdatalen = ret-p-2)== 0) 
272                 return p;
273
274         s2n(extdatalen,p);
275         return ret;
276         }
277
278 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
279         {
280         int extdatalen=0;
281         unsigned char *ret = p;
282
283         /* don't add extensions for SSLv3 */
284         if (s->version == SSL3_VERSION)
285                 return p;
286         
287         ret+=2;
288         if (ret>=limit) return NULL; /* this really never occurs, but ... */
289
290         if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
291                 { 
292                 if (limit - ret - 4 < 0) return NULL; 
293
294                 s2n(TLSEXT_TYPE_server_name,ret);
295                 s2n(0,ret);
296                 }
297
298         if(s->s3->send_connection_binding)
299         {
300           int el;
301           
302           if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0))
303               {
304               SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
305               return NULL;
306               }
307
308           if((limit - p - 4 - el) < 0) return NULL;
309           
310           s2n(TLSEXT_TYPE_renegotiate,ret);
311           s2n(el,ret);
312
313           if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el))
314               {
315               SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
316               return NULL;
317               }
318
319           ret += el;
320         }
321         
322         if (s->tlsext_ticket_expected
323                 && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) 
324                 { 
325                 if (limit - ret - 4 < 0) return NULL; 
326                 s2n(TLSEXT_TYPE_session_ticket,ret);
327                 s2n(0,ret);
328                 }
329
330         if (s->tlsext_status_expected)
331                 { 
332                 if ((long)(limit - ret - 4) < 0) return NULL; 
333                 s2n(TLSEXT_TYPE_status_request,ret);
334                 s2n(0,ret);
335                 }
336
337         if ((extdatalen = ret-p-2)== 0) 
338                 return p;
339
340         s2n(extdatalen,p);
341         return ret;
342         }
343
344 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
345         {
346         unsigned short type;
347         unsigned short size;
348         unsigned short len;
349         unsigned char *data = *p;
350         int renegotiate_seen = 0;
351
352         s->servername_done = 0;
353         s->tlsext_status_type = -1;
354         s->s3->send_connection_binding = 0;
355
356         if (data >= (d+n-2))
357                 {
358                 if (s->new_session
359                         && !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
360                         {
361                         /* We should always see one extension: the renegotiate extension */
362                         *al = SSL_AD_ILLEGAL_PARAMETER; /* is this the right alert? */
363                         return 0;
364                         }
365                 return 1;
366                 }
367         n2s(data,len);
368
369         if (data > (d+n-len)) 
370                 return 1;
371
372         while (data <= (d+n-4))
373                 {
374                 n2s(data,type);
375                 n2s(data,size);
376
377                 if (data+size > (d+n))
378                         return 1;
379
380                 if (s->tlsext_debug_cb)
381                         s->tlsext_debug_cb(s, 0, type, data, size,
382                                                 s->tlsext_debug_arg);
383 /* The servername extension is treated as follows:
384
385    - Only the hostname type is supported with a maximum length of 255.
386    - The servername is rejected if too long or if it contains zeros,
387      in which case an fatal alert is generated.
388    - The servername field is maintained together with the session cache.
389    - When a session is resumed, the servername call back invoked in order
390      to allow the application to position itself to the right context. 
391    - The servername is acknowledged if it is new for a session or when 
392      it is identical to a previously used for the same session. 
393      Applications can control the behaviour.  They can at any time
394      set a 'desirable' servername for a new SSL object. This can be the
395      case for example with HTTPS when a Host: header field is received and
396      a renegotiation is requested. In this case, a possible servername
397      presented in the new client hello is only acknowledged if it matches
398      the value of the Host: field. 
399    - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
400      if they provide for changing an explicit servername context for the session,
401      i.e. when the session has been established with a servername extension. 
402    - On session reconnect, the servername extension may be absent. 
403
404 */      
405
406                 if (type == TLSEXT_TYPE_server_name)
407                         {
408                         unsigned char *sdata;
409                         int servname_type;
410                         int dsize; 
411                 
412                         if (size < 2) 
413                                 {
414                                 *al = SSL_AD_DECODE_ERROR;
415                                 return 0;
416                                 }
417                         n2s(data,dsize);  
418                         size -= 2;
419                         if (dsize > size  ) 
420                                 {
421                                 *al = SSL_AD_DECODE_ERROR;
422                                 return 0;
423                                 } 
424
425                         sdata = data;
426                         while (dsize > 3) 
427                                 {
428                                 servname_type = *(sdata++); 
429                                 n2s(sdata,len);
430                                 dsize -= 3;
431
432                                 if (len > dsize) 
433                                         {
434                                         *al = SSL_AD_DECODE_ERROR;
435                                         return 0;
436                                         }
437                                 if (s->servername_done == 0)
438                                 switch (servname_type)
439                                         {
440                                 case TLSEXT_NAMETYPE_host_name:
441                                         if (s->session->tlsext_hostname == NULL)
442                                                 {
443                                                 if (len > TLSEXT_MAXLEN_host_name || 
444                                                         ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
445                                                         {
446                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
447                                                         return 0;
448                                                         }
449                                                 memcpy(s->session->tlsext_hostname, sdata, len);
450                                                 s->session->tlsext_hostname[len]='\0';
451                                                 if (strlen(s->session->tlsext_hostname) != len) {
452                                                         OPENSSL_free(s->session->tlsext_hostname);
453                                                         s->session->tlsext_hostname = NULL;
454                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
455                                                         return 0;
456                                                 }
457                                                 s->servername_done = 1; 
458
459                                                 }
460                                         else 
461                                                 s->servername_done = strlen(s->session->tlsext_hostname) == len 
462                                                         && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
463                                         
464                                         break;
465
466                                 default:
467                                         break;
468                                         }
469                                  
470                                 dsize -= len;
471                                 }
472                         if (dsize != 0) 
473                                 {
474                                 *al = SSL_AD_DECODE_ERROR;
475                                 return 0;
476                                 }
477
478                         }
479                 else if (type == TLSEXT_TYPE_renegotiate)
480                         {
481                         if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
482                                 return 0;
483                         renegotiate_seen = 1;
484                         }
485                 else if (type == TLSEXT_TYPE_status_request
486                                                 && s->ctx->tlsext_status_cb)
487                         {
488                 
489                         if (size < 5) 
490                                 {
491                                 *al = SSL_AD_DECODE_ERROR;
492                                 return 0;
493                                 }
494
495                         s->tlsext_status_type = *data++;
496                         size--;
497                         if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
498                                 {
499                                 const unsigned char *sdata;
500                                 int dsize;
501                                 /* Read in responder_id_list */
502                                 n2s(data,dsize);
503                                 size -= 2;
504                                 if (dsize > size  ) 
505                                         {
506                                         *al = SSL_AD_DECODE_ERROR;
507                                         return 0;
508                                         }
509                                 while (dsize > 0)
510                                         {
511                                         OCSP_RESPID *id;
512                                         int idsize;
513                                         if (dsize < 4)
514                                                 {
515                                                 *al = SSL_AD_DECODE_ERROR;
516                                                 return 0;
517                                                 }
518                                         n2s(data, idsize);
519                                         dsize -= 2 + idsize;
520                                         if (dsize < 0)
521                                                 {
522                                                 *al = SSL_AD_DECODE_ERROR;
523                                                 return 0;
524                                                 }
525                                         sdata = data;
526                                         data += idsize;
527                                         id = d2i_OCSP_RESPID(NULL,
528                                                                 &sdata, idsize);
529                                         if (!id)
530                                                 {
531                                                 *al = SSL_AD_DECODE_ERROR;
532                                                 return 0;
533                                                 }
534                                         if (data != sdata)
535                                                 {
536                                                 OCSP_RESPID_free(id);
537                                                 *al = SSL_AD_DECODE_ERROR;
538                                                 return 0;
539                                                 }
540                                         if (!s->tlsext_ocsp_ids
541                                                 && !(s->tlsext_ocsp_ids =
542                                                 sk_OCSP_RESPID_new_null()))
543                                                 {
544                                                 OCSP_RESPID_free(id);
545                                                 *al = SSL_AD_INTERNAL_ERROR;
546                                                 return 0;
547                                                 }
548                                         if (!sk_OCSP_RESPID_push(
549                                                         s->tlsext_ocsp_ids, id))
550                                                 {
551                                                 OCSP_RESPID_free(id);
552                                                 *al = SSL_AD_INTERNAL_ERROR;
553                                                 return 0;
554                                                 }
555                                         }
556
557                                 /* Read in request_extensions */
558                                 n2s(data,dsize);
559                                 size -= 2;
560                                 if (dsize > size) 
561                                         {
562                                         *al = SSL_AD_DECODE_ERROR;
563                                         return 0;
564                                         }
565                                 sdata = data;
566                                 if (dsize > 0)
567                                         {
568                                         s->tlsext_ocsp_exts =
569                                                 d2i_X509_EXTENSIONS(NULL,
570                                                         &sdata, dsize);
571                                         if (!s->tlsext_ocsp_exts
572                                                 || (data + dsize != sdata))
573                                                 {
574                                                 *al = SSL_AD_DECODE_ERROR;
575                                                 return 0;
576                                                 }
577                                         }
578                                 }
579                                 /* We don't know what to do with any other type
580                                 * so ignore it.
581                                 */
582                                 else
583                                         s->tlsext_status_type = -1;
584                         }
585
586                 /* session ticket processed earlier */
587
588                 data+=size;             
589                 }
590
591         if (s->new_session && !renegotiate_seen
592                 && !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
593                 {
594                 *al = SSL_AD_ILLEGAL_PARAMETER; /* is this the right alert? */
595                 return 0;
596                 }
597
598         *p = data;
599         return 1;
600         }
601
602 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
603         {
604         unsigned short type;
605         unsigned short size;
606         unsigned short len;  
607         unsigned char *data = *p;
608         int tlsext_servername = 0;
609         int renegotiate_seen = 0;
610
611         if (data >= (d+n-2))
612                 {
613                 /* Because the client does not see any renegotiation during an
614                    attack, we must enforce this on all server hellos, even the
615                    first */
616                 if (!(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
617                         {
618                         /* We should always see one extension: the renegotiate extension */
619                         *al = SSL_AD_ILLEGAL_PARAMETER; /* is this the right alert? */
620                         return 0;
621                         }
622                 return 1;
623                 }
624
625         n2s(data,len);
626
627         while(data <= (d+n-4))
628                 {
629                 n2s(data,type);
630                 n2s(data,size);
631
632                 if (data+size > (d+n))
633                         return 1;
634
635                 if (s->tlsext_debug_cb)
636                         s->tlsext_debug_cb(s, 1, type, data, size,
637                                                 s->tlsext_debug_arg);
638
639                 if (type == TLSEXT_TYPE_server_name)
640                         {
641                         if (s->tlsext_hostname == NULL || size > 0)
642                                 {
643                                 *al = TLS1_AD_UNRECOGNIZED_NAME;
644                                 return 0;
645                                 }
646                         tlsext_servername = 1;   
647                         }
648                 else if (type == TLSEXT_TYPE_session_ticket)
649                         {
650                         if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
651                                 || (size > 0))
652                                 {
653                                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
654                                 return 0;
655                                 }
656                         s->tlsext_ticket_expected = 1;
657                         }
658                 else if (type == TLSEXT_TYPE_status_request)
659                         {
660                         /* MUST be empty and only sent if we've requested
661                          * a status request message.
662                          */ 
663                         if ((s->tlsext_status_type == -1) || (size > 0))
664                                 {
665                                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
666                                 return 0;
667                                 }
668                         /* Set flag to expect CertificateStatus message */
669                         s->tlsext_status_expected = 1;
670                         }
671                 else if (type == TLSEXT_TYPE_renegotiate)
672                         {
673                         if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
674                                 return 0;
675                         renegotiate_seen = 1;
676                         }
677                 data+=size;             
678                 }
679
680         if (data != d+n)
681                 {
682                 *al = SSL_AD_DECODE_ERROR;
683                 return 0;
684                 }
685
686         if (!renegotiate_seen
687                 && !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
688                 {
689                 *al = SSL_AD_ILLEGAL_PARAMETER; /* is this the right alert? */
690                 return 0;
691                 }
692
693         if (!s->hit && tlsext_servername == 1)
694                 {
695                 if (s->tlsext_hostname)
696                         {
697                         if (s->session->tlsext_hostname == NULL)
698                                 {
699                                 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);   
700                                 if (!s->session->tlsext_hostname)
701                                         {
702                                         *al = SSL_AD_UNRECOGNIZED_NAME;
703                                         return 0;
704                                         }
705                                 }
706                         else 
707                                 {
708                                 *al = SSL_AD_DECODE_ERROR;
709                                 return 0;
710                                 }
711                         }
712                 }
713
714         *p = data;
715         return 1;
716         }
717
718 int ssl_check_clienthello_tlsext(SSL *s)
719         {
720         int ret=SSL_TLSEXT_ERR_NOACK;
721         int al = SSL_AD_UNRECOGNIZED_NAME;
722
723         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
724                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
725         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
726                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
727
728         /* If status request then ask callback what to do.
729          * Note: this must be called after servername callbacks in case 
730          * the certificate has changed.
731          */
732         if ((s->tlsext_status_type != -1) && s->ctx->tlsext_status_cb)
733                 {
734                 int r;
735                 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
736                 switch (r)
737                         {
738                         /* We don't want to send a status request response */
739                         case SSL_TLSEXT_ERR_NOACK:
740                                 s->tlsext_status_expected = 0;
741                                 break;
742                         /* status request response should be sent */
743                         case SSL_TLSEXT_ERR_OK:
744                                 if (s->tlsext_ocsp_resp)
745                                         s->tlsext_status_expected = 1;
746                                 else
747                                         s->tlsext_status_expected = 0;
748                                 break;
749                         /* something bad happened */
750                         case SSL_TLSEXT_ERR_ALERT_FATAL:
751                                 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
752                                 al = SSL_AD_INTERNAL_ERROR;
753                                 goto err;
754                         }
755                 }
756         else
757                 s->tlsext_status_expected = 0;
758         err:
759         switch (ret)
760                 {
761                 case SSL_TLSEXT_ERR_ALERT_FATAL:
762                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
763                         return -1;
764
765                 case SSL_TLSEXT_ERR_ALERT_WARNING:
766                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
767                         return 1; 
768                                         
769                 case SSL_TLSEXT_ERR_NOACK:
770                         s->servername_done=0;
771                         default:
772                 return 1;
773                 }
774         }
775
776 int ssl_check_serverhello_tlsext(SSL *s)
777         {
778         int ret=SSL_TLSEXT_ERR_NOACK;
779         int al = SSL_AD_UNRECOGNIZED_NAME;
780
781         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
782                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
783         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
784                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
785
786         /* If we've requested certificate status and we wont get one
787          * tell the callback
788          */
789         if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
790                         && s->ctx->tlsext_status_cb)
791                 {
792                 int r;
793                 /* Set resp to NULL, resplen to -1 so callback knows
794                  * there is no response.
795                  */
796                 if (s->tlsext_ocsp_resp)
797                         {
798                         OPENSSL_free(s->tlsext_ocsp_resp);
799                         s->tlsext_ocsp_resp = NULL;
800                         }
801                 s->tlsext_ocsp_resplen = -1;
802                 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
803                 if (r == 0)
804                         {
805                         al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
806                         ret = SSL_TLSEXT_ERR_ALERT_FATAL;
807                         }
808                 if (r < 0)
809                         {
810                         al = SSL_AD_INTERNAL_ERROR;
811                         ret = SSL_TLSEXT_ERR_ALERT_FATAL;
812                         }
813                 }
814
815         switch (ret)
816                 {
817                 case SSL_TLSEXT_ERR_ALERT_FATAL:
818                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
819                         return -1;
820
821                 case SSL_TLSEXT_ERR_ALERT_WARNING:
822                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
823                         return 1; 
824                                         
825                 case SSL_TLSEXT_ERR_NOACK:
826                         s->servername_done=0;
827                         default:
828                 return 1;
829                 }
830         }
831
832 /* Since the server cache lookup is done early on in the processing of client
833  * hello and other operations depend on the result we need to handle any TLS
834  * session ticket extension at the same time.
835  */
836
837 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
838                                 const unsigned char *limit, SSL_SESSION **ret)
839         {
840         /* Point after session ID in client hello */
841         const unsigned char *p = session_id + len;
842         unsigned short i;
843
844         /* If tickets disabled behave as if no ticket present
845          * to permit stateful resumption.
846          */
847         if (SSL_get_options(s) & SSL_OP_NO_TICKET)
848                 return 1;
849
850         if ((s->version <= SSL3_VERSION) || !limit)
851                 return 1;
852         if (p >= limit)
853                 return -1;
854         /* Skip past DTLS cookie */
855         if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
856                 {
857                 i = *(p++);
858                 p+= i;
859                 if (p >= limit)
860                         return -1;
861                 }
862         /* Skip past cipher list */
863         n2s(p, i);
864         p+= i;
865         if (p >= limit)
866                 return -1;
867         /* Skip past compression algorithm list */
868         i = *(p++);
869         p += i;
870         if (p > limit)
871                 return -1;
872         /* Now at start of extensions */
873         if ((p + 2) >= limit)
874                 return 1;
875         n2s(p, i);
876         while ((p + 4) <= limit)
877                 {
878                 unsigned short type, size;
879                 n2s(p, type);
880                 n2s(p, size);
881                 if (p + size > limit)
882                         return 1;
883                 if (type == TLSEXT_TYPE_session_ticket)
884                         {
885                         /* If zero length note client will accept a ticket
886                          * and indicate cache miss to trigger full handshake
887                          */
888                         if (size == 0)
889                                 {
890                                 s->tlsext_ticket_expected = 1;
891                                 return 0;       /* Cache miss */
892                                 }
893                         return tls_decrypt_ticket(s, p, size, session_id, len,
894                                                                         ret);
895                         }
896                 p += size;
897                 }
898         return 1;
899         }
900
901 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
902                                 const unsigned char *sess_id, int sesslen,
903                                 SSL_SESSION **psess)
904         {
905         SSL_SESSION *sess;
906         unsigned char *sdec;
907         const unsigned char *p;
908         int slen, mlen, renew_ticket = 0;
909         unsigned char tick_hmac[EVP_MAX_MD_SIZE];
910         HMAC_CTX hctx;
911         EVP_CIPHER_CTX ctx;
912         SSL_CTX *tctx = s->initial_ctx;
913         /* Need at least keyname + iv + some encrypted data */
914         if (eticklen < 48)
915                 goto tickerr;
916         /* Initialize session ticket encryption and HMAC contexts */
917         HMAC_CTX_init(&hctx);
918         EVP_CIPHER_CTX_init(&ctx);
919         if (tctx->tlsext_ticket_key_cb)
920                 {
921                 unsigned char *nctick = (unsigned char *)etick;
922                 int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
923                                                         &ctx, &hctx, 0);
924                 if (rv < 0)
925                         return -1;
926                 if (rv == 0)
927                         goto tickerr;
928                 if (rv == 2)
929                         renew_ticket = 1;
930                 }
931         else
932                 {
933                 /* Check key name matches */
934                 if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
935                         goto tickerr;
936                 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
937                                         tlsext_tick_md(), NULL);
938                 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
939                                 tctx->tlsext_tick_aes_key, etick + 16);
940                 }
941         /* Attempt to process session ticket, first conduct sanity and
942          * integrity checks on ticket.
943          */
944         mlen = HMAC_size(&hctx);
945         eticklen -= mlen;
946         /* Check HMAC of encrypted ticket */
947         HMAC_Update(&hctx, etick, eticklen);
948         HMAC_Final(&hctx, tick_hmac, NULL);
949         HMAC_CTX_cleanup(&hctx);
950         if (memcmp(tick_hmac, etick + eticklen, mlen))
951                 goto tickerr;
952         /* Attempt to decrypt session data */
953         /* Move p after IV to start of encrypted ticket, update length */
954         p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
955         eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
956         sdec = OPENSSL_malloc(eticklen);
957         if (!sdec)
958                 {
959                 EVP_CIPHER_CTX_cleanup(&ctx);
960                 return -1;
961                 }
962         EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
963         if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
964                 goto tickerr;
965         slen += mlen;
966         EVP_CIPHER_CTX_cleanup(&ctx);
967         p = sdec;
968                 
969         sess = d2i_SSL_SESSION(NULL, &p, slen);
970         OPENSSL_free(sdec);
971         if (sess)
972                 {
973                 /* The session ID if non-empty is used by some clients to
974                  * detect that the ticket has been accepted. So we copy it to
975                  * the session structure. If it is empty set length to zero
976                  * as required by standard.
977                  */
978                 if (sesslen)
979                         memcpy(sess->session_id, sess_id, sesslen);
980                 sess->session_id_length = sesslen;
981                 *psess = sess;
982                 s->tlsext_ticket_expected = renew_ticket;
983                 return 1;
984                 }
985         /* If session decrypt failure indicate a cache miss and set state to
986          * send a new ticket
987          */
988         tickerr:        
989         s->tlsext_ticket_expected = 1;
990         return 0;
991         }
992
993 #endif