blob: ce4607ec2a09e35b54eea4485f22605228a76e9e [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)
Gabor Mezei2a020512022-03-10 15:15:46 +010091 psa_set_key_usage_flags( &attributes,
92 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
93 psa_set_key_algorithm( &attributes, key->alg );
94 psa_set_key_type( &attributes, key->key_type );
95 psa_set_key_bits( &attributes, key->key_bits );
96
97 ret = psa_ssl_status_to_mbedtls(
98 psa_import_key( &attributes, buf,
99 PSA_BITS_TO_BYTES( key->key_bits ),
100 &key->key ) );
Gabor Mezei2a020512022-03-10 15:15:46 +0100101#else
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200102 /* With GCM and CCM, same context can encrypt & decrypt */
103 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200104 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200105 MBEDTLS_ENCRYPT );
Gabor Mezei2a020512022-03-10 15:15:46 +0100106#endif /* MBEDTLS_USE_PSA_CRYPTO */
107
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500108 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200109
110 return( ret );
111}
112
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200113/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200114 * Rotate/generate keys if necessary
115 */
116static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
117{
118#if !defined(MBEDTLS_HAVE_TIME)
119 ((void) ctx);
120#else
121 if( ctx->ticket_lifetime != 0 )
122 {
SimonBd5800b72016-04-26 07:43:27 +0100123 uint32_t current_time = (uint32_t) mbedtls_time( NULL );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200124 uint32_t key_time = ctx->keys[ctx->active].generation_time;
125
Gabor Mezei2a020512022-03-10 15:15:46 +0100126#if defined(MBEDTLS_USE_PSA_CRYPTO)
127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
128#endif
129
Hanno Beckeraa715002018-08-21 13:55:31 +0100130 if( current_time >= key_time &&
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200131 current_time - key_time < ctx->ticket_lifetime )
132 {
133 return( 0 );
134 }
135
136 ctx->active = 1 - ctx->active;
137
Gabor Mezei2a020512022-03-10 15:15:46 +0100138#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100139 if( ( ret = psa_ssl_status_to_mbedtls(
140 psa_destroy_key( ctx->keys[ctx->active].key ) ) ) != 0 )
141 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100142#endif /* MBEDTLS_USE_PSA_CRYPTO */
143
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200144 return( ssl_ticket_gen_key( ctx, ctx->active ) );
145 }
146 else
147#endif /* MBEDTLS_HAVE_TIME */
148 return( 0 );
149}
150
151/*
Glenn Straussa9509382022-02-02 23:32:18 -0500152 * Rotate active session ticket encryption key
153 */
154int mbedtls_ssl_ticket_rotate( mbedtls_ssl_ticket_context *ctx,
155 const unsigned char *name, size_t nlength,
156 const unsigned char *k, size_t klength,
157 uint32_t lifetime )
158{
159 const unsigned char idx = 1 - ctx->active;
160 mbedtls_ssl_ticket_key * const key = ctx->keys + idx;
Gabor Mezei2a020512022-03-10 15:15:46 +0100161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
162
163#if defined(MBEDTLS_USE_PSA_CRYPTO)
164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gabor Mezei36c9f512022-03-16 12:55:32 +0100165 const size_t bitlen = key->key_bits;
Gabor Mezei2a020512022-03-10 15:15:46 +0100166#else
Glenn Straussa9509382022-02-02 23:32:18 -0500167 const int bitlen = mbedtls_cipher_get_key_bitlen( &key->ctx );
Gabor Mezei2a020512022-03-10 15:15:46 +0100168#endif
169
Glenn Straussa9509382022-02-02 23:32:18 -0500170 if( nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t)bitlen )
171 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
172
Gabor Mezei2a020512022-03-10 15:15:46 +0100173#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100174 if( ( ret = psa_ssl_status_to_mbedtls(
175 psa_destroy_key( key->key ) ) ) != 0 )
176 return( ret );
177
178 psa_set_key_usage_flags( &attributes,
179 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
180 psa_set_key_algorithm( &attributes, key->alg );
181 psa_set_key_type( &attributes, key->key_type );
182 psa_set_key_bits( &attributes, key->key_bits );
183
184 if( ( ret = psa_ssl_status_to_mbedtls(
185 psa_import_key( &attributes, k,
186 PSA_BITS_TO_BYTES( key->key_bits ),
187 &key->key ) ) ) != 0 )
188 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100189#else
Glenn Straussa9509382022-02-02 23:32:18 -0500190 ret = mbedtls_cipher_setkey( &key->ctx, k, bitlen, MBEDTLS_ENCRYPT );
191 if( ret != 0 )
192 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100193#endif /* MBEDTLS_USE_PSA_CRYPTO */
194
Glenn Straussa9509382022-02-02 23:32:18 -0500195 ctx->active = idx;
196 ctx->ticket_lifetime = lifetime;
197 memcpy( key->name, name, TICKET_KEY_NAME_BYTES );
198#if defined(MBEDTLS_HAVE_TIME)
199 key->generation_time = (uint32_t) mbedtls_time( NULL );
200#endif
201 return 0;
202}
203
204/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200205 * Setup context for actual use
206 */
207int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
208 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200209 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200210 uint32_t lifetime )
211{
Janos Follath865b3eb2019-12-16 11:46:15 +0000212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200213 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200214
Gabor Mezei2a020512022-03-10 15:15:46 +0100215#if defined(MBEDTLS_USE_PSA_CRYPTO)
216 psa_algorithm_t alg;
217 psa_key_type_t key_type;
218 size_t key_bits;
219#endif
220
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200221 ctx->f_rng = f_rng;
222 ctx->p_rng = p_rng;
223
224 ctx->ticket_lifetime = lifetime;
225
Gabor Mezeie6d867f2022-03-10 15:04:58 +0100226 cipher_info = mbedtls_cipher_info_from_type( cipher );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200227
Gilles Peskinee720dbe2021-07-19 17:37:46 +0200228 if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM &&
Gabor Mezei49c8eb32022-03-10 16:13:17 +0100229 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CCM &&
230 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CHACHAPOLY )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200231 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200232 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200233 }
234
Gilles Peskinee720dbe2021-07-19 17:37:46 +0200235 if( mbedtls_cipher_info_get_key_bitlen( cipher_info ) > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
237
Hanno Becker679d8ce2018-11-17 21:25:59 +0000238#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100239 if( mbedtls_ssl_cipher_to_psa( cipher_info->type, TICKET_AUTH_TAG_BYTES,
240 &alg, &key_type, &key_bits ) != PSA_SUCCESS )
241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
242
243 ctx->keys[0].alg = alg;
244 ctx->keys[0].key_type = key_type;
245 ctx->keys[0].key_bits = key_bits;
246
247 ctx->keys[1].alg = alg;
248 ctx->keys[1].key_type = key_type;
249 ctx->keys[1].key_bits = key_bits;
Gabor Mezei2a020512022-03-10 15:15:46 +0100250#else
Hanno Becker679d8ce2018-11-17 21:25:59 +0000251 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
252 return( ret );
253
Hanno Becker679d8ce2018-11-17 21:25:59 +0000254 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
255 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100256#endif /* MBEDTLS_USE_PSA_CRYPTO */
257
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200258 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
259 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200260 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200261 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200262 }
263
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200264 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200265}
266
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200267/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200268 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200269 *
270 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200271 * opaque key_name[4];
272 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200273 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200274 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200275 * } ticket;
276 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200277 * The key_name, iv, and length of encrypted_state are the additional
278 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200279 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000280
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200281int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200282 const mbedtls_ssl_session *session,
283 unsigned char *start,
284 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200285 size_t *tlen,
286 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200287{
Janos Follath865b3eb2019-12-16 11:46:15 +0000288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200289 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200290 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200291 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000292 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
293 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
294 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200295 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200296
297 *tlen = 0;
298
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200299 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200300 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
301
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200302 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
303 * in addition to session itself, that will be checked when writing it. */
Hanno Becker261602c2017-04-12 14:54:42 +0100304 MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200305
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200306#if defined(MBEDTLS_THREADING_C)
307 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
308 return( ret );
309#endif
310
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200311 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
312 goto cleanup;
313
314 key = &ctx->keys[ctx->active];
315
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200316 *ticket_lifetime = ctx->ticket_lifetime;
317
Hanno Beckerd140d082018-11-17 21:18:01 +0000318 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200319
Hanno Beckerd140d082018-11-17 21:18:01 +0000320 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200321 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200322
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200323 /* Dump session state */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200324 if( ( ret = mbedtls_ssl_session_save( session,
325 state, end - state,
326 &clear_len ) ) != 0 ||
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200327 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200328 {
329 goto cleanup;
330 }
Joe Subbiani6dd73642021-07-19 11:56:54 +0100331 MBEDTLS_PUT_UINT16_BE( clear_len, state_len_bytes, 0 );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200332
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200333 /* Encrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100334#if defined(MBEDTLS_USE_PSA_CRYPTO)
335 if( ( ret = psa_ssl_status_to_mbedtls(
336 psa_aead_encrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
337 key_name, TICKET_ADD_DATA_LEN,
338 state, clear_len,
339 state, end - state, &ciph_len ) ) ) != 0 )
340#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100341 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000342 iv, TICKET_IV_BYTES,
343 /* Additional data: key name, IV and length */
344 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100345 state, clear_len,
346 state, end - state, &ciph_len,
347 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Gabor Mezei2a020512022-03-10 15:15:46 +0100348#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200349 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200350 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200351 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100352
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100353 if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200354 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200355 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200356 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200357 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200358
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100359 *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200360
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200361cleanup:
362#if defined(MBEDTLS_THREADING_C)
363 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
364 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
365#endif
366
367 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200368}
369
370/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200371 * Select key based on name
372 */
373static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
374 mbedtls_ssl_ticket_context *ctx,
375 const unsigned char name[4] )
376{
377 unsigned char i;
378
379 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
380 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
381 return( &ctx->keys[i] );
382
383 return( NULL );
384}
385
386/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200387 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
388 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200389int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200390 mbedtls_ssl_session *session,
391 unsigned char *buf,
392 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200393{
Janos Follath865b3eb2019-12-16 11:46:15 +0000394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200395 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200396 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200397 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000398 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
399 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
400 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200401 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200402
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200403 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200404 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200405
Hanno Beckerd140d082018-11-17 21:18:01 +0000406 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200407 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200408
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200409#if defined(MBEDTLS_THREADING_C)
410 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
411 return( ret );
412#endif
413
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200414 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
415 goto cleanup;
416
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200417 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200418
Hanno Beckerd140d082018-11-17 21:18:01 +0000419 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200420 {
421 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
422 goto cleanup;
423 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200424
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200425 /* Select key */
426 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200427 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200428 /* We can't know for sure but this is a likely option unless we're
429 * under attack - this is only informative anyway */
430 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200431 goto cleanup;
432 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200433
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200434 /* Decrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100435#if defined(MBEDTLS_USE_PSA_CRYPTO)
436 if( ( ret = psa_ssl_status_to_mbedtls(
437 psa_aead_decrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
438 key_name, TICKET_ADD_DATA_LEN,
439 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
440 ticket, enc_len, &clear_len ) ) ) != 0 )
441#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100442 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000443 iv, TICKET_IV_BYTES,
444 /* Additional data: key name, IV and length */
445 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100446 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
447 ticket, enc_len, &clear_len,
448 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Gabor Mezei2a020512022-03-10 15:15:46 +0100449#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200450 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200451 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
452 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
453
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200454 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200455 }
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200456 if( clear_len != enc_len )
457 {
458 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200459 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200460 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200461
462 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200463 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200464 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200465
466#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200467 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200468 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100469 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200470
471 if( current_time < session->start ||
472 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
473 {
474 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
475 goto cleanup;
476 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200477 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200478#endif
479
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200480cleanup:
481#if defined(MBEDTLS_THREADING_C)
482 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
483 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
484#endif
485
486 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200487}
488
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200489/*
490 * Free context
491 */
492void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
493{
Gabor Mezei2a020512022-03-10 15:15:46 +0100494#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100495 psa_destroy_key( ctx->keys[0].key );
496 psa_destroy_key( ctx->keys[1].key );
Gabor Mezei2a020512022-03-10 15:15:46 +0100497#else
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200498 mbedtls_cipher_free( &ctx->keys[0].ctx );
499 mbedtls_cipher_free( &ctx->keys[1].ctx );
Gabor Mezei2a020512022-03-10 15:15:46 +0100500#endif /* MBEDTLS_USE_PSA_CRYPTO */
501
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200502#if defined(MBEDTLS_THREADING_C)
503 mbedtls_mutex_free( &ctx->mutex );
504#endif
505
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500506 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200507}
508
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200509#endif /* MBEDTLS_SSL_TICKET_C */