blob: 0789245bacd7e44d1667ce2ffacccb0a4b118a3b [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
Hanno Becker261602c2017-04-12 14:54:42 +010026#include "mbedtls/ssl_internal.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 Case0e7791f2021-12-20 21:14:10 -080034 * Initialize context
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020035 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020037{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020045#define MAX_KEY_BYTES 32 /* 256 bits */
46
Hanno Beckerd140d082018-11-17 21:18:01 +000047#define TICKET_KEY_NAME_BYTES 4
48#define TICKET_IV_BYTES 12
49#define TICKET_CRYPT_LEN_BYTES 2
50#define TICKET_AUTH_TAG_BYTES 16
51
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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é-Gonnardd904d662022-06-17 10:24:00 +020063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020068 unsigned char buf[MAX_KEY_BYTES];
69 mbedtls_ssl_ticket_key *key = ctx->keys + index;
70
71#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 key->generation_time = (uint32_t) mbedtls_time(NULL);
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020073#endif
74
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 if ((ret = ctx->f_rng(ctx->p_rng, key->name, sizeof(key->name))) != 0) {
76 return ret;
77 }
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 if ((ret = ctx->f_rng(ctx->p_rng, buf, sizeof(buf))) != 0) {
80 return ret;
81 }
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020082
83 /* With GCM and CCM, same context can encrypt & decrypt */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 ret = mbedtls_cipher_setkey(&key->ctx, buf,
85 mbedtls_cipher_get_key_bitlen(&key->ctx),
86 MBEDTLS_ENCRYPT);
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 mbedtls_platform_zeroize(buf, sizeof(buf));
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 return ret;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020091}
92
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020093/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +020094 * Rotate/generate keys if necessary
95 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +020096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097static int ssl_ticket_update_keys(mbedtls_ssl_ticket_context *ctx)
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +020098{
99#if !defined(MBEDTLS_HAVE_TIME)
100 ((void) ctx);
101#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 if (ctx->ticket_lifetime != 0) {
103 uint32_t current_time = (uint32_t) mbedtls_time(NULL);
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200104 uint32_t key_time = ctx->keys[ctx->active].generation_time;
105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (current_time >= key_time &&
107 current_time - key_time < ctx->ticket_lifetime) {
108 return 0;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200109 }
110
111 ctx->active = 1 - ctx->active;
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 return ssl_ticket_gen_key(ctx, ctx->active);
114 } else
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200115#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 return 0;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200117}
118
119/*
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200120 * Setup context for actual use
121 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
124 mbedtls_cipher_type_t cipher,
125 uint32_t lifetime)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200126{
Janos Follath865b3eb2019-12-16 11:46:15 +0000127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200128 const mbedtls_cipher_info_t *cipher_info;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200129
130 ctx->f_rng = f_rng;
131 ctx->p_rng = p_rng;
132
133 ctx->ticket_lifetime = lifetime;
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 cipher_info = mbedtls_cipher_info_from_type(cipher);
136 if (cipher_info == NULL) {
137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200138 }
139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 if (cipher_info->mode != MBEDTLS_MODE_GCM &&
141 cipher_info->mode != MBEDTLS_MODE_CCM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
144
145 if (cipher_info->key_bitlen > 8 * MAX_KEY_BYTES) {
146 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
147 }
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +0200148
David Horstmann04020ab2022-10-27 11:30:09 +0100149 int do_mbedtls_cipher_setup = 1;
Hanno Becker679d8ce2018-11-17 21:25:59 +0000150#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 ret = mbedtls_cipher_setup_psa(&ctx->keys[0].ctx,
152 cipher_info, TICKET_AUTH_TAG_BYTES);
David Horstmannbcc18f22022-11-07 14:41:13 +0000153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 switch (ret) {
David Horstmannbcc18f22022-11-07 14:41:13 +0000155 case 0:
156 do_mbedtls_cipher_setup = 0;
157 break;
158 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
159 /* We don't yet expect to support all ciphers through PSA,
160 * so allow fallback to ordinary mbedtls_cipher_setup(). */
161 do_mbedtls_cipher_setup = 1;
162 break;
163 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 return ret;
David Horstmannbcc18f22022-11-07 14:41:13 +0000165 }
Hanno Becker679d8ce2018-11-17 21:25:59 +0000166#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if (do_mbedtls_cipher_setup) {
168 if ((ret = mbedtls_cipher_setup(&ctx->keys[0].ctx, cipher_info))
169 != 0) {
170 return ret;
171 }
172 }
Hanno Becker679d8ce2018-11-17 21:25:59 +0000173
David Horstmann04020ab2022-10-27 11:30:09 +0100174 do_mbedtls_cipher_setup = 1;
Hanno Becker679d8ce2018-11-17 21:25:59 +0000175#if defined(MBEDTLS_USE_PSA_CRYPTO)
David Horstmann04020ab2022-10-27 11:30:09 +0100176 do_mbedtls_cipher_setup = 0;
177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 ret = mbedtls_cipher_setup_psa(&ctx->keys[1].ctx,
179 cipher_info, TICKET_AUTH_TAG_BYTES);
180 if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
181 return ret;
182 }
183 if (ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) {
David Horstmann04020ab2022-10-27 11:30:09 +0100184 do_mbedtls_cipher_setup = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 }
Hanno Becker679d8ce2018-11-17 21:25:59 +0000186#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if (do_mbedtls_cipher_setup) {
188 if ((ret = mbedtls_cipher_setup(&ctx->keys[1].ctx, cipher_info))
189 != 0) {
190 return ret;
191 }
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200192 }
193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if ((ret = ssl_ticket_gen_key(ctx, 0)) != 0 ||
195 (ret = ssl_ticket_gen_key(ctx, 1)) != 0) {
196 return ret;
197 }
198
199 return 0;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200200}
201
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200202/*
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200203 * Create session ticket, with the following structure:
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200204 *
205 * struct {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200206 * opaque key_name[4];
207 * opaque iv[12];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200208 * opaque encrypted_state<0..2^16-1>;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200209 * opaque tag[16];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200210 * } ticket;
211 *
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200212 * The key_name, iv, and length of encrypted_state are the additional
213 * authenticated data.
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200214 */
Hanno Beckerd140d082018-11-17 21:18:01 +0000215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216int mbedtls_ssl_ticket_write(void *p_ticket,
217 const mbedtls_ssl_session *session,
218 unsigned char *start,
219 const unsigned char *end,
220 size_t *tlen,
221 uint32_t *ticket_lifetime)
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200222{
Janos Follath865b3eb2019-12-16 11:46:15 +0000223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200224 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200225 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200226 unsigned char *key_name = start;
Hanno Beckerd140d082018-11-17 21:18:01 +0000227 unsigned char *iv = start + TICKET_KEY_NAME_BYTES;
228 unsigned char *state_len_bytes = iv + TICKET_IV_BYTES;
229 unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200230 size_t clear_len, ciph_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200231
232 *tlen = 0;
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 if (ctx == NULL || ctx->f_rng == NULL) {
235 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
236 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200237
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200238 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
239 * in addition to session itself, that will be checked when writing it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 MBEDTLS_SSL_CHK_BUF_PTR(start, end, TICKET_MIN_LEN);
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200241
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200242#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
244 return ret;
245 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200246#endif
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if ((ret = ssl_ticket_update_keys(ctx)) != 0) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200249 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200251
252 key = &ctx->keys[ctx->active];
253
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200254 *ticket_lifetime = ctx->ticket_lifetime;
255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 memcpy(key_name, key->name, TICKET_KEY_NAME_BYTES);
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if ((ret = ctx->f_rng(ctx->p_rng, iv, TICKET_IV_BYTES)) != 0) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200259 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200261
Manuel Pégourié-Gonnard69f17282015-05-18 14:35:08 +0200262 /* Dump session state */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if ((ret = mbedtls_ssl_session_save(session,
264 state, end - state,
265 &clear_len)) != 0 ||
266 (unsigned long) clear_len > 65535) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200267 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200268 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 MBEDTLS_PUT_UINT16_BE(clear_len, state_len_bytes, 0);
270
271 /* Encrypt and authenticate */
272 if ((ret = mbedtls_cipher_auth_encrypt_ext(&key->ctx,
273 iv, TICKET_IV_BYTES,
274 /* Additional data: key name, IV and length */
275 key_name, TICKET_ADD_DATA_LEN,
276 state, clear_len,
277 state, end - state, &ciph_len,
278 TICKET_AUTH_TAG_BYTES)) != 0) {
279 goto cleanup;
280 }
281 if (ciph_len != clear_len + TICKET_AUTH_TAG_BYTES) {
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200282 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200283 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200284 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200285
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100286 *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200287
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200288cleanup:
289#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
291 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
292 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200293#endif
294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 return ret;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200296}
297
298/*
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200299 * Select key based on name
300 */
301static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 mbedtls_ssl_ticket_context *ctx,
303 const unsigned char name[4])
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200304{
305 unsigned char i;
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 for (i = 0; i < sizeof(ctx->keys) / sizeof(*ctx->keys); i++) {
308 if (memcmp(name, ctx->keys[i].name, 4) == 0) {
309 return &ctx->keys[i];
310 }
311 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 return NULL;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200314}
315
316/*
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200317 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
318 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319int mbedtls_ssl_ticket_parse(void *p_ticket,
320 mbedtls_ssl_session *session,
321 unsigned char *buf,
322 size_t len)
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200323{
Janos Follath865b3eb2019-12-16 11:46:15 +0000324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200325 mbedtls_ssl_ticket_context *ctx = p_ticket;
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200326 mbedtls_ssl_ticket_key *key;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200327 unsigned char *key_name = buf;
Hanno Beckerd140d082018-11-17 21:18:01 +0000328 unsigned char *iv = buf + TICKET_KEY_NAME_BYTES;
329 unsigned char *enc_len_p = iv + TICKET_IV_BYTES;
330 unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200331 size_t enc_len, clear_len;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if (ctx == NULL || ctx->f_rng == NULL) {
334 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
335 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 if (len < TICKET_MIN_LEN) {
338 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
339 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200340
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200341#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
343 return ret;
344 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200345#endif
346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 if ((ret = ssl_ticket_update_keys(ctx)) != 0) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200348 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 enc_len = (enc_len_p[0] << 8) | enc_len_p[1];
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 if (len != TICKET_MIN_LEN + enc_len) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200354 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
355 goto cleanup;
356 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200357
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200358 /* Select key */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if ((key = ssl_ticket_select_key(ctx, key_name)) == NULL) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200360 /* We can't know for sure but this is a likely option unless we're
361 * under attack - this is only informative anyway */
362 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200363 goto cleanup;
364 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200365
Manuel Pégourié-Gonnard1041a392015-05-20 19:59:39 +0200366 /* Decrypt and authenticate */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 if ((ret = mbedtls_cipher_auth_decrypt_ext(&key->ctx,
368 iv, TICKET_IV_BYTES,
369 /* Additional data: key name, IV and length */
370 key_name, TICKET_ADD_DATA_LEN,
371 ticket, enc_len + TICKET_AUTH_TAG_BYTES,
372 ticket, enc_len, &clear_len,
373 TICKET_AUTH_TAG_BYTES)) != 0) {
374 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200375 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 }
Manuel Pégourié-Gonnard1e9c4db2015-05-25 14:07:08 +0200377
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200378 goto cleanup;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200379 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 if (clear_len != enc_len) {
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é-Gonnard1041a392015-05-20 19:59:39 +0200383 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200384
385 /* Actually load session */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 if ((ret = mbedtls_ssl_session_load(session, ticket, clear_len)) != 0) {
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200387 goto cleanup;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200389
390#if defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200391 {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200392 /* Check for expiration */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 mbedtls_time_t current_time = mbedtls_time(NULL);
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 if (current_time < session->start ||
396 (uint32_t) (current_time - session->start) > ctx->ticket_lifetime) {
Manuel Pégourié-Gonnard8eff5122015-05-20 11:41:36 +0200397 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
398 goto cleanup;
399 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200400 }
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200401#endif
402
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200403cleanup:
404#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
406 return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
407 }
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200408#endif
409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return ret;
Manuel Pégourié-Gonnarda4a47352015-05-15 15:14:54 +0200411}
412
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200413/*
414 * Free context
415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200417{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 mbedtls_cipher_free(&ctx->keys[0].ctx);
419 mbedtls_cipher_free(&ctx->keys[1].ctx);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200420
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200421#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +0200423#endif
424
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_ticket_context));
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200426}
427
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200428#endif /* MBEDTLS_SSL_TICKET_C */