blob: 23098d9f7c35aa2a69a576a4a2efa63c8d69c9e7 [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
36#define mbedtls_malloc malloc
37#define mbedtls_free free
38#endif
39
40#include <string.h>
41
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020042/* Implementation that should never be optimized out by the compiler */
43static void mbedtls_zeroize( void *v, size_t n ) {
44 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
45}
46
47/*
48 * Initialze context
49 */
50void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
51{
52 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020053
54#if defined(MBEDTLS_THREADING_C)
55 mbedtls_mutex_init( &ctx->mutex );
56#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020057}
58
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020059#define MAX_KEY_BYTES 32 /* 256 bits */
60
61/*
62 * Generate/update a key
63 */
64static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
65 unsigned char index )
66{
67 int ret;
68 unsigned char buf[MAX_KEY_BYTES];
69 mbedtls_ssl_ticket_key *key = ctx->keys + index;
70
71#if defined(MBEDTLS_HAVE_TIME)
72 key->generation_time = (uint32_t) time( NULL );
73#endif
74
75 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
76 return( ret );
77
78 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
79 return( ret );
80
81 /* With GCM and CCM, same context can encrypt & decrypt */
82 ret = mbedtls_cipher_setkey( &key->ctx, buf,
83 mbedtls_cipher_get_key_size( &key->ctx ),
84 MBEDTLS_ENCRYPT );
85
86 mbedtls_zeroize( buf, sizeof( buf ) );
87
88 return( ret );
89}
90
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020091/*
92 * Setup context for actual use
93 */
94int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
95 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +020096 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020097 uint32_t lifetime )
98{
99 int ret;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200100 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200101
102 ctx->f_rng = f_rng;
103 ctx->p_rng = p_rng;
104
105 ctx->ticket_lifetime = lifetime;
106
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200107 cipher_info = mbedtls_cipher_info_from_type( cipher);
108 if( cipher_info == NULL )
109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
110
111 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
112 cipher_info->mode != MBEDTLS_MODE_CCM )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200113 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200114 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200115 }
116
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200117 if( cipher_info->key_length > 8 * MAX_KEY_BYTES )
118 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
119
120 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ||
121 ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200122 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200123 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200124 }
125
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200126 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
127 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200128 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200129 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200130 }
131
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200132 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200133}
134
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200135/*
136 * Serialize a session in the following format:
137 * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
138 * n . n+2 peer_cert length = m (0 if no certificate)
139 * n+3 . n+2+m peer cert ASN.1
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200140 */
141static int ssl_save_session( const mbedtls_ssl_session *session,
142 unsigned char *buf, size_t buf_len,
143 size_t *olen )
144{
145 unsigned char *p = buf;
146 size_t left = buf_len;
147#if defined(MBEDTLS_X509_CRT_PARSE_C)
148 size_t cert_len;
149#endif /* MBEDTLS_X509_CRT_PARSE_C */
150
151 if( left < sizeof( mbedtls_ssl_session ) )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200152 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200153
154 memcpy( p, session, sizeof( mbedtls_ssl_session ) );
155 p += sizeof( mbedtls_ssl_session );
156 left -= sizeof( mbedtls_ssl_session );
157
158#if defined(MBEDTLS_X509_CRT_PARSE_C)
159 if( session->peer_cert == NULL )
160 cert_len = 0;
161 else
162 cert_len = session->peer_cert->raw.len;
163
164 if( left < 3 + cert_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200165 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200166
167 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
168 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
169 *p++ = (unsigned char)( cert_len & 0xFF );
170
171 if( session->peer_cert != NULL )
172 memcpy( p, session->peer_cert->raw.p, cert_len );
173
174 p += cert_len;
175#endif /* MBEDTLS_X509_CRT_PARSE_C */
176
177 *olen = p - buf;
178
179 return( 0 );
180}
181
182/*
183 * Unserialise session, see ssl_save_session()
184 */
185static int ssl_load_session( mbedtls_ssl_session *session,
186 const unsigned char *buf, size_t len )
187{
188 const unsigned char *p = buf;
189 const unsigned char * const end = buf + len;
190#if defined(MBEDTLS_X509_CRT_PARSE_C)
191 size_t cert_len;
192#endif /* MBEDTLS_X509_CRT_PARSE_C */
193
194 if( p + sizeof( mbedtls_ssl_session ) > end )
195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
196
197 memcpy( session, p, sizeof( mbedtls_ssl_session ) );
198 p += sizeof( mbedtls_ssl_session );
199
200#if defined(MBEDTLS_X509_CRT_PARSE_C)
201 if( p + 3 > end )
202 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
203
204 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
205 p += 3;
206
207 if( cert_len == 0 )
208 {
209 session->peer_cert = NULL;
210 }
211 else
212 {
213 int ret;
214
215 if( p + cert_len > end )
216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
217
218 session->peer_cert = mbedtls_malloc( sizeof( mbedtls_x509_crt ) );
219
220 if( session->peer_cert == NULL )
221 return( MBEDTLS_ERR_SSL_MALLOC_FAILED );
222
223 mbedtls_x509_crt_init( session->peer_cert );
224
225 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
226 p, cert_len ) ) != 0 )
227 {
228 mbedtls_x509_crt_free( session->peer_cert );
229 mbedtls_free( session->peer_cert );
230 session->peer_cert = NULL;
231 return( ret );
232 }
233
234 p += cert_len;
235 }
236#endif /* MBEDTLS_X509_CRT_PARSE_C */
237
238 if( p != end )
239 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
240
241 return( 0 );
242}
243
244/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200245 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200246 *
247 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200248 * opaque key_name[4];
249 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200250 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200251 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200252 * } ticket;
253 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200254 * The key_name, iv, and length of encrypted_state are the additional
255 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200256 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200257int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200258 const mbedtls_ssl_session *session,
259 unsigned char *start,
260 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200261 size_t *tlen,
262 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200263{
264 int ret;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200265 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200266 mbedtls_ssl_ticket_key *key = ctx->keys;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200267 unsigned char *key_name = start;
268 unsigned char *iv = start + 4;
269 unsigned char *state_len_bytes = iv + 12;
270 unsigned char *state = state_len_bytes + 2;
271 unsigned char *tag;
272 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200273
274 *tlen = 0;
275
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200276 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200277 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
278
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200279 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
280 * in addition to session itself, that will be checked when writing it. */
281 if( end - start < 4 + 12 + 2 + 16 )
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200282 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
283
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200284#if defined(MBEDTLS_THREADING_C)
285 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
286 return( ret );
287#endif
288
289 *ticket_lifetime = ctx->ticket_lifetime;
290
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200291 memcpy( key_name, key->name, 4 );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200292
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200293 if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200294 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200295
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200296 /* Dump session state */
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200297 if( ( ret = ssl_save_session( session,
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200298 state, end - state, &clear_len ) ) != 0 ||
299 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200300 {
301 goto cleanup;
302 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200303 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
304 state_len_bytes[1] = ( clear_len ) & 0xff;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200305
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200306 /* Encrypt and authenticate */
307 tag = state + clear_len;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200308 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200309 iv, 12, key_name, 4 + 12 + 2,
310 state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200311 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200312 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200313 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200314 if( ciph_len != clear_len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200315 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200316 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200317 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200318 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200319
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200320 *tlen = 4 + 12 + 2 + 16 + ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200321
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200322cleanup:
323#if defined(MBEDTLS_THREADING_C)
324 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
325 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
326#endif
327
328 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200329}
330
331/*
332 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
333 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200334int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200335 mbedtls_ssl_session *session,
336 unsigned char *buf,
337 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200338{
339 int ret;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200340 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200341 mbedtls_ssl_ticket_key *key = ctx->keys;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200342 unsigned char *key_name = buf;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200343 unsigned char *iv = buf + 4;
344 unsigned char *enc_len_p = iv + 12;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200345 unsigned char *ticket = enc_len_p + 2;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200346 unsigned char *tag;
347 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200348
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200349 if( ctx == NULL || ctx->f_rng == NULL ||
350 len < 4 + 12 + 2 + 16 )
351 {
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200352 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200353 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200354
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200355#if defined(MBEDTLS_THREADING_C)
356 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
357 return( ret );
358#endif
359
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200360 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200361 tag = ticket + enc_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200362
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200363 if( len != 4 + 12 + 2 + enc_len + 16 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200364 {
365 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
366 goto cleanup;
367 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200368
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200369 /* Check name (public data) */
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200370 if( memcmp( key_name, key->name, 4 ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200371 {
372 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
373 goto cleanup;
374 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200375
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200376 /* Decrypt and authenticate */
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200377 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12,
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200378 key_name, 4 + 12 + 2, ticket, enc_len,
379 ticket, &clear_len, tag, 16 ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200380 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200381 /* TODO: convert AUTH_FAILED to INVALID_MAC */
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200382 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200383 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200384 if( clear_len != enc_len )
385 {
386 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200387 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200388 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200389
390 /* Actually load session */
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200391 if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200392 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200393
394#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200395 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200396 /* Check for expiration */
397 time_t current_time = time( NULL );
398
399 if( current_time < session->start ||
400 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
401 {
402 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
403 goto cleanup;
404 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200405 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200406#endif
407
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200408cleanup:
409#if defined(MBEDTLS_THREADING_C)
410 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
411 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
412#endif
413
414 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200415}
416
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200417/*
418 * Free context
419 */
420void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
421{
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200422 mbedtls_cipher_free( &ctx->keys[0].ctx );
423 mbedtls_cipher_free( &ctx->keys[1].ctx );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200424
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200425#if defined(MBEDTLS_THREADING_C)
426 mbedtls_mutex_free( &ctx->mutex );
427#endif
428
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200429 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
430}
431
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200432#endif /* MBEDTLS_SSL_TICKET_C */