]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/t1_lib.c
Implement the Opaque PRF Input TLS extension
[thirdparty/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-2007 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 <openssl/evp.h>
115 #include <openssl/hmac.h>
116 #include "ssl_locl.h"
117
118 const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT;
119
120 #ifndef OPENSSL_NO_TLSEXT
121 static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
122 const unsigned char *sess_id, int sesslen,
123 SSL_SESSION **psess);
124 #endif
125
126 SSL3_ENC_METHOD TLSv1_enc_data={
127 tls1_enc,
128 tls1_mac,
129 tls1_setup_key_block,
130 tls1_generate_master_secret,
131 tls1_change_cipher_state,
132 tls1_final_finish_mac,
133 TLS1_FINISH_MAC_LENGTH,
134 tls1_cert_verify_mac,
135 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
136 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
137 tls1_alert_code,
138 };
139
140 long tls1_default_timeout(void)
141 {
142 /* 2 hours, the 24 hours mentioned in the TLSv1 spec
143 * is way too long for http, the cache would over fill */
144 return(60*60*2);
145 }
146
147 int tls1_new(SSL *s)
148 {
149 if (!ssl3_new(s)) return(0);
150 s->method->ssl_clear(s);
151 return(1);
152 }
153
154 void tls1_free(SSL *s)
155 {
156 ssl3_free(s);
157 }
158
159 void tls1_clear(SSL *s)
160 {
161 ssl3_clear(s);
162 s->version=TLS1_VERSION;
163 }
164
165 #ifndef OPENSSL_NO_EC
166 static int nid_list[] =
167 {
168 NID_sect163k1, /* sect163k1 (1) */
169 NID_sect163r1, /* sect163r1 (2) */
170 NID_sect163r2, /* sect163r2 (3) */
171 NID_sect193r1, /* sect193r1 (4) */
172 NID_sect193r2, /* sect193r2 (5) */
173 NID_sect233k1, /* sect233k1 (6) */
174 NID_sect233r1, /* sect233r1 (7) */
175 NID_sect239k1, /* sect239k1 (8) */
176 NID_sect283k1, /* sect283k1 (9) */
177 NID_sect283r1, /* sect283r1 (10) */
178 NID_sect409k1, /* sect409k1 (11) */
179 NID_sect409r1, /* sect409r1 (12) */
180 NID_sect571k1, /* sect571k1 (13) */
181 NID_sect571r1, /* sect571r1 (14) */
182 NID_secp160k1, /* secp160k1 (15) */
183 NID_secp160r1, /* secp160r1 (16) */
184 NID_secp160r2, /* secp160r2 (17) */
185 NID_secp192k1, /* secp192k1 (18) */
186 NID_X9_62_prime192v1, /* secp192r1 (19) */
187 NID_secp224k1, /* secp224k1 (20) */
188 NID_secp224r1, /* secp224r1 (21) */
189 NID_secp256k1, /* secp256k1 (22) */
190 NID_X9_62_prime256v1, /* secp256r1 (23) */
191 NID_secp384r1, /* secp384r1 (24) */
192 NID_secp521r1 /* secp521r1 (25) */
193 };
194
195 int tls1_ec_curve_id2nid(int curve_id)
196 {
197 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
198 if ((curve_id < 1) || (curve_id > sizeof(nid_list)/sizeof(nid_list[0]))) return 0;
199 return nid_list[curve_id-1];
200 }
201
202 int tls1_ec_nid2curve_id(int nid)
203 {
204 /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */
205 switch (nid)
206 {
207 case NID_sect163k1: /* sect163k1 (1) */
208 return 1;
209 case NID_sect163r1: /* sect163r1 (2) */
210 return 2;
211 case NID_sect163r2: /* sect163r2 (3) */
212 return 3;
213 case NID_sect193r1: /* sect193r1 (4) */
214 return 4;
215 case NID_sect193r2: /* sect193r2 (5) */
216 return 5;
217 case NID_sect233k1: /* sect233k1 (6) */
218 return 6;
219 case NID_sect233r1: /* sect233r1 (7) */
220 return 7;
221 case NID_sect239k1: /* sect239k1 (8) */
222 return 8;
223 case NID_sect283k1: /* sect283k1 (9) */
224 return 9;
225 case NID_sect283r1: /* sect283r1 (10) */
226 return 10;
227 case NID_sect409k1: /* sect409k1 (11) */
228 return 11;
229 case NID_sect409r1: /* sect409r1 (12) */
230 return 12;
231 case NID_sect571k1: /* sect571k1 (13) */
232 return 13;
233 case NID_sect571r1: /* sect571r1 (14) */
234 return 14;
235 case NID_secp160k1: /* secp160k1 (15) */
236 return 15;
237 case NID_secp160r1: /* secp160r1 (16) */
238 return 16;
239 case NID_secp160r2: /* secp160r2 (17) */
240 return 17;
241 case NID_secp192k1: /* secp192k1 (18) */
242 return 18;
243 case NID_X9_62_prime192v1: /* secp192r1 (19) */
244 return 19;
245 case NID_secp224k1: /* secp224k1 (20) */
246 return 20;
247 case NID_secp224r1: /* secp224r1 (21) */
248 return 21;
249 case NID_secp256k1: /* secp256k1 (22) */
250 return 22;
251 case NID_X9_62_prime256v1: /* secp256r1 (23) */
252 return 23;
253 case NID_secp384r1: /* secp384r1 (24) */
254 return 24;
255 case NID_secp521r1: /* secp521r1 (25) */
256 return 25;
257 default:
258 return 0;
259 }
260 }
261 #endif /* OPENSSL_NO_EC */
262
263 #ifndef OPENSSL_NO_TLSEXT
264 unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
265 {
266 int extdatalen=0;
267 unsigned char *ret = p;
268
269 ret+=2;
270
271 if (ret>=limit) return NULL; /* this really never occurs, but ... */
272
273 if (s->tlsext_hostname != NULL)
274 {
275 /* Add TLS extension servername to the Client Hello message */
276 unsigned long size_str;
277 long lenmax;
278
279 /* check for enough space.
280 4 for the servername type and entension length
281 2 for servernamelist length
282 1 for the hostname type
283 2 for hostname length
284 + hostname length
285 */
286
287 if ((lenmax = limit - ret - 9) < 0
288 || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
289 return NULL;
290
291 /* extension type and length */
292 s2n(TLSEXT_TYPE_server_name,ret);
293 s2n(size_str+5,ret);
294
295 /* length of servername list */
296 s2n(size_str+3,ret);
297
298 /* hostname type, length and hostname */
299 *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name;
300 s2n(size_str,ret);
301 memcpy(ret, s->tlsext_hostname, size_str);
302 ret+=size_str;
303 }
304
305 #ifndef OPENSSL_NO_EC
306 if (s->tlsext_ecpointformatlist != NULL)
307 {
308 /* Add TLS extension ECPointFormats to the ClientHello message */
309 long lenmax;
310
311 if ((lenmax = limit - ret - 5) < 0) return NULL;
312 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
313 if (s->tlsext_ecpointformatlist_length > 255)
314 {
315 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
316 return NULL;
317 }
318
319 s2n(TLSEXT_TYPE_ec_point_formats,ret);
320 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
321 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
322 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
323 ret+=s->tlsext_ecpointformatlist_length;
324 }
325 if (s->tlsext_ellipticcurvelist != NULL)
326 {
327 /* Add TLS extension EllipticCurves to the ClientHello message */
328 long lenmax;
329
330 if ((lenmax = limit - ret - 6) < 0) return NULL;
331 if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL;
332 if (s->tlsext_ellipticcurvelist_length > 65532)
333 {
334 SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
335 return NULL;
336 }
337
338 s2n(TLSEXT_TYPE_elliptic_curves,ret);
339 s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
340
341 /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for
342 * elliptic_curve_list, but the examples use two bytes.
343 * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html
344 * resolves this to two bytes.
345 */
346 s2n(s->tlsext_ellipticcurvelist_length, ret);
347 memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length);
348 ret+=s->tlsext_ellipticcurvelist_length;
349 }
350 #endif /* OPENSSL_NO_EC */
351
352 if (!(SSL_get_options(s) & SSL_OP_NO_TICKET))
353 {
354 int ticklen;
355 if (s->session && s->session->tlsext_tick)
356 ticklen = s->session->tlsext_ticklen;
357 else
358 ticklen = 0;
359 /* Check for enough room 2 for extension type, 2 for len
360 * rest for ticket
361 */
362 if ((long)(limit - ret - 4 - ticklen) < 0) return NULL;
363 s2n(TLSEXT_TYPE_session_ticket,ret);
364 s2n(ticklen,ret);
365 if (ticklen)
366 {
367 memcpy(ret, s->session->tlsext_tick, ticklen);
368 ret += ticklen;
369 }
370 }
371
372 #ifdef TLSEXT_TYPE_opaque_prf_input
373 if (s->s3->client_opaque_prf_input != NULL)
374 {
375 size_t col = s->s3->client_opaque_prf_input_len;
376
377 if ((long)(limit - ret - 6 - col < 0))
378 return NULL;
379 if (col > 0xFFFD) /* can't happen */
380 return NULL;
381
382 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
383 s2n(col + 2, ret);
384 s2n(col, ret);
385 memcpy(ret, s->s3->client_opaque_prf_input, col);
386 ret += col;
387 }
388 #endif
389
390 if ((extdatalen = ret-p-2)== 0)
391 return p;
392
393 s2n(extdatalen,p);
394 return ret;
395 }
396
397 unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit)
398 {
399 int extdatalen=0;
400 unsigned char *ret = p;
401
402 ret+=2;
403 if (ret>=limit) return NULL; /* this really never occurs, but ... */
404
405 if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL)
406 {
407 if ((long)(limit - ret - 4) < 0) return NULL;
408
409 s2n(TLSEXT_TYPE_server_name,ret);
410 s2n(0,ret);
411 }
412 #ifndef OPENSSL_NO_EC
413 if (s->tlsext_ecpointformatlist != NULL)
414 {
415 /* Add TLS extension ECPointFormats to the ServerHello message */
416 long lenmax;
417
418 if ((lenmax = limit - ret - 5) < 0) return NULL;
419 if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL;
420 if (s->tlsext_ecpointformatlist_length > 255)
421 {
422 SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
423 return NULL;
424 }
425
426 s2n(TLSEXT_TYPE_ec_point_formats,ret);
427 s2n(s->tlsext_ecpointformatlist_length + 1,ret);
428 *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length;
429 memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length);
430 ret+=s->tlsext_ecpointformatlist_length;
431
432 }
433 /* Currently the server should not respond with a SupportedCurves extension */
434 #endif /* OPENSSL_NO_EC */
435
436 if (s->tlsext_ticket_expected
437 && !(SSL_get_options(s) & SSL_OP_NO_TICKET))
438 {
439 if ((long)(limit - ret - 4) < 0) return NULL;
440 s2n(TLSEXT_TYPE_session_ticket,ret);
441 s2n(0,ret);
442 }
443
444 #ifdef TLSEXT_TYPE_opaque_prf_input
445 if (s->s3->server_opaque_prf_input != NULL)
446 {
447 size_t sol = s->s3->server_opaque_prf_input_len;
448
449 if ((long)(limit - ret - 6 - sol) < 0)
450 return NULL;
451 if (sol > 0xFFFD) /* can't happen */
452 return NULL;
453
454 s2n(TLSEXT_TYPE_opaque_prf_input, ret);
455 s2n(sol + 2, ret);
456 s2n(sol, ret);
457 memcpy(ret, s->s3->server_opaque_prf_input, sol);
458 ret += sol;
459 }
460 #endif
461
462 if ((extdatalen = ret-p-2)== 0)
463 return p;
464
465 s2n(extdatalen,p);
466 return ret;
467 }
468
469 int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
470 {
471 unsigned short type;
472 unsigned short size;
473 unsigned short len;
474 unsigned char *data = *p;
475 s->servername_done = 0;
476
477 if (data >= (d+n-2))
478 return 1;
479 n2s(data,len);
480
481 if (data > (d+n-len))
482 return 1;
483
484 while (data <= (d+n-4))
485 {
486 n2s(data,type);
487 n2s(data,size);
488
489 if (data+size > (d+n))
490 return 1;
491
492 if (s->tlsext_debug_cb)
493 s->tlsext_debug_cb(s, 0, type, data, size,
494 s->tlsext_debug_arg);
495 /* The servername extension is treated as follows:
496
497 - Only the hostname type is supported with a maximum length of 255.
498 - The servername is rejected if too long or if it contains zeros,
499 in which case an fatal alert is generated.
500 - The servername field is maintained together with the session cache.
501 - When a session is resumed, the servername call back invoked in order
502 to allow the application to position itself to the right context.
503 - The servername is acknowledged if it is new for a session or when
504 it is identical to a previously used for the same session.
505 Applications can control the behaviour. They can at any time
506 set a 'desirable' servername for a new SSL object. This can be the
507 case for example with HTTPS when a Host: header field is received and
508 a renegotiation is requested. In this case, a possible servername
509 presented in the new client hello is only acknowledged if it matches
510 the value of the Host: field.
511 - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
512 if they provide for changing an explicit servername context for the session,
513 i.e. when the session has been established with a servername extension.
514 - On session reconnect, the servername extension may be absent.
515
516 */
517
518 if (type == TLSEXT_TYPE_server_name)
519 {
520 unsigned char *sdata;
521 int servname_type;
522 int dsize;
523
524 if (size < 2)
525 {
526 *al = SSL_AD_DECODE_ERROR;
527 return 0;
528 }
529 n2s(data,dsize);
530 size -= 2;
531 if (dsize > size )
532 {
533 *al = SSL_AD_DECODE_ERROR;
534 return 0;
535 }
536
537 sdata = data;
538 while (dsize > 3)
539 {
540 servname_type = *(sdata++);
541 n2s(sdata,len);
542 dsize -= 3;
543
544 if (len > dsize)
545 {
546 *al = SSL_AD_DECODE_ERROR;
547 return 0;
548 }
549 if (s->servername_done == 0)
550 switch (servname_type)
551 {
552 case TLSEXT_NAMETYPE_host_name:
553 if (s->session->tlsext_hostname == NULL)
554 {
555 if (len > TLSEXT_MAXLEN_host_name ||
556 ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL))
557 {
558 *al = TLS1_AD_UNRECOGNIZED_NAME;
559 return 0;
560 }
561 memcpy(s->session->tlsext_hostname, sdata, len);
562 s->session->tlsext_hostname[len]='\0';
563 if (strlen(s->session->tlsext_hostname) != len) {
564 OPENSSL_free(s->session->tlsext_hostname);
565 *al = TLS1_AD_UNRECOGNIZED_NAME;
566 return 0;
567 }
568 s->servername_done = 1;
569
570 }
571 else
572 s->servername_done = strlen(s->session->tlsext_hostname) == len
573 && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0;
574
575 break;
576
577 default:
578 break;
579 }
580
581 dsize -= len;
582 }
583 if (dsize != 0)
584 {
585 *al = SSL_AD_DECODE_ERROR;
586 return 0;
587 }
588
589 }
590
591 #ifndef OPENSSL_NO_EC
592 else if (type == TLSEXT_TYPE_ec_point_formats)
593 {
594 unsigned char *sdata = data;
595 int ecpointformatlist_length = *(sdata++);
596
597 if (ecpointformatlist_length != size - 1)
598 {
599 *al = TLS1_AD_DECODE_ERROR;
600 return 0;
601 }
602 s->session->tlsext_ecpointformatlist_length = 0;
603 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
604 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
605 {
606 *al = TLS1_AD_INTERNAL_ERROR;
607 return 0;
608 }
609 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
610 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
611 #if 0
612 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length);
613 sdata = s->session->tlsext_ecpointformatlist;
614 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
615 fprintf(stderr,"%i ",*(sdata++));
616 fprintf(stderr,"\n");
617 #endif
618 }
619 else if (type == TLSEXT_TYPE_elliptic_curves)
620 {
621 unsigned char *sdata = data;
622 int ellipticcurvelist_length = (*(sdata++) << 8);
623 ellipticcurvelist_length += (*(sdata++));
624
625 if (ellipticcurvelist_length != size - 2)
626 {
627 *al = TLS1_AD_DECODE_ERROR;
628 return 0;
629 }
630 s->session->tlsext_ellipticcurvelist_length = 0;
631 if (s->session->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->session->tlsext_ellipticcurvelist);
632 if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL)
633 {
634 *al = TLS1_AD_INTERNAL_ERROR;
635 return 0;
636 }
637 s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length;
638 memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length);
639 #if 0
640 fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length);
641 sdata = s->session->tlsext_ellipticcurvelist;
642 for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
643 fprintf(stderr,"%i ",*(sdata++));
644 fprintf(stderr,"\n");
645 #endif
646 }
647 #endif /* OPENSSL_NO_EC */
648 #ifdef TLSEXT_TYPE_opaque_prf_input
649 else if (type == TLSEXT_TYPE_opaque_prf_input)
650 {
651 unsigned char *sdata = data;
652
653 if (size < 2)
654 {
655 *al = SSL_AD_DECODE_ERROR;
656 return 0;
657 }
658 n2s(sdata, s->s3->client_opaque_prf_input_len);
659 if (s->s3->client_opaque_prf_input_len != size - 2)
660 {
661 *al = SSL_AD_DECODE_ERROR;
662 return 0;
663 }
664
665 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
666 OPENSSL_free(s->s3->client_opaque_prf_input);
667
668 s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
669 if (s->s3->client_opaque_prf_input == NULL)
670 {
671 *al = TLS1_AD_INTERNAL_ERROR;
672 return 0;
673 }
674 }
675 #endif
676
677 /* session ticket processed earlier */
678 data+=size;
679 }
680
681 *p = data;
682 return 1;
683 }
684
685 int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al)
686 {
687 unsigned short type;
688 unsigned short size;
689 unsigned short len;
690 unsigned char *data = *p;
691
692 int tlsext_servername = 0;
693
694 if (data >= (d+n-2))
695 return 1;
696
697 n2s(data,len);
698
699 while(data <= (d+n-4))
700 {
701 n2s(data,type);
702 n2s(data,size);
703
704 if (data+size > (d+n))
705 return 1;
706
707 if (s->tlsext_debug_cb)
708 s->tlsext_debug_cb(s, 1, type, data, size,
709 s->tlsext_debug_arg);
710
711 if (type == TLSEXT_TYPE_server_name)
712 {
713 if (s->tlsext_hostname == NULL || size > 0)
714 {
715 *al = TLS1_AD_UNRECOGNIZED_NAME;
716 return 0;
717 }
718 tlsext_servername = 1;
719 }
720
721 #ifndef OPENSSL_NO_EC
722 else if (type == TLSEXT_TYPE_ec_point_formats)
723 {
724 unsigned char *sdata = data;
725 int ecpointformatlist_length = *(sdata++);
726
727 if (ecpointformatlist_length != size - 1)
728 {
729 *al = TLS1_AD_DECODE_ERROR;
730 return 0;
731 }
732 s->session->tlsext_ecpointformatlist_length = 0;
733 if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist);
734 if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL)
735 {
736 *al = TLS1_AD_INTERNAL_ERROR;
737 return 0;
738 }
739 s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length;
740 memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length);
741 #if 0
742 fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
743 sdata = s->session->tlsext_ecpointformatlist;
744 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
745 fprintf(stderr,"%i ",*(sdata++));
746 fprintf(stderr,"\n");
747 #endif
748 }
749 #endif /* OPENSSL_NO_EC */
750
751 else if (type == TLSEXT_TYPE_session_ticket)
752 {
753 if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
754 || (size > 0))
755 {
756 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
757 return 0;
758 }
759 s->tlsext_ticket_expected = 1;
760 }
761 #ifdef TLSEXT_TYPE_opaque_prf_input
762 else if (type == TLSEXT_TYPE_opaque_prf_input)
763 {
764 unsigned char *sdata = data;
765
766 if (size < 2)
767 {
768 *al = SSL_AD_DECODE_ERROR;
769 return 0;
770 }
771 n2s(sdata, s->s3->server_opaque_prf_input_len);
772 if (s->s3->server_opaque_prf_input_len != size - 2)
773 {
774 *al = SSL_AD_DECODE_ERROR;
775 return 0;
776 }
777
778 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
779 OPENSSL_free(s->s3->server_opaque_prf_input);
780 s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
781
782 if (s->s3->server_opaque_prf_input == NULL)
783 {
784 *al = TLS1_AD_INTERNAL_ERROR;
785 return 0;
786 }
787 }
788 #endif
789
790 data+=size;
791 }
792
793 if (data != d+n)
794 {
795 *al = SSL_AD_DECODE_ERROR;
796 return 0;
797 }
798
799 if (!s->hit && tlsext_servername == 1)
800 {
801 if (s->tlsext_hostname)
802 {
803 if (s->session->tlsext_hostname == NULL)
804 {
805 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
806 if (!s->session->tlsext_hostname)
807 {
808 *al = SSL_AD_UNRECOGNIZED_NAME;
809 return 0;
810 }
811 }
812 else
813 {
814 *al = SSL_AD_DECODE_ERROR;
815 return 0;
816 }
817 }
818 }
819
820 *p = data;
821 return 1;
822 }
823
824
825 int ssl_prepare_clienthello_tlsext(SSL *s)
826 {
827 #ifndef OPENSSL_NO_EC
828 /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats
829 * and elliptic curves we support.
830 */
831 int using_ecc = 0;
832 int i;
833 unsigned char *j;
834 unsigned long alg_k, alg_a;
835 STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
836
837 for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++)
838 {
839 SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
840
841 alg_k = c->algorithm_mkey;
842 alg_a = c->algorithm_auth;
843 if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA)))
844 {
845 using_ecc = 1;
846 break;
847 }
848 }
849 using_ecc = using_ecc && (s->version == TLS1_VERSION);
850 if (using_ecc)
851 {
852 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
853 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
854 {
855 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
856 return -1;
857 }
858 s->tlsext_ecpointformatlist_length = 3;
859 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
860 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
861 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
862
863 /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */
864 if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist);
865 s->tlsext_ellipticcurvelist_length = sizeof(nid_list)/sizeof(nid_list[0]) * 2;
866 if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL)
867 {
868 s->tlsext_ellipticcurvelist_length = 0;
869 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
870 return -1;
871 }
872 for (i = 1, j = s->tlsext_ellipticcurvelist; i <= sizeof(nid_list)/sizeof(nid_list[0]); i++)
873 s2n(i,j);
874 }
875 #endif /* OPENSSL_NO_EC */
876
877 #ifdef TLSEXT_TYPE_opaque_prf_input
878 {
879 int r = 1;
880
881 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
882 {
883 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
884 if (!r)
885 return -1;
886 }
887
888 if (s->tlsext_opaque_prf_input != NULL)
889 {
890 if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */
891 OPENSSL_free(s->s3->client_opaque_prf_input);
892
893 s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
894 if (s->s3->client_opaque_prf_input == NULL)
895 {
896 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
897 return -1;
898 }
899 s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
900 }
901
902 if (r == 2)
903 /* at callback's request, insist on receiving an appropriate server opaque PRF input */
904 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
905 }
906 #endif
907
908 return 1;
909 }
910
911 int ssl_prepare_serverhello_tlsext(SSL *s)
912 {
913 #ifndef OPENSSL_NO_EC
914 /* If we are server and using an ECC cipher suite, send the point formats we support
915 * if the client sent us an ECPointsFormat extension. Note that the server is not
916 * supposed to send an EllipticCurves extension.
917 */
918
919 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
920 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
921 int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA);
922 using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
923
924 if (using_ecc)
925 {
926 if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist);
927 if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL)
928 {
929 SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE);
930 return -1;
931 }
932 s->tlsext_ecpointformatlist_length = 3;
933 s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
934 s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
935 s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
936 }
937 #endif /* OPENSSL_NO_EC */
938
939 return 1;
940 }
941
942 int ssl_check_clienthello_tlsext(SSL *s)
943 {
944 int ret=SSL_TLSEXT_ERR_NOACK;
945 int al = SSL_AD_UNRECOGNIZED_NAME;
946
947 #ifndef OPENSSL_NO_EC
948 /* The handling of the ECPointFormats extension is done elsewhere, namely in
949 * ssl3_choose_cipher in s3_lib.c.
950 */
951 /* The handling of the EllipticCurves extension is done elsewhere, namely in
952 * ssl3_choose_cipher in s3_lib.c.
953 */
954 #endif
955
956 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
957 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
958 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
959 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
960
961
962 #ifdef TLSEXT_TYPE_opaque_prf_input
963 {
964 /* This sort of belongs into ssl_prepare_serverhello_tlsext(),
965 * but we might be sending an alert in response to the client hello,
966 * so this has to happen here in ssl_check_clienthello_tlsext(). */
967
968 int r = 1;
969
970 if (s->ctx->tlsext_opaque_prf_input_callback != 0)
971 {
972 r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg);
973 if (!r)
974 {
975 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
976 al = SSL_AD_INTERNAL_ERROR;
977 goto err;
978 }
979 }
980
981 if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */
982 OPENSSL_free(s->s3->server_opaque_prf_input);
983 s->s3->server_opaque_prf_input = NULL;
984
985 if (s->tlsext_opaque_prf_input != NULL)
986 {
987 if (s->s3->client_opaque_prf_input != NULL &&
988 s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len)
989 {
990 /* can only use this extension if we have a server opaque PRF input
991 * of the same length as the client opaque PRF input! */
992
993 s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len);
994 if (s->s3->server_opaque_prf_input == NULL)
995 {
996 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
997 al = SSL_AD_INTERNAL_ERROR;
998 goto err;
999 }
1000 s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len;
1001 }
1002 }
1003
1004 if (r == 2 && s->s3->server_opaque_prf_input == NULL)
1005 {
1006 /* The callback wants to enforce use of the extension,
1007 * but we can't do that with the client opaque PRF input;
1008 * abort the handshake.
1009 */
1010 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1011 al = SSL_AD_HANDSHAKE_FAILURE;
1012 }
1013 }
1014 #endif
1015
1016 err:
1017 switch (ret)
1018 {
1019 case SSL_TLSEXT_ERR_ALERT_FATAL:
1020 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1021 return -1;
1022
1023 case SSL_TLSEXT_ERR_ALERT_WARNING:
1024 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1025 return 1;
1026
1027 case SSL_TLSEXT_ERR_NOACK:
1028 s->servername_done=0;
1029 default:
1030 return 1;
1031 }
1032 }
1033
1034 int ssl_check_serverhello_tlsext(SSL *s)
1035 {
1036 int ret=SSL_TLSEXT_ERR_NOACK;
1037 int al = SSL_AD_UNRECOGNIZED_NAME;
1038
1039 #ifndef OPENSSL_NO_EC
1040 /* If we are client and using an elliptic curve cryptography cipher suite, then server
1041 * must return a an EC point formats lists containing uncompressed.
1042 */
1043 unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1044 unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1045 if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) &&
1046 ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA)))
1047 {
1048 /* we are using an ECC cipher */
1049 size_t i;
1050 unsigned char *list;
1051 int found_uncompressed = 0;
1052 if ((s->session->tlsext_ecpointformatlist == NULL) || (s->session->tlsext_ecpointformatlist_length == 0))
1053 {
1054 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1055 return -1;
1056 }
1057 list = s->session->tlsext_ecpointformatlist;
1058 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1059 {
1060 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed)
1061 {
1062 found_uncompressed = 1;
1063 break;
1064 }
1065 }
1066 if (!found_uncompressed)
1067 {
1068 SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1069 return -1;
1070 }
1071 }
1072 ret = SSL_TLSEXT_ERR_OK;
1073 #endif /* OPENSSL_NO_EC */
1074
1075 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1076 ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg);
1077 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
1078 ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg);
1079
1080 #ifdef TLSEXT_TYPE_opaque_prf_input
1081 if (s->s3->server_opaque_prf_input_len > 0)
1082 {
1083 /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs.
1084 * So first verify that we really have a value from the server too. */
1085
1086 if (s->s3->server_opaque_prf_input == NULL)
1087 {
1088 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1089 al = SSL_AD_HANDSHAKE_FAILURE;
1090 }
1091
1092 /* Anytime the server *has* sent an opaque PRF input, we need to check
1093 * that we have a client opaque PRF input of the same size. */
1094 if (s->s3->client_opaque_prf_input == NULL ||
1095 s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len)
1096 {
1097 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1098 al = SSL_AD_ILLEGAL_PARAMETER;
1099 }
1100 }
1101 #endif
1102
1103 switch (ret)
1104 {
1105 case SSL_TLSEXT_ERR_ALERT_FATAL:
1106 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1107 return -1;
1108
1109 case SSL_TLSEXT_ERR_ALERT_WARNING:
1110 ssl3_send_alert(s,SSL3_AL_WARNING,al);
1111 return 1;
1112
1113 case SSL_TLSEXT_ERR_NOACK:
1114 s->servername_done=0;
1115 default:
1116 return 1;
1117 }
1118 }
1119
1120 /* Since the server cache lookup is done early on in the processing of client
1121 * hello and other operations depend on the result we need to handle any TLS
1122 * session ticket extension at the same time.
1123 */
1124
1125 int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
1126 const unsigned char *limit, SSL_SESSION **ret)
1127 {
1128 /* Point after session ID in client hello */
1129 const unsigned char *p = session_id + len;
1130 unsigned short i;
1131 if ((s->version <= SSL3_VERSION) || !limit)
1132 return 1;
1133 if (p >= limit)
1134 return -1;
1135 /* Skip past cipher list */
1136 n2s(p, i);
1137 p+= i;
1138 if (p >= limit)
1139 return -1;
1140 /* Skip past compression algorithm list */
1141 i = *(p++);
1142 p += i;
1143 if (p > limit)
1144 return -1;
1145 /* Now at start of extensions */
1146 if ((p + 2) >= limit)
1147 return 1;
1148 n2s(p, i);
1149 while ((p + 4) <= limit)
1150 {
1151 unsigned short type, size;
1152 n2s(p, type);
1153 n2s(p, size);
1154 if (p + size > limit)
1155 return 1;
1156 if (type == TLSEXT_TYPE_session_ticket)
1157 {
1158 /* If tickets disabled indicate cache miss which will
1159 * trigger a full handshake
1160 */
1161 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
1162 return 0;
1163 /* If zero length not client will accept a ticket
1164 * and indicate cache miss to trigger full handshake
1165 */
1166 if (size == 0)
1167 {
1168 s->tlsext_ticket_expected = 1;
1169 return 0; /* Cache miss */
1170 }
1171 return tls_decrypt_ticket(s, p, size, session_id, len,
1172 ret);
1173 }
1174 p += size;
1175 }
1176 return 1;
1177 }
1178
1179 static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen,
1180 const unsigned char *sess_id, int sesslen,
1181 SSL_SESSION **psess)
1182 {
1183 SSL_SESSION *sess;
1184 unsigned char *sdec;
1185 const unsigned char *p;
1186 int slen, mlen;
1187 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1188 HMAC_CTX hctx;
1189 EVP_CIPHER_CTX ctx;
1190 /* Attempt to process session ticket, first conduct sanity and
1191 * integrity checks on ticket.
1192 */
1193 mlen = EVP_MD_size(tlsext_tick_md());
1194 eticklen -= mlen;
1195 /* Need at least keyname + iv + some encrypted data */
1196 if (eticklen < 48)
1197 goto tickerr;
1198 /* Check key name matches */
1199 if (memcmp(etick, s->ctx->tlsext_tick_key_name, 16))
1200 goto tickerr;
1201 /* Check HMAC of encrypted ticket */
1202 HMAC_CTX_init(&hctx);
1203 HMAC_Init_ex(&hctx, s->ctx->tlsext_tick_hmac_key, 16,
1204 tlsext_tick_md(), NULL);
1205 HMAC_Update(&hctx, etick, eticklen);
1206 HMAC_Final(&hctx, tick_hmac, NULL);
1207 HMAC_CTX_cleanup(&hctx);
1208 if (memcmp(tick_hmac, etick + eticklen, mlen))
1209 goto tickerr;
1210 /* Set p to start of IV */
1211 p = etick + 16;
1212 EVP_CIPHER_CTX_init(&ctx);
1213 /* Attempt to decrypt session data */
1214 EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
1215 s->ctx->tlsext_tick_aes_key, p);
1216 /* Move p after IV to start of encrypted ticket, update length */
1217 p += 16;
1218 eticklen -= 32;
1219 sdec = OPENSSL_malloc(eticklen);
1220 if (!sdec)
1221 {
1222 EVP_CIPHER_CTX_cleanup(&ctx);
1223 return -1;
1224 }
1225 EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
1226 if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0)
1227 goto tickerr;
1228 slen += mlen;
1229 EVP_CIPHER_CTX_cleanup(&ctx);
1230 p = sdec;
1231
1232 sess = d2i_SSL_SESSION(NULL, &p, slen);
1233 OPENSSL_free(sdec);
1234 if (sess)
1235 {
1236 /* The session ID if non-empty is used by some clients to
1237 * detect that the ticket has been accepted. So we copy it to
1238 * the session structure. If it is empty set length to zero
1239 * as required by standard.
1240 */
1241 if (sesslen)
1242 memcpy(sess->session_id, sess_id, sesslen);
1243 sess->session_id_length = sesslen;
1244 *psess = sess;
1245 return 1;
1246 }
1247 /* If session decrypt failure indicate a cache miss and set state to
1248 * send a new ticket
1249 */
1250 tickerr:
1251 s->tlsext_ticket_expected = 1;
1252 return 0;
1253 }
1254
1255 #endif