xref: /optee_os/lib/libmbedtls/mbedtls/library/ssl_cookie.c (revision 11fa71b9ddb429088f325cfda430183003ccd1db)
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  *  DTLS cookie callbacks implementation
4  *
5  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 /*
22  * These session callbacks use a simple chained list
23  * to store and retrieve the session information.
24  */
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #if defined(MBEDTLS_SSL_COOKIE_C)
33 
34 #if defined(MBEDTLS_PLATFORM_C)
35 #include "mbedtls/platform.h"
36 #else
37 #define mbedtls_calloc    calloc
38 #define mbedtls_free      free
39 #endif
40 
41 #include "mbedtls/ssl_cookie.h"
42 #include "mbedtls/ssl_internal.h"
43 #include "mbedtls/error.h"
44 #include "mbedtls/platform_util.h"
45 
46 #include <string.h>
47 
48 /*
49  * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
50  * available. Try SHA-256 first, 512 wastes resources since we need to stay
51  * with max 32 bytes of cookie for DTLS 1.0
52  */
53 #if defined(MBEDTLS_SHA256_C)
54 #define COOKIE_MD           MBEDTLS_MD_SHA224
55 #define COOKIE_MD_OUTLEN    32
56 #define COOKIE_HMAC_LEN     28
57 #elif defined(MBEDTLS_SHA512_C)
58 #define COOKIE_MD           MBEDTLS_MD_SHA384
59 #define COOKIE_MD_OUTLEN    48
60 #define COOKIE_HMAC_LEN     28
61 #elif defined(MBEDTLS_SHA1_C)
62 #define COOKIE_MD           MBEDTLS_MD_SHA1
63 #define COOKIE_MD_OUTLEN    20
64 #define COOKIE_HMAC_LEN     20
65 #else
66 #error "DTLS hello verify needs SHA-1 or SHA-2"
67 #endif
68 
69 /*
70  * Cookies are formed of a 4-bytes timestamp (or serial number) and
71  * an HMAC of timestemp and client ID.
72  */
73 #define COOKIE_LEN      ( 4 + COOKIE_HMAC_LEN )
74 
75 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
76 {
77     mbedtls_md_init( &ctx->hmac_ctx );
78 #if !defined(MBEDTLS_HAVE_TIME)
79     ctx->serial = 0;
80 #endif
81     ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
82 
83 #if defined(MBEDTLS_THREADING_C)
84     mbedtls_mutex_init( &ctx->mutex );
85 #endif
86 }
87 
88 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
89 {
90     ctx->timeout = delay;
91 }
92 
93 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
94 {
95     mbedtls_md_free( &ctx->hmac_ctx );
96 
97 #if defined(MBEDTLS_THREADING_C)
98     mbedtls_mutex_free( &ctx->mutex );
99 #endif
100 
101     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
102 }
103 
104 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
105                       int (*f_rng)(void *, unsigned char *, size_t),
106                       void *p_rng )
107 {
108     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
109     unsigned char key[COOKIE_MD_OUTLEN];
110 
111     if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
112         return( ret );
113 
114     ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
115     if( ret != 0 )
116         return( ret );
117 
118     ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
119     if( ret != 0 )
120         return( ret );
121 
122     mbedtls_platform_zeroize( key, sizeof( key ) );
123 
124     return( 0 );
125 }
126 
127 /*
128  * Generate the HMAC part of a cookie
129  */
130 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
131                             const unsigned char time[4],
132                             unsigned char **p, unsigned char *end,
133                             const unsigned char *cli_id, size_t cli_id_len )
134 {
135     unsigned char hmac_out[COOKIE_MD_OUTLEN];
136 
137     if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
138         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
139 
140     if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 ||
141         mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
142         mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
143         mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
144     {
145         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
146     }
147 
148     memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
149     *p += COOKIE_HMAC_LEN;
150 
151     return( 0 );
152 }
153 
154 /*
155  * Generate cookie for DTLS ClientHello verification
156  */
157 int mbedtls_ssl_cookie_write( void *p_ctx,
158                       unsigned char **p, unsigned char *end,
159                       const unsigned char *cli_id, size_t cli_id_len )
160 {
161     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
162     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
163     unsigned long t;
164 
165     if( ctx == NULL || cli_id == NULL )
166         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167 
168     if( (size_t)( end - *p ) < COOKIE_LEN )
169         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
170 
171 #if defined(MBEDTLS_HAVE_TIME)
172     t = (unsigned long) mbedtls_time( NULL );
173 #else
174     t = ctx->serial++;
175 #endif
176 
177     (*p)[0] = (unsigned char)( t >> 24 );
178     (*p)[1] = (unsigned char)( t >> 16 );
179     (*p)[2] = (unsigned char)( t >>  8 );
180     (*p)[3] = (unsigned char)( t       );
181     *p += 4;
182 
183 #if defined(MBEDTLS_THREADING_C)
184     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
185         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
186 #endif
187 
188     ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
189                            p, end, cli_id, cli_id_len );
190 
191 #if defined(MBEDTLS_THREADING_C)
192     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
193         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
194                 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
195 #endif
196 
197     return( ret );
198 }
199 
200 /*
201  * Check a cookie
202  */
203 int mbedtls_ssl_cookie_check( void *p_ctx,
204                       const unsigned char *cookie, size_t cookie_len,
205                       const unsigned char *cli_id, size_t cli_id_len )
206 {
207     unsigned char ref_hmac[COOKIE_HMAC_LEN];
208     int ret = 0;
209     unsigned char *p = ref_hmac;
210     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
211     unsigned long cur_time, cookie_time;
212 
213     if( ctx == NULL || cli_id == NULL )
214         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
215 
216     if( cookie_len != COOKIE_LEN )
217         return( -1 );
218 
219 #if defined(MBEDTLS_THREADING_C)
220     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
221         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
222 #endif
223 
224     if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
225                          &p, p + sizeof( ref_hmac ),
226                          cli_id, cli_id_len ) != 0 )
227         ret = -1;
228 
229 #if defined(MBEDTLS_THREADING_C)
230     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
231         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
232                 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
233 #endif
234 
235     if( ret != 0 )
236         return( ret );
237 
238     if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
239         return( -1 );
240 
241 #if defined(MBEDTLS_HAVE_TIME)
242     cur_time = (unsigned long) mbedtls_time( NULL );
243 #else
244     cur_time = ctx->serial;
245 #endif
246 
247     cookie_time = ( (unsigned long) cookie[0] << 24 ) |
248                   ( (unsigned long) cookie[1] << 16 ) |
249                   ( (unsigned long) cookie[2] <<  8 ) |
250                   ( (unsigned long) cookie[3]       );
251 
252     if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
253         return( -1 );
254 
255     return( 0 );
256 }
257 #endif /* MBEDTLS_SSL_COOKIE_C */
258