blob: 6dad5d1b29f96941858cd4ea80e8193917a4e038 [file] [log] [blame]
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02001/*
2 * TLS server tickets callbacks implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020018 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020020 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_SSL_TICKET_C)
29
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020030#if defined(MBEDTLS_PLATFORM_C)
31#include "mbedtls/platform.h"
32#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020033#include <stdlib.h>
34#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010035#define mbedtls_free free
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020036#endif
37
SimonBd5800b72016-04-26 07:43:27 +010038#include "mbedtls/ssl_ticket.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010040
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020041#include <string.h>
42
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020043/*
44 * Initialze context
45 */
46void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
47{
48 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020049
50#if defined(MBEDTLS_THREADING_C)
51 mbedtls_mutex_init( &ctx->mutex );
52#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020053}
54
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020055#define MAX_KEY_BYTES 32 /* 256 bits */
56
Hanno Beckerd140d082018-11-17 21:18:01 +000057#define TICKET_KEY_NAME_BYTES 4
58#define TICKET_IV_BYTES 12
59#define TICKET_CRYPT_LEN_BYTES 2
60#define TICKET_AUTH_TAG_BYTES 16
61
62#define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
63 TICKET_IV_BYTES + \
64 TICKET_CRYPT_LEN_BYTES + \
65 TICKET_AUTH_TAG_BYTES )
66#define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
67 TICKET_IV_BYTES + \
68 TICKET_CRYPT_LEN_BYTES )
69
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020070/*
71 * Generate/update a key
72 */
73static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
74 unsigned char index )
75{
76 int ret;
77 unsigned char buf[MAX_KEY_BYTES];
78 mbedtls_ssl_ticket_key *key = ctx->keys + index;
79
80#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010081 key->generation_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020082#endif
83
84 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
85 return( ret );
86
87 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
88 return( ret );
89
90 /* With GCM and CCM, same context can encrypt & decrypt */
91 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020092 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020093 MBEDTLS_ENCRYPT );
94
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050095 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020096
97 return( ret );
98}
99
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200100/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200101 * Rotate/generate keys if necessary
102 */
103static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
104{
105#if !defined(MBEDTLS_HAVE_TIME)
106 ((void) ctx);
107#else
108 if( ctx->ticket_lifetime != 0 )
109 {
SimonBd5800b72016-04-26 07:43:27 +0100110 uint32_t current_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200111 uint32_t key_time = ctx->keys[ctx->active].generation_time;
112
Hanno Beckeraa715002018-08-21 13:55:31 +0100113 if( current_time >= key_time &&
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200114 current_time - key_time < ctx->ticket_lifetime )
115 {
116 return( 0 );
117 }
118
119 ctx->active = 1 - ctx->active;
120
121 return( ssl_ticket_gen_key( ctx, ctx->active ) );
122 }
123 else
124#endif /* MBEDTLS_HAVE_TIME */
125 return( 0 );
126}
127
128/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200129 * Setup context for actual use
130 */
131int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
132 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200133 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200134 uint32_t lifetime )
135{
136 int ret;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200137 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200138
139 ctx->f_rng = f_rng;
140 ctx->p_rng = p_rng;
141
142 ctx->ticket_lifetime = lifetime;
143
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200144 cipher_info = mbedtls_cipher_info_from_type( cipher);
145 if( cipher_info == NULL )
146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
147
148 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
149 cipher_info->mode != MBEDTLS_MODE_CCM )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200150 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200152 }
153
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200154 if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
156
Hanno Becker679d8ce2018-11-17 21:25:59 +0000157#if defined(MBEDTLS_USE_PSA_CRYPTO)
158 ret = mbedtls_cipher_setup_psa( &ctx->keys[0].ctx,
159 cipher_info, TICKET_AUTH_TAG_BYTES );
160 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200161 return( ret );
Hanno Becker679d8ce2018-11-17 21:25:59 +0000162 /* We don't yet expect to support all ciphers through PSA,
163 * so allow fallback to ordinary mbedtls_cipher_setup(). */
164 if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
165#endif /* MBEDTLS_USE_PSA_CRYPTO */
166 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
167 return( ret );
168
169#if defined(MBEDTLS_USE_PSA_CRYPTO)
170 ret = mbedtls_cipher_setup_psa( &ctx->keys[1].ctx,
171 cipher_info, TICKET_AUTH_TAG_BYTES );
172 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
173 return( ret );
174 if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
175#endif /* MBEDTLS_USE_PSA_CRYPTO */
176 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
177 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200178
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200179 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
180 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200181 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200182 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200183 }
184
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200185 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200186}
187
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200188/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200189 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200190 *
191 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200192 * opaque key_name[4];
193 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200194 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200195 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200196 * } ticket;
197 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200198 * The key_name, iv, and length of encrypted_state are the additional
199 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200200 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000201
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200202int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200203 const mbedtls_ssl_session *session,
204 unsigned char *start,
205 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200206 size_t *tlen,
207 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200208{
209 int ret;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200210 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200211 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200212 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000213 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
214 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
215 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200216 unsigned char *tag;
217 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200218
219 *tlen = 0;
220
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200221 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200222 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
223
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200224 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
225 * in addition to session itself, that will be checked when writing it. */
Hanno Beckerd140d082018-11-17 21:18:01 +0000226 if( end - start < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200227 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
228
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200229#if defined(MBEDTLS_THREADING_C)
230 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
231 return( ret );
232#endif
233
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200234 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
235 goto cleanup;
236
237 key = &ctx->keys[ctx->active];
238
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200239 *ticket_lifetime = ctx->ticket_lifetime;
240
Hanno Beckerd140d082018-11-17 21:18:01 +0000241 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200242
Hanno Beckerd140d082018-11-17 21:18:01 +0000243 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200244 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200245
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200246 /* Dump session state */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200247 if( ( ret = mbedtls_ssl_session_save( session,
248 state, end - state,
249 &clear_len ) ) != 0 ||
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200250 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200251 {
252 goto cleanup;
253 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200254 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
255 state_len_bytes[1] = ( clear_len ) & 0xff;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200256
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200257 /* Encrypt and authenticate */
258 tag = state + clear_len;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200259 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000260 iv, TICKET_IV_BYTES,
261 /* Additional data: key name, IV and length */
262 key_name, TICKET_ADD_DATA_LEN,
263 state, clear_len, state, &ciph_len,
264 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200265 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200266 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200267 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200268 if( ciph_len != clear_len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200269 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200270 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200271 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200272 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200273
Hanno Beckerd140d082018-11-17 21:18:01 +0000274 *tlen = TICKET_MIN_LEN + ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200275
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200276cleanup:
277#if defined(MBEDTLS_THREADING_C)
278 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
279 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
280#endif
281
282 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200283}
284
285/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200286 * Select key based on name
287 */
288static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
289 mbedtls_ssl_ticket_context *ctx,
290 const unsigned char name[4] )
291{
292 unsigned char i;
293
294 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
295 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
296 return( &ctx->keys[i] );
297
298 return( NULL );
299}
300
301/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200302 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
303 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200304int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200305 mbedtls_ssl_session *session,
306 unsigned char *buf,
307 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200308{
309 int ret;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200310 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200311 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200312 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000313 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
314 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
315 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200316 unsigned char *tag;
317 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200318
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200319 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200321
Hanno Beckerd140d082018-11-17 21:18:01 +0000322 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200323 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200324
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200325#if defined(MBEDTLS_THREADING_C)
326 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
327 return( ret );
328#endif
329
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200330 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
331 goto cleanup;
332
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200333 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200334 tag = ticket + enc_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200335
Hanno Beckerd140d082018-11-17 21:18:01 +0000336 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200337 {
338 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
339 goto cleanup;
340 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200341
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200342 /* Select key */
343 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200344 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200345 /* We can't know for sure but this is a likely option unless we're
346 * under attack - this is only informative anyway */
347 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200348 goto cleanup;
349 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200350
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200351 /* Decrypt and authenticate */
Hanno Beckerd140d082018-11-17 21:18:01 +0000352 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx,
353 iv, TICKET_IV_BYTES,
354 /* Additional data: key name, IV and length */
355 key_name, TICKET_ADD_DATA_LEN,
356 ticket, enc_len,
357 ticket, &clear_len,
358 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200359 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200360 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
361 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
362
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200363 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200364 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200365 if( clear_len != enc_len )
366 {
367 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200368 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200369 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200370
371 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200372 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200373 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200374
375#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200376 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200377 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100378 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200379
380 if( current_time < session->start ||
381 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
382 {
383 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
384 goto cleanup;
385 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200386 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200387#endif
388
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200389cleanup:
390#if defined(MBEDTLS_THREADING_C)
391 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
392 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
393#endif
394
395 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200396}
397
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200398/*
399 * Free context
400 */
401void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
402{
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200403 mbedtls_cipher_free( &ctx->keys[0].ctx );
404 mbedtls_cipher_free( &ctx->keys[1].ctx );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200405
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200406#if defined(MBEDTLS_THREADING_C)
407 mbedtls_mutex_free( &ctx->mutex );
408#endif
409
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500410 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200411}
412
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200413#endif /* MBEDTLS_SSL_TICKET_C */