PR: 2028
[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         if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
178                 {
179                 int ticklen;
180                 if (s->session && s->session->tlsext_tick)
181                         ticklen = s->session->tlsext_ticklen;
182                 else
183                         ticklen = 0;
184                 /* Check for enough room 2 for extension type, 2 for len
185                  * rest for ticket
186                  */
187                 if (limit - ret - 4 - ticklen < 0)
188                         return NULL;
189                 s2n(TLSEXT_TYPE_session_ticket,ret); 
190                 s2n(ticklen,ret);
191                 if (ticklen)
192                         {
193                         memcpy(ret, s->session->tlsext_tick, ticklen);
194                         ret += ticklen;
195                         }
196                 }
197
198         if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
199                 {
200                 int i;
201                 long extlen, idlen, itmp;
202                 OCSP_RESPID *id;
203
204                 idlen = 0;
205                 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
206                         {
207                         id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
208                         itmp = i2d_OCSP_RESPID(id, NULL);
209                         if (itmp <= 0)
210                                 return NULL;
211                         idlen += itmp + 2;
212                         }
213
214                 if (s->tlsext_ocsp_exts)
215                         {
216                         extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
217                         if (extlen < 0)
218                                 return NULL;
219                         }
220                 else
221                         extlen = 0;
222                         
223                 if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL;
224                 s2n(TLSEXT_TYPE_status_request, ret);
225                 if (extlen + idlen > 0xFFF0)
226                         return NULL;
227                 s2n(extlen + idlen + 5, ret);
228                 *(ret++) = TLSEXT_STATUSTYPE_ocsp;
229                 s2n(idlen, ret);
230                 for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++)
231                         {
232                         /* save position of id len */
233                         unsigned char *q = ret;
234                         id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
235                         /* skip over id len */
236                         ret += 2;
237                         itmp = i2d_OCSP_RESPID(id, &ret);
238                         /* write id len */
239                         s2n(itmp, q);
240                         }
241                 s2n(extlen, ret);
242                 if (extlen > 0)
243                         i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
244                 }
245
246         if ((extdatalen = ret-p-2)== 0) 
247                 return p;
248
249         s2n(extdatalen,p);
250         return ret;
251         }
252
253 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
254         {
255         int extdatalen=0;
256         unsigned char *ret = p;
257
258         /* don't add extensions for SSLv3 */
259         if (s->version == SSL3_VERSION)
260                 return p;
261         
262         ret+=2;
263         if (ret>=limit) return NULL; /* this really never occurs, but ... */
264
265         if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
266                 { 
267                 if (limit - ret - 4 < 0) return NULL; 
268
269                 s2n(TLSEXT_TYPE_server_name,ret);
270                 s2n(0,ret);
271                 }
272         
273         if (s->tlsext_ticket_expected
274                 && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) 
275                 { 
276                 if (limit - ret - 4 < 0) return NULL; 
277                 s2n(TLSEXT_TYPE_session_ticket,ret);
278                 s2n(0,ret);
279                 }
280
281         if (s->tlsext_status_expected)
282                 { 
283                 if ((long)(limit - ret - 4) < 0) return NULL; 
284                 s2n(TLSEXT_TYPE_status_request,ret);
285                 s2n(0,ret);
286                 }
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         s->tlsext_status_type = -1;
303
304         if (data >= (d+n-2))
305                 return 1;
306         n2s(data,len);
307
308         if (data > (d+n-len)) 
309                 return 1;
310
311         while (data <= (d+n-4))
312                 {
313                 n2s(data,type);
314                 n2s(data,size);
315
316                 if (data+size > (d+n))
317                         return 1;
318
319                 if (s->tlsext_debug_cb)
320                         s->tlsext_debug_cb(s, 0, type, data, size,
321                                                 s->tlsext_debug_arg);
322 /* The servername extension is treated as follows:
323
324    - Only the hostname type is supported with a maximum length of 255.
325    - The servername is rejected if too long or if it contains zeros,
326      in which case an fatal alert is generated.
327    - The servername field is maintained together with the session cache.
328    - When a session is resumed, the servername call back invoked in order
329      to allow the application to position itself to the right context. 
330    - The servername is acknowledged if it is new for a session or when 
331      it is identical to a previously used for the same session. 
332      Applications can control the behaviour.  They can at any time
333      set a 'desirable' servername for a new SSL object. This can be the
334      case for example with HTTPS when a Host: header field is received and
335      a renegotiation is requested. In this case, a possible servername
336      presented in the new client hello is only acknowledged if it matches
337      the value of the Host: field. 
338    - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
339      if they provide for changing an explicit servername context for the session,
340      i.e. when the session has been established with a servername extension. 
341    - On session reconnect, the servername extension may be absent. 
342
343 */      
344
345                 if (type == TLSEXT_TYPE_server_name)
346                         {
347                         unsigned char *sdata;
348                         int servname_type;
349                         int dsize; 
350                 
351                         if (size < 2) 
352                                 {
353                                 *al = SSL_AD_DECODE_ERROR;
354                                 return 0;
355                                 }
356                         n2s(data,dsize);  
357                         size -= 2;
358                         if (dsize > size  ) 
359                                 {
360                                 *al = SSL_AD_DECODE_ERROR;
361                                 return 0;
362                                 } 
363
364                         sdata = data;
365                         while (dsize > 3) 
366                                 {
367                                 servname_type = *(sdata++); 
368                                 n2s(sdata,len);
369                                 dsize -= 3;
370
371                                 if (len > dsize) 
372                                         {
373                                         *al = SSL_AD_DECODE_ERROR;
374                                         return 0;
375                                         }
376                                 if (s->servername_done == 0)
377                                 switch (servname_type)
378                                         {
379                                 case TLSEXT_NAMETYPE_host_name:
380                                         if (s->session->tlsext_hostname == NULL)
381                                                 {
382                                                 if (len > TLSEXT_MAXLEN_host_name || 
383                                                         ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
384                                                         {
385                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
386                                                         return 0;
387                                                         }
388                                                 memcpy(s->session->tlsext_hostname, sdata, len);
389                                                 s->session->tlsext_hostname[len]='\0';
390                                                 if (strlen(s->session->tlsext_hostname) != len) {
391                                                         OPENSSL_free(s->session->tlsext_hostname);
392                                                         s->session->tlsext_hostname = NULL;
393                                                         *al = TLS1_AD_UNRECOGNIZED_NAME;
394                                                         return 0;
395                                                 }
396                                                 s->servername_done = 1; 
397
398                                                 }
399                                         else 
400                                                 s->servername_done = strlen(s->session->tlsext_hostname) == len 
401                                                         && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
402                                         
403                                         break;
404
405                                 default:
406                                         break;
407                                         }
408                                  
409                                 dsize -= len;
410                                 }
411                         if (dsize != 0) 
412                                 {
413                                 *al = SSL_AD_DECODE_ERROR;
414                                 return 0;
415                                 }
416
417                         }
418                 else if (type == TLSEXT_TYPE_status_request
419                                                 && s->ctx->tlsext_status_cb)
420                         {
421                 
422                         if (size < 5) 
423                                 {
424                                 *al = SSL_AD_DECODE_ERROR;
425                                 return 0;
426                                 }
427
428                         s->tlsext_status_type = *data++;
429                         size--;
430                         if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp)
431                                 {
432                                 const unsigned char *sdata;
433                                 int dsize;
434                                 /* Read in responder_id_list */
435                                 n2s(data,dsize);
436                                 size -= 2;
437                                 if (dsize > size  ) 
438                                         {
439                                         *al = SSL_AD_DECODE_ERROR;
440                                         return 0;
441                                         }
442                                 while (dsize > 0)
443                                         {
444                                         OCSP_RESPID *id;
445                                         int idsize;
446                                         if (dsize < 4)
447                                                 {
448                                                 *al = SSL_AD_DECODE_ERROR;
449                                                 return 0;
450                                                 }
451                                         n2s(data, idsize);
452                                         dsize -= 2 + idsize;
453                                         if (dsize < 0)
454                                                 {
455                                                 *al = SSL_AD_DECODE_ERROR;
456                                                 return 0;
457                                                 }
458                                         sdata = data;
459                                         data += idsize;
460                                         id = d2i_OCSP_RESPID(NULL,
461                                                                 &sdata, idsize);
462                                         if (!id)
463                                                 {
464                                                 *al = SSL_AD_DECODE_ERROR;
465                                                 return 0;
466                                                 }
467                                         if (data != sdata)
468                                                 {
469                                                 OCSP_RESPID_free(id);
470                                                 *al = SSL_AD_DECODE_ERROR;
471                                                 return 0;
472                                                 }
473                                         if (!s->tlsext_ocsp_ids
474                                                 && !(s->tlsext_ocsp_ids =
475                                                 sk_OCSP_RESPID_new_null()))
476                                                 {
477                                                 OCSP_RESPID_free(id);
478                                                 *al = SSL_AD_INTERNAL_ERROR;
479                                                 return 0;
480                                                 }
481                                         if (!sk_OCSP_RESPID_push(
482                                                         s->tlsext_ocsp_ids, id))
483                                                 {
484                                                 OCSP_RESPID_free(id);
485                                                 *al = SSL_AD_INTERNAL_ERROR;
486                                                 return 0;
487                                                 }
488                                         }
489
490                                 /* Read in request_extensions */
491                                 n2s(data,dsize);
492                                 size -= 2;
493                                 if (dsize > size) 
494                                         {
495                                         *al = SSL_AD_DECODE_ERROR;
496                                         return 0;
497                                         }
498                                 sdata = data;
499                                 if (dsize > 0)
500                                         {
501                                         s->tlsext_ocsp_exts =
502                                                 d2i_X509_EXTENSIONS(NULL,
503                                                         &sdata, dsize);
504                                         if (!s->tlsext_ocsp_exts
505                                                 || (data + dsize != sdata))
506                                                 {
507                                                 *al = SSL_AD_DECODE_ERROR;
508                                                 return 0;
509                                                 }
510                                         }
511                                 }
512                                 /* We don't know what to do with any other type
513                                 * so ignore it.
514                                 */
515                                 else
516                                         s->tlsext_status_type = -1;
517                         }
518                 /* session ticket processed earlier */
519
520                 data+=size;             
521                 }
522
523         *p = data;
524         return 1;
525         }
526
527 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
528         {
529         unsigned short type;
530         unsigned short size;
531         unsigned short len;  
532         unsigned char *data = *p;
533
534         int tlsext_servername = 0;
535
536         if (data >= (d+n-2))
537                 return 1;
538
539         n2s(data,len);
540
541         while(data <= (d+n-4))
542                 {
543                 n2s(data,type);
544                 n2s(data,size);
545
546                 if (data+size > (d+n))
547                         return 1;
548
549                 if (s->tlsext_debug_cb)
550                         s->tlsext_debug_cb(s, 1, type, data, size,
551                                                 s->tlsext_debug_arg);
552
553                 if (type == TLSEXT_TYPE_server_name)
554                         {
555                         if (s->tlsext_hostname == NULL || size > 0)
556                                 {
557                                 *al = TLS1_AD_UNRECOGNIZED_NAME;
558                                 return 0;
559                                 }
560                         tlsext_servername = 1;   
561                         }
562                 else if (type == TLSEXT_TYPE_session_ticket)
563                         {
564                         if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
565                                 || (size > 0))
566                                 {
567                                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
568                                 return 0;
569                                 }
570                         s->tlsext_ticket_expected = 1;
571                         }
572                 else if (type == TLSEXT_TYPE_status_request)
573                         {
574                         /* MUST be empty and only sent if we've requested
575                          * a status request message.
576                          */ 
577                         if ((s->tlsext_status_type == -1) || (size > 0))
578                                 {
579                                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
580                                 return 0;
581                                 }
582                         /* Set flag to expect CertificateStatus message */
583                         s->tlsext_status_expected = 1;
584                         }
585
586                 data+=size;             
587                 }
588
589         if (data != d+n)
590                 {
591                 *al = SSL_AD_DECODE_ERROR;
592                 return 0;
593                 }
594
595         if (!s->hit && tlsext_servername == 1)
596                 {
597                 if (s->tlsext_hostname)
598                         {
599                         if (s->session->tlsext_hostname == NULL)
600                                 {
601                                 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);   
602                                 if (!s->session->tlsext_hostname)
603                                         {
604                                         *al = SSL_AD_UNRECOGNIZED_NAME;
605                                         return 0;
606                                         }
607                                 }
608                         else 
609                                 {
610                                 *al = SSL_AD_DECODE_ERROR;
611                                 return 0;
612                                 }
613                         }
614                 }
615
616         *p = data;
617         return 1;
618         }
619
620 int ssl_check_clienthello_tlsext(SSL *s)
621         {
622         int ret=SSL_TLSEXT_ERR_NOACK;
623         int al = SSL_AD_UNRECOGNIZED_NAME;
624
625         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
626                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
627         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
628                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
629
630         /* If status request then ask callback what to do.
631          * Note: this must be called after servername callbacks in case 
632          * the certificate has changed.
633          */
634         if ((s->tlsext_status_type != -1) && s->ctx->tlsext_status_cb)
635                 {
636                 int r;
637                 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
638                 switch (r)
639                         {
640                         /* We don't want to send a status request response */
641                         case SSL_TLSEXT_ERR_NOACK:
642                                 s->tlsext_status_expected = 0;
643                                 break;
644                         /* status request response should be sent */
645                         case SSL_TLSEXT_ERR_OK:
646                                 if (s->tlsext_ocsp_resp)
647                                         s->tlsext_status_expected = 1;
648                                 else
649                                         s->tlsext_status_expected = 0;
650                                 break;
651                         /* something bad happened */
652                         case SSL_TLSEXT_ERR_ALERT_FATAL:
653                                 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
654                                 al = SSL_AD_INTERNAL_ERROR;
655                                 goto err;
656                         }
657                 }
658         else
659                 s->tlsext_status_expected = 0;
660         err:
661         switch (ret)
662                 {
663                 case SSL_TLSEXT_ERR_ALERT_FATAL:
664                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
665                         return -1;
666
667                 case SSL_TLSEXT_ERR_ALERT_WARNING:
668                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
669                         return 1; 
670                                         
671                 case SSL_TLSEXT_ERR_NOACK:
672                         s->servername_done=0;
673                         default:
674                 return 1;
675                 }
676         }
677
678 int ssl_check_serverhello_tlsext(SSL *s)
679         {
680         int ret=SSL_TLSEXT_ERR_NOACK;
681         int al = SSL_AD_UNRECOGNIZED_NAME;
682
683         if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) 
684                 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
685         else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)             
686                 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
687
688         /* If we've requested certificate status and we wont get one
689          * tell the callback
690          */
691         if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
692                         && s->ctx->tlsext_status_cb)
693                 {
694                 int r;
695                 /* Set resp to NULL, resplen to -1 so callback knows
696                  * there is no response.
697                  */
698                 if (s->tlsext_ocsp_resp)
699                         {
700                         OPENSSL_free(s->tlsext_ocsp_resp);
701                         s->tlsext_ocsp_resp = NULL;
702                         }
703                 s->tlsext_ocsp_resplen = -1;
704                 r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
705                 if (r == 0)
706                         {
707                         al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
708                         ret = SSL_TLSEXT_ERR_ALERT_FATAL;
709                         }
710                 if (r < 0)
711                         {
712                         al = SSL_AD_INTERNAL_ERROR;
713                         ret = SSL_TLSEXT_ERR_ALERT_FATAL;
714                         }
715                 }
716
717         switch (ret)
718                 {
719                 case SSL_TLSEXT_ERR_ALERT_FATAL:
720                         ssl3_send_alert(s,SSL3_AL_FATAL,al); 
721                         return -1;
722
723                 case SSL_TLSEXT_ERR_ALERT_WARNING:
724                         ssl3_send_alert(s,SSL3_AL_WARNING,al);
725                         return 1; 
726                                         
727                 case SSL_TLSEXT_ERR_NOACK:
728                         s->servername_done=0;
729                         default:
730                 return 1;
731                 }
732         }
733
734 /* Since the server cache lookup is done early on in the processing of client
735  * hello and other operations depend on the result we need to handle any TLS
736  * session ticket extension at the same time.
737  */
738
739 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
740                                 const unsigned char *limit, SSL_SESSION **ret)
741         {
742         /* Point after session ID in client hello */
743         const unsigned char *p = session_id + len;
744         unsigned short i;
745
746         /* If tickets disabled behave as if no ticket present
747          * to permit stateful resumption.
748          */
749         if (SSL_get_options(s) & SSL_OP_NO_TICKET)
750                 return 1;
751
752         if ((s->version <= SSL3_VERSION) || !limit)
753                 return 1;
754         if (p >= limit)
755                 return -1;
756         /* Skip past DTLS cookie */
757         if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
758                 {
759                 i = *(p++);
760                 p+= i;
761                 if (p >= limit)
762                         return -1;
763                 }
764         /* Skip past cipher list */
765         n2s(p, i);
766         p+= i;
767         if (p >= limit)
768                 return -1;
769         /* Skip past compression algorithm list */
770         i = *(p++);
771         p += i;
772         if (p > limit)
773                 return -1;
774         /* Now at start of extensions */
775         if ((p + 2) >= limit)
776                 return 1;
777         n2s(p, i);
778         while ((p + 4) <= limit)
779                 {
780                 unsigned short type, size;
781                 n2s(p, type);
782                 n2s(p, size);
783                 if (p + size > limit)
784                         return 1;
785                 if (type == TLSEXT_TYPE_session_ticket)
786                         {
787                         /* If zero length note client will accept a ticket
788                          * and indicate cache miss to trigger full handshake
789                          */
790                         if (size == 0)
791                                 {
792                                 s->tlsext_ticket_expected = 1;
793                                 return 0;       /* Cache miss */
794                                 }
795                         return tls_decrypt_ticket(s, p, size, session_id, len,
796                                                                         ret);
797                         }
798                 p += size;
799                 }
800         return 1;
801         }
802
803 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
804                                 const unsigned char *sess_id, int sesslen,
805                                 SSL_SESSION **psess)
806         {
807         SSL_SESSION *sess;
808         unsigned char *sdec;
809         const unsigned char *p;
810         int slen, mlen, renew_ticket = 0;
811         unsigned char tick_hmac[EVP_MAX_MD_SIZE];
812         HMAC_CTX hctx;
813         EVP_CIPHER_CTX ctx;
814         /* Need at least keyname + iv + some encrypted data */
815         if (eticklen < 48)
816                 goto tickerr;
817         /* Initialize session ticket encryption and HMAC contexts */
818         HMAC_CTX_init(&hctx);
819         EVP_CIPHER_CTX_init(&ctx);
820         if (s->ctx->tlsext_ticket_key_cb)
821                 {
822                 unsigned char *nctick = (unsigned char *)etick;
823                 int rv = s->ctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
824                                                         &ctx, &hctx, 0);
825                 if (rv < 0)
826                         return -1;
827                 if (rv == 0)
828                         goto tickerr;
829                 if (rv == 2)
830                         renew_ticket = 1;
831                 }
832         else
833                 {
834                 /* Check key name matches */
835                 if (memcmp(etick, s->ctx->tlsext_tick_key_name, 16))
836                         goto tickerr;
837                 HMAC_Init_ex(&hctx, s->ctx->tlsext_tick_hmac_key, 16,
838                                         tlsext_tick_md(), NULL);
839                 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
840                                 s->ctx->tlsext_tick_aes_key, etick + 16);
841                 }
842         /* Attempt to process session ticket, first conduct sanity and
843          * integrity checks on ticket.
844          */
845         mlen = HMAC_size(&hctx);
846         eticklen -= mlen;
847         /* Check HMAC of encrypted ticket */
848         HMAC_Update(&hctx, etick, eticklen);
849         HMAC_Final(&hctx, tick_hmac, NULL);
850         HMAC_CTX_cleanup(&hctx);
851         if (memcmp(tick_hmac, etick + eticklen, mlen))
852                 goto tickerr;
853         /* Attempt to decrypt session data */
854         /* Move p after IV to start of encrypted ticket, update length */
855         p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
856         eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
857         sdec = OPENSSL_malloc(eticklen);
858         if (!sdec)
859                 {
860                 EVP_CIPHER_CTX_cleanup(&ctx);
861                 return -1;
862                 }
863         EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
864         if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
865                 goto tickerr;
866         slen += mlen;
867         EVP_CIPHER_CTX_cleanup(&ctx);
868         p = sdec;
869                 
870         sess = d2i_SSL_SESSION(NULL, &p, slen);
871         OPENSSL_free(sdec);
872         if (sess)
873                 {
874                 /* The session ID if non-empty is used by some clients to
875                  * detect that the ticket has been accepted. So we copy it to
876                  * the session structure. If it is empty set length to zero
877                  * as required by standard.
878                  */
879                 if (sesslen)
880                         memcpy(sess->session_id, sess_id, sesslen);
881                 sess->session_id_length = sesslen;
882                 *psess = sess;
883                 s->tlsext_ticket_expected = renew_ticket;
884                 return 1;
885                 }
886         /* If session decrypt failure indicate a cache miss and set state to
887          * send a new ticket
888          */
889         tickerr:        
890         s->tlsext_ticket_expected = 1;
891         return 0;
892         }
893
894 #endif