blob: 01b90e14b10e955ccadb5ff988a7299df1402c00 [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 Rodgman16799db2023-11-02 19:47:20 +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
Harry Ramsey0f6bc412024-10-04 10:36:54 +010012#include "ssl_misc.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"
Janos Follath73c616b2019-12-18 15:07:04 +000019#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050020#include "mbedtls/platform_util.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020021#include "mbedtls/constant_time.h"
SimonBd5800b72016-04-26 07:43:27 +010022
Manuel Pégourié-Gonnardd901d172015-02-16 18:37:53 +000023#include <string.h>
24
Valerio Setti384fbde2024-01-02 13:26:40 +010025#include "mbedtls/psa_util.h"
Andrzej Kurek00644842023-05-30 05:45:00 -040026/* Define a local translating function to save code size by not using too many
27 * arguments in each translating place. */
28static int local_err_translation(psa_status_t status)
29{
30 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040031 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040032 psa_generic_status_to_mbedtls);
33}
34#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050035
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020036/*
Valerio Setti543d00e2022-12-22 14:27:34 +010037 * If DTLS is in use, then at least one of SHA-256 or SHA-384 is
38 * available. Try SHA-256 first as 384 wastes resources
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020039 */
Elena Uziunaite0916cd72024-05-23 17:01:07 +010040#if defined(PSA_WANT_ALG_SHA_256)
Valerio Setti543d00e2022-12-22 14:27:34 +010041#define COOKIE_MD MBEDTLS_MD_SHA256
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020042#define COOKIE_MD_OUTLEN 32
43#define COOKIE_HMAC_LEN 28
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +010044#elif defined(PSA_WANT_ALG_SHA_384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#define COOKIE_MD MBEDTLS_MD_SHA384
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020046#define COOKIE_MD_OUTLEN 48
47#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020048#else
Valerio Setti543d00e2022-12-22 14:27:34 +010049#error "DTLS hello verify needs SHA-256 or SHA-384"
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020050#endif
51
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020052/*
53 * Cookies are formed of a 4-bytes timestamp (or serial number) and
Shaun Case8b0ecbc2021-12-20 21:14:10 -080054 * an HMAC of timestamp and client ID.
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020055 */
Gilles Peskine449bd832023-01-11 14:50:10 +010056#define COOKIE_LEN (4 + COOKIE_HMAC_LEN)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020059{
Neil Armstrong488a40e2022-03-22 10:41:38 +010060 ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if !defined(MBEDTLS_HAVE_TIME)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020062 ctx->serial = 0;
63#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020065
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020066}
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020069{
70 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020071}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020074{
Troy-Butler9ac3e232024-03-22 14:46:04 -040075 if (ctx == NULL) {
76 return;
77 }
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 psa_destroy_key(ctx->psa_hmac_key);
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
85 int (*f_rng)(void *, unsigned char *, size_t),
86 void *p_rng)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020087{
Neil Armstrongd6332012022-03-04 10:26:16 +010088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
89 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
90 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 (void) f_rng;
93 (void) p_rng;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020094
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020095 alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (alg == 0) {
97 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
98 }
Neil Armstrongd6332012022-03-04 10:26:16 +010099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
101 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +0100102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
104 PSA_KEY_USAGE_SIGN_MESSAGE);
105 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
106 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
107 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if ((status = psa_generate_key(&attributes,
110 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500111 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100112 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115}
116
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200117
118/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200119 * Generate cookie for DTLS ClientHello verification
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_ssl_cookie_write(void *p_ctx,
122 unsigned char **p, unsigned char *end,
123 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200124{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100125 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100126 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100127 size_t sign_mac_length = 0;
Janos Follath865b3eb2019-12-16 11:46:15 +0000128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200130 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (ctx == NULL || cli_id == NULL) {
133 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
134 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200140#else
141 t = ctx->serial++;
142#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200143
Joe Subbianib6511b02021-07-16 15:02:55 +0100144 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200145 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
148 ctx->psa_hmac_alg);
149 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500150 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100151 goto exit;
152 }
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 status = psa_mac_update(&operation, *p - 4, 4);
155 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500156 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100157 goto exit;
158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 status = psa_mac_update(&operation, cli_id, cli_id_len);
161 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500162 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100163 goto exit;
164 }
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
167 &sign_mac_length);
168 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500169 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100170 goto exit;
171 }
172
173 *p += COOKIE_HMAC_LEN;
174
175 ret = 0;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200176
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100177exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 status = psa_mac_abort(&operation);
179 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500180 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200183}
184
185/*
186 * Check a cookie
187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188int mbedtls_ssl_cookie_check(void *p_ctx,
189 const unsigned char *cookie, size_t cookie_len,
190 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200191{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100192 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100193 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100194 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200196 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (ctx == NULL || cli_id == NULL) {
199 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
200 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (cookie_len != COOKIE_LEN) {
203 return -1;
204 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
207 ctx->psa_hmac_alg);
208 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500209 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100210 goto exit;
211 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 status = psa_mac_update(&operation, cookie, 4);
214 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500215 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100216 goto exit;
217 }
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 status = psa_mac_update(&operation, cli_id,
220 cli_id_len);
221 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500222 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100223 goto exit;
224 }
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 status = psa_mac_verify_finish(&operation, cookie + 4,
227 COOKIE_HMAC_LEN);
228 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500229 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100230 goto exit;
231 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100232
233 ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200237#else
238 cur_time = ctx->serial;
239#endif
240
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +0100241 cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100244 ret = -1;
245 goto exit;
246 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200247
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100248exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 status = psa_mac_abort(&operation);
250 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500251 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#endif /* MBEDTLS_SSL_COOKIE_C */