blob: f68ae71cc466caf1ac89e6625f7d731d9a45c011 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020030#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020031#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010032#define mbedtls_free free
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020033#endif
34
SimonBd5800b72016-04-26 07:43:27 +010035#include "mbedtls/ssl_cookie.h"
Chris Jones84a773f2021-03-05 18:38:47 +000036#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050038#include "mbedtls/platform_util.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020039#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010040
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000041#include <string.h>
42
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020043/*
44 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
TRodziewicz0f82ec62021-05-12 17:49:18 +020045 * available. Try SHA-256 first, 512 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020046 */
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020047#if defined(MBEDTLS_SHA224_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020049#define COOKIE_MD_OUTLEN 32
50#define COOKIE_HMAC_LEN 28
Mateusz Starzyk6326a8d2021-05-10 13:51:53 +020051#elif defined(MBEDTLS_SHA384_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020053#define COOKIE_MD_OUTLEN 48
54#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#elif defined(MBEDTLS_SHA1_C)
56#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020057#define COOKIE_MD_OUTLEN 20
58#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020059#else
60#error "DTLS hello verify needs SHA-1 or SHA-2"
61#endif
62
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020063/*
64 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080065 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020066 */
67#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020070{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010071#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010072 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010073#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_md_init( &ctx->hmac_ctx );
Neil Armstrong77b69ab2022-03-04 14:35:13 +010075#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020077 ctx->serial = 0;
78#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020080
Neil Armstrong7cd02702022-03-04 15:08:43 +010081#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020082#if defined(MBEDTLS_THREADING_C)
83 mbedtls_mutex_init( &ctx->mutex );
84#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010085#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020086}
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020089{
90 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020091}
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020094{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010095#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010096 psa_destroy_key( ctx->psa_hmac_key );
Neil Armstrong77b69ab2022-03-04 14:35:13 +010097#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020099
100#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +0200101 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200102#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100103#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200104
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500105 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200106}
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200109 int (*f_rng)(void *, unsigned char *, size_t),
110 void *p_rng )
111{
Neil Armstrongd6332012022-03-04 10:26:16 +0100112#if defined(MBEDTLS_USE_PSA_CRYPTO)
113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
114 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
115 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200116
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100117 (void)f_rng;
118 (void)p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200119
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200120 alg = mbedtls_hash_info_psa_from_md( COOKIE_MD );
Neil Armstrongd6332012022-03-04 10:26:16 +0100121 if( alg == 0 )
122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
123
Neil Armstrongbe52f502022-03-07 14:17:26 +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
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100127 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
128 PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstrongbe52f502022-03-07 14:17:26 +0100129 psa_set_key_algorithm( &attributes, ctx->psa_hmac_alg );
Neil Armstrongd6332012022-03-04 10:26:16 +0100130 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100131 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( COOKIE_MD_OUTLEN ) );
Neil Armstrongd6332012022-03-04 10:26:16 +0100132
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100133 if( ( status = psa_generate_key( &attributes,
Neil Armstrong488a40e2022-03-22 10:41:38 +0100134 &ctx->psa_hmac_key ) ) != PSA_SUCCESS )
Neil Armstrongd6332012022-03-04 10:26:16 +0100135 {
136 return psa_ssl_status_to_mbedtls( status );
137 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100138#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
140 unsigned char key[COOKIE_MD_OUTLEN];
141
142 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
143 return( ret );
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200146 if( ret != 0 )
147 return( ret );
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200150 if( ret != 0 )
151 return( ret );
152
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500153 mbedtls_platform_zeroize( key, sizeof( key ) );
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100154#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200155
156 return( 0 );
157}
158
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100159#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200160/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200161 * Generate the HMAC part of a cookie
162 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200163MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200165 const unsigned char time[4],
166 unsigned char **p, unsigned char *end,
167 const unsigned char *cli_id, size_t cli_id_len )
168{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200169 unsigned char hmac_out[COOKIE_MD_OUTLEN];
170
Hanno Becker261602c2017-04-12 14:54:42 +0100171 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200172
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200173 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
174 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
175 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
176 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200179 }
180
181 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
182 *p += COOKIE_HMAC_LEN;
183
184 return( 0 );
185}
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200192 unsigned char **p, unsigned char *end,
193 const unsigned char *cli_id, size_t cli_id_len )
194{
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
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200204 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200206
Hanno Becker261602c2017-04-12 14:54:42 +0100207 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100210 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200211#else
212 t = ctx->serial++;
213#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200214
Joe Subbianib6511b02021-07-16 15:02:55 +0100215 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200216 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200217
Neil Armstrong7cd02702022-03-04 15:08:43 +0100218#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100219 status = psa_mac_sign_setup( &operation, ctx->psa_hmac_key,
Neil Armstrong79daea22022-03-21 12:05:51 +0100220 ctx->psa_hmac_alg );
221 if( status != PSA_SUCCESS )
222 {
223 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100224 goto exit;
225 }
226
Neil Armstrong79daea22022-03-21 12:05:51 +0100227 status = psa_mac_update( &operation, *p - 4, 4 );
228 if( status != PSA_SUCCESS )
229 {
230 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100231 goto exit;
232 }
233
Neil Armstrong79daea22022-03-21 12:05:51 +0100234 status = psa_mac_update( &operation, cli_id, cli_id_len );
235 if( status != PSA_SUCCESS )
236 {
237 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100238 goto exit;
239 }
240
Neil Armstrong79daea22022-03-21 12:05:51 +0100241 status = psa_mac_sign_finish( &operation, *p, COOKIE_MD_OUTLEN,
242 &sign_mac_length );
243 if( status != PSA_SUCCESS )
244 {
245 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100246 goto exit;
247 }
248
249 *p += COOKIE_HMAC_LEN;
250
251 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100252#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200253#if defined(MBEDTLS_THREADING_C)
254 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100255 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200256#endif
257
258 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
259 p, end, cli_id, cli_id_len );
260
261#if defined(MBEDTLS_THREADING_C)
262 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100263 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
264 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
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:
Neil Armstrong79daea22022-03-21 12:05:51 +0100270 status = psa_mac_abort( &operation );
271 if( status != PSA_SUCCESS )
272 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100273#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200274 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200275}
276
277/*
278 * Check a cookie
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200281 const unsigned char *cookie, size_t cookie_len,
282 const unsigned char *cli_id, size_t cli_id_len )
283{
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
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200295 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200297
298 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200299 return( -1 );
300
Neil Armstrong7cd02702022-03-04 15:08:43 +0100301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100302 status = psa_mac_verify_setup( &operation, ctx->psa_hmac_key,
Neil Armstrong79daea22022-03-21 12:05:51 +0100303 ctx->psa_hmac_alg );
304 if( status != PSA_SUCCESS )
305 {
306 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100307 goto exit;
308 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100309
Neil Armstrong79daea22022-03-21 12:05:51 +0100310 status = psa_mac_update( &operation, cookie, 4 );
311 if( status != PSA_SUCCESS )
312 {
313 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100314 goto exit;
315 }
316
Neil Armstrong79daea22022-03-21 12:05:51 +0100317 status = psa_mac_update( &operation, cli_id,
318 cli_id_len );
319 if( status != PSA_SUCCESS )
320 {
321 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100322 goto exit;
323 }
324
Neil Armstrong79daea22022-03-21 12:05:51 +0100325 status = psa_mac_verify_finish( &operation, cookie + 4,
326 COOKIE_HMAC_LEN );
327 if( status != PSA_SUCCESS )
328 {
329 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100330 goto exit;
331 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100332
333 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100334#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200335#if defined(MBEDTLS_THREADING_C)
336 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100337 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200338#endif
339
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200340 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
341 &p, p + sizeof( ref_hmac ),
342 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200343 ret = -1;
344
345#if defined(MBEDTLS_THREADING_C)
346 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100347 {
348 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
349 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
350 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200351#endif
352
353 if( ret != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100354 goto exit;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200355
Gabor Mezei90437e32021-10-20 11:59:27 +0200356 if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100357 {
358 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)
SimonBd5800b72016-04-26 07:43:27 +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
369 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
370 ( (unsigned long) cookie[1] << 16 ) |
371 ( (unsigned long) cookie[2] << 8 ) |
372 ( (unsigned long) cookie[3] );
373
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200374 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100375 {
376 ret = -1;
377 goto exit;
378 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200379
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100380exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100381#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong79daea22022-03-21 12:05:51 +0100382 status = psa_mac_abort( &operation );
383 if( status != PSA_SUCCESS )
384 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100385#else
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100386 mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100387#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskinec2f7b752021-12-13 12:35:08 +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 */