ReactOS 0.4.15-dev-8417-gb6b82fe
ssl_cookie.c
Go to the documentation of this file.
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"
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
100{
101 mbedtls_md_init( &ctx->hmac_ctx );
102#if !defined(MBEDTLS_HAVE_TIME)
103 ctx->serial = 0;
104#endif
106
107#if defined(MBEDTLS_THREADING_C)
108 mbedtls_mutex_init( &ctx->mutex );
109#endif
110}
111
113{
114 ctx->timeout = delay;
115}
116
118{
119 mbedtls_md_free( &ctx->hmac_ctx );
120
121#if defined(MBEDTLS_THREADING_C)
122 mbedtls_mutex_free( &ctx->mutex );
123#endif
124
126}
127
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 */
154static 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 {
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 */
180int 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;
186 unsigned long t;
187
188 if( ctx == NULL || cli_id == NULL )
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 )
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 )
217#endif
218
219 return( ret );
220}
221
222/*
223 * Check a cookie
224 */
225int 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;
233 unsigned long cur_time, cookie_time;
234
235 if( ctx == NULL || cli_id == NULL )
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 )
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 )
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 */
time_t cur_time
#define NULL
Definition: types.h:112
unsigned char
Definition: typeof.h:29
GLuint GLuint end
Definition: gl.h:1545
GLdouble GLdouble t
Definition: gl.h:2047
GLfloat GLfloat p
Definition: glext.h:8902
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
This function selects the message digest algorithm to use, and allocates internal structures.
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
This function prepares to authenticate a new message with the same key as the previous HMAC operation...
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
This function finishes the HMAC operation, and writes the result to the output buffer.
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing HMAC computation.
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
This function sets the HMAC key and prepares to authenticate a new message.
void mbedtls_md_init(mbedtls_md_context_t *ctx)
This function initializes a message-digest context without binding it to a particular message-digest ...
void mbedtls_md_free(mbedtls_md_context_t *ctx)
This function clears the internal structure of ctx and frees any embedded internal structure,...
__u16 time
Definition: mkdosfs.c:8
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define mbedtls_time
Definition: platform_time.h:99
void mbedtls_platform_zeroize(void *buf, size_t len)
Securely zeroize a buffer.
Definition: platform_util.c:98
Common and shared functions used by multiple modules in the Mbed TLS library.
#define long
Definition: qsort.c:33
#define mbedtls_md_info_from_type
Configuration options (set of defines)
This file contains the definitions and functions of the Mbed TLS platform abstraction layer.
#define MBEDTLS_ERR_SSL_BAD_INPUT_DATA
Definition: ssl.h:97
#define MBEDTLS_ERR_SSL_INTERNAL_ERROR
Definition: ssl.h:134
Internal functions shared by the SSL modules.
static int mbedtls_ssl_safer_memcmp(const void *a, const void *b, size_t n)
Definition: ssl_internal.h:826
#define MBEDTLS_SSL_CHK_BUF_PTR(cur, end, need)
This macro checks if the remaining size in a buffer is greater or equal than a needed space....
Definition: ssl_internal.h:315
Definition: cookie.c:34
Definition: copy.c:22
#define MBEDTLS_ERR_THREADING_MUTEX_ERROR
Definition: threading.h:69
int ret