blob: 5398c3970c23affd4f3414978d09f6fa6d3d05e8 [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/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -080040 * Initialize context
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020041 */
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 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020069MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020070static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
71 unsigned char index )
72{
Janos Follath865b3eb2019-12-16 11:46:15 +000073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Leonid Rozenboima3008e72022-04-21 17:28:18 -070074 unsigned char buf[MAX_KEY_BYTES] = {0};
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020075 mbedtls_ssl_ticket_key *key = ctx->keys + index;
76
Gabor Mezei2a020512022-03-10 15:15:46 +010077#if defined(MBEDTLS_USE_PSA_CRYPTO)
78 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
79#endif
80
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020081#if defined(MBEDTLS_HAVE_TIME)
Dave Rodgman392f7142022-08-17 11:19:41 +010082 key->generation_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020083#endif
84
85 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
86 return( ret );
87
88 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
89 return( ret );
90
Gabor Mezei2a020512022-03-10 15:15:46 +010091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +010092 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 ) );
Gabor Mezei2a020512022-03-10 15:15:46 +0100102#else
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200103 /* With GCM and CCM, same context can encrypt & decrypt */
104 ret = mbedtls_cipher_setkey( &key->ctx, buf,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200105 mbedtls_cipher_get_key_bitlen( &key->ctx ),
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200106 MBEDTLS_ENCRYPT );
Gabor Mezei2a020512022-03-10 15:15:46 +0100107#endif /* MBEDTLS_USE_PSA_CRYPTO */
108
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500109 mbedtls_platform_zeroize( buf, sizeof( buf ) );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200110
111 return( ret );
112}
113
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200114/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200115 * Rotate/generate keys if necessary
116 */
Przemek Stekiela82290b2022-09-27 13:41:12 +0200117#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200118MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200119static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
120{
121#if !defined(MBEDTLS_HAVE_TIME)
122 ((void) ctx);
123#else
124 if( ctx->ticket_lifetime != 0 )
125 {
Dave Rodgman392f7142022-08-17 11:19:41 +0100126 mbedtls_time_t current_time = mbedtls_time( NULL );
127 mbedtls_time_t key_time = ctx->keys[ctx->active].generation_time;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200128
Gabor Mezei2a020512022-03-10 15:15:46 +0100129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100130 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei2a020512022-03-10 15:15:46 +0100131#endif
132
Hanno Beckeraa715002018-08-21 13:55:31 +0100133 if( current_time >= key_time &&
Dave Rodgman86c333e2022-08-17 16:57:26 +0100134 (uint64_t) ( current_time - key_time ) < ctx->ticket_lifetime )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200135 {
136 return( 0 );
137 }
138
139 ctx->active = 1 - ctx->active;
140
Gabor Mezei2a020512022-03-10 15:15:46 +0100141#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100142 if( ( status = psa_destroy_key( ctx->keys[ctx->active].key ) ) != PSA_SUCCESS )
143 {
Gabor Mezei103e08a2022-03-16 13:40:11 +0100144 return psa_ssl_status_to_mbedtls( status );
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100145 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100146#endif /* MBEDTLS_USE_PSA_CRYPTO */
147
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200148 return( ssl_ticket_gen_key( ctx, ctx->active ) );
149 }
150 else
151#endif /* MBEDTLS_HAVE_TIME */
152 return( 0 );
153}
Przemek Stekiela82290b2022-09-27 13:41:12 +0200154#endif
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200155
156/*
Glenn Straussa9509382022-02-02 23:32:18 -0500157 * Rotate active session ticket encryption key
158 */
159int mbedtls_ssl_ticket_rotate( mbedtls_ssl_ticket_context *ctx,
160 const unsigned char *name, size_t nlength,
161 const unsigned char *k, size_t klength,
162 uint32_t lifetime )
163{
164 const unsigned char idx = 1 - ctx->active;
165 mbedtls_ssl_ticket_key * const key = ctx->keys + idx;
Gabor Mezei2a020512022-03-10 15:15:46 +0100166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
167
168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100169 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei2a020512022-03-10 15:15:46 +0100170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gabor Mezei36c9f512022-03-16 12:55:32 +0100171 const size_t bitlen = key->key_bits;
Gabor Mezei2a020512022-03-10 15:15:46 +0100172#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)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100180 if( ( status = psa_destroy_key( key->key ) ) != PSA_SUCCESS )
181 {
182 ret = psa_ssl_status_to_mbedtls( status );
Gabor Mezei2a020512022-03-10 15:15:46 +0100183 return( ret );
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100184 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100185
186 psa_set_key_usage_flags( &attributes,
187 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
188 psa_set_key_algorithm( &attributes, key->alg );
189 psa_set_key_type( &attributes, key->key_type );
190 psa_set_key_bits( &attributes, key->key_bits );
191
Gabor Mezei103e08a2022-03-16 13:40:11 +0100192 if( ( status = psa_import_key( &attributes, k,
193 PSA_BITS_TO_BYTES( key->key_bits ),
194 &key->key ) ) != PSA_SUCCESS )
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100195 {
196 ret = psa_ssl_status_to_mbedtls( status );
Gabor Mezei2a020512022-03-10 15:15:46 +0100197 return( ret );
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100198 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100199#else
Glenn Straussa9509382022-02-02 23:32:18 -0500200 ret = mbedtls_cipher_setkey( &key->ctx, k, bitlen, MBEDTLS_ENCRYPT );
201 if( ret != 0 )
202 return( ret );
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)
Dave Rodgman392f7142022-08-17 11:19:41 +0100209 key->generation_time = mbedtls_time( NULL );
Glenn Straussa9509382022-02-02 23:32:18 -0500210#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;
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200223 size_t key_bits;
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;
Neil Armstrong858581e2022-04-01 18:03:15 +0200228#else
229 const mbedtls_cipher_info_t *cipher_info;
230#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gabor Mezei2a020512022-03-10 15:15:46 +0100231
Neil Armstrong858581e2022-04-01 18:03:15 +0200232#if defined(MBEDTLS_USE_PSA_CRYPTO)
233 if( mbedtls_ssl_cipher_to_psa( cipher, TICKET_AUTH_TAG_BYTES,
234 &alg, &key_type, &key_bits ) != PSA_SUCCESS )
235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
236
237 if( PSA_ALG_IS_AEAD( alg ) == 0 )
238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Neil Armstrong858581e2022-04-01 18:03:15 +0200239#else
Gabor Mezeie6d867f2022-03-10 15:04:58 +0100240 cipher_info = mbedtls_cipher_info_from_type( cipher );
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200241
Gilles Peskinee720dbe2021-07-19 17:37:46 +0200242 if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM &&
Gabor Mezei49c8eb32022-03-10 16:13:17 +0100243 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CCM &&
244 mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CHACHAPOLY )
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200245 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200247 }
248
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200249 key_bits = mbedtls_cipher_info_get_key_bitlen( cipher_info );
250#endif /* MBEDTLS_USE_PSA_CRYPTO */
251
252 if( key_bits > 8 * MAX_KEY_BYTES )
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200253 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
254
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200255 ctx->f_rng = f_rng;
256 ctx->p_rng = p_rng;
257
258 ctx->ticket_lifetime = lifetime;
259
260#if defined(MBEDTLS_USE_PSA_CRYPTO)
261 ctx->keys[0].alg = alg;
262 ctx->keys[0].key_type = key_type;
263 ctx->keys[0].key_bits = key_bits;
264
265 ctx->keys[1].alg = alg;
266 ctx->keys[1].key_type = key_type;
267 ctx->keys[1].key_bits = key_bits;
268#else
Hanno Becker679d8ce2018-11-17 21:25:59 +0000269 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 )
270 return( ret );
271
Hanno Becker679d8ce2018-11-17 21:25:59 +0000272 if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
273 return( ret );
Gabor Mezei2a020512022-03-10 15:15:46 +0100274#endif /* MBEDTLS_USE_PSA_CRYPTO */
275
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200276 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
277 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200278 {
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200279 return( ret );
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200280 }
281
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200282 return( 0 );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200283}
284
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200285/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200286 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200287 *
288 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200289 * opaque key_name[4];
290 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200291 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200292 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200293 * } ticket;
294 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200295 * The key_name, iv, and length of encrypted_state are the additional
296 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200297 */
Przemek Stekiela82290b2022-09-27 13:41:12 +0200298#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200299int mbedtls_ssl_ticket_write( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200300 const mbedtls_ssl_session *session,
301 unsigned char *start,
302 const unsigned char *end,
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200303 size_t *tlen,
304 uint32_t *ticket_lifetime )
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é-Gonnard1041a392015-05-20 19:59:39 +0200309 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000310 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
311 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
312 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200313 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200314
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100315#if defined(MBEDTLS_USE_PSA_CRYPTO)
316 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
317#endif
318
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200319 *tlen = 0;
320
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200321 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
323
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200324 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
325 * in addition to session itself, that will be checked when writing it. */
Hanno Becker261602c2017-04-12 14:54:42 +0100326 MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN );
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200327
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200328#if defined(MBEDTLS_THREADING_C)
329 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
330 return( ret );
331#endif
332
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200333 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
334 goto cleanup;
335
336 key = &ctx->keys[ctx->active];
337
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200338 *ticket_lifetime = ctx->ticket_lifetime;
339
Hanno Beckerd140d082018-11-17 21:18:01 +0000340 memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200341
Hanno Beckerd140d082018-11-17 21:18:01 +0000342 if( ( ret = ctx->f_rng( ctx->p_rng, iv, TICKET_IV_BYTES ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200343 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200344
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200345 /* Dump session state */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200346 if( ( ret = mbedtls_ssl_session_save( session,
347 state, end - state,
348 &clear_len ) ) != 0 ||
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200349 (unsigned long) clear_len > 65535 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200350 {
351 goto cleanup;
352 }
Joe Subbiani6dd73642021-07-19 11:56:54 +0100353 MBEDTLS_PUT_UINT16_BE( clear_len, state_len_bytes, 0 );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200354
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200355 /* Encrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100356#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100357 if( ( status = psa_aead_encrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
358 key_name, TICKET_ADD_DATA_LEN,
359 state, clear_len,
360 state, end - state,
361 &ciph_len ) ) != PSA_SUCCESS )
362 {
363 ret = psa_ssl_status_to_mbedtls( status );
364 goto cleanup;
365 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100366#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100367 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000368 iv, TICKET_IV_BYTES,
369 /* Additional data: key name, IV and length */
370 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100371 state, clear_len,
372 state, end - state, &ciph_len,
373 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200374 {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200375 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200376 }
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100377#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gabor Mezei2a020512022-03-10 15:15:46 +0100378
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100379 if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200380 {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200381 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200382 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200383 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200384
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100385 *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200386
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200387cleanup:
388#if defined(MBEDTLS_THREADING_C)
389 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
390 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
391#endif
392
393 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200394}
Przemek Stekiela82290b2022-09-27 13:41:12 +0200395#endif
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200396
Przemek Stekiela82290b2022-09-27 13:41:12 +0200397#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200398/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200399 * Select key based on name
400 */
401static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
402 mbedtls_ssl_ticket_context *ctx,
403 const unsigned char name[4] )
404{
405 unsigned char i;
406
407 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
408 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
409 return( &ctx->keys[i] );
410
411 return( NULL );
412}
413
414/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200415 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
416 */
Manuel Pégourié-Gonnardb0394be2015-05-19 11:40:30 +0200417int mbedtls_ssl_ticket_parse( void *p_ticket,
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200418 mbedtls_ssl_session *session,
419 unsigned char *buf,
420 size_t len )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200421{
Janos Follath865b3eb2019-12-16 11:46:15 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200423 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200424 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200425 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000426 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
427 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
428 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200429 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200430
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100431#if defined(MBEDTLS_USE_PSA_CRYPTO)
432 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
433#endif
434
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200435 if( ctx == NULL || ctx->f_rng == NULL )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200436 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200437
Hanno Beckerd140d082018-11-17 21:18:01 +0000438 if( len < TICKET_MIN_LEN )
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200439 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200440
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200441#if defined(MBEDTLS_THREADING_C)
442 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
443 return( ret );
444#endif
445
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200446 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
447 goto cleanup;
448
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200449 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200450
Hanno Beckerd140d082018-11-17 21:18:01 +0000451 if( len != TICKET_MIN_LEN + enc_len )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200452 {
453 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
454 goto cleanup;
455 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200456
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200457 /* Select key */
458 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200459 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200460 /* We can't know for sure but this is a likely option unless we're
461 * under attack - this is only informative anyway */
462 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200463 goto cleanup;
464 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200465
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200466 /* Decrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100467#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100468 if( ( status = psa_aead_decrypt( key->key, key->alg, iv, TICKET_IV_BYTES,
469 key_name, TICKET_ADD_DATA_LEN,
470 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
471 ticket, enc_len, &clear_len ) ) != PSA_SUCCESS )
472 {
473 ret = psa_ssl_status_to_mbedtls( status );
474 goto cleanup;
475 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100476#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100477 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx,
Hanno Beckerd140d082018-11-17 21:18:01 +0000478 iv, TICKET_IV_BYTES,
479 /* Additional data: key name, IV and length */
480 key_name, TICKET_ADD_DATA_LEN,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100481 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
482 ticket, enc_len, &clear_len,
483 TICKET_AUTH_TAG_BYTES ) ) != 0 )
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200484 {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200485 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
486 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
487
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200488 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200489 }
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100490#endif /* MBEDTLS_USE_PSA_CRYPTO */
491
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200492 if( clear_len != enc_len )
493 {
494 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200495 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200496 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200497
498 /* Actually load session */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +0200499 if( ( ret = mbedtls_ssl_session_load( session, ticket, clear_len ) ) != 0 )
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200500 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200501
502#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200503 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200504 /* Check for expiration */
SimonBd5800b72016-04-26 07:43:27 +0100505 mbedtls_time_t current_time = mbedtls_time( NULL );
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200506
507 if( current_time < session->start ||
508 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
509 {
510 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
511 goto cleanup;
512 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200513 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200514#endif
515
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200516cleanup:
517#if defined(MBEDTLS_THREADING_C)
518 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
519 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
520#endif
521
522 return( ret );
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200523}
Przemek Stekiela82290b2022-09-27 13:41:12 +0200524#endif
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200525
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200526/*
527 * Free context
528 */
529void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
530{
Gabor Mezei2a020512022-03-10 15:15:46 +0100531#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei2a020512022-03-10 15:15:46 +0100532 psa_destroy_key( ctx->keys[0].key );
533 psa_destroy_key( ctx->keys[1].key );
Gabor Mezei2a020512022-03-10 15:15:46 +0100534#else
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200535 mbedtls_cipher_free( &ctx->keys[0].ctx );
536 mbedtls_cipher_free( &ctx->keys[1].ctx );
Gabor Mezei2a020512022-03-10 15:15:46 +0100537#endif /* MBEDTLS_USE_PSA_CRYPTO */
538
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200539#if defined(MBEDTLS_THREADING_C)
540 mbedtls_mutex_free( &ctx->mutex );
541#endif
542
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500543 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200544}
545
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200546#endif /* MBEDTLS_SSL_TICKET_C */