blob: a996174eb3e568c52cbec3d0454ce01e26c21551 [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 */
60#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062void 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 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)
76 mbedtls_mutex_init( &ctx->mutex );
77#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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086void 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)
Neil Armstrong488a40e2022-03-22 10:41:38 +010089 psa_destroy_key( ctx->psa_hmac_key );
Neil Armstrong77b69ab2022-03-04 14:35:13 +010090#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020092
93#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +020094 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
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050098 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020099}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200102 int (*f_rng)(void *, unsigned char *, size_t),
103 void *p_rng )
104{
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
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100110 (void)f_rng;
111 (void)p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200112
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200113 alg = mbedtls_hash_info_psa_from_md( COOKIE_MD );
Neil Armstrongd6332012022-03-04 10:26:16 +0100114 if( alg == 0 )
115 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
116
Neil Armstrongbe52f502022-03-07 14:17:26 +0100117 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC( PSA_ALG_HMAC( alg ),
118 COOKIE_HMAC_LEN );
Neil Armstrongd6332012022-03-04 10:26:16 +0100119
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100120 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
121 PSA_KEY_USAGE_SIGN_MESSAGE );
Neil Armstrongbe52f502022-03-07 14:17:26 +0100122 psa_set_key_algorithm( &attributes, ctx->psa_hmac_alg );
Neil Armstrongd6332012022-03-04 10:26:16 +0100123 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100124 psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( COOKIE_MD_OUTLEN ) );
Neil Armstrongd6332012022-03-04 10:26:16 +0100125
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100126 if( ( status = psa_generate_key( &attributes,
Neil Armstrong488a40e2022-03-22 10:41:38 +0100127 &ctx->psa_hmac_key ) ) != PSA_SUCCESS )
Neil Armstrongd6332012022-03-04 10:26:16 +0100128 {
129 return psa_ssl_status_to_mbedtls( status );
130 }
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
135 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
136 return( ret );
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200139 if( ret != 0 )
140 return( ret );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200143 if( ret != 0 )
144 return( ret );
145
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500146 mbedtls_platform_zeroize( key, sizeof( key ) );
Neil Armstrong2217d6f2022-03-04 15:00:22 +0100147#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200148
149 return( 0 );
150}
151
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100152#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200153/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200154 * Generate the HMAC part of a cookie
155 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200156MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200158 const unsigned char time[4],
159 unsigned char **p, unsigned char *end,
160 const unsigned char *cli_id, size_t cli_id_len )
161{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200162 unsigned char hmac_out[COOKIE_MD_OUTLEN];
163
Hanno Becker261602c2017-04-12 14:54:42 +0100164 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200165
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200166 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
167 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
168 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
169 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200172 }
173
174 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
175 *p += COOKIE_HMAC_LEN;
176
177 return( 0 );
178}
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100179#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200180
181/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200182 * Generate cookie for DTLS ClientHello verification
183 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200185 unsigned char **p, unsigned char *end,
186 const unsigned char *cli_id, size_t cli_id_len )
187{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100188#if defined(MBEDTLS_USE_PSA_CRYPTO)
189 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100191 size_t sign_mac_length = 0;
192#endif
Janos Follath865b3eb2019-12-16 11:46:15 +0000193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200195 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200196
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200197 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200199
Hanno Becker261602c2017-04-12 14:54:42 +0100200 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100203 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200204#else
205 t = ctx->serial++;
206#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200207
Joe Subbianib6511b02021-07-16 15:02:55 +0100208 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200209 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200210
Neil Armstrong7cd02702022-03-04 15:08:43 +0100211#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100212 status = psa_mac_sign_setup( &operation, ctx->psa_hmac_key,
Neil Armstrong79daea22022-03-21 12:05:51 +0100213 ctx->psa_hmac_alg );
214 if( status != PSA_SUCCESS )
215 {
216 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100217 goto exit;
218 }
219
Neil Armstrong79daea22022-03-21 12:05:51 +0100220 status = psa_mac_update( &operation, *p - 4, 4 );
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, cli_id, cli_id_len );
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_sign_finish( &operation, *p, COOKIE_MD_OUTLEN,
235 &sign_mac_length );
236 if( status != PSA_SUCCESS )
237 {
238 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100239 goto exit;
240 }
241
242 *p += COOKIE_HMAC_LEN;
243
244 ret = 0;
Neil Armstrong7cd02702022-03-04 15:08:43 +0100245#else
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200246#if defined(MBEDTLS_THREADING_C)
247 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100248 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200249#endif
250
251 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
252 p, end, cli_id, cli_id_len );
253
254#if defined(MBEDTLS_THREADING_C)
255 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100256 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
257 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200258#endif
Neil Armstrong7cd02702022-03-04 15:08:43 +0100259#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200260
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100261#if defined(MBEDTLS_USE_PSA_CRYPTO)
262exit:
Neil Armstrong79daea22022-03-21 12:05:51 +0100263 status = psa_mac_abort( &operation );
264 if( status != PSA_SUCCESS )
265 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100266#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200267 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200268}
269
270/*
271 * Check a cookie
272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200274 const unsigned char *cookie, size_t cookie_len,
275 const unsigned char *cli_id, size_t cli_id_len )
276{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100277#if defined(MBEDTLS_USE_PSA_CRYPTO)
278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100280#else
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200281 unsigned char ref_hmac[COOKIE_HMAC_LEN];
282 unsigned char *p = ref_hmac;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100283#endif
284 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200286 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200287
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200288 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200290
291 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200292 return( -1 );
293
Neil Armstrong7cd02702022-03-04 15:08:43 +0100294#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong488a40e2022-03-22 10:41:38 +0100295 status = psa_mac_verify_setup( &operation, ctx->psa_hmac_key,
Neil Armstrong79daea22022-03-21 12:05:51 +0100296 ctx->psa_hmac_alg );
297 if( status != PSA_SUCCESS )
298 {
299 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100300 goto exit;
301 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100302
Neil Armstrong79daea22022-03-21 12:05:51 +0100303 status = psa_mac_update( &operation, cookie, 4 );
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 }
309
Neil Armstrong79daea22022-03-21 12:05:51 +0100310 status = psa_mac_update( &operation, cli_id,
311 cli_id_len );
312 if( status != PSA_SUCCESS )
313 {
314 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100315 goto exit;
316 }
317
Neil Armstrong79daea22022-03-21 12:05:51 +0100318 status = psa_mac_verify_finish( &operation, cookie + 4,
319 COOKIE_HMAC_LEN );
320 if( status != PSA_SUCCESS )
321 {
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)
329 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100330 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200331#endif
332
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200333 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
334 &p, p + sizeof( ref_hmac ),
335 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200336 ret = -1;
337
338#if defined(MBEDTLS_THREADING_C)
339 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100340 {
341 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
342 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
343 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200344#endif
345
346 if( ret != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100347 goto exit;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200348
Gabor Mezei90437e32021-10-20 11:59:27 +0200349 if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100350 {
351 ret = -1;
352 goto exit;
353 }
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100354#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100357 cur_time = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200358#else
359 cur_time = ctx->serial;
360#endif
361
362 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
363 ( (unsigned long) cookie[1] << 16 ) |
364 ( (unsigned long) cookie[2] << 8 ) |
365 ( (unsigned long) cookie[3] );
366
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200367 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100368 {
369 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)
Neil Armstrong79daea22022-03-21 12:05:51 +0100375 status = psa_mac_abort( &operation );
376 if( status != PSA_SUCCESS )
377 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100378#else
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100379 mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100380#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100381 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_SSL_COOKIE_C */