Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Hanno Becker | f1a3828 | 2020-02-05 16:14:29 +0000 | [diff] [blame] | 2 | * Generic SSL/TLS messaging layer functions |
| 3 | * (record layer + retransmission state machine) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9 | * http://www.ietf.org/rfc/rfc2246.txt |
| 10 | * http://www.ietf.org/rfc/rfc4346.txt |
| 11 | */ |
| 12 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 13 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 14 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 15 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 16 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 17 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 19 | #include "mbedtls/ssl.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 20 | #include "ssl_misc.h" |
Valerio Setti | b4f5076 | 2024-01-17 10:24:52 +0100 | [diff] [blame] | 21 | #include "debug_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 22 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 23 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 24 | #include "mbedtls/version.h" |
Gabor Mezei | 22c9a6f | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 25 | #include "constant_time_internal.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 26 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 27 | |
Gilles Peskine | ebdd405 | 2025-02-17 16:25:24 +0100 | [diff] [blame] | 28 | #include <limits.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 31 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 32 | #include "psa_util_internal.h" |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 33 | #include "psa/crypto.h" |
| 34 | #endif |
| 35 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 36 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 38 | #endif |
| 39 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 40 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 41 | /* Define a local translating function to save code size by not using too many |
| 42 | * arguments in each translating place. */ |
| 43 | static int local_err_translation(psa_status_t status) |
| 44 | { |
| 45 | return psa_status_to_mbedtls(status, psa_to_ssl_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 46 | ARRAY_LENGTH(psa_to_ssl_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 47 | psa_generic_status_to_mbedtls); |
| 48 | } |
| 49 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 50 | #endif |
| 51 | |
Dave Rodgman | 2801f7f | 2023-05-09 11:00:07 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 53 | |
| 54 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 55 | |
| 56 | #if defined(PSA_WANT_ALG_SHA_384) |
| 57 | #define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_384) |
| 58 | #elif defined(PSA_WANT_ALG_SHA_256) |
| 59 | #define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_256) |
| 60 | #else /* See check_config.h */ |
| 61 | #define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_1) |
| 62 | #endif |
| 63 | |
| 64 | MBEDTLS_STATIC_TESTABLE |
| 65 | int mbedtls_ct_hmac(mbedtls_svc_key_id_t key, |
| 66 | psa_algorithm_t mac_alg, |
| 67 | const unsigned char *add_data, |
| 68 | size_t add_data_len, |
| 69 | const unsigned char *data, |
| 70 | size_t data_len_secret, |
| 71 | size_t min_data_len, |
| 72 | size_t max_data_len, |
| 73 | unsigned char *output) |
| 74 | { |
| 75 | /* |
| 76 | * This function breaks the HMAC abstraction and uses psa_hash_clone() |
| 77 | * extension in order to get constant-flow behaviour. |
| 78 | * |
| 79 | * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means |
| 80 | * concatenation, and okey/ikey are the XOR of the key with some fixed bit |
| 81 | * patterns (see RFC 2104, sec. 2). |
| 82 | * |
| 83 | * We'll first compute ikey/okey, then inner_hash = HASH(ikey + msg) by |
| 84 | * hashing up to minlen, then cloning the context, and for each byte up |
| 85 | * to maxlen finishing up the hash computation, keeping only the |
| 86 | * correct result. |
| 87 | * |
| 88 | * Then we only need to compute HASH(okey + inner_hash) and we're done. |
| 89 | */ |
| 90 | psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH(mac_alg); |
| 91 | const size_t block_size = PSA_HASH_BLOCK_LENGTH(hash_alg); |
| 92 | unsigned char key_buf[MAX_HASH_BLOCK_LENGTH]; |
| 93 | const size_t hash_size = PSA_HASH_LENGTH(hash_alg); |
| 94 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
| 95 | size_t hash_length; |
| 96 | |
| 97 | unsigned char aux_out[PSA_HASH_MAX_SIZE]; |
| 98 | psa_hash_operation_t aux_operation = PSA_HASH_OPERATION_INIT; |
| 99 | size_t offset; |
| 100 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 101 | |
| 102 | size_t mac_key_length; |
| 103 | size_t i; |
| 104 | |
| 105 | #define PSA_CHK(func_call) \ |
| 106 | do { \ |
| 107 | status = (func_call); \ |
| 108 | if (status != PSA_SUCCESS) \ |
| 109 | goto cleanup; \ |
| 110 | } while (0) |
| 111 | |
| 112 | /* Export MAC key |
| 113 | * We assume key length is always exactly the output size |
| 114 | * which is never more than the block size, thus we use block_size |
| 115 | * as the key buffer size. |
| 116 | */ |
| 117 | PSA_CHK(psa_export_key(key, key_buf, block_size, &mac_key_length)); |
| 118 | |
| 119 | /* Calculate ikey */ |
| 120 | for (i = 0; i < mac_key_length; i++) { |
| 121 | key_buf[i] = (unsigned char) (key_buf[i] ^ 0x36); |
| 122 | } |
| 123 | for (; i < block_size; ++i) { |
| 124 | key_buf[i] = 0x36; |
| 125 | } |
| 126 | |
| 127 | PSA_CHK(psa_hash_setup(&operation, hash_alg)); |
| 128 | |
| 129 | /* Now compute inner_hash = HASH(ikey + msg) */ |
| 130 | PSA_CHK(psa_hash_update(&operation, key_buf, block_size)); |
| 131 | PSA_CHK(psa_hash_update(&operation, add_data, add_data_len)); |
| 132 | PSA_CHK(psa_hash_update(&operation, data, min_data_len)); |
| 133 | |
| 134 | /* Fill the hash buffer in advance with something that is |
| 135 | * not a valid hash (barring an attack on the hash and |
| 136 | * deliberately-crafted input), in case the caller doesn't |
| 137 | * check the return status properly. */ |
| 138 | memset(output, '!', hash_size); |
| 139 | |
| 140 | /* For each possible length, compute the hash up to that point */ |
| 141 | for (offset = min_data_len; offset <= max_data_len; offset++) { |
| 142 | PSA_CHK(psa_hash_clone(&operation, &aux_operation)); |
| 143 | PSA_CHK(psa_hash_finish(&aux_operation, aux_out, |
| 144 | PSA_HASH_MAX_SIZE, &hash_length)); |
| 145 | /* Keep only the correct inner_hash in the output buffer */ |
Dave Rodgman | 48fb8a3 | 2023-08-10 14:01:51 +0100 | [diff] [blame] | 146 | mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret), |
Dave Rodgman | a81373f | 2023-05-17 12:36:01 +0100 | [diff] [blame] | 147 | output, aux_out, NULL, hash_size); |
Dave Rodgman | 2801f7f | 2023-05-09 11:00:07 +0100 | [diff] [blame] | 148 | |
| 149 | if (offset < max_data_len) { |
| 150 | PSA_CHK(psa_hash_update(&operation, data + offset, 1)); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* Abort current operation to prepare for final operation */ |
| 155 | PSA_CHK(psa_hash_abort(&operation)); |
| 156 | |
| 157 | /* Calculate okey */ |
| 158 | for (i = 0; i < mac_key_length; i++) { |
| 159 | key_buf[i] = (unsigned char) ((key_buf[i] ^ 0x36) ^ 0x5C); |
| 160 | } |
| 161 | for (; i < block_size; ++i) { |
| 162 | key_buf[i] = 0x5C; |
| 163 | } |
| 164 | |
| 165 | /* Now compute HASH(okey + inner_hash) */ |
| 166 | PSA_CHK(psa_hash_setup(&operation, hash_alg)); |
| 167 | PSA_CHK(psa_hash_update(&operation, key_buf, block_size)); |
| 168 | PSA_CHK(psa_hash_update(&operation, output, hash_size)); |
| 169 | PSA_CHK(psa_hash_finish(&operation, output, hash_size, &hash_length)); |
| 170 | |
| 171 | #undef PSA_CHK |
| 172 | |
| 173 | cleanup: |
| 174 | mbedtls_platform_zeroize(key_buf, MAX_HASH_BLOCK_LENGTH); |
| 175 | mbedtls_platform_zeroize(aux_out, PSA_HASH_MAX_SIZE); |
| 176 | |
| 177 | psa_hash_abort(&operation); |
| 178 | psa_hash_abort(&aux_operation); |
| 179 | return PSA_TO_MBEDTLS_ERR(status); |
| 180 | } |
| 181 | |
| 182 | #undef MAX_HASH_BLOCK_LENGTH |
| 183 | |
| 184 | #else |
| 185 | MBEDTLS_STATIC_TESTABLE |
| 186 | int mbedtls_ct_hmac(mbedtls_md_context_t *ctx, |
| 187 | const unsigned char *add_data, |
| 188 | size_t add_data_len, |
| 189 | const unsigned char *data, |
| 190 | size_t data_len_secret, |
| 191 | size_t min_data_len, |
| 192 | size_t max_data_len, |
| 193 | unsigned char *output) |
| 194 | { |
| 195 | /* |
| 196 | * This function breaks the HMAC abstraction and uses the md_clone() |
| 197 | * extension to the MD API in order to get constant-flow behaviour. |
| 198 | * |
| 199 | * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means |
| 200 | * concatenation, and okey/ikey are the XOR of the key with some fixed bit |
| 201 | * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx. |
| 202 | * |
| 203 | * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to |
| 204 | * minlen, then cloning the context, and for each byte up to maxlen |
| 205 | * finishing up the hash computation, keeping only the correct result. |
| 206 | * |
| 207 | * Then we only need to compute HASH(okey + inner_hash) and we're done. |
| 208 | */ |
| 209 | const mbedtls_md_type_t md_alg = mbedtls_md_get_type(ctx->md_info); |
| 210 | /* TLS 1.2 only supports SHA-384, SHA-256, SHA-1, MD-5, |
| 211 | * all of which have the same block size except SHA-384. */ |
| 212 | const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64; |
| 213 | const unsigned char * const ikey = ctx->hmac_ctx; |
| 214 | const unsigned char * const okey = ikey + block_size; |
| 215 | const size_t hash_size = mbedtls_md_get_size(ctx->md_info); |
| 216 | |
| 217 | unsigned char aux_out[MBEDTLS_MD_MAX_SIZE]; |
| 218 | mbedtls_md_context_t aux; |
| 219 | size_t offset; |
| 220 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 221 | |
| 222 | mbedtls_md_init(&aux); |
| 223 | |
| 224 | #define MD_CHK(func_call) \ |
| 225 | do { \ |
| 226 | ret = (func_call); \ |
| 227 | if (ret != 0) \ |
| 228 | goto cleanup; \ |
| 229 | } while (0) |
| 230 | |
| 231 | MD_CHK(mbedtls_md_setup(&aux, ctx->md_info, 0)); |
| 232 | |
| 233 | /* After hmac_start() of hmac_reset(), ikey has already been hashed, |
| 234 | * so we can start directly with the message */ |
| 235 | MD_CHK(mbedtls_md_update(ctx, add_data, add_data_len)); |
| 236 | MD_CHK(mbedtls_md_update(ctx, data, min_data_len)); |
| 237 | |
| 238 | /* Fill the hash buffer in advance with something that is |
| 239 | * not a valid hash (barring an attack on the hash and |
| 240 | * deliberately-crafted input), in case the caller doesn't |
| 241 | * check the return status properly. */ |
| 242 | memset(output, '!', hash_size); |
| 243 | |
| 244 | /* For each possible length, compute the hash up to that point */ |
| 245 | for (offset = min_data_len; offset <= max_data_len; offset++) { |
| 246 | MD_CHK(mbedtls_md_clone(&aux, ctx)); |
| 247 | MD_CHK(mbedtls_md_finish(&aux, aux_out)); |
| 248 | /* Keep only the correct inner_hash in the output buffer */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 249 | mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret), |
Dave Rodgman | a81373f | 2023-05-17 12:36:01 +0100 | [diff] [blame] | 250 | output, aux_out, NULL, hash_size); |
Dave Rodgman | 2801f7f | 2023-05-09 11:00:07 +0100 | [diff] [blame] | 251 | |
| 252 | if (offset < max_data_len) { |
| 253 | MD_CHK(mbedtls_md_update(ctx, data + offset, 1)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* The context needs to finish() before it starts() again */ |
| 258 | MD_CHK(mbedtls_md_finish(ctx, aux_out)); |
| 259 | |
| 260 | /* Now compute HASH(okey + inner_hash) */ |
| 261 | MD_CHK(mbedtls_md_starts(ctx)); |
| 262 | MD_CHK(mbedtls_md_update(ctx, okey, block_size)); |
| 263 | MD_CHK(mbedtls_md_update(ctx, output, hash_size)); |
| 264 | MD_CHK(mbedtls_md_finish(ctx, output)); |
| 265 | |
| 266 | /* Done, get ready for next time */ |
| 267 | MD_CHK(mbedtls_md_hmac_reset(ctx)); |
| 268 | |
| 269 | #undef MD_CHK |
| 270 | |
| 271 | cleanup: |
| 272 | mbedtls_md_free(&aux); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 277 | |
| 278 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 280 | static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 281 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 282 | /* |
| 283 | * Start a timer. |
| 284 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 285 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 287 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | if (ssl->f_set_timer == NULL) { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 289 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | } |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs)); |
| 293 | ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Return -1 is timer is expired, 0 if it isn't. |
| 298 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 300 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | if (ssl->f_get_timer == NULL) { |
| 302 | return 0; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 303 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | if (ssl->f_get_timer(ssl->p_timer) == 2) { |
| 306 | MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired")); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | return 0; |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 311 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 312 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 313 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 315 | unsigned char *buf, |
| 316 | size_t len, |
| 317 | mbedtls_record *rec); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl, |
| 320 | unsigned char *buf, |
| 321 | size_t buflen) |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 322 | { |
| 323 | int ret = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record")); |
| 325 | MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 326 | |
| 327 | /* We don't support record checking in TLS because |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 328 | * there doesn't seem to be a usecase for it. |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 329 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) { |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 331 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 332 | goto exit; |
| 333 | } |
| 334 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | else { |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 336 | mbedtls_record rec; |
| 337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | ret = ssl_parse_record_header(ssl, buf, buflen, &rec); |
| 339 | if (ret != 0) { |
| 340 | MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 341 | goto exit; |
| 342 | } |
| 343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | if (ssl->transform_in != NULL) { |
| 345 | ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec); |
| 346 | if (ret != 0) { |
| 347 | MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 348 | goto exit; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 353 | |
| 354 | exit: |
| 355 | /* On success, we have decrypted the buffer in-place, so make |
| 356 | * sure we don't leak any plaintext data. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | mbedtls_platform_zeroize(buf, buflen); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 358 | |
| 359 | /* For the purpose of this API, treat messages with unexpected CID |
| 360 | * as well as such from future epochs as unexpected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 362 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 363 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 364 | } |
| 365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record")); |
| 367 | return ret; |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 370 | #define SSL_DONT_FORCE_FLUSH 0 |
| 371 | #define SSL_FORCE_FLUSH 1 |
| 372 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 373 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 374 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 375 | /* Forward declarations for functions related to message buffering. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, |
| 377 | uint8_t slot); |
| 378 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 379 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 381 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 383 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | static int ssl_buffer_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 385 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 387 | mbedtls_record const *rec); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 388 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 392 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 394 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 395 | size_t out_buf_len = ssl->out_buf_len; |
| 396 | #else |
| 397 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 398 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | if (mtu != 0 && mtu < out_buf_len) { |
| 401 | return mtu; |
| 402 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | return out_buf_len; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 407 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 409 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 410 | size_t const bytes_written = ssl->out_left; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | size_t const mtu = ssl_get_maximum_datagram_size(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 412 | |
| 413 | /* Double-check that the write-index hasn't gone |
| 414 | * past what we can transmit in a single datagram. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | if (bytes_written > mtu) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 416 | /* Should never happen... */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 418 | } |
| 419 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | return (int) (mtu - bytes_written); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 423 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 425 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 426 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 427 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 428 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 429 | |
| 430 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (max_len > mfl) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 434 | max_len = mfl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | } |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 436 | |
| 437 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 438 | * only limits the maximum record payload size, so in theory |
| 439 | * we would be allowed to pack multiple records of payload size |
| 440 | * MFL into a single datagram. However, this would mean that there's |
| 441 | * no way to explicitly communicate MTU restrictions to the peer. |
| 442 | * |
| 443 | * The following reduction of max_len makes sure that we never |
| 444 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 445 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | if (max_len <= ssl->out_left) { |
| 447 | return 0; |
| 448 | } |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 449 | |
| 450 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 451 | #endif |
| 452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 454 | if (ret < 0) { |
| 455 | return ret; |
| 456 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 457 | remaining = (size_t) ret; |
| 458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | ret = mbedtls_ssl_get_record_expansion(ssl); |
| 460 | if (ret < 0) { |
| 461 | return ret; |
| 462 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 463 | expansion = (size_t) ret; |
| 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | if (remaining <= expansion) { |
| 466 | return 0; |
| 467 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 468 | |
| 469 | remaining -= expansion; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | if (remaining >= max_len) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 471 | remaining = max_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | return (int) remaining; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 475 | } |
| 476 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 477 | /* |
| 478 | * Double the retransmit timeout value, within the allowed range, |
| 479 | * returning -1 if the maximum value has already been reached. |
| 480 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 481 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 483 | { |
| 484 | uint32_t new_timeout; |
| 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) { |
| 487 | return -1; |
| 488 | } |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 489 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 490 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 491 | * in the following way: after the initial transmission and a first |
| 492 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 493 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 494 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 495 | * on most non-IP stacks too. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 497 | ssl->handshake->mtu = 508; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu)); |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 499 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 500 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 501 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 502 | |
| 503 | /* Avoid arithmetic overflow and range overflow */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (new_timeout < ssl->handshake->retransmit_timeout || |
| 505 | new_timeout > ssl->conf->hs_timeout_max) { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 506 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | ssl->handshake->retransmit_timeout = new_timeout; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs", |
| 511 | (unsigned long) ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | return 0; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 517 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 518 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs", |
| 520 | (unsigned long) ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 521 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 523 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 524 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 525 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 526 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 527 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 528 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | static size_t ssl_compute_padding_length(size_t len, |
| 531 | size_t granularity) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 532 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | return (granularity - (len + 1) % granularity) % granularity; |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 536 | /* This functions transforms a (D)TLS plaintext fragment and a record content |
| 537 | * type into an instance of the (D)TLSInnerPlaintext structure. This is used |
| 538 | * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect |
| 539 | * a record's content type. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 540 | * |
| 541 | * struct { |
| 542 | * opaque content[DTLSPlaintext.length]; |
| 543 | * ContentType real_type; |
| 544 | * uint8 zeros[length_of_padding]; |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 545 | * } (D)TLSInnerPlaintext; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 546 | * |
| 547 | * Input: |
| 548 | * - `content`: The beginning of the buffer holding the |
| 549 | * plaintext to be wrapped. |
| 550 | * - `*content_size`: The length of the plaintext in Bytes. |
| 551 | * - `max_len`: The number of Bytes available starting from |
| 552 | * `content`. This must be `>= *content_size`. |
| 553 | * - `rec_type`: The desired record content type. |
| 554 | * |
| 555 | * Output: |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 556 | * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure. |
| 557 | * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 558 | * |
| 559 | * Returns: |
| 560 | * - `0` on success. |
| 561 | * - A negative error code if `max_len` didn't offer enough space |
| 562 | * for the expansion. |
| 563 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 564 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | static int ssl_build_inner_plaintext(unsigned char *content, |
| 566 | size_t *content_size, |
| 567 | size_t remaining, |
| 568 | uint8_t rec_type, |
| 569 | size_t pad) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 570 | { |
| 571 | size_t len = *content_size; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 572 | |
| 573 | /* Write real content type */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | if (remaining == 0) { |
| 575 | return -1; |
| 576 | } |
| 577 | content[len] = rec_type; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 578 | len++; |
| 579 | remaining--; |
| 580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | if (remaining < pad) { |
| 582 | return -1; |
| 583 | } |
| 584 | memset(content + len, 0, pad); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 585 | len += pad; |
| 586 | remaining -= pad; |
| 587 | |
| 588 | *content_size = len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 592 | /* This function parses a (D)TLSInnerPlaintext structure. |
| 593 | * See ssl_build_inner_plaintext() for details. */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 594 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | static int ssl_parse_inner_plaintext(unsigned char const *content, |
| 596 | size_t *content_size, |
| 597 | uint8_t *rec_type) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 598 | { |
| 599 | size_t remaining = *content_size; |
| 600 | |
| 601 | /* Determine length of padding by skipping zeroes from the back. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | do { |
| 603 | if (remaining == 0) { |
| 604 | return -1; |
| 605 | } |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 606 | remaining--; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | } while (content[remaining] == 0); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 608 | |
| 609 | *content_size = remaining; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | *rec_type = content[remaining]; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 613 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 614 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 615 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 616 | /* The size of the `add_data` structure depends on various |
| 617 | * factors, namely |
| 618 | * |
| 619 | * 1) CID functionality disabled |
| 620 | * |
| 621 | * additional_data = |
| 622 | * 8: seq_num + |
| 623 | * 1: type + |
| 624 | * 2: version + |
| 625 | * 2: length of inner plaintext + |
| 626 | * |
| 627 | * size = 13 bytes |
| 628 | * |
| 629 | * 2) CID functionality based on RFC 9146 enabled |
| 630 | * |
| 631 | * size = 8 + 1 + 1 + 1 + 2 + 2 + 6 + 2 + CID-length |
| 632 | * = 23 + CID-length |
| 633 | * |
| 634 | * 3) CID functionality based on legacy CID version |
| 635 | according to draft-ietf-tls-dtls-connection-id-05 |
| 636 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05 |
| 637 | * |
| 638 | * size = 13 + 1 + CID-length |
| 639 | * |
| 640 | * More information about the CID usage: |
| 641 | * |
| 642 | * Per Section 5.3 of draft-ietf-tls-dtls-connection-id-05 the |
| 643 | * size of the additional data structure is calculated as: |
| 644 | * |
| 645 | * additional_data = |
| 646 | * 8: seq_num + |
| 647 | * 1: tls12_cid + |
| 648 | * 2: DTLSCipherText.version + |
| 649 | * n: cid + |
| 650 | * 1: cid_length + |
| 651 | * 2: length_of_DTLSInnerPlaintext |
| 652 | * |
| 653 | * Per RFC 9146 the size of the add_data structure is calculated as: |
| 654 | * |
| 655 | * additional_data = |
| 656 | * 8: seq_num_placeholder + |
| 657 | * 1: tls12_cid + |
| 658 | * 1: cid_length + |
| 659 | * 1: tls12_cid + |
| 660 | * 2: DTLSCiphertext.version + |
| 661 | * 2: epoch + |
| 662 | * 6: sequence_number + |
| 663 | * n: cid + |
| 664 | * 2: length_of_DTLSInnerPlaintext |
| 665 | * |
| 666 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | static void ssl_extract_add_data_from_record(unsigned char *add_data, |
| 668 | size_t *add_data_len, |
| 669 | mbedtls_record *rec, |
| 670 | mbedtls_ssl_protocol_version |
| 671 | tls_version, |
| 672 | size_t taglen) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 673 | { |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 674 | /* Several types of ciphers have been defined for use with TLS and DTLS, |
| 675 | * and the MAC calculations for those ciphers differ slightly. Further |
| 676 | * variants were added when the CID functionality was added with RFC 9146. |
| 677 | * This implementations also considers the use of a legacy version of the |
| 678 | * CID specification published in draft-ietf-tls-dtls-connection-id-05, |
| 679 | * which is used in deployments. |
| 680 | * |
| 681 | * We will distinguish between the non-CID and the CID cases below. |
| 682 | * |
| 683 | * --- Non-CID cases --- |
| 684 | * |
| 685 | * Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 686 | * |
| 687 | * additional_data = seq_num + TLSCompressed.type + |
| 688 | * TLSCompressed.version + TLSCompressed.length; |
| 689 | * |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 690 | * For TLS 1.3, the record sequence number is dropped from the AAD |
| 691 | * and encoded within the nonce of the AEAD operation instead. |
Hanno Becker | 79e2d1b | 2021-03-22 11:42:19 +0000 | [diff] [blame] | 692 | * Moreover, the additional data involves the length of the TLS |
| 693 | * ciphertext, not the TLS plaintext as in earlier versions. |
| 694 | * Quoting RFC 8446 (TLS 1.3): |
| 695 | * |
| 696 | * additional_data = TLSCiphertext.opaque_type || |
| 697 | * TLSCiphertext.legacy_record_version || |
| 698 | * TLSCiphertext.length |
| 699 | * |
| 700 | * We pass the tag length to this function in order to compute the |
| 701 | * ciphertext length from the inner plaintext length rec->data_len via |
| 702 | * |
| 703 | * TLSCiphertext.length = TLSInnerPlaintext.length + taglen. |
| 704 | * |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 705 | * --- CID cases --- |
| 706 | * |
| 707 | * RFC 9146 uses a common pattern when constructing the data |
| 708 | * passed into a MAC / AEAD cipher. |
| 709 | * |
| 710 | * Data concatenation for MACs used with block ciphers with |
| 711 | * Encrypt-then-MAC Processing (with CID): |
| 712 | * |
| 713 | * data = seq_num_placeholder + |
| 714 | * tls12_cid + |
| 715 | * cid_length + |
| 716 | * tls12_cid + |
| 717 | * DTLSCiphertext.version + |
| 718 | * epoch + |
| 719 | * sequence_number + |
| 720 | * cid + |
| 721 | * DTLSCiphertext.length + |
| 722 | * IV + |
| 723 | * ENC(content + padding + padding_length) |
| 724 | * |
| 725 | * Data concatenation for MACs used with block ciphers (with CID): |
| 726 | * |
| 727 | * data = seq_num_placeholder + |
| 728 | * tls12_cid + |
| 729 | * cid_length + |
| 730 | * tls12_cid + |
| 731 | * DTLSCiphertext.version + |
| 732 | * epoch + |
| 733 | * sequence_number + |
| 734 | * cid + |
| 735 | * length_of_DTLSInnerPlaintext + |
| 736 | * DTLSInnerPlaintext.content + |
| 737 | * DTLSInnerPlaintext.real_type + |
| 738 | * DTLSInnerPlaintext.zeros |
| 739 | * |
| 740 | * AEAD ciphers use the following additional data calculation (with CIDs): |
| 741 | * |
| 742 | * additional_data = seq_num_placeholder + |
| 743 | * tls12_cid + |
| 744 | * cid_length + |
| 745 | * tls12_cid + |
| 746 | * DTLSCiphertext.version + |
| 747 | * epoch + |
| 748 | * sequence_number + |
| 749 | * cid + |
| 750 | * length_of_DTLSInnerPlaintext |
| 751 | * |
| 752 | * Section 5.3 of draft-ietf-tls-dtls-connection-id-05 (for legacy CID use) |
| 753 | * defines the additional data calculation as follows: |
| 754 | * |
| 755 | * additional_data = seq_num + |
| 756 | * tls12_cid + |
| 757 | * DTLSCipherText.version + |
| 758 | * cid + |
| 759 | * cid_length + |
| 760 | * length_of_DTLSInnerPlaintext |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 762 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 763 | unsigned char *cur = add_data; |
Hanno Becker | 79e2d1b | 2021-03-22 11:42:19 +0000 | [diff] [blame] | 764 | size_t ad_len_field = rec->data_len; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 765 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 766 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
| 767 | MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0 |
| 768 | const unsigned char seq_num_placeholder[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 769 | #endif |
| 770 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 771 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | 79e2d1b | 2021-03-22 11:42:19 +0000 | [diff] [blame] | 773 | /* In TLS 1.3, the AAD contains the length of the TLSCiphertext, |
| 774 | * which differs from the length of the TLSInnerPlaintext |
| 775 | * by the length of the authentication tag. */ |
| 776 | ad_len_field += taglen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | } else |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 778 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 779 | { |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 780 | ((void) tls_version); |
Hanno Becker | 79e2d1b | 2021-03-22 11:42:19 +0000 | [diff] [blame] | 781 | ((void) taglen); |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 782 | |
Manuel Pégourié-Gonnard | 6133684 | 2022-11-25 11:12:38 +0100 | [diff] [blame] | 783 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 784 | MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0 |
| 785 | if (rec->cid_len != 0) { |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 786 | // seq_num_placeholder |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | memcpy(cur, seq_num_placeholder, sizeof(seq_num_placeholder)); |
| 788 | cur += sizeof(seq_num_placeholder); |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 789 | |
| 790 | // tls12_cid type |
| 791 | *cur = rec->type; |
| 792 | cur++; |
| 793 | |
| 794 | // cid_length |
| 795 | *cur = rec->cid_len; |
| 796 | cur++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | } else |
Manuel Pégourié-Gonnard | 6133684 | 2022-11-25 11:12:38 +0100 | [diff] [blame] | 798 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 799 | { |
| 800 | // epoch + sequence number |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | memcpy(cur, rec->ctr, sizeof(rec->ctr)); |
| 802 | cur += sizeof(rec->ctr); |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 803 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 806 | // type |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 807 | *cur = rec->type; |
| 808 | cur++; |
| 809 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 810 | // version |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | memcpy(cur, rec->ver, sizeof(rec->ver)); |
| 812 | cur += sizeof(rec->ver); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 813 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 814 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
| 815 | MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 1 |
| 816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | if (rec->cid_len != 0) { |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 818 | // CID |
| 819 | memcpy(cur, rec->cid, rec->cid_len); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 820 | cur += rec->cid_len; |
| 821 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 822 | // cid_length |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 823 | *cur = rec->cid_len; |
| 824 | cur++; |
| 825 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 826 | // length of inner plaintext |
| 827 | MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0); |
| 828 | cur += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | } else |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 830 | #elif defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \ |
| 831 | MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0 |
| 832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | if (rec->cid_len != 0) { |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 834 | // epoch + sequence number |
| 835 | memcpy(cur, rec->ctr, sizeof(rec->ctr)); |
| 836 | cur += sizeof(rec->ctr); |
| 837 | |
| 838 | // CID |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | memcpy(cur, rec->cid, rec->cid_len); |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 840 | cur += rec->cid_len; |
| 841 | |
| 842 | // length of inner plaintext |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 843 | MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 844 | cur += 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | } else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 846 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 847 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 849 | cur += 2; |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 850 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 851 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 852 | *add_data_len = (size_t) (cur - add_data); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 855 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 856 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 857 | static int ssl_transform_aead_dynamic_iv_is_explicit( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | mbedtls_ssl_transform const *transform) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 859 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 860 | return transform->ivlen != transform->fixed_ivlen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 863 | /* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV ) |
| 864 | * |
| 865 | * Concretely, this occurs in two variants: |
| 866 | * |
| 867 | * a) Fixed and dynamic IV lengths add up to total IV length, giving |
| 868 | * IV = fixed_iv || dynamic_iv |
| 869 | * |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 870 | * This variant is used in TLS 1.2 when used with GCM or CCM. |
| 871 | * |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 872 | * b) Fixed IV lengths matches total IV length, giving |
| 873 | * IV = fixed_iv XOR ( 0 || dynamic_iv ) |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 874 | * |
| 875 | * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly. |
| 876 | * |
| 877 | * See also the documentation of mbedtls_ssl_transform. |
Hanno Becker | f486e28 | 2020-06-04 13:33:08 +0100 | [diff] [blame] | 878 | * |
| 879 | * This function has the precondition that |
| 880 | * |
| 881 | * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len ) |
| 882 | * |
| 883 | * which has to be ensured by the caller. If this precondition |
| 884 | * violated, the behavior of this function is undefined. |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 885 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | static void ssl_build_record_nonce(unsigned char *dst_iv, |
| 887 | size_t dst_iv_len, |
| 888 | unsigned char const *fixed_iv, |
| 889 | size_t fixed_iv_len, |
| 890 | unsigned char const *dynamic_iv, |
| 891 | size_t dynamic_iv_len) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 892 | { |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 893 | /* Start with Fixed IV || 0 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | memset(dst_iv, 0, dst_iv_len); |
| 895 | memcpy(dst_iv, fixed_iv, fixed_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 896 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 897 | dst_iv += dst_iv_len - dynamic_iv_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 898 | mbedtls_xor(dst_iv, dst_iv, dynamic_iv, dynamic_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 899 | } |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 900 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 902 | int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl, |
| 903 | mbedtls_ssl_transform *transform, |
| 904 | mbedtls_record *rec, |
| 905 | int (*f_rng)(void *, unsigned char *, size_t), |
| 906 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 907 | { |
Neil Armstrong | 136f840 | 2022-03-30 10:58:01 +0200 | [diff] [blame] | 908 | mbedtls_ssl_mode_t ssl_mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 909 | int auth_done = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 910 | unsigned char *data; |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 911 | /* For an explanation of the additional data length see |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 912 | * the description of ssl_extract_add_data_from_record(). |
| 913 | */ |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 914 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 915 | unsigned char add_data[23 + MBEDTLS_SSL_CID_OUT_LEN_MAX]; |
| 916 | #else |
| 917 | unsigned char add_data[13]; |
| 918 | #endif |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 919 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 920 | size_t post_avail; |
| 921 | |
| 922 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 923 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 924 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 925 | ((void) ssl); |
| 926 | #endif |
| 927 | |
| 928 | /* The PRNG is used for dynamic IV generation that's used |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 929 | * for CBC transformations in TLS 1.2. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | #if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \ |
| 931 | defined(MBEDTLS_SSL_PROTO_TLS1_2)) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 932 | ((void) f_rng); |
| 933 | ((void) p_rng); |
| 934 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 938 | if (transform == NULL) { |
| 939 | MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf")); |
| 940 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 941 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | if (rec == NULL |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 943 | || rec->buf == NULL |
| 944 | || rec->buf_len < rec->data_offset |
| 945 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 946 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 947 | || rec->cid_len != 0 |
| 948 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | ) { |
| 950 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf")); |
| 951 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 952 | } |
| 953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | ssl_mode = mbedtls_ssl_get_mode_from_transform(transform); |
Neil Armstrong | 136f840 | 2022-03-30 10:58:01 +0200 | [diff] [blame] | 955 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 956 | data = rec->buf + rec->data_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
| 958 | MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload", |
| 959 | data, rec->data_len); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 962 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET |
| 963 | " too large, maximum %" MBEDTLS_PRINTF_SIZET, |
| 964 | rec->data_len, |
| 965 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 966 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 967 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 968 | |
Hanno Becker | 9231340 | 2020-05-20 13:58:58 +0100 | [diff] [blame] | 969 | /* The following two code paths implement the (D)TLSInnerPlaintext |
| 970 | * structure present in TLS 1.3 and DTLS 1.2 + CID. |
| 971 | * |
| 972 | * See ssl_build_inner_plaintext() for more information. |
| 973 | * |
| 974 | * Note that this changes `rec->data_len`, and hence |
| 975 | * `post_avail` needs to be recalculated afterwards. |
| 976 | * |
| 977 | * Note also that the two code paths cannot occur simultaneously |
| 978 | * since they apply to different versions of the protocol. There |
| 979 | * is hence no risk of double-addition of the inner plaintext. |
| 980 | */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 981 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 982 | if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 983 | size_t padding = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | ssl_compute_padding_length(rec->data_len, |
| 985 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY); |
| 986 | if (ssl_build_inner_plaintext(data, |
| 987 | &rec->data_len, |
| 988 | post_avail, |
| 989 | rec->type, |
| 990 | padding) != 0) { |
| 991 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 995 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 996 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 997 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 998 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 999 | /* |
| 1000 | * Add CID information |
| 1001 | */ |
| 1002 | rec->cid_len = transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | memcpy(rec->cid, transform->out_cid, transform->out_cid_len); |
| 1004 | MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | if (rec->cid_len != 0) { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 1007 | size_t padding = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1008 | ssl_compute_padding_length(rec->data_len, |
| 1009 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1010 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1011 | * Wrap plaintext into DTLSInnerPlaintext structure. |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 1012 | * See ssl_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1013 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 1014 | * Note that this changes `rec->data_len`, and hence |
| 1015 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1016 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1017 | if (ssl_build_inner_plaintext(data, |
| 1018 | &rec->data_len, |
| 1019 | post_avail, |
| 1020 | rec->type, |
| 1021 | padding) != 0) { |
| 1022 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 1026 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1027 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1028 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1030 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1031 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1032 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1033 | */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1034 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM || |
| 1036 | ssl_mode == MBEDTLS_SSL_MODE_CBC) { |
| 1037 | if (post_avail < transform->maclen) { |
| 1038 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1039 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1040 | } |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1041 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1042 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1043 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1044 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1045 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1046 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 1047 | size_t sign_mac_length = 0; |
| 1048 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1050 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1051 | transform->tls_version, |
| 1052 | transform->taglen); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1053 | |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1054 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1055 | status = psa_mac_sign_setup(&operation, transform->psa_mac_enc, |
| 1056 | transform->psa_mac_alg); |
| 1057 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1058 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1059 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | status = psa_mac_update(&operation, add_data, add_data_len); |
| 1062 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1063 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1064 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1065 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1066 | status = psa_mac_update(&operation, data, rec->data_len); |
| 1067 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1068 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1069 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1071 | status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD, |
| 1072 | &sign_mac_length); |
| 1073 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1074 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1076 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data, |
| 1078 | add_data_len); |
| 1079 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1080 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | } |
| 1082 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len); |
| 1083 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1084 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | } |
| 1086 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 1087 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1088 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1089 | } |
| 1090 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
| 1091 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1092 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1094 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1096 | memcpy(data + rec->data_len, mac, transform->maclen); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1097 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1099 | MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len, |
| 1100 | transform->maclen); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1101 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1102 | rec->data_len += transform->maclen; |
| 1103 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1104 | auth_done++; |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1106 | hmac_failed_etm_disabled: |
| 1107 | mbedtls_platform_zeroize(mac, transform->maclen); |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1108 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1109 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1110 | status = psa_mac_abort(&operation); |
| 1111 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1112 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | } |
Neil Armstrong | 4313f55 | 2022-03-02 15:14:07 +0100 | [diff] [blame] | 1114 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1115 | if (ret != 0) { |
| 1116 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret); |
| 1117 | return ret; |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1118 | } |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1119 | } |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1120 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1121 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1122 | /* |
| 1123 | * Encrypt |
| 1124 | */ |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 1125 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1126 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
| 1127 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 1128 | "including %d bytes of padding", |
| 1129 | rec->data_len, 0)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1130 | |
Przemyslaw Stekiel | c8a06fe | 2022-02-07 10:52:47 +0100 | [diff] [blame] | 1131 | /* The only supported stream cipher is "NULL", |
| 1132 | * so there's nothing to do here.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | } else |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 1134 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1135 | |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 1136 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1138 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1139 | unsigned char *dynamic_iv; |
| 1140 | size_t dynamic_iv_len; |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1141 | int dynamic_iv_is_explicit = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | ssl_transform_aead_dynamic_iv_is_explicit(transform); |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1143 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1144 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1145 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1146 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1147 | |
Hanno Becker | bd5ed1d | 2020-05-21 15:26:39 +0100 | [diff] [blame] | 1148 | /* Check that there's space for the authentication tag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | if (post_avail < transform->taglen) { |
| 1150 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1151 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1152 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1153 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1154 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1155 | * Build nonce for AEAD encryption. |
| 1156 | * |
| 1157 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 1158 | * part of the IV is prepended to the ciphertext and |
| 1159 | * can be chosen freely - in particular, it need not |
| 1160 | * agree with the record sequence number. |
| 1161 | * However, since ChaChaPoly as well as all AEAD modes |
| 1162 | * in TLS 1.3 use the record sequence number as the |
| 1163 | * dynamic part of the nonce, we uniformly use the |
| 1164 | * record sequence number here in all cases. |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1165 | */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1166 | dynamic_iv = rec->ctr; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | dynamic_iv_len = sizeof(rec->ctr); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1169 | ssl_build_record_nonce(iv, sizeof(iv), |
| 1170 | transform->iv_enc, |
| 1171 | transform->fixed_ivlen, |
| 1172 | dynamic_iv, |
| 1173 | dynamic_iv_len); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1174 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 1175 | /* |
| 1176 | * Build additional data for AEAD encryption. |
| 1177 | * This depends on the TLS version. |
| 1178 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1180 | transform->tls_version, |
| 1181 | transform->taglen); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 1182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1183 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)", |
| 1184 | iv, transform->ivlen); |
| 1185 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)", |
| 1186 | dynamic_iv, |
| 1187 | dynamic_iv_is_explicit ? dynamic_iv_len : 0); |
| 1188 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", |
| 1189 | add_data, add_data_len); |
| 1190 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 1191 | "including 0 bytes of padding", |
| 1192 | rec->data_len)); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1193 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1194 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1195 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1196 | */ |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1197 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1198 | status = psa_aead_encrypt(transform->psa_key_enc, |
| 1199 | transform->psa_alg, |
| 1200 | iv, transform->ivlen, |
| 1201 | add_data, add_data_len, |
| 1202 | data, rec->data_len, |
| 1203 | data, rec->buf_len - (data - rec->buf), |
| 1204 | &rec->data_len); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1207 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret); |
| 1209 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1210 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1211 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc, |
| 1213 | iv, transform->ivlen, |
| 1214 | add_data, add_data_len, |
| 1215 | data, rec->data_len, /* src */ |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 1216 | data, rec->buf_len - (size_t) (data - rec->buf), /* dst */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1217 | &rec->data_len, |
| 1218 | transform->taglen)) != 0) { |
| 1219 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt_ext", ret); |
| 1220 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1221 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1222 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag", |
| 1225 | data + rec->data_len - transform->taglen, |
| 1226 | transform->taglen); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1227 | /* Account for authentication tag. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1228 | post_avail -= transform->taglen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1229 | |
| 1230 | /* |
| 1231 | * Prefix record content with dynamic IV in case it is explicit. |
| 1232 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | if (dynamic_iv_is_explicit != 0) { |
| 1234 | if (rec->data_offset < dynamic_iv_len) { |
| 1235 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1236 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1239 | memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1240 | rec->data_offset -= dynamic_iv_len; |
| 1241 | rec->data_len += dynamic_iv_len; |
| 1242 | } |
| 1243 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1244 | auth_done++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1245 | } else |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 1246 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1247 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1248 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 1249 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1250 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1251 | size_t padlen, i; |
| 1252 | size_t olen; |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1253 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1254 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1255 | size_t part_len; |
| 1256 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
| 1257 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1258 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1259 | /* Currently we're always using minimal padding |
| 1260 | * (up to 255 bytes would be allowed). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1261 | padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen; |
| 1262 | if (padlen == transform->ivlen) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1263 | padlen = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | /* Check there's enough space in the buffer for the padding. */ |
| 1267 | if (post_avail < padlen + 1) { |
| 1268 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1269 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 1270 | } |
| 1271 | |
| 1272 | for (i = 0; i <= padlen; i++) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1273 | data[rec->data_len + i] = (unsigned char) padlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1275 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1276 | rec->data_len += padlen + 1; |
| 1277 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1278 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1279 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1280 | /* |
TRodziewicz | 2d8800e | 2021-05-13 19:14:19 +0200 | [diff] [blame] | 1281 | * Prepend per-record IV for block cipher in TLS v1.2 as per |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1282 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1283 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1284 | if (f_rng == NULL) { |
| 1285 | MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine")); |
| 1286 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1287 | } |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | if (rec->data_offset < transform->ivlen) { |
| 1290 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1291 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Generate IV |
| 1296 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | ret = f_rng(p_rng, transform->iv_enc, transform->ivlen); |
| 1298 | if (ret != 0) { |
| 1299 | return ret; |
| 1300 | } |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | memcpy(data - transform->ivlen, transform->iv_enc, transform->ivlen); |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1303 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 1306 | "including %" |
| 1307 | MBEDTLS_PRINTF_SIZET |
| 1308 | " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding", |
| 1309 | rec->data_len, transform->ivlen, |
| 1310 | padlen + 1)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1311 | |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1312 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1313 | status = psa_cipher_encrypt_setup(&cipher_op, |
| 1314 | transform->psa_key_enc, transform->psa_alg); |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1317 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1318 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_encrypt_setup", ret); |
| 1319 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1320 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1322 | status = psa_cipher_set_iv(&cipher_op, transform->iv_enc, transform->ivlen); |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1324 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1325 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret); |
| 1327 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1328 | |
| 1329 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | status = psa_cipher_update(&cipher_op, |
| 1332 | data, rec->data_len, |
| 1333 | data, rec->data_len, &olen); |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1336 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret); |
| 1338 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1339 | |
| 1340 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1342 | status = psa_cipher_finish(&cipher_op, |
| 1343 | data + olen, rec->data_len - olen, |
| 1344 | &part_len); |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1346 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1347 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1348 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret); |
| 1349 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1350 | |
| 1351 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1352 | |
| 1353 | olen += part_len; |
| 1354 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1355 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 1356 | transform->iv_enc, |
| 1357 | transform->ivlen, |
| 1358 | data, rec->data_len, |
| 1359 | data, &olen)) != 0) { |
| 1360 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1361 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1362 | } |
Przemyslaw Stekiel | b37fae1 | 2022-01-13 14:28:44 +0100 | [diff] [blame] | 1363 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | if (rec->data_len != olen) { |
| 1366 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1367 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1368 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1369 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1370 | data -= transform->ivlen; |
| 1371 | rec->data_offset -= transform->ivlen; |
| 1372 | rec->data_len += transform->ivlen; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | if (auth_done == 0) { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1376 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1377 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1378 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1379 | size_t sign_mac_length = 0; |
| 1380 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1381 | |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 1382 | /* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length)) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1383 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1385 | if (post_avail < transform->maclen) { |
| 1386 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1387 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1388 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1390 | ssl_extract_add_data_from_record(add_data, &add_data_len, |
| 1391 | rec, transform->tls_version, |
| 1392 | transform->taglen); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 1393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
| 1395 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, |
| 1396 | add_data_len); |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1397 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1398 | status = psa_mac_sign_setup(&operation, transform->psa_mac_enc, |
| 1399 | transform->psa_mac_alg); |
| 1400 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1401 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1404 | status = psa_mac_update(&operation, add_data, add_data_len); |
| 1405 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1406 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1407 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1409 | status = psa_mac_update(&operation, data, rec->data_len); |
| 1410 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1411 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1412 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1414 | status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD, |
| 1415 | &sign_mac_length); |
| 1416 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1417 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1418 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1419 | #else |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1421 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data, |
| 1422 | add_data_len); |
| 1423 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1424 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | } |
| 1426 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 1427 | data, rec->data_len); |
| 1428 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1429 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1430 | } |
| 1431 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 1432 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1433 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | } |
| 1435 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
| 1436 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1437 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1438 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1439 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1440 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1441 | memcpy(data + rec->data_len, mac, transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1442 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1443 | rec->data_len += transform->maclen; |
| 1444 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1445 | auth_done++; |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1447 | hmac_failed_etm_enabled: |
| 1448 | mbedtls_platform_zeroize(mac, transform->maclen); |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1449 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1450 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1451 | status = psa_mac_abort(&operation); |
| 1452 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1453 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | } |
Neil Armstrong | 4313f55 | 2022-03-02 15:14:07 +0100 | [diff] [blame] | 1455 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1456 | if (ret != 0) { |
| 1457 | MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret); |
| 1458 | return ret; |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1459 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1460 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | } else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1463 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1464 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1465 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1466 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1467 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1468 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1469 | /* Make extra sure authentication was performed, exactly once */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | if (auth_done != 1) { |
| 1471 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1472 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1475 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1480 | int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl, |
| 1481 | mbedtls_ssl_transform *transform, |
| 1482 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1483 | { |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 1484 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) || defined(MBEDTLS_SSL_HAVE_AEAD) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1485 | size_t olen; |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 1486 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC || MBEDTLS_SSL_HAVE_AEAD */ |
Neil Armstrong | 136f840 | 2022-03-30 10:58:01 +0200 | [diff] [blame] | 1487 | mbedtls_ssl_mode_t ssl_mode; |
Przemyslaw Stekiel | b97556e | 2022-02-01 14:52:19 +0100 | [diff] [blame] | 1488 | int ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1489 | |
Przemyslaw Stekiel | b97556e | 2022-02-01 14:52:19 +0100 | [diff] [blame] | 1490 | int auth_done = 0; |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1491 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 1492 | size_t padlen = 0; |
| 1493 | mbedtls_ct_condition_t correct = MBEDTLS_CT_TRUE; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1494 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1495 | unsigned char *data; |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 1496 | /* For an explanation of the additional data length see |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | * the description of ssl_extract_add_data_from_record(). |
| 1498 | */ |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 1499 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1500 | unsigned char add_data[23 + MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 1501 | #else |
| 1502 | unsigned char add_data[13]; |
| 1503 | #endif |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1504 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1505 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1506 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1507 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1508 | ((void) ssl); |
| 1509 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1511 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf")); |
| 1512 | if (rec == NULL || |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1513 | rec->buf == NULL || |
| 1514 | rec->buf_len < rec->data_offset || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1515 | rec->buf_len - rec->data_offset < rec->data_len) { |
| 1516 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf")); |
| 1517 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1520 | data = rec->buf + rec->data_offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | ssl_mode = mbedtls_ssl_get_mode_from_transform(transform); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1522 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1523 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1524 | /* |
| 1525 | * Match record's CID with incoming CID. |
| 1526 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1527 | if (rec->cid_len != transform->in_cid_len || |
| 1528 | memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) { |
| 1529 | return MBEDTLS_ERR_SSL_UNEXPECTED_CID; |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 1530 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1531 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1532 | |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 1533 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1534 | if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) { |
Gilles Peskine | faf0b86 | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1535 | if (rec->data_len < transform->maclen) { |
| 1536 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1537 | ("Record too short for MAC:" |
| 1538 | " %" MBEDTLS_PRINTF_SIZET " < %" MBEDTLS_PRINTF_SIZET, |
| 1539 | rec->data_len, transform->maclen)); |
| 1540 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1541 | } |
| 1542 | |
Przemyslaw Stekiel | c8a06fe | 2022-02-07 10:52:47 +0100 | [diff] [blame] | 1543 | /* The only supported stream cipher is "NULL", |
Gilles Peskine | faf0b86 | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1544 | * so there's no encryption to do here.*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1545 | } else |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 1546 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */ |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 1547 | #if defined(MBEDTLS_SSL_HAVE_AEAD) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1549 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1550 | unsigned char *dynamic_iv; |
| 1551 | size_t dynamic_iv_len; |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1552 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1553 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1554 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1555 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1556 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1557 | * Extract dynamic part of nonce for AEAD decryption. |
| 1558 | * |
| 1559 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 1560 | * part of the IV is prepended to the ciphertext and |
| 1561 | * can be chosen freely - in particular, it need not |
| 1562 | * agree with the record sequence number. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1563 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1564 | dynamic_iv_len = sizeof(rec->ctr); |
| 1565 | if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) { |
| 1566 | if (rec->data_len < dynamic_iv_len) { |
| 1567 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1568 | " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1569 | rec->data_len, |
| 1570 | dynamic_iv_len)); |
| 1571 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1572 | } |
| 1573 | dynamic_iv = data; |
| 1574 | |
| 1575 | data += dynamic_iv_len; |
| 1576 | rec->data_offset += dynamic_iv_len; |
| 1577 | rec->data_len -= dynamic_iv_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1578 | } else { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1579 | dynamic_iv = rec->ctr; |
| 1580 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1581 | |
| 1582 | /* Check that there's space for the authentication tag. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | if (rec->data_len < transform->taglen) { |
| 1584 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1585 | ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1586 | rec->data_len, |
| 1587 | transform->taglen)); |
| 1588 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1589 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1590 | rec->data_len -= transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1591 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1592 | /* |
| 1593 | * Prepare nonce from dynamic and static parts. |
| 1594 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1595 | ssl_build_record_nonce(iv, sizeof(iv), |
| 1596 | transform->iv_dec, |
| 1597 | transform->fixed_ivlen, |
| 1598 | dynamic_iv, |
| 1599 | dynamic_iv_len); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1600 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1601 | /* |
| 1602 | * Build additional data for AEAD encryption. |
| 1603 | * This depends on the TLS version. |
| 1604 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1606 | transform->tls_version, |
| 1607 | transform->taglen); |
| 1608 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", |
| 1609 | add_data, add_data_len); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1610 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1611 | /* Because of the check above, we know that there are |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1612 | * explicit_iv_len Bytes preceding data, and taglen |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1613 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 1614 | * the debug message and the invocation of |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 1615 | * mbedtls_cipher_auth_decrypt_ext() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1617 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen); |
| 1618 | MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len, |
| 1619 | transform->taglen); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1620 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1621 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1622 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1623 | */ |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1624 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | status = psa_aead_decrypt(transform->psa_key_dec, |
| 1626 | transform->psa_alg, |
| 1627 | iv, transform->ivlen, |
| 1628 | add_data, add_data_len, |
| 1629 | data, rec->data_len + transform->taglen, |
| 1630 | data, rec->buf_len - (data - rec->buf), |
| 1631 | &olen); |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1633 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1634 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret); |
| 1636 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1637 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1638 | #else |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 1639 | if ((ret = mbedtls_cipher_auth_decrypt_ext |
| 1640 | (&transform->cipher_ctx_dec, |
| 1641 | iv, transform->ivlen, |
| 1642 | add_data, add_data_len, |
| 1643 | data, rec->data_len + transform->taglen, /* src */ |
| 1644 | data, rec->buf_len - (size_t) (data - rec->buf), &olen, /* dst */ |
| 1645 | transform->taglen)) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1646 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1648 | if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) { |
| 1649 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1650 | } |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1653 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1654 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1655 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1656 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1657 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1658 | /* Double-check that AEAD decryption doesn't change content length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | if (olen != rec->data_len) { |
| 1660 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1661 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1662 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1663 | } else |
Valerio Setti | e570704 | 2023-10-11 11:54:42 +0200 | [diff] [blame] | 1664 | #endif /* MBEDTLS_SSL_HAVE_AEAD */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1665 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC || |
| 1667 | ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1668 | size_t minlen = 0; |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1669 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1670 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1671 | size_t part_len; |
| 1672 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
| 1673 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1674 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1675 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1676 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1677 | */ |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1678 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1679 | /* The ciphertext is prefixed with the CBC IV. */ |
| 1680 | minlen += transform->ivlen; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1681 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1682 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1683 | /* Size considerations: |
| 1684 | * |
| 1685 | * - The CBC cipher text must not be empty and hence |
| 1686 | * at least of size transform->ivlen. |
| 1687 | * |
| 1688 | * Together with the potential IV-prefix, this explains |
| 1689 | * the first of the two checks below. |
| 1690 | * |
| 1691 | * - The record must contain a MAC, either in plain or |
| 1692 | * encrypted, depending on whether Encrypt-then-MAC |
| 1693 | * is used or not. |
| 1694 | * - If it is, the message contains the IV-prefix, |
| 1695 | * the CBC ciphertext, and the MAC. |
| 1696 | * - If it is not, the padded plaintext, and hence |
| 1697 | * the CBC ciphertext, has at least length maclen + 1 |
| 1698 | * because there is at least the padding length byte. |
| 1699 | * |
| 1700 | * As the CBC ciphertext is not empty, both cases give the |
| 1701 | * lower bound minlen + maclen + 1 on the record size, which |
| 1702 | * we test for in the second check below. |
| 1703 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1704 | if (rec->data_len < minlen + transform->ivlen || |
| 1705 | rec->data_len < minlen + transform->maclen + 1) { |
| 1706 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1707 | ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET |
| 1708 | "), maclen (%" MBEDTLS_PRINTF_SIZET ") " |
| 1709 | "+ 1 ) ( + expl IV )", |
| 1710 | rec->data_len, |
| 1711 | transform->ivlen, |
| 1712 | transform->maclen)); |
| 1713 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1716 | /* |
| 1717 | * Authenticate before decrypt if enabled |
| 1718 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1719 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1721 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1722 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 1723 | #else |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1724 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1725 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1728 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1729 | /* Update data_len in tandem with add_data. |
| 1730 | * |
| 1731 | * The subtraction is safe because of the previous check |
| 1732 | * data_len >= minlen + maclen + 1. |
| 1733 | * |
| 1734 | * Afterwards, we know that data + data_len is followed by at |
| 1735 | * least maclen Bytes, which justifies the call to |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1736 | * mbedtls_ct_memcmp() below. |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1737 | * |
| 1738 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1739 | rec->data_len -= transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1741 | transform->tls_version, |
| 1742 | transform->taglen); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1743 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1744 | /* Calculate expected MAC. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, |
| 1746 | add_data_len); |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1747 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1748 | status = psa_mac_verify_setup(&operation, transform->psa_mac_dec, |
| 1749 | transform->psa_mac_alg); |
| 1750 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1751 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1752 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | status = psa_mac_update(&operation, add_data, add_data_len); |
| 1755 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1756 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1757 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1759 | status = psa_mac_update(&operation, data, rec->data_len); |
| 1760 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1761 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1762 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1763 | |
| 1764 | /* Compare expected MAC with MAC at the end of the record. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1765 | status = psa_mac_verify_finish(&operation, data + rec->data_len, |
| 1766 | transform->maclen); |
| 1767 | if (status != PSA_SUCCESS) { |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1768 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1770 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data, |
| 1772 | add_data_len); |
| 1773 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1774 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1775 | } |
| 1776 | ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, |
| 1777 | data, rec->data_len); |
| 1778 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1779 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1780 | } |
| 1781 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect); |
| 1782 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1783 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1784 | } |
| 1785 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec); |
| 1786 | if (ret != 0) { |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1787 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1788 | } |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1790 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len, |
| 1791 | transform->maclen); |
| 1792 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, |
| 1793 | transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1794 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1795 | /* Compare expected MAC with MAC at the end of the record. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect, |
| 1797 | transform->maclen) != 0) { |
| 1798 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
Gilles Peskine | d5ba50e | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1799 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1800 | goto hmac_failed_etm_enabled; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1801 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1802 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1803 | auth_done++; |
Gilles Peskine | d5ba50e | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | hmac_failed_etm_enabled: |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1806 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1807 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | status = psa_mac_abort(&operation); |
| 1809 | if (ret == 0 && status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1810 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1811 | } |
Neil Armstrong | 26e6d67 | 2022-02-23 09:30:33 +0100 | [diff] [blame] | 1812 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1813 | mbedtls_platform_zeroize(mac_expect, transform->maclen); |
Neil Armstrong | 4313f55 | 2022-03-02 15:14:07 +0100 | [diff] [blame] | 1814 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1815 | if (ret != 0) { |
| 1816 | if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 1817 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret); |
| 1818 | } |
| 1819 | return ret; |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1820 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1821 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1823 | |
| 1824 | /* |
| 1825 | * Check length sanity |
| 1826 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1827 | |
| 1828 | /* We know from above that data_len > minlen >= 0, |
| 1829 | * so the following check in particular implies that |
| 1830 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | if (rec->data_len % transform->ivlen != 0) { |
| 1832 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1833 | ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0", |
| 1834 | rec->data_len, transform->ivlen)); |
| 1835 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1838 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1839 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1840 | * Initialize for prepended IV for block cipher in TLS v1.2 |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1841 | */ |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1842 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1843 | memcpy(transform->iv_dec, data, transform->ivlen); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1844 | |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1845 | data += transform->ivlen; |
| 1846 | rec->data_offset += transform->ivlen; |
| 1847 | rec->data_len -= transform->ivlen; |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1848 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1849 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1850 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 1851 | |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1852 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | status = psa_cipher_decrypt_setup(&cipher_op, |
| 1854 | transform->psa_key_dec, transform->psa_alg); |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1857 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1858 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_decrypt_setup", ret); |
| 1859 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1860 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1862 | status = psa_cipher_set_iv(&cipher_op, transform->iv_dec, transform->ivlen); |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1865 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1866 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret); |
| 1867 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1868 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1870 | status = psa_cipher_update(&cipher_op, |
| 1871 | data, rec->data_len, |
| 1872 | data, rec->data_len, &olen); |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1874 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1875 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret); |
| 1877 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1878 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1880 | status = psa_cipher_finish(&cipher_op, |
| 1881 | data + olen, rec->data_len - olen, |
| 1882 | &part_len); |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1884 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1885 | ret = PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret); |
| 1887 | return ret; |
Przemyslaw Stekiel | 6b2eedd | 2022-02-03 09:54:34 +0100 | [diff] [blame] | 1888 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1889 | |
| 1890 | olen += part_len; |
| 1891 | #else |
| 1892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1893 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec, |
| 1894 | transform->iv_dec, transform->ivlen, |
| 1895 | data, rec->data_len, data, &olen)) != 0) { |
| 1896 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1897 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1898 | } |
Przemyslaw Stekiel | 2e9711f | 2022-01-13 14:50:15 +0100 | [diff] [blame] | 1899 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1900 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1901 | /* Double-check that length hasn't changed during decryption. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | if (rec->data_len != olen) { |
| 1903 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1904 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1905 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1906 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1907 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 1908 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1909 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 1910 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1911 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | if (auth_done == 1) { |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 1914 | const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1915 | rec->data_len, |
| 1916 | padlen + 1); |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 1917 | correct = mbedtls_ct_bool_and(ge, correct); |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 1918 | padlen = mbedtls_ct_size_if_else_0(ge, padlen); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1920 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1921 | if (rec->data_len < transform->maclen + padlen + 1) { |
| 1922 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1923 | ") < maclen (%" MBEDTLS_PRINTF_SIZET |
| 1924 | ") + padlen (%" MBEDTLS_PRINTF_SIZET ")", |
| 1925 | rec->data_len, |
| 1926 | transform->maclen, |
| 1927 | padlen + 1)); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1928 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1929 | #endif |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 1930 | const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1931 | rec->data_len, |
| 1932 | transform->maclen + padlen + 1); |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 1933 | correct = mbedtls_ct_bool_and(ge, correct); |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 1934 | padlen = mbedtls_ct_size_if_else_0(ge, padlen); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1935 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1936 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1937 | padlen++; |
| 1938 | |
| 1939 | /* Regardless of the validity of the padding, |
| 1940 | * we have data_len >= padlen here. */ |
| 1941 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1942 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1943 | /* The padding check involves a series of up to 256 |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | * consecutive memory reads at the end of the record |
| 1945 | * plaintext buffer. In order to hide the length and |
| 1946 | * validity of the padding, always perform exactly |
| 1947 | * `min(256,plaintext_len)` reads (but take into account |
| 1948 | * only the last `padlen` bytes for the padding check). */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1949 | size_t pad_count = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1950 | volatile unsigned char * const check = data; |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* Index of first padding byte; it has been ensured above |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1953 | * that the subtraction is safe. */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1954 | size_t const padding_idx = rec->data_len - padlen; |
| 1955 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 1956 | size_t const start_idx = rec->data_len - num_checks; |
| 1957 | size_t idx; |
| 1958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1959 | for (idx = start_idx; idx < rec->data_len; idx++) { |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1960 | /* pad_count += (idx >= padding_idx) && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | * (check[idx] == padlen - 1); |
| 1962 | */ |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 1963 | const mbedtls_ct_condition_t a = mbedtls_ct_uint_ge(idx, padding_idx); |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 1964 | size_t increment = mbedtls_ct_size_if_else_0(a, 1); |
Dave Rodgman | b7825ce | 2023-08-10 11:58:18 +0100 | [diff] [blame] | 1965 | const mbedtls_ct_condition_t b = mbedtls_ct_uint_eq(check[idx], padlen - 1); |
Dave Rodgman | 98ddc01 | 2023-08-10 12:11:31 +0100 | [diff] [blame] | 1966 | increment = mbedtls_ct_size_if_else_0(b, increment); |
Dave Rodgman | a81373f | 2023-05-17 12:36:01 +0100 | [diff] [blame] | 1967 | pad_count += increment; |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1968 | } |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 1969 | correct = mbedtls_ct_bool_and(mbedtls_ct_uint_eq(pad_count, padlen), correct); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1971 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 1972 | if (padlen > 0 && correct == MBEDTLS_CT_FALSE) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1973 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected")); |
| 1974 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1975 | #endif |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 1976 | padlen = mbedtls_ct_size_if_else_0(correct, padlen); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1977 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1978 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1979 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1980 | /* If the padding was found to be invalid, padlen == 0 |
| 1981 | * and the subtraction is safe. If the padding was found valid, |
| 1982 | * padlen hasn't been changed and the previous assertion |
| 1983 | * data_len >= padlen still holds. */ |
| 1984 | rec->data_len -= padlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1985 | } else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1986 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1987 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1988 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1989 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1990 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1991 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1992 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption", |
| 1994 | data, rec->data_len); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1995 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1996 | |
| 1997 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1998 | * Authenticate if not done yet. |
| 1999 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2000 | */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 2001 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2002 | if (auth_done == 0) { |
Paul Elliott | 5260ce2 | 2022-05-09 18:15:54 +0100 | [diff] [blame] | 2003 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 }; |
| 2004 | unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 }; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 2005 | |
Gilles Peskine | faf0b86 | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 2006 | /* For CBC+MAC, If the initial value of padlen was such that |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2007 | * data_len < maclen + padlen + 1, then padlen |
| 2008 | * got reset to 1, and the initial check |
| 2009 | * data_len >= minlen + maclen + 1 |
| 2010 | * guarantees that at this point we still |
| 2011 | * have at least data_len >= maclen. |
| 2012 | * |
| 2013 | * If the initial value of padlen was such that |
| 2014 | * data_len >= maclen + padlen + 1, then we have |
| 2015 | * subtracted either padlen + 1 (if the padding was correct) |
| 2016 | * or 0 (if the padding was incorrect) since then, |
| 2017 | * hence data_len >= maclen in any case. |
Gilles Peskine | faf0b86 | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 2018 | * |
| 2019 | * For stream ciphers, we checked above that |
| 2020 | * data_len >= maclen. |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 2021 | */ |
| 2022 | rec->data_len -= transform->maclen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2023 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 2024 | transform->tls_version, |
| 2025 | transform->taglen); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2026 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 2027 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2028 | /* |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | * The next two sizes are the minimum and maximum values of |
| 2030 | * data_len over all padlen values. |
| 2031 | * |
| 2032 | * They're independent of padlen, since we previously did |
| 2033 | * data_len -= padlen. |
| 2034 | * |
| 2035 | * Note that max_len + maclen is never more than the buffer |
| 2036 | * length, as we previously did in_msglen -= maclen too. |
| 2037 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2038 | const size_t max_len = rec->data_len + padlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2039 | const size_t min_len = (max_len > 256) ? max_len - 256 : 0; |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2040 | |
Neil Armstrong | e858996 | 2022-02-25 15:14:29 +0100 | [diff] [blame] | 2041 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | ret = mbedtls_ct_hmac(transform->psa_mac_dec, |
| 2043 | transform->psa_mac_alg, |
| 2044 | add_data, add_data_len, |
| 2045 | data, rec->data_len, min_len, max_len, |
| 2046 | mac_expect); |
Neil Armstrong | e858996 | 2022-02-25 15:14:29 +0100 | [diff] [blame] | 2047 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2048 | ret = mbedtls_ct_hmac(&transform->md_ctx_dec, |
| 2049 | add_data, add_data_len, |
| 2050 | data, rec->data_len, min_len, max_len, |
| 2051 | mac_expect); |
Neil Armstrong | e858996 | 2022-02-25 15:14:29 +0100 | [diff] [blame] | 2052 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2053 | if (ret != 0) { |
| 2054 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret); |
Gilles Peskine | d5ba50e | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 2055 | goto hmac_failed_etm_disabled; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2056 | } |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2058 | mbedtls_ct_memcpy_offset(mac_peer, data, |
| 2059 | rec->data_len, |
| 2060 | min_len, max_len, |
| 2061 | transform->maclen); |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 2062 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2063 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2064 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2065 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen); |
| 2066 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 2067 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | if (mbedtls_ct_memcmp(mac_peer, mac_expect, |
| 2070 | transform->maclen) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2071 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2072 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 2073 | #endif |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 2074 | correct = MBEDTLS_CT_FALSE; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2075 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2076 | auth_done++; |
Gilles Peskine | d5ba50e | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 2077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | hmac_failed_etm_disabled: |
| 2079 | mbedtls_platform_zeroize(mac_peer, transform->maclen); |
| 2080 | mbedtls_platform_zeroize(mac_expect, transform->maclen); |
| 2081 | if (ret != 0) { |
| 2082 | return ret; |
| 2083 | } |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 2084 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* |
| 2087 | * Finally check the correct flag |
| 2088 | */ |
Dave Rodgman | 7d52f2a | 2023-09-12 16:29:39 +0100 | [diff] [blame] | 2089 | if (correct == MBEDTLS_CT_FALSE) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 2091 | } |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 2092 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2093 | |
| 2094 | /* Make extra sure authentication was performed, exactly once */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2095 | if (auth_done != 1) { |
| 2096 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2097 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 2098 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2099 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2100 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 2102 | /* Remove inner padding and infer true content type. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2103 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, |
| 2104 | &rec->type); |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 2105 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2106 | if (ret != 0) { |
| 2107 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 2108 | } |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 2109 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2110 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 2111 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2112 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | if (rec->cid_len != 0) { |
| 2114 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, |
| 2115 | &rec->type); |
| 2116 | if (ret != 0) { |
| 2117 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 2118 | } |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2119 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2120 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 2121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2124 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2127 | #undef MAC_NONE |
| 2128 | #undef MAC_PLAINTEXT |
| 2129 | #undef MAC_CIPHERTEXT |
| 2130 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2131 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2132 | * Fill the input message buffer by appending data to it. |
| 2133 | * The amount of data already fetched is in ssl->in_left. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2134 | * |
| 2135 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 2136 | * available (from this read and/or a previous one). Otherwise, an error code |
| 2137 | * is returned (possibly EOF or WANT_READ). |
| 2138 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2139 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 2140 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 2141 | * since we always read a whole datagram at once. |
| 2142 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 2143 | * For DTLS, it is up to the caller to set ssl->next_record_offset when |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2144 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2145 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2146 | int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2147 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2148 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2149 | size_t len; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2150 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2151 | size_t in_buf_len = ssl->in_buf_len; |
| 2152 | #else |
| 2153 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 2154 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2158 | if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) { |
| 2159 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() ")); |
| 2160 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2161 | } |
| 2162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2163 | if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { |
| 2164 | MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits")); |
| 2165 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 2166 | } |
| 2167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2168 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2169 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2170 | uint32_t timeout; |
| 2171 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2172 | /* |
| 2173 | * The point is, we need to always read a full datagram at once, so we |
| 2174 | * sometimes read more then requested, and handle the additional data. |
| 2175 | * It could be the rest of the current record (while fetching the |
| 2176 | * header) and/or some other records in the same datagram. |
| 2177 | */ |
| 2178 | |
| 2179 | /* |
| 2180 | * Move to the next record in the already read datagram if applicable |
| 2181 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2182 | if (ssl->next_record_offset != 0) { |
| 2183 | if (ssl->in_left < ssl->next_record_offset) { |
| 2184 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2185 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | ssl->in_left -= ssl->next_record_offset; |
| 2189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | if (ssl->in_left != 0) { |
| 2191 | MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %" |
| 2192 | MBEDTLS_PRINTF_SIZET, |
| 2193 | ssl->next_record_offset)); |
| 2194 | memmove(ssl->in_hdr, |
| 2195 | ssl->in_hdr + ssl->next_record_offset, |
| 2196 | ssl->in_left); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | ssl->next_record_offset = 0; |
| 2200 | } |
| 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 2203 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 2204 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2205 | |
| 2206 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2207 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2208 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2209 | if (nb_want <= ssl->in_left) { |
| 2210 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
| 2211 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2212 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2213 | |
| 2214 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 2215 | * A record can't be split across datagrams. If we need to read but |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2216 | * are not at the beginning of a new record, the caller did something |
| 2217 | * wrong. |
| 2218 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | if (ssl->in_left != 0) { |
| 2220 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2221 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2222 | } |
| 2223 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2224 | /* |
| 2225 | * Don't even try to read if time's out already. |
| 2226 | * This avoids by-passing the timer when repeatedly receiving messages |
| 2227 | * that will end up being dropped. |
| 2228 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
| 2230 | MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired")); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2231 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2232 | } else { |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 2233 | len = in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | if (mbedtls_ssl_is_handshake_over(ssl) == 0) { |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2236 | timeout = ssl->handshake->retransmit_timeout; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2237 | } else { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2238 | timeout = ssl->conf->read_timeout; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout)); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2243 | if (ssl->f_recv_timeout != NULL) { |
| 2244 | ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len, |
| 2245 | timeout); |
| 2246 | } else { |
| 2247 | ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len); |
| 2248 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2250 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | if (ret == 0) { |
| 2253 | return MBEDTLS_ERR_SSL_CONN_EOF; |
| 2254 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2257 | if (ret == MBEDTLS_ERR_SSL_TIMEOUT) { |
| 2258 | MBEDTLS_SSL_DEBUG_MSG(2, ("timeout")); |
| 2259 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2261 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 2262 | if (ssl_double_retransmit_timeout(ssl) != 0) { |
| 2263 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout")); |
| 2264 | return MBEDTLS_ERR_SSL_TIMEOUT; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 2268 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 2269 | return ret; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2270 | } |
| 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2273 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2274 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2275 | else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 2276 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 2277 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 2278 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 2279 | ret); |
| 2280 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2283 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2284 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2285 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | if (ret < 0) { |
| 2289 | return ret; |
| 2290 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2291 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2292 | ssl->in_left = ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2293 | } else |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2294 | #endif |
| 2295 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2296 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 2297 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 2298 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2300 | while (ssl->in_left < nb_want) { |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2301 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2304 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2305 | } else { |
| 2306 | if (ssl->f_recv_timeout != NULL) { |
| 2307 | ret = ssl->f_recv_timeout(ssl->p_bio, |
| 2308 | ssl->in_hdr + ssl->in_left, len, |
| 2309 | ssl->conf->read_timeout); |
| 2310 | } else { |
| 2311 | ret = ssl->f_recv(ssl->p_bio, |
| 2312 | ssl->in_hdr + ssl->in_left, len); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 2313 | } |
| 2314 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2316 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 2317 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 2318 | ssl->in_left, nb_want)); |
| 2319 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | if (ret == 0) { |
| 2322 | return MBEDTLS_ERR_SSL_CONN_EOF; |
| 2323 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | if (ret < 0) { |
| 2326 | return ret; |
| 2327 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2328 | |
Dave Rodgman | 4a5c9ee | 2023-02-10 16:03:44 +0000 | [diff] [blame] | 2329 | if ((size_t) ret > len) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2330 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2331 | ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 2332 | " were requested", |
| 2333 | ret, len)); |
| 2334 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2337 | ssl->in_left += ret; |
| 2338 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * Flush any data not yet written |
| 2348 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2349 | int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2350 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2351 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2352 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2354 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2356 | if (ssl->f_send == NULL) { |
| 2357 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() ")); |
| 2358 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2361 | /* Avoid incrementing counter if data is flushed */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2362 | if (ssl->out_left == 0) { |
| 2363 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
| 2364 | return 0; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2365 | } |
| 2366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2367 | while (ssl->out_left > 0) { |
| 2368 | MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET |
| 2369 | ", out_left: %" MBEDTLS_PRINTF_SIZET, |
| 2370 | mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2371 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2372 | buf = ssl->out_hdr - ssl->out_left; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 2374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2375 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2377 | if (ret <= 0) { |
| 2378 | return ret; |
| 2379 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2380 | |
Dave Rodgman | 4a5c9ee | 2023-02-10 16:03:44 +0000 | [diff] [blame] | 2381 | if ((size_t) ret > ssl->out_left) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2383 | ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 2384 | " bytes were sent", |
| 2385 | ret, ssl->out_left)); |
| 2386 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 2387 | } |
| 2388 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2389 | ssl->out_left -= ret; |
| 2390 | } |
| 2391 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2392 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2393 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2394 | ssl->out_hdr = ssl->out_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2395 | } else |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2396 | #endif |
| 2397 | { |
| 2398 | ssl->out_hdr = ssl->out_buf + 8; |
| 2399 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2400 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2402 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2404 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2407 | /* |
| 2408 | * Functions to handle the DTLS retransmission state machine |
| 2409 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2410 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2411 | /* |
| 2412 | * Append current handshake message to current outgoing flight |
| 2413 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2414 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2415 | static int ssl_flight_append(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | mbedtls_ssl_flight_item *msg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append")); |
| 2419 | MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight", |
| 2420 | ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2421 | |
| 2422 | /* Allocate space for current message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2423 | if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) { |
| 2424 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2425 | sizeof(mbedtls_ssl_flight_item))); |
| 2426 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2427 | } |
| 2428 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2429 | if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) { |
| 2430 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2431 | ssl->out_msglen)); |
| 2432 | mbedtls_free(msg); |
| 2433 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | /* Copy current handshake message with headers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2437 | memcpy(msg->p, ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2438 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2439 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2440 | msg->next = NULL; |
| 2441 | |
| 2442 | /* Append to the current flight */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2443 | if (ssl->handshake->flight == NULL) { |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2444 | ssl->handshake->flight = msg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2445 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2446 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2447 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2448 | cur = cur->next; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2450 | cur->next = msg; |
| 2451 | } |
| 2452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append")); |
| 2454 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | /* |
| 2458 | * Free the current flight of handshake messages |
| 2459 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2460 | void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2462 | mbedtls_ssl_flight_item *cur = flight; |
| 2463 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2465 | while (cur != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2466 | next = cur->next; |
| 2467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2468 | mbedtls_free(cur->p); |
| 2469 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2470 | |
| 2471 | cur = next; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2476 | * Swap transform_out and out_ctr with the alternative ones |
| 2477 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 2478 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2479 | static int ssl_swap_epochs(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2481 | mbedtls_ssl_transform *tmp_transform; |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 2482 | unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN]; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2484 | if (ssl->transform_out == ssl->handshake->alt_transform_out) { |
| 2485 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs")); |
| 2486 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2487 | } |
| 2488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2489 | MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2490 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2491 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2492 | tmp_transform = ssl->transform_out; |
| 2493 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 2494 | ssl->handshake->alt_transform_out = tmp_transform; |
| 2495 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2496 | /* Swap epoch + sequence_number */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2497 | memcpy(tmp_out_ctr, ssl->cur_out_ctr, sizeof(tmp_out_ctr)); |
| 2498 | memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, |
| 2499 | sizeof(ssl->cur_out_ctr)); |
| 2500 | memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, |
| 2501 | sizeof(ssl->handshake->alt_out_ctr)); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2502 | |
| 2503 | /* Adjust to the newly activated transform */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2504 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2506 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2511 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2512 | int mbedtls_ssl_resend(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2513 | { |
| 2514 | int ret = 0; |
| 2515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2516 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2518 | ret = mbedtls_ssl_flight_transmit(ssl); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2527 | * |
| 2528 | * Need to remember the current message in case flush_output returns |
| 2529 | * WANT_WRITE, causing us to exit this function and come back later. |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2530 | * This function must be called until state is no longer SENDING. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2531 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2532 | int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2533 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2534 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2537 | if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) { |
| 2538 | MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2539 | |
| 2540 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2541 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | ret = ssl_swap_epochs(ssl); |
| 2543 | if (ret != 0) { |
| 2544 | return ret; |
| 2545 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2547 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2548 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2550 | while (ssl->handshake->cur_msg != NULL) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2551 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2552 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2553 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2554 | int const is_finished = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2555 | (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2556 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED); |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2557 | |
Ronald Cron | 00d012f2 | 2022-03-08 15:57:12 +0100 | [diff] [blame] | 2558 | int const force_flush = ssl->disable_datagram_packing == 1 ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2559 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2560 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2561 | /* Swap epochs before sending Finished: we can't do it after |
| 2562 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 2563 | * Must be done before copying, may change out_msg pointer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) { |
| 2565 | MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message")); |
| 2566 | ret = ssl_swap_epochs(ssl); |
| 2567 | if (ret != 0) { |
| 2568 | return ret; |
| 2569 | } |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2570 | } |
| 2571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2572 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2573 | if (ret < 0) { |
| 2574 | return ret; |
| 2575 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2576 | max_frag_len = (size_t) ret; |
| 2577 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2578 | /* CCS is copied as is, while HS messages may need fragmentation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2579 | if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 2580 | if (max_frag_len == 0) { |
| 2581 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2582 | return ret; |
| 2583 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2584 | |
| 2585 | continue; |
| 2586 | } |
| 2587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2588 | memcpy(ssl->out_msg, cur->p, cur->len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2589 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2590 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2591 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2592 | /* Update position inside current message */ |
| 2593 | ssl->handshake->cur_msg_p += cur->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2594 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2595 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 2596 | const size_t hs_len = cur->len - 12; |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 2597 | const size_t frag_off = (size_t) (p - (cur->p + 12)); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2598 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2599 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2601 | if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) { |
| 2602 | if (is_finished) { |
| 2603 | ret = ssl_swap_epochs(ssl); |
| 2604 | if (ret != 0) { |
| 2605 | return ret; |
| 2606 | } |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2607 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2610 | return ret; |
| 2611 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2612 | |
| 2613 | continue; |
| 2614 | } |
| 2615 | max_hs_frag_len = max_frag_len - 12; |
| 2616 | |
| 2617 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2618 | max_hs_frag_len : rem_len; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2620 | if (frag_off == 0 && cur_hs_frag_len != hs_len) { |
| 2621 | MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)", |
| 2622 | (unsigned) cur_hs_frag_len, |
| 2623 | (unsigned) max_hs_frag_len)); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2624 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 2625 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2626 | /* Messages are stored with handshake headers as if not fragmented, |
| 2627 | * copy beginning of headers then fill fragmentation fields. |
| 2628 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | memcpy(ssl->out_msg, cur->p, 6); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off); |
| 2632 | ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off); |
| 2633 | ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len); |
| 2636 | ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len); |
| 2637 | ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2639 | MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2640 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 2641 | /* Copy the handshake message content and set records fields */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2642 | memcpy(ssl->out_msg + 12, p, cur_hs_frag_len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2643 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2644 | ssl->out_msgtype = cur->type; |
| 2645 | |
| 2646 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2647 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | /* If done with the current message move to the next one if any */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2651 | if (ssl->handshake->cur_msg_p >= cur->p + cur->len) { |
| 2652 | if (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2653 | ssl->handshake->cur_msg = cur->next; |
| 2654 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2656 | ssl->handshake->cur_msg = NULL; |
| 2657 | ssl->handshake->cur_msg_p = NULL; |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | /* Actually send the message out */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { |
| 2663 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 2664 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2668 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2669 | return ret; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2670 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | /* Update state and set timer */ |
| 2673 | if (mbedtls_ssl_is_handshake_over(ssl) == 1) { |
| 2674 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
| 2675 | } else { |
| 2676 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
| 2677 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
| 2678 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2680 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit")); |
| 2681 | |
| 2682 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2683 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2684 | |
| 2685 | /* |
| 2686 | * To be called when the last message of an incoming flight is received. |
| 2687 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2689 | { |
| 2690 | /* We won't need to resend that one any more */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | mbedtls_ssl_flight_free(ssl->handshake->flight); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2692 | ssl->handshake->flight = NULL; |
| 2693 | ssl->handshake->cur_msg = NULL; |
| 2694 | |
| 2695 | /* The next incoming flight will start with this msg_seq */ |
| 2696 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 2697 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2698 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 2699 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2700 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2701 | /* Clear future message buffering structure. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2702 | mbedtls_ssl_buffering_free(ssl); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2703 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2704 | /* Cancel timer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2705 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2708 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2709 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2711 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2712 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2713 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2714 | |
| 2715 | /* |
| 2716 | * To be called when the last message of an outgoing flight is send. |
| 2717 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2718 | void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2719 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2720 | ssl_reset_retransmit_timeout(ssl); |
| 2721 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2723 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2724 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2725 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2726 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2727 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2729 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2730 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2731 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2732 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2733 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2734 | */ |
Dave Rodgman | c37ad44 | 2023-11-03 23:36:06 +0000 | [diff] [blame] | 2735 | int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned char hs_type, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2736 | unsigned char **buf, size_t *buf_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 2737 | { |
| 2738 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2739 | * Reserve 4 bytes for handshake header. ( Section 4,RFC 8446 ) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 2740 | * ... |
| 2741 | * HandshakeType msg_type; |
| 2742 | * uint24 length; |
| 2743 | * ... |
| 2744 | */ |
| 2745 | *buf = ssl->out_msg + 4; |
| 2746 | *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; |
| 2747 | |
| 2748 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 2749 | ssl->out_msg[0] = hs_type; |
| 2750 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2751 | return 0; |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 2752 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2753 | |
| 2754 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2755 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2756 | * |
| 2757 | * - fill in handshake headers |
| 2758 | * - update handshake checksum |
| 2759 | * - DTLS: save message for resending |
| 2760 | * - then pass to the record layer |
| 2761 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2762 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 2763 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2764 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2765 | * Inputs: |
| 2766 | * - ssl->out_msglen: 4 + actual handshake message len |
| 2767 | * (4 is the size of handshake headers for TLS) |
| 2768 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 2769 | * - ssl->out_msg + 4: the handshake message body |
| 2770 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 2771 | * Outputs, ie state before passing to flight_append() or write_record(): |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2772 | * - ssl->out_msglen: the length of the record contents |
| 2773 | * (including handshake headers but excluding record headers) |
| 2774 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2775 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2776 | int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl, |
| 2777 | int update_checksum, |
| 2778 | int force_flush) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2779 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2780 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2781 | const size_t hs_len = ssl->out_msglen - 4; |
| 2782 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2783 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2784 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2785 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2786 | /* |
| 2787 | * Sanity checks |
| 2788 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2789 | if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2790 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 2791 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2792 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2793 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2794 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2795 | /* Whenever we send anything different from a |
| 2796 | * HelloRequest we should be in a handshake - double check. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2798 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) && |
| 2799 | ssl->handshake == NULL) { |
| 2800 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2801 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2802 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2803 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2804 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2805 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2806 | ssl->handshake != NULL && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2807 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 2808 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2809 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2810 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2811 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2812 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2813 | /* Double-check that we did not exceed the bounds |
| 2814 | * of the outgoing record buffer. |
| 2815 | * This should never fail as the various message |
| 2816 | * writing functions must obey the bounds of the |
| 2817 | * outgoing record buffer, but better be safe. |
| 2818 | * |
| 2819 | * Note: We deliberately do not check for the MTU or MFL here. |
| 2820 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2822 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: " |
| 2823 | "size %" MBEDTLS_PRINTF_SIZET |
| 2824 | ", maximum %" MBEDTLS_PRINTF_SIZET, |
| 2825 | ssl->out_msglen, |
| 2826 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 2827 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2828 | } |
| 2829 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2830 | /* |
| 2831 | * Fill handshake headers |
| 2832 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2833 | if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 2834 | ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len); |
| 2835 | ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len); |
| 2836 | ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2837 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2838 | /* |
| 2839 | * DTLS has additional fields in the Handshake layer, |
| 2840 | * between the length field and the actual payload: |
| 2841 | * uint16 message_seq; |
| 2842 | * uint24 fragment_offset; |
| 2843 | * uint24 fragment_length; |
| 2844 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2845 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2846 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2847 | /* Make room for the additional DTLS fields */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2848 | if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) { |
| 2849 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: " |
| 2850 | "size %" MBEDTLS_PRINTF_SIZET ", maximum %" |
| 2851 | MBEDTLS_PRINTF_SIZET, |
| 2852 | hs_len, |
| 2853 | (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12))); |
| 2854 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 2855 | } |
| 2856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2858 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2859 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2860 | /* Write message_seq and update it, except for HelloRequest */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) { |
| 2862 | MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4); |
| 2863 | ++(ssl->handshake->out_msg_seq); |
| 2864 | } else { |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2865 | ssl->out_msg[4] = 0; |
| 2866 | ssl->out_msg[5] = 0; |
| 2867 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2868 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2869 | /* Handshake hashes are computed without fragmentation, |
| 2870 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | memset(ssl->out_msg + 6, 0x00, 3); |
| 2872 | memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2873 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2874 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2875 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 2876 | /* Update running hashes of handshake messages seen */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2877 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 2878 | ret = ssl->handshake->update_checksum(ssl, ssl->out_msg, |
| 2879 | ssl->out_msglen); |
| 2880 | if (ret != 0) { |
| 2881 | MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret); |
| 2882 | return ret; |
| 2883 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2887 | /* Either send now, or just save to be sent (and resent) later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2888 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2890 | !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2891 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) { |
| 2892 | if ((ret = ssl_flight_append(ssl)) != 0) { |
| 2893 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret); |
| 2894 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2895 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | } else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2897 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2898 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { |
| 2900 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret); |
| 2901 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2902 | } |
| 2903 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2904 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2905 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2906 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2907 | return 0; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2910 | int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl, |
| 2911 | size_t buf_len, size_t msg_len) |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 2912 | { |
| 2913 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2914 | size_t msg_with_header_len; |
| 2915 | ((void) buf_len); |
| 2916 | |
| 2917 | /* Add reserved 4 bytes for handshake header */ |
| 2918 | msg_with_header_len = msg_len + 4; |
| 2919 | ssl->out_msglen = msg_with_header_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_handshake_msg_ext(ssl, 0, 0)); |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 2921 | |
| 2922 | cleanup: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | return ret; |
Ronald Cron | 8f6d39a | 2022-03-10 18:56:50 +0100 | [diff] [blame] | 2924 | } |
| 2925 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2926 | /* |
| 2927 | * Record layer functions |
| 2928 | */ |
| 2929 | |
| 2930 | /* |
| 2931 | * Write current record. |
| 2932 | * |
| 2933 | * Uses: |
| 2934 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 2935 | * - ssl->out_msglen: length of the record content (excl headers) |
| 2936 | * - ssl->out_msg: record content |
| 2937 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush) |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2939 | { |
| 2940 | int ret, done = 0; |
| 2941 | size_t len = ssl->out_msglen; |
Ronald Cron | 00d012f2 | 2022-03-08 15:57:12 +0100 | [diff] [blame] | 2942 | int flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2944 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2946 | if (!done) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2947 | unsigned i; |
| 2948 | size_t protected_record_size; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2949 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2950 | size_t out_buf_len = ssl->out_buf_len; |
| 2951 | #else |
| 2952 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 2953 | #endif |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2954 | /* Skip writing the record content type to after the encryption, |
| 2955 | * as it may change when using the CID extension. */ |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2956 | mbedtls_ssl_protocol_version tls_ver = ssl->tls_version; |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2957 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Jerry Yu | 1ca80f7 | 2021-11-08 10:30:54 +0800 | [diff] [blame] | 2958 | /* TLS 1.3 still uses the TLS 1.2 version identifier |
| 2959 | * for backwards compatibility. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2960 | if (tls_ver == MBEDTLS_SSL_VERSION_TLS1_3) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 2961 | tls_ver = MBEDTLS_SSL_VERSION_TLS1_2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 2963 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2964 | mbedtls_ssl_write_version(ssl->out_hdr + 1, ssl->conf->transport, |
| 2965 | tls_ver); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2967 | memcpy(ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
| 2968 | MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2970 | if (ssl->transform_out != NULL) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2971 | mbedtls_record rec; |
| 2972 | |
| 2973 | rec.buf = ssl->out_iv; |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 2974 | rec.buf_len = out_buf_len - (size_t) (ssl->out_iv - ssl->out_buf); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2975 | rec.data_len = ssl->out_msglen; |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 2976 | rec.data_offset = (size_t) (ssl->out_msg - rec.buf); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | memcpy(&rec.ctr[0], ssl->out_ctr, sizeof(rec.ctr)); |
| 2979 | mbedtls_ssl_write_version(rec.ver, ssl->conf->transport, tls_ver); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2980 | rec.type = ssl->out_msgtype; |
| 2981 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2982 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2983 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2984 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2985 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2986 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2987 | if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec, |
| 2988 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 2989 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret); |
| 2990 | return ret; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2993 | if (rec.data_offset != 0) { |
| 2994 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2995 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2998 | /* Update the record content type and CID. */ |
| 2999 | ssl->out_msgtype = rec.type; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3001 | memcpy(ssl->out_cid, rec.cid, rec.cid_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3002 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 3003 | ssl->out_msglen = len = rec.data_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3004 | MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3008 | |
| 3009 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3010 | /* In case of DTLS, double-check that we don't exceed |
| 3011 | * the remaining space in the datagram. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3012 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3013 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 3014 | if (ret < 0) { |
| 3015 | return ret; |
| 3016 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | if (protected_record_size > (size_t) ret) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3019 | /* Should never happen */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3021 | } |
| 3022 | } |
| 3023 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3024 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3025 | /* Now write the potentially updated record content type. */ |
| 3026 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 3027 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3028 | MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, " |
| 3029 | "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 3030 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 3031 | ssl->out_hdr[2], len)); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3032 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3033 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", |
| 3034 | ssl->out_hdr, protected_record_size); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3035 | |
| 3036 | ssl->out_left += protected_record_size; |
| 3037 | ssl->out_hdr += protected_record_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 3039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3040 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) { |
| 3041 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Gabor Mezei | 05ebf3b | 2022-06-28 11:55:35 +0200 | [diff] [blame] | 3042 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3043 | } |
| 3044 | } |
Gabor Mezei | 05ebf3b | 2022-06-28 11:55:35 +0200 | [diff] [blame] | 3045 | |
Gabor Mezei | 96ae926 | 2022-06-28 11:45:18 +0200 | [diff] [blame] | 3046 | /* The loop goes to its end if the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 3048 | MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap")); |
| 3049 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 3050 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3053 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3054 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3055 | flush == SSL_DONT_FORCE_FLUSH) { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 3056 | size_t remaining; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 3058 | if (ret < 0) { |
| 3059 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram", |
| 3060 | ret); |
| 3061 | return ret; |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | remaining = (size_t) ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3065 | if (remaining == 0) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3066 | flush = SSL_FORCE_FLUSH; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3067 | } else { |
| 3068 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 3069 | ("Still %u bytes available in current datagram", |
| 3070 | (unsigned) remaining)); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3075 | if ((flush == SSL_FORCE_FLUSH) && |
| 3076 | (ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 3077 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 3078 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3083 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3086 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3087 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3088 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3090 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | if (ssl->in_msglen < ssl->in_hslen || |
| 3092 | memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 || |
| 3093 | memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) { |
| 3094 | return 1; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3095 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3096 | return 0; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3097 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3099 | static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3100 | { |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3101 | return MBEDTLS_GET_UINT24_BE(ssl->in_msg, 9); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3102 | } |
| 3103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3105 | { |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3106 | return MBEDTLS_GET_UINT24_BE(ssl->in_msg, 6); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3109 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3110 | static int ssl_check_hs_header(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3111 | { |
| 3112 | uint32_t msg_len, frag_off, frag_len; |
| 3113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3114 | msg_len = ssl_get_hs_total_len(ssl); |
| 3115 | frag_off = ssl_get_hs_frag_off(ssl); |
| 3116 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3117 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3118 | if (frag_off > msg_len) { |
| 3119 | return -1; |
| 3120 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3122 | if (frag_len > msg_len - frag_off) { |
| 3123 | return -1; |
| 3124 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3126 | if (frag_len + 12 > ssl->in_msglen) { |
| 3127 | return -1; |
| 3128 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | return 0; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3131 | } |
| 3132 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3133 | /* |
| 3134 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 3135 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3136 | static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3137 | { |
| 3138 | unsigned int start_bits, end_bits; |
| 3139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3140 | start_bits = 8 - (offset % 8); |
| 3141 | if (start_bits != 8) { |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3142 | size_t first_byte_idx = offset / 8; |
| 3143 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 3144 | /* Special case */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3145 | if (len <= start_bits) { |
| 3146 | for (; len != 0; len--) { |
| 3147 | mask[first_byte_idx] |= 1 << (start_bits - len); |
| 3148 | } |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 3149 | |
| 3150 | /* Avoid potential issues with offset or len becoming invalid */ |
| 3151 | return; |
| 3152 | } |
| 3153 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3154 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 3155 | len -= start_bits; |
| 3156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3157 | for (; start_bits != 0; start_bits--) { |
| 3158 | mask[first_byte_idx] |= 1 << (start_bits - 1); |
| 3159 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | end_bits = len % 8; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | if (end_bits != 0) { |
| 3164 | size_t last_byte_idx = (offset + len) / 8; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3165 | |
| 3166 | len -= end_bits; /* Now len % 8 == 0 */ |
| 3167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3168 | for (; end_bits != 0; end_bits--) { |
| 3169 | mask[last_byte_idx] |= 1 << (8 - end_bits); |
| 3170 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3171 | } |
| 3172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3173 | memset(mask + offset / 8, 0xFF, len / 8); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | /* |
| 3177 | * Check that bitmask is full |
| 3178 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3179 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3180 | static int ssl_bitmask_check(unsigned char *mask, size_t len) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3181 | { |
| 3182 | size_t i; |
| 3183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | for (i = 0; i < len / 8; i++) { |
| 3185 | if (mask[i] != 0xFF) { |
| 3186 | return -1; |
| 3187 | } |
| 3188 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3190 | for (i = 0; i < len % 8; i++) { |
| 3191 | if ((mask[len / 8] & (1 << (7 - i))) == 0) { |
| 3192 | return -1; |
| 3193 | } |
| 3194 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3197 | } |
| 3198 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3199 | /* msg_len does not include the handshake header */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3200 | static size_t ssl_get_reassembly_buffer_size(size_t msg_len, |
| 3201 | unsigned add_bitmap) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3202 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3203 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3204 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3205 | alloc_len = 12; /* Handshake header */ |
| 3206 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | if (add_bitmap) { |
| 3209 | alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3210 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3211 | } |
| 3212 | return alloc_len; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3213 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 3214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3215 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3217 | static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl) |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 3218 | { |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3219 | return MBEDTLS_GET_UINT24_BE(ssl->in_msg, 1); |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 3220 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3223 | { |
Deomid rojer Ryabkov | 5c853ea | 2025-01-26 11:10:54 +0200 | [diff] [blame] | 3224 | /* First handshake fragment must at least include the header. */ |
| 3225 | if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl) && ssl->in_hslen == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET, |
| 3227 | ssl->in_msglen)); |
| 3228 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3229 | } |
| 3230 | |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3231 | if (ssl->in_hslen == 0) { |
| 3232 | ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl); |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3233 | ssl->badmac_seen_or_in_hsfraglen = 0; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3234 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3236 | MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen =" |
| 3237 | " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" |
| 3238 | MBEDTLS_PRINTF_SIZET, |
| 3239 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen)); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3241 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3242 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3243 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3244 | unsigned int recv_msg_seq = MBEDTLS_GET_UINT16_BE(ssl->in_msg, 4); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3246 | if (ssl_check_hs_header(ssl) != 0) { |
| 3247 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header")); |
| 3248 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 3249 | } |
| 3250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3251 | if (ssl->handshake != NULL && |
| 3252 | ((mbedtls_ssl_is_handshake_over(ssl) == 0 && |
| 3253 | recv_msg_seq != ssl->handshake->in_msg_seq) || |
| 3254 | (mbedtls_ssl_is_handshake_over(ssl) == 1 && |
| 3255 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) { |
| 3256 | if (recv_msg_seq > ssl->handshake->in_msg_seq) { |
| 3257 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 3258 | ( |
| 3259 | "received future handshake message of sequence number %u (next %u)", |
| 3260 | recv_msg_seq, |
| 3261 | ssl->handshake->in_msg_seq)); |
| 3262 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 3263 | } |
| 3264 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 3265 | /* Retransmit only on last message from previous flight, to avoid |
| 3266 | * too many retransmissions. |
| 3267 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3268 | if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
| 3269 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) { |
| 3270 | MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, " |
| 3271 | "message_seq = %u, start_of_flight = %u", |
| 3272 | recv_msg_seq, |
| 3273 | ssl->handshake->in_flight_start_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3275 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 3276 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 3277 | return ret; |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3278 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3279 | } else { |
| 3280 | MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: " |
| 3281 | "message_seq = %u, expected = %u", |
| 3282 | recv_msg_seq, |
| 3283 | ssl->handshake->in_msg_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3284 | } |
| 3285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3286 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3287 | } |
| 3288 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3289 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3290 | /* Message reassembly is handled alongside buffering of future |
| 3291 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 3292 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 3293 | * handshake logic layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3294 | if (ssl_hs_is_proper_fragment(ssl) == 1) { |
| 3295 | MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message")); |
| 3296 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3297 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3298 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3299 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3300 | if (ssl->badmac_seen_or_in_hsfraglen <= ssl->in_hslen) { |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3301 | int ret; |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3302 | const size_t hs_remain = ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3303 | MBEDTLS_SSL_DEBUG_MSG(3, |
Gilles Peskine | ebdd405 | 2025-02-17 16:25:24 +0100 | [diff] [blame] | 3304 | ("handshake fragment: %u .. %" |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3305 | MBEDTLS_PRINTF_SIZET " of %" |
| 3306 | MBEDTLS_PRINTF_SIZET " msglen %" MBEDTLS_PRINTF_SIZET, |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3307 | ssl->badmac_seen_or_in_hsfraglen, |
| 3308 | (size_t) ssl->badmac_seen_or_in_hsfraglen + |
Deomid rojer Ryabkov | 1f4088c | 2025-01-18 15:58:57 +0200 | [diff] [blame] | 3309 | (hs_remain <= ssl->in_msglen ? hs_remain : ssl->in_msglen), |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3310 | ssl->in_hslen, ssl->in_msglen)); |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3311 | if (ssl->in_msglen < hs_remain) { |
Gilles Peskine | ebdd405 | 2025-02-17 16:25:24 +0100 | [diff] [blame] | 3312 | /* ssl->in_msglen is a 25-bit value since it is the sum of the |
| 3313 | * header length plus the payload length, the header length is 4 |
| 3314 | * and the payload length was received on the wire encoded as |
| 3315 | * 3 octets. We don't support 16-bit platforms; more specifically, |
| 3316 | * we assume that both unsigned and size_t are at least 32 bits. |
| 3317 | * Therefore there is no possible integer overflow here. |
| 3318 | */ |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3319 | ssl->badmac_seen_or_in_hsfraglen += (unsigned) ssl->in_msglen; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3320 | ssl->in_hdr = ssl->in_msg + ssl->in_msglen; |
| 3321 | ssl->in_msglen = 0; |
| 3322 | mbedtls_ssl_update_in_pointers(ssl); |
| 3323 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 3324 | } |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3325 | if (ssl->badmac_seen_or_in_hsfraglen > 0) { |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3326 | /* |
Deomid rojer Ryabkov | bbe8745 | 2025-02-13 13:41:51 +0300 | [diff] [blame] | 3327 | * At in_first_hdr we have a sequence of records that cover the next handshake |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3328 | * record, each with its own record header that we need to remove. |
| 3329 | * Note that the reassembled record size may not equal the size of the message, |
Deomid rojer Ryabkov | bbe8745 | 2025-02-13 13:41:51 +0300 | [diff] [blame] | 3330 | * there may be more messages after it, complete or partial. |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3331 | */ |
Deomid rojer Ryabkov | bbe8745 | 2025-02-13 13:41:51 +0300 | [diff] [blame] | 3332 | unsigned char *in_first_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
| 3333 | unsigned char *p = in_first_hdr, *q = NULL; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3334 | size_t merged_rec_len = 0; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3335 | do { |
| 3336 | mbedtls_record rec; |
| 3337 | ret = ssl_parse_record_header(ssl, p, mbedtls_ssl_in_hdr_len(ssl), &rec); |
| 3338 | if (ret != 0) { |
| 3339 | return ret; |
| 3340 | } |
| 3341 | merged_rec_len += rec.data_len; |
| 3342 | p = rec.buf + rec.buf_len; |
| 3343 | if (q != NULL) { |
| 3344 | memmove(q, rec.buf + rec.data_offset, rec.data_len); |
| 3345 | q += rec.data_len; |
| 3346 | } else { |
| 3347 | q = p; |
| 3348 | } |
| 3349 | } while (merged_rec_len < ssl->in_hslen); |
Deomid rojer Ryabkov | bbe8745 | 2025-02-13 13:41:51 +0300 | [diff] [blame] | 3350 | ssl->in_hdr = in_first_hdr; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3351 | mbedtls_ssl_update_in_pointers(ssl); |
| 3352 | ssl->in_msglen = merged_rec_len; |
| 3353 | /* Adjust message length. */ |
| 3354 | MBEDTLS_PUT_UINT16_BE(merged_rec_len, ssl->in_len, 0); |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 3355 | ssl->badmac_seen_or_in_hsfraglen = 0; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 3356 | MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record", |
| 3357 | ssl->in_hdr, mbedtls_ssl_in_hdr_len(ssl) + merged_rec_len); |
| 3358 | } |
Deomid rojer Ryabkov | bbe8745 | 2025-02-13 13:41:51 +0300 | [diff] [blame] | 3359 | } else { |
| 3360 | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3363 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3364 | } |
| 3365 | |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 3366 | int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3367 | { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 3368 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3369 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 3372 | ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen); |
| 3373 | if (ret != 0) { |
| 3374 | MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret); |
| 3375 | return ret; |
| 3376 | } |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 3377 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3378 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3379 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3380 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3381 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3382 | ssl->handshake != NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3383 | unsigned offset; |
| 3384 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3385 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3386 | /* Increment handshake sequence number */ |
| 3387 | hs->in_msg_seq++; |
| 3388 | |
| 3389 | /* |
| 3390 | * Clear up handshake buffering and reassembly structure. |
| 3391 | */ |
| 3392 | |
| 3393 | /* Free first entry */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3394 | ssl_buffering_free_slot(ssl, 0); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3395 | |
| 3396 | /* Shift all other entries */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3397 | for (offset = 0, hs_buf = &hs->buffering.hs[0]; |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3398 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3399 | offset++, hs_buf++) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3400 | *hs_buf = *(hs_buf + 1); |
| 3401 | } |
| 3402 | |
| 3403 | /* Create a fresh last entry */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3404 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3405 | } |
| 3406 | #endif |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 3407 | return 0; |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3408 | } |
| 3409 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3410 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3411 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 3412 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3413 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 3414 | * Bit n is set iff record number in_window_top - n has been seen. |
| 3415 | * |
| 3416 | * Usually, in_window_top is the last record number seen and the lsb of |
| 3417 | * in_window is set. The only exception is the initial state (record number 0 |
| 3418 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3419 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3420 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3422 | { |
| 3423 | ssl->in_window_top = 0; |
| 3424 | ssl->in_window = 0; |
| 3425 | } |
| 3426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3427 | static inline uint64_t ssl_load_six_bytes(unsigned char *buf) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3428 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | return ((uint64_t) buf[0] << 40) | |
| 3430 | ((uint64_t) buf[1] << 32) | |
| 3431 | ((uint64_t) buf[2] << 24) | |
| 3432 | ((uint64_t) buf[3] << 16) | |
| 3433 | ((uint64_t) buf[4] << 8) | |
| 3434 | ((uint64_t) buf[5]); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3435 | } |
| 3436 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3437 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3438 | static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr) |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3439 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3440 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3441 | unsigned char *original_in_ctr; |
| 3442 | |
| 3443 | // save original in_ctr |
| 3444 | original_in_ctr = ssl->in_ctr; |
| 3445 | |
| 3446 | // use counter from record |
| 3447 | ssl->in_ctr = record_in_ctr; |
| 3448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3449 | ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl); |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3450 | |
| 3451 | // restore the counter |
| 3452 | ssl->in_ctr = original_in_ctr; |
| 3453 | |
| 3454 | return ret; |
| 3455 | } |
| 3456 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3457 | /* |
| 3458 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 3459 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3460 | int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3461 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3462 | uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3463 | uint64_t bit; |
| 3464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { |
| 3466 | return 0; |
| 3467 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | if (rec_seqnum > ssl->in_window_top) { |
| 3470 | return 0; |
| 3471 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3472 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3473 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3475 | if (bit >= 64) { |
| 3476 | return -1; |
| 3477 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) { |
| 3480 | return -1; |
| 3481 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | return 0; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | /* |
| 3487 | * Update replay window on new validated record |
| 3488 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3489 | void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3490 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3491 | uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3493 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3494 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3495 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3496 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3497 | if (rec_seqnum > ssl->in_window_top) { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3498 | /* Update window_top and the contents of the window */ |
| 3499 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 3500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3501 | if (shift >= 64) { |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3502 | ssl->in_window = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3503 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3504 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3505 | ssl->in_window |= 1; |
| 3506 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3507 | |
| 3508 | ssl->in_window_top = rec_seqnum; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3509 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3510 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3511 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3513 | if (bit < 64) { /* Always true, but be extra sure */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3514 | ssl->in_window |= (uint64_t) 1 << bit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3515 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3516 | } |
| 3517 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3518 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3519 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3520 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3521 | /* |
Gilles Peskine | 364fd8b | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3522 | * Check if a datagram looks like a ClientHello with a valid cookie, |
| 3523 | * and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 3524 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3525 | * |
| 3526 | * - if cookie is valid, return 0 |
| 3527 | * - if ClientHello looks superficially valid but cookie is not, |
| 3528 | * fill obuf and set olen, then |
| 3529 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 3530 | * - otherwise return a specific error code |
| 3531 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3532 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 078e9bc | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 3533 | MBEDTLS_STATIC_TESTABLE |
| 3534 | int mbedtls_ssl_check_dtls_clihlo_cookie( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3535 | mbedtls_ssl_context *ssl, |
| 3536 | const unsigned char *cli_id, size_t cli_id_len, |
| 3537 | const unsigned char *in, size_t in_len, |
| 3538 | unsigned char *obuf, size_t buf_len, size_t *olen) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3539 | { |
Andrzej Kurek | cbe14ec | 2022-06-15 07:17:28 -0400 | [diff] [blame] | 3540 | size_t sid_len, cookie_len, epoch, fragment_offset; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3541 | unsigned char *p; |
| 3542 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3543 | /* |
| 3544 | * Structure of ClientHello with record and handshake headers, |
| 3545 | * and expected values. We don't need to check a lot, more checks will be |
| 3546 | * done when actually parsing the ClientHello - skipping those checks |
| 3547 | * avoids code duplication and does not make cookie forging any easier. |
| 3548 | * |
| 3549 | * 0-0 ContentType type; copied, must be handshake |
| 3550 | * 1-2 ProtocolVersion version; copied |
| 3551 | * 3-4 uint16 epoch; copied, must be 0 |
| 3552 | * 5-10 uint48 sequence_number; copied |
| 3553 | * 11-12 uint16 length; (ignored) |
| 3554 | * |
| 3555 | * 13-13 HandshakeType msg_type; (ignored) |
| 3556 | * 14-16 uint24 length; (ignored) |
| 3557 | * 17-18 uint16 message_seq; copied |
| 3558 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 3559 | * 22-24 uint24 fragment_length; (ignored) |
| 3560 | * |
| 3561 | * 25-26 ProtocolVersion client_version; (ignored) |
| 3562 | * 27-58 Random random; (ignored) |
| 3563 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 3564 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 3565 | * ... |
| 3566 | * |
| 3567 | * Minimum length is 61 bytes. |
| 3568 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3569 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u", |
| 3570 | (unsigned) in_len)); |
| 3571 | MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len); |
| 3572 | if (in_len < 61) { |
| 3573 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short")); |
| 3574 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gilles Peskine | 364fd8b | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3575 | } |
Andrzej Kurek | cbe14ec | 2022-06-15 07:17:28 -0400 | [diff] [blame] | 3576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3577 | epoch = MBEDTLS_GET_UINT16_BE(in, 3); |
| 3578 | fragment_offset = MBEDTLS_GET_UINT24_BE(in, 19); |
Andrzej Kurek | cbe14ec | 2022-06-15 07:17:28 -0400 | [diff] [blame] | 3579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || epoch != 0 || |
| 3581 | fragment_offset != 0) { |
| 3582 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello")); |
| 3583 | MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u", |
| 3584 | in[0], (unsigned) epoch, |
| 3585 | (unsigned) fragment_offset)); |
| 3586 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | sid_len = in[59]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3590 | if (59 + 1 + sid_len + 1 > in_len) { |
| 3591 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u", |
| 3592 | (unsigned) sid_len, |
| 3593 | (unsigned) in_len - 61)); |
| 3594 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gilles Peskine | 364fd8b | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3595 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3596 | MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network", |
| 3597 | in + 60, sid_len); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3598 | |
| 3599 | cookie_len = in[60 + sid_len]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | if (59 + 1 + sid_len + 1 + cookie_len > in_len) { |
| 3601 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u", |
| 3602 | (unsigned) cookie_len, |
| 3603 | (unsigned) (in_len - sid_len - 61))); |
| 3604 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Gilles Peskine | 364fd8b | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3605 | } |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3607 | MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network", |
| 3608 | in + sid_len + 61, cookie_len); |
| 3609 | if (ssl->conf->f_cookie_check(ssl->conf->p_cookie, |
| 3610 | in + sid_len + 61, cookie_len, |
| 3611 | cli_id, cli_id_len) == 0) { |
| 3612 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid")); |
| 3613 | return 0; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | /* |
| 3617 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 3618 | * |
| 3619 | * 0-0 ContentType type; copied |
| 3620 | * 1-2 ProtocolVersion version; copied |
| 3621 | * 3-4 uint16 epoch; copied |
| 3622 | * 5-10 uint48 sequence_number; copied |
| 3623 | * 11-12 uint16 length; olen - 13 |
| 3624 | * |
| 3625 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 3626 | * 14-16 uint24 length; olen - 25 |
| 3627 | * 17-18 uint16 message_seq; copied |
| 3628 | * 19-21 uint24 fragment_offset; copied |
| 3629 | * 22-24 uint24 fragment_length; olen - 25 |
| 3630 | * |
| 3631 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 3632 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 3633 | * |
| 3634 | * Minimum length is 28. |
| 3635 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3636 | if (buf_len < 28) { |
| 3637 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3638 | } |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3639 | |
| 3640 | /* Copy most fields and adapt others */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3641 | memcpy(obuf, in, 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3642 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 3643 | obuf[25] = 0xfe; |
| 3644 | obuf[26] = 0xff; |
| 3645 | |
| 3646 | /* Generate and write actual cookie */ |
| 3647 | p = obuf + 28; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3648 | if (ssl->conf->f_cookie_write(ssl->conf->p_cookie, |
| 3649 | &p, obuf + buf_len, |
| 3650 | cli_id, cli_id_len) != 0) { |
| 3651 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3652 | } |
| 3653 | |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 3654 | *olen = (size_t) (p - obuf); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3655 | |
| 3656 | /* Go back and fill length fields */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3657 | obuf[27] = (unsigned char) (*olen - 28); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3659 | obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25); |
| 3660 | obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25); |
| 3661 | obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3663 | MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3665 | return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
| 3668 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3669 | * Handle possible client reconnect with the same UDP quadruplet |
| 3670 | * (RFC 6347 Section 4.2.8). |
| 3671 | * |
| 3672 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 3673 | * that looks like a ClientHello. |
| 3674 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3675 | * - if the input looks like a ClientHello without cookies, |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3676 | * send back HelloVerifyRequest, then return 0 |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3677 | * - if the input looks like a ClientHello with a valid cookie, |
| 3678 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 3679 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3680 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3681 | * |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3682 | * This function is called (through ssl_check_client_reconnect()) when an |
| 3683 | * unexpected record is found in ssl_get_next_record(), which will discard the |
| 3684 | * record if we return 0, and bubble up the return value otherwise (this |
| 3685 | * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected |
| 3686 | * errors, and is the right thing to do in both cases). |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3687 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3688 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3690 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3691 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Matthias Schulz | 9916b06 | 2023-11-09 14:25:01 +0100 | [diff] [blame] | 3692 | size_t len = 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3694 | if (ssl->conf->f_cookie_write == NULL || |
| 3695 | ssl->conf->f_cookie_check == NULL) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3696 | /* If we can't use cookies to verify reachability of the peer, |
| 3697 | * drop the record. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3698 | MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, " |
| 3699 | "can't check reconnect validity")); |
| 3700 | return 0; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3701 | } |
| 3702 | |
Andrzej Kurek | 078e9bc | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 3703 | ret = mbedtls_ssl_check_dtls_clihlo_cookie( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3704 | ssl, |
| 3705 | ssl->cli_id, ssl->cli_id_len, |
| 3706 | ssl->in_buf, ssl->in_left, |
| 3707 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3709 | MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3710 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3711 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3712 | int send_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3713 | MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest")); |
| 3714 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", |
| 3715 | ssl->out_buf, len); |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3716 | /* Don't check write errors as we can't do anything here. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3717 | * If the error is permanent we'll catch it later, |
| 3718 | * if it's not, then hopefully it'll work next time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3719 | send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len); |
| 3720 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret); |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3721 | (void) send_ret; |
| 3722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3723 | return 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3724 | } |
| 3725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3726 | if (ret == 0) { |
| 3727 | MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context")); |
| 3728 | if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) { |
| 3729 | MBEDTLS_SSL_DEBUG_RET(1, "reset", ret); |
| 3730 | return ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3731 | } |
| 3732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3733 | return MBEDTLS_ERR_SSL_CLIENT_RECONNECT; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3734 | } |
| 3735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3736 | return ret; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3737 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3738 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3739 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3740 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3741 | static int ssl_check_record_type(uint8_t record_type) |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3742 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3744 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 3745 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3746 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 3747 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3750 | return 0; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3751 | } |
| 3752 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3753 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3754 | * ContentType type; |
| 3755 | * ProtocolVersion version; |
| 3756 | * uint16 epoch; // DTLS only |
| 3757 | * uint48 sequence_number; // DTLS only |
| 3758 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3759 | * |
| 3760 | * Return 0 if header looks sane (and, for DTLS, the record is expected) |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3761 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3762 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 3763 | * |
| 3764 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3765 | * 1. proceed with the record if this function returns 0 |
| 3766 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 3767 | * 3. return CLIENT_RECONNECT if this function return that value |
| 3768 | * 4. drop the whole datagram if this function returns anything else. |
| 3769 | * Point 2 is needed when the peer is resending, and we have already received |
| 3770 | * the first record from a datagram but are still waiting for the others. |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3771 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3772 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 3774 | unsigned char *buf, |
| 3775 | size_t len, |
| 3776 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3777 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 3778 | mbedtls_ssl_protocol_version tls_version; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3779 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3780 | size_t const rec_hdr_type_offset = 0; |
| 3781 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3782 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3783 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 3784 | rec_hdr_type_len; |
| 3785 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3786 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3787 | size_t const rec_hdr_ctr_len = 8; |
| 3788 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3789 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3790 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 3791 | rec_hdr_version_len; |
| 3792 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3793 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3794 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 3795 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3796 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3797 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3798 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3799 | |
| 3800 | size_t rec_hdr_len_offset; /* To be determined */ |
| 3801 | size_t const rec_hdr_len_len = 2; |
| 3802 | |
| 3803 | /* |
| 3804 | * Check minimum lengths for record header. |
| 3805 | */ |
| 3806 | |
| 3807 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3808 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3809 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3810 | } else |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3811 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3812 | { |
| 3813 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 3814 | } |
| 3815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3816 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3817 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3818 | ( |
| 3819 | "datagram of length %u too small to hold DTLS record header of length %u", |
| 3820 | (unsigned) len, |
| 3821 | (unsigned) (rec_hdr_len_len + rec_hdr_len_len))); |
| 3822 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * Parse and validate record content type |
| 3827 | */ |
| 3828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3829 | rec->type = buf[rec_hdr_type_offset]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3830 | |
| 3831 | /* Check record content type */ |
| 3832 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3833 | rec->cid_len = 0; |
| 3834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3835 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3836 | ssl->conf->cid_len != 0 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3837 | rec->type == MBEDTLS_SSL_MSG_CID) { |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3838 | /* Shift pointers to account for record header including CID |
| 3839 | * struct { |
Hannes Tschofenig | fd6cca4 | 2021-10-12 09:22:33 +0200 | [diff] [blame] | 3840 | * ContentType outer_type = tls12_cid; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3841 | * ProtocolVersion version; |
| 3842 | * uint16 epoch; |
| 3843 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 3844 | * opaque cid[cid_length]; // Additional field compared to |
| 3845 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3846 | * uint16 length; |
| 3847 | * opaque enc_content[DTLSCiphertext.length]; |
| 3848 | * } DTLSCiphertext; |
| 3849 | */ |
| 3850 | |
| 3851 | /* So far, we only support static CID lengths |
| 3852 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3853 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 3854 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3856 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3857 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3858 | ( |
| 3859 | "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 3860 | (unsigned) len, |
| 3861 | (unsigned) (rec_hdr_len_offset + rec_hdr_len_len))); |
| 3862 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3863 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3864 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 3865 | /* configured CID len is guaranteed at most 255, see |
| 3866 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 3867 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3868 | memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len); |
| 3869 | } else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3870 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3871 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3872 | if (ssl_check_record_type(rec->type)) { |
| 3873 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u", |
| 3874 | (unsigned) rec->type)); |
| 3875 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3876 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3877 | } |
| 3878 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3879 | /* |
| 3880 | * Parse and validate record version |
| 3881 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3882 | rec->ver[0] = buf[rec_hdr_version_offset + 0]; |
| 3883 | rec->ver[1] = buf[rec_hdr_version_offset + 1]; |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 3884 | tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version( |
| 3885 | buf + rec_hdr_version_offset, |
| 3886 | ssl->conf->transport); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3887 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3888 | if (tls_version > ssl->conf->max_tls_version) { |
| 3889 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS version mismatch: got %u, expected max %u", |
| 3890 | (unsigned) tls_version, |
| 3891 | (unsigned) ssl->conf->max_tls_version)); |
Gilles Peskine | 364fd8b | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3893 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3894 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3895 | /* |
| 3896 | * Parse/Copy record sequence number. |
| 3897 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3898 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3899 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3900 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3901 | /* Copy explicit record sequence number from input buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3902 | memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 3903 | rec_hdr_ctr_len); |
| 3904 | } else |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3905 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3906 | { |
| 3907 | /* Copy implicit record sequence number from SSL context structure. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3908 | memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3909 | } |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3910 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3911 | /* |
| 3912 | * Parse record length. |
| 3913 | */ |
| 3914 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3915 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3916 | rec->data_len = MBEDTLS_GET_UINT16_BE(buf, rec_hdr_len_offset); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3917 | MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3919 | MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, " |
| 3920 | "version = [0x%x], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 3921 | rec->type, (unsigned) tls_version, rec->data_len)); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3922 | |
| 3923 | rec->buf = buf; |
| 3924 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3926 | if (rec->data_len == 0) { |
| 3927 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 3928 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3929 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3930 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 3931 | * DTLS-related tests. |
| 3932 | * Check epoch before checking length constraint because |
| 3933 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 3934 | * message gets duplicated before the corresponding Finished message, |
| 3935 | * the second ChangeCipherSpec should be discarded because it belongs |
| 3936 | * to an old epoch, but not because its length is shorter than |
| 3937 | * the minimum record length for packets using the new record transform. |
| 3938 | * Note that these two kinds of failures are handled differently, |
| 3939 | * as an unexpected record is silently skipped but an invalid |
| 3940 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3941 | */ |
| 3942 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3943 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3944 | rec_epoch = MBEDTLS_GET_UINT16_BE(rec->ctr, 0); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3945 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3946 | /* Check that the datagram is large enough to contain a record |
| 3947 | * of the advertised length. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | if (len < rec->data_offset + rec->data_len) { |
| 3949 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3950 | ( |
| 3951 | "Datagram of length %u too small to contain record of advertised length %u.", |
| 3952 | (unsigned) len, |
| 3953 | (unsigned) (rec->data_offset + rec->data_len))); |
| 3954 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3955 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3956 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3957 | /* Records from other, non-matching epochs are silently discarded. |
| 3958 | * (The case of same-port Client reconnects must be considered in |
| 3959 | * the caller). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3960 | if (rec_epoch != ssl->in_epoch) { |
| 3961 | MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: " |
| 3962 | "expected %u, received %lu", |
| 3963 | ssl->in_epoch, (unsigned long) rec_epoch)); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3964 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 3965 | /* Records from the next epoch are considered for buffering |
| 3966 | * (concretely: early Finished messages). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | if (rec_epoch == (unsigned) ssl->in_epoch + 1) { |
| 3968 | MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering")); |
| 3969 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3970 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3971 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3973 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3974 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3975 | /* For records from the correct epoch, check whether their |
| 3976 | * sequence number has been seen before. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3977 | else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl, |
| 3978 | &rec->ctr[0]) != 0) { |
| 3979 | MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record")); |
| 3980 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3981 | } |
| 3982 | #endif |
| 3983 | } |
| 3984 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3985 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3986 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3987 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3988 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3989 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3990 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 3991 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3993 | { |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 3994 | unsigned int rec_epoch = MBEDTLS_GET_UINT16_BE(ssl->in_ctr, 0); |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3995 | |
| 3996 | /* |
| 3997 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 3998 | * access the first byte of record content (handshake type), as we |
| 3999 | * have an active transform (possibly iv_len != 0), so use the |
| 4000 | * fact that the record header len is 13 instead. |
| 4001 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | if (rec_epoch == 0 && |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4003 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4004 | mbedtls_ssl_is_handshake_over(ssl) == 1 && |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4005 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4006 | ssl->in_left > 13 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4007 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 4008 | MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect " |
| 4009 | "from the same port")); |
| 4010 | return ssl_handle_possible_reconnect(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4013 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4014 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4015 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4016 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4017 | /* |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4018 | * If applicable, decrypt record content |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4019 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4020 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4021 | static int ssl_prepare_record_content(mbedtls_ssl_context *ssl, |
| 4022 | mbedtls_record *rec) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4023 | { |
| 4024 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 4025 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | MBEDTLS_SSL_DEBUG_BUF(4, "input record from network", |
| 4027 | rec->buf, rec->buf_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4028 | |
Ronald Cron | 7e38cba | 2021-11-24 12:43:39 +0100 | [diff] [blame] | 4029 | /* |
| 4030 | * In TLS 1.3, always treat ChangeCipherSpec records |
| 4031 | * as unencrypted. The only thing we do with them is |
| 4032 | * check the length and content and ignore them. |
| 4033 | */ |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4034 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | if (ssl->transform_in != NULL && |
| 4036 | ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4037 | if (rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
Ronald Cron | 7e38cba | 2021-11-24 12:43:39 +0100 | [diff] [blame] | 4038 | done = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4039 | } |
Ronald Cron | 7e38cba | 2021-11-24 12:43:39 +0100 | [diff] [blame] | 4040 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4041 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Ronald Cron | 7e38cba | 2021-11-24 12:43:39 +0100 | [diff] [blame] | 4042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4043 | if (!done && ssl->transform_in != NULL) { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 4044 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4046 | if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, |
| 4047 | rec)) != 0) { |
| 4048 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4049 | |
Ronald Cron | 2995d35 | 2024-01-18 16:59:39 +0100 | [diff] [blame] | 4050 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C) |
| 4051 | /* |
| 4052 | * Although the server rejected early data, it might receive early |
| 4053 | * data as long as it has not received the client Finished message. |
| 4054 | * It is encrypted with early keys and should be ignored as stated |
| 4055 | * in section 4.2.10 of RFC 8446: |
| 4056 | * |
| 4057 | * "Ignore the extension and return a regular 1-RTT response. The |
| 4058 | * server then skips past early data by attempting to deprotect |
| 4059 | * received records using the handshake traffic key, discarding |
| 4060 | * records which fail deprotection (up to the configured |
| 4061 | * max_early_data_size). Once a record is deprotected successfully, |
| 4062 | * it is treated as the start of the client's second flight and the |
| 4063 | * server proceeds as with an ordinary 1-RTT handshake." |
| 4064 | */ |
| 4065 | if ((old_msg_type == MBEDTLS_SSL_MSG_APPLICATION_DATA) && |
| 4066 | (ssl->discard_early_data_record == |
| 4067 | MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD)) { |
| 4068 | MBEDTLS_SSL_DEBUG_MSG( |
| 4069 | 3, ("EarlyData: deprotect and discard app data records.")); |
Ronald Cron | 919e596 | 2024-02-08 15:48:29 +0100 | [diff] [blame] | 4070 | |
| 4071 | ret = mbedtls_ssl_tls13_check_early_data_len(ssl, rec->data_len); |
| 4072 | if (ret != 0) { |
| 4073 | return ret; |
| 4074 | } |
Ronald Cron | 2995d35 | 2024-01-18 16:59:39 +0100 | [diff] [blame] | 4075 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4076 | } |
| 4077 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */ |
| 4078 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4079 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4081 | ssl->conf->ignore_unexpected_cid |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4082 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 4083 | MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID")); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 4084 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 4085 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4086 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 4087 | |
Ronald Cron | 71c6e65 | 2024-02-05 16:48:10 +0100 | [diff] [blame] | 4088 | /* |
| 4089 | * The decryption of the record failed, no reason to ignore it, |
| 4090 | * return in error with the decryption error code. |
| 4091 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Ronald Cron | 2995d35 | 2024-01-18 16:59:39 +0100 | [diff] [blame] | 4095 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C) |
| 4096 | /* |
| 4097 | * If the server were discarding protected records that it fails to |
| 4098 | * deprotect because it has rejected early data, as we have just |
| 4099 | * deprotected successfully a record, the server has to resume normal |
| 4100 | * operation and fail the connection if the deprotection of a record |
| 4101 | * fails. |
| 4102 | */ |
| 4103 | if (ssl->discard_early_data_record == |
| 4104 | MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD) { |
| 4105 | ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD; |
| 4106 | } |
| 4107 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */ |
| 4108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4109 | if (old_msg_type != rec->type) { |
| 4110 | MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d", |
| 4111 | old_msg_type, rec->type)); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4112 | } |
| 4113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4114 | MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt", |
| 4115 | rec->buf + rec->data_offset, rec->data_len); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 4116 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4117 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4118 | /* We have already checked the record content type |
| 4119 | * in ssl_parse_record_header(), failing or silently |
| 4120 | * dropping the record in the case of an unknown type. |
| 4121 | * |
| 4122 | * Since with the use of CIDs, the record content type |
| 4123 | * might change during decryption, re-check the record |
| 4124 | * content type, but treat a failure as fatal this time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4125 | if (ssl_check_record_type(rec->type)) { |
| 4126 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type")); |
| 4127 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4128 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4129 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 4130 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4131 | if (rec->data_len == 0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4132 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 |
| 4134 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4135 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4136 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype)); |
| 4137 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4138 | } |
| 4139 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4140 | |
| 4141 | ssl->nb_zero++; |
| 4142 | |
| 4143 | /* |
| 4144 | * Three or more empty messages may be a DoS attack |
| 4145 | * (excessive CPU consumption). |
| 4146 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4147 | if (ssl->nb_zero > 3) { |
| 4148 | MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty " |
| 4149 | "messages, possible DoS attack")); |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 4150 | /* Treat the records as if they were not properly authenticated, |
| 4151 | * thereby failing the connection if we see more than allowed |
| 4152 | * by the configured bad MAC threshold. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4154 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4155 | } else { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4156 | ssl->nb_zero = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4157 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4158 | |
| 4159 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4161 | ; /* in_ctr read from peer, not maintained internally */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4162 | } else |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4163 | #endif |
| 4164 | { |
| 4165 | unsigned i; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | for (i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
| 4167 | i > mbedtls_ssl_ep_len(ssl); i--) { |
| 4168 | if (++ssl->in_ctr[i - 1] != 0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4169 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4170 | } |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 4171 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4172 | |
| 4173 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4174 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 4175 | MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap")); |
| 4176 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 4177 | } |
| 4178 | } |
| 4179 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4180 | } |
| 4181 | |
Jerry Yu | f57d14b | 2023-11-15 16:40:09 +0800 | [diff] [blame] | 4182 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C) |
| 4183 | /* |
| 4184 | * Although the server rejected early data because it needed to send an |
| 4185 | * HelloRetryRequest message, it might receive early data as long as it has |
| 4186 | * not received the client Finished message. |
| 4187 | * The early data is encrypted with early keys and should be ignored as |
| 4188 | * stated in section 4.2.10 of RFC 8446 (second case): |
| 4189 | * |
| 4190 | * "The server then ignores early data by skipping all records with an |
| 4191 | * external content type of "application_data" (indicating that they are |
| 4192 | * encrypted), up to the configured max_early_data_size. Ignore application |
| 4193 | * data message before 2nd ClientHello when early_data was received in 1st |
| 4194 | * ClientHello." |
| 4195 | */ |
| 4196 | if (ssl->discard_early_data_record == MBEDTLS_SSL_EARLY_DATA_DISCARD) { |
| 4197 | if (rec->type == MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Ronald Cron | 01d273d | 2024-02-09 16:17:10 +0100 | [diff] [blame] | 4198 | |
| 4199 | ret = mbedtls_ssl_tls13_check_early_data_len(ssl, rec->data_len); |
| 4200 | if (ret != 0) { |
| 4201 | return ret; |
| 4202 | } |
| 4203 | |
Jerry Yu | f57d14b | 2023-11-15 16:40:09 +0800 | [diff] [blame] | 4204 | MBEDTLS_SSL_DEBUG_MSG( |
| 4205 | 3, ("EarlyData: Ignore application message before 2nd ClientHello")); |
Ronald Cron | db944a7 | 2024-03-08 11:32:53 +0100 | [diff] [blame] | 4206 | |
Jerry Yu | f57d14b | 2023-11-15 16:40:09 +0800 | [diff] [blame] | 4207 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4208 | } else if (rec->type == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4209 | ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD; |
| 4210 | } |
| 4211 | } |
| 4212 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */ |
| 4213 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4214 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4216 | mbedtls_ssl_dtls_replay_update(ssl); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 4217 | } |
| 4218 | #endif |
| 4219 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 4220 | /* Check actual (decrypted) record content length against |
| 4221 | * configured maximum. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 4223 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length")); |
| 4224 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 4225 | } |
| 4226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4227 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4228 | } |
| 4229 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4230 | /* |
| 4231 | * Read a record. |
| 4232 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4233 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 4234 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 4235 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4236 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4237 | |
| 4238 | /* Helper functions for mbedtls_ssl_read_record(). */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4239 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4240 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4241 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | static int ssl_get_next_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4243 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4244 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 4245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4246 | int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl, |
| 4247 | unsigned update_hs_digest) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4248 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4249 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4251 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record")); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4253 | if (ssl->keep_current_message == 0) { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4254 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4256 | ret = ssl_consume_current_message(ssl); |
| 4257 | if (ret != 0) { |
| 4258 | return ret; |
| 4259 | } |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 4260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4261 | if (ssl_record_is_in_progress(ssl) == 0) { |
David Horstmann | 10be134 | 2022-10-06 18:31:25 +0100 | [diff] [blame] | 4262 | int dtls_have_buffered = 0; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4263 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4264 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4265 | /* We only check for buffered messages if the |
| 4266 | * current datagram is fully consumed. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4267 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4268 | ssl_next_record_is_in_datagram(ssl) == 0) { |
| 4269 | if (ssl_load_buffered_message(ssl) == 0) { |
David Horstmann | 10be134 | 2022-10-06 18:31:25 +0100 | [diff] [blame] | 4270 | dtls_have_buffered = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4271 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4272 | } |
| 4273 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4274 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | if (dtls_have_buffered == 0) { |
| 4276 | ret = ssl_get_next_record(ssl); |
| 4277 | if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4278 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4279 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4281 | if (ret != 0) { |
| 4282 | MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret); |
| 4283 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4284 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4285 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4286 | } |
| 4287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4288 | ret = mbedtls_ssl_handle_message_type(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4289 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4290 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4291 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4292 | /* Buffer future message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4293 | ret = ssl_buffer_message(ssl); |
| 4294 | if (ret != 0) { |
| 4295 | return ret; |
| 4296 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4297 | |
| 4298 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4299 | } |
| 4300 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4302 | } while (MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 4303 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4305 | if (0 != ret) { |
| 4306 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret); |
| 4307 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4308 | } |
| 4309 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4310 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 4311 | update_hs_digest == 1) { |
Manuel Pégourié-Gonnard | b8b07aa | 2023-02-06 00:34:21 +0100 | [diff] [blame] | 4312 | ret = mbedtls_ssl_update_handshake_status(ssl); |
| 4313 | if (0 != ret) { |
| 4314 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret); |
| 4315 | return ret; |
| 4316 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4317 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4318 | } else { |
| 4319 | MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message")); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 4320 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4321 | } |
| 4322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4323 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record")); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4325 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4326 | } |
| 4327 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4328 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4329 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4330 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4331 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | if (ssl->in_left > ssl->next_record_offset) { |
| 4333 | return 1; |
| 4334 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4335 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4336 | return 0; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4337 | } |
| 4338 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4339 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4341 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4342 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4343 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4344 | int ret = 0; |
| 4345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4346 | if (hs == NULL) { |
| 4347 | return -1; |
| 4348 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message")); |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 4351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 4353 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4354 | /* Check if we have seen a ChangeCipherSpec before. |
| 4355 | * If yes, synthesize a CCS record. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | if (!hs->buffering.seen_ccs) { |
| 4357 | MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4358 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 4359 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4360 | } |
| 4361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4362 | MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4363 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 4364 | ssl->in_msglen = 1; |
| 4365 | ssl->in_msg[0] = 1; |
| 4366 | |
| 4367 | /* As long as they are equal, the exact value doesn't matter. */ |
| 4368 | ssl->in_left = 0; |
| 4369 | ssl->next_record_offset = 0; |
| 4370 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4371 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4372 | goto exit; |
| 4373 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4374 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 4375 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4376 | /* Debug only */ |
| 4377 | { |
| 4378 | unsigned offset; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4379 | for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4380 | hs_buf = &hs->buffering.hs[offset]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4381 | if (hs_buf->is_valid == 1) { |
| 4382 | MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.", |
| 4383 | hs->in_msg_seq + offset, |
| 4384 | hs_buf->is_complete ? "fully" : "partially")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4385 | } |
| 4386 | } |
| 4387 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 4388 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4389 | |
| 4390 | /* Check if we have buffered and/or fully reassembled the |
| 4391 | * next handshake message. */ |
| 4392 | hs_buf = &hs->buffering.hs[0]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4393 | if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4394 | /* Synthesize a record containing the buffered HS message. */ |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 4395 | size_t msg_len = MBEDTLS_GET_UINT24_BE(hs_buf->data, 1); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4396 | |
| 4397 | /* Double-check that we haven't accidentally buffered |
| 4398 | * a message that doesn't fit into the input buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 4400 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4401 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4402 | } |
| 4403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4404 | MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load")); |
| 4405 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)", |
| 4406 | hs_buf->data, msg_len + 12); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4407 | |
| 4408 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4409 | ssl->in_hslen = msg_len + 12; |
| 4410 | ssl->in_msglen = msg_len + 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4411 | memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4412 | |
| 4413 | ret = 0; |
| 4414 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4415 | } else { |
| 4416 | MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered", |
| 4417 | hs->in_msg_seq)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4420 | ret = -1; |
| 4421 | |
| 4422 | exit: |
| 4423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4424 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message")); |
| 4425 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4428 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4429 | static int ssl_buffer_make_space(mbedtls_ssl_context *ssl, |
| 4430 | size_t desired) |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4431 | { |
| 4432 | int offset; |
| 4433 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4434 | MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available", |
| 4435 | (unsigned) desired)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4436 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4437 | /* Get rid of future records epoch first, if such exist. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | ssl_free_buffered_record(ssl); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4439 | |
| 4440 | /* Check if we have enough space available now. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4442 | hs->buffering.total_bytes_buffered)) { |
| 4443 | MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record")); |
| 4444 | return 0; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4445 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4446 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 4447 | /* We don't have enough space to buffer the next expected handshake |
| 4448 | * message. Remove buffers used for future messages to gain space, |
| 4449 | * starting with the most distant one. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 4451 | offset >= 0; offset--) { |
| 4452 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4453 | ( |
| 4454 | "Free buffering slot %d to make space for reassembly of next handshake message", |
| 4455 | offset)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | ssl_buffering_free_slot(ssl, (uint8_t) offset); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4458 | |
| 4459 | /* Check if we have enough space available now. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4460 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4461 | hs->buffering.total_bytes_buffered)) { |
| 4462 | MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages")); |
| 4463 | return 0; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4464 | } |
| 4465 | } |
| 4466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | return -1; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4468 | } |
| 4469 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4470 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | static int ssl_buffer_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4472 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4473 | int ret = 0; |
| 4474 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4476 | if (hs == NULL) { |
| 4477 | return 0; |
| 4478 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4480 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4482 | switch (ssl->in_msgtype) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4483 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message")); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4485 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4486 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4487 | break; |
| 4488 | |
| 4489 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4490 | { |
| 4491 | unsigned recv_msg_seq_offset; |
Dave Rodgman | a3d0f61 | 2023-11-03 23:34:02 +0000 | [diff] [blame] | 4492 | unsigned recv_msg_seq = MBEDTLS_GET_UINT16_BE(ssl->in_msg, 4); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4493 | mbedtls_ssl_hs_buffer *hs_buf; |
| 4494 | size_t msg_len = ssl->in_hslen - 12; |
| 4495 | |
| 4496 | /* We should never receive an old handshake |
| 4497 | * message - double-check nonetheless. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4498 | if (recv_msg_seq < ssl->handshake->in_msg_seq) { |
| 4499 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4500 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4504 | if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4505 | /* Silently ignore -- message too far in the future */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4506 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4507 | ("Ignore future HS message with sequence number %u, " |
| 4508 | "buffering window %u - %u", |
| 4509 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 4510 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - |
| 4511 | 1)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4512 | |
| 4513 | goto exit; |
| 4514 | } |
| 4515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4516 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ", |
| 4517 | recv_msg_seq, recv_msg_seq_offset)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | hs_buf = &hs->buffering.hs[recv_msg_seq_offset]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4520 | |
| 4521 | /* Check if the buffering for this seq nr has already commenced. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | if (!hs_buf->is_valid) { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4523 | size_t reassembly_buf_sz; |
| 4524 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4525 | hs_buf->is_fragmented = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4526 | (ssl_hs_is_proper_fragment(ssl) == 1); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4527 | |
| 4528 | /* We copy the message back into the input buffer |
| 4529 | * after reassembly, so check that it's not too large. |
| 4530 | * This is an implementation-specific limitation |
| 4531 | * and not one from the standard, hence it is not |
| 4532 | * checked in ssl_check_hs_header(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4533 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4534 | /* Ignore message */ |
| 4535 | goto exit; |
| 4536 | } |
| 4537 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4538 | /* Check if we have enough space to buffer the message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | if (hs->buffering.total_bytes_buffered > |
| 4540 | MBEDTLS_SSL_DTLS_MAX_BUFFERING) { |
| 4541 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4542 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4543 | } |
| 4544 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4545 | reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len, |
| 4546 | hs_buf->is_fragmented); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4549 | hs->buffering.total_bytes_buffered)) { |
| 4550 | if (recv_msg_seq_offset > 0) { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4551 | /* If we can't buffer a future message because |
| 4552 | * of space limitations -- ignore. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4553 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4554 | ("Buffering of future message of size %" |
| 4555 | MBEDTLS_PRINTF_SIZET |
| 4556 | " would exceed the compile-time limit %" |
| 4557 | MBEDTLS_PRINTF_SIZET |
| 4558 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4559 | " bytes buffered) -- ignore\n", |
| 4560 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4561 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4562 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4563 | } else { |
| 4564 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4565 | ("Buffering of future message of size %" |
| 4566 | MBEDTLS_PRINTF_SIZET |
| 4567 | " would exceed the compile-time limit %" |
| 4568 | MBEDTLS_PRINTF_SIZET |
| 4569 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4570 | " bytes buffered) -- attempt to make space by freeing buffered future messages\n", |
| 4571 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4572 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 4573 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) { |
| 4576 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4577 | ("Reassembly of next message of size %" |
| 4578 | MBEDTLS_PRINTF_SIZET |
| 4579 | " (%" MBEDTLS_PRINTF_SIZET |
| 4580 | " with bitmap) would exceed" |
| 4581 | " the compile-time limit %" |
| 4582 | MBEDTLS_PRINTF_SIZET |
| 4583 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4584 | " bytes buffered) -- fail\n", |
| 4585 | msg_len, |
| 4586 | reassembly_buf_sz, |
| 4587 | (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4588 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4589 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4590 | goto exit; |
| 4591 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4592 | } |
| 4593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4594 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4595 | ("initialize reassembly, total length = %" |
| 4596 | MBEDTLS_PRINTF_SIZET, |
| 4597 | msg_len)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4599 | hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz); |
| 4600 | if (hs_buf->data == NULL) { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4601 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4602 | goto exit; |
| 4603 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4604 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* Prepare final header: copy msg_type, length and message_seq, |
| 4607 | * then add standardised fragment_offset and fragment_length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | memcpy(hs_buf->data, ssl->in_msg, 6); |
| 4609 | memset(hs_buf->data + 6, 0, 3); |
| 4610 | memcpy(hs_buf->data + 9, hs_buf->data + 1, 3); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4611 | |
| 4612 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4613 | |
| 4614 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4615 | } else { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4616 | /* Make sure msg_type and length are consistent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4617 | if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) { |
| 4618 | MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4619 | /* Ignore */ |
| 4620 | goto exit; |
| 4621 | } |
| 4622 | } |
| 4623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | if (!hs_buf->is_complete) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4625 | size_t frag_len, frag_off; |
| 4626 | unsigned char * const msg = hs_buf->data + 12; |
| 4627 | |
| 4628 | /* |
| 4629 | * Check and copy current fragment |
| 4630 | */ |
| 4631 | |
| 4632 | /* Validation of header fields already done in |
| 4633 | * mbedtls_ssl_prepare_handshake_record(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4634 | frag_off = ssl_get_hs_frag_off(ssl); |
| 4635 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4637 | MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET |
| 4638 | ", length = %" MBEDTLS_PRINTF_SIZET, |
| 4639 | frag_off, frag_len)); |
| 4640 | memcpy(msg + frag_off, ssl->in_msg + 12, frag_len); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4642 | if (hs_buf->is_fragmented) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4643 | unsigned char * const bitmask = msg + msg_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4644 | ssl_bitmask_set(bitmask, frag_off, frag_len); |
| 4645 | hs_buf->is_complete = (ssl_bitmask_check(bitmask, |
| 4646 | msg_len) == 0); |
| 4647 | } else { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4648 | hs_buf->is_complete = 1; |
| 4649 | } |
| 4650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4651 | MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete", |
| 4652 | hs_buf->is_complete ? "" : "not yet ")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4653 | } |
| 4654 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4655 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4656 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4657 | |
| 4658 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 4659 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4660 | break; |
| 4661 | } |
| 4662 | |
| 4663 | exit: |
| 4664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4665 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message")); |
| 4666 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4667 | } |
| 4668 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4669 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4670 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4671 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4672 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4673 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4674 | * Consume last content-layer message and potentially |
| 4675 | * update in_msglen which keeps track of the contents' |
| 4676 | * consumption state. |
| 4677 | * |
| 4678 | * (1) Handshake messages: |
| 4679 | * Remove last handshake message, move content |
| 4680 | * and adapt in_msglen. |
| 4681 | * |
| 4682 | * (2) Alert messages: |
| 4683 | * Consume whole record content, in_msglen = 0. |
| 4684 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4685 | * (3) Change cipher spec: |
| 4686 | * Consume whole record content, in_msglen = 0. |
| 4687 | * |
| 4688 | * (4) Application data: |
| 4689 | * Don't do anything - the record layer provides |
| 4690 | * the application data as a stream transport |
| 4691 | * and consumes through mbedtls_ssl_read only. |
| 4692 | * |
| 4693 | */ |
| 4694 | |
| 4695 | /* Case (1): Handshake messages */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4696 | if (ssl->in_hslen != 0) { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4697 | /* Hard assertion to be sure that no application data |
| 4698 | * is in flight, as corrupting ssl->in_msglen during |
| 4699 | * ssl->in_offt != NULL is fatal. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | if (ssl->in_offt != NULL) { |
| 4701 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4702 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4703 | } |
| 4704 | |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 4705 | if (ssl->badmac_seen_or_in_hsfraglen != 0) { |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 4706 | /* Not all handshake fragments have arrived, do not consume. */ |
| 4707 | MBEDTLS_SSL_DEBUG_MSG(3, |
Gilles Peskine | ebdd405 | 2025-02-17 16:25:24 +0100 | [diff] [blame] | 4708 | ("waiting for more fragments (%u of %" |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 4709 | MBEDTLS_PRINTF_SIZET ", %" MBEDTLS_PRINTF_SIZET " left)", |
Gilles Peskine | b710599 | 2025-02-17 16:28:51 +0100 | [diff] [blame] | 4710 | ssl->badmac_seen_or_in_hsfraglen, ssl->in_hslen, |
| 4711 | ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen)); |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 4712 | return 0; |
| 4713 | } |
| 4714 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4715 | /* |
| 4716 | * Get next Handshake message in the current record |
| 4717 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4718 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4719 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4720 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4721 | * current handshake content: If DTLS handshake |
| 4722 | * fragmentation is used, that's the fragment |
| 4723 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4724 | * size here is faulty and should be changed at |
| 4725 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4726 | * (2) While it doesn't seem to cause problems, one |
| 4727 | * has to be very careful not to assume that in_hslen |
| 4728 | * is always <= in_msglen in a sensible communication. |
| 4729 | * Again, it's wrong for DTLS handshake fragmentation. |
| 4730 | * The following check is therefore mandatory, and |
| 4731 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4732 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 4733 | * bounds after handling a DTLS message with an unexpected |
| 4734 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4735 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4736 | if (ssl->in_hslen < ssl->in_msglen) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4737 | ssl->in_msglen -= ssl->in_hslen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4738 | memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 4739 | ssl->in_msglen); |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 4740 | MBEDTLS_PUT_UINT16_BE(ssl->in_msglen, ssl->in_len, 0); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4742 | MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record", |
| 4743 | ssl->in_msg, ssl->in_msglen); |
| 4744 | } else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4745 | ssl->in_msglen = 0; |
| 4746 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 4747 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4748 | ssl->in_hslen = 0; |
| 4749 | } |
| 4750 | /* Case (4): Application data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4751 | else if (ssl->in_offt != NULL) { |
| 4752 | return 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4753 | } |
| 4754 | /* Everything else (CCS & Alerts) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4755 | else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4756 | ssl->in_msglen = 0; |
| 4757 | } |
| 4758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4759 | return 0; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4760 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4761 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4762 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4764 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4765 | if (ssl->in_msglen > 0) { |
| 4766 | return 1; |
| 4767 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4769 | return 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4770 | } |
| 4771 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4772 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4775 | { |
| 4776 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4777 | if (hs == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4778 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4779 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | if (hs->buffering.future_record.data != NULL) { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4782 | hs->buffering.total_bytes_buffered -= |
| 4783 | hs->buffering.future_record.len; |
| 4784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4785 | mbedtls_free(hs->buffering.future_record.data); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4786 | hs->buffering.future_record.data = NULL; |
| 4787 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4788 | } |
| 4789 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4790 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4791 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4792 | { |
| 4793 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4794 | unsigned char *rec; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4795 | size_t rec_len; |
| 4796 | unsigned rec_epoch; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4797 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4798 | size_t in_buf_len = ssl->in_buf_len; |
| 4799 | #else |
| 4800 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4801 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4802 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4803 | return 0; |
| 4804 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | if (hs == NULL) { |
| 4807 | return 0; |
| 4808 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4809 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4810 | rec = hs->buffering.future_record.data; |
| 4811 | rec_len = hs->buffering.future_record.len; |
| 4812 | rec_epoch = hs->buffering.future_record.epoch; |
| 4813 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4814 | if (rec == NULL) { |
| 4815 | return 0; |
| 4816 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4817 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4818 | /* Only consider loading future records if the |
| 4819 | * input buffer is empty. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4820 | if (ssl_next_record_is_in_datagram(ssl) == 1) { |
| 4821 | return 0; |
| 4822 | } |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4826 | if (rec_epoch != ssl->in_epoch) { |
| 4827 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch.")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4828 | goto exit; |
| 4829 | } |
| 4830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4832 | |
| 4833 | /* Double-check that the record is not too large */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4834 | if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { |
| 4835 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4836 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | memcpy(ssl->in_hdr, rec, rec_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4840 | ssl->in_left = rec_len; |
| 4841 | ssl->next_record_offset = 0; |
| 4842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | ssl_free_buffered_record(ssl); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4844 | |
| 4845 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4846 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record")); |
| 4847 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4848 | } |
| 4849 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4850 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4851 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 4852 | mbedtls_record const *rec) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4853 | { |
| 4854 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4855 | |
| 4856 | /* Don't buffer future records outside handshakes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | if (hs == NULL) { |
| 4858 | return 0; |
| 4859 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4860 | |
| 4861 | /* Only buffer handshake records (we are only interested |
| 4862 | * in Finished messages). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4864 | return 0; |
| 4865 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4866 | |
| 4867 | /* Don't buffer more than one future epoch record. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | if (hs->buffering.future_record.data != NULL) { |
| 4869 | return 0; |
| 4870 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4871 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4872 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4874 | hs->buffering.total_bytes_buffered)) { |
| 4875 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET |
| 4876 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 4877 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4878 | " bytes buffered) -- ignore\n", |
| 4879 | rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4880 | hs->buffering.total_bytes_buffered)); |
| 4881 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4882 | } |
| 4883 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4884 | /* Buffer record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u", |
| 4886 | ssl->in_epoch + 1U)); |
| 4887 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4888 | |
| 4889 | /* ssl_parse_record_header() only considers records |
| 4890 | * of the next epoch as candidates for buffering. */ |
| 4891 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4892 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4893 | |
| 4894 | hs->buffering.future_record.data = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | mbedtls_calloc(1, hs->buffering.future_record.len); |
| 4896 | if (hs->buffering.future_record.data == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4897 | /* If we run out of RAM trying to buffer a |
| 4898 | * record from the next epoch, just ignore. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4900 | } |
| 4901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4902 | memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4903 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4904 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4905 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4909 | |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 4910 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | static int ssl_get_next_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4912 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4913 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4914 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4915 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4916 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4917 | /* We might have buffered a future record; if so, |
| 4918 | * and if the epoch matches now, load it. |
| 4919 | * On success, this call will set ssl->in_left to |
| 4920 | * the length of the buffered record, so that |
| 4921 | * the calls to ssl_fetch_input() below will |
| 4922 | * essentially be no-ops. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | ret = ssl_load_buffered_record(ssl); |
| 4924 | if (ret != 0) { |
| 4925 | return ret; |
| 4926 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4927 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4928 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4929 | /* Ensure that we have enough space available for the default form |
| 4930 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 4931 | * with no space for CIDs counted in). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl)); |
| 4933 | if (ret != 0) { |
| 4934 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4935 | return ret; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4936 | } |
| 4937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4938 | ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec); |
| 4939 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4940 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4942 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
| 4943 | ret = ssl_buffer_future_record(ssl, &rec); |
| 4944 | if (ret != 0) { |
| 4945 | return ret; |
| 4946 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4947 | |
| 4948 | /* Fall through to handling of unexpected records */ |
| 4949 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 4950 | } |
| 4951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4952 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4953 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4954 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4955 | * assuming no CID and no offset between record content and |
| 4956 | * record plaintext. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4957 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4958 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4959 | /* Setup internal message pointers from record structure. */ |
| 4960 | ssl->in_msgtype = rec.type; |
| 4961 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4962 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4963 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4964 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4965 | ssl->in_msglen = rec.data_len; |
| 4966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4967 | ret = ssl_check_client_reconnect(ssl); |
| 4968 | MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret); |
| 4969 | if (ret != 0) { |
| 4970 | return ret; |
| 4971 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4972 | #endif |
| 4973 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4974 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 4975 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4976 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4977 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record " |
| 4978 | "(header)")); |
| 4979 | } else { |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4980 | /* Skip invalid record and the rest of the datagram */ |
| 4981 | ssl->next_record_offset = 0; |
| 4982 | ssl->in_left = 0; |
| 4983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4984 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record " |
| 4985 | "(header)")); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | /* Get next record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4989 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4990 | } else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4991 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4992 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4993 | return ret; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4994 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4995 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4996 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4997 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4998 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4999 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 5000 | ssl->next_record_offset = rec.buf_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5001 | if (ssl->next_record_offset < ssl->in_left) { |
| 5002 | MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram")); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 5003 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5004 | } else |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5005 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 5006 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 5007 | /* |
| 5008 | * Fetch record contents from underlying transport. |
| 5009 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len); |
| 5011 | if (ret != 0) { |
| 5012 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 5013 | return ret; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 5014 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5015 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5016 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | /* |
| 5020 | * Decrypt record contents. |
| 5021 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5022 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5023 | if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5024 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5025 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5026 | /* Silently discard invalid records */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5027 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5028 | /* Except when waiting for Finished as a bad mac here |
| 5029 | * probably means something went wrong in the handshake |
| 5030 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5031 | if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 5032 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5033 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5034 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 5035 | mbedtls_ssl_send_alert_message(ssl, |
| 5036 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5037 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5038 | } |
| 5039 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5040 | return ret; |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 5041 | } |
| 5042 | |
Gilles Peskine | 69f8f45 | 2025-02-17 16:08:59 +0100 | [diff] [blame] | 5043 | if (ssl->conf->badmac_limit != 0) { |
Gilles Peskine | f6a676d | 2025-02-17 16:10:14 +0100 | [diff] [blame] | 5044 | ++ssl->badmac_seen_or_in_hsfraglen; |
| 5045 | if (ssl->badmac_seen_or_in_hsfraglen >= ssl->conf->badmac_limit) { |
Gilles Peskine | 69f8f45 | 2025-02-17 16:08:59 +0100 | [diff] [blame] | 5046 | MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC")); |
| 5047 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 5048 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5049 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5050 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5051 | /* As above, invalid records cause |
| 5052 | * dismissal of the whole datagram. */ |
| 5053 | |
| 5054 | ssl->next_record_offset = 0; |
| 5055 | ssl->in_left = 0; |
| 5056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5057 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)")); |
| 5058 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5059 | } |
| 5060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5061 | return ret; |
| 5062 | } else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5063 | #endif |
| 5064 | { |
| 5065 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5066 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5067 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 5068 | mbedtls_ssl_send_alert_message(ssl, |
| 5069 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5070 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5071 | } |
| 5072 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5073 | return ret; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 5074 | } |
| 5075 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5076 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 5077 | |
| 5078 | /* Reset in pointers to default state for TLS/DTLS records, |
| 5079 | * assuming no CID and no offset between record content and |
| 5080 | * record plaintext. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5081 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 5082 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 5083 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 5084 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
irwir | 89af51f | 2019-09-26 21:04:56 +0300 | [diff] [blame] | 5085 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 5086 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 5087 | /* The record content type may change during decryption, |
| 5088 | * so re-read it. */ |
| 5089 | ssl->in_msgtype = rec.type; |
| 5090 | /* Also update the input buffer, because unfortunately |
| 5091 | * the server-side ssl_parse_client_hello() reparses the |
| 5092 | * record header when receiving a ClientHello initiating |
| 5093 | * a renegotiation. */ |
| 5094 | ssl->in_hdr[0] = rec.type; |
| 5095 | ssl->in_msg = rec.buf + rec.data_offset; |
| 5096 | ssl->in_msglen = rec.data_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0); |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 5098 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5099 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5100 | } |
| 5101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5102 | int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5103 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5104 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5105 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5106 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 5107 | * Handle particular types of records |
| 5108 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5109 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 5110 | if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) { |
| 5111 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5112 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5115 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 5116 | if (ssl->in_msglen != 1) { |
| 5117 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET, |
| 5118 | ssl->in_msglen)); |
| 5119 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5120 | } |
| 5121 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5122 | if (ssl->in_msg[0] != 1) { |
| 5123 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x", |
| 5124 | ssl->in_msg[0])); |
| 5125 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5129 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5130 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5131 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
| 5132 | if (ssl->handshake == NULL) { |
| 5133 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake")); |
| 5134 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5135 | } |
| 5136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5137 | MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember")); |
| 5138 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5139 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5140 | #endif |
Ronald Cron | 7e38cba | 2021-11-24 12:43:39 +0100 | [diff] [blame] | 5141 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5142 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Gilles Peskine | 5a668dd | 2024-09-13 16:03:41 +0200 | [diff] [blame] | 5144 | MBEDTLS_SSL_DEBUG_MSG(2, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | ("Ignore ChangeCipherSpec in TLS 1.3 compatibility mode")); |
| 5146 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Ronald Cron | 7e38cba | 2021-11-24 12:43:39 +0100 | [diff] [blame] | 5147 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 5148 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5149 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 5150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5151 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 5152 | if (ssl->in_msglen != 2) { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 5153 | /* Note: Standard allows for more than one 2 byte alert |
| 5154 | to be packed in a single message, but Mbed TLS doesn't |
| 5155 | currently support this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5156 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET, |
| 5157 | ssl->in_msglen)); |
| 5158 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 5159 | } |
| 5160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5161 | MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]", |
| 5162 | ssl->in_msg[0], ssl->in_msg[1])); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5163 | |
| 5164 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 5165 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5166 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5167 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) { |
| 5168 | MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)", |
| 5169 | ssl->in_msg[1])); |
| 5170 | return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5173 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5174 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) { |
| 5175 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message")); |
| 5176 | return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5177 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5178 | |
| 5179 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5180 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 5181 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) { |
| 5182 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a no renegotiation alert")); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5183 | /* Will be handled when trying to parse ServerHello */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | return 0; |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5185 | } |
| 5186 | #endif |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 5187 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 5188 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5191 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5192 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5193 | /* Drop unexpected ApplicationData records, |
| 5194 | * except at the beginning of renegotiations */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5195 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 5196 | mbedtls_ssl_is_handshake_over(ssl) == 0 |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5197 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5198 | && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 5199 | ssl->state == MBEDTLS_SSL_SERVER_HELLO) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5200 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5201 | ) { |
| 5202 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData")); |
| 5203 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5204 | } |
| 5205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5206 | if (ssl->handshake != NULL && |
| 5207 | mbedtls_ssl_is_handshake_over(ssl) == 1) { |
| 5208 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 5209 | } |
| 5210 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 5211 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 5212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5216 | int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5217 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5218 | return mbedtls_ssl_send_alert_message(ssl, |
| 5219 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5220 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5223 | int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl, |
| 5224 | unsigned char level, |
| 5225 | unsigned char message) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5226 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5229 | if (ssl == NULL || ssl->conf == NULL) { |
| 5230 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5231 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | if (ssl->out_left != 0) { |
| 5234 | return mbedtls_ssl_flush_output(ssl); |
| 5235 | } |
Hanno Becker | 5e18f74 | 2018-08-06 11:35:16 +0100 | [diff] [blame] | 5236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5237 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message")); |
| 5238 | MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message)); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5240 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5241 | ssl->out_msglen = 2; |
| 5242 | ssl->out_msg[0] = level; |
| 5243 | ssl->out_msg[1] = message; |
| 5244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5245 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 5246 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 5247 | return ret; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5248 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message")); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | return 0; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5254 | int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5255 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5256 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5260 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5261 | ssl->out_msglen = 1; |
| 5262 | ssl->out_msg[0] = 1; |
| 5263 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5264 | ssl->state++; |
| 5265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5266 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 5267 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 5268 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5271 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5273 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5276 | int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5277 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5278 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5280 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5283 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5284 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5287 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 5288 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message")); |
| 5289 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5290 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 5291 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 5294 | /* CCS records are only accepted if they have length 1 and content '1', |
| 5295 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5296 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5297 | /* |
| 5298 | * Switch to our negotiated transform and session parameters for inbound |
| 5299 | * data. |
| 5300 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5301 | MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data")); |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5302 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5303 | ssl->transform_in = ssl->transform_negotiate; |
Jerry Yu | 2e19981 | 2022-12-01 18:57:19 +0800 | [diff] [blame] | 5304 | #endif |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5305 | ssl->session_in = ssl->session_negotiate; |
| 5306 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5307 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5309 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5310 | mbedtls_ssl_dtls_replay_reset(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5311 | #endif |
| 5312 | |
| 5313 | /* Increment epoch */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | if (++ssl->in_epoch == 0) { |
| 5315 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 5316 | /* This is highly unlikely to happen for legitimate reasons, so |
| 5317 | treat it as an attack and don't send an alert. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5318 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5319 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5320 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5321 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5322 | memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5324 | mbedtls_ssl_update_in_pointers(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5325 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5326 | ssl->state++; |
| 5327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5328 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5329 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5330 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5333 | /* Once ssl->out_hdr as the address of the beginning of the |
| 5334 | * next outgoing record is set, deduce the other pointers. |
| 5335 | * |
| 5336 | * Note: For TLS, we save the implicit record sequence number |
| 5337 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 5338 | * and the caller has to make sure there's space for this. |
| 5339 | */ |
| 5340 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 5341 | static size_t ssl_transform_get_explicit_iv_len( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | mbedtls_ssl_transform const *transform) |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 5343 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5344 | return transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 5345 | } |
| 5346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl, |
| 5348 | mbedtls_ssl_transform *transform) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5349 | { |
| 5350 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5351 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5352 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5353 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5354 | ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5355 | ssl->out_len = ssl->out_cid; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | if (transform != NULL) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5357 | ssl->out_len += transform->out_cid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5359 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5360 | ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5361 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5362 | ssl->out_iv = ssl->out_len + 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5363 | } else |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5364 | #endif |
| 5365 | { |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5366 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5367 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 5368 | ssl->out_cid = ssl->out_len; |
| 5369 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5370 | ssl->out_iv = ssl->out_hdr + 5; |
| 5371 | } |
| 5372 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 5373 | ssl->out_msg = ssl->out_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5374 | /* Adjust out_msg to make space for explicit IV, if used. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | if (transform != NULL) { |
| 5376 | ssl->out_msg += ssl_transform_get_explicit_iv_len(transform); |
| 5377 | } |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | /* Once ssl->in_hdr as the address of the beginning of the |
| 5381 | * next incoming record is set, deduce the other pointers. |
| 5382 | * |
| 5383 | * Note: For TLS, we save the implicit record sequence number |
| 5384 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 5385 | * and the caller has to make sure there's space for this. |
| 5386 | */ |
| 5387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5389 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 5390 | /* This function sets the pointers to match the case |
| 5391 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 5392 | * and ssl->in_msg pointing to the beginning of the record |
| 5393 | * content. |
| 5394 | * |
| 5395 | * When decrypting a protected record, ssl->in_msg |
| 5396 | * will be shifted to point to the beginning of the |
| 5397 | * record plaintext. |
| 5398 | */ |
| 5399 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5400 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5401 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5402 | /* This sets the header pointers to match records |
| 5403 | * without CID. When we receive a record containing |
| 5404 | * a CID, the fields are shifted accordingly in |
| 5405 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5406 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5407 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5408 | ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5409 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5410 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Jerry Yu | ae0b2e2 | 2021-10-08 15:21:19 +0800 | [diff] [blame] | 5411 | ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5412 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 5413 | ssl->in_iv = ssl->in_len + 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | } else |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5415 | #endif |
| 5416 | { |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 5417 | ssl->in_ctr = ssl->in_buf; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5418 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5419 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 5420 | ssl->in_cid = ssl->in_len; |
| 5421 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5422 | ssl->in_iv = ssl->in_hdr + 5; |
| 5423 | } |
| 5424 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 5425 | /* This will be adjusted at record decryption time. */ |
| 5426 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5427 | } |
| 5428 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5429 | /* |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 5430 | * Setup an SSL context |
| 5431 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5432 | |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 5433 | void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl) |
| 5434 | { |
| 5435 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5436 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5437 | ssl->in_hdr = ssl->in_buf; |
| 5438 | } else |
Deomid rojer Ryabkov | 1f4088c | 2025-01-18 15:58:57 +0200 | [diff] [blame] | 5439 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 5440 | { |
| 5441 | ssl->in_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
| 5442 | } |
| 5443 | |
| 5444 | /* Derive other internal pointers. */ |
| 5445 | mbedtls_ssl_update_in_pointers(ssl); |
| 5446 | } |
| 5447 | |
| 5448 | void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5449 | { |
| 5450 | /* Set the incoming and outgoing record pointers. */ |
| 5451 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5452 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5453 | ssl->out_hdr = ssl->out_buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | } else |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5455 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5456 | { |
Hanno Becker | 12078f4 | 2021-03-02 15:28:41 +0000 | [diff] [blame] | 5457 | ssl->out_ctr = ssl->out_buf; |
Deomid rojer Ryabkov | 3fc5a4d | 2024-03-10 02:11:03 +0000 | [diff] [blame] | 5458 | ssl->out_hdr = ssl->out_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN; |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5459 | } |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5460 | /* Derive other internal pointers. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5462 | } |
| 5463 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5464 | /* |
| 5465 | * SSL get accessors |
| 5466 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5467 | size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5468 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5469 | return ssl->in_offt == NULL ? 0 : ssl->in_msglen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5472 | int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl) |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5473 | { |
| 5474 | /* |
| 5475 | * Case A: We're currently holding back |
| 5476 | * a message for further processing. |
| 5477 | */ |
| 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | if (ssl->keep_current_message == 1) { |
| 5480 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing")); |
| 5481 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | /* |
| 5485 | * Case B: Further records are pending in the current datagram. |
| 5486 | */ |
| 5487 | |
| 5488 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5489 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5490 | ssl->in_left > ssl->next_record_offset) { |
| 5491 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram")); |
| 5492 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5493 | } |
| 5494 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5495 | |
| 5496 | /* |
| 5497 | * Case C: A handshake message is being processed. |
| 5498 | */ |
| 5499 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) { |
| 5501 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 5502 | ("ssl_check_pending: more handshake messages within current record")); |
| 5503 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | /* |
| 5507 | * Case D: An application data message is being processed |
| 5508 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5509 | if (ssl->in_offt != NULL) { |
| 5510 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed")); |
| 5511 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | /* |
| 5515 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5516 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5517 | * we implement support for multiple alerts in single records. |
| 5518 | */ |
| 5519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending")); |
| 5521 | return 0; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5522 | } |
| 5523 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5525 | int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5526 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5527 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5528 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5529 | unsigned block_size; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5530 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5531 | psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT; |
| 5532 | psa_key_type_t key_type; |
| 5533 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5537 | if (transform == NULL) { |
| 5538 | return (int) out_hdr_len; |
| 5539 | } |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 5540 | |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5541 | |
| 5542 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | if (transform->psa_alg == PSA_ALG_GCM || |
| 5544 | transform->psa_alg == PSA_ALG_CCM || |
| 5545 | transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 8) || |
| 5546 | transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 || |
| 5547 | transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) { |
Przemyslaw Stekiel | 1d71447 | 2022-01-24 23:46:50 +0100 | [diff] [blame] | 5548 | transform_expansion = transform->minlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5549 | } else if (transform->psa_alg == PSA_ALG_CBC_NO_PADDING) { |
| 5550 | (void) psa_get_key_attributes(transform->psa_key_enc, &attr); |
| 5551 | key_type = psa_get_key_type(&attr); |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5552 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5553 | block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type); |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5554 | |
Przemyslaw Stekiel | 1d71447 | 2022-01-24 23:46:50 +0100 | [diff] [blame] | 5555 | /* Expansion due to the addition of the MAC. */ |
| 5556 | transform_expansion += transform->maclen; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5557 | |
Przemyslaw Stekiel | 1d71447 | 2022-01-24 23:46:50 +0100 | [diff] [blame] | 5558 | /* Expansion due to the addition of CBC padding; |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 5559 | * Theoretically up to 256 bytes, but we never use |
| 5560 | * more than the block size of the underlying cipher. */ |
Przemyslaw Stekiel | 1d71447 | 2022-01-24 23:46:50 +0100 | [diff] [blame] | 5561 | transform_expansion += block_size; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5562 | |
Przemyslaw Stekiel | 1d71447 | 2022-01-24 23:46:50 +0100 | [diff] [blame] | 5563 | /* For TLS 1.2 or higher, an explicit IV is added |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 5564 | * after the record header. */ |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5565 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Przemyslaw Stekiel | 1d71447 | 2022-01-24 23:46:50 +0100 | [diff] [blame] | 5566 | transform_expansion += block_size; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5567 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | } else { |
| 5569 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 5570 | ("Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()")); |
| 5571 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5572 | } |
| 5573 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5575 | case MBEDTLS_MODE_GCM: |
| 5576 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5577 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5578 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5579 | transform_expansion = transform->minlen; |
| 5580 | break; |
| 5581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5582 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5583 | |
| 5584 | block_size = mbedtls_cipher_get_block_size( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | &transform->cipher_ctx_enc); |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5586 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5587 | /* Expansion due to the addition of the MAC. */ |
| 5588 | transform_expansion += transform->maclen; |
| 5589 | |
| 5590 | /* Expansion due to the addition of CBC padding; |
| 5591 | * Theoretically up to 256 bytes, but we never use |
| 5592 | * more than the block size of the underlying cipher. */ |
| 5593 | transform_expansion += block_size; |
| 5594 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 5595 | /* For TLS 1.2 or higher, an explicit IV is added |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5596 | * after the record header. */ |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 5597 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 5598 | transform_expansion += block_size; |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 5599 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5600 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5601 | break; |
| 5602 | |
| 5603 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5604 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 5605 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5606 | } |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 5607 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5608 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5609 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5610 | if (transform->out_cid_len != 0) { |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 5611 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5612 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5613 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 5614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5615 | return (int) (out_hdr_len + transform_expansion); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5616 | } |
| 5617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5618 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5619 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5620 | * Check record counters and renegotiate if they're above the limit. |
| 5621 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 5622 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5624 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5625 | size_t ep_len = mbedtls_ssl_ep_len(ssl); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5626 | int in_ctr_cmp; |
| 5627 | int out_ctr_cmp; |
| 5628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5629 | if (mbedtls_ssl_is_handshake_over(ssl) == 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5630 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5631 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5632 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5633 | } |
| 5634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | in_ctr_cmp = memcmp(ssl->in_ctr + ep_len, |
| 5636 | &ssl->conf->renego_period[ep_len], |
| 5637 | MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len); |
| 5638 | out_ctr_cmp = memcmp(&ssl->cur_out_ctr[ep_len], |
Jerry Yu | d9a94fe | 2021-09-28 18:58:59 +0800 | [diff] [blame] | 5639 | &ssl->conf->renego_period[ep_len], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5640 | sizeof(ssl->cur_out_ctr) - ep_len); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5641 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5642 | if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) { |
| 5643 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5644 | } |
| 5645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate")); |
| 5647 | return mbedtls_ssl_renegotiate(ssl); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5648 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5649 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5650 | |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5651 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 5652 | |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5653 | #if defined(MBEDTLS_SSL_CLI_C) |
Jerry Yu | a0446a0 | 2022-07-13 11:22:55 +0800 | [diff] [blame] | 5654 | MBEDTLS_CHECK_RETURN_CRITICAL |
Ronald Cron | 698c8e9 | 2024-04-02 13:19:57 +0200 | [diff] [blame] | 5655 | static int ssl_tls13_is_new_session_ticket(mbedtls_ssl_context *ssl) |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5656 | { |
| 5657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5658 | if ((ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl)) || |
| 5659 | (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET)) { |
Ronald Cron | 698c8e9 | 2024-04-02 13:19:57 +0200 | [diff] [blame] | 5660 | return 0; |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5661 | } |
| 5662 | |
Ronald Cron | 698c8e9 | 2024-04-02 13:19:57 +0200 | [diff] [blame] | 5663 | return 1; |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5664 | } |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5665 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5666 | |
Jerry Yu | a0446a0 | 2022-07-13 11:22:55 +0800 | [diff] [blame] | 5667 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5668 | static int ssl_tls13_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5669 | { |
| 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | MBEDTLS_SSL_DEBUG_MSG(3, ("received post-handshake message")); |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5672 | |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5673 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5674 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Ronald Cron | 698c8e9 | 2024-04-02 13:19:57 +0200 | [diff] [blame] | 5675 | if (ssl_tls13_is_new_session_ticket(ssl)) { |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5676 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 5677 | MBEDTLS_SSL_DEBUG_MSG(3, ("NewSessionTicket received")); |
Ronald Cron | 9f44c88 | 2024-08-28 16:44:10 +0200 | [diff] [blame] | 5678 | if (mbedtls_ssl_conf_is_signal_new_session_tickets_enabled(ssl->conf) == |
| 5679 | MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED) { |
Ronald Cron | b675b2b | 2024-08-27 09:19:40 +0200 | [diff] [blame] | 5680 | ssl->keep_current_message = 1; |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5681 | |
Ronald Cron | b675b2b | 2024-08-27 09:19:40 +0200 | [diff] [blame] | 5682 | mbedtls_ssl_handshake_set_state(ssl, |
| 5683 | MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET); |
| 5684 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 5685 | } else { |
Ronald Cron | 97dc583 | 2024-08-28 09:34:34 +0200 | [diff] [blame] | 5686 | MBEDTLS_SSL_DEBUG_MSG(3, ("Ignoring NewSessionTicket, handling disabled.")); |
Ronald Cron | b675b2b | 2024-08-27 09:19:40 +0200 | [diff] [blame] | 5687 | return 0; |
| 5688 | } |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5689 | #else |
Ronald Cron | 97dc583 | 2024-08-28 09:34:34 +0200 | [diff] [blame] | 5690 | MBEDTLS_SSL_DEBUG_MSG(3, ("Ignoring NewSessionTicket, not supported.")); |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5691 | return 0; |
| 5692 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | } |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5694 | } |
Ronald Cron | 6071f61 | 2024-03-25 13:42:07 +0100 | [diff] [blame] | 5695 | #endif /* MBEDTLS_SSL_CLI_C */ |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5696 | |
| 5697 | /* Fail in all other cases. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5698 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5699 | } |
| 5700 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 5701 | |
| 5702 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5703 | /* This function is called from mbedtls_ssl_read() when a handshake message is |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5704 | * received after the initial handshake. In this context, handshake messages |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5705 | * may only be sent for the purpose of initiating renegotiations. |
| 5706 | * |
| 5707 | * This function is introduced as a separate helper since the handling |
| 5708 | * of post-handshake handshake messages changes significantly in TLS 1.3, |
| 5709 | * and having a helper function allows to distinguish between TLS <= 1.2 and |
| 5710 | * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read(). |
| 5711 | */ |
Jerry Yu | a0446a0 | 2022-07-13 11:22:55 +0800 | [diff] [blame] | 5712 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5713 | static int ssl_tls12_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5714 | { |
Hanno Becker | fae12cf | 2021-04-21 07:20:20 +0100 | [diff] [blame] | 5715 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5716 | |
| 5717 | /* |
| 5718 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 5719 | * - For server-side, expect CLIENT_HELLO. |
| 5720 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 5721 | */ |
| 5722 | |
| 5723 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5724 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5725 | (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
| 5726 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) { |
| 5727 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)")); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5728 | |
| 5729 | /* With DTLS, drop the packet (probably from last handshake) */ |
| 5730 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5731 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5732 | return 0; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5733 | } |
| 5734 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5736 | } |
| 5737 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5738 | |
| 5739 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5741 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 5742 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)")); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5743 | |
| 5744 | /* With DTLS, drop the packet (probably from last handshake) */ |
| 5745 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5747 | return 0; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5748 | } |
| 5749 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5751 | } |
| 5752 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 5753 | |
| 5754 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5755 | /* Determine whether renegotiation attempt should be accepted */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5756 | if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 5757 | (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 5758 | ssl->conf->allow_legacy_renegotiation == |
| 5759 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) { |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5760 | /* |
| 5761 | * Accept renegotiation request |
| 5762 | */ |
| 5763 | |
| 5764 | /* DTLS clients need to know renego is server-initiated */ |
| 5765 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5766 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5767 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5768 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 5769 | } |
| 5770 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | ret = mbedtls_ssl_start_renegotiation(ssl); |
| 5772 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5773 | ret != 0) { |
| 5774 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", |
| 5775 | ret); |
| 5776 | return ret; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5777 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | } else |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5779 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 5780 | { |
| 5781 | /* |
| 5782 | * Refuse renegotiation |
| 5783 | */ |
| 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert")); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5787 | if ((ret = mbedtls_ssl_send_alert_message(ssl, |
| 5788 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5789 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) != 0) { |
| 5790 | return ret; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5791 | } |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5794 | return 0; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5795 | } |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5796 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5797 | |
| 5798 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5800 | { |
| 5801 | /* Check protocol version and dispatch accordingly. */ |
| 5802 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 5804 | return ssl_tls13_handle_hs_message_post_handshake(ssl); |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5805 | } |
| 5806 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 5807 | |
| 5808 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | if (ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) { |
| 5810 | return ssl_tls12_handle_hs_message_post_handshake(ssl); |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5811 | } |
| 5812 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 5813 | |
| 5814 | /* Should never happen */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Jerry Yu | c62ae5f | 2022-07-07 09:42:26 +0000 | [diff] [blame] | 5816 | } |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5817 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5818 | /* |
Jerry Yu | 739a1d4 | 2022-12-08 21:10:25 +0800 | [diff] [blame] | 5819 | * brief Read at most 'len' application data bytes from the input |
| 5820 | * buffer. |
| 5821 | * |
| 5822 | * param ssl SSL context: |
| 5823 | * - First byte of application data not read yet in the input |
| 5824 | * buffer located at address `in_offt`. |
| 5825 | * - The number of bytes of data not read yet is `in_msglen`. |
| 5826 | * param buf buffer that will hold the data |
| 5827 | * param len maximum number of bytes to read |
| 5828 | * |
| 5829 | * note The function updates the fields `in_offt` and `in_msglen` |
| 5830 | * according to the number of bytes read. |
| 5831 | * |
| 5832 | * return The number of bytes read. |
| 5833 | */ |
| 5834 | static int ssl_read_application_data( |
| 5835 | mbedtls_ssl_context *ssl, unsigned char *buf, size_t len) |
| 5836 | { |
| 5837 | size_t n = (len < ssl->in_msglen) ? len : ssl->in_msglen; |
| 5838 | |
| 5839 | if (len != 0) { |
| 5840 | memcpy(buf, ssl->in_offt, n); |
| 5841 | ssl->in_msglen -= n; |
| 5842 | } |
| 5843 | |
| 5844 | /* Zeroising the plaintext buffer to erase unused application data |
| 5845 | from the memory. */ |
| 5846 | mbedtls_platform_zeroize(ssl->in_offt, n); |
| 5847 | |
| 5848 | if (ssl->in_msglen == 0) { |
| 5849 | /* all bytes consumed */ |
| 5850 | ssl->in_offt = NULL; |
| 5851 | ssl->keep_current_message = 0; |
| 5852 | } else { |
| 5853 | /* more data available */ |
| 5854 | ssl->in_offt += n; |
| 5855 | } |
| 5856 | |
| 5857 | return (int) n; |
| 5858 | } |
| 5859 | |
| 5860 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5861 | * Receive application data decrypted from the SSL layer |
| 5862 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5864 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5865 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | if (ssl == NULL || ssl->conf == NULL) { |
| 5868 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5869 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5873 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5874 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5875 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 5876 | return ret; |
| 5877 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5878 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5879 | if (ssl->handshake != NULL && |
| 5880 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 5881 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 5882 | return ret; |
| 5883 | } |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5884 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5885 | } |
| 5886 | #endif |
| 5887 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5888 | /* |
| 5889 | * Check if renegotiation is necessary and/or handshake is |
| 5890 | * in process. If yes, perform/continue, and fall through |
| 5891 | * if an unexpected packet is received while the client |
| 5892 | * is waiting for the ServerHello. |
| 5893 | * |
| 5894 | * (There is no equivalent to the last condition on |
| 5895 | * the server-side as it is not treated as within |
| 5896 | * a handshake while waiting for the ClientHello |
| 5897 | * after a renegotiation request.) |
| 5898 | */ |
| 5899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5900 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5901 | ret = ssl_check_ctr_renegotiate(ssl); |
| 5902 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5903 | ret != 0) { |
| 5904 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 5905 | return ret; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5906 | } |
| 5907 | #endif |
| 5908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5909 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5910 | ret = mbedtls_ssl_handshake(ssl); |
| 5911 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5912 | ret != 0) { |
| 5913 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5914 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 5918 | /* Loop as long as no application data record is available */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | while (ssl->in_offt == NULL) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5920 | /* Start timer if not already running */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5921 | if (ssl->f_get_timer != NULL && |
| 5922 | ssl->f_get_timer(ssl->p_timer) == -1) { |
| 5923 | mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 5924 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5925 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5926 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5927 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) { |
| 5928 | return 0; |
| 5929 | } |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5930 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5931 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5932 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5933 | } |
| 5934 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5935 | if (ssl->in_msglen == 0 && |
| 5936 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5937 | /* |
| 5938 | * OpenSSL sends empty messages to randomize the IV |
| 5939 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5940 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5941 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) { |
| 5942 | return 0; |
| 5943 | } |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5944 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5945 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5946 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5947 | } |
| 5948 | } |
| 5949 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5950 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 5951 | ret = ssl_handle_hs_message_post_handshake(ssl); |
| 5952 | if (ret != 0) { |
| 5953 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_handle_hs_message_post_handshake", |
| 5954 | ret); |
| 5955 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5956 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5957 | |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5958 | /* At this point, we don't know whether the renegotiation triggered |
| 5959 | * by the post-handshake message has been completed or not. The cases |
| 5960 | * to consider are the following: |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5961 | * 1) The renegotiation is complete. In this case, no new record |
| 5962 | * has been read yet. |
| 5963 | * 2) The renegotiation is incomplete because the client received |
| 5964 | * an application data record while awaiting the ServerHello. |
| 5965 | * 3) The renegotiation is incomplete because the client received |
| 5966 | * a non-handshake, non-application data message while awaiting |
| 5967 | * the ServerHello. |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5968 | * |
| 5969 | * In each of these cases, looping will be the proper action: |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5970 | * - For 1), the next iteration will read a new record and check |
| 5971 | * if it's application data. |
| 5972 | * - For 2), the loop condition isn't satisfied as application data |
| 5973 | * is present, hence continue is the same as break |
| 5974 | * - For 3), the loop condition is satisfied and read_record |
| 5975 | * will re-deliver the message that was held back by the client |
| 5976 | * when expecting the ServerHello. |
| 5977 | */ |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5978 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5979 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5980 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5981 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5982 | else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 5983 | if (ssl->conf->renego_max_records >= 0) { |
| 5984 | if (++ssl->renego_records_seen > ssl->conf->renego_max_records) { |
| 5985 | MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, " |
| 5986 | "but not honored by client")); |
| 5987 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 5988 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5989 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 5990 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5991 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5992 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5993 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 5995 | MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert")); |
| 5996 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5997 | } |
| 5998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5999 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 6000 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message")); |
| 6001 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6002 | } |
| 6003 | |
| 6004 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 6005 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 6006 | /* We're going to return something now, cancel timer, |
| 6007 | * except if handshake (renegotiation) is in progress */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | if (mbedtls_ssl_is_handshake_over(ssl) == 1) { |
| 6009 | mbedtls_ssl_set_timer(ssl, 0); |
| 6010 | } |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6011 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 6012 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6013 | /* If we requested renego but received AppData, resend HelloRequest. |
| 6014 | * Do it now, after setting in_offt, to avoid taking this branch |
| 6015 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6016 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6017 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 6018 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 6019 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 6020 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 6021 | ret); |
| 6022 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6023 | } |
| 6024 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6025 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 6026 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
Jerry Yu | 739a1d4 | 2022-12-08 21:10:25 +0800 | [diff] [blame] | 6029 | ret = ssl_read_application_data(ssl, buf, len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6030 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6031 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6032 | |
Jerry Yu | 739a1d4 | 2022-12-08 21:10:25 +0800 | [diff] [blame] | 6033 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6034 | } |
| 6035 | |
Jerry Yu | d9ca354 | 2023-12-06 17:23:52 +0800 | [diff] [blame] | 6036 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA) |
| 6037 | int mbedtls_ssl_read_early_data(mbedtls_ssl_context *ssl, |
| 6038 | unsigned char *buf, size_t len) |
| 6039 | { |
Ronald Cron | ed7d4bf | 2024-01-31 07:55:19 +0100 | [diff] [blame] | 6040 | if (ssl == NULL || (ssl->conf == NULL)) { |
Jerry Yu | d9ca354 | 2023-12-06 17:23:52 +0800 | [diff] [blame] | 6041 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6042 | } |
| 6043 | |
Ronald Cron | 164537c | 2024-02-01 18:05:47 +0100 | [diff] [blame] | 6044 | /* |
| 6045 | * The server may receive early data only while waiting for the End of |
| 6046 | * Early Data handshake message. |
| 6047 | */ |
Ronald Cron | ed7d4bf | 2024-01-31 07:55:19 +0100 | [diff] [blame] | 6048 | if ((ssl->state != MBEDTLS_SSL_END_OF_EARLY_DATA) || |
| 6049 | (ssl->in_offt == NULL)) { |
Jerry Yu | d9ca354 | 2023-12-06 17:23:52 +0800 | [diff] [blame] | 6050 | return MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA; |
| 6051 | } |
| 6052 | |
Ronald Cron | ed7d4bf | 2024-01-31 07:55:19 +0100 | [diff] [blame] | 6053 | return ssl_read_application_data(ssl, buf, len); |
Jerry Yu | d9ca354 | 2023-12-06 17:23:52 +0800 | [diff] [blame] | 6054 | } |
| 6055 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA */ |
| 6056 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6057 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 6058 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 6059 | * fragment length and buffer size. |
| 6060 | * |
| 6061 | * According to RFC 5246 Section 6.2.1: |
| 6062 | * |
| 6063 | * Zero-length fragments of Application data MAY be sent as they are |
| 6064 | * potentially useful as a traffic analysis countermeasure. |
| 6065 | * |
| 6066 | * Therefore, it is possible that the input message length is 0 and the |
| 6067 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6068 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 6069 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6070 | static int ssl_write_real(mbedtls_ssl_context *ssl, |
| 6071 | const unsigned char *buf, size_t len) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6072 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6073 | int ret = mbedtls_ssl_get_max_out_record_payload(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 6074 | const size_t max_len = (size_t) ret; |
| 6075 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6076 | if (ret < 0) { |
| 6077 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret); |
| 6078 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 6079 | } |
| 6080 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6081 | if (len > max_len) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6082 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6083 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 6084 | MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) " |
| 6085 | "maximum fragment length: %" MBEDTLS_PRINTF_SIZET |
| 6086 | " > %" MBEDTLS_PRINTF_SIZET, |
| 6087 | len, max_len)); |
| 6088 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6089 | } else |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6090 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6091 | len = max_len; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6092 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6093 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6094 | if (ssl->out_left != 0) { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 6095 | /* |
| 6096 | * The user has previously tried to send the data and |
| 6097 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 6098 | * written. In this case, we expect the high-level write function |
| 6099 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 6100 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6101 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 6102 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 6103 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6104 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6105 | } else { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 6106 | /* |
| 6107 | * The user is trying to send a message the first time, so we need to |
| 6108 | * copy the data into the internal buffers and setup the data structure |
| 6109 | * to keep track of partial writes |
| 6110 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6111 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6112 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Dave Rodgman | f684025 | 2023-02-24 15:41:34 +0000 | [diff] [blame] | 6113 | if (len > 0) { |
| 6114 | memcpy(ssl->out_msg, buf, len); |
| 6115 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6117 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 6118 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 6119 | return ret; |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6120 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6123 | return (int) len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6127 | * Write application data (public-facing wrapper) |
| 6128 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6129 | int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6130 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6131 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6133 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6135 | if (ssl == NULL || ssl->conf == NULL) { |
| 6136 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6137 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6138 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6139 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6140 | if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) { |
| 6141 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 6142 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6143 | } |
| 6144 | #endif |
| 6145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6146 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 6147 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 6148 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 6149 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6150 | } |
| 6151 | } |
| 6152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6153 | ret = ssl_write_real(ssl, buf, len); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6155 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6156 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6157 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6158 | } |
| 6159 | |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6160 | #if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_CLI_C) |
| 6161 | int mbedtls_ssl_write_early_data(mbedtls_ssl_context *ssl, |
| 6162 | const unsigned char *buf, size_t len) |
| 6163 | { |
| 6164 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 6165 | const struct mbedtls_ssl_config *conf; |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6166 | uint32_t remaining; |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6167 | |
| 6168 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write early_data")); |
| 6169 | |
| 6170 | if (ssl == NULL || (conf = ssl->conf) == NULL) { |
| 6171 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6172 | } |
| 6173 | |
Ronald Cron | 4922190 | 2024-02-21 13:39:14 +0100 | [diff] [blame] | 6174 | if (conf->endpoint != MBEDTLS_SSL_IS_CLIENT) { |
| 6175 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6176 | } |
| 6177 | |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6178 | if ((!mbedtls_ssl_conf_is_tls13_enabled(conf)) || |
| 6179 | (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) || |
| 6180 | (conf->early_data_enabled != MBEDTLS_SSL_EARLY_DATA_ENABLED)) { |
| 6181 | return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA; |
| 6182 | } |
| 6183 | |
| 6184 | if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) { |
| 6185 | return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA; |
| 6186 | } |
| 6187 | |
| 6188 | /* |
Ronald Cron | d288466 | 2024-03-03 15:03:22 +0100 | [diff] [blame] | 6189 | * If we are at the beginning of the handshake, the early data state being |
Ronald Cron | 05d7cfb | 2024-03-03 15:39:30 +0100 | [diff] [blame] | 6190 | * equal to MBEDTLS_SSL_EARLY_DATA_STATE_IDLE or |
Ronald Cron | 3641df2 | 2024-03-03 16:10:58 +0100 | [diff] [blame] | 6191 | * MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT advance the handshake just |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6192 | * enough to be able to send early data if possible. That way, we can |
| 6193 | * guarantee that when starting the handshake with this function we will |
Ronald Cron | d288466 | 2024-03-03 15:03:22 +0100 | [diff] [blame] | 6194 | * send at least one record of early data. Note that when the state is |
Ronald Cron | 3641df2 | 2024-03-03 16:10:58 +0100 | [diff] [blame] | 6195 | * MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT and not yet |
| 6196 | * MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE, we cannot send early data |
Ronald Cron | d406924 | 2024-02-21 13:45:52 +0100 | [diff] [blame] | 6197 | * as the early data outbound transform has not been set as we may have to |
| 6198 | * first send a dummy CCS in clear. |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6199 | */ |
Ronald Cron | 05d7cfb | 2024-03-03 15:39:30 +0100 | [diff] [blame] | 6200 | if ((ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IDLE) || |
Ronald Cron | 3641df2 | 2024-03-03 16:10:58 +0100 | [diff] [blame] | 6201 | (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT)) { |
Ronald Cron | 05d7cfb | 2024-03-03 15:39:30 +0100 | [diff] [blame] | 6202 | while ((ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IDLE) || |
Ronald Cron | 3641df2 | 2024-03-03 16:10:58 +0100 | [diff] [blame] | 6203 | (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT)) { |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6204 | ret = mbedtls_ssl_handshake_step(ssl); |
| 6205 | if (ret != 0) { |
| 6206 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake_step", ret); |
| 6207 | return ret; |
| 6208 | } |
| 6209 | |
| 6210 | ret = mbedtls_ssl_flush_output(ssl); |
| 6211 | if (ret != 0) { |
| 6212 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 6213 | return ret; |
| 6214 | } |
| 6215 | } |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6216 | remaining = ssl->session_negotiate->max_early_data_size; |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6217 | } else { |
Ronald Cron | d406924 | 2024-02-21 13:45:52 +0100 | [diff] [blame] | 6218 | /* |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6219 | * If we are past the point where we can send early data or we have |
| 6220 | * already reached the maximum early data size, return immediatly. |
| 6221 | * Otherwise, progress the handshake as much as possible to not delay |
| 6222 | * it too much. If we reach a point where we can still send early data, |
| 6223 | * then we will send some. |
Ronald Cron | d406924 | 2024-02-21 13:45:52 +0100 | [diff] [blame] | 6224 | */ |
Ronald Cron | d288466 | 2024-03-03 15:03:22 +0100 | [diff] [blame] | 6225 | if ((ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE) && |
| 6226 | (ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED)) { |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6227 | return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA; |
| 6228 | } |
| 6229 | |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6230 | remaining = ssl->session_negotiate->max_early_data_size - |
Ronald Cron | de9b03d | 2024-03-01 15:14:17 +0100 | [diff] [blame] | 6231 | ssl->total_early_data_size; |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6232 | |
| 6233 | if (remaining == 0) { |
| 6234 | return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA; |
| 6235 | } |
| 6236 | |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6237 | ret = mbedtls_ssl_handshake(ssl); |
| 6238 | if ((ret != 0) && (ret != MBEDTLS_ERR_SSL_WANT_READ)) { |
| 6239 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 6240 | return ret; |
| 6241 | } |
| 6242 | } |
| 6243 | |
Ronald Cron | d288466 | 2024-03-03 15:03:22 +0100 | [diff] [blame] | 6244 | if (((ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE) && |
| 6245 | (ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED)) |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6246 | || (remaining == 0)) { |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6247 | return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA; |
| 6248 | } |
| 6249 | |
Ronald Cron | 62f971a | 2024-02-23 08:24:12 +0100 | [diff] [blame] | 6250 | if (len > remaining) { |
| 6251 | len = remaining; |
| 6252 | } |
| 6253 | |
Ronald Cron | 5dbfcce | 2024-02-26 17:50:38 +0100 | [diff] [blame] | 6254 | ret = ssl_write_real(ssl, buf, len); |
| 6255 | if (ret >= 0) { |
| 6256 | ssl->total_early_data_size += ret; |
| 6257 | } |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6258 | |
Ronald Cron | 5dbfcce | 2024-02-26 17:50:38 +0100 | [diff] [blame] | 6259 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write early_data, ret=%d", ret)); |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6260 | |
Ronald Cron | 5dbfcce | 2024-02-26 17:50:38 +0100 | [diff] [blame] | 6261 | return ret; |
Xiaokang Qian | b62732e | 2023-11-30 09:58:08 +0000 | [diff] [blame] | 6262 | } |
| 6263 | #endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_CLI_C */ |
| 6264 | |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6265 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6266 | * Notify the peer that the connection is being closed |
| 6267 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6268 | int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6269 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 6270 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6272 | if (ssl == NULL || ssl->conf == NULL) { |
| 6273 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 6274 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 6275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6276 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6278 | if (mbedtls_ssl_is_handshake_over(ssl) == 1) { |
| 6279 | if ((ret = mbedtls_ssl_send_alert_message(ssl, |
| 6280 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 6281 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) { |
| 6282 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret); |
| 6283 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6284 | } |
| 6285 | } |
| 6286 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6287 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6288 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6289 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6292 | void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6293 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6294 | if (transform == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6295 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6296 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6297 | |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 6298 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6299 | psa_destroy_key(transform->psa_key_enc); |
| 6300 | psa_destroy_key(transform->psa_key_dec); |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 6301 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6302 | mbedtls_cipher_free(&transform->cipher_ctx_enc); |
| 6303 | mbedtls_cipher_free(&transform->cipher_ctx_dec); |
Przemyslaw Stekiel | 6be9cf5 | 2022-01-19 16:00:22 +0100 | [diff] [blame] | 6304 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemyslaw Stekiel | 8f80fb9 | 2022-01-11 08:28:13 +0100 | [diff] [blame] | 6305 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 6306 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Neil Armstrong | 39b8e7d | 2022-02-23 09:24:45 +0100 | [diff] [blame] | 6307 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6308 | psa_destroy_key(transform->psa_mac_enc); |
| 6309 | psa_destroy_key(transform->psa_mac_dec); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 6310 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6311 | mbedtls_md_free(&transform->md_ctx_enc); |
| 6312 | mbedtls_md_free(&transform->md_ctx_dec); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 6313 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 6314 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6316 | mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6319 | void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl, |
| 6320 | mbedtls_ssl_transform *transform) |
Jerry Yu | c7875b5 | 2021-09-05 21:05:50 +0800 | [diff] [blame] | 6321 | { |
Jerry Yu | c7875b5 | 2021-09-05 21:05:50 +0800 | [diff] [blame] | 6322 | ssl->transform_in = transform; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6323 | memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN); |
Jerry Yu | c7875b5 | 2021-09-05 21:05:50 +0800 | [diff] [blame] | 6324 | } |
| 6325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6326 | void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl, |
| 6327 | mbedtls_ssl_transform *transform) |
Jerry Yu | c7875b5 | 2021-09-05 21:05:50 +0800 | [diff] [blame] | 6328 | { |
| 6329 | ssl->transform_out = transform; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6330 | memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr)); |
Jerry Yu | c7875b5 | 2021-09-05 21:05:50 +0800 | [diff] [blame] | 6331 | } |
| 6332 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 6333 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6335 | void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 6336 | { |
| 6337 | unsigned offset; |
| 6338 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6340 | if (hs == NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 6341 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6342 | } |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 6343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6344 | ssl_free_buffered_record(ssl); |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 6345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6346 | for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
| 6347 | ssl_buffering_free_slot(ssl, offset); |
| 6348 | } |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 6349 | } |
| 6350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6351 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, |
| 6352 | uint8_t slot) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 6353 | { |
| 6354 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 6355 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 6356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6357 | if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 6358 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6359 | } |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 6360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6361 | if (hs_buf->is_valid == 1) { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 6362 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 6363 | mbedtls_zeroize_and_free(hs_buf->data, hs_buf->data_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6364 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 6365 | } |
| 6366 | } |
| 6367 | |
| 6368 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 6369 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6370 | /* |
| 6371 | * Convert version numbers to/from wire format |
| 6372 | * and, for DTLS, to/from TLS equivalent. |
| 6373 | * |
| 6374 | * For TLS this is the identity. |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 6375 | * For DTLS, map as follows, then use 1's complement (v -> ~v): |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6376 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 6377 | * DTLS 1.0 is stored as TLS 1.1 internally |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6378 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6379 | void mbedtls_ssl_write_version(unsigned char version[2], int transport, |
| 6380 | mbedtls_ssl_protocol_version tls_version) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6381 | { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 6382 | uint16_t tls_version_formatted; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6383 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6384 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 6385 | tls_version_formatted = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6386 | ~(tls_version - (tls_version == 0x0302 ? 0x0202 : 0x0201)); |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 6387 | } else |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 6388 | #else |
| 6389 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6390 | #endif |
Agathiyan Bragadeesh | 8b52b88 | 2023-07-13 13:12:40 +0100 | [diff] [blame] | 6391 | { |
| 6392 | tls_version_formatted = (uint16_t) tls_version; |
| 6393 | } |
| 6394 | MBEDTLS_PUT_UINT16_BE(tls_version_formatted, version, 0); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6395 | } |
| 6396 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6397 | uint16_t mbedtls_ssl_read_version(const unsigned char version[2], |
| 6398 | int transport) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6399 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6400 | uint16_t tls_version = MBEDTLS_GET_UINT16_BE(version, 0); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6401 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6402 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 6403 | tls_version = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6404 | ~(tls_version - (tls_version == 0xfeff ? 0x0202 : 0x0201)); |
| 6405 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 6406 | #else |
| 6407 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6408 | #endif |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 6409 | return tls_version; |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 6410 | } |
| 6411 | |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 6412 | /* |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 6413 | * Send pending fatal alert. |
| 6414 | * 0, No alert message. |
| 6415 | * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it |
| 6416 | * returned, ssl->alert_reason otherwise. |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 6417 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6418 | int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl) |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 6419 | { |
| 6420 | int ret; |
| 6421 | |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 6422 | /* No pending alert, return success*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6423 | if (ssl->send_alert == 0) { |
| 6424 | return 0; |
| 6425 | } |
Jerry Yu | 394ece6 | 2021-09-14 22:17:21 +0800 | [diff] [blame] | 6426 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6427 | ret = mbedtls_ssl_send_alert_message(ssl, |
| 6428 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 6429 | ssl->alert_type); |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 6430 | |
Jerry Yu | 3bf1f97 | 2021-09-22 21:37:18 +0800 | [diff] [blame] | 6431 | /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE, |
| 6432 | * do not clear the alert to be able to send it later. |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 6433 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6434 | if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 6435 | ssl->send_alert = 0; |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 6436 | } |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 6437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6438 | if (ret != 0) { |
| 6439 | return ret; |
| 6440 | } |
Jerry Yu | bbd5a3f | 2021-09-18 20:50:22 +0800 | [diff] [blame] | 6441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6442 | return ssl->alert_reason; |
Jerry Yu | e704781 | 2021-09-13 19:26:39 +0800 | [diff] [blame] | 6443 | } |
| 6444 | |
Jerry Yu | 394ece6 | 2021-09-14 22:17:21 +0800 | [diff] [blame] | 6445 | /* |
| 6446 | * Set pending fatal alert flag. |
| 6447 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6448 | void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl, |
| 6449 | unsigned char alert_type, |
| 6450 | int alert_reason) |
Jerry Yu | 394ece6 | 2021-09-14 22:17:21 +0800 | [diff] [blame] | 6451 | { |
| 6452 | ssl->send_alert = 1; |
| 6453 | ssl->alert_type = alert_type; |
| 6454 | ssl->alert_reason = alert_reason; |
| 6455 | } |
| 6456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6457 | #endif /* MBEDTLS_SSL_TLS_C */ |