blob: 6cd0f413777d7b250bd44b84ef73136e51ce8c3f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Hanno Beckerf1a38282020-02-05 16:14:29 +00002 * Generic SSL/TLS messaging layer functions
3 * (record layer + retransmission state machine)
Paul Bakker5121ce52009-01-03 21:22:43 +00004 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00007 */
8/*
Paul Bakker5121ce52009-01-03 21:22:43 +00009 * http://www.ietf.org/rfc/rfc2246.txt
10 * http://www.ietf.org/rfc/rfc4346.txt
11 */
12
Gilles Peskinedb09ef62020-06-03 01:43:33 +020013#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000016
SimonBd5800b72016-04-26 07:43:27 +010017#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010018
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000020#include "ssl_misc.h"
Valerio Settib4f50762024-01-17 10:24:52 +010021#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000022#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050023#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010024#include "mbedtls/version.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020025#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020026#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020027
Gilles Peskineebdd4052025-02-17 16:25:24 +010028#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050031#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020032#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050033#include "psa/crypto.h"
34#endif
35
Janos Follath23bdca02016-10-07 14:47:14 +010036#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020038#endif
39
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050040#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040041/* Define a local translating function to save code size by not using too many
42 * arguments in each translating place. */
43static int local_err_translation(psa_status_t status)
44{
45 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040046 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040047 psa_generic_status_to_mbedtls);
48}
49#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050050#endif
51
Dave Rodgman2801f7f2023-05-09 11:00:07 +010052#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
64MBEDTLS_STATIC_TESTABLE
65int 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 Rodgman48fb8a32023-08-10 14:01:51 +0100146 mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret),
Dave Rodgmana81373f2023-05-17 12:36:01 +0100147 output, aux_out, NULL, hash_size);
Dave Rodgman2801f7f2023-05-09 11:00:07 +0100148
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
173cleanup:
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
185MBEDTLS_STATIC_TESTABLE
186int 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 Rodgmanb7825ce2023-08-10 11:58:18 +0100249 mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret),
Dave Rodgmana81373f2023-05-17 12:36:01 +0100250 output, aux_out, NULL, hash_size);
Dave Rodgman2801f7f2023-05-09 11:00:07 +0100251
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
271cleanup:
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 Peskine449bd832023-01-11 14:50:10 +0100280static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +0100281
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200282/*
283 * Start a timer.
284 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200287{
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (ssl->f_set_timer == NULL) {
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200289 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 }
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 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é-Gonnarddb2858c2014-09-29 14:04:42 +0200294}
295
296/*
297 * Return -1 is timer is expired, 0 if it isn't.
298 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200300{
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (ssl->f_get_timer == NULL) {
302 return 0;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200303 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 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é-Gonnarddb2858c2014-09-29 14:04:42 +0200311}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200312
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200313MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100314static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
315 unsigned char *buf,
316 size_t len,
317 mbedtls_record *rec);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl,
320 unsigned char *buf,
321 size_t buflen)
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200322{
323 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record"));
325 MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200326
327 /* We don't support record checking in TLS because
TRodziewicz2abf03c2021-06-25 14:40:09 +0200328 * there doesn't seem to be a usecase for it.
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200329 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200331 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
332 goto exit;
333 }
334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 else {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200336 mbedtls_record rec;
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 ret = ssl_parse_record_header(ssl, buf, buflen, &rec);
339 if (ret != 0) {
340 MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200341 goto exit;
342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 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);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200348 goto exit;
349 }
350 }
351 }
352#endif /* MBEDTLS_SSL_PROTO_DTLS */
353
354exit:
355 /* On success, we have decrypted the buffer in-place, so make
356 * sure we don't leak any plaintext data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_platform_zeroize(buf, buflen);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200358
359 /* For the purpose of this API, treat messages with unexpected CID
360 * as well as such from future epochs as unexpected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
362 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200363 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record"));
367 return ret;
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200368}
369
Hanno Becker67bc7c32018-08-06 11:33:50 +0100370#define SSL_DONT_FORCE_FLUSH 0
371#define SSL_FORCE_FLUSH 1
372
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200373#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100374
Hanno Beckerd5847772018-08-28 10:09:23 +0100375/* Forward declarations for functions related to message buffering. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
377 uint8_t slot);
378static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200379MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100380static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100382static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200383MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100384static int ssl_buffer_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200385MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100386static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
387 mbedtls_record const *rec);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200388MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100389static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
Hanno Beckerd5847772018-08-28 10:09:23 +0100390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100392{
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
Darryl Greenb33cc762019-11-28 14:29:44 +0000394#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 Becker2b1e3542018-08-06 11:19:13 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (mtu != 0 && mtu < out_buf_len) {
401 return mtu;
402 }
Hanno Becker2b1e3542018-08-06 11:19:13 +0100403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return out_buf_len;
Hanno Becker2b1e3542018-08-06 11:19:13 +0100405}
406
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200407MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100408static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100409{
Hanno Becker11682cc2018-08-22 14:41:02 +0100410 size_t const bytes_written = ssl->out_left;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 size_t const mtu = ssl_get_maximum_datagram_size(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100412
413 /* Double-check that the write-index hasn't gone
414 * past what we can transmit in a single datagram. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (bytes_written > mtu) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100416 /* Should never happen... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100418 }
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return (int) (mtu - bytes_written);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100421}
422
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200423MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100424static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100425{
Janos Follath865b3eb2019-12-16 11:46:15 +0000426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100427 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400428 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100429
430#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (max_len > mfl) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100434 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100436
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 Peskine449bd832023-01-11 14:50:10 +0100446 if (max_len <= ssl->out_left) {
447 return 0;
448 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100449
450 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100451#endif
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 ret = ssl_get_remaining_space_in_datagram(ssl);
454 if (ret < 0) {
455 return ret;
456 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100457 remaining = (size_t) ret;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 ret = mbedtls_ssl_get_record_expansion(ssl);
460 if (ret < 0) {
461 return ret;
462 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100463 expansion = (size_t) ret;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (remaining <= expansion) {
466 return 0;
467 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100468
469 remaining -= expansion;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (remaining >= max_len) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100471 remaining = max_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return (int) remaining;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100475}
476
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200477/*
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é-Gonnarda3115dc2022-06-17 10:52:54 +0200481MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100482static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200483{
484 uint32_t new_timeout;
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) {
487 return -1;
488 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200489
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200490 /* 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 Peskine449bd832023-01-11 14:50:10 +0100496 if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200497 ssl->handshake->mtu = 508;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu));
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400499 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200500
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200501 new_timeout = 2 * ssl->handshake->retransmit_timeout;
502
503 /* Avoid arithmetic overflow and range overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (new_timeout < ssl->handshake->retransmit_timeout ||
505 new_timeout > ssl->conf->hs_timeout_max) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200506 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200507 }
508
509 ssl->handshake->retransmit_timeout = new_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
511 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return 0;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200514}
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200517{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200518 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
520 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200523
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100524/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200526 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000527
Ronald Cron6f135e12021-12-08 16:57:54 +0100528#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker13996922020-05-28 16:15:19 +0100529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530static size_t ssl_compute_padding_length(size_t len,
531 size_t granularity)
Hanno Becker13996922020-05-28 16:15:19 +0100532{
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return (granularity - (len + 1) % granularity) % granularity;
Hanno Becker13996922020-05-28 16:15:19 +0100534}
535
Hanno Becker581bc1b2020-05-04 12:20:03 +0100536/* 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 Becker8b3eb5a2019-04-29 17:31:37 +0100540 *
541 * struct {
542 * opaque content[DTLSPlaintext.length];
543 * ContentType real_type;
544 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100545 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100546 *
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 Becker581bc1b2020-05-04 12:20:03 +0100556 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
557 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100558 *
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é-Gonnarda3115dc2022-06-17 10:52:54 +0200564MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100565static 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 Becker8b3eb5a2019-04-29 17:31:37 +0100570{
571 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100572
573 /* Write real content type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (remaining == 0) {
575 return -1;
576 }
577 content[len] = rec_type;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100578 len++;
579 remaining--;
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (remaining < pad) {
582 return -1;
583 }
584 memset(content + len, 0, pad);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100585 len += pad;
586 remaining -= pad;
587
588 *content_size = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100590}
591
Hanno Becker581bc1b2020-05-04 12:20:03 +0100592/* This function parses a (D)TLSInnerPlaintext structure.
593 * See ssl_build_inner_plaintext() for details. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200594MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100595static int ssl_parse_inner_plaintext(unsigned char const *content,
596 size_t *content_size,
597 uint8_t *rec_type)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100598{
599 size_t remaining = *content_size;
600
601 /* Determine length of padding by skipping zeroes from the back. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 do {
603 if (remaining == 0) {
604 return -1;
605 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100606 remaining--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 } while (content[remaining] == 0);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100608
609 *content_size = remaining;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 *rec_type = content[remaining];
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100613}
Ronald Cron6f135e12021-12-08 16:57:54 +0100614#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100615
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200616/* 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 Peskine449bd832023-01-11 14:50:10 +0100667static 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 Becker9eddaeb2017-12-27 21:37:21 +0000673{
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200674 /* 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 Beckercab87e62019-04-29 13:52:53 +0100686 *
687 * additional_data = seq_num + TLSCompressed.type +
688 * TLSCompressed.version + TLSCompressed.length;
689 *
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100690 * 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 Becker79e2d1b2021-03-22 11:42:19 +0000692 * 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 Tschofenigfd6cca42021-10-12 09:22:33 +0200705 * --- 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 Peskine449bd832023-01-11 14:50:10 +0100761 */
Hanno Beckercab87e62019-04-29 13:52:53 +0100762
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100763 unsigned char *cur = add_data;
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000764 size_t ad_len_field = rec->data_len;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100765
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200766#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 Cron6f135e12021-12-08 16:57:54 +0100771#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000773 /* 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 Peskine449bd832023-01-11 14:50:10 +0100777 } else
Ronald Cron6f135e12021-12-08 16:57:54 +0100778#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100779 {
Glenn Strauss07c64162022-03-14 12:34:51 -0400780 ((void) tls_version);
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000781 ((void) taglen);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200782
Manuel Pégourié-Gonnard61336842022-11-25 11:12:38 +0100783#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
785 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200786 // seq_num_placeholder
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 memcpy(cur, seq_num_placeholder, sizeof(seq_num_placeholder));
788 cur += sizeof(seq_num_placeholder);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200789
790 // tls12_cid type
791 *cur = rec->type;
792 cur++;
793
794 // cid_length
795 *cur = rec->cid_len;
796 cur++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 } else
Manuel Pégourié-Gonnard61336842022-11-25 11:12:38 +0100798#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200799 {
800 // epoch + sequence number
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 memcpy(cur, rec->ctr, sizeof(rec->ctr));
802 cur += sizeof(rec->ctr);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200803 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100804 }
805
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200806 // type
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100807 *cur = rec->type;
808 cur++;
809
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200810 // version
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 memcpy(cur, rec->ver, sizeof(rec->ver));
812 cur += sizeof(rec->ver);
Hanno Beckercab87e62019-04-29 13:52:53 +0100813
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200814#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
815 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 1
816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200818 // CID
819 memcpy(cur, rec->cid, rec->cid_len);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100820 cur += rec->cid_len;
821
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200822 // cid_length
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100823 *cur = rec->cid_len;
824 cur++;
825
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200826 // length of inner plaintext
827 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
828 cur += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 } else
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200830#elif defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
831 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200834 // epoch + sequence number
835 memcpy(cur, rec->ctr, sizeof(rec->ctr));
836 cur += sizeof(rec->ctr);
837
838 // CID
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 memcpy(cur, rec->cid, rec->cid_len);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200840 cur += rec->cid_len;
841
842 // length of inner plaintext
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100844 cur += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100846#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100847 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100849 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100850 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100851
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000852 *add_data_len = (size_t) (cur - add_data);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000853}
854
Valerio Settie5707042023-10-11 11:54:42 +0200855#if defined(MBEDTLS_SSL_HAVE_AEAD)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200856MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker17263802020-05-28 07:05:48 +0100857static int ssl_transform_aead_dynamic_iv_is_explicit(
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 mbedtls_ssl_transform const *transform)
Hanno Beckerdf8be222020-05-21 15:30:57 +0100859{
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return transform->ivlen != transform->fixed_ivlen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100861}
862
Hanno Becker17263802020-05-28 07:05:48 +0100863/* 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 Becker15952812020-06-04 13:31:46 +0100870 * This variant is used in TLS 1.2 when used with GCM or CCM.
871 *
Hanno Becker17263802020-05-28 07:05:48 +0100872 * b) Fixed IV lengths matches total IV length, giving
873 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100874 *
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 Beckerf486e282020-06-04 13:33:08 +0100878 *
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 Becker17263802020-05-28 07:05:48 +0100885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886static 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 Becker17263802020-05-28 07:05:48 +0100892{
Hanno Beckerdf8be222020-05-21 15:30:57 +0100893 /* Start with Fixed IV || 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 memset(dst_iv, 0, dst_iv_len);
895 memcpy(dst_iv, fixed_iv, fixed_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100896
Hanno Becker17263802020-05-28 07:05:48 +0100897 dst_iv += dst_iv_len - dynamic_iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_xor(dst_iv, dst_iv, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100899}
Valerio Settie5707042023-10-11 11:54:42 +0200900#endif /* MBEDTLS_SSL_HAVE_AEAD */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902int 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 Bakker5121ce52009-01-03 21:22:43 +0000907{
Neil Armstrong136f8402022-03-30 10:58:01 +0200908 mbedtls_ssl_mode_t ssl_mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100909 int auth_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 unsigned char *data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200911 /* For an explanation of the additional data length see
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 * the description of ssl_extract_add_data_from_record().
913 */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200914#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 Beckercab87e62019-04-29 13:52:53 +0100919 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000920 size_t post_avail;
921
922 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000923#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200924 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000925 ((void) ssl);
926#endif
927
928 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200929 * for CBC transformations in TLS 1.2. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930#if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
931 defined(MBEDTLS_SSL_PROTO_TLS1_2))
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000932 ((void) f_rng);
933 ((void) p_rng);
934#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (transform == NULL) {
939 MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf"));
940 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000941 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (rec == NULL
Hanno Becker43c24b82019-05-01 09:45:57 +0100943 || rec->buf == NULL
944 || rec->buf_len < rec->data_offset
945 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100946#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100947 || rec->cid_len != 0
948#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 ) {
950 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf"));
951 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100952 }
953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
Neil Armstrong136f8402022-03-30 10:58:01 +0200955
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000956 data = rec->buf + rec->data_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 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 Becker9eddaeb2017-12-27 21:37:21 +0000960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 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 Becker9eddaeb2017-12-27 21:37:21 +0000967 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100968
Hanno Becker92313402020-05-20 13:58:58 +0100969 /* 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 Cron6f135e12021-12-08 16:57:54 +0100981#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Becker13996922020-05-28 16:15:19 +0100983 size_t padding =
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 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 Beckerccc13d02020-05-04 12:30:04 +0100992 }
993
994 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
995 }
Ronald Cron6f135e12021-12-08 16:57:54 +0100996#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100997
Hanno Beckera0e20d02019-05-15 14:03:01 +0100998#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100999 /*
1000 * Add CID information
1001 */
1002 rec->cid_len = transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 memcpy(rec->cid, transform->out_cid, transform->out_cid_len);
1004 MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (rec->cid_len != 0) {
Hanno Becker13996922020-05-28 16:15:19 +01001007 size_t padding =
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 ssl_compute_padding_length(rec->data_len,
1009 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001010 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01001011 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +01001012 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001013 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01001014 * Note that this changes `rec->data_len`, and hence
1015 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 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 Becker8b3eb5a2019-04-29 17:31:37 +01001023 }
1024
1025 rec->type = MBEDTLS_SSL_MSG_CID;
1026 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001027#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001030
Paul Bakker5121ce52009-01-03 21:22:43 +00001031 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001032 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001034#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 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 Becker9eddaeb2017-12-27 21:37:21 +00001040 }
TRodziewicz0f82ec62021-05-12 17:49:18 +02001041#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001042 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong26e6d672022-02-23 09:30:33 +01001044#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 Becker992b6872017-11-09 18:57:39 +00001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1051 transform->tls_version,
1052 transform->taglen);
Hanno Becker992b6872017-11-09 18:57:39 +00001053
Neil Armstrong26e6d672022-02-23 09:30:33 +01001054#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
1056 transform->psa_mac_alg);
1057 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001058 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 status = psa_mac_update(&operation, add_data, add_data_len);
1062 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001063 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 status = psa_mac_update(&operation, data, rec->data_len);
1067 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001068 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
1072 &sign_mac_length);
1073 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001074 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001076#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
1078 add_data_len);
1079 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001080 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
1082 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len);
1083 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001084 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 }
1086 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
1087 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001088 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 }
1090 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
1091 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001092 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001094#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 memcpy(data + rec->data_len, mac, transform->maclen);
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001097#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len,
1100 transform->maclen);
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001101
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001102 rec->data_len += transform->maclen;
1103 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001104 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106hmac_failed_etm_disabled:
1107 mbedtls_platform_zeroize(mac, transform->maclen);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001108#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001109 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 status = psa_mac_abort(&operation);
1111 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001112 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 }
Neil Armstrong4313f552022-03-02 15:14:07 +01001114#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (ret != 0) {
1116 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
1117 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001118 }
Paul Bakker577e0062013-08-28 11:57:20 +02001119 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001120#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001121
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001122 /*
1123 * Encrypt
1124 */
Hanno Beckerd086bf02021-03-22 13:01:27 +00001125#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 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 Bakker5121ce52009-01-03 21:22:43 +00001130
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +01001131 /* The only supported stream cipher is "NULL",
1132 * so there's nothing to do here.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 } else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001134#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001135
Valerio Settie5707042023-10-11 11:54:42 +02001136#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001138 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001139 unsigned char *dynamic_iv;
1140 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +01001141 int dynamic_iv_is_explicit =
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 ssl_transform_aead_dynamic_iv_is_explicit(transform);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001143#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001144 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001145#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001147
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +01001148 /* Check that there's space for the authentication tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 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 Becker9eddaeb2017-12-27 21:37:21 +00001152 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001153
Paul Bakker68884e32013-01-07 18:20:04 +01001154 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001155 * 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 Bakker68884e32013-01-07 18:20:04 +01001165 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001166 dynamic_iv = rec->ctr;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 dynamic_iv_len = sizeof(rec->ctr);
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 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é-Gonnardd056ce02014-10-29 22:29:20 +01001174
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001175 /*
1176 * Build additional data for AEAD encryption.
1177 * This depends on the TLS version.
1178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1180 transform->tls_version,
1181 transform->taglen);
Hanno Becker1f10d762019-04-26 13:34:37 +01001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 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 Bakkerca4ab492012-04-18 14:23:57 +00001193
Paul Bakker68884e32013-01-07 18:20:04 +01001194 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001195 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001196 */
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001197#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 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 Becker9eddaeb2017-12-27 21:37:21 +00001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001207 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret);
1209 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001210 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001211#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 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 Rodgmane4a6f5a2023-11-04 12:20:09 +00001216 data, rec->buf_len - (size_t) (data - rec->buf), /* dst */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 &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é-Gonnardd13a4092013-09-05 16:10:41 +02001221 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001222#endif /* MBEDTLS_USE_PSA_CRYPTO */
1223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
1225 data + rec->data_len - transform->taglen,
1226 transform->taglen);
Hanno Beckerdf8be222020-05-21 15:30:57 +01001227 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001228 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001229
1230 /*
1231 * Prefix record content with dynamic IV in case it is explicit.
1232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 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 Beckerdf8be222020-05-21 15:30:57 +01001237 }
1238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +01001240 rec->data_offset -= dynamic_iv_len;
1241 rec->data_len += dynamic_iv_len;
1242 }
1243
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001244 auth_done++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 } else
Valerio Settie5707042023-10-11 11:54:42 +02001246#endif /* MBEDTLS_SSL_HAVE_AEAD */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001247#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1249 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001251 size_t padlen, i;
1252 size_t olen;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001253#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001254 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001255 size_t part_len;
1256 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1257#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001258
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001259 /* Currently we're always using minimal padding
1260 * (up to 255 bytes would be allowed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen;
1262 if (padlen == transform->ivlen) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 padlen = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001264 }
1265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 /* 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 Becker9eddaeb2017-12-27 21:37:21 +00001273 data[rec->data_len + i] = (unsigned char) padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001276 rec->data_len += padlen + 1;
1277 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001278
TRodziewicz0f82ec62021-05-12 17:49:18 +02001279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001280 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +02001281 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +00001282 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 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 Bakker2e11f7d2010-07-25 14:24:53 +00001287 }
TRodziewicz345165c2021-07-06 13:42:11 +02001288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 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;
TRodziewicz345165c2021-07-06 13:42:11 +02001292 }
1293
1294 /*
1295 * Generate IV
1296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 ret = f_rng(p_rng, transform->iv_enc, transform->ivlen);
1298 if (ret != 0) {
1299 return ret;
1300 }
TRodziewicz345165c2021-07-06 13:42:11 +02001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 memcpy(data - transform->ivlen, transform->iv_enc, transform->ivlen);
TRodziewicz0f82ec62021-05-12 17:49:18 +02001303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 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 Bakker5121ce52009-01-03 21:22:43 +00001311
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001312#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 status = psa_cipher_encrypt_setup(&cipher_op,
1314 transform->psa_key_enc, transform->psa_alg);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001317 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_encrypt_setup", ret);
1319 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001320 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 status = psa_cipher_set_iv(&cipher_op, transform->iv_enc, transform->ivlen);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001325 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
1327 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001328
1329 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 status = psa_cipher_update(&cipher_op,
1332 data, rec->data_len,
1333 data, rec->data_len, &olen);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001336 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
1338 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001339
1340 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 status = psa_cipher_finish(&cipher_op,
1343 data + olen, rec->data_len - olen,
1344 &part_len);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001347 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
1349 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001350
1351 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001352
1353 olen += part_len;
1354#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 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 Bakkercca5b812013-08-31 17:40:26 +02001362 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001363#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 if (rec->data_len != olen) {
1366 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1367 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001368 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001369
TRodziewicz0f82ec62021-05-12 17:49:18 +02001370 data -= transform->ivlen;
1371 rec->data_offset -= transform->ivlen;
1372 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if (auth_done == 0) {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001376 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001377#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 Becker3d8c9072018-01-05 16:24:22 +00001381
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001382 /* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length))
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 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 Becker9eddaeb2017-12-27 21:37:21 +00001388 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 ssl_extract_add_data_from_record(add_data, &add_data_len,
1391 rec, transform->tls_version,
1392 transform->taglen);
Hanno Becker1f10d762019-04-26 13:34:37 +01001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 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 Armstrong26e6d672022-02-23 09:30:33 +01001397#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
1399 transform->psa_mac_alg);
1400 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001401 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 status = psa_mac_update(&operation, add_data, add_data_len);
1405 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001406 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 status = psa_mac_update(&operation, data, rec->data_len);
1410 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001411 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
1415 &sign_mac_length);
1416 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001417 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001419#else
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
1422 add_data_len);
1423 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001424 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 }
1426 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
1427 data, rec->data_len);
1428 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001429 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 }
1431 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
1432 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001433 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 }
1435 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
1436 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001437 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001439#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 memcpy(data + rec->data_len, mac, transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001442
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001443 rec->data_len += transform->maclen;
1444 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001445 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447hmac_failed_etm_enabled:
1448 mbedtls_platform_zeroize(mac, transform->maclen);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001449#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001450 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 status = psa_mac_abort(&operation);
1452 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001453 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 }
Neil Armstrong4313f552022-03-02 15:14:07 +01001455#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (ret != 0) {
1457 MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
1458 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001459 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001460 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001463#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001464 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1466 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001468
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001469 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (auth_done != 1) {
1471 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1472 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001473 }
1474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001478}
1479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1481 mbedtls_ssl_transform *transform,
1482 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00001483{
Valerio Settie5707042023-10-11 11:54:42 +02001484#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) || defined(MBEDTLS_SSL_HAVE_AEAD)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001485 size_t olen;
Valerio Settie5707042023-10-11 11:54:42 +02001486#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC || MBEDTLS_SSL_HAVE_AEAD */
Neil Armstrong136f8402022-03-30 10:58:01 +02001487 mbedtls_ssl_mode_t ssl_mode;
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001488 int ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001489
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001490 int auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001491#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01001492 size_t padlen = 0;
1493 mbedtls_ct_condition_t correct = MBEDTLS_CT_TRUE;
Paul Bakker1e5369c2013-12-19 16:40:57 +01001494#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 unsigned char *data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001496 /* For an explanation of the additional data length see
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 * the description of ssl_extract_add_data_from_record().
1498 */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001499#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 Beckercab87e62019-04-29 13:52:53 +01001504 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001505
Hanno Beckera18d1322018-01-03 14:27:32 +00001506#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001507 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001508 ((void) ssl);
1509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf"));
1512 if (rec == NULL ||
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001513 rec->buf == NULL ||
1514 rec->buf_len < rec->data_offset ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 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é-Gonnard352143f2015-01-13 10:59:51 +01001518 }
1519
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001520 data = rec->buf + rec->data_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
Paul Bakker5121ce52009-01-03 21:22:43 +00001522
Hanno Beckera0e20d02019-05-15 14:03:01 +01001523#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001524 /*
1525 * Match record's CID with incoming CID.
1526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 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 Becker938489a2019-05-08 13:02:22 +01001530 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001531#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001532
Hanno Beckerd086bf02021-03-22 13:01:27 +00001533#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Gilles Peskinefaf0b862023-09-18 14:08:11 +02001535 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 Stekielc8a06fe2022-02-07 10:52:47 +01001543 /* The only supported stream cipher is "NULL",
Gilles Peskinefaf0b862023-09-18 14:08:11 +02001544 * so there's no encryption to do here.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 } else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001546#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Valerio Settie5707042023-10-11 11:54:42 +02001547#if defined(MBEDTLS_SSL_HAVE_AEAD)
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001549 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001550 unsigned char *dynamic_iv;
1551 size_t dynamic_iv_len;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001552#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001553 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001554#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001555
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001556 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001557 * 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é-Gonnard2e58e8e2018-06-18 11:16:43 +02001563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 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 Beckerdf8be222020-05-21 15:30:57 +01001572 }
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 Peskine449bd832023-01-11 14:50:10 +01001578 } else {
Hanno Becker17263802020-05-28 07:05:48 +01001579 dynamic_iv = rec->ctr;
1580 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001581
1582 /* Check that there's space for the authentication tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 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é-Gonnard0bcc4e12014-06-17 10:54:17 +02001589 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001590 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001591
Hanno Beckerdf8be222020-05-21 15:30:57 +01001592 /*
1593 * Prepare nonce from dynamic and static parts.
1594 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ssl_build_record_nonce(iv, sizeof(iv),
1596 transform->iv_dec,
1597 transform->fixed_ivlen,
1598 dynamic_iv,
1599 dynamic_iv_len);
Paul Bakker68884e32013-01-07 18:20:04 +01001600
Hanno Beckerdf8be222020-05-21 15:30:57 +01001601 /*
1602 * Build additional data for AEAD encryption.
1603 * This depends on the TLS version.
1604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 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 Becker2e24c3b2017-12-27 21:28:58 +00001610
Hanno Beckerd96a6522019-07-10 13:55:25 +01001611 /* Because of the check above, we know that there are
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001612 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001613 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001614 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001615 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 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 Bakker68884e32013-01-07 18:20:04 +01001620
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001621 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001622 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001623 */
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001624#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 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 Stekiel2e9711f2022-01-13 14:50:15 +01001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001634 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret);
1636 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001637 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001638#else
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00001639 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 Peskine449bd832023-01-11 14:50:10 +01001646 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret);
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001647
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
1649 return MBEDTLS_ERR_SSL_INVALID_MAC;
1650 }
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001653 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001654#endif /* MBEDTLS_USE_PSA_CRYPTO */
1655
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001656 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001657
Hanno Beckerd96a6522019-07-10 13:55:25 +01001658 /* Double-check that AEAD decryption doesn't change content length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (olen != rec->data_len) {
1660 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1661 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001662 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 } else
Valerio Settie5707042023-10-11 11:54:42 +02001664#endif /* MBEDTLS_SSL_HAVE_AEAD */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001665#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1667 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001668 size_t minlen = 0;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001669#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001670 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001671 size_t part_len;
1672 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1673#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001674
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 /*
Paul Bakker45829992013-01-03 14:52:21 +01001676 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001678#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001679 /* The ciphertext is prefixed with the CBC IV. */
1680 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001681#endif
Paul Bakker45829992013-01-03 14:52:21 +01001682
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001683 /* 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 Peskine449bd832023-01-11 14:50:10 +01001704 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 Bakker45829992013-01-03 14:52:21 +01001714 }
1715
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001716 /*
1717 * Authenticate before decrypt if enabled
1718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001721#if defined(MBEDTLS_USE_PSA_CRYPTO)
1722 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1723#else
Hanno Becker992b6872017-11-09 18:57:39 +00001724 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001725#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001728
Hanno Beckerd96a6522019-07-10 13:55:25 +01001729 /* 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 Mezei90437e32021-10-20 11:59:27 +02001736 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001737 *
1738 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001739 rec->data_len -= transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1741 transform->tls_version,
1742 transform->taglen);
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001743
Hanno Beckerd96a6522019-07-10 13:55:25 +01001744 /* Calculate expected MAC. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1746 add_data_len);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001747#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 status = psa_mac_verify_setup(&operation, transform->psa_mac_dec,
1749 transform->psa_mac_alg);
1750 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001751 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 status = psa_mac_update(&operation, add_data, add_data_len);
1755 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001756 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 status = psa_mac_update(&operation, data, rec->data_len);
1760 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001761 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001763
1764 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 status = psa_mac_verify_finish(&operation, data + rec->data_len,
1766 transform->maclen);
1767 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001768 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001770#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
1772 add_data_len);
1773 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001774 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 }
1776 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
1777 data, rec->data_len);
1778 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001779 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 }
1781 ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
1782 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001783 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 }
1785 ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
1786 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001787 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 }
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 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é-Gonnard313d7962014-10-29 12:07:57 +01001794
Hanno Beckerd96a6522019-07-10 13:55:25 +01001795 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 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 Peskined5ba50e2021-12-10 21:33:21 +01001799 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1800 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001801 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001802#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001803 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805hmac_failed_etm_enabled:
Neil Armstrong26e6d672022-02-23 09:30:33 +01001806#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001807 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001808 status = psa_mac_abort(&operation);
1809 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001810 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001812#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 mbedtls_platform_zeroize(mac_expect, transform->maclen);
Neil Armstrong4313f552022-03-02 15:14:07 +01001814#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 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 Peskineecf6beb2021-12-10 21:35:10 +01001820 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001823
1824 /*
1825 * Check length sanity
1826 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001827
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 Peskine449bd832023-01-11 14:50:10 +01001831 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é-Gonnard313d7962014-10-29 12:07:57 +01001836 }
1837
TRodziewicz0f82ec62021-05-12 17:49:18 +02001838#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001839 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001840 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001841 */
TRodziewicz345165c2021-07-06 13:42:11 +02001842 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 memcpy(transform->iv_dec, data, transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001844
TRodziewicz345165c2021-07-06 13:42:11 +02001845 data += transform->ivlen;
1846 rec->data_offset += transform->ivlen;
1847 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001848#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001849
Hanno Beckerd96a6522019-07-10 13:55:25 +01001850 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1851
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001852#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 status = psa_cipher_decrypt_setup(&cipher_op,
1854 transform->psa_key_dec, transform->psa_alg);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001857 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_decrypt_setup", ret);
1859 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001860 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 status = psa_cipher_set_iv(&cipher_op, transform->iv_dec, transform->ivlen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001863
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001865 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
1867 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001868 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 status = psa_cipher_update(&cipher_op,
1871 data, rec->data_len,
1872 data, rec->data_len, &olen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001875 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
1877 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001878 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 status = psa_cipher_finish(&cipher_op,
1881 data + olen, rec->data_len - olen,
1882 &part_len);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001885 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
1887 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001888 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001889
1890 olen += part_len;
1891#else
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 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 Bakkercca5b812013-08-31 17:40:26 +02001898 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001899#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001900
Hanno Beckerd96a6522019-07-10 13:55:25 +01001901 /* Double-check that length hasn't changed during decryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (rec->data_len != olen) {
1903 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1904 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001905 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001906
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001907 /* Safe since data_len >= minlen + maclen + 1, so after having
1908 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001909 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1910 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001911 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 if (auth_done == 1) {
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001914 const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge(
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 rec->data_len,
1916 padlen + 1);
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01001917 correct = mbedtls_ct_bool_and(ge, correct);
Dave Rodgman98ddc012023-08-10 12:11:31 +01001918 padlen = mbedtls_ct_size_if_else_0(ge, padlen);
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 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 Becker2e24c3b2017-12-27 21:28:58 +00001928 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001929#endif
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001930 const mbedtls_ct_condition_t ge = mbedtls_ct_uint_ge(
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 rec->data_len,
1932 transform->maclen + padlen + 1);
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01001933 correct = mbedtls_ct_bool_and(ge, correct);
Dave Rodgman98ddc012023-08-10 12:11:31 +01001934 padlen = mbedtls_ct_size_if_else_0(ge, padlen);
Paul Bakker45829992013-01-03 14:52:21 +01001935 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001937 padlen++;
1938
1939 /* Regardless of the validity of the padding,
1940 * we have data_len >= padlen here. */
1941
TRodziewicz0f82ec62021-05-12 17:49:18 +02001942#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001943 /* The padding check involves a series of up to 256
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 * 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 Starzyk06b07fb2021-02-18 13:55:21 +01001949 size_t pad_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 volatile unsigned char * const check = data;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001951
1952 /* Index of first padding byte; it has been ensured above
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 * that the subtraction is safe. */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001954 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 Peskine449bd832023-01-11 14:50:10 +01001959 for (idx = start_idx; idx < rec->data_len; idx++) {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001960 /* pad_count += (idx >= padding_idx) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 * (check[idx] == padlen - 1);
1962 */
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001963 const mbedtls_ct_condition_t a = mbedtls_ct_uint_ge(idx, padding_idx);
Dave Rodgman98ddc012023-08-10 12:11:31 +01001964 size_t increment = mbedtls_ct_size_if_else_0(a, 1);
Dave Rodgmanb7825ce2023-08-10 11:58:18 +01001965 const mbedtls_ct_condition_t b = mbedtls_ct_uint_eq(check[idx], padlen - 1);
Dave Rodgman98ddc012023-08-10 12:11:31 +01001966 increment = mbedtls_ct_size_if_else_0(b, increment);
Dave Rodgmana81373f2023-05-17 12:36:01 +01001967 pad_count += increment;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001968 }
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01001969 correct = mbedtls_ct_bool_and(mbedtls_ct_uint_eq(pad_count, padlen), correct);
Paul Bakkere47b34b2013-02-27 14:48:00 +01001970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001971#if defined(MBEDTLS_SSL_DEBUG_ALL)
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01001972 if (padlen > 0 && correct == MBEDTLS_CT_FALSE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
1974 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001975#endif
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01001976 padlen = mbedtls_ct_size_if_else_0(correct, padlen);
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001977
TRodziewicz0f82ec62021-05-12 17:49:18 +02001978#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001979
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001980 /* 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 Peskine449bd832023-01-11 14:50:10 +01001985 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001986#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001987 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1989 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001990 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001991
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001992#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
1994 data, rec->data_len);
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001995#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
1997 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001998 * Authenticate if not done yet.
1999 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002000 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00002001#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (auth_done == 0) {
Paul Elliott5260ce22022-05-09 18:15:54 +01002003 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
2004 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01002005
Gilles Peskinefaf0b862023-09-18 14:08:11 +02002006 /* For CBC+MAC, If the initial value of padlen was such that
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002007 * 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 Peskinefaf0b862023-09-18 14:08:11 +02002018 *
2019 * For stream ciphers, we checked above that
2020 * data_len >= maclen.
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002021 */
2022 rec->data_len -= transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
2024 transform->tls_version,
2025 transform->taglen);
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
TRodziewicz0f82ec62021-05-12 17:49:18 +02002027#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002028 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 * 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 Starzyk06b07fb2021-02-18 13:55:21 +01002038 const size_t max_len = rec->data_len + padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002040
Neil Armstronge8589962022-02-25 15:14:29 +01002041#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 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 Armstronge8589962022-02-25 15:14:29 +01002047#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 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 Armstronge8589962022-02-25 15:14:29 +01002052#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 if (ret != 0) {
2054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
Gilles Peskined5ba50e2021-12-10 21:33:21 +01002055 goto hmac_failed_etm_disabled;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002056 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 mbedtls_ct_memcpy_offset(mac_peer, data,
2059 rec->data_len,
2060 min_len, max_len,
2061 transform->maclen);
TRodziewicz0f82ec62021-05-12 17:49:18 +02002062#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002063
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002064#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 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é-Gonnard7b420302018-06-28 10:38:35 +02002067#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (mbedtls_ct_memcmp(mac_peer, mac_expect,
2070 transform->maclen) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Paul Bakkere47b34b2013-02-27 14:48:00 +01002073#endif
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01002074 correct = MBEDTLS_CT_FALSE;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002075 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002076 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078hmac_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é-Gonnard71096242013-10-25 19:31:25 +02002084 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01002085
2086 /*
2087 * Finally check the correct flag
2088 */
Dave Rodgman7d52f2a2023-09-12 16:29:39 +01002089 if (correct == MBEDTLS_CT_FALSE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return MBEDTLS_ERR_SSL_INVALID_MAC;
2091 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +00002092#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002093
2094 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (auth_done != 1) {
2096 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2097 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002098 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002099
Ronald Cron6f135e12021-12-08 16:57:54 +01002100#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Beckerccc13d02020-05-04 12:30:04 +01002102 /* Remove inner padding and infer true content type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
2104 &rec->type);
Hanno Beckerccc13d02020-05-04 12:30:04 +01002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 if (ret != 0) {
2107 return MBEDTLS_ERR_SSL_INVALID_RECORD;
2108 }
Hanno Beckerccc13d02020-05-04 12:30:04 +01002109 }
Ronald Cron6f135e12021-12-08 16:57:54 +01002110#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +01002111
Hanno Beckera0e20d02019-05-15 14:03:01 +01002112#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 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 Becker8b3eb5a2019-04-29 17:31:37 +01002119 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002120#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002125}
2126
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002127#undef MAC_NONE
2128#undef MAC_PLAINTEXT
2129#undef MAC_CIPHERTEXT
2130
Paul Bakker5121ce52009-01-03 21:22:43 +00002131/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002132 * 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é-Gonnardfe98ace2014-03-24 13:13:01 +01002134 *
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é-Gonnardb2f3be82014-07-10 17:54:52 +02002139 * 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é-Gonnard64dffc52014-09-02 13:39:16 +02002143 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002144 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
Paul Bakker5121ce52009-01-03 21:22:43 +00002147{
Janos Follath865b3eb2019-12-16 11:46:15 +00002148 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002149 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00002150#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 Bakker5121ce52009-01-03 21:22:43 +00002155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 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é-Gonnarde6bdc442014-09-17 11:34:57 +02002161 }
2162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 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 Bakker1a1fbba2014-04-30 14:38:05 +02002166 }
2167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002170 uint32_t timeout;
2171
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002172 /*
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 Peskine449bd832023-01-11 14:50:10 +01002182 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é-Gonnardb2f3be82014-07-10 17:54:52 +02002186 }
2187
2188 ssl->in_left -= ssl->next_record_offset;
2189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 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é-Gonnardb2f3be82014-07-10 17:54:52 +02002197 }
2198
2199 ssl->next_record_offset = 0;
2200 }
2201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 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é-Gonnardfe98ace2014-03-24 13:13:01 +01002205
2206 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002207 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (nb_want <= ssl->in_left) {
2210 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
2211 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002212 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002213
2214 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002215 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002216 * are not at the beginning of a new record, the caller did something
2217 * wrong.
2218 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (ssl->in_left != 0) {
2220 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2221 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002222 }
2223
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002224 /*
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 Peskine449bd832023-01-11 14:50:10 +01002229 if (mbedtls_ssl_check_timer(ssl) != 0) {
2230 MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002231 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 } else {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002233 len = in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002236 timeout = ssl->handshake->retransmit_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 } else {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002238 timeout = ssl->conf->read_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 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é-Gonnard4e2f2452014-10-02 16:51:56 +02002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (ret == 0) {
2253 return MBEDTLS_ERR_SSL_CONN_EOF;
2254 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002255 }
2256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
2258 MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
2259 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 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é-Gonnard6b651412014-10-01 18:29:03 +02002265 }
2266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
2268 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
2269 return ret;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002270 }
2271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002273 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 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é-Gonnard26a4cf62014-10-15 13:52:48 +02002281 }
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002284 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002286 }
2287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (ret < 0) {
2289 return ret;
2290 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002291
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002292 ssl->in_left = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 } else
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002294#endif
2295 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 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é-Gonnardb2f3be82014-07-10 17:54:52 +02002299
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 while (ssl->in_left < nb_want) {
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002301 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 if (mbedtls_ssl_check_timer(ssl) != 0) {
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002304 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 } 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é-Gonnard07617332015-06-24 23:00:03 +02002313 }
2314 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 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é-Gonnardfe98ace2014-03-24 13:13:01 +01002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 if (ret == 0) {
2322 return MBEDTLS_ERR_SSL_CONN_EOF;
2323 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (ret < 0) {
2326 return ret;
2327 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002328
Dave Rodgman4a5c9ee2023-02-10 16:03:44 +00002329 if ((size_t) ret > len) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 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;
mohammad16035bd15cb2018-02-28 04:30:59 -08002335 }
2336
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002337 ssl->in_left += ret;
2338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 }
2340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002344}
2345
2346/*
2347 * Flush any data not yet written
2348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002350{
Janos Follath865b3eb2019-12-16 11:46:15 +00002351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002352 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 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é-Gonnarde6bdc442014-09-17 11:34:57 +02002359 }
2360
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002361 /* Avoid incrementing counter if data is flushed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (ssl->out_left == 0) {
2363 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
2364 return 0;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002365 }
2366
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 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 Bakker5121ce52009-01-03 21:22:43 +00002371
Hanno Becker2b1e3542018-08-06 11:19:13 +01002372 buf = ssl->out_hdr - ssl->out_left;
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
Paul Bakker186751d2012-05-08 13:16:14 +00002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if (ret <= 0) {
2378 return ret;
2379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
Dave Rodgman4a5c9ee2023-02-10 16:03:44 +00002381 if ((size_t) ret > ssl->out_left) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 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;
mohammad16034bbaeb42018-02-22 04:29:04 -08002387 }
2388
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 ssl->out_left -= ret;
2390 }
2391
Hanno Becker2b1e3542018-08-06 11:19:13 +01002392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002394 ssl->out_hdr = ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 } else
Hanno Becker2b1e3542018-08-06 11:19:13 +01002396#endif
2397 {
2398 ssl->out_hdr = ssl->out_buf + 8;
2399 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002405}
2406
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002407/*
2408 * Functions to handle the DTLS retransmission state machine
2409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002411/*
2412 * Append current handshake message to current outgoing flight
2413 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002414MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002415static int ssl_flight_append(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 mbedtls_ssl_flight_item *msg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 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é-Gonnardffa67be2014-09-19 11:18:57 +02002421
2422 /* Allocate space for current message */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 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é-Gonnardffa67be2014-09-19 11:18:57 +02002427 }
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 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é-Gonnardffa67be2014-09-19 11:18:57 +02002434 }
2435
2436 /* Copy current handshake message with headers */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002438 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002439 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002440 msg->next = NULL;
2441
2442 /* Append to the current flight */
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 if (ssl->handshake->flight == NULL) {
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002444 ssl->handshake->flight = msg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002446 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 while (cur->next != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002448 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002450 cur->next = msg;
2451 }
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
2454 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002455}
2456
2457/*
2458 * Free the current flight of handshake messages
2459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002460void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 mbedtls_ssl_flight_item *cur = flight;
2463 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002464
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 while (cur != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002466 next = cur->next;
2467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 mbedtls_free(cur->p);
2469 mbedtls_free(cur);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002470
2471 cur = next;
2472 }
2473}
2474
2475/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002476 * Swap transform_out and out_ctr with the alternative ones
2477 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002479static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 mbedtls_ssl_transform *tmp_transform;
Jerry Yuae0b2e22021-10-08 15:21:19 +08002482 unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 if (ssl->transform_out == ssl->handshake->alt_transform_out) {
2485 MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
2486 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002487 }
2488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002490
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002491 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002492 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é-Gonnardc715aed2014-09-19 21:39:13 +02002496 /* Swap epoch + sequence_number */
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 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é-Gonnardc715aed2014-09-19 21:39:13 +02002502
2503 /* Adjust to the newly activated transform */
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002507}
2508
2509/*
2510 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002512int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002513{
2514 int ret = 0;
2515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 ret = mbedtls_ssl_flight_transmit(ssl);
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002523}
2524
2525/*
2526 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002527 *
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é-Gonnard5d8ba532014-09-19 15:09:21 +02002530 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002533{
Janos Follath865b3eb2019-12-16 11:46:15 +00002534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
2538 MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002539
2540 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002541 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 ret = ssl_swap_epochs(ssl);
2543 if (ret != 0) {
2544 return ret;
2545 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002548 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 while (ssl->handshake->cur_msg != NULL) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002551 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002552 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002553
Hanno Beckere1dcb032018-08-17 16:47:58 +01002554 int const is_finished =
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2556 cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
Hanno Beckere1dcb032018-08-17 16:47:58 +01002557
Ronald Cron00d012f22022-03-08 15:57:12 +01002558 int const force_flush = ssl->disable_datagram_packing == 1 ?
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
Hanno Becker04da1892018-08-14 13:22:10 +01002560
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002561 /* 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 Peskine449bd832023-01-11 14:50:10 +01002564 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é-Gonnardc715aed2014-09-19 21:39:13 +02002570 }
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 ret = ssl_get_remaining_payload_in_datagram(ssl);
2573 if (ret < 0) {
2574 return ret;
2575 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002576 max_frag_len = (size_t) ret;
2577
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002578 /* CCS is copied as is, while HS messages may need fragmentation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 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 Becker67bc7c32018-08-06 11:33:50 +01002584
2585 continue;
2586 }
2587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 memcpy(ssl->out_msg, cur->p, cur->len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002589 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002590 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002591
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002592 /* Update position inside current message */
2593 ssl->handshake->cur_msg_p += cur->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002595 const unsigned char * const p = ssl->handshake->cur_msg_p;
2596 const size_t hs_len = cur->len - 12;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002597 const size_t frag_off = (size_t) (p - (cur->p + 12));
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002598 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002599 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 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é-Gonnarde07bc202020-02-26 09:53:42 +01002607 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2610 return ret;
2611 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002612
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 Peskine449bd832023-01-11 14:50:10 +01002618 max_hs_frag_len : rem_len;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 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é-Gonnard19c62f92018-08-16 10:50:39 +02002624 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002625
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002626 /* 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 Peskine449bd832023-01-11 14:50:10 +01002629 memcpy(ssl->out_msg, cur->p, 6);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 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é-Gonnard28f4bea2017-09-13 14:00:05 +02002634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 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é-Gonnard28f4bea2017-09-13 14:00:05 +02002638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002640
Hanno Becker3f7b9732018-08-28 09:53:25 +01002641 /* Copy the handshake message content and set records fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002643 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002644 ssl->out_msgtype = cur->type;
2645
2646 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002647 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002648 }
2649
2650 /* If done with the current message move to the next one if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
2652 if (cur->next != NULL) {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002653 ssl->handshake->cur_msg = cur->next;
2654 ssl->handshake->cur_msg_p = cur->next->p + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002656 ssl->handshake->cur_msg = NULL;
2657 ssl->handshake->cur_msg_p = NULL;
2658 }
2659 }
2660
2661 /* Actually send the message out */
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 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é-Gonnardffa67be2014-09-19 11:18:57 +02002665 }
2666 }
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2669 return ret;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002670 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 /* 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é-Gonnardffa67be2014-09-19 11:18:57 +02002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
2681
2682 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002683}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002684
2685/*
2686 * To be called when the last message of an incoming flight is received.
2687 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002688void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002689{
2690 /* We won't need to resend that one any more */
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 mbedtls_ssl_flight_free(ssl->handshake->flight);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002692 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 Becker2ed6bcc2018-08-15 15:11:57 +01002698 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002699 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002700
Hanno Becker0271f962018-08-16 13:23:47 +01002701 /* Clear future message buffering structure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 mbedtls_ssl_buffering_free(ssl);
Hanno Becker0271f962018-08-16 13:23:47 +01002703
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002704 /* Cancel timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2708 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002713}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002714
2715/*
2716 * To be called when the last message of an outgoing flight is send.
2717 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002718void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002719{
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 ssl_reset_retransmit_timeout(ssl);
2721 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2724 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002731
Paul Bakker5121ce52009-01-03 21:22:43 +00002732/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002733 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 */
Dave Rodgmanc37ad442023-11-03 23:36:06 +00002735int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned char hs_type,
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 unsigned char **buf, size_t *buf_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002737{
2738 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002739 * Reserve 4 bytes for handshake header. ( Section 4,RFC 8446 )
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002740 * ...
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 Peskine449bd832023-01-11 14:50:10 +01002751 return 0;
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002752}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002753
2754/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002755 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002756 *
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é-Gonnard87a346f2017-09-13 12:45:21 +02002762 * DTLS: except for HelloRequest, messages are only queued, and will only be
2763 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002764 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002765 * 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é-Gonnard065a2a32018-08-20 11:09:26 +02002771 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002772 * - 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é-Gonnardffa67be2014-09-19 11:18:57 +02002775 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002776int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
2777 int update_checksum,
2778 int force_flush)
Paul Bakker5121ce52009-01-03 21:22:43 +00002779{
Janos Follath865b3eb2019-12-16 11:46:15 +00002780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002781 const size_t hs_len = ssl->out_msglen - 4;
2782 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002785
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002786 /*
2787 * Sanity checks
2788 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 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é-Gonnard31c15862017-09-13 09:38:11 +02002793 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002795 /* Whenever we send anything different from a
2796 * HelloRequest we should be in a handshake - double check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 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é-Gonnard9c3a8ca2017-09-13 09:54:27 +02002802 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002806 ssl->handshake != NULL &&
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 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é-Gonnardffa67be2014-09-19 11:18:57 +02002810 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002811#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002812
Hanno Beckerb50a2532018-08-06 11:52:54 +01002813 /* 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 Peskine449bd832023-01-11 14:50:10 +01002821 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 Beckerb50a2532018-08-06 11:52:54 +01002828 }
2829
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002830 /*
2831 * Fill handshake headers
2832 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 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 Bakker5121ce52009-01-03 21:22:43 +00002837
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002838 /*
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002847 /* Make room for the additional DTLS fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 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 Becker9648f8b2017-09-18 10:55:54 +01002855 }
2856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002858 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002859
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002860 /* Write message_seq and update it, except for HelloRequest */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 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é-Gonnardc392b242014-08-19 17:53:11 +02002865 ssl->out_msg[4] = 0;
2866 ssl->out_msg[5] = 0;
2867 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002868
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002869 /* Handshake hashes are computed without fragmentation,
2870 * so set frag_offset = 0 and frag_len = hs_len for now */
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 memset(ssl->out_msg + 6, 0x00, 3);
2872 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002873 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002875
Hanno Becker0207e532018-08-28 10:28:28 +01002876 /* Update running hashes of handshake messages seen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002878 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 Peskine449bd832023-01-11 14:50:10 +01002884 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 }
2886
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002887 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 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é-Gonnardffa67be2014-09-19 11:18:57 +02002895 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 } else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002897#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002898 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 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é-Gonnard87a346f2017-09-13 12:45:21 +02002902 }
2903 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002904
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 return 0;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002908}
2909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl,
2911 size_t buf_len, size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002912{
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 Peskine449bd832023-01-11 14:50:10 +01002920 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_handshake_msg_ext(ssl, 0, 0));
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002921
2922cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 return ret;
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002924}
2925
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002926/*
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 Peskine449bd832023-01-11 14:50:10 +01002938int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush)
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002939{
2940 int ret, done = 0;
2941 size_t len = ssl->out_msglen;
Ronald Cron00d012f22022-03-08 15:57:12 +01002942 int flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 if (!done) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002947 unsigned i;
2948 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002949#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 Becker6430faf2019-05-08 11:57:13 +01002954 /* Skip writing the record content type to after the encryption,
2955 * as it may change when using the CID extension. */
Glenn Strauss60bfe602022-03-14 19:04:24 -04002956 mbedtls_ssl_protocol_version tls_ver = ssl->tls_version;
Ronald Cron6f135e12021-12-08 16:57:54 +01002957#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ca80f72021-11-08 10:30:54 +08002958 /* TLS 1.3 still uses the TLS 1.2 version identifier
2959 * for backwards compatibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 if (tls_ver == MBEDTLS_SSL_VERSION_TLS1_3) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002961 tls_ver = MBEDTLS_SSL_VERSION_TLS1_2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 }
Ronald Cron6f135e12021-12-08 16:57:54 +01002963#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 mbedtls_ssl_write_version(ssl->out_hdr + 1, ssl->conf->transport,
2965 tls_ver);
Hanno Becker6430faf2019-05-08 11:57:13 +01002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 memcpy(ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
2968 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (ssl->transform_out != NULL) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002971 mbedtls_record rec;
2972
2973 rec.buf = ssl->out_iv;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002974 rec.buf_len = out_buf_len - (size_t) (ssl->out_iv - ssl->out_buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002975 rec.data_len = ssl->out_msglen;
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00002976 rec.data_offset = (size_t) (ssl->out_msg - rec.buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 memcpy(&rec.ctr[0], ssl->out_ctr, sizeof(rec.ctr));
2979 mbedtls_ssl_write_version(rec.ver, ssl->conf->transport, tls_ver);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002980 rec.type = ssl->out_msgtype;
2981
Hanno Beckera0e20d02019-05-15 14:03:01 +01002982#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002983 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002984 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002985#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 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 Bakker05ef8352012-05-08 09:17:57 +00002991 }
2992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 if (rec.data_offset != 0) {
2994 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2995 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002996 }
2997
Hanno Becker6430faf2019-05-08 11:57:13 +01002998 /* Update the record content type and CID. */
2999 ssl->out_msgtype = rec.type;
Gilles Peskine449bd832023-01-11 14:50:10 +01003000#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3001 memcpy(ssl->out_cid, rec.cid, rec.cid_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01003002#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00003003 ssl->out_msglen = len = rec.data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00003005 }
3006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker2b1e3542018-08-06 11:19:13 +01003008
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 Peskine449bd832023-01-11 14:50:10 +01003012 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 Becker2b1e3542018-08-06 11:19:13 +01003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 if (protected_record_size > (size_t) ret) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003019 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker2b1e3542018-08-06 11:19:13 +01003021 }
3022 }
3023#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00003024
Hanno Becker6430faf2019-05-08 11:57:13 +01003025 /* Now write the potentially updated record content type. */
3026 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 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 Bakker05ef8352012-05-08 09:17:57 +00003032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3034 ssl->out_hdr, protected_record_size);
Hanno Becker2b1e3542018-08-06 11:19:13 +01003035
3036 ssl->out_left += protected_record_size;
3037 ssl->out_hdr += protected_record_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Hanno Becker2b1e3542018-08-06 11:19:13 +01003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
3041 if (++ssl->cur_out_ctr[i - 1] != 0) {
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02003042 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 }
3044 }
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02003045
Gabor Mezei96ae9262022-06-28 11:45:18 +02003046 /* The loop goes to its end if the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 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 Becker04484622018-08-06 09:49:38 +01003050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 }
3052
Hanno Becker67bc7c32018-08-06 11:33:50 +01003053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3055 flush == SSL_DONT_FORCE_FLUSH) {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01003056 size_t remaining;
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 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 Becker1f5a15d2018-08-21 13:31:31 +01003062 }
3063
3064 remaining = (size_t) ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (remaining == 0) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003066 flush = SSL_FORCE_FLUSH;
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 } else {
3068 MBEDTLS_SSL_DEBUG_MSG(2,
3069 ("Still %u bytes available in current datagram",
3070 (unsigned) remaining));
Hanno Becker67bc7c32018-08-06 11:33:50 +01003071 }
3072 }
3073#endif /* MBEDTLS_SSL_PROTO_DTLS */
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 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 Bakker5121ce52009-01-03 21:22:43 +00003079 }
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003084}
3085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003087
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003088MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003089static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003090{
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 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 Beckere25e3b72018-08-16 09:30:53 +01003095 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 return 0;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003097}
Hanno Becker44650b72018-08-16 12:51:11 +01003098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01003100{
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003101 return MBEDTLS_GET_UINT24_BE(ssl->in_msg, 9);
Hanno Becker44650b72018-08-16 12:51:11 +01003102}
3103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01003105{
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003106 return MBEDTLS_GET_UINT24_BE(ssl->in_msg, 6);
Hanno Becker44650b72018-08-16 12:51:11 +01003107}
3108
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003110static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01003111{
3112 uint32_t msg_len, frag_off, frag_len;
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 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 Becker44650b72018-08-16 12:51:11 +01003117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 if (frag_off > msg_len) {
3119 return -1;
3120 }
Hanno Becker44650b72018-08-16 12:51:11 +01003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if (frag_len > msg_len - frag_off) {
3123 return -1;
3124 }
Hanno Becker44650b72018-08-16 12:51:11 +01003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (frag_len + 12 > ssl->in_msglen) {
3127 return -1;
3128 }
Hanno Becker44650b72018-08-16 12:51:11 +01003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 return 0;
Hanno Becker44650b72018-08-16 12:51:11 +01003131}
3132
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003133/*
3134 * Mark bits in bitmask (used for DTLS HS reassembly)
3135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003136static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003137{
3138 unsigned int start_bits, end_bits;
3139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 start_bits = 8 - (offset % 8);
3141 if (start_bits != 8) {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003142 size_t first_byte_idx = offset / 8;
3143
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003144 /* Special case */
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if (len <= start_bits) {
3146 for (; len != 0; len--) {
3147 mask[first_byte_idx] |= 1 << (start_bits - len);
3148 }
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003149
3150 /* Avoid potential issues with offset or len becoming invalid */
3151 return;
3152 }
3153
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003154 offset += start_bits; /* Now offset % 8 == 0 */
3155 len -= start_bits;
3156
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 for (; start_bits != 0; start_bits--) {
3158 mask[first_byte_idx] |= 1 << (start_bits - 1);
3159 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003160 }
3161
3162 end_bits = len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if (end_bits != 0) {
3164 size_t last_byte_idx = (offset + len) / 8;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003165
3166 len -= end_bits; /* Now len % 8 == 0 */
3167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 for (; end_bits != 0; end_bits--) {
3169 mask[last_byte_idx] |= 1 << (8 - end_bits);
3170 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003171 }
3172
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 memset(mask + offset / 8, 0xFF, len / 8);
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003174}
3175
3176/*
3177 * Check that bitmask is full
3178 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003179MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003180static int ssl_bitmask_check(unsigned char *mask, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003181{
3182 size_t i;
3183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 for (i = 0; i < len / 8; i++) {
3185 if (mask[i] != 0xFF) {
3186 return -1;
3187 }
3188 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 for (i = 0; i < len % 8; i++) {
3191 if ((mask[len / 8] & (1 << (7 - i))) == 0) {
3192 return -1;
3193 }
3194 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003197}
3198
Hanno Becker56e205e2018-08-16 09:06:12 +01003199/* msg_len does not include the handshake header */
Gilles Peskine449bd832023-01-11 14:50:10 +01003200static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
3201 unsigned add_bitmap)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003202{
Hanno Becker56e205e2018-08-16 09:06:12 +01003203 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003204
Hanno Becker56e205e2018-08-16 09:06:12 +01003205 alloc_len = 12; /* Handshake header */
3206 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 if (add_bitmap) {
3209 alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 }
3212 return alloc_len;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003213}
Hanno Becker56e205e2018-08-16 09:06:12 +01003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
Hanno Becker12555c62018-08-16 12:47:53 +01003218{
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003219 return MBEDTLS_GET_UINT24_BE(ssl->in_msg, 1);
Hanno Becker12555c62018-08-16 12:47:53 +01003220}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003223{
Gilles Peskineaf0c4612025-02-28 21:59:12 +01003224 if (ssl->badmac_seen_or_in_hsfraglen == 0) {
3225 /* The handshake message must at least include the header.
3226 * We may not have the full message yet in case of fragmentation.
3227 * To simplify the code, we insist on having the header (and in
3228 * particular the handshake message length) in the first
3229 * fragment. */
3230 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
3231 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
3232 ssl->in_msglen));
3233 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3234 }
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003235
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00003236 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00003237 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
3240 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
3241 MBEDTLS_PRINTF_SIZET,
3242 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003243
Gilles Peskine22c51b92025-02-28 22:02:52 +01003244 if (ssl->transform_in != NULL) {
3245 MBEDTLS_SSL_DEBUG_MSG(4, ("decrypted handshake message:"
3246 " iv-buf=%d hdr-buf=%d hdr-buf=%d",
3247 (int) (ssl->in_iv - ssl->in_buf),
3248 (int) (ssl->in_hdr - ssl->in_buf),
3249 (int) (ssl->in_msg - ssl->in_buf)));
3250 }
3251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003255 unsigned int recv_msg_seq = MBEDTLS_GET_UINT16_BE(ssl->in_msg, 4);
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl_check_hs_header(ssl) != 0) {
3258 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
3259 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker44650b72018-08-16 12:51:11 +01003260 }
3261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 if (ssl->handshake != NULL &&
3263 ((mbedtls_ssl_is_handshake_over(ssl) == 0 &&
3264 recv_msg_seq != ssl->handshake->in_msg_seq) ||
3265 (mbedtls_ssl_is_handshake_over(ssl) == 1 &&
3266 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
3267 if (recv_msg_seq > ssl->handshake->in_msg_seq) {
3268 MBEDTLS_SSL_DEBUG_MSG(2,
3269 (
3270 "received future handshake message of sequence number %u (next %u)",
3271 recv_msg_seq,
3272 ssl->handshake->in_msg_seq));
3273 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Becker9e1ec222018-08-15 15:54:43 +01003274 }
3275
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003276 /* Retransmit only on last message from previous flight, to avoid
3277 * too many retransmissions.
3278 * Besides, No sane server ever retransmits HelloVerifyRequest */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
3280 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
3281 MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
3282 "message_seq = %u, start_of_flight = %u",
3283 recv_msg_seq,
3284 ssl->handshake->in_flight_start_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
3287 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
3288 return ret;
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 } else {
3291 MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
3292 "message_seq = %u, expected = %u",
3293 recv_msg_seq,
3294 ssl->handshake->in_msg_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003295 }
3296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003298 }
3299 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003300
Hanno Becker6d97ef52018-08-16 13:09:04 +01003301 /* Message reassembly is handled alongside buffering of future
3302 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003303 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003304 * handshake logic layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 if (ssl_hs_is_proper_fragment(ssl) == 1) {
3306 MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
3307 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003308 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskinecc856a22025-02-28 22:24:56 +01003311 {
3312 unsigned char *const reassembled_record_start =
3313 ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3314 unsigned char *const payload_start =
3315 reassembled_record_start + mbedtls_ssl_in_hdr_len(ssl);
3316 unsigned char *payload_end = payload_start + ssl->badmac_seen_or_in_hsfraglen;
Gilles Peskineb888cca2025-03-06 19:03:00 +01003317 /* How many more bytes we want to have a complete handshake message. */
3318 const size_t hs_remain = ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen;
3319 /* How many bytes of the current record are part of the first
3320 * handshake message. There may be more handshake messages (possibly
3321 * incomplete) in the same record; if so, we leave them after the
3322 * current record, and ssl_consume_current_message() will take
3323 * care of consuming the next handshake message. */
3324 const size_t hs_this_fragment_len =
3325 ssl->in_msglen > hs_remain ? hs_remain : ssl->in_msglen;
Gilles Peskinecc856a22025-02-28 22:24:56 +01003326
3327 if (ssl->badmac_seen_or_in_hsfraglen != 0) {
3328 /* We already had a handshake fragment. Prepare to append
3329 * to the initial segment. */
3330 MBEDTLS_SSL_DEBUG_MSG(3,
3331 ("subsequent handshake fragment: %" MBEDTLS_PRINTF_SIZET
3332 ", %u..%u of %" MBEDTLS_PRINTF_SIZET,
3333 ssl->in_msglen,
3334 ssl->badmac_seen_or_in_hsfraglen,
3335 ssl->badmac_seen_or_in_hsfraglen +
Gilles Peskineb888cca2025-03-06 19:03:00 +01003336 (unsigned) hs_this_fragment_len,
Gilles Peskinecc856a22025-02-28 22:24:56 +01003337 ssl->in_hslen));
Gilles Peskineb888cca2025-03-06 19:03:00 +01003338 } else if (hs_this_fragment_len == ssl->in_hslen) {
Gilles Peskinecc856a22025-02-28 22:24:56 +01003339 /* This is the sole fragment. */
3340 /* Emit a log message in the same format as when there are
3341 * multiple fragments, for ease of matching. */
3342 MBEDTLS_SSL_DEBUG_MSG(3,
3343 ("sole handshake fragment: %" MBEDTLS_PRINTF_SIZET
3344 ", %u..%u of %" MBEDTLS_PRINTF_SIZET,
3345 ssl->in_msglen,
3346 ssl->badmac_seen_or_in_hsfraglen,
3347 ssl->badmac_seen_or_in_hsfraglen +
Gilles Peskineb888cca2025-03-06 19:03:00 +01003348 (unsigned) hs_this_fragment_len,
Gilles Peskinecc856a22025-02-28 22:24:56 +01003349 ssl->in_hslen));
3350 } else {
3351 /* This is the first fragment of many. */
3352 MBEDTLS_SSL_DEBUG_MSG(3,
3353 ("initial handshake fragment: %" MBEDTLS_PRINTF_SIZET
3354 ", %u..%u of %" MBEDTLS_PRINTF_SIZET,
3355 ssl->in_msglen,
3356 ssl->badmac_seen_or_in_hsfraglen,
3357 ssl->badmac_seen_or_in_hsfraglen +
Gilles Peskineb888cca2025-03-06 19:03:00 +01003358 (unsigned) hs_this_fragment_len,
Gilles Peskinecc856a22025-02-28 22:24:56 +01003359 ssl->in_hslen));
3360 }
3361
3362 /* Move the received handshake fragment to have the whole message
3363 * (at least the part received so far) in a single segment at a
3364 * known offset in the input buffer.
3365 * - When receiving a non-initial handshake fragment, append it to
3366 * the initial segment.
3367 * - Even the initial handshake fragment is moved, if it was
3368 * encrypted with an explicit IV: decryption leaves the payload
3369 * after the explicit IV, but here we move it to start where the
3370 * IV was.
3371 */
3372#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3373 size_t const in_buf_len = ssl->in_buf_len;
3374#else
3375 size_t const in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3376#endif
Gilles Peskine3175fc32025-03-06 15:15:20 +01003377 if (payload_end + ssl->in_msglen > ssl->in_buf + in_buf_len) {
Gilles Peskinecc856a22025-02-28 22:24:56 +01003378 MBEDTLS_SSL_DEBUG_MSG(1,
3379 ("Shouldn't happen: no room to move handshake fragment %"
3380 MBEDTLS_PRINTF_SIZET " from %p to %p (buf=%p len=%"
3381 MBEDTLS_PRINTF_SIZET ")",
3382 ssl->in_msglen,
Gilles Peskine302f37b2025-02-25 23:57:20 +01003383 (void *) ssl->in_msg, (void *) payload_end,
3384 (void *) ssl->in_buf, in_buf_len));
Gilles Peskinecc856a22025-02-28 22:24:56 +01003385 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3386 }
3387 memmove(payload_end, ssl->in_msg, ssl->in_msglen);
3388
Gilles Peskine58c33012025-03-04 10:30:24 +01003389 ssl->badmac_seen_or_in_hsfraglen += (unsigned) ssl->in_msglen;
Gilles Peskinecc856a22025-02-28 22:24:56 +01003390 payload_end += ssl->in_msglen;
3391
3392 if (ssl->badmac_seen_or_in_hsfraglen < ssl->in_hslen) {
Gilles Peskine3d490a92025-02-28 21:29:59 +01003393 MBEDTLS_SSL_DEBUG_MSG(3, ("Prepare: waiting for more handshake fragments "
3394 "%u/%" MBEDTLS_PRINTF_SIZET,
3395 ssl->badmac_seen_or_in_hsfraglen, ssl->in_hslen));
Gilles Peskinecc856a22025-02-28 22:24:56 +01003396 ssl->in_hdr = payload_end;
3397 ssl->in_msglen = 0;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00003398 mbedtls_ssl_update_in_pointers(ssl);
Gilles Peskinecc856a22025-02-28 22:24:56 +01003399 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3400 } else {
3401 ssl->in_msglen = ssl->badmac_seen_or_in_hsfraglen;
Gilles Peskineb7105992025-02-17 16:28:51 +01003402 ssl->badmac_seen_or_in_hsfraglen = 0;
Gilles Peskinecc856a22025-02-28 22:24:56 +01003403 ssl->in_hdr = reassembled_record_start;
3404 mbedtls_ssl_update_in_pointers(ssl);
3405
3406 /* Update the record length in the fully reassembled record */
3407 if (ssl->in_msglen > 0xffff) {
3408 MBEDTLS_SSL_DEBUG_MSG(1,
Gilles Peskineb888cca2025-03-06 19:03:00 +01003409 ("Shouldn't happen: in_hslen=%"
Gilles Peskinecc856a22025-02-28 22:24:56 +01003410 MBEDTLS_PRINTF_SIZET " > 0xffff",
3411 ssl->in_msglen));
3412 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3413 }
3414 MBEDTLS_PUT_UINT16_BE(ssl->in_msglen, ssl->in_len, 0);
3415
Gilles Peskineb888cca2025-03-06 19:03:00 +01003416 size_t record_len = mbedtls_ssl_in_hdr_len(ssl) + ssl->in_msglen;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00003417 MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record",
Gilles Peskineb888cca2025-03-06 19:03:00 +01003418 ssl->in_hdr, record_len);
3419 if (ssl->in_hslen < ssl->in_msglen) {
3420 MBEDTLS_SSL_DEBUG_MSG(3,
3421 ("More handshake messages in the record: "
3422 "%" MBEDTLS_PRINTF_SIZET " + "
3423 "%" MBEDTLS_PRINTF_SIZET,
3424 ssl->in_hslen,
3425 ssl->in_msglen - ssl->in_hslen));
3426 }
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00003427 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003428 }
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003431}
3432
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003433int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003434{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0271f962018-08-16 13:23:47 +01003436 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003439 ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
3440 if (ret != 0) {
3441 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
3442 return ret;
3443 }
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003444 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003445
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003446 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3449 ssl->handshake != NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01003450 unsigned offset;
3451 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003452
Hanno Becker0271f962018-08-16 13:23:47 +01003453 /* Increment handshake sequence number */
3454 hs->in_msg_seq++;
3455
3456 /*
3457 * Clear up handshake buffering and reassembly structure.
3458 */
3459
3460 /* Free first entry */
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ssl_buffering_free_slot(ssl, 0);
Hanno Becker0271f962018-08-16 13:23:47 +01003462
3463 /* Shift all other entries */
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 for (offset = 0, hs_buf = &hs->buffering.hs[0];
Hanno Beckere605b192018-08-21 15:59:07 +01003465 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 offset++, hs_buf++) {
Hanno Becker0271f962018-08-16 13:23:47 +01003467 *hs_buf = *(hs_buf + 1);
3468 }
3469
3470 /* Create a fresh last entry */
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003472 }
3473#endif
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003474 return 0;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003475}
3476
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003477/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003478 * DTLS anti-replay: RFC 6347 4.1.2.6
3479 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003480 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3481 * Bit n is set iff record number in_window_top - n has been seen.
3482 *
3483 * Usually, in_window_top is the last record number seen and the lsb of
3484 * in_window is set. The only exception is the initial state (record number 0
3485 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01003488void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003489{
3490 ssl->in_window_top = 0;
3491 ssl->in_window = 0;
3492}
3493
Gilles Peskine449bd832023-01-11 14:50:10 +01003494static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003495{
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 return ((uint64_t) buf[0] << 40) |
3497 ((uint64_t) buf[1] << 32) |
3498 ((uint64_t) buf[2] << 24) |
3499 ((uint64_t) buf[3] << 16) |
3500 ((uint64_t) buf[4] << 8) |
3501 ((uint64_t) buf[5]);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003502}
3503
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003504MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003505static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003506{
Janos Follath865b3eb2019-12-16 11:46:15 +00003507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003508 unsigned char *original_in_ctr;
3509
3510 // save original in_ctr
3511 original_in_ctr = ssl->in_ctr;
3512
3513 // use counter from record
3514 ssl->in_ctr = record_in_ctr;
3515
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003517
3518 // restore the counter
3519 ssl->in_ctr = original_in_ctr;
3520
3521 return ret;
3522}
3523
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003524/*
3525 * Return 0 if sequence number is acceptable, -1 otherwise
3526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003527int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003528{
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003530 uint64_t bit;
3531
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
3533 return 0;
3534 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003535
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 if (rec_seqnum > ssl->in_window_top) {
3537 return 0;
3538 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003539
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003540 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003541
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 if (bit >= 64) {
3543 return -1;
3544 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003545
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
3547 return -1;
3548 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003549
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 return 0;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003551}
3552
3553/*
3554 * Update replay window on new validated record
3555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003556void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003557{
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003559
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003561 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003563
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 if (rec_seqnum > ssl->in_window_top) {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003565 /* Update window_top and the contents of the window */
3566 uint64_t shift = rec_seqnum - ssl->in_window_top;
3567
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 if (shift >= 64) {
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003569 ssl->in_window = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003571 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003572 ssl->in_window |= 1;
3573 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003574
3575 ssl->in_window_top = rec_seqnum;
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003577 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003578 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003579
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 if (bit < 64) { /* Always true, but be extra sure */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003581 ssl->in_window |= (uint64_t) 1 << bit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003583 }
3584}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003586
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003587#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003588/*
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003589 * Check if a datagram looks like a ClientHello with a valid cookie,
3590 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003591 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003592 *
3593 * - if cookie is valid, return 0
3594 * - if ClientHello looks superficially valid but cookie is not,
3595 * fill obuf and set olen, then
3596 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3597 * - otherwise return a specific error code
3598 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003599MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003600MBEDTLS_STATIC_TESTABLE
3601int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 mbedtls_ssl_context *ssl,
3603 const unsigned char *cli_id, size_t cli_id_len,
3604 const unsigned char *in, size_t in_len,
3605 unsigned char *obuf, size_t buf_len, size_t *olen)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003606{
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003607 size_t sid_len, cookie_len, epoch, fragment_offset;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003608 unsigned char *p;
3609
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003610 /*
3611 * Structure of ClientHello with record and handshake headers,
3612 * and expected values. We don't need to check a lot, more checks will be
3613 * done when actually parsing the ClientHello - skipping those checks
3614 * avoids code duplication and does not make cookie forging any easier.
3615 *
3616 * 0-0 ContentType type; copied, must be handshake
3617 * 1-2 ProtocolVersion version; copied
3618 * 3-4 uint16 epoch; copied, must be 0
3619 * 5-10 uint48 sequence_number; copied
3620 * 11-12 uint16 length; (ignored)
3621 *
3622 * 13-13 HandshakeType msg_type; (ignored)
3623 * 14-16 uint24 length; (ignored)
3624 * 17-18 uint16 message_seq; copied
3625 * 19-21 uint24 fragment_offset; copied, must be 0
3626 * 22-24 uint24 fragment_length; (ignored)
3627 *
3628 * 25-26 ProtocolVersion client_version; (ignored)
3629 * 27-58 Random random; (ignored)
3630 * 59-xx SessionID session_id; 1 byte len + sid_len content
3631 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3632 * ...
3633 *
3634 * Minimum length is 61 bytes.
3635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
3637 (unsigned) in_len));
3638 MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
3639 if (in_len < 61) {
3640 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
3641 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003642 }
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003643
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 epoch = MBEDTLS_GET_UINT16_BE(in, 3);
3645 fragment_offset = MBEDTLS_GET_UINT24_BE(in, 19);
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003646
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || epoch != 0 ||
3648 fragment_offset != 0) {
3649 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
3650 MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
3651 in[0], (unsigned) epoch,
3652 (unsigned) fragment_offset));
3653 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003654 }
3655
3656 sid_len = in[59];
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 if (59 + 1 + sid_len + 1 > in_len) {
3658 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
3659 (unsigned) sid_len,
3660 (unsigned) in_len - 61));
3661 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003662 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
3664 in + 60, sid_len);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003665
3666 cookie_len = in[60 + sid_len];
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
3668 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
3669 (unsigned) cookie_len,
3670 (unsigned) (in_len - sid_len - 61)));
3671 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003672 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003673
Gilles Peskine449bd832023-01-11 14:50:10 +01003674 MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
3675 in + sid_len + 61, cookie_len);
3676 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
3677 in + sid_len + 61, cookie_len,
3678 cli_id, cli_id_len) == 0) {
3679 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
3680 return 0;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003681 }
3682
3683 /*
3684 * If we get here, we've got an invalid cookie, let's prepare HVR.
3685 *
3686 * 0-0 ContentType type; copied
3687 * 1-2 ProtocolVersion version; copied
3688 * 3-4 uint16 epoch; copied
3689 * 5-10 uint48 sequence_number; copied
3690 * 11-12 uint16 length; olen - 13
3691 *
3692 * 13-13 HandshakeType msg_type; hello_verify_request
3693 * 14-16 uint24 length; olen - 25
3694 * 17-18 uint16 message_seq; copied
3695 * 19-21 uint24 fragment_offset; copied
3696 * 22-24 uint24 fragment_length; olen - 25
3697 *
3698 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3699 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3700 *
3701 * Minimum length is 28.
3702 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 if (buf_len < 28) {
3704 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3705 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003706
3707 /* Copy most fields and adapt others */
Gilles Peskine449bd832023-01-11 14:50:10 +01003708 memcpy(obuf, in, 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003709 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3710 obuf[25] = 0xfe;
3711 obuf[26] = 0xff;
3712
3713 /* Generate and write actual cookie */
3714 p = obuf + 28;
Gilles Peskine449bd832023-01-11 14:50:10 +01003715 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
3716 &p, obuf + buf_len,
3717 cli_id, cli_id_len) != 0) {
3718 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003719 }
3720
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +00003721 *olen = (size_t) (p - obuf);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003722
3723 /* Go back and fill length fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01003724 obuf[27] = (unsigned char) (*olen - 28);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003725
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
3727 obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
3728 obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003729
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003731
Gilles Peskine449bd832023-01-11 14:50:10 +01003732 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003733}
3734
3735/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003736 * Handle possible client reconnect with the same UDP quadruplet
3737 * (RFC 6347 Section 4.2.8).
3738 *
3739 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3740 * that looks like a ClientHello.
3741 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003742 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003743 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003744 * - if the input looks like a ClientHello with a valid cookie,
3745 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003746 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003747 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003748 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003749 * This function is called (through ssl_check_client_reconnect()) when an
3750 * unexpected record is found in ssl_get_next_record(), which will discard the
3751 * record if we return 0, and bubble up the return value otherwise (this
3752 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3753 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003754 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003755MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003756static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003757{
Janos Follath865b3eb2019-12-16 11:46:15 +00003758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Matthias Schulz9916b062023-11-09 14:25:01 +01003759 size_t len = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003760
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 if (ssl->conf->f_cookie_write == NULL ||
3762 ssl->conf->f_cookie_check == NULL) {
Hanno Becker2fddd372019-07-10 14:37:41 +01003763 /* If we can't use cookies to verify reachability of the peer,
3764 * drop the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003765 MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
3766 "can't check reconnect validity"));
3767 return 0;
Hanno Becker2fddd372019-07-10 14:37:41 +01003768 }
3769
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003770 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 ssl,
3772 ssl->cli_id, ssl->cli_id_len,
3773 ssl->in_buf, ssl->in_left,
3774 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003775
Gilles Peskine449bd832023-01-11 14:50:10 +01003776 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003777
Gilles Peskine449bd832023-01-11 14:50:10 +01003778 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003779 int send_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
3781 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3782 ssl->out_buf, len);
Brian J Murray1903fb32016-11-06 04:45:15 -08003783 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003784 * If the error is permanent we'll catch it later,
3785 * if it's not, then hopefully it'll work next time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
3787 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003788 (void) send_ret;
3789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 return 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003791 }
3792
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 if (ret == 0) {
3794 MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
3795 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
3796 MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
3797 return ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003798 }
3799
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003801 }
3802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 return ret;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003804}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003805#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003806
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003808static int ssl_check_record_type(uint8_t record_type)
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003809{
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003811 record_type != MBEDTLS_SSL_MSG_ALERT &&
3812 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
3814 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003815 }
3816
Gilles Peskine449bd832023-01-11 14:50:10 +01003817 return 0;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003818}
3819
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003820/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003821 * ContentType type;
3822 * ProtocolVersion version;
3823 * uint16 epoch; // DTLS only
3824 * uint48 sequence_number; // DTLS only
3825 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003826 *
3827 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003828 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003829 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3830 *
3831 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003832 * 1. proceed with the record if this function returns 0
3833 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3834 * 3. return CLIENT_RECONNECT if this function return that value
3835 * 4. drop the whole datagram if this function returns anything else.
3836 * Point 2 is needed when the peer is resending, and we have already received
3837 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003838 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003840static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
3841 unsigned char *buf,
3842 size_t len,
3843 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00003844{
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003845 mbedtls_ssl_protocol_version tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
Hanno Beckere5e7e782019-07-11 12:29:35 +01003847 size_t const rec_hdr_type_offset = 0;
3848 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003849
Hanno Beckere5e7e782019-07-11 12:29:35 +01003850 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3851 rec_hdr_type_len;
3852 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003853
Hanno Beckere5e7e782019-07-11 12:29:35 +01003854 size_t const rec_hdr_ctr_len = 8;
3855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003856 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003857 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3858 rec_hdr_version_len;
3859
Hanno Beckera0e20d02019-05-15 14:03:01 +01003860#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003861 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3862 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003863 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003864#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3865#endif /* MBEDTLS_SSL_PROTO_DTLS */
3866
3867 size_t rec_hdr_len_offset; /* To be determined */
3868 size_t const rec_hdr_len_len = 2;
3869
3870 /*
3871 * Check minimum lengths for record header.
3872 */
3873
3874#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003876 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003878#endif /* MBEDTLS_SSL_PROTO_DTLS */
3879 {
3880 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3881 }
3882
Gilles Peskine449bd832023-01-11 14:50:10 +01003883 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3884 MBEDTLS_SSL_DEBUG_MSG(1,
3885 (
3886 "datagram of length %u too small to hold DTLS record header of length %u",
3887 (unsigned) len,
3888 (unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
3889 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003890 }
3891
3892 /*
3893 * Parse and validate record content type
3894 */
3895
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 rec->type = buf[rec_hdr_type_offset];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003897
3898 /* Check record content type */
3899#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3900 rec->cid_len = 0;
3901
Gilles Peskine449bd832023-01-11 14:50:10 +01003902 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003903 ssl->conf->cid_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 rec->type == MBEDTLS_SSL_MSG_CID) {
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003905 /* Shift pointers to account for record header including CID
3906 * struct {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02003907 * ContentType outer_type = tls12_cid;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003908 * ProtocolVersion version;
3909 * uint16 epoch;
3910 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003911 * opaque cid[cid_length]; // Additional field compared to
3912 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003913 * uint16 length;
3914 * opaque enc_content[DTLSCiphertext.length];
3915 * } DTLSCiphertext;
3916 */
3917
3918 /* So far, we only support static CID lengths
3919 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003920 rec_hdr_cid_len = ssl->conf->cid_len;
3921 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3924 MBEDTLS_SSL_DEBUG_MSG(1,
3925 (
3926 "datagram of length %u too small to hold DTLS record header including CID, length %u",
3927 (unsigned) len,
3928 (unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
3929 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere538d822019-07-10 14:50:10 +01003930 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003931
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003932 /* configured CID len is guaranteed at most 255, see
3933 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3934 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
3936 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003937#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003938 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if (ssl_check_record_type(rec->type)) {
3940 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
3941 (unsigned) rec->type));
3942 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003943 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003944 }
3945
Hanno Beckere5e7e782019-07-11 12:29:35 +01003946 /*
3947 * Parse and validate record version
3948 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 rec->ver[0] = buf[rec_hdr_version_offset + 0];
3950 rec->ver[1] = buf[rec_hdr_version_offset + 1];
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01003951 tls_version = (mbedtls_ssl_protocol_version) mbedtls_ssl_read_version(
3952 buf + rec_hdr_version_offset,
3953 ssl->conf->transport);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003954
Gilles Peskine449bd832023-01-11 14:50:10 +01003955 if (tls_version > ssl->conf->max_tls_version) {
3956 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS version mismatch: got %u, expected max %u",
3957 (unsigned) tls_version,
3958 (unsigned) ssl->conf->max_tls_version));
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003961 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003962 /*
3963 * Parse/Copy record sequence number.
3964 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003965
Hanno Beckere5e7e782019-07-11 12:29:35 +01003966#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003968 /* Copy explicit record sequence number from input buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
3970 rec_hdr_ctr_len);
3971 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003972#endif /* MBEDTLS_SSL_PROTO_DTLS */
3973 {
3974 /* Copy implicit record sequence number from SSL context structure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003976 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003977
Hanno Beckere5e7e782019-07-11 12:29:35 +01003978 /*
3979 * Parse record length.
3980 */
3981
Hanno Beckere5e7e782019-07-11 12:29:35 +01003982 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Dave Rodgmana3d0f612023-11-03 23:34:02 +00003983 rec->data_len = MBEDTLS_GET_UINT16_BE(buf, rec_hdr_len_offset);
Gilles Peskine449bd832023-01-11 14:50:10 +01003984 MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
3987 "version = [0x%x], msglen = %" MBEDTLS_PRINTF_SIZET,
3988 rec->type, (unsigned) tls_version, rec->data_len));
Hanno Beckere5e7e782019-07-11 12:29:35 +01003989
3990 rec->buf = buf;
3991 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003992
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 if (rec->data_len == 0) {
3994 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003996
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003997 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003998 * DTLS-related tests.
3999 * Check epoch before checking length constraint because
4000 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4001 * message gets duplicated before the corresponding Finished message,
4002 * the second ChangeCipherSpec should be discarded because it belongs
4003 * to an old epoch, but not because its length is shorter than
4004 * the minimum record length for packets using the new record transform.
4005 * Note that these two kinds of failures are handled differently,
4006 * as an unexpected record is silently skipped but an invalid
4007 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004008 */
4009#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Dave Rodgmana3d0f612023-11-03 23:34:02 +00004011 rec_epoch = MBEDTLS_GET_UINT16_BE(rec->ctr, 0);
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004012
Hanno Becker955a5c92019-07-10 17:12:07 +01004013 /* Check that the datagram is large enough to contain a record
4014 * of the advertised length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 if (len < rec->data_offset + rec->data_len) {
4016 MBEDTLS_SSL_DEBUG_MSG(1,
4017 (
4018 "Datagram of length %u too small to contain record of advertised length %u.",
4019 (unsigned) len,
4020 (unsigned) (rec->data_offset + rec->data_len)));
4021 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker955a5c92019-07-10 17:12:07 +01004022 }
Hanno Becker37cfe732019-07-10 17:20:01 +01004023
Hanno Becker37cfe732019-07-10 17:20:01 +01004024 /* Records from other, non-matching epochs are silently discarded.
4025 * (The case of same-port Client reconnects must be considered in
4026 * the caller). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (rec_epoch != ssl->in_epoch) {
4028 MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
4029 "expected %u, received %lu",
4030 ssl->in_epoch, (unsigned long) rec_epoch));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004031
Hanno Becker552f7472019-07-19 10:59:12 +01004032 /* Records from the next epoch are considered for buffering
4033 * (concretely: early Finished messages). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
4035 MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
4036 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004037 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004038
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004040 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004041#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01004042 /* For records from the correct epoch, check whether their
4043 * sequence number has been seen before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
4045 &rec->ctr[0]) != 0) {
4046 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
4047 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004048 }
4049#endif
4050 }
4051#endif /* MBEDTLS_SSL_PROTO_DTLS */
4052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004054}
Paul Bakker5121ce52009-01-03 21:22:43 +00004055
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Hanno Becker2fddd372019-07-10 14:37:41 +01004057#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004059static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
Hanno Becker2fddd372019-07-10 14:37:41 +01004060{
Dave Rodgmana3d0f612023-11-03 23:34:02 +00004061 unsigned int rec_epoch = MBEDTLS_GET_UINT16_BE(ssl->in_ctr, 0);
Hanno Becker2fddd372019-07-10 14:37:41 +01004062
4063 /*
4064 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4065 * access the first byte of record content (handshake type), as we
4066 * have an active transform (possibly iv_len != 0), so use the
4067 * fact that the record header len is 13 instead.
4068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (rec_epoch == 0 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01004070 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 mbedtls_ssl_is_handshake_over(ssl) == 1 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01004072 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4073 ssl->in_left > 13 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
4075 MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
4076 "from the same port"));
4077 return ssl_handle_possible_reconnect(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00004078 }
4079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004081}
Hanno Becker2fddd372019-07-10 14:37:41 +01004082#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004083
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004084/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004085 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004086 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004087MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004088static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
4089 mbedtls_record *rec)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004090{
4091 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004092
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
4094 rec->buf, rec->buf_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00004095
Ronald Cron7e38cba2021-11-24 12:43:39 +01004096 /*
4097 * In TLS 1.3, always treat ChangeCipherSpec records
4098 * as unencrypted. The only thing we do with them is
4099 * check the length and content and ignore them.
4100 */
Ronald Cron6f135e12021-12-08 16:57:54 +01004101#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (ssl->transform_in != NULL &&
4103 ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4104 if (rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
Ronald Cron7e38cba2021-11-24 12:43:39 +01004105 done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 }
Ronald Cron7e38cba2021-11-24 12:43:39 +01004107 }
Ronald Cron6f135e12021-12-08 16:57:54 +01004108#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron7e38cba2021-11-24 12:43:39 +01004109
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 if (!done && ssl->transform_in != NULL) {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01004111 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004112
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
4114 rec)) != 0) {
4115 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
Hanno Becker8367ccc2019-05-14 11:30:10 +01004116
Ronald Cron2995d352024-01-18 16:59:39 +01004117#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C)
4118 /*
4119 * Although the server rejected early data, it might receive early
4120 * data as long as it has not received the client Finished message.
4121 * It is encrypted with early keys and should be ignored as stated
4122 * in section 4.2.10 of RFC 8446:
4123 *
4124 * "Ignore the extension and return a regular 1-RTT response. The
4125 * server then skips past early data by attempting to deprotect
4126 * received records using the handshake traffic key, discarding
4127 * records which fail deprotection (up to the configured
4128 * max_early_data_size). Once a record is deprotected successfully,
4129 * it is treated as the start of the client's second flight and the
4130 * server proceeds as with an ordinary 1-RTT handshake."
4131 */
4132 if ((old_msg_type == MBEDTLS_SSL_MSG_APPLICATION_DATA) &&
4133 (ssl->discard_early_data_record ==
4134 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD)) {
4135 MBEDTLS_SSL_DEBUG_MSG(
4136 3, ("EarlyData: deprotect and discard app data records."));
Ronald Cron919e5962024-02-08 15:48:29 +01004137
4138 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, rec->data_len);
4139 if (ret != 0) {
4140 return ret;
4141 }
Ronald Cron2995d352024-01-18 16:59:39 +01004142 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4143 }
4144#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */
4145
Hanno Beckera0e20d02019-05-15 14:03:01 +01004146#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Becker8367ccc2019-05-14 11:30:10 +01004148 ssl->conf->ignore_unexpected_cid
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
4150 MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
Hanno Becker16ded982019-05-08 13:02:55 +01004151 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01004152 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01004154
Ronald Cron71c6e652024-02-05 16:48:10 +01004155 /*
4156 * The decryption of the record failed, no reason to ignore it,
4157 * return in error with the decryption error code.
4158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004160 }
4161
Ronald Cron2995d352024-01-18 16:59:39 +01004162#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C)
4163 /*
4164 * If the server were discarding protected records that it fails to
4165 * deprotect because it has rejected early data, as we have just
4166 * deprotected successfully a record, the server has to resume normal
4167 * operation and fail the connection if the deprotection of a record
4168 * fails.
4169 */
4170 if (ssl->discard_early_data_record ==
4171 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD) {
4172 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
4173 }
4174#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */
4175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (old_msg_type != rec->type) {
4177 MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
4178 old_msg_type, rec->type));
Hanno Becker6430faf2019-05-08 11:57:13 +01004179 }
4180
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
4182 rec->buf + rec->data_offset, rec->data_len);
Hanno Becker1c0c37f2018-08-07 14:29:29 +01004183
Hanno Beckera0e20d02019-05-15 14:03:01 +01004184#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01004185 /* We have already checked the record content type
4186 * in ssl_parse_record_header(), failing or silently
4187 * dropping the record in the case of an unknown type.
4188 *
4189 * Since with the use of CIDs, the record content type
4190 * might change during decryption, re-check the record
4191 * content type, but treat a failure as fatal this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (ssl_check_record_type(rec->type)) {
4193 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
4194 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker6430faf2019-05-08 11:57:13 +01004195 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01004197
Gilles Peskine449bd832023-01-11 14:50:10 +01004198 if (rec->data_len == 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004199#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2
4201 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004202 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
4204 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004205 }
4206#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4207
4208 ssl->nb_zero++;
4209
4210 /*
4211 * Three or more empty messages may be a DoS attack
4212 * (excessive CPU consumption).
4213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 if (ssl->nb_zero > 3) {
4215 MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
4216 "messages, possible DoS attack"));
Hanno Becker6e7700d2019-05-08 10:38:32 +01004217 /* Treat the records as if they were not properly authenticated,
4218 * thereby failing the connection if we see more than allowed
4219 * by the configured bad MAC threshold. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004221 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004222 } else {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004223 ssl->nb_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004225
4226#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004227 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004228 ; /* in_ctr read from peer, not maintained internally */
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 } else
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004230#endif
4231 {
4232 unsigned i;
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 for (i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
4234 i > mbedtls_ssl_ep_len(ssl); i--) {
4235 if (++ssl->in_ctr[i - 1] != 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004236 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 }
Jerry Yuae0b2e22021-10-08 15:21:19 +08004238 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004239
4240 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 if (i == mbedtls_ssl_ep_len(ssl)) {
4242 MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
4243 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00004244 }
4245 }
4246
Paul Bakker5121ce52009-01-03 21:22:43 +00004247 }
4248
Jerry Yuf57d14b2023-11-15 16:40:09 +08004249#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_SRV_C)
4250 /*
4251 * Although the server rejected early data because it needed to send an
4252 * HelloRetryRequest message, it might receive early data as long as it has
4253 * not received the client Finished message.
4254 * The early data is encrypted with early keys and should be ignored as
4255 * stated in section 4.2.10 of RFC 8446 (second case):
4256 *
4257 * "The server then ignores early data by skipping all records with an
4258 * external content type of "application_data" (indicating that they are
4259 * encrypted), up to the configured max_early_data_size. Ignore application
4260 * data message before 2nd ClientHello when early_data was received in 1st
4261 * ClientHello."
4262 */
4263 if (ssl->discard_early_data_record == MBEDTLS_SSL_EARLY_DATA_DISCARD) {
4264 if (rec->type == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Cron01d273d2024-02-09 16:17:10 +01004265
4266 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, rec->data_len);
4267 if (ret != 0) {
4268 return ret;
4269 }
4270
Jerry Yuf57d14b2023-11-15 16:40:09 +08004271 MBEDTLS_SSL_DEBUG_MSG(
4272 3, ("EarlyData: Ignore application message before 2nd ClientHello"));
Ronald Crondb944a72024-03-08 11:32:53 +01004273
Jerry Yuf57d14b2023-11-15 16:40:09 +08004274 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4275 } else if (rec->type == MBEDTLS_SSL_MSG_HANDSHAKE) {
4276 ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
4277 }
4278 }
4279#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_SRV_C */
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4283 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004284 }
4285#endif
4286
Hanno Beckerd96e10b2019-07-09 17:30:02 +01004287 /* Check actual (decrypted) record content length against
4288 * configured maximum. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
4290 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
4291 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerd96e10b2019-07-09 17:30:02 +01004292 }
4293
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004295}
4296
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004297/*
4298 * Read a record.
4299 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004300 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4301 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4302 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004303 */
Hanno Becker1097b342018-08-15 14:09:41 +01004304
4305/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004307static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004309static int ssl_get_next_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004310MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004311static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
Hanno Becker4162b112018-08-15 14:05:04 +01004312
Gilles Peskine449bd832023-01-11 14:50:10 +01004313int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
4314 unsigned update_hs_digest)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004315{
Janos Follath865b3eb2019-12-16 11:46:15 +00004316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004317
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004319
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 if (ssl->keep_current_message == 0) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004321 do {
Simon Butcher99000142016-10-13 17:21:01 +01004322
Gilles Peskine449bd832023-01-11 14:50:10 +01004323 ret = ssl_consume_current_message(ssl);
4324 if (ret != 0) {
4325 return ret;
4326 }
Hanno Becker26994592018-08-15 14:14:59 +01004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 if (ssl_record_is_in_progress(ssl) == 0) {
David Horstmann10be1342022-10-06 18:31:25 +01004329 int dtls_have_buffered = 0;
Hanno Becker40f50842018-08-15 14:48:01 +01004330#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere74d5562018-08-15 14:26:08 +01004331
Hanno Becker40f50842018-08-15 14:48:01 +01004332 /* We only check for buffered messages if the
4333 * current datagram is fully consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4335 ssl_next_record_is_in_datagram(ssl) == 0) {
4336 if (ssl_load_buffered_message(ssl) == 0) {
David Horstmann10be1342022-10-06 18:31:25 +01004337 dtls_have_buffered = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 }
Hanno Becker40f50842018-08-15 14:48:01 +01004339 }
4340
Hanno Becker40f50842018-08-15 14:48:01 +01004341#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 if (dtls_have_buffered == 0) {
4343 ret = ssl_get_next_record(ssl);
4344 if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
Hanno Becker40f50842018-08-15 14:48:01 +01004345 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 }
Hanno Becker40f50842018-08-15 14:48:01 +01004347
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 if (ret != 0) {
4349 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
4350 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004351 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004352 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004353 }
4354
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 ret = mbedtls_ssl_handle_message_type(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004356
Hanno Becker40f50842018-08-15 14:48:01 +01004357#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker40f50842018-08-15 14:48:01 +01004359 /* Buffer future message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 ret = ssl_buffer_message(ssl);
4361 if (ret != 0) {
4362 return ret;
4363 }
Hanno Becker40f50842018-08-15 14:48:01 +01004364
4365 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4366 }
4367#endif /* MBEDTLS_SSL_PROTO_DTLS */
4368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 } while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4370 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004371
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (0 != ret) {
4373 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
4374 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01004375 }
4376
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4378 update_hs_digest == 1) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01004379 ret = mbedtls_ssl_update_handshake_status(ssl);
4380 if (0 != ret) {
4381 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
4382 return ret;
4383 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004384 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 } else {
4386 MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004387 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004388 }
4389
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
Simon Butcher99000142016-10-13 17:21:01 +01004391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01004393}
4394
Hanno Becker40f50842018-08-15 14:48:01 +01004395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004397static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01004398{
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (ssl->in_left > ssl->next_record_offset) {
4400 return 1;
4401 }
Simon Butcher99000142016-10-13 17:21:01 +01004402
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 return 0;
Hanno Becker40f50842018-08-15 14:48:01 +01004404}
4405
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004406MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004407static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004408{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004409 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004411 int ret = 0;
4412
Gilles Peskine449bd832023-01-11 14:50:10 +01004413 if (hs == NULL) {
4414 return -1;
4415 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
Hanno Beckere00ae372018-08-20 09:39:42 +01004418
Gilles Peskine449bd832023-01-11 14:50:10 +01004419 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4420 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004421 /* Check if we have seen a ChangeCipherSpec before.
4422 * If yes, synthesize a CCS record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 if (!hs->buffering.seen_ccs) {
4424 MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004425 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004426 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004427 }
4428
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004430 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4431 ssl->in_msglen = 1;
4432 ssl->in_msg[0] = 1;
4433
4434 /* As long as they are equal, the exact value doesn't matter. */
4435 ssl->in_left = 0;
4436 ssl->next_record_offset = 0;
4437
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004438 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004439 goto exit;
4440 }
Hanno Becker37f95322018-08-16 13:55:32 +01004441
Hanno Beckerb8f50142018-08-28 10:01:34 +01004442#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004443 /* Debug only */
4444 {
4445 unsigned offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
Hanno Becker37f95322018-08-16 13:55:32 +01004447 hs_buf = &hs->buffering.hs[offset];
Gilles Peskine449bd832023-01-11 14:50:10 +01004448 if (hs_buf->is_valid == 1) {
4449 MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
4450 hs->in_msg_seq + offset,
4451 hs_buf->is_complete ? "fully" : "partially"));
Hanno Becker37f95322018-08-16 13:55:32 +01004452 }
4453 }
4454 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004455#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004456
4457 /* Check if we have buffered and/or fully reassembled the
4458 * next handshake message. */
4459 hs_buf = &hs->buffering.hs[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
Hanno Becker37f95322018-08-16 13:55:32 +01004461 /* Synthesize a record containing the buffered HS message. */
Dave Rodgmana3d0f612023-11-03 23:34:02 +00004462 size_t msg_len = MBEDTLS_GET_UINT24_BE(hs_buf->data, 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004463
4464 /* Double-check that we haven't accidentally buffered
4465 * a message that doesn't fit into the input buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
4467 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4468 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004469 }
4470
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
4472 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
4473 hs_buf->data, msg_len + 12);
Hanno Becker37f95322018-08-16 13:55:32 +01004474
4475 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4476 ssl->in_hslen = msg_len + 12;
4477 ssl->in_msglen = msg_len + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
Hanno Becker37f95322018-08-16 13:55:32 +01004479
4480 ret = 0;
4481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 } else {
4483 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
4484 hs->in_msg_seq));
Hanno Becker37f95322018-08-16 13:55:32 +01004485 }
4486
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004487 ret = -1;
4488
4489exit:
4490
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
4492 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004493}
4494
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004495MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004496static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
4497 size_t desired)
Hanno Beckera02b0b42018-08-21 17:20:27 +01004498{
4499 int offset;
4500 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
4502 (unsigned) desired));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004503
Hanno Becker01315ea2018-08-21 17:22:17 +01004504 /* Get rid of future records epoch first, if such exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 ssl_free_buffered_record(ssl);
Hanno Becker01315ea2018-08-21 17:22:17 +01004506
4507 /* Check if we have enough space available now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4509 hs->buffering.total_bytes_buffered)) {
4510 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
4511 return 0;
Hanno Becker01315ea2018-08-21 17:22:17 +01004512 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004513
Hanno Becker4f432ad2018-08-28 10:02:32 +01004514 /* We don't have enough space to buffer the next expected handshake
4515 * message. Remove buffers used for future messages to gain space,
4516 * starting with the most distant one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4518 offset >= 0; offset--) {
4519 MBEDTLS_SSL_DEBUG_MSG(2,
4520 (
4521 "Free buffering slot %d to make space for reassembly of next handshake message",
4522 offset));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004523
Gilles Peskine449bd832023-01-11 14:50:10 +01004524 ssl_buffering_free_slot(ssl, (uint8_t) offset);
Hanno Beckera02b0b42018-08-21 17:20:27 +01004525
4526 /* Check if we have enough space available now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4528 hs->buffering.total_bytes_buffered)) {
4529 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
4530 return 0;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004531 }
4532 }
4533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 return -1;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004535}
4536
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004538static int ssl_buffer_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004539{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004540 int ret = 0;
4541 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4542
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (hs == NULL) {
4544 return 0;
4545 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004548
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 switch (ssl->in_msgtype) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004550 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
Hanno Beckere678eaa2018-08-21 14:57:46 +01004552
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004553 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004554 break;
4555
4556 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004557 {
4558 unsigned recv_msg_seq_offset;
Dave Rodgmana3d0f612023-11-03 23:34:02 +00004559 unsigned recv_msg_seq = MBEDTLS_GET_UINT16_BE(ssl->in_msg, 4);
Hanno Becker37f95322018-08-16 13:55:32 +01004560 mbedtls_ssl_hs_buffer *hs_buf;
4561 size_t msg_len = ssl->in_hslen - 12;
4562
4563 /* We should never receive an old handshake
4564 * message - double-check nonetheless. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 if (recv_msg_seq < ssl->handshake->in_msg_seq) {
4566 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4567 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004568 }
4569
4570 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Becker37f95322018-08-16 13:55:32 +01004572 /* Silently ignore -- message too far in the future */
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 MBEDTLS_SSL_DEBUG_MSG(2,
4574 ("Ignore future HS message with sequence number %u, "
4575 "buffering window %u - %u",
4576 recv_msg_seq, ssl->handshake->in_msg_seq,
4577 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
4578 1));
Hanno Becker37f95322018-08-16 13:55:32 +01004579
4580 goto exit;
4581 }
4582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
4584 recv_msg_seq, recv_msg_seq_offset));
Hanno Becker37f95322018-08-16 13:55:32 +01004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
Hanno Becker37f95322018-08-16 13:55:32 +01004587
4588 /* Check if the buffering for this seq nr has already commenced. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (!hs_buf->is_valid) {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004590 size_t reassembly_buf_sz;
4591
Hanno Becker37f95322018-08-16 13:55:32 +01004592 hs_buf->is_fragmented =
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 (ssl_hs_is_proper_fragment(ssl) == 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004594
4595 /* We copy the message back into the input buffer
4596 * after reassembly, so check that it's not too large.
4597 * This is an implementation-specific limitation
4598 * and not one from the standard, hence it is not
4599 * checked in ssl_check_hs_header(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
Hanno Becker37f95322018-08-16 13:55:32 +01004601 /* Ignore message */
4602 goto exit;
4603 }
4604
Hanno Beckere0b150f2018-08-21 15:51:03 +01004605 /* Check if we have enough space to buffer the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 if (hs->buffering.total_bytes_buffered >
4607 MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
4608 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4609 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004610 }
4611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
4613 hs_buf->is_fragmented);
Hanno Beckere0b150f2018-08-21 15:51:03 +01004614
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4616 hs->buffering.total_bytes_buffered)) {
4617 if (recv_msg_seq_offset > 0) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004618 /* If we can't buffer a future message because
4619 * of space limitations -- ignore. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 MBEDTLS_SSL_DEBUG_MSG(2,
4621 ("Buffering of future message of size %"
4622 MBEDTLS_PRINTF_SIZET
4623 " would exceed the compile-time limit %"
4624 MBEDTLS_PRINTF_SIZET
4625 " (already %" MBEDTLS_PRINTF_SIZET
4626 " bytes buffered) -- ignore\n",
4627 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4628 hs->buffering.total_bytes_buffered));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004629 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 } else {
4631 MBEDTLS_SSL_DEBUG_MSG(2,
4632 ("Buffering of future message of size %"
4633 MBEDTLS_PRINTF_SIZET
4634 " would exceed the compile-time limit %"
4635 MBEDTLS_PRINTF_SIZET
4636 " (already %" MBEDTLS_PRINTF_SIZET
4637 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4638 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4639 hs->buffering.total_bytes_buffered));
Hanno Beckere1801392018-08-21 16:51:05 +01004640 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004641
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
4643 MBEDTLS_SSL_DEBUG_MSG(2,
4644 ("Reassembly of next message of size %"
4645 MBEDTLS_PRINTF_SIZET
4646 " (%" MBEDTLS_PRINTF_SIZET
4647 " with bitmap) would exceed"
4648 " the compile-time limit %"
4649 MBEDTLS_PRINTF_SIZET
4650 " (already %" MBEDTLS_PRINTF_SIZET
4651 " bytes buffered) -- fail\n",
4652 msg_len,
4653 reassembly_buf_sz,
4654 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4655 hs->buffering.total_bytes_buffered));
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004656 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4657 goto exit;
4658 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004659 }
4660
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 MBEDTLS_SSL_DEBUG_MSG(2,
4662 ("initialize reassembly, total length = %"
4663 MBEDTLS_PRINTF_SIZET,
4664 msg_len));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004665
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
4667 if (hs_buf->data == NULL) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004668 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004669 goto exit;
4670 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004671 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004672
4673 /* Prepare final header: copy msg_type, length and message_seq,
4674 * then add standardised fragment_offset and fragment_length */
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 memcpy(hs_buf->data, ssl->in_msg, 6);
4676 memset(hs_buf->data + 6, 0, 3);
4677 memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
Hanno Becker37f95322018-08-16 13:55:32 +01004678
4679 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004680
4681 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004683 /* Make sure msg_type and length are consistent */
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
4685 MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
Hanno Becker37f95322018-08-16 13:55:32 +01004686 /* Ignore */
4687 goto exit;
4688 }
4689 }
4690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 if (!hs_buf->is_complete) {
Hanno Becker37f95322018-08-16 13:55:32 +01004692 size_t frag_len, frag_off;
4693 unsigned char * const msg = hs_buf->data + 12;
4694
4695 /*
4696 * Check and copy current fragment
4697 */
4698
4699 /* Validation of header fields already done in
4700 * mbedtls_ssl_prepare_handshake_record(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 frag_off = ssl_get_hs_frag_off(ssl);
4702 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker37f95322018-08-16 13:55:32 +01004703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4705 ", length = %" MBEDTLS_PRINTF_SIZET,
4706 frag_off, frag_len));
4707 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
Hanno Becker37f95322018-08-16 13:55:32 +01004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if (hs_buf->is_fragmented) {
Hanno Becker37f95322018-08-16 13:55:32 +01004710 unsigned char * const bitmask = msg + msg_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004711 ssl_bitmask_set(bitmask, frag_off, frag_len);
4712 hs_buf->is_complete = (ssl_bitmask_check(bitmask,
4713 msg_len) == 0);
4714 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004715 hs_buf->is_complete = 1;
4716 }
4717
Gilles Peskine449bd832023-01-11 14:50:10 +01004718 MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
4719 hs_buf->is_complete ? "" : "not yet "));
Hanno Becker37f95322018-08-16 13:55:32 +01004720 }
4721
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004722 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004723 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004724
4725 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004726 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004727 break;
4728 }
4729
4730exit:
4731
Gilles Peskine449bd832023-01-11 14:50:10 +01004732 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
4733 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004734}
4735#endif /* MBEDTLS_SSL_PROTO_DTLS */
4736
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004737MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004738static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004739{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004740 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004741 * Consume last content-layer message and potentially
4742 * update in_msglen which keeps track of the contents'
4743 * consumption state.
4744 *
4745 * (1) Handshake messages:
4746 * Remove last handshake message, move content
4747 * and adapt in_msglen.
4748 *
4749 * (2) Alert messages:
4750 * Consume whole record content, in_msglen = 0.
4751 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004752 * (3) Change cipher spec:
4753 * Consume whole record content, in_msglen = 0.
4754 *
4755 * (4) Application data:
4756 * Don't do anything - the record layer provides
4757 * the application data as a stream transport
4758 * and consumes through mbedtls_ssl_read only.
4759 *
4760 */
4761
4762 /* Case (1): Handshake messages */
Gilles Peskine449bd832023-01-11 14:50:10 +01004763 if (ssl->in_hslen != 0) {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004764 /* Hard assertion to be sure that no application data
4765 * is in flight, as corrupting ssl->in_msglen during
4766 * ssl->in_offt != NULL is fatal. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if (ssl->in_offt != NULL) {
4768 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4769 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004770 }
4771
Gilles Peskineb7105992025-02-17 16:28:51 +01004772 if (ssl->badmac_seen_or_in_hsfraglen != 0) {
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00004773 /* Not all handshake fragments have arrived, do not consume. */
Gilles Peskine3d490a92025-02-28 21:29:59 +01004774 MBEDTLS_SSL_DEBUG_MSG(3, ("Consume: waiting for more handshake fragments "
4775 "%u/%" MBEDTLS_PRINTF_SIZET,
4776 ssl->badmac_seen_or_in_hsfraglen, ssl->in_hslen));
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00004777 return 0;
4778 }
4779
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004780 /*
4781 * Get next Handshake message in the current record
4782 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004783
Hanno Becker4a810fb2017-05-24 16:27:30 +01004784 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004785 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004786 * current handshake content: If DTLS handshake
4787 * fragmentation is used, that's the fragment
4788 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004789 * size here is faulty and should be changed at
4790 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004791 * (2) While it doesn't seem to cause problems, one
4792 * has to be very careful not to assume that in_hslen
4793 * is always <= in_msglen in a sensible communication.
4794 * Again, it's wrong for DTLS handshake fragmentation.
4795 * The following check is therefore mandatory, and
4796 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004797 * Additionally, ssl->in_hslen might be arbitrarily out of
4798 * bounds after handling a DTLS message with an unexpected
4799 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004800 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 if (ssl->in_hslen < ssl->in_msglen) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004802 ssl->in_msglen -= ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4804 ssl->in_msglen);
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00004805 MBEDTLS_PUT_UINT16_BE(ssl->in_msglen, ssl->in_len, 0);
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
4808 ssl->in_msg, ssl->in_msglen);
4809 } else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004810 ssl->in_msglen = 0;
4811 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004812
Hanno Becker4a810fb2017-05-24 16:27:30 +01004813 ssl->in_hslen = 0;
4814 }
4815 /* Case (4): Application data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 else if (ssl->in_offt != NULL) {
4817 return 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01004818 }
4819 /* Everything else (CCS & Alerts) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004821 ssl->in_msglen = 0;
4822 }
4823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 return 0;
Hanno Becker1097b342018-08-15 14:09:41 +01004825}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004826
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004827MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004828static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
Hanno Beckere74d5562018-08-15 14:26:08 +01004829{
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (ssl->in_msglen > 0) {
4831 return 1;
4832 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 return 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004835}
4836
Hanno Becker5f066e72018-08-16 14:56:31 +01004837#if defined(MBEDTLS_SSL_PROTO_DTLS)
4838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004840{
4841 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 if (hs == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004843 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 if (hs->buffering.future_record.data != NULL) {
Hanno Becker01315ea2018-08-21 17:22:17 +01004847 hs->buffering.total_bytes_buffered -=
4848 hs->buffering.future_record.len;
4849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 mbedtls_free(hs->buffering.future_record.data);
Hanno Becker01315ea2018-08-21 17:22:17 +01004851 hs->buffering.future_record.data = NULL;
4852 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004853}
4854
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004855MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004856static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004857{
4858 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 unsigned char *rec;
Hanno Becker5f066e72018-08-16 14:56:31 +01004860 size_t rec_len;
4861 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004862#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4863 size_t in_buf_len = ssl->in_buf_len;
4864#else
4865 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4866#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4868 return 0;
4869 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (hs == NULL) {
4872 return 0;
4873 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004874
Hanno Becker5f066e72018-08-16 14:56:31 +01004875 rec = hs->buffering.future_record.data;
4876 rec_len = hs->buffering.future_record.len;
4877 rec_epoch = hs->buffering.future_record.epoch;
4878
Gilles Peskine449bd832023-01-11 14:50:10 +01004879 if (rec == NULL) {
4880 return 0;
4881 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004882
Hanno Becker4cb782d2018-08-20 11:19:05 +01004883 /* Only consider loading future records if the
4884 * input buffer is empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (ssl_next_record_is_in_datagram(ssl) == 1) {
4886 return 0;
4887 }
Hanno Becker4cb782d2018-08-20 11:19:05 +01004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (rec_epoch != ssl->in_epoch) {
4892 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
Hanno Becker5f066e72018-08-16 14:56:31 +01004893 goto exit;
4894 }
4895
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004897
4898 /* Double-check that the record is not too large */
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
4900 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4901 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker5f066e72018-08-16 14:56:31 +01004902 }
4903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 memcpy(ssl->in_hdr, rec, rec_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004905 ssl->in_left = rec_len;
4906 ssl->next_record_offset = 0;
4907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 ssl_free_buffered_record(ssl);
Hanno Becker5f066e72018-08-16 14:56:31 +01004909
4910exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
4912 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004913}
4914
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004915MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004916static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
4917 mbedtls_record const *rec)
Hanno Becker5f066e72018-08-16 14:56:31 +01004918{
4919 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004920
4921 /* Don't buffer future records outside handshakes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 if (hs == NULL) {
4923 return 0;
4924 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004925
4926 /* Only buffer handshake records (we are only interested
4927 * in Finished messages). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
4929 return 0;
4930 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004931
4932 /* Don't buffer more than one future epoch record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 if (hs->buffering.future_record.data != NULL) {
4934 return 0;
4935 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004936
Hanno Becker01315ea2018-08-21 17:22:17 +01004937 /* Don't buffer record if there's not enough buffering space remaining. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4939 hs->buffering.total_bytes_buffered)) {
4940 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4941 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4942 " (already %" MBEDTLS_PRINTF_SIZET
4943 " bytes buffered) -- ignore\n",
4944 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4945 hs->buffering.total_bytes_buffered));
4946 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004947 }
4948
Hanno Becker5f066e72018-08-16 14:56:31 +01004949 /* Buffer record */
Gilles Peskine449bd832023-01-11 14:50:10 +01004950 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
4951 ssl->in_epoch + 1U));
4952 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004953
4954 /* ssl_parse_record_header() only considers records
4955 * of the next epoch as candidates for buffering. */
4956 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004957 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004958
4959 hs->buffering.future_record.data =
Gilles Peskine449bd832023-01-11 14:50:10 +01004960 mbedtls_calloc(1, hs->buffering.future_record.len);
4961 if (hs->buffering.future_record.data == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004962 /* If we run out of RAM trying to buffer a
4963 * record from the next epoch, just ignore. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004964 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004965 }
4966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004968
Hanno Becker519f15d2019-07-11 12:43:20 +01004969 hs->buffering.total_bytes_buffered += rec->buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004971}
4972
4973#endif /* MBEDTLS_SSL_PROTO_DTLS */
4974
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004976static int ssl_get_next_record(mbedtls_ssl_context *ssl)
Hanno Becker1097b342018-08-15 14:09:41 +01004977{
Janos Follath865b3eb2019-12-16 11:46:15 +00004978 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004979 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004980
Hanno Becker5f066e72018-08-16 14:56:31 +01004981#if defined(MBEDTLS_SSL_PROTO_DTLS)
4982 /* We might have buffered a future record; if so,
4983 * and if the epoch matches now, load it.
4984 * On success, this call will set ssl->in_left to
4985 * the length of the buffered record, so that
4986 * the calls to ssl_fetch_input() below will
4987 * essentially be no-ops. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 ret = ssl_load_buffered_record(ssl);
4989 if (ret != 0) {
4990 return ret;
4991 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004992#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004993
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004994 /* Ensure that we have enough space available for the default form
4995 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4996 * with no space for CIDs counted in). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004997 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
4998 if (ret != 0) {
4999 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
5000 return ret;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001 }
5002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
5004 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005005#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5007 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
5008 ret = ssl_buffer_future_record(ssl, &rec);
5009 if (ret != 0) {
5010 return ret;
5011 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005012
5013 /* Fall through to handling of unexpected records */
5014 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5015 }
5016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
Hanno Becker2fddd372019-07-10 14:37:41 +01005018#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01005019 /* Reset in pointers to default state for TLS/DTLS records,
5020 * assuming no CID and no offset between record content and
5021 * record plaintext. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 mbedtls_ssl_update_in_pointers(ssl);
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01005023
Hanno Becker7ae20e02019-07-12 08:33:49 +01005024 /* Setup internal message pointers from record structure. */
5025 ssl->in_msgtype = rec.type;
5026#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5027 ssl->in_len = ssl->in_cid + rec.cid_len;
5028#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5029 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
5030 ssl->in_msglen = rec.data_len;
5031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 ret = ssl_check_client_reconnect(ssl);
5033 MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
5034 if (ret != 0) {
5035 return ret;
5036 }
Hanno Becker2fddd372019-07-10 14:37:41 +01005037#endif
5038
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005039 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01005040 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005041
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
5043 "(header)"));
5044 } else {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005045 /* Skip invalid record and the rest of the datagram */
5046 ssl->next_record_offset = 0;
5047 ssl->in_left = 0;
5048
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
5050 "(header)"));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005051 }
5052
5053 /* Get next record */
Gilles Peskine449bd832023-01-11 14:50:10 +01005054 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5055 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005056#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01005057 {
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 return ret;
Hanno Becker2fddd372019-07-10 14:37:41 +01005059 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005060 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckera8814792019-07-10 15:01:45 +01005064 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01005065 ssl->next_record_offset = rec.buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 if (ssl->next_record_offset < ssl->in_left) {
5067 MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
Hanno Beckere65ce782017-05-22 14:47:48 +01005068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 } else
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005070#endif
Hanno Beckera8814792019-07-10 15:01:45 +01005071 {
Hanno Becker955a5c92019-07-10 17:12:07 +01005072 /*
5073 * Fetch record contents from underlying transport.
5074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
5076 if (ret != 0) {
5077 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
5078 return ret;
Hanno Beckera8814792019-07-10 15:01:45 +01005079 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005080
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005081 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01005082 }
5083
5084 /*
5085 * Decrypt record contents.
5086 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005087
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005091 /* Silently discard invalid records */
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005093 /* Except when waiting for Finished as a bad mac here
5094 * probably means something went wrong in the handshake
5095 * (eg wrong psk used, mitm downgrade attempt, etc.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005096 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5097 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005098#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
5100 mbedtls_ssl_send_alert_message(ssl,
5101 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5102 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005103 }
5104#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 return ret;
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005106 }
5107
Gilles Peskine69f8f452025-02-17 16:08:59 +01005108 if (ssl->conf->badmac_limit != 0) {
Gilles Peskinef6a676d2025-02-17 16:10:14 +01005109 ++ssl->badmac_seen_or_in_hsfraglen;
5110 if (ssl->badmac_seen_or_in_hsfraglen >= ssl->conf->badmac_limit) {
Gilles Peskine69f8f452025-02-17 16:08:59 +01005111 MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
5112 return MBEDTLS_ERR_SSL_INVALID_MAC;
5113 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005114 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005115
Hanno Becker4a810fb2017-05-24 16:27:30 +01005116 /* As above, invalid records cause
5117 * dismissal of the whole datagram. */
5118
5119 ssl->next_record_offset = 0;
5120 ssl->in_left = 0;
5121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
5123 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005124 }
5125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 return ret;
5127 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005128#endif
5129 {
5130 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine449bd832023-01-11 14:50:10 +01005132 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
5133 mbedtls_ssl_send_alert_message(ssl,
5134 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5135 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005136 }
5137#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005138 return ret;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005139 }
5140 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005141
Hanno Becker44d89b22019-07-12 09:40:44 +01005142
5143 /* Reset in pointers to default state for TLS/DTLS records,
5144 * assuming no CID and no offset between record content and
5145 * record plaintext. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker44d89b22019-07-12 09:40:44 +01005147#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5148 ssl->in_len = ssl->in_cid + rec.cid_len;
5149#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03005150 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01005151
Hanno Becker8685c822019-07-12 09:37:30 +01005152 /* The record content type may change during decryption,
5153 * so re-read it. */
5154 ssl->in_msgtype = rec.type;
5155 /* Also update the input buffer, because unfortunately
5156 * the server-side ssl_parse_client_hello() reparses the
5157 * record header when receiving a ClientHello initiating
5158 * a renegotiation. */
5159 ssl->in_hdr[0] = rec.type;
5160 ssl->in_msg = rec.buf + rec.data_offset;
5161 ssl->in_msglen = rec.data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
Hanno Becker8685c822019-07-12 09:37:30 +01005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01005165}
5166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01005168{
Janos Follath865b3eb2019-12-16 11:46:15 +00005169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01005170
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005171 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005172 * Handle particular types of records
5173 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
5175 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
5176 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01005177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 }
5179
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
5181 if (ssl->in_msglen != 1) {
5182 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
5183 ssl->in_msglen));
5184 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005185 }
5186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 if (ssl->in_msg[0] != 1) {
5188 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
5189 ssl->in_msg[0]));
5190 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01005191 }
5192
5193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01005195 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
5197 if (ssl->handshake == NULL) {
5198 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
5199 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01005200 }
5201
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
5203 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Beckere678eaa2018-08-21 14:57:46 +01005204 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005205#endif
Ronald Cron7e38cba2021-11-24 12:43:39 +01005206
Ronald Cron6f135e12021-12-08 16:57:54 +01005207#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine5a668dd2024-09-13 16:03:41 +02005209 MBEDTLS_SSL_DEBUG_MSG(2,
Gilles Peskine449bd832023-01-11 14:50:10 +01005210 ("Ignore ChangeCipherSpec in TLS 1.3 compatibility mode"));
5211 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Ronald Cron7e38cba2021-11-24 12:43:39 +01005212 }
Ronald Cron6f135e12021-12-08 16:57:54 +01005213#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere678eaa2018-08-21 14:57:46 +01005214 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005215
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
5217 if (ssl->in_msglen != 2) {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005218 /* Note: Standard allows for more than one 2 byte alert
5219 to be packed in a single message, but Mbed TLS doesn't
5220 currently support this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
5222 ssl->in_msglen));
5223 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Angus Gratton1a7a17e2018-06-20 15:43:50 +10005224 }
5225
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
5227 ssl->in_msg[0], ssl->in_msg[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00005228
5229 /*
Simon Butcher459a9502015-10-27 16:09:03 +00005230 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00005231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
5233 MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
5234 ssl->in_msg[1]));
5235 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005236 }
5237
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5239 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
5240 MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
5241 return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00005242 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005243
5244#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5246 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
5247 MBEDTLS_SSL_DEBUG_MSG(2, ("is a no renegotiation alert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005248 /* Will be handled when trying to parse ServerHello */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005250 }
5251#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005252 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005253 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005254 }
5255
Hanno Beckerc76c6192017-06-06 10:03:17 +01005256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker37ae9522019-05-03 16:54:26 +01005258 /* Drop unexpected ApplicationData records,
5259 * except at the beginning of renegotiations */
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5261 mbedtls_ssl_is_handshake_over(ssl) == 0
Hanno Becker37ae9522019-05-03 16:54:26 +01005262#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005263 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5264 ssl->state == MBEDTLS_SSL_SERVER_HELLO)
Hanno Beckerc76c6192017-06-06 10:03:17 +01005265#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 ) {
5267 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
5268 return MBEDTLS_ERR_SSL_NON_FATAL;
Hanno Becker37ae9522019-05-03 16:54:26 +01005269 }
5270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (ssl->handshake != NULL &&
5272 mbedtls_ssl_is_handshake_over(ssl) == 1) {
5273 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Hanno Becker37ae9522019-05-03 16:54:26 +01005274 }
5275 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01005276#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005279}
5280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005282{
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 return mbedtls_ssl_send_alert_message(ssl,
5284 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5285 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005286}
5287
Gilles Peskine449bd832023-01-11 14:50:10 +01005288int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
5289 unsigned char level,
5290 unsigned char message)
Paul Bakker0a925182012-04-16 06:46:41 +00005291{
Janos Follath865b3eb2019-12-16 11:46:15 +00005292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00005293
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 if (ssl == NULL || ssl->conf == NULL) {
5295 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5296 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005297
Gilles Peskine449bd832023-01-11 14:50:10 +01005298 if (ssl->out_left != 0) {
5299 return mbedtls_ssl_flush_output(ssl);
5300 }
Hanno Becker5e18f742018-08-06 11:35:16 +01005301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
5303 MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
Paul Bakker0a925182012-04-16 06:46:41 +00005304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005306 ssl->out_msglen = 2;
5307 ssl->out_msg[0] = level;
5308 ssl->out_msg[1] = message;
5309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
5311 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
5312 return ret;
Paul Bakker0a925182012-04-16 06:46:41 +00005313 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
Paul Bakker0a925182012-04-16 06:46:41 +00005315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 return 0;
Paul Bakker0a925182012-04-16 06:46:41 +00005317}
5318
Gilles Peskine449bd832023-01-11 14:50:10 +01005319int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005320{
Janos Follath865b3eb2019-12-16 11:46:15 +00005321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005326 ssl->out_msglen = 1;
5327 ssl->out_msg[0] = 1;
5328
Paul Bakker5121ce52009-01-03 21:22:43 +00005329 ssl->state++;
5330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
5332 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
5333 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005334 }
5335
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005337
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005339}
5340
Gilles Peskine449bd832023-01-11 14:50:10 +01005341int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005342{
Janos Follath865b3eb2019-12-16 11:46:15 +00005343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005346
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5348 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5349 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005350 }
5351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
5353 MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
5354 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5355 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
5356 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005357 }
5358
Hanno Beckere678eaa2018-08-21 14:57:46 +01005359 /* CCS records are only accepted if they have length 1 and content '1',
5360 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005361
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005362 /*
5363 * Switch to our negotiated transform and session parameters for inbound
5364 * data.
5365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
Jerry Yu2e199812022-12-01 18:57:19 +08005367#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005368 ssl->transform_in = ssl->transform_negotiate;
Jerry Yu2e199812022-12-01 18:57:19 +08005369#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005370 ssl->session_in = ssl->session_negotiate;
5371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005376#endif
5377
5378 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 if (++ssl->in_epoch == 0) {
5380 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005381 /* This is highly unlikely to happen for legitimate reasons, so
5382 treat it as an attack and don't send an alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005384 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005388
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 mbedtls_ssl_update_in_pointers(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005390
Paul Bakker5121ce52009-01-03 21:22:43 +00005391 ssl->state++;
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005394
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005396}
5397
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005398/* Once ssl->out_hdr as the address of the beginning of the
5399 * next outgoing record is set, deduce the other pointers.
5400 *
5401 * Note: For TLS, we save the implicit record sequence number
5402 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5403 * and the caller has to make sure there's space for this.
5404 */
5405
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005406static size_t ssl_transform_get_explicit_iv_len(
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 mbedtls_ssl_transform const *transform)
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005408{
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 return transform->ivlen - transform->fixed_ivlen;
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005410}
5411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
5413 mbedtls_ssl_transform *transform)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005414{
5415#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005417 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005418#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08005419 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005420 ssl->out_len = ssl->out_cid;
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (transform != NULL) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005422 ssl->out_len += transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005424#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08005425 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005426#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005427 ssl->out_iv = ssl->out_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005429#endif
5430 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005431 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005432#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005433 ssl->out_cid = ssl->out_len;
5434#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005435 ssl->out_iv = ssl->out_hdr + 5;
5436 }
5437
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005438 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005439 /* Adjust out_msg to make space for explicit IV, if used. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 if (transform != NULL) {
5441 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
5442 }
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005443}
5444
5445/* Once ssl->in_hdr as the address of the beginning of the
5446 * next incoming record is set, deduce the other pointers.
5447 *
5448 * Note: For TLS, we save the implicit record sequence number
5449 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5450 * and the caller has to make sure there's space for this.
5451 */
5452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005454{
Hanno Becker79594fd2019-05-08 09:38:41 +01005455 /* This function sets the pointers to match the case
5456 * of unprotected TLS/DTLS records, with both ssl->in_iv
5457 * and ssl->in_msg pointing to the beginning of the record
5458 * content.
5459 *
5460 * When decrypting a protected record, ssl->in_msg
5461 * will be shifted to point to the beginning of the
5462 * record plaintext.
5463 */
5464
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005467 /* This sets the header pointers to match records
5468 * without CID. When we receive a record containing
5469 * a CID, the fields are shifted accordingly in
5470 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005471 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005472#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08005473 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005474 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005475#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08005476 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005477#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005478 ssl->in_iv = ssl->in_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005480#endif
5481 {
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00005482 ssl->in_ctr = ssl->in_buf;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005483 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005484#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005485 ssl->in_cid = ssl->in_len;
5486#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005487 ssl->in_iv = ssl->in_hdr + 5;
5488 }
5489
Hanno Becker79594fd2019-05-08 09:38:41 +01005490 /* This will be adjusted at record decryption time. */
5491 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005492}
5493
Paul Bakker5121ce52009-01-03 21:22:43 +00005494/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005495 * Setup an SSL context
5496 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005497
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00005498void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl)
5499{
5500#if defined(MBEDTLS_SSL_PROTO_DTLS)
5501 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5502 ssl->in_hdr = ssl->in_buf;
5503 } else
Deomid rojer Ryabkov1f4088c2025-01-18 15:58:57 +02005504#endif /* MBEDTLS_SSL_PROTO_DTLS */
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00005505 {
5506 ssl->in_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
5507 }
5508
5509 /* Derive other internal pointers. */
5510 mbedtls_ssl_update_in_pointers(ssl);
5511}
5512
5513void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005514{
5515 /* Set the incoming and outgoing record pointers. */
5516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005518 ssl->out_hdr = ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 } else
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005520#endif /* MBEDTLS_SSL_PROTO_DTLS */
5521 {
Hanno Becker12078f42021-03-02 15:28:41 +00005522 ssl->out_ctr = ssl->out_buf;
Deomid rojer Ryabkov3fc5a4d2024-03-10 02:11:03 +00005523 ssl->out_hdr = ssl->out_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005524 }
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005525 /* Derive other internal pointers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005527}
5528
Paul Bakker5121ce52009-01-03 21:22:43 +00005529/*
5530 * SSL get accessors
5531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005532size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005533{
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00005535}
5536
Gilles Peskine449bd832023-01-11 14:50:10 +01005537int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
Hanno Becker8b170a02017-10-10 11:51:19 +01005538{
5539 /*
5540 * Case A: We're currently holding back
5541 * a message for further processing.
5542 */
5543
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 if (ssl->keep_current_message == 1) {
5545 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
5546 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005547 }
5548
5549 /*
5550 * Case B: Further records are pending in the current datagram.
5551 */
5552
5553#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5555 ssl->in_left > ssl->next_record_offset) {
5556 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
5557 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005558 }
5559#endif /* MBEDTLS_SSL_PROTO_DTLS */
5560
5561 /*
5562 * Case C: A handshake message is being processed.
5563 */
5564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
5566 MBEDTLS_SSL_DEBUG_MSG(3,
5567 ("ssl_check_pending: more handshake messages within current record"));
5568 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005569 }
5570
5571 /*
5572 * Case D: An application data message is being processed
5573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 if (ssl->in_offt != NULL) {
5575 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
5576 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005577 }
5578
5579 /*
5580 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005581 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005582 * we implement support for multiple alerts in single records.
5583 */
5584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
5586 return 0;
Hanno Becker8b170a02017-10-10 11:51:19 +01005587}
5588
Paul Bakker43ca69c2011-01-15 17:35:19 +00005589
Gilles Peskine449bd832023-01-11 14:50:10 +01005590int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005591{
Hanno Becker3136ede2018-08-17 15:28:19 +01005592 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005594 unsigned block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005595#if defined(MBEDTLS_USE_PSA_CRYPTO)
5596 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
5597 psa_key_type_t key_type;
5598#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005599
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker5903de42019-05-03 14:46:38 +01005601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 if (transform == NULL) {
5603 return (int) out_hdr_len;
5604 }
Hanno Becker78640902018-08-13 16:35:15 +01005605
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005606
5607#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 if (transform->psa_alg == PSA_ALG_GCM ||
5609 transform->psa_alg == PSA_ALG_CCM ||
5610 transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 8) ||
5611 transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 ||
5612 transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) {
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005613 transform_expansion = transform->minlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 } else if (transform->psa_alg == PSA_ALG_CBC_NO_PADDING) {
5615 (void) psa_get_key_attributes(transform->psa_key_enc, &attr);
5616 key_type = psa_get_key_type(&attr);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005619
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005620 /* Expansion due to the addition of the MAC. */
5621 transform_expansion += transform->maclen;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005622
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005623 /* Expansion due to the addition of CBC padding;
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005624 * Theoretically up to 256 bytes, but we never use
5625 * more than the block size of the underlying cipher. */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005626 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005627
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005628 /* For TLS 1.2 or higher, an explicit IV is added
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005629 * after the record header. */
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005630#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005631 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005632#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 } else {
5634 MBEDTLS_SSL_DEBUG_MSG(1,
5635 ("Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()"));
5636 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005637 }
5638#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 case MBEDTLS_MODE_GCM:
5641 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005642 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005644 transform_expansion = transform->minlen;
5645 break;
5646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005648
5649 block_size = mbedtls_cipher_get_block_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 &transform->cipher_ctx_enc);
Hanno Becker5b559ac2018-08-03 09:40:07 +01005651
Hanno Becker3136ede2018-08-17 15:28:19 +01005652 /* Expansion due to the addition of the MAC. */
5653 transform_expansion += transform->maclen;
5654
5655 /* Expansion due to the addition of CBC padding;
5656 * Theoretically up to 256 bytes, but we never use
5657 * more than the block size of the underlying cipher. */
5658 transform_expansion += block_size;
5659
TRodziewicz4ca18aa2021-05-20 14:46:20 +02005660 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01005661 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02005662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005663 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02005664#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005665
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005666 break;
5667
5668 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5670 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005671 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005672#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005673
Hanno Beckera0e20d02019-05-15 14:03:01 +01005674#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 if (transform->out_cid_len != 0) {
Hanno Becker6cbad552019-05-08 15:40:11 +01005676 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005678#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005679
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 return (int) (out_hdr_len + transform_expansion);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005681}
5682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005684/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005685 * Check record counters and renegotiate if they're above the limit.
5686 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005687MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005688static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005689{
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 size_t ep_len = mbedtls_ssl_ep_len(ssl);
Andres AG2196c7f2016-12-15 17:01:16 +00005691 int in_ctr_cmp;
5692 int out_ctr_cmp;
5693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 if (mbedtls_ssl_is_handshake_over(ssl) == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5697 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005698 }
5699
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
5701 &ssl->conf->renego_period[ep_len],
5702 MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len);
5703 out_ctr_cmp = memcmp(&ssl->cur_out_ctr[ep_len],
Jerry Yud9a94fe2021-09-28 18:58:59 +08005704 &ssl->conf->renego_period[ep_len],
Gilles Peskine449bd832023-01-11 14:50:10 +01005705 sizeof(ssl->cur_out_ctr) - ep_len);
Andres AG2196c7f2016-12-15 17:01:16 +00005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
5708 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005709 }
5710
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
5712 return mbedtls_ssl_renegotiate(ssl);
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005715
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005716#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5717
Ronald Cron6071f612024-03-25 13:42:07 +01005718#if defined(MBEDTLS_SSL_CLI_C)
Jerry Yua0446a02022-07-13 11:22:55 +08005719MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron698c8e92024-04-02 13:19:57 +02005720static int ssl_tls13_is_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005721{
5722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 if ((ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl)) ||
5724 (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET)) {
Ronald Cron698c8e92024-04-02 13:19:57 +02005725 return 0;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005726 }
5727
Ronald Cron698c8e92024-04-02 13:19:57 +02005728 return 1;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005729}
Ronald Cron6071f612024-03-25 13:42:07 +01005730#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005731
Jerry Yua0446a02022-07-13 11:22:55 +08005732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005733static int ssl_tls13_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005734{
5735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 MBEDTLS_SSL_DEBUG_MSG(3, ("received post-handshake message"));
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005737
Ronald Cron6071f612024-03-25 13:42:07 +01005738#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron698c8e92024-04-02 13:19:57 +02005740 if (ssl_tls13_is_new_session_ticket(ssl)) {
Ronald Cron6071f612024-03-25 13:42:07 +01005741#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5742 MBEDTLS_SSL_DEBUG_MSG(3, ("NewSessionTicket received"));
Ronald Cron9f44c882024-08-28 16:44:10 +02005743 if (mbedtls_ssl_conf_is_signal_new_session_tickets_enabled(ssl->conf) ==
5744 MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED) {
Ronald Cronb675b2b2024-08-27 09:19:40 +02005745 ssl->keep_current_message = 1;
Ronald Cron6071f612024-03-25 13:42:07 +01005746
Ronald Cronb675b2b2024-08-27 09:19:40 +02005747 mbedtls_ssl_handshake_set_state(ssl,
5748 MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
5749 return MBEDTLS_ERR_SSL_WANT_READ;
5750 } else {
Ronald Cron97dc5832024-08-28 09:34:34 +02005751 MBEDTLS_SSL_DEBUG_MSG(3, ("Ignoring NewSessionTicket, handling disabled."));
Ronald Cronb675b2b2024-08-27 09:19:40 +02005752 return 0;
5753 }
Ronald Cron6071f612024-03-25 13:42:07 +01005754#else
Ronald Cron97dc5832024-08-28 09:34:34 +02005755 MBEDTLS_SSL_DEBUG_MSG(3, ("Ignoring NewSessionTicket, not supported."));
Ronald Cron6071f612024-03-25 13:42:07 +01005756 return 0;
5757#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 }
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005759 }
Ronald Cron6071f612024-03-25 13:42:07 +01005760#endif /* MBEDTLS_SSL_CLI_C */
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005761
5762 /* Fail in all other cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005764}
5765#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5766
5767#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005768/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005769 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005770 * may only be sent for the purpose of initiating renegotiations.
5771 *
5772 * This function is introduced as a separate helper since the handling
5773 * of post-handshake handshake messages changes significantly in TLS 1.3,
5774 * and having a helper function allows to distinguish between TLS <= 1.2 and
5775 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5776 */
Jerry Yua0446a02022-07-13 11:22:55 +08005777MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005778static int ssl_tls12_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005779{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005781
5782 /*
5783 * - For client-side, expect SERVER_HELLO_REQUEST.
5784 * - For server-side, expect CLIENT_HELLO.
5785 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5786 */
5787
5788#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5790 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5791 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
5792 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005793
5794 /* With DTLS, drop the packet (probably from last handshake) */
5795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5797 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005798 }
5799#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005801 }
5802#endif /* MBEDTLS_SSL_CLI_C */
5803
5804#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5806 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
5807 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005808
5809 /* With DTLS, drop the packet (probably from last handshake) */
5810#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5812 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005813 }
5814#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005816 }
5817#endif /* MBEDTLS_SSL_SRV_C */
5818
5819#if defined(MBEDTLS_SSL_RENEGOTIATION)
5820 /* Determine whether renegotiation attempt should be accepted */
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5822 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5823 ssl->conf->allow_legacy_renegotiation ==
5824 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005825 /*
5826 * Accept renegotiation request
5827 */
5828
5829 /* DTLS clients need to know renego is server-initiated */
5830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5832 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005833 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5834 }
5835#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005836 ret = mbedtls_ssl_start_renegotiation(ssl);
5837 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5838 ret != 0) {
5839 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
5840 ret);
5841 return ret;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005842 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 } else
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005844#endif /* MBEDTLS_SSL_RENEGOTIATION */
5845 {
5846 /*
5847 * Refuse renegotiation
5848 */
5849
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5853 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5854 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) != 0) {
5855 return ret;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005856 }
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005857 }
5858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005860}
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005861#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5862
5863MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005864static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005865{
5866 /* Check protocol version and dispatch accordingly. */
5867#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005868 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
5869 return ssl_tls13_handle_hs_message_post_handshake(ssl);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005870 }
5871#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5872
5873#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 if (ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
5875 return ssl_tls12_handle_hs_message_post_handshake(ssl);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005876 }
5877#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5878
5879 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005881}
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005882
Paul Bakker48916f92012-09-16 19:57:18 +00005883/*
Jerry Yu739a1d42022-12-08 21:10:25 +08005884 * brief Read at most 'len' application data bytes from the input
5885 * buffer.
5886 *
5887 * param ssl SSL context:
5888 * - First byte of application data not read yet in the input
5889 * buffer located at address `in_offt`.
5890 * - The number of bytes of data not read yet is `in_msglen`.
5891 * param buf buffer that will hold the data
5892 * param len maximum number of bytes to read
5893 *
5894 * note The function updates the fields `in_offt` and `in_msglen`
5895 * according to the number of bytes read.
5896 *
5897 * return The number of bytes read.
5898 */
5899static int ssl_read_application_data(
5900 mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
5901{
5902 size_t n = (len < ssl->in_msglen) ? len : ssl->in_msglen;
5903
5904 if (len != 0) {
5905 memcpy(buf, ssl->in_offt, n);
5906 ssl->in_msglen -= n;
5907 }
5908
5909 /* Zeroising the plaintext buffer to erase unused application data
5910 from the memory. */
5911 mbedtls_platform_zeroize(ssl->in_offt, n);
5912
5913 if (ssl->in_msglen == 0) {
5914 /* all bytes consumed */
5915 ssl->in_offt = NULL;
5916 ssl->keep_current_message = 0;
5917 } else {
5918 /* more data available */
5919 ssl->in_offt += n;
5920 }
5921
5922 return (int) n;
5923}
5924
5925/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 * Receive application data decrypted from the SSL layer
5927 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005928int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005929{
Janos Follath865b3eb2019-12-16 11:46:15 +00005930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (ssl == NULL || ssl->conf == NULL) {
5933 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5934 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005939 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5940 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5941 return ret;
5942 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005943
Gilles Peskine449bd832023-01-11 14:50:10 +01005944 if (ssl->handshake != NULL &&
5945 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
5946 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
5947 return ret;
5948 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005949 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005950 }
5951#endif
5952
Hanno Becker4a810fb2017-05-24 16:27:30 +01005953 /*
5954 * Check if renegotiation is necessary and/or handshake is
5955 * in process. If yes, perform/continue, and fall through
5956 * if an unexpected packet is received while the client
5957 * is waiting for the ServerHello.
5958 *
5959 * (There is no equivalent to the last condition on
5960 * the server-side as it is not treated as within
5961 * a handshake while waiting for the ClientHello
5962 * after a renegotiation request.)
5963 */
5964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 ret = ssl_check_ctr_renegotiate(ssl);
5967 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5968 ret != 0) {
5969 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5970 return ret;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005971 }
5972#endif
5973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5975 ret = mbedtls_ssl_handshake(ssl);
5976 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5977 ret != 0) {
5978 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5979 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005980 }
5981 }
5982
Hanno Beckere41158b2017-10-23 13:30:32 +01005983 /* Loop as long as no application data record is available */
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 while (ssl->in_offt == NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005985 /* Start timer if not already running */
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (ssl->f_get_timer != NULL &&
5987 ssl->f_get_timer(ssl->p_timer) == -1) {
5988 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005989 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5992 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5993 return 0;
5994 }
Paul Bakker831a7552011-05-18 13:32:51 +00005995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5997 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005998 }
5999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 if (ssl->in_msglen == 0 &&
6001 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00006002 /*
6003 * OpenSSL sends empty messages to randomize the IV
6004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
6006 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
6007 return 0;
6008 }
Paul Bakker831a7552011-05-18 13:32:51 +00006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
6011 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 }
6013 }
6014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
6016 ret = ssl_handle_hs_message_post_handshake(ssl);
6017 if (ret != 0) {
6018 MBEDTLS_SSL_DEBUG_RET(1, "ssl_handle_hs_message_post_handshake",
6019 ret);
6020 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00006021 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006022
Hanno Beckerf26cc722021-04-21 07:30:13 +01006023 /* At this point, we don't know whether the renegotiation triggered
6024 * by the post-handshake message has been completed or not. The cases
6025 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01006026 * 1) The renegotiation is complete. In this case, no new record
6027 * has been read yet.
6028 * 2) The renegotiation is incomplete because the client received
6029 * an application data record while awaiting the ServerHello.
6030 * 3) The renegotiation is incomplete because the client received
6031 * a non-handshake, non-application data message while awaiting
6032 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01006033 *
6034 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01006035 * - For 1), the next iteration will read a new record and check
6036 * if it's application data.
6037 * - For 2), the loop condition isn't satisfied as application data
6038 * is present, hence continue is the same as break
6039 * - For 3), the loop condition is satisfied and read_record
6040 * will re-deliver the message that was held back by the client
6041 * when expecting the ServerHello.
6042 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01006043
Hanno Becker90333da2017-10-10 11:27:13 +01006044 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00006045 }
Hanno Becker21df7f92017-10-17 11:03:26 +01006046#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01006047 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
6048 if (ssl->conf->renego_max_records >= 0) {
6049 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
6050 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
6051 "but not honored by client"));
6052 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006053 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006054 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
6060 MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
6061 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006062 }
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
6065 MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
6066 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 }
6068
6069 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006070
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006071 /* We're going to return something now, cancel timer,
6072 * except if handshake (renegotiation) is in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
6074 mbedtls_ssl_set_timer(ssl, 0);
6075 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006076
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02006077#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006078 /* If we requested renego but received AppData, resend HelloRequest.
6079 * Do it now, after setting in_offt, to avoid taking this branch
6080 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006081#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6083 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
6084 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
6085 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
6086 ret);
6087 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006088 }
6089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006090#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006091#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 }
6093
Jerry Yu739a1d42022-12-08 21:10:25 +08006094 ret = ssl_read_application_data(ssl, buf, len);
Paul Bakker5121ce52009-01-03 21:22:43 +00006095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006097
Jerry Yu739a1d42022-12-08 21:10:25 +08006098 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006099}
6100
Jerry Yud9ca3542023-12-06 17:23:52 +08006101#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_EARLY_DATA)
6102int mbedtls_ssl_read_early_data(mbedtls_ssl_context *ssl,
6103 unsigned char *buf, size_t len)
6104{
Ronald Croned7d4bf2024-01-31 07:55:19 +01006105 if (ssl == NULL || (ssl->conf == NULL)) {
Jerry Yud9ca3542023-12-06 17:23:52 +08006106 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6107 }
6108
Ronald Cron164537c2024-02-01 18:05:47 +01006109 /*
6110 * The server may receive early data only while waiting for the End of
6111 * Early Data handshake message.
6112 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01006113 if ((ssl->state != MBEDTLS_SSL_END_OF_EARLY_DATA) ||
6114 (ssl->in_offt == NULL)) {
Jerry Yud9ca3542023-12-06 17:23:52 +08006115 return MBEDTLS_ERR_SSL_CANNOT_READ_EARLY_DATA;
6116 }
6117
Ronald Croned7d4bf2024-01-31 07:55:19 +01006118 return ssl_read_application_data(ssl, buf, len);
Jerry Yud9ca3542023-12-06 17:23:52 +08006119}
6120#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_EARLY_DATA */
6121
Paul Bakker5121ce52009-01-03 21:22:43 +00006122/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01006123 * Send application data to be encrypted by the SSL layer, taking care of max
6124 * fragment length and buffer size.
6125 *
6126 * According to RFC 5246 Section 6.2.1:
6127 *
6128 * Zero-length fragments of Application data MAY be sent as they are
6129 * potentially useful as a traffic analysis countermeasure.
6130 *
6131 * Therefore, it is possible that the input message length is 0 and the
6132 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006134MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006135static int ssl_write_real(mbedtls_ssl_context *ssl,
6136 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00006137{
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02006139 const size_t max_len = (size_t) ret;
6140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 if (ret < 0) {
6142 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
6143 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02006144 }
6145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 if (len > max_len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006147#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
6149 MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
6150 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
6151 " > %" MBEDTLS_PRINTF_SIZET,
6152 len, max_len));
6153 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6154 } else
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006155#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 len = max_len;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006157 }
Paul Bakker887bd502011-06-08 13:10:54 +00006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 if (ssl->out_left != 0) {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01006160 /*
6161 * The user has previously tried to send the data and
6162 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
6163 * written. In this case, we expect the high-level write function
6164 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
6165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
6167 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
6168 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 } else {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01006171 /*
6172 * The user is trying to send a message the first time, so we need to
6173 * copy the data into the internal buffers and setup the data structure
6174 * to keep track of partial writes
6175 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006176 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006177 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Dave Rodgmanf6840252023-02-24 15:41:34 +00006178 if (len > 0) {
6179 memcpy(ssl->out_msg, buf, len);
6180 }
Paul Bakker887bd502011-06-08 13:10:54 +00006181
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
6183 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
6184 return ret;
Paul Bakker887bd502011-06-08 13:10:54 +00006185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006186 }
6187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 return (int) len;
Paul Bakker5121ce52009-01-03 21:22:43 +00006189}
6190
6191/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006192 * Write application data (public-facing wrapper)
6193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006194int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006195{
Janos Follath865b3eb2019-12-16 11:46:15 +00006196 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 if (ssl == NULL || ssl->conf == NULL) {
6201 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6202 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006203
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02006204#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
6206 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
6207 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006208 }
6209#endif
6210
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
6212 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
6213 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6214 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006215 }
6216 }
6217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 ret = ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006219
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006223}
6224
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006225#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_CLI_C)
6226int mbedtls_ssl_write_early_data(mbedtls_ssl_context *ssl,
6227 const unsigned char *buf, size_t len)
6228{
6229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6230 const struct mbedtls_ssl_config *conf;
Ronald Cron62f971a2024-02-23 08:24:12 +01006231 uint32_t remaining;
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006232
6233 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write early_data"));
6234
6235 if (ssl == NULL || (conf = ssl->conf) == NULL) {
6236 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6237 }
6238
Ronald Cron49221902024-02-21 13:39:14 +01006239 if (conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
6240 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6241 }
6242
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006243 if ((!mbedtls_ssl_conf_is_tls13_enabled(conf)) ||
6244 (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
6245 (conf->early_data_enabled != MBEDTLS_SSL_EARLY_DATA_ENABLED)) {
6246 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA;
6247 }
6248
6249 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
6250 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA;
6251 }
6252
6253 /*
Ronald Crond2884662024-03-03 15:03:22 +01006254 * If we are at the beginning of the handshake, the early data state being
Ronald Cron05d7cfb2024-03-03 15:39:30 +01006255 * equal to MBEDTLS_SSL_EARLY_DATA_STATE_IDLE or
Ronald Cron3641df22024-03-03 16:10:58 +01006256 * MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT advance the handshake just
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006257 * enough to be able to send early data if possible. That way, we can
6258 * guarantee that when starting the handshake with this function we will
Ronald Crond2884662024-03-03 15:03:22 +01006259 * send at least one record of early data. Note that when the state is
Ronald Cron3641df22024-03-03 16:10:58 +01006260 * MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT and not yet
6261 * MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE, we cannot send early data
Ronald Crond4069242024-02-21 13:45:52 +01006262 * as the early data outbound transform has not been set as we may have to
6263 * first send a dummy CCS in clear.
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006264 */
Ronald Cron05d7cfb2024-03-03 15:39:30 +01006265 if ((ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IDLE) ||
Ronald Cron3641df22024-03-03 16:10:58 +01006266 (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT)) {
Ronald Cron05d7cfb2024-03-03 15:39:30 +01006267 while ((ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IDLE) ||
Ronald Cron3641df22024-03-03 16:10:58 +01006268 (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT)) {
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006269 ret = mbedtls_ssl_handshake_step(ssl);
6270 if (ret != 0) {
6271 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake_step", ret);
6272 return ret;
6273 }
6274
6275 ret = mbedtls_ssl_flush_output(ssl);
6276 if (ret != 0) {
6277 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
6278 return ret;
6279 }
6280 }
Ronald Cron62f971a2024-02-23 08:24:12 +01006281 remaining = ssl->session_negotiate->max_early_data_size;
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006282 } else {
Ronald Crond4069242024-02-21 13:45:52 +01006283 /*
Ronald Cron62f971a2024-02-23 08:24:12 +01006284 * If we are past the point where we can send early data or we have
6285 * already reached the maximum early data size, return immediatly.
6286 * Otherwise, progress the handshake as much as possible to not delay
6287 * it too much. If we reach a point where we can still send early data,
6288 * then we will send some.
Ronald Crond4069242024-02-21 13:45:52 +01006289 */
Ronald Crond2884662024-03-03 15:03:22 +01006290 if ((ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE) &&
6291 (ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED)) {
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006292 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA;
6293 }
6294
Ronald Cron62f971a2024-02-23 08:24:12 +01006295 remaining = ssl->session_negotiate->max_early_data_size -
Ronald Cronde9b03d2024-03-01 15:14:17 +01006296 ssl->total_early_data_size;
Ronald Cron62f971a2024-02-23 08:24:12 +01006297
6298 if (remaining == 0) {
6299 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA;
6300 }
6301
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006302 ret = mbedtls_ssl_handshake(ssl);
6303 if ((ret != 0) && (ret != MBEDTLS_ERR_SSL_WANT_READ)) {
6304 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
6305 return ret;
6306 }
6307 }
6308
Ronald Crond2884662024-03-03 15:03:22 +01006309 if (((ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE) &&
6310 (ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED))
Ronald Cron62f971a2024-02-23 08:24:12 +01006311 || (remaining == 0)) {
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006312 return MBEDTLS_ERR_SSL_CANNOT_WRITE_EARLY_DATA;
6313 }
6314
Ronald Cron62f971a2024-02-23 08:24:12 +01006315 if (len > remaining) {
6316 len = remaining;
6317 }
6318
Ronald Cron5dbfcce2024-02-26 17:50:38 +01006319 ret = ssl_write_real(ssl, buf, len);
6320 if (ret >= 0) {
6321 ssl->total_early_data_size += ret;
6322 }
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006323
Ronald Cron5dbfcce2024-02-26 17:50:38 +01006324 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write early_data, ret=%d", ret));
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006325
Ronald Cron5dbfcce2024-02-26 17:50:38 +01006326 return ret;
Xiaokang Qianb62732e2023-11-30 09:58:08 +00006327}
6328#endif /* MBEDTLS_SSL_EARLY_DATA && MBEDTLS_SSL_CLI_C */
6329
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02006330/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 * Notify the peer that the connection is being closed
6332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006333int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00006334{
Janos Follath865b3eb2019-12-16 11:46:15 +00006335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00006336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 if (ssl == NULL || ssl->conf == NULL) {
6338 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6339 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
6344 if ((ret = mbedtls_ssl_send_alert_message(ssl,
6345 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
6346 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
6347 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
6348 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 }
6350 }
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00006353
Gilles Peskine449bd832023-01-11 14:50:10 +01006354 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00006355}
6356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Paul Bakker48916f92012-09-16 19:57:18 +00006358{
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 if (transform == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006360 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006362
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01006363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 psa_destroy_key(transform->psa_key_enc);
6365 psa_destroy_key(transform->psa_key_dec);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01006366#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 mbedtls_cipher_free(&transform->cipher_ctx_enc);
6368 mbedtls_cipher_free(&transform->cipher_ctx_dec);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01006369#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01006370
Hanno Beckerfd86ca82020-11-30 08:54:23 +00006371#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01006372#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 psa_destroy_key(transform->psa_mac_enc);
6374 psa_destroy_key(transform->psa_mac_dec);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006375#else
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 mbedtls_md_free(&transform->md_ctx_enc);
6377 mbedtls_md_free(&transform->md_ctx_dec);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006378#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd56ed242018-01-03 15:32:51 +00006379#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
Paul Bakker48916f92012-09-16 19:57:18 +00006382}
6383
Gilles Peskine449bd832023-01-11 14:50:10 +01006384void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
6385 mbedtls_ssl_transform *transform)
Jerry Yuc7875b52021-09-05 21:05:50 +08006386{
Jerry Yuc7875b52021-09-05 21:05:50 +08006387 ssl->transform_in = transform;
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuc7875b52021-09-05 21:05:50 +08006389}
6390
Gilles Peskine449bd832023-01-11 14:50:10 +01006391void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl,
6392 mbedtls_ssl_transform *transform)
Jerry Yuc7875b52021-09-05 21:05:50 +08006393{
6394 ssl->transform_out = transform;
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yuc7875b52021-09-05 21:05:50 +08006396}
6397
Hanno Becker0271f962018-08-16 13:23:47 +01006398#if defined(MBEDTLS_SSL_PROTO_DTLS)
6399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
Hanno Becker0271f962018-08-16 13:23:47 +01006401{
6402 unsigned offset;
6403 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 if (hs == NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01006406 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 }
Hanno Becker0271f962018-08-16 13:23:47 +01006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 ssl_free_buffered_record(ssl);
Hanno Becker283f5ef2018-08-24 09:34:47 +01006410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
6412 ssl_buffering_free_slot(ssl, offset);
6413 }
Hanno Beckere605b192018-08-21 15:59:07 +01006414}
6415
Gilles Peskine449bd832023-01-11 14:50:10 +01006416static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
6417 uint8_t slot)
Hanno Beckere605b192018-08-21 15:59:07 +01006418{
6419 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6420 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Beckerb309b922018-08-23 13:18:05 +01006423 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01006424 }
Hanno Beckerb309b922018-08-23 13:18:05 +01006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 if (hs_buf->is_valid == 1) {
Hanno Beckere605b192018-08-21 15:59:07 +01006427 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01006428 mbedtls_zeroize_and_free(hs_buf->data, hs_buf->data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Hanno Becker0271f962018-08-16 13:23:47 +01006430 }
6431}
6432
6433#endif /* MBEDTLS_SSL_PROTO_DTLS */
6434
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006435/*
6436 * Convert version numbers to/from wire format
6437 * and, for DTLS, to/from TLS equivalent.
6438 *
6439 * For TLS this is the identity.
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006440 * For DTLS, map as follows, then use 1's complement (v -> ~v):
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006441 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006442 * DTLS 1.0 is stored as TLS 1.1 internally
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006443 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006444void mbedtls_ssl_write_version(unsigned char version[2], int transport,
6445 mbedtls_ssl_protocol_version tls_version)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006446{
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01006447 uint16_t tls_version_formatted;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01006450 tls_version_formatted =
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 ~(tls_version - (tls_version == 0x0302 ? 0x0202 : 0x0201));
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01006452 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006453#else
6454 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006455#endif
Agathiyan Bragadeesh8b52b882023-07-13 13:12:40 +01006456 {
6457 tls_version_formatted = (uint16_t) tls_version;
6458 }
6459 MBEDTLS_PUT_UINT16_BE(tls_version_formatted, version, 0);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006460}
6461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462uint16_t mbedtls_ssl_read_version(const unsigned char version[2],
6463 int transport)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006464{
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 uint16_t tls_version = MBEDTLS_GET_UINT16_BE(version, 0);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006468 tls_version =
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 ~(tls_version - (tls_version == 0xfeff ? 0x0202 : 0x0201));
6470 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006471#else
6472 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006473#endif
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006474 return tls_version;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006475}
6476
Jerry Yue7047812021-09-13 19:26:39 +08006477/*
Jerry Yu3bf1f972021-09-22 21:37:18 +08006478 * Send pending fatal alert.
6479 * 0, No alert message.
6480 * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
6481 * returned, ssl->alert_reason otherwise.
Jerry Yue7047812021-09-13 19:26:39 +08006482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006483int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl)
Jerry Yue7047812021-09-13 19:26:39 +08006484{
6485 int ret;
6486
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006487 /* No pending alert, return success*/
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 if (ssl->send_alert == 0) {
6489 return 0;
6490 }
Jerry Yu394ece62021-09-14 22:17:21 +08006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 ret = mbedtls_ssl_send_alert_message(ssl,
6493 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6494 ssl->alert_type);
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006495
Jerry Yu3bf1f972021-09-22 21:37:18 +08006496 /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
6497 * do not clear the alert to be able to send it later.
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006500 ssl->send_alert = 0;
Jerry Yue7047812021-09-13 19:26:39 +08006501 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 if (ret != 0) {
6504 return ret;
6505 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006506
Gilles Peskine449bd832023-01-11 14:50:10 +01006507 return ssl->alert_reason;
Jerry Yue7047812021-09-13 19:26:39 +08006508}
6509
Jerry Yu394ece62021-09-14 22:17:21 +08006510/*
6511 * Set pending fatal alert flag.
6512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006513void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl,
6514 unsigned char alert_type,
6515 int alert_reason)
Jerry Yu394ece62021-09-14 22:17:21 +08006516{
6517 ssl->send_alert = 1;
6518 ssl->alert_type = alert_type;
6519 ssl->alert_reason = alert_reason;
6520}
6521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522#endif /* MBEDTLS_SSL_TLS_C */