blob: ed9c7a5b4c2c546e4ff919074369a4cb564a11ac [file] [log] [blame]
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02001/*
2 * TLS server tickets callbacks implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020021
22#if defined(MBEDTLS_SSL_TICKET_C)
23
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020024#if defined(MBEDTLS_PLATFORM_C)
25#include "mbedtls/platform.h"
26#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020027#include <stdlib.h>
28#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010029#define mbedtls_free free
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020030#endif
31
Chris Jones84a773f2021-03-05 18:38:47 +000032#include "ssl_misc.h"
SimonBd5800b72016-04-26 07:43:27 +010033#include "mbedtls/ssl_ticket.h"
Janos Follath865b3eb2019-12-16 11:46:15 +000034#include "mbedtls/error.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010036
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020037#include <string.h>
38
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020039/*
40 * Initialze context
41 */
42void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
43{
44 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020045
46#if defined(MBEDTLS_THREADING_C)
47 mbedtls_mutex_init( &ctx->mutex );
48#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020049}
50
Glenn Straussa941b622022-02-09 15:24:56 -050051#define MAX_KEY_BYTES MBEDTLS_SSL_TICKET_MAX_KEY_BYTES
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020052
Glenn Straussa941b622022-02-09 15:24:56 -050053#define TICKET_KEY_NAME_BYTES MBEDTLS_SSL_TICKET_KEY_NAME_BYTES
Hanno Beckerd140d082018-11-17 21:18:01 +000054#define TICKET_IV_BYTES 12
55#define TICKET_CRYPT_LEN_BYTES 2
56#define TICKET_AUTH_TAG_BYTES 16
57
58#define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
59 TICKET_IV_BYTES + \
60 TICKET_CRYPT_LEN_BYTES + \
61 TICKET_AUTH_TAG_BYTES )
62#define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
63 TICKET_IV_BYTES + \
64 TICKET_CRYPT_LEN_BYTES )
65
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020066/*
67 * Generate/update a key
68 */
69static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
70 unsigned char index )
71{
Janos Follath865b3eb2019-12-16 11:46:15 +000072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020073 unsigned char buf[MAX_KEY_BYTES];
74 mbedtls_ssl_ticket_key *key = ctx->keys + index;
75
Gabor Mezei2a020512022-03-10 15:15:46 +010076#if defined(MBEDTLS_USE_PSA_CRYPTO)
77 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
78#endif
79
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020080#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
Gabor Mezei2a020512022-03-10 15:15:46 +010090#if defined(MBEDTLS_USE_PSA_CRYPTO)
91
92 psa_set_key_usage_flags( &attributes,
93 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
94 psa_set_key_algorithm( &attributes, key->alg );
95 psa_set_key_type( &attributes, key->key_type );
96 psa_set_key_bits( &attributes, key->key_bits );
97
98 ret = psa_ssl_status_to_mbedtls(
99 psa_import_key( &attributes, buf,
100 PSA_BITS_TO_BYTES( key->key_bits ),
101 &key->key ) );
102
103#else
104
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200105 /* With GCM and CCM, same context can encrypt & decrypt */
106 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200107 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200108 MBEDTLS_ENCRYPT );
109
Gabor Mezei2a020512022-03-10 15:15:46 +0100110#endif /* MBEDTLS_USE_PSA_CRYPTO */
111
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500112 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200113
114 return( ret );
115}
116
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200117/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200118 * Rotate/generate keys if necessary
119 */
120static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
121{
122#if !defined(MBEDTLS_HAVE_TIME)
123 ((void) ctx);
124#else
125 if( ctx->ticket_lifetime != 0 )
126 {
SimonBd5800b72016-04-26 07:43:27 +0100127 uint32_t current_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200128 uint32_t key_time = ctx->keys[ctx->active].generation_time;
129
Gabor Mezei2a020512022-03-10 15:15:46 +0100130#if defined(MBEDTLS_USE_PSA_CRYPTO)
131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
132#endif
133
Hanno Beckeraa715002018-08-21 13:55:31 +0100134 if( current_time >= key_time &&
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200135 current_time - key_time < ctx->ticket_lifetime )
136 {
137 return( 0 );
138 }
139
140 ctx->active = 1 - ctx->active;
141
Gabor Mezei2a020512022-03-10 15:15:46 +0100142#if defined(MBEDTLS_USE_PSA_CRYPTO)
143
144 if( ( ret = psa_ssl_status_to_mbedtls(
145 psa_destroy_key( ctx->keys[ctx->active].key ) ) ) != 0 )
146 return( ret );
147
148#endif /* MBEDTLS_USE_PSA_CRYPTO */
149
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200150 return( ssl_ticket_gen_key( ctx, ctx->active ) );
151 }
152 else
153#endif /* MBEDTLS_HAVE_TIME */
154 return( 0 );
155}
156
157/*
Glenn Straussa9509382022-02-02 23:32:18 -0500158 * Rotate active session ticket encryption key
159 */
160int mbedtls_ssl_ticket_rotate( mbedtls_ssl_ticket_context *ctx,
161 const unsigned char *name, size_t nlength,
162 const unsigned char *k, size_t klength,
163 uint32_t lifetime )
164{
165 const unsigned char idx = 1 - ctx->active;
166 mbedtls_ssl_ticket_key * const key = ctx->keys + idx;
Gabor Mezei2a020512022-03-10 15:15:46 +0100167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
168
169#if defined(MBEDTLS_USE_PSA_CRYPTO)
170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
171 const int bitlen = key->key_bits;
172#else
Glenn Straussa9509382022-02-02 23:32:18 -0500173 const int bitlen = mbedtls_cipher_get_key_bitlen( &key->ctx );
Gabor Mezei2a020512022-03-10 15:15:46 +0100174#endif
175
Glenn Straussa9509382022-02-02 23:32:18 -0500176 if( nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t)bitlen )
177 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
178
Gabor Mezei2a020512022-03-10 15:15:46 +0100179#if defined(MBEDTLS_USE_PSA_CRYPTO)
180
181 if( ( ret = psa_ssl_status_to_mbedtls(
182 psa_destroy_key( key->key ) ) ) != 0 )
183 return( ret );
184
185 psa_set_key_usage_flags( &attributes,
186 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
187 psa_set_key_algorithm( &attributes, key->alg );
188 psa_set_key_type( &attributes, key->key_type );
189 psa_set_key_bits( &attributes, key->key_bits );
190
191 if( ( ret = psa_ssl_status_to_mbedtls(
192 psa_import_key( &attributes, k,
193 PSA_BITS_TO_BYTES( key->key_bits ),
194 &key->key ) ) ) != 0 )
195 return( ret );
196
197#else
198
Glenn Straussa9509382022-02-02 23:32:18 -0500199 ret = mbedtls_cipher_setkey( &key->ctx, k, bitlen, MBEDTLS_ENCRYPT );
200 if( ret != 0 )
201 return( ret );
202
Gabor Mezei2a020512022-03-10 15:15:46 +0100203#endif /* MBEDTLS_USE_PSA_CRYPTO */
204
Glenn Straussa9509382022-02-02 23:32:18 -0500205 ctx->active = idx;
206 ctx->ticket_lifetime = lifetime;
207 memcpy( key->name, name, TICKET_KEY_NAME_BYTES );
208#if defined(MBEDTLS_HAVE_TIME)
209 key->generation_time = (uint32_t) mbedtls_time( NULL );
210#endif
211 return 0;
212}
213
214/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200215 * Setup context for actual use
216 */
217int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
218 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200219 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200220 uint32_t lifetime )
221{
Janos Follath865b3eb2019-12-16 11:46:15 +0000222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200223 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200224
Gabor Mezei2a020512022-03-10 15:15:46 +0100225#if defined(MBEDTLS_USE_PSA_CRYPTO)
226 psa_algorithm_t alg;
227 psa_key_type_t key_type;
228 size_t key_bits;
229#endif
230
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200231 ctx->f_rng = f_rng;
232 ctx->p_rng = p_rng;
233
234 ctx->ticket_lifetime = lifetime;
235
Gabor Mezeie6d867f2022-03-10 15:04:58 +0100236 cipher_info = mbedtls_cipher_info_from_type( cipher );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200237
Gilles Peskinee720dbe2021-07-19 17:37:46 +0200238 if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM &&
Gabor Mezei49c8eb32022-03-10 16:13:17 +0100239 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CCM &&
240 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CHACHAPOLY )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200241 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200243 }
244
Gilles Peskinee720dbe2021-07-19 17:37:46 +0200245 if( mbedtls_cipher_info_get_key_bitlen( cipher_info ) > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
247
Hanno Becker679d8ce2018-11-17 21:25:59 +0000248#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100249
250 if( mbedtls_ssl_cipher_to_psa( cipher_info->type, TICKET_AUTH_TAG_BYTES,
251 &alg, &key_type, &key_bits ) != PSA_SUCCESS )
252 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
253
254 ctx->keys[0].alg = alg;
255 ctx->keys[0].key_type = key_type;
256 ctx->keys[0].key_bits = key_bits;
257
258 ctx->keys[1].alg = alg;
259 ctx->keys[1].key_type = key_type;
260 ctx->keys[1].key_bits = key_bits;
261
262#else
263
Hanno Becker679d8ce2018-11-17 21:25:59 +0000264 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
265 return( ret );
266
Hanno Becker679d8ce2018-11-17 21:25:59 +0000267 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
268 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200269
Gabor Mezei2a020512022-03-10 15:15:46 +0100270#endif /* MBEDTLS_USE_PSA_CRYPTO */
271
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200272 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
273 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200274 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200275 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200276 }
277
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200278 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200279}
280
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200281/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200282 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200283 *
284 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200285 * opaque key_name[4];
286 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200287 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200288 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200289 * } ticket;
290 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200291 * The key_name, iv, and length of encrypted_state are the additional
292 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200293 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000294
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200295int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200296 const mbedtls_ssl_session *session,
297 unsigned char *start,
298 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200299 size_t *tlen,
300 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200301{
Janos Follath865b3eb2019-12-16 11:46:15 +0000302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200303 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200304 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200305 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000306 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
307 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
308 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200309 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200310
311 *tlen = 0;
312
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200313 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
315
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200316 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
317 * in addition to session itself, that will be checked when writing it. */
Hanno Becker261602c2017-04-12 14:54:42 +0100318 MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200319
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200320#if defined(MBEDTLS_THREADING_C)
321 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
322 return( ret );
323#endif
324
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200325 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
326 goto cleanup;
327
328 key = &ctx->keys[ctx->active];
329
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200330 *ticket_lifetime = ctx->ticket_lifetime;
331
Hanno Beckerd140d082018-11-17 21:18:01 +0000332 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200333
Hanno Beckerd140d082018-11-17 21:18:01 +0000334 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200335 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200336
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200337 /* Dump session state */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200338 if( ( ret = mbedtls_ssl_session_save( session,
339 state, end - state,
340 &clear_len ) ) != 0 ||
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200341 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200342 {
343 goto cleanup;
344 }
Joe Subbiani6dd73642021-07-19 11:56:54 +0100345 MBEDTLS_PUT_UINT16_BE( clear_len, state_len_bytes, 0 );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200346
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200347 /* Encrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100348#if defined(MBEDTLS_USE_PSA_CRYPTO)
349 if( ( ret = psa_ssl_status_to_mbedtls(
350 psa_aead_encrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
351 key_name, TICKET_ADD_DATA_LEN,
352 state, clear_len,
353 state, end - state, &ciph_len ) ) ) != 0 )
354#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100355 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000356 iv, TICKET_IV_BYTES,
357 /* Additional data: key name, IV and length */
358 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100359 state, clear_len,
360 state, end - state, &ciph_len,
361 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Gabor Mezei2a020512022-03-10 15:15:46 +0100362#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200363 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200364 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200365 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100366
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100367 if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200368 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200369 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200370 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200371 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200372
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100373 *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200374
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200375cleanup:
376#if defined(MBEDTLS_THREADING_C)
377 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
378 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
379#endif
380
381 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200382}
383
384/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200385 * Select key based on name
386 */
387static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
388 mbedtls_ssl_ticket_context *ctx,
389 const unsigned char name[4] )
390{
391 unsigned char i;
392
393 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
394 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
395 return( &ctx->keys[i] );
396
397 return( NULL );
398}
399
400/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200401 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
402 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200403int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200404 mbedtls_ssl_session *session,
405 unsigned char *buf,
406 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200407{
Janos Follath865b3eb2019-12-16 11:46:15 +0000408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200409 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200410 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200411 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000412 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
413 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
414 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200415 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200416
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200417 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200418 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200419
Hanno Beckerd140d082018-11-17 21:18:01 +0000420 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200421 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200422
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200423#if defined(MBEDTLS_THREADING_C)
424 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
425 return( ret );
426#endif
427
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200428 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
429 goto cleanup;
430
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200431 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200432
Hanno Beckerd140d082018-11-17 21:18:01 +0000433 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200434 {
435 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
436 goto cleanup;
437 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200438
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200439 /* Select key */
440 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200441 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200442 /* We can't know for sure but this is a likely option unless we're
443 * under attack - this is only informative anyway */
444 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200445 goto cleanup;
446 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200447
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200448 /* Decrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100449#if defined(MBEDTLS_USE_PSA_CRYPTO)
450 if( ( ret = psa_ssl_status_to_mbedtls(
451 psa_aead_decrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
452 key_name, TICKET_ADD_DATA_LEN,
453 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
454 ticket, enc_len, &clear_len ) ) ) != 0 )
455#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100456 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000457 iv, TICKET_IV_BYTES,
458 /* Additional data: key name, IV and length */
459 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100460 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
461 ticket, enc_len, &clear_len,
462 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Gabor Mezei2a020512022-03-10 15:15:46 +0100463#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200464 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200465 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
466 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
467
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200468 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200469 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200470 if( clear_len != enc_len )
471 {
472 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200473 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200474 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200475
476 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200477 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200478 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200479
480#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200481 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200482 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100483 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200484
485 if( current_time < session->start ||
486 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
487 {
488 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
489 goto cleanup;
490 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200491 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200492#endif
493
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200494cleanup:
495#if defined(MBEDTLS_THREADING_C)
496 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
497 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
498#endif
499
500 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200501}
502
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200503/*
504 * Free context
505 */
506void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
507{
Gabor Mezei2a020512022-03-10 15:15:46 +0100508#if defined(MBEDTLS_USE_PSA_CRYPTO)
509
510 psa_destroy_key( ctx->keys[0].key );
511 psa_destroy_key( ctx->keys[1].key );
512
513#else
514
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200515 mbedtls_cipher_free( &ctx->keys[0].ctx );
516 mbedtls_cipher_free( &ctx->keys[1].ctx );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200517
Gabor Mezei2a020512022-03-10 15:15:46 +0100518#endif /* MBEDTLS_USE_PSA_CRYPTO */
519
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200520#if defined(MBEDTLS_THREADING_C)
521 mbedtls_mutex_free( &ctx->mutex );
522#endif
523
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500524 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200525}
526
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200527#endif /* MBEDTLS_SSL_TICKET_C */