blob: acc9e8c080bf25b5a7b3bfeea1bbf89e7909e31e [file] [log] [blame]
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001/*
2 * DTLS cookie 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é-Gonnard232edd42014-07-23 16:56:27 +02006 */
7/*
8 * These session callbacks use a simple chained list
9 * to store and retrieve the session information.
10 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020017
SimonBd5800b72016-04-26 07:43:27 +010018#include "mbedtls/ssl_cookie.h"
Chris Jones84a773f2021-03-05 18:38:47 +000019#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000020#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050021#include "mbedtls/platform_util.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020022#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010023
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000024#include <string.h>
25
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti384fbde2024-01-02 13:26:40 +010027#include "mbedtls/psa_util.h"
Andrzej Kurek00644842023-05-30 05:45:00 -040028/* Define a local translating function to save code size by not using too many
29 * arguments in each translating place. */
30static int local_err_translation(psa_status_t status)
31{
32 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040033 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040034 psa_generic_status_to_mbedtls);
35}
36#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050037#endif
38
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020039/*
Valerio Setti543d00e2022-12-22 14:27:34 +010040 * If DTLS is in use, then at least one of SHA-256 or SHA-384 is
41 * available. Try SHA-256 first as 384 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020042 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010043#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Setti543d00e2022-12-22 14:27:34 +010044#define COOKIE_MD MBEDTLS_MD_SHA256
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020045#define COOKIE_MD_OUTLEN 32
46#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010047#elif defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020049#define COOKIE_MD_OUTLEN 48
50#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020051#else
Valerio Setti543d00e2022-12-22 14:27:34 +010052#error "DTLS hello verify needs SHA-256 or SHA-384"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020053#endif
54
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020055/*
56 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080057 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020060
Gilles Peskine449bd832023-01-11 14:50:10 +010061void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020062{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010063#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010064 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010065#else
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_md_init(&ctx->hmac_ctx);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010067#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020069 ctx->serial = 0;
70#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020072
Neil Armstrong7cd02702022-03-04 15:08:43 +010073#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020074#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020076#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010077#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020078}
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020081{
82 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020083}
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020086{
Troy-Butler9ac3e232024-03-22 14:46:04 -040087 if (ctx == NULL) {
88 return;
89 }
90
Neil Armstrongbca99ee2022-03-04 10:20:20 +010091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010092 psa_destroy_key(ctx->psa_hmac_key);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010093#else
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020095
96#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020098#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010099#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200102}
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
105 int (*f_rng)(void *, unsigned char *, size_t),
106 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200107{
Neil Armstrongd6332012022-03-04 10:26:16 +0100108#if defined(MBEDTLS_USE_PSA_CRYPTO)
109 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
111 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 (void) f_rng;
114 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200116 alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (alg == 0) {
118 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
119 }
Neil Armstrongd6332012022-03-04 10:26:16 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
122 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
125 PSA_KEY_USAGE_SIGN_MESSAGE);
126 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
127 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
128 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 if ((status = psa_generate_key(&attributes,
131 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500132 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100133 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100134#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
136 unsigned char key[COOKIE_MD_OUTLEN];
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
139 return ret;
140 }
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
143 if (ret != 0) {
144 return ret;
145 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
148 if (ret != 0) {
149 return ret;
150 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 mbedtls_platform_zeroize(key, sizeof(key));
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100153#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200156}
157
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100158#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200159/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200160 * Generate the HMAC part of a cookie
161 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200162MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100163static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
164 const unsigned char time[4],
165 unsigned char **p, unsigned char *end,
166 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200167{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200168 unsigned char hmac_out[COOKIE_MD_OUTLEN];
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
173 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
174 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
175 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
176 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200177 }
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200180 *p += COOKIE_HMAC_LEN;
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200183}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100184#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200185
186/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200187 * Generate cookie for DTLS ClientHello verification
188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189int mbedtls_ssl_cookie_write(void *p_ctx,
190 unsigned char **p, unsigned char *end,
191 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200192{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100193#if defined(MBEDTLS_USE_PSA_CRYPTO)
194 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100196 size_t sign_mac_length = 0;
197#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200200 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (ctx == NULL || cli_id == NULL) {
203 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
204 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200210#else
211 t = ctx->serial++;
212#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200213
Joe Subbianib6511b02021-07-16 15:02:55 +0100214 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200215 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200216
Neil Armstrong7cd02702022-03-04 15:08:43 +0100217#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
219 ctx->psa_hmac_alg);
220 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500221 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100222 goto exit;
223 }
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 status = psa_mac_update(&operation, *p - 4, 4);
226 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500227 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100228 goto exit;
229 }
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 status = psa_mac_update(&operation, cli_id, cli_id_len);
232 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500233 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100234 goto exit;
235 }
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
238 &sign_mac_length);
239 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500240 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100241 goto exit;
242 }
243
244 *p += COOKIE_HMAC_LEN;
245
246 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100247#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200248#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
250 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
251 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200252#endif
253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
255 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200256
257#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
259 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
260 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
261 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200262#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100263#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200264
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100265#if defined(MBEDTLS_USE_PSA_CRYPTO)
266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 status = psa_mac_abort(&operation);
268 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500269 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 }
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100271#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200273}
274
275/*
276 * Check a cookie
277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278int mbedtls_ssl_cookie_check(void *p_ctx,
279 const unsigned char *cookie, size_t cookie_len,
280 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200281{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100282#if defined(MBEDTLS_USE_PSA_CRYPTO)
283 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100285#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200286 unsigned char ref_hmac[COOKIE_HMAC_LEN];
287 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100288#endif
289 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200291 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (ctx == NULL || cli_id == NULL) {
294 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
295 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (cookie_len != COOKIE_LEN) {
298 return -1;
299 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200300
Neil Armstrong7cd02702022-03-04 15:08:43 +0100301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
303 ctx->psa_hmac_alg);
304 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500305 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100306 goto exit;
307 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 status = psa_mac_update(&operation, cookie, 4);
310 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500311 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100312 goto exit;
313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 status = psa_mac_update(&operation, cli_id,
316 cli_id_len);
317 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500318 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100319 goto exit;
320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 status = psa_mac_verify_finish(&operation, cookie + 4,
323 COOKIE_HMAC_LEN);
324 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500325 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100326 goto exit;
327 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100328
329 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100330#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200331#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
333 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100334 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200335#endif
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
338 &p, p + sizeof(ref_hmac),
339 cli_id, cli_id_len) != 0) {
340 ret = -1;
341 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343#if defined(MBEDTLS_THREADING_C)
344 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
345 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
346 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
347 }
348#endif
349
350 if (ret != 0) {
351 goto exit;
352 }
353
354 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100355 ret = -1;
356 goto exit;
357 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100358#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200362#else
363 cur_time = ctx->serial;
364#endif
365
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +0100366 cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100369 ret = -1;
370 goto exit;
371 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200372
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100373exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100374#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 status = psa_mac_abort(&operation);
376 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500377 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100379#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100381#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_SSL_COOKIE_C */