Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * DTLS cookie callbacks implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * These session callbacks use a simple chained list |
| 9 | * to store and retrieve the session information. |
| 10 | */ |
| 11 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 12 | #include "common.h" |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #if defined(MBEDTLS_SSL_COOKIE_C) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 16 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 17 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 18 | #include "mbedtls/ssl_cookie.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 19 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 20 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 21 | #include "mbedtls/platform_util.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 22 | #include "mbedtls/constant_time.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 26 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 384fbde | 2024-01-02 13:26:40 +0100 | [diff] [blame] | 27 | #include "mbedtls/psa_util.h" |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 28 | /* Define a local translating function to save code size by not using too many |
| 29 | * arguments in each translating place. */ |
| 30 | static int local_err_translation(psa_status_t status) |
| 31 | { |
| 32 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 33 | ARRAY_LENGTH(psa_to_ssl_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 34 | psa_generic_status_to_mbedtls); |
| 35 | } |
| 36 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 37 | #endif |
| 38 | |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 39 | /* |
Valerio Setti | 543d00e | 2022-12-22 14:27:34 +0100 | [diff] [blame] | 40 | * If DTLS is in use, then at least one of SHA-256 or SHA-384 is |
| 41 | * available. Try SHA-256 first as 384 wastes resources |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 42 | */ |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 43 | #if defined(MBEDTLS_MD_CAN_SHA256) |
Valerio Setti | 543d00e | 2022-12-22 14:27:34 +0100 | [diff] [blame] | 44 | #define COOKIE_MD MBEDTLS_MD_SHA256 |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 45 | #define COOKIE_MD_OUTLEN 32 |
| 46 | #define COOKIE_HMAC_LEN 28 |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 47 | #elif defined(MBEDTLS_MD_CAN_SHA384) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #define COOKIE_MD MBEDTLS_MD_SHA384 |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 49 | #define COOKIE_MD_OUTLEN 48 |
| 50 | #define COOKIE_HMAC_LEN 28 |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 51 | #else |
Valerio Setti | 543d00e | 2022-12-22 14:27:34 +0100 | [diff] [blame] | 52 | #error "DTLS hello verify needs SHA-256 or SHA-384" |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Cookies are formed of a 4-bytes timestamp (or serial number) and |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 57 | * an HMAC of timestamp and client ID. |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 58 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | #define COOKIE_LEN (4 + COOKIE_HMAC_LEN) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 60 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 62 | { |
Neil Armstrong | bca99ee | 2022-03-04 10:20:20 +0100 | [diff] [blame] | 63 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Neil Armstrong | 488a40e | 2022-03-22 10:41:38 +0100 | [diff] [blame] | 64 | ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | 77b69ab | 2022-03-04 14:35:13 +0100 | [diff] [blame] | 65 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | mbedtls_md_init(&ctx->hmac_ctx); |
Neil Armstrong | 77b69ab | 2022-03-04 14:35:13 +0100 | [diff] [blame] | 67 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #if !defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 69 | ctx->serial = 0; |
| 70 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT; |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 72 | |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 73 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 74 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | mbedtls_mutex_init(&ctx->mutex); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 76 | #endif |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 77 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay) |
Manuel Pégourié-Gonnard | bef8f09 | 2014-07-23 23:40:29 +0200 | [diff] [blame] | 81 | { |
| 82 | ctx->timeout = delay; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 86 | { |
Troy-Butler | 9ac3e23 | 2024-03-22 14:46:04 -0400 | [diff] [blame] | 87 | if (ctx == NULL) { |
| 88 | return; |
| 89 | } |
| 90 | |
Neil Armstrong | bca99ee | 2022-03-04 10:20:20 +0100 | [diff] [blame] | 91 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | psa_destroy_key(ctx->psa_hmac_key); |
Neil Armstrong | 77b69ab | 2022-03-04 14:35:13 +0100 | [diff] [blame] | 93 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | mbedtls_md_free(&ctx->hmac_ctx); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 95 | |
| 96 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | mbedtls_mutex_free(&ctx->mutex); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 98 | #endif |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 99 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx)); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx, |
| 105 | int (*f_rng)(void *, unsigned char *, size_t), |
| 106 | void *p_rng) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 107 | { |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 108 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 109 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 110 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 111 | psa_algorithm_t alg; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | (void) f_rng; |
| 114 | (void) p_rng; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 116 | alg = mbedtls_md_psa_alg_from_type(COOKIE_MD); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | if (alg == 0) { |
| 118 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 119 | } |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | ctx->psa_hmac_alg = PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(alg), |
| 122 | COOKIE_HMAC_LEN); |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_MESSAGE | |
| 125 | PSA_KEY_USAGE_SIGN_MESSAGE); |
| 126 | psa_set_key_algorithm(&attributes, ctx->psa_hmac_alg); |
| 127 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
| 128 | psa_set_key_bits(&attributes, PSA_BYTES_TO_BITS(COOKIE_MD_OUTLEN)); |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | if ((status = psa_generate_key(&attributes, |
| 131 | &ctx->psa_hmac_key)) != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 132 | return PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | d633201 | 2022-03-04 10:26:16 +0100 | [diff] [blame] | 133 | } |
Neil Armstrong | 23d34ce | 2022-03-04 10:32:26 +0100 | [diff] [blame] | 134 | #else |
Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 135 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 136 | unsigned char key[COOKIE_MD_OUTLEN]; |
| 137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) { |
| 139 | return ret; |
| 140 | } |
Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1); |
| 143 | if (ret != 0) { |
| 144 | return ret; |
| 145 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key)); |
| 148 | if (ret != 0) { |
| 149 | return ret; |
| 150 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | mbedtls_platform_zeroize(key, sizeof(key)); |
Neil Armstrong | 2217d6f | 2022-03-04 15:00:22 +0100 | [diff] [blame] | 153 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | return 0; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 158 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 159 | /* |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 160 | * Generate the HMAC part of a cookie |
| 161 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 162 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx, |
| 164 | const unsigned char time[4], |
| 165 | unsigned char **p, unsigned char *end, |
| 166 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 167 | { |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 168 | unsigned char hmac_out[COOKIE_MD_OUTLEN]; |
| 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | if (mbedtls_md_hmac_reset(hmac_ctx) != 0 || |
| 173 | mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 || |
| 174 | mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 || |
| 175 | mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) { |
| 176 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | memcpy(*p, hmac_out, COOKIE_HMAC_LEN); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 180 | *p += COOKIE_HMAC_LEN; |
| 181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | return 0; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 183 | } |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 184 | #endif /* !MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 185 | |
| 186 | /* |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 187 | * Generate cookie for DTLS ClientHello verification |
| 188 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | int mbedtls_ssl_cookie_write(void *p_ctx, |
| 190 | unsigned char **p, unsigned char *end, |
| 191 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 192 | { |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 193 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 194 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 195 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 196 | size_t sign_mac_length = 0; |
| 197 | #endif |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 198 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 200 | unsigned long t; |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | if (ctx == NULL || cli_id == NULL) { |
| 203 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 204 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_LEN); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | t = (unsigned long) mbedtls_time(NULL); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 210 | #else |
| 211 | t = ctx->serial++; |
| 212 | #endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 213 | |
Joe Subbiani | b6511b0 | 2021-07-16 15:02:55 +0100 | [diff] [blame] | 214 | MBEDTLS_PUT_UINT32_BE(t, *p, 0); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 215 | *p += 4; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 216 | |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 217 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key, |
| 219 | ctx->psa_hmac_alg); |
| 220 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 221 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 222 | goto exit; |
| 223 | } |
| 224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | status = psa_mac_update(&operation, *p - 4, 4); |
| 226 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 227 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 228 | goto exit; |
| 229 | } |
| 230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | status = psa_mac_update(&operation, cli_id, cli_id_len); |
| 232 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 233 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 234 | goto exit; |
| 235 | } |
| 236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN, |
| 238 | &sign_mac_length); |
| 239 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 240 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 241 | goto exit; |
| 242 | } |
| 243 | |
| 244 | *p += COOKIE_HMAC_LEN; |
| 245 | |
| 246 | ret = 0; |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 247 | #else |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 248 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 250 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret); |
| 251 | } |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 252 | #endif |
| 253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4, |
| 255 | p, end, cli_id, cli_id_len); |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 256 | |
| 257 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 259 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 260 | MBEDTLS_ERR_THREADING_MUTEX_ERROR); |
| 261 | } |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 262 | #endif |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 263 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 264 | |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 265 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 266 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | status = psa_mac_abort(&operation); |
| 268 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 269 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | } |
Neil Armstrong | 2d5e343 | 2022-03-21 11:39:52 +0100 | [diff] [blame] | 271 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Check a cookie |
| 277 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | int mbedtls_ssl_cookie_check(void *p_ctx, |
| 279 | const unsigned char *cookie, size_t cookie_len, |
| 280 | const unsigned char *cli_id, size_t cli_id_len) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 281 | { |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 282 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 283 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 284 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 285 | #else |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 286 | unsigned char ref_hmac[COOKIE_HMAC_LEN]; |
| 287 | unsigned char *p = ref_hmac; |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 288 | #endif |
| 289 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 291 | unsigned long cur_time, cookie_time; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | if (ctx == NULL || cli_id == NULL) { |
| 294 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 295 | } |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | if (cookie_len != COOKIE_LEN) { |
| 298 | return -1; |
| 299 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 300 | |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 301 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key, |
| 303 | ctx->psa_hmac_alg); |
| 304 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 305 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 306 | goto exit; |
| 307 | } |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | status = psa_mac_update(&operation, cookie, 4); |
| 310 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 311 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 312 | goto exit; |
| 313 | } |
| 314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | status = psa_mac_update(&operation, cli_id, |
| 316 | cli_id_len); |
| 317 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 318 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 319 | goto exit; |
| 320 | } |
| 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | status = psa_mac_verify_finish(&operation, cookie + 4, |
| 323 | COOKIE_HMAC_LEN); |
| 324 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 325 | ret = PSA_TO_MBEDTLS_ERR(status); |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 326 | goto exit; |
| 327 | } |
Neil Armstrong | 79daea2 | 2022-03-21 12:05:51 +0100 | [diff] [blame] | 328 | |
| 329 | ret = 0; |
Neil Armstrong | 7cd0270 | 2022-03-04 15:08:43 +0100 | [diff] [blame] | 330 | #else |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 331 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 333 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret); |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 334 | } |
Manuel Pégourié-Gonnard | 2a84dfd | 2015-05-28 15:48:09 +0200 | [diff] [blame] | 335 | #endif |
| 336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie, |
| 338 | &p, p + sizeof(ref_hmac), |
| 339 | cli_id, cli_id_len) != 0) { |
| 340 | ret = -1; |
| 341 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | #if defined(MBEDTLS_THREADING_C) |
| 344 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 345 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 346 | MBEDTLS_ERR_THREADING_MUTEX_ERROR); |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | if (ret != 0) { |
| 351 | goto exit; |
| 352 | } |
| 353 | |
| 354 | if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) { |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 355 | ret = -1; |
| 356 | goto exit; |
| 357 | } |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 358 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 360 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | cur_time = (unsigned long) mbedtls_time(NULL); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 362 | #else |
| 363 | cur_time = ctx->serial; |
| 364 | #endif |
| 365 | |
Thomas Daubney | f9f0ba8 | 2023-05-23 17:34:33 +0100 | [diff] [blame] | 366 | cookie_time = (unsigned long) MBEDTLS_GET_UINT32_BE(cookie, 0); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame] | 367 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout) { |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 369 | ret = -1; |
| 370 | goto exit; |
| 371 | } |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 372 | |
Gilles Peskine | c2f7b75 | 2021-12-13 12:35:08 +0100 | [diff] [blame] | 373 | exit: |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 374 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | status = psa_mac_abort(&operation); |
| 376 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 377 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | } |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 379 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac)); |
Neil Armstrong | 6d5baf5 | 2022-03-07 14:25:18 +0100 | [diff] [blame] | 381 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | return ret; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 383 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | #endif /* MBEDTLS_SSL_COOKIE_C */ |