blob: bfa254607f4394dbc621b7c6f21429024e1d92d0 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020023
24#if defined(MBEDTLS_SSL_TICKET_C)
25
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020026#if defined(MBEDTLS_PLATFORM_C)
27#include "mbedtls/platform.h"
28#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020029#include <stdlib.h>
30#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010031#define mbedtls_free free
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020032#endif
33
Hanno Becker261602c2017-04-12 14:54:42 +010034#include "mbedtls/ssl_internal.h"
SimonBd5800b72016-04-26 07:43:27 +010035#include "mbedtls/ssl_ticket.h"
Janos Follath865b3eb2019-12-16 11:46:15 +000036#include "mbedtls/error.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010038
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020039#include <string.h>
40
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020041/*
42 * Initialze context
43 */
44void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
45{
46 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020047
48#if defined(MBEDTLS_THREADING_C)
49 mbedtls_mutex_init( &ctx->mutex );
50#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020051}
52
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020053#define MAX_KEY_BYTES 32 /* 256 bits */
54
Hanno Beckerd140d082018-11-17 21:18:01 +000055#define TICKET_KEY_NAME_BYTES 4
56#define TICKET_IV_BYTES 12
57#define TICKET_CRYPT_LEN_BYTES 2
58#define TICKET_AUTH_TAG_BYTES 16
59
60#define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
61 TICKET_IV_BYTES + \
62 TICKET_CRYPT_LEN_BYTES + \
63 TICKET_AUTH_TAG_BYTES )
64#define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
65 TICKET_IV_BYTES + \
66 TICKET_CRYPT_LEN_BYTES )
67
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020068/*
69 * Generate/update a key
70 */
71static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
72 unsigned char index )
73{
Janos Follath865b3eb2019-12-16 11:46:15 +000074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020075 unsigned char buf[MAX_KEY_BYTES];
76 mbedtls_ssl_ticket_key *key = ctx->keys + index;
77
78#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010079 key->generation_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020080#endif
81
82 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
83 return( ret );
84
85 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
86 return( ret );
87
88 /* With GCM and CCM, same context can encrypt & decrypt */
89 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020090 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020091 MBEDTLS_ENCRYPT );
92
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050093 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020094
95 return( ret );
96}
97
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020098/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +020099 * Rotate/generate keys if necessary
100 */
101static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
102{
103#if !defined(MBEDTLS_HAVE_TIME)
104 ((void) ctx);
105#else
106 if( ctx->ticket_lifetime != 0 )
107 {
SimonBd5800b72016-04-26 07:43:27 +0100108 uint32_t current_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200109 uint32_t key_time = ctx->keys[ctx->active].generation_time;
110
Hanno Beckeraa715002018-08-21 13:55:31 +0100111 if( current_time >= key_time &&
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200112 current_time - key_time < ctx->ticket_lifetime )
113 {
114 return( 0 );
115 }
116
117 ctx->active = 1 - ctx->active;
118
119 return( ssl_ticket_gen_key( ctx, ctx->active ) );
120 }
121 else
122#endif /* MBEDTLS_HAVE_TIME */
123 return( 0 );
124}
125
126/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200127 * Setup context for actual use
128 */
129int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
130 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200131 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200132 uint32_t lifetime )
133{
Janos Follath865b3eb2019-12-16 11:46:15 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200135 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200136
137 ctx->f_rng = f_rng;
138 ctx->p_rng = p_rng;
139
140 ctx->ticket_lifetime = lifetime;
141
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200142 cipher_info = mbedtls_cipher_info_from_type( cipher);
143 if( cipher_info == NULL )
144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145
146 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
147 cipher_info->mode != MBEDTLS_MODE_CCM )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200148 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200149 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200150 }
151
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200152 if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
154
Hanno Becker679d8ce2018-11-17 21:25:59 +0000155#if defined(MBEDTLS_USE_PSA_CRYPTO)
156 ret = mbedtls_cipher_setup_psa( &ctx->keys[0].ctx,
157 cipher_info, TICKET_AUTH_TAG_BYTES );
158 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200159 return( ret );
Hanno Becker679d8ce2018-11-17 21:25:59 +0000160 /* We don't yet expect to support all ciphers through PSA,
161 * so allow fallback to ordinary mbedtls_cipher_setup(). */
162 if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
163#endif /* MBEDTLS_USE_PSA_CRYPTO */
164 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
165 return( ret );
166
167#if defined(MBEDTLS_USE_PSA_CRYPTO)
168 ret = mbedtls_cipher_setup_psa( &ctx->keys[1].ctx,
169 cipher_info, TICKET_AUTH_TAG_BYTES );
170 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
171 return( ret );
172 if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
173#endif /* MBEDTLS_USE_PSA_CRYPTO */
174 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
175 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200176
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200177 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
178 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200179 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200180 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200181 }
182
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200183 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200184}
185
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200186/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200187 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200188 *
189 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200190 * opaque key_name[4];
191 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200192 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200193 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200194 * } ticket;
195 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200196 * The key_name, iv, and length of encrypted_state are the additional
197 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200198 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000199
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200200int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200201 const mbedtls_ssl_session *session,
202 unsigned char *start,
203 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200204 size_t *tlen,
205 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200206{
Janos Follath865b3eb2019-12-16 11:46:15 +0000207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200208 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200209 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200210 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000211 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
212 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
213 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200214 unsigned char *tag;
215 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200216
217 *tlen = 0;
218
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200219 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
221
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200222 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
223 * in addition to session itself, that will be checked when writing it. */
Hanno Becker261602c2017-04-12 14:54:42 +0100224 MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200225
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200226#if defined(MBEDTLS_THREADING_C)
227 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
228 return( ret );
229#endif
230
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200231 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
232 goto cleanup;
233
234 key = &ctx->keys[ctx->active];
235
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200236 *ticket_lifetime = ctx->ticket_lifetime;
237
Hanno Beckerd140d082018-11-17 21:18:01 +0000238 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200239
Hanno Beckerd140d082018-11-17 21:18:01 +0000240 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200241 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200242
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200243 /* Dump session state */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200244 if( ( ret = mbedtls_ssl_session_save( session,
245 state, end - state,
246 &clear_len ) ) != 0 ||
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200247 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200248 {
249 goto cleanup;
250 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200251 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
252 state_len_bytes[1] = ( clear_len ) & 0xff;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200253
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200254 /* Encrypt and authenticate */
255 tag = state + clear_len;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200256 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000257 iv, TICKET_IV_BYTES,
258 /* Additional data: key name, IV and length */
259 key_name, TICKET_ADD_DATA_LEN,
260 state, clear_len, state, &ciph_len,
261 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200262 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200263 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200264 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200265 if( ciph_len != clear_len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200266 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200267 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200268 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200269 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200270
Hanno Beckerd140d082018-11-17 21:18:01 +0000271 *tlen = TICKET_MIN_LEN + ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200272
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200273cleanup:
274#if defined(MBEDTLS_THREADING_C)
275 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
276 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
277#endif
278
279 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200280}
281
282/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200283 * Select key based on name
284 */
285static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
286 mbedtls_ssl_ticket_context *ctx,
287 const unsigned char name[4] )
288{
289 unsigned char i;
290
291 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
292 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
293 return( &ctx->keys[i] );
294
295 return( NULL );
296}
297
298/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200299 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
300 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200301int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200302 mbedtls_ssl_session *session,
303 unsigned char *buf,
304 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200305{
Janos Follath865b3eb2019-12-16 11:46:15 +0000306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200307 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200308 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200309 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000310 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
311 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
312 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200313 unsigned char *tag;
314 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200315
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200316 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200318
Hanno Beckerd140d082018-11-17 21:18:01 +0000319 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200321
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200322#if defined(MBEDTLS_THREADING_C)
323 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
324 return( ret );
325#endif
326
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200327 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
328 goto cleanup;
329
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200330 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200331 tag = ticket + enc_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200332
Hanno Beckerd140d082018-11-17 21:18:01 +0000333 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200334 {
335 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
336 goto cleanup;
337 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200338
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200339 /* Select key */
340 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200341 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200342 /* We can't know for sure but this is a likely option unless we're
343 * under attack - this is only informative anyway */
344 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200345 goto cleanup;
346 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200347
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200348 /* Decrypt and authenticate */
Hanno Beckerd140d082018-11-17 21:18:01 +0000349 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx,
350 iv, TICKET_IV_BYTES,
351 /* Additional data: key name, IV and length */
352 key_name, TICKET_ADD_DATA_LEN,
353 ticket, enc_len,
354 ticket, &clear_len,
355 tag, TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200356 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200357 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
358 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
359
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200360 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200361 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200362 if( clear_len != enc_len )
363 {
364 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200365 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200366 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200367
368 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200369 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200370 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200371
372#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200373 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200374 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100375 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200376
377 if( current_time < session->start ||
378 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
379 {
380 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
381 goto cleanup;
382 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200383 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200384#endif
385
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200386cleanup:
387#if defined(MBEDTLS_THREADING_C)
388 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
389 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
390#endif
391
392 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200393}
394
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200395/*
396 * Free context
397 */
398void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
399{
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200400 mbedtls_cipher_free( &ctx->keys[0].ctx );
401 mbedtls_cipher_free( &ctx->keys[1].ctx );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200402
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200403#if defined(MBEDTLS_THREADING_C)
404 mbedtls_mutex_free( &ctx->mutex );
405#endif
406
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500407 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200408}
409
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200410#endif /* MBEDTLS_SSL_TICKET_C */