blob: 371edce3a9d4ac9668ebce97709ae3a6c738b6f1 [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 Kurek00644842023-05-30 05:45:00 -040040/* Define a local translating function to save code size by not using too many
41 * arguments in each translating place. */
42static int local_err_translation(psa_status_t status)
43{
44 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
45 sizeof(psa_to_ssl_errors),
46 psa_generic_status_to_mbedtls);
47}
48#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050049#endif
50
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020051/*
Valerio Setti543d00e2022-12-22 14:27:34 +010052 * If DTLS is in use, then at least one of SHA-256 or SHA-384 is
53 * available. Try SHA-256 first as 384 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020054 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010055#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Setti543d00e2022-12-22 14:27:34 +010056#define COOKIE_MD MBEDTLS_MD_SHA256
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020057#define COOKIE_MD_OUTLEN 32
58#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010059#elif defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020061#define COOKIE_MD_OUTLEN 48
62#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020063#else
Valerio Setti543d00e2022-12-22 14:27:34 +010064#error "DTLS hello verify needs SHA-256 or SHA-384"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020065#endif
66
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020067/*
68 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080069 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020072
Gilles Peskine449bd832023-01-11 14:50:10 +010073void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020074{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010075#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010076 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010077#else
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_md_init(&ctx->hmac_ctx);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010079#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020081 ctx->serial = 0;
82#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020084
Neil Armstrong7cd02702022-03-04 15:08:43 +010085#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020086#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020088#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010089#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020090}
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020093{
94 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020095}
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020098{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 psa_destroy_key(ctx->psa_hmac_key);
Neil Armstrong77b69ab2022-03-04 14:35:13 +0100101#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200103
104#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200106#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100107#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200110}
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
113 int (*f_rng)(void *, unsigned char *, size_t),
114 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115{
Neil Armstrongd6332012022-03-04 10:26:16 +0100116#if defined(MBEDTLS_USE_PSA_CRYPTO)
117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
119 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 (void) f_rng;
122 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200123
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200124 alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (alg == 0) {
126 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
127 }
Neil Armstrongd6332012022-03-04 10:26:16 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
130 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
133 PSA_KEY_USAGE_SIGN_MESSAGE);
134 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
135 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
136 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((status = psa_generate_key(&attributes,
139 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500140 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100141 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100142#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
144 unsigned char key[COOKIE_MD_OUTLEN];
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
147 return ret;
148 }
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
151 if (ret != 0) {
152 return ret;
153 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
156 if (ret != 0) {
157 return ret;
158 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_platform_zeroize(key, sizeof(key));
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100161#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200164}
165
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100166#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200167/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200168 * Generate the HMAC part of a cookie
169 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200170MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100171static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
172 const unsigned char time[4],
173 unsigned char **p, unsigned char *end,
174 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200175{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200176 unsigned char hmac_out[COOKIE_MD_OUTLEN];
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
181 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
182 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
183 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
184 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200185 }
186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200188 *p += COOKIE_HMAC_LEN;
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200191}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100192#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200193
194/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200195 * Generate cookie for DTLS ClientHello verification
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_ssl_cookie_write(void *p_ctx,
198 unsigned char **p, unsigned char *end,
199 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200200{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100201#if defined(MBEDTLS_USE_PSA_CRYPTO)
202 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100204 size_t sign_mac_length = 0;
205#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200208 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (ctx == NULL || cli_id == NULL) {
211 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
212 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200218#else
219 t = ctx->serial++;
220#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200221
Joe Subbianib6511b02021-07-16 15:02:55 +0100222 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200223 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200224
Neil Armstrong7cd02702022-03-04 15:08:43 +0100225#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
227 ctx->psa_hmac_alg);
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, *p - 4, 4);
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_update(&operation, cli_id, cli_id_len);
240 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500241 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100242 goto exit;
243 }
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
246 &sign_mac_length);
247 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500248 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100249 goto exit;
250 }
251
252 *p += COOKIE_HMAC_LEN;
253
254 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100255#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200256#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
258 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
259 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200260#endif
261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
263 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200264
265#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
267 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
268 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
269 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200270#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100271#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200272
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100273#if defined(MBEDTLS_USE_PSA_CRYPTO)
274exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 status = psa_mac_abort(&operation);
276 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500277 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 }
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100279#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200281}
282
283/*
284 * Check a cookie
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286int mbedtls_ssl_cookie_check(void *p_ctx,
287 const unsigned char *cookie, size_t cookie_len,
288 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200289{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100290#if defined(MBEDTLS_USE_PSA_CRYPTO)
291 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100292 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100293#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200294 unsigned char ref_hmac[COOKIE_HMAC_LEN];
295 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100296#endif
297 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200299 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (ctx == NULL || cli_id == NULL) {
302 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
303 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (cookie_len != COOKIE_LEN) {
306 return -1;
307 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200308
Neil Armstrong7cd02702022-03-04 15:08:43 +0100309#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
311 ctx->psa_hmac_alg);
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 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 status = psa_mac_update(&operation, cookie, 4);
318 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500319 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100320 goto exit;
321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 status = psa_mac_update(&operation, cli_id,
324 cli_id_len);
325 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500326 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100327 goto exit;
328 }
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 status = psa_mac_verify_finish(&operation, cookie + 4,
331 COOKIE_HMAC_LEN);
332 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500333 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100334 goto exit;
335 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100336
337 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100338#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200339#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
341 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100342 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200343#endif
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
346 &p, p + sizeof(ref_hmac),
347 cli_id, cli_id_len) != 0) {
348 ret = -1;
349 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351#if defined(MBEDTLS_THREADING_C)
352 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
353 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
354 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
355 }
356#endif
357
358 if (ret != 0) {
359 goto exit;
360 }
361
362 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100363 ret = -1;
364 goto exit;
365 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100366#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200370#else
371 cur_time = ctx->serial;
372#endif
373
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +0100374 cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100377 ret = -1;
378 goto exit;
379 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200380
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100381exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100382#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 status = psa_mac_abort(&operation);
384 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500385 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100387#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100389#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#endif /* MBEDTLS_SSL_COOKIE_C */