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