blob: 9e2136865de8dc6c9e8b51010b373d72c4b84555 [file] [log] [blame]
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001/*
2 * DTLS cookie callbacks implementation
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020045 */
46/*
47 * These session callbacks use a simple chained list
48 * to store and retrieve the session information.
49 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/config.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020053#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020061#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020062#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010063#define mbedtls_free free
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020064#endif
65
SimonBd5800b72016-04-26 07:43:27 +010066#include "mbedtls/ssl_cookie.h"
67#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050068#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010069
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000070#include <string.h>
71
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020072/*
73 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
74 * available. Try SHA-256 first, 512 wastes resources since we need to stay
75 * with max 32 bytes of cookie for DTLS 1.0
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_SHA256_C)
78#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020079#define COOKIE_MD_OUTLEN 32
80#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#elif defined(MBEDTLS_SHA512_C)
82#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020083#define COOKIE_MD_OUTLEN 48
84#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#elif defined(MBEDTLS_SHA1_C)
86#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020087#define COOKIE_MD_OUTLEN 20
88#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020089#else
90#error "DTLS hello verify needs SHA-1 or SHA-2"
91#endif
92
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020093/*
94 * Cookies are formed of a 4-bytes timestamp (or serial number) and
95 * an HMAC of timestemp and client ID.
96 */
97#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_md_init( &ctx->hmac_ctx );
102#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200103 ctx->serial = 0;
104#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200106
107#if defined(MBEDTLS_THREADING_C)
108 mbedtls_mutex_init( &ctx->mutex );
109#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200113{
114 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115}
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200120
121#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +0200122 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200123#endif
124
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500125 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200126}
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200129 int (*f_rng)(void *, unsigned char *, size_t),
130 void *p_rng )
131{
132 int ret;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200133 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200134
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 ) );
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 Beckerf8f61aa2017-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{
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200184 int ret;
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 Beckerf8f61aa2017-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
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200199 (*p)[0] = (unsigned char)( t >> 24 );
200 (*p)[1] = (unsigned char)( t >> 16 );
201 (*p)[2] = (unsigned char)( t >> 8 );
202 (*p)[3] = (unsigned char)( t );
203 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200204
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200205#if defined(MBEDTLS_THREADING_C)
206 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
208#endif
209
210 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
211 p, end, cli_id, cli_id_len );
212
213#if defined(MBEDTLS_THREADING_C)
214 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
215 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
216 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
217#endif
218
219 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200220}
221
222/*
223 * Check a cookie
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200226 const unsigned char *cookie, size_t cookie_len,
227 const unsigned char *cli_id, size_t cli_id_len )
228{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200229 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200230 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200231 unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200233 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200234
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200235 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200237
238 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200239 return( -1 );
240
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200241#if defined(MBEDTLS_THREADING_C)
242 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
244#endif
245
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200246 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
247 &p, p + sizeof( ref_hmac ),
248 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200249 ret = -1;
250
251#if defined(MBEDTLS_THREADING_C)
252 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Gilles Peskine75b596f2021-12-13 12:35:08 +0100253 ret = ( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200254 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
255#endif
256
257 if( ret != 0 )
Gilles Peskine75b596f2021-12-13 12:35:08 +0100258 goto exit;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Gilles Peskine75b596f2021-12-13 12:35:08 +0100261 {
262 ret = -1;
263 goto exit;
264 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100267 cur_time = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200268#else
269 cur_time = ctx->serial;
270#endif
271
272 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
273 ( (unsigned long) cookie[1] << 16 ) |
274 ( (unsigned long) cookie[2] << 8 ) |
275 ( (unsigned long) cookie[3] );
276
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200277 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Gilles Peskine75b596f2021-12-13 12:35:08 +0100278 {
279 ret = -1;
280 goto exit;
281 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200282
Gilles Peskine75b596f2021-12-13 12:35:08 +0100283exit:
284 mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
285 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_SSL_COOKIE_C */