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 #include "mbedtls/platform.h"
29 
30 #include "mbedtls/ssl_cookie.h"
31 #include "ssl_misc.h"
32 #include "mbedtls/error.h"
33 #include "mbedtls/platform_util.h"
34 #include "mbedtls/constant_time.h"
35 
36 #include "mbedtls/legacy_or_psa.h"
37 
38 #include <string.h>
39 
40 /*
41  * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-384 is
42  * available. Try SHA-256 first, 384 wastes resources
43  */
44 #if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_LOWLEVEL_OR_PSA)
45 #define COOKIE_MD           MBEDTLS_MD_SHA224
46 #define COOKIE_MD_OUTLEN    32
47 #define COOKIE_HMAC_LEN     28
48 #elif defined(MBEDTLS_HAS_ALG_SHA_384_VIA_LOWLEVEL_OR_PSA)
49 #define COOKIE_MD           MBEDTLS_MD_SHA384
50 #define COOKIE_MD_OUTLEN    48
51 #define COOKIE_HMAC_LEN     28
52 #elif defined(MBEDTLS_HAS_ALG_SHA_1_VIA_LOWLEVEL_OR_PSA)
53 #define COOKIE_MD           MBEDTLS_MD_SHA1
54 #define COOKIE_MD_OUTLEN    20
55 #define COOKIE_HMAC_LEN     20
56 #else
57 #error "DTLS hello verify needs SHA-1 or SHA-2"
58 #endif
59 
60 /*
61  * Cookies are formed of a 4-bytes timestamp (or serial number) and
62  * an HMAC of timestamp and client ID.
63  */
64 #define COOKIE_LEN      ( 4 + COOKIE_HMAC_LEN )
65 
mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx * ctx)66 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
67 {
68 #if defined(MBEDTLS_USE_PSA_CRYPTO)
69     ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
70 #else
71     mbedtls_md_init( &ctx->hmac_ctx );
72 #endif /* MBEDTLS_USE_PSA_CRYPTO */
73 #if !defined(MBEDTLS_HAVE_TIME)
74     ctx->serial = 0;
75 #endif
76     ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
77 
78 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
79 #if defined(MBEDTLS_THREADING_C)
80     mbedtls_mutex_init( &ctx->mutex );
81 #endif
82 #endif /* !MBEDTLS_USE_PSA_CRYPTO */
83 }
84 
mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx * ctx,unsigned long delay)85 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
86 {
87     ctx->timeout = delay;
88 }
89 
mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx * ctx)90 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
91 {
92 #if defined(MBEDTLS_USE_PSA_CRYPTO)
93     psa_destroy_key( ctx->psa_hmac_key );
94 #else
95     mbedtls_md_free( &ctx->hmac_ctx );
96 
97 #if defined(MBEDTLS_THREADING_C)
98     mbedtls_mutex_free( &ctx->mutex );
99 #endif
100 #endif /* MBEDTLS_USE_PSA_CRYPTO */
101 
102     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
103 }
104 
mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx * ctx,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)105 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
106                       int (*f_rng)(void *, unsigned char *, size_t),
107                       void *p_rng )
108 {
109 #if defined(MBEDTLS_USE_PSA_CRYPTO)
110     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
111     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
112     psa_algorithm_t alg;
113 
114     (void)f_rng;
115     (void)p_rng;
116 
117     alg = mbedtls_hash_info_psa_from_md( COOKIE_MD );
118     if( alg == 0 )
119         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
120 
121     ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC( PSA_ALG_HMAC( alg ),
122                                                COOKIE_HMAC_LEN );
123 
124     psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
125                                           PSA_KEY_USAGE_SIGN_MESSAGE );
126     psa_set_key_algorithm( &attributes, ctx->psa_hmac_alg );
127     psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );
128     psa_set_key_bits( &attributes, PSA_BYTES_TO_BITS( COOKIE_MD_OUTLEN ) );
129 
130     if( ( status = psa_generate_key( &attributes,
131                                      &ctx->psa_hmac_key ) ) != PSA_SUCCESS )
132     {
133         return psa_ssl_status_to_mbedtls( status );
134     }
135 #else
136     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
137     unsigned char key[COOKIE_MD_OUTLEN];
138 
139     if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
140         return( ret );
141 
142     ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
143     if( ret != 0 )
144         return( ret );
145 
146     ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
147     if( ret != 0 )
148         return( ret );
149 
150     mbedtls_platform_zeroize( key, sizeof( key ) );
151 #endif /* MBEDTLS_USE_PSA_CRYPTO */
152 
153     return( 0 );
154 }
155 
156 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
157 /*
158  * Generate the HMAC part of a cookie
159  */
160 MBEDTLS_CHECK_RETURN_CRITICAL
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)161 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
162                             const unsigned char time[4],
163                             unsigned char **p, unsigned char *end,
164                             const unsigned char *cli_id, size_t cli_id_len )
165 {
166     unsigned char hmac_out[COOKIE_MD_OUTLEN];
167 
168     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN );
169 
170     if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 ||
171         mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
172         mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
173         mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
174     {
175         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
176     }
177 
178     memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
179     *p += COOKIE_HMAC_LEN;
180 
181     return( 0 );
182 }
183 #endif /* !MBEDTLS_USE_PSA_CRYPTO */
184 
185 /*
186  * Generate cookie for DTLS ClientHello verification
187  */
mbedtls_ssl_cookie_write(void * p_ctx,unsigned char ** p,unsigned char * end,const unsigned char * cli_id,size_t cli_id_len)188 int mbedtls_ssl_cookie_write( void *p_ctx,
189                       unsigned char **p, unsigned char *end,
190                       const unsigned char *cli_id, size_t cli_id_len )
191 {
192 #if defined(MBEDTLS_USE_PSA_CRYPTO)
193     psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
194     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
195     size_t sign_mac_length = 0;
196 #endif
197     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
198     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
199     unsigned long t;
200 
201     if( ctx == NULL || cli_id == NULL )
202         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
203 
204     MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN );
205 
206 #if defined(MBEDTLS_HAVE_TIME)
207     t = (unsigned long) mbedtls_time( NULL );
208 #else
209     t = ctx->serial++;
210 #endif
211 
212     MBEDTLS_PUT_UINT32_BE(t, *p, 0);
213     *p += 4;
214 
215 #if defined(MBEDTLS_USE_PSA_CRYPTO)
216     status = psa_mac_sign_setup( &operation, ctx->psa_hmac_key,
217                                  ctx->psa_hmac_alg );
218     if( status != PSA_SUCCESS )
219     {
220         ret = psa_ssl_status_to_mbedtls( status );
221         goto exit;
222     }
223 
224     status = psa_mac_update( &operation, *p - 4, 4 );
225     if( status != PSA_SUCCESS )
226     {
227         ret = psa_ssl_status_to_mbedtls( status );
228         goto exit;
229     }
230 
231     status = psa_mac_update( &operation, cli_id, cli_id_len );
232     if( status != PSA_SUCCESS )
233     {
234         ret = psa_ssl_status_to_mbedtls( status );
235         goto exit;
236     }
237 
238     status = psa_mac_sign_finish( &operation, *p, COOKIE_MD_OUTLEN,
239                                   &sign_mac_length );
240     if( status != PSA_SUCCESS )
241     {
242         ret = psa_ssl_status_to_mbedtls( status );
243         goto exit;
244     }
245 
246     *p += COOKIE_HMAC_LEN;
247 
248     ret = 0;
249 #else
250 #if defined(MBEDTLS_THREADING_C)
251     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
252         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
253 #endif
254 
255     ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
256                            p, end, cli_id, cli_id_len );
257 
258 #if defined(MBEDTLS_THREADING_C)
259     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
260         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
261                 MBEDTLS_ERR_THREADING_MUTEX_ERROR ) );
262 #endif
263 #endif /* MBEDTLS_USE_PSA_CRYPTO */
264 
265 #if defined(MBEDTLS_USE_PSA_CRYPTO)
266 exit:
267     status = psa_mac_abort( &operation );
268     if( status != PSA_SUCCESS )
269         ret = psa_ssl_status_to_mbedtls( status );
270 #endif /* MBEDTLS_USE_PSA_CRYPTO */
271     return( ret );
272 }
273 
274 /*
275  * Check a cookie
276  */
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)277 int mbedtls_ssl_cookie_check( void *p_ctx,
278                       const unsigned char *cookie, size_t cookie_len,
279                       const unsigned char *cli_id, size_t cli_id_len )
280 {
281 #if defined(MBEDTLS_USE_PSA_CRYPTO)
282     psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
283     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
284 #else
285     unsigned char ref_hmac[COOKIE_HMAC_LEN];
286     unsigned char *p = ref_hmac;
287 #endif
288     int ret = 0;
289     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
290     unsigned long cur_time, cookie_time;
291 
292     if( ctx == NULL || cli_id == NULL )
293         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
294 
295     if( cookie_len != COOKIE_LEN )
296         return( -1 );
297 
298 #if defined(MBEDTLS_USE_PSA_CRYPTO)
299     status = psa_mac_verify_setup( &operation, ctx->psa_hmac_key,
300                                    ctx->psa_hmac_alg );
301     if( status != PSA_SUCCESS )
302     {
303         ret = psa_ssl_status_to_mbedtls( status );
304         goto exit;
305     }
306 
307     status = psa_mac_update( &operation, cookie, 4 );
308     if( status != PSA_SUCCESS )
309     {
310         ret = psa_ssl_status_to_mbedtls( status );
311         goto exit;
312     }
313 
314     status = psa_mac_update( &operation, cli_id,
315                              cli_id_len );
316     if( status != PSA_SUCCESS )
317     {
318         ret = psa_ssl_status_to_mbedtls( status );
319         goto exit;
320     }
321 
322     status = psa_mac_verify_finish( &operation, cookie + 4,
323                                     COOKIE_HMAC_LEN );
324     if( status != PSA_SUCCESS )
325     {
326         ret = psa_ssl_status_to_mbedtls( status );
327         goto exit;
328     }
329 
330     ret = 0;
331 #else
332 #if defined(MBEDTLS_THREADING_C)
333     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
334         return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) );
335 #endif
336 
337     if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
338                          &p, p + sizeof( ref_hmac ),
339                          cli_id, cli_id_len ) != 0 )
340         ret = -1;
341 
342 #if defined(MBEDTLS_THREADING_C)
343     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
344     {
345         ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR,
346                                  MBEDTLS_ERR_THREADING_MUTEX_ERROR );
347     }
348 #endif
349 
350     if( ret != 0 )
351         goto exit;
352 
353     if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
354     {
355         ret = -1;
356         goto exit;
357     }
358 #endif /* MBEDTLS_USE_PSA_CRYPTO */
359 
360 #if defined(MBEDTLS_HAVE_TIME)
361     cur_time = (unsigned long) mbedtls_time( NULL );
362 #else
363     cur_time = ctx->serial;
364 #endif
365 
366     cookie_time = ( (unsigned long) cookie[0] << 24 ) |
367                   ( (unsigned long) cookie[1] << 16 ) |
368                   ( (unsigned long) cookie[2] <<  8 ) |
369                   ( (unsigned long) cookie[3]       );
370 
371     if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
372     {
373         ret = -1;
374         goto exit;
375     }
376 
377 exit:
378 #if defined(MBEDTLS_USE_PSA_CRYPTO)
379     status = psa_mac_abort( &operation );
380     if( status != PSA_SUCCESS )
381         ret = psa_ssl_status_to_mbedtls( status );
382 #else
383     mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) );
384 #endif /* MBEDTLS_USE_PSA_CRYPTO */
385     return( ret );
386 }
387 #endif /* MBEDTLS_SSL_COOKIE_C */
388