blob: 1cad588077c64e95e37b51dd2d62577eea637909 [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)
43#include "mbedtls/psa_util.h"
44#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)
52#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
53 psa_to_ssl_errors, \
54 psa_generic_status_to_mbedtls)
55#endif
56
Gilles Peskine449bd832023-01-11 14:50:10 +010057static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020059/*
60 * Start a timer.
61 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020064{
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (ssl->f_set_timer == NULL) {
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020066 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 }
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs));
70 ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs);
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071}
72
73/*
74 * Return -1 is timer is expired, 0 if it isn't.
75 */
Gilles Peskine449bd832023-01-11 14:50:10 +010076int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (ssl->f_get_timer == NULL) {
79 return 0;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020080 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (ssl->f_get_timer(ssl->p_timer) == 2) {
83 MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired"));
84 return -1;
85 }
86
87 return 0;
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010091static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
92 unsigned char *buf,
93 size_t len,
94 mbedtls_record *rec);
TRodziewicz4ca18aa2021-05-20 14:46:20 +020095
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl,
97 unsigned char *buf,
98 size_t buflen)
TRodziewicz4ca18aa2021-05-20 14:46:20 +020099{
100 int ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record"));
102 MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200103
104 /* We don't support record checking in TLS because
TRodziewicz2abf03c2021-06-25 14:40:09 +0200105 * there doesn't seem to be a usecase for it.
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200108 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
109 goto exit;
110 }
111#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 else {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200113 mbedtls_record rec;
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 ret = ssl_parse_record_header(ssl, buf, buflen, &rec);
116 if (ret != 0) {
117 MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200118 goto exit;
119 }
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (ssl->transform_in != NULL) {
122 ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec);
123 if (ret != 0) {
124 MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200125 goto exit;
126 }
127 }
128 }
129#endif /* MBEDTLS_SSL_PROTO_DTLS */
130
131exit:
132 /* On success, we have decrypted the buffer in-place, so make
133 * sure we don't leak any plaintext data. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_platform_zeroize(buf, buflen);
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200135
136 /* For the purpose of this API, treat messages with unexpected CID
137 * as well as such from future epochs as unexpected. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
139 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200140 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
141 }
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record"));
144 return ret;
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200145}
146
Hanno Becker67bc7c32018-08-06 11:33:50 +0100147#define SSL_DONT_FORCE_FLUSH 0
148#define SSL_FORCE_FLUSH 1
149
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100151
Hanno Beckerd5847772018-08-28 10:09:23 +0100152/* Forward declarations for functions related to message buffering. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
154 uint8_t slot);
155static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200156MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100157static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100159static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200160MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100161static int ssl_buffer_message(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_buffer_future_record(mbedtls_ssl_context *ssl,
164 mbedtls_record const *rec);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200165MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100166static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
Hanno Beckerd5847772018-08-28 10:09:23 +0100167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100169{
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
Darryl Greenb33cc762019-11-28 14:29:44 +0000171#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
172 size_t out_buf_len = ssl->out_buf_len;
173#else
174 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
175#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (mtu != 0 && mtu < out_buf_len) {
178 return mtu;
179 }
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 return out_buf_len;
Hanno Becker2b1e3542018-08-06 11:19:13 +0100182}
183
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200184MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100186{
Hanno Becker11682cc2018-08-22 14:41:02 +0100187 size_t const bytes_written = ssl->out_left;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 size_t const mtu = ssl_get_maximum_datagram_size(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100189
190 /* Double-check that the write-index hasn't gone
191 * past what we can transmit in a single datagram. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (bytes_written > mtu) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100193 /* Should never happen... */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100195 }
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return (int) (mtu - bytes_written);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100198}
199
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200200MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100201static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100202{
Janos Follath865b3eb2019-12-16 11:46:15 +0000203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100204 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400205 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100206
207#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (max_len > mfl) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100213
214 /* By the standard (RFC 6066 Sect. 4), the MFL extension
215 * only limits the maximum record payload size, so in theory
216 * we would be allowed to pack multiple records of payload size
217 * MFL into a single datagram. However, this would mean that there's
218 * no way to explicitly communicate MTU restrictions to the peer.
219 *
220 * The following reduction of max_len makes sure that we never
221 * write datagrams larger than MFL + Record Expansion Overhead.
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (max_len <= ssl->out_left) {
224 return 0;
225 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100226
227 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100228#endif
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 ret = ssl_get_remaining_space_in_datagram(ssl);
231 if (ret < 0) {
232 return ret;
233 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100234 remaining = (size_t) ret;
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 ret = mbedtls_ssl_get_record_expansion(ssl);
237 if (ret < 0) {
238 return ret;
239 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240 expansion = (size_t) ret;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (remaining <= expansion) {
243 return 0;
244 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100245
246 remaining -= expansion;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (remaining >= max_len) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100248 remaining = max_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return (int) remaining;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100252}
253
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200254/*
255 * Double the retransmit timeout value, within the allowed range,
256 * returning -1 if the maximum value has already been reached.
257 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200258MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100259static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200260{
261 uint32_t new_timeout;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) {
264 return -1;
265 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200266
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200267 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
268 * in the following way: after the initial transmission and a first
269 * retransmission, back off to a temporary estimated MTU of 508 bytes.
270 * This value is guaranteed to be deliverable (if not guaranteed to be
271 * delivered) of any compliant IPv4 (and IPv6) network, and should work
272 * on most non-IP stacks too. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200274 ssl->handshake->mtu = 508;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu));
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400276 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200277
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200278 new_timeout = 2 * ssl->handshake->retransmit_timeout;
279
280 /* Avoid arithmetic overflow and range overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (new_timeout < ssl->handshake->retransmit_timeout ||
282 new_timeout > ssl->conf->hs_timeout_max) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200283 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284 }
285
286 ssl->handshake->retransmit_timeout = new_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
288 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 return 0;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200291}
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200294{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200295 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
297 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100301/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200303 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000304
Ronald Cron6f135e12021-12-08 16:57:54 +0100305#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker13996922020-05-28 16:15:19 +0100306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static size_t ssl_compute_padding_length(size_t len,
308 size_t granularity)
Hanno Becker13996922020-05-28 16:15:19 +0100309{
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return (granularity - (len + 1) % granularity) % granularity;
Hanno Becker13996922020-05-28 16:15:19 +0100311}
312
Hanno Becker581bc1b2020-05-04 12:20:03 +0100313/* This functions transforms a (D)TLS plaintext fragment and a record content
314 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
315 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
316 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100317 *
318 * struct {
319 * opaque content[DTLSPlaintext.length];
320 * ContentType real_type;
321 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100322 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100323 *
324 * Input:
325 * - `content`: The beginning of the buffer holding the
326 * plaintext to be wrapped.
327 * - `*content_size`: The length of the plaintext in Bytes.
328 * - `max_len`: The number of Bytes available starting from
329 * `content`. This must be `>= *content_size`.
330 * - `rec_type`: The desired record content type.
331 *
332 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100333 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
334 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100335 *
336 * Returns:
337 * - `0` on success.
338 * - A negative error code if `max_len` didn't offer enough space
339 * for the expansion.
340 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200341MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static int ssl_build_inner_plaintext(unsigned char *content,
343 size_t *content_size,
344 size_t remaining,
345 uint8_t rec_type,
346 size_t pad)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100347{
348 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100349
350 /* Write real content type */
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (remaining == 0) {
352 return -1;
353 }
354 content[len] = rec_type;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100355 len++;
356 remaining--;
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (remaining < pad) {
359 return -1;
360 }
361 memset(content + len, 0, pad);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100362 len += pad;
363 remaining -= pad;
364
365 *content_size = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100367}
368
Hanno Becker581bc1b2020-05-04 12:20:03 +0100369/* This function parses a (D)TLSInnerPlaintext structure.
370 * See ssl_build_inner_plaintext() for details. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100372static int ssl_parse_inner_plaintext(unsigned char const *content,
373 size_t *content_size,
374 uint8_t *rec_type)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100375{
376 size_t remaining = *content_size;
377
378 /* Determine length of padding by skipping zeroes from the back. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 do {
380 if (remaining == 0) {
381 return -1;
382 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100383 remaining--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 } while (content[remaining] == 0);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100385
386 *content_size = remaining;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 *rec_type = content[remaining];
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100390}
Ronald Cron6f135e12021-12-08 16:57:54 +0100391#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100392
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200393/* The size of the `add_data` structure depends on various
394 * factors, namely
395 *
396 * 1) CID functionality disabled
397 *
398 * additional_data =
399 * 8: seq_num +
400 * 1: type +
401 * 2: version +
402 * 2: length of inner plaintext +
403 *
404 * size = 13 bytes
405 *
406 * 2) CID functionality based on RFC 9146 enabled
407 *
408 * size = 8 + 1 + 1 + 1 + 2 + 2 + 6 + 2 + CID-length
409 * = 23 + CID-length
410 *
411 * 3) CID functionality based on legacy CID version
412 according to draft-ietf-tls-dtls-connection-id-05
413 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
414 *
415 * size = 13 + 1 + CID-length
416 *
417 * More information about the CID usage:
418 *
419 * Per Section 5.3 of draft-ietf-tls-dtls-connection-id-05 the
420 * size of the additional data structure is calculated as:
421 *
422 * additional_data =
423 * 8: seq_num +
424 * 1: tls12_cid +
425 * 2: DTLSCipherText.version +
426 * n: cid +
427 * 1: cid_length +
428 * 2: length_of_DTLSInnerPlaintext
429 *
430 * Per RFC 9146 the size of the add_data structure is calculated as:
431 *
432 * additional_data =
433 * 8: seq_num_placeholder +
434 * 1: tls12_cid +
435 * 1: cid_length +
436 * 1: tls12_cid +
437 * 2: DTLSCiphertext.version +
438 * 2: epoch +
439 * 6: sequence_number +
440 * n: cid +
441 * 2: length_of_DTLSInnerPlaintext
442 *
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444static void ssl_extract_add_data_from_record(unsigned char *add_data,
445 size_t *add_data_len,
446 mbedtls_record *rec,
447 mbedtls_ssl_protocol_version
448 tls_version,
449 size_t taglen)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000450{
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200451 /* Several types of ciphers have been defined for use with TLS and DTLS,
452 * and the MAC calculations for those ciphers differ slightly. Further
453 * variants were added when the CID functionality was added with RFC 9146.
454 * This implementations also considers the use of a legacy version of the
455 * CID specification published in draft-ietf-tls-dtls-connection-id-05,
456 * which is used in deployments.
457 *
458 * We will distinguish between the non-CID and the CID cases below.
459 *
460 * --- Non-CID cases ---
461 *
462 * Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100463 *
464 * additional_data = seq_num + TLSCompressed.type +
465 * TLSCompressed.version + TLSCompressed.length;
466 *
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100467 * For TLS 1.3, the record sequence number is dropped from the AAD
468 * and encoded within the nonce of the AEAD operation instead.
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000469 * Moreover, the additional data involves the length of the TLS
470 * ciphertext, not the TLS plaintext as in earlier versions.
471 * Quoting RFC 8446 (TLS 1.3):
472 *
473 * additional_data = TLSCiphertext.opaque_type ||
474 * TLSCiphertext.legacy_record_version ||
475 * TLSCiphertext.length
476 *
477 * We pass the tag length to this function in order to compute the
478 * ciphertext length from the inner plaintext length rec->data_len via
479 *
480 * TLSCiphertext.length = TLSInnerPlaintext.length + taglen.
481 *
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200482 * --- CID cases ---
483 *
484 * RFC 9146 uses a common pattern when constructing the data
485 * passed into a MAC / AEAD cipher.
486 *
487 * Data concatenation for MACs used with block ciphers with
488 * Encrypt-then-MAC Processing (with CID):
489 *
490 * data = seq_num_placeholder +
491 * tls12_cid +
492 * cid_length +
493 * tls12_cid +
494 * DTLSCiphertext.version +
495 * epoch +
496 * sequence_number +
497 * cid +
498 * DTLSCiphertext.length +
499 * IV +
500 * ENC(content + padding + padding_length)
501 *
502 * Data concatenation for MACs used with block ciphers (with CID):
503 *
504 * data = seq_num_placeholder +
505 * tls12_cid +
506 * cid_length +
507 * tls12_cid +
508 * DTLSCiphertext.version +
509 * epoch +
510 * sequence_number +
511 * cid +
512 * length_of_DTLSInnerPlaintext +
513 * DTLSInnerPlaintext.content +
514 * DTLSInnerPlaintext.real_type +
515 * DTLSInnerPlaintext.zeros
516 *
517 * AEAD ciphers use the following additional data calculation (with CIDs):
518 *
519 * additional_data = seq_num_placeholder +
520 * tls12_cid +
521 * cid_length +
522 * tls12_cid +
523 * DTLSCiphertext.version +
524 * epoch +
525 * sequence_number +
526 * cid +
527 * length_of_DTLSInnerPlaintext
528 *
529 * Section 5.3 of draft-ietf-tls-dtls-connection-id-05 (for legacy CID use)
530 * defines the additional data calculation as follows:
531 *
532 * additional_data = seq_num +
533 * tls12_cid +
534 * DTLSCipherText.version +
535 * cid +
536 * cid_length +
537 * length_of_DTLSInnerPlaintext
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 */
Hanno Beckercab87e62019-04-29 13:52:53 +0100539
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100540 unsigned char *cur = add_data;
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000541 size_t ad_len_field = rec->data_len;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100542
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200543#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
544 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
545 const unsigned char seq_num_placeholder[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
546#endif
547
Ronald Cron6f135e12021-12-08 16:57:54 +0100548#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000550 /* In TLS 1.3, the AAD contains the length of the TLSCiphertext,
551 * which differs from the length of the TLSInnerPlaintext
552 * by the length of the authentication tag. */
553 ad_len_field += taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 } else
Ronald Cron6f135e12021-12-08 16:57:54 +0100555#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100556 {
Glenn Strauss07c64162022-03-14 12:34:51 -0400557 ((void) tls_version);
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000558 ((void) taglen);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200559
Manuel Pégourié-Gonnard61336842022-11-25 11:12:38 +0100560#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
562 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200563 // seq_num_placeholder
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 memcpy(cur, seq_num_placeholder, sizeof(seq_num_placeholder));
565 cur += sizeof(seq_num_placeholder);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200566
567 // tls12_cid type
568 *cur = rec->type;
569 cur++;
570
571 // cid_length
572 *cur = rec->cid_len;
573 cur++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 } else
Manuel Pégourié-Gonnard61336842022-11-25 11:12:38 +0100575#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200576 {
577 // epoch + sequence number
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 memcpy(cur, rec->ctr, sizeof(rec->ctr));
579 cur += sizeof(rec->ctr);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200580 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100581 }
582
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200583 // type
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100584 *cur = rec->type;
585 cur++;
586
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200587 // version
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 memcpy(cur, rec->ver, sizeof(rec->ver));
589 cur += sizeof(rec->ver);
Hanno Beckercab87e62019-04-29 13:52:53 +0100590
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
592 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 1
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200595 // CID
596 memcpy(cur, rec->cid, rec->cid_len);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100597 cur += rec->cid_len;
598
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200599 // cid_length
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100600 *cur = rec->cid_len;
601 cur++;
602
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200603 // length of inner plaintext
604 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
605 cur += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 } else
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200607#elif defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
608 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (rec->cid_len != 0) {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200611 // epoch + sequence number
612 memcpy(cur, rec->ctr, sizeof(rec->ctr));
613 cur += sizeof(rec->ctr);
614
615 // CID
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 memcpy(cur, rec->cid, rec->cid_len);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200617 cur += rec->cid_len;
618
619 // length of inner plaintext
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100621 cur += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100623#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100624 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100626 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100627 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100628
629 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000630}
631
Hanno Becker67a37db2020-05-28 16:27:07 +0100632#if defined(MBEDTLS_GCM_C) || \
633 defined(MBEDTLS_CCM_C) || \
634 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200635MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker17263802020-05-28 07:05:48 +0100636static int ssl_transform_aead_dynamic_iv_is_explicit(
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 mbedtls_ssl_transform const *transform)
Hanno Beckerdf8be222020-05-21 15:30:57 +0100638{
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 return transform->ivlen != transform->fixed_ivlen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100640}
641
Hanno Becker17263802020-05-28 07:05:48 +0100642/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
643 *
644 * Concretely, this occurs in two variants:
645 *
646 * a) Fixed and dynamic IV lengths add up to total IV length, giving
647 * IV = fixed_iv || dynamic_iv
648 *
Hanno Becker15952812020-06-04 13:31:46 +0100649 * This variant is used in TLS 1.2 when used with GCM or CCM.
650 *
Hanno Becker17263802020-05-28 07:05:48 +0100651 * b) Fixed IV lengths matches total IV length, giving
652 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100653 *
654 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
655 *
656 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100657 *
658 * This function has the precondition that
659 *
660 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
661 *
662 * which has to be ensured by the caller. If this precondition
663 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665static void ssl_build_record_nonce(unsigned char *dst_iv,
666 size_t dst_iv_len,
667 unsigned char const *fixed_iv,
668 size_t fixed_iv_len,
669 unsigned char const *dynamic_iv,
670 size_t dynamic_iv_len)
Hanno Becker17263802020-05-28 07:05:48 +0100671{
Hanno Beckerdf8be222020-05-21 15:30:57 +0100672 /* Start with Fixed IV || 0 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 memset(dst_iv, 0, dst_iv_len);
674 memcpy(dst_iv, fixed_iv, fixed_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100675
Hanno Becker17263802020-05-28 07:05:48 +0100676 dst_iv += dst_iv_len - dynamic_iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 mbedtls_xor(dst_iv, dst_iv, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100678}
Hanno Becker67a37db2020-05-28 16:27:07 +0100679#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
682 mbedtls_ssl_transform *transform,
683 mbedtls_record *rec,
684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +0000686{
Neil Armstrong136f8402022-03-30 10:58:01 +0200687 mbedtls_ssl_mode_t ssl_mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100688 int auth_done = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 unsigned char *data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200690 /* For an explanation of the additional data length see
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 * the description of ssl_extract_add_data_from_record().
692 */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200693#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
694 unsigned char add_data[23 + MBEDTLS_SSL_CID_OUT_LEN_MAX];
695#else
696 unsigned char add_data[13];
697#endif
Hanno Beckercab87e62019-04-29 13:52:53 +0100698 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000699 size_t post_avail;
700
701 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000702#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200703 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000704 ((void) ssl);
705#endif
706
707 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200708 * for CBC transformations in TLS 1.2. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709#if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
710 defined(MBEDTLS_SSL_PROTO_TLS1_2))
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000711 ((void) f_rng);
712 ((void) p_rng);
713#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (transform == NULL) {
718 MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf"));
719 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000720 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (rec == NULL
Hanno Becker43c24b82019-05-01 09:45:57 +0100722 || rec->buf == NULL
723 || rec->buf_len < rec->data_offset
724 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100725#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100726 || rec->cid_len != 0
727#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 ) {
729 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf"));
730 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100731 }
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
Neil Armstrong136f8402022-03-30 10:58:01 +0200734
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000735 data = rec->buf + rec->data_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
737 MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload",
738 data, rec->data_len);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
741 MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET
742 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
743 rec->data_len,
744 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000746 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100747
Hanno Becker92313402020-05-20 13:58:58 +0100748 /* The following two code paths implement the (D)TLSInnerPlaintext
749 * structure present in TLS 1.3 and DTLS 1.2 + CID.
750 *
751 * See ssl_build_inner_plaintext() for more information.
752 *
753 * Note that this changes `rec->data_len`, and hence
754 * `post_avail` needs to be recalculated afterwards.
755 *
756 * Note also that the two code paths cannot occur simultaneously
757 * since they apply to different versions of the protocol. There
758 * is hence no risk of double-addition of the inner plaintext.
759 */
Ronald Cron6f135e12021-12-08 16:57:54 +0100760#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Becker13996922020-05-28 16:15:19 +0100762 size_t padding =
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 ssl_compute_padding_length(rec->data_len,
764 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
765 if (ssl_build_inner_plaintext(data,
766 &rec->data_len,
767 post_avail,
768 rec->type,
769 padding) != 0) {
770 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerccc13d02020-05-04 12:30:04 +0100771 }
772
773 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
774 }
Ronald Cron6f135e12021-12-08 16:57:54 +0100775#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100776
Hanno Beckera0e20d02019-05-15 14:03:01 +0100777#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100778 /*
779 * Add CID information
780 */
781 rec->cid_len = transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 memcpy(rec->cid, transform->out_cid, transform->out_cid_len);
783 MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (rec->cid_len != 0) {
Hanno Becker13996922020-05-28 16:15:19 +0100786 size_t padding =
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 ssl_compute_padding_length(rec->data_len,
788 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100789 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100790 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100791 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100792 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100793 * Note that this changes `rec->data_len`, and hence
794 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100795 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (ssl_build_inner_plaintext(data,
797 &rec->data_len,
798 post_avail,
799 rec->type,
800 padding) != 0) {
801 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100802 }
803
804 rec->type = MBEDTLS_SSL_MSG_CID;
805 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100806#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100809
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100811 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000813#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
815 ssl_mode == MBEDTLS_SSL_MODE_CBC) {
816 if (post_avail < transform->maclen) {
817 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
818 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000819 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200820#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +0200821 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong26e6d672022-02-23 09:30:33 +0100823#if defined(MBEDTLS_USE_PSA_CRYPTO)
824 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
825 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
826 size_t sign_mac_length = 0;
827#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker992b6872017-11-09 18:57:39 +0000828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
830 transform->tls_version,
831 transform->taglen);
Hanno Becker992b6872017-11-09 18:57:39 +0000832
Neil Armstrong26e6d672022-02-23 09:30:33 +0100833#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
835 transform->psa_mac_alg);
836 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100837 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 status = psa_mac_update(&operation, add_data, add_data_len);
841 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100842 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 status = psa_mac_update(&operation, data, rec->data_len);
846 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +0100847 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
851 &sign_mac_length);
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#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
857 add_data_len);
858 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100859 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 }
861 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len);
862 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100863 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 }
865 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
866 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100867 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 }
869 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
870 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100871 goto hmac_failed_etm_disabled;
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 }
Neil Armstrong26e6d672022-02-23 09:30:33 +0100873#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 memcpy(data + rec->data_len, mac, transform->maclen);
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200876#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len,
879 transform->maclen);
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200880
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000881 rec->data_len += transform->maclen;
882 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100883 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885hmac_failed_etm_disabled:
886 mbedtls_platform_zeroize(mac, transform->maclen);
Neil Armstrong26e6d672022-02-23 09:30:33 +0100887#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500888 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 status = psa_mac_abort(&operation);
890 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500891 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 }
Neil Armstrong4313f552022-03-02 15:14:07 +0100893#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (ret != 0) {
895 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
896 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100897 }
Paul Bakker577e0062013-08-28 11:57:20 +0200898 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000899#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200901 /*
902 * Encrypt
903 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000904#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
906 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
907 "including %d bytes of padding",
908 rec->data_len, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +0100910 /* The only supported stream cipher is "NULL",
911 * so there's nothing to do here.*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 } else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000913#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000914
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200915#if defined(MBEDTLS_GCM_C) || \
916 defined(MBEDTLS_CCM_C) || \
917 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200919 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100920 unsigned char *dynamic_iv;
921 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100922 int dynamic_iv_is_explicit =
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 ssl_transform_aead_dynamic_iv_is_explicit(transform);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100924#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +0100925 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100926#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000928
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100929 /* Check that there's space for the authentication tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (post_avail < transform->taglen) {
931 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
932 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000933 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000934
Paul Bakker68884e32013-01-07 18:20:04 +0100935 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100936 * Build nonce for AEAD encryption.
937 *
938 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
939 * part of the IV is prepended to the ciphertext and
940 * can be chosen freely - in particular, it need not
941 * agree with the record sequence number.
942 * However, since ChaChaPoly as well as all AEAD modes
943 * in TLS 1.3 use the record sequence number as the
944 * dynamic part of the nonce, we uniformly use the
945 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100946 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100947 dynamic_iv = rec->ctr;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 dynamic_iv_len = sizeof(rec->ctr);
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 ssl_build_record_nonce(iv, sizeof(iv),
951 transform->iv_enc,
952 transform->fixed_ivlen,
953 dynamic_iv,
954 dynamic_iv_len);
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100955
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100956 /*
957 * Build additional data for AEAD encryption.
958 * This depends on the TLS version.
959 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
961 transform->tls_version,
962 transform->taglen);
Hanno Becker1f10d762019-04-26 13:34:37 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)",
965 iv, transform->ivlen);
966 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)",
967 dynamic_iv,
968 dynamic_iv_is_explicit ? dynamic_iv_len : 0);
969 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
970 add_data, add_data_len);
971 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
972 "including 0 bytes of padding",
973 rec->data_len));
Paul Bakkerca4ab492012-04-18 14:23:57 +0000974
Paul Bakker68884e32013-01-07 18:20:04 +0100975 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200976 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200977 */
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100978#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 status = psa_aead_encrypt(transform->psa_key_enc,
980 transform->psa_alg,
981 iv, transform->ivlen,
982 add_data, add_data_len,
983 data, rec->data_len,
984 data, rec->buf_len - (data - rec->buf),
985 &rec->data_len);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500988 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret);
990 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100991 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100992#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc,
994 iv, transform->ivlen,
995 add_data, add_data_len,
996 data, rec->data_len, /* src */
997 data, rec->buf_len - (data - rec->buf), /* dst */
998 &rec->data_len,
999 transform->taglen)) != 0) {
1000 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt_ext", ret);
1001 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001002 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001003#endif /* MBEDTLS_USE_PSA_CRYPTO */
1004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
1006 data + rec->data_len - transform->taglen,
1007 transform->taglen);
Hanno Beckerdf8be222020-05-21 15:30:57 +01001008 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001009 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001010
1011 /*
1012 * Prefix record content with dynamic IV in case it is explicit.
1013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 if (dynamic_iv_is_explicit != 0) {
1015 if (rec->data_offset < dynamic_iv_len) {
1016 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1017 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001018 }
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +01001021 rec->data_offset -= dynamic_iv_len;
1022 rec->data_len += dynamic_iv_len;
1023 }
1024
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001025 auth_done++;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 } else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +01001027#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001028#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1030 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00001031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001032 size_t padlen, i;
1033 size_t olen;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001034#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001035 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001036 size_t part_len;
1037 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1038#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001039
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001040 /* Currently we're always using minimal padding
1041 * (up to 255 bytes would be allowed). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen;
1043 if (padlen == transform->ivlen) {
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 padlen = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001045 }
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 /* Check there's enough space in the buffer for the padding. */
1048 if (post_avail < padlen + 1) {
1049 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1050 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1051 }
1052
1053 for (i = 0; i <= padlen; i++) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001054 data[rec->data_len + i] = (unsigned char) padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001056
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001057 rec->data_len += padlen + 1;
1058 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001059
TRodziewicz0f82ec62021-05-12 17:49:18 +02001060#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001061 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +02001062 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +00001063 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001064 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (f_rng == NULL) {
1066 MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine"));
1067 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001068 }
TRodziewicz345165c2021-07-06 13:42:11 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (rec->data_offset < transform->ivlen) {
1071 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1072 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
TRodziewicz345165c2021-07-06 13:42:11 +02001073 }
1074
1075 /*
1076 * Generate IV
1077 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 ret = f_rng(p_rng, transform->iv_enc, transform->ivlen);
1079 if (ret != 0) {
1080 return ret;
1081 }
TRodziewicz345165c2021-07-06 13:42:11 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 memcpy(data - transform->ivlen, transform->iv_enc, transform->ivlen);
TRodziewicz0f82ec62021-05-12 17:49:18 +02001084#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
1087 "including %"
1088 MBEDTLS_PRINTF_SIZET
1089 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
1090 rec->data_len, transform->ivlen,
1091 padlen + 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001093#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 status = psa_cipher_encrypt_setup(&cipher_op,
1095 transform->psa_key_enc, transform->psa_alg);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001098 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_encrypt_setup", ret);
1100 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001101 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 status = psa_cipher_set_iv(&cipher_op, transform->iv_enc, transform->ivlen);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001106 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
1108 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001109
1110 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 status = psa_cipher_update(&cipher_op,
1113 data, rec->data_len,
1114 data, rec->data_len, &olen);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001117 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
1119 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001120
1121 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 status = psa_cipher_finish(&cipher_op,
1124 data + olen, rec->data_len - olen,
1125 &part_len);
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001128 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
1130 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001131
1132 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001133
1134 olen += part_len;
1135#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1137 transform->iv_enc,
1138 transform->ivlen,
1139 data, rec->data_len,
1140 data, &olen)) != 0) {
1141 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1142 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001143 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001144#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (rec->data_len != olen) {
1147 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1148 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001149 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001150
TRodziewicz0f82ec62021-05-12 17:49:18 +02001151 data -= transform->ivlen;
1152 rec->data_offset -= transform->ivlen;
1153 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (auth_done == 0) {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001157 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001158#if defined(MBEDTLS_USE_PSA_CRYPTO)
1159 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1160 size_t sign_mac_length = 0;
1161#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker3d8c9072018-01-05 16:24:22 +00001162
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001163 /* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length))
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001164 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (post_avail < transform->maclen) {
1167 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1168 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001169 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 ssl_extract_add_data_from_record(add_data, &add_data_len,
1172 rec, transform->tls_version,
1173 transform->taglen);
Hanno Becker1f10d762019-04-26 13:34:37 +01001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
1176 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1177 add_data_len);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001178#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
1180 transform->psa_mac_alg);
1181 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001182 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 status = psa_mac_update(&operation, add_data, add_data_len);
1186 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001187 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 status = psa_mac_update(&operation, data, rec->data_len);
1191 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001192 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 status = psa_mac_sign_finish(&operation, mac, MBEDTLS_SSL_MAC_ADD,
1196 &sign_mac_length);
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#else
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
1203 add_data_len);
1204 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001205 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
1207 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
1208 data, rec->data_len);
1209 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001210 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 }
1212 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
1213 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001214 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 }
1216 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
1217 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001218 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001220#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 memcpy(data + rec->data_len, mac, transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001223
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001224 rec->data_len += transform->maclen;
1225 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001226 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228hmac_failed_etm_enabled:
1229 mbedtls_platform_zeroize(mac, transform->maclen);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001231 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 status = psa_mac_abort(&operation);
1233 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001234 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 }
Neil Armstrong4313f552022-03-02 15:14:07 +01001236#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if (ret != 0) {
1238 MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
1239 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001240 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001241 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001244#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001245 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1247 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001248 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001250 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (auth_done != 1) {
1252 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1253 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001254 }
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259}
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1262 mbedtls_ssl_transform *transform,
1263 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00001264{
Przemek Stekiel4c499272022-09-27 13:55:37 +02001265#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) || defined(MBEDTLS_CIPHER_MODE_AEAD)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001266 size_t olen;
Przemek Stekiel4c499272022-09-27 13:55:37 +02001267#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC || MBEDTLS_CIPHER_MODE_AEAD */
Neil Armstrong136f8402022-03-30 10:58:01 +02001268 mbedtls_ssl_mode_t ssl_mode;
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001269 int ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001270
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001271 int auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001272#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001273 size_t padlen = 0, correct = 1;
1274#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 unsigned char *data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001276 /* For an explanation of the additional data length see
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 * the description of ssl_extract_add_data_from_record().
1278 */
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001279#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1280 unsigned char add_data[23 + MBEDTLS_SSL_CID_IN_LEN_MAX];
1281#else
1282 unsigned char add_data[13];
1283#endif
Hanno Beckercab87e62019-04-29 13:52:53 +01001284 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001285
Hanno Beckera18d1322018-01-03 14:27:32 +00001286#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001287 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001288 ((void) ssl);
1289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf"));
1292 if (rec == NULL ||
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001293 rec->buf == NULL ||
1294 rec->buf_len < rec->data_offset ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 rec->buf_len - rec->data_offset < rec->data_len) {
1296 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf"));
1297 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001298 }
1299
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001300 data = rec->buf + rec->data_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 ssl_mode = mbedtls_ssl_get_mode_from_transform(transform);
Paul Bakker5121ce52009-01-03 21:22:43 +00001302
Hanno Beckera0e20d02019-05-15 14:03:01 +01001303#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001304 /*
1305 * Match record's CID with incoming CID.
1306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (rec->cid_len != transform->in_cid_len ||
1308 memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) {
1309 return MBEDTLS_ERR_SSL_UNEXPECTED_CID;
Hanno Becker938489a2019-05-08 13:02:22 +01001310 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001311#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001312
Hanno Beckerd086bf02021-03-22 13:01:27 +00001313#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +01001315 /* The only supported stream cipher is "NULL",
1316 * so there's nothing to do here.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 } else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001318#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001319#if defined(MBEDTLS_GCM_C) || \
1320 defined(MBEDTLS_CCM_C) || \
1321 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001323 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001324 unsigned char *dynamic_iv;
1325 size_t dynamic_iv_len;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001326#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001327 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001328#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001329
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001330 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001331 * Extract dynamic part of nonce for AEAD decryption.
1332 *
1333 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1334 * part of the IV is prepended to the ciphertext and
1335 * can be chosen freely - in particular, it need not
1336 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 dynamic_iv_len = sizeof(rec->ctr);
1339 if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) {
1340 if (rec->data_len < dynamic_iv_len) {
1341 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1342 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
1343 rec->data_len,
1344 dynamic_iv_len));
1345 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001346 }
1347 dynamic_iv = data;
1348
1349 data += dynamic_iv_len;
1350 rec->data_offset += dynamic_iv_len;
1351 rec->data_len -= dynamic_iv_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 } else {
Hanno Becker17263802020-05-28 07:05:48 +01001353 dynamic_iv = rec->ctr;
1354 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001355
1356 /* Check that there's space for the authentication tag. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (rec->data_len < transform->taglen) {
1358 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1359 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
1360 rec->data_len,
1361 transform->taglen));
1362 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001363 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001364 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001365
Hanno Beckerdf8be222020-05-21 15:30:57 +01001366 /*
1367 * Prepare nonce from dynamic and static parts.
1368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 ssl_build_record_nonce(iv, sizeof(iv),
1370 transform->iv_dec,
1371 transform->fixed_ivlen,
1372 dynamic_iv,
1373 dynamic_iv_len);
Paul Bakker68884e32013-01-07 18:20:04 +01001374
Hanno Beckerdf8be222020-05-21 15:30:57 +01001375 /*
1376 * Build additional data for AEAD encryption.
1377 * This depends on the TLS version.
1378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1380 transform->tls_version,
1381 transform->taglen);
1382 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
1383 add_data, add_data_len);
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001384
Hanno Beckerd96a6522019-07-10 13:55:25 +01001385 /* Because of the check above, we know that there are
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001386 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001387 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001388 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001389 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen);
1392 MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len,
1393 transform->taglen);
Paul Bakker68884e32013-01-07 18:20:04 +01001394
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001395 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001396 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001397 */
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 status = psa_aead_decrypt(transform->psa_key_dec,
1400 transform->psa_alg,
1401 iv, transform->ivlen,
1402 add_data, add_data_len,
1403 data, rec->data_len + transform->taglen,
1404 data, rec->buf_len - (data - rec->buf),
1405 &olen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001408 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret);
1410 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001411 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001412#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec,
1414 iv, transform->ivlen,
1415 add_data, add_data_len,
1416 data, rec->data_len + transform->taglen, /* src */
1417 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
1418 transform->taglen)) != 0) {
1419 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret);
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
1422 return MBEDTLS_ERR_SSL_INVALID_MAC;
1423 }
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001426 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001427#endif /* MBEDTLS_USE_PSA_CRYPTO */
1428
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001429 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001430
Hanno Beckerd96a6522019-07-10 13:55:25 +01001431 /* Double-check that AEAD decryption doesn't change content length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if (olen != rec->data_len) {
1433 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1434 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001438#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1440 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001441 size_t minlen = 0;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001442#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001444 size_t part_len;
1445 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1446#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001447
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 /*
Paul Bakker45829992013-01-03 14:52:21 +01001449 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001452 /* The ciphertext is prefixed with the CBC IV. */
1453 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001454#endif
Paul Bakker45829992013-01-03 14:52:21 +01001455
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001456 /* Size considerations:
1457 *
1458 * - The CBC cipher text must not be empty and hence
1459 * at least of size transform->ivlen.
1460 *
1461 * Together with the potential IV-prefix, this explains
1462 * the first of the two checks below.
1463 *
1464 * - The record must contain a MAC, either in plain or
1465 * encrypted, depending on whether Encrypt-then-MAC
1466 * is used or not.
1467 * - If it is, the message contains the IV-prefix,
1468 * the CBC ciphertext, and the MAC.
1469 * - If it is not, the padded plaintext, and hence
1470 * the CBC ciphertext, has at least length maclen + 1
1471 * because there is at least the padding length byte.
1472 *
1473 * As the CBC ciphertext is not empty, both cases give the
1474 * lower bound minlen + maclen + 1 on the record size, which
1475 * we test for in the second check below.
1476 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if (rec->data_len < minlen + transform->ivlen ||
1478 rec->data_len < minlen + transform->maclen + 1) {
1479 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1480 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1481 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
1482 "+ 1 ) ( + expl IV )",
1483 rec->data_len,
1484 transform->ivlen,
1485 transform->maclen));
1486 return MBEDTLS_ERR_SSL_INVALID_MAC;
Paul Bakker45829992013-01-03 14:52:21 +01001487 }
1488
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001489 /*
1490 * Authenticate before decrypt if enabled
1491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001494#if defined(MBEDTLS_USE_PSA_CRYPTO)
1495 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1496#else
Hanno Becker992b6872017-11-09 18:57:39 +00001497 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001498#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001501
Hanno Beckerd96a6522019-07-10 13:55:25 +01001502 /* Update data_len in tandem with add_data.
1503 *
1504 * The subtraction is safe because of the previous check
1505 * data_len >= minlen + maclen + 1.
1506 *
1507 * Afterwards, we know that data + data_len is followed by at
1508 * least maclen Bytes, which justifies the call to
Gabor Mezei90437e32021-10-20 11:59:27 +02001509 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001510 *
1511 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001512 rec->data_len -= transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1514 transform->tls_version,
1515 transform->taglen);
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001516
Hanno Beckerd96a6522019-07-10 13:55:25 +01001517 /* Calculate expected MAC. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1519 add_data_len);
Neil Armstrong26e6d672022-02-23 09:30:33 +01001520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 status = psa_mac_verify_setup(&operation, transform->psa_mac_dec,
1522 transform->psa_mac_alg);
1523 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001524 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 status = psa_mac_update(&operation, add_data, add_data_len);
1528 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001529 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 status = psa_mac_update(&operation, data, rec->data_len);
1533 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001534 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001536
1537 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 status = psa_mac_verify_finish(&operation, data + rec->data_len,
1539 transform->maclen);
1540 if (status != PSA_SUCCESS) {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001541 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001543#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
1545 add_data_len);
1546 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001547 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 }
1549 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
1550 data, rec->data_len);
1551 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001552 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 }
1554 ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
1555 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001556 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 }
1558 ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
1559 if (ret != 0) {
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001560 goto hmac_failed_etm_enabled;
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 }
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
1564 transform->maclen);
1565 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
1566 transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001567
Hanno Beckerd96a6522019-07-10 13:55:25 +01001568 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
1570 transform->maclen) != 0) {
1571 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001572 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1573 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001574 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001575#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001576 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578hmac_failed_etm_enabled:
Neil Armstrong26e6d672022-02-23 09:30:33 +01001579#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001580 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 status = psa_mac_abort(&operation);
1582 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001583 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001585#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 mbedtls_platform_zeroize(mac_expect, transform->maclen);
Neil Armstrong4313f552022-03-02 15:14:07 +01001587#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ret != 0) {
1589 if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
1590 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
1591 }
1592 return ret;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001593 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001596
1597 /*
1598 * Check length sanity
1599 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001600
1601 /* We know from above that data_len > minlen >= 0,
1602 * so the following check in particular implies that
1603 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (rec->data_len % transform->ivlen != 0) {
1605 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1606 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
1607 rec->data_len, transform->ivlen));
1608 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001609 }
1610
TRodziewicz0f82ec62021-05-12 17:49:18 +02001611#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001612 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001613 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001614 */
TRodziewicz345165c2021-07-06 13:42:11 +02001615 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 memcpy(transform->iv_dec, data, transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001617
TRodziewicz345165c2021-07-06 13:42:11 +02001618 data += transform->ivlen;
1619 rec->data_offset += transform->ivlen;
1620 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001621#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001622
Hanno Beckerd96a6522019-07-10 13:55:25 +01001623 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1624
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001625#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 status = psa_cipher_decrypt_setup(&cipher_op,
1627 transform->psa_key_dec, transform->psa_alg);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001630 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_decrypt_setup", ret);
1632 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001633 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 status = psa_cipher_set_iv(&cipher_op, transform->iv_dec, transform->ivlen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001638 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_set_iv", ret);
1640 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001641 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 status = psa_cipher_update(&cipher_op,
1644 data, rec->data_len,
1645 data, rec->data_len, &olen);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001648 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_update", ret);
1650 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001651 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 status = psa_cipher_finish(&cipher_op,
1654 data + olen, rec->data_len - olen,
1655 &part_len);
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001658 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MBEDTLS_SSL_DEBUG_RET(1, "psa_cipher_finish", ret);
1660 return ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001661 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001662
1663 olen += part_len;
1664#else
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1667 transform->iv_dec, transform->ivlen,
1668 data, rec->data_len, data, &olen)) != 0) {
1669 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1670 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001671 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001672#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001673
Hanno Beckerd96a6522019-07-10 13:55:25 +01001674 /* Double-check that length hasn't changed during decryption. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (rec->data_len != olen) {
1676 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1677 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001678 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001679
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001680 /* Safe since data_len >= minlen + maclen + 1, so after having
1681 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001682 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1683 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001684 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 if (auth_done == 1) {
Gabor Mezei90437e32021-10-20 11:59:27 +02001687 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 rec->data_len,
1689 padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001690 correct &= mask;
1691 padlen &= mask;
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if (rec->data_len < transform->maclen + padlen + 1) {
1695 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1696 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1697 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
1698 rec->data_len,
1699 transform->maclen,
1700 padlen + 1));
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001701 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001702#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001703
Gabor Mezei90437e32021-10-20 11:59:27 +02001704 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 rec->data_len,
1706 transform->maclen + padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001707 correct &= mask;
1708 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001711 padlen++;
1712
1713 /* Regardless of the validity of the padding,
1714 * we have data_len >= padlen here. */
1715
TRodziewicz0f82ec62021-05-12 17:49:18 +02001716#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001717 /* The padding check involves a series of up to 256
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 * consecutive memory reads at the end of the record
1719 * plaintext buffer. In order to hide the length and
1720 * validity of the padding, always perform exactly
1721 * `min(256,plaintext_len)` reads (but take into account
1722 * only the last `padlen` bytes for the padding check). */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001723 size_t pad_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 volatile unsigned char * const check = data;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001725
1726 /* Index of first padding byte; it has been ensured above
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 * that the subtraction is safe. */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001728 size_t const padding_idx = rec->data_len - padlen;
1729 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1730 size_t const start_idx = rec->data_len - num_checks;
1731 size_t idx;
1732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 for (idx = start_idx; idx < rec->data_len; idx++) {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001734 /* pad_count += (idx >= padding_idx) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 * (check[idx] == padlen - 1);
1736 */
1737 const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx);
1738 const size_t equal = mbedtls_ct_size_bool_eq(check[idx],
1739 padlen - 1);
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001740 pad_count += mask & equal;
1741 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 correct &= mbedtls_ct_size_bool_eq(pad_count, padlen);
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (padlen > 0 && correct == 0) {
1746 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
1747 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001748#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 padlen &= mbedtls_ct_size_mask(correct);
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001750
TRodziewicz0f82ec62021-05-12 17:49:18 +02001751#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001752
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001753 /* If the padding was found to be invalid, padlen == 0
1754 * and the subtraction is safe. If the padding was found valid,
1755 * padlen hasn't been changed and the previous assertion
1756 * data_len >= padlen still holds. */
1757 rec->data_len -= padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001759#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001760 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1762 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001763 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001765#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
1767 data, rec->data_len);
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001768#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001769
1770 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001771 * Authenticate if not done yet.
1772 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001774#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if (auth_done == 0) {
Paul Elliott5260ce22022-05-09 18:15:54 +01001776 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
1777 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01001778
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001779 /* If the initial value of padlen was such that
1780 * data_len < maclen + padlen + 1, then padlen
1781 * got reset to 1, and the initial check
1782 * data_len >= minlen + maclen + 1
1783 * guarantees that at this point we still
1784 * have at least data_len >= maclen.
1785 *
1786 * If the initial value of padlen was such that
1787 * data_len >= maclen + padlen + 1, then we have
1788 * subtracted either padlen + 1 (if the padding was correct)
1789 * or 0 (if the padding was incorrect) since then,
1790 * hence data_len >= maclen in any case.
1791 */
1792 rec->data_len -= transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1794 transform->tls_version,
1795 transform->taglen);
Paul Bakker5121ce52009-01-03 21:22:43 +00001796
TRodziewicz0f82ec62021-05-12 17:49:18 +02001797#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001798 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 * The next two sizes are the minimum and maximum values of
1800 * data_len over all padlen values.
1801 *
1802 * They're independent of padlen, since we previously did
1803 * data_len -= padlen.
1804 *
1805 * Note that max_len + maclen is never more than the buffer
1806 * length, as we previously did in_msglen -= maclen too.
1807 */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001808 const size_t max_len = rec->data_len + padlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001810
Neil Armstronge8589962022-02-25 15:14:29 +01001811#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 ret = mbedtls_ct_hmac(transform->psa_mac_dec,
1813 transform->psa_mac_alg,
1814 add_data, add_data_len,
1815 data, rec->data_len, min_len, max_len,
1816 mac_expect);
Neil Armstronge8589962022-02-25 15:14:29 +01001817#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
1819 add_data, add_data_len,
1820 data, rec->data_len, min_len, max_len,
1821 mac_expect);
Neil Armstronge8589962022-02-25 15:14:29 +01001822#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 if (ret != 0) {
1824 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001825 goto hmac_failed_etm_disabled;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001826 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 mbedtls_ct_memcpy_offset(mac_peer, data,
1829 rec->data_len,
1830 min_len, max_len,
1831 transform->maclen);
TRodziewicz0f82ec62021-05-12 17:49:18 +02001832#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001834#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen);
1836 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen);
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001837#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (mbedtls_ct_memcmp(mac_peer, mac_expect,
1840 transform->maclen) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Paul Bakkere47b34b2013-02-27 14:48:00 +01001843#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 correct = 0;
1845 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001846 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848hmac_failed_etm_disabled:
1849 mbedtls_platform_zeroize(mac_peer, transform->maclen);
1850 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1851 if (ret != 0) {
1852 return ret;
1853 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001855
1856 /*
1857 * Finally check the correct flag
1858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (correct == 0) {
1860 return MBEDTLS_ERR_SSL_INVALID_MAC;
1861 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001862#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001863
1864 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (auth_done != 1) {
1866 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1867 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Ronald Cron6f135e12021-12-08 16:57:54 +01001870#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Beckerccc13d02020-05-04 12:30:04 +01001872 /* Remove inner padding and infer true content type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1874 &rec->type);
Hanno Beckerccc13d02020-05-04 12:30:04 +01001875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (ret != 0) {
1877 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1878 }
Hanno Beckerccc13d02020-05-04 12:30:04 +01001879 }
Ronald Cron6f135e12021-12-08 16:57:54 +01001880#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +01001881
Hanno Beckera0e20d02019-05-15 14:03:01 +01001882#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if (rec->cid_len != 0) {
1884 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1885 &rec->type);
1886 if (ret != 0) {
1887 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1888 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001889 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001890#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001895}
1896
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001897#undef MAC_NONE
1898#undef MAC_PLAINTEXT
1899#undef MAC_CIPHERTEXT
1900
Paul Bakker5121ce52009-01-03 21:22:43 +00001901/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001902 * Fill the input message buffer by appending data to it.
1903 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001904 *
1905 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1906 * available (from this read and/or a previous one). Otherwise, an error code
1907 * is returned (possibly EOF or WANT_READ).
1908 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001909 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1910 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1911 * since we always read a whole datagram at once.
1912 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001913 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001914 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001916int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
Paul Bakker5121ce52009-01-03 21:22:43 +00001917{
Janos Follath865b3eb2019-12-16 11:46:15 +00001918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001919 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001920#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1921 size_t in_buf_len = ssl->in_buf_len;
1922#else
1923 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1924#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) {
1929 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
1930 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001931 }
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
1934 MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits"));
1935 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001936 }
1937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001940 uint32_t timeout;
1941
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001942 /*
1943 * The point is, we need to always read a full datagram at once, so we
1944 * sometimes read more then requested, and handle the additional data.
1945 * It could be the rest of the current record (while fetching the
1946 * header) and/or some other records in the same datagram.
1947 */
1948
1949 /*
1950 * Move to the next record in the already read datagram if applicable
1951 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (ssl->next_record_offset != 0) {
1953 if (ssl->in_left < ssl->next_record_offset) {
1954 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1955 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001956 }
1957
1958 ssl->in_left -= ssl->next_record_offset;
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (ssl->in_left != 0) {
1961 MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %"
1962 MBEDTLS_PRINTF_SIZET,
1963 ssl->next_record_offset));
1964 memmove(ssl->in_hdr,
1965 ssl->in_hdr + ssl->next_record_offset,
1966 ssl->in_left);
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001967 }
1968
1969 ssl->next_record_offset = 0;
1970 }
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1973 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1974 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001975
1976 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001977 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (nb_want <= ssl->in_left) {
1980 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
1981 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001982 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001983
1984 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001985 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001986 * are not at the beginning of a new record, the caller did something
1987 * wrong.
1988 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if (ssl->in_left != 0) {
1990 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1991 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001992 }
1993
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001994 /*
1995 * Don't even try to read if time's out already.
1996 * This avoids by-passing the timer when repeatedly receiving messages
1997 * that will end up being dropped.
1998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if (mbedtls_ssl_check_timer(ssl) != 0) {
2000 MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002001 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 } else {
2003 len = in_buf_len - (ssl->in_hdr - ssl->in_buf);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002006 timeout = ssl->handshake->retransmit_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 } else {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002008 timeout = ssl->conf->read_timeout;
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (ssl->f_recv_timeout != NULL) {
2014 ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len,
2015 timeout);
2016 } else {
2017 ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len);
2018 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 if (ret == 0) {
2023 return MBEDTLS_ERR_SSL_CONN_EOF;
2024 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002025 }
2026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
2028 MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
2029 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
2032 if (ssl_double_retransmit_timeout(ssl) != 0) {
2033 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout"));
2034 return MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002035 }
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
2038 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
2039 return ret;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002040 }
2041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002043 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
2046 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
2047 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
2048 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
2049 ret);
2050 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002051 }
2052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002055#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002056 }
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (ret < 0) {
2059 return ret;
2060 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002062 ssl->in_left = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 } else
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002064#endif
2065 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
2067 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
2068 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002069
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 while (ssl->in_left < nb_want) {
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002071 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (mbedtls_ssl_check_timer(ssl) != 0) {
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002074 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 } else {
2076 if (ssl->f_recv_timeout != NULL) {
2077 ret = ssl->f_recv_timeout(ssl->p_bio,
2078 ssl->in_hdr + ssl->in_left, len,
2079 ssl->conf->read_timeout);
2080 } else {
2081 ret = ssl->f_recv(ssl->p_bio,
2082 ssl->in_hdr + ssl->in_left, len);
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002083 }
2084 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
2087 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
2088 ssl->in_left, nb_want));
2089 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (ret == 0) {
2092 return MBEDTLS_ERR_SSL_CONN_EOF;
2093 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (ret < 0) {
2096 return ret;
2097 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002098
Dave Rodgman4a5c9ee2023-02-10 16:03:44 +00002099 if ((size_t) ret > len) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 MBEDTLS_SSL_DEBUG_MSG(1,
2101 ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2102 " were requested",
2103 ret, len));
2104 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16035bd15cb2018-02-28 04:30:59 -08002105 }
2106
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002107 ssl->in_left += ret;
2108 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 }
2110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002114}
2115
2116/*
2117 * Flush any data not yet written
2118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002119int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002120{
Janos Follath865b3eb2019-12-16 11:46:15 +00002121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002122 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (ssl->f_send == NULL) {
2127 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
2128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002129 }
2130
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002131 /* Avoid incrementing counter if data is flushed */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (ssl->out_left == 0) {
2133 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
2134 return 0;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002135 }
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 while (ssl->out_left > 0) {
2138 MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET
2139 ", out_left: %" MBEDTLS_PRINTF_SIZET,
2140 mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left));
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Hanno Becker2b1e3542018-08-06 11:19:13 +01002142 buf = ssl->out_hdr - ssl->out_left;
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
Paul Bakker186751d2012-05-08 13:16:14 +00002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (ret <= 0) {
2148 return ret;
2149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Dave Rodgman4a5c9ee2023-02-10 16:03:44 +00002151 if ((size_t) ret > ssl->out_left) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 MBEDTLS_SSL_DEBUG_MSG(1,
2153 ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2154 " bytes were sent",
2155 ret, ssl->out_left));
2156 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16034bbaeb42018-02-22 04:29:04 -08002157 }
2158
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 ssl->out_left -= ret;
2160 }
2161
Hanno Becker2b1e3542018-08-06 11:19:13 +01002162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002164 ssl->out_hdr = ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 } else
Hanno Becker2b1e3542018-08-06 11:19:13 +01002166#endif
2167 {
2168 ssl->out_hdr = ssl->out_buf + 8;
2169 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002175}
2176
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002177/*
2178 * Functions to handle the DTLS retransmission state machine
2179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002181/*
2182 * Append current handshake message to current outgoing flight
2183 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002184MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002185static int ssl_flight_append(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 mbedtls_ssl_flight_item *msg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append"));
2189 MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight",
2190 ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002191
2192 /* Allocate space for current message */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) {
2194 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2195 sizeof(mbedtls_ssl_flight_item)));
2196 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002197 }
2198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) {
2200 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2201 ssl->out_msglen));
2202 mbedtls_free(msg);
2203 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002204 }
2205
2206 /* Copy current handshake message with headers */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002208 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002209 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002210 msg->next = NULL;
2211
2212 /* Append to the current flight */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (ssl->handshake->flight == NULL) {
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002214 ssl->handshake->flight = msg;
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002216 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 while (cur->next != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002218 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002220 cur->next = msg;
2221 }
2222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
2224 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002225}
2226
2227/*
2228 * Free the current flight of handshake messages
2229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002230void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002231{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_ssl_flight_item *cur = flight;
2233 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 while (cur != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002236 next = cur->next;
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 mbedtls_free(cur->p);
2239 mbedtls_free(cur);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002240
2241 cur = next;
2242 }
2243}
2244
2245/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002246 * Swap transform_out and out_ctr with the alternative ones
2247 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002249static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002251 mbedtls_ssl_transform *tmp_transform;
Jerry Yuae0b2e22021-10-08 15:21:19 +08002252 unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (ssl->transform_out == ssl->handshake->alt_transform_out) {
2255 MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
2256 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002257 }
2258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002260
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002261 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002262 tmp_transform = ssl->transform_out;
2263 ssl->transform_out = ssl->handshake->alt_transform_out;
2264 ssl->handshake->alt_transform_out = tmp_transform;
2265
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002266 /* Swap epoch + sequence_number */
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 memcpy(tmp_out_ctr, ssl->cur_out_ctr, sizeof(tmp_out_ctr));
2268 memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr,
2269 sizeof(ssl->cur_out_ctr));
2270 memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr,
2271 sizeof(ssl->handshake->alt_out_ctr));
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002272
2273 /* Adjust to the newly activated transform */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002277}
2278
2279/*
2280 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002283{
2284 int ret = 0;
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 ret = mbedtls_ssl_flight_transmit(ssl);
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002293}
2294
2295/*
2296 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002297 *
2298 * Need to remember the current message in case flush_output returns
2299 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002300 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002302int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002303{
Janos Follath865b3eb2019-12-16 11:46:15 +00002304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
2308 MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002309
2310 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002311 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 ret = ssl_swap_epochs(ssl);
2313 if (ret != 0) {
2314 return ret;
2315 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002318 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 while (ssl->handshake->cur_msg != NULL) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002321 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002322 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002323
Hanno Beckere1dcb032018-08-17 16:47:58 +01002324 int const is_finished =
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2326 cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
Hanno Beckere1dcb032018-08-17 16:47:58 +01002327
Ronald Cron00d012f22022-03-08 15:57:12 +01002328 int const force_flush = ssl->disable_datagram_packing == 1 ?
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
Hanno Becker04da1892018-08-14 13:22:10 +01002330
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002331 /* Swap epochs before sending Finished: we can't do it after
2332 * sending ChangeCipherSpec, in case write returns WANT_READ.
2333 * Must be done before copying, may change out_msg pointer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) {
2335 MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message"));
2336 ret = ssl_swap_epochs(ssl);
2337 if (ret != 0) {
2338 return ret;
2339 }
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002340 }
2341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 ret = ssl_get_remaining_payload_in_datagram(ssl);
2343 if (ret < 0) {
2344 return ret;
2345 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002346 max_frag_len = (size_t) ret;
2347
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002348 /* CCS is copied as is, while HS messages may need fragmentation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2350 if (max_frag_len == 0) {
2351 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2352 return ret;
2353 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002354
2355 continue;
2356 }
2357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 memcpy(ssl->out_msg, cur->p, cur->len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002359 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002360 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002361
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002362 /* Update position inside current message */
2363 ssl->handshake->cur_msg_p += cur->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002365 const unsigned char * const p = ssl->handshake->cur_msg_p;
2366 const size_t hs_len = cur->len - 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 const size_t frag_off = p - (cur->p + 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002368 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002369 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) {
2372 if (is_finished) {
2373 ret = ssl_swap_epochs(ssl);
2374 if (ret != 0) {
2375 return ret;
2376 }
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002377 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002378
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2380 return ret;
2381 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002382
2383 continue;
2384 }
2385 max_hs_frag_len = max_frag_len - 12;
2386
2387 cur_hs_frag_len = rem_len > max_hs_frag_len ?
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 max_hs_frag_len : rem_len;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 if (frag_off == 0 && cur_hs_frag_len != hs_len) {
2391 MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)",
2392 (unsigned) cur_hs_frag_len,
2393 (unsigned) max_hs_frag_len));
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002394 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002395
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002396 /* Messages are stored with handshake headers as if not fragmented,
2397 * copy beginning of headers then fill fragmentation fields.
2398 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 memcpy(ssl->out_msg, cur->p, 6);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off);
2402 ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off);
2403 ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len);
2406 ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len);
2407 ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002410
Hanno Becker3f7b9732018-08-28 09:53:25 +01002411 /* Copy the handshake message content and set records fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002413 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002414 ssl->out_msgtype = cur->type;
2415
2416 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002417 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002418 }
2419
2420 /* If done with the current message move to the next one if any */
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
2422 if (cur->next != NULL) {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002423 ssl->handshake->cur_msg = cur->next;
2424 ssl->handshake->cur_msg_p = cur->next->p + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002426 ssl->handshake->cur_msg = NULL;
2427 ssl->handshake->cur_msg_p = NULL;
2428 }
2429 }
2430
2431 /* Actually send the message out */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2433 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
2434 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002435 }
2436 }
2437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2439 return ret;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002440 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 /* Update state and set timer */
2443 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
2444 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
2445 } else {
2446 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
2447 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
2448 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
2451
2452 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002453}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002454
2455/*
2456 * To be called when the last message of an incoming flight is received.
2457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002458void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002459{
2460 /* We won't need to resend that one any more */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 mbedtls_ssl_flight_free(ssl->handshake->flight);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002462 ssl->handshake->flight = NULL;
2463 ssl->handshake->cur_msg = NULL;
2464
2465 /* The next incoming flight will start with this msg_seq */
2466 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2467
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002468 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002469 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002470
Hanno Becker0271f962018-08-16 13:23:47 +01002471 /* Clear future message buffering structure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 mbedtls_ssl_buffering_free(ssl);
Hanno Becker0271f962018-08-16 13:23:47 +01002473
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002474 /* Cancel timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2478 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002483}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002484
2485/*
2486 * To be called when the last message of an outgoing flight is send.
2487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002488void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002489{
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 ssl_reset_retransmit_timeout(ssl);
2491 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2494 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002501
Paul Bakker5121ce52009-01-03 21:22:43 +00002502/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002503 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002505int mbedtls_ssl_start_handshake_msg(mbedtls_ssl_context *ssl, unsigned hs_type,
2506 unsigned char **buf, size_t *buf_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002507{
2508 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002509 * Reserve 4 bytes for handshake header. ( Section 4,RFC 8446 )
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002510 * ...
2511 * HandshakeType msg_type;
2512 * uint24 length;
2513 * ...
2514 */
2515 *buf = ssl->out_msg + 4;
2516 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
2517
2518 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2519 ssl->out_msg[0] = hs_type;
2520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 return 0;
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002522}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002523
2524/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002525 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002526 *
2527 * - fill in handshake headers
2528 * - update handshake checksum
2529 * - DTLS: save message for resending
2530 * - then pass to the record layer
2531 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002532 * DTLS: except for HelloRequest, messages are only queued, and will only be
2533 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002534 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002535 * Inputs:
2536 * - ssl->out_msglen: 4 + actual handshake message len
2537 * (4 is the size of handshake headers for TLS)
2538 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2539 * - ssl->out_msg + 4: the handshake message body
2540 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002541 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002542 * - ssl->out_msglen: the length of the record contents
2543 * (including handshake headers but excluding record headers)
2544 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002546int mbedtls_ssl_write_handshake_msg_ext(mbedtls_ssl_context *ssl,
2547 int update_checksum,
2548 int force_flush)
Paul Bakker5121ce52009-01-03 21:22:43 +00002549{
Janos Follath865b3eb2019-12-16 11:46:15 +00002550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002551 const size_t hs_len = ssl->out_msglen - 4;
2552 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002555
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002556 /*
2557 * Sanity checks
2558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
2560 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2561 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2562 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002563 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002565 /* Whenever we send anything different from a
2566 * HelloRequest we should be in a handshake - double check. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2568 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) &&
2569 ssl->handshake == NULL) {
2570 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2571 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002572 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002576 ssl->handshake != NULL &&
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
2578 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2579 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002580 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002581#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002582
Hanno Beckerb50a2532018-08-06 11:52:54 +01002583 /* Double-check that we did not exceed the bounds
2584 * of the outgoing record buffer.
2585 * This should never fail as the various message
2586 * writing functions must obey the bounds of the
2587 * outgoing record buffer, but better be safe.
2588 *
2589 * Note: We deliberately do not check for the MTU or MFL here.
2590 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2592 MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: "
2593 "size %" MBEDTLS_PRINTF_SIZET
2594 ", maximum %" MBEDTLS_PRINTF_SIZET,
2595 ssl->out_msglen,
2596 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2597 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerb50a2532018-08-06 11:52:54 +01002598 }
2599
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002600 /*
2601 * Fill handshake headers
2602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
2604 ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len);
2605 ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len);
2606 ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002608 /*
2609 * DTLS has additional fields in the Handshake layer,
2610 * between the length field and the actual payload:
2611 * uint16 message_seq;
2612 * uint24 fragment_offset;
2613 * uint24 fragment_length;
2614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002617 /* Make room for the additional DTLS fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) {
2619 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: "
2620 "size %" MBEDTLS_PRINTF_SIZET ", maximum %"
2621 MBEDTLS_PRINTF_SIZET,
2622 hs_len,
2623 (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12)));
2624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9648f8b2017-09-18 10:55:54 +01002625 }
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002628 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002629
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002630 /* Write message_seq and update it, except for HelloRequest */
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2632 MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4);
2633 ++(ssl->handshake->out_msg_seq);
2634 } else {
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002635 ssl->out_msg[4] = 0;
2636 ssl->out_msg[5] = 0;
2637 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002638
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002639 /* Handshake hashes are computed without fragmentation,
2640 * so set frag_offset = 0 and frag_len = hs_len for now */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 memset(ssl->out_msg + 6, 0x00, 3);
2642 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002643 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002645
Hanno Becker0207e532018-08-28 10:28:28 +01002646 /* Update running hashes of handshake messages seen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002648 ret = ssl->handshake->update_checksum(ssl, ssl->out_msg,
2649 ssl->out_msglen);
2650 if (ret != 0) {
2651 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
2652 return ret;
2653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002657 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2660 !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2661 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) {
2662 if ((ret = ssl_flight_append(ssl)) != 0) {
2663 MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret);
2664 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002665 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 } else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002667#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002668 {
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2670 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret);
2671 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002672 }
2673 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 return 0;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002678}
2679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680int mbedtls_ssl_finish_handshake_msg(mbedtls_ssl_context *ssl,
2681 size_t buf_len, size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002682{
2683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2684 size_t msg_with_header_len;
2685 ((void) buf_len);
2686
2687 /* Add reserved 4 bytes for handshake header */
2688 msg_with_header_len = msg_len + 4;
2689 ssl->out_msglen = msg_with_header_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_handshake_msg_ext(ssl, 0, 0));
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002691
2692cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 return ret;
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002694}
2695
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002696/*
2697 * Record layer functions
2698 */
2699
2700/*
2701 * Write current record.
2702 *
2703 * Uses:
2704 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2705 * - ssl->out_msglen: length of the record content (excl headers)
2706 * - ssl->out_msg: record content
2707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002708int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, int force_flush)
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002709{
2710 int ret, done = 0;
2711 size_t len = ssl->out_msglen;
Ronald Cron00d012f22022-03-08 15:57:12 +01002712 int flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (!done) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002717 unsigned i;
2718 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002719#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2720 size_t out_buf_len = ssl->out_buf_len;
2721#else
2722 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2723#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002724 /* Skip writing the record content type to after the encryption,
2725 * as it may change when using the CID extension. */
Glenn Strauss60bfe602022-03-14 19:04:24 -04002726 mbedtls_ssl_protocol_version tls_ver = ssl->tls_version;
Ronald Cron6f135e12021-12-08 16:57:54 +01002727#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ca80f72021-11-08 10:30:54 +08002728 /* TLS 1.3 still uses the TLS 1.2 version identifier
2729 * for backwards compatibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (tls_ver == MBEDTLS_SSL_VERSION_TLS1_3) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04002731 tls_ver = MBEDTLS_SSL_VERSION_TLS1_2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 }
Ronald Cron6f135e12021-12-08 16:57:54 +01002733#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 mbedtls_ssl_write_version(ssl->out_hdr + 1, ssl->conf->transport,
2735 tls_ver);
Hanno Becker6430faf2019-05-08 11:57:13 +01002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 memcpy(ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
2738 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if (ssl->transform_out != NULL) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002741 mbedtls_record rec;
2742
2743 rec.buf = ssl->out_iv;
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002745 rec.data_len = ssl->out_msglen;
2746 rec.data_offset = ssl->out_msg - rec.buf;
2747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 memcpy(&rec.ctr[0], ssl->out_ctr, sizeof(rec.ctr));
2749 mbedtls_ssl_write_version(rec.ver, ssl->conf->transport, tls_ver);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002750 rec.type = ssl->out_msgtype;
2751
Hanno Beckera0e20d02019-05-15 14:03:01 +01002752#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002753 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002754 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002755#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002756
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec,
2758 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2759 MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret);
2760 return ret;
Paul Bakker05ef8352012-05-08 09:17:57 +00002761 }
2762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (rec.data_offset != 0) {
2764 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2765 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002766 }
2767
Hanno Becker6430faf2019-05-08 11:57:13 +01002768 /* Update the record content type and CID. */
2769 ssl->out_msgtype = rec.type;
Gilles Peskine449bd832023-01-11 14:50:10 +01002770#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2771 memcpy(ssl->out_cid, rec.cid, rec.cid_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01002772#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002773 ssl->out_msglen = len = rec.data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002775 }
2776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002778
2779#if defined(MBEDTLS_SSL_PROTO_DTLS)
2780 /* In case of DTLS, double-check that we don't exceed
2781 * the remaining space in the datagram. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2783 ret = ssl_get_remaining_space_in_datagram(ssl);
2784 if (ret < 0) {
2785 return ret;
2786 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 if (protected_record_size > (size_t) ret) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002789 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker2b1e3542018-08-06 11:19:13 +01002791 }
2792 }
2793#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002794
Hanno Becker6430faf2019-05-08 11:57:13 +01002795 /* Now write the potentially updated record content type. */
2796 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, "
2799 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
2800 ssl->out_hdr[0], ssl->out_hdr[1],
2801 ssl->out_hdr[2], len));
Paul Bakker05ef8352012-05-08 09:17:57 +00002802
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
2804 ssl->out_hdr, protected_record_size);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002805
2806 ssl->out_left += protected_record_size;
2807 ssl->out_hdr += protected_record_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
2811 if (++ssl->cur_out_ctr[i - 1] != 0) {
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002812 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 }
2814 }
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002815
Gabor Mezei96ae9262022-06-28 11:45:18 +02002816 /* The loop goes to its end if the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 if (i == mbedtls_ssl_ep_len(ssl)) {
2818 MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap"));
2819 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker04484622018-08-06 09:49:38 +01002820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 }
2822
Hanno Becker67bc7c32018-08-06 11:33:50 +01002823#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2825 flush == SSL_DONT_FORCE_FLUSH) {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002826 size_t remaining;
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 ret = ssl_get_remaining_payload_in_datagram(ssl);
2828 if (ret < 0) {
2829 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram",
2830 ret);
2831 return ret;
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002832 }
2833
2834 remaining = (size_t) ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 if (remaining == 0) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002836 flush = SSL_FORCE_FLUSH;
Gilles Peskine449bd832023-01-11 14:50:10 +01002837 } else {
2838 MBEDTLS_SSL_DEBUG_MSG(2,
2839 ("Still %u bytes available in current datagram",
2840 (unsigned) remaining));
Hanno Becker67bc7c32018-08-06 11:33:50 +01002841 }
2842 }
2843#endif /* MBEDTLS_SSL_PROTO_DTLS */
2844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if ((flush == SSL_FORCE_FLUSH) &&
2846 (ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2847 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
2848 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002849 }
2850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002854}
2855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002857
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002859static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002860{
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (ssl->in_msglen < ssl->in_hslen ||
2862 memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 ||
2863 memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) {
2864 return 1;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002865 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 return 0;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002867}
Hanno Becker44650b72018-08-16 12:51:11 +01002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002870{
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 return (ssl->in_msg[9] << 16) |
2872 (ssl->in_msg[10] << 8) |
2873 ssl->in_msg[11];
Hanno Becker44650b72018-08-16 12:51:11 +01002874}
2875
Gilles Peskine449bd832023-01-11 14:50:10 +01002876static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002877{
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 return (ssl->in_msg[6] << 16) |
2879 (ssl->in_msg[7] << 8) |
2880 ssl->in_msg[8];
Hanno Becker44650b72018-08-16 12:51:11 +01002881}
2882
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002884static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002885{
2886 uint32_t msg_len, frag_off, frag_len;
2887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 msg_len = ssl_get_hs_total_len(ssl);
2889 frag_off = ssl_get_hs_frag_off(ssl);
2890 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker44650b72018-08-16 12:51:11 +01002891
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 if (frag_off > msg_len) {
2893 return -1;
2894 }
Hanno Becker44650b72018-08-16 12:51:11 +01002895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if (frag_len > msg_len - frag_off) {
2897 return -1;
2898 }
Hanno Becker44650b72018-08-16 12:51:11 +01002899
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (frag_len + 12 > ssl->in_msglen) {
2901 return -1;
2902 }
Hanno Becker44650b72018-08-16 12:51:11 +01002903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 return 0;
Hanno Becker44650b72018-08-16 12:51:11 +01002905}
2906
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002907/*
2908 * Mark bits in bitmask (used for DTLS HS reassembly)
2909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002911{
2912 unsigned int start_bits, end_bits;
2913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 start_bits = 8 - (offset % 8);
2915 if (start_bits != 8) {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002916 size_t first_byte_idx = offset / 8;
2917
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002918 /* Special case */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919 if (len <= start_bits) {
2920 for (; len != 0; len--) {
2921 mask[first_byte_idx] |= 1 << (start_bits - len);
2922 }
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002923
2924 /* Avoid potential issues with offset or len becoming invalid */
2925 return;
2926 }
2927
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002928 offset += start_bits; /* Now offset % 8 == 0 */
2929 len -= start_bits;
2930
Gilles Peskine449bd832023-01-11 14:50:10 +01002931 for (; start_bits != 0; start_bits--) {
2932 mask[first_byte_idx] |= 1 << (start_bits - 1);
2933 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002934 }
2935
2936 end_bits = len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if (end_bits != 0) {
2938 size_t last_byte_idx = (offset + len) / 8;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002939
2940 len -= end_bits; /* Now len % 8 == 0 */
2941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 for (; end_bits != 0; end_bits--) {
2943 mask[last_byte_idx] |= 1 << (8 - end_bits);
2944 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002945 }
2946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 memset(mask + offset / 8, 0xFF, len / 8);
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002948}
2949
2950/*
2951 * Check that bitmask is full
2952 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002953MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002954static int ssl_bitmask_check(unsigned char *mask, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002955{
2956 size_t i;
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 for (i = 0; i < len / 8; i++) {
2959 if (mask[i] != 0xFF) {
2960 return -1;
2961 }
2962 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 for (i = 0; i < len % 8; i++) {
2965 if ((mask[len / 8] & (1 << (7 - i))) == 0) {
2966 return -1;
2967 }
2968 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002971}
2972
Hanno Becker56e205e2018-08-16 09:06:12 +01002973/* msg_len does not include the handshake header */
Gilles Peskine449bd832023-01-11 14:50:10 +01002974static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
2975 unsigned add_bitmap)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002976{
Hanno Becker56e205e2018-08-16 09:06:12 +01002977 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002978
Hanno Becker56e205e2018-08-16 09:06:12 +01002979 alloc_len = 12; /* Handshake header */
2980 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (add_bitmap) {
2983 alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 }
2986 return alloc_len;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002987}
Hanno Becker56e205e2018-08-16 09:06:12 +01002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002990
Gilles Peskine449bd832023-01-11 14:50:10 +01002991static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
Hanno Becker12555c62018-08-16 12:47:53 +01002992{
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 return (ssl->in_msg[1] << 16) |
2994 (ssl->in_msg[2] << 8) |
2995 ssl->in_msg[3];
Hanno Becker12555c62018-08-16 12:47:53 +01002996}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002999{
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
3001 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
3002 ssl->in_msglen));
3003 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003004 }
3005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
3009 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
3010 MBEDTLS_PRINTF_SIZET,
3011 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00003015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003017
Gilles Peskine449bd832023-01-11 14:50:10 +01003018 if (ssl_check_hs_header(ssl) != 0) {
3019 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
3020 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker44650b72018-08-16 12:51:11 +01003021 }
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (ssl->handshake != NULL &&
3024 ((mbedtls_ssl_is_handshake_over(ssl) == 0 &&
3025 recv_msg_seq != ssl->handshake->in_msg_seq) ||
3026 (mbedtls_ssl_is_handshake_over(ssl) == 1 &&
3027 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
3028 if (recv_msg_seq > ssl->handshake->in_msg_seq) {
3029 MBEDTLS_SSL_DEBUG_MSG(2,
3030 (
3031 "received future handshake message of sequence number %u (next %u)",
3032 recv_msg_seq,
3033 ssl->handshake->in_msg_seq));
3034 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Becker9e1ec222018-08-15 15:54:43 +01003035 }
3036
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003037 /* Retransmit only on last message from previous flight, to avoid
3038 * too many retransmissions.
3039 * Besides, No sane server ever retransmits HelloVerifyRequest */
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
3041 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
3042 MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
3043 "message_seq = %u, start_of_flight = %u",
3044 recv_msg_seq,
3045 ssl->handshake->in_flight_start_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
3048 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
3049 return ret;
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003050 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 } else {
3052 MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
3053 "message_seq = %u, expected = %u",
3054 recv_msg_seq,
3055 ssl->handshake->in_msg_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003056 }
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003059 }
3060 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003061
Hanno Becker6d97ef52018-08-16 13:09:04 +01003062 /* Message reassembly is handled alongside buffering of future
3063 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003064 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003065 * handshake logic layer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (ssl_hs_is_proper_fragment(ssl) == 1) {
3067 MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
3068 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003069 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003072 /* With TLS we don't handle fragmentation (for now) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (ssl->in_msglen < ssl->in_hslen) {
3074 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
3075 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003076 }
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003079}
3080
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003081int mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003082{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker0271f962018-08-16 13:23:47 +01003084 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 if (mbedtls_ssl_is_handshake_over(ssl) == 0 && hs != NULL) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003087 ret = ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
3088 if (ret != 0) {
3089 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
3090 return ret;
3091 }
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003092 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003093
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003094 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3097 ssl->handshake != NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01003098 unsigned offset;
3099 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003100
Hanno Becker0271f962018-08-16 13:23:47 +01003101 /* Increment handshake sequence number */
3102 hs->in_msg_seq++;
3103
3104 /*
3105 * Clear up handshake buffering and reassembly structure.
3106 */
3107
3108 /* Free first entry */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 ssl_buffering_free_slot(ssl, 0);
Hanno Becker0271f962018-08-16 13:23:47 +01003110
3111 /* Shift all other entries */
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 for (offset = 0, hs_buf = &hs->buffering.hs[0];
Hanno Beckere605b192018-08-21 15:59:07 +01003113 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 offset++, hs_buf++) {
Hanno Becker0271f962018-08-16 13:23:47 +01003115 *hs_buf = *(hs_buf + 1);
3116 }
3117
3118 /* Create a fresh last entry */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003120 }
3121#endif
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003122 return 0;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003123}
3124
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003125/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003126 * DTLS anti-replay: RFC 6347 4.1.2.6
3127 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003128 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3129 * Bit n is set iff record number in_window_top - n has been seen.
3130 *
3131 * Usually, in_window_top is the last record number seen and the lsb of
3132 * in_window is set. The only exception is the initial state (record number 0
3133 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01003136void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003137{
3138 ssl->in_window_top = 0;
3139 ssl->in_window = 0;
3140}
3141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003143{
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 return ((uint64_t) buf[0] << 40) |
3145 ((uint64_t) buf[1] << 32) |
3146 ((uint64_t) buf[2] << 24) |
3147 ((uint64_t) buf[3] << 16) |
3148 ((uint64_t) buf[4] << 8) |
3149 ((uint64_t) buf[5]);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003150}
3151
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003152MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003153static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003154{
Janos Follath865b3eb2019-12-16 11:46:15 +00003155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003156 unsigned char *original_in_ctr;
3157
3158 // save original in_ctr
3159 original_in_ctr = ssl->in_ctr;
3160
3161 // use counter from record
3162 ssl->in_ctr = record_in_ctr;
3163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003165
3166 // restore the counter
3167 ssl->in_ctr = original_in_ctr;
3168
3169 return ret;
3170}
3171
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003172/*
3173 * Return 0 if sequence number is acceptable, -1 otherwise
3174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003176{
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003178 uint64_t bit;
3179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
3181 return 0;
3182 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003183
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 if (rec_seqnum > ssl->in_window_top) {
3185 return 0;
3186 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003187
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003188 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 if (bit >= 64) {
3191 return -1;
3192 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
3195 return -1;
3196 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 return 0;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003199}
3200
3201/*
3202 * Update replay window on new validated record
3203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003204void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003205{
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003209 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 if (rec_seqnum > ssl->in_window_top) {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003213 /* Update window_top and the contents of the window */
3214 uint64_t shift = rec_seqnum - ssl->in_window_top;
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 if (shift >= 64) {
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003217 ssl->in_window = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003219 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003220 ssl->in_window |= 1;
3221 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003222
3223 ssl->in_window_top = rec_seqnum;
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003225 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003226 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (bit < 64) { /* Always true, but be extra sure */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003229 ssl->in_window |= (uint64_t) 1 << bit;
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003231 }
3232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003234
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003235#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003236/*
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003237 * Check if a datagram looks like a ClientHello with a valid cookie,
3238 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003239 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003240 *
3241 * - if cookie is valid, return 0
3242 * - if ClientHello looks superficially valid but cookie is not,
3243 * fill obuf and set olen, then
3244 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3245 * - otherwise return a specific error code
3246 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003247MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003248MBEDTLS_STATIC_TESTABLE
3249int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 mbedtls_ssl_context *ssl,
3251 const unsigned char *cli_id, size_t cli_id_len,
3252 const unsigned char *in, size_t in_len,
3253 unsigned char *obuf, size_t buf_len, size_t *olen)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003254{
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003255 size_t sid_len, cookie_len, epoch, fragment_offset;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003256 unsigned char *p;
3257
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003258 /*
3259 * Structure of ClientHello with record and handshake headers,
3260 * and expected values. We don't need to check a lot, more checks will be
3261 * done when actually parsing the ClientHello - skipping those checks
3262 * avoids code duplication and does not make cookie forging any easier.
3263 *
3264 * 0-0 ContentType type; copied, must be handshake
3265 * 1-2 ProtocolVersion version; copied
3266 * 3-4 uint16 epoch; copied, must be 0
3267 * 5-10 uint48 sequence_number; copied
3268 * 11-12 uint16 length; (ignored)
3269 *
3270 * 13-13 HandshakeType msg_type; (ignored)
3271 * 14-16 uint24 length; (ignored)
3272 * 17-18 uint16 message_seq; copied
3273 * 19-21 uint24 fragment_offset; copied, must be 0
3274 * 22-24 uint24 fragment_length; (ignored)
3275 *
3276 * 25-26 ProtocolVersion client_version; (ignored)
3277 * 27-58 Random random; (ignored)
3278 * 59-xx SessionID session_id; 1 byte len + sid_len content
3279 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3280 * ...
3281 *
3282 * Minimum length is 61 bytes.
3283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
3285 (unsigned) in_len));
3286 MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
3287 if (in_len < 61) {
3288 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
3289 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003290 }
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 epoch = MBEDTLS_GET_UINT16_BE(in, 3);
3293 fragment_offset = MBEDTLS_GET_UINT24_BE(in, 19);
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || epoch != 0 ||
3296 fragment_offset != 0) {
3297 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
3298 MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
3299 in[0], (unsigned) epoch,
3300 (unsigned) fragment_offset));
3301 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003302 }
3303
3304 sid_len = in[59];
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 if (59 + 1 + sid_len + 1 > in_len) {
3306 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
3307 (unsigned) sid_len,
3308 (unsigned) in_len - 61));
3309 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003310 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
3312 in + 60, sid_len);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003313
3314 cookie_len = in[60 + sid_len];
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
3316 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
3317 (unsigned) cookie_len,
3318 (unsigned) (in_len - sid_len - 61)));
3319 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003320 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
3323 in + sid_len + 61, cookie_len);
3324 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
3325 in + sid_len + 61, cookie_len,
3326 cli_id, cli_id_len) == 0) {
3327 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
3328 return 0;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003329 }
3330
3331 /*
3332 * If we get here, we've got an invalid cookie, let's prepare HVR.
3333 *
3334 * 0-0 ContentType type; copied
3335 * 1-2 ProtocolVersion version; copied
3336 * 3-4 uint16 epoch; copied
3337 * 5-10 uint48 sequence_number; copied
3338 * 11-12 uint16 length; olen - 13
3339 *
3340 * 13-13 HandshakeType msg_type; hello_verify_request
3341 * 14-16 uint24 length; olen - 25
3342 * 17-18 uint16 message_seq; copied
3343 * 19-21 uint24 fragment_offset; copied
3344 * 22-24 uint24 fragment_length; olen - 25
3345 *
3346 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3347 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3348 *
3349 * Minimum length is 28.
3350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 if (buf_len < 28) {
3352 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3353 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003354
3355 /* Copy most fields and adapt others */
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 memcpy(obuf, in, 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003357 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3358 obuf[25] = 0xfe;
3359 obuf[26] = 0xff;
3360
3361 /* Generate and write actual cookie */
3362 p = obuf + 28;
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
3364 &p, obuf + buf_len,
3365 cli_id, cli_id_len) != 0) {
3366 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003367 }
3368
3369 *olen = p - obuf;
3370
3371 /* Go back and fill length fields */
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 obuf[27] = (unsigned char) (*olen - 28);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
3375 obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
3376 obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003381}
3382
3383/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003384 * Handle possible client reconnect with the same UDP quadruplet
3385 * (RFC 6347 Section 4.2.8).
3386 *
3387 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3388 * that looks like a ClientHello.
3389 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003390 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003391 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003392 * - if the input looks like a ClientHello with a valid cookie,
3393 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003394 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003395 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003396 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003397 * This function is called (through ssl_check_client_reconnect()) when an
3398 * unexpected record is found in ssl_get_next_record(), which will discard the
3399 * record if we return 0, and bubble up the return value otherwise (this
3400 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3401 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003402 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003403MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003404static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003405{
Janos Follath865b3eb2019-12-16 11:46:15 +00003406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003407 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (ssl->conf->f_cookie_write == NULL ||
3410 ssl->conf->f_cookie_check == NULL) {
Hanno Becker2fddd372019-07-10 14:37:41 +01003411 /* If we can't use cookies to verify reachability of the peer,
3412 * drop the record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
3414 "can't check reconnect validity"));
3415 return 0;
Hanno Becker2fddd372019-07-10 14:37:41 +01003416 }
3417
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003418 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 ssl,
3420 ssl->cli_id, ssl->cli_id_len,
3421 ssl->in_buf, ssl->in_left,
3422 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003425
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003427 int send_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
3429 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3430 ssl->out_buf, len);
Brian J Murray1903fb32016-11-06 04:45:15 -08003431 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003432 * If the error is permanent we'll catch it later,
3433 * if it's not, then hopefully it'll work next time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
3435 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003436 (void) send_ret;
3437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 return 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003439 }
3440
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 if (ret == 0) {
3442 MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
3443 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
3444 MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
3445 return ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003446 }
3447
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003449 }
3450
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 return ret;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003452}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003453#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003454
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003455MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003456static int ssl_check_record_type(uint8_t record_type)
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003457{
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003459 record_type != MBEDTLS_SSL_MSG_ALERT &&
3460 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
3462 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003463 }
3464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 return 0;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003466}
3467
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003468/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003469 * ContentType type;
3470 * ProtocolVersion version;
3471 * uint16 epoch; // DTLS only
3472 * uint48 sequence_number; // DTLS only
3473 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003474 *
3475 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003476 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003477 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3478 *
3479 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003480 * 1. proceed with the record if this function returns 0
3481 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3482 * 3. return CLIENT_RECONNECT if this function return that value
3483 * 4. drop the whole datagram if this function returns anything else.
3484 * Point 2 is needed when the peer is resending, and we have already received
3485 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003486 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003487MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003488static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
3489 unsigned char *buf,
3490 size_t len,
3491 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00003492{
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003493 mbedtls_ssl_protocol_version tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00003494
Hanno Beckere5e7e782019-07-11 12:29:35 +01003495 size_t const rec_hdr_type_offset = 0;
3496 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003497
Hanno Beckere5e7e782019-07-11 12:29:35 +01003498 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3499 rec_hdr_type_len;
3500 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003501
Hanno Beckere5e7e782019-07-11 12:29:35 +01003502 size_t const rec_hdr_ctr_len = 8;
3503#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003504 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003505 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3506 rec_hdr_version_len;
3507
Hanno Beckera0e20d02019-05-15 14:03:01 +01003508#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003509 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3510 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003511 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003512#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3513#endif /* MBEDTLS_SSL_PROTO_DTLS */
3514
3515 size_t rec_hdr_len_offset; /* To be determined */
3516 size_t const rec_hdr_len_len = 2;
3517
3518 /*
3519 * Check minimum lengths for record header.
3520 */
3521
3522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003524 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003526#endif /* MBEDTLS_SSL_PROTO_DTLS */
3527 {
3528 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3529 }
3530
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3532 MBEDTLS_SSL_DEBUG_MSG(1,
3533 (
3534 "datagram of length %u too small to hold DTLS record header of length %u",
3535 (unsigned) len,
3536 (unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
3537 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003538 }
3539
3540 /*
3541 * Parse and validate record content type
3542 */
3543
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 rec->type = buf[rec_hdr_type_offset];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003545
3546 /* Check record content type */
3547#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3548 rec->cid_len = 0;
3549
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003551 ssl->conf->cid_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 rec->type == MBEDTLS_SSL_MSG_CID) {
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003553 /* Shift pointers to account for record header including CID
3554 * struct {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02003555 * ContentType outer_type = tls12_cid;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003556 * ProtocolVersion version;
3557 * uint16 epoch;
3558 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003559 * opaque cid[cid_length]; // Additional field compared to
3560 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003561 * uint16 length;
3562 * opaque enc_content[DTLSCiphertext.length];
3563 * } DTLSCiphertext;
3564 */
3565
3566 /* So far, we only support static CID lengths
3567 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003568 rec_hdr_cid_len = ssl->conf->cid_len;
3569 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3572 MBEDTLS_SSL_DEBUG_MSG(1,
3573 (
3574 "datagram of length %u too small to hold DTLS record header including CID, length %u",
3575 (unsigned) len,
3576 (unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
3577 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere538d822019-07-10 14:50:10 +01003578 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003579
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003580 /* configured CID len is guaranteed at most 255, see
3581 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3582 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
3584 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003585#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003586 {
Gilles Peskine449bd832023-01-11 14:50:10 +01003587 if (ssl_check_record_type(rec->type)) {
3588 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
3589 (unsigned) rec->type));
3590 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003591 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003592 }
3593
Hanno Beckere5e7e782019-07-11 12:29:35 +01003594 /*
3595 * Parse and validate record version
3596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 rec->ver[0] = buf[rec_hdr_version_offset + 0];
3598 rec->ver[1] = buf[rec_hdr_version_offset + 1];
3599 tls_version = mbedtls_ssl_read_version(buf + rec_hdr_version_offset,
3600 ssl->conf->transport);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 if (tls_version > ssl->conf->max_tls_version) {
3603 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS version mismatch: got %u, expected max %u",
3604 (unsigned) tls_version,
3605 (unsigned) ssl->conf->max_tls_version));
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003606
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003609 /*
3610 * Parse/Copy record sequence number.
3611 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003612
Hanno Beckere5e7e782019-07-11 12:29:35 +01003613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003615 /* Copy explicit record sequence number from input buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
3617 rec_hdr_ctr_len);
3618 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003619#endif /* MBEDTLS_SSL_PROTO_DTLS */
3620 {
3621 /* Copy implicit record sequence number from SSL context structure. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003623 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003624
Hanno Beckere5e7e782019-07-11 12:29:35 +01003625 /*
3626 * Parse record length.
3627 */
3628
Hanno Beckere5e7e782019-07-11 12:29:35 +01003629 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) |
3631 ((size_t) buf[rec_hdr_len_offset + 1] << 0);
3632 MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
3635 "version = [0x%x], msglen = %" MBEDTLS_PRINTF_SIZET,
3636 rec->type, (unsigned) tls_version, rec->data_len));
Hanno Beckere5e7e782019-07-11 12:29:35 +01003637
3638 rec->buf = buf;
3639 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003640
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 if (rec->data_len == 0) {
3642 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003644
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003645 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003646 * DTLS-related tests.
3647 * Check epoch before checking length constraint because
3648 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3649 * message gets duplicated before the corresponding Finished message,
3650 * the second ChangeCipherSpec should be discarded because it belongs
3651 * to an old epoch, but not because its length is shorter than
3652 * the minimum record length for packets using the new record transform.
3653 * Note that these two kinds of failures are handled differently,
3654 * as an unexpected record is silently skipped but an invalid
3655 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003656 */
3657#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3659 rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003660
Hanno Becker955a5c92019-07-10 17:12:07 +01003661 /* Check that the datagram is large enough to contain a record
3662 * of the advertised length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 if (len < rec->data_offset + rec->data_len) {
3664 MBEDTLS_SSL_DEBUG_MSG(1,
3665 (
3666 "Datagram of length %u too small to contain record of advertised length %u.",
3667 (unsigned) len,
3668 (unsigned) (rec->data_offset + rec->data_len)));
3669 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker955a5c92019-07-10 17:12:07 +01003670 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003671
Hanno Becker37cfe732019-07-10 17:20:01 +01003672 /* Records from other, non-matching epochs are silently discarded.
3673 * (The case of same-port Client reconnects must be considered in
3674 * the caller). */
Gilles Peskine449bd832023-01-11 14:50:10 +01003675 if (rec_epoch != ssl->in_epoch) {
3676 MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
3677 "expected %u, received %lu",
3678 ssl->in_epoch, (unsigned long) rec_epoch));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003679
Hanno Becker552f7472019-07-19 10:59:12 +01003680 /* Records from the next epoch are considered for buffering
3681 * (concretely: early Finished messages). */
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
3683 MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
3684 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003685 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003686
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003688 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003689#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003690 /* For records from the correct epoch, check whether their
3691 * sequence number has been seen before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
3693 &rec->ctr[0]) != 0) {
3694 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
3695 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003696 }
3697#endif
3698 }
3699#endif /* MBEDTLS_SSL_PROTO_DTLS */
3700
Gilles Peskine449bd832023-01-11 14:50:10 +01003701 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003702}
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
Paul Bakker5121ce52009-01-03 21:22:43 +00003704
Hanno Becker2fddd372019-07-10 14:37:41 +01003705#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003707static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
Hanno Becker2fddd372019-07-10 14:37:41 +01003708{
Gilles Peskine449bd832023-01-11 14:50:10 +01003709 unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1];
Hanno Becker2fddd372019-07-10 14:37:41 +01003710
3711 /*
3712 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3713 * access the first byte of record content (handshake type), as we
3714 * have an active transform (possibly iv_len != 0), so use the
3715 * fact that the record header len is 13 instead.
3716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 if (rec_epoch == 0 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003718 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 mbedtls_ssl_is_handshake_over(ssl) == 1 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003720 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3721 ssl->in_left > 13 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
3723 MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
3724 "from the same port"));
3725 return ssl_handle_possible_reconnect(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003726 }
3727
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003729}
Hanno Becker2fddd372019-07-10 14:37:41 +01003730#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003732/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003733 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003734 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003736static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
3737 mbedtls_record *rec)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003738{
3739 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003740
Gilles Peskine449bd832023-01-11 14:50:10 +01003741 MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
3742 rec->buf, rec->buf_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003743
Ronald Cron7e38cba2021-11-24 12:43:39 +01003744 /*
3745 * In TLS 1.3, always treat ChangeCipherSpec records
3746 * as unencrypted. The only thing we do with them is
3747 * check the length and content and ignore them.
3748 */
Ronald Cron6f135e12021-12-08 16:57:54 +01003749#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003750 if (ssl->transform_in != NULL &&
3751 ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3752 if (rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
Ronald Cron7e38cba2021-11-24 12:43:39 +01003753 done = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 }
Ronald Cron7e38cba2021-11-24 12:43:39 +01003755 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003756#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron7e38cba2021-11-24 12:43:39 +01003757
Gilles Peskine449bd832023-01-11 14:50:10 +01003758 if (!done && ssl->transform_in != NULL) {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003759 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003760
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
3762 rec)) != 0) {
3763 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
Hanno Becker8367ccc2019-05-14 11:30:10 +01003764
Hanno Beckera0e20d02019-05-15 14:03:01 +01003765#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Becker8367ccc2019-05-14 11:30:10 +01003767 ssl->conf->ignore_unexpected_cid
Gilles Peskine449bd832023-01-11 14:50:10 +01003768 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
3769 MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
Hanno Becker16ded982019-05-08 13:02:55 +01003770 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003771 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003772#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003773
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 }
3776
Gilles Peskine449bd832023-01-11 14:50:10 +01003777 if (old_msg_type != rec->type) {
3778 MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
3779 old_msg_type, rec->type));
Hanno Becker6430faf2019-05-08 11:57:13 +01003780 }
3781
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
3783 rec->buf + rec->data_offset, rec->data_len);
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003784
Hanno Beckera0e20d02019-05-15 14:03:01 +01003785#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003786 /* We have already checked the record content type
3787 * in ssl_parse_record_header(), failing or silently
3788 * dropping the record in the case of an unknown type.
3789 *
3790 * Since with the use of CIDs, the record content type
3791 * might change during decryption, re-check the record
3792 * content type, but treat a failure as fatal this time. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 if (ssl_check_record_type(rec->type)) {
3794 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
3795 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker6430faf2019-05-08 11:57:13 +01003796 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003797#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003798
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 if (rec->data_len == 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003800#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2
3802 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003803 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
3805 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003806 }
3807#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3808
3809 ssl->nb_zero++;
3810
3811 /*
3812 * Three or more empty messages may be a DoS attack
3813 * (excessive CPU consumption).
3814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 if (ssl->nb_zero > 3) {
3816 MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
3817 "messages, possible DoS attack"));
Hanno Becker6e7700d2019-05-08 10:38:32 +01003818 /* Treat the records as if they were not properly authenticated,
3819 * thereby failing the connection if we see more than allowed
3820 * by the configured bad MAC threshold. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003822 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 } else {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003824 ssl->nb_zero = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003825 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003826
3827#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003828 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003829 ; /* in_ctr read from peer, not maintained internally */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 } else
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003831#endif
3832 {
3833 unsigned i;
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 for (i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3835 i > mbedtls_ssl_ep_len(ssl); i--) {
3836 if (++ssl->in_ctr[i - 1] != 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003837 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003838 }
Jerry Yuae0b2e22021-10-08 15:21:19 +08003839 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003840
3841 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 if (i == mbedtls_ssl_ep_len(ssl)) {
3843 MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
3844 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003845 }
3846 }
3847
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 }
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3852 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003853 }
3854#endif
3855
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003856 /* Check actual (decrypted) record content length against
3857 * configured maximum. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
3859 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
3860 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003861 }
3862
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003864}
3865
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003866/*
3867 * Read a record.
3868 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003869 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3870 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3871 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003872 */
Hanno Becker1097b342018-08-15 14:09:41 +01003873
3874/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003875MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003876static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003877MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003878static int ssl_get_next_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003879MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003880static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
Hanno Becker4162b112018-08-15 14:05:04 +01003881
Gilles Peskine449bd832023-01-11 14:50:10 +01003882int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
3883 unsigned update_hs_digest)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003884{
Janos Follath865b3eb2019-12-16 11:46:15 +00003885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003886
Gilles Peskine449bd832023-01-11 14:50:10 +01003887 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003888
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 if (ssl->keep_current_message == 0) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003890 do {
Simon Butcher99000142016-10-13 17:21:01 +01003891
Gilles Peskine449bd832023-01-11 14:50:10 +01003892 ret = ssl_consume_current_message(ssl);
3893 if (ret != 0) {
3894 return ret;
3895 }
Hanno Becker26994592018-08-15 14:14:59 +01003896
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 if (ssl_record_is_in_progress(ssl) == 0) {
David Horstmann10be1342022-10-06 18:31:25 +01003898 int dtls_have_buffered = 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003899#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere74d5562018-08-15 14:26:08 +01003900
Hanno Becker40f50842018-08-15 14:48:01 +01003901 /* We only check for buffered messages if the
3902 * current datagram is fully consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3904 ssl_next_record_is_in_datagram(ssl) == 0) {
3905 if (ssl_load_buffered_message(ssl) == 0) {
David Horstmann10be1342022-10-06 18:31:25 +01003906 dtls_have_buffered = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 }
Hanno Becker40f50842018-08-15 14:48:01 +01003908 }
3909
Hanno Becker40f50842018-08-15 14:48:01 +01003910#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 if (dtls_have_buffered == 0) {
3912 ret = ssl_get_next_record(ssl);
3913 if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
Hanno Becker40f50842018-08-15 14:48:01 +01003914 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 }
Hanno Becker40f50842018-08-15 14:48:01 +01003916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 if (ret != 0) {
3918 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
3919 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003920 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003921 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003922 }
3923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 ret = mbedtls_ssl_handle_message_type(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003925
Hanno Becker40f50842018-08-15 14:48:01 +01003926#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker40f50842018-08-15 14:48:01 +01003928 /* Buffer future message */
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 ret = ssl_buffer_message(ssl);
3930 if (ret != 0) {
3931 return ret;
3932 }
Hanno Becker40f50842018-08-15 14:48:01 +01003933
3934 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3935 }
3936#endif /* MBEDTLS_SSL_PROTO_DTLS */
3937
Gilles Peskine449bd832023-01-11 14:50:10 +01003938 } while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3939 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003940
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 if (0 != ret) {
3942 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
3943 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01003944 }
3945
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3947 update_hs_digest == 1) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01003948 ret = mbedtls_ssl_update_handshake_status(ssl);
3949 if (0 != ret) {
3950 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_update_handshake_status"), ret);
3951 return ret;
3952 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 } else {
3955 MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003956 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003957 }
3958
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
Simon Butcher99000142016-10-13 17:21:01 +01003960
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003962}
3963
Hanno Becker40f50842018-08-15 14:48:01 +01003964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003965MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003966static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003967{
Gilles Peskine449bd832023-01-11 14:50:10 +01003968 if (ssl->in_left > ssl->next_record_offset) {
3969 return 1;
3970 }
Simon Butcher99000142016-10-13 17:21:01 +01003971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 return 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003973}
3974
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003976static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01003977{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003978 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003980 int ret = 0;
3981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if (hs == NULL) {
3983 return -1;
3984 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003985
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
Hanno Beckere00ae372018-08-20 09:39:42 +01003987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3989 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003990 /* Check if we have seen a ChangeCipherSpec before.
3991 * If yes, synthesize a CCS record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 if (!hs->buffering.seen_ccs) {
3993 MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003994 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003995 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003996 }
3997
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003999 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4000 ssl->in_msglen = 1;
4001 ssl->in_msg[0] = 1;
4002
4003 /* As long as they are equal, the exact value doesn't matter. */
4004 ssl->in_left = 0;
4005 ssl->next_record_offset = 0;
4006
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004007 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004008 goto exit;
4009 }
Hanno Becker37f95322018-08-16 13:55:32 +01004010
Hanno Beckerb8f50142018-08-28 10:01:34 +01004011#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004012 /* Debug only */
4013 {
4014 unsigned offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
Hanno Becker37f95322018-08-16 13:55:32 +01004016 hs_buf = &hs->buffering.hs[offset];
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 if (hs_buf->is_valid == 1) {
4018 MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
4019 hs->in_msg_seq + offset,
4020 hs_buf->is_complete ? "fully" : "partially"));
Hanno Becker37f95322018-08-16 13:55:32 +01004021 }
4022 }
4023 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004024#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004025
4026 /* Check if we have buffered and/or fully reassembled the
4027 * next handshake message. */
4028 hs_buf = &hs->buffering.hs[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
Hanno Becker37f95322018-08-16 13:55:32 +01004030 /* Synthesize a record containing the buffered HS message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 size_t msg_len = (hs_buf->data[1] << 16) |
4032 (hs_buf->data[2] << 8) |
4033 hs_buf->data[3];
Hanno Becker37f95322018-08-16 13:55:32 +01004034
4035 /* Double-check that we haven't accidentally buffered
4036 * a message that doesn't fit into the input buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
4038 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4039 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004040 }
4041
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
4043 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
4044 hs_buf->data, msg_len + 12);
Hanno Becker37f95322018-08-16 13:55:32 +01004045
4046 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4047 ssl->in_hslen = msg_len + 12;
4048 ssl->in_msglen = msg_len + 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
Hanno Becker37f95322018-08-16 13:55:32 +01004050
4051 ret = 0;
4052 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 } else {
4054 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
4055 hs->in_msg_seq));
Hanno Becker37f95322018-08-16 13:55:32 +01004056 }
4057
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004058 ret = -1;
4059
4060exit:
4061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
4063 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004064}
4065
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004067static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
4068 size_t desired)
Hanno Beckera02b0b42018-08-21 17:20:27 +01004069{
4070 int offset;
4071 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
4073 (unsigned) desired));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004074
Hanno Becker01315ea2018-08-21 17:22:17 +01004075 /* Get rid of future records epoch first, if such exist. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 ssl_free_buffered_record(ssl);
Hanno Becker01315ea2018-08-21 17:22:17 +01004077
4078 /* Check if we have enough space available now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4080 hs->buffering.total_bytes_buffered)) {
4081 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
4082 return 0;
Hanno Becker01315ea2018-08-21 17:22:17 +01004083 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004084
Hanno Becker4f432ad2018-08-28 10:02:32 +01004085 /* We don't have enough space to buffer the next expected handshake
4086 * message. Remove buffers used for future messages to gain space,
4087 * starting with the most distant one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4089 offset >= 0; offset--) {
4090 MBEDTLS_SSL_DEBUG_MSG(2,
4091 (
4092 "Free buffering slot %d to make space for reassembly of next handshake message",
4093 offset));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004094
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 ssl_buffering_free_slot(ssl, (uint8_t) offset);
Hanno Beckera02b0b42018-08-21 17:20:27 +01004096
4097 /* Check if we have enough space available now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4099 hs->buffering.total_bytes_buffered)) {
4100 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
4101 return 0;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004102 }
4103 }
4104
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 return -1;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004106}
4107
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004108MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004109static int ssl_buffer_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004110{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004111 int ret = 0;
4112 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4113
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 if (hs == NULL) {
4115 return 0;
4116 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004117
Gilles Peskine449bd832023-01-11 14:50:10 +01004118 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004119
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 switch (ssl->in_msgtype) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004121 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
Hanno Beckere678eaa2018-08-21 14:57:46 +01004123
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004124 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004125 break;
4126
4127 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004128 {
4129 unsigned recv_msg_seq_offset;
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Hanno Becker37f95322018-08-16 13:55:32 +01004131 mbedtls_ssl_hs_buffer *hs_buf;
4132 size_t msg_len = ssl->in_hslen - 12;
4133
4134 /* We should never receive an old handshake
4135 * message - double-check nonetheless. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 if (recv_msg_seq < ssl->handshake->in_msg_seq) {
4137 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4138 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004139 }
4140
4141 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Becker37f95322018-08-16 13:55:32 +01004143 /* Silently ignore -- message too far in the future */
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 MBEDTLS_SSL_DEBUG_MSG(2,
4145 ("Ignore future HS message with sequence number %u, "
4146 "buffering window %u - %u",
4147 recv_msg_seq, ssl->handshake->in_msg_seq,
4148 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
4149 1));
Hanno Becker37f95322018-08-16 13:55:32 +01004150
4151 goto exit;
4152 }
4153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
4155 recv_msg_seq, recv_msg_seq_offset));
Hanno Becker37f95322018-08-16 13:55:32 +01004156
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
Hanno Becker37f95322018-08-16 13:55:32 +01004158
4159 /* Check if the buffering for this seq nr has already commenced. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 if (!hs_buf->is_valid) {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004161 size_t reassembly_buf_sz;
4162
Hanno Becker37f95322018-08-16 13:55:32 +01004163 hs_buf->is_fragmented =
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 (ssl_hs_is_proper_fragment(ssl) == 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004165
4166 /* We copy the message back into the input buffer
4167 * after reassembly, so check that it's not too large.
4168 * This is an implementation-specific limitation
4169 * and not one from the standard, hence it is not
4170 * checked in ssl_check_hs_header(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
Hanno Becker37f95322018-08-16 13:55:32 +01004172 /* Ignore message */
4173 goto exit;
4174 }
4175
Hanno Beckere0b150f2018-08-21 15:51:03 +01004176 /* Check if we have enough space to buffer the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (hs->buffering.total_bytes_buffered >
4178 MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
4179 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4180 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004181 }
4182
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
4184 hs_buf->is_fragmented);
Hanno Beckere0b150f2018-08-21 15:51:03 +01004185
Gilles Peskine449bd832023-01-11 14:50:10 +01004186 if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4187 hs->buffering.total_bytes_buffered)) {
4188 if (recv_msg_seq_offset > 0) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004189 /* If we can't buffer a future message because
4190 * of space limitations -- ignore. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004191 MBEDTLS_SSL_DEBUG_MSG(2,
4192 ("Buffering of future message of size %"
4193 MBEDTLS_PRINTF_SIZET
4194 " would exceed the compile-time limit %"
4195 MBEDTLS_PRINTF_SIZET
4196 " (already %" MBEDTLS_PRINTF_SIZET
4197 " bytes buffered) -- ignore\n",
4198 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4199 hs->buffering.total_bytes_buffered));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004200 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 } else {
4202 MBEDTLS_SSL_DEBUG_MSG(2,
4203 ("Buffering of future message of size %"
4204 MBEDTLS_PRINTF_SIZET
4205 " would exceed the compile-time limit %"
4206 MBEDTLS_PRINTF_SIZET
4207 " (already %" MBEDTLS_PRINTF_SIZET
4208 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4209 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4210 hs->buffering.total_bytes_buffered));
Hanno Beckere1801392018-08-21 16:51:05 +01004211 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004212
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
4214 MBEDTLS_SSL_DEBUG_MSG(2,
4215 ("Reassembly of next message of size %"
4216 MBEDTLS_PRINTF_SIZET
4217 " (%" MBEDTLS_PRINTF_SIZET
4218 " with bitmap) would exceed"
4219 " the compile-time limit %"
4220 MBEDTLS_PRINTF_SIZET
4221 " (already %" MBEDTLS_PRINTF_SIZET
4222 " bytes buffered) -- fail\n",
4223 msg_len,
4224 reassembly_buf_sz,
4225 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4226 hs->buffering.total_bytes_buffered));
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004227 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4228 goto exit;
4229 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004230 }
4231
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 MBEDTLS_SSL_DEBUG_MSG(2,
4233 ("initialize reassembly, total length = %"
4234 MBEDTLS_PRINTF_SIZET,
4235 msg_len));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
4238 if (hs_buf->data == NULL) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004239 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004240 goto exit;
4241 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004242 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004243
4244 /* Prepare final header: copy msg_type, length and message_seq,
4245 * then add standardised fragment_offset and fragment_length */
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 memcpy(hs_buf->data, ssl->in_msg, 6);
4247 memset(hs_buf->data + 6, 0, 3);
4248 memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
Hanno Becker37f95322018-08-16 13:55:32 +01004249
4250 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004251
4252 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Gilles Peskine449bd832023-01-11 14:50:10 +01004253 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004254 /* Make sure msg_type and length are consistent */
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
4256 MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
Hanno Becker37f95322018-08-16 13:55:32 +01004257 /* Ignore */
4258 goto exit;
4259 }
4260 }
4261
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 if (!hs_buf->is_complete) {
Hanno Becker37f95322018-08-16 13:55:32 +01004263 size_t frag_len, frag_off;
4264 unsigned char * const msg = hs_buf->data + 12;
4265
4266 /*
4267 * Check and copy current fragment
4268 */
4269
4270 /* Validation of header fields already done in
4271 * mbedtls_ssl_prepare_handshake_record(). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 frag_off = ssl_get_hs_frag_off(ssl);
4273 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker37f95322018-08-16 13:55:32 +01004274
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4276 ", length = %" MBEDTLS_PRINTF_SIZET,
4277 frag_off, frag_len));
4278 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
Hanno Becker37f95322018-08-16 13:55:32 +01004279
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (hs_buf->is_fragmented) {
Hanno Becker37f95322018-08-16 13:55:32 +01004281 unsigned char * const bitmask = msg + msg_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 ssl_bitmask_set(bitmask, frag_off, frag_len);
4283 hs_buf->is_complete = (ssl_bitmask_check(bitmask,
4284 msg_len) == 0);
4285 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004286 hs_buf->is_complete = 1;
4287 }
4288
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
4290 hs_buf->is_complete ? "" : "not yet "));
Hanno Becker37f95322018-08-16 13:55:32 +01004291 }
4292
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004293 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004294 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004295
4296 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004297 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004298 break;
4299 }
4300
4301exit:
4302
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
4304 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004305}
4306#endif /* MBEDTLS_SSL_PROTO_DTLS */
4307
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004309static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004310{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004311 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004312 * Consume last content-layer message and potentially
4313 * update in_msglen which keeps track of the contents'
4314 * consumption state.
4315 *
4316 * (1) Handshake messages:
4317 * Remove last handshake message, move content
4318 * and adapt in_msglen.
4319 *
4320 * (2) Alert messages:
4321 * Consume whole record content, in_msglen = 0.
4322 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004323 * (3) Change cipher spec:
4324 * Consume whole record content, in_msglen = 0.
4325 *
4326 * (4) Application data:
4327 * Don't do anything - the record layer provides
4328 * the application data as a stream transport
4329 * and consumes through mbedtls_ssl_read only.
4330 *
4331 */
4332
4333 /* Case (1): Handshake messages */
Gilles Peskine449bd832023-01-11 14:50:10 +01004334 if (ssl->in_hslen != 0) {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004335 /* Hard assertion to be sure that no application data
4336 * is in flight, as corrupting ssl->in_msglen during
4337 * ssl->in_offt != NULL is fatal. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004338 if (ssl->in_offt != NULL) {
4339 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4340 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004341 }
4342
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004343 /*
4344 * Get next Handshake message in the current record
4345 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004346
Hanno Becker4a810fb2017-05-24 16:27:30 +01004347 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004348 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004349 * current handshake content: If DTLS handshake
4350 * fragmentation is used, that's the fragment
4351 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004352 * size here is faulty and should be changed at
4353 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004354 * (2) While it doesn't seem to cause problems, one
4355 * has to be very careful not to assume that in_hslen
4356 * is always <= in_msglen in a sensible communication.
4357 * Again, it's wrong for DTLS handshake fragmentation.
4358 * The following check is therefore mandatory, and
4359 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004360 * Additionally, ssl->in_hslen might be arbitrarily out of
4361 * bounds after handling a DTLS message with an unexpected
4362 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (ssl->in_hslen < ssl->in_msglen) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004365 ssl->in_msglen -= ssl->in_hslen;
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4367 ssl->in_msglen);
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004368
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
4370 ssl->in_msg, ssl->in_msglen);
4371 } else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004372 ssl->in_msglen = 0;
4373 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004374
Hanno Becker4a810fb2017-05-24 16:27:30 +01004375 ssl->in_hslen = 0;
4376 }
4377 /* Case (4): Application data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 else if (ssl->in_offt != NULL) {
4379 return 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01004380 }
4381 /* Everything else (CCS & Alerts) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004383 ssl->in_msglen = 0;
4384 }
4385
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 return 0;
Hanno Becker1097b342018-08-15 14:09:41 +01004387}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004388
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004389MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004390static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
Hanno Beckere74d5562018-08-15 14:26:08 +01004391{
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 if (ssl->in_msglen > 0) {
4393 return 1;
4394 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 return 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004397}
4398
Hanno Becker5f066e72018-08-16 14:56:31 +01004399#if defined(MBEDTLS_SSL_PROTO_DTLS)
4400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004402{
4403 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 if (hs == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004405 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004407
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 if (hs->buffering.future_record.data != NULL) {
Hanno Becker01315ea2018-08-21 17:22:17 +01004409 hs->buffering.total_bytes_buffered -=
4410 hs->buffering.future_record.len;
4411
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 mbedtls_free(hs->buffering.future_record.data);
Hanno Becker01315ea2018-08-21 17:22:17 +01004413 hs->buffering.future_record.data = NULL;
4414 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004415}
4416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004417MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004418static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004419{
4420 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 unsigned char *rec;
Hanno Becker5f066e72018-08-16 14:56:31 +01004422 size_t rec_len;
4423 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004424#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4425 size_t in_buf_len = ssl->in_buf_len;
4426#else
4427 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4428#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4430 return 0;
4431 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (hs == NULL) {
4434 return 0;
4435 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004436
Hanno Becker5f066e72018-08-16 14:56:31 +01004437 rec = hs->buffering.future_record.data;
4438 rec_len = hs->buffering.future_record.len;
4439 rec_epoch = hs->buffering.future_record.epoch;
4440
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 if (rec == NULL) {
4442 return 0;
4443 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004444
Hanno Becker4cb782d2018-08-20 11:19:05 +01004445 /* Only consider loading future records if the
4446 * input buffer is empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (ssl_next_record_is_in_datagram(ssl) == 1) {
4448 return 0;
4449 }
Hanno Becker4cb782d2018-08-20 11:19:05 +01004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 if (rec_epoch != ssl->in_epoch) {
4454 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
Hanno Becker5f066e72018-08-16 14:56:31 +01004455 goto exit;
4456 }
4457
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004459
4460 /* Double-check that the record is not too large */
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
4462 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4463 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker5f066e72018-08-16 14:56:31 +01004464 }
4465
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 memcpy(ssl->in_hdr, rec, rec_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004467 ssl->in_left = rec_len;
4468 ssl->next_record_offset = 0;
4469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 ssl_free_buffered_record(ssl);
Hanno Becker5f066e72018-08-16 14:56:31 +01004471
4472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
4474 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004475}
4476
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004478static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
4479 mbedtls_record const *rec)
Hanno Becker5f066e72018-08-16 14:56:31 +01004480{
4481 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004482
4483 /* Don't buffer future records outside handshakes. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (hs == NULL) {
4485 return 0;
4486 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004487
4488 /* Only buffer handshake records (we are only interested
4489 * in Finished messages). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
4491 return 0;
4492 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004493
4494 /* Don't buffer more than one future epoch record. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 if (hs->buffering.future_record.data != NULL) {
4496 return 0;
4497 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004498
Hanno Becker01315ea2018-08-21 17:22:17 +01004499 /* Don't buffer record if there's not enough buffering space remaining. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4501 hs->buffering.total_bytes_buffered)) {
4502 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4503 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4504 " (already %" MBEDTLS_PRINTF_SIZET
4505 " bytes buffered) -- ignore\n",
4506 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4507 hs->buffering.total_bytes_buffered));
4508 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004509 }
4510
Hanno Becker5f066e72018-08-16 14:56:31 +01004511 /* Buffer record */
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
4513 ssl->in_epoch + 1U));
4514 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004515
4516 /* ssl_parse_record_header() only considers records
4517 * of the next epoch as candidates for buffering. */
4518 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004519 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004520
4521 hs->buffering.future_record.data =
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 mbedtls_calloc(1, hs->buffering.future_record.len);
4523 if (hs->buffering.future_record.data == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004524 /* If we run out of RAM trying to buffer a
4525 * record from the next epoch, just ignore. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004527 }
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004530
Hanno Becker519f15d2019-07-11 12:43:20 +01004531 hs->buffering.total_bytes_buffered += rec->buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004532 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004533}
4534
4535#endif /* MBEDTLS_SSL_PROTO_DTLS */
4536
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004538static int ssl_get_next_record(mbedtls_ssl_context *ssl)
Hanno Becker1097b342018-08-15 14:09:41 +01004539{
Janos Follath865b3eb2019-12-16 11:46:15 +00004540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004541 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004542
Hanno Becker5f066e72018-08-16 14:56:31 +01004543#if defined(MBEDTLS_SSL_PROTO_DTLS)
4544 /* We might have buffered a future record; if so,
4545 * and if the epoch matches now, load it.
4546 * On success, this call will set ssl->in_left to
4547 * the length of the buffered record, so that
4548 * the calls to ssl_fetch_input() below will
4549 * essentially be no-ops. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 ret = ssl_load_buffered_record(ssl);
4551 if (ret != 0) {
4552 return ret;
4553 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004554#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004555
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004556 /* Ensure that we have enough space available for the default form
4557 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4558 * with no space for CIDs counted in). */
Gilles Peskine449bd832023-01-11 14:50:10 +01004559 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
4560 if (ret != 0) {
4561 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4562 return ret;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004563 }
4564
Gilles Peskine449bd832023-01-11 14:50:10 +01004565 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
4566 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004568 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4569 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
4570 ret = ssl_buffer_future_record(ssl, &rec);
4571 if (ret != 0) {
4572 return ret;
4573 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004574
4575 /* Fall through to handling of unexpected records */
4576 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4577 }
4578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
Hanno Becker2fddd372019-07-10 14:37:41 +01004580#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004581 /* Reset in pointers to default state for TLS/DTLS records,
4582 * assuming no CID and no offset between record content and
4583 * record plaintext. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004584 mbedtls_ssl_update_in_pointers(ssl);
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004585
Hanno Becker7ae20e02019-07-12 08:33:49 +01004586 /* Setup internal message pointers from record structure. */
4587 ssl->in_msgtype = rec.type;
4588#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4589 ssl->in_len = ssl->in_cid + rec.cid_len;
4590#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4591 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4592 ssl->in_msglen = rec.data_len;
4593
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 ret = ssl_check_client_reconnect(ssl);
4595 MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
4596 if (ret != 0) {
4597 return ret;
4598 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004599#endif
4600
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004601 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004602 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
4605 "(header)"));
4606 } else {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004607 /* Skip invalid record and the rest of the datagram */
4608 ssl->next_record_offset = 0;
4609 ssl->in_left = 0;
4610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
4612 "(header)"));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004613 }
4614
4615 /* Get next record */
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4617 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004618#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004619 {
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 return ret;
Hanno Becker2fddd372019-07-10 14:37:41 +01004621 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004622 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckera8814792019-07-10 15:01:45 +01004626 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004627 ssl->next_record_offset = rec.buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if (ssl->next_record_offset < ssl->in_left) {
4629 MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
Hanno Beckere65ce782017-05-22 14:47:48 +01004630 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 } else
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004632#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004633 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004634 /*
4635 * Fetch record contents from underlying transport.
4636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
4638 if (ret != 0) {
4639 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4640 return ret;
Hanno Beckera8814792019-07-10 15:01:45 +01004641 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004642
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004643 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004644 }
4645
4646 /*
4647 * Decrypt record contents.
4648 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004653 /* Silently discard invalid records */
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004655 /* Except when waiting for Finished as a bad mac here
4656 * probably means something went wrong in the handshake
4657 * (eg wrong psk used, mitm downgrade attempt, etc.) */
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4659 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004660#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4662 mbedtls_ssl_send_alert_message(ssl,
4663 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4664 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004665 }
4666#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 return ret;
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004668 }
4669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if (ssl->conf->badmac_limit != 0 &&
4671 ++ssl->badmac_seen >= ssl->conf->badmac_limit) {
4672 MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
4673 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004674 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004675
Hanno Becker4a810fb2017-05-24 16:27:30 +01004676 /* As above, invalid records cause
4677 * dismissal of the whole datagram. */
4678
4679 ssl->next_record_offset = 0;
4680 ssl->in_left = 0;
4681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
4683 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004684 }
4685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 return ret;
4687 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004688#endif
4689 {
4690 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4693 mbedtls_ssl_send_alert_message(ssl,
4694 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4695 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004696 }
4697#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 return ret;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004699 }
4700 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004701
Hanno Becker44d89b22019-07-12 09:40:44 +01004702
4703 /* Reset in pointers to default state for TLS/DTLS records,
4704 * assuming no CID and no offset between record content and
4705 * record plaintext. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker44d89b22019-07-12 09:40:44 +01004707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4708 ssl->in_len = ssl->in_cid + rec.cid_len;
4709#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004710 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004711
Hanno Becker8685c822019-07-12 09:37:30 +01004712 /* The record content type may change during decryption,
4713 * so re-read it. */
4714 ssl->in_msgtype = rec.type;
4715 /* Also update the input buffer, because unfortunately
4716 * the server-side ssl_parse_client_hello() reparses the
4717 * record header when receiving a ClientHello initiating
4718 * a renegotiation. */
4719 ssl->in_hdr[0] = rec.type;
4720 ssl->in_msg = rec.buf + rec.data_offset;
4721 ssl->in_msglen = rec.data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
Hanno Becker8685c822019-07-12 09:37:30 +01004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01004725}
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01004728{
Janos Follath865b3eb2019-12-16 11:46:15 +00004729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004730
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004731 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004732 * Handle particular types of records
4733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
4735 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
4736 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01004737 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 }
4739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4741 if (ssl->in_msglen != 1) {
4742 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
4743 ssl->in_msglen));
4744 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004745 }
4746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if (ssl->in_msg[0] != 1) {
4748 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
4749 ssl->in_msg[0]));
4750 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004751 }
4752
4753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004754 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01004755 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
4757 if (ssl->handshake == NULL) {
4758 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
4759 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004760 }
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
4763 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004764 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004765#endif
Ronald Cron7e38cba2021-11-24 12:43:39 +01004766
Ronald Cron6f135e12021-12-08 16:57:54 +01004767#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Ronald Cron7e38cba2021-11-24 12:43:39 +01004769#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 MBEDTLS_SSL_DEBUG_MSG(1,
4771 ("Ignore ChangeCipherSpec in TLS 1.3 compatibility mode"));
4772 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Ronald Cron7e38cba2021-11-24 12:43:39 +01004773#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 MBEDTLS_SSL_DEBUG_MSG(1,
4775 ("ChangeCipherSpec invalid in TLS 1.3 without compatibility mode"));
4776 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Ronald Cron7e38cba2021-11-24 12:43:39 +01004777#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
4778 }
Ronald Cron6f135e12021-12-08 16:57:54 +01004779#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere678eaa2018-08-21 14:57:46 +01004780 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
4783 if (ssl->in_msglen != 2) {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004784 /* Note: Standard allows for more than one 2 byte alert
4785 to be packed in a single message, but Mbed TLS doesn't
4786 currently support this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
4788 ssl->in_msglen));
4789 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004790 }
4791
Gilles Peskine449bd832023-01-11 14:50:10 +01004792 MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
4793 ssl->in_msg[0], ssl->in_msg[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00004794
4795 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004796 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
4799 MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
4800 ssl->in_msg[1]));
4801 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 }
4803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4805 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
4806 MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
4807 return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004808 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004809
4810#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004811 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4812 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
4813 MBEDTLS_SSL_DEBUG_MSG(2, ("is a no renegotiation alert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004814 /* Will be handled when trying to parse ServerHello */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004816 }
4817#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004818 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004819 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 }
4821
Hanno Beckerc76c6192017-06-06 10:03:17 +01004822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker37ae9522019-05-03 16:54:26 +01004824 /* Drop unexpected ApplicationData records,
4825 * except at the beginning of renegotiations */
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4827 mbedtls_ssl_is_handshake_over(ssl) == 0
Hanno Becker37ae9522019-05-03 16:54:26 +01004828#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4830 ssl->state == MBEDTLS_SSL_SERVER_HELLO)
Hanno Beckerc76c6192017-06-06 10:03:17 +01004831#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 ) {
4833 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
4834 return MBEDTLS_ERR_SSL_NON_FATAL;
Hanno Becker37ae9522019-05-03 16:54:26 +01004835 }
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 if (ssl->handshake != NULL &&
4838 mbedtls_ssl_is_handshake_over(ssl) == 1) {
4839 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Hanno Becker37ae9522019-05-03 16:54:26 +01004840 }
4841 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004842#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004843
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845}
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004848{
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 return mbedtls_ssl_send_alert_message(ssl,
4850 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4851 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004852}
4853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
4855 unsigned char level,
4856 unsigned char message)
Paul Bakker0a925182012-04-16 06:46:41 +00004857{
Janos Follath865b3eb2019-12-16 11:46:15 +00004858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 if (ssl == NULL || ssl->conf == NULL) {
4861 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4862 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (ssl->out_left != 0) {
4865 return mbedtls_ssl_flush_output(ssl);
4866 }
Hanno Becker5e18f742018-08-06 11:35:16 +01004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
4869 MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
Paul Bakker0a925182012-04-16 06:46:41 +00004870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004872 ssl->out_msglen = 2;
4873 ssl->out_msg[0] = level;
4874 ssl->out_msg[1] = message;
4875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
4877 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
4878 return ret;
Paul Bakker0a925182012-04-16 06:46:41 +00004879 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004880 MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
Paul Bakker0a925182012-04-16 06:46:41 +00004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 return 0;
Paul Bakker0a925182012-04-16 06:46:41 +00004883}
4884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004886{
Janos Follath865b3eb2019-12-16 11:46:15 +00004887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004891 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 ssl->out_msglen = 1;
4893 ssl->out_msg[0] = 1;
4894
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 ssl->state++;
4896
Gilles Peskine449bd832023-01-11 14:50:10 +01004897 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4898 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4899 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 }
4901
Gilles Peskine449bd832023-01-11 14:50:10 +01004902 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004903
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004905}
4906
Gilles Peskine449bd832023-01-11 14:50:10 +01004907int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004908{
Janos Follath865b3eb2019-12-16 11:46:15 +00004909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004910
Gilles Peskine449bd832023-01-11 14:50:10 +01004911 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004912
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
4914 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
4915 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004916 }
4917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4919 MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
4920 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4921 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
4922 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004923 }
4924
Hanno Beckere678eaa2018-08-21 14:57:46 +01004925 /* CCS records are only accepted if they have length 1 and content '1',
4926 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004927
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004928 /*
4929 * Switch to our negotiated transform and session parameters for inbound
4930 * data.
4931 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
Jerry Yu2e199812022-12-01 18:57:19 +08004933#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004934 ssl->transform_in = ssl->transform_negotiate;
Jerry Yu2e199812022-12-01 18:57:19 +08004935#endif
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004936 ssl->session_in = ssl->session_negotiate;
4937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004938#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004939 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004940#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004941 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004942#endif
4943
4944 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 if (++ssl->in_epoch == 0) {
4946 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004947 /* This is highly unlikely to happen for legitimate reasons, so
4948 treat it as an attack and don't send an alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004949 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004950 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004952#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004953 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 mbedtls_ssl_update_in_pointers(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004956
Paul Bakker5121ce52009-01-03 21:22:43 +00004957 ssl->state++;
4958
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004960
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004962}
4963
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004964/* Once ssl->out_hdr as the address of the beginning of the
4965 * next outgoing record is set, deduce the other pointers.
4966 *
4967 * Note: For TLS, we save the implicit record sequence number
4968 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4969 * and the caller has to make sure there's space for this.
4970 */
4971
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004972static size_t ssl_transform_get_explicit_iv_len(
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 mbedtls_ssl_transform const *transform)
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004974{
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 return transform->ivlen - transform->fixed_ivlen;
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004976}
4977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
4979 mbedtls_ssl_transform *transform)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004980{
4981#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004983 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004984#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08004985 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004986 ssl->out_len = ssl->out_cid;
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 if (transform != NULL) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004988 ssl->out_len += transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004989 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004990#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08004991 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004992#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004993 ssl->out_iv = ssl->out_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004994 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004995#endif
4996 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004997 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004998#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004999 ssl->out_cid = ssl->out_len;
5000#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005001 ssl->out_iv = ssl->out_hdr + 5;
5002 }
5003
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005004 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005005 /* Adjust out_msg to make space for explicit IV, if used. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005006 if (transform != NULL) {
5007 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
5008 }
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005009}
5010
5011/* Once ssl->in_hdr as the address of the beginning of the
5012 * next incoming record is set, deduce the other pointers.
5013 *
5014 * Note: For TLS, we save the implicit record sequence number
5015 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5016 * and the caller has to make sure there's space for this.
5017 */
5018
Gilles Peskine449bd832023-01-11 14:50:10 +01005019void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005020{
Hanno Becker79594fd2019-05-08 09:38:41 +01005021 /* This function sets the pointers to match the case
5022 * of unprotected TLS/DTLS records, with both ssl->in_iv
5023 * and ssl->in_msg pointing to the beginning of the record
5024 * content.
5025 *
5026 * When decrypting a protected record, ssl->in_msg
5027 * will be shifted to point to the beginning of the
5028 * record plaintext.
5029 */
5030
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005033 /* This sets the header pointers to match records
5034 * without CID. When we receive a record containing
5035 * a CID, the fields are shifted accordingly in
5036 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005037 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005038#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08005039 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005040 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005041#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08005042 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005043#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005044 ssl->in_iv = ssl->in_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005046#endif
5047 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08005048 ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005049 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005050#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005051 ssl->in_cid = ssl->in_len;
5052#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005053 ssl->in_iv = ssl->in_hdr + 5;
5054 }
5055
Hanno Becker79594fd2019-05-08 09:38:41 +01005056 /* This will be adjusted at record decryption time. */
5057 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005058}
5059
Paul Bakker5121ce52009-01-03 21:22:43 +00005060/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005061 * Setup an SSL context
5062 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005065{
5066 /* Set the incoming and outgoing record pointers. */
5067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005068 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005069 ssl->out_hdr = ssl->out_buf;
5070 ssl->in_hdr = ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01005071 } else
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005072#endif /* MBEDTLS_SSL_PROTO_DTLS */
5073 {
Hanno Becker12078f42021-03-02 15:28:41 +00005074 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005075 ssl->out_hdr = ssl->out_buf + 8;
5076 ssl->in_hdr = ssl->in_buf + 8;
5077 }
5078
5079 /* Derive other internal pointers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
5081 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005082}
5083
Paul Bakker5121ce52009-01-03 21:22:43 +00005084/*
5085 * SSL get accessors
5086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005087size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005088{
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00005090}
5091
Gilles Peskine449bd832023-01-11 14:50:10 +01005092int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
Hanno Becker8b170a02017-10-10 11:51:19 +01005093{
5094 /*
5095 * Case A: We're currently holding back
5096 * a message for further processing.
5097 */
5098
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (ssl->keep_current_message == 1) {
5100 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
5101 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005102 }
5103
5104 /*
5105 * Case B: Further records are pending in the current datagram.
5106 */
5107
5108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005109 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5110 ssl->in_left > ssl->next_record_offset) {
5111 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
5112 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005113 }
5114#endif /* MBEDTLS_SSL_PROTO_DTLS */
5115
5116 /*
5117 * Case C: A handshake message is being processed.
5118 */
5119
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
5121 MBEDTLS_SSL_DEBUG_MSG(3,
5122 ("ssl_check_pending: more handshake messages within current record"));
5123 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005124 }
5125
5126 /*
5127 * Case D: An application data message is being processed
5128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 if (ssl->in_offt != NULL) {
5130 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
5131 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005132 }
5133
5134 /*
5135 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005136 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005137 * we implement support for multiple alerts in single records.
5138 */
5139
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
5141 return 0;
Hanno Becker8b170a02017-10-10 11:51:19 +01005142}
5143
Paul Bakker43ca69c2011-01-15 17:35:19 +00005144
Gilles Peskine449bd832023-01-11 14:50:10 +01005145int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005146{
Hanno Becker3136ede2018-08-17 15:28:19 +01005147 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005149 unsigned block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005150#if defined(MBEDTLS_USE_PSA_CRYPTO)
5151 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
5152 psa_key_type_t key_type;
5153#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005154
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker5903de42019-05-03 14:46:38 +01005156
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 if (transform == NULL) {
5158 return (int) out_hdr_len;
5159 }
Hanno Becker78640902018-08-13 16:35:15 +01005160
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005161
5162#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (transform->psa_alg == PSA_ALG_GCM ||
5164 transform->psa_alg == PSA_ALG_CCM ||
5165 transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 8) ||
5166 transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 ||
5167 transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) {
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005168 transform_expansion = transform->minlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 } else if (transform->psa_alg == PSA_ALG_CBC_NO_PADDING) {
5170 (void) psa_get_key_attributes(transform->psa_key_enc, &attr);
5171 key_type = psa_get_key_type(&attr);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005172
Gilles Peskine449bd832023-01-11 14:50:10 +01005173 block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005174
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005175 /* Expansion due to the addition of the MAC. */
5176 transform_expansion += transform->maclen;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005177
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005178 /* Expansion due to the addition of CBC padding;
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005179 * Theoretically up to 256 bytes, but we never use
5180 * more than the block size of the underlying cipher. */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005181 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005182
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005183 /* For TLS 1.2 or higher, an explicit IV is added
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005184 * after the record header. */
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005185#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005186 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005187#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 } else {
5189 MBEDTLS_SSL_DEBUG_MSG(1,
5190 ("Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()"));
5191 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005192 }
5193#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005194 switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 case MBEDTLS_MODE_GCM:
5196 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005197 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005199 transform_expansion = transform->minlen;
5200 break;
5201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005202 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005203
5204 block_size = mbedtls_cipher_get_block_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 &transform->cipher_ctx_enc);
Hanno Becker5b559ac2018-08-03 09:40:07 +01005206
Hanno Becker3136ede2018-08-17 15:28:19 +01005207 /* Expansion due to the addition of the MAC. */
5208 transform_expansion += transform->maclen;
5209
5210 /* Expansion due to the addition of CBC padding;
5211 * Theoretically up to 256 bytes, but we never use
5212 * more than the block size of the underlying cipher. */
5213 transform_expansion += block_size;
5214
TRodziewicz4ca18aa2021-05-20 14:46:20 +02005215 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01005216 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02005217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005218 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02005219#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005220
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005221 break;
5222
5223 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5225 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005226 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005227#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005228
Hanno Beckera0e20d02019-05-15 14:03:01 +01005229#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 if (transform->out_cid_len != 0) {
Hanno Becker6cbad552019-05-08 15:40:11 +01005231 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Gilles Peskine449bd832023-01-11 14:50:10 +01005232 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005233#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005234
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 return (int) (out_hdr_len + transform_expansion);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005236}
5237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005239/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005240 * Check record counters and renegotiate if they're above the limit.
5241 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005243static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005244{
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 size_t ep_len = mbedtls_ssl_ep_len(ssl);
Andres AG2196c7f2016-12-15 17:01:16 +00005246 int in_ctr_cmp;
5247 int out_ctr_cmp;
5248
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 if (mbedtls_ssl_is_handshake_over(ssl) == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005250 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5252 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005253 }
5254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
5256 &ssl->conf->renego_period[ep_len],
5257 MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len);
5258 out_ctr_cmp = memcmp(&ssl->cur_out_ctr[ep_len],
Jerry Yud9a94fe2021-09-28 18:58:59 +08005259 &ssl->conf->renego_period[ep_len],
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 sizeof(ssl->cur_out_ctr) - ep_len);
Andres AG2196c7f2016-12-15 17:01:16 +00005261
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
5263 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005264 }
5265
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
5267 return mbedtls_ssl_renegotiate(ssl);
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005268}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005270
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005271#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5272
5273#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Jerry Yua0446a02022-07-13 11:22:55 +08005274MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005275static int ssl_tls13_check_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005276{
5277
Gilles Peskine449bd832023-01-11 14:50:10 +01005278 if ((ssl->in_hslen == mbedtls_ssl_hs_hdr_len(ssl)) ||
5279 (ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET)) {
5280 return 0;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005281 }
5282
5283 ssl->keep_current_message = 1;
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 MBEDTLS_SSL_DEBUG_MSG(3, ("NewSessionTicket received"));
5286 mbedtls_ssl_handshake_set_state(ssl,
5287 MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005288
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 return MBEDTLS_ERR_SSL_WANT_READ;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005290}
5291#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5292
Jerry Yua0446a02022-07-13 11:22:55 +08005293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005294static int ssl_tls13_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005295{
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 MBEDTLS_SSL_DEBUG_MSG(3, ("received post-handshake message"));
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005298
5299#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
5301 int ret = ssl_tls13_check_new_session_ticket(ssl);
5302 if (ret != 0) {
5303 return ret;
5304 }
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005305 }
5306#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5307
5308 /* Fail in all other cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005310}
5311#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5312
5313#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005314/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005315 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005316 * may only be sent for the purpose of initiating renegotiations.
5317 *
5318 * This function is introduced as a separate helper since the handling
5319 * of post-handshake handshake messages changes significantly in TLS 1.3,
5320 * and having a helper function allows to distinguish between TLS <= 1.2 and
5321 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5322 */
Jerry Yua0446a02022-07-13 11:22:55 +08005323MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005324static int ssl_tls12_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005325{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005327
5328 /*
5329 * - For client-side, expect SERVER_HELLO_REQUEST.
5330 * - For server-side, expect CLIENT_HELLO.
5331 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5332 */
5333
5334#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5336 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5337 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
5338 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005339
5340 /* With DTLS, drop the packet (probably from last handshake) */
5341#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5343 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005344 }
5345#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005347 }
5348#endif /* MBEDTLS_SSL_CLI_C */
5349
5350#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5352 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
5353 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005354
5355 /* With DTLS, drop the packet (probably from last handshake) */
5356#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5358 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005359 }
5360#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005362 }
5363#endif /* MBEDTLS_SSL_SRV_C */
5364
5365#if defined(MBEDTLS_SSL_RENEGOTIATION)
5366 /* Determine whether renegotiation attempt should be accepted */
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5368 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5369 ssl->conf->allow_legacy_renegotiation ==
5370 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005371 /*
5372 * Accept renegotiation request
5373 */
5374
5375 /* DTLS clients need to know renego is server-initiated */
5376#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5378 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005379 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5380 }
5381#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 ret = mbedtls_ssl_start_renegotiation(ssl);
5383 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5384 ret != 0) {
5385 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
5386 ret);
5387 return ret;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005388 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 } else
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005390#endif /* MBEDTLS_SSL_RENEGOTIATION */
5391 {
5392 /*
5393 * Refuse renegotiation
5394 */
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5399 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5400 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) != 0) {
5401 return ret;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005402 }
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005403 }
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 return 0;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005406}
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005407#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5408
5409MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005410static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl)
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005411{
5412 /* Check protocol version and dispatch accordingly. */
5413#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
5415 return ssl_tls13_handle_hs_message_post_handshake(ssl);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005416 }
5417#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5418
5419#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 if (ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
5421 return ssl_tls12_handle_hs_message_post_handshake(ssl);
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005422 }
5423#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5424
5425 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005427}
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005428
Paul Bakker48916f92012-09-16 19:57:18 +00005429/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005430 * Receive application data decrypted from the SSL layer
5431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005432int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005433{
Janos Follath865b3eb2019-12-16 11:46:15 +00005434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005435 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005436
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (ssl == NULL || ssl->conf == NULL) {
5438 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5439 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005443#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5445 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5446 return ret;
5447 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 if (ssl->handshake != NULL &&
5450 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
5451 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
5452 return ret;
5453 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005454 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005455 }
5456#endif
5457
Hanno Becker4a810fb2017-05-24 16:27:30 +01005458 /*
5459 * Check if renegotiation is necessary and/or handshake is
5460 * in process. If yes, perform/continue, and fall through
5461 * if an unexpected packet is received while the client
5462 * is waiting for the ServerHello.
5463 *
5464 * (There is no equivalent to the last condition on
5465 * the server-side as it is not treated as within
5466 * a handshake while waiting for the ClientHello
5467 * after a renegotiation request.)
5468 */
5469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 ret = ssl_check_ctr_renegotiate(ssl);
5472 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5473 ret != 0) {
5474 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5475 return ret;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005476 }
5477#endif
5478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5480 ret = mbedtls_ssl_handshake(ssl);
5481 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5482 ret != 0) {
5483 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5484 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005485 }
5486 }
5487
Hanno Beckere41158b2017-10-23 13:30:32 +01005488 /* Loop as long as no application data record is available */
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 while (ssl->in_offt == NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005490 /* Start timer if not already running */
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 if (ssl->f_get_timer != NULL &&
5492 ssl->f_get_timer(ssl->p_timer) == -1) {
5493 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005494 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005495
Gilles Peskine449bd832023-01-11 14:50:10 +01005496 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5497 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5498 return 0;
5499 }
Paul Bakker831a7552011-05-18 13:32:51 +00005500
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5502 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005503 }
5504
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 if (ssl->in_msglen == 0 &&
5506 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 /*
5508 * OpenSSL sends empty messages to randomize the IV
5509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5511 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5512 return 0;
5513 }
Paul Bakker831a7552011-05-18 13:32:51 +00005514
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5516 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005517 }
5518 }
5519
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
5521 ret = ssl_handle_hs_message_post_handshake(ssl);
5522 if (ret != 0) {
5523 MBEDTLS_SSL_DEBUG_RET(1, "ssl_handle_hs_message_post_handshake",
5524 ret);
5525 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00005526 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005527
Hanno Beckerf26cc722021-04-21 07:30:13 +01005528 /* At this point, we don't know whether the renegotiation triggered
5529 * by the post-handshake message has been completed or not. The cases
5530 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005531 * 1) The renegotiation is complete. In this case, no new record
5532 * has been read yet.
5533 * 2) The renegotiation is incomplete because the client received
5534 * an application data record while awaiting the ServerHello.
5535 * 3) The renegotiation is incomplete because the client received
5536 * a non-handshake, non-application data message while awaiting
5537 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005538 *
5539 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005540 * - For 1), the next iteration will read a new record and check
5541 * if it's application data.
5542 * - For 2), the loop condition isn't satisfied as application data
5543 * is present, hence continue is the same as break
5544 * - For 3), the loop condition is satisfied and read_record
5545 * will re-deliver the message that was held back by the client
5546 * when expecting the ServerHello.
5547 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005548
Hanno Becker90333da2017-10-10 11:27:13 +01005549 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005550 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005551#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005552 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5553 if (ssl->conf->renego_max_records >= 0) {
5554 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
5555 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
5556 "but not honored by client"));
5557 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005558 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005559 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005560 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005563 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
5565 MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
5566 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005567 }
5568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
5570 MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
5571 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005572 }
5573
5574 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005575
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005576 /* We're going to return something now, cancel timer,
5577 * except if handshake (renegotiation) is in progress */
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
5579 mbedtls_ssl_set_timer(ssl, 0);
5580 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005581
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005583 /* If we requested renego but received AppData, resend HelloRequest.
5584 * Do it now, after setting in_offt, to avoid taking this branch
5585 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5588 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5589 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
5590 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
5591 ret);
5592 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005593 }
5594 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005596#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005597 }
5598
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 n = (len < ssl->in_msglen)
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 ? len : ssl->in_msglen;
5601
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 memcpy(buf, ssl->in_offt, n);
Paul Bakker5121ce52009-01-03 21:22:43 +00005603 ssl->in_msglen -= n;
5604
gabor-mezei-arma3214132020-07-15 10:55:00 +02005605 /* Zeroising the plaintext buffer to erase unused application data
5606 from the memory. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 mbedtls_platform_zeroize(ssl->in_offt, n);
gabor-mezei-arma3214132020-07-15 10:55:00 +02005608
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 if (ssl->in_msglen == 0) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005610 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005612 ssl->keep_current_message = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 /* more data available */
5615 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005617
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 return (int) n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005621}
5622
5623/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005624 * Send application data to be encrypted by the SSL layer, taking care of max
5625 * fragment length and buffer size.
5626 *
5627 * According to RFC 5246 Section 6.2.1:
5628 *
5629 * Zero-length fragments of Application data MAY be sent as they are
5630 * potentially useful as a traffic analysis countermeasure.
5631 *
5632 * Therefore, it is possible that the input message length is 0 and the
5633 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005635MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005636static int ssl_write_real(mbedtls_ssl_context *ssl,
5637 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005638{
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005640 const size_t max_len = (size_t) ret;
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 if (ret < 0) {
5643 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
5644 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005645 }
5646
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 if (len > max_len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005648#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5650 MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
5651 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5652 " > %" MBEDTLS_PRINTF_SIZET,
5653 len, max_len));
5654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5655 } else
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005656#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 len = max_len;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005658 }
Paul Bakker887bd502011-06-08 13:10:54 +00005659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 if (ssl->out_left != 0) {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005661 /*
5662 * The user has previously tried to send the data and
5663 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5664 * written. In this case, we expect the high-level write function
5665 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
5669 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 } else {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005672 /*
5673 * The user is trying to send a message the first time, so we need to
5674 * copy the data into the internal buffers and setup the data structure
5675 * to keep track of partial writes
5676 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005677 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 memcpy(ssl->out_msg, buf, len);
Paul Bakker887bd502011-06-08 13:10:54 +00005680
Gilles Peskine449bd832023-01-11 14:50:10 +01005681 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
5682 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
5683 return ret;
Paul Bakker887bd502011-06-08 13:10:54 +00005684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005685 }
5686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 return (int) len;
Paul Bakker5121ce52009-01-03 21:22:43 +00005688}
5689
5690/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005691 * Write application data (public-facing wrapper)
5692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005693int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005694{
Janos Follath865b3eb2019-12-16 11:46:15 +00005695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005698
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 if (ssl == NULL || ssl->conf == NULL) {
5700 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5701 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005702
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005703#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
5705 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5706 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005707 }
5708#endif
5709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5711 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5712 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5713 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005714 }
5715 }
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 ret = ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005722}
5723
5724/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 * Notify the peer that the connection is being closed
5726 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005727int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005728{
Janos Follath865b3eb2019-12-16 11:46:15 +00005729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 if (ssl == NULL || ssl->conf == NULL) {
5732 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5733 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if (mbedtls_ssl_is_handshake_over(ssl) == 1) {
5738 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5739 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5740 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
5741 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
5742 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005743 }
5744 }
5745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005749}
5750
Gilles Peskine449bd832023-01-11 14:50:10 +01005751void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Paul Bakker48916f92012-09-16 19:57:18 +00005752{
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (transform == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005754 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005756
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005757#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 psa_destroy_key(transform->psa_key_enc);
5759 psa_destroy_key(transform->psa_key_dec);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005760#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 mbedtls_cipher_free(&transform->cipher_ctx_enc);
5762 mbedtls_cipher_free(&transform->cipher_ctx_dec);
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005764
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005765#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01005766#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 psa_destroy_key(transform->psa_mac_enc);
5768 psa_destroy_key(transform->psa_mac_dec);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01005769#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 mbedtls_md_free(&transform->md_ctx_enc);
5771 mbedtls_md_free(&transform->md_ctx_dec);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01005772#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd56ed242018-01-03 15:32:51 +00005773#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
Paul Bakker48916f92012-09-16 19:57:18 +00005776}
5777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778void mbedtls_ssl_set_inbound_transform(mbedtls_ssl_context *ssl,
5779 mbedtls_ssl_transform *transform)
Jerry Yuc7875b52021-09-05 21:05:50 +08005780{
Jerry Yuc7875b52021-09-05 21:05:50 +08005781 ssl->transform_in = transform;
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 memset(ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuc7875b52021-09-05 21:05:50 +08005783}
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785void mbedtls_ssl_set_outbound_transform(mbedtls_ssl_context *ssl,
5786 mbedtls_ssl_transform *transform)
Jerry Yuc7875b52021-09-05 21:05:50 +08005787{
5788 ssl->transform_out = transform;
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yuc7875b52021-09-05 21:05:50 +08005790}
5791
Hanno Becker0271f962018-08-16 13:23:47 +01005792#if defined(MBEDTLS_SSL_PROTO_DTLS)
5793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
Hanno Becker0271f962018-08-16 13:23:47 +01005795{
5796 unsigned offset;
5797 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 if (hs == NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01005800 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 }
Hanno Becker0271f962018-08-16 13:23:47 +01005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 ssl_free_buffered_record(ssl);
Hanno Becker283f5ef2018-08-24 09:34:47 +01005804
Gilles Peskine449bd832023-01-11 14:50:10 +01005805 for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
5806 ssl_buffering_free_slot(ssl, offset);
5807 }
Hanno Beckere605b192018-08-21 15:59:07 +01005808}
5809
Gilles Peskine449bd832023-01-11 14:50:10 +01005810static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
5811 uint8_t slot)
Hanno Beckere605b192018-08-21 15:59:07 +01005812{
5813 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5814 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Beckerb309b922018-08-23 13:18:05 +01005817 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 }
Hanno Beckerb309b922018-08-23 13:18:05 +01005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (hs_buf->is_valid == 1) {
Hanno Beckere605b192018-08-21 15:59:07 +01005821 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005822 mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len);
5823 mbedtls_free(hs_buf->data);
5824 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Hanno Becker0271f962018-08-16 13:23:47 +01005825 }
5826}
5827
5828#endif /* MBEDTLS_SSL_PROTO_DTLS */
5829
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005830/*
5831 * Convert version numbers to/from wire format
5832 * and, for DTLS, to/from TLS equivalent.
5833 *
5834 * For TLS this is the identity.
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005835 * For DTLS, map as follows, then use 1's complement (v -> ~v):
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005836 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005837 * DTLS 1.0 is stored as TLS 1.1 internally
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005839void mbedtls_ssl_write_version(unsigned char version[2], int transport,
5840 mbedtls_ssl_protocol_version tls_version)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005841{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005844 tls_version =
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 ~(tls_version - (tls_version == 0x0302 ? 0x0202 : 0x0201));
5846 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005847#else
5848 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005849#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005850 MBEDTLS_PUT_UINT16_BE(tls_version, version, 0);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005851}
5852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853uint16_t mbedtls_ssl_read_version(const unsigned char version[2],
5854 int transport)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005855{
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 uint16_t tls_version = MBEDTLS_GET_UINT16_BE(version, 0);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005857#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005859 tls_version =
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 ~(tls_version - (tls_version == 0xfeff ? 0x0202 : 0x0201));
5861 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005862#else
5863 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005864#endif
Glenn Strausse3af4cb2022-03-15 03:23:42 -04005865 return tls_version;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005866}
5867
Jerry Yue7047812021-09-13 19:26:39 +08005868/*
Jerry Yu3bf1f972021-09-22 21:37:18 +08005869 * Send pending fatal alert.
5870 * 0, No alert message.
5871 * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
5872 * returned, ssl->alert_reason otherwise.
Jerry Yue7047812021-09-13 19:26:39 +08005873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005874int mbedtls_ssl_handle_pending_alert(mbedtls_ssl_context *ssl)
Jerry Yue7047812021-09-13 19:26:39 +08005875{
5876 int ret;
5877
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005878 /* No pending alert, return success*/
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 if (ssl->send_alert == 0) {
5880 return 0;
5881 }
Jerry Yu394ece62021-09-14 22:17:21 +08005882
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 ret = mbedtls_ssl_send_alert_message(ssl,
5884 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5885 ssl->alert_type);
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005886
Jerry Yu3bf1f972021-09-22 21:37:18 +08005887 /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
5888 * do not clear the alert to be able to send it later.
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005891 ssl->send_alert = 0;
Jerry Yue7047812021-09-13 19:26:39 +08005892 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (ret != 0) {
5895 return ret;
5896 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 return ssl->alert_reason;
Jerry Yue7047812021-09-13 19:26:39 +08005899}
5900
Jerry Yu394ece62021-09-14 22:17:21 +08005901/*
5902 * Set pending fatal alert flag.
5903 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005904void mbedtls_ssl_pend_fatal_alert(mbedtls_ssl_context *ssl,
5905 unsigned char alert_type,
5906 int alert_reason)
Jerry Yu394ece62021-09-14 22:17:21 +08005907{
5908 ssl->send_alert = 1;
5909 ssl->alert_type = alert_type;
5910 ssl->alert_reason = alert_reason;
5911}
5912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005913#endif /* MBEDTLS_SSL_TLS_C */