blob: 067a4916a5c1902a6e2f46096e64464b1bc021d1 [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02006 */
7/*
8 * These session callbacks use a simple chained list
9 * to store and retrieve the session information.
10 */
11
Gilles Peskinedb09ef62020-06-03 01:43:33 +020012#include "common.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020015
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000016#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020017
SimonBd5800b72016-04-26 07:43:27 +010018#include "mbedtls/ssl_cookie.h"
19#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000020#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050021#include "mbedtls/platform_util.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020022#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010023
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000024#include <string.h>
25
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020026/*
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_SHA256_C)
32#define COOKIE_MD MBEDTLS_MD_SHA224
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020033#define COOKIE_MD_OUTLEN 32
34#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#elif defined(MBEDTLS_SHA512_C)
36#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020037#define COOKIE_MD_OUTLEN 48
38#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#elif defined(MBEDTLS_SHA1_C)
40#define COOKIE_MD MBEDTLS_MD_SHA1
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020041#define COOKIE_MD_OUTLEN 20
42#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020043#else
44#error "DTLS hello verify needs SHA-1 or SHA-2"
45#endif
46
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020047/*
48 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case0e7791f2021-12-20 21:14:10 -080049 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020050 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020054{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 mbedtls_md_init(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020057 ctx->serial = 0;
58#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020060
61#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 mbedtls_mutex_init(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020063#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020064}
65
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020067{
68 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020069}
70
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020072{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_md_free(&ctx->hmac_ctx);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020074
75#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_mutex_free(&ctx->mutex);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020077#endif
78
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020080}
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
83 int (*f_rng)(void *, unsigned char *, size_t),
84 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020085{
Janos Follath865b3eb2019-12-16 11:46:15 +000086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020087 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
90 return ret;
91 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
94 if (ret != 0) {
95 return ret;
96 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
99 if (ret != 0) {
100 return ret;
101 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 mbedtls_platform_zeroize(key, sizeof(key));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200106}
107
108/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200109 * Generate the HMAC part of a cookie
110 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200111MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112static 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)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200116{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200117 unsigned char hmac_out[COOKIE_MD_OUTLEN];
118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 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;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200126 }
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200129 *p += COOKIE_HMAC_LEN;
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 return 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200132}
133
134/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200135 * Generate cookie for DTLS ClientHello verification
136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137int 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)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200140{
Janos Follath865b3eb2019-12-16 11:46:15 +0000141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200143 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 if (ctx == NULL || cli_id == NULL) {
146 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
147 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200153#else
154 t = ctx->serial++;
155#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200156
Joe Subbiani6627fb22021-07-16 15:02:55 +0100157 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200158 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200159
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200160#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
162 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
163 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200164#endif
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
167 p, end, cli_id, cli_id_len);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200168
169#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
171 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
172 MBEDTLS_ERR_THREADING_MUTEX_ERROR);
173 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200174#endif
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200177}
178
179/*
180 * Check a cookie
181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182int 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)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200185{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200186 unsigned char ref_hmac[COOKIE_HMAC_LEN];
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200187 int ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200188 unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200190 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 if (ctx == NULL || cli_id == NULL) {
193 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
194 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 if (cookie_len != COOKIE_LEN) {
197 return -1;
198 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200199
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200200#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
202 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
Gilles Peskine69d3b862021-12-13 12:35:08 +0100203 }
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200204#endif
205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 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 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212#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) {
Gilles Peskine69d3b862021-12-13 12:35:08 +0100224 ret = -1;
225 goto exit;
226 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200230#else
231 cur_time = ctx->serial;
232#endif
233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 cookie_time = ((unsigned long) cookie[0] << 24) |
235 ((unsigned long) cookie[1] << 16) |
236 ((unsigned long) cookie[2] << 8) |
237 ((unsigned long) cookie[3]);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskine69d3b862021-12-13 12:35:08 +0100240 ret = -1;
241 goto exit;
242 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200243
Gilles Peskine69d3b862021-12-13 12:35:08 +0100244exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
246 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_SSL_COOKIE_C */