blob: ae7a4204ca62ba0d0761bc2a12433541c87404a8 [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
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é-Gonnard232edd42014-07-23 16:56:27 +020018 */
19/*
20 * These session callbacks use a simple chained list
21 * to store and retrieve the session information.
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020029
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/ssl_cookie.h"
Chris Jones84a773f2021-03-05 18:38:47 +000031#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000032#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020034#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010035
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000036#include <string.h>
37
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050038#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020039#include "md_psa.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050040#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
41 psa_to_ssl_errors, \
42 psa_generic_status_to_mbedtls)
43#endif
44
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020045/*
Valerio Setti543d00e2022-12-22 14:27:34 +010046 * If DTLS is in use, then at least one of SHA-256 or SHA-384 is
47 * available. Try SHA-256 first as 384 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020048 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010049#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Setti543d00e2022-12-22 14:27:34 +010050#define COOKIE_MD MBEDTLS_MD_SHA256
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020051#define COOKIE_MD_OUTLEN 32
52#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010053#elif defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020055#define COOKIE_MD_OUTLEN 48
56#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020057#else
Valerio Setti543d00e2022-12-22 14:27:34 +010058#error "DTLS hello verify needs SHA-256 or SHA-384"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020059#endif
60
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020061/*
62 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080063 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020064 */
Gilles Peskine449bd832023-01-11 14:50:10 +010065#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020066
Gilles Peskine449bd832023-01-11 14:50:10 +010067void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020068{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010069#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010070 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010071#else
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_md_init(&ctx->hmac_ctx);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010073#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020075 ctx->serial = 0;
76#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020078
Neil Armstrong7cd02702022-03-04 15:08:43 +010079#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020080#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020082#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010083#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020084}
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020087{
88 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020089}
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020092{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010093#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010094 psa_destroy_key(ctx->psa_hmac_key);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010095#else
Gilles Peskine449bd832023-01-11 14:50:10 +010096 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020097
98#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010099 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200100#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100101#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200104}
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
107 int (*f_rng)(void *, unsigned char *, size_t),
108 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200109{
Neil Armstrongd6332012022-03-04 10:26:16 +0100110#if defined(MBEDTLS_USE_PSA_CRYPTO)
111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
113 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 (void) f_rng;
116 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200117
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200118 alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (alg == 0) {
120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
121 }
Neil Armstrongd6332012022-03-04 10:26:16 +0100122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
124 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
127 PSA_KEY_USAGE_SIGN_MESSAGE);
128 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
129 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
130 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if ((status = psa_generate_key(&attributes,
133 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500134 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100135 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100136#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
138 unsigned char key[COOKIE_MD_OUTLEN];
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
141 return ret;
142 }
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
145 if (ret != 0) {
146 return ret;
147 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
150 if (ret != 0) {
151 return ret;
152 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_platform_zeroize(key, sizeof(key));
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100155#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200158}
159
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100160#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200161/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200162 * Generate the HMAC part of a cookie
163 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200164MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100165static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
166 const unsigned char time[4],
167 unsigned char **p, unsigned char *end,
168 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200169{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200170 unsigned char hmac_out[COOKIE_MD_OUTLEN];
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
175 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
176 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
177 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
178 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200179 }
180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200182 *p += COOKIE_HMAC_LEN;
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200185}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100186#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200187
188/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200189 * Generate cookie for DTLS ClientHello verification
190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int mbedtls_ssl_cookie_write(void *p_ctx,
192 unsigned char **p, unsigned char *end,
193 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200194{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100195#if defined(MBEDTLS_USE_PSA_CRYPTO)
196 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100198 size_t sign_mac_length = 0;
199#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200202 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (ctx == NULL || cli_id == NULL) {
205 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
206 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200212#else
213 t = ctx->serial++;
214#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200215
Joe Subbianib6511b02021-07-16 15:02:55 +0100216 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200217 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200218
Neil Armstrong7cd02702022-03-04 15:08:43 +0100219#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
221 ctx->psa_hmac_alg);
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, *p - 4, 4);
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_update(&operation, cli_id, cli_id_len);
234 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500235 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100236 goto exit;
237 }
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
240 &sign_mac_length);
241 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500242 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100243 goto exit;
244 }
245
246 *p += COOKIE_HMAC_LEN;
247
248 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100249#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200250#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
252 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
253 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200254#endif
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
257 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200258
259#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
261 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
262 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
263 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200264#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100265#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200266
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100267#if defined(MBEDTLS_USE_PSA_CRYPTO)
268exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 status = psa_mac_abort(&operation);
270 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500271 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 }
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100273#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200275}
276
277/*
278 * Check a cookie
279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280int mbedtls_ssl_cookie_check(void *p_ctx,
281 const unsigned char *cookie, size_t cookie_len,
282 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200283{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100284#if defined(MBEDTLS_USE_PSA_CRYPTO)
285 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100286 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100287#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200288 unsigned char ref_hmac[COOKIE_HMAC_LEN];
289 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100290#endif
291 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200293 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (ctx == NULL || cli_id == NULL) {
296 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
297 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (cookie_len != COOKIE_LEN) {
300 return -1;
301 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200302
Neil Armstrong7cd02702022-03-04 15:08:43 +0100303#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
305 ctx->psa_hmac_alg);
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 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 status = psa_mac_update(&operation, cookie, 4);
312 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500313 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100314 goto exit;
315 }
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 status = psa_mac_update(&operation, cli_id,
318 cli_id_len);
319 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500320 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100321 goto exit;
322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 status = psa_mac_verify_finish(&operation, cookie + 4,
325 COOKIE_HMAC_LEN);
326 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500327 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100328 goto exit;
329 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100330
331 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100332#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200333#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
335 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100336 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200337#endif
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
340 &p, p + sizeof(ref_hmac),
341 cli_id, cli_id_len) != 0) {
342 ret = -1;
343 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345#if defined(MBEDTLS_THREADING_C)
346 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
347 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
348 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
349 }
350#endif
351
352 if (ret != 0) {
353 goto exit;
354 }
355
356 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100357 ret = -1;
358 goto exit;
359 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100360#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200364#else
365 cur_time = ctx->serial;
366#endif
367
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +0100368 cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100371 ret = -1;
372 goto exit;
373 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200374
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100375exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100376#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 status = psa_mac_abort(&operation);
378 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500379 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100381#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100383#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386#endif /* MBEDTLS_SSL_COOKIE_C */