blob: 8a76b42b6b6dbde4e54e4f8e658c6df92ba94e2e [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"
Janos Follath865b3eb2019-12-16 11:46:15 +000039#include "mbedtls/error.h"
Janos Follath73c616b2019-12-18 15:07:04 +000040#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010041
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020042#include <string.h>
43
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020044/*
45 * Initialze context
46 */
47void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
48{
49 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020050
51#if defined(MBEDTLS_THREADING_C)
52 mbedtls_mutex_init( &ctx->mutex );
53#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020054}
55
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020056#define MAX_KEY_BYTES 32 /* 256 bits */
57
Hanno Beckerd140d082018-11-17 21:18:01 +000058#define TICKET_KEY_NAME_BYTES 4
59#define TICKET_IV_BYTES 12
60#define TICKET_CRYPT_LEN_BYTES 2
61#define TICKET_AUTH_TAG_BYTES 16
62
63#define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
64 TICKET_IV_BYTES + \
65 TICKET_CRYPT_LEN_BYTES + \
66 TICKET_AUTH_TAG_BYTES )
67#define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
68 TICKET_IV_BYTES + \
69 TICKET_CRYPT_LEN_BYTES )
70
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020071/*
72 * Generate/update a key
73 */
74static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
75 unsigned char index )
76{
Janos Follath865b3eb2019-12-16 11:46:15 +000077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020078 unsigned char buf[MAX_KEY_BYTES];
79 mbedtls_ssl_ticket_key *key = ctx->keys + index;
80
81#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010082 key->generation_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020083#endif
84
85 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
86 return( ret );
87
88 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
89 return( ret );
90
91 /* With GCM and CCM, same context can encrypt & decrypt */
92 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020093 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020094 MBEDTLS_ENCRYPT );
95
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050096 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020097
98 return( ret );
99}
100
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200101/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200102 * Rotate/generate keys if necessary
103 */
104static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
105{
106#if !defined(MBEDTLS_HAVE_TIME)
107 ((void) ctx);
108#else
109 if( ctx->ticket_lifetime != 0 )
110 {
SimonBd5800b72016-04-26 07:43:27 +0100111 uint32_t current_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200112 uint32_t key_time = ctx->keys[ctx->active].generation_time;
113
Hanno Beckeraa715002018-08-21 13:55:31 +0100114 if( current_time >= key_time &&
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200115 current_time - key_time < ctx->ticket_lifetime )
116 {
117 return( 0 );
118 }
119
120 ctx->active = 1 - ctx->active;
121
122 return( ssl_ticket_gen_key( ctx, ctx->active ) );
123 }
124 else
125#endif /* MBEDTLS_HAVE_TIME */
126 return( 0 );
127}
128
129/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200130 * Setup context for actual use
131 */
132int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
133 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200134 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200135 uint32_t lifetime )
136{
Janos Follath865b3eb2019-12-16 11:46:15 +0000137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200138 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200139
140 ctx->f_rng = f_rng;
141 ctx->p_rng = p_rng;
142
143 ctx->ticket_lifetime = lifetime;
144
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200145 cipher_info = mbedtls_cipher_info_from_type( cipher);
146 if( cipher_info == NULL )
147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
148
149 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
150 cipher_info->mode != MBEDTLS_MODE_CCM )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200151 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200153 }
154
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200155 if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
157
Hanno Becker679d8ce2018-11-17 21:25:59 +0000158#if defined(MBEDTLS_USE_PSA_CRYPTO)
159 ret = mbedtls_cipher_setup_psa( &ctx->keys[0].ctx,
160 cipher_info, TICKET_AUTH_TAG_BYTES );
161 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200162 return( ret );
Hanno Becker679d8ce2018-11-17 21:25:59 +0000163 /* We don't yet expect to support all ciphers through PSA,
164 * so allow fallback to ordinary mbedtls_cipher_setup(). */
165 if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
166#endif /* MBEDTLS_USE_PSA_CRYPTO */
167 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
168 return( ret );
169
170#if defined(MBEDTLS_USE_PSA_CRYPTO)
171 ret = mbedtls_cipher_setup_psa( &ctx->keys[1].ctx,
172 cipher_info, TICKET_AUTH_TAG_BYTES );
173 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
174 return( ret );
175 if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
176#endif /* MBEDTLS_USE_PSA_CRYPTO */
177 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
178 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200179
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200180 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
181 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200182 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200183 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200184 }
185
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200186 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200187}
188
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200189/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200190 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200191 *
192 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200193 * opaque key_name[4];
194 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200195 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200196 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200197 * } ticket;
198 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200199 * The key_name, iv, and length of encrypted_state are the additional
200 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200201 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000202
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200203int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200204 const mbedtls_ssl_session *session,
205 unsigned char *start,
206 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200207 size_t *tlen,
208 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200209{
Janos Follath865b3eb2019-12-16 11:46:15 +0000210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200211 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200212 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200213 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000214 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
215 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
216 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200217 unsigned char *tag;
218 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200219
220 *tlen = 0;
221
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200222 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
224
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200225 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
226 * in addition to session itself, that will be checked when writing it. */
Hanno Beckerd140d082018-11-17 21:18:01 +0000227 if( end - start < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200228 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
229
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200230#if defined(MBEDTLS_THREADING_C)
231 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
232 return( ret );
233#endif
234
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200235 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
236 goto cleanup;
237
238 key = &ctx->keys[ctx->active];
239
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200240 *ticket_lifetime = ctx->ticket_lifetime;
241
Hanno Beckerd140d082018-11-17 21:18:01 +0000242 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200243
Hanno Beckerd140d082018-11-17 21:18:01 +0000244 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200245 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200246
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200247 /* Dump session state */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200248 if( ( ret = mbedtls_ssl_session_save( session,
249 state, end - state,
250 &clear_len ) ) != 0 ||
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200251 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200252 {
253 goto cleanup;
254 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200255 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
256 state_len_bytes[1] = ( clear_len ) & 0xff;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200257
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200258 /* Encrypt and authenticate */
259 tag = state + clear_len;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200260 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000261 iv, TICKET_IV_BYTES,
262 /* Additional data: key name, IV and length */
263 key_name, TICKET_ADD_DATA_LEN,
264 state, clear_len, state, &ciph_len,
265 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200266 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200267 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200268 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200269 if( ciph_len != clear_len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200270 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200271 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200272 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200273 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200274
Hanno Beckerd140d082018-11-17 21:18:01 +0000275 *tlen = TICKET_MIN_LEN + ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200276
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200277cleanup:
278#if defined(MBEDTLS_THREADING_C)
279 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
280 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
281#endif
282
283 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200284}
285
286/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200287 * Select key based on name
288 */
289static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
290 mbedtls_ssl_ticket_context *ctx,
291 const unsigned char name[4] )
292{
293 unsigned char i;
294
295 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
296 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
297 return( &ctx->keys[i] );
298
299 return( NULL );
300}
301
302/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200303 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
304 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200305int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200306 mbedtls_ssl_session *session,
307 unsigned char *buf,
308 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200309{
Janos Follath865b3eb2019-12-16 11:46:15 +0000310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200311 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200312 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200313 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000314 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
315 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
316 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200317 unsigned char *tag;
318 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200319
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200320 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200322
Hanno Beckerd140d082018-11-17 21:18:01 +0000323 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200324 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200325
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200326#if defined(MBEDTLS_THREADING_C)
327 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
328 return( ret );
329#endif
330
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200331 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
332 goto cleanup;
333
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200334 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200335 tag = ticket + enc_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200336
Hanno Beckerd140d082018-11-17 21:18:01 +0000337 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200338 {
339 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
340 goto cleanup;
341 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200342
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200343 /* Select key */
344 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200345 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200346 /* We can't know for sure but this is a likely option unless we're
347 * under attack - this is only informative anyway */
348 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200349 goto cleanup;
350 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200351
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200352 /* Decrypt and authenticate */
Hanno Beckerd140d082018-11-17 21:18:01 +0000353 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx,
354 iv, TICKET_IV_BYTES,
355 /* Additional data: key name, IV and length */
356 key_name, TICKET_ADD_DATA_LEN,
357 ticket, enc_len,
358 ticket, &clear_len,
359 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200360 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200361 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
362 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
363
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200364 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200365 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200366 if( clear_len != enc_len )
367 {
368 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200369 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200370 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200371
372 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200373 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200374 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200375
376#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200377 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200378 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100379 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200380
381 if( current_time < session->start ||
382 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
383 {
384 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
385 goto cleanup;
386 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200387 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200388#endif
389
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200390cleanup:
391#if defined(MBEDTLS_THREADING_C)
392 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
393 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
394#endif
395
396 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200397}
398
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200399/*
400 * Free context
401 */
402void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
403{
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200404 mbedtls_cipher_free( &ctx->keys[0].ctx );
405 mbedtls_cipher_free( &ctx->keys[1].ctx );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200406
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200407#if defined(MBEDTLS_THREADING_C)
408 mbedtls_mutex_free( &ctx->mutex );
409#endif
410
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500411 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200412}
413
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200414#endif /* MBEDTLS_SSL_TICKET_C */