blob: 1ac9c41760d8742ce0c706831b9a98a5dda88787 [file] [log] [blame]
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001/*
2 * DTLS cookie callbacks implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020018 */
19/*
20 * These session callbacks use a simple chained list
21 * to store and retrieve the session information.
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020029
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/ssl_cookie.h"
31#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000032#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020034#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010035
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000036#include <string.h>
37
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020038/*
39 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
40 * available. Try SHA-256 first, 512 wastes resources since we need to stay
41 * with max 32 bytes of cookie for DTLS 1.0
42 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_SHA256_C)
44#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020045#define COOKIE_MD_OUTLEN 32
46#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#elif defined(MBEDTLS_SHA512_C)
48#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020049#define COOKIE_MD_OUTLEN 48
50#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#elif defined(MBEDTLS_SHA1_C)
52#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020053#define COOKIE_MD_OUTLEN 20
54#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020055#else
56#error "DTLS hello verify needs SHA-1 or SHA-2"
57#endif
58
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020059/*
60 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case0e7791f2021-12-20 21:14:10 -080061 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020062 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020066{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 mbedtls_md_init(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020069 ctx->serial = 0;
70#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020072
73#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020075#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020076}
77
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020079{
80 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020081}
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020084{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020086
87#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020089#endif
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020092}
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
95 int (*f_rng)(void *, unsigned char *, size_t),
96 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020097{
Janos Follath865b3eb2019-12-16 11:46:15 +000098 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020099 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
102 return ret;
103 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
106 if (ret != 0) {
107 return ret;
108 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
111 if (ret != 0) {
112 return ret;
113 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 mbedtls_platform_zeroize(key, sizeof(key));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200118}
119
120/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200121 * Generate the HMAC part of a cookie
122 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200123MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
125 const unsigned char time[4],
126 unsigned char **p, unsigned char *end,
127 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200128{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200129 unsigned char hmac_out[COOKIE_MD_OUTLEN];
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
134 mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
135 mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
136 mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
137 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200138 }
139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200141 *p += COOKIE_HMAC_LEN;
142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200144}
145
146/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200147 * Generate cookie for DTLS ClientHello verification
148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149int 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)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200152{
Janos Follath865b3eb2019-12-16 11:46:15 +0000153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200155 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 if (ctx == NULL || cli_id == NULL) {
158 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
159 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200165#else
166 t = ctx->serial++;
167#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200168
Joe Subbiani6627fb22021-07-16 15:02:55 +0100169 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200170 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200171
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200172#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
174 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
175 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200176#endif
177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
179 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200180
181#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
183 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
184 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
185 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200186#endif
187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200189}
190
191/*
192 * Check a cookie
193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194int mbedtls_ssl_cookie_check(void *p_ctx,
195 const unsigned char *cookie, size_t cookie_len,
196 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200197{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200198 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200199 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200200 unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200202 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 if (ctx == NULL || cli_id == NULL) {
205 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
206 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 if (cookie_len != COOKIE_LEN) {
209 return -1;
210 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200211
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200212#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
214 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskine69d3b862021-12-13 12:35:08 +0100215 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200216#endif
217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
219 &p, p + sizeof(ref_hmac),
220 cli_id, cli_id_len) != 0) {
221 ret = -1;
222 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224#if defined(MBEDTLS_THREADING_C)
225 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
226 ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
227 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
228 }
229#endif
230
231 if (ret != 0) {
232 goto exit;
233 }
234
235 if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
Gilles Peskine69d3b862021-12-13 12:35:08 +0100236 ret = -1;
237 goto exit;
238 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200242#else
243 cur_time = ctx->serial;
244#endif
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 cookie_time = ((unsigned long) cookie[0] << 24) |
247 ((unsigned long) cookie[1] << 16) |
248 ((unsigned long) cookie[2] << 8) |
249 ((unsigned long) cookie[3]);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskine69d3b862021-12-13 12:35:08 +0100252 ret = -1;
253 goto exit;
254 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200255
Gilles Peskine69d3b862021-12-13 12:35:08 +0100256exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
258 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200259}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#endif /* MBEDTLS_SSL_COOKIE_C */