blob: ee81eb420f0ccada798c2f027033c60b51258e2c [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)
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020027#include "md_psa.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{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010087#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010088 psa_destroy_key(ctx->psa_hmac_key);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010089#else
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020091
92#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010093 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020094#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010095#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020098}
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
101 int (*f_rng)(void *, unsigned char *, size_t),
102 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200103{
Neil Armstrongd6332012022-03-04 10:26:16 +0100104#if defined(MBEDTLS_USE_PSA_CRYPTO)
105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
106 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
107 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 (void) f_rng;
110 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200111
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200112 alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (alg == 0) {
114 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
115 }
Neil Armstrongd6332012022-03-04 10:26:16 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
118 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
121 PSA_KEY_USAGE_SIGN_MESSAGE);
122 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
123 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
124 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if ((status = psa_generate_key(&attributes,
127 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500128 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100129 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100130#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
132 unsigned char key[COOKIE_MD_OUTLEN];
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
135 return ret;
136 }
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
139 if (ret != 0) {
140 return ret;
141 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
144 if (ret != 0) {
145 return ret;
146 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_platform_zeroize(key, sizeof(key));
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100149#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200152}
153
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100154#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200155/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200156 * Generate the HMAC part of a cookie
157 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100159static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
160 const unsigned char time[4],
161 unsigned char **p, unsigned char *end,
162 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200163{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200164 unsigned char hmac_out[COOKIE_MD_OUTLEN];
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
169 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
170 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
171 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
172 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200176 *p += COOKIE_HMAC_LEN;
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200179}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100180#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200181
182/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200183 * Generate cookie for DTLS ClientHello verification
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185int mbedtls_ssl_cookie_write(void *p_ctx,
186 unsigned char **p, unsigned char *end,
187 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200188{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100189#if defined(MBEDTLS_USE_PSA_CRYPTO)
190 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100192 size_t sign_mac_length = 0;
193#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200196 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (ctx == NULL || cli_id == NULL) {
199 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
200 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200206#else
207 t = ctx->serial++;
208#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200209
Joe Subbianib6511b02021-07-16 15:02:55 +0100210 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200211 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200212
Neil Armstrong7cd02702022-03-04 15:08:43 +0100213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
215 ctx->psa_hmac_alg);
216 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500217 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100218 goto exit;
219 }
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 status = psa_mac_update(&operation, *p - 4, 4);
222 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500223 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100224 goto exit;
225 }
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 status = psa_mac_update(&operation, cli_id, cli_id_len);
228 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500229 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100230 goto exit;
231 }
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
234 &sign_mac_length);
235 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500236 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100237 goto exit;
238 }
239
240 *p += COOKIE_HMAC_LEN;
241
242 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100243#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200244#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
246 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
247 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200248#endif
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
251 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200252
253#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
255 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
256 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
257 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200258#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100259#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200260
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100261#if defined(MBEDTLS_USE_PSA_CRYPTO)
262exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 status = psa_mac_abort(&operation);
264 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500265 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 }
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100267#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200269}
270
271/*
272 * Check a cookie
273 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274int mbedtls_ssl_cookie_check(void *p_ctx,
275 const unsigned char *cookie, size_t cookie_len,
276 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200277{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100278#if defined(MBEDTLS_USE_PSA_CRYPTO)
279 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100280 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100281#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200282 unsigned char ref_hmac[COOKIE_HMAC_LEN];
283 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100284#endif
285 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200287 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (ctx == NULL || cli_id == NULL) {
290 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
291 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (cookie_len != COOKIE_LEN) {
294 return -1;
295 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200296
Neil Armstrong7cd02702022-03-04 15:08:43 +0100297#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
299 ctx->psa_hmac_alg);
300 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500301 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100302 goto exit;
303 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 status = psa_mac_update(&operation, cookie, 4);
306 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500307 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100308 goto exit;
309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 status = psa_mac_update(&operation, cli_id,
312 cli_id_len);
313 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500314 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100315 goto exit;
316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 status = psa_mac_verify_finish(&operation, cookie + 4,
319 COOKIE_HMAC_LEN);
320 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500321 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100322 goto exit;
323 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100324
325 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100326#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200327#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
329 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100330 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200331#endif
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
334 &p, p + sizeof(ref_hmac),
335 cli_id, cli_id_len) != 0) {
336 ret = -1;
337 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339#if defined(MBEDTLS_THREADING_C)
340 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
341 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
342 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
343 }
344#endif
345
346 if (ret != 0) {
347 goto exit;
348 }
349
350 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100351 ret = -1;
352 goto exit;
353 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100354#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200358#else
359 cur_time = ctx->serial;
360#endif
361
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +0100362 cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100365 ret = -1;
366 goto exit;
367 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200368
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100369exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100370#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 status = psa_mac_abort(&operation);
372 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500373 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100375#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100377#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#endif /* MBEDTLS_SSL_COOKIE_C */