blob: e00ee43656ca4e4240049c5f7ff746e416cc7a10 [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
Andrzej Kurek25f27152022-08-17 16:09:31 -040041#if defined(MBEDTLS_USE_PSA_CRYPTO)
42#include "psa/crypto.h"
43#endif
44#include "legacy_or_psa.h"
45
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000046#include <string.h>
47
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020048/*
49 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
TRodziewicz0f82ec62021-05-12 17:49:18 +020050 * available. Try SHA-256 first, 512 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020051 */
Andrzej Kurek25f27152022-08-17 16:09:31 -040052#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020054#define COOKIE_MD_OUTLEN 32
55#define COOKIE_HMAC_LEN 28
Andrzej Kurek25f27152022-08-17 16:09:31 -040056#elif defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020058#define COOKIE_MD_OUTLEN 48
59#define COOKIE_HMAC_LEN 28
Andrzej Kurek25f27152022-08-17 16:09:31 -040060#elif defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020062#define COOKIE_MD_OUTLEN 20
63#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020064#else
65#error "DTLS hello verify needs SHA-1 or SHA-2"
66#endif
67
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020068/*
69 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080070 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020071 */
72#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020075{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010076#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +010077 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong77b69ab2022-03-04 14:35:13 +010078#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_md_init( &ctx->hmac_ctx );
Neil Armstrong77b69ab2022-03-04 14:35:13 +010080#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020082 ctx->serial = 0;
83#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020085
Neil Armstrong7cd02702022-03-04 15:08:43 +010086#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020087#if defined(MBEDTLS_THREADING_C)
88 mbedtls_mutex_init( &ctx->mutex );
89#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +010090#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020091}
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020094{
95 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020096}
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020099{
Neil Armstrongbca99ee2022-03-04 10:20:20 +0100100#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100101 psa_destroy_key( ctx->psa_hmac_key );
Neil Armstrong77b69ab2022-03-04 14:35:13 +0100102#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200104
105#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +0200106 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200107#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100108#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200109
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500110 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200114 int (*f_rng)(void *, unsigned char *, size_t),
115 void *p_rng )
116{
Neil Armstrongd6332012022-03-04 10:26:16 +0100117#if defined(MBEDTLS_USE_PSA_CRYPTO)
118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
119 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
120 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200121
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100122 (void)f_rng;
123 (void)p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200124
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200125 alg = mbedtls_hash_info_psa_from_md( COOKIE_MD );
Neil Armstrongd6332012022-03-04 10:26:16 +0100126 if( alg == 0 )
127 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
128
Neil Armstrongbe52f502022-03-07 14:17:26 +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
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100132 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
133 PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstrongbe52f502022-03-07 14:17:26 +0100134 psa_set_key_algorithm( &attributes, ctx->psa_hmac_alg );
Neil Armstrongd6332012022-03-04 10:26:16 +0100135 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100136 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( COOKIE_MD_OUTLEN ) );
Neil Armstrongd6332012022-03-04 10:26:16 +0100137
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100138 if( ( status = psa_generate_key( &attributes,
Neil Armstrong488a40e2022-03-22 10:41:38 +0100139 &ctx->psa_hmac_key ) ) != PSA_SUCCESS )
Neil Armstrongd6332012022-03-04 10:26:16 +0100140 {
141 return psa_ssl_status_to_mbedtls( status );
142 }
Neil Armstrong23d34ce2022-03-04 10:32:26 +0100143#else
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
145 unsigned char key[COOKIE_MD_OUTLEN];
146
147 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
148 return( ret );
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200151 if( ret != 0 )
152 return( ret );
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200155 if( ret != 0 )
156 return( ret );
157
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500158 mbedtls_platform_zeroize( key, sizeof( key ) );
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200160
161 return( 0 );
162}
163
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100164#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200165/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200166 * Generate the HMAC part of a cookie
167 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200168MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200170 const unsigned char time[4],
171 unsigned char **p, unsigned char *end,
172 const unsigned char *cli_id, size_t cli_id_len )
173{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200174 unsigned char hmac_out[COOKIE_MD_OUTLEN];
175
Hanno Becker261602c2017-04-12 14:54:42 +0100176 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200177
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200178 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
179 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
180 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
181 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200184 }
185
186 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
187 *p += COOKIE_HMAC_LEN;
188
189 return( 0 );
190}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100191#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200192
193/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200194 * Generate cookie for DTLS ClientHello verification
195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200197 unsigned char **p, unsigned char *end,
198 const unsigned char *cli_id, size_t cli_id_len )
199{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100200#if defined(MBEDTLS_USE_PSA_CRYPTO)
201 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100202 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100203 size_t sign_mac_length = 0;
204#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200207 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200208
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200209 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200211
Hanno Becker261602c2017-04-12 14:54:42 +0100212 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100215 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200216#else
217 t = ctx->serial++;
218#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200219
Joe Subbianib6511b02021-07-16 15:02:55 +0100220 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200221 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200222
Neil Armstrong7cd02702022-03-04 15:08:43 +0100223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100224 status = psa_mac_sign_setup( &operation, ctx->psa_hmac_key,
Neil Armstrong79daea22022-03-21 12:05:51 +0100225 ctx->psa_hmac_alg );
226 if( status != PSA_SUCCESS )
227 {
228 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100229 goto exit;
230 }
231
Neil Armstrong79daea22022-03-21 12:05:51 +0100232 status = psa_mac_update( &operation, *p - 4, 4 );
233 if( status != PSA_SUCCESS )
234 {
235 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100236 goto exit;
237 }
238
Neil Armstrong79daea22022-03-21 12:05:51 +0100239 status = psa_mac_update( &operation, cli_id, cli_id_len );
240 if( status != PSA_SUCCESS )
241 {
242 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100243 goto exit;
244 }
245
Neil Armstrong79daea22022-03-21 12:05:51 +0100246 status = psa_mac_sign_finish( &operation, *p, COOKIE_MD_OUTLEN,
247 &sign_mac_length );
248 if( status != PSA_SUCCESS )
249 {
250 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100251 goto exit;
252 }
253
254 *p += COOKIE_HMAC_LEN;
255
256 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100257#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200258#if defined(MBEDTLS_THREADING_C)
259 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100260 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200261#endif
262
263 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
264 p, end, cli_id, cli_id_len );
265
266#if defined(MBEDTLS_THREADING_C)
267 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100268 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
269 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
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:
Neil Armstrong79daea22022-03-21 12:05:51 +0100275 status = psa_mac_abort( &operation );
276 if( status != PSA_SUCCESS )
277 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100278#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200279 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200280}
281
282/*
283 * Check a cookie
284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200286 const unsigned char *cookie, size_t cookie_len,
287 const unsigned char *cli_id, size_t cli_id_len )
288{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100289#if defined(MBEDTLS_USE_PSA_CRYPTO)
290 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100292#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200293 unsigned char ref_hmac[COOKIE_HMAC_LEN];
294 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100295#endif
296 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200298 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200299
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200300 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200302
303 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200304 return( -1 );
305
Neil Armstrong7cd02702022-03-04 15:08:43 +0100306#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100307 status = psa_mac_verify_setup( &operation, ctx->psa_hmac_key,
Neil Armstrong79daea22022-03-21 12:05:51 +0100308 ctx->psa_hmac_alg );
309 if( status != PSA_SUCCESS )
310 {
311 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100312 goto exit;
313 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100314
Neil Armstrong79daea22022-03-21 12:05:51 +0100315 status = psa_mac_update( &operation, cookie, 4 );
316 if( status != PSA_SUCCESS )
317 {
318 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100319 goto exit;
320 }
321
Neil Armstrong79daea22022-03-21 12:05:51 +0100322 status = psa_mac_update( &operation, cli_id,
323 cli_id_len );
324 if( status != PSA_SUCCESS )
325 {
326 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100327 goto exit;
328 }
329
Neil Armstrong79daea22022-03-21 12:05:51 +0100330 status = psa_mac_verify_finish( &operation, cookie + 4,
331 COOKIE_HMAC_LEN );
332 if( status != PSA_SUCCESS )
333 {
334 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100335 goto exit;
336 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100337
338 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100339#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200340#if defined(MBEDTLS_THREADING_C)
341 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100342 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200343#endif
344
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200345 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
346 &p, p + sizeof( ref_hmac ),
347 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200348 ret = -1;
349
350#if defined(MBEDTLS_THREADING_C)
351 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100352 {
353 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
354 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
355 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200356#endif
357
358 if( ret != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100359 goto exit;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200360
Gabor Mezei90437e32021-10-20 11:59:27 +0200361 if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100362 {
363 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)
SimonBd5800b72016-04-26 07:43:27 +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
374 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
375 ( (unsigned long) cookie[1] << 16 ) |
376 ( (unsigned long) cookie[2] << 8 ) |
377 ( (unsigned long) cookie[3] );
378
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200379 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100380 {
381 ret = -1;
382 goto exit;
383 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200384
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100385exit:
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100386#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong79daea22022-03-21 12:05:51 +0100387 status = psa_mac_abort( &operation );
388 if( status != PSA_SUCCESS )
389 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100390#else
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100391 mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100392#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100393 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#endif /* MBEDTLS_SSL_COOKIE_C */