blob: bfb656cf62c9ac8ec21bcd75382ed72b3d052ab4 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02009
10#if defined(MBEDTLS_SSL_TICKET_C)
11
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020012#include "mbedtls/platform.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020013
Chris Jones84a773f2021-03-05 18:38:47 +000014#include "ssl_misc.h"
SimonBd5800b72016-04-26 07:43:27 +010015#include "mbedtls/ssl_ticket.h"
Janos Follath865b3eb2019-12-16 11:46:15 +000016#include "mbedtls/error.h"
Janos Follath73c616b2019-12-18 15:07:04 +000017#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010018
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020019#include <string.h>
20
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050021#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040022/* Define a local translating function to save code size by not using too many
23 * arguments in each translating place. */
24static int local_err_translation(psa_status_t status)
25{
26 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040027 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040028 psa_generic_status_to_mbedtls);
29}
30#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050031#endif
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +010036void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020037{
Gilles Peskine449bd832023-01-11 14:50:10 +010038 memset(ctx, 0, sizeof(mbedtls_ssl_ticket_context));
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020039
40#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010041 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020042#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
Gilles Peskine449bd832023-01-11 14:50:10 +010052#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)
Hanno Beckerd140d082018-11-17 21:18:01 +000059
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
Gilles Peskine449bd832023-01-11 14:50:10 +010064static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx,
65 unsigned char index)
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020066{
Janos Follath865b3eb2019-12-16 11:46:15 +000067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010068 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)
Gilles Peskine449bd832023-01-11 14:50:10 +010076 key->generation_time = mbedtls_time(NULL);
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020077#endif
Ronald Cronba5165e2023-11-21 13:53:18 +010078 /* The lifetime of a key is the configured lifetime of the tickets when
79 * the key is created.
80 */
81 key->lifetime = ctx->ticket_lifetime;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if ((ret = ctx->f_rng(ctx->p_rng, key->name, sizeof(key->name))) != 0) {
84 return ret;
85 }
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((ret = ctx->f_rng(ctx->p_rng, buf, sizeof(buf))) != 0) {
88 return ret;
89 }
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020090
Gabor Mezei2a020512022-03-10 15:15:46 +010091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +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);
Gabor Mezei2a020512022-03-10 15:15:46 +010097
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050098 ret = PSA_TO_MBEDTLS_ERR(
Gilles Peskine449bd832023-01-11 14:50:10 +010099 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 ret = mbedtls_cipher_setkey(&key->ctx, buf,
105 mbedtls_cipher_get_key_bitlen(&key->ctx),
106 MBEDTLS_ENCRYPT);
Gabor Mezei2a020512022-03-10 15:15:46 +0100107#endif /* MBEDTLS_USE_PSA_CRYPTO */
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_platform_zeroize(buf, sizeof(buf));
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return ret;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200112}
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 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200117MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100118static int ssl_ticket_update_keys(mbedtls_ssl_ticket_context *ctx)
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200119{
120#if !defined(MBEDTLS_HAVE_TIME)
121 ((void) ctx);
122#else
Ronald Cronba5165e2023-11-21 13:53:18 +0100123 mbedtls_ssl_ticket_key * const key = ctx->keys + ctx->active;
124 if (key->lifetime != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_time_t current_time = mbedtls_time(NULL);
Ronald Cronba5165e2023-11-21 13:53:18 +0100126 mbedtls_time_t key_time = key->generation_time;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200127
Gabor Mezei2a020512022-03-10 15:15:46 +0100128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100129 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei2a020512022-03-10 15:15:46 +0100130#endif
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (current_time >= key_time &&
Ronald Cronba5165e2023-11-21 13:53:18 +0100133 (uint64_t) (current_time - key_time) < key->lifetime) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 return 0;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200135 }
136
137 ctx->active = 1 - ctx->active;
138
Gabor Mezei2a020512022-03-10 15:15:46 +0100139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if ((status = psa_destroy_key(ctx->keys[ctx->active].key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500141 return PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100142 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100143#endif /* MBEDTLS_USE_PSA_CRYPTO */
144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return ssl_ticket_gen_key(ctx, ctx->active);
146 } else
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200147#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return 0;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200149}
150
151/*
Glenn Straussa9509382022-02-02 23:32:18 -0500152 * Rotate active session ticket encryption key
153 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154int 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)
Glenn Straussa9509382022-02-02 23:32:18 -0500158{
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)
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100164 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gabor Mezei2a020512022-03-10 15:15:46 +0100165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gabor Mezei36c9f512022-03-16 12:55:32 +0100166 const size_t bitlen = key->key_bits;
Gabor Mezei2a020512022-03-10 15:15:46 +0100167#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 const int bitlen = mbedtls_cipher_get_key_bitlen(&key->ctx);
Gabor Mezei2a020512022-03-10 15:15:46 +0100169#endif
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t) bitlen) {
172 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
173 }
Glenn Straussa9509382022-02-02 23:32:18 -0500174
Gabor Mezei2a020512022-03-10 15:15:46 +0100175#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if ((status = psa_destroy_key(key->key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500177 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return ret;
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100179 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 psa_set_key_usage_flags(&attributes,
182 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
183 psa_set_key_algorithm(&attributes, key->alg);
184 psa_set_key_type(&attributes, key->key_type);
185 psa_set_key_bits(&attributes, key->key_bits);
Gabor Mezei2a020512022-03-10 15:15:46 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if ((status = psa_import_key(&attributes, k,
188 PSA_BITS_TO_BYTES(key->key_bits),
189 &key->key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500190 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return ret;
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100192 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100193#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 ret = mbedtls_cipher_setkey(&key->ctx, k, bitlen, MBEDTLS_ENCRYPT);
195 if (ret != 0) {
196 return ret;
197 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100198#endif /* MBEDTLS_USE_PSA_CRYPTO */
199
Glenn Straussa9509382022-02-02 23:32:18 -0500200 ctx->active = idx;
201 ctx->ticket_lifetime = lifetime;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memcpy(key->name, name, TICKET_KEY_NAME_BYTES);
Glenn Straussa9509382022-02-02 23:32:18 -0500203#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 key->generation_time = mbedtls_time(NULL);
Glenn Straussa9509382022-02-02 23:32:18 -0500205#endif
Ronald Cronba5165e2023-11-21 13:53:18 +0100206 key->lifetime = lifetime;
207
Glenn Straussa9509382022-02-02 23:32:18 -0500208 return 0;
209}
210
211/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200212 * Setup context for actual use
213 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
215 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
216 mbedtls_cipher_type_t cipher,
217 uint32_t lifetime)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200218{
Janos Follath865b3eb2019-12-16 11:46:15 +0000219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200220 size_t key_bits;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200221
Gabor Mezei2a020512022-03-10 15:15:46 +0100222#if defined(MBEDTLS_USE_PSA_CRYPTO)
223 psa_algorithm_t alg;
224 psa_key_type_t key_type;
Neil Armstrong858581e2022-04-01 18:03:15 +0200225#else
226 const mbedtls_cipher_info_t *cipher_info;
227#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gabor Mezei2a020512022-03-10 15:15:46 +0100228
Neil Armstrong858581e2022-04-01 18:03:15 +0200229#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (mbedtls_ssl_cipher_to_psa(cipher, TICKET_AUTH_TAG_BYTES,
231 &alg, &key_type, &key_bits) != PSA_SUCCESS) {
232 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200233 }
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (PSA_ALG_IS_AEAD(alg) == 0) {
236 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
237 }
238#else
239 cipher_info = mbedtls_cipher_info_from_type(cipher);
240
241 if (mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_GCM &&
242 mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_CCM &&
243 mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_CHACHAPOLY) {
244 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
245 }
246
247 key_bits = mbedtls_cipher_info_get_key_bitlen(cipher_info);
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200248#endif /* MBEDTLS_USE_PSA_CRYPTO */
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (key_bits > 8 * MAX_KEY_BYTES) {
251 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
252 }
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200253
Neil Armstrong3bf040e2022-04-27 10:35:24 +0200254 ctx->f_rng = f_rng;
255 ctx->p_rng = p_rng;
256
257 ctx->ticket_lifetime = lifetime;
258
259#if defined(MBEDTLS_USE_PSA_CRYPTO)
260 ctx->keys[0].alg = alg;
261 ctx->keys[0].key_type = key_type;
262 ctx->keys[0].key_bits = key_bits;
263
264 ctx->keys[1].alg = alg;
265 ctx->keys[1].key_type = key_type;
266 ctx->keys[1].key_bits = key_bits;
267#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if ((ret = mbedtls_cipher_setup(&ctx->keys[0].ctx, cipher_info)) != 0) {
269 return ret;
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200270 }
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if ((ret = mbedtls_cipher_setup(&ctx->keys[1].ctx, cipher_info)) != 0) {
273 return ret;
274 }
275#endif /* MBEDTLS_USE_PSA_CRYPTO */
276
277 if ((ret = ssl_ticket_gen_key(ctx, 0)) != 0 ||
278 (ret = ssl_ticket_gen_key(ctx, 1)) != 0) {
279 return ret;
280 }
281
282 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 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_ssl_ticket_write(void *p_ticket,
300 const mbedtls_ssl_session *session,
301 unsigned char *start,
302 const unsigned char *end,
303 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 if (ctx == NULL || ctx->f_rng == NULL) {
322 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
323 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200324
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200325 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
326 * in addition to session itself, that will be checked when writing it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 MBEDTLS_SSL_CHK_BUF_PTR(start, end, TICKET_MIN_LEN);
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200328
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200329#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
331 return ret;
332 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200333#endif
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if ((ret = ssl_ticket_update_keys(ctx)) != 0) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200336 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200338
339 key = &ctx->keys[ctx->active];
340
Ronald Cronba5165e2023-11-21 13:53:18 +0100341 *ticket_lifetime = key->lifetime;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 memcpy(key_name, key->name, TICKET_KEY_NAME_BYTES);
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if ((ret = ctx->f_rng(ctx->p_rng, iv, TICKET_IV_BYTES)) != 0) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200346 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200348
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200349 /* Dump session state */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if ((ret = mbedtls_ssl_session_save(session,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000351 state, (size_t) (end - state),
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 &clear_len)) != 0 ||
353 (unsigned long) clear_len > 65535) {
354 goto cleanup;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200355 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 MBEDTLS_PUT_UINT16_BE(clear_len, state_len_bytes, 0);
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200357
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200358 /* Encrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100359#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if ((status = psa_aead_encrypt(key->key, key->alg, iv, TICKET_IV_BYTES,
361 key_name, TICKET_ADD_DATA_LEN,
362 state, clear_len,
363 state, end - state,
364 &ciph_len)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500365 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100366 goto cleanup;
367 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100368#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if ((ret = mbedtls_cipher_auth_encrypt_ext(&key->ctx,
370 iv, TICKET_IV_BYTES,
371 /* Additional data: key name, IV and length */
372 key_name, TICKET_ADD_DATA_LEN,
373 state, clear_len,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000374 state, (size_t) (end - state), &ciph_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 TICKET_AUTH_TAG_BYTES)) != 0) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200376 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200377 }
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100378#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gabor Mezei2a020512022-03-10 15:15:46 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ciph_len != clear_len + TICKET_AUTH_TAG_BYTES) {
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)
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
390 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
391 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200392#endif
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return ret;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200395}
396
397/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200398 * Select key based on name
399 */
400static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 mbedtls_ssl_ticket_context *ctx,
402 const unsigned char name[4])
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200403{
404 unsigned char i;
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 for (i = 0; i < sizeof(ctx->keys) / sizeof(*ctx->keys); i++) {
407 if (memcmp(name, ctx->keys[i].name, 4) == 0) {
408 return &ctx->keys[i];
409 }
410 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return NULL;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200413}
414
415/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200416 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_ssl_ticket_parse(void *p_ticket,
419 mbedtls_ssl_session *session,
420 unsigned char *buf,
421 size_t len)
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200422{
Janos Follath865b3eb2019-12-16 11:46:15 +0000423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200424 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200425 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200426 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000427 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
428 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
429 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200430 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200431
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100432#if defined(MBEDTLS_USE_PSA_CRYPTO)
433 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
434#endif
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (ctx == NULL || ctx->f_rng == NULL) {
437 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
438 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (len < TICKET_MIN_LEN) {
441 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
442 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200443
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200444#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
446 return ret;
447 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200448#endif
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if ((ret = ssl_ticket_update_keys(ctx)) != 0) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200451 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200453
Dave Rodgmana3d0f612023-11-03 23:34:02 +0000454 enc_len = MBEDTLS_GET_UINT16_BE(enc_len_p, 0);
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (len != TICKET_MIN_LEN + enc_len) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200457 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
458 goto cleanup;
459 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200460
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200461 /* Select key */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if ((key = ssl_ticket_select_key(ctx, key_name)) == NULL) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200463 /* We can't know for sure but this is a likely option unless we're
464 * under attack - this is only informative anyway */
465 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200466 goto cleanup;
467 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200468
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200469 /* Decrypt and authenticate */
Gabor Mezei2a020512022-03-10 15:15:46 +0100470#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if ((status = psa_aead_decrypt(key->key, key->alg, iv, TICKET_IV_BYTES,
472 key_name, TICKET_ADD_DATA_LEN,
473 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
474 ticket, enc_len, &clear_len)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500475 ret = PSA_TO_MBEDTLS_ERR(status);
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100476 goto cleanup;
477 }
Gabor Mezei2a020512022-03-10 15:15:46 +0100478#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if ((ret = mbedtls_cipher_auth_decrypt_ext(&key->ctx,
480 iv, TICKET_IV_BYTES,
481 /* Additional data: key name, IV and length */
482 key_name, TICKET_ADD_DATA_LEN,
483 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
484 ticket, enc_len, &clear_len,
485 TICKET_AUTH_TAG_BYTES)) != 0) {
486 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200487 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200489
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200490 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200491 }
Gabor Mezei5b8b8902022-03-16 12:56:58 +0100492#endif /* MBEDTLS_USE_PSA_CRYPTO */
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (clear_len != enc_len) {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200495 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200496 goto cleanup;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200497 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200498
499 /* Actually load session */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if ((ret = mbedtls_ssl_session_load(session, ticket, clear_len)) != 0) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200501 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200503
504#if defined(MBEDTLS_HAVE_TIME)
Ronald Cron3c0072b2023-11-22 10:00:14 +0100505 mbedtls_ms_time_t ticket_creation_time, ticket_age;
506 mbedtls_ms_time_t ticket_lifetime =
Ronald Cron97dfc722024-03-08 16:34:59 +0100507 (mbedtls_ms_time_t) key->lifetime * 1000;
Jerry Yucebffc32022-12-15 18:00:05 +0800508
Ronald Cron3c0072b2023-11-22 10:00:14 +0100509 ret = mbedtls_ssl_session_get_ticket_creation_time(session,
510 &ticket_creation_time);
511 if (ret != 0) {
512 goto cleanup;
Jerry Yuec6d0782023-10-31 14:42:20 +0800513 }
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200514
Ronald Cron3c0072b2023-11-22 10:00:14 +0100515 ticket_age = mbedtls_ms_time() - ticket_creation_time;
516 if (ticket_age < 0 || ticket_age > ticket_lifetime) {
517 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
518 goto cleanup;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200519 }
Ronald Cron3c0072b2023-11-22 10:00:14 +0100520#endif
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200521
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200522cleanup:
523#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
525 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
526 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200527#endif
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return ret;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200530}
531
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200532/*
533 * Free context
534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200536{
Troy-Butler9ac3e232024-03-22 14:46:04 -0400537 if (ctx == NULL) {
538 return;
539 }
540
Gabor Mezei2a020512022-03-10 15:15:46 +0100541#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 psa_destroy_key(ctx->keys[0].key);
543 psa_destroy_key(ctx->keys[1].key);
Gabor Mezei2a020512022-03-10 15:15:46 +0100544#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 mbedtls_cipher_free(&ctx->keys[0].ctx);
546 mbedtls_cipher_free(&ctx->keys[1].ctx);
Gabor Mezei2a020512022-03-10 15:15:46 +0100547#endif /* MBEDTLS_USE_PSA_CRYPTO */
548
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200549#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200551#endif
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_ticket_context));
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200554}
555
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200556#endif /* MBEDTLS_SSL_TICKET_C */