blob: 11811ee30f01a0b011a6f86a836a13a61d104bbf [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
Ben Taylor0cfe54e2025-03-05 15:49:08 +000084int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020085{
Neil Armstrongd6332012022-03-04 10:26:16 +010086 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
87 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
88 psa_algorithm_t alg;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020089
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020090
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020091 alg = mbedtls_md_psa_alg_from_type(COOKIE_MD);
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (alg == 0) {
93 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
94 }
Neil Armstrongd6332012022-03-04 10:26:16 +010095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg),
97 COOKIE_HMAC_LEN);
Neil Armstrongd6332012022-03-04 10:26:16 +010098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE |
100 PSA_KEY_USAGE_SIGN_MESSAGE);
101 psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg);
102 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
103 psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN));
Neil Armstrongd6332012022-03-04 10:26:16 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if ((status = psa_generate_key(&attributes,
106 &ctx->psa_hmac_key)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500107 return PSA_TO_MBEDTLS_ERR(status);
Neil Armstrongd6332012022-03-04 10:26:16 +0100108 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 return 0;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200111}
112
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200113
114/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200115 * Generate cookie for DTLS ClientHello verification
116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117int mbedtls_ssl_cookie_write(void *p_ctx,
118 unsigned char **p, unsigned char *end,
119 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200120{
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100121 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100122 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100123 size_t sign_mac_length = 0;
Janos Follath865b3eb2019-12-16 11:46:15 +0000124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200126 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (ctx == NULL || cli_id == NULL) {
129 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
130 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN);
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 t = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200136#else
137 t = ctx->serial++;
138#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200139
Joe Subbianib6511b02021-07-16 15:02:55 +0100140 MBEDTLS_PUT_UINT32_BE(t, *p, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200141 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
144 ctx->psa_hmac_alg);
145 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500146 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100147 goto exit;
148 }
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 status = psa_mac_update(&operation, *p - 4, 4);
151 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500152 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100153 goto exit;
154 }
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 status = psa_mac_update(&operation, cli_id, cli_id_len);
157 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500158 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100159 goto exit;
160 }
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
163 &sign_mac_length);
164 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500165 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100166 goto exit;
167 }
168
169 *p += COOKIE_HMAC_LEN;
170
171 ret = 0;
Manuel Pégourié-Gonnard2a84dfd2015-05-28 15:48:09 +0200172
Neil Armstrong2d5e3432022-03-21 11:39:52 +0100173exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 status = psa_mac_abort(&operation);
175 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500176 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200179}
180
181/*
182 * Check a cookie
183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184int mbedtls_ssl_cookie_check(void *p_ctx,
185 const unsigned char *cookie, size_t cookie_len,
186 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200187{
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100188 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Neil Armstrong79daea22022-03-21 12:05:51 +0100189 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100190 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200192 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (ctx == NULL || cli_id == NULL) {
195 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
196 }
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (cookie_len != COOKIE_LEN) {
199 return -1;
200 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
203 ctx->psa_hmac_alg);
204 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500205 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100206 goto exit;
207 }
Neil Armstrong7cd02702022-03-04 15:08:43 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 status = psa_mac_update(&operation, cookie, 4);
210 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500211 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100212 goto exit;
213 }
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 status = psa_mac_update(&operation, cli_id,
216 cli_id_len);
217 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500218 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100219 goto exit;
220 }
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 status = psa_mac_verify_finish(&operation, cookie + 4,
223 COOKIE_HMAC_LEN);
224 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500225 ret = PSA_TO_MBEDTLS_ERR(status);
Neil Armstrong6d5baf52022-03-07 14:25:18 +0100226 goto exit;
227 }
Neil Armstrong79daea22022-03-21 12:05:51 +0100228
229 ret = 0;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 cur_time = (unsigned long) mbedtls_time(NULL);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200233#else
234 cur_time = ctx->serial;
235#endif
236
Thomas Daubneyf9f0ba82023-05-23 17:34:33 +0100237 cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0);
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) {
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100240 ret = -1;
241 goto exit;
242 }
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200243
Gilles Peskinec2f7b752021-12-13 12:35:08 +0100244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 status = psa_mac_abort(&operation);
246 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500247 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 return ret;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_SSL_COOKIE_C */