blob: b7eead402ea00e340cb67c6f7fefbfc9c57b0419 [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é-Gonnard07018f92022-09-15 11:29:35 +020036#include "mbedtls/legacy_or_psa.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040037
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000038#include <string.h>
39
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020040/*
Valerio Setti543d00e2022-12-22 14:27:34 +010041 * If DTLS is in use, then at least one of SHA-256 or SHA-384 is
42 * available. Try SHA-256 first as 384 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020043 */
Valerio Setti543d00e2022-12-22 14:27:34 +010044#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA)
45#define COOKIE_MD MBEDTLS_MD_SHA256
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020046#define COOKIE_MD_OUTLEN 32
47#define COOKIE_HMAC_LEN 28
Andrzej Kurek25f27152022-08-17 16:09:31 -040048#elif defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020050#define COOKIE_MD_OUTLEN 48
51#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020052#else
Valerio Setti543d00e2022-12-22 14:27:34 +010053#error "DTLS hello verify needs SHA-256 or SHA-384"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020054#endif
55
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020056/*
57 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080058 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020059 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020061
Gilles Peskine449bd832023-01-11 14:50:10 +010062void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020063{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010064#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010065 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010066#else
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_md_init(&ctx->hmac_ctx);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010068#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020070 ctx->serial = 0;
71#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020073
Neil Armstrong7cd02702022-03-04 15:08:43 +010074#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020075#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020077#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010078#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020079}
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020082{
83 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020084}
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020087{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010088#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010089 psa_destroy_key(ctx->psa_hmac_key);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010090#else
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020092
93#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020095#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010096#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
102 int (*f_rng)(void *, unsigned char *, size_t),
103 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200104{
Neil Armstrongd6332012022-03-04 10:26:16 +0100105#if defined(MBEDTLS_USE_PSA_CRYPTO)
106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
108 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 (void) f_rng;
111 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 alg = mbedtls_hash_info_psa_from_md(COOKIE_MD);
114 if (alg == 0) {
115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
116 }
Neil Armstrongd6332012022-03-04 10:26:16 +0100117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
119 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
122 PSA_KEY_USAGE_SIGN_MESSAGE);
123 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
124 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
125 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if ((status = psa_generate_key(&attributes,
128 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
129 return psa_ssl_status_to_mbedtls(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100130 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100131#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
133 unsigned char key[COOKIE_MD_OUTLEN];
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
136 return ret;
137 }
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
140 if (ret != 0) {
141 return ret;
142 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
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 mbedtls_platform_zeroize(key, sizeof(key));
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100150#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200153}
154
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100155#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200156/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200157 * Generate the HMAC part of a cookie
158 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200159MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100160static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
161 const unsigned char time[4],
162 unsigned char **p, unsigned char *end,
163 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200164{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200165 unsigned char hmac_out[COOKIE_MD_OUTLEN];
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
170 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
171 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
172 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
173 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200174 }
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200177 *p += COOKIE_HMAC_LEN;
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200180}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100181#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200182
183/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200184 * Generate cookie for DTLS ClientHello verification
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int mbedtls_ssl_cookie_write(void *p_ctx,
187 unsigned char **p, unsigned char *end,
188 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200189{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100190#if defined(MBEDTLS_USE_PSA_CRYPTO)
191 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100192 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100193 size_t sign_mac_length = 0;
194#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200197 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (ctx == NULL || cli_id == NULL) {
200 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
201 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200207#else
208 t = ctx->serial++;
209#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200210
Joe Subbianib6511b02021-07-16 15:02:55 +0100211 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200212 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200213
Neil Armstrong7cd02702022-03-04 15:08:43 +0100214#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
216 ctx->psa_hmac_alg);
217 if (status != PSA_SUCCESS) {
218 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100219 goto exit;
220 }
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 status = psa_mac_update(&operation, *p - 4, 4);
223 if (status != PSA_SUCCESS) {
224 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100225 goto exit;
226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 status = psa_mac_update(&operation, cli_id, cli_id_len);
229 if (status != PSA_SUCCESS) {
230 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100231 goto exit;
232 }
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
235 &sign_mac_length);
236 if (status != PSA_SUCCESS) {
237 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100238 goto exit;
239 }
240
241 *p += COOKIE_HMAC_LEN;
242
243 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100244#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200245#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
247 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
248 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200249#endif
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
252 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200253
254#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
256 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
257 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
258 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200259#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100260#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200261
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100262#if defined(MBEDTLS_USE_PSA_CRYPTO)
263exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 status = psa_mac_abort(&operation);
265 if (status != PSA_SUCCESS) {
266 ret = psa_ssl_status_to_mbedtls(status);
267 }
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100268#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200270}
271
272/*
273 * Check a cookie
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275int mbedtls_ssl_cookie_check(void *p_ctx,
276 const unsigned char *cookie, size_t cookie_len,
277 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200278{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100279#if defined(MBEDTLS_USE_PSA_CRYPTO)
280 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100282#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200283 unsigned char ref_hmac[COOKIE_HMAC_LEN];
284 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100285#endif
286 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200288 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (ctx == NULL || cli_id == NULL) {
291 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
292 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (cookie_len != COOKIE_LEN) {
295 return -1;
296 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200297
Neil Armstrong7cd02702022-03-04 15:08:43 +0100298#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
300 ctx->psa_hmac_alg);
301 if (status != PSA_SUCCESS) {
302 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100303 goto exit;
304 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = psa_mac_update(&operation, cookie, 4);
307 if (status != PSA_SUCCESS) {
308 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100309 goto exit;
310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 status = psa_mac_update(&operation, cli_id,
313 cli_id_len);
314 if (status != PSA_SUCCESS) {
315 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100316 goto exit;
317 }
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 status = psa_mac_verify_finish(&operation, cookie + 4,
320 COOKIE_HMAC_LEN);
321 if (status != PSA_SUCCESS) {
322 ret = psa_ssl_status_to_mbedtls(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100323 goto exit;
324 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100325
326 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100327#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200328#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
330 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100331 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200332#endif
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
335 &p, p + sizeof(ref_hmac),
336 cli_id, cli_id_len) != 0) {
337 ret = -1;
338 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340#if defined(MBEDTLS_THREADING_C)
341 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
342 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
343 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
344 }
345#endif
346
347 if (ret != 0) {
348 goto exit;
349 }
350
351 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100352 ret = -1;
353 goto exit;
354 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100355#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200359#else
360 cur_time = ctx->serial;
361#endif
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 cookie_time = ((unsigned long) cookie[0] << 24) |
364 ((unsigned long) cookie[1] << 16) |
365 ((unsigned long) cookie[2] << 8) |
366 ((unsigned long) cookie[3]);
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) {
377 ret = psa_ssl_status_to_mbedtls(status);
378 }
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 */