• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  DTLS cookie callbacks implementation
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  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:
12  *
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.
24  *
25  *  **********
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  */
46 /*
47  * These session callbacks use a simple chained list
48  * to store and retrieve the session information.
49  */
50 
51 #if !defined(MBEDTLS_CONFIG_FILE)
52 #include "mbedtls/config.h"
53 #else
54 #include MBEDTLS_CONFIG_FILE
55 #endif
56 
57 #if defined(MBEDTLS_SSL_COOKIE_C)
58 
59 #if defined(MBEDTLS_PLATFORM_C)
60 #include "mbedtls/platform.h"
61 #else
62 #define mbedtls_calloc    calloc
63 #define mbedtls_free      free
64 #endif
65 
66 #include "mbedtls/ssl_cookie.h"
67 #include "mbedtls/ssl_internal.h"
68 #include "mbedtls/platform_util.h"
69 
70 #include <string.h>
71 
72 /*
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  */
77 #if defined(MBEDTLS_SHA256_C)
78 #define COOKIE_MD           MBEDTLS_MD_SHA224
79 #define COOKIE_MD_OUTLEN    32
80 #define COOKIE_HMAC_LEN     28
81 #elif defined(MBEDTLS_SHA512_C)
82 #define COOKIE_MD           MBEDTLS_MD_SHA384
83 #define COOKIE_MD_OUTLEN    48
84 #define COOKIE_HMAC_LEN     28
85 #elif defined(MBEDTLS_SHA1_C)
86 #define COOKIE_MD           MBEDTLS_MD_SHA1
87 #define COOKIE_MD_OUTLEN    20
88 #define COOKIE_HMAC_LEN     20
89 #else
90 #error "DTLS hello verify needs SHA-1 or SHA-2"
91 #endif
92 
93 /*
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 
mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx * ctx)99 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
100 {
101     mbedtls_md_init( &ctx->hmac_ctx );
102 #if !defined(MBEDTLS_HAVE_TIME)
103     ctx->serial = 0;
104 #endif
105     ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
106 
107 #if defined(MBEDTLS_THREADING_C)
108     mbedtls_mutex_init( &ctx->mutex );
109 #endif
110 }
111 
mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx * ctx,unsigned long delay)112 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
113 {
114     ctx->timeout = delay;
115 }
116 
mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx * ctx)117 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
118 {
119     mbedtls_md_free( &ctx->hmac_ctx );
120 
121 #if defined(MBEDTLS_THREADING_C)
122     mbedtls_mutex_free( &ctx->mutex );
123 #endif
124 
125     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
126 }
127 
mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx * ctx,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)128 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
129                       int (*f_rng)(void *, unsigned char *, size_t),
130                       void *p_rng )
131 {
132     int ret;
133     unsigned char key[COOKIE_MD_OUTLEN];
134 
135     if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
136         return( ret );
137 
138     ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
139     if( ret != 0 )
140         return( ret );
141 
142     ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
143     if( ret != 0 )
144         return( ret );
145 
146     mbedtls_platform_zeroize( key, sizeof( key ) );
147 
148     return( 0 );
149 }
150 
151 /*
152  * Generate the HMAC part of a cookie
153  */
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)154 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
155                             const unsigned char time[4],
156                             unsigned char **p, unsigned char *end,
157                             const unsigned char *cli_id, size_t cli_id_len )
158 {
159     unsigned char hmac_out[COOKIE_MD_OUTLEN];
160 
161     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
162 
163     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 )
167     {
168         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
169     }
170 
171     memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
172     *p += COOKIE_HMAC_LEN;
173 
174     return( 0 );
175 }
176 
177 /*
178  * Generate cookie for DTLS ClientHello verification
179  */
mbedtls_ssl_cookie_write(void * p_ctx,unsigned char ** p,unsigned char * end,const unsigned char * cli_id,size_t cli_id_len)180 int mbedtls_ssl_cookie_write( void *p_ctx,
181                       unsigned char **p, unsigned char *end,
182                       const unsigned char *cli_id, size_t cli_id_len )
183 {
184     int ret;
185     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
186     unsigned long t;
187 
188     if( ctx == NULL || cli_id == NULL )
189         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
190 
191     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
192 
193 #if defined(MBEDTLS_HAVE_TIME)
194     t = (unsigned long) mbedtls_time( NULL );
195 #else
196     t = ctx->serial++;
197 #endif
198 
199     (*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;
204 
205 #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 );
220 }
221 
222 /*
223  * Check a cookie
224  */
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)225 int mbedtls_ssl_cookie_check( void *p_ctx,
226                       const unsigned char *cookie, size_t cookie_len,
227                       const unsigned char *cli_id, size_t cli_id_len )
228 {
229     unsigned char ref_hmac[COOKIE_HMAC_LEN];
230     int ret = 0;
231     unsigned char *p = ref_hmac;
232     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
233     unsigned long cur_time, cookie_time;
234 
235     if( ctx == NULL || cli_id == NULL )
236         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
237 
238     if( cookie_len != COOKIE_LEN )
239         return( -1 );
240 
241 #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 
246     if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
247                          &p, p + sizeof( ref_hmac ),
248                          cli_id, cli_id_len ) != 0 )
249         ret = -1;
250 
251 #if defined(MBEDTLS_THREADING_C)
252     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
253         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
254                 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
255 #endif
256 
257     if( ret != 0 )
258         return( ret );
259 
260     if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
261         return( -1 );
262 
263 #if defined(MBEDTLS_HAVE_TIME)
264     cur_time = (unsigned long) mbedtls_time( NULL );
265 #else
266     cur_time = ctx->serial;
267 #endif
268 
269     cookie_time = ( (unsigned long) cookie[0] << 24 ) |
270                   ( (unsigned long) cookie[1] << 16 ) |
271                   ( (unsigned long) cookie[2] <<  8 ) |
272                   ( (unsigned long) cookie[3]       );
273 
274     if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
275         return( -1 );
276 
277     return( 0 );
278 }
279 #endif /* MBEDTLS_SSL_COOKIE_C */
280