blob: baa8b825b6afd34fe90b527cea031f48c134e933 [file] [log] [blame]
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02001/*
2 * TLS server tickets callbacks implementation
3 *
4 * Copyright (C) 2015, ARM Limited, All Rights Reserved
5 *
6 * This file is part of mbed TLS (https://tls.mbed.org)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#if !defined(MBEDTLS_CONFIG_FILE)
24#include "mbedtls/config.h"
25#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
29#if defined(MBEDTLS_SSL_TICKET_C)
30
31#include "mbedtls/ssl_ticket.h"
32
33#if defined(MBEDTLS_PLATFORM_C)
34#include "mbedtls/platform.h"
35#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020036#include <stdlib.h>
37#define mbedtls_calloc calloc
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020038#define mbedtls_free free
39#endif
40
41#include <string.h>
42
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020043/* Implementation that should never be optimized out by the compiler */
44static void mbedtls_zeroize( void *v, size_t n ) {
45 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
46}
47
48/*
49 * Initialze context
50 */
51void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
52{
53 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020054
55#if defined(MBEDTLS_THREADING_C)
56 mbedtls_mutex_init( &ctx->mutex );
57#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020058}
59
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020060#define MAX_KEY_BYTES 32 /* 256 bits */
61
62/*
63 * Generate/update a key
64 */
65static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
66 unsigned char index )
67{
68 int ret;
69 unsigned char buf[MAX_KEY_BYTES];
70 mbedtls_ssl_ticket_key *key = ctx->keys + index;
71
72#if defined(MBEDTLS_HAVE_TIME)
73 key->generation_time = (uint32_t) time( NULL );
74#endif
75
76 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
77 return( ret );
78
79 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
80 return( ret );
81
82 /* With GCM and CCM, same context can encrypt & decrypt */
83 ret = mbedtls_cipher_setkey( &key->ctx, buf,
84 mbedtls_cipher_get_key_size( &key->ctx ),
85 MBEDTLS_ENCRYPT );
86
87 mbedtls_zeroize( buf, sizeof( buf ) );
88
89 return( ret );
90}
91
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020092/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +020093 * Rotate/generate keys if necessary
94 */
95static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
96{
97#if !defined(MBEDTLS_HAVE_TIME)
98 ((void) ctx);
99#else
100 if( ctx->ticket_lifetime != 0 )
101 {
102 uint32_t current_time = (uint32_t) time( NULL );
103 uint32_t key_time = ctx->keys[ctx->active].generation_time;
104
105 if( current_time > key_time &&
106 current_time - key_time < ctx->ticket_lifetime )
107 {
108 return( 0 );
109 }
110
111 ctx->active = 1 - ctx->active;
112
113 return( ssl_ticket_gen_key( ctx, ctx->active ) );
114 }
115 else
116#endif /* MBEDTLS_HAVE_TIME */
117 return( 0 );
118}
119
120/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200121 * Setup context for actual use
122 */
123int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
124 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200125 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200126 uint32_t lifetime )
127{
128 int ret;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200129 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200130
131 ctx->f_rng = f_rng;
132 ctx->p_rng = p_rng;
133
134 ctx->ticket_lifetime = lifetime;
135
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200136 cipher_info = mbedtls_cipher_info_from_type( cipher);
137 if( cipher_info == NULL )
138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
139
140 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
141 cipher_info->mode != MBEDTLS_MODE_CCM )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200142 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200144 }
145
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200146 if( cipher_info->key_length > 8 * MAX_KEY_BYTES )
147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
148
149 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ||
150 ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200151 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200152 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200153 }
154
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200155 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
156 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200157 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200158 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200159 }
160
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200161 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200162}
163
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200164/*
165 * Serialize a session in the following format:
166 * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
167 * n . n+2 peer_cert length = m (0 if no certificate)
168 * n+3 . n+2+m peer cert ASN.1
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200169 */
170static int ssl_save_session( const mbedtls_ssl_session *session,
171 unsigned char *buf, size_t buf_len,
172 size_t *olen )
173{
174 unsigned char *p = buf;
175 size_t left = buf_len;
176#if defined(MBEDTLS_X509_CRT_PARSE_C)
177 size_t cert_len;
178#endif /* MBEDTLS_X509_CRT_PARSE_C */
179
180 if( left < sizeof( mbedtls_ssl_session ) )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200181 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200182
183 memcpy( p, session, sizeof( mbedtls_ssl_session ) );
184 p += sizeof( mbedtls_ssl_session );
185 left -= sizeof( mbedtls_ssl_session );
186
187#if defined(MBEDTLS_X509_CRT_PARSE_C)
188 if( session->peer_cert == NULL )
189 cert_len = 0;
190 else
191 cert_len = session->peer_cert->raw.len;
192
193 if( left < 3 + cert_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200194 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200195
196 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
197 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
198 *p++ = (unsigned char)( cert_len & 0xFF );
199
200 if( session->peer_cert != NULL )
201 memcpy( p, session->peer_cert->raw.p, cert_len );
202
203 p += cert_len;
204#endif /* MBEDTLS_X509_CRT_PARSE_C */
205
206 *olen = p - buf;
207
208 return( 0 );
209}
210
211/*
212 * Unserialise session, see ssl_save_session()
213 */
214static int ssl_load_session( mbedtls_ssl_session *session,
215 const unsigned char *buf, size_t len )
216{
217 const unsigned char *p = buf;
218 const unsigned char * const end = buf + len;
219#if defined(MBEDTLS_X509_CRT_PARSE_C)
220 size_t cert_len;
221#endif /* MBEDTLS_X509_CRT_PARSE_C */
222
223 if( p + sizeof( mbedtls_ssl_session ) > end )
224 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
225
226 memcpy( session, p, sizeof( mbedtls_ssl_session ) );
227 p += sizeof( mbedtls_ssl_session );
228
229#if defined(MBEDTLS_X509_CRT_PARSE_C)
230 if( p + 3 > end )
231 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
232
233 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
234 p += 3;
235
236 if( cert_len == 0 )
237 {
238 session->peer_cert = NULL;
239 }
240 else
241 {
242 int ret;
243
244 if( p + cert_len > end )
245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
246
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200247 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200248
249 if( session->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200250 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200251
252 mbedtls_x509_crt_init( session->peer_cert );
253
254 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
255 p, cert_len ) ) != 0 )
256 {
257 mbedtls_x509_crt_free( session->peer_cert );
258 mbedtls_free( session->peer_cert );
259 session->peer_cert = NULL;
260 return( ret );
261 }
262
263 p += cert_len;
264 }
265#endif /* MBEDTLS_X509_CRT_PARSE_C */
266
267 if( p != end )
268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
269
270 return( 0 );
271}
272
273/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200274 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200275 *
276 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200277 * opaque key_name[4];
278 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200279 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200280 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200281 * } ticket;
282 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200283 * The key_name, iv, and length of encrypted_state are the additional
284 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200285 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200286int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200287 const mbedtls_ssl_session *session,
288 unsigned char *start,
289 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200290 size_t *tlen,
291 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200292{
293 int ret;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200294 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200295 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200296 unsigned char *key_name = start;
297 unsigned char *iv = start + 4;
298 unsigned char *state_len_bytes = iv + 12;
299 unsigned char *state = state_len_bytes + 2;
300 unsigned char *tag;
301 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200302
303 *tlen = 0;
304
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200305 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200306 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
307
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200308 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
309 * in addition to session itself, that will be checked when writing it. */
310 if( end - start < 4 + 12 + 2 + 16 )
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200311 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
312
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200313#if defined(MBEDTLS_THREADING_C)
314 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
315 return( ret );
316#endif
317
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200318 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
319 goto cleanup;
320
321 key = &ctx->keys[ctx->active];
322
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200323 *ticket_lifetime = ctx->ticket_lifetime;
324
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200325 memcpy( key_name, key->name, 4 );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200326
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200327 if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200328 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200329
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200330 /* Dump session state */
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200331 if( ( ret = ssl_save_session( session,
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200332 state, end - state, &clear_len ) ) != 0 ||
333 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200334 {
335 goto cleanup;
336 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200337 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
338 state_len_bytes[1] = ( clear_len ) & 0xff;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200339
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200340 /* Encrypt and authenticate */
341 tag = state + clear_len;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200342 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200343 iv, 12, key_name, 4 + 12 + 2,
344 state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200345 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200346 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200347 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200348 if( ciph_len != clear_len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200349 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200350 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200351 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200352 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200353
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200354 *tlen = 4 + 12 + 2 + 16 + ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200355
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200356cleanup:
357#if defined(MBEDTLS_THREADING_C)
358 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
359 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
360#endif
361
362 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200363}
364
365/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200366 * Select key based on name
367 */
368static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
369 mbedtls_ssl_ticket_context *ctx,
370 const unsigned char name[4] )
371{
372 unsigned char i;
373
374 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
375 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
376 return( &ctx->keys[i] );
377
378 return( NULL );
379}
380
381/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200382 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
383 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200384int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200385 mbedtls_ssl_session *session,
386 unsigned char *buf,
387 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200388{
389 int ret;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200390 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200391 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200392 unsigned char *key_name = buf;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200393 unsigned char *iv = buf + 4;
394 unsigned char *enc_len_p = iv + 12;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200395 unsigned char *ticket = enc_len_p + 2;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200396 unsigned char *tag;
397 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200398
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200399 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200401
402 /* See mbedtls_ssl_ticket_write() */
403 if( len < 4 + 12 + 2 + 16 )
404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200405
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200406#if defined(MBEDTLS_THREADING_C)
407 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
408 return( ret );
409#endif
410
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200411 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
412 goto cleanup;
413
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200414 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200415 tag = ticket + enc_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200416
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200417 if( len != 4 + 12 + 2 + enc_len + 16 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200418 {
419 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
420 goto cleanup;
421 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200422
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200423 /* Select key */
424 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200425 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200426 /* We can't know for sure but this is a likely option unless we're
427 * under attack - this is only informative anyway */
428 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200429 goto cleanup;
430 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200431
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200432 /* Decrypt and authenticate */
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200433 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12,
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200434 key_name, 4 + 12 + 2, ticket, enc_len,
435 ticket, &clear_len, tag, 16 ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200436 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200437 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
438 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
439
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200440 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200441 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200442 if( clear_len != enc_len )
443 {
444 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200445 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200446 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200447
448 /* Actually load session */
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200449 if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200450 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200451
452#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200453 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200454 /* Check for expiration */
455 time_t current_time = time( NULL );
456
457 if( current_time < session->start ||
458 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
459 {
460 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
461 goto cleanup;
462 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200463 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200464#endif
465
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200466cleanup:
467#if defined(MBEDTLS_THREADING_C)
468 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
469 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
470#endif
471
472 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200473}
474
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200475/*
476 * Free context
477 */
478void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
479{
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200480 mbedtls_cipher_free( &ctx->keys[0].ctx );
481 mbedtls_cipher_free( &ctx->keys[1].ctx );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200482
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200483#if defined(MBEDTLS_THREADING_C)
484 mbedtls_mutex_free( &ctx->mutex );
485#endif
486
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200487 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
488}
489
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200490#endif /* MBEDTLS_SSL_TICKET_C */