blob: e39563bc4f706b75e8e0e0a64c7fcfa0f0f2819b [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#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020025
Chris Jones84a773f2021-03-05 18:38:47 +000026#include "ssl_misc.h"
SimonBd5800b72016-04-26 07:43:27 +010027#include "mbedtls/ssl_ticket.h"
Janos Follath865b3eb2019-12-16 11:46:15 +000028#include "mbedtls/error.h"
Janos Follath73c616b2019-12-18 15:07:04 +000029#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010030
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020031#include <string.h>
32
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020033/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -080034 * Initialize context
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020035 */
36void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
37{
38 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020039
40#if defined(MBEDTLS_THREADING_C)
41 mbedtls_mutex_init( &ctx->mutex );
42#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020043}
44
Glenn Straussa941b622022-02-09 15:24:56 -050045#define MAX_KEY_BYTES MBEDTLS_SSL_TICKET_MAX_KEY_BYTES
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020046
Glenn Straussa941b622022-02-09 15:24:56 -050047#define TICKET_KEY_NAME_BYTES MBEDTLS_SSL_TICKET_KEY_NAME_BYTES
Hanno Beckerd140d082018-11-17 21:18:01 +000048#define TICKET_IV_BYTES 12
49#define TICKET_CRYPT_LEN_BYTES 2
50#define TICKET_AUTH_TAG_BYTES 16
51
52#define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \
53 TICKET_IV_BYTES + \
54 TICKET_CRYPT_LEN_BYTES + \
55 TICKET_AUTH_TAG_BYTES )
56#define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \
57 TICKET_IV_BYTES + \
58 TICKET_CRYPT_LEN_BYTES )
59
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020060/*
61 * Generate/update a key
62 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020063MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020064static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
65 unsigned char index )
66{
Janos Follath865b3eb2019-12-16 11:46:15 +000067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Leonid Rozenboima3008e72022-04-21 17:28:18 -070068 unsigned char buf[MAX_KEY_BYTES] = {0};
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020069 mbedtls_ssl_ticket_key *key = ctx->keys + index;
70
Gabor Mezei2a020512022-03-10 15:15:46 +010071#if defined(MBEDTLS_USE_PSA_CRYPTO)
72 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
73#endif
74
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020075#if defined(MBEDTLS_HAVE_TIME)
Dave Rodgman392f7142022-08-17 11:19:41 +010076 key->generation_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020077#endif
78
79 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
80 return( ret );
81
82 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
83 return( ret );
84
Gabor Mezei2a020512022-03-10 15:15:46 +010085#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +010086 psa_set_key_usage_flags( &attributes,
87 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
88 psa_set_key_algorithm( &attributes, key->alg );
89 psa_set_key_type( &attributes, key->key_type );
90 psa_set_key_bits( &attributes, key->key_bits );
91
92 ret = psa_ssl_status_to_mbedtls(
93 psa_import_key( &attributes, buf,
94 PSA_BITS_TO_BYTES( key->key_bits ),
95 &key->key ) );
Gabor Mezei2a020512022-03-10 15:15:46 +010096#else
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020097 /* With GCM and CCM, same context can encrypt & decrypt */
98 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +020099 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200100 MBEDTLS_ENCRYPT );
Gabor Mezei2a020512022-03-10 15:15:46 +0100101#endif /* MBEDTLS_USE_PSA_CRYPTO */
102
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500103 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200104
105 return( ret );
106}
107
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200108/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200109 * Rotate/generate keys if necessary
110 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200111MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200112static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
113{
114#if !defined(MBEDTLS_HAVE_TIME)
115 ((void) ctx);
116#else
117 if( ctx->ticket_lifetime != 0 )
118 {
Dave Rodgman392f7142022-08-17 11:19:41 +0100119 mbedtls_time_t current_time = mbedtls_time( NULL );
120 mbedtls_time_t key_time = ctx->keys[ctx->active].generation_time;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200121
Gabor Mezei2a020512022-03-10 15:15:46 +0100122#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100123 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei2a020512022-03-10 15:15:46 +0100124#endif
125
Hanno Beckeraa715002018-08-21 13:55:31 +0100126 if( current_time >= key_time &&
Dave Rodgman86c333e2022-08-17 16:57:26 +0100127 (uint64_t) ( current_time - key_time ) < ctx->ticket_lifetime )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200128 {
129 return( 0 );
130 }
131
132 ctx->active = 1 - ctx->active;
133
Gabor Mezei2a020512022-03-10 15:15:46 +0100134#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100135 if( ( status = psa_destroy_key( ctx->keys[ctx->active].key ) ) != PSA_SUCCESS )
136 {
Gabor Mezei103e08a2022-03-16 13:40:11 +0100137 return psa_ssl_status_to_mbedtls( status );
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100138 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100139#endif /* MBEDTLS_USE_PSA_CRYPTO */
140
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200141 return( ssl_ticket_gen_key( ctx, ctx->active ) );
142 }
143 else
144#endif /* MBEDTLS_HAVE_TIME */
145 return( 0 );
146}
147
148/*
Glenn Straussa9509382022-02-02 23:32:18 -0500149 * Rotate active session ticket encryption key
150 */
151int mbedtls_ssl_ticket_rotate( mbedtls_ssl_ticket_context *ctx,
152 const unsigned char *name, size_t nlength,
153 const unsigned char *k, size_t klength,
154 uint32_t lifetime )
155{
156 const unsigned char idx = 1 - ctx->active;
157 mbedtls_ssl_ticket_key * const key = ctx->keys + idx;
Gabor Mezei2a020512022-03-10 15:15:46 +0100158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
159
160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei2a020512022-03-10 15:15:46 +0100162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gabor Mezei36c9f512022-03-16 12:55:32 +0100163 const size_t bitlen = key->key_bits;
Gabor Mezei2a020512022-03-10 15:15:46 +0100164#else
Glenn Straussa9509382022-02-02 23:32:18 -0500165 const int bitlen = mbedtls_cipher_get_key_bitlen( &key->ctx );
Gabor Mezei2a020512022-03-10 15:15:46 +0100166#endif
167
Glenn Straussa9509382022-02-02 23:32:18 -0500168 if( nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t)bitlen )
169 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
170
Gabor Mezei2a020512022-03-10 15:15:46 +0100171#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100172 if( ( status = psa_destroy_key( key->key ) ) != PSA_SUCCESS )
173 {
174 ret = psa_ssl_status_to_mbedtls( status );
Gabor Mezei2a020512022-03-10 15:15:46 +0100175 return( ret );
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100176 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100177
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
Gabor Mezei103e08a2022-03-16 13:40:11 +0100184 if( ( status = psa_import_key( &attributes, k,
185 PSA_BITS_TO_BYTES( key->key_bits ),
186 &key->key ) ) != PSA_SUCCESS )
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100187 {
188 ret = psa_ssl_status_to_mbedtls( status );
Gabor Mezei2a020512022-03-10 15:15:46 +0100189 return( ret );
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100190 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100191#else
Glenn Straussa9509382022-02-02 23:32:18 -0500192 ret = mbedtls_cipher_setkey( &key->ctx, k, bitlen, MBEDTLS_ENCRYPT );
193 if( ret != 0 )
194 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100195#endif /* MBEDTLS_USE_PSA_CRYPTO */
196
Glenn Straussa9509382022-02-02 23:32:18 -0500197 ctx->active = idx;
198 ctx->ticket_lifetime = lifetime;
199 memcpy( key->name, name, TICKET_KEY_NAME_BYTES );
200#if defined(MBEDTLS_HAVE_TIME)
Dave Rodgman392f7142022-08-17 11:19:41 +0100201 key->generation_time = mbedtls_time( NULL );
Glenn Straussa9509382022-02-02 23:32:18 -0500202#endif
203 return 0;
204}
205
206/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200207 * Setup context for actual use
208 */
209int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
210 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200211 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200212 uint32_t lifetime )
213{
Janos Follath865b3eb2019-12-16 11:46:15 +0000214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200215 size_t key_bits;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200216
Gabor Mezei2a020512022-03-10 15:15:46 +0100217#if defined(MBEDTLS_USE_PSA_CRYPTO)
218 psa_algorithm_t alg;
219 psa_key_type_t key_type;
Neil Armstrong858581e2022-04-01 18:03:15 +0200220#else
221 const mbedtls_cipher_info_t *cipher_info;
222#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gabor Mezei2a020512022-03-10 15:15:46 +0100223
Neil Armstrong858581e2022-04-01 18:03:15 +0200224#if defined(MBEDTLS_USE_PSA_CRYPTO)
225 if( mbedtls_ssl_cipher_to_psa( cipher, TICKET_AUTH_TAG_BYTES,
226 &alg, &key_type, &key_bits ) != PSA_SUCCESS )
227 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
228
229 if( PSA_ALG_IS_AEAD( alg ) == 0 )
230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrong858581e2022-04-01 18:03:15 +0200231#else
Gabor Mezeie6d867f2022-03-10 15:04:58 +0100232 cipher_info = mbedtls_cipher_info_from_type( cipher );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200233
Gilles Peskinee720dbe2021-07-19 17:37:46 +0200234 if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM &&
Gabor Mezei49c8eb32022-03-10 16:13:17 +0100235 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CCM &&
236 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CHACHAPOLY )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200237 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200239 }
240
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200241 key_bits = mbedtls_cipher_info_get_key_bitlen( cipher_info );
242#endif /* MBEDTLS_USE_PSA_CRYPTO */
243
244 if( key_bits > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200245 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
246
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200247 ctx->f_rng = f_rng;
248 ctx->p_rng = p_rng;
249
250 ctx->ticket_lifetime = lifetime;
251
252#if defined(MBEDTLS_USE_PSA_CRYPTO)
253 ctx->keys[0].alg = alg;
254 ctx->keys[0].key_type = key_type;
255 ctx->keys[0].key_bits = key_bits;
256
257 ctx->keys[1].alg = alg;
258 ctx->keys[1].key_type = key_type;
259 ctx->keys[1].key_bits = key_bits;
260#else
Hanno Becker679d8ce2018-11-17 21:25:59 +0000261 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
262 return( ret );
263
Hanno Becker679d8ce2018-11-17 21:25:59 +0000264 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
265 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100266#endif /* MBEDTLS_USE_PSA_CRYPTO */
267
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200268 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
269 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200270 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200271 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200272 }
273
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200274 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200275}
276
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200277/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200278 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200279 *
280 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200281 * opaque key_name[4];
282 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200283 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200284 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200285 * } ticket;
286 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200287 * The key_name, iv, and length of encrypted_state are the additional
288 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200289 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000290
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200291int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200292 const mbedtls_ssl_session *session,
293 unsigned char *start,
294 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200295 size_t *tlen,
296 uint32_t *ticket_lifetime )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200297{
Janos Follath865b3eb2019-12-16 11:46:15 +0000298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200299 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200300 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200301 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000302 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
303 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
304 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200305 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200306
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100307#if defined(MBEDTLS_USE_PSA_CRYPTO)
308 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
309#endif
310
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200311 *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)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100349 if( ( status = psa_aead_encrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
350 key_name, TICKET_ADD_DATA_LEN,
351 state, clear_len,
352 state, end - state,
353 &ciph_len ) ) != PSA_SUCCESS )
354 {
355 ret = psa_ssl_status_to_mbedtls( status );
356 goto cleanup;
357 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100358#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100359 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000360 iv, TICKET_IV_BYTES,
361 /* Additional data: key name, IV and length */
362 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100363 state, clear_len,
364 state, end - state, &ciph_len,
365 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200366 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200367 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200368 }
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100369#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gabor Mezei2a020512022-03-10 15:15:46 +0100370
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100371 if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200372 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200373 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200374 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200375 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200376
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100377 *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200378
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200379cleanup:
380#if defined(MBEDTLS_THREADING_C)
381 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
382 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
383#endif
384
385 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200386}
387
388/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200389 * Select key based on name
390 */
391static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
392 mbedtls_ssl_ticket_context *ctx,
393 const unsigned char name[4] )
394{
395 unsigned char i;
396
397 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
398 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
399 return( &ctx->keys[i] );
400
401 return( NULL );
402}
403
404/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200405 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
406 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200407int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200408 mbedtls_ssl_session *session,
409 unsigned char *buf,
410 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200411{
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200413 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200414 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200415 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000416 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
417 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
418 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200419 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200420
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100421#if defined(MBEDTLS_USE_PSA_CRYPTO)
422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
423#endif
424
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200425 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200426 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200427
Hanno Beckerd140d082018-11-17 21:18:01 +0000428 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200429 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200430
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200431#if defined(MBEDTLS_THREADING_C)
432 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
433 return( ret );
434#endif
435
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200436 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
437 goto cleanup;
438
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200439 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200440
Hanno Beckerd140d082018-11-17 21:18:01 +0000441 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200442 {
443 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
444 goto cleanup;
445 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200446
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200447 /* Select key */
448 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200449 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200450 /* We can't know for sure but this is a likely option unless we're
451 * under attack - this is only informative anyway */
452 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200453 goto cleanup;
454 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200455
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200456 /* Decrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100457#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100458 if( ( status = psa_aead_decrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
459 key_name, TICKET_ADD_DATA_LEN,
460 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
461 ticket, enc_len, &clear_len ) ) != PSA_SUCCESS )
462 {
463 ret = psa_ssl_status_to_mbedtls( status );
464 goto cleanup;
465 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100466#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100467 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000468 iv, TICKET_IV_BYTES,
469 /* Additional data: key name, IV and length */
470 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100471 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
472 ticket, enc_len, &clear_len,
473 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200474 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200475 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
476 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
477
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200478 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200479 }
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100480#endif /* MBEDTLS_USE_PSA_CRYPTO */
481
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200482 if( clear_len != enc_len )
483 {
484 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200485 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200486 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200487
488 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200489 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200490 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200491
492#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200493 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200494 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100495 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200496
497 if( current_time < session->start ||
498 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
499 {
500 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
501 goto cleanup;
502 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200503 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200504#endif
505
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200506cleanup:
507#if defined(MBEDTLS_THREADING_C)
508 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
509 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
510#endif
511
512 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200513}
514
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200515/*
516 * Free context
517 */
518void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
519{
Gabor Mezei2a020512022-03-10 15:15:46 +0100520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100521 psa_destroy_key( ctx->keys[0].key );
522 psa_destroy_key( ctx->keys[1].key );
Gabor Mezei2a020512022-03-10 15:15:46 +0100523#else
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200524 mbedtls_cipher_free( &ctx->keys[0].ctx );
525 mbedtls_cipher_free( &ctx->keys[1].ctx );
Gabor Mezei2a020512022-03-10 15:15:46 +0100526#endif /* MBEDTLS_USE_PSA_CRYPTO */
527
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200528#if defined(MBEDTLS_THREADING_C)
529 mbedtls_mutex_free( &ctx->mutex );
530#endif
531
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500532 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200533}
534
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200535#endif /* MBEDTLS_SSL_TICKET_C */