blob: 053a11bf95e9515366e3c47a42262cc3508237ce [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úti4e9f7122020-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úti4e9f7122020-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"
70
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000071#include <string.h>
72
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020073/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074static void mbedtls_zeroize( void *v, size_t n ) {
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020075 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
76}
77
78/*
79 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
80 * available. Try SHA-256 first, 512 wastes resources since we need to stay
81 * with max 32 bytes of cookie for DTLS 1.0
82 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_SHA256_C)
84#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020085#define COOKIE_MD_OUTLEN 32
86#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#elif defined(MBEDTLS_SHA512_C)
88#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020089#define COOKIE_MD_OUTLEN 48
90#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#elif defined(MBEDTLS_SHA1_C)
92#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020093#define COOKIE_MD_OUTLEN 20
94#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020095#else
96#error "DTLS hello verify needs SHA-1 or SHA-2"
97#endif
98
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020099/*
100 * Cookies are formed of a 4-bytes timestamp (or serial number) and
101 * an HMAC of timestemp and client ID.
102 */
103#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_md_init( &ctx->hmac_ctx );
108#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200109 ctx->serial = 0;
110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200112
113#if defined(MBEDTLS_THREADING_C)
114 mbedtls_mutex_init( &ctx->mutex );
115#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200116}
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200119{
120 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200121}
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200124{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_md_free( &ctx->hmac_ctx );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200126
127#if defined(MBEDTLS_THREADING_C)
Ron Eldor04965ed2017-01-29 18:51:35 +0200128 mbedtls_mutex_free( &ctx->mutex );
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200129#endif
130
131 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200135 int (*f_rng)(void *, unsigned char *, size_t),
136 void *p_rng )
137{
138 int ret;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200139 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200140
141 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
142 return( ret );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200145 if( ret != 0 )
146 return( ret );
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200149 if( ret != 0 )
150 return( ret );
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_zeroize( key, sizeof( key ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200153
154 return( 0 );
155}
156
157/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200158 * Generate the HMAC part of a cookie
159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200161 const unsigned char time[4],
162 unsigned char **p, unsigned char *end,
163 const unsigned char *cli_id, size_t cli_id_len )
164{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200165 unsigned char hmac_out[COOKIE_MD_OUTLEN];
166
Hanno Becker0e8dc482017-04-12 14:54:42 +0100167 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200168
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +0200169 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
170 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
171 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
172 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200175 }
176
177 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
178 *p += COOKIE_HMAC_LEN;
179
180 return( 0 );
181}
182
183/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200184 * Generate cookie for DTLS ClientHello verification
185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200187 unsigned char **p, unsigned char *end,
188 const unsigned char *cli_id, size_t cli_id_len )
189{
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200190 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200192 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200193
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200194 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200196
Hanno Becker0e8dc482017-04-12 14:54:42 +0100197 MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100200 t = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200201#else
202 t = ctx->serial++;
203#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200204
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200205 (*p)[0] = (unsigned char)( t >> 24 );
206 (*p)[1] = (unsigned char)( t >> 16 );
207 (*p)[2] = (unsigned char)( t >> 8 );
208 (*p)[3] = (unsigned char)( t );
209 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200210
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200211#if defined(MBEDTLS_THREADING_C)
212 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
213 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
214#endif
215
216 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
217 p, end, cli_id, cli_id_len );
218
219#if defined(MBEDTLS_THREADING_C)
220 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
222 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
223#endif
224
225 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200226}
227
228/*
229 * Check a cookie
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200232 const unsigned char *cookie, size_t cookie_len,
233 const unsigned char *cli_id, size_t cli_id_len )
234{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200235 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200236 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200237 unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200239 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200240
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200241 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200243
244 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200245 return( -1 );
246
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200247#if defined(MBEDTLS_THREADING_C)
248 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
249 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
250#endif
251
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200252 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
253 &p, p + sizeof( ref_hmac ),
254 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200255 ret = -1;
256
257#if defined(MBEDTLS_THREADING_C)
258 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
259 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
260 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
261#endif
262
263 if( ret != 0 )
264 return( ret );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200267 return( -1 );
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +0100270 cur_time = (unsigned long) mbedtls_time( NULL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200271#else
272 cur_time = ctx->serial;
273#endif
274
275 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
276 ( (unsigned long) cookie[1] << 16 ) |
277 ( (unsigned long) cookie[2] << 8 ) |
278 ( (unsigned long) cookie[3] );
279
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200280 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200281 return( -1 );
282
283 return( 0 );
284}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#endif /* MBEDTLS_SSL_COOKIE_C */