blob: 1a314a87f0021047f6c3f1489940fa7f7b89a165 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000019 */
20/*
Paul Bakker5121ce52009-01-03 21:22:43 +000021 * http://www.ietf.org/rfc/rfc2246.txt
22 * http://www.ietf.org/rfc/rfc4346.txt
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
SimonBd5800b72016-04-26 07:43:27 +010029#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000032#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000033#include "mbedtls/debug.h"
34#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010036#include "mbedtls/version.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020037#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020038#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050042#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020043#include "psa_util_internal.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050044#include "psa/crypto.h"
45#endif
46
Janos Follath23bdca02016-10-07 14:47:14 +010047#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020049#endif
50
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek00644842023-05-30 05:45:00 -040052/* Define a local translating function to save code size by not using too many
53 * arguments in each translating place. */
54static int local_err_translation(psa_status_t status)
55{
56 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040057 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040058 psa_generic_status_to_mbedtls);
59}
60#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050061#endif
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020065/*
66 * Start a timer.
67 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020068 */
Gilles Peskine449bd832023-01-11 14:50:10 +010069void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020070{
Gilles Peskine449bd832023-01-11 14:50:10 +010071 if (ssl->f_set_timer == NULL) {
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020072 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010073 }
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs));
76 ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs);
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077}
78
79/*
80 * Return -1 is timer is expired, 0 if it isn't.
81 */
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (ssl->f_get_timer == NULL) {
85 return 0;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020086 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (ssl->f_get_timer(ssl->p_timer) == 2) {
89 MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired"));
90 return -1;
91 }
92
93 return 0;
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010097static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
98 unsigned char *buf,
99 size_t len,
100 mbedtls_record *rec);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl,
103 unsigned char *buf,
104 size_t buflen)
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200105{
106 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record"));
108 MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200109
110 /* We don't support record checking in TLS because
TRodziewicz2abf03c2021-06-25 14:40:09 +0200111 * there doesn't seem to be a usecase for it.
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200114 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
115 goto exit;
116 }
117#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 else {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200119 mbedtls_record rec;
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 ret = ssl_parse_record_header(ssl, buf, buflen, &rec);
122 if (ret != 0) {
123 MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200124 goto exit;
125 }
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (ssl->transform_in != NULL) {
128 ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec);
129 if (ret != 0) {
130 MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200131 goto exit;
132 }
133 }
134 }
135#endif /* MBEDTLS_SSL_PROTO_DTLS */
136
137exit:
138 /* On success, we have decrypted the buffer in-place, so make
139 * sure we don't leak any plaintext data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_platform_zeroize(buf, buflen);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200141
142 /* For the purpose of this API, treat messages with unexpected CID
143 * as well as such from future epochs as unexpected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
145 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200146 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
147 }
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record"));
150 return ret;
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200151}
152
Hanno Becker67bc7c32018-08-06 11:33:50 +0100153#define SSL_DONT_FORCE_FLUSH 0
154#define SSL_FORCE_FLUSH 1
155
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100157
Hanno Beckerd5847772018-08-28 10:09:23 +0100158/* Forward declarations for functions related to message buffering. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
160 uint8_t slot);
161static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200162MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100163static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200164MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100165static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200166MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100167static int ssl_buffer_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200168MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100169static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
170 mbedtls_record const *rec);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200171MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
Hanno Beckerd5847772018-08-28 10:09:23 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100175{
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
Darryl Greenb33cc762019-11-28 14:29:44 +0000177#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
178 size_t out_buf_len = ssl->out_buf_len;
179#else
180 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
181#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (mtu != 0 && mtu < out_buf_len) {
184 return mtu;
185 }
Hanno Becker2b1e3542018-08-06 11:19:13 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return out_buf_len;
Hanno Becker2b1e3542018-08-06 11:19:13 +0100188}
189
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200190MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100191static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100192{
Hanno Becker11682cc2018-08-22 14:41:02 +0100193 size_t const bytes_written = ssl->out_left;
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 size_t const mtu = ssl_get_maximum_datagram_size(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100195
196 /* Double-check that the write-index hasn't gone
197 * past what we can transmit in a single datagram. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (bytes_written > mtu) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100199 /* Should never happen... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return (int) (mtu - bytes_written);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100204}
205
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200206MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100207static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100208{
Janos Follath865b3eb2019-12-16 11:46:15 +0000209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400211 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100212
213#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (max_len > mfl) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100217 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100219
220 /* By the standard (RFC 6066 Sect. 4), the MFL extension
221 * only limits the maximum record payload size, so in theory
222 * we would be allowed to pack multiple records of payload size
223 * MFL into a single datagram. However, this would mean that there's
224 * no way to explicitly communicate MTU restrictions to the peer.
225 *
226 * The following reduction of max_len makes sure that we never
227 * write datagrams larger than MFL + Record Expansion Overhead.
228 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (max_len <= ssl->out_left) {
230 return 0;
231 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100232
233 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100234#endif
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 ret = ssl_get_remaining_space_in_datagram(ssl);
237 if (ret < 0) {
238 return ret;
239 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 remaining = (size_t) ret;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 ret = mbedtls_ssl_get_record_expansion(ssl);
243 if (ret < 0) {
244 return ret;
245 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100246 expansion = (size_t) ret;
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (remaining <= expansion) {
249 return 0;
250 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251
252 remaining -= expansion;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (remaining >= max_len) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100254 remaining = max_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return (int) remaining;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100258}
259
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200260/*
261 * Double the retransmit timeout value, within the allowed range,
262 * returning -1 if the maximum value has already been reached.
263 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100265static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200266{
267 uint32_t new_timeout;
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) {
270 return -1;
271 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200272
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200273 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
274 * in the following way: after the initial transmission and a first
275 * retransmission, back off to a temporary estimated MTU of 508 bytes.
276 * This value is guaranteed to be deliverable (if not guaranteed to be
277 * delivered) of any compliant IPv4 (and IPv6) network, and should work
278 * on most non-IP stacks too. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200280 ssl->handshake->mtu = 508;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu));
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400282 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200283
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284 new_timeout = 2 * ssl->handshake->retransmit_timeout;
285
286 /* Avoid arithmetic overflow and range overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (new_timeout < ssl->handshake->retransmit_timeout ||
288 new_timeout > ssl->conf->hs_timeout_max) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200289 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290 }
291
292 ssl->handshake->retransmit_timeout = new_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
294 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return 0;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297}
298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200301 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
303 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200306
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100307/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200309 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000310
Ronald Cron6f135e12021-12-08 16:57:54 +0100311#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker13996922020-05-28 16:15:19 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313static size_t ssl_compute_padding_length(size_t len,
314 size_t granularity)
Hanno Becker13996922020-05-28 16:15:19 +0100315{
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return (granularity - (len + 1) % granularity) % granularity;
Hanno Becker13996922020-05-28 16:15:19 +0100317}
318
Hanno Becker581bc1b2020-05-04 12:20:03 +0100319/* This functions transforms a (D)TLS plaintext fragment and a record content
320 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
321 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
322 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100323 *
324 * struct {
325 * opaque content[DTLSPlaintext.length];
326 * ContentType real_type;
327 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100328 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100329 *
330 * Input:
331 * - `content`: The beginning of the buffer holding the
332 * plaintext to be wrapped.
333 * - `*content_size`: The length of the plaintext in Bytes.
334 * - `max_len`: The number of Bytes available starting from
335 * `content`. This must be `>= *content_size`.
336 * - `rec_type`: The desired record content type.
337 *
338 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100339 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
340 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100341 *
342 * Returns:
343 * - `0` on success.
344 * - A negative error code if `max_len` didn't offer enough space
345 * for the expansion.
346 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200347MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100348static int ssl_build_inner_plaintext(unsigned char *content,
349 size_t *content_size,
350 size_t remaining,
351 uint8_t rec_type,
352 size_t pad)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100353{
354 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100355
356 /* Write real content type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (remaining == 0) {
358 return -1;
359 }
360 content[len] = rec_type;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100361 len++;
362 remaining--;
363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (remaining < pad) {
365 return -1;
366 }
367 memset(content + len, 0, pad);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100368 len += pad;
369 remaining -= pad;
370
371 *content_size = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100373}
374
Hanno Becker581bc1b2020-05-04 12:20:03 +0100375/* This function parses a (D)TLSInnerPlaintext structure.
376 * See ssl_build_inner_plaintext() for details. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200377MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100378static int ssl_parse_inner_plaintext(unsigned char const *content,
379 size_t *content_size,
380 uint8_t *rec_type)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100381{
382 size_t remaining = *content_size;
383
384 /* Determine length of padding by skipping zeroes from the back. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 do {
386 if (remaining == 0) {
387 return -1;
388 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100389 remaining--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } while (content[remaining] == 0);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100391
392 *content_size = remaining;
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 *rec_type = content[remaining];
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100396}
Ronald Cron6f135e12021-12-08 16:57:54 +0100397#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100398
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200399/* The size of the `add_data` structure depends on various
400 * factors, namely
401 *
402 * 1) CID functionality disabled
403 *
404 * additional_data =
405 * 8: seq_num +
406 * 1: type +
407 * 2: version +
408 * 2: length of inner plaintext +
409 *
410 * size = 13 bytes
411 *
412 * 2) CID functionality based on RFC 9146 enabled
413 *
414 * size = 8 + 1 + 1 + 1 + 2 + 2 + 6 + 2 + CID-length
415 * = 23 + CID-length
416 *
417 * 3) CID functionality based on legacy CID version
418 according to draft-ietf-tls-dtls-connection-id-05
419 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
420 *
421 * size = 13 + 1 + CID-length
422 *
423 * More information about the CID usage:
424 *
425 * Per Section 5.3 of draft-ietf-tls-dtls-connection-id-05 the
426 * size of the additional data structure is calculated as:
427 *
428 * additional_data =
429 * 8: seq_num +
430 * 1: tls12_cid +
431 * 2: DTLSCipherText.version +
432 * n: cid +
433 * 1: cid_length +
434 * 2: length_of_DTLSInnerPlaintext
435 *
436 * Per RFC 9146 the size of the add_data structure is calculated as:
437 *
438 * additional_data =
439 * 8: seq_num_placeholder +
440 * 1: tls12_cid +
441 * 1: cid_length +
442 * 1: tls12_cid +
443 * 2: DTLSCiphertext.version +
444 * 2: epoch +
445 * 6: sequence_number +
446 * n: cid +
447 * 2: length_of_DTLSInnerPlaintext
448 *
449 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100450static void ssl_extract_add_data_from_record(unsigned char *add_data,
451 size_t *add_data_len,
452 mbedtls_record *rec,
453 mbedtls_ssl_protocol_version
454 tls_version,
455 size_t taglen)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000456{
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200457 /* Several types of ciphers have been defined for use with TLS and DTLS,
458 * and the MAC calculations for those ciphers differ slightly. Further
459 * variants were added when the CID functionality was added with RFC 9146.
460 * This implementations also considers the use of a legacy version of the
461 * CID specification published in draft-ietf-tls-dtls-connection-id-05,
462 * which is used in deployments.
463 *
464 * We will distinguish between the non-CID and the CID cases below.
465 *
466 * --- Non-CID cases ---
467 *
468 * Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100469 *
470 * additional_data = seq_num + TLSCompressed.type +
471 * TLSCompressed.version + TLSCompressed.length;
472 *
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100473 * For TLS 1.3, the record sequence number is dropped from the AAD
474 * and encoded within the nonce of the AEAD operation instead.
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000475 * Moreover, the additional data involves the length of the TLS
476 * ciphertext, not the TLS plaintext as in earlier versions.
477 * Quoting RFC 8446 (TLS 1.3):
478 *
479 * additional_data = TLSCiphertext.opaque_type ||
480 * TLSCiphertext.legacy_record_version ||
481 * TLSCiphertext.length
482 *
483 * We pass the tag length to this function in order to compute the
484 * ciphertext length from the inner plaintext length rec->data_len via
485 *
486 * TLSCiphertext.length = TLSInnerPlaintext.length + taglen.
487 *
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200488 * --- CID cases ---
489 *
490 * RFC 9146 uses a common pattern when constructing the data
491 * passed into a MAC / AEAD cipher.
492 *
493 * Data concatenation for MACs used with block ciphers with
494 * Encrypt-then-MAC Processing (with CID):
495 *
496 * data = seq_num_placeholder +
497 * tls12_cid +
498 * cid_length +
499 * tls12_cid +
500 * DTLSCiphertext.version +
501 * epoch +
502 * sequence_number +
503 * cid +
504 * DTLSCiphertext.length +
505 * IV +
506 * ENC(content + padding + padding_length)
507 *
508 * Data concatenation for MACs used with block ciphers (with CID):
509 *
510 * data = seq_num_placeholder +
511 * tls12_cid +
512 * cid_length +
513 * tls12_cid +
514 * DTLSCiphertext.version +
515 * epoch +
516 * sequence_number +
517 * cid +
518 * length_of_DTLSInnerPlaintext +
519 * DTLSInnerPlaintext.content +
520 * DTLSInnerPlaintext.real_type +
521 * DTLSInnerPlaintext.zeros
522 *
523 * AEAD ciphers use the following additional data calculation (with CIDs):
524 *
525 * additional_data = seq_num_placeholder +
526 * tls12_cid +
527 * cid_length +
528 * tls12_cid +
529 * DTLSCiphertext.version +
530 * epoch +
531 * sequence_number +
532 * cid +
533 * length_of_DTLSInnerPlaintext
534 *
535 * Section 5.3 of draft-ietf-tls-dtls-connection-id-05 (for legacy CID use)
536 * defines the additional data calculation as follows:
537 *
538 * additional_data = seq_num +
539 * tls12_cid +
540 * DTLSCipherText.version +
541 * cid +
542 * cid_length +
543 * length_of_DTLSInnerPlaintext
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 */
Hanno Beckercab87e62019-04-29 13:52:53 +0100545
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100546 unsigned char *cur = add_data;
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000547 size_t ad_len_field = rec->data_len;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100548
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200549#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
550 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
551 const unsigned char seq_num_placeholder[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
552#endif
553
Ronald Cron6f135e12021-12-08 16:57:54 +0100554#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000556 /* In TLS 1.3, the AAD contains the length of the TLSCiphertext,
557 * which differs from the length of the TLSInnerPlaintext
558 * by the length of the authentication tag. */
559 ad_len_field += taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 } else
Ronald Cron6f135e12021-12-08 16:57:54 +0100561#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100562 {
Glenn Strauss07c64162022-03-14 12:34:51 -0400563 ((void) tls_version);
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000564 ((void) taglen);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200565
Manuel Pégourié-Gonnard61336842022-11-25 11:12:38 +0100566#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
568 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200569 // seq_num_placeholder
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memcpy(cur, seq_num_placeholder, sizeof(seq_num_placeholder));
571 cur += sizeof(seq_num_placeholder);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200572
573 // tls12_cid type
574 *cur = rec->type;
575 cur++;
576
577 // cid_length
578 *cur = rec->cid_len;
579 cur++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 } else
Manuel Pégourié-Gonnard61336842022-11-25 11:12:38 +0100581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200582 {
583 // epoch + sequence number
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 memcpy(cur, rec->ctr, sizeof(rec->ctr));
585 cur += sizeof(rec->ctr);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200586 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100587 }
588
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200589 // type
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100590 *cur = rec->type;
591 cur++;
592
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200593 // version
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 memcpy(cur, rec->ver, sizeof(rec->ver));
595 cur += sizeof(rec->ver);
Hanno Beckercab87e62019-04-29 13:52:53 +0100596
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200597#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
598 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 1
599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200601 // CID
602 memcpy(cur, rec->cid, rec->cid_len);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100603 cur += rec->cid_len;
604
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200605 // cid_length
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100606 *cur = rec->cid_len;
607 cur++;
608
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200609 // length of inner plaintext
610 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
611 cur += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 } else
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200613#elif defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
614 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200617 // epoch + sequence number
618 memcpy(cur, rec->ctr, sizeof(rec->ctr));
619 cur += sizeof(rec->ctr);
620
621 // CID
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 memcpy(cur, rec->cid, rec->cid_len);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200623 cur += rec->cid_len;
624
625 // length of inner plaintext
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100627 cur += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100629#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100630 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100632 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100633 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100634
635 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000636}
637
Hanno Becker67a37db2020-05-28 16:27:07 +0100638#if defined(MBEDTLS_GCM_C) || \
639 defined(MBEDTLS_CCM_C) || \
640 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200641MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker17263802020-05-28 07:05:48 +0100642static int ssl_transform_aead_dynamic_iv_is_explicit(
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 mbedtls_ssl_transform const *transform)
Hanno Beckerdf8be222020-05-21 15:30:57 +0100644{
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return transform->ivlen != transform->fixed_ivlen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100646}
647
Hanno Becker17263802020-05-28 07:05:48 +0100648/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
649 *
650 * Concretely, this occurs in two variants:
651 *
652 * a) Fixed and dynamic IV lengths add up to total IV length, giving
653 * IV = fixed_iv || dynamic_iv
654 *
Hanno Becker15952812020-06-04 13:31:46 +0100655 * This variant is used in TLS 1.2 when used with GCM or CCM.
656 *
Hanno Becker17263802020-05-28 07:05:48 +0100657 * b) Fixed IV lengths matches total IV length, giving
658 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100659 *
660 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
661 *
662 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100663 *
664 * This function has the precondition that
665 *
666 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
667 *
668 * which has to be ensured by the caller. If this precondition
669 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671static void ssl_build_record_nonce(unsigned char *dst_iv,
672 size_t dst_iv_len,
673 unsigned char const *fixed_iv,
674 size_t fixed_iv_len,
675 unsigned char const *dynamic_iv,
676 size_t dynamic_iv_len)
Hanno Becker17263802020-05-28 07:05:48 +0100677{
Hanno Beckerdf8be222020-05-21 15:30:57 +0100678 /* Start with Fixed IV || 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 memset(dst_iv, 0, dst_iv_len);
680 memcpy(dst_iv, fixed_iv, fixed_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100681
Hanno Becker17263802020-05-28 07:05:48 +0100682 dst_iv += dst_iv_len - dynamic_iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_xor(dst_iv, dst_iv, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100684}
Hanno Becker67a37db2020-05-28 16:27:07 +0100685#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
688 mbedtls_ssl_transform *transform,
689 mbedtls_record *rec,
690 int (*f_rng)(void *, unsigned char *, size_t),
691 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +0000692{
Neil Armstrong136f8402022-03-30 10:58:01 +0200693 mbedtls_ssl_mode_t ssl_mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100694 int auth_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 unsigned char *data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200696 /* For an explanation of the additional data length see
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 * the description of ssl_extract_add_data_from_record().
698 */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200699#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
700 unsigned char add_data[23 + MBEDTLS_SSL_CID_OUT_LEN_MAX];
701#else
702 unsigned char add_data[13];
703#endif
Hanno Beckercab87e62019-04-29 13:52:53 +0100704 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000705 size_t post_avail;
706
707 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000708#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200709 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000710 ((void) ssl);
711#endif
712
713 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200714 * for CBC transformations in TLS 1.2. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715#if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
716 defined(MBEDTLS_SSL_PROTO_TLS1_2))
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000717 ((void) f_rng);
718 ((void) p_rng);
719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (transform == NULL) {
724 MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf"));
725 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000726 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (rec == NULL
Hanno Becker43c24b82019-05-01 09:45:57 +0100728 || rec->buf == NULL
729 || rec->buf_len < rec->data_offset
730 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100731#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100732 || rec->cid_len != 0
733#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 ) {
735 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf"));
736 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
Neil Armstrong136f8402022-03-30 10:58:01 +0200740
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000741 data = rec->buf + rec->data_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
743 MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload",
744 data, rec->data_len);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
747 MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET
748 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
749 rec->data_len,
750 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
751 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000752 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100753
Hanno Becker92313402020-05-20 13:58:58 +0100754 /* The following two code paths implement the (D)TLSInnerPlaintext
755 * structure present in TLS 1.3 and DTLS 1.2 + CID.
756 *
757 * See ssl_build_inner_plaintext() for more information.
758 *
759 * Note that this changes `rec->data_len`, and hence
760 * `post_avail` needs to be recalculated afterwards.
761 *
762 * Note also that the two code paths cannot occur simultaneously
763 * since they apply to different versions of the protocol. There
764 * is hence no risk of double-addition of the inner plaintext.
765 */
Ronald Cron6f135e12021-12-08 16:57:54 +0100766#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Becker13996922020-05-28 16:15:19 +0100768 size_t padding =
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 ssl_compute_padding_length(rec->data_len,
770 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
771 if (ssl_build_inner_plaintext(data,
772 &rec->data_len,
773 post_avail,
774 rec->type,
775 padding) != 0) {
776 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerccc13d02020-05-04 12:30:04 +0100777 }
778
779 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
780 }
Ronald Cron6f135e12021-12-08 16:57:54 +0100781#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100782
Hanno Beckera0e20d02019-05-15 14:03:01 +0100783#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100784 /*
785 * Add CID information
786 */
787 rec->cid_len = transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 memcpy(rec->cid, transform->out_cid, transform->out_cid_len);
789 MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (rec->cid_len != 0) {
Hanno Becker13996922020-05-28 16:15:19 +0100792 size_t padding =
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 ssl_compute_padding_length(rec->data_len,
794 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100795 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100796 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100797 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100798 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100799 * Note that this changes `rec->data_len`, and hence
800 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100801 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (ssl_build_inner_plaintext(data,
803 &rec->data_len,
804 post_avail,
805 rec->type,
806 padding) != 0) {
807 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100808 }
809
810 rec->type = MBEDTLS_SSL_MSG_CID;
811 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100812#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100815
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100817 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000819#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
821 ssl_mode == MBEDTLS_SSL_MODE_CBC) {
822 if (post_avail < transform->maclen) {
823 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
824 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000825 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200826#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +0200827 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong26e6d672022-02-23 09:30:33 +0100829#if defined(MBEDTLS_USE_PSA_CRYPTO)
830 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
831 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
832 size_t sign_mac_length = 0;
833#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker992b6872017-11-09 18:57:39 +0000834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
836 transform->tls_version,
837 transform->taglen);
Hanno Becker992b6872017-11-09 18:57:39 +0000838
Neil Armstrong26e6d672022-02-23 09:30:33 +0100839#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
841 transform->psa_mac_alg);
842 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100843 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 status = psa_mac_update(&operation, add_data, add_data_len);
847 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100848 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 status = psa_mac_update(&operation, data, rec->data_len);
852 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100853 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
857 &sign_mac_length);
858 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100859 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100861#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
863 add_data_len);
864 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100865 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 }
867 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len);
868 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100869 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 }
871 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
872 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100873 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 }
875 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
876 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100877 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100879#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 memcpy(data + rec->data_len, mac, transform->maclen);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200882#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len,
885 transform->maclen);
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200886
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000887 rec->data_len += transform->maclen;
888 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100889 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891hmac_failed_etm_disabled:
892 mbedtls_platform_zeroize(mac, transform->maclen);
Neil Armstrong26e6d672022-02-23 09:30:33 +0100893#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500894 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 status = psa_mac_abort(&operation);
896 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500897 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 }
Neil Armstrong4313f552022-03-02 15:14:07 +0100899#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (ret != 0) {
901 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
902 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100903 }
Paul Bakker577e0062013-08-28 11:57:20 +0200904 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000905#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200907 /*
908 * Encrypt
909 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000910#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
912 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
913 "including %d bytes of padding",
914 rec->data_len, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +0000915
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +0100916 /* The only supported stream cipher is "NULL",
917 * so there's nothing to do here.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 } else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000919#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000920
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200921#if defined(MBEDTLS_GCM_C) || \
922 defined(MBEDTLS_CCM_C) || \
923 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200925 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100926 unsigned char *dynamic_iv;
927 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100928 int dynamic_iv_is_explicit =
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 ssl_transform_aead_dynamic_iv_is_explicit(transform);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100930#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +0100931 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100932#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000934
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100935 /* Check that there's space for the authentication tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (post_avail < transform->taglen) {
937 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
938 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000939 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000940
Paul Bakker68884e32013-01-07 18:20:04 +0100941 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100942 * Build nonce for AEAD encryption.
943 *
944 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
945 * part of the IV is prepended to the ciphertext and
946 * can be chosen freely - in particular, it need not
947 * agree with the record sequence number.
948 * However, since ChaChaPoly as well as all AEAD modes
949 * in TLS 1.3 use the record sequence number as the
950 * dynamic part of the nonce, we uniformly use the
951 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100952 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100953 dynamic_iv = rec->ctr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 dynamic_iv_len = sizeof(rec->ctr);
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 ssl_build_record_nonce(iv, sizeof(iv),
957 transform->iv_enc,
958 transform->fixed_ivlen,
959 dynamic_iv,
960 dynamic_iv_len);
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100961
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100962 /*
963 * Build additional data for AEAD encryption.
964 * This depends on the TLS version.
965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
967 transform->tls_version,
968 transform->taglen);
Hanno Becker1f10d762019-04-26 13:34:37 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)",
971 iv, transform->ivlen);
972 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)",
973 dynamic_iv,
974 dynamic_iv_is_explicit ? dynamic_iv_len : 0);
975 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
976 add_data, add_data_len);
977 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
978 "including 0 bytes of padding",
979 rec->data_len));
Paul Bakkerca4ab492012-04-18 14:23:57 +0000980
Paul Bakker68884e32013-01-07 18:20:04 +0100981 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200982 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200983 */
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100984#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 status = psa_aead_encrypt(transform->psa_key_enc,
986 transform->psa_alg,
987 iv, transform->ivlen,
988 add_data, add_data_len,
989 data, rec->data_len,
990 data, rec->buf_len - (data - rec->buf),
991 &rec->data_len);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500994 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret);
996 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100997 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100998#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc,
1000 iv, transform->ivlen,
1001 add_data, add_data_len,
1002 data, rec->data_len, /* src */
1003 data, rec->buf_len - (data - rec->buf), /* dst */
1004 &rec->data_len,
1005 transform->taglen)) != 0) {
1006 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt_ext", ret);
1007 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001008 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001009#endif /* MBEDTLS_USE_PSA_CRYPTO */
1010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
1012 data + rec->data_len - transform->taglen,
1013 transform->taglen);
Hanno Beckerdf8be222020-05-21 15:30:57 +01001014 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001016
1017 /*
1018 * Prefix record content with dynamic IV in case it is explicit.
1019 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 if (dynamic_iv_is_explicit != 0) {
1021 if (rec->data_offset < dynamic_iv_len) {
1022 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1023 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001024 }
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +01001027 rec->data_offset -= dynamic_iv_len;
1028 rec->data_len += dynamic_iv_len;
1029 }
1030
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001031 auth_done++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 } else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +01001033#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001034#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1036 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001037 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001038 size_t padlen, i;
1039 size_t olen;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001040#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001041 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001042 size_t part_len;
1043 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1044#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001045
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001046 /* Currently we're always using minimal padding
1047 * (up to 255 bytes would be allowed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen;
1049 if (padlen == transform->ivlen) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 padlen = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001051 }
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 /* Check there's enough space in the buffer for the padding. */
1054 if (post_avail < padlen + 1) {
1055 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1056 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1057 }
1058
1059 for (i = 0; i <= padlen; i++) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001060 data[rec->data_len + i] = (unsigned char) padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001062
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001063 rec->data_len += padlen + 1;
1064 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001065
TRodziewicz0f82ec62021-05-12 17:49:18 +02001066#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001067 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +02001068 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +00001069 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (f_rng == NULL) {
1072 MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine"));
1073 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001074 }
TRodziewicz345165c2021-07-06 13:42:11 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (rec->data_offset < transform->ivlen) {
1077 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1078 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
TRodziewicz345165c2021-07-06 13:42:11 +02001079 }
1080
1081 /*
1082 * Generate IV
1083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 ret = f_rng(p_rng, transform->iv_enc, transform->ivlen);
1085 if (ret != 0) {
1086 return ret;
1087 }
TRodziewicz345165c2021-07-06 13:42:11 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 memcpy(data - transform->ivlen, transform->iv_enc, transform->ivlen);
TRodziewicz0f82ec62021-05-12 17:49:18 +02001090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
1093 "including %"
1094 MBEDTLS_PRINTF_SIZET
1095 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
1096 rec->data_len, transform->ivlen,
1097 padlen + 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001099#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 status = psa_cipher_encrypt_setup(&cipher_op,
1101 transform->psa_key_enc, transform->psa_alg);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001104 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_encrypt_setup", ret);
1106 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001107 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 status = psa_cipher_set_iv(&cipher_op, transform->iv_enc, transform->ivlen);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if (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 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
1114 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001115
1116 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 status = psa_cipher_update(&cipher_op,
1119 data, rec->data_len,
1120 data, rec->data_len, &olen);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001123 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
1125 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001126
1127 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 status = psa_cipher_finish(&cipher_op,
1130 data + olen, rec->data_len - olen,
1131 &part_len);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001134 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
1136 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001137
1138 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001139
1140 olen += part_len;
1141#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1143 transform->iv_enc,
1144 transform->ivlen,
1145 data, rec->data_len,
1146 data, &olen)) != 0) {
1147 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1148 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001149 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001150#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (rec->data_len != olen) {
1153 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1154 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001155 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001156
TRodziewicz0f82ec62021-05-12 17:49:18 +02001157 data -= transform->ivlen;
1158 rec->data_offset -= transform->ivlen;
1159 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (auth_done == 0) {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001163 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001164#if defined(MBEDTLS_USE_PSA_CRYPTO)
1165 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1166 size_t sign_mac_length = 0;
1167#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker3d8c9072018-01-05 16:24:22 +00001168
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001169 /* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length))
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001170 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (post_avail < transform->maclen) {
1173 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1174 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001175 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 ssl_extract_add_data_from_record(add_data, &add_data_len,
1178 rec, transform->tls_version,
1179 transform->taglen);
Hanno Becker1f10d762019-04-26 13:34:37 +01001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
1182 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1183 add_data_len);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001184#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
1186 transform->psa_mac_alg);
1187 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001188 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 status = psa_mac_update(&operation, add_data, add_data_len);
1192 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001193 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 status = psa_mac_update(&operation, data, rec->data_len);
1197 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001198 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
1202 &sign_mac_length);
1203 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001204 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001206#else
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
1209 add_data_len);
1210 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001211 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
1213 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
1214 data, rec->data_len);
1215 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001216 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 }
1218 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
1219 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001220 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 }
1222 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
1223 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001224 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001226#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 memcpy(data + rec->data_len, mac, transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001229
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001230 rec->data_len += transform->maclen;
1231 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001232 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234hmac_failed_etm_enabled:
1235 mbedtls_platform_zeroize(mac, transform->maclen);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001237 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 status = psa_mac_abort(&operation);
1239 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001240 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 }
Neil Armstrong4313f552022-03-02 15:14:07 +01001242#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (ret != 0) {
1244 MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
1245 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001246 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001250#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001251 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1253 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001254 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001256 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (auth_done != 1) {
1258 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1259 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001260 }
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001265}
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1268 mbedtls_ssl_transform *transform,
1269 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00001270{
Przemek Stekiel4c499272022-09-27 13:55:37 +02001271#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) || defined(MBEDTLS_CIPHER_MODE_AEAD)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001272 size_t olen;
Przemek Stekiel4c499272022-09-27 13:55:37 +02001273#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC || MBEDTLS_CIPHER_MODE_AEAD */
Neil Armstrong136f8402022-03-30 10:58:01 +02001274 mbedtls_ssl_mode_t ssl_mode;
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001275 int ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001276
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001277 int auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001278#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001279 size_t padlen = 0, correct = 1;
1280#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 unsigned char *data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001282 /* For an explanation of the additional data length see
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 * the description of ssl_extract_add_data_from_record().
1284 */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001285#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1286 unsigned char add_data[23 + MBEDTLS_SSL_CID_IN_LEN_MAX];
1287#else
1288 unsigned char add_data[13];
1289#endif
Hanno Beckercab87e62019-04-29 13:52:53 +01001290 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001291
Hanno Beckera18d1322018-01-03 14:27:32 +00001292#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001293 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001294 ((void) ssl);
1295#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf"));
1298 if (rec == NULL ||
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001299 rec->buf == NULL ||
1300 rec->buf_len < rec->data_offset ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 rec->buf_len - rec->data_offset < rec->data_len) {
1302 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf"));
1303 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001304 }
1305
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001306 data = rec->buf + rec->data_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Hanno Beckera0e20d02019-05-15 14:03:01 +01001309#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001310 /*
1311 * Match record's CID with incoming CID.
1312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (rec->cid_len != transform->in_cid_len ||
1314 memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) {
1315 return MBEDTLS_ERR_SSL_UNEXPECTED_CID;
Hanno Becker938489a2019-05-08 13:02:22 +01001316 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001318
Hanno Beckerd086bf02021-03-22 13:01:27 +00001319#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +01001321 /* The only supported stream cipher is "NULL",
1322 * so there's nothing to do here.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 } else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001324#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001325#if defined(MBEDTLS_GCM_C) || \
1326 defined(MBEDTLS_CCM_C) || \
1327 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001329 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001330 unsigned char *dynamic_iv;
1331 size_t dynamic_iv_len;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001332#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001333 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001334#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001335
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001336 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001337 * Extract dynamic part of nonce for AEAD decryption.
1338 *
1339 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1340 * part of the IV is prepended to the ciphertext and
1341 * can be chosen freely - in particular, it need not
1342 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 dynamic_iv_len = sizeof(rec->ctr);
1345 if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) {
1346 if (rec->data_len < dynamic_iv_len) {
1347 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1348 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
1349 rec->data_len,
1350 dynamic_iv_len));
1351 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001352 }
1353 dynamic_iv = data;
1354
1355 data += dynamic_iv_len;
1356 rec->data_offset += dynamic_iv_len;
1357 rec->data_len -= dynamic_iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 } else {
Hanno Becker17263802020-05-28 07:05:48 +01001359 dynamic_iv = rec->ctr;
1360 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001361
1362 /* Check that there's space for the authentication tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 if (rec->data_len < transform->taglen) {
1364 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1365 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
1366 rec->data_len,
1367 transform->taglen));
1368 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001369 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001370 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001371
Hanno Beckerdf8be222020-05-21 15:30:57 +01001372 /*
1373 * Prepare nonce from dynamic and static parts.
1374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 ssl_build_record_nonce(iv, sizeof(iv),
1376 transform->iv_dec,
1377 transform->fixed_ivlen,
1378 dynamic_iv,
1379 dynamic_iv_len);
Paul Bakker68884e32013-01-07 18:20:04 +01001380
Hanno Beckerdf8be222020-05-21 15:30:57 +01001381 /*
1382 * Build additional data for AEAD encryption.
1383 * This depends on the TLS version.
1384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1386 transform->tls_version,
1387 transform->taglen);
1388 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
1389 add_data, add_data_len);
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001390
Hanno Beckerd96a6522019-07-10 13:55:25 +01001391 /* Because of the check above, we know that there are
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001392 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001393 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001394 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001395 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen);
1398 MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len,
1399 transform->taglen);
Paul Bakker68884e32013-01-07 18:20:04 +01001400
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001401 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001402 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001403 */
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001404#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 status = psa_aead_decrypt(transform->psa_key_dec,
1406 transform->psa_alg,
1407 iv, transform->ivlen,
1408 add_data, add_data_len,
1409 data, rec->data_len + transform->taglen,
1410 data, rec->buf_len - (data - rec->buf),
1411 &olen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001414 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret);
1416 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001417 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001418#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec,
1420 iv, transform->ivlen,
1421 add_data, add_data_len,
1422 data, rec->data_len + transform->taglen, /* src */
1423 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
1424 transform->taglen)) != 0) {
1425 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret);
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
1428 return MBEDTLS_ERR_SSL_INVALID_MAC;
1429 }
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001432 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001433#endif /* MBEDTLS_USE_PSA_CRYPTO */
1434
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001435 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436
Hanno Beckerd96a6522019-07-10 13:55:25 +01001437 /* Double-check that AEAD decryption doesn't change content length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (olen != rec->data_len) {
1439 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1440 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001441 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001444#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1446 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001447 size_t minlen = 0;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001448#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001450 size_t part_len;
1451 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1452#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001453
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 /*
Paul Bakker45829992013-01-03 14:52:21 +01001455 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001457#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001458 /* The ciphertext is prefixed with the CBC IV. */
1459 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001460#endif
Paul Bakker45829992013-01-03 14:52:21 +01001461
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001462 /* Size considerations:
1463 *
1464 * - The CBC cipher text must not be empty and hence
1465 * at least of size transform->ivlen.
1466 *
1467 * Together with the potential IV-prefix, this explains
1468 * the first of the two checks below.
1469 *
1470 * - The record must contain a MAC, either in plain or
1471 * encrypted, depending on whether Encrypt-then-MAC
1472 * is used or not.
1473 * - If it is, the message contains the IV-prefix,
1474 * the CBC ciphertext, and the MAC.
1475 * - If it is not, the padded plaintext, and hence
1476 * the CBC ciphertext, has at least length maclen + 1
1477 * because there is at least the padding length byte.
1478 *
1479 * As the CBC ciphertext is not empty, both cases give the
1480 * lower bound minlen + maclen + 1 on the record size, which
1481 * we test for in the second check below.
1482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (rec->data_len < minlen + transform->ivlen ||
1484 rec->data_len < minlen + transform->maclen + 1) {
1485 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1486 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1487 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
1488 "+ 1 ) ( + expl IV )",
1489 rec->data_len,
1490 transform->ivlen,
1491 transform->maclen));
1492 return MBEDTLS_ERR_SSL_INVALID_MAC;
Paul Bakker45829992013-01-03 14:52:21 +01001493 }
1494
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001495 /*
1496 * Authenticate before decrypt if enabled
1497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001500#if defined(MBEDTLS_USE_PSA_CRYPTO)
1501 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1502#else
Hanno Becker992b6872017-11-09 18:57:39 +00001503 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001504#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001507
Hanno Beckerd96a6522019-07-10 13:55:25 +01001508 /* Update data_len in tandem with add_data.
1509 *
1510 * The subtraction is safe because of the previous check
1511 * data_len >= minlen + maclen + 1.
1512 *
1513 * Afterwards, we know that data + data_len is followed by at
1514 * least maclen Bytes, which justifies the call to
Gabor Mezei90437e32021-10-20 11:59:27 +02001515 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001516 *
1517 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001518 rec->data_len -= transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1520 transform->tls_version,
1521 transform->taglen);
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001522
Hanno Beckerd96a6522019-07-10 13:55:25 +01001523 /* Calculate expected MAC. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1525 add_data_len);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001526#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 status = psa_mac_verify_setup(&operation, transform->psa_mac_dec,
1528 transform->psa_mac_alg);
1529 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001530 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 status = psa_mac_update(&operation, add_data, add_data_len);
1534 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001535 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 status = psa_mac_update(&operation, data, rec->data_len);
1539 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001540 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001542
1543 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 status = psa_mac_verify_finish(&operation, data + rec->data_len,
1545 transform->maclen);
1546 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001547 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001549#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
1551 add_data_len);
1552 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001553 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 }
1555 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
1556 data, rec->data_len);
1557 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001558 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 }
1560 ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
1561 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001562 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 }
1564 ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
1565 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001566 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 }
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
1570 transform->maclen);
1571 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
1572 transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001573
Hanno Beckerd96a6522019-07-10 13:55:25 +01001574 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
1576 transform->maclen) != 0) {
1577 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001578 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1579 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001580 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001581#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001582 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584hmac_failed_etm_enabled:
Neil Armstrong26e6d672022-02-23 09:30:33 +01001585#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001586 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 status = psa_mac_abort(&operation);
1588 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001589 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001591#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 mbedtls_platform_zeroize(mac_expect, transform->maclen);
Neil Armstrong4313f552022-03-02 15:14:07 +01001593#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (ret != 0) {
1595 if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
1596 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
1597 }
1598 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001599 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001602
1603 /*
1604 * Check length sanity
1605 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001606
1607 /* We know from above that data_len > minlen >= 0,
1608 * so the following check in particular implies that
1609 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (rec->data_len % transform->ivlen != 0) {
1611 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1612 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
1613 rec->data_len, transform->ivlen));
1614 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001615 }
1616
TRodziewicz0f82ec62021-05-12 17:49:18 +02001617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001619 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001620 */
TRodziewicz345165c2021-07-06 13:42:11 +02001621 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 memcpy(transform->iv_dec, data, transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001623
TRodziewicz345165c2021-07-06 13:42:11 +02001624 data += transform->ivlen;
1625 rec->data_offset += transform->ivlen;
1626 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001627#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001628
Hanno Beckerd96a6522019-07-10 13:55:25 +01001629 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1630
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001631#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 status = psa_cipher_decrypt_setup(&cipher_op,
1633 transform->psa_key_dec, transform->psa_alg);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001636 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_decrypt_setup", ret);
1638 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001639 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 status = psa_cipher_set_iv(&cipher_op, transform->iv_dec, transform->ivlen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001644 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
1646 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001647 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 status = psa_cipher_update(&cipher_op,
1650 data, rec->data_len,
1651 data, rec->data_len, &olen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001654 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
1656 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001657 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 status = psa_cipher_finish(&cipher_op,
1660 data + olen, rec->data_len - olen,
1661 &part_len);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001664 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
1666 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001667 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001668
1669 olen += part_len;
1670#else
1671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1673 transform->iv_dec, transform->ivlen,
1674 data, rec->data_len, data, &olen)) != 0) {
1675 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1676 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001677 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001678#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001679
Hanno Beckerd96a6522019-07-10 13:55:25 +01001680 /* Double-check that length hasn't changed during decryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if (rec->data_len != olen) {
1682 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1683 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001684 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001685
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001686 /* Safe since data_len >= minlen + maclen + 1, so after having
1687 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001688 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1689 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001690 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 if (auth_done == 1) {
Gabor Mezei90437e32021-10-20 11:59:27 +02001693 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 rec->data_len,
1695 padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001696 correct &= mask;
1697 padlen &= mask;
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 if (rec->data_len < transform->maclen + padlen + 1) {
1701 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1702 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1703 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
1704 rec->data_len,
1705 transform->maclen,
1706 padlen + 1));
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001707 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001708#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001709
Gabor Mezei90437e32021-10-20 11:59:27 +02001710 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 rec->data_len,
1712 transform->maclen + padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001713 correct &= mask;
1714 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001717 padlen++;
1718
1719 /* Regardless of the validity of the padding,
1720 * we have data_len >= padlen here. */
1721
TRodziewicz0f82ec62021-05-12 17:49:18 +02001722#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001723 /* The padding check involves a series of up to 256
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 * consecutive memory reads at the end of the record
1725 * plaintext buffer. In order to hide the length and
1726 * validity of the padding, always perform exactly
1727 * `min(256,plaintext_len)` reads (but take into account
1728 * only the last `padlen` bytes for the padding check). */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001729 size_t pad_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 volatile unsigned char * const check = data;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001731
1732 /* Index of first padding byte; it has been ensured above
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 * that the subtraction is safe. */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001734 size_t const padding_idx = rec->data_len - padlen;
1735 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1736 size_t const start_idx = rec->data_len - num_checks;
1737 size_t idx;
1738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 for (idx = start_idx; idx < rec->data_len; idx++) {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001740 /* pad_count += (idx >= padding_idx) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 * (check[idx] == padlen - 1);
1742 */
1743 const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx);
1744 const size_t equal = mbedtls_ct_size_bool_eq(check[idx],
1745 padlen - 1);
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001746 pad_count += mask & equal;
1747 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 correct &= mbedtls_ct_size_bool_eq(pad_count, padlen);
Paul Bakkere47b34b2013-02-27 14:48:00 +01001749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (padlen > 0 && correct == 0) {
1752 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
1753 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001754#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 padlen &= mbedtls_ct_size_mask(correct);
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001756
TRodziewicz0f82ec62021-05-12 17:49:18 +02001757#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001758
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001759 /* If the padding was found to be invalid, padlen == 0
1760 * and the subtraction is safe. If the padding was found valid,
1761 * padlen hasn't been changed and the previous assertion
1762 * data_len >= padlen still holds. */
1763 rec->data_len -= padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001765#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001766 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1768 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001771#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
1773 data, rec->data_len);
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001774#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
1776 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 * Authenticate if not done yet.
1778 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001780#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (auth_done == 0) {
Paul Elliott5260ce22022-05-09 18:15:54 +01001782 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
1783 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01001784
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001785 /* If the initial value of padlen was such that
1786 * data_len < maclen + padlen + 1, then padlen
1787 * got reset to 1, and the initial check
1788 * data_len >= minlen + maclen + 1
1789 * guarantees that at this point we still
1790 * have at least data_len >= maclen.
1791 *
1792 * If the initial value of padlen was such that
1793 * data_len >= maclen + padlen + 1, then we have
1794 * subtracted either padlen + 1 (if the padding was correct)
1795 * or 0 (if the padding was incorrect) since then,
1796 * hence data_len >= maclen in any case.
1797 */
1798 rec->data_len -= transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1800 transform->tls_version,
1801 transform->taglen);
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
TRodziewicz0f82ec62021-05-12 17:49:18 +02001803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001804 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 * The next two sizes are the minimum and maximum values of
1806 * data_len over all padlen values.
1807 *
1808 * They're independent of padlen, since we previously did
1809 * data_len -= padlen.
1810 *
1811 * Note that max_len + maclen is never more than the buffer
1812 * length, as we previously did in_msglen -= maclen too.
1813 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001814 const size_t max_len = rec->data_len + padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001816
Neil Armstronge8589962022-02-25 15:14:29 +01001817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 ret = mbedtls_ct_hmac(transform->psa_mac_dec,
1819 transform->psa_mac_alg,
1820 add_data, add_data_len,
1821 data, rec->data_len, min_len, max_len,
1822 mac_expect);
Neil Armstronge8589962022-02-25 15:14:29 +01001823#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
1825 add_data, add_data_len,
1826 data, rec->data_len, min_len, max_len,
1827 mac_expect);
Neil Armstronge8589962022-02-25 15:14:29 +01001828#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (ret != 0) {
1830 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001831 goto hmac_failed_etm_disabled;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001832 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 mbedtls_ct_memcpy_offset(mac_peer, data,
1835 rec->data_len,
1836 min_len, max_len,
1837 transform->maclen);
TRodziewicz0f82ec62021-05-12 17:49:18 +02001838#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001840#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen);
1842 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen);
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001843#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 if (mbedtls_ct_memcmp(mac_peer, mac_expect,
1846 transform->maclen) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Paul Bakkere47b34b2013-02-27 14:48:00 +01001849#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001850 correct = 0;
1851 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001852 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854hmac_failed_etm_disabled:
1855 mbedtls_platform_zeroize(mac_peer, transform->maclen);
1856 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1857 if (ret != 0) {
1858 return ret;
1859 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001860 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001861
1862 /*
1863 * Finally check the correct flag
1864 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (correct == 0) {
1866 return MBEDTLS_ERR_SSL_INVALID_MAC;
1867 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001868#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001869
1870 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (auth_done != 1) {
1872 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1873 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001874 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Ronald Cron6f135e12021-12-08 16:57:54 +01001876#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Beckerccc13d02020-05-04 12:30:04 +01001878 /* Remove inner padding and infer true content type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1880 &rec->type);
Hanno Beckerccc13d02020-05-04 12:30:04 +01001881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 if (ret != 0) {
1883 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1884 }
Hanno Beckerccc13d02020-05-04 12:30:04 +01001885 }
Ronald Cron6f135e12021-12-08 16:57:54 +01001886#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +01001887
Hanno Beckera0e20d02019-05-15 14:03:01 +01001888#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (rec->cid_len != 0) {
1890 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1891 &rec->type);
1892 if (ret != 0) {
1893 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1894 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001895 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001896#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901}
1902
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001903#undef MAC_NONE
1904#undef MAC_PLAINTEXT
1905#undef MAC_CIPHERTEXT
1906
Paul Bakker5121ce52009-01-03 21:22:43 +00001907/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001908 * Fill the input message buffer by appending data to it.
1909 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001910 *
1911 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1912 * available (from this read and/or a previous one). Otherwise, an error code
1913 * is returned (possibly EOF or WANT_READ).
1914 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001915 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1916 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1917 * since we always read a whole datagram at once.
1918 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001919 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001920 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
Paul Bakker5121ce52009-01-03 21:22:43 +00001923{
Janos Follath865b3eb2019-12-16 11:46:15 +00001924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001925 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001926#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1927 size_t in_buf_len = ssl->in_buf_len;
1928#else
1929 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1930#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) {
1935 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
1936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001937 }
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
1940 MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits"));
1941 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001942 }
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001946 uint32_t timeout;
1947
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001948 /*
1949 * The point is, we need to always read a full datagram at once, so we
1950 * sometimes read more then requested, and handle the additional data.
1951 * It could be the rest of the current record (while fetching the
1952 * header) and/or some other records in the same datagram.
1953 */
1954
1955 /*
1956 * Move to the next record in the already read datagram if applicable
1957 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if (ssl->next_record_offset != 0) {
1959 if (ssl->in_left < ssl->next_record_offset) {
1960 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1961 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001962 }
1963
1964 ssl->in_left -= ssl->next_record_offset;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 if (ssl->in_left != 0) {
1967 MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %"
1968 MBEDTLS_PRINTF_SIZET,
1969 ssl->next_record_offset));
1970 memmove(ssl->in_hdr,
1971 ssl->in_hdr + ssl->next_record_offset,
1972 ssl->in_left);
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001973 }
1974
1975 ssl->next_record_offset = 0;
1976 }
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1979 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1980 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001981
1982 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001983 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001984 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (nb_want <= ssl->in_left) {
1986 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
1987 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001988 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001989
1990 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001991 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001992 * are not at the beginning of a new record, the caller did something
1993 * wrong.
1994 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 if (ssl->in_left != 0) {
1996 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1997 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001998 }
1999
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002000 /*
2001 * Don't even try to read if time's out already.
2002 * This avoids by-passing the timer when repeatedly receiving messages
2003 * that will end up being dropped.
2004 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 if (mbedtls_ssl_check_timer(ssl) != 0) {
2006 MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002007 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 } else {
2009 len = in_buf_len - (ssl->in_hdr - ssl->in_buf);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002012 timeout = ssl->handshake->retransmit_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 } else {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002014 timeout = ssl->conf->read_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 if (ssl->f_recv_timeout != NULL) {
2020 ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len,
2021 timeout);
2022 } else {
2023 ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len);
2024 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (ret == 0) {
2029 return MBEDTLS_ERR_SSL_CONN_EOF;
2030 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002031 }
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
2034 MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
2035 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
2038 if (ssl_double_retransmit_timeout(ssl) != 0) {
2039 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout"));
2040 return MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002041 }
2042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
2044 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
2045 return ret;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002046 }
2047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002050#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
2052 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
2053 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
2054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
2055 ret);
2056 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002057 }
2058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002060 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002062 }
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (ret < 0) {
2065 return ret;
2066 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002068 ssl->in_left = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 } else
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002070#endif
2071 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
2073 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
2074 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 while (ssl->in_left < nb_want) {
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002077 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (mbedtls_ssl_check_timer(ssl) != 0) {
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002080 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 } else {
2082 if (ssl->f_recv_timeout != NULL) {
2083 ret = ssl->f_recv_timeout(ssl->p_bio,
2084 ssl->in_hdr + ssl->in_left, len,
2085 ssl->conf->read_timeout);
2086 } else {
2087 ret = ssl->f_recv(ssl->p_bio,
2088 ssl->in_hdr + ssl->in_left, len);
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002089 }
2090 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
2093 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
2094 ssl->in_left, nb_want));
2095 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if (ret == 0) {
2098 return MBEDTLS_ERR_SSL_CONN_EOF;
2099 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 if (ret < 0) {
2102 return ret;
2103 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002104
Dave Rodgman4a5c9ee2023-02-10 16:03:44 +00002105 if ((size_t) ret > len) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_SSL_DEBUG_MSG(1,
2107 ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2108 " were requested",
2109 ret, len));
2110 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16035bd15cb2018-02-28 04:30:59 -08002111 }
2112
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002113 ssl->in_left += ret;
2114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 }
2116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002120}
2121
2122/*
2123 * Flush any data not yet written
2124 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002125int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002126{
Janos Follath865b3eb2019-12-16 11:46:15 +00002127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002128 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (ssl->f_send == NULL) {
2133 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
2134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002135 }
2136
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002137 /* Avoid incrementing counter if data is flushed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (ssl->out_left == 0) {
2139 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
2140 return 0;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002141 }
2142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 while (ssl->out_left > 0) {
2144 MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET
2145 ", out_left: %" MBEDTLS_PRINTF_SIZET,
2146 mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left));
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Hanno Becker2b1e3542018-08-06 11:19:13 +01002148 buf = ssl->out_hdr - ssl->out_left;
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
Paul Bakker186751d2012-05-08 13:16:14 +00002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 if (ret <= 0) {
2154 return ret;
2155 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Dave Rodgman4a5c9ee2023-02-10 16:03:44 +00002157 if ((size_t) ret > ssl->out_left) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 MBEDTLS_SSL_DEBUG_MSG(1,
2159 ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2160 " bytes were sent",
2161 ret, ssl->out_left));
2162 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16034bbaeb42018-02-22 04:29:04 -08002163 }
2164
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 ssl->out_left -= ret;
2166 }
2167
Hanno Becker2b1e3542018-08-06 11:19:13 +01002168#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002170 ssl->out_hdr = ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 } else
Hanno Becker2b1e3542018-08-06 11:19:13 +01002172#endif
2173 {
2174 ssl->out_hdr = ssl->out_buf + 8;
2175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002181}
2182
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002183/*
2184 * Functions to handle the DTLS retransmission state machine
2185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002187/*
2188 * Append current handshake message to current outgoing flight
2189 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002190MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002191static int ssl_flight_append(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002192{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 mbedtls_ssl_flight_item *msg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append"));
2195 MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight",
2196 ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002197
2198 /* Allocate space for current message */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) {
2200 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2201 sizeof(mbedtls_ssl_flight_item)));
2202 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002203 }
2204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) {
2206 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2207 ssl->out_msglen));
2208 mbedtls_free(msg);
2209 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002210 }
2211
2212 /* Copy current handshake message with headers */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002214 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002215 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002216 msg->next = NULL;
2217
2218 /* Append to the current flight */
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 if (ssl->handshake->flight == NULL) {
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002220 ssl->handshake->flight = msg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 while (cur->next != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002224 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002226 cur->next = msg;
2227 }
2228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
2230 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002231}
2232
2233/*
2234 * Free the current flight of handshake messages
2235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002236void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002237{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002238 mbedtls_ssl_flight_item *cur = flight;
2239 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 while (cur != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002242 next = cur->next;
2243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 mbedtls_free(cur->p);
2245 mbedtls_free(cur);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002246
2247 cur = next;
2248 }
2249}
2250
2251/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002252 * Swap transform_out and out_ctr with the alternative ones
2253 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002254MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002255static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 mbedtls_ssl_transform *tmp_transform;
Jerry Yuae0b2e22021-10-08 15:21:19 +08002258 unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 if (ssl->transform_out == ssl->handshake->alt_transform_out) {
2261 MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
2262 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002263 }
2264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002266
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002267 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002268 tmp_transform = ssl->transform_out;
2269 ssl->transform_out = ssl->handshake->alt_transform_out;
2270 ssl->handshake->alt_transform_out = tmp_transform;
2271
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002272 /* Swap epoch + sequence_number */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 memcpy(tmp_out_ctr, ssl->cur_out_ctr, sizeof(tmp_out_ctr));
2274 memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr,
2275 sizeof(ssl->cur_out_ctr));
2276 memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr,
2277 sizeof(ssl->handshake->alt_out_ctr));
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002278
2279 /* Adjust to the newly activated transform */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002281
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002283}
2284
2285/*
2286 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002289{
2290 int ret = 0;
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 ret = mbedtls_ssl_flight_transmit(ssl);
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002299}
2300
2301/*
2302 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002303 *
2304 * Need to remember the current message in case flush_output returns
2305 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002306 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002309{
Janos Follath865b3eb2019-12-16 11:46:15 +00002310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
2314 MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002315
2316 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002317 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 ret = ssl_swap_epochs(ssl);
2319 if (ret != 0) {
2320 return ret;
2321 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002324 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 while (ssl->handshake->cur_msg != NULL) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002327 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002328 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002329
Hanno Beckere1dcb032018-08-17 16:47:58 +01002330 int const is_finished =
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2332 cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
Hanno Beckere1dcb032018-08-17 16:47:58 +01002333
Ronald Cron00d012f22022-03-08 15:57:12 +01002334 int const force_flush = ssl->disable_datagram_packing == 1 ?
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
Hanno Becker04da1892018-08-14 13:22:10 +01002336
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002337 /* Swap epochs before sending Finished: we can't do it after
2338 * sending ChangeCipherSpec, in case write returns WANT_READ.
2339 * Must be done before copying, may change out_msg pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) {
2341 MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message"));
2342 ret = ssl_swap_epochs(ssl);
2343 if (ret != 0) {
2344 return ret;
2345 }
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002346 }
2347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 ret = ssl_get_remaining_payload_in_datagram(ssl);
2349 if (ret < 0) {
2350 return ret;
2351 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002352 max_frag_len = (size_t) ret;
2353
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002354 /* CCS is copied as is, while HS messages may need fragmentation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2356 if (max_frag_len == 0) {
2357 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2358 return ret;
2359 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002360
2361 continue;
2362 }
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 memcpy(ssl->out_msg, cur->p, cur->len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002365 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002366 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002367
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002368 /* Update position inside current message */
2369 ssl->handshake->cur_msg_p += cur->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002371 const unsigned char * const p = ssl->handshake->cur_msg_p;
2372 const size_t hs_len = cur->len - 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 const size_t frag_off = p - (cur->p + 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002374 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002375 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) {
2378 if (is_finished) {
2379 ret = ssl_swap_epochs(ssl);
2380 if (ret != 0) {
2381 return ret;
2382 }
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002383 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2386 return ret;
2387 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002388
2389 continue;
2390 }
2391 max_hs_frag_len = max_frag_len - 12;
2392
2393 cur_hs_frag_len = rem_len > max_hs_frag_len ?
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 max_hs_frag_len : rem_len;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 if (frag_off == 0 && cur_hs_frag_len != hs_len) {
2397 MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)",
2398 (unsigned) cur_hs_frag_len,
2399 (unsigned) max_hs_frag_len));
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002400 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002401
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002402 /* Messages are stored with handshake headers as if not fragmented,
2403 * copy beginning of headers then fill fragmentation fields.
2404 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 memcpy(ssl->out_msg, cur->p, 6);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off);
2408 ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off);
2409 ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len);
2412 ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len);
2413 ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002416
Hanno Becker3f7b9732018-08-28 09:53:25 +01002417 /* Copy the handshake message content and set records fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002419 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002420 ssl->out_msgtype = cur->type;
2421
2422 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002423 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002424 }
2425
2426 /* If done with the current message move to the next one if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
2428 if (cur->next != NULL) {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002429 ssl->handshake->cur_msg = cur->next;
2430 ssl->handshake->cur_msg_p = cur->next->p + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002432 ssl->handshake->cur_msg = NULL;
2433 ssl->handshake->cur_msg_p = NULL;
2434 }
2435 }
2436
2437 /* Actually send the message out */
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2439 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
2440 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002441 }
2442 }
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2445 return ret;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002446 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 /* Update state and set timer */
2449 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
2450 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
2451 } else {
2452 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
2453 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
2454 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
2457
2458 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002459}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002460
2461/*
2462 * To be called when the last message of an incoming flight is received.
2463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002464void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002465{
2466 /* We won't need to resend that one any more */
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 mbedtls_ssl_flight_free(ssl->handshake->flight);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002468 ssl->handshake->flight = NULL;
2469 ssl->handshake->cur_msg = NULL;
2470
2471 /* The next incoming flight will start with this msg_seq */
2472 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2473
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002474 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002475 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002476
Hanno Becker0271f962018-08-16 13:23:47 +01002477 /* Clear future message buffering structure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 mbedtls_ssl_buffering_free(ssl);
Hanno Becker0271f962018-08-16 13:23:47 +01002479
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002480 /* Cancel timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2484 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002485 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002489}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002490
2491/*
2492 * To be called when the last message of an outgoing flight is send.
2493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002494void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002495{
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 ssl_reset_retransmit_timeout(ssl);
2497 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2500 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01002504 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002506#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002509 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002511int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned hs_type,
2512 unsigned char **buf, size_t *buf_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002513{
2514 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002515 * Reserve 4 bytes for handshake header. ( Section 4,RFC 8446 )
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002516 * ...
2517 * HandshakeType msg_type;
2518 * uint24 length;
2519 * ...
2520 */
2521 *buf = ssl->out_msg + 4;
2522 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
2523
2524 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2525 ssl->out_msg[0] = hs_type;
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 return 0;
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002528}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002529
2530/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002531 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002532 *
2533 * - fill in handshake headers
2534 * - update handshake checksum
2535 * - DTLS: save message for resending
2536 * - then pass to the record layer
2537 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002538 * DTLS: except for HelloRequest, messages are only queued, and will only be
2539 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002540 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002541 * Inputs:
2542 * - ssl->out_msglen: 4 + actual handshake message len
2543 * (4 is the size of handshake headers for TLS)
2544 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2545 * - ssl->out_msg + 4: the handshake message body
2546 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002547 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002548 * - ssl->out_msglen: the length of the record contents
2549 * (including handshake headers but excluding record headers)
2550 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
2553 int update_checksum,
2554 int force_flush)
Paul Bakker5121ce52009-01-03 21:22:43 +00002555{
Janos Follath865b3eb2019-12-16 11:46:15 +00002556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002557 const size_t hs_len = ssl->out_msglen - 4;
2558 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002561
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002562 /*
2563 * Sanity checks
2564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
2566 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2567 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2568 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002569 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002571 /* Whenever we send anything different from a
2572 * HelloRequest we should be in a handshake - double check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2574 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) &&
2575 ssl->handshake == NULL) {
2576 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2577 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002578 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002580#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002582 ssl->handshake != NULL &&
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
2584 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2585 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002586 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002587#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002588
Hanno Beckerb50a2532018-08-06 11:52:54 +01002589 /* Double-check that we did not exceed the bounds
2590 * of the outgoing record buffer.
2591 * This should never fail as the various message
2592 * writing functions must obey the bounds of the
2593 * outgoing record buffer, but better be safe.
2594 *
2595 * Note: We deliberately do not check for the MTU or MFL here.
2596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2598 MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: "
2599 "size %" MBEDTLS_PRINTF_SIZET
2600 ", maximum %" MBEDTLS_PRINTF_SIZET,
2601 ssl->out_msglen,
2602 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2603 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerb50a2532018-08-06 11:52:54 +01002604 }
2605
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002606 /*
2607 * Fill handshake headers
2608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
2610 ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len);
2611 ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len);
2612 ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00002613
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002614 /*
2615 * DTLS has additional fields in the Handshake layer,
2616 * between the length field and the actual payload:
2617 * uint16 message_seq;
2618 * uint24 fragment_offset;
2619 * uint24 fragment_length;
2620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002623 /* Make room for the additional DTLS fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) {
2625 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: "
2626 "size %" MBEDTLS_PRINTF_SIZET ", maximum %"
2627 MBEDTLS_PRINTF_SIZET,
2628 hs_len,
2629 (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12)));
2630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9648f8b2017-09-18 10:55:54 +01002631 }
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002634 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002635
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002636 /* Write message_seq and update it, except for HelloRequest */
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2638 MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4);
2639 ++(ssl->handshake->out_msg_seq);
2640 } else {
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002641 ssl->out_msg[4] = 0;
2642 ssl->out_msg[5] = 0;
2643 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002644
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002645 /* Handshake hashes are computed without fragmentation,
2646 * so set frag_offset = 0 and frag_len = hs_len for now */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 memset(ssl->out_msg + 6, 0x00, 3);
2648 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002649 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002651
Hanno Becker0207e532018-08-28 10:28:28 +01002652 /* Update running hashes of handshake messages seen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002654 ret = ssl->handshake->update_checksum(ssl, ssl->out_msg,
2655 ssl->out_msglen);
2656 if (ret != 0) {
2657 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
2658 return ret;
2659 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 }
2662
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002663 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2666 !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2667 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) {
2668 if ((ret = ssl_flight_append(ssl)) != 0) {
2669 MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret);
2670 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002671 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 } else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002673#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002674 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2676 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret);
2677 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002678 }
2679 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return 0;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002684}
2685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl,
2687 size_t buf_len, size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002688{
2689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2690 size_t msg_with_header_len;
2691 ((void) buf_len);
2692
2693 /* Add reserved 4 bytes for handshake header */
2694 msg_with_header_len = msg_len + 4;
2695 ssl->out_msglen = msg_with_header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_handshake_msg_ext(ssl, 0, 0));
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002697
2698cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return ret;
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002700}
2701
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002702/*
2703 * Record layer functions
2704 */
2705
2706/*
2707 * Write current record.
2708 *
2709 * Uses:
2710 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2711 * - ssl->out_msglen: length of the record content (excl headers)
2712 * - ssl->out_msg: record content
2713 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002714int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush)
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002715{
2716 int ret, done = 0;
2717 size_t len = ssl->out_msglen;
Ronald Cron00d012f22022-03-08 15:57:12 +01002718 int flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (!done) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002723 unsigned i;
2724 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002725#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2726 size_t out_buf_len = ssl->out_buf_len;
2727#else
2728 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2729#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002730 /* Skip writing the record content type to after the encryption,
2731 * as it may change when using the CID extension. */
Glenn Strauss60bfe602022-03-14 19:04:24 -04002732 mbedtls_ssl_protocol_version tls_ver = ssl->tls_version;
Ronald Cron6f135e12021-12-08 16:57:54 +01002733#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ca80f72021-11-08 10:30:54 +08002734 /* TLS 1.3 still uses the TLS 1.2 version identifier
2735 * for backwards compatibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 if (tls_ver == MBEDTLS_SSL_VERSION_TLS1_3) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002737 tls_ver = MBEDTLS_SSL_VERSION_TLS1_2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 }
Ronald Cron6f135e12021-12-08 16:57:54 +01002739#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 mbedtls_ssl_write_version(ssl->out_hdr + 1, ssl->conf->transport,
2741 tls_ver);
Hanno Becker6430faf2019-05-08 11:57:13 +01002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 memcpy(ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
2744 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 if (ssl->transform_out != NULL) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002747 mbedtls_record rec;
2748
2749 rec.buf = ssl->out_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002751 rec.data_len = ssl->out_msglen;
2752 rec.data_offset = ssl->out_msg - rec.buf;
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 memcpy(&rec.ctr[0], ssl->out_ctr, sizeof(rec.ctr));
2755 mbedtls_ssl_write_version(rec.ver, ssl->conf->transport, tls_ver);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002756 rec.type = ssl->out_msgtype;
2757
Hanno Beckera0e20d02019-05-15 14:03:01 +01002758#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002759 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002760 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002761#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec,
2764 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2765 MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret);
2766 return ret;
Paul Bakker05ef8352012-05-08 09:17:57 +00002767 }
2768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 if (rec.data_offset != 0) {
2770 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2771 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002772 }
2773
Hanno Becker6430faf2019-05-08 11:57:13 +01002774 /* Update the record content type and CID. */
2775 ssl->out_msgtype = rec.type;
Gilles Peskine449bd832023-01-11 14:50:10 +01002776#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2777 memcpy(ssl->out_cid, rec.cid, rec.cid_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01002778#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002779 ssl->out_msglen = len = rec.data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002781 }
2782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002784
2785#if defined(MBEDTLS_SSL_PROTO_DTLS)
2786 /* In case of DTLS, double-check that we don't exceed
2787 * the remaining space in the datagram. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2789 ret = ssl_get_remaining_space_in_datagram(ssl);
2790 if (ret < 0) {
2791 return ret;
2792 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (protected_record_size > (size_t) ret) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002795 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker2b1e3542018-08-06 11:19:13 +01002797 }
2798 }
2799#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002800
Hanno Becker6430faf2019-05-08 11:57:13 +01002801 /* Now write the potentially updated record content type. */
2802 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, "
2805 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
2806 ssl->out_hdr[0], ssl->out_hdr[1],
2807 ssl->out_hdr[2], len));
Paul Bakker05ef8352012-05-08 09:17:57 +00002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
2810 ssl->out_hdr, protected_record_size);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002811
2812 ssl->out_left += protected_record_size;
2813 ssl->out_hdr += protected_record_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
2817 if (++ssl->cur_out_ctr[i - 1] != 0) {
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002818 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 }
2820 }
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002821
Gabor Mezei96ae9262022-06-28 11:45:18 +02002822 /* The loop goes to its end if the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 if (i == mbedtls_ssl_ep_len(ssl)) {
2824 MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap"));
2825 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker04484622018-08-06 09:49:38 +01002826 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 }
2828
Hanno Becker67bc7c32018-08-06 11:33:50 +01002829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2831 flush == SSL_DONT_FORCE_FLUSH) {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002832 size_t remaining;
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 ret = ssl_get_remaining_payload_in_datagram(ssl);
2834 if (ret < 0) {
2835 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram",
2836 ret);
2837 return ret;
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002838 }
2839
2840 remaining = (size_t) ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (remaining == 0) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002842 flush = SSL_FORCE_FLUSH;
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 } else {
2844 MBEDTLS_SSL_DEBUG_MSG(2,
2845 ("Still %u bytes available in current datagram",
2846 (unsigned) remaining));
Hanno Becker67bc7c32018-08-06 11:33:50 +01002847 }
2848 }
2849#endif /* MBEDTLS_SSL_PROTO_DTLS */
2850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if ((flush == SSL_FORCE_FLUSH) &&
2852 (ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
2854 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 }
2856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002860}
2861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002863
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002865static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002866{
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 if (ssl->in_msglen < ssl->in_hslen ||
2868 memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 ||
2869 memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) {
2870 return 1;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002871 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 return 0;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002873}
Hanno Becker44650b72018-08-16 12:51:11 +01002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002876{
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return (ssl->in_msg[9] << 16) |
2878 (ssl->in_msg[10] << 8) |
2879 ssl->in_msg[11];
Hanno Becker44650b72018-08-16 12:51:11 +01002880}
2881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002883{
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 return (ssl->in_msg[6] << 16) |
2885 (ssl->in_msg[7] << 8) |
2886 ssl->in_msg[8];
Hanno Becker44650b72018-08-16 12:51:11 +01002887}
2888
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002889MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002890static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002891{
2892 uint32_t msg_len, frag_off, frag_len;
2893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 msg_len = ssl_get_hs_total_len(ssl);
2895 frag_off = ssl_get_hs_frag_off(ssl);
2896 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker44650b72018-08-16 12:51:11 +01002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (frag_off > msg_len) {
2899 return -1;
2900 }
Hanno Becker44650b72018-08-16 12:51:11 +01002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (frag_len > msg_len - frag_off) {
2903 return -1;
2904 }
Hanno Becker44650b72018-08-16 12:51:11 +01002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 if (frag_len + 12 > ssl->in_msglen) {
2907 return -1;
2908 }
Hanno Becker44650b72018-08-16 12:51:11 +01002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 return 0;
Hanno Becker44650b72018-08-16 12:51:11 +01002911}
2912
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002913/*
2914 * Mark bits in bitmask (used for DTLS HS reassembly)
2915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002916static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002917{
2918 unsigned int start_bits, end_bits;
2919
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 start_bits = 8 - (offset % 8);
2921 if (start_bits != 8) {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002922 size_t first_byte_idx = offset / 8;
2923
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002924 /* Special case */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (len <= start_bits) {
2926 for (; len != 0; len--) {
2927 mask[first_byte_idx] |= 1 << (start_bits - len);
2928 }
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002929
2930 /* Avoid potential issues with offset or len becoming invalid */
2931 return;
2932 }
2933
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002934 offset += start_bits; /* Now offset % 8 == 0 */
2935 len -= start_bits;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 for (; start_bits != 0; start_bits--) {
2938 mask[first_byte_idx] |= 1 << (start_bits - 1);
2939 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002940 }
2941
2942 end_bits = len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (end_bits != 0) {
2944 size_t last_byte_idx = (offset + len) / 8;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002945
2946 len -= end_bits; /* Now len % 8 == 0 */
2947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 for (; end_bits != 0; end_bits--) {
2949 mask[last_byte_idx] |= 1 << (8 - end_bits);
2950 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002951 }
2952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 memset(mask + offset / 8, 0xFF, len / 8);
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002954}
2955
2956/*
2957 * Check that bitmask is full
2958 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002959MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002960static int ssl_bitmask_check(unsigned char *mask, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002961{
2962 size_t i;
2963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 for (i = 0; i < len / 8; i++) {
2965 if (mask[i] != 0xFF) {
2966 return -1;
2967 }
2968 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 for (i = 0; i < len % 8; i++) {
2971 if ((mask[len / 8] & (1 << (7 - i))) == 0) {
2972 return -1;
2973 }
2974 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002977}
2978
Hanno Becker56e205e2018-08-16 09:06:12 +01002979/* msg_len does not include the handshake header */
Gilles Peskine449bd832023-01-11 14:50:10 +01002980static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
2981 unsigned add_bitmap)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002982{
Hanno Becker56e205e2018-08-16 09:06:12 +01002983 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002984
Hanno Becker56e205e2018-08-16 09:06:12 +01002985 alloc_len = 12; /* Handshake header */
2986 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 if (add_bitmap) {
2989 alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 }
2992 return alloc_len;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002993}
Hanno Becker56e205e2018-08-16 09:06:12 +01002994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
Hanno Becker12555c62018-08-16 12:47:53 +01002998{
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 return (ssl->in_msg[1] << 16) |
3000 (ssl->in_msg[2] << 8) |
3001 ssl->in_msg[3];
Hanno Becker12555c62018-08-16 12:47:53 +01003002}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003005{
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
3007 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
3008 ssl->in_msglen));
3009 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003010 }
3011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
3015 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
3016 MBEDTLS_PRINTF_SIZET,
3017 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (ssl_check_hs_header(ssl) != 0) {
3025 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
3026 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker44650b72018-08-16 12:51:11 +01003027 }
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (ssl->handshake != NULL &&
3030 ((mbedtls_ssl_is_handshake_over(ssl) == 0 &&
3031 recv_msg_seq != ssl->handshake->in_msg_seq) ||
3032 (mbedtls_ssl_is_handshake_over(ssl) == 1 &&
3033 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
3034 if (recv_msg_seq > ssl->handshake->in_msg_seq) {
3035 MBEDTLS_SSL_DEBUG_MSG(2,
3036 (
3037 "received future handshake message of sequence number %u (next %u)",
3038 recv_msg_seq,
3039 ssl->handshake->in_msg_seq));
3040 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Becker9e1ec222018-08-15 15:54:43 +01003041 }
3042
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003043 /* Retransmit only on last message from previous flight, to avoid
3044 * too many retransmissions.
3045 * Besides, No sane server ever retransmits HelloVerifyRequest */
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
3047 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
3048 MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
3049 "message_seq = %u, start_of_flight = %u",
3050 recv_msg_seq,
3051 ssl->handshake->in_flight_start_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
3054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
3055 return ret;
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003056 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 } else {
3058 MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
3059 "message_seq = %u, expected = %u",
3060 recv_msg_seq,
3061 ssl->handshake->in_msg_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003062 }
3063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003065 }
3066 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003067
Hanno Becker6d97ef52018-08-16 13:09:04 +01003068 /* Message reassembly is handled alongside buffering of future
3069 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003070 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003071 * handshake logic layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (ssl_hs_is_proper_fragment(ssl) == 1) {
3073 MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
3074 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003077#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003078 /* With TLS we don't handle fragmentation (for now) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 if (ssl->in_msglen < ssl->in_hslen) {
3080 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
3081 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003082 }
3083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003085}
3086
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003087int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003088{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0271f962018-08-16 13:23:47 +01003090 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003093 ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
3094 if (ret != 0) {
3095 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
3096 return ret;
3097 }
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003098 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003099
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003100 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3103 ssl->handshake != NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01003104 unsigned offset;
3105 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003106
Hanno Becker0271f962018-08-16 13:23:47 +01003107 /* Increment handshake sequence number */
3108 hs->in_msg_seq++;
3109
3110 /*
3111 * Clear up handshake buffering and reassembly structure.
3112 */
3113
3114 /* Free first entry */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 ssl_buffering_free_slot(ssl, 0);
Hanno Becker0271f962018-08-16 13:23:47 +01003116
3117 /* Shift all other entries */
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 for (offset = 0, hs_buf = &hs->buffering.hs[0];
Hanno Beckere605b192018-08-21 15:59:07 +01003119 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 offset++, hs_buf++) {
Hanno Becker0271f962018-08-16 13:23:47 +01003121 *hs_buf = *(hs_buf + 1);
3122 }
3123
3124 /* Create a fresh last entry */
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003126 }
3127#endif
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003128 return 0;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003129}
3130
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003131/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003132 * DTLS anti-replay: RFC 6347 4.1.2.6
3133 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003134 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3135 * Bit n is set iff record number in_window_top - n has been seen.
3136 *
3137 * Usually, in_window_top is the last record number seen and the lsb of
3138 * in_window is set. The only exception is the initial state (record number 0
3139 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01003142void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003143{
3144 ssl->in_window_top = 0;
3145 ssl->in_window = 0;
3146}
3147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003149{
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 return ((uint64_t) buf[0] << 40) |
3151 ((uint64_t) buf[1] << 32) |
3152 ((uint64_t) buf[2] << 24) |
3153 ((uint64_t) buf[3] << 16) |
3154 ((uint64_t) buf[4] << 8) |
3155 ((uint64_t) buf[5]);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003156}
3157
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003159static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003160{
Janos Follath865b3eb2019-12-16 11:46:15 +00003161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003162 unsigned char *original_in_ctr;
3163
3164 // save original in_ctr
3165 original_in_ctr = ssl->in_ctr;
3166
3167 // use counter from record
3168 ssl->in_ctr = record_in_ctr;
3169
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003171
3172 // restore the counter
3173 ssl->in_ctr = original_in_ctr;
3174
3175 return ret;
3176}
3177
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003178/*
3179 * Return 0 if sequence number is acceptable, -1 otherwise
3180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003181int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003182{
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003184 uint64_t bit;
3185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
3187 return 0;
3188 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 if (rec_seqnum > ssl->in_window_top) {
3191 return 0;
3192 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003194 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003195
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 if (bit >= 64) {
3197 return -1;
3198 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
3201 return -1;
3202 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return 0;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003205}
3206
3207/*
3208 * Update replay window on new validated record
3209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003210void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003211{
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003215 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 if (rec_seqnum > ssl->in_window_top) {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003219 /* Update window_top and the contents of the window */
3220 uint64_t shift = rec_seqnum - ssl->in_window_top;
3221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 if (shift >= 64) {
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003223 ssl->in_window = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003225 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003226 ssl->in_window |= 1;
3227 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003228
3229 ssl->in_window_top = rec_seqnum;
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003231 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003232 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 if (bit < 64) { /* Always true, but be extra sure */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003235 ssl->in_window |= (uint64_t) 1 << bit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003237 }
3238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003240
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003241#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003242/*
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003243 * Check if a datagram looks like a ClientHello with a valid cookie,
3244 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003245 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003246 *
3247 * - if cookie is valid, return 0
3248 * - if ClientHello looks superficially valid but cookie is not,
3249 * fill obuf and set olen, then
3250 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3251 * - otherwise return a specific error code
3252 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003253MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003254MBEDTLS_STATIC_TESTABLE
3255int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 mbedtls_ssl_context *ssl,
3257 const unsigned char *cli_id, size_t cli_id_len,
3258 const unsigned char *in, size_t in_len,
3259 unsigned char *obuf, size_t buf_len, size_t *olen)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003260{
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003261 size_t sid_len, cookie_len, epoch, fragment_offset;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003262 unsigned char *p;
3263
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003264 /*
3265 * Structure of ClientHello with record and handshake headers,
3266 * and expected values. We don't need to check a lot, more checks will be
3267 * done when actually parsing the ClientHello - skipping those checks
3268 * avoids code duplication and does not make cookie forging any easier.
3269 *
3270 * 0-0 ContentType type; copied, must be handshake
3271 * 1-2 ProtocolVersion version; copied
3272 * 3-4 uint16 epoch; copied, must be 0
3273 * 5-10 uint48 sequence_number; copied
3274 * 11-12 uint16 length; (ignored)
3275 *
3276 * 13-13 HandshakeType msg_type; (ignored)
3277 * 14-16 uint24 length; (ignored)
3278 * 17-18 uint16 message_seq; copied
3279 * 19-21 uint24 fragment_offset; copied, must be 0
3280 * 22-24 uint24 fragment_length; (ignored)
3281 *
3282 * 25-26 ProtocolVersion client_version; (ignored)
3283 * 27-58 Random random; (ignored)
3284 * 59-xx SessionID session_id; 1 byte len + sid_len content
3285 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3286 * ...
3287 *
3288 * Minimum length is 61 bytes.
3289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
3291 (unsigned) in_len));
3292 MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
3293 if (in_len < 61) {
3294 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
3295 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003296 }
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003297
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 epoch = MBEDTLS_GET_UINT16_BE(in, 3);
3299 fragment_offset = MBEDTLS_GET_UINT24_BE(in, 19);
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || epoch != 0 ||
3302 fragment_offset != 0) {
3303 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
3304 MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
3305 in[0], (unsigned) epoch,
3306 (unsigned) fragment_offset));
3307 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003308 }
3309
3310 sid_len = in[59];
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 if (59 + 1 + sid_len + 1 > in_len) {
3312 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
3313 (unsigned) sid_len,
3314 (unsigned) in_len - 61));
3315 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003316 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
3318 in + 60, sid_len);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003319
3320 cookie_len = in[60 + sid_len];
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
3322 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
3323 (unsigned) cookie_len,
3324 (unsigned) (in_len - sid_len - 61)));
3325 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003326 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
3329 in + sid_len + 61, cookie_len);
3330 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
3331 in + sid_len + 61, cookie_len,
3332 cli_id, cli_id_len) == 0) {
3333 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
3334 return 0;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003335 }
3336
3337 /*
3338 * If we get here, we've got an invalid cookie, let's prepare HVR.
3339 *
3340 * 0-0 ContentType type; copied
3341 * 1-2 ProtocolVersion version; copied
3342 * 3-4 uint16 epoch; copied
3343 * 5-10 uint48 sequence_number; copied
3344 * 11-12 uint16 length; olen - 13
3345 *
3346 * 13-13 HandshakeType msg_type; hello_verify_request
3347 * 14-16 uint24 length; olen - 25
3348 * 17-18 uint16 message_seq; copied
3349 * 19-21 uint24 fragment_offset; copied
3350 * 22-24 uint24 fragment_length; olen - 25
3351 *
3352 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3353 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3354 *
3355 * Minimum length is 28.
3356 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 if (buf_len < 28) {
3358 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3359 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003360
3361 /* Copy most fields and adapt others */
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 memcpy(obuf, in, 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003363 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3364 obuf[25] = 0xfe;
3365 obuf[26] = 0xff;
3366
3367 /* Generate and write actual cookie */
3368 p = obuf + 28;
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
3370 &p, obuf + buf_len,
3371 cli_id, cli_id_len) != 0) {
3372 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003373 }
3374
3375 *olen = p - obuf;
3376
3377 /* Go back and fill length fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 obuf[27] = (unsigned char) (*olen - 28);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
3381 obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
3382 obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003387}
3388
3389/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003390 * Handle possible client reconnect with the same UDP quadruplet
3391 * (RFC 6347 Section 4.2.8).
3392 *
3393 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3394 * that looks like a ClientHello.
3395 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003396 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003397 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003398 * - if the input looks like a ClientHello with a valid cookie,
3399 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003400 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003401 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003402 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003403 * This function is called (through ssl_check_client_reconnect()) when an
3404 * unexpected record is found in ssl_get_next_record(), which will discard the
3405 * record if we return 0, and bubble up the return value otherwise (this
3406 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3407 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003408 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003409MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003410static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003411{
Janos Follath865b3eb2019-12-16 11:46:15 +00003412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003413 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003414
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 if (ssl->conf->f_cookie_write == NULL ||
3416 ssl->conf->f_cookie_check == NULL) {
Hanno Becker2fddd372019-07-10 14:37:41 +01003417 /* If we can't use cookies to verify reachability of the peer,
3418 * drop the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
3420 "can't check reconnect validity"));
3421 return 0;
Hanno Becker2fddd372019-07-10 14:37:41 +01003422 }
3423
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003424 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 ssl,
3426 ssl->cli_id, ssl->cli_id_len,
3427 ssl->in_buf, ssl->in_left,
3428 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003433 int send_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
3435 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3436 ssl->out_buf, len);
Brian J Murray1903fb32016-11-06 04:45:15 -08003437 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003438 * If the error is permanent we'll catch it later,
3439 * if it's not, then hopefully it'll work next time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
3441 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003442 (void) send_ret;
3443
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 return 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003445 }
3446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 if (ret == 0) {
3448 MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
3449 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
3450 MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
3451 return ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003452 }
3453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003455 }
3456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 return ret;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003458}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003459#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003460
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003462static int ssl_check_record_type(uint8_t record_type)
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003463{
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003465 record_type != MBEDTLS_SSL_MSG_ALERT &&
3466 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
3468 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003469 }
3470
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 return 0;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003472}
3473
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003474/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003475 * ContentType type;
3476 * ProtocolVersion version;
3477 * uint16 epoch; // DTLS only
3478 * uint48 sequence_number; // DTLS only
3479 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003480 *
3481 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003482 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003483 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3484 *
3485 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003486 * 1. proceed with the record if this function returns 0
3487 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3488 * 3. return CLIENT_RECONNECT if this function return that value
3489 * 4. drop the whole datagram if this function returns anything else.
3490 * Point 2 is needed when the peer is resending, and we have already received
3491 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003492 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003493MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003494static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
3495 unsigned char *buf,
3496 size_t len,
3497 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00003498{
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003499 mbedtls_ssl_protocol_version tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00003500
Hanno Beckere5e7e782019-07-11 12:29:35 +01003501 size_t const rec_hdr_type_offset = 0;
3502 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003503
Hanno Beckere5e7e782019-07-11 12:29:35 +01003504 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3505 rec_hdr_type_len;
3506 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
Hanno Beckere5e7e782019-07-11 12:29:35 +01003508 size_t const rec_hdr_ctr_len = 8;
3509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003510 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003511 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3512 rec_hdr_version_len;
3513
Hanno Beckera0e20d02019-05-15 14:03:01 +01003514#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003515 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3516 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003517 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003518#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3519#endif /* MBEDTLS_SSL_PROTO_DTLS */
3520
3521 size_t rec_hdr_len_offset; /* To be determined */
3522 size_t const rec_hdr_len_len = 2;
3523
3524 /*
3525 * Check minimum lengths for record header.
3526 */
3527
3528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003530 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003532#endif /* MBEDTLS_SSL_PROTO_DTLS */
3533 {
3534 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3535 }
3536
Gilles Peskine449bd832023-01-11 14:50:10 +01003537 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3538 MBEDTLS_SSL_DEBUG_MSG(1,
3539 (
3540 "datagram of length %u too small to hold DTLS record header of length %u",
3541 (unsigned) len,
3542 (unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
3543 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003544 }
3545
3546 /*
3547 * Parse and validate record content type
3548 */
3549
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 rec->type = buf[rec_hdr_type_offset];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003551
3552 /* Check record content type */
3553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3554 rec->cid_len = 0;
3555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003557 ssl->conf->cid_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 rec->type == MBEDTLS_SSL_MSG_CID) {
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003559 /* Shift pointers to account for record header including CID
3560 * struct {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02003561 * ContentType outer_type = tls12_cid;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003562 * ProtocolVersion version;
3563 * uint16 epoch;
3564 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003565 * opaque cid[cid_length]; // Additional field compared to
3566 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003567 * uint16 length;
3568 * opaque enc_content[DTLSCiphertext.length];
3569 * } DTLSCiphertext;
3570 */
3571
3572 /* So far, we only support static CID lengths
3573 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003574 rec_hdr_cid_len = ssl->conf->cid_len;
3575 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003576
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3578 MBEDTLS_SSL_DEBUG_MSG(1,
3579 (
3580 "datagram of length %u too small to hold DTLS record header including CID, length %u",
3581 (unsigned) len,
3582 (unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
3583 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere538d822019-07-10 14:50:10 +01003584 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003585
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003586 /* configured CID len is guaranteed at most 255, see
3587 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3588 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
3590 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003591#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003592 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 if (ssl_check_record_type(rec->type)) {
3594 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
3595 (unsigned) rec->type));
3596 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003597 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003598 }
3599
Hanno Beckere5e7e782019-07-11 12:29:35 +01003600 /*
3601 * Parse and validate record version
3602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003603 rec->ver[0] = buf[rec_hdr_version_offset + 0];
3604 rec->ver[1] = buf[rec_hdr_version_offset + 1];
3605 tls_version = mbedtls_ssl_read_version(buf + rec_hdr_version_offset,
3606 ssl->conf->transport);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003607
Gilles Peskine449bd832023-01-11 14:50:10 +01003608 if (tls_version > ssl->conf->max_tls_version) {
3609 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS version mismatch: got %u, expected max %u",
3610 (unsigned) tls_version,
3611 (unsigned) ssl->conf->max_tls_version));
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003612
Gilles Peskine449bd832023-01-11 14:50:10 +01003613 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003615 /*
3616 * Parse/Copy record sequence number.
3617 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003618
Hanno Beckere5e7e782019-07-11 12:29:35 +01003619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003620 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003621 /* Copy explicit record sequence number from input buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
3623 rec_hdr_ctr_len);
3624 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003625#endif /* MBEDTLS_SSL_PROTO_DTLS */
3626 {
3627 /* Copy implicit record sequence number from SSL context structure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003629 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003630
Hanno Beckere5e7e782019-07-11 12:29:35 +01003631 /*
3632 * Parse record length.
3633 */
3634
Hanno Beckere5e7e782019-07-11 12:29:35 +01003635 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) |
3637 ((size_t) buf[rec_hdr_len_offset + 1] << 0);
3638 MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003639
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
3641 "version = [0x%x], msglen = %" MBEDTLS_PRINTF_SIZET,
3642 rec->type, (unsigned) tls_version, rec->data_len));
Hanno Beckere5e7e782019-07-11 12:29:35 +01003643
3644 rec->buf = buf;
3645 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003646
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 if (rec->data_len == 0) {
3648 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3649 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003650
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003651 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003652 * DTLS-related tests.
3653 * Check epoch before checking length constraint because
3654 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3655 * message gets duplicated before the corresponding Finished message,
3656 * the second ChangeCipherSpec should be discarded because it belongs
3657 * to an old epoch, but not because its length is shorter than
3658 * the minimum record length for packets using the new record transform.
3659 * Note that these two kinds of failures are handled differently,
3660 * as an unexpected record is silently skipped but an invalid
3661 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003662 */
3663#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3665 rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003666
Hanno Becker955a5c92019-07-10 17:12:07 +01003667 /* Check that the datagram is large enough to contain a record
3668 * of the advertised length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003669 if (len < rec->data_offset + rec->data_len) {
3670 MBEDTLS_SSL_DEBUG_MSG(1,
3671 (
3672 "Datagram of length %u too small to contain record of advertised length %u.",
3673 (unsigned) len,
3674 (unsigned) (rec->data_offset + rec->data_len)));
3675 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker955a5c92019-07-10 17:12:07 +01003676 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003677
Hanno Becker37cfe732019-07-10 17:20:01 +01003678 /* Records from other, non-matching epochs are silently discarded.
3679 * (The case of same-port Client reconnects must be considered in
3680 * the caller). */
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 if (rec_epoch != ssl->in_epoch) {
3682 MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
3683 "expected %u, received %lu",
3684 ssl->in_epoch, (unsigned long) rec_epoch));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003685
Hanno Becker552f7472019-07-19 10:59:12 +01003686 /* Records from the next epoch are considered for buffering
3687 * (concretely: early Finished messages). */
Gilles Peskine449bd832023-01-11 14:50:10 +01003688 if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
3689 MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
3690 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003691 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003692
Gilles Peskine449bd832023-01-11 14:50:10 +01003693 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003694 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003695#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003696 /* For records from the correct epoch, check whether their
3697 * sequence number has been seen before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003698 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
3699 &rec->ctr[0]) != 0) {
3700 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
3701 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003702 }
3703#endif
3704 }
3705#endif /* MBEDTLS_SSL_PROTO_DTLS */
3706
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003708}
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Paul Bakker5121ce52009-01-03 21:22:43 +00003710
Hanno Becker2fddd372019-07-10 14:37:41 +01003711#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003712MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003713static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
Hanno Becker2fddd372019-07-10 14:37:41 +01003714{
Gilles Peskine449bd832023-01-11 14:50:10 +01003715 unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1];
Hanno Becker2fddd372019-07-10 14:37:41 +01003716
3717 /*
3718 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3719 * access the first byte of record content (handshake type), as we
3720 * have an active transform (possibly iv_len != 0), so use the
3721 * fact that the record header len is 13 instead.
3722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003723 if (rec_epoch == 0 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003724 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 mbedtls_ssl_is_handshake_over(ssl) == 1 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003726 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3727 ssl->in_left > 13 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
3729 MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
3730 "from the same port"));
3731 return ssl_handle_possible_reconnect(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 }
3733
Gilles Peskine449bd832023-01-11 14:50:10 +01003734 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003735}
Hanno Becker2fddd372019-07-10 14:37:41 +01003736#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003738/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003739 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003740 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003742static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
3743 mbedtls_record *rec)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003744{
3745 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003746
Gilles Peskine449bd832023-01-11 14:50:10 +01003747 MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
3748 rec->buf, rec->buf_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003749
Ronald Cron7e38cba2021-11-24 12:43:39 +01003750 /*
3751 * In TLS 1.3, always treat ChangeCipherSpec records
3752 * as unencrypted. The only thing we do with them is
3753 * check the length and content and ignore them.
3754 */
Ronald Cron6f135e12021-12-08 16:57:54 +01003755#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 if (ssl->transform_in != NULL &&
3757 ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3758 if (rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
Ronald Cron7e38cba2021-11-24 12:43:39 +01003759 done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 }
Ronald Cron7e38cba2021-11-24 12:43:39 +01003761 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003762#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron7e38cba2021-11-24 12:43:39 +01003763
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 if (!done && ssl->transform_in != NULL) {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003765 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003766
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
3768 rec)) != 0) {
3769 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
Hanno Becker8367ccc2019-05-14 11:30:10 +01003770
Hanno Beckera0e20d02019-05-15 14:03:01 +01003771#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Becker8367ccc2019-05-14 11:30:10 +01003773 ssl->conf->ignore_unexpected_cid
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
3775 MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
Hanno Becker16ded982019-05-08 13:02:55 +01003776 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003777 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003778#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003779
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003781 }
3782
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 if (old_msg_type != rec->type) {
3784 MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
3785 old_msg_type, rec->type));
Hanno Becker6430faf2019-05-08 11:57:13 +01003786 }
3787
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
3789 rec->buf + rec->data_offset, rec->data_len);
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003790
Hanno Beckera0e20d02019-05-15 14:03:01 +01003791#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003792 /* We have already checked the record content type
3793 * in ssl_parse_record_header(), failing or silently
3794 * dropping the record in the case of an unknown type.
3795 *
3796 * Since with the use of CIDs, the record content type
3797 * might change during decryption, re-check the record
3798 * content type, but treat a failure as fatal this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 if (ssl_check_record_type(rec->type)) {
3800 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
3801 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker6430faf2019-05-08 11:57:13 +01003802 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003803#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003804
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 if (rec->data_len == 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003806#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2
3808 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003809 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
3811 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003812 }
3813#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3814
3815 ssl->nb_zero++;
3816
3817 /*
3818 * Three or more empty messages may be a DoS attack
3819 * (excessive CPU consumption).
3820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 if (ssl->nb_zero > 3) {
3822 MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
3823 "messages, possible DoS attack"));
Hanno Becker6e7700d2019-05-08 10:38:32 +01003824 /* Treat the records as if they were not properly authenticated,
3825 * thereby failing the connection if we see more than allowed
3826 * by the configured bad MAC threshold. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 } else {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003830 ssl->nb_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003831 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003832
3833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003835 ; /* in_ctr read from peer, not maintained internally */
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 } else
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003837#endif
3838 {
3839 unsigned i;
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 for (i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3841 i > mbedtls_ssl_ep_len(ssl); i--) {
3842 if (++ssl->in_ctr[i - 1] != 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003843 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 }
Jerry Yuae0b2e22021-10-08 15:21:19 +08003845 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003846
3847 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 if (i == mbedtls_ssl_ep_len(ssl)) {
3849 MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
3850 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003851 }
3852 }
3853
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 }
3855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3858 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003859 }
3860#endif
3861
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003862 /* Check actual (decrypted) record content length against
3863 * configured maximum. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003864 if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
3865 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
3866 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003867 }
3868
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003870}
3871
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003872/*
3873 * Read a record.
3874 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003875 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3876 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3877 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003878 */
Hanno Becker1097b342018-08-15 14:09:41 +01003879
3880/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003881MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003882static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003884static int ssl_get_next_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003885MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003886static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
Hanno Becker4162b112018-08-15 14:05:04 +01003887
Gilles Peskine449bd832023-01-11 14:50:10 +01003888int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
3889 unsigned update_hs_digest)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003890{
Janos Follath865b3eb2019-12-16 11:46:15 +00003891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003892
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003894
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 if (ssl->keep_current_message == 0) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003896 do {
Simon Butcher99000142016-10-13 17:21:01 +01003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 ret = ssl_consume_current_message(ssl);
3899 if (ret != 0) {
3900 return ret;
3901 }
Hanno Becker26994592018-08-15 14:14:59 +01003902
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 if (ssl_record_is_in_progress(ssl) == 0) {
David Horstmann10be1342022-10-06 18:31:25 +01003904 int dtls_have_buffered = 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere74d5562018-08-15 14:26:08 +01003906
Hanno Becker40f50842018-08-15 14:48:01 +01003907 /* We only check for buffered messages if the
3908 * current datagram is fully consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3910 ssl_next_record_is_in_datagram(ssl) == 0) {
3911 if (ssl_load_buffered_message(ssl) == 0) {
David Horstmann10be1342022-10-06 18:31:25 +01003912 dtls_have_buffered = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 }
Hanno Becker40f50842018-08-15 14:48:01 +01003914 }
3915
Hanno Becker40f50842018-08-15 14:48:01 +01003916#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 if (dtls_have_buffered == 0) {
3918 ret = ssl_get_next_record(ssl);
3919 if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
Hanno Becker40f50842018-08-15 14:48:01 +01003920 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 }
Hanno Becker40f50842018-08-15 14:48:01 +01003922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 if (ret != 0) {
3924 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
3925 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003926 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003927 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003928 }
3929
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 ret = mbedtls_ssl_handle_message_type(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003931
Hanno Becker40f50842018-08-15 14:48:01 +01003932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker40f50842018-08-15 14:48:01 +01003934 /* Buffer future message */
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 ret = ssl_buffer_message(ssl);
3936 if (ret != 0) {
3937 return ret;
3938 }
Hanno Becker40f50842018-08-15 14:48:01 +01003939
3940 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3941 }
3942#endif /* MBEDTLS_SSL_PROTO_DTLS */
3943
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 } while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3945 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003946
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 if (0 != ret) {
3948 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
3949 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01003950 }
3951
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3953 update_hs_digest == 1) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003954 ret = mbedtls_ssl_update_handshake_status(ssl);
3955 if (0 != ret) {
3956 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
3957 return ret;
3958 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003959 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 } else {
3961 MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003962 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003963 }
3964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
Simon Butcher99000142016-10-13 17:21:01 +01003966
Gilles Peskine449bd832023-01-11 14:50:10 +01003967 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003968}
3969
Hanno Becker40f50842018-08-15 14:48:01 +01003970#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003971MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003972static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003973{
Gilles Peskine449bd832023-01-11 14:50:10 +01003974 if (ssl->in_left > ssl->next_record_offset) {
3975 return 1;
3976 }
Simon Butcher99000142016-10-13 17:21:01 +01003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 return 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003979}
3980
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003981MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003982static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01003983{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003984 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003986 int ret = 0;
3987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 if (hs == NULL) {
3989 return -1;
3990 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
Hanno Beckere00ae372018-08-20 09:39:42 +01003993
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3995 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003996 /* Check if we have seen a ChangeCipherSpec before.
3997 * If yes, synthesize a CCS record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if (!hs->buffering.seen_ccs) {
3999 MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004000 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004001 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004002 }
4003
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004005 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4006 ssl->in_msglen = 1;
4007 ssl->in_msg[0] = 1;
4008
4009 /* As long as they are equal, the exact value doesn't matter. */
4010 ssl->in_left = 0;
4011 ssl->next_record_offset = 0;
4012
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004013 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004014 goto exit;
4015 }
Hanno Becker37f95322018-08-16 13:55:32 +01004016
Hanno Beckerb8f50142018-08-28 10:01:34 +01004017#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004018 /* Debug only */
4019 {
4020 unsigned offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
Hanno Becker37f95322018-08-16 13:55:32 +01004022 hs_buf = &hs->buffering.hs[offset];
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 if (hs_buf->is_valid == 1) {
4024 MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
4025 hs->in_msg_seq + offset,
4026 hs_buf->is_complete ? "fully" : "partially"));
Hanno Becker37f95322018-08-16 13:55:32 +01004027 }
4028 }
4029 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004030#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004031
4032 /* Check if we have buffered and/or fully reassembled the
4033 * next handshake message. */
4034 hs_buf = &hs->buffering.hs[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
Hanno Becker37f95322018-08-16 13:55:32 +01004036 /* Synthesize a record containing the buffered HS message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 size_t msg_len = (hs_buf->data[1] << 16) |
4038 (hs_buf->data[2] << 8) |
4039 hs_buf->data[3];
Hanno Becker37f95322018-08-16 13:55:32 +01004040
4041 /* Double-check that we haven't accidentally buffered
4042 * a message that doesn't fit into the input buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
4044 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4045 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004046 }
4047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
4049 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
4050 hs_buf->data, msg_len + 12);
Hanno Becker37f95322018-08-16 13:55:32 +01004051
4052 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4053 ssl->in_hslen = msg_len + 12;
4054 ssl->in_msglen = msg_len + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
Hanno Becker37f95322018-08-16 13:55:32 +01004056
4057 ret = 0;
4058 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 } else {
4060 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
4061 hs->in_msg_seq));
Hanno Becker37f95322018-08-16 13:55:32 +01004062 }
4063
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004064 ret = -1;
4065
4066exit:
4067
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
4069 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004070}
4071
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004072MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004073static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
4074 size_t desired)
Hanno Beckera02b0b42018-08-21 17:20:27 +01004075{
4076 int offset;
4077 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004078 MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
4079 (unsigned) desired));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004080
Hanno Becker01315ea2018-08-21 17:22:17 +01004081 /* Get rid of future records epoch first, if such exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 ssl_free_buffered_record(ssl);
Hanno Becker01315ea2018-08-21 17:22:17 +01004083
4084 /* Check if we have enough space available now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4086 hs->buffering.total_bytes_buffered)) {
4087 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
4088 return 0;
Hanno Becker01315ea2018-08-21 17:22:17 +01004089 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004090
Hanno Becker4f432ad2018-08-28 10:02:32 +01004091 /* We don't have enough space to buffer the next expected handshake
4092 * message. Remove buffers used for future messages to gain space,
4093 * starting with the most distant one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4095 offset >= 0; offset--) {
4096 MBEDTLS_SSL_DEBUG_MSG(2,
4097 (
4098 "Free buffering slot %d to make space for reassembly of next handshake message",
4099 offset));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004100
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 ssl_buffering_free_slot(ssl, (uint8_t) offset);
Hanno Beckera02b0b42018-08-21 17:20:27 +01004102
4103 /* Check if we have enough space available now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4105 hs->buffering.total_bytes_buffered)) {
4106 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
4107 return 0;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004108 }
4109 }
4110
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 return -1;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004112}
4113
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004115static int ssl_buffer_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004116{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004117 int ret = 0;
4118 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 if (hs == NULL) {
4121 return 0;
4122 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004123
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004125
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 switch (ssl->in_msgtype) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004127 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
Hanno Beckere678eaa2018-08-21 14:57:46 +01004129
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004130 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004131 break;
4132
4133 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004134 {
4135 unsigned recv_msg_seq_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Hanno Becker37f95322018-08-16 13:55:32 +01004137 mbedtls_ssl_hs_buffer *hs_buf;
4138 size_t msg_len = ssl->in_hslen - 12;
4139
4140 /* We should never receive an old handshake
4141 * message - double-check nonetheless. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (recv_msg_seq < ssl->handshake->in_msg_seq) {
4143 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4144 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004145 }
4146
4147 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Becker37f95322018-08-16 13:55:32 +01004149 /* Silently ignore -- message too far in the future */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 MBEDTLS_SSL_DEBUG_MSG(2,
4151 ("Ignore future HS message with sequence number %u, "
4152 "buffering window %u - %u",
4153 recv_msg_seq, ssl->handshake->in_msg_seq,
4154 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
4155 1));
Hanno Becker37f95322018-08-16 13:55:32 +01004156
4157 goto exit;
4158 }
4159
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
4161 recv_msg_seq, recv_msg_seq_offset));
Hanno Becker37f95322018-08-16 13:55:32 +01004162
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
Hanno Becker37f95322018-08-16 13:55:32 +01004164
4165 /* Check if the buffering for this seq nr has already commenced. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (!hs_buf->is_valid) {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004167 size_t reassembly_buf_sz;
4168
Hanno Becker37f95322018-08-16 13:55:32 +01004169 hs_buf->is_fragmented =
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 (ssl_hs_is_proper_fragment(ssl) == 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004171
4172 /* We copy the message back into the input buffer
4173 * after reassembly, so check that it's not too large.
4174 * This is an implementation-specific limitation
4175 * and not one from the standard, hence it is not
4176 * checked in ssl_check_hs_header(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
Hanno Becker37f95322018-08-16 13:55:32 +01004178 /* Ignore message */
4179 goto exit;
4180 }
4181
Hanno Beckere0b150f2018-08-21 15:51:03 +01004182 /* Check if we have enough space to buffer the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (hs->buffering.total_bytes_buffered >
4184 MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
4185 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4186 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004187 }
4188
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
4190 hs_buf->is_fragmented);
Hanno Beckere0b150f2018-08-21 15:51:03 +01004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4193 hs->buffering.total_bytes_buffered)) {
4194 if (recv_msg_seq_offset > 0) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004195 /* If we can't buffer a future message because
4196 * of space limitations -- ignore. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 MBEDTLS_SSL_DEBUG_MSG(2,
4198 ("Buffering of future message of size %"
4199 MBEDTLS_PRINTF_SIZET
4200 " would exceed the compile-time limit %"
4201 MBEDTLS_PRINTF_SIZET
4202 " (already %" MBEDTLS_PRINTF_SIZET
4203 " bytes buffered) -- ignore\n",
4204 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4205 hs->buffering.total_bytes_buffered));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 } else {
4208 MBEDTLS_SSL_DEBUG_MSG(2,
4209 ("Buffering of future message of size %"
4210 MBEDTLS_PRINTF_SIZET
4211 " would exceed the compile-time limit %"
4212 MBEDTLS_PRINTF_SIZET
4213 " (already %" MBEDTLS_PRINTF_SIZET
4214 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4215 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4216 hs->buffering.total_bytes_buffered));
Hanno Beckere1801392018-08-21 16:51:05 +01004217 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
4220 MBEDTLS_SSL_DEBUG_MSG(2,
4221 ("Reassembly of next message of size %"
4222 MBEDTLS_PRINTF_SIZET
4223 " (%" MBEDTLS_PRINTF_SIZET
4224 " with bitmap) would exceed"
4225 " the compile-time limit %"
4226 MBEDTLS_PRINTF_SIZET
4227 " (already %" MBEDTLS_PRINTF_SIZET
4228 " bytes buffered) -- fail\n",
4229 msg_len,
4230 reassembly_buf_sz,
4231 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4232 hs->buffering.total_bytes_buffered));
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004233 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4234 goto exit;
4235 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004236 }
4237
Gilles Peskine449bd832023-01-11 14:50:10 +01004238 MBEDTLS_SSL_DEBUG_MSG(2,
4239 ("initialize reassembly, total length = %"
4240 MBEDTLS_PRINTF_SIZET,
4241 msg_len));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004242
Gilles Peskine449bd832023-01-11 14:50:10 +01004243 hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
4244 if (hs_buf->data == NULL) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004245 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004246 goto exit;
4247 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004248 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004249
4250 /* Prepare final header: copy msg_type, length and message_seq,
4251 * then add standardised fragment_offset and fragment_length */
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 memcpy(hs_buf->data, ssl->in_msg, 6);
4253 memset(hs_buf->data + 6, 0, 3);
4254 memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
Hanno Becker37f95322018-08-16 13:55:32 +01004255
4256 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004257
4258 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004260 /* Make sure msg_type and length are consistent */
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
4262 MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
Hanno Becker37f95322018-08-16 13:55:32 +01004263 /* Ignore */
4264 goto exit;
4265 }
4266 }
4267
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 if (!hs_buf->is_complete) {
Hanno Becker37f95322018-08-16 13:55:32 +01004269 size_t frag_len, frag_off;
4270 unsigned char * const msg = hs_buf->data + 12;
4271
4272 /*
4273 * Check and copy current fragment
4274 */
4275
4276 /* Validation of header fields already done in
4277 * mbedtls_ssl_prepare_handshake_record(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004278 frag_off = ssl_get_hs_frag_off(ssl);
4279 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker37f95322018-08-16 13:55:32 +01004280
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4282 ", length = %" MBEDTLS_PRINTF_SIZET,
4283 frag_off, frag_len));
4284 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
Hanno Becker37f95322018-08-16 13:55:32 +01004285
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (hs_buf->is_fragmented) {
Hanno Becker37f95322018-08-16 13:55:32 +01004287 unsigned char * const bitmask = msg + msg_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004288 ssl_bitmask_set(bitmask, frag_off, frag_len);
4289 hs_buf->is_complete = (ssl_bitmask_check(bitmask,
4290 msg_len) == 0);
4291 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004292 hs_buf->is_complete = 1;
4293 }
4294
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
4296 hs_buf->is_complete ? "" : "not yet "));
Hanno Becker37f95322018-08-16 13:55:32 +01004297 }
4298
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004299 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004300 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004301
4302 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004303 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004304 break;
4305 }
4306
4307exit:
4308
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
4310 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004311}
4312#endif /* MBEDTLS_SSL_PROTO_DTLS */
4313
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004314MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004315static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004316{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004317 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004318 * Consume last content-layer message and potentially
4319 * update in_msglen which keeps track of the contents'
4320 * consumption state.
4321 *
4322 * (1) Handshake messages:
4323 * Remove last handshake message, move content
4324 * and adapt in_msglen.
4325 *
4326 * (2) Alert messages:
4327 * Consume whole record content, in_msglen = 0.
4328 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004329 * (3) Change cipher spec:
4330 * Consume whole record content, in_msglen = 0.
4331 *
4332 * (4) Application data:
4333 * Don't do anything - the record layer provides
4334 * the application data as a stream transport
4335 * and consumes through mbedtls_ssl_read only.
4336 *
4337 */
4338
4339 /* Case (1): Handshake messages */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 if (ssl->in_hslen != 0) {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004341 /* Hard assertion to be sure that no application data
4342 * is in flight, as corrupting ssl->in_msglen during
4343 * ssl->in_offt != NULL is fatal. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 if (ssl->in_offt != NULL) {
4345 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4346 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004347 }
4348
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004349 /*
4350 * Get next Handshake message in the current record
4351 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004352
Hanno Becker4a810fb2017-05-24 16:27:30 +01004353 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004354 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004355 * current handshake content: If DTLS handshake
4356 * fragmentation is used, that's the fragment
4357 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004358 * size here is faulty and should be changed at
4359 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004360 * (2) While it doesn't seem to cause problems, one
4361 * has to be very careful not to assume that in_hslen
4362 * is always <= in_msglen in a sensible communication.
4363 * Again, it's wrong for DTLS handshake fragmentation.
4364 * The following check is therefore mandatory, and
4365 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004366 * Additionally, ssl->in_hslen might be arbitrarily out of
4367 * bounds after handling a DTLS message with an unexpected
4368 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004370 if (ssl->in_hslen < ssl->in_msglen) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004371 ssl->in_msglen -= ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4373 ssl->in_msglen);
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004374
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
4376 ssl->in_msg, ssl->in_msglen);
4377 } else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004378 ssl->in_msglen = 0;
4379 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004380
Hanno Becker4a810fb2017-05-24 16:27:30 +01004381 ssl->in_hslen = 0;
4382 }
4383 /* Case (4): Application data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 else if (ssl->in_offt != NULL) {
4385 return 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01004386 }
4387 /* Everything else (CCS & Alerts) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004389 ssl->in_msglen = 0;
4390 }
4391
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 return 0;
Hanno Becker1097b342018-08-15 14:09:41 +01004393}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004394
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004396static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
Hanno Beckere74d5562018-08-15 14:26:08 +01004397{
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 if (ssl->in_msglen > 0) {
4399 return 1;
4400 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004401
Gilles Peskine449bd832023-01-11 14:50:10 +01004402 return 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004403}
4404
Hanno Becker5f066e72018-08-16 14:56:31 +01004405#if defined(MBEDTLS_SSL_PROTO_DTLS)
4406
Gilles Peskine449bd832023-01-11 14:50:10 +01004407static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004408{
4409 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (hs == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004411 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (hs->buffering.future_record.data != NULL) {
Hanno Becker01315ea2018-08-21 17:22:17 +01004415 hs->buffering.total_bytes_buffered -=
4416 hs->buffering.future_record.len;
4417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 mbedtls_free(hs->buffering.future_record.data);
Hanno Becker01315ea2018-08-21 17:22:17 +01004419 hs->buffering.future_record.data = NULL;
4420 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004421}
4422
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004423MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004424static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004425{
4426 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 unsigned char *rec;
Hanno Becker5f066e72018-08-16 14:56:31 +01004428 size_t rec_len;
4429 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004430#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4431 size_t in_buf_len = ssl->in_buf_len;
4432#else
4433 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4434#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4436 return 0;
4437 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004438
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 if (hs == NULL) {
4440 return 0;
4441 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004442
Hanno Becker5f066e72018-08-16 14:56:31 +01004443 rec = hs->buffering.future_record.data;
4444 rec_len = hs->buffering.future_record.len;
4445 rec_epoch = hs->buffering.future_record.epoch;
4446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (rec == NULL) {
4448 return 0;
4449 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004450
Hanno Becker4cb782d2018-08-20 11:19:05 +01004451 /* Only consider loading future records if the
4452 * input buffer is empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (ssl_next_record_is_in_datagram(ssl) == 1) {
4454 return 0;
4455 }
Hanno Becker4cb782d2018-08-20 11:19:05 +01004456
Gilles Peskine449bd832023-01-11 14:50:10 +01004457 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 if (rec_epoch != ssl->in_epoch) {
4460 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
Hanno Becker5f066e72018-08-16 14:56:31 +01004461 goto exit;
4462 }
4463
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004465
4466 /* Double-check that the record is not too large */
Gilles Peskine449bd832023-01-11 14:50:10 +01004467 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
4468 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4469 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker5f066e72018-08-16 14:56:31 +01004470 }
4471
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 memcpy(ssl->in_hdr, rec, rec_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004473 ssl->in_left = rec_len;
4474 ssl->next_record_offset = 0;
4475
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 ssl_free_buffered_record(ssl);
Hanno Becker5f066e72018-08-16 14:56:31 +01004477
4478exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004479 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
4480 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004481}
4482
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004483MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004484static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
4485 mbedtls_record const *rec)
Hanno Becker5f066e72018-08-16 14:56:31 +01004486{
4487 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004488
4489 /* Don't buffer future records outside handshakes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (hs == NULL) {
4491 return 0;
4492 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004493
4494 /* Only buffer handshake records (we are only interested
4495 * in Finished messages). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
4497 return 0;
4498 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004499
4500 /* Don't buffer more than one future epoch record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 if (hs->buffering.future_record.data != NULL) {
4502 return 0;
4503 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004504
Hanno Becker01315ea2018-08-21 17:22:17 +01004505 /* Don't buffer record if there's not enough buffering space remaining. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4507 hs->buffering.total_bytes_buffered)) {
4508 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4509 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4510 " (already %" MBEDTLS_PRINTF_SIZET
4511 " bytes buffered) -- ignore\n",
4512 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4513 hs->buffering.total_bytes_buffered));
4514 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004515 }
4516
Hanno Becker5f066e72018-08-16 14:56:31 +01004517 /* Buffer record */
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
4519 ssl->in_epoch + 1U));
4520 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004521
4522 /* ssl_parse_record_header() only considers records
4523 * of the next epoch as candidates for buffering. */
4524 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004525 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004526
4527 hs->buffering.future_record.data =
Gilles Peskine449bd832023-01-11 14:50:10 +01004528 mbedtls_calloc(1, hs->buffering.future_record.len);
4529 if (hs->buffering.future_record.data == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004530 /* If we run out of RAM trying to buffer a
4531 * record from the next epoch, just ignore. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004533 }
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004536
Hanno Becker519f15d2019-07-11 12:43:20 +01004537 hs->buffering.total_bytes_buffered += rec->buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004539}
4540
4541#endif /* MBEDTLS_SSL_PROTO_DTLS */
4542
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004543MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004544static int ssl_get_next_record(mbedtls_ssl_context *ssl)
Hanno Becker1097b342018-08-15 14:09:41 +01004545{
Janos Follath865b3eb2019-12-16 11:46:15 +00004546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004547 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004548
Hanno Becker5f066e72018-08-16 14:56:31 +01004549#if defined(MBEDTLS_SSL_PROTO_DTLS)
4550 /* We might have buffered a future record; if so,
4551 * and if the epoch matches now, load it.
4552 * On success, this call will set ssl->in_left to
4553 * the length of the buffered record, so that
4554 * the calls to ssl_fetch_input() below will
4555 * essentially be no-ops. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 ret = ssl_load_buffered_record(ssl);
4557 if (ret != 0) {
4558 return ret;
4559 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004560#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004561
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004562 /* Ensure that we have enough space available for the default form
4563 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4564 * with no space for CIDs counted in). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
4566 if (ret != 0) {
4567 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4568 return ret;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004569 }
4570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
4572 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4575 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
4576 ret = ssl_buffer_future_record(ssl, &rec);
4577 if (ret != 0) {
4578 return ret;
4579 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004580
4581 /* Fall through to handling of unexpected records */
4582 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4583 }
4584
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
Hanno Becker2fddd372019-07-10 14:37:41 +01004586#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004587 /* Reset in pointers to default state for TLS/DTLS records,
4588 * assuming no CID and no offset between record content and
4589 * record plaintext. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 mbedtls_ssl_update_in_pointers(ssl);
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004591
Hanno Becker7ae20e02019-07-12 08:33:49 +01004592 /* Setup internal message pointers from record structure. */
4593 ssl->in_msgtype = rec.type;
4594#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4595 ssl->in_len = ssl->in_cid + rec.cid_len;
4596#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4597 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4598 ssl->in_msglen = rec.data_len;
4599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 ret = ssl_check_client_reconnect(ssl);
4601 MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
4602 if (ret != 0) {
4603 return ret;
4604 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004605#endif
4606
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004607 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004608 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004609
Gilles Peskine449bd832023-01-11 14:50:10 +01004610 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
4611 "(header)"));
4612 } else {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004613 /* Skip invalid record and the rest of the datagram */
4614 ssl->next_record_offset = 0;
4615 ssl->in_left = 0;
4616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
4618 "(header)"));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004619 }
4620
4621 /* Get next record */
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4623 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004624#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004625 {
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 return ret;
Hanno Becker2fddd372019-07-10 14:37:41 +01004627 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004628 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckera8814792019-07-10 15:01:45 +01004632 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004633 ssl->next_record_offset = rec.buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if (ssl->next_record_offset < ssl->in_left) {
4635 MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
Hanno Beckere65ce782017-05-22 14:47:48 +01004636 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 } else
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004638#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004639 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004640 /*
4641 * Fetch record contents from underlying transport.
4642 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
4644 if (ret != 0) {
4645 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4646 return ret;
Hanno Beckera8814792019-07-10 15:01:45 +01004647 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004648
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004649 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004650 }
4651
4652 /*
4653 * Decrypt record contents.
4654 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004659 /* Silently discard invalid records */
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004661 /* Except when waiting for Finished as a bad mac here
4662 * probably means something went wrong in the handshake
4663 * (eg wrong psk used, mitm downgrade attempt, etc.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4665 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004666#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4668 mbedtls_ssl_send_alert_message(ssl,
4669 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4670 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004671 }
4672#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 return ret;
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004674 }
4675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 if (ssl->conf->badmac_limit != 0 &&
4677 ++ssl->badmac_seen >= ssl->conf->badmac_limit) {
4678 MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
4679 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004680 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004681
Hanno Becker4a810fb2017-05-24 16:27:30 +01004682 /* As above, invalid records cause
4683 * dismissal of the whole datagram. */
4684
4685 ssl->next_record_offset = 0;
4686 ssl->in_left = 0;
4687
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
4689 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004690 }
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 return ret;
4693 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004694#endif
4695 {
4696 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004697#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4699 mbedtls_ssl_send_alert_message(ssl,
4700 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4701 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004702 }
4703#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 return ret;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004705 }
4706 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004707
Hanno Becker44d89b22019-07-12 09:40:44 +01004708
4709 /* Reset in pointers to default state for TLS/DTLS records,
4710 * assuming no CID and no offset between record content and
4711 * record plaintext. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004712 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker44d89b22019-07-12 09:40:44 +01004713#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4714 ssl->in_len = ssl->in_cid + rec.cid_len;
4715#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004716 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004717
Hanno Becker8685c822019-07-12 09:37:30 +01004718 /* The record content type may change during decryption,
4719 * so re-read it. */
4720 ssl->in_msgtype = rec.type;
4721 /* Also update the input buffer, because unfortunately
4722 * the server-side ssl_parse_client_hello() reparses the
4723 * record header when receiving a ClientHello initiating
4724 * a renegotiation. */
4725 ssl->in_hdr[0] = rec.type;
4726 ssl->in_msg = rec.buf + rec.data_offset;
4727 ssl->in_msglen = rec.data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004728 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
Hanno Becker8685c822019-07-12 09:37:30 +01004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01004731}
4732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01004734{
Janos Follath865b3eb2019-12-16 11:46:15 +00004735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004736
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004737 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004738 * Handle particular types of records
4739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
4741 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
4742 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01004743 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004744 }
4745
Gilles Peskine449bd832023-01-11 14:50:10 +01004746 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4747 if (ssl->in_msglen != 1) {
4748 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
4749 ssl->in_msglen));
4750 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004751 }
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (ssl->in_msg[0] != 1) {
4754 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
4755 ssl->in_msg[0]));
4756 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004757 }
4758
4759#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01004761 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
4763 if (ssl->handshake == NULL) {
4764 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
4765 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004766 }
4767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
4769 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004770 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004771#endif
Ronald Cron7e38cba2021-11-24 12:43:39 +01004772
Ronald Cron6f135e12021-12-08 16:57:54 +01004773#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron7e38cba2021-11-24 12:43:39 +01004775#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004776 MBEDTLS_SSL_DEBUG_MSG(1,
4777 ("Ignore ChangeCipherSpec in TLS 1.3 compatibility mode"));
4778 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Ronald Cron7e38cba2021-11-24 12:43:39 +01004779#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 MBEDTLS_SSL_DEBUG_MSG(1,
4781 ("ChangeCipherSpec invalid in TLS 1.3 without compatibility mode"));
4782 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Ronald Cron7e38cba2021-11-24 12:43:39 +01004783#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
4784 }
Ronald Cron6f135e12021-12-08 16:57:54 +01004785#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere678eaa2018-08-21 14:57:46 +01004786 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
4789 if (ssl->in_msglen != 2) {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004790 /* Note: Standard allows for more than one 2 byte alert
4791 to be packed in a single message, but Mbed TLS doesn't
4792 currently support this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
4794 ssl->in_msglen));
4795 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004796 }
4797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
4799 ssl->in_msg[0], ssl->in_msg[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00004800
4801 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004802 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
4805 MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
4806 ssl->in_msg[1]));
4807 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 }
4809
Gilles Peskine449bd832023-01-11 14:50:10 +01004810 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4811 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
4812 MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
4813 return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004815
4816#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4818 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
4819 MBEDTLS_SSL_DEBUG_MSG(2, ("is a no renegotiation alert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004820 /* Will be handled when trying to parse ServerHello */
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004822 }
4823#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004824 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004825 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
4827
Hanno Beckerc76c6192017-06-06 10:03:17 +01004828#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker37ae9522019-05-03 16:54:26 +01004830 /* Drop unexpected ApplicationData records,
4831 * except at the beginning of renegotiations */
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4833 mbedtls_ssl_is_handshake_over(ssl) == 0
Hanno Becker37ae9522019-05-03 16:54:26 +01004834#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4836 ssl->state == MBEDTLS_SSL_SERVER_HELLO)
Hanno Beckerc76c6192017-06-06 10:03:17 +01004837#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 ) {
4839 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
4840 return MBEDTLS_ERR_SSL_NON_FATAL;
Hanno Becker37ae9522019-05-03 16:54:26 +01004841 }
4842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 if (ssl->handshake != NULL &&
4844 mbedtls_ssl_is_handshake_over(ssl) == 1) {
4845 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Hanno Becker37ae9522019-05-03 16:54:26 +01004846 }
4847 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004848#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004851}
4852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004854{
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 return mbedtls_ssl_send_alert_message(ssl,
4856 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4857 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004858}
4859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
4861 unsigned char level,
4862 unsigned char message)
Paul Bakker0a925182012-04-16 06:46:41 +00004863{
Janos Follath865b3eb2019-12-16 11:46:15 +00004864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004865
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 if (ssl == NULL || ssl->conf == NULL) {
4867 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4868 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (ssl->out_left != 0) {
4871 return mbedtls_ssl_flush_output(ssl);
4872 }
Hanno Becker5e18f742018-08-06 11:35:16 +01004873
Gilles Peskine449bd832023-01-11 14:50:10 +01004874 MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
4875 MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
Paul Bakker0a925182012-04-16 06:46:41 +00004876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004877 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004878 ssl->out_msglen = 2;
4879 ssl->out_msg[0] = level;
4880 ssl->out_msg[1] = message;
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
4883 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
4884 return ret;
Paul Bakker0a925182012-04-16 06:46:41 +00004885 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
Paul Bakker0a925182012-04-16 06:46:41 +00004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 return 0;
Paul Bakker0a925182012-04-16 06:46:41 +00004889}
4890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004892{
Janos Follath865b3eb2019-12-16 11:46:15 +00004893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004897 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004898 ssl->out_msglen = 1;
4899 ssl->out_msg[0] = 1;
4900
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 ssl->state++;
4902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4904 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4905 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004906 }
4907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004909
Gilles Peskine449bd832023-01-11 14:50:10 +01004910 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004911}
4912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004914{
Janos Follath865b3eb2019-12-16 11:46:15 +00004915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004918
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
4920 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
4921 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004922 }
4923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4925 MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
4926 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4927 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
4928 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 }
4930
Hanno Beckere678eaa2018-08-21 14:57:46 +01004931 /* CCS records are only accepted if they have length 1 and content '1',
4932 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004933
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004934 /*
4935 * Switch to our negotiated transform and session parameters for inbound
4936 * data.
4937 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004938 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
Jerry Yu2e199812022-12-01 18:57:19 +08004939#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004940 ssl->transform_in = ssl->transform_negotiate;
Jerry Yu2e199812022-12-01 18:57:19 +08004941#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004942 ssl->session_in = ssl->session_negotiate;
4943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004947 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004948#endif
4949
4950 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 if (++ssl->in_epoch == 0) {
4952 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004953 /* This is highly unlikely to happen for legitimate reasons, so
4954 treat it as an attack and don't send an alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004956 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004957 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004958#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 mbedtls_ssl_update_in_pointers(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004962
Paul Bakker5121ce52009-01-03 21:22:43 +00004963 ssl->state++;
4964
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004966
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004968}
4969
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004970/* Once ssl->out_hdr as the address of the beginning of the
4971 * next outgoing record is set, deduce the other pointers.
4972 *
4973 * Note: For TLS, we save the implicit record sequence number
4974 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4975 * and the caller has to make sure there's space for this.
4976 */
4977
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004978static size_t ssl_transform_get_explicit_iv_len(
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 mbedtls_ssl_transform const *transform)
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004980{
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 return transform->ivlen - transform->fixed_ivlen;
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004982}
4983
Gilles Peskine449bd832023-01-11 14:50:10 +01004984void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
4985 mbedtls_ssl_transform *transform)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004986{
4987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004988 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004989 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004990#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08004991 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004992 ssl->out_len = ssl->out_cid;
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 if (transform != NULL) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004994 ssl->out_len += transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004995 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004996#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08004997 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004998#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004999 ssl->out_iv = ssl->out_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005000 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005001#endif
5002 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005003 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005004#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005005 ssl->out_cid = ssl->out_len;
5006#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005007 ssl->out_iv = ssl->out_hdr + 5;
5008 }
5009
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005010 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005011 /* Adjust out_msg to make space for explicit IV, if used. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005012 if (transform != NULL) {
5013 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
5014 }
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005015}
5016
5017/* Once ssl->in_hdr as the address of the beginning of the
5018 * next incoming record is set, deduce the other pointers.
5019 *
5020 * Note: For TLS, we save the implicit record sequence number
5021 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5022 * and the caller has to make sure there's space for this.
5023 */
5024
Gilles Peskine449bd832023-01-11 14:50:10 +01005025void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005026{
Hanno Becker79594fd2019-05-08 09:38:41 +01005027 /* This function sets the pointers to match the case
5028 * of unprotected TLS/DTLS records, with both ssl->in_iv
5029 * and ssl->in_msg pointing to the beginning of the record
5030 * content.
5031 *
5032 * When decrypting a protected record, ssl->in_msg
5033 * will be shifted to point to the beginning of the
5034 * record plaintext.
5035 */
5036
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005039 /* This sets the header pointers to match records
5040 * without CID. When we receive a record containing
5041 * a CID, the fields are shifted accordingly in
5042 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005043 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005044#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08005045 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005046 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005047#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08005048 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005049#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005050 ssl->in_iv = ssl->in_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005052#endif
5053 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08005054 ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005055 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005056#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005057 ssl->in_cid = ssl->in_len;
5058#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005059 ssl->in_iv = ssl->in_hdr + 5;
5060 }
5061
Hanno Becker79594fd2019-05-08 09:38:41 +01005062 /* This will be adjusted at record decryption time. */
5063 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005064}
5065
Paul Bakker5121ce52009-01-03 21:22:43 +00005066/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005067 * Setup an SSL context
5068 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005069
Gilles Peskine449bd832023-01-11 14:50:10 +01005070void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005071{
5072 /* Set the incoming and outgoing record pointers. */
5073#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005075 ssl->out_hdr = ssl->out_buf;
5076 ssl->in_hdr = ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01005077 } else
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005078#endif /* MBEDTLS_SSL_PROTO_DTLS */
5079 {
Hanno Becker12078f42021-03-02 15:28:41 +00005080 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005081 ssl->out_hdr = ssl->out_buf + 8;
5082 ssl->in_hdr = ssl->in_buf + 8;
5083 }
5084
5085 /* Derive other internal pointers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
5087 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005088}
5089
Paul Bakker5121ce52009-01-03 21:22:43 +00005090/*
5091 * SSL get accessors
5092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005094{
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00005096}
5097
Gilles Peskine449bd832023-01-11 14:50:10 +01005098int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
Hanno Becker8b170a02017-10-10 11:51:19 +01005099{
5100 /*
5101 * Case A: We're currently holding back
5102 * a message for further processing.
5103 */
5104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (ssl->keep_current_message == 1) {
5106 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
5107 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005108 }
5109
5110 /*
5111 * Case B: Further records are pending in the current datagram.
5112 */
5113
5114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5116 ssl->in_left > ssl->next_record_offset) {
5117 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
5118 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005119 }
5120#endif /* MBEDTLS_SSL_PROTO_DTLS */
5121
5122 /*
5123 * Case C: A handshake message is being processed.
5124 */
5125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
5127 MBEDTLS_SSL_DEBUG_MSG(3,
5128 ("ssl_check_pending: more handshake messages within current record"));
5129 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005130 }
5131
5132 /*
5133 * Case D: An application data message is being processed
5134 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005135 if (ssl->in_offt != NULL) {
5136 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
5137 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005138 }
5139
5140 /*
5141 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005142 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005143 * we implement support for multiple alerts in single records.
5144 */
5145
Gilles Peskine449bd832023-01-11 14:50:10 +01005146 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
5147 return 0;
Hanno Becker8b170a02017-10-10 11:51:19 +01005148}
5149
Paul Bakker43ca69c2011-01-15 17:35:19 +00005150
Gilles Peskine449bd832023-01-11 14:50:10 +01005151int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005152{
Hanno Becker3136ede2018-08-17 15:28:19 +01005153 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005154 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005155 unsigned block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005156#if defined(MBEDTLS_USE_PSA_CRYPTO)
5157 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
5158 psa_key_type_t key_type;
5159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker5903de42019-05-03 14:46:38 +01005162
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (transform == NULL) {
5164 return (int) out_hdr_len;
5165 }
Hanno Becker78640902018-08-13 16:35:15 +01005166
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005167
5168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (transform->psa_alg == PSA_ALG_GCM ||
5170 transform->psa_alg == PSA_ALG_CCM ||
5171 transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 8) ||
5172 transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 ||
5173 transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) {
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005174 transform_expansion = transform->minlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 } else if (transform->psa_alg == PSA_ALG_CBC_NO_PADDING) {
5176 (void) psa_get_key_attributes(transform->psa_key_enc, &attr);
5177 key_type = psa_get_key_type(&attr);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005180
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005181 /* Expansion due to the addition of the MAC. */
5182 transform_expansion += transform->maclen;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005183
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005184 /* Expansion due to the addition of CBC padding;
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005185 * Theoretically up to 256 bytes, but we never use
5186 * more than the block size of the underlying cipher. */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005187 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005188
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005189 /* For TLS 1.2 or higher, an explicit IV is added
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005190 * after the record header. */
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005191#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005192 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005193#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 } else {
5195 MBEDTLS_SSL_DEBUG_MSG(1,
5196 ("Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()"));
5197 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005198 }
5199#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 case MBEDTLS_MODE_GCM:
5202 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005203 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005204 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005205 transform_expansion = transform->minlen;
5206 break;
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005209
5210 block_size = mbedtls_cipher_get_block_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 &transform->cipher_ctx_enc);
Hanno Becker5b559ac2018-08-03 09:40:07 +01005212
Hanno Becker3136ede2018-08-17 15:28:19 +01005213 /* Expansion due to the addition of the MAC. */
5214 transform_expansion += transform->maclen;
5215
5216 /* Expansion due to the addition of CBC padding;
5217 * Theoretically up to 256 bytes, but we never use
5218 * more than the block size of the underlying cipher. */
5219 transform_expansion += block_size;
5220
TRodziewicz4ca18aa2021-05-20 14:46:20 +02005221 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01005222 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02005223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005224 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02005225#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005226
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005227 break;
5228
5229 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5231 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005232 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005233#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005234
Hanno Beckera0e20d02019-05-15 14:03:01 +01005235#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if (transform->out_cid_len != 0) {
Hanno Becker6cbad552019-05-08 15:40:11 +01005237 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005239#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005240
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 return (int) (out_hdr_len + transform_expansion);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005242}
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005245/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005246 * Check record counters and renegotiate if they're above the limit.
5247 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005249static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005250{
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 size_t ep_len = mbedtls_ssl_ep_len(ssl);
Andres AG2196c7f2016-12-15 17:01:16 +00005252 int in_ctr_cmp;
5253 int out_ctr_cmp;
5254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 if (mbedtls_ssl_is_handshake_over(ssl) == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5258 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005259 }
5260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
5262 &ssl->conf->renego_period[ep_len],
5263 MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len);
5264 out_ctr_cmp = memcmp(&ssl->cur_out_ctr[ep_len],
Jerry Yud9a94fe2021-09-28 18:58:59 +08005265 &ssl->conf->renego_period[ep_len],
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 sizeof(ssl->cur_out_ctr) - ep_len);
Andres AG2196c7f2016-12-15 17:01:16 +00005267
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
5269 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005270 }
5271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
5273 return mbedtls_ssl_renegotiate(ssl);
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005274}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005276
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005277#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5278
5279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Jerry Yua0446a02022-07-13 11:22:55 +08005280MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005281static int ssl_tls13_check_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005282{
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 if ((ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl)) ||
5285 (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET)) {
5286 return 0;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005287 }
5288
5289 ssl->keep_current_message = 1;
5290
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 MBEDTLS_SSL_DEBUG_MSG(3, ("NewSessionTicket received"));
5292 mbedtls_ssl_handshake_set_state(ssl,
5293 MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005294
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 return MBEDTLS_ERR_SSL_WANT_READ;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005296}
5297#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5298
Jerry Yua0446a02022-07-13 11:22:55 +08005299MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005300static int ssl_tls13_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005301{
5302
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 MBEDTLS_SSL_DEBUG_MSG(3, ("received post-handshake message"));
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005304
5305#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5307 int ret = ssl_tls13_check_new_session_ticket(ssl);
5308 if (ret != 0) {
5309 return ret;
5310 }
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005311 }
5312#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5313
5314 /* Fail in all other cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005315 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005316}
5317#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5318
5319#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005320/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005321 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005322 * may only be sent for the purpose of initiating renegotiations.
5323 *
5324 * This function is introduced as a separate helper since the handling
5325 * of post-handshake handshake messages changes significantly in TLS 1.3,
5326 * and having a helper function allows to distinguish between TLS <= 1.2 and
5327 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5328 */
Jerry Yua0446a02022-07-13 11:22:55 +08005329MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005330static int ssl_tls12_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005331{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005333
5334 /*
5335 * - For client-side, expect SERVER_HELLO_REQUEST.
5336 * - For server-side, expect CLIENT_HELLO.
5337 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5338 */
5339
5340#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5342 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5343 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
5344 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005345
5346 /* With DTLS, drop the packet (probably from last handshake) */
5347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5349 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005350 }
5351#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005353 }
5354#endif /* MBEDTLS_SSL_CLI_C */
5355
5356#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5358 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
5359 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005360
5361 /* With DTLS, drop the packet (probably from last handshake) */
5362#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5364 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005365 }
5366#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005368 }
5369#endif /* MBEDTLS_SSL_SRV_C */
5370
5371#if defined(MBEDTLS_SSL_RENEGOTIATION)
5372 /* Determine whether renegotiation attempt should be accepted */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5374 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5375 ssl->conf->allow_legacy_renegotiation ==
5376 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005377 /*
5378 * Accept renegotiation request
5379 */
5380
5381 /* DTLS clients need to know renego is server-initiated */
5382#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5384 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005385 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5386 }
5387#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 ret = mbedtls_ssl_start_renegotiation(ssl);
5389 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5390 ret != 0) {
5391 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
5392 ret);
5393 return ret;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005394 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 } else
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005396#endif /* MBEDTLS_SSL_RENEGOTIATION */
5397 {
5398 /*
5399 * Refuse renegotiation
5400 */
5401
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005403
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5405 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5406 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) != 0) {
5407 return ret;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005408 }
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005409 }
5410
Gilles Peskine449bd832023-01-11 14:50:10 +01005411 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005412}
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005413#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5414
5415MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005416static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005417{
5418 /* Check protocol version and dispatch accordingly. */
5419#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
5421 return ssl_tls13_handle_hs_message_post_handshake(ssl);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005422 }
5423#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5424
5425#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 if (ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
5427 return ssl_tls12_handle_hs_message_post_handshake(ssl);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005428 }
5429#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5430
5431 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005433}
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005434
Paul Bakker48916f92012-09-16 19:57:18 +00005435/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005436 * Receive application data decrypted from the SSL layer
5437 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005438int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005439{
Janos Follath865b3eb2019-12-16 11:46:15 +00005440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005441 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005442
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 if (ssl == NULL || ssl->conf == NULL) {
5444 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5445 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5451 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5452 return ret;
5453 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005454
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 if (ssl->handshake != NULL &&
5456 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
5457 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
5458 return ret;
5459 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005460 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005461 }
5462#endif
5463
Hanno Becker4a810fb2017-05-24 16:27:30 +01005464 /*
5465 * Check if renegotiation is necessary and/or handshake is
5466 * in process. If yes, perform/continue, and fall through
5467 * if an unexpected packet is received while the client
5468 * is waiting for the ServerHello.
5469 *
5470 * (There is no equivalent to the last condition on
5471 * the server-side as it is not treated as within
5472 * a handshake while waiting for the ClientHello
5473 * after a renegotiation request.)
5474 */
5475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 ret = ssl_check_ctr_renegotiate(ssl);
5478 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5479 ret != 0) {
5480 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5481 return ret;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005482 }
5483#endif
5484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5486 ret = mbedtls_ssl_handshake(ssl);
5487 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5488 ret != 0) {
5489 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5490 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 }
5492 }
5493
Hanno Beckere41158b2017-10-23 13:30:32 +01005494 /* Loop as long as no application data record is available */
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 while (ssl->in_offt == NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005496 /* Start timer if not already running */
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 if (ssl->f_get_timer != NULL &&
5498 ssl->f_get_timer(ssl->p_timer) == -1) {
5499 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005500 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005501
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5503 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5504 return 0;
5505 }
Paul Bakker831a7552011-05-18 13:32:51 +00005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5508 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 }
5510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 if (ssl->in_msglen == 0 &&
5512 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00005513 /*
5514 * OpenSSL sends empty messages to randomize the IV
5515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5517 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5518 return 0;
5519 }
Paul Bakker831a7552011-05-18 13:32:51 +00005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5522 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005523 }
5524 }
5525
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
5527 ret = ssl_handle_hs_message_post_handshake(ssl);
5528 if (ret != 0) {
5529 MBEDTLS_SSL_DEBUG_RET(1, "ssl_handle_hs_message_post_handshake",
5530 ret);
5531 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005532 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005533
Hanno Beckerf26cc722021-04-21 07:30:13 +01005534 /* At this point, we don't know whether the renegotiation triggered
5535 * by the post-handshake message has been completed or not. The cases
5536 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005537 * 1) The renegotiation is complete. In this case, no new record
5538 * has been read yet.
5539 * 2) The renegotiation is incomplete because the client received
5540 * an application data record while awaiting the ServerHello.
5541 * 3) The renegotiation is incomplete because the client received
5542 * a non-handshake, non-application data message while awaiting
5543 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005544 *
5545 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005546 * - For 1), the next iteration will read a new record and check
5547 * if it's application data.
5548 * - For 2), the loop condition isn't satisfied as application data
5549 * is present, hence continue is the same as break
5550 * - For 3), the loop condition is satisfied and read_record
5551 * will re-deliver the message that was held back by the client
5552 * when expecting the ServerHello.
5553 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005554
Hanno Becker90333da2017-10-10 11:27:13 +01005555 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005556 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005557#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5559 if (ssl->conf->renego_max_records >= 0) {
5560 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
5561 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
5562 "but not honored by client"));
5563 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005564 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005565 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005569 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
5571 MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
5572 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005573 }
5574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
5576 MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
5577 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005578 }
5579
5580 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005581
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005582 /* We're going to return something now, cancel timer,
5583 * except if handshake (renegotiation) is in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
5585 mbedtls_ssl_set_timer(ssl, 0);
5586 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005587
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005589 /* If we requested renego but received AppData, resend HelloRequest.
5590 * Do it now, after setting in_offt, to avoid taking this branch
5591 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005593 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5594 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5595 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
5596 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
5597 ret);
5598 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005599 }
5600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005602#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 }
5604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 n = (len < ssl->in_msglen)
Paul Bakker5121ce52009-01-03 21:22:43 +00005606 ? len : ssl->in_msglen;
5607
ashesman937d6d52022-02-17 11:08:27 +13005608 if (len != 0) {
Ashley Duncan358f94a2022-02-11 09:57:18 +13005609 memcpy(buf, ssl->in_offt, n);
5610 ssl->in_msglen -= n;
5611 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005612
gabor-mezei-arma3214132020-07-15 10:55:00 +02005613 /* Zeroising the plaintext buffer to erase unused application data
5614 from the memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 mbedtls_platform_zeroize(ssl->in_offt, n);
gabor-mezei-arma3214132020-07-15 10:55:00 +02005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 if (ssl->in_msglen == 0) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005618 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005620 ssl->keep_current_message = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00005622 /* more data available */
5623 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005625
Gilles Peskine449bd832023-01-11 14:50:10 +01005626 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 return (int) n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005629}
5630
5631/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005632 * Send application data to be encrypted by the SSL layer, taking care of max
5633 * fragment length and buffer size.
5634 *
5635 * According to RFC 5246 Section 6.2.1:
5636 *
5637 * Zero-length fragments of Application data MAY be sent as they are
5638 * potentially useful as a traffic analysis countermeasure.
5639 *
5640 * Therefore, it is possible that the input message length is 0 and the
5641 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005642 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005643MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005644static int ssl_write_real(mbedtls_ssl_context *ssl,
5645 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005646{
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005648 const size_t max_len = (size_t) ret;
5649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 if (ret < 0) {
5651 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
5652 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005653 }
5654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 if (len > max_len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5658 MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
5659 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5660 " > %" MBEDTLS_PRINTF_SIZET,
5661 len, max_len));
5662 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5663 } else
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005664#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 len = max_len;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005666 }
Paul Bakker887bd502011-06-08 13:10:54 +00005667
Gilles Peskine449bd832023-01-11 14:50:10 +01005668 if (ssl->out_left != 0) {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005669 /*
5670 * The user has previously tried to send the data and
5671 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5672 * written. In this case, we expect the high-level write function
5673 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5676 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
5677 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 } else {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005680 /*
5681 * The user is trying to send a message the first time, so we need to
5682 * copy the data into the internal buffers and setup the data structure
5683 * to keep track of partial writes
5684 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005685 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Dave Rodgmanf6840252023-02-24 15:41:34 +00005687 if (len > 0) {
5688 memcpy(ssl->out_msg, buf, len);
5689 }
Paul Bakker887bd502011-06-08 13:10:54 +00005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
5692 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
5693 return ret;
Paul Bakker887bd502011-06-08 13:10:54 +00005694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005695 }
5696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 return (int) len;
Paul Bakker5121ce52009-01-03 21:22:43 +00005698}
5699
5700/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005701 * Write application data (public-facing wrapper)
5702 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005703int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005704{
Janos Follath865b3eb2019-12-16 11:46:15 +00005705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005708
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (ssl == NULL || ssl->conf == NULL) {
5710 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5711 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005712
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005713#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
5715 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5716 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005717 }
5718#endif
5719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5721 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5722 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5723 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005724 }
5725 }
5726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 ret = ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005728
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005732}
5733
5734/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005735 * Notify the peer that the connection is being closed
5736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005737int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005738{
Janos Follath865b3eb2019-12-16 11:46:15 +00005739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005740
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 if (ssl == NULL || ssl->conf == NULL) {
5742 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5743 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
5748 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5749 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5750 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
5751 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
5752 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005753 }
5754 }
5755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005759}
5760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Paul Bakker48916f92012-09-16 19:57:18 +00005762{
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 if (transform == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005764 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005766
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005767#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 psa_destroy_key(transform->psa_key_enc);
5769 psa_destroy_key(transform->psa_key_dec);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005770#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 mbedtls_cipher_free(&transform->cipher_ctx_enc);
5772 mbedtls_cipher_free(&transform->cipher_ctx_dec);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005773#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005774
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005775#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01005776#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 psa_destroy_key(transform->psa_mac_enc);
5778 psa_destroy_key(transform->psa_mac_dec);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01005779#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005780 mbedtls_md_free(&transform->md_ctx_enc);
5781 mbedtls_md_free(&transform->md_ctx_dec);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01005782#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd56ed242018-01-03 15:32:51 +00005783#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
Paul Bakker48916f92012-09-16 19:57:18 +00005786}
5787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
5789 mbedtls_ssl_transform *transform)
Jerry Yuc7875b52021-09-05 21:05:50 +08005790{
Jerry Yuc7875b52021-09-05 21:05:50 +08005791 ssl->transform_in = transform;
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuc7875b52021-09-05 21:05:50 +08005793}
5794
Gilles Peskine449bd832023-01-11 14:50:10 +01005795void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl,
5796 mbedtls_ssl_transform *transform)
Jerry Yuc7875b52021-09-05 21:05:50 +08005797{
5798 ssl->transform_out = transform;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yuc7875b52021-09-05 21:05:50 +08005800}
5801
Hanno Becker0271f962018-08-16 13:23:47 +01005802#if defined(MBEDTLS_SSL_PROTO_DTLS)
5803
Gilles Peskine449bd832023-01-11 14:50:10 +01005804void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
Hanno Becker0271f962018-08-16 13:23:47 +01005805{
5806 unsigned offset;
5807 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if (hs == NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01005810 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 }
Hanno Becker0271f962018-08-16 13:23:47 +01005812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 ssl_free_buffered_record(ssl);
Hanno Becker283f5ef2018-08-24 09:34:47 +01005814
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
5816 ssl_buffering_free_slot(ssl, offset);
5817 }
Hanno Beckere605b192018-08-21 15:59:07 +01005818}
5819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
5821 uint8_t slot)
Hanno Beckere605b192018-08-21 15:59:07 +01005822{
5823 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5824 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005825
Gilles Peskine449bd832023-01-11 14:50:10 +01005826 if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Beckerb309b922018-08-23 13:18:05 +01005827 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 }
Hanno Beckerb309b922018-08-23 13:18:05 +01005829
Gilles Peskine449bd832023-01-11 14:50:10 +01005830 if (hs_buf->is_valid == 1) {
Hanno Beckere605b192018-08-21 15:59:07 +01005831 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01005832 mbedtls_zeroize_and_free(hs_buf->data, hs_buf->data_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Hanno Becker0271f962018-08-16 13:23:47 +01005834 }
5835}
5836
5837#endif /* MBEDTLS_SSL_PROTO_DTLS */
5838
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005839/*
5840 * Convert version numbers to/from wire format
5841 * and, for DTLS, to/from TLS equivalent.
5842 *
5843 * For TLS this is the identity.
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005844 * For DTLS, map as follows, then use 1's complement (v -> ~v):
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005845 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005846 * DTLS 1.0 is stored as TLS 1.1 internally
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005848void mbedtls_ssl_write_version(unsigned char version[2], int transport,
5849 mbedtls_ssl_protocol_version tls_version)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005850{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005853 tls_version =
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 ~(tls_version - (tls_version == 0x0302 ? 0x0202 : 0x0201));
5855 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005856#else
5857 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005858#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 MBEDTLS_PUT_UINT16_BE(tls_version, version, 0);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005860}
5861
Gilles Peskine449bd832023-01-11 14:50:10 +01005862uint16_t mbedtls_ssl_read_version(const unsigned char version[2],
5863 int transport)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005864{
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 uint16_t tls_version = MBEDTLS_GET_UINT16_BE(version, 0);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005868 tls_version =
Gilles Peskine449bd832023-01-11 14:50:10 +01005869 ~(tls_version - (tls_version == 0xfeff ? 0x0202 : 0x0201));
5870 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005871#else
5872 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005873#endif
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005874 return tls_version;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005875}
5876
Jerry Yue7047812021-09-13 19:26:39 +08005877/*
Jerry Yu3bf1f972021-09-22 21:37:18 +08005878 * Send pending fatal alert.
5879 * 0, No alert message.
5880 * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
5881 * returned, ssl->alert_reason otherwise.
Jerry Yue7047812021-09-13 19:26:39 +08005882 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005883int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl)
Jerry Yue7047812021-09-13 19:26:39 +08005884{
5885 int ret;
5886
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005887 /* No pending alert, return success*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 if (ssl->send_alert == 0) {
5889 return 0;
5890 }
Jerry Yu394ece62021-09-14 22:17:21 +08005891
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 ret = mbedtls_ssl_send_alert_message(ssl,
5893 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5894 ssl->alert_type);
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005895
Jerry Yu3bf1f972021-09-22 21:37:18 +08005896 /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
5897 * do not clear the alert to be able to send it later.
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005898 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005900 ssl->send_alert = 0;
Jerry Yue7047812021-09-13 19:26:39 +08005901 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005902
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 if (ret != 0) {
5904 return ret;
5905 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 return ssl->alert_reason;
Jerry Yue7047812021-09-13 19:26:39 +08005908}
5909
Jerry Yu394ece62021-09-14 22:17:21 +08005910/*
5911 * Set pending fatal alert flag.
5912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005913void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl,
5914 unsigned char alert_type,
5915 int alert_reason)
Jerry Yu394ece62021-09-14 22:17:21 +08005916{
5917 ssl->send_alert = 1;
5918 ssl->alert_type = alert_type;
5919 ssl->alert_reason = alert_reason;
5920}
5921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005922#endif /* MBEDTLS_SSL_TLS_C */