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