1 /*
2  *  DTLS cookie callbacks implementation
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  */
19 /*
20  * These session callbacks use a simple chained list
21  * to store and retrieve the session information.
22  */
23 
24 #include "common.h"
25 
26 #if defined(MBEDTLS_SSL_COOKIE_C)
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #define mbedtls_calloc    calloc
32 #define mbedtls_free      free
33 #endif
34 
35 #include "mbedtls/ssl_cookie.h"
36 #include "ssl_misc.h"
37 #include "mbedtls/error.h"
38 #include "mbedtls/platform_util.h"
39 
40 #include <string.h>
41 
42 /*
43  * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
44  * available. Try SHA-256 first, 512 wastes resources
45  */
46 #if defined(MBEDTLS_SHA224_C)
47 #define COOKIE_MD           MBEDTLS_MD_SHA224
48 #define COOKIE_MD_OUTLEN    32
49 #define COOKIE_HMAC_LEN     28
50 #elif defined(MBEDTLS_SHA384_C)
51 #define COOKIE_MD           MBEDTLS_MD_SHA384
52 #define COOKIE_MD_OUTLEN    48
53 #define COOKIE_HMAC_LEN     28
54 #elif defined(MBEDTLS_SHA1_C)
55 #define COOKIE_MD           MBEDTLS_MD_SHA1
56 #define COOKIE_MD_OUTLEN    20
57 #define COOKIE_HMAC_LEN     20
58 #else
59 #error "DTLS hello verify needs SHA-1 or SHA-2"
60 #endif
61 
62 /*
63  * Cookies are formed of a 4-bytes timestamp (or serial number) and
64  * an HMAC of timestemp and client ID.
65  */
66 #define COOKIE_LEN      ( 4 + COOKIE_HMAC_LEN )
67 
mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx * ctx)68 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
69 {
70     mbedtls_md_init( &ctx->hmac_ctx );
71 #if !defined(MBEDTLS_HAVE_TIME)
72     ctx->serial = 0;
73 #endif
74     ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
75 
76 #if defined(MBEDTLS_THREADING_C)
77     mbedtls_mutex_init( &ctx->mutex );
78 #endif
79 }
80 
mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx * ctx,unsigned long delay)81 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
82 {
83     ctx->timeout = delay;
84 }
85 
mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx * ctx)86 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
87 {
88     mbedtls_md_free( &ctx->hmac_ctx );
89 
90 #if defined(MBEDTLS_THREADING_C)
91     mbedtls_mutex_free( &ctx->mutex );
92 #endif
93 
94     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
95 }
96 
mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx * ctx,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)97 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
98                       int (*f_rng)(void *, unsigned char *, size_t),
99                       void *p_rng )
100 {
101     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
102     unsigned char key[COOKIE_MD_OUTLEN];
103 
104     if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
105         return( ret );
106 
107     ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
108     if( ret != 0 )
109         return( ret );
110 
111     ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
112     if( ret != 0 )
113         return( ret );
114 
115     mbedtls_platform_zeroize( key, sizeof( key ) );
116 
117     return( 0 );
118 }
119 
120 /*
121  * Generate the HMAC part of a cookie
122  */
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)123 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
124                             const unsigned char time[4],
125                             unsigned char **p, unsigned char *end,
126                             const unsigned char *cli_id, size_t cli_id_len )
127 {
128     unsigned char hmac_out[COOKIE_MD_OUTLEN];
129 
130     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
131 
132     if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 ||
133         mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
134         mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
135         mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
136     {
137         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
138     }
139 
140     memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
141     *p += COOKIE_HMAC_LEN;
142 
143     return( 0 );
144 }
145 
146 /*
147  * Generate cookie for DTLS ClientHello verification
148  */
mbedtls_ssl_cookie_write(void * p_ctx,unsigned char ** p,unsigned char * end,const unsigned char * cli_id,size_t cli_id_len)149 int mbedtls_ssl_cookie_write( void *p_ctx,
150                       unsigned char **p, unsigned char *end,
151                       const unsigned char *cli_id, size_t cli_id_len )
152 {
153     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
154     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
155     unsigned long t;
156 
157     if( ctx == NULL || cli_id == NULL )
158         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
159 
160     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
161 
162 #if defined(MBEDTLS_HAVE_TIME)
163     t = (unsigned long) mbedtls_time( NULL );
164 #else
165     t = ctx->serial++;
166 #endif
167 
168     MBEDTLS_PUT_UINT32_BE(t, *p, 0);
169     *p += 4;
170 
171 #if defined(MBEDTLS_THREADING_C)
172     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
173         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
174 #endif
175 
176     ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
177                            p, end, cli_id, cli_id_len );
178 
179 #if defined(MBEDTLS_THREADING_C)
180     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
181         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
182                 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
183 #endif
184 
185     return( ret );
186 }
187 
188 /*
189  * Check a cookie
190  */
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)191 int mbedtls_ssl_cookie_check( void *p_ctx,
192                       const unsigned char *cookie, size_t cookie_len,
193                       const unsigned char *cli_id, size_t cli_id_len )
194 {
195     unsigned char ref_hmac[COOKIE_HMAC_LEN];
196     int ret = 0;
197     unsigned char *p = ref_hmac;
198     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
199     unsigned long cur_time, cookie_time;
200 
201     if( ctx == NULL || cli_id == NULL )
202         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
203 
204     if( cookie_len != COOKIE_LEN )
205         return( -1 );
206 
207 #if defined(MBEDTLS_THREADING_C)
208     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
209         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
210 #endif
211 
212     if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
213                          &p, p + sizeof( ref_hmac ),
214                          cli_id, cli_id_len ) != 0 )
215         ret = -1;
216 
217 #if defined(MBEDTLS_THREADING_C)
218     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
219         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
220                 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
221 #endif
222 
223     if( ret != 0 )
224         return( ret );
225 
226     if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
227         return( -1 );
228 
229 #if defined(MBEDTLS_HAVE_TIME)
230     cur_time = (unsigned long) mbedtls_time( NULL );
231 #else
232     cur_time = ctx->serial;
233 #endif
234 
235     cookie_time = ( (unsigned long) cookie[0] << 24 ) |
236                   ( (unsigned long) cookie[1] << 16 ) |
237                   ( (unsigned long) cookie[2] <<  8 ) |
238                   ( (unsigned long) cookie[3]       );
239 
240     if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
241         return( -1 );
242 
243     return( 0 );
244 }
245 #endif /* MBEDTLS_SSL_COOKIE_C */
246