blob: 3781796b7219d22e7050af3d0cb7f397168bcb98 [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"
36#include "mbedtls/ssl_internal.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 Mezeie24dea82021-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
45 * available. Try SHA-256 first, 512 wastes resources since we need to stay
46 * with max 32 bytes of cookie for DTLS 1.0
47 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_SHA256_C)
49#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020050#define COOKIE_MD_OUTLEN 32
51#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#elif defined(MBEDTLS_SHA512_C)
53#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020054#define COOKIE_MD_OUTLEN 48
55#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#elif defined(MBEDTLS_SHA1_C)
57#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020058#define COOKIE_MD_OUTLEN 20
59#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020060#else
61#error "DTLS hello verify needs SHA-1 or SHA-2"
62#endif
63
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020064/*
65 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case0e7791f2021-12-20 21:14:10 -080066 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020067 */
68#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_md_init( &ctx->hmac_ctx );
73#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020074 ctx->serial = 0;
75#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020077
78#if defined(MBEDTLS_THREADING_C)
79 mbedtls_mutex_init( &ctx->mutex );
80#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020081}
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020084{
85 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020086}
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020091
92#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +020093 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020094#endif
95
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050096 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020097}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200100 int (*f_rng)(void *, unsigned char *, size_t),
101 void *p_rng )
102{
Janos Follath865b3eb2019-12-16 11:46:15 +0000103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200104 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200105
106 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
107 return( ret );
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200110 if( ret != 0 )
111 return( ret );
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200114 if( ret != 0 )
115 return( ret );
116
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500117 mbedtls_platform_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200118
119 return( 0 );
120}
121
122/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200123 * Generate the HMAC part of a cookie
124 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200125MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200127 const unsigned char time[4],
128 unsigned char **p, unsigned char *end,
129 const unsigned char *cli_id, size_t cli_id_len )
130{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200131 unsigned char hmac_out[COOKIE_MD_OUTLEN];
132
Hanno Becker261602c2017-04-12 14:54:42 +0100133 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200134
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200135 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
136 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
137 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
138 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200141 }
142
143 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
144 *p += COOKIE_HMAC_LEN;
145
146 return( 0 );
147}
148
149/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200150 * Generate cookie for DTLS ClientHello verification
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200153 unsigned char **p, unsigned char *end,
154 const unsigned char *cli_id, size_t cli_id_len )
155{
Janos Follath865b3eb2019-12-16 11:46:15 +0000156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200158 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200159
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200160 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200162
Hanno Becker261602c2017-04-12 14:54:42 +0100163 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100166 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200167#else
168 t = ctx->serial++;
169#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200170
Joe Subbiani6627fb22021-07-16 15:02:55 +0100171 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200172 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200173
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200174#if defined(MBEDTLS_THREADING_C)
175 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100176 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200177#endif
178
179 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
180 p, end, cli_id, cli_id_len );
181
182#if defined(MBEDTLS_THREADING_C)
183 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100184 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
185 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200186#endif
187
188 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200189}
190
191/*
192 * Check a cookie
193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200195 const unsigned char *cookie, size_t cookie_len,
196 const unsigned char *cli_id, size_t cli_id_len )
197{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200198 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200199 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200200 unsigned char *p = ref_hmac;
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 cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200203
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +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é-Gonnarde9030812014-07-23 21:29:11 +0200206
207 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200208 return( -1 );
209
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200210#if defined(MBEDTLS_THREADING_C)
211 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100212 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200213#endif
214
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200215 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
216 &p, p + sizeof( ref_hmac ),
217 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200218 ret = -1;
219
220#if defined(MBEDTLS_THREADING_C)
221 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Gilles Peskine69d3b862021-12-13 12:35:08 +0100222 {
223 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
224 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
225 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200226#endif
227
228 if( ret != 0 )
Gilles Peskine69d3b862021-12-13 12:35:08 +0100229 goto exit;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200230
Gabor Mezei18a44942021-10-20 11:59:27 +0200231 if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Gilles Peskine69d3b862021-12-13 12:35:08 +0100232 {
233 ret = -1;
234 goto exit;
235 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100238 cur_time = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200239#else
240 cur_time = ctx->serial;
241#endif
242
243 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
244 ( (unsigned long) cookie[1] << 16 ) |
245 ( (unsigned long) cookie[2] << 8 ) |
246 ( (unsigned long) cookie[3] );
247
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200248 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Gilles Peskine69d3b862021-12-13 12:35:08 +0100249 {
250 ret = -1;
251 goto exit;
252 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200253
Gilles Peskine69d3b862021-12-13 12:35:08 +0100254exit:
255 mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
256 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* MBEDTLS_SSL_COOKIE_C */