blob: 15a31737733ca870316e90b7ad53e051ddc732c8 [file] [log] [blame]
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001/*
2 * DTLS cookie callbacks implementation
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnarde4d48902015-03-06 13:40:52 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020047 */
48/*
49 * These session callbacks use a simple chained list
50 * to store and retrieve the session information.
51 */
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/config.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020055#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020063#else
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020064#define mbedtls_calloc calloc
SimonBd5800b72016-04-26 07:43:27 +010065#define mbedtls_free free
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020066#endif
67
SimonBd5800b72016-04-26 07:43:27 +010068#include "mbedtls/ssl_cookie.h"
69#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050070#include "mbedtls/platform_util.h"
SimonBd5800b72016-04-26 07:43:27 +010071
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000072#include <string.h>
73
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020074/*
75 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
76 * available. Try SHA-256 first, 512 wastes resources since we need to stay
77 * with max 32 bytes of cookie for DTLS 1.0
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_SHA256_C)
80#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020081#define COOKIE_MD_OUTLEN 32
82#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#elif defined(MBEDTLS_SHA512_C)
84#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020085#define COOKIE_MD_OUTLEN 48
86#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#elif defined(MBEDTLS_SHA1_C)
88#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020089#define COOKIE_MD_OUTLEN 20
90#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020091#else
92#error "DTLS hello verify needs SHA-1 or SHA-2"
93#endif
94
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020095/*
96 * Cookies are formed of a 4-bytes timestamp (or serial number) and
97 * an HMAC of timestemp and client ID.
98 */
99#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_md_init( &ctx->hmac_ctx );
104#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200105 ctx->serial = 0;
106#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200108
109#if defined(MBEDTLS_THREADING_C)
110 mbedtls_mutex_init( &ctx->mutex );
111#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200112}
113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200115{
116 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200117}
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200122
123#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +0200124 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200125#endif
126
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500127 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200128}
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200131 int (*f_rng)(void *, unsigned char *, size_t),
132 void *p_rng )
133{
134 int ret;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200135 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200136
137 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
138 return( ret );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200141 if( ret != 0 )
142 return( ret );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200145 if( ret != 0 )
146 return( ret );
147
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500148 mbedtls_platform_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200149
150 return( 0 );
151}
152
153/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200154 * Generate the HMAC part of a cookie
155 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200157 const unsigned char time[4],
158 unsigned char **p, unsigned char *end,
159 const unsigned char *cli_id, size_t cli_id_len )
160{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200161 unsigned char hmac_out[COOKIE_MD_OUTLEN];
162
Hanno Beckerf8f61aa2017-04-12 14:54:42 +0100163 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200164
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200165 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
166 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
167 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
168 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200171 }
172
173 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
174 *p += COOKIE_HMAC_LEN;
175
176 return( 0 );
177}
178
179/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200180 * Generate cookie for DTLS ClientHello verification
181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200183 unsigned char **p, unsigned char *end,
184 const unsigned char *cli_id, size_t cli_id_len )
185{
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200186 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200188 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200189
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200190 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200192
Hanno Beckerf8f61aa2017-04-12 14:54:42 +0100193 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100196 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200197#else
198 t = ctx->serial++;
199#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200200
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200201 (*p)[0] = (unsigned char)( t >> 24 );
202 (*p)[1] = (unsigned char)( t >> 16 );
203 (*p)[2] = (unsigned char)( t >> 8 );
204 (*p)[3] = (unsigned char)( t );
205 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200206
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200207#if defined(MBEDTLS_THREADING_C)
208 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
210#endif
211
212 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
213 p, end, cli_id, cli_id_len );
214
215#if defined(MBEDTLS_THREADING_C)
216 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
217 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
218 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
219#endif
220
221 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200222}
223
224/*
225 * Check a cookie
226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200228 const unsigned char *cookie, size_t cookie_len,
229 const unsigned char *cli_id, size_t cli_id_len )
230{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200231 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200232 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200233 unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200235 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200236
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200237 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200239
240 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200241 return( -1 );
242
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200243#if defined(MBEDTLS_THREADING_C)
244 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
246#endif
247
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200248 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
249 &p, p + sizeof( ref_hmac ),
250 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200251 ret = -1;
252
253#if defined(MBEDTLS_THREADING_C)
254 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
256 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
257#endif
258
259 if( ret != 0 )
260 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200263 return( -1 );
264
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 )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200277 return( -1 );
278
279 return( 0 );
280}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_SSL_COOKIE_C */