blob: 82b7ea3645232eac4f6e988622286528811489f2 [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
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050040#if defined(MBEDTLS_USE_PSA_CRYPTO)
41#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
42 psa_to_ssl_errors, \
43 psa_generic_status_to_mbedtls)
44#endif
45
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020046/*
Valerio Setti543d00e2022-12-22 14:27:34 +010047 * If DTLS is in use, then at least one of SHA-256 or SHA-384 is
48 * available. Try SHA-256 first as 384 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020049 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010050#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Setti543d00e2022-12-22 14:27:34 +010051#define COOKIE_MD MBEDTLS_MD_SHA256
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020052#define COOKIE_MD_OUTLEN 32
53#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +010054#elif defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020056#define COOKIE_MD_OUTLEN 48
57#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020058#else
Valerio Setti543d00e2022-12-22 14:27:34 +010059#error "DTLS hello verify needs SHA-256 or SHA-384"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020060#endif
61
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020062/*
63 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080064 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020065 */
Gilles Peskine449bd832023-01-11 14:50:10 +010066#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020067
Gilles Peskine449bd832023-01-11 14:50:10 +010068void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020069{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010070#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010071 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010072#else
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_md_init(&ctx->hmac_ctx);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010074#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020076 ctx->serial = 0;
77#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020079
Neil Armstrong7cd02702022-03-04 15:08:43 +010080#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020081#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010082 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020083#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010084#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020085}
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020088{
89 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020090}
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020093{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010094#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +010095 psa_destroy_key(ctx->psa_hmac_key);
Neil Armstrong77b69ab2022-03-04 14:35:13 +010096#else
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020098
99#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200101#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100102#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
108 int (*f_rng)(void *, unsigned char *, size_t),
109 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200110{
Neil Armstrongd6332012022-03-04 10:26:16 +0100111#if defined(MBEDTLS_USE_PSA_CRYPTO)
112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
113 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
114 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 (void) f_rng;
117 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 alg = mbedtls_hash_info_psa_from_md(COOKIE_MD);
120 if (alg == 0) {
121 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
122 }
Neil Armstrongd6332012022-03-04 10:26:16 +0100123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
125 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
128 PSA_KEY_USAGE_SIGN_MESSAGE);
129 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
130 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
131 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if ((status = psa_generate_key(&attributes,
134 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500135 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100136 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100137#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
139 unsigned char key[COOKIE_MD_OUTLEN];
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
142 return ret;
143 }
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
146 if (ret != 0) {
147 return ret;
148 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
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 mbedtls_platform_zeroize(key, sizeof(key));
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100156#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200159}
160
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100161#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200162/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200163 * Generate the HMAC part of a cookie
164 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200165MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100166static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
167 const unsigned char time[4],
168 unsigned char **p, unsigned char *end,
169 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200170{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200171 unsigned char hmac_out[COOKIE_MD_OUTLEN];
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
176 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
177 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
178 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
179 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200180 }
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200183 *p += COOKIE_HMAC_LEN;
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200186}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100187#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200188
189/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200190 * Generate cookie for DTLS ClientHello verification
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_ssl_cookie_write(void *p_ctx,
193 unsigned char **p, unsigned char *end,
194 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200195{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100196#if defined(MBEDTLS_USE_PSA_CRYPTO)
197 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100198 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100199 size_t sign_mac_length = 0;
200#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200203 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (ctx == NULL || cli_id == NULL) {
206 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
207 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200213#else
214 t = ctx->serial++;
215#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200216
Joe Subbianib6511b02021-07-16 15:02:55 +0100217 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200218 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200219
Neil Armstrong7cd02702022-03-04 15:08:43 +0100220#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
222 ctx->psa_hmac_alg);
223 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500224 ret = PSA_TO_MBEDTLS_ERR(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, *p - 4, 4);
229 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500230 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100231 goto exit;
232 }
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 status = psa_mac_update(&operation, cli_id, cli_id_len);
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
241 &sign_mac_length);
242 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500243 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100244 goto exit;
245 }
246
247 *p += COOKIE_HMAC_LEN;
248
249 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100250#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200251#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
253 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
254 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200255#endif
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
258 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200259
260#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
262 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
263 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
264 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200265#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100266#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200267
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100268#if defined(MBEDTLS_USE_PSA_CRYPTO)
269exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 status = psa_mac_abort(&operation);
271 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500272 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 }
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100274#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200276}
277
278/*
279 * Check a cookie
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281int mbedtls_ssl_cookie_check(void *p_ctx,
282 const unsigned char *cookie, size_t cookie_len,
283 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200284{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100285#if defined(MBEDTLS_USE_PSA_CRYPTO)
286 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100287 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100288#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200289 unsigned char ref_hmac[COOKIE_HMAC_LEN];
290 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100291#endif
292 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200294 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (ctx == NULL || cli_id == NULL) {
297 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
298 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (cookie_len != COOKIE_LEN) {
301 return -1;
302 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200303
Neil Armstrong7cd02702022-03-04 15:08:43 +0100304#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
306 ctx->psa_hmac_alg);
307 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500308 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100309 goto exit;
310 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 status = psa_mac_update(&operation, cookie, 4);
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_update(&operation, cli_id,
319 cli_id_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 }
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 status = psa_mac_verify_finish(&operation, cookie + 4,
326 COOKIE_HMAC_LEN);
327 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500328 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100329 goto exit;
330 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100331
332 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100333#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200334#if defined(MBEDTLS_THREADING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
336 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100337 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200338#endif
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
341 &p, p + sizeof(ref_hmac),
342 cli_id, cli_id_len) != 0) {
343 ret = -1;
344 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346#if defined(MBEDTLS_THREADING_C)
347 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
348 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
349 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
350 }
351#endif
352
353 if (ret != 0) {
354 goto exit;
355 }
356
357 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100358 ret = -1;
359 goto exit;
360 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100361#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200365#else
366 cur_time = ctx->serial;
367#endif
368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 cookie_time = ((unsigned long) cookie[0] << 24) |
370 ((unsigned long) cookie[1] << 16) |
371 ((unsigned long) cookie[2] << 8) |
372 ((unsigned long) cookie[3]);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100375 ret = -1;
376 goto exit;
377 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200378
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100379exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100380#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 status = psa_mac_abort(&operation);
382 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500383 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100385#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200389}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#endif /* MBEDTLS_SSL_COOKIE_C */