blob: 8a2ab7b9ba563d507f9a947f54bb81ab39954233 [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/*
21 * The SSL 3.0 specification was drafted by Netscape in 1996,
22 * and became an IETF standard in 1999.
23 *
24 * http://wp.netscape.com/eng/ssl3/
25 * http://www.ietf.org/rfc/rfc2246.txt
26 * http://www.ietf.org/rfc/rfc4346.txt
27 */
28
Gilles Peskinedb09ef62020-06-03 01:43:33 +020029#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
SimonBd5800b72016-04-26 07:43:27 +010033#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020036#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezeic0ae1cf2021-10-20 12:09:35 +020041#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020042#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020043
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <string.h>
45
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050046#if defined(MBEDTLS_USE_PSA_CRYPTO)
47#include "mbedtls/psa_util.h"
48#include "psa/crypto.h"
49#endif
50
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +010056
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020057/*
58 * Start a timer.
59 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020060 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020062{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 if (ssl->f_set_timer == NULL) {
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020064 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 }
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs));
68 ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs);
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020069}
70
71/*
72 * Return -1 is timer is expired, 0 if it isn't.
73 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 if (ssl->f_get_timer == NULL) {
77 return 0;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020078 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (ssl->f_get_timer(ssl->p_timer) == 2) {
81 MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired"));
82 return -1;
83 }
84
85 return 0;
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087
Hanno Beckercfe45792019-07-03 16:13:00 +010088#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +020089MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
91 unsigned char *buf,
92 size_t len,
93 mbedtls_record *rec);
Hanno Becker54229812019-07-12 14:40:00 +010094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl,
96 unsigned char *buf,
97 size_t buflen)
Hanno Beckercfe45792019-07-03 16:13:00 +010098{
Hanno Becker54229812019-07-12 14:40:00 +010099 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record"));
101 MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen);
Hanno Becker54229812019-07-12 14:40:00 +0100102
103 /* We don't support record checking in TLS because
104 * (a) there doesn't seem to be a usecase for it, and
105 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
106 * and we'd need to backup the transform here.
107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
Hanno Becker54229812019-07-12 14:40:00 +0100109 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
110 goto exit;
111 }
112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 else {
irwir734f0cf2019-09-26 21:03:24 +0300114 mbedtls_record rec;
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 ret = ssl_parse_record_header(ssl, buf, buflen, &rec);
117 if (ret != 0) {
118 MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret);
Hanno Becker54229812019-07-12 14:40:00 +0100119 goto exit;
120 }
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if (ssl->transform_in != NULL) {
123 ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec);
124 if (ret != 0) {
125 MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret);
Hanno Becker54229812019-07-12 14:40:00 +0100126 goto exit;
127 }
128 }
129 }
130#endif /* MBEDTLS_SSL_PROTO_DTLS */
131
132exit:
133 /* On success, we have decrypted the buffer in-place, so make
134 * sure we don't leak any plaintext data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_platform_zeroize(buf, buflen);
Hanno Becker54229812019-07-12 14:40:00 +0100136
137 /* For the purpose of this API, treat messages with unexpected CID
138 * as well as such from future epochs as unexpected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
140 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker54229812019-07-12 14:40:00 +0100141 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
142 }
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record"));
145 return ret;
Hanno Beckercfe45792019-07-03 16:13:00 +0100146}
147#endif /* MBEDTLS_SSL_RECORD_CHECKING */
148
Hanno Becker67bc7c32018-08-06 11:33:50 +0100149#define SSL_DONT_FORCE_FLUSH 0
150#define SSL_FORCE_FLUSH 1
151
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200152#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100153
Hanno Beckerd5847772018-08-28 10:09:23 +0100154/* Forward declarations for functions related to message buffering. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
156 uint8_t slot);
157static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200160MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200162MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163static int ssl_buffer_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200164MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
166 mbedtls_record const *rec);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200167MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
Hanno Beckerd5847772018-08-28 10:09:23 +0100169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100171{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
Darryl Greenb33cc762019-11-28 14:29:44 +0000173#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
174 size_t out_buf_len = ssl->out_buf_len;
175#else
176 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
177#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (mtu != 0 && mtu < out_buf_len) {
180 return mtu;
181 }
Hanno Becker2b1e3542018-08-06 11:19:13 +0100182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 return out_buf_len;
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184}
185
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200186MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100188{
Hanno Becker11682cc2018-08-22 14:41:02 +0100189 size_t const bytes_written = ssl->out_left;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 size_t const mtu = ssl_get_maximum_datagram_size(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100191
192 /* Double-check that the write-index hasn't gone
193 * past what we can transmit in a single datagram. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (bytes_written > mtu) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100195 /* Should never happen... */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100197 }
198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 return (int) (mtu - bytes_written);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100200}
201
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200202MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100204{
Janos Follath865b3eb2019-12-16 11:46:15 +0000205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100206 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400207 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100208
209#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 if (max_len > mfl) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100213 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100215
216 /* By the standard (RFC 6066 Sect. 4), the MFL extension
217 * only limits the maximum record payload size, so in theory
218 * we would be allowed to pack multiple records of payload size
219 * MFL into a single datagram. However, this would mean that there's
220 * no way to explicitly communicate MTU restrictions to the peer.
221 *
222 * The following reduction of max_len makes sure that we never
223 * write datagrams larger than MFL + Record Expansion Overhead.
224 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 if (max_len <= ssl->out_left) {
226 return 0;
227 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100228
229 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100230#endif
231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 ret = ssl_get_remaining_space_in_datagram(ssl);
233 if (ret < 0) {
234 return ret;
235 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100236 remaining = (size_t) ret;
237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 ret = mbedtls_ssl_get_record_expansion(ssl);
239 if (ret < 0) {
240 return ret;
241 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100242 expansion = (size_t) ret;
243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 if (remaining <= expansion) {
245 return 0;
246 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100247
248 remaining -= expansion;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 if (remaining >= max_len) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100250 remaining = max_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 return (int) remaining;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100254}
255
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200256/*
257 * Double the retransmit timeout value, within the allowed range,
258 * returning -1 if the maximum value has already been reached.
259 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262{
263 uint32_t new_timeout;
264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) {
266 return -1;
267 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200268
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200269 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
270 * in the following way: after the initial transmission and a first
271 * retransmission, back off to a temporary estimated MTU of 508 bytes.
272 * This value is guaranteed to be deliverable (if not guaranteed to be
273 * delivered) of any compliant IPv4 (and IPv6) network, and should work
274 * on most non-IP stacks too. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200276 ssl->handshake->mtu = 508;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu));
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400278 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200279
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200280 new_timeout = 2 * ssl->handshake->retransmit_timeout;
281
282 /* Avoid arithmetic overflow and range overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if (new_timeout < ssl->handshake->retransmit_timeout ||
284 new_timeout > ssl->conf->hs_timeout_max) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200285 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200286 }
287
288 ssl->handshake->retransmit_timeout = new_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
290 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 return 0;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200293}
294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200297 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100298 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
299 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl,
305 const unsigned char *key_enc, const unsigned char *key_dec,
306 size_t keylen,
307 const unsigned char *iv_enc, const unsigned char *iv_dec,
308 size_t ivlen,
309 const unsigned char *mac_enc, const unsigned char *mac_dec,
310 size_t maclen) = NULL;
311int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction) = NULL;
312int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl) = NULL;
313int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl) = NULL;
314int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl) = NULL;
315int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000317
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100318/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200320 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000321
Hanno Beckerccc13d02020-05-04 12:30:04 +0100322#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
323 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325static size_t ssl_compute_padding_length(size_t len,
326 size_t granularity)
Hanno Becker13996922020-05-28 16:15:19 +0100327{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 return (granularity - (len + 1) % granularity) % granularity;
Hanno Becker13996922020-05-28 16:15:19 +0100329}
330
Hanno Becker581bc1b2020-05-04 12:20:03 +0100331/* This functions transforms a (D)TLS plaintext fragment and a record content
332 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
333 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
334 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100335 *
336 * struct {
337 * opaque content[DTLSPlaintext.length];
338 * ContentType real_type;
339 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100340 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100341 *
342 * Input:
343 * - `content`: The beginning of the buffer holding the
344 * plaintext to be wrapped.
345 * - `*content_size`: The length of the plaintext in Bytes.
346 * - `max_len`: The number of Bytes available starting from
347 * `content`. This must be `>= *content_size`.
348 * - `rec_type`: The desired record content type.
349 *
350 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100351 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
352 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100353 *
354 * Returns:
355 * - `0` on success.
356 * - A negative error code if `max_len` didn't offer enough space
357 * for the expansion.
358 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200359MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360static int ssl_build_inner_plaintext(unsigned char *content,
361 size_t *content_size,
362 size_t remaining,
363 uint8_t rec_type,
364 size_t pad)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100365{
366 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100367
368 /* Write real content type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 if (remaining == 0) {
370 return -1;
371 }
372 content[len] = rec_type;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100373 len++;
374 remaining--;
375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 if (remaining < pad) {
377 return -1;
378 }
379 memset(content + len, 0, pad);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100380 len += pad;
381 remaining -= pad;
382
383 *content_size = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100385}
386
Hanno Becker581bc1b2020-05-04 12:20:03 +0100387/* This function parses a (D)TLSInnerPlaintext structure.
388 * See ssl_build_inner_plaintext() for details. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200389MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390static int ssl_parse_inner_plaintext(unsigned char const *content,
391 size_t *content_size,
392 uint8_t *rec_type)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100393{
394 size_t remaining = *content_size;
395
396 /* Determine length of padding by skipping zeroes from the back. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 do {
398 if (remaining == 0) {
399 return -1;
400 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100401 remaining--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402 } while (content[remaining] == 0);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100403
404 *content_size = remaining;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 *rec_type = content[remaining];
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100406
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100408}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100409#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
410 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100411
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100412/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100413 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414static void ssl_extract_add_data_from_record(unsigned char *add_data,
415 size_t *add_data_len,
416 mbedtls_record *rec,
417 unsigned minor_ver)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000418{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100419 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100420 *
421 * additional_data = seq_num + TLSCompressed.type +
422 * TLSCompressed.version + TLSCompressed.length;
423 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100424 * For the CID extension, this is extended as follows
425 * (quoting draft-ietf-tls-dtls-connection-id-05,
426 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100427 *
428 * additional_data = seq_num + DTLSPlaintext.type +
429 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100430 * cid +
431 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100432 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100433 *
434 * For TLS 1.3, the record sequence number is dropped from the AAD
435 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100436 */
437
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100438 unsigned char *cur = add_data;
439
David Horstmann197b2402022-10-26 18:06:31 +0100440 int is_tls13 = 0;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100441#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
David Horstmann197b2402022-10-26 18:06:31 +0100443 is_tls13 = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100445#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100446 if (!is_tls13) {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100447 ((void) minor_ver);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 memcpy(cur, rec->ctr, sizeof(rec->ctr));
449 cur += sizeof(rec->ctr);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100450 }
451
452 *cur = rec->type;
453 cur++;
454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 memcpy(cur, rec->ver, sizeof(rec->ver));
456 cur += sizeof(rec->ver);
Hanno Beckercab87e62019-04-29 13:52:53 +0100457
Hanno Beckera0e20d02019-05-15 14:03:01 +0100458#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 if (rec->cid_len != 0) {
460 memcpy(cur, rec->cid, rec->cid_len);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100461 cur += rec->cid_len;
462
463 *cur = rec->cid_len;
464 cur++;
465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100467 cur += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100470 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100472 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100473 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100474
475 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000476}
477
Hanno Becker9d062f92020-02-07 10:26:36 +0000478#if defined(MBEDTLS_SSL_PROTO_SSL3)
479
480#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
481
482/*
483 * SSLv3.0 MAC functions
484 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200485MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100486static int ssl_mac(mbedtls_md_context_t *md_ctx,
487 const unsigned char *secret,
488 const unsigned char *buf, size_t len,
489 const unsigned char *ctr, int type,
490 unsigned char out[SSL3_MAC_MAX_BYTES])
Hanno Becker9d062f92020-02-07 10:26:36 +0000491{
492 unsigned char header[11];
493 unsigned char padding[48];
494 int padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 int md_size = mbedtls_md_get_size(md_ctx->md_info);
496 int md_type = mbedtls_md_get_type(md_ctx->md_info);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9d062f92020-02-07 10:26:36 +0000498
499 /* Only MD5 and SHA-1 supported */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 if (md_type == MBEDTLS_MD_MD5) {
Hanno Becker9d062f92020-02-07 10:26:36 +0000501 padlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100502 } else {
Hanno Becker9d062f92020-02-07 10:26:36 +0000503 padlen = 40;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100504 }
Hanno Becker9d062f92020-02-07 10:26:36 +0000505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 memcpy(header, ctr, 8);
Joe Subbiania651e6f2021-08-23 11:35:25 +0100507 header[8] = (unsigned char) type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 MBEDTLS_PUT_UINT16_BE(len, header, 9);
Hanno Becker9d062f92020-02-07 10:26:36 +0000509
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 memset(padding, 0x36, padlen);
511 ret = mbedtls_md_starts(md_ctx);
512 if (ret != 0) {
513 return ret;
514 }
515 ret = mbedtls_md_update(md_ctx, secret, md_size);
516 if (ret != 0) {
517 return ret;
518 }
519 ret = mbedtls_md_update(md_ctx, padding, padlen);
520 if (ret != 0) {
521 return ret;
522 }
523 ret = mbedtls_md_update(md_ctx, header, 11);
524 if (ret != 0) {
525 return ret;
526 }
527 ret = mbedtls_md_update(md_ctx, buf, len);
528 if (ret != 0) {
529 return ret;
530 }
531 ret = mbedtls_md_finish(md_ctx, out);
532 if (ret != 0) {
533 return ret;
534 }
Hanno Becker9d062f92020-02-07 10:26:36 +0000535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 memset(padding, 0x5C, padlen);
537 ret = mbedtls_md_starts(md_ctx);
538 if (ret != 0) {
539 return ret;
540 }
541 ret = mbedtls_md_update(md_ctx, secret, md_size);
542 if (ret != 0) {
543 return ret;
544 }
545 ret = mbedtls_md_update(md_ctx, padding, padlen);
546 if (ret != 0) {
547 return ret;
548 }
549 ret = mbedtls_md_update(md_ctx, out, md_size);
550 if (ret != 0) {
551 return ret;
552 }
553 ret = mbedtls_md_finish(md_ctx, out);
554 if (ret != 0) {
555 return ret;
556 }
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100558 return 0;
Hanno Becker9d062f92020-02-07 10:26:36 +0000559}
560#endif /* MBEDTLS_SSL_PROTO_SSL3 */
561
Hanno Becker67a37db2020-05-28 16:27:07 +0100562#if defined(MBEDTLS_GCM_C) || \
563 defined(MBEDTLS_CCM_C) || \
564 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200565MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker17263802020-05-28 07:05:48 +0100566static int ssl_transform_aead_dynamic_iv_is_explicit(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100567 mbedtls_ssl_transform const *transform)
Hanno Beckerdf8be222020-05-21 15:30:57 +0100568{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 return transform->ivlen != transform->fixed_ivlen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100570}
571
Hanno Becker17263802020-05-28 07:05:48 +0100572/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
573 *
574 * Concretely, this occurs in two variants:
575 *
576 * a) Fixed and dynamic IV lengths add up to total IV length, giving
577 * IV = fixed_iv || dynamic_iv
578 *
Hanno Becker15952812020-06-04 13:31:46 +0100579 * This variant is used in TLS 1.2 when used with GCM or CCM.
580 *
Hanno Becker17263802020-05-28 07:05:48 +0100581 * b) Fixed IV lengths matches total IV length, giving
582 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100583 *
584 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
585 *
586 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100587 *
588 * This function has the precondition that
589 *
590 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
591 *
592 * which has to be ensured by the caller. If this precondition
593 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100594 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100595static void ssl_build_record_nonce(unsigned char *dst_iv,
596 size_t dst_iv_len,
597 unsigned char const *fixed_iv,
598 size_t fixed_iv_len,
599 unsigned char const *dynamic_iv,
600 size_t dynamic_iv_len)
Hanno Becker17263802020-05-28 07:05:48 +0100601{
602 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100603
604 /* Start with Fixed IV || 0 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100605 memset(dst_iv, 0, dst_iv_len);
606 memcpy(dst_iv, fixed_iv, fixed_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100607
Hanno Becker17263802020-05-28 07:05:48 +0100608 dst_iv += dst_iv_len - dynamic_iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100609 for (i = 0; i < dynamic_iv_len; i++) {
Hanno Becker17263802020-05-28 07:05:48 +0100610 dst_iv[i] ^= dynamic_iv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 }
Hanno Beckerdf8be222020-05-21 15:30:57 +0100612}
Hanno Becker67a37db2020-05-28 16:27:07 +0100613#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100615int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
616 mbedtls_ssl_transform *transform,
617 mbedtls_record *rec,
618 int (*f_rng)(void *, unsigned char *, size_t),
619 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +0000620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100622 int auth_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100623 unsigned char *data;
624 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX];
Hanno Beckercab87e62019-04-29 13:52:53 +0100625 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000626 size_t post_avail;
627
628 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000629#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200630 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000631 ((void) ssl);
632#endif
633
634 /* The PRNG is used for dynamic IV generation that's used
635 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636#if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
637 (defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)))
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000638 ((void) f_rng);
639 ((void) p_rng);
640#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100642 MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100644 if (transform == NULL) {
645 MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf"));
646 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000647 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100648 if (rec == NULL
Hanno Becker43c24b82019-05-01 09:45:57 +0100649 || rec->buf == NULL
650 || rec->buf_len < rec->data_offset
651 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100652#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100653 || rec->cid_len != 0
654#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 ) {
656 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf"));
657 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100658 }
659
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000660 data = rec->buf + rec->data_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100661 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
662 MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload",
663 data, rec->data_len);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000664
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100665 mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100667 if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
668 MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET
669 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
670 rec->data_len,
671 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000673 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100674
Hanno Becker92313402020-05-20 13:58:58 +0100675 /* The following two code paths implement the (D)TLSInnerPlaintext
676 * structure present in TLS 1.3 and DTLS 1.2 + CID.
677 *
678 * See ssl_build_inner_plaintext() for more information.
679 *
680 * Note that this changes `rec->data_len`, and hence
681 * `post_avail` needs to be recalculated afterwards.
682 *
683 * Note also that the two code paths cannot occur simultaneously
684 * since they apply to different versions of the protocol. There
685 * is hence no risk of double-addition of the inner plaintext.
686 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100687#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100688 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Hanno Becker13996922020-05-28 16:15:19 +0100689 size_t padding =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100690 ssl_compute_padding_length(rec->data_len,
691 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY);
692 if (ssl_build_inner_plaintext(data,
693 &rec->data_len,
694 post_avail,
695 rec->type,
696 padding) != 0) {
697 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerccc13d02020-05-04 12:30:04 +0100698 }
699
700 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
701 }
702#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
703
Hanno Beckera0e20d02019-05-15 14:03:01 +0100704#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100705 /*
706 * Add CID information
707 */
708 rec->cid_len = transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100709 memcpy(rec->cid, transform->out_cid, transform->out_cid_len);
710 MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100711
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100712 if (rec->cid_len != 0) {
Hanno Becker13996922020-05-28 16:15:19 +0100713 size_t padding =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100714 ssl_compute_padding_length(rec->data_len,
715 MBEDTLS_SSL_CID_PADDING_GRANULARITY);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100716 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100717 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100718 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100719 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100720 * Note that this changes `rec->data_len`, and hence
721 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100722 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 if (ssl_build_inner_plaintext(data,
724 &rec->data_len,
725 post_avail,
726 rec->type,
727 padding) != 0) {
728 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100729 }
730
731 rec->type = MBEDTLS_SSL_MSG_CID;
732 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100733#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100735 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100736
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100738 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 */
Hanno Becker52344c22018-01-03 15:24:20 +0000740#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 if (mode == MBEDTLS_MODE_STREAM ||
742 (mode == MBEDTLS_MODE_CBC
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100745#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100746 )) {
747 if (post_avail < transform->maclen) {
748 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
749 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100753 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Hanno Becker9d062f92020-02-07 10:26:36 +0000754 unsigned char mac[SSL3_MAC_MAX_BYTES];
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100756 ret = ssl_mac(&transform->md_ctx_enc, transform->mac_enc,
757 data, rec->data_len, rec->ctr, rec->type, mac);
758 if (ret == 0) {
759 memcpy(data + rec->data_len, mac, transform->maclen);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100760 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100761 mbedtls_platform_zeroize(mac, transform->maclen);
762 if (ret != 0) {
763 MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret);
764 return ret;
765 }
766 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
769 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Hanno Becker992b6872017-11-09 18:57:39 +0000771 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker992b6872017-11-09 18:57:39 +0000773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100774 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
775 transform->minor_ver);
Hanno Becker992b6872017-11-09 18:57:39 +0000776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100777 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
778 add_data, add_data_len);
779 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100780 goto hmac_failed_etm_disabled;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100781 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100782 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
783 data, rec->data_len);
784 if (ret != 0) {
785 goto hmac_failed_etm_disabled;
786 }
787 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
788 if (ret != 0) {
789 goto hmac_failed_etm_disabled;
790 }
791 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
792 if (ret != 0) {
793 goto hmac_failed_etm_disabled;
794 }
795
796 memcpy(data + rec->data_len, mac, transform->maclen);
797
798hmac_failed_etm_disabled:
799 mbedtls_platform_zeroize(mac, transform->maclen);
800 if (ret != 0) {
801 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
802 return ret;
803 }
804 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200806 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100807 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
808 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200809 }
810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100811 MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len,
812 transform->maclen);
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200813
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000814 rec->data_len += transform->maclen;
815 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100816 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200817 }
Hanno Becker52344c22018-01-03 15:24:20 +0000818#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000819
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200820 /*
821 * Encrypt
822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100824 if (mode == MBEDTLS_MODE_STREAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000825 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000826 size_t olen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
828 "including %d bytes of padding",
829 rec->data_len, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
832 transform->iv_enc, transform->ivlen,
833 data, rec->data_len,
834 data, &olen)) != 0) {
835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
836 return ret;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200837 }
838
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 if (rec->data_len != olen) {
840 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
841 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200842 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100843 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000845
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200846#if defined(MBEDTLS_GCM_C) || \
847 defined(MBEDTLS_CCM_C) || \
848 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100849 if (mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200850 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100851 mode == MBEDTLS_MODE_CHACHAPOLY) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200853 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100854 unsigned char *dynamic_iv;
855 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100856 int dynamic_iv_is_explicit =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100857 ssl_transform_aead_dynamic_iv_is_explicit(transform);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000858
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100859 /* Check that there's space for the authentication tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100860 if (post_avail < transform->taglen) {
861 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
862 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000863 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000864
Paul Bakker68884e32013-01-07 18:20:04 +0100865 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100866 * Build nonce for AEAD encryption.
867 *
868 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
869 * part of the IV is prepended to the ciphertext and
870 * can be chosen freely - in particular, it need not
871 * agree with the record sequence number.
872 * However, since ChaChaPoly as well as all AEAD modes
873 * in TLS 1.3 use the record sequence number as the
874 * dynamic part of the nonce, we uniformly use the
875 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100876 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100877 dynamic_iv = rec->ctr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878 dynamic_iv_len = sizeof(rec->ctr);
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100880 ssl_build_record_nonce(iv, sizeof(iv),
881 transform->iv_enc,
882 transform->fixed_ivlen,
883 dynamic_iv,
884 dynamic_iv_len);
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100885
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100886 /*
887 * Build additional data for AEAD encryption.
888 * This depends on the TLS version.
889 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100890 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
891 transform->minor_ver);
Hanno Becker1f10d762019-04-26 13:34:37 +0100892
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100893 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)",
894 iv, transform->ivlen);
895 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)",
896 dynamic_iv,
897 dynamic_iv_is_explicit ? dynamic_iv_len : 0);
898 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
899 add_data, add_data_len);
900 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
901 "including 0 bytes of padding",
902 rec->data_len));
Paul Bakkerca4ab492012-04-18 14:23:57 +0000903
Paul Bakker68884e32013-01-07 18:20:04 +0100904 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200905 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200906 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100908 if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc,
909 iv, transform->ivlen,
910 add_data, add_data_len,
911 data, rec->data_len, /* src */
912 data, rec->buf_len - (data - rec->buf), /* dst */
913 &rec->data_len,
914 transform->taglen)) != 0) {
915 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt", ret);
916 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200917 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100918 MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
919 data + rec->data_len - transform->taglen,
920 transform->taglen);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100921 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000922 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100923
924 /*
925 * Prefix record content with dynamic IV in case it is explicit.
926 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 if (dynamic_iv_is_explicit != 0) {
928 if (rec->data_offset < dynamic_iv_len) {
929 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
930 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100931 }
932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100933 memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100934 rec->data_offset -= dynamic_iv_len;
935 rec->data_len += dynamic_iv_len;
936 }
937
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100938 auth_done++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100939 } else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100940#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200941#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942 if (mode == MBEDTLS_MODE_CBC) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000944 size_t padlen, i;
945 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000946
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000947 /* Currently we're always using minimal padding
948 * (up to 255 bytes would be allowed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100949 padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen;
950 if (padlen == transform->ivlen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 padlen = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000952 }
953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100954 /* Check there's enough space in the buffer for the padding. */
955 if (post_avail < padlen + 1) {
956 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
957 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
958 }
959
960 for (i = 0; i <= padlen; i++) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000961 data[rec->data_len + i] = (unsigned char) padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100962 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000963
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000964 rec->data_len += padlen + 1;
965 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000968 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000969 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
970 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000971 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100972 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
973 if (f_rng == NULL) {
974 MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine"));
975 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000976 }
977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100978 if (rec->data_offset < transform->ivlen) {
979 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
980 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000981 }
982
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000983 /*
984 * Generate IV
985 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100986 ret = f_rng(p_rng, transform->iv_enc, transform->ivlen);
987 if (ret != 0) {
988 return ret;
989 }
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000990
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100991 memcpy(data - transform->ivlen, transform->iv_enc,
992 transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000993
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000994 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100997 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
998 "including %"
999 MBEDTLS_PRINTF_SIZET
1000 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
1001 rec->data_len, transform->ivlen,
1002 padlen + 1));
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001004 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1005 transform->iv_enc,
1006 transform->ivlen,
1007 data, rec->data_len,
1008 data, &olen)) != 0) {
1009 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1010 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001011 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001012
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001013 if (rec->data_len != olen) {
1014 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1015 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001016 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001019 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
Paul Bakkercca5b812013-08-31 17:40:26 +02001020 /*
1021 * Save IV in SSL3 and TLS1
1022 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001023 memcpy(transform->iv_enc, transform->cipher_ctx_enc.iv,
1024 transform->ivlen);
1025 } else
Paul Bakkercca5b812013-08-31 17:40:26 +02001026#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001027 {
1028 data -= transform->ivlen;
1029 rec->data_offset -= transform->ivlen;
1030 rec->data_len += transform->ivlen;
1031 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001034 if (auth_done == 0) {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001035 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1036
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001037 /*
1038 * MAC(MAC_write_key, seq_num +
1039 * TLSCipherText.type +
1040 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001041 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001042 * IV + // except for TLS 1.0
1043 * ENC(content + padding + padding_length));
1044 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 if (post_avail < transform->maclen) {
1047 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1048 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001049 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001051 ssl_extract_add_data_from_record(add_data, &add_data_len,
1052 rec, transform->minor_ver);
Hanno Becker1f10d762019-04-26 13:34:37 +01001053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001054 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
1055 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1056 add_data_len);
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001058 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
1059 add_data_len);
1060 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001061 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001062 }
1063 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
1064 data, rec->data_len);
1065 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001066 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001067 }
1068 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
1069 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001070 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071 }
1072 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
1073 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001074 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001075 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001077 memcpy(data + rec->data_len, mac, transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001078
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001079 rec->data_len += transform->maclen;
1080 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001081 auth_done++;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001083hmac_failed_etm_enabled:
1084 mbedtls_platform_zeroize(mac, transform->maclen);
1085 if (ret != 0) {
1086 MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
1087 return ret;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001088 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001089 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001091 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001092#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001093 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1095 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001096 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001098 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001099 if (auth_done != 1) {
1100 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1101 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001102 }
1103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001106 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001107}
1108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001109int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1110 mbedtls_ssl_transform *transform,
1111 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00001112{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001113 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001115 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001116#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001117 size_t padlen = 0, correct = 1;
1118#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001119 unsigned char *data;
1120 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckercab87e62019-04-29 13:52:53 +01001121 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001122
Hanno Beckera18d1322018-01-03 14:27:32 +00001123#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001124 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001125 ((void) ssl);
1126#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001128 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf"));
1129 if (rec == NULL ||
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001130 rec->buf == NULL ||
1131 rec->buf_len < rec->data_offset ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001132 rec->buf_len - rec->data_offset < rec->data_len) {
1133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf"));
1134 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001135 }
1136
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001137 data = rec->buf + rec->data_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001138 mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_dec);
Paul Bakker5121ce52009-01-03 21:22:43 +00001139
Hanno Beckera0e20d02019-05-15 14:03:01 +01001140#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001141 /*
1142 * Match record's CID with incoming CID.
1143 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001144 if (rec->cid_len != transform->in_cid_len ||
1145 memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) {
1146 return MBEDTLS_ERR_SSL_UNEXPECTED_CID;
Hanno Becker938489a2019-05-08 13:02:22 +01001147 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001148#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001151 if (mode == MBEDTLS_MODE_STREAM) {
Paul Bakker68884e32013-01-07 18:20:04 +01001152 padlen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001153 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1154 transform->iv_dec,
1155 transform->ivlen,
1156 data, rec->data_len,
1157 data, &olen)) != 0) {
1158 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1159 return ret;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001160 }
1161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001162 if (rec->data_len != olen) {
1163 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1164 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001165 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001166 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001168#if defined(MBEDTLS_GCM_C) || \
1169 defined(MBEDTLS_CCM_C) || \
1170 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001171 if (mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001172 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001173 mode == MBEDTLS_MODE_CHACHAPOLY) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001174 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001175 unsigned char *dynamic_iv;
1176 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001178 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001179 * Extract dynamic part of nonce for AEAD decryption.
1180 *
1181 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1182 * part of the IV is prepended to the ciphertext and
1183 * can be chosen freely - in particular, it need not
1184 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001186 dynamic_iv_len = sizeof(rec->ctr);
1187 if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) {
1188 if (rec->data_len < dynamic_iv_len) {
1189 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1190 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
1191 rec->data_len,
1192 dynamic_iv_len));
1193 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001194 }
1195 dynamic_iv = data;
1196
1197 data += dynamic_iv_len;
1198 rec->data_offset += dynamic_iv_len;
1199 rec->data_len -= dynamic_iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001200 } else {
Hanno Becker17263802020-05-28 07:05:48 +01001201 dynamic_iv = rec->ctr;
1202 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001203
1204 /* Check that there's space for the authentication tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001205 if (rec->data_len < transform->taglen) {
1206 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1207 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
1208 rec->data_len,
1209 transform->taglen));
1210 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001211 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001212 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001213
Hanno Beckerdf8be222020-05-21 15:30:57 +01001214 /*
1215 * Prepare nonce from dynamic and static parts.
1216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001217 ssl_build_record_nonce(iv, sizeof(iv),
1218 transform->iv_dec,
1219 transform->fixed_ivlen,
1220 dynamic_iv,
1221 dynamic_iv_len);
Paul Bakker68884e32013-01-07 18:20:04 +01001222
Hanno Beckerdf8be222020-05-21 15:30:57 +01001223 /*
1224 * Build additional data for AEAD encryption.
1225 * This depends on the TLS version.
1226 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001227 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1228 transform->minor_ver);
1229 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
1230 add_data, add_data_len);
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001231
Hanno Beckerd96a6522019-07-10 13:55:25 +01001232 /* Because of the check above, we know that there are
Shaun Case0e7791f2021-12-20 21:14:10 -08001233 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001234 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001235 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001236 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001238 MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen);
1239 MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len,
1240 transform->taglen);
Paul Bakker68884e32013-01-07 18:20:04 +01001241
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001242 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001243 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001244 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001245 if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec,
1246 iv, transform->ivlen,
1247 add_data, add_data_len,
1248 data, rec->data_len + transform->taglen, /* src */
1249 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
1250 transform->taglen)) != 0) {
1251 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt", ret);
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
1254 return MBEDTLS_ERR_SSL_INVALID_MAC;
1255 }
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001257 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001259 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001260
Hanno Beckerd96a6522019-07-10 13:55:25 +01001261 /* Double-check that AEAD decryption doesn't change content length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 if (olen != rec->data_len) {
1263 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1264 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001266 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001268#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001269 if (mode == MBEDTLS_MODE_CBC) {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001270 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001271
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 /*
Paul Bakker45829992013-01-03 14:52:21 +01001273 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001276 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001277 /* The ciphertext is prefixed with the CBC IV. */
1278 minlen += transform->ivlen;
1279 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001280#endif
Paul Bakker45829992013-01-03 14:52:21 +01001281
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001282 /* Size considerations:
1283 *
1284 * - The CBC cipher text must not be empty and hence
1285 * at least of size transform->ivlen.
1286 *
1287 * Together with the potential IV-prefix, this explains
1288 * the first of the two checks below.
1289 *
1290 * - The record must contain a MAC, either in plain or
1291 * encrypted, depending on whether Encrypt-then-MAC
1292 * is used or not.
1293 * - If it is, the message contains the IV-prefix,
1294 * the CBC ciphertext, and the MAC.
1295 * - If it is not, the padded plaintext, and hence
1296 * the CBC ciphertext, has at least length maclen + 1
1297 * because there is at least the padding length byte.
1298 *
1299 * As the CBC ciphertext is not empty, both cases give the
1300 * lower bound minlen + maclen + 1 on the record size, which
1301 * we test for in the second check below.
1302 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001303 if (rec->data_len < minlen + transform->ivlen ||
1304 rec->data_len < minlen + transform->maclen + 1) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1306 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1307 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
1308 "+ 1 ) ( + expl IV )",
1309 rec->data_len,
1310 transform->ivlen,
1311 transform->maclen));
1312 return MBEDTLS_ERR_SSL_INVALID_MAC;
Paul Bakker45829992013-01-03 14:52:21 +01001313 }
1314
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001315 /*
1316 * Authenticate before decrypt if enabled
1317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001319 if (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Hanno Becker992b6872017-11-09 18:57:39 +00001320 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001322 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001323
Hanno Beckerd96a6522019-07-10 13:55:25 +01001324 /* Update data_len in tandem with add_data.
1325 *
1326 * The subtraction is safe because of the previous check
1327 * data_len >= minlen + maclen + 1.
1328 *
1329 * Afterwards, we know that data + data_len is followed by at
1330 * least maclen Bytes, which justifies the call to
Gabor Mezei18a44942021-10-20 11:59:27 +02001331 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001332 *
1333 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001334 rec->data_len -= transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1336 transform->minor_ver);
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001337
Hanno Beckerd96a6522019-07-10 13:55:25 +01001338 /* Calculate expected MAC. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001339 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1340 add_data_len);
1341 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
1342 add_data_len);
1343 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001344 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001345 }
1346 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
1347 data, rec->data_len);
1348 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001349 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 }
1351 ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
1352 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001353 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 }
1355 ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
1356 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001357 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001358 }
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001360 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
1361 transform->maclen);
1362 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
1363 transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001364
Hanno Beckerd96a6522019-07-10 13:55:25 +01001365 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
1367 transform->maclen) != 0) {
1368 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Gilles Peskined8e2e832021-12-10 21:33:21 +01001369 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1370 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001371 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001372 auth_done++;
Gilles Peskined8e2e832021-12-10 21:33:21 +01001373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374hmac_failed_etm_enabled:
1375 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1376 if (ret != 0) {
1377 if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
1378 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
1379 }
1380 return ret;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001381 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001384
1385 /*
1386 * Check length sanity
1387 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001388
1389 /* We know from above that data_len > minlen >= 0,
1390 * so the following check in particular implies that
1391 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001392 if (rec->data_len % transform->ivlen != 0) {
1393 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1394 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
1395 rec->data_len, transform->ivlen));
1396 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001397 }
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001400 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001401 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001402 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001403 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001404 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001405 memcpy(transform->iv_dec, data, transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001406
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001407 data += transform->ivlen;
1408 rec->data_offset += transform->ivlen;
1409 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001412
Hanno Beckerd96a6522019-07-10 13:55:25 +01001413 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1414
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001415 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1416 transform->iv_dec, transform->ivlen,
1417 data, rec->data_len, data, &olen)) != 0) {
1418 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1419 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001420 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001421
Hanno Beckerd96a6522019-07-10 13:55:25 +01001422 /* Double-check that length hasn't changed during decryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if (rec->data_len != olen) {
1424 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1425 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001426 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001429 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
Paul Bakkercca5b812013-08-31 17:40:26 +02001430 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001431 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1432 * records is equivalent to CBC decryption of the concatenation
1433 * of the records; in other words, IVs are maintained across
1434 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001435 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001436 memcpy(transform->iv_dec, transform->cipher_ctx_dec.iv,
1437 transform->ivlen);
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001439#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001440
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001441 /* Safe since data_len >= minlen + maclen + 1, so after having
1442 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001443 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1444 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001445 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001447 if (auth_done == 1) {
Gabor Mezei18a44942021-10-20 11:59:27 +02001448 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 rec->data_len,
1450 padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001451 correct &= mask;
1452 padlen &= mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001453 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001455 if (rec->data_len < transform->maclen + padlen + 1) {
1456 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1457 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1458 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
1459 rec->data_len,
1460 transform->maclen,
1461 padlen + 1));
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001462 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001463#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001464
Gabor Mezei18a44942021-10-20 11:59:27 +02001465 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001466 rec->data_len,
1467 transform->maclen + padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001468 correct &= mask;
1469 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001471
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001472 padlen++;
1473
1474 /* Regardless of the validity of the padding,
1475 * we have data_len >= padlen here. */
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001478 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001479 /* This is the SSL 3.0 path, we don't have to worry about Lucky
1480 * 13, because there's a strictly worse padding attack built in
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001481 * the protocol (known as part of POODLE), so we don't care if the
1482 * code is not constant-time, in particular branches are OK. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001483 if (padlen > transform->ivlen) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001485 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding length: is %" MBEDTLS_PRINTF_SIZET ", "
1486 "should be no more than %"
1487 MBEDTLS_PRINTF_SIZET,
1488 padlen, transform->ivlen));
Paul Bakkerd66f0702013-01-31 16:57:45 +01001489#endif
Paul Bakker45829992013-01-03 14:52:21 +01001490 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001492 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1494#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001495 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1496 if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001497 /* The padding check involves a series of up to 256
1498 * consecutive memory reads at the end of the record
1499 * plaintext buffer. In order to hide the length and
1500 * validity of the padding, always perform exactly
1501 * `min(256,plaintext_len)` reads (but take into account
1502 * only the last `padlen` bytes for the padding check). */
1503 size_t pad_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001504 volatile unsigned char * const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001505
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001506 /* Index of first padding byte; it has been ensured above
1507 * that the subtraction is safe. */
1508 size_t const padding_idx = rec->data_len - padlen;
1509 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1510 size_t const start_idx = rec->data_len - num_checks;
1511 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 for (idx = start_idx; idx < rec->data_len; idx++) {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001514 /* pad_count += (idx >= padding_idx) &&
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001515 * (check[idx] == padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001516 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001517 const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx);
1518 const size_t equal = mbedtls_ct_size_bool_eq(check[idx],
1519 padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001520 pad_count += mask & equal;
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001521 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001522 correct &= mbedtls_ct_size_bool_eq(pad_count, padlen);
Paul Bakkere47b34b2013-02-27 14:48:00 +01001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 if (padlen > 0 && correct == 0) {
1526 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
1527 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001528#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001529 padlen &= mbedtls_ct_size_mask(correct);
1530 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1532 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001533 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001534 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1535 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02001536 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001537
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001538 /* If the padding was found to be invalid, padlen == 0
1539 * and the subtraction is safe. If the padding was found valid,
1540 * padlen hasn't been changed and the previous assertion
1541 * data_len >= padlen still holds. */
1542 rec->data_len -= padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001543 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001544#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001545 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001546 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1547 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001549
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001550#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
1552 data, rec->data_len);
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001554
1555 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001556 * Authenticate if not done yet.
1557 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 */
Hanno Becker52344c22018-01-03 15:24:20 +00001559#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001560 if (auth_done == 0) {
Paul Elliottb8300282022-05-19 18:31:35 +01001561 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
1562 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01001563
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001564 /* If the initial value of padlen was such that
1565 * data_len < maclen + padlen + 1, then padlen
1566 * got reset to 1, and the initial check
1567 * data_len >= minlen + maclen + 1
1568 * guarantees that at this point we still
1569 * have at least data_len >= maclen.
1570 *
1571 * If the initial value of padlen was such that
1572 * data_len >= maclen + padlen + 1, then we have
1573 * subtracted either padlen + 1 (if the padding was correct)
1574 * or 0 (if the padding was incorrect) since then,
1575 * hence data_len >= maclen in any case.
1576 */
1577 rec->data_len -= transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001578 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1579 transform->minor_ver);
Paul Bakker5121ce52009-01-03 21:22:43 +00001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001582 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1583 ret = ssl_mac(&transform->md_ctx_dec,
1584 transform->mac_dec,
1585 data, rec->data_len,
1586 rec->ctr, rec->type,
1587 mac_expect);
1588 if (ret != 0) {
1589 MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001590 goto hmac_failed_etm_disabled;
1591 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001592 memcpy(mac_peer, data + rec->data_len, transform->maclen);
1593 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1595#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1596 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001597 if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001598 /*
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001599 * The next two sizes are the minimum and maximum values of
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001600 * data_len over all padlen values.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001601 *
1602 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001603 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001604 *
1605 * Note that max_len + maclen is never more than the buffer
1606 * length, as we previously did in_msglen -= maclen too.
1607 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001608 const size_t max_len = rec->data_len + padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001609 const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001611 ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
1612 add_data, add_data_len,
1613 data, rec->data_len, min_len, max_len,
1614 mac_expect);
1615 if (ret != 0) {
1616 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
Gilles Peskined8e2e832021-12-10 21:33:21 +01001617 goto hmac_failed_etm_disabled;
Gilles Peskine20b44082018-05-29 14:06:49 +02001618 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001619
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 mbedtls_ct_memcpy_offset(mac_peer, data,
1621 rec->data_len,
1622 min_len, max_len,
1623 transform->maclen);
1624 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1626 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001628 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1629 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001630 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001632#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001633 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen);
1634 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen);
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001635#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001637 if (mbedtls_ct_memcmp(mac_peer, mac_expect,
1638 transform->maclen) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001640 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Paul Bakkere47b34b2013-02-27 14:48:00 +01001641#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001642 correct = 0;
1643 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001644 auth_done++;
Gilles Peskined8e2e832021-12-10 21:33:21 +01001645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001646hmac_failed_etm_disabled:
1647 mbedtls_platform_zeroize(mac_peer, transform->maclen);
1648 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1649 if (ret != 0) {
1650 return ret;
1651 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001653
1654 /*
1655 * Finally check the correct flag
1656 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001657 if (correct == 0) {
1658 return MBEDTLS_ERR_SSL_INVALID_MAC;
1659 }
Hanno Becker52344c22018-01-03 15:24:20 +00001660#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001661
1662 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001663 if (auth_done != 1) {
1664 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1665 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001666 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Hanno Beckerccc13d02020-05-04 12:30:04 +01001668#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001669 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Hanno Beckerccc13d02020-05-04 12:30:04 +01001670 /* Remove inner padding and infer true content type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001671 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1672 &rec->type);
Hanno Beckerccc13d02020-05-04 12:30:04 +01001673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 if (ret != 0) {
1675 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1676 }
Hanno Beckerccc13d02020-05-04 12:30:04 +01001677 }
1678#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1679
Hanno Beckera0e20d02019-05-15 14:03:01 +01001680#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001681 if (rec->cid_len != 0) {
1682 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1683 &rec->type);
1684 if (ret != 0) {
1685 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1686 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001687 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001688#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001689
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001690 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001693}
1694
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001695#undef MAC_NONE
1696#undef MAC_PLAINTEXT
1697#undef MAC_CIPHERTEXT
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001700/*
1701 * Compression/decompression functions
1702 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001703MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001704static int ssl_compress_buf(mbedtls_ssl_context *ssl)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001705{
Janos Follath865b3eb2019-12-16 11:46:15 +00001706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001707 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001708 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001709 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001710 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001711#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1712 size_t out_buf_len = ssl->out_buf_len;
1713#else
1714 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1715#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001717 MBEDTLS_SSL_DEBUG_MSG(2, ("=> compress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001719 if (len_pre == 0) {
1720 return 0;
1721 }
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001723 memcpy(msg_pre, ssl->out_msg, len_pre);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001725 MBEDTLS_SSL_DEBUG_MSG(3, ("before compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1726 ssl->out_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 MBEDTLS_SSL_DEBUG_BUF(4, "before compression: output payload",
1729 ssl->out_msg, ssl->out_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001730
Paul Bakker48916f92012-09-16 19:57:18 +00001731 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1732 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1733 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001734 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001736 ret = deflate(&ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH);
1737 if (ret != Z_OK) {
1738 MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform compression (%d)", ret));
1739 return MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001740 }
1741
Darryl Greenb33cc762019-11-28 14:29:44 +00001742 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001743 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001744
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001745 MBEDTLS_SSL_DEBUG_MSG(3, ("after compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1746 ssl->out_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001747
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001748 MBEDTLS_SSL_DEBUG_BUF(4, "after compression: output payload",
1749 ssl->out_msg, ssl->out_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001751 MBEDTLS_SSL_DEBUG_MSG(2, ("<= compress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001752
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001753 return 0;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754}
1755
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001756MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001757static int ssl_decompress_buf(mbedtls_ssl_context *ssl)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758{
Janos Follath865b3eb2019-12-16 11:46:15 +00001759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001761 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001762 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001763 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001764#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1765 size_t in_buf_len = ssl->in_buf_len;
1766#else
1767 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1768#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001769
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001770 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decompress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001772 if (len_pre == 0) {
1773 return 0;
1774 }
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001776 memcpy(msg_pre, ssl->in_msg, len_pre);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001778 MBEDTLS_SSL_DEBUG_MSG(3, ("before decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1779 ssl->in_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 MBEDTLS_SSL_DEBUG_BUF(4, "before decompression: input payload",
1782 ssl->in_msg, ssl->in_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783
Paul Bakker48916f92012-09-16 19:57:18 +00001784 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1785 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1786 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001787 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001789 ret = inflate(&ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH);
1790 if (ret != Z_OK) {
1791 MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform decompression (%d)", ret));
1792 return MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001793 }
1794
Darryl Greenb33cc762019-11-28 14:29:44 +00001795 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001796 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001798 MBEDTLS_SSL_DEBUG_MSG(3, ("after decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1799 ssl->in_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001801 MBEDTLS_SSL_DEBUG_BUF(4, "after decompression: input payload",
1802 ssl->in_msg, ssl->in_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001804 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decompress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001806 return 0;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809
Paul Bakker5121ce52009-01-03 21:22:43 +00001810/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001811 * Fill the input message buffer by appending data to it.
1812 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001813 *
1814 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1815 * available (from this read and/or a previous one). Otherwise, an error code
1816 * is returned (possibly EOF or WANT_READ).
1817 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001818 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1819 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1820 * since we always read a whole datagram at once.
1821 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001822 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001823 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001825int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
Paul Bakker5121ce52009-01-03 21:22:43 +00001826{
Janos Follath865b3eb2019-12-16 11:46:15 +00001827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001828 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001829#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1830 size_t in_buf_len = ssl->in_buf_len;
1831#else
1832 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1833#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001835 MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001837 if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) {
1838 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
1839 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001840 }
1841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001842 if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
1843 MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits"));
1844 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001845 }
1846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001848 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001849 uint32_t timeout;
1850
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001851 /*
1852 * The point is, we need to always read a full datagram at once, so we
1853 * sometimes read more then requested, and handle the additional data.
1854 * It could be the rest of the current record (while fetching the
1855 * header) and/or some other records in the same datagram.
1856 */
1857
1858 /*
1859 * Move to the next record in the already read datagram if applicable
1860 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001861 if (ssl->next_record_offset != 0) {
1862 if (ssl->in_left < ssl->next_record_offset) {
1863 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1864 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001865 }
1866
1867 ssl->in_left -= ssl->next_record_offset;
1868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001869 if (ssl->in_left != 0) {
1870 MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %"
1871 MBEDTLS_PRINTF_SIZET,
1872 ssl->next_record_offset));
1873 memmove(ssl->in_hdr,
1874 ssl->in_hdr + ssl->next_record_offset,
1875 ssl->in_left);
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001876 }
1877
1878 ssl->next_record_offset = 0;
1879 }
1880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001881 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1882 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1883 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001884
1885 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001886 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001887 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001888 if (nb_want <= ssl->in_left) {
1889 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
1890 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001891 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001892
1893 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001894 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001895 * are not at the beginning of a new record, the caller did something
1896 * wrong.
1897 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001898 if (ssl->in_left != 0) {
1899 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1900 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001901 }
1902
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001903 /*
1904 * Don't even try to read if time's out already.
1905 * This avoids by-passing the timer when repeatedly receiving messages
1906 * that will end up being dropped.
1907 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001908 if (mbedtls_ssl_check_timer(ssl) != 0) {
1909 MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001910 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001911 } else {
1912 len = in_buf_len - (ssl->in_hdr - ssl->in_buf);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001914 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001915 timeout = ssl->handshake->retransmit_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001916 } else {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001917 timeout = ssl->conf->read_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001918 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001920 MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 if (ssl->f_recv_timeout != NULL) {
1923 ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len,
1924 timeout);
1925 } else {
1926 ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len);
1927 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001928
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 if (ret == 0) {
1932 return MBEDTLS_ERR_SSL_CONN_EOF;
1933 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001934 }
1935
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001936 if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
1937 MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
1938 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
1941 if (ssl_double_retransmit_timeout(ssl) != 0) {
1942 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout"));
1943 return MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001944 }
1945
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001946 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
1947 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
1948 return ret;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001949 }
1950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001952 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001954 else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
1955 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
1956 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
1957 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
1958 ret);
1959 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001960 }
1961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001963 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001965 }
1966
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001967 if (ret < 0) {
1968 return ret;
1969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001971 ssl->in_left = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001972 } else
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001973#endif
1974 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001975 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1976 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1977 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001979 while (ssl->in_left < nb_want) {
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001980 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001982 if (mbedtls_ssl_check_timer(ssl) != 0) {
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001983 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001984 } else {
1985 if (ssl->f_recv_timeout != NULL) {
1986 ret = ssl->f_recv_timeout(ssl->p_bio,
1987 ssl->in_hdr + ssl->in_left, len,
1988 ssl->conf->read_timeout);
1989 } else {
1990 ret = ssl->f_recv(ssl->p_bio,
1991 ssl->in_hdr + ssl->in_left, len);
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001992 }
1993 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1996 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1997 ssl->in_left, nb_want));
1998 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001999
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002000 if (ret == 0) {
2001 return MBEDTLS_ERR_SSL_CONN_EOF;
2002 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002003
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002004 if (ret < 0) {
2005 return ret;
2006 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002007
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002008 if ((size_t) ret > len || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) {
2009 MBEDTLS_SSL_DEBUG_MSG(1,
2010 ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2011 " were requested",
2012 ret, len));
2013 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16035bd15cb2018-02-28 04:30:59 -08002014 }
2015
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002016 ssl->in_left += ret;
2017 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 }
2019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002020 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002022 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002023}
2024
2025/*
2026 * Flush any data not yet written
2027 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002028int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002029{
Janos Follath865b3eb2019-12-16 11:46:15 +00002030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002031 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002035 if (ssl->f_send == NULL) {
2036 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
2037 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002038 }
2039
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002040 /* Avoid incrementing counter if data is flushed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002041 if (ssl->out_left == 0) {
2042 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
2043 return 0;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002044 }
2045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 while (ssl->out_left > 0) {
2047 MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET
2048 ", out_left: %" MBEDTLS_PRINTF_SIZET,
2049 mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left));
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
Hanno Becker2b1e3542018-08-06 11:19:13 +01002051 buf = ssl->out_hdr - ssl->out_left;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
Paul Bakker186751d2012-05-08 13:16:14 +00002053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002054 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002056 if (ret <= 0) {
2057 return ret;
2058 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002059
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002060 if ((size_t) ret > ssl->out_left || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) {
2061 MBEDTLS_SSL_DEBUG_MSG(1,
2062 ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2063 " bytes were sent",
2064 ret, ssl->out_left));
2065 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16034bbaeb42018-02-22 04:29:04 -08002066 }
2067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 ssl->out_left -= ret;
2069 }
2070
Hanno Becker2b1e3542018-08-06 11:19:13 +01002071#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002072 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002073 ssl->out_hdr = ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002074 } else
Hanno Becker2b1e3542018-08-06 11:19:13 +01002075#endif
2076 {
2077 ssl->out_hdr = ssl->out_buf + 8;
2078 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002079 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002081 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002084}
2085
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002086/*
2087 * Functions to handle the DTLS retransmission state machine
2088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002090/*
2091 * Append current handshake message to current outgoing flight
2092 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094static int ssl_flight_append(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 mbedtls_ssl_flight_item *msg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002097 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append"));
2098 MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight",
2099 ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002100
2101 /* Allocate space for current message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002102 if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) {
2103 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2104 sizeof(mbedtls_ssl_flight_item)));
2105 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002106 }
2107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) {
2109 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2110 ssl->out_msglen));
2111 mbedtls_free(msg);
2112 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002113 }
2114
2115 /* Copy current handshake message with headers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002116 memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002117 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002118 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002119 msg->next = NULL;
2120
2121 /* Append to the current flight */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002122 if (ssl->handshake->flight == NULL) {
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002123 ssl->handshake->flight = msg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002124 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002126 while (cur->next != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002127 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002128 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002129 cur->next = msg;
2130 }
2131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002132 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
2133 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002134}
2135
2136/*
2137 * Free the current flight of handshake messages
2138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 mbedtls_ssl_flight_item *cur = flight;
2142 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002144 while (cur != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002145 next = cur->next;
2146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002147 mbedtls_free(cur->p);
2148 mbedtls_free(cur);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002149
2150 cur = next;
2151 }
2152}
2153
2154/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002155 * Swap transform_out and out_ctr with the alternative ones
2156 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002157MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002159{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002161 unsigned char tmp_out_ctr[8];
2162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002163 if (ssl->transform_out == ssl->handshake->alt_transform_out) {
2164 MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
2165 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002166 }
2167
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002168 MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002169
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002170 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002171 tmp_transform = ssl->transform_out;
2172 ssl->transform_out = ssl->handshake->alt_transform_out;
2173 ssl->handshake->alt_transform_out = tmp_transform;
2174
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002175 /* Swap epoch + sequence_number */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002176 memcpy(tmp_out_ctr, ssl->cur_out_ctr, 8);
2177 memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8);
2178 memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, 8);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002179
2180 /* Adjust to the newly activated transform */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002181 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002184 if (mbedtls_ssl_hw_record_activate != NULL) {
2185 int ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND);
2186 if (ret != 0) {
2187 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
2188 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002189 }
2190 }
2191#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002193 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002194}
2195
2196/*
2197 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002198 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002199int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002200{
2201 int ret = 0;
2202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002203 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002205 ret = mbedtls_ssl_flight_transmit(ssl);
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002207 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002209 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002210}
2211
2212/*
2213 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002214 *
2215 * Need to remember the current message in case flush_output returns
2216 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002217 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002218 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002219int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002220{
Janos Follath865b3eb2019-12-16 11:46:15 +00002221 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002222 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002224 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
2225 MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002226
2227 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002228 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002229 ret = ssl_swap_epochs(ssl);
2230 if (ret != 0) {
2231 return ret;
2232 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002235 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002237 while (ssl->handshake->cur_msg != NULL) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002238 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002239 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002240
Hanno Beckere1dcb032018-08-17 16:47:58 +01002241 int const is_finished =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002242 (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2243 cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
Hanno Beckere1dcb032018-08-17 16:47:58 +01002244
Hanno Becker04da1892018-08-14 13:22:10 +01002245 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002246 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
Hanno Becker04da1892018-08-14 13:22:10 +01002247
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002248 /* Swap epochs before sending Finished: we can't do it after
2249 * sending ChangeCipherSpec, in case write returns WANT_READ.
2250 * Must be done before copying, may change out_msg pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002251 if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) {
2252 MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message"));
2253 ret = ssl_swap_epochs(ssl);
2254 if (ret != 0) {
2255 return ret;
2256 }
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002257 }
2258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002259 ret = ssl_get_remaining_payload_in_datagram(ssl);
2260 if (ret < 0) {
2261 return ret;
2262 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002263 max_frag_len = (size_t) ret;
2264
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002265 /* CCS is copied as is, while HS messages may need fragmentation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002266 if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2267 if (max_frag_len == 0) {
2268 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2269 return ret;
2270 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002271
2272 continue;
2273 }
2274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002275 memcpy(ssl->out_msg, cur->p, cur->len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002276 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002277 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002278
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002279 /* Update position inside current message */
2280 ssl->handshake->cur_msg_p += cur->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002281 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002282 const unsigned char * const p = ssl->handshake->cur_msg_p;
2283 const size_t hs_len = cur->len - 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002284 const size_t frag_off = p - (cur->p + 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002285 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002286 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002288 if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) {
2289 if (is_finished) {
2290 ret = ssl_swap_epochs(ssl);
2291 if (ret != 0) {
2292 return ret;
2293 }
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002294 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002296 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2297 return ret;
2298 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002299
2300 continue;
2301 }
2302 max_hs_frag_len = max_frag_len - 12;
2303
2304 cur_hs_frag_len = rem_len > max_hs_frag_len ?
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002305 max_hs_frag_len : rem_len;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 if (frag_off == 0 && cur_hs_frag_len != hs_len) {
2308 MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)",
2309 (unsigned) cur_hs_frag_len,
2310 (unsigned) max_hs_frag_len));
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002311 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002312
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002313 /* Messages are stored with handshake headers as if not fragmented,
2314 * copy beginning of headers then fill fragmentation fields.
2315 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 memcpy(ssl->out_msg, cur->p, 6);
Joe Subbiani61f7d732021-06-24 09:06:23 +01002317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off);
2319 ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off);
2320 ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002322 ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len);
2323 ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len);
2324 ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002326 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002327
Hanno Becker3f7b9732018-08-28 09:53:25 +01002328 /* Copy the handshake message content and set records fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002330 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002331 ssl->out_msgtype = cur->type;
2332
2333 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002334 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002335 }
2336
2337 /* If done with the current message move to the next one if any */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002338 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
2339 if (cur->next != NULL) {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002340 ssl->handshake->cur_msg = cur->next;
2341 ssl->handshake->cur_msg_p = cur->next->p + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002342 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002343 ssl->handshake->cur_msg = NULL;
2344 ssl->handshake->cur_msg_p = NULL;
2345 }
2346 }
2347
2348 /* Actually send the message out */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2350 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
2351 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002352 }
2353 }
2354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002355 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2356 return ret;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002357 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002359 /* Update state and set timer */
2360 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
2361 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
2362 } else {
2363 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
2364 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
2365 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002367 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
2368
2369 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002370}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002371
2372/*
2373 * To be called when the last message of an incoming flight is received.
2374 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002375void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002376{
2377 /* We won't need to resend that one any more */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002378 mbedtls_ssl_flight_free(ssl->handshake->flight);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002379 ssl->handshake->flight = NULL;
2380 ssl->handshake->cur_msg = NULL;
2381
2382 /* The next incoming flight will start with this msg_seq */
2383 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2384
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002385 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002386 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002387
Hanno Becker0271f962018-08-16 13:23:47 +01002388 /* Clear future message buffering structure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 mbedtls_ssl_buffering_free(ssl);
Hanno Becker0271f962018-08-16 13:23:47 +01002390
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002391 /* Cancel timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002392 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002394 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2395 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002397 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002399 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002400}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002401
2402/*
2403 * To be called when the last message of an outgoing flight is send.
2404 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002406{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002407 ssl_reset_retransmit_timeout(ssl);
2408 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002410 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2411 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002413 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002415 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002416}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002420 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002422
2423/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002424 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002425 *
2426 * - fill in handshake headers
2427 * - update handshake checksum
2428 * - DTLS: save message for resending
2429 * - then pass to the record layer
2430 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002431 * DTLS: except for HelloRequest, messages are only queued, and will only be
2432 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002433 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002434 * Inputs:
2435 * - ssl->out_msglen: 4 + actual handshake message len
2436 * (4 is the size of handshake headers for TLS)
2437 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2438 * - ssl->out_msg + 4: the handshake message body
2439 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002440 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002441 * - ssl->out_msglen: the length of the record contents
2442 * (including handshake headers but excluding record headers)
2443 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002444 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002445int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002446{
Janos Follath865b3eb2019-12-16 11:46:15 +00002447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002448 const size_t hs_len = ssl->out_msglen - 4;
2449 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002451 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002452
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002453 /*
2454 * Sanity checks
2455 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456 if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
2457 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002458 /* In SSLv3, the client might send a NoCertificate alert. */
2459#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002460 if (!(ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2461 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2462 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT))
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002463#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2464 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002465 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2466 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002467 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002468 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002470 /* Whenever we send anything different from a
2471 * HelloRequest we should be in a handshake - double check. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002472 if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2473 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) &&
2474 ssl->handshake == NULL) {
2475 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2476 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002480 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002481 ssl->handshake != NULL &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002482 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
2483 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2484 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002485 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002486#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002487
Hanno Beckerb50a2532018-08-06 11:52:54 +01002488 /* Double-check that we did not exceed the bounds
2489 * of the outgoing record buffer.
2490 * This should never fail as the various message
2491 * writing functions must obey the bounds of the
2492 * outgoing record buffer, but better be safe.
2493 *
2494 * Note: We deliberately do not check for the MTU or MFL here.
2495 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002496 if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2497 MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: "
2498 "size %" MBEDTLS_PRINTF_SIZET
2499 ", maximum %" MBEDTLS_PRINTF_SIZET,
2500 ssl->out_msglen,
2501 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2502 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerb50a2532018-08-06 11:52:54 +01002503 }
2504
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002505 /*
2506 * Fill handshake headers
2507 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002508 if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
2509 ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len);
2510 ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len);
2511 ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00002512
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002513 /*
2514 * DTLS has additional fields in the Handshake layer,
2515 * between the length field and the actual payload:
2516 * uint16 message_seq;
2517 * uint24 fragment_offset;
2518 * uint24 fragment_length;
2519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002521 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002522 /* Make room for the additional DTLS fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002523 if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) {
2524 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: "
2525 "size %" MBEDTLS_PRINTF_SIZET ", maximum %"
2526 MBEDTLS_PRINTF_SIZET,
2527 hs_len,
2528 (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12)));
2529 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9648f8b2017-09-18 10:55:54 +01002530 }
2531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002532 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002533 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002534
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002535 /* Write message_seq and update it, except for HelloRequest */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002536 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2537 MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4);
2538 ++(ssl->handshake->out_msg_seq);
2539 } else {
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002540 ssl->out_msg[4] = 0;
2541 ssl->out_msg[5] = 0;
2542 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002543
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002544 /* Handshake hashes are computed without fragmentation,
2545 * so set frag_offset = 0 and frag_len = hs_len for now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002546 memset(ssl->out_msg + 6, 0x00, 3);
2547 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002548 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002550
Hanno Becker0207e532018-08-28 10:28:28 +01002551 /* Update running hashes of handshake messages seen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002552 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2553 ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen);
2554 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002557 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002559 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2560 !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2561 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) {
2562 if ((ret = ssl_flight_append(ssl)) != 0) {
2563 MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret);
2564 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002565 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002566 } else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002567#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002568 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002569 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
2570 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret);
2571 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002572 }
2573 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002575 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002576
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 return 0;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002578}
2579
2580/*
2581 * Record layer functions
2582 */
2583
2584/*
2585 * Write current record.
2586 *
2587 * Uses:
2588 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2589 * - ssl->out_msglen: length of the record content (excl headers)
2590 * - ssl->out_msg: record content
2591 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002592int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, uint8_t force_flush)
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002593{
2594 int ret, done = 0;
2595 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002596 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002598 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002601 if (ssl->transform_out != NULL &&
2602 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
2603 if ((ret = ssl_compress_buf(ssl)) != 0) {
2604 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compress_buf", ret);
2605 return ret;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002606 }
2607
2608 len = ssl->out_msglen;
2609 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002613 if (mbedtls_ssl_hw_record_write != NULL) {
2614 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_write()"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002615
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002616 ret = mbedtls_ssl_hw_record_write(ssl);
2617 if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) {
2618 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_write", ret);
2619 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker05ef8352012-05-08 09:17:57 +00002620 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002622 if (ret == 0) {
Paul Bakkerc7878112012-12-19 14:41:14 +01002623 done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624 }
Paul Bakker05ef8352012-05-08 09:17:57 +00002625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002627 if (!done) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002628 unsigned i;
2629 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002630#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2631 size_t out_buf_len = ssl->out_buf_len;
2632#else
2633 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2634#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002635 /* Skip writing the record content type to after the encryption,
2636 * as it may change when using the CID extension. */
2637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002638 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2639 ssl->conf->transport, ssl->out_hdr + 1);
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002640
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002641 memcpy(ssl->out_ctr, ssl->cur_out_ctr, 8);
2642 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002644 if (ssl->transform_out != NULL) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002645 mbedtls_record rec;
2646
2647 rec.buf = ssl->out_iv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002648 rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002649 rec.data_len = ssl->out_msglen;
2650 rec.data_offset = ssl->out_msg - rec.buf;
2651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 memcpy(&rec.ctr[0], ssl->out_ctr, 8);
2653 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2654 ssl->conf->transport, rec.ver);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002655 rec.type = ssl->out_msgtype;
2656
Hanno Beckera0e20d02019-05-15 14:03:01 +01002657#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002658 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002659 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002660#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002662 if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec,
2663 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2664 MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret);
2665 return ret;
Paul Bakker05ef8352012-05-08 09:17:57 +00002666 }
2667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002668 if (rec.data_offset != 0) {
2669 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2670 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002671 }
2672
Hanno Becker6430faf2019-05-08 11:57:13 +01002673 /* Update the record content type and CID. */
2674 ssl->out_msgtype = rec.type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002675#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2676 memcpy(ssl->out_cid, rec.cid, rec.cid_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01002677#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002678 ssl->out_msglen = len = rec.data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002680 }
2681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002682 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002683
2684#if defined(MBEDTLS_SSL_PROTO_DTLS)
2685 /* In case of DTLS, double-check that we don't exceed
2686 * the remaining space in the datagram. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002687 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2688 ret = ssl_get_remaining_space_in_datagram(ssl);
2689 if (ret < 0) {
2690 return ret;
2691 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002693 if (protected_record_size > (size_t) ret) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002694 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002695 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker2b1e3542018-08-06 11:19:13 +01002696 }
2697 }
2698#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002699
Hanno Becker6430faf2019-05-08 11:57:13 +01002700 /* Now write the potentially updated record content type. */
2701 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002703 MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, "
2704 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
2705 ssl->out_hdr[0], ssl->out_hdr[1],
2706 ssl->out_hdr[2], len));
Paul Bakker05ef8352012-05-08 09:17:57 +00002707
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002708 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
2709 ssl->out_hdr, protected_record_size);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002710
2711 ssl->out_left += protected_record_size;
2712 ssl->out_hdr += protected_record_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002713 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002714
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002715 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
2716 if (++ssl->cur_out_ctr[i - 1] != 0) {
Hanno Becker04484622018-08-06 09:49:38 +01002717 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002718 }
2719 }
Hanno Becker04484622018-08-06 09:49:38 +01002720
2721 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002722 if (i == mbedtls_ssl_ep_len(ssl)) {
2723 MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap"));
2724 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker04484622018-08-06 09:49:38 +01002725 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
Hanno Becker67bc7c32018-08-06 11:33:50 +01002728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002729 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2730 flush == SSL_DONT_FORCE_FLUSH) {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002731 size_t remaining;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002732 ret = ssl_get_remaining_payload_in_datagram(ssl);
2733 if (ret < 0) {
2734 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram",
2735 ret);
2736 return ret;
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002737 }
2738
2739 remaining = (size_t) ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 if (remaining == 0) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002741 flush = SSL_FORCE_FLUSH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002742 } else {
2743 MBEDTLS_SSL_DEBUG_MSG(2,
2744 ("Still %u bytes available in current datagram",
2745 (unsigned) remaining));
Hanno Becker67bc7c32018-08-06 11:33:50 +01002746 }
2747 }
2748#endif /* MBEDTLS_SSL_PROTO_DTLS */
2749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002750 if ((flush == SSL_FORCE_FLUSH) &&
2751 (ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2752 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
2753 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 }
2755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002756 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002758 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002759}
2760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002762
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002763MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002764static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002765{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002766 if (ssl->in_msglen < ssl->in_hslen ||
2767 memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 ||
2768 memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) {
2769 return 1;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002770 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002771 return 0;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002772}
Hanno Becker44650b72018-08-16 12:51:11 +01002773
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002774static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002775{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002776 return (ssl->in_msg[9] << 16) |
2777 (ssl->in_msg[10] << 8) |
2778 ssl->in_msg[11];
Hanno Becker44650b72018-08-16 12:51:11 +01002779}
2780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002781static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002782{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002783 return (ssl->in_msg[6] << 16) |
2784 (ssl->in_msg[7] << 8) |
2785 ssl->in_msg[8];
Hanno Becker44650b72018-08-16 12:51:11 +01002786}
2787
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002789static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002790{
2791 uint32_t msg_len, frag_off, frag_len;
2792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002793 msg_len = ssl_get_hs_total_len(ssl);
2794 frag_off = ssl_get_hs_frag_off(ssl);
2795 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker44650b72018-08-16 12:51:11 +01002796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002797 if (frag_off > msg_len) {
2798 return -1;
2799 }
Hanno Becker44650b72018-08-16 12:51:11 +01002800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002801 if (frag_len > msg_len - frag_off) {
2802 return -1;
2803 }
Hanno Becker44650b72018-08-16 12:51:11 +01002804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002805 if (frag_len + 12 > ssl->in_msglen) {
2806 return -1;
2807 }
Hanno Becker44650b72018-08-16 12:51:11 +01002808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002809 return 0;
Hanno Becker44650b72018-08-16 12:51:11 +01002810}
2811
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002812/*
2813 * Mark bits in bitmask (used for DTLS HS reassembly)
2814 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002815static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002816{
2817 unsigned int start_bits, end_bits;
2818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002819 start_bits = 8 - (offset % 8);
2820 if (start_bits != 8) {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002821 size_t first_byte_idx = offset / 8;
2822
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002823 /* Special case */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002824 if (len <= start_bits) {
2825 for (; len != 0; len--) {
2826 mask[first_byte_idx] |= 1 << (start_bits - len);
2827 }
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002828
2829 /* Avoid potential issues with offset or len becoming invalid */
2830 return;
2831 }
2832
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002833 offset += start_bits; /* Now offset % 8 == 0 */
2834 len -= start_bits;
2835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002836 for (; start_bits != 0; start_bits--) {
2837 mask[first_byte_idx] |= 1 << (start_bits - 1);
2838 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002839 }
2840
2841 end_bits = len % 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002842 if (end_bits != 0) {
2843 size_t last_byte_idx = (offset + len) / 8;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002844
2845 len -= end_bits; /* Now len % 8 == 0 */
2846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 for (; end_bits != 0; end_bits--) {
2848 mask[last_byte_idx] |= 1 << (8 - end_bits);
2849 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002850 }
2851
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002852 memset(mask + offset / 8, 0xFF, len / 8);
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002853}
2854
2855/*
2856 * Check that bitmask is full
2857 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002859static int ssl_bitmask_check(unsigned char *mask, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002860{
2861 size_t i;
2862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002863 for (i = 0; i < len / 8; i++) {
2864 if (mask[i] != 0xFF) {
2865 return -1;
2866 }
2867 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002868
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002869 for (i = 0; i < len % 8; i++) {
2870 if ((mask[len / 8] & (1 << (7 - i))) == 0) {
2871 return -1;
2872 }
2873 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002874
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002875 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002876}
2877
Hanno Becker56e205e2018-08-16 09:06:12 +01002878/* msg_len does not include the handshake header */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002879static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
2880 unsigned add_bitmap)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002881{
Hanno Becker56e205e2018-08-16 09:06:12 +01002882 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002883
Hanno Becker56e205e2018-08-16 09:06:12 +01002884 alloc_len = 12; /* Handshake header */
2885 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002887 if (add_bitmap) {
2888 alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002889
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002890 }
2891 return alloc_len;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002892}
Hanno Becker56e205e2018-08-16 09:06:12 +01002893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002896static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
Hanno Becker12555c62018-08-16 12:47:53 +01002897{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 return (ssl->in_msg[1] << 16) |
2899 (ssl->in_msg[2] << 8) |
2900 ssl->in_msg[3];
Hanno Becker12555c62018-08-16 12:47:53 +01002901}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002902
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002903int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002904{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002905 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
2906 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
2907 ssl->in_msglen));
2908 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002909 }
2910
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002911 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002913 MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
2914 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
2915 MBEDTLS_PRINTF_SIZET,
2916 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002919 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002921 unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002922
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002923 if (ssl_check_hs_header(ssl) != 0) {
2924 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
2925 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker44650b72018-08-16 12:51:11 +01002926 }
2927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002928 if (ssl->handshake != NULL &&
2929 ((ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2930 recv_msg_seq != ssl->handshake->in_msg_seq) ||
2931 (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2932 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
2933 if (recv_msg_seq > ssl->handshake->in_msg_seq) {
2934 MBEDTLS_SSL_DEBUG_MSG(2,
2935 (
2936 "received future handshake message of sequence number %u (next %u)",
2937 recv_msg_seq,
2938 ssl->handshake->in_msg_seq));
2939 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Becker9e1ec222018-08-15 15:54:43 +01002940 }
2941
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002942 /* Retransmit only on last message from previous flight, to avoid
2943 * too many retransmissions.
2944 * Besides, No sane server ever retransmits HelloVerifyRequest */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002945 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
2946 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
2947 MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
2948 "message_seq = %u, start_of_flight = %u",
2949 recv_msg_seq,
2950 ssl->handshake->in_flight_start_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002951
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002952 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
2953 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
2954 return ret;
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002955 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956 } else {
2957 MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
2958 "message_seq = %u, expected = %u",
2959 recv_msg_seq,
2960 ssl->handshake->in_msg_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002961 }
2962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002963 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002964 }
2965 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002966
Hanno Becker6d97ef52018-08-16 13:09:04 +01002967 /* Message reassembly is handled alongside buffering of future
2968 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002969 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002970 * handshake logic layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002971 if (ssl_hs_is_proper_fragment(ssl) == 1) {
2972 MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
2973 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002974 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002975 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002977 /* With TLS we don't handle fragmentation (for now) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002978 if (ssl->in_msglen < ssl->in_hslen) {
2979 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
2980 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002981 }
2982
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002983 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01002984}
2985
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01002987{
Hanno Becker0271f962018-08-16 13:23:47 +01002988 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002990 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL) {
2991 ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002992 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002993
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002994 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002996 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2997 ssl->handshake != NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01002998 unsigned offset;
2999 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003000
Hanno Becker0271f962018-08-16 13:23:47 +01003001 /* Increment handshake sequence number */
3002 hs->in_msg_seq++;
3003
3004 /*
3005 * Clear up handshake buffering and reassembly structure.
3006 */
3007
3008 /* Free first entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003009 ssl_buffering_free_slot(ssl, 0);
Hanno Becker0271f962018-08-16 13:23:47 +01003010
3011 /* Shift all other entries */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003012 for (offset = 0, hs_buf = &hs->buffering.hs[0];
Hanno Beckere605b192018-08-21 15:59:07 +01003013 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003014 offset++, hs_buf++) {
Hanno Becker0271f962018-08-16 13:23:47 +01003015 *hs_buf = *(hs_buf + 1);
3016 }
3017
3018 /* Create a fresh last entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003019 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003020 }
3021#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003022}
3023
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003024/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003025 * DTLS anti-replay: RFC 6347 4.1.2.6
3026 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003027 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3028 * Bit n is set iff record number in_window_top - n has been seen.
3029 *
3030 * Usually, in_window_top is the last record number seen and the lsb of
3031 * in_window is set. The only exception is the initial state (record number 0
3032 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003033 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003035void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003036{
3037 ssl->in_window_top = 0;
3038 ssl->in_window = 0;
3039}
3040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003041static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003042{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003043 return ((uint64_t) buf[0] << 40) |
3044 ((uint64_t) buf[1] << 32) |
3045 ((uint64_t) buf[2] << 24) |
3046 ((uint64_t) buf[3] << 16) |
3047 ((uint64_t) buf[4] << 8) |
3048 ((uint64_t) buf[5]);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003049}
3050
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003051MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003053{
Janos Follath865b3eb2019-12-16 11:46:15 +00003054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003055 unsigned char *original_in_ctr;
3056
3057 // save original in_ctr
3058 original_in_ctr = ssl->in_ctr;
3059
3060 // use counter from record
3061 ssl->in_ctr = record_in_ctr;
3062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003064
3065 // restore the counter
3066 ssl->in_ctr = original_in_ctr;
3067
3068 return ret;
3069}
3070
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003071/*
3072 * Return 0 if sequence number is acceptable, -1 otherwise
3073 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003075{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003076 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003077 uint64_t bit;
3078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003079 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
3080 return 0;
3081 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003083 if (rec_seqnum > ssl->in_window_top) {
3084 return 0;
3085 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003086
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003087 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003088
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003089 if (bit >= 64) {
3090 return -1;
3091 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003093 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
3094 return -1;
3095 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003097 return 0;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003098}
3099
3100/*
3101 * Update replay window on new validated record
3102 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003103void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003104{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003105 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003107 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003108 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003109 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003111 if (rec_seqnum > ssl->in_window_top) {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003112 /* Update window_top and the contents of the window */
3113 uint64_t shift = rec_seqnum - ssl->in_window_top;
3114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003115 if (shift >= 64) {
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003116 ssl->in_window = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003117 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003118 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003119 ssl->in_window |= 1;
3120 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003121
3122 ssl->in_window_top = rec_seqnum;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003123 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003124 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003125 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003127 if (bit < 64) { /* Always true, but be extra sure */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003128 ssl->in_window |= (uint64_t) 1 << bit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003129 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003130 }
3131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003133
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003134#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003135/*
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003136 * Check if a datagram looks like a ClientHello with a valid cookie,
3137 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003138 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003139 *
3140 * - if cookie is valid, return 0
3141 * - if ClientHello looks superficially valid but cookie is not,
3142 * fill obuf and set olen, then
3143 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3144 * - otherwise return a specific error code
3145 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003146MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek33f41a82022-06-08 11:47:33 -04003147MBEDTLS_STATIC_TESTABLE
3148int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003149 mbedtls_ssl_context *ssl,
3150 const unsigned char *cli_id, size_t cli_id_len,
3151 const unsigned char *in, size_t in_len,
3152 unsigned char *obuf, size_t buf_len, size_t *olen)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003153{
3154 size_t sid_len, cookie_len;
3155 unsigned char *p;
3156
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003157 /*
3158 * Structure of ClientHello with record and handshake headers,
3159 * and expected values. We don't need to check a lot, more checks will be
3160 * done when actually parsing the ClientHello - skipping those checks
3161 * avoids code duplication and does not make cookie forging any easier.
3162 *
3163 * 0-0 ContentType type; copied, must be handshake
3164 * 1-2 ProtocolVersion version; copied
3165 * 3-4 uint16 epoch; copied, must be 0
3166 * 5-10 uint48 sequence_number; copied
3167 * 11-12 uint16 length; (ignored)
3168 *
3169 * 13-13 HandshakeType msg_type; (ignored)
3170 * 14-16 uint24 length; (ignored)
3171 * 17-18 uint16 message_seq; copied
3172 * 19-21 uint24 fragment_offset; copied, must be 0
3173 * 22-24 uint24 fragment_length; (ignored)
3174 *
3175 * 25-26 ProtocolVersion client_version; (ignored)
3176 * 27-58 Random random; (ignored)
3177 * 59-xx SessionID session_id; 1 byte len + sid_len content
3178 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3179 * ...
3180 *
3181 * Minimum length is 61 bytes.
3182 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003183 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
3184 (unsigned) in_len));
3185 MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
3186 if (in_len < 61) {
3187 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
3188 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003189 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003190 if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003191 in[3] != 0 || in[4] != 0 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003192 in[19] != 0 || in[20] != 0 || in[21] != 0) {
3193 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
3194 MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
3195 in[0],
3196 (unsigned) in[3] << 8 | in[4],
3197 (unsigned) in[19] << 16 | in[20] << 8 | in[21]));
3198 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003199 }
3200
3201 sid_len = in[59];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003202 if (59 + 1 + sid_len + 1 > in_len) {
3203 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
3204 (unsigned) sid_len,
3205 (unsigned) in_len - 61));
3206 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003207 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003208 MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
3209 in + 60, sid_len);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003210
3211 cookie_len = in[60 + sid_len];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003212 if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
3213 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
3214 (unsigned) cookie_len,
3215 (unsigned) (in_len - sid_len - 61)));
3216 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003217 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
3220 in + sid_len + 61, cookie_len);
3221 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
3222 in + sid_len + 61, cookie_len,
3223 cli_id, cli_id_len) == 0) {
3224 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
3225 return 0;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003226 }
3227
3228 /*
3229 * If we get here, we've got an invalid cookie, let's prepare HVR.
3230 *
3231 * 0-0 ContentType type; copied
3232 * 1-2 ProtocolVersion version; copied
3233 * 3-4 uint16 epoch; copied
3234 * 5-10 uint48 sequence_number; copied
3235 * 11-12 uint16 length; olen - 13
3236 *
3237 * 13-13 HandshakeType msg_type; hello_verify_request
3238 * 14-16 uint24 length; olen - 25
3239 * 17-18 uint16 message_seq; copied
3240 * 19-21 uint24 fragment_offset; copied
3241 * 22-24 uint24 fragment_length; olen - 25
3242 *
3243 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3244 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3245 *
3246 * Minimum length is 28.
3247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003248 if (buf_len < 28) {
3249 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3250 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003251
3252 /* Copy most fields and adapt others */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003253 memcpy(obuf, in, 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003254 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3255 obuf[25] = 0xfe;
3256 obuf[26] = 0xff;
3257
3258 /* Generate and write actual cookie */
3259 p = obuf + 28;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003260 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
3261 &p, obuf + buf_len,
3262 cli_id, cli_id_len) != 0) {
3263 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003264 }
3265
3266 *olen = p - obuf;
3267
3268 /* Go back and fill length fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003269 obuf[27] = (unsigned char) (*olen - 28);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003270
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
3272 obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
3273 obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003274
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003275 MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003277 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003278}
3279
3280/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003281 * Handle possible client reconnect with the same UDP quadruplet
3282 * (RFC 6347 Section 4.2.8).
3283 *
3284 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3285 * that looks like a ClientHello.
3286 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003287 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003288 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003289 * - if the input looks like a ClientHello with a valid cookie,
3290 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003291 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003292 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003293 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003294 * This function is called (through ssl_check_client_reconnect()) when an
3295 * unexpected record is found in ssl_get_next_record(), which will discard the
3296 * record if we return 0, and bubble up the return value otherwise (this
3297 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3298 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003299 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003300MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003301static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003302{
Janos Follath865b3eb2019-12-16 11:46:15 +00003303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003304 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003306 if (ssl->conf->f_cookie_write == NULL ||
3307 ssl->conf->f_cookie_check == NULL) {
Hanno Becker2fddd372019-07-10 14:37:41 +01003308 /* If we can't use cookies to verify reachability of the peer,
3309 * drop the record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003310 MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
3311 "can't check reconnect validity"));
3312 return 0;
Hanno Becker2fddd372019-07-10 14:37:41 +01003313 }
3314
Andrzej Kurek33f41a82022-06-08 11:47:33 -04003315 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003316 ssl,
3317 ssl->cli_id, ssl->cli_id_len,
3318 ssl->in_buf, ssl->in_left,
3319 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003322
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003323 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003324 int send_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003325 MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
3326 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3327 ssl->out_buf, len);
Brian J Murray1903fb32016-11-06 04:45:15 -08003328 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003329 * If the error is permanent we'll catch it later,
3330 * if it's not, then hopefully it'll work next time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003331 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
3332 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003333 (void) send_ret;
3334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003335 return 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003336 }
3337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003338 if (ret == 0) {
3339 MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
3340 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
3341 MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
3342 return ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003343 }
3344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003345 return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003346 }
3347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003348 return ret;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003349}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003350#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003351
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003352MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003353static int ssl_check_record_type(uint8_t record_type)
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003354{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003355 if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003356 record_type != MBEDTLS_SSL_MSG_ALERT &&
3357 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003358 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
3359 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003360 }
3361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003362 return 0;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003363}
3364
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003365/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003366 * ContentType type;
3367 * ProtocolVersion version;
3368 * uint16 epoch; // DTLS only
3369 * uint48 sequence_number; // DTLS only
3370 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003371 *
3372 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003373 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003374 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3375 *
3376 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003377 * 1. proceed with the record if this function returns 0
3378 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3379 * 3. return CLIENT_RECONNECT if this function return that value
3380 * 4. drop the whole datagram if this function returns anything else.
3381 * Point 2 is needed when the peer is resending, and we have already received
3382 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003383 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003384MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003385static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
3386 unsigned char *buf,
3387 size_t len,
3388 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00003389{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003390 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
Hanno Beckere5e7e782019-07-11 12:29:35 +01003392 size_t const rec_hdr_type_offset = 0;
3393 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003394
Hanno Beckere5e7e782019-07-11 12:29:35 +01003395 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3396 rec_hdr_type_len;
3397 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003398
Hanno Beckere5e7e782019-07-11 12:29:35 +01003399 size_t const rec_hdr_ctr_len = 8;
3400#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003401 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003402 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3403 rec_hdr_version_len;
3404
Hanno Beckera0e20d02019-05-15 14:03:01 +01003405#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003406 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3407 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003408 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003409#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3410#endif /* MBEDTLS_SSL_PROTO_DTLS */
3411
3412 size_t rec_hdr_len_offset; /* To be determined */
3413 size_t const rec_hdr_len_len = 2;
3414
3415 /*
3416 * Check minimum lengths for record header.
3417 */
3418
3419#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003420 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003421 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003422 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003423#endif /* MBEDTLS_SSL_PROTO_DTLS */
3424 {
3425 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3426 }
3427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003428 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3429 MBEDTLS_SSL_DEBUG_MSG(1,
3430 (
3431 "datagram of length %u too small to hold DTLS record header of length %u",
3432 (unsigned) len,
3433 (unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
3434 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003435 }
3436
3437 /*
3438 * Parse and validate record content type
3439 */
3440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003441 rec->type = buf[rec_hdr_type_offset];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003442
3443 /* Check record content type */
3444#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3445 rec->cid_len = 0;
3446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003447 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003448 ssl->conf->cid_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003449 rec->type == MBEDTLS_SSL_MSG_CID) {
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003450 /* Shift pointers to account for record header including CID
3451 * struct {
3452 * ContentType special_type = tls12_cid;
3453 * ProtocolVersion version;
3454 * uint16 epoch;
3455 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003456 * opaque cid[cid_length]; // Additional field compared to
3457 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003458 * uint16 length;
3459 * opaque enc_content[DTLSCiphertext.length];
3460 * } DTLSCiphertext;
3461 */
3462
3463 /* So far, we only support static CID lengths
3464 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003465 rec_hdr_cid_len = ssl->conf->cid_len;
3466 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003468 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3469 MBEDTLS_SSL_DEBUG_MSG(1,
3470 (
3471 "datagram of length %u too small to hold DTLS record header including CID, length %u",
3472 (unsigned) len,
3473 (unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
3474 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere538d822019-07-10 14:50:10 +01003475 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003476
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003477 /* configured CID len is guaranteed at most 255, see
3478 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3479 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003480 memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
3481 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003483 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003484 if (ssl_check_record_type(rec->type)) {
3485 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
3486 (unsigned) rec->type));
3487 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003488 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003489 }
3490
Hanno Beckere5e7e782019-07-11 12:29:35 +01003491 /*
3492 * Parse and validate record version
3493 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003494 rec->ver[0] = buf[rec_hdr_version_offset + 0];
3495 rec->ver[1] = buf[rec_hdr_version_offset + 1];
3496 mbedtls_ssl_read_version(&major_ver, &minor_ver,
3497 ssl->conf->transport,
3498 &rec->ver[0]);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003499
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003500 if (major_ver != ssl->major_ver) {
3501 MBEDTLS_SSL_DEBUG_MSG(1, ("major version mismatch: got %u, expected %u",
3502 (unsigned) major_ver,
3503 (unsigned) ssl->major_ver));
3504 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003505 }
3506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003507 if (minor_ver > ssl->conf->max_minor_ver) {
3508 MBEDTLS_SSL_DEBUG_MSG(1, ("minor version mismatch: got %u, expected max %u",
3509 (unsigned) minor_ver,
3510 (unsigned) ssl->conf->max_minor_ver));
3511 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003512 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003513 /*
3514 * Parse/Copy record sequence number.
3515 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003516
Hanno Beckere5e7e782019-07-11 12:29:35 +01003517#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003519 /* Copy explicit record sequence number from input buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003520 memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
3521 rec_hdr_ctr_len);
3522 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003523#endif /* MBEDTLS_SSL_PROTO_DTLS */
3524 {
3525 /* Copy implicit record sequence number from SSL context structure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003526 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003527 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003528
Hanno Beckere5e7e782019-07-11 12:29:35 +01003529 /*
3530 * Parse record length.
3531 */
3532
Hanno Beckere5e7e782019-07-11 12:29:35 +01003533 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003534 rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) |
3535 ((size_t) buf[rec_hdr_len_offset + 1] << 0);
3536 MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003538 MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
3539 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
3540 rec->type,
3541 major_ver, minor_ver, rec->data_len));
Hanno Beckere5e7e782019-07-11 12:29:35 +01003542
3543 rec->buf = buf;
3544 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003546 if (rec->data_len == 0) {
3547 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003549
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003550 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003551 * DTLS-related tests.
3552 * Check epoch before checking length constraint because
3553 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3554 * message gets duplicated before the corresponding Finished message,
3555 * the second ChangeCipherSpec should be discarded because it belongs
3556 * to an old epoch, but not because its length is shorter than
3557 * the minimum record length for packets using the new record transform.
3558 * Note that these two kinds of failures are handled differently,
3559 * as an unexpected record is silently skipped but an invalid
3560 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003561 */
3562#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003563 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3564 rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003565
Hanno Becker955a5c92019-07-10 17:12:07 +01003566 /* Check that the datagram is large enough to contain a record
3567 * of the advertised length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003568 if (len < rec->data_offset + rec->data_len) {
3569 MBEDTLS_SSL_DEBUG_MSG(1,
3570 (
3571 "Datagram of length %u too small to contain record of advertised length %u.",
3572 (unsigned) len,
3573 (unsigned) (rec->data_offset + rec->data_len)));
3574 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker955a5c92019-07-10 17:12:07 +01003575 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003576
Hanno Becker37cfe732019-07-10 17:20:01 +01003577 /* Records from other, non-matching epochs are silently discarded.
3578 * (The case of same-port Client reconnects must be considered in
3579 * the caller). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003580 if (rec_epoch != ssl->in_epoch) {
3581 MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
3582 "expected %u, received %lu",
3583 ssl->in_epoch, (unsigned long) rec_epoch));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003584
Hanno Becker552f7472019-07-19 10:59:12 +01003585 /* Records from the next epoch are considered for buffering
3586 * (concretely: early Finished messages). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003587 if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
3588 MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
3589 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003590 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003591
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003592 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003593 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003594#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003595 /* For records from the correct epoch, check whether their
3596 * sequence number has been seen before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003597 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
3598 &rec->ctr[0]) != 0) {
3599 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
3600 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003601 }
3602#endif
3603 }
3604#endif /* MBEDTLS_SSL_PROTO_DTLS */
3605
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003606 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003607}
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Hanno Becker2fddd372019-07-10 14:37:41 +01003610#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003611MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003612static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
Hanno Becker2fddd372019-07-10 14:37:41 +01003613{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003614 unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1];
Hanno Becker2fddd372019-07-10 14:37:41 +01003615
3616 /*
3617 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3618 * access the first byte of record content (handshake type), as we
3619 * have an active transform (possibly iv_len != 0), so use the
3620 * fact that the record header len is 13 instead.
3621 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003622 if (rec_epoch == 0 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003623 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3624 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3625 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3626 ssl->in_left > 13 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003627 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
3628 MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
3629 "from the same port"));
3630 return ssl_handle_possible_reconnect(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 }
3632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003633 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003634}
Hanno Becker2fddd372019-07-10 14:37:41 +01003635#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003637/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003638 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003639 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003640MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003641static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
3642 mbedtls_record *rec)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003643{
3644 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003646 MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
3647 rec->buf, rec->buf_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003650 if (mbedtls_ssl_hw_record_read != NULL) {
3651 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_read()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00003652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003653 ret = mbedtls_ssl_hw_record_read(ssl);
3654 if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) {
3655 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_read", ret);
3656 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker05ef8352012-05-08 09:17:57 +00003657 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003659 if (ret == 0) {
Paul Bakkerc7878112012-12-19 14:41:14 +01003660 done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 if (!done && ssl->transform_in != NULL) {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003665 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003667 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
3668 rec)) != 0) {
3669 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
Hanno Becker8367ccc2019-05-14 11:30:10 +01003670
Hanno Beckera0e20d02019-05-15 14:03:01 +01003671#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Becker8367ccc2019-05-14 11:30:10 +01003673 ssl->conf->ignore_unexpected_cid
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003674 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
3675 MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
Hanno Becker16ded982019-05-08 13:02:55 +01003676 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003677 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003678#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003679
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003680 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 }
3682
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003683 if (old_msg_type != rec->type) {
3684 MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
3685 old_msg_type, rec->type));
Hanno Becker6430faf2019-05-08 11:57:13 +01003686 }
3687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003688 MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
3689 rec->buf + rec->data_offset, rec->data_len);
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003690
Hanno Beckera0e20d02019-05-15 14:03:01 +01003691#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003692 /* We have already checked the record content type
3693 * in ssl_parse_record_header(), failing or silently
3694 * dropping the record in the case of an unknown type.
3695 *
3696 * Since with the use of CIDs, the record content type
3697 * might change during decryption, re-check the record
3698 * content type, but treat a failure as fatal this time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699 if (ssl_check_record_type(rec->type)) {
3700 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
3701 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker6430faf2019-05-08 11:57:13 +01003702 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003703#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003705 if (rec->data_len == 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003706#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003707 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
3708 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003709 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
3711 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003712 }
3713#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3714
3715 ssl->nb_zero++;
3716
3717 /*
3718 * Three or more empty messages may be a DoS attack
3719 * (excessive CPU consumption).
3720 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 if (ssl->nb_zero > 3) {
3722 MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
3723 "messages, possible DoS attack"));
Hanno Becker6e7700d2019-05-08 10:38:32 +01003724 /* Treat the records as if they were not properly authenticated,
3725 * thereby failing the connection if we see more than allowed
3726 * by the configured bad MAC threshold. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003727 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003728 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003729 } else {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003730 ssl->nb_zero = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003731 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003732
3733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003734 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003735 ; /* in_ctr read from peer, not maintained internally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003736 } else
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003737#endif
3738 {
3739 unsigned i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003740 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
3741 if (++ssl->in_ctr[i - 1] != 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003742 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003743 }
3744 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003745
3746 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 if (i == mbedtls_ssl_ep_len(ssl)) {
3748 MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
3749 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003750 }
3751 }
3752
Paul Bakker5121ce52009-01-03 21:22:43 +00003753 }
3754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003756 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3757 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003758 }
3759#endif
3760
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003761 /* Check actual (decrypted) record content length against
3762 * configured maximum. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003763 if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
3764 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
3765 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003766 }
3767
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003768 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003769}
3770
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003771/*
3772 * Read a record.
3773 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003774 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3775 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3776 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003777 */
Hanno Becker1097b342018-08-15 14:09:41 +01003778
3779/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003780MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003781static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003782MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003783static int ssl_get_next_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003785static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
Hanno Becker4162b112018-08-15 14:05:04 +01003786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003787int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
3788 unsigned update_hs_digest)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003789{
Janos Follath865b3eb2019-12-16 11:46:15 +00003790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794 if (ssl->keep_current_message == 0) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003795 do {
Simon Butcher99000142016-10-13 17:21:01 +01003796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003797 ret = ssl_consume_current_message(ssl);
3798 if (ret != 0) {
3799 return ret;
3800 }
Hanno Becker26994592018-08-15 14:14:59 +01003801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003802 if (ssl_record_is_in_progress(ssl) == 0) {
David Horstmann5846c9d2022-10-06 18:31:25 +01003803 int dtls_have_buffered = 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere74d5562018-08-15 14:26:08 +01003805
Hanno Becker40f50842018-08-15 14:48:01 +01003806 /* We only check for buffered messages if the
3807 * current datagram is fully consumed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3809 ssl_next_record_is_in_datagram(ssl) == 0) {
3810 if (ssl_load_buffered_message(ssl) == 0) {
David Horstmann5846c9d2022-10-06 18:31:25 +01003811 dtls_have_buffered = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003812 }
Hanno Becker40f50842018-08-15 14:48:01 +01003813 }
3814
Hanno Becker40f50842018-08-15 14:48:01 +01003815#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003816 if (dtls_have_buffered == 0) {
3817 ret = ssl_get_next_record(ssl);
3818 if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
Hanno Becker40f50842018-08-15 14:48:01 +01003819 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003820 }
Hanno Becker40f50842018-08-15 14:48:01 +01003821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003822 if (ret != 0) {
3823 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
3824 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003825 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003826 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003827 }
3828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003829 ret = mbedtls_ssl_handle_message_type(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003830
Hanno Becker40f50842018-08-15 14:48:01 +01003831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003832 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker40f50842018-08-15 14:48:01 +01003833 /* Buffer future message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003834 ret = ssl_buffer_message(ssl);
3835 if (ret != 0) {
3836 return ret;
3837 }
Hanno Becker40f50842018-08-15 14:48:01 +01003838
3839 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3840 }
3841#endif /* MBEDTLS_SSL_PROTO_DTLS */
3842
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 } while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3844 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003846 if (0 != ret) {
3847 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
3848 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01003849 }
3850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003851 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3852 update_hs_digest == 1) {
3853 mbedtls_ssl_update_handshake_status(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003854 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003855 } else {
3856 MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003857 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003858 }
3859
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003860 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
Simon Butcher99000142016-10-13 17:21:01 +01003861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003863}
3864
Hanno Becker40f50842018-08-15 14:48:01 +01003865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003866MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003867static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003868{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003869 if (ssl->in_left > ssl->next_record_offset) {
3870 return 1;
3871 }
Simon Butcher99000142016-10-13 17:21:01 +01003872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 return 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003874}
3875
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003876MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003877static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01003878{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003879 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003881 int ret = 0;
3882
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003883 if (hs == NULL) {
3884 return -1;
3885 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003887 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
Hanno Beckere00ae372018-08-20 09:39:42 +01003888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003889 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3890 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003891 /* Check if we have seen a ChangeCipherSpec before.
3892 * If yes, synthesize a CCS record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003893 if (!hs->buffering.seen_ccs) {
3894 MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003895 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003896 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003897 }
3898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003899 MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003900 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3901 ssl->in_msglen = 1;
3902 ssl->in_msg[0] = 1;
3903
3904 /* As long as they are equal, the exact value doesn't matter. */
3905 ssl->in_left = 0;
3906 ssl->next_record_offset = 0;
3907
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003908 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003909 goto exit;
3910 }
Hanno Becker37f95322018-08-16 13:55:32 +01003911
Hanno Beckerb8f50142018-08-28 10:01:34 +01003912#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003913 /* Debug only */
3914 {
3915 unsigned offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003916 for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
Hanno Becker37f95322018-08-16 13:55:32 +01003917 hs_buf = &hs->buffering.hs[offset];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003918 if (hs_buf->is_valid == 1) {
3919 MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
3920 hs->in_msg_seq + offset,
3921 hs_buf->is_complete ? "fully" : "partially"));
Hanno Becker37f95322018-08-16 13:55:32 +01003922 }
3923 }
3924 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003925#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003926
3927 /* Check if we have buffered and/or fully reassembled the
3928 * next handshake message. */
3929 hs_buf = &hs->buffering.hs[0];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003930 if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
Hanno Becker37f95322018-08-16 13:55:32 +01003931 /* Synthesize a record containing the buffered HS message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003932 size_t msg_len = (hs_buf->data[1] << 16) |
3933 (hs_buf->data[2] << 8) |
3934 hs_buf->data[3];
Hanno Becker37f95322018-08-16 13:55:32 +01003935
3936 /* Double-check that we haven't accidentally buffered
3937 * a message that doesn't fit into the input buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003938 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
3939 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3940 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01003941 }
3942
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
3944 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
3945 hs_buf->data, msg_len + 12);
Hanno Becker37f95322018-08-16 13:55:32 +01003946
3947 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3948 ssl->in_hslen = msg_len + 12;
3949 ssl->in_msglen = msg_len + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003950 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
Hanno Becker37f95322018-08-16 13:55:32 +01003951
3952 ret = 0;
3953 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 } else {
3955 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
3956 hs->in_msg_seq));
Hanno Becker37f95322018-08-16 13:55:32 +01003957 }
3958
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003959 ret = -1;
3960
3961exit:
3962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003963 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
3964 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003965}
3966
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003967MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003968static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
3969 size_t desired)
Hanno Beckera02b0b42018-08-21 17:20:27 +01003970{
3971 int offset;
3972 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003973 MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
3974 (unsigned) desired));
Hanno Beckera02b0b42018-08-21 17:20:27 +01003975
Hanno Becker01315ea2018-08-21 17:22:17 +01003976 /* Get rid of future records epoch first, if such exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003977 ssl_free_buffered_record(ssl);
Hanno Becker01315ea2018-08-21 17:22:17 +01003978
3979 /* Check if we have enough space available now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003980 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3981 hs->buffering.total_bytes_buffered)) {
3982 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
3983 return 0;
Hanno Becker01315ea2018-08-21 17:22:17 +01003984 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003985
Hanno Becker4f432ad2018-08-28 10:02:32 +01003986 /* We don't have enough space to buffer the next expected handshake
3987 * message. Remove buffers used for future messages to gain space,
3988 * starting with the most distant one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003989 for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3990 offset >= 0; offset--) {
3991 MBEDTLS_SSL_DEBUG_MSG(2,
3992 (
3993 "Free buffering slot %d to make space for reassembly of next handshake message",
3994 offset));
Hanno Beckera02b0b42018-08-21 17:20:27 +01003995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003996 ssl_buffering_free_slot(ssl, (uint8_t) offset);
Hanno Beckera02b0b42018-08-21 17:20:27 +01003997
3998 /* Check if we have enough space available now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003999 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4000 hs->buffering.total_bytes_buffered)) {
4001 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
4002 return 0;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004003 }
4004 }
4005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004006 return -1;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004007}
4008
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004009MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004010static int ssl_buffer_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004011{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004012 int ret = 0;
4013 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004015 if (hs == NULL) {
4016 return 0;
4017 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004019 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004021 switch (ssl->in_msgtype) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004022 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004023 MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
Hanno Beckere678eaa2018-08-21 14:57:46 +01004024
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004025 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004026 break;
4027
4028 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004029 {
4030 unsigned recv_msg_seq_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004031 unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Hanno Becker37f95322018-08-16 13:55:32 +01004032 mbedtls_ssl_hs_buffer *hs_buf;
4033 size_t msg_len = ssl->in_hslen - 12;
4034
4035 /* We should never receive an old handshake
4036 * message - double-check nonetheless. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004037 if (recv_msg_seq < ssl->handshake->in_msg_seq) {
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
4042 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004043 if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Becker37f95322018-08-16 13:55:32 +01004044 /* Silently ignore -- message too far in the future */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004045 MBEDTLS_SSL_DEBUG_MSG(2,
4046 ("Ignore future HS message with sequence number %u, "
4047 "buffering window %u - %u",
4048 recv_msg_seq, ssl->handshake->in_msg_seq,
4049 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
4050 1));
Hanno Becker37f95322018-08-16 13:55:32 +01004051
4052 goto exit;
4053 }
4054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004055 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
4056 recv_msg_seq, recv_msg_seq_offset));
Hanno Becker37f95322018-08-16 13:55:32 +01004057
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004058 hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
Hanno Becker37f95322018-08-16 13:55:32 +01004059
4060 /* Check if the buffering for this seq nr has already commenced. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004061 if (!hs_buf->is_valid) {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004062 size_t reassembly_buf_sz;
4063
Hanno Becker37f95322018-08-16 13:55:32 +01004064 hs_buf->is_fragmented =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004065 (ssl_hs_is_proper_fragment(ssl) == 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004066
4067 /* We copy the message back into the input buffer
4068 * after reassembly, so check that it's not too large.
4069 * This is an implementation-specific limitation
4070 * and not one from the standard, hence it is not
4071 * checked in ssl_check_hs_header(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
Hanno Becker37f95322018-08-16 13:55:32 +01004073 /* Ignore message */
4074 goto exit;
4075 }
4076
Hanno Beckere0b150f2018-08-21 15:51:03 +01004077 /* Check if we have enough space to buffer the message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004078 if (hs->buffering.total_bytes_buffered >
4079 MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
4080 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4081 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004082 }
4083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004084 reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
4085 hs_buf->is_fragmented);
Hanno Beckere0b150f2018-08-21 15:51:03 +01004086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004087 if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4088 hs->buffering.total_bytes_buffered)) {
4089 if (recv_msg_seq_offset > 0) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004090 /* If we can't buffer a future message because
4091 * of space limitations -- ignore. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004092 MBEDTLS_SSL_DEBUG_MSG(2,
4093 ("Buffering of future message of size %"
4094 MBEDTLS_PRINTF_SIZET
4095 " would exceed the compile-time limit %"
4096 MBEDTLS_PRINTF_SIZET
4097 " (already %" MBEDTLS_PRINTF_SIZET
4098 " bytes buffered) -- ignore\n",
4099 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4100 hs->buffering.total_bytes_buffered));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004101 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004102 } else {
4103 MBEDTLS_SSL_DEBUG_MSG(2,
4104 ("Buffering of future message of size %"
4105 MBEDTLS_PRINTF_SIZET
4106 " would exceed the compile-time limit %"
4107 MBEDTLS_PRINTF_SIZET
4108 " (already %" MBEDTLS_PRINTF_SIZET
4109 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4110 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4111 hs->buffering.total_bytes_buffered));
Hanno Beckere1801392018-08-21 16:51:05 +01004112 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004114 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
4115 MBEDTLS_SSL_DEBUG_MSG(2,
4116 ("Reassembly of next message of size %"
4117 MBEDTLS_PRINTF_SIZET
4118 " (%" MBEDTLS_PRINTF_SIZET
4119 " with bitmap) would exceed"
4120 " the compile-time limit %"
4121 MBEDTLS_PRINTF_SIZET
4122 " (already %" MBEDTLS_PRINTF_SIZET
4123 " bytes buffered) -- fail\n",
4124 msg_len,
4125 reassembly_buf_sz,
4126 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4127 hs->buffering.total_bytes_buffered));
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004128 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4129 goto exit;
4130 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004131 }
4132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004133 MBEDTLS_SSL_DEBUG_MSG(2,
4134 ("initialize reassembly, total length = %"
4135 MBEDTLS_PRINTF_SIZET,
4136 msg_len));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004138 hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
4139 if (hs_buf->data == NULL) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004140 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004141 goto exit;
4142 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004143 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004144
4145 /* Prepare final header: copy msg_type, length and message_seq,
4146 * then add standardised fragment_offset and fragment_length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004147 memcpy(hs_buf->data, ssl->in_msg, 6);
4148 memset(hs_buf->data + 6, 0, 3);
4149 memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
Hanno Becker37f95322018-08-16 13:55:32 +01004150
4151 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004152
4153 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004154 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004155 /* Make sure msg_type and length are consistent */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004156 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
4157 MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
Hanno Becker37f95322018-08-16 13:55:32 +01004158 /* Ignore */
4159 goto exit;
4160 }
4161 }
4162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004163 if (!hs_buf->is_complete) {
Hanno Becker37f95322018-08-16 13:55:32 +01004164 size_t frag_len, frag_off;
4165 unsigned char * const msg = hs_buf->data + 12;
4166
4167 /*
4168 * Check and copy current fragment
4169 */
4170
4171 /* Validation of header fields already done in
4172 * mbedtls_ssl_prepare_handshake_record(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004173 frag_off = ssl_get_hs_frag_off(ssl);
4174 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker37f95322018-08-16 13:55:32 +01004175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004176 MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4177 ", length = %" MBEDTLS_PRINTF_SIZET,
4178 frag_off, frag_len));
4179 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
Hanno Becker37f95322018-08-16 13:55:32 +01004180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004181 if (hs_buf->is_fragmented) {
Hanno Becker37f95322018-08-16 13:55:32 +01004182 unsigned char * const bitmask = msg + msg_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004183 ssl_bitmask_set(bitmask, frag_off, frag_len);
4184 hs_buf->is_complete = (ssl_bitmask_check(bitmask,
4185 msg_len) == 0);
4186 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004187 hs_buf->is_complete = 1;
4188 }
4189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004190 MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
4191 hs_buf->is_complete ? "" : "not yet "));
Hanno Becker37f95322018-08-16 13:55:32 +01004192 }
4193
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004194 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004195 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004196
4197 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004198 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004199 break;
4200 }
4201
4202exit:
4203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004204 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
4205 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004206}
4207#endif /* MBEDTLS_SSL_PROTO_DTLS */
4208
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004209MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004210static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004211{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004212 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004213 * Consume last content-layer message and potentially
4214 * update in_msglen which keeps track of the contents'
4215 * consumption state.
4216 *
4217 * (1) Handshake messages:
4218 * Remove last handshake message, move content
4219 * and adapt in_msglen.
4220 *
4221 * (2) Alert messages:
4222 * Consume whole record content, in_msglen = 0.
4223 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004224 * (3) Change cipher spec:
4225 * Consume whole record content, in_msglen = 0.
4226 *
4227 * (4) Application data:
4228 * Don't do anything - the record layer provides
4229 * the application data as a stream transport
4230 * and consumes through mbedtls_ssl_read only.
4231 *
4232 */
4233
4234 /* Case (1): Handshake messages */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004235 if (ssl->in_hslen != 0) {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004236 /* Hard assertion to be sure that no application data
4237 * is in flight, as corrupting ssl->in_msglen during
4238 * ssl->in_offt != NULL is fatal. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004239 if (ssl->in_offt != NULL) {
4240 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4241 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004242 }
4243
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004244 /*
4245 * Get next Handshake message in the current record
4246 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004247
Hanno Becker4a810fb2017-05-24 16:27:30 +01004248 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004249 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004250 * current handshake content: If DTLS handshake
4251 * fragmentation is used, that's the fragment
4252 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004253 * size here is faulty and should be changed at
4254 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004255 * (2) While it doesn't seem to cause problems, one
4256 * has to be very careful not to assume that in_hslen
4257 * is always <= in_msglen in a sensible communication.
4258 * Again, it's wrong for DTLS handshake fragmentation.
4259 * The following check is therefore mandatory, and
4260 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004261 * Additionally, ssl->in_hslen might be arbitrarily out of
4262 * bounds after handling a DTLS message with an unexpected
4263 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004264 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004265 if (ssl->in_hslen < ssl->in_msglen) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004266 ssl->in_msglen -= ssl->in_hslen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004267 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4268 ssl->in_msglen);
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004270 MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
4271 ssl->in_msg, ssl->in_msglen);
4272 } else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004273 ssl->in_msglen = 0;
4274 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004275
Hanno Becker4a810fb2017-05-24 16:27:30 +01004276 ssl->in_hslen = 0;
4277 }
4278 /* Case (4): Application data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004279 else if (ssl->in_offt != NULL) {
4280 return 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01004281 }
4282 /* Everything else (CCS & Alerts) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004283 else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004284 ssl->in_msglen = 0;
4285 }
4286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004287 return 0;
Hanno Becker1097b342018-08-15 14:09:41 +01004288}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004289
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004290MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004291static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
Hanno Beckere74d5562018-08-15 14:26:08 +01004292{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004293 if (ssl->in_msglen > 0) {
4294 return 1;
4295 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004297 return 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004298}
4299
Hanno Becker5f066e72018-08-16 14:56:31 +01004300#if defined(MBEDTLS_SSL_PROTO_DTLS)
4301
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004303{
4304 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004305 if (hs == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004306 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004307 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004309 if (hs->buffering.future_record.data != NULL) {
Hanno Becker01315ea2018-08-21 17:22:17 +01004310 hs->buffering.total_bytes_buffered -=
4311 hs->buffering.future_record.len;
4312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313 mbedtls_free(hs->buffering.future_record.data);
Hanno Becker01315ea2018-08-21 17:22:17 +01004314 hs->buffering.future_record.data = NULL;
4315 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004316}
4317
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004318MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004319static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004320{
4321 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004322 unsigned char *rec;
Hanno Becker5f066e72018-08-16 14:56:31 +01004323 size_t rec_len;
4324 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004325#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4326 size_t in_buf_len = ssl->in_buf_len;
4327#else
4328 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4329#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004330 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4331 return 0;
4332 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004334 if (hs == NULL) {
4335 return 0;
4336 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004337
Hanno Becker5f066e72018-08-16 14:56:31 +01004338 rec = hs->buffering.future_record.data;
4339 rec_len = hs->buffering.future_record.len;
4340 rec_epoch = hs->buffering.future_record.epoch;
4341
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004342 if (rec == NULL) {
4343 return 0;
4344 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004345
Hanno Becker4cb782d2018-08-20 11:19:05 +01004346 /* Only consider loading future records if the
4347 * input buffer is empty. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004348 if (ssl_next_record_is_in_datagram(ssl) == 1) {
4349 return 0;
4350 }
Hanno Becker4cb782d2018-08-20 11:19:05 +01004351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004352 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004354 if (rec_epoch != ssl->in_epoch) {
4355 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
Hanno Becker5f066e72018-08-16 14:56:31 +01004356 goto exit;
4357 }
4358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004360
4361 /* Double-check that the record is not too large */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004362 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
4363 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4364 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker5f066e72018-08-16 14:56:31 +01004365 }
4366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004367 memcpy(ssl->in_hdr, rec, rec_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004368 ssl->in_left = rec_len;
4369 ssl->next_record_offset = 0;
4370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004371 ssl_free_buffered_record(ssl);
Hanno Becker5f066e72018-08-16 14:56:31 +01004372
4373exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004374 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
4375 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004376}
4377
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004378MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004379static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
4380 mbedtls_record const *rec)
Hanno Becker5f066e72018-08-16 14:56:31 +01004381{
4382 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004383
4384 /* Don't buffer future records outside handshakes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385 if (hs == NULL) {
4386 return 0;
4387 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004388
4389 /* Only buffer handshake records (we are only interested
4390 * in Finished messages). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004391 if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
4392 return 0;
4393 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004394
4395 /* Don't buffer more than one future epoch record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 if (hs->buffering.future_record.data != NULL) {
4397 return 0;
4398 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004399
Hanno Becker01315ea2018-08-21 17:22:17 +01004400 /* Don't buffer record if there's not enough buffering space remaining. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004401 if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4402 hs->buffering.total_bytes_buffered)) {
4403 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4404 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4405 " (already %" MBEDTLS_PRINTF_SIZET
4406 " bytes buffered) -- ignore\n",
4407 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4408 hs->buffering.total_bytes_buffered));
4409 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004410 }
4411
Hanno Becker5f066e72018-08-16 14:56:31 +01004412 /* Buffer record */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004413 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
4414 ssl->in_epoch + 1U));
4415 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004416
4417 /* ssl_parse_record_header() only considers records
4418 * of the next epoch as candidates for buffering. */
4419 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004420 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004421
4422 hs->buffering.future_record.data =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004423 mbedtls_calloc(1, hs->buffering.future_record.len);
4424 if (hs->buffering.future_record.data == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004425 /* If we run out of RAM trying to buffer a
4426 * record from the next epoch, just ignore. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004427 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004428 }
4429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004430 memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004431
Hanno Becker519f15d2019-07-11 12:43:20 +01004432 hs->buffering.total_bytes_buffered += rec->buf_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004433 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004434}
4435
4436#endif /* MBEDTLS_SSL_PROTO_DTLS */
4437
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004438MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004439static int ssl_get_next_record(mbedtls_ssl_context *ssl)
Hanno Becker1097b342018-08-15 14:09:41 +01004440{
Janos Follath865b3eb2019-12-16 11:46:15 +00004441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004442 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004443
Hanno Becker5f066e72018-08-16 14:56:31 +01004444#if defined(MBEDTLS_SSL_PROTO_DTLS)
4445 /* We might have buffered a future record; if so,
4446 * and if the epoch matches now, load it.
4447 * On success, this call will set ssl->in_left to
4448 * the length of the buffered record, so that
4449 * the calls to ssl_fetch_input() below will
4450 * essentially be no-ops. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004451 ret = ssl_load_buffered_record(ssl);
4452 if (ret != 0) {
4453 return ret;
4454 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004455#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004456
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004457 /* Ensure that we have enough space available for the default form
4458 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4459 * with no space for CIDs counted in). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004460 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
4461 if (ret != 0) {
4462 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4463 return ret;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004464 }
4465
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004466 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
4467 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004469 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4470 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
4471 ret = ssl_buffer_future_record(ssl, &rec);
4472 if (ret != 0) {
4473 return ret;
4474 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004475
4476 /* Fall through to handling of unexpected records */
4477 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4478 }
4479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
Hanno Becker2fddd372019-07-10 14:37:41 +01004481#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004482 /* Reset in pointers to default state for TLS/DTLS records,
4483 * assuming no CID and no offset between record content and
4484 * record plaintext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004485 mbedtls_ssl_update_in_pointers(ssl);
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004486
Hanno Becker7ae20e02019-07-12 08:33:49 +01004487 /* Setup internal message pointers from record structure. */
4488 ssl->in_msgtype = rec.type;
4489#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4490 ssl->in_len = ssl->in_cid + rec.cid_len;
4491#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4492 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4493 ssl->in_msglen = rec.data_len;
4494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004495 ret = ssl_check_client_reconnect(ssl);
4496 MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
4497 if (ret != 0) {
4498 return ret;
4499 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004500#endif
4501
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004502 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004503 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004504
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004505 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
4506 "(header)"));
4507 } else {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004508 /* Skip invalid record and the rest of the datagram */
4509 ssl->next_record_offset = 0;
4510 ssl->in_left = 0;
4511
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004512 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
4513 "(header)"));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004514 }
4515
4516 /* Get next record */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004517 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4518 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004519#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004520 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004521 return ret;
Hanno Becker2fddd372019-07-10 14:37:41 +01004522 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004523 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004525#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004526 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckera8814792019-07-10 15:01:45 +01004527 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004528 ssl->next_record_offset = rec.buf_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004529 if (ssl->next_record_offset < ssl->in_left) {
4530 MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
Hanno Beckere65ce782017-05-22 14:47:48 +01004531 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 } else
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004533#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004534 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004535 /*
4536 * Fetch record contents from underlying transport.
4537 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004538 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
4539 if (ret != 0) {
4540 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4541 return ret;
Hanno Beckera8814792019-07-10 15:01:45 +01004542 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004543
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004544 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004545 }
4546
4547 /*
4548 * Decrypt record contents.
4549 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004550
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004551 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004553 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004554 /* Silently discard invalid records */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004555 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004556 /* Except when waiting for Finished as a bad mac here
4557 * probably means something went wrong in the handshake
4558 * (eg wrong psk used, mitm downgrade attempt, etc.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004559 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4560 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004561#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4563 mbedtls_ssl_send_alert_message(ssl,
4564 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4565 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004566 }
4567#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004568 return ret;
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004569 }
4570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004572 if (ssl->conf->badmac_limit != 0 &&
4573 ++ssl->badmac_seen >= ssl->conf->badmac_limit) {
4574 MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
4575 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004576 }
4577#endif
4578
Hanno Becker4a810fb2017-05-24 16:27:30 +01004579 /* As above, invalid records cause
4580 * dismissal of the whole datagram. */
4581
4582 ssl->next_record_offset = 0;
4583 ssl->in_left = 0;
4584
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004585 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
4586 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004587 }
4588
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004589 return ret;
4590 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004591#endif
4592 {
4593 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004595 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4596 mbedtls_ssl_send_alert_message(ssl,
4597 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4598 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004599 }
4600#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004601 return ret;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004602 }
4603 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004604
Hanno Becker44d89b22019-07-12 09:40:44 +01004605
4606 /* Reset in pointers to default state for TLS/DTLS records,
4607 * assuming no CID and no offset between record content and
4608 * record plaintext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004609 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker44d89b22019-07-12 09:40:44 +01004610#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4611 ssl->in_len = ssl->in_cid + rec.cid_len;
4612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004613 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004614
Hanno Becker8685c822019-07-12 09:37:30 +01004615 /* The record content type may change during decryption,
4616 * so re-read it. */
4617 ssl->in_msgtype = rec.type;
4618 /* Also update the input buffer, because unfortunately
4619 * the server-side ssl_parse_client_hello() reparses the
4620 * record header when receiving a ClientHello initiating
4621 * a renegotiation. */
4622 ssl->in_hdr[0] = rec.type;
4623 ssl->in_msg = rec.buf + rec.data_offset;
4624 ssl->in_msglen = rec.data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004625 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
Hanno Becker8685c822019-07-12 09:37:30 +01004626
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004627#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004628 if (ssl->transform_in != NULL &&
4629 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
4630 if ((ret = ssl_decompress_buf(ssl)) != 0) {
4631 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decompress_buf", ret);
4632 return ret;
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004633 }
4634
4635 /* Check actual (decompress) record content length against
4636 * configured maximum. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004637 if (ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN) {
4638 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
4639 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004640 }
4641 }
4642#endif /* MBEDTLS_ZLIB_SUPPORT */
4643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004644 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01004645}
4646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004647int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01004648{
Janos Follath865b3eb2019-12-16 11:46:15 +00004649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004650
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004651 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004652 * Handle particular types of records
4653 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004654 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
4655 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
4656 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01004657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004658 }
4659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004660 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4661 if (ssl->in_msglen != 1) {
4662 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
4663 ssl->in_msglen));
4664 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004665 }
4666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004667 if (ssl->in_msg[0] != 1) {
4668 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
4669 ssl->in_msg[0]));
4670 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004671 }
4672
4673#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004674 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01004675 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004676 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
4677 if (ssl->handshake == NULL) {
4678 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
4679 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004680 }
4681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004682 MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
4683 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004684 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004685#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004686 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004687
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004688 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
4689 if (ssl->in_msglen != 2) {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004690 /* Note: Standard allows for more than one 2 byte alert
4691 to be packed in a single message, but Mbed TLS doesn't
4692 currently support this. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004693 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
4694 ssl->in_msglen));
4695 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004696 }
4697
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004698 MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
4699 ssl->in_msg[0], ssl->in_msg[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00004700
4701 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004702 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
4705 MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
4706 ssl->in_msg[1]));
4707 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004708 }
4709
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004710 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4711 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
4712 MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
4713 return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004715
4716#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004717 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4718 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
4719 MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no renegotiation alert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004720 /* Will be handled when trying to parse ServerHello */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004722 }
4723#endif
4724
4725#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004726 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004727 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4728 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004729 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
4730 MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no_cert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004731 /* Will be handled in mbedtls_ssl_parse_certificate() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004733 }
4734#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4735
4736 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004737 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004738 }
4739
Hanno Beckerc76c6192017-06-06 10:03:17 +01004740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004741 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker37ae9522019-05-03 16:54:26 +01004742 /* Drop unexpected ApplicationData records,
4743 * except at the beginning of renegotiations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004744 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
Hanno Becker37ae9522019-05-03 16:54:26 +01004745 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4746#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004747 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4748 ssl->state == MBEDTLS_SSL_SERVER_HELLO)
Hanno Beckerc76c6192017-06-06 10:03:17 +01004749#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004750 ) {
4751 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
4752 return MBEDTLS_ERR_SSL_NON_FATAL;
Hanno Becker37ae9522019-05-03 16:54:26 +01004753 }
4754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 if (ssl->handshake != NULL &&
4756 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4757 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Hanno Becker37ae9522019-05-03 16:54:26 +01004758 }
4759 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004760#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004762 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004763}
4764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004765int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004766{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004767 return mbedtls_ssl_send_alert_message(ssl,
4768 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4769 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004770}
4771
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004772int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
4773 unsigned char level,
4774 unsigned char message)
Paul Bakker0a925182012-04-16 06:46:41 +00004775{
Janos Follath865b3eb2019-12-16 11:46:15 +00004776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004777
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 if (ssl == NULL || ssl->conf == NULL) {
4779 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4780 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004781
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004782 if (ssl->out_left != 0) {
4783 return mbedtls_ssl_flush_output(ssl);
4784 }
Hanno Beckerd9c66c02018-08-06 11:35:16 +01004785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004786 MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
4787 MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
Paul Bakker0a925182012-04-16 06:46:41 +00004788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004790 ssl->out_msglen = 2;
4791 ssl->out_msg[0] = level;
4792 ssl->out_msg[1] = message;
4793
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004794 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
4795 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
4796 return ret;
Paul Bakker0a925182012-04-16 06:46:41 +00004797 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004798 MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
Paul Bakker0a925182012-04-16 06:46:41 +00004799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004800 return 0;
Paul Bakker0a925182012-04-16 06:46:41 +00004801}
4802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004803int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004804{
Janos Follath865b3eb2019-12-16 11:46:15 +00004805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004806
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004807 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004809 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004810 ssl->out_msglen = 1;
4811 ssl->out_msg[0] = 1;
4812
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 ssl->state++;
4814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004815 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4816 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4817 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004818 }
4819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004820 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004821
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004822 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004823}
4824
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004825int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004826{
Janos Follath865b3eb2019-12-16 11:46:15 +00004827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004828
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004829 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
4832 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
4833 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 }
4835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4837 MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
4838 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4839 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
4840 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004841 }
4842
Hanno Beckere678eaa2018-08-21 14:57:46 +01004843 /* CCS records are only accepted if they have length 1 and content '1',
4844 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004845
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004846 /*
4847 * Switch to our negotiated transform and session parameters for inbound
4848 * data.
4849 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004850 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004851 ssl->transform_in = ssl->transform_negotiate;
4852 ssl->session_in = ssl->session_negotiate;
4853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004855 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004857 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004858#endif
4859
4860 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 if (++ssl->in_epoch == 0) {
4862 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004863 /* This is highly unlikely to happen for legitimate reasons, so
4864 treat it as an attack and don't send an alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004865 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004866 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004867 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004869 memset(ssl->in_ctr, 0, 8);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004871 mbedtls_ssl_update_in_pointers(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004874 if (mbedtls_ssl_hw_record_activate != NULL) {
4875 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_INBOUND)) != 0) {
4876 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
4877 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4878 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
4879 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004880 }
4881 }
4882#endif
4883
Paul Bakker5121ce52009-01-03 21:22:43 +00004884 ssl->state++;
4885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004888 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004889}
4890
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004891/* Once ssl->out_hdr as the address of the beginning of the
4892 * next outgoing record is set, deduce the other pointers.
4893 *
4894 * Note: For TLS, we save the implicit record sequence number
4895 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4896 * and the caller has to make sure there's space for this.
4897 */
4898
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004899static size_t ssl_transform_get_explicit_iv_len(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004900 mbedtls_ssl_transform const *transform)
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004901{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004902 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
4903 return 0;
4904 }
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004905
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004906 return transform->ivlen - transform->fixed_ivlen;
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004907}
4908
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004909void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
4910 mbedtls_ssl_transform *transform)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004911{
4912#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004913 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004914 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004915#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004916 ssl->out_cid = ssl->out_ctr + 8;
4917 ssl->out_len = ssl->out_cid;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004918 if (transform != NULL) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004919 ssl->out_len += transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004920 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004921#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004922 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004924 ssl->out_iv = ssl->out_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004925 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004926#endif
4927 {
4928 ssl->out_ctr = ssl->out_hdr - 8;
4929 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004930#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004931 ssl->out_cid = ssl->out_len;
4932#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004933 ssl->out_iv = ssl->out_hdr + 5;
4934 }
4935
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004936 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004937 /* Adjust out_msg to make space for explicit IV, if used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004938 if (transform != NULL) {
4939 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
4940 }
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004941}
4942
4943/* Once ssl->in_hdr as the address of the beginning of the
4944 * next incoming record is set, deduce the other pointers.
4945 *
4946 * Note: For TLS, we save the implicit record sequence number
4947 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4948 * and the caller has to make sure there's space for this.
4949 */
4950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004951void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004952{
Hanno Becker79594fd2019-05-08 09:38:41 +01004953 /* This function sets the pointers to match the case
4954 * of unprotected TLS/DTLS records, with both ssl->in_iv
4955 * and ssl->in_msg pointing to the beginning of the record
4956 * content.
4957 *
4958 * When decrypting a protected record, ssl->in_msg
4959 * will be shifted to point to the beginning of the
4960 * record plaintext.
4961 */
4962
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004964 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004965 /* This sets the header pointers to match records
4966 * without CID. When we receive a record containing
4967 * a CID, the fields are shifted accordingly in
4968 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004969 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004970#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004971 ssl->in_cid = ssl->in_ctr + 8;
4972 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004973#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004974 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004975#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004976 ssl->in_iv = ssl->in_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004977 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004978#endif
4979 {
4980 ssl->in_ctr = ssl->in_hdr - 8;
4981 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004982#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004983 ssl->in_cid = ssl->in_len;
4984#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004985 ssl->in_iv = ssl->in_hdr + 5;
4986 }
4987
Hanno Becker79594fd2019-05-08 09:38:41 +01004988 /* This will be adjusted at record decryption time. */
4989 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004990}
4991
Paul Bakker5121ce52009-01-03 21:22:43 +00004992/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004993 * Setup an SSL context
4994 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004995
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004996void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004997{
4998 /* Set the incoming and outgoing record pointers. */
4999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005000 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005001 ssl->out_hdr = ssl->out_buf;
5002 ssl->in_hdr = ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005003 } else
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005004#endif /* MBEDTLS_SSL_PROTO_DTLS */
5005 {
5006 ssl->out_hdr = ssl->out_buf + 8;
5007 ssl->in_hdr = ssl->in_buf + 8;
5008 }
5009
5010 /* Derive other internal pointers. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
5012 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005013}
5014
Paul Bakker5121ce52009-01-03 21:22:43 +00005015/*
5016 * SSL get accessors
5017 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005018size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005019{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005020 return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00005021}
5022
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005023int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
Hanno Becker8b170a02017-10-10 11:51:19 +01005024{
5025 /*
5026 * Case A: We're currently holding back
5027 * a message for further processing.
5028 */
5029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005030 if (ssl->keep_current_message == 1) {
5031 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
5032 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005033 }
5034
5035 /*
5036 * Case B: Further records are pending in the current datagram.
5037 */
5038
5039#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005040 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5041 ssl->in_left > ssl->next_record_offset) {
5042 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
5043 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005044 }
5045#endif /* MBEDTLS_SSL_PROTO_DTLS */
5046
5047 /*
5048 * Case C: A handshake message is being processed.
5049 */
5050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
5052 MBEDTLS_SSL_DEBUG_MSG(3,
5053 ("ssl_check_pending: more handshake messages within current record"));
5054 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005055 }
5056
5057 /*
5058 * Case D: An application data message is being processed
5059 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005060 if (ssl->in_offt != NULL) {
5061 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
5062 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005063 }
5064
5065 /*
5066 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005067 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005068 * we implement support for multiple alerts in single records.
5069 */
5070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
5072 return 0;
Hanno Becker8b170a02017-10-10 11:51:19 +01005073}
5074
Paul Bakker43ca69c2011-01-15 17:35:19 +00005075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005076int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005077{
Hanno Becker3136ede2018-08-17 15:28:19 +01005078 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005080 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker5903de42019-05-03 14:46:38 +01005083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005084 if (transform == NULL) {
5085 return (int) out_hdr_len;
5086 }
Hanno Becker78640902018-08-13 16:35:15 +01005087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005089 if (ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL) {
5090 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5091 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005092#endif
5093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005094 switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005095 case MBEDTLS_MODE_GCM:
5096 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005097 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005099 transform_expansion = transform->minlen;
5100 break;
5101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005103
5104 block_size = mbedtls_cipher_get_block_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 &transform->cipher_ctx_enc);
Hanno Becker5b559ac2018-08-03 09:40:07 +01005106
Hanno Becker3136ede2018-08-17 15:28:19 +01005107 /* Expansion due to the addition of the MAC. */
5108 transform_expansion += transform->maclen;
5109
5110 /* Expansion due to the addition of CBC padding;
5111 * Theoretically up to 256 bytes, but we never use
5112 * more than the block size of the underlying cipher. */
5113 transform_expansion += block_size;
5114
5115 /* For TLS 1.1 or higher, an explicit IV is added
5116 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005117#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005118 if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Becker3136ede2018-08-17 15:28:19 +01005119 transform_expansion += block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005120 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01005121#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005122
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005123 break;
5124
5125 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005126 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5127 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005128 }
5129
Hanno Beckera0e20d02019-05-15 14:03:01 +01005130#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 if (transform->out_cid_len != 0) {
Hanno Becker6cbad552019-05-08 15:40:11 +01005132 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005133 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005134#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005136 return (int) (out_hdr_len + transform_expansion);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005137}
5138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005140/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005141 * Check record counters and renegotiate if they're above the limit.
5142 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005143MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005144static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005145{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005146 size_t ep_len = mbedtls_ssl_ep_len(ssl);
Andres AG2196c7f2016-12-15 17:01:16 +00005147 int in_ctr_cmp;
5148 int out_ctr_cmp;
5149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005150 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005152 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5153 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005154 }
5155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005156 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
5157 ssl->conf->renego_period + ep_len, 8 - ep_len);
5158 out_ctr_cmp = memcmp(ssl->cur_out_ctr + ep_len,
5159 ssl->conf->renego_period + ep_len, 8 - ep_len);
Andres AG2196c7f2016-12-15 17:01:16 +00005160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
5162 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005163 }
5164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005165 MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
5166 return mbedtls_ssl_renegotiate(ssl);
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005168#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005169
5170/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005171 * Receive application data decrypted from the SSL layer
5172 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005173int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005174{
Janos Follath865b3eb2019-12-16 11:46:15 +00005175 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005176 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005178 if (ssl == NULL || ssl->conf == NULL) {
5179 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5180 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005182 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005185 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5186 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5187 return ret;
5188 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005190 if (ssl->handshake != NULL &&
5191 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
5192 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
5193 return ret;
5194 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005195 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005196 }
5197#endif
5198
Hanno Becker4a810fb2017-05-24 16:27:30 +01005199 /*
5200 * Check if renegotiation is necessary and/or handshake is
5201 * in process. If yes, perform/continue, and fall through
5202 * if an unexpected packet is received while the client
5203 * is waiting for the ServerHello.
5204 *
5205 * (There is no equivalent to the last condition on
5206 * the server-side as it is not treated as within
5207 * a handshake while waiting for the ClientHello
5208 * after a renegotiation request.)
5209 */
5210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005211#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005212 ret = ssl_check_ctr_renegotiate(ssl);
5213 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5214 ret != 0) {
5215 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5216 return ret;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005217 }
5218#endif
5219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005220 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5221 ret = mbedtls_ssl_handshake(ssl);
5222 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5223 ret != 0) {
5224 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5225 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005226 }
5227 }
5228
Hanno Beckere41158b2017-10-23 13:30:32 +01005229 /* Loop as long as no application data record is available */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005230 while (ssl->in_offt == NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005231 /* Start timer if not already running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005232 if (ssl->f_get_timer != NULL &&
5233 ssl->f_get_timer(ssl->p_timer) == -1) {
5234 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005235 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005237 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5238 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5239 return 0;
5240 }
Paul Bakker831a7552011-05-18 13:32:51 +00005241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005242 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5243 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005244 }
5245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005246 if (ssl->in_msglen == 0 &&
5247 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00005248 /*
5249 * OpenSSL sends empty messages to randomize the IV
5250 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005251 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5252 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5253 return 0;
5254 }
Paul Bakker831a7552011-05-18 13:32:51 +00005255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005256 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5257 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005258 }
5259 }
5260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005261 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
5262 MBEDTLS_SSL_DEBUG_MSG(1, ("received handshake message"));
Paul Bakker48916f92012-09-16 19:57:18 +00005263
Hanno Becker4a810fb2017-05-24 16:27:30 +01005264 /*
5265 * - For client-side, expect SERVER_HELLO_REQUEST.
5266 * - For server-side, expect CLIENT_HELLO.
5267 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5268 */
5269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005271 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5272 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5273 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
5274 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005275
5276 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005278 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker90333da2017-10-10 11:27:13 +01005279 continue;
5280 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005281#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005283 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005284#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005285
Hanno Becker4a810fb2017-05-24 16:27:30 +01005286#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005287 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5288 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
5289 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005290
5291 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005293 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker90333da2017-10-10 11:27:13 +01005294 continue;
5295 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005296#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005297 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker48916f92012-09-16 19:57:18 +00005298 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005299#endif /* MBEDTLS_SSL_SRV_C */
5300
Hanno Becker21df7f92017-10-17 11:03:26 +01005301#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005302 /* Determine whether renegotiation attempt should be accepted */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005303 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5304 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5305 ssl->conf->allow_legacy_renegotiation ==
5306 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005307 /*
5308 * Accept renegotiation request
5309 */
Paul Bakker48916f92012-09-16 19:57:18 +00005310
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005311 /* DTLS clients need to know renego is server-initiated */
5312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005313 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5314 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005315 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5316 }
5317#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005318 ret = mbedtls_ssl_start_renegotiation(ssl);
5319 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5320 ret != 0) {
5321 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
5322 ret);
5323 return ret;
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005324 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005325 } else
Hanno Becker21df7f92017-10-17 11:03:26 +01005326#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005327 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005328 /*
5329 * Refuse renegotiation
5330 */
5331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005332 MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
Paul Bakker48916f92012-09-16 19:57:18 +00005333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005335 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine92e44262017-05-10 17:27:49 +02005336 /* SSLv3 does not have a "no_renegotiation" warning, so
5337 we send a fatal alert and abort the connection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005338 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5339 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
5340 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
5341 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005342#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5343#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005344 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5345 if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
5346 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5347 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5348 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION))
5349 != 0) {
5350 return ret;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005351 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005352 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5354 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005355 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005356 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5357 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02005358 }
Paul Bakker48916f92012-09-16 19:57:18 +00005359 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005360
Hanno Becker90333da2017-10-10 11:27:13 +01005361 /* At this point, we don't know whether the renegotiation has been
5362 * completed or not. The cases to consider are the following:
5363 * 1) The renegotiation is complete. In this case, no new record
5364 * has been read yet.
5365 * 2) The renegotiation is incomplete because the client received
5366 * an application data record while awaiting the ServerHello.
5367 * 3) The renegotiation is incomplete because the client received
5368 * a non-handshake, non-application data message while awaiting
5369 * the ServerHello.
5370 * In each of these case, looping will be the proper action:
5371 * - For 1), the next iteration will read a new record and check
5372 * if it's application data.
5373 * - For 2), the loop condition isn't satisfied as application data
5374 * is present, hence continue is the same as break
5375 * - For 3), the loop condition is satisfied and read_record
5376 * will re-deliver the message that was held back by the client
5377 * when expecting the ServerHello.
5378 */
5379 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005380 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005381#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005382 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5383 if (ssl->conf->renego_max_records >= 0) {
5384 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
5385 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
5386 "but not honored by client"));
5387 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005388 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005389 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005394 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
5395 MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
5396 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005397 }
5398
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005399 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
5400 MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
5401 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005402 }
5403
5404 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005405
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005406 /* We're going to return something now, cancel timer,
5407 * except if handshake (renegotiation) is in progress */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005408 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
5409 mbedtls_ssl_set_timer(ssl, 0);
5410 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005411
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005412#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005413 /* If we requested renego but received AppData, resend HelloRequest.
5414 * Do it now, after setting in_offt, to avoid taking this branch
5415 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005417 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5418 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5419 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
5420 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
5421 ret);
5422 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005423 }
5424 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005425#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005426#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005427 }
5428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005429 n = (len < ssl->in_msglen)
Paul Bakker5121ce52009-01-03 21:22:43 +00005430 ? len : ssl->in_msglen;
5431
ashesmancf01d782022-02-17 11:08:27 +13005432 if (len != 0) {
Ashley Duncan272cc192022-02-11 09:57:18 +13005433 memcpy(buf, ssl->in_offt, n);
5434 ssl->in_msglen -= n;
5435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005436
gabor-mezei-arma3214132020-07-15 10:55:00 +02005437 /* Zeroising the plaintext buffer to erase unused application data
5438 from the memory. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005439 mbedtls_platform_zeroize(ssl->in_offt, n);
gabor-mezei-arma3214132020-07-15 10:55:00 +02005440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005441 if (ssl->in_msglen == 0) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005442 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005443 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005444 ssl->keep_current_message = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005445 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00005446 /* more data available */
5447 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005448 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 return (int) n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005453}
5454
5455/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005456 * Send application data to be encrypted by the SSL layer, taking care of max
5457 * fragment length and buffer size.
5458 *
5459 * According to RFC 5246 Section 6.2.1:
5460 *
5461 * Zero-length fragments of Application data MAY be sent as they are
5462 * potentially useful as a traffic analysis countermeasure.
5463 *
5464 * Therefore, it is possible that the input message length is 0 and the
5465 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005466 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005468static int ssl_write_real(mbedtls_ssl_context *ssl,
5469 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005470{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005471 int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005472 const size_t max_len = (size_t) ret;
5473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005474 if (ret < 0) {
5475 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
5476 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005477 }
5478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479 if (len > max_len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005481 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5482 MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
5483 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5484 " > %" MBEDTLS_PRINTF_SIZET,
5485 len, max_len));
5486 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5487 } else
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005488#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005489 len = max_len;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005490 }
Paul Bakker887bd502011-06-08 13:10:54 +00005491
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 if (ssl->out_left != 0) {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005493 /*
5494 * The user has previously tried to send the data and
5495 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5496 * written. In this case, we expect the high-level write function
5497 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5498 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005499 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5500 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
5501 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503 } else {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005504 /*
5505 * The user is trying to send a message the first time, so we need to
5506 * copy the data into the internal buffers and setup the data structure
5507 * to keep track of partial writes
5508 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005509 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005510 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Dave Rodgman12155572023-02-24 15:41:34 +00005511 if (len > 0) {
5512 memcpy(ssl->out_msg, buf, len);
5513 }
Paul Bakker887bd502011-06-08 13:10:54 +00005514
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005515 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
5516 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
5517 return ret;
Paul Bakker887bd502011-06-08 13:10:54 +00005518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 }
5520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005521 return (int) len;
Paul Bakker5121ce52009-01-03 21:22:43 +00005522}
5523
5524/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005525 * Write application data, doing 1/n-1 splitting if necessary.
5526 *
5527 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005528 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005529 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005531#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005532MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005533static int ssl_write_split(mbedtls_ssl_context *ssl,
5534 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005535{
Janos Follath865b3eb2019-12-16 11:46:15 +00005536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005538 if (ssl->conf->cbc_record_splitting ==
5539 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005540 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005542 mbedtls_cipher_get_cipher_mode(&ssl->transform_out->cipher_ctx_enc)
5543 != MBEDTLS_MODE_CBC) {
5544 return ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005545 }
5546
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005547 if (ssl->split_done == 0) {
5548 if ((ret = ssl_write_real(ssl, buf, 1)) <= 0) {
5549 return ret;
5550 }
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005551 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005552 }
5553
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005554 if ((ret = ssl_write_real(ssl, buf + 1, len - 1)) <= 0) {
5555 return ret;
5556 }
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005557 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005558
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005559 return ret + 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005561#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005562
5563/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005564 * Write application data (public-facing wrapper)
5565 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005566int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005567{
Janos Follath865b3eb2019-12-16 11:46:15 +00005568 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005570 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005572 if (ssl == NULL || ssl->conf == NULL) {
5573 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5574 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005575
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005576#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005577 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
5578 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5579 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005580 }
5581#endif
5582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005583 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5584 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5585 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5586 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005587 }
5588 }
5589
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005590#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005591 ret = ssl_write_split(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005592#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005593 ret = ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005594#endif
5595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005596 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005597
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005598 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005599}
5600
5601/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005602 * Notify the peer that the connection is being closed
5603 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005604int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005605{
Janos Follath865b3eb2019-12-16 11:46:15 +00005606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005607
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005608 if (ssl == NULL || ssl->conf == NULL) {
5609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5610 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005611
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005612 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005613
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005614 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
5615 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5616 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5617 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
5618 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
5619 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005620 }
5621 }
5622
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005623 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005624
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005625 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005626}
5627
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005628void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Paul Bakker48916f92012-09-16 19:57:18 +00005629{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005630 if (transform == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005631 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005632 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005635 deflateEnd(&transform->ctx_deflate);
5636 inflateEnd(&transform->ctx_inflate);
Paul Bakker48916f92012-09-16 19:57:18 +00005637#endif
5638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005639 mbedtls_cipher_free(&transform->cipher_ctx_enc);
5640 mbedtls_cipher_free(&transform->cipher_ctx_dec);
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005641
Hanno Beckerd56ed242018-01-03 15:32:51 +00005642#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005643 mbedtls_md_free(&transform->md_ctx_enc);
5644 mbedtls_md_free(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00005645#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005646
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005647 mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
Paul Bakker48916f92012-09-16 19:57:18 +00005648}
5649
Hanno Becker0271f962018-08-16 13:23:47 +01005650#if defined(MBEDTLS_SSL_PROTO_DTLS)
5651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005652void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
Hanno Becker0271f962018-08-16 13:23:47 +01005653{
5654 unsigned offset;
5655 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005657 if (hs == NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01005658 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005659 }
Hanno Becker0271f962018-08-16 13:23:47 +01005660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005661 ssl_free_buffered_record(ssl);
Hanno Becker283f5ef2018-08-24 09:34:47 +01005662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005663 for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
5664 ssl_buffering_free_slot(ssl, offset);
5665 }
Hanno Beckere605b192018-08-21 15:59:07 +01005666}
5667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005668static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
5669 uint8_t slot)
Hanno Beckere605b192018-08-21 15:59:07 +01005670{
5671 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5672 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005674 if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Beckerb309b922018-08-23 13:18:05 +01005675 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005676 }
Hanno Beckerb309b922018-08-23 13:18:05 +01005677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005678 if (hs_buf->is_valid == 1) {
Hanno Beckere605b192018-08-21 15:59:07 +01005679 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005680 mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len);
5681 mbedtls_free(hs_buf->data);
5682 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Hanno Becker0271f962018-08-16 13:23:47 +01005683 }
5684}
5685
5686#endif /* MBEDTLS_SSL_PROTO_DTLS */
5687
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005688/*
5689 * Convert version numbers to/from wire format
5690 * and, for DTLS, to/from TLS equivalent.
5691 *
5692 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005693 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005694 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5695 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5696 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005697void mbedtls_ssl_write_version(int major, int minor, int transport,
5698 unsigned char ver[2])
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005699{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005701 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5702 if (minor == MBEDTLS_SSL_MINOR_VERSION_2) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005703 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5704
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005705 }
5706 ver[0] = (unsigned char) (255 - (major - 2));
5707 ver[1] = (unsigned char) (255 - (minor - 1));
5708 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005709#else
5710 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005711#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005712 {
5713 ver[0] = (unsigned char) major;
5714 ver[1] = (unsigned char) minor;
5715 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005716}
5717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005718void mbedtls_ssl_read_version(int *major, int *minor, int transport,
5719 const unsigned char ver[2])
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005720{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005722 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005723 *major = 255 - ver[0] + 2;
5724 *minor = 255 - ver[1] + 1;
5725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005726 if (*minor == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005727 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005728 }
5729 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005730#else
5731 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005732#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005733 {
5734 *major = ver[0];
5735 *minor = ver[1];
5736 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005737}
5738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739#endif /* MBEDTLS_SSL_TLS_C */