1c2c66affSColin Finck /*
2c2c66affSColin Finck * DTLS cookie callbacks implementation
3c2c66affSColin Finck *
4218e2596SThomas Faber * Copyright The Mbed TLS Contributors
5e57126f5SThomas Faber * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6e57126f5SThomas Faber *
7e57126f5SThomas Faber * This file is provided under the Apache License 2.0, or the
8e57126f5SThomas Faber * GNU General Public License v2.0 or later.
9e57126f5SThomas Faber *
10e57126f5SThomas Faber * **********
11e57126f5SThomas Faber * Apache License 2.0:
12e57126f5SThomas Faber *
13e57126f5SThomas Faber * Licensed under the Apache License, Version 2.0 (the "License"); you may
14e57126f5SThomas Faber * not use this file except in compliance with the License.
15e57126f5SThomas Faber * You may obtain a copy of the License at
16e57126f5SThomas Faber *
17e57126f5SThomas Faber * http://www.apache.org/licenses/LICENSE-2.0
18e57126f5SThomas Faber *
19e57126f5SThomas Faber * Unless required by applicable law or agreed to in writing, software
20e57126f5SThomas Faber * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21e57126f5SThomas Faber * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22e57126f5SThomas Faber * See the License for the specific language governing permissions and
23e57126f5SThomas Faber * limitations under the License.
24e57126f5SThomas Faber *
25e57126f5SThomas Faber * **********
26e57126f5SThomas Faber *
27e57126f5SThomas Faber * **********
28e57126f5SThomas Faber * GNU General Public License v2.0 or later:
29c2c66affSColin Finck *
30c2c66affSColin Finck * This program is free software; you can redistribute it and/or modify
31c2c66affSColin Finck * it under the terms of the GNU General Public License as published by
32c2c66affSColin Finck * the Free Software Foundation; either version 2 of the License, or
33c2c66affSColin Finck * (at your option) any later version.
34c2c66affSColin Finck *
35c2c66affSColin Finck * This program is distributed in the hope that it will be useful,
36c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
37c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38c2c66affSColin Finck * GNU General Public License for more details.
39c2c66affSColin Finck *
40c2c66affSColin Finck * You should have received a copy of the GNU General Public License along
41c2c66affSColin Finck * with this program; if not, write to the Free Software Foundation, Inc.,
42c2c66affSColin Finck * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43c2c66affSColin Finck *
44e57126f5SThomas Faber * **********
45c2c66affSColin Finck */
46c2c66affSColin Finck /*
47c2c66affSColin Finck * These session callbacks use a simple chained list
48c2c66affSColin Finck * to store and retrieve the session information.
49c2c66affSColin Finck */
50c2c66affSColin Finck
51c2c66affSColin Finck #if !defined(MBEDTLS_CONFIG_FILE)
52c2c66affSColin Finck #include "mbedtls/config.h"
53c2c66affSColin Finck #else
54c2c66affSColin Finck #include MBEDTLS_CONFIG_FILE
55c2c66affSColin Finck #endif
56c2c66affSColin Finck
57c2c66affSColin Finck #if defined(MBEDTLS_SSL_COOKIE_C)
58c2c66affSColin Finck
59c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_C)
60c2c66affSColin Finck #include "mbedtls/platform.h"
61c2c66affSColin Finck #else
62c2c66affSColin Finck #define mbedtls_calloc calloc
63c2c66affSColin Finck #define mbedtls_free free
64c2c66affSColin Finck #endif
65c2c66affSColin Finck
66c2c66affSColin Finck #include "mbedtls/ssl_cookie.h"
67c2c66affSColin Finck #include "mbedtls/ssl_internal.h"
68*cbda039fSThomas Faber #include "mbedtls/platform_util.h"
69c2c66affSColin Finck
70c2c66affSColin Finck #include <string.h>
71c2c66affSColin Finck
72c2c66affSColin Finck /*
73c2c66affSColin Finck * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
74c2c66affSColin Finck * available. Try SHA-256 first, 512 wastes resources since we need to stay
75c2c66affSColin Finck * with max 32 bytes of cookie for DTLS 1.0
76c2c66affSColin Finck */
77c2c66affSColin Finck #if defined(MBEDTLS_SHA256_C)
78c2c66affSColin Finck #define COOKIE_MD MBEDTLS_MD_SHA224
79c2c66affSColin Finck #define COOKIE_MD_OUTLEN 32
80c2c66affSColin Finck #define COOKIE_HMAC_LEN 28
81c2c66affSColin Finck #elif defined(MBEDTLS_SHA512_C)
82c2c66affSColin Finck #define COOKIE_MD MBEDTLS_MD_SHA384
83c2c66affSColin Finck #define COOKIE_MD_OUTLEN 48
84c2c66affSColin Finck #define COOKIE_HMAC_LEN 28
85c2c66affSColin Finck #elif defined(MBEDTLS_SHA1_C)
86c2c66affSColin Finck #define COOKIE_MD MBEDTLS_MD_SHA1
87c2c66affSColin Finck #define COOKIE_MD_OUTLEN 20
88c2c66affSColin Finck #define COOKIE_HMAC_LEN 20
89c2c66affSColin Finck #else
90c2c66affSColin Finck #error "DTLS hello verify needs SHA-1 or SHA-2"
91c2c66affSColin Finck #endif
92c2c66affSColin Finck
93c2c66affSColin Finck /*
94c2c66affSColin Finck * Cookies are formed of a 4-bytes timestamp (or serial number) and
95c2c66affSColin Finck * an HMAC of timestemp and client ID.
96c2c66affSColin Finck */
97c2c66affSColin Finck #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
98c2c66affSColin Finck
mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx * ctx)99c2c66affSColin Finck void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
100c2c66affSColin Finck {
101c2c66affSColin Finck mbedtls_md_init( &ctx->hmac_ctx );
102c2c66affSColin Finck #if !defined(MBEDTLS_HAVE_TIME)
103c2c66affSColin Finck ctx->serial = 0;
104c2c66affSColin Finck #endif
105c2c66affSColin Finck ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
106c2c66affSColin Finck
107c2c66affSColin Finck #if defined(MBEDTLS_THREADING_C)
108c2c66affSColin Finck mbedtls_mutex_init( &ctx->mutex );
109c2c66affSColin Finck #endif
110c2c66affSColin Finck }
111c2c66affSColin Finck
mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx * ctx,unsigned long delay)112c2c66affSColin Finck void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
113c2c66affSColin Finck {
114c2c66affSColin Finck ctx->timeout = delay;
115c2c66affSColin Finck }
116c2c66affSColin Finck
mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx * ctx)117c2c66affSColin Finck void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
118c2c66affSColin Finck {
119c2c66affSColin Finck mbedtls_md_free( &ctx->hmac_ctx );
120c2c66affSColin Finck
121c2c66affSColin Finck #if defined(MBEDTLS_THREADING_C)
122c2c66affSColin Finck mbedtls_mutex_free( &ctx->mutex );
123c2c66affSColin Finck #endif
124c2c66affSColin Finck
125*cbda039fSThomas Faber mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
126c2c66affSColin Finck }
127c2c66affSColin Finck
mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx * ctx,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)128c2c66affSColin Finck int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
129c2c66affSColin Finck int (*f_rng)(void *, unsigned char *, size_t),
130c2c66affSColin Finck void *p_rng )
131c2c66affSColin Finck {
132c2c66affSColin Finck int ret;
133c2c66affSColin Finck unsigned char key[COOKIE_MD_OUTLEN];
134c2c66affSColin Finck
135c2c66affSColin Finck if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
136c2c66affSColin Finck return( ret );
137c2c66affSColin Finck
138c2c66affSColin Finck ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
139c2c66affSColin Finck if( ret != 0 )
140c2c66affSColin Finck return( ret );
141c2c66affSColin Finck
142c2c66affSColin Finck ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
143c2c66affSColin Finck if( ret != 0 )
144c2c66affSColin Finck return( ret );
145c2c66affSColin Finck
146*cbda039fSThomas Faber mbedtls_platform_zeroize( key, sizeof( key ) );
147c2c66affSColin Finck
148c2c66affSColin Finck return( 0 );
149c2c66affSColin Finck }
150c2c66affSColin Finck
151c2c66affSColin Finck /*
152c2c66affSColin Finck * Generate the HMAC part of a cookie
153c2c66affSColin Finck */
ssl_cookie_hmac(mbedtls_md_context_t * hmac_ctx,const unsigned char time[4],unsigned char ** p,unsigned char * end,const unsigned char * cli_id,size_t cli_id_len)154c2c66affSColin Finck static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
155c2c66affSColin Finck const unsigned char time[4],
156c2c66affSColin Finck unsigned char **p, unsigned char *end,
157c2c66affSColin Finck const unsigned char *cli_id, size_t cli_id_len )
158c2c66affSColin Finck {
159c2c66affSColin Finck unsigned char hmac_out[COOKIE_MD_OUTLEN];
160c2c66affSColin Finck
161292f67afSThomas Faber MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
162c2c66affSColin Finck
163c2c66affSColin Finck if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
164c2c66affSColin Finck mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
165c2c66affSColin Finck mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
166c2c66affSColin Finck mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
167c2c66affSColin Finck {
168c2c66affSColin Finck return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
169c2c66affSColin Finck }
170c2c66affSColin Finck
171c2c66affSColin Finck memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
172c2c66affSColin Finck *p += COOKIE_HMAC_LEN;
173c2c66affSColin Finck
174c2c66affSColin Finck return( 0 );
175c2c66affSColin Finck }
176c2c66affSColin Finck
177c2c66affSColin Finck /*
178c2c66affSColin Finck * Generate cookie for DTLS ClientHello verification
179c2c66affSColin Finck */
mbedtls_ssl_cookie_write(void * p_ctx,unsigned char ** p,unsigned char * end,const unsigned char * cli_id,size_t cli_id_len)180c2c66affSColin Finck int mbedtls_ssl_cookie_write( void *p_ctx,
181c2c66affSColin Finck unsigned char **p, unsigned char *end,
182c2c66affSColin Finck const unsigned char *cli_id, size_t cli_id_len )
183c2c66affSColin Finck {
184c2c66affSColin Finck int ret;
185c2c66affSColin Finck mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
186c2c66affSColin Finck unsigned long t;
187c2c66affSColin Finck
188c2c66affSColin Finck if( ctx == NULL || cli_id == NULL )
189c2c66affSColin Finck return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190c2c66affSColin Finck
191292f67afSThomas Faber MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
192c2c66affSColin Finck
193c2c66affSColin Finck #if defined(MBEDTLS_HAVE_TIME)
194c2c66affSColin Finck t = (unsigned long) mbedtls_time( NULL );
195c2c66affSColin Finck #else
196c2c66affSColin Finck t = ctx->serial++;
197c2c66affSColin Finck #endif
198c2c66affSColin Finck
199c2c66affSColin Finck (*p)[0] = (unsigned char)( t >> 24 );
200c2c66affSColin Finck (*p)[1] = (unsigned char)( t >> 16 );
201c2c66affSColin Finck (*p)[2] = (unsigned char)( t >> 8 );
202c2c66affSColin Finck (*p)[3] = (unsigned char)( t );
203c2c66affSColin Finck *p += 4;
204c2c66affSColin Finck
205c2c66affSColin Finck #if defined(MBEDTLS_THREADING_C)
206c2c66affSColin Finck if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
207c2c66affSColin Finck return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
208c2c66affSColin Finck #endif
209c2c66affSColin Finck
210c2c66affSColin Finck ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
211c2c66affSColin Finck p, end, cli_id, cli_id_len );
212c2c66affSColin Finck
213c2c66affSColin Finck #if defined(MBEDTLS_THREADING_C)
214c2c66affSColin Finck if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
215c2c66affSColin Finck return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
216c2c66affSColin Finck MBEDTLS_ERR_THREADING_MUTEX_ERROR );
217c2c66affSColin Finck #endif
218c2c66affSColin Finck
219c2c66affSColin Finck return( ret );
220c2c66affSColin Finck }
221c2c66affSColin Finck
222c2c66affSColin Finck /*
223c2c66affSColin Finck * Check a cookie
224c2c66affSColin Finck */
mbedtls_ssl_cookie_check(void * p_ctx,const unsigned char * cookie,size_t cookie_len,const unsigned char * cli_id,size_t cli_id_len)225c2c66affSColin Finck int mbedtls_ssl_cookie_check( void *p_ctx,
226c2c66affSColin Finck const unsigned char *cookie, size_t cookie_len,
227c2c66affSColin Finck const unsigned char *cli_id, size_t cli_id_len )
228c2c66affSColin Finck {
229c2c66affSColin Finck unsigned char ref_hmac[COOKIE_HMAC_LEN];
230c2c66affSColin Finck int ret = 0;
231c2c66affSColin Finck unsigned char *p = ref_hmac;
232c2c66affSColin Finck mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
233c2c66affSColin Finck unsigned long cur_time, cookie_time;
234c2c66affSColin Finck
235c2c66affSColin Finck if( ctx == NULL || cli_id == NULL )
236c2c66affSColin Finck return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
237c2c66affSColin Finck
238c2c66affSColin Finck if( cookie_len != COOKIE_LEN )
239c2c66affSColin Finck return( -1 );
240c2c66affSColin Finck
241c2c66affSColin Finck #if defined(MBEDTLS_THREADING_C)
242c2c66affSColin Finck if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
243c2c66affSColin Finck return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
244c2c66affSColin Finck #endif
245c2c66affSColin Finck
246c2c66affSColin Finck if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
247c2c66affSColin Finck &p, p + sizeof( ref_hmac ),
248c2c66affSColin Finck cli_id, cli_id_len ) != 0 )
249c2c66affSColin Finck ret = -1;
250c2c66affSColin Finck
251c2c66affSColin Finck #if defined(MBEDTLS_THREADING_C)
252c2c66affSColin Finck if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
253c2c66affSColin Finck return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
254c2c66affSColin Finck MBEDTLS_ERR_THREADING_MUTEX_ERROR );
255c2c66affSColin Finck #endif
256c2c66affSColin Finck
257c2c66affSColin Finck if( ret != 0 )
258c2c66affSColin Finck return( ret );
259c2c66affSColin Finck
260c2c66affSColin Finck if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
261c2c66affSColin Finck return( -1 );
262c2c66affSColin Finck
263c2c66affSColin Finck #if defined(MBEDTLS_HAVE_TIME)
264c2c66affSColin Finck cur_time = (unsigned long) mbedtls_time( NULL );
265c2c66affSColin Finck #else
266c2c66affSColin Finck cur_time = ctx->serial;
267c2c66affSColin Finck #endif
268c2c66affSColin Finck
269c2c66affSColin Finck cookie_time = ( (unsigned long) cookie[0] << 24 ) |
270c2c66affSColin Finck ( (unsigned long) cookie[1] << 16 ) |
271c2c66affSColin Finck ( (unsigned long) cookie[2] << 8 ) |
272c2c66affSColin Finck ( (unsigned long) cookie[3] );
273c2c66affSColin Finck
274c2c66affSColin Finck if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
275c2c66affSColin Finck return( -1 );
276c2c66affSColin Finck
277c2c66affSColin Finck return( 0 );
278c2c66affSColin Finck }
279c2c66affSColin Finck #endif /* MBEDTLS_SSL_COOKIE_C */
280