blob: ba5d8b95c5055f8dd6b04c19005c212df7b5d7a1 [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
65 * an HMAC of timestemp and client ID.
66 */
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)
72 ctx->psa_hmac = MBEDTLS_SVC_KEY_ID_INIT;
73#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_md_init( &ctx->hmac_ctx );
75#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
80#if defined(MBEDTLS_THREADING_C)
81 mbedtls_mutex_init( &ctx->mutex );
82#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020083}
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020086{
87 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020088}
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020091{
Neil Armstrongbca99ee2022-03-04 10:20:20 +010092#if defined(MBEDTLS_USE_PSA_CRYPTO)
93 psa_destroy_key( ctx->psa_hmac );
94#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020096
97#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +020098 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020099#endif
100
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500101 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200102}
103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200105 int (*f_rng)(void *, unsigned char *, size_t),
106 void *p_rng )
107{
Janos Follath865b3eb2019-12-16 11:46:15 +0000108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200109 unsigned char key[COOKIE_MD_OUTLEN];
Neil Armstrongd6332012022-03-04 10:26:16 +0100110#if defined(MBEDTLS_USE_PSA_CRYPTO)
111 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
113 psa_algorithm_t alg;
114#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115
116 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
117 return( ret );
118
Neil Armstrongd6332012022-03-04 10:26:16 +0100119#if defined(MBEDTLS_USE_PSA_CRYPTO)
120 alg = mbedtls_psa_translate_md( COOKIE_MD );
121 if( alg == 0 )
122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
123
124 ctx->psa_hmac_alg = PSA_ALG_HMAC( alg );
125
126 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
127 psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) );
128 psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
129
130 if( ( status = psa_import_key( &attributes,
131 key, sizeof( key ),
132 &ctx->psa_hmac ) ) != PSA_SUCCESS )
133 {
134 return psa_ssl_status_to_mbedtls( status );
135 }
136#endif /* MBEDTLS_USE_PSA_CRYPTO */
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 ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200147
148 return( 0 );
149}
150
151/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200152 * Generate the HMAC part of a cookie
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200155 const unsigned char time[4],
156 unsigned char **p, unsigned char *end,
157 const unsigned char *cli_id, size_t cli_id_len )
158{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200159 unsigned char hmac_out[COOKIE_MD_OUTLEN];
160
Hanno Becker261602c2017-04-12 14:54:42 +0100161 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200162
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200163 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
164 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
165 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
166 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200169 }
170
171 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
172 *p += COOKIE_HMAC_LEN;
173
174 return( 0 );
175}
176
177/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200178 * Generate cookie for DTLS ClientHello verification
179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200181 unsigned char **p, unsigned char *end,
182 const unsigned char *cli_id, size_t cli_id_len )
183{
Janos Follath865b3eb2019-12-16 11:46:15 +0000184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200186 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200187
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200188 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200190
Hanno Becker261602c2017-04-12 14:54:42 +0100191 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100194 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200195#else
196 t = ctx->serial++;
197#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200198
Joe Subbianib6511b02021-07-16 15:02:55 +0100199 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200200 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200201
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200202#if defined(MBEDTLS_THREADING_C)
203 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100204 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200205#endif
206
207 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
208 p, end, cli_id, cli_id_len );
209
210#if defined(MBEDTLS_THREADING_C)
211 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100212 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
213 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200214#endif
215
216 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200217}
218
219/*
220 * Check a cookie
221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200223 const unsigned char *cookie, size_t cookie_len,
224 const unsigned char *cli_id, size_t cli_id_len )
225{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200226 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200227 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200228 unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200230 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200231
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200232 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200234
235 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200236 return( -1 );
237
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200238#if defined(MBEDTLS_THREADING_C)
239 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100240 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200241#endif
242
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200243 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
244 &p, p + sizeof( ref_hmac ),
245 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200246 ret = -1;
247
248#if defined(MBEDTLS_THREADING_C)
249 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100250 {
251 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
252 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
253 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200254#endif
255
256 if( ret != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100257 goto exit;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200258
Gabor Mezei90437e32021-10-20 11:59:27 +0200259 if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100260 {
261 ret = -1;
262 goto exit;
263 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100266 cur_time = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200267#else
268 cur_time = ctx->serial;
269#endif
270
271 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
272 ( (unsigned long) cookie[1] << 16 ) |
273 ( (unsigned long) cookie[2] << 8 ) |
274 ( (unsigned long) cookie[3] );
275
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200276 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100277 {
278 ret = -1;
279 goto exit;
280 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200281
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100282exit:
283 mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
284 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_SSL_COOKIE_C */