blob: 4e9cc7ff355568ea296e495e0a6a70a553812142 [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) {
Gilles Peskine326ba3c2023-09-18 14:08:11 +02001152 if (rec->data_len < transform->maclen) {
1153 MBEDTLS_SSL_DEBUG_MSG(1,
1154 ("Record too short for MAC:"
1155 " %" MBEDTLS_PRINTF_SIZET " < %" MBEDTLS_PRINTF_SIZET,
1156 rec->data_len, transform->maclen));
1157 return MBEDTLS_ERR_SSL_INVALID_MAC;
1158 }
1159
Paul Bakker68884e32013-01-07 18:20:04 +01001160 padlen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001161 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1162 transform->iv_dec,
1163 transform->ivlen,
1164 data, rec->data_len,
1165 data, &olen)) != 0) {
1166 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1167 return ret;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001168 }
1169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001170 if (rec->data_len != olen) {
1171 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1172 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001173 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001174 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001176#if defined(MBEDTLS_GCM_C) || \
1177 defined(MBEDTLS_CCM_C) || \
1178 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001179 if (mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001180 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001181 mode == MBEDTLS_MODE_CHACHAPOLY) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001182 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001183 unsigned char *dynamic_iv;
1184 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001185
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001186 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001187 * Extract dynamic part of nonce for AEAD decryption.
1188 *
1189 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1190 * part of the IV is prepended to the ciphertext and
1191 * can be chosen freely - in particular, it need not
1192 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001194 dynamic_iv_len = sizeof(rec->ctr);
1195 if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) {
1196 if (rec->data_len < dynamic_iv_len) {
1197 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1198 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
1199 rec->data_len,
1200 dynamic_iv_len));
1201 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001202 }
1203 dynamic_iv = data;
1204
1205 data += dynamic_iv_len;
1206 rec->data_offset += dynamic_iv_len;
1207 rec->data_len -= dynamic_iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001208 } else {
Hanno Becker17263802020-05-28 07:05:48 +01001209 dynamic_iv = rec->ctr;
1210 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001211
1212 /* Check that there's space for the authentication tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 if (rec->data_len < transform->taglen) {
1214 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1215 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
1216 rec->data_len,
1217 transform->taglen));
1218 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001219 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001220 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001221
Hanno Beckerdf8be222020-05-21 15:30:57 +01001222 /*
1223 * Prepare nonce from dynamic and static parts.
1224 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001225 ssl_build_record_nonce(iv, sizeof(iv),
1226 transform->iv_dec,
1227 transform->fixed_ivlen,
1228 dynamic_iv,
1229 dynamic_iv_len);
Paul Bakker68884e32013-01-07 18:20:04 +01001230
Hanno Beckerdf8be222020-05-21 15:30:57 +01001231 /*
1232 * Build additional data for AEAD encryption.
1233 * This depends on the TLS version.
1234 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001235 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1236 transform->minor_ver);
1237 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
1238 add_data, add_data_len);
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001239
Hanno Beckerd96a6522019-07-10 13:55:25 +01001240 /* Because of the check above, we know that there are
Shaun Case0e7791f2021-12-20 21:14:10 -08001241 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001242 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001243 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001244 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001246 MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen);
1247 MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len,
1248 transform->taglen);
Paul Bakker68884e32013-01-07 18:20:04 +01001249
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001250 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001251 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001252 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec,
1254 iv, transform->ivlen,
1255 add_data, add_data_len,
1256 data, rec->data_len + transform->taglen, /* src */
1257 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
1258 transform->taglen)) != 0) {
1259 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt", ret);
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001261 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
1262 return MBEDTLS_ERR_SSL_INVALID_MAC;
1263 }
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001266 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001267 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001268
Hanno Beckerd96a6522019-07-10 13:55:25 +01001269 /* Double-check that AEAD decryption doesn't change content length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001270 if (olen != rec->data_len) {
1271 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1272 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001273 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001274 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001276#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001277 if (mode == MBEDTLS_MODE_CBC) {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001278 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001279
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 /*
Paul Bakker45829992013-01-03 14:52:21 +01001281 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001284 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001285 /* The ciphertext is prefixed with the CBC IV. */
1286 minlen += transform->ivlen;
1287 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001288#endif
Paul Bakker45829992013-01-03 14:52:21 +01001289
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001290 /* Size considerations:
1291 *
1292 * - The CBC cipher text must not be empty and hence
1293 * at least of size transform->ivlen.
1294 *
1295 * Together with the potential IV-prefix, this explains
1296 * the first of the two checks below.
1297 *
1298 * - The record must contain a MAC, either in plain or
1299 * encrypted, depending on whether Encrypt-then-MAC
1300 * is used or not.
1301 * - If it is, the message contains the IV-prefix,
1302 * the CBC ciphertext, and the MAC.
1303 * - If it is not, the padded plaintext, and hence
1304 * the CBC ciphertext, has at least length maclen + 1
1305 * because there is at least the padding length byte.
1306 *
1307 * As the CBC ciphertext is not empty, both cases give the
1308 * lower bound minlen + maclen + 1 on the record size, which
1309 * we test for in the second check below.
1310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001311 if (rec->data_len < minlen + transform->ivlen ||
1312 rec->data_len < minlen + transform->maclen + 1) {
1313 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1314 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1315 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
1316 "+ 1 ) ( + expl IV )",
1317 rec->data_len,
1318 transform->ivlen,
1319 transform->maclen));
1320 return MBEDTLS_ERR_SSL_INVALID_MAC;
Paul Bakker45829992013-01-03 14:52:21 +01001321 }
1322
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001323 /*
1324 * Authenticate before decrypt if enabled
1325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001327 if (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Hanno Becker992b6872017-11-09 18:57:39 +00001328 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001330 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001331
Hanno Beckerd96a6522019-07-10 13:55:25 +01001332 /* Update data_len in tandem with add_data.
1333 *
1334 * The subtraction is safe because of the previous check
1335 * data_len >= minlen + maclen + 1.
1336 *
1337 * Afterwards, we know that data + data_len is followed by at
1338 * least maclen Bytes, which justifies the call to
Gabor Mezei18a44942021-10-20 11:59:27 +02001339 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001340 *
1341 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001342 rec->data_len -= transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001343 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1344 transform->minor_ver);
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001345
Hanno Beckerd96a6522019-07-10 13:55:25 +01001346 /* Calculate expected MAC. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001347 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1348 add_data_len);
1349 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
1350 add_data_len);
1351 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001352 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001353 }
1354 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
1355 data, rec->data_len);
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 }
1359 ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
1360 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001361 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 }
1363 ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
1364 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001365 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001366 }
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001368 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
1369 transform->maclen);
1370 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
1371 transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372
Hanno Beckerd96a6522019-07-10 13:55:25 +01001373 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001374 if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
1375 transform->maclen) != 0) {
1376 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Gilles Peskined8e2e832021-12-10 21:33:21 +01001377 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1378 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001379 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001380 auth_done++;
Gilles Peskined8e2e832021-12-10 21:33:21 +01001381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001382hmac_failed_etm_enabled:
1383 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1384 if (ret != 0) {
1385 if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
1386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
1387 }
1388 return ret;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001389 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001390 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001392
1393 /*
1394 * Check length sanity
1395 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001396
1397 /* We know from above that data_len > minlen >= 0,
1398 * so the following check in particular implies that
1399 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001400 if (rec->data_len % transform->ivlen != 0) {
1401 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1402 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
1403 rec->data_len, transform->ivlen));
1404 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001405 }
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001408 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001409 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001410 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001412 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001413 memcpy(transform->iv_dec, data, transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001414
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001415 data += transform->ivlen;
1416 rec->data_offset += transform->ivlen;
1417 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001418 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001420
Hanno Beckerd96a6522019-07-10 13:55:25 +01001421 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001423 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1424 transform->iv_dec, transform->ivlen,
1425 data, rec->data_len, data, &olen)) != 0) {
1426 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1427 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001428 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001429
Hanno Beckerd96a6522019-07-10 13:55:25 +01001430 /* Double-check that length hasn't changed during decryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001431 if (rec->data_len != olen) {
1432 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1433 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001434 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001437 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
Paul Bakkercca5b812013-08-31 17:40:26 +02001438 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001439 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1440 * records is equivalent to CBC decryption of the concatenation
1441 * of the records; in other words, IVs are maintained across
1442 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001443 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001444 memcpy(transform->iv_dec, transform->cipher_ctx_dec.iv,
1445 transform->ivlen);
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001447#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001448
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001449 /* Safe since data_len >= minlen + maclen + 1, so after having
1450 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001451 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1452 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001453 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001455 if (auth_done == 1) {
Gabor Mezei18a44942021-10-20 11:59:27 +02001456 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001457 rec->data_len,
1458 padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001459 correct &= mask;
1460 padlen &= mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001461 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001463 if (rec->data_len < transform->maclen + padlen + 1) {
1464 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1465 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1466 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
1467 rec->data_len,
1468 transform->maclen,
1469 padlen + 1));
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001470 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001471#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001472
Gabor Mezei18a44942021-10-20 11:59:27 +02001473 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001474 rec->data_len,
1475 transform->maclen + padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001476 correct &= mask;
1477 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001478 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001479
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001480 padlen++;
1481
1482 /* Regardless of the validity of the padding,
1483 * we have data_len >= padlen here. */
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001486 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001487 /* This is the SSL 3.0 path, we don't have to worry about Lucky
1488 * 13, because there's a strictly worse padding attack built in
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001489 * the protocol (known as part of POODLE), so we don't care if the
1490 * code is not constant-time, in particular branches are OK. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 if (padlen > transform->ivlen) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001493 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding length: is %" MBEDTLS_PRINTF_SIZET ", "
1494 "should be no more than %"
1495 MBEDTLS_PRINTF_SIZET,
1496 padlen, transform->ivlen));
Paul Bakkerd66f0702013-01-31 16:57:45 +01001497#endif
Paul Bakker45829992013-01-03 14:52:21 +01001498 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1502#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001503 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1504 if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001505 /* The padding check involves a series of up to 256
1506 * consecutive memory reads at the end of the record
1507 * plaintext buffer. In order to hide the length and
1508 * validity of the padding, always perform exactly
1509 * `min(256,plaintext_len)` reads (but take into account
1510 * only the last `padlen` bytes for the padding check). */
1511 size_t pad_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001512 volatile unsigned char * const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001513
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001514 /* Index of first padding byte; it has been ensured above
1515 * that the subtraction is safe. */
1516 size_t const padding_idx = rec->data_len - padlen;
1517 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1518 size_t const start_idx = rec->data_len - num_checks;
1519 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001521 for (idx = start_idx; idx < rec->data_len; idx++) {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001522 /* pad_count += (idx >= padding_idx) &&
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001523 * (check[idx] == padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001524 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx);
1526 const size_t equal = mbedtls_ct_size_bool_eq(check[idx],
1527 padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001528 pad_count += mask & equal;
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001529 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 correct &= mbedtls_ct_size_bool_eq(pad_count, padlen);
Paul Bakkere47b34b2013-02-27 14:48:00 +01001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001533 if (padlen > 0 && correct == 0) {
1534 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
1535 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001536#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001537 padlen &= mbedtls_ct_size_mask(correct);
1538 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1540 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001541 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1543 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02001544 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001545
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001546 /* If the padding was found to be invalid, padlen == 0
1547 * and the subtraction is safe. If the padding was found valid,
1548 * padlen hasn't been changed and the previous assertion
1549 * data_len >= padlen still holds. */
1550 rec->data_len -= padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001551 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001552#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001553 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001554 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1555 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001556 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001557
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001558#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001559 MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
1560 data, rec->data_len);
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001561#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
1563 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001564 * Authenticate if not done yet.
1565 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 */
Hanno Becker52344c22018-01-03 15:24:20 +00001567#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001568 if (auth_done == 0) {
Paul Elliottb8300282022-05-19 18:31:35 +01001569 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
1570 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01001571
Gilles Peskine326ba3c2023-09-18 14:08:11 +02001572 /* For CBC+MAC, If the initial value of padlen was such that
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001573 * data_len < maclen + padlen + 1, then padlen
1574 * got reset to 1, and the initial check
1575 * data_len >= minlen + maclen + 1
1576 * guarantees that at this point we still
1577 * have at least data_len >= maclen.
1578 *
1579 * If the initial value of padlen was such that
1580 * data_len >= maclen + padlen + 1, then we have
1581 * subtracted either padlen + 1 (if the padding was correct)
1582 * or 0 (if the padding was incorrect) since then,
1583 * hence data_len >= maclen in any case.
Gilles Peskine326ba3c2023-09-18 14:08:11 +02001584 *
1585 * For stream ciphers, we checked above that
1586 * data_len >= maclen.
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001587 */
1588 rec->data_len -= transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001589 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1590 transform->minor_ver);
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001593 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1594 ret = ssl_mac(&transform->md_ctx_dec,
1595 transform->mac_dec,
1596 data, rec->data_len,
1597 rec->ctr, rec->type,
1598 mac_expect);
1599 if (ret != 0) {
1600 MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001601 goto hmac_failed_etm_disabled;
1602 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001603 memcpy(mac_peer, data + rec->data_len, transform->maclen);
1604 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1606#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1607 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001608 if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 /*
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001610 * The next two sizes are the minimum and maximum values of
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001611 * data_len over all padlen values.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001612 *
1613 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001614 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001615 *
1616 * Note that max_len + maclen is never more than the buffer
1617 * length, as we previously did in_msglen -= maclen too.
1618 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001619 const size_t max_len = rec->data_len + padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001620 const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001622 ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
1623 add_data, add_data_len,
1624 data, rec->data_len, min_len, max_len,
1625 mac_expect);
1626 if (ret != 0) {
1627 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
Gilles Peskined8e2e832021-12-10 21:33:21 +01001628 goto hmac_failed_etm_disabled;
Gilles Peskine20b44082018-05-29 14:06:49 +02001629 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001630
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001631 mbedtls_ct_memcpy_offset(mac_peer, data,
1632 rec->data_len,
1633 min_len, max_len,
1634 transform->maclen);
1635 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1637 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1640 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001643#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001644 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen);
1645 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen);
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001646#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001648 if (mbedtls_ct_memcmp(mac_peer, mac_expect,
1649 transform->maclen) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001651 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Paul Bakkere47b34b2013-02-27 14:48:00 +01001652#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001653 correct = 0;
1654 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001655 auth_done++;
Gilles Peskined8e2e832021-12-10 21:33:21 +01001656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001657hmac_failed_etm_disabled:
1658 mbedtls_platform_zeroize(mac_peer, transform->maclen);
1659 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1660 if (ret != 0) {
1661 return ret;
1662 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001664
1665 /*
1666 * Finally check the correct flag
1667 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 if (correct == 0) {
1669 return MBEDTLS_ERR_SSL_INVALID_MAC;
1670 }
Hanno Becker52344c22018-01-03 15:24:20 +00001671#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001672
1673 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001674 if (auth_done != 1) {
1675 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1676 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001677 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Hanno Beckerccc13d02020-05-04 12:30:04 +01001679#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Hanno Beckerccc13d02020-05-04 12:30:04 +01001681 /* Remove inner padding and infer true content type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001682 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1683 &rec->type);
Hanno Beckerccc13d02020-05-04 12:30:04 +01001684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001685 if (ret != 0) {
1686 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1687 }
Hanno Beckerccc13d02020-05-04 12:30:04 +01001688 }
1689#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1690
Hanno Beckera0e20d02019-05-15 14:03:01 +01001691#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001692 if (rec->cid_len != 0) {
1693 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1694 &rec->type);
1695 if (ret != 0) {
1696 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1697 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001698 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001699#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001700
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001701 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001704}
1705
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001706#undef MAC_NONE
1707#undef MAC_PLAINTEXT
1708#undef MAC_CIPHERTEXT
1709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001711/*
1712 * Compression/decompression functions
1713 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001714MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001715static int ssl_compress_buf(mbedtls_ssl_context *ssl)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001716{
Janos Follath865b3eb2019-12-16 11:46:15 +00001717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001718 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001719 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001720 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001721 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001722#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1723 size_t out_buf_len = ssl->out_buf_len;
1724#else
1725 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1726#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001727
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001728 MBEDTLS_SSL_DEBUG_MSG(2, ("=> compress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001729
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001730 if (len_pre == 0) {
1731 return 0;
1732 }
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001733
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001734 memcpy(msg_pre, ssl->out_msg, len_pre);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001735
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001736 MBEDTLS_SSL_DEBUG_MSG(3, ("before compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1737 ssl->out_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001738
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001739 MBEDTLS_SSL_DEBUG_BUF(4, "before compression: output payload",
1740 ssl->out_msg, ssl->out_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741
Paul Bakker48916f92012-09-16 19:57:18 +00001742 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1743 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1744 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001745 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001747 ret = deflate(&ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH);
1748 if (ret != Z_OK) {
1749 MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform compression (%d)", ret));
1750 return MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751 }
1752
Darryl Greenb33cc762019-11-28 14:29:44 +00001753 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001754 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001755
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001756 MBEDTLS_SSL_DEBUG_MSG(3, ("after compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1757 ssl->out_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001759 MBEDTLS_SSL_DEBUG_BUF(4, "after compression: output payload",
1760 ssl->out_msg, ssl->out_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001762 MBEDTLS_SSL_DEBUG_MSG(2, ("<= compress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001764 return 0;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001765}
1766
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001767MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001768static int ssl_decompress_buf(mbedtls_ssl_context *ssl)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001769{
Janos Follath865b3eb2019-12-16 11:46:15 +00001770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001772 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001774 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001775#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1776 size_t in_buf_len = ssl->in_buf_len;
1777#else
1778 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1779#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001781 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decompress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001783 if (len_pre == 0) {
1784 return 0;
1785 }
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001786
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001787 memcpy(msg_pre, ssl->in_msg, len_pre);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001789 MBEDTLS_SSL_DEBUG_MSG(3, ("before decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1790 ssl->in_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001792 MBEDTLS_SSL_DEBUG_BUF(4, "before decompression: input payload",
1793 ssl->in_msg, ssl->in_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1796 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1797 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001798 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001800 ret = inflate(&ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH);
1801 if (ret != Z_OK) {
1802 MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform decompression (%d)", ret));
1803 return MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001804 }
1805
Darryl Greenb33cc762019-11-28 14:29:44 +00001806 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001807 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001808
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001809 MBEDTLS_SSL_DEBUG_MSG(3, ("after decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1810 ssl->in_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001812 MBEDTLS_SSL_DEBUG_BUF(4, "after decompression: input payload",
1813 ssl->in_msg, ssl->in_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001814
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001815 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decompress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001817 return 0;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820
Paul Bakker5121ce52009-01-03 21:22:43 +00001821/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001822 * Fill the input message buffer by appending data to it.
1823 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001824 *
1825 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1826 * available (from this read and/or a previous one). Otherwise, an error code
1827 * is returned (possibly EOF or WANT_READ).
1828 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001829 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1830 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1831 * since we always read a whole datagram at once.
1832 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001833 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001834 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
Paul Bakker5121ce52009-01-03 21:22:43 +00001837{
Janos Follath865b3eb2019-12-16 11:46:15 +00001838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001839 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001840#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1841 size_t in_buf_len = ssl->in_buf_len;
1842#else
1843 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001846 MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001848 if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) {
1849 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
1850 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001851 }
1852
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001853 if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
1854 MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits"));
1855 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001856 }
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001859 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001860 uint32_t timeout;
1861
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001862 /*
1863 * The point is, we need to always read a full datagram at once, so we
1864 * sometimes read more then requested, and handle the additional data.
1865 * It could be the rest of the current record (while fetching the
1866 * header) and/or some other records in the same datagram.
1867 */
1868
1869 /*
1870 * Move to the next record in the already read datagram if applicable
1871 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001872 if (ssl->next_record_offset != 0) {
1873 if (ssl->in_left < ssl->next_record_offset) {
1874 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1875 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001876 }
1877
1878 ssl->in_left -= ssl->next_record_offset;
1879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 if (ssl->in_left != 0) {
1881 MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %"
1882 MBEDTLS_PRINTF_SIZET,
1883 ssl->next_record_offset));
1884 memmove(ssl->in_hdr,
1885 ssl->in_hdr + ssl->next_record_offset,
1886 ssl->in_left);
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001887 }
1888
1889 ssl->next_record_offset = 0;
1890 }
1891
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001892 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1893 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1894 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001895
1896 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001897 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001898 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001899 if (nb_want <= ssl->in_left) {
1900 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
1901 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001902 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001903
1904 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001905 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001906 * are not at the beginning of a new record, the caller did something
1907 * wrong.
1908 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001909 if (ssl->in_left != 0) {
1910 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1911 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001912 }
1913
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001914 /*
1915 * Don't even try to read if time's out already.
1916 * This avoids by-passing the timer when repeatedly receiving messages
1917 * that will end up being dropped.
1918 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 if (mbedtls_ssl_check_timer(ssl) != 0) {
1920 MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001921 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001922 } else {
1923 len = in_buf_len - (ssl->in_hdr - ssl->in_buf);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001924
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001925 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001926 timeout = ssl->handshake->retransmit_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001927 } else {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001928 timeout = ssl->conf->read_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001929 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001930
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001931 MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001932
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001933 if (ssl->f_recv_timeout != NULL) {
1934 ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len,
1935 timeout);
1936 } else {
1937 ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len);
1938 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001939
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001940 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001942 if (ret == 0) {
1943 return MBEDTLS_ERR_SSL_CONN_EOF;
1944 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001945 }
1946
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001947 if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
1948 MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
1949 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001951 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
1952 if (ssl_double_retransmit_timeout(ssl) != 0) {
1953 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout"));
1954 return MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001955 }
1956
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001957 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
1958 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
1959 return ret;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001960 }
1961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001962 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001963 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001965 else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
1966 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
1967 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
1968 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
1969 ret);
1970 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001971 }
1972
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001973 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001974 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001975#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001976 }
1977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 if (ret < 0) {
1979 return ret;
1980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001982 ssl->in_left = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 } else
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001984#endif
1985 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001986 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1987 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1988 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001989
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001990 while (ssl->in_left < nb_want) {
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001991 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001992
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001993 if (mbedtls_ssl_check_timer(ssl) != 0) {
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001994 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001995 } else {
1996 if (ssl->f_recv_timeout != NULL) {
1997 ret = ssl->f_recv_timeout(ssl->p_bio,
1998 ssl->in_hdr + ssl->in_left, len,
1999 ssl->conf->read_timeout);
2000 } else {
2001 ret = ssl->f_recv(ssl->p_bio,
2002 ssl->in_hdr + ssl->in_left, len);
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002003 }
2004 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002005
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002006 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
2007 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
2008 ssl->in_left, nb_want));
2009 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002010
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002011 if (ret == 0) {
2012 return MBEDTLS_ERR_SSL_CONN_EOF;
2013 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002014
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002015 if (ret < 0) {
2016 return ret;
2017 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 if ((size_t) ret > len || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) {
2020 MBEDTLS_SSL_DEBUG_MSG(1,
2021 ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2022 " were requested",
2023 ret, len));
2024 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16035bd15cb2018-02-28 04:30:59 -08002025 }
2026
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002027 ssl->in_left += ret;
2028 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 }
2030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002031 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002033 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002034}
2035
2036/*
2037 * Flush any data not yet written
2038 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002039int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002040{
Janos Follath865b3eb2019-12-16 11:46:15 +00002041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002042 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002044 MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002046 if (ssl->f_send == NULL) {
2047 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
2048 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002049 }
2050
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002051 /* Avoid incrementing counter if data is flushed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002052 if (ssl->out_left == 0) {
2053 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
2054 return 0;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002055 }
2056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002057 while (ssl->out_left > 0) {
2058 MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET
2059 ", out_left: %" MBEDTLS_PRINTF_SIZET,
2060 mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left));
Paul Bakker5121ce52009-01-03 21:22:43 +00002061
Hanno Becker2b1e3542018-08-06 11:19:13 +01002062 buf = ssl->out_hdr - ssl->out_left;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002063 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
Paul Bakker186751d2012-05-08 13:16:14 +00002064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002065 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002067 if (ret <= 0) {
2068 return ret;
2069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 if ((size_t) ret > ssl->out_left || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) {
2072 MBEDTLS_SSL_DEBUG_MSG(1,
2073 ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2074 " bytes were sent",
2075 ret, ssl->out_left));
2076 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16034bbaeb42018-02-22 04:29:04 -08002077 }
2078
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 ssl->out_left -= ret;
2080 }
2081
Hanno Becker2b1e3542018-08-06 11:19:13 +01002082#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002083 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002084 ssl->out_hdr = ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002085 } else
Hanno Becker2b1e3542018-08-06 11:19:13 +01002086#endif
2087 {
2088 ssl->out_hdr = ssl->out_buf + 8;
2089 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002090 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002092 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002094 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002095}
2096
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002097/*
2098 * Functions to handle the DTLS retransmission state machine
2099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002101/*
2102 * Append current handshake message to current outgoing flight
2103 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002105static int ssl_flight_append(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 mbedtls_ssl_flight_item *msg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002108 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append"));
2109 MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight",
2110 ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002111
2112 /* Allocate space for current message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002113 if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) {
2114 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2115 sizeof(mbedtls_ssl_flight_item)));
2116 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002117 }
2118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002119 if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) {
2120 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2121 ssl->out_msglen));
2122 mbedtls_free(msg);
2123 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002124 }
2125
2126 /* Copy current handshake message with headers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002128 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002129 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002130 msg->next = NULL;
2131
2132 /* Append to the current flight */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002133 if (ssl->handshake->flight == NULL) {
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002134 ssl->handshake->flight = msg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002135 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002137 while (cur->next != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002138 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002139 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002140 cur->next = msg;
2141 }
2142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
2144 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002145}
2146
2147/*
2148 * Free the current flight of handshake messages
2149 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002150void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002151{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 mbedtls_ssl_flight_item *cur = flight;
2153 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002155 while (cur != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002156 next = cur->next;
2157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002158 mbedtls_free(cur->p);
2159 mbedtls_free(cur);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002160
2161 cur = next;
2162 }
2163}
2164
2165/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002166 * Swap transform_out and out_ctr with the alternative ones
2167 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002168MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002169static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002172 unsigned char tmp_out_ctr[8];
2173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002174 if (ssl->transform_out == ssl->handshake->alt_transform_out) {
2175 MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
2176 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002177 }
2178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002179 MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002180
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002181 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002182 tmp_transform = ssl->transform_out;
2183 ssl->transform_out = ssl->handshake->alt_transform_out;
2184 ssl->handshake->alt_transform_out = tmp_transform;
2185
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002186 /* Swap epoch + sequence_number */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002187 memcpy(tmp_out_ctr, ssl->cur_out_ctr, 8);
2188 memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8);
2189 memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, 8);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002190
2191 /* Adjust to the newly activated transform */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002195 if (mbedtls_ssl_hw_record_activate != NULL) {
2196 int ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND);
2197 if (ret != 0) {
2198 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
2199 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002200 }
2201 }
2202#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002205}
2206
2207/*
2208 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002210int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002211{
2212 int ret = 0;
2213
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002214 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002216 ret = mbedtls_ssl_flight_transmit(ssl);
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002220 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002221}
2222
2223/*
2224 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002225 *
2226 * Need to remember the current message in case flush_output returns
2227 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002228 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002229 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002230int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002231{
Janos Follath865b3eb2019-12-16 11:46:15 +00002232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002233 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002235 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
2236 MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002237
2238 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002239 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002240 ret = ssl_swap_epochs(ssl);
2241 if (ret != 0) {
2242 return ret;
2243 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002246 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002248 while (ssl->handshake->cur_msg != NULL) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002249 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002250 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002251
Hanno Beckere1dcb032018-08-17 16:47:58 +01002252 int const is_finished =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002253 (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2254 cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
Hanno Beckere1dcb032018-08-17 16:47:58 +01002255
Hanno Becker04da1892018-08-14 13:22:10 +01002256 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002257 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
Hanno Becker04da1892018-08-14 13:22:10 +01002258
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002259 /* Swap epochs before sending Finished: we can't do it after
2260 * sending ChangeCipherSpec, in case write returns WANT_READ.
2261 * Must be done before copying, may change out_msg pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002262 if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) {
2263 MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message"));
2264 ret = ssl_swap_epochs(ssl);
2265 if (ret != 0) {
2266 return ret;
2267 }
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002268 }
2269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002270 ret = ssl_get_remaining_payload_in_datagram(ssl);
2271 if (ret < 0) {
2272 return ret;
2273 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002274 max_frag_len = (size_t) ret;
2275
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002276 /* CCS is copied as is, while HS messages may need fragmentation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002277 if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2278 if (max_frag_len == 0) {
2279 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2280 return ret;
2281 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002282
2283 continue;
2284 }
2285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002286 memcpy(ssl->out_msg, cur->p, cur->len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002287 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002288 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002289
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002290 /* Update position inside current message */
2291 ssl->handshake->cur_msg_p += cur->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002292 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002293 const unsigned char * const p = ssl->handshake->cur_msg_p;
2294 const size_t hs_len = cur->len - 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002295 const size_t frag_off = p - (cur->p + 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002296 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002297 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002299 if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) {
2300 if (is_finished) {
2301 ret = ssl_swap_epochs(ssl);
2302 if (ret != 0) {
2303 return ret;
2304 }
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002305 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002307 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2308 return ret;
2309 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002310
2311 continue;
2312 }
2313 max_hs_frag_len = max_frag_len - 12;
2314
2315 cur_hs_frag_len = rem_len > max_hs_frag_len ?
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002316 max_hs_frag_len : rem_len;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002318 if (frag_off == 0 && cur_hs_frag_len != hs_len) {
2319 MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)",
2320 (unsigned) cur_hs_frag_len,
2321 (unsigned) max_hs_frag_len));
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002322 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002323
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002324 /* Messages are stored with handshake headers as if not fragmented,
2325 * copy beginning of headers then fill fragmentation fields.
2326 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002327 memcpy(ssl->out_msg, cur->p, 6);
Joe Subbiani61f7d732021-06-24 09:06:23 +01002328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002329 ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off);
2330 ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off);
2331 ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002333 ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len);
2334 ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len);
2335 ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002338
Hanno Becker3f7b9732018-08-28 09:53:25 +01002339 /* Copy the handshake message content and set records fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002340 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002341 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002342 ssl->out_msgtype = cur->type;
2343
2344 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002345 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002346 }
2347
2348 /* If done with the current message move to the next one if any */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002349 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
2350 if (cur->next != NULL) {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002351 ssl->handshake->cur_msg = cur->next;
2352 ssl->handshake->cur_msg_p = cur->next->p + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002353 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002354 ssl->handshake->cur_msg = NULL;
2355 ssl->handshake->cur_msg_p = NULL;
2356 }
2357 }
2358
2359 /* Actually send the message out */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002360 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2361 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
2362 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002363 }
2364 }
2365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2367 return ret;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002368 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002370 /* Update state and set timer */
2371 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
2372 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
2373 } else {
2374 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
2375 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
2376 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002378 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
2379
2380 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002381}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002382
2383/*
2384 * To be called when the last message of an incoming flight is received.
2385 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002386void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002387{
2388 /* We won't need to resend that one any more */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002389 mbedtls_ssl_flight_free(ssl->handshake->flight);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002390 ssl->handshake->flight = NULL;
2391 ssl->handshake->cur_msg = NULL;
2392
2393 /* The next incoming flight will start with this msg_seq */
2394 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2395
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002396 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002397 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002398
Hanno Becker0271f962018-08-16 13:23:47 +01002399 /* Clear future message buffering structure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002400 mbedtls_ssl_buffering_free(ssl);
Hanno Becker0271f962018-08-16 13:23:47 +01002401
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002402 /* Cancel timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002403 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002404
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002405 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2406 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002407 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002408 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002410 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002411}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002412
2413/*
2414 * To be called when the last message of an outgoing flight is send.
2415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002416void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002417{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002418 ssl_reset_retransmit_timeout(ssl);
2419 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002421 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2422 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002424 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002426 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002429
Paul Bakker5121ce52009-01-03 21:22:43 +00002430/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002431 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002433
2434/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002435 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002436 *
2437 * - fill in handshake headers
2438 * - update handshake checksum
2439 * - DTLS: save message for resending
2440 * - then pass to the record layer
2441 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002442 * DTLS: except for HelloRequest, messages are only queued, and will only be
2443 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002444 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002445 * Inputs:
2446 * - ssl->out_msglen: 4 + actual handshake message len
2447 * (4 is the size of handshake headers for TLS)
2448 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2449 * - ssl->out_msg + 4: the handshake message body
2450 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002451 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002452 * - ssl->out_msglen: the length of the record contents
2453 * (including handshake headers but excluding record headers)
2454 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002455 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002456int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002457{
Janos Follath865b3eb2019-12-16 11:46:15 +00002458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002459 const size_t hs_len = ssl->out_msglen - 4;
2460 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002462 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002463
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002464 /*
2465 * Sanity checks
2466 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002467 if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
2468 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002469 /* In SSLv3, the client might send a NoCertificate alert. */
2470#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 if (!(ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2472 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2473 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT))
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002474#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2475 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002476 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2477 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002478 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002481 /* Whenever we send anything different from a
2482 * HelloRequest we should be in a handshake - double check. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002483 if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2484 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) &&
2485 ssl->handshake == NULL) {
2486 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2487 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002488 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002491 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002492 ssl->handshake != NULL &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002493 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
2494 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2495 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002496 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002497#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002498
Hanno Beckerb50a2532018-08-06 11:52:54 +01002499 /* Double-check that we did not exceed the bounds
2500 * of the outgoing record buffer.
2501 * This should never fail as the various message
2502 * writing functions must obey the bounds of the
2503 * outgoing record buffer, but better be safe.
2504 *
2505 * Note: We deliberately do not check for the MTU or MFL here.
2506 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002507 if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2508 MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: "
2509 "size %" MBEDTLS_PRINTF_SIZET
2510 ", maximum %" MBEDTLS_PRINTF_SIZET,
2511 ssl->out_msglen,
2512 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2513 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerb50a2532018-08-06 11:52:54 +01002514 }
2515
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002516 /*
2517 * Fill handshake headers
2518 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002519 if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
2520 ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len);
2521 ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len);
2522 ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00002523
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002524 /*
2525 * DTLS has additional fields in the Handshake layer,
2526 * between the length field and the actual payload:
2527 * uint16 message_seq;
2528 * uint24 fragment_offset;
2529 * uint24 fragment_length;
2530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002532 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002533 /* Make room for the additional DTLS fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002534 if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) {
2535 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: "
2536 "size %" MBEDTLS_PRINTF_SIZET ", maximum %"
2537 MBEDTLS_PRINTF_SIZET,
2538 hs_len,
2539 (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12)));
2540 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9648f8b2017-09-18 10:55:54 +01002541 }
2542
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002543 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002544 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002545
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002546 /* Write message_seq and update it, except for HelloRequest */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002547 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2548 MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4);
2549 ++(ssl->handshake->out_msg_seq);
2550 } else {
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002551 ssl->out_msg[4] = 0;
2552 ssl->out_msg[5] = 0;
2553 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002554
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002555 /* Handshake hashes are computed without fragmentation,
2556 * so set frag_offset = 0 and frag_len = hs_len for now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002557 memset(ssl->out_msg + 6, 0x00, 3);
2558 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002561
Hanno Becker0207e532018-08-28 10:28:28 +01002562 /* Update running hashes of handshake messages seen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002563 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2564 ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen);
2565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002568 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002570 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2571 !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2572 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) {
2573 if ((ret = ssl_flight_append(ssl)) != 0) {
2574 MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret);
2575 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002576 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002577 } else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002578#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002579 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002580 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
2581 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret);
2582 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002583 }
2584 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002585
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002586 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002588 return 0;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002589}
2590
2591/*
2592 * Record layer functions
2593 */
2594
2595/*
2596 * Write current record.
2597 *
2598 * Uses:
2599 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2600 * - ssl->out_msglen: length of the record content (excl headers)
2601 * - ssl->out_msg: record content
2602 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002603int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, uint8_t force_flush)
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002604{
2605 int ret, done = 0;
2606 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002607 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002609 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 if (ssl->transform_out != NULL &&
2613 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
2614 if ((ret = ssl_compress_buf(ssl)) != 0) {
2615 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compress_buf", ret);
2616 return ret;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002617 }
2618
2619 len = ssl->out_msglen;
2620 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002624 if (mbedtls_ssl_hw_record_write != NULL) {
2625 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_write()"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002627 ret = mbedtls_ssl_hw_record_write(ssl);
2628 if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) {
2629 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_write", ret);
2630 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker05ef8352012-05-08 09:17:57 +00002631 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002632
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002633 if (ret == 0) {
Paul Bakkerc7878112012-12-19 14:41:14 +01002634 done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002635 }
Paul Bakker05ef8352012-05-08 09:17:57 +00002636 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002637#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002638 if (!done) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002639 unsigned i;
2640 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002641#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2642 size_t out_buf_len = ssl->out_buf_len;
2643#else
2644 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2645#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002646 /* Skip writing the record content type to after the encryption,
2647 * as it may change when using the CID extension. */
2648
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002649 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2650 ssl->conf->transport, ssl->out_hdr + 1);
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002652 memcpy(ssl->out_ctr, ssl->cur_out_ctr, 8);
2653 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002655 if (ssl->transform_out != NULL) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002656 mbedtls_record rec;
2657
2658 rec.buf = ssl->out_iv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002659 rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 rec.data_len = ssl->out_msglen;
2661 rec.data_offset = ssl->out_msg - rec.buf;
2662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002663 memcpy(&rec.ctr[0], ssl->out_ctr, 8);
2664 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2665 ssl->conf->transport, rec.ver);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002666 rec.type = ssl->out_msgtype;
2667
Hanno Beckera0e20d02019-05-15 14:03:01 +01002668#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002669 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002670 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002671#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002673 if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec,
2674 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2675 MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret);
2676 return ret;
Paul Bakker05ef8352012-05-08 09:17:57 +00002677 }
2678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002679 if (rec.data_offset != 0) {
2680 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2681 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002682 }
2683
Hanno Becker6430faf2019-05-08 11:57:13 +01002684 /* Update the record content type and CID. */
2685 ssl->out_msgtype = rec.type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2687 memcpy(ssl->out_cid, rec.cid, rec.cid_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01002688#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002689 ssl->out_msglen = len = rec.data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002690 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002691 }
2692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002693 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002694
2695#if defined(MBEDTLS_SSL_PROTO_DTLS)
2696 /* In case of DTLS, double-check that we don't exceed
2697 * the remaining space in the datagram. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002698 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2699 ret = ssl_get_remaining_space_in_datagram(ssl);
2700 if (ret < 0) {
2701 return ret;
2702 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002704 if (protected_record_size > (size_t) ret) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002705 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002706 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker2b1e3542018-08-06 11:19:13 +01002707 }
2708 }
2709#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002710
Hanno Becker6430faf2019-05-08 11:57:13 +01002711 /* Now write the potentially updated record content type. */
2712 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, "
2715 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
2716 ssl->out_hdr[0], ssl->out_hdr[1],
2717 ssl->out_hdr[2], len));
Paul Bakker05ef8352012-05-08 09:17:57 +00002718
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002719 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
2720 ssl->out_hdr, protected_record_size);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002721
2722 ssl->out_left += protected_record_size;
2723 ssl->out_hdr += protected_record_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002724 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002725
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002726 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
2727 if (++ssl->cur_out_ctr[i - 1] != 0) {
Hanno Becker04484622018-08-06 09:49:38 +01002728 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002729 }
2730 }
Hanno Becker04484622018-08-06 09:49:38 +01002731
2732 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002733 if (i == mbedtls_ssl_ep_len(ssl)) {
2734 MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap"));
2735 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker04484622018-08-06 09:49:38 +01002736 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002737 }
2738
Hanno Becker67bc7c32018-08-06 11:33:50 +01002739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002740 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2741 flush == SSL_DONT_FORCE_FLUSH) {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002742 size_t remaining;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002743 ret = ssl_get_remaining_payload_in_datagram(ssl);
2744 if (ret < 0) {
2745 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram",
2746 ret);
2747 return ret;
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002748 }
2749
2750 remaining = (size_t) ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002751 if (remaining == 0) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002752 flush = SSL_FORCE_FLUSH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002753 } else {
2754 MBEDTLS_SSL_DEBUG_MSG(2,
2755 ("Still %u bytes available in current datagram",
2756 (unsigned) remaining));
Hanno Becker67bc7c32018-08-06 11:33:50 +01002757 }
2758 }
2759#endif /* MBEDTLS_SSL_PROTO_DTLS */
2760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002761 if ((flush == SSL_FORCE_FLUSH) &&
2762 (ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2763 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
2764 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002765 }
2766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002767 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002769 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002770}
2771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002772#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002773
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002774MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002775static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002776{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002777 if (ssl->in_msglen < ssl->in_hslen ||
2778 memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 ||
2779 memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) {
2780 return 1;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002781 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 return 0;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002783}
Hanno Becker44650b72018-08-16 12:51:11 +01002784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002785static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002786{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002787 return (ssl->in_msg[9] << 16) |
2788 (ssl->in_msg[10] << 8) |
2789 ssl->in_msg[11];
Hanno Becker44650b72018-08-16 12:51:11 +01002790}
2791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002793{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002794 return (ssl->in_msg[6] << 16) |
2795 (ssl->in_msg[7] << 8) |
2796 ssl->in_msg[8];
Hanno Becker44650b72018-08-16 12:51:11 +01002797}
2798
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002799MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002801{
2802 uint32_t msg_len, frag_off, frag_len;
2803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 msg_len = ssl_get_hs_total_len(ssl);
2805 frag_off = ssl_get_hs_frag_off(ssl);
2806 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker44650b72018-08-16 12:51:11 +01002807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 if (frag_off > msg_len) {
2809 return -1;
2810 }
Hanno Becker44650b72018-08-16 12:51:11 +01002811
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002812 if (frag_len > msg_len - frag_off) {
2813 return -1;
2814 }
Hanno Becker44650b72018-08-16 12:51:11 +01002815
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002816 if (frag_len + 12 > ssl->in_msglen) {
2817 return -1;
2818 }
Hanno Becker44650b72018-08-16 12:51:11 +01002819
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002820 return 0;
Hanno Becker44650b72018-08-16 12:51:11 +01002821}
2822
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002823/*
2824 * Mark bits in bitmask (used for DTLS HS reassembly)
2825 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002826static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002827{
2828 unsigned int start_bits, end_bits;
2829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002830 start_bits = 8 - (offset % 8);
2831 if (start_bits != 8) {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002832 size_t first_byte_idx = offset / 8;
2833
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002834 /* Special case */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 if (len <= start_bits) {
2836 for (; len != 0; len--) {
2837 mask[first_byte_idx] |= 1 << (start_bits - len);
2838 }
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002839
2840 /* Avoid potential issues with offset or len becoming invalid */
2841 return;
2842 }
2843
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002844 offset += start_bits; /* Now offset % 8 == 0 */
2845 len -= start_bits;
2846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002847 for (; start_bits != 0; start_bits--) {
2848 mask[first_byte_idx] |= 1 << (start_bits - 1);
2849 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002850 }
2851
2852 end_bits = len % 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002853 if (end_bits != 0) {
2854 size_t last_byte_idx = (offset + len) / 8;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002855
2856 len -= end_bits; /* Now len % 8 == 0 */
2857
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858 for (; end_bits != 0; end_bits--) {
2859 mask[last_byte_idx] |= 1 << (8 - end_bits);
2860 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002861 }
2862
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002863 memset(mask + offset / 8, 0xFF, len / 8);
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002864}
2865
2866/*
2867 * Check that bitmask is full
2868 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002869MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002870static int ssl_bitmask_check(unsigned char *mask, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002871{
2872 size_t i;
2873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 for (i = 0; i < len / 8; i++) {
2875 if (mask[i] != 0xFF) {
2876 return -1;
2877 }
2878 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002880 for (i = 0; i < len % 8; i++) {
2881 if ((mask[len / 8] & (1 << (7 - i))) == 0) {
2882 return -1;
2883 }
2884 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002887}
2888
Hanno Becker56e205e2018-08-16 09:06:12 +01002889/* msg_len does not include the handshake header */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002890static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
2891 unsigned add_bitmap)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002892{
Hanno Becker56e205e2018-08-16 09:06:12 +01002893 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002894
Hanno Becker56e205e2018-08-16 09:06:12 +01002895 alloc_len = 12; /* Handshake header */
2896 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002898 if (add_bitmap) {
2899 alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002900
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002901 }
2902 return alloc_len;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002903}
Hanno Becker56e205e2018-08-16 09:06:12 +01002904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002906
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002907static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
Hanno Becker12555c62018-08-16 12:47:53 +01002908{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002909 return (ssl->in_msg[1] << 16) |
2910 (ssl->in_msg[2] << 8) |
2911 ssl->in_msg[3];
Hanno Becker12555c62018-08-16 12:47:53 +01002912}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002913
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002914int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002915{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002916 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
2917 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
2918 ssl->in_msglen));
2919 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002920 }
2921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002923
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002924 MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
2925 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
2926 MBEDTLS_PRINTF_SIZET,
2927 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002930 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002932 unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002933
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002934 if (ssl_check_hs_header(ssl) != 0) {
2935 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
2936 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker44650b72018-08-16 12:51:11 +01002937 }
2938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002939 if (ssl->handshake != NULL &&
2940 ((ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2941 recv_msg_seq != ssl->handshake->in_msg_seq) ||
2942 (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2943 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
2944 if (recv_msg_seq > ssl->handshake->in_msg_seq) {
2945 MBEDTLS_SSL_DEBUG_MSG(2,
2946 (
2947 "received future handshake message of sequence number %u (next %u)",
2948 recv_msg_seq,
2949 ssl->handshake->in_msg_seq));
2950 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Becker9e1ec222018-08-15 15:54:43 +01002951 }
2952
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002953 /* Retransmit only on last message from previous flight, to avoid
2954 * too many retransmissions.
2955 * Besides, No sane server ever retransmits HelloVerifyRequest */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002956 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
2957 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
2958 MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
2959 "message_seq = %u, start_of_flight = %u",
2960 recv_msg_seq,
2961 ssl->handshake->in_flight_start_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002962
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002963 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
2964 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
2965 return ret;
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002966 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002967 } else {
2968 MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
2969 "message_seq = %u, expected = %u",
2970 recv_msg_seq,
2971 ssl->handshake->in_msg_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002972 }
2973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002975 }
2976 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002977
Hanno Becker6d97ef52018-08-16 13:09:04 +01002978 /* Message reassembly is handled alongside buffering of future
2979 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002980 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002981 * handshake logic layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 if (ssl_hs_is_proper_fragment(ssl) == 1) {
2983 MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
2984 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002985 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002986 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002988 /* With TLS we don't handle fragmentation (for now) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002989 if (ssl->in_msglen < ssl->in_hslen) {
2990 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
2991 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002992 }
2993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002994 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01002995}
2996
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002997void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01002998{
Hanno Becker0271f962018-08-16 13:23:47 +01002999 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003001 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL) {
3002 ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003003 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003004
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003005 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003006#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003007 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3008 ssl->handshake != NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01003009 unsigned offset;
3010 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003011
Hanno Becker0271f962018-08-16 13:23:47 +01003012 /* Increment handshake sequence number */
3013 hs->in_msg_seq++;
3014
3015 /*
3016 * Clear up handshake buffering and reassembly structure.
3017 */
3018
3019 /* Free first entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003020 ssl_buffering_free_slot(ssl, 0);
Hanno Becker0271f962018-08-16 13:23:47 +01003021
3022 /* Shift all other entries */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003023 for (offset = 0, hs_buf = &hs->buffering.hs[0];
Hanno Beckere605b192018-08-21 15:59:07 +01003024 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003025 offset++, hs_buf++) {
Hanno Becker0271f962018-08-16 13:23:47 +01003026 *hs_buf = *(hs_buf + 1);
3027 }
3028
3029 /* Create a fresh last entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003030 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003031 }
3032#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003033}
3034
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003035/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003036 * DTLS anti-replay: RFC 6347 4.1.2.6
3037 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003038 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3039 * Bit n is set iff record number in_window_top - n has been seen.
3040 *
3041 * Usually, in_window_top is the last record number seen and the lsb of
3042 * in_window is set. The only exception is the initial state (record number 0
3043 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003044 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003046void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003047{
3048 ssl->in_window_top = 0;
3049 ssl->in_window = 0;
3050}
3051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003052static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003053{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003054 return ((uint64_t) buf[0] << 40) |
3055 ((uint64_t) buf[1] << 32) |
3056 ((uint64_t) buf[2] << 24) |
3057 ((uint64_t) buf[3] << 16) |
3058 ((uint64_t) buf[4] << 8) |
3059 ((uint64_t) buf[5]);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003060}
3061
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003063static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003064{
Janos Follath865b3eb2019-12-16 11:46:15 +00003065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003066 unsigned char *original_in_ctr;
3067
3068 // save original in_ctr
3069 original_in_ctr = ssl->in_ctr;
3070
3071 // use counter from record
3072 ssl->in_ctr = record_in_ctr;
3073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003074 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003075
3076 // restore the counter
3077 ssl->in_ctr = original_in_ctr;
3078
3079 return ret;
3080}
3081
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003082/*
3083 * Return 0 if sequence number is acceptable, -1 otherwise
3084 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003085int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003086{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003087 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003088 uint64_t bit;
3089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003090 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
3091 return 0;
3092 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003094 if (rec_seqnum > ssl->in_window_top) {
3095 return 0;
3096 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003097
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003098 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003099
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003100 if (bit >= 64) {
3101 return -1;
3102 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
3105 return -1;
3106 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 return 0;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003109}
3110
3111/*
3112 * Update replay window on new validated record
3113 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003115{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003118 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003119 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003120 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 if (rec_seqnum > ssl->in_window_top) {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003123 /* Update window_top and the contents of the window */
3124 uint64_t shift = rec_seqnum - ssl->in_window_top;
3125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126 if (shift >= 64) {
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003127 ssl->in_window = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003129 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003130 ssl->in_window |= 1;
3131 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003132
3133 ssl->in_window_top = rec_seqnum;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003134 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003135 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003136 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003138 if (bit < 64) { /* Always true, but be extra sure */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003139 ssl->in_window |= (uint64_t) 1 << bit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003140 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003141 }
3142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003144
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003145#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003146/*
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003147 * Check if a datagram looks like a ClientHello with a valid cookie,
3148 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003149 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003150 *
3151 * - if cookie is valid, return 0
3152 * - if ClientHello looks superficially valid but cookie is not,
3153 * fill obuf and set olen, then
3154 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3155 * - otherwise return a specific error code
3156 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003157MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek33f41a82022-06-08 11:47:33 -04003158MBEDTLS_STATIC_TESTABLE
3159int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003160 mbedtls_ssl_context *ssl,
3161 const unsigned char *cli_id, size_t cli_id_len,
3162 const unsigned char *in, size_t in_len,
3163 unsigned char *obuf, size_t buf_len, size_t *olen)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003164{
3165 size_t sid_len, cookie_len;
3166 unsigned char *p;
3167
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003168 /*
3169 * Structure of ClientHello with record and handshake headers,
3170 * and expected values. We don't need to check a lot, more checks will be
3171 * done when actually parsing the ClientHello - skipping those checks
3172 * avoids code duplication and does not make cookie forging any easier.
3173 *
3174 * 0-0 ContentType type; copied, must be handshake
3175 * 1-2 ProtocolVersion version; copied
3176 * 3-4 uint16 epoch; copied, must be 0
3177 * 5-10 uint48 sequence_number; copied
3178 * 11-12 uint16 length; (ignored)
3179 *
3180 * 13-13 HandshakeType msg_type; (ignored)
3181 * 14-16 uint24 length; (ignored)
3182 * 17-18 uint16 message_seq; copied
3183 * 19-21 uint24 fragment_offset; copied, must be 0
3184 * 22-24 uint24 fragment_length; (ignored)
3185 *
3186 * 25-26 ProtocolVersion client_version; (ignored)
3187 * 27-58 Random random; (ignored)
3188 * 59-xx SessionID session_id; 1 byte len + sid_len content
3189 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3190 * ...
3191 *
3192 * Minimum length is 61 bytes.
3193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003194 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
3195 (unsigned) in_len));
3196 MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
3197 if (in_len < 61) {
3198 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
3199 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003200 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003202 in[3] != 0 || in[4] != 0 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003203 in[19] != 0 || in[20] != 0 || in[21] != 0) {
3204 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
3205 MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
3206 in[0],
3207 (unsigned) in[3] << 8 | in[4],
3208 (unsigned) in[19] << 16 | in[20] << 8 | in[21]));
3209 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003210 }
3211
3212 sid_len = in[59];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003213 if (59 + 1 + sid_len + 1 > in_len) {
3214 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
3215 (unsigned) sid_len,
3216 (unsigned) in_len - 61));
3217 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003218 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003219 MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
3220 in + 60, sid_len);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003221
3222 cookie_len = in[60 + sid_len];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003223 if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
3224 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
3225 (unsigned) cookie_len,
3226 (unsigned) (in_len - sid_len - 61)));
3227 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003228 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003230 MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
3231 in + sid_len + 61, cookie_len);
3232 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
3233 in + sid_len + 61, cookie_len,
3234 cli_id, cli_id_len) == 0) {
3235 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
3236 return 0;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003237 }
3238
3239 /*
3240 * If we get here, we've got an invalid cookie, let's prepare HVR.
3241 *
3242 * 0-0 ContentType type; copied
3243 * 1-2 ProtocolVersion version; copied
3244 * 3-4 uint16 epoch; copied
3245 * 5-10 uint48 sequence_number; copied
3246 * 11-12 uint16 length; olen - 13
3247 *
3248 * 13-13 HandshakeType msg_type; hello_verify_request
3249 * 14-16 uint24 length; olen - 25
3250 * 17-18 uint16 message_seq; copied
3251 * 19-21 uint24 fragment_offset; copied
3252 * 22-24 uint24 fragment_length; olen - 25
3253 *
3254 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3255 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3256 *
3257 * Minimum length is 28.
3258 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 if (buf_len < 28) {
3260 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3261 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003262
3263 /* Copy most fields and adapt others */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003264 memcpy(obuf, in, 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003265 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3266 obuf[25] = 0xfe;
3267 obuf[26] = 0xff;
3268
3269 /* Generate and write actual cookie */
3270 p = obuf + 28;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003271 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
3272 &p, obuf + buf_len,
3273 cli_id, cli_id_len) != 0) {
3274 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003275 }
3276
3277 *olen = p - obuf;
3278
3279 /* Go back and fill length fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003280 obuf[27] = (unsigned char) (*olen - 28);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003282 obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
3283 obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
3284 obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003286 MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003288 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003289}
3290
3291/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003292 * Handle possible client reconnect with the same UDP quadruplet
3293 * (RFC 6347 Section 4.2.8).
3294 *
3295 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3296 * that looks like a ClientHello.
3297 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003298 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003299 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003300 * - if the input looks like a ClientHello with a valid cookie,
3301 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003302 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003303 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003304 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003305 * This function is called (through ssl_check_client_reconnect()) when an
3306 * unexpected record is found in ssl_get_next_record(), which will discard the
3307 * record if we return 0, and bubble up the return value otherwise (this
3308 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3309 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003310 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003311MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003312static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003313{
Janos Follath865b3eb2019-12-16 11:46:15 +00003314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003315 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003317 if (ssl->conf->f_cookie_write == NULL ||
3318 ssl->conf->f_cookie_check == NULL) {
Hanno Becker2fddd372019-07-10 14:37:41 +01003319 /* If we can't use cookies to verify reachability of the peer,
3320 * drop the record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003321 MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
3322 "can't check reconnect validity"));
3323 return 0;
Hanno Becker2fddd372019-07-10 14:37:41 +01003324 }
3325
Andrzej Kurek33f41a82022-06-08 11:47:33 -04003326 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003327 ssl,
3328 ssl->cli_id, ssl->cli_id_len,
3329 ssl->in_buf, ssl->in_left,
3330 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003332 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003335 int send_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003336 MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
3337 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3338 ssl->out_buf, len);
Brian J Murray1903fb32016-11-06 04:45:15 -08003339 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003340 * If the error is permanent we'll catch it later,
3341 * if it's not, then hopefully it'll work next time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003342 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
3343 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003344 (void) send_ret;
3345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003346 return 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003347 }
3348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003349 if (ret == 0) {
3350 MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
3351 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
3352 MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
3353 return ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003354 }
3355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003356 return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003357 }
3358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003359 return ret;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003360}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003361#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003362
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003363MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003364static int ssl_check_record_type(uint8_t record_type)
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003365{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003366 if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003367 record_type != MBEDTLS_SSL_MSG_ALERT &&
3368 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003369 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
3370 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003371 }
3372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003373 return 0;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003374}
3375
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003376/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003377 * ContentType type;
3378 * ProtocolVersion version;
3379 * uint16 epoch; // DTLS only
3380 * uint48 sequence_number; // DTLS only
3381 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003382 *
3383 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003384 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003385 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3386 *
3387 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003388 * 1. proceed with the record if this function returns 0
3389 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3390 * 3. return CLIENT_RECONNECT if this function return that value
3391 * 4. drop the whole datagram if this function returns anything else.
3392 * Point 2 is needed when the peer is resending, and we have already received
3393 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003394 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003396static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
3397 unsigned char *buf,
3398 size_t len,
3399 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00003400{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003401 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003402
Hanno Beckere5e7e782019-07-11 12:29:35 +01003403 size_t const rec_hdr_type_offset = 0;
3404 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003405
Hanno Beckere5e7e782019-07-11 12:29:35 +01003406 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3407 rec_hdr_type_len;
3408 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
Hanno Beckere5e7e782019-07-11 12:29:35 +01003410 size_t const rec_hdr_ctr_len = 8;
3411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003412 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003413 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3414 rec_hdr_version_len;
3415
Hanno Beckera0e20d02019-05-15 14:03:01 +01003416#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003417 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3418 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003419 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003420#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3421#endif /* MBEDTLS_SSL_PROTO_DTLS */
3422
3423 size_t rec_hdr_len_offset; /* To be determined */
3424 size_t const rec_hdr_len_len = 2;
3425
3426 /*
3427 * Check minimum lengths for record header.
3428 */
3429
3430#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003431 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003432 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003433 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003434#endif /* MBEDTLS_SSL_PROTO_DTLS */
3435 {
3436 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3437 }
3438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003439 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3440 MBEDTLS_SSL_DEBUG_MSG(1,
3441 (
3442 "datagram of length %u too small to hold DTLS record header of length %u",
3443 (unsigned) len,
3444 (unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
3445 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003446 }
3447
3448 /*
3449 * Parse and validate record content type
3450 */
3451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003452 rec->type = buf[rec_hdr_type_offset];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003453
3454 /* Check record content type */
3455#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3456 rec->cid_len = 0;
3457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003458 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003459 ssl->conf->cid_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003460 rec->type == MBEDTLS_SSL_MSG_CID) {
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003461 /* Shift pointers to account for record header including CID
3462 * struct {
3463 * ContentType special_type = tls12_cid;
3464 * ProtocolVersion version;
3465 * uint16 epoch;
3466 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003467 * opaque cid[cid_length]; // Additional field compared to
3468 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003469 * uint16 length;
3470 * opaque enc_content[DTLSCiphertext.length];
3471 * } DTLSCiphertext;
3472 */
3473
3474 /* So far, we only support static CID lengths
3475 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003476 rec_hdr_cid_len = ssl->conf->cid_len;
3477 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3480 MBEDTLS_SSL_DEBUG_MSG(1,
3481 (
3482 "datagram of length %u too small to hold DTLS record header including CID, length %u",
3483 (unsigned) len,
3484 (unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
3485 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere538d822019-07-10 14:50:10 +01003486 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003487
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003488 /* configured CID len is guaranteed at most 255, see
3489 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3490 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003491 memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
3492 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003493#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003494 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003495 if (ssl_check_record_type(rec->type)) {
3496 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
3497 (unsigned) rec->type));
3498 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003499 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003500 }
3501
Hanno Beckere5e7e782019-07-11 12:29:35 +01003502 /*
3503 * Parse and validate record version
3504 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003505 rec->ver[0] = buf[rec_hdr_version_offset + 0];
3506 rec->ver[1] = buf[rec_hdr_version_offset + 1];
3507 mbedtls_ssl_read_version(&major_ver, &minor_ver,
3508 ssl->conf->transport,
3509 &rec->ver[0]);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003511 if (major_ver != ssl->major_ver) {
3512 MBEDTLS_SSL_DEBUG_MSG(1, ("major version mismatch: got %u, expected %u",
3513 (unsigned) major_ver,
3514 (unsigned) ssl->major_ver));
3515 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 }
3517
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003518 if (minor_ver > ssl->conf->max_minor_ver) {
3519 MBEDTLS_SSL_DEBUG_MSG(1, ("minor version mismatch: got %u, expected max %u",
3520 (unsigned) minor_ver,
3521 (unsigned) ssl->conf->max_minor_ver));
3522 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003524 /*
3525 * Parse/Copy record sequence number.
3526 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003527
Hanno Beckere5e7e782019-07-11 12:29:35 +01003528#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003529 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003530 /* Copy explicit record sequence number from input buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003531 memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
3532 rec_hdr_ctr_len);
3533 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003534#endif /* MBEDTLS_SSL_PROTO_DTLS */
3535 {
3536 /* Copy implicit record sequence number from SSL context structure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003538 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003539
Hanno Beckere5e7e782019-07-11 12:29:35 +01003540 /*
3541 * Parse record length.
3542 */
3543
Hanno Beckere5e7e782019-07-11 12:29:35 +01003544 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) |
3546 ((size_t) buf[rec_hdr_len_offset + 1] << 0);
3547 MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003549 MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
3550 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
3551 rec->type,
3552 major_ver, minor_ver, rec->data_len));
Hanno Beckere5e7e782019-07-11 12:29:35 +01003553
3554 rec->buf = buf;
3555 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003556
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003557 if (rec->data_len == 0) {
3558 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3559 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003561 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003562 * DTLS-related tests.
3563 * Check epoch before checking length constraint because
3564 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3565 * message gets duplicated before the corresponding Finished message,
3566 * the second ChangeCipherSpec should be discarded because it belongs
3567 * to an old epoch, but not because its length is shorter than
3568 * the minimum record length for packets using the new record transform.
3569 * Note that these two kinds of failures are handled differently,
3570 * as an unexpected record is silently skipped but an invalid
3571 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003572 */
3573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003574 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3575 rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003576
Hanno Becker955a5c92019-07-10 17:12:07 +01003577 /* Check that the datagram is large enough to contain a record
3578 * of the advertised length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 if (len < rec->data_offset + rec->data_len) {
3580 MBEDTLS_SSL_DEBUG_MSG(1,
3581 (
3582 "Datagram of length %u too small to contain record of advertised length %u.",
3583 (unsigned) len,
3584 (unsigned) (rec->data_offset + rec->data_len)));
3585 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker955a5c92019-07-10 17:12:07 +01003586 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003587
Hanno Becker37cfe732019-07-10 17:20:01 +01003588 /* Records from other, non-matching epochs are silently discarded.
3589 * (The case of same-port Client reconnects must be considered in
3590 * the caller). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 if (rec_epoch != ssl->in_epoch) {
3592 MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
3593 "expected %u, received %lu",
3594 ssl->in_epoch, (unsigned long) rec_epoch));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003595
Hanno Becker552f7472019-07-19 10:59:12 +01003596 /* Records from the next epoch are considered for buffering
3597 * (concretely: early Finished messages). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003598 if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
3599 MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
3600 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003601 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003603 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003604 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003605#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003606 /* For records from the correct epoch, check whether their
3607 * sequence number has been seen before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003608 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
3609 &rec->ctr[0]) != 0) {
3610 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
3611 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003612 }
3613#endif
3614 }
3615#endif /* MBEDTLS_SSL_PROTO_DTLS */
3616
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003617 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003618}
Paul Bakker5121ce52009-01-03 21:22:43 +00003619
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
Hanno Becker2fddd372019-07-10 14:37:41 +01003621#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003622MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003623static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
Hanno Becker2fddd372019-07-10 14:37:41 +01003624{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003625 unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1];
Hanno Becker2fddd372019-07-10 14:37:41 +01003626
3627 /*
3628 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3629 * access the first byte of record content (handshake type), as we
3630 * have an active transform (possibly iv_len != 0), so use the
3631 * fact that the record header len is 13 instead.
3632 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003633 if (rec_epoch == 0 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003634 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3635 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3636 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3637 ssl->in_left > 13 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003638 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
3639 MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
3640 "from the same port"));
3641 return ssl_handle_possible_reconnect(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 }
3643
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003644 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003645}
Hanno Becker2fddd372019-07-10 14:37:41 +01003646#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003648/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003649 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003650 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003651MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003652static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
3653 mbedtls_record *rec)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003654{
3655 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003656
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003657 MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
3658 rec->buf, rec->buf_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003661 if (mbedtls_ssl_hw_record_read != NULL) {
3662 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_read()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00003663
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003664 ret = mbedtls_ssl_hw_record_read(ssl);
3665 if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) {
3666 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_read", ret);
3667 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker05ef8352012-05-08 09:17:57 +00003668 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003669
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003670 if (ret == 0) {
Paul Bakkerc7878112012-12-19 14:41:14 +01003671 done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003672 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003675 if (!done && ssl->transform_in != NULL) {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003676 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003678 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
3679 rec)) != 0) {
3680 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
Hanno Becker8367ccc2019-05-14 11:30:10 +01003681
Hanno Beckera0e20d02019-05-15 14:03:01 +01003682#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003683 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Becker8367ccc2019-05-14 11:30:10 +01003684 ssl->conf->ignore_unexpected_cid
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003685 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
3686 MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
Hanno Becker16ded982019-05-08 13:02:55 +01003687 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003688 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003689#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003691 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 }
3693
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003694 if (old_msg_type != rec->type) {
3695 MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
3696 old_msg_type, rec->type));
Hanno Becker6430faf2019-05-08 11:57:13 +01003697 }
3698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003699 MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
3700 rec->buf + rec->data_offset, rec->data_len);
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003701
Hanno Beckera0e20d02019-05-15 14:03:01 +01003702#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003703 /* We have already checked the record content type
3704 * in ssl_parse_record_header(), failing or silently
3705 * dropping the record in the case of an unknown type.
3706 *
3707 * Since with the use of CIDs, the record content type
3708 * might change during decryption, re-check the record
3709 * content type, but treat a failure as fatal this time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003710 if (ssl_check_record_type(rec->type)) {
3711 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
3712 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker6430faf2019-05-08 11:57:13 +01003713 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003714#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003716 if (rec->data_len == 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003718 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
3719 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003720 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003721 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
3722 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003723 }
3724#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3725
3726 ssl->nb_zero++;
3727
3728 /*
3729 * Three or more empty messages may be a DoS attack
3730 * (excessive CPU consumption).
3731 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003732 if (ssl->nb_zero > 3) {
3733 MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
3734 "messages, possible DoS attack"));
Hanno Becker6e7700d2019-05-08 10:38:32 +01003735 /* Treat the records as if they were not properly authenticated,
3736 * thereby failing the connection if we see more than allowed
3737 * by the configured bad MAC threshold. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003738 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003739 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003740 } else {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003741 ssl->nb_zero = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003742 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003743
3744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003745 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003746 ; /* in_ctr read from peer, not maintained internally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003747 } else
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003748#endif
3749 {
3750 unsigned i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003751 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
3752 if (++ssl->in_ctr[i - 1] != 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003753 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003754 }
3755 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003756
3757 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003758 if (i == mbedtls_ssl_ep_len(ssl)) {
3759 MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
3760 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003761 }
3762 }
3763
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 }
3765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3768 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003769 }
3770#endif
3771
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003772 /* Check actual (decrypted) record content length against
3773 * configured maximum. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003774 if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
3775 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
3776 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003777 }
3778
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003779 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003780}
3781
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003782/*
3783 * Read a record.
3784 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003785 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3786 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3787 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003788 */
Hanno Becker1097b342018-08-15 14:09:41 +01003789
3790/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003792static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003793MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003794static int ssl_get_next_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
Hanno Becker4162b112018-08-15 14:05:04 +01003797
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003798int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
3799 unsigned update_hs_digest)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003800{
Janos Follath865b3eb2019-12-16 11:46:15 +00003801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003803 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003805 if (ssl->keep_current_message == 0) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003806 do {
Simon Butcher99000142016-10-13 17:21:01 +01003807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003808 ret = ssl_consume_current_message(ssl);
3809 if (ret != 0) {
3810 return ret;
3811 }
Hanno Becker26994592018-08-15 14:14:59 +01003812
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003813 if (ssl_record_is_in_progress(ssl) == 0) {
David Horstmann5846c9d2022-10-06 18:31:25 +01003814 int dtls_have_buffered = 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003815#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere74d5562018-08-15 14:26:08 +01003816
Hanno Becker40f50842018-08-15 14:48:01 +01003817 /* We only check for buffered messages if the
3818 * current datagram is fully consumed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3820 ssl_next_record_is_in_datagram(ssl) == 0) {
3821 if (ssl_load_buffered_message(ssl) == 0) {
David Horstmann5846c9d2022-10-06 18:31:25 +01003822 dtls_have_buffered = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003823 }
Hanno Becker40f50842018-08-15 14:48:01 +01003824 }
3825
Hanno Becker40f50842018-08-15 14:48:01 +01003826#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003827 if (dtls_have_buffered == 0) {
3828 ret = ssl_get_next_record(ssl);
3829 if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
Hanno Becker40f50842018-08-15 14:48:01 +01003830 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 }
Hanno Becker40f50842018-08-15 14:48:01 +01003832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003833 if (ret != 0) {
3834 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
3835 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003836 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003837 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003838 }
3839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003840 ret = mbedtls_ssl_handle_message_type(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003841
Hanno Becker40f50842018-08-15 14:48:01 +01003842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003843 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker40f50842018-08-15 14:48:01 +01003844 /* Buffer future message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 ret = ssl_buffer_message(ssl);
3846 if (ret != 0) {
3847 return ret;
3848 }
Hanno Becker40f50842018-08-15 14:48:01 +01003849
3850 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3851 }
3852#endif /* MBEDTLS_SSL_PROTO_DTLS */
3853
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 } while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3855 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003856
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003857 if (0 != ret) {
3858 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
3859 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01003860 }
3861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003862 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3863 update_hs_digest == 1) {
3864 mbedtls_ssl_update_handshake_status(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003865 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003866 } else {
3867 MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003868 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003869 }
3870
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003871 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
Simon Butcher99000142016-10-13 17:21:01 +01003872
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003873 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003874}
3875
Hanno Becker40f50842018-08-15 14:48:01 +01003876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003877MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003878static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003879{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003880 if (ssl->in_left > ssl->next_record_offset) {
3881 return 1;
3882 }
Simon Butcher99000142016-10-13 17:21:01 +01003883
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003884 return 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003885}
3886
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003887MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01003889{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003890 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003891 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003892 int ret = 0;
3893
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003894 if (hs == NULL) {
3895 return -1;
3896 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
Hanno Beckere00ae372018-08-20 09:39:42 +01003899
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003900 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3901 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003902 /* Check if we have seen a ChangeCipherSpec before.
3903 * If yes, synthesize a CCS record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003904 if (!hs->buffering.seen_ccs) {
3905 MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003906 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003907 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003908 }
3909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003910 MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003911 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3912 ssl->in_msglen = 1;
3913 ssl->in_msg[0] = 1;
3914
3915 /* As long as they are equal, the exact value doesn't matter. */
3916 ssl->in_left = 0;
3917 ssl->next_record_offset = 0;
3918
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003919 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003920 goto exit;
3921 }
Hanno Becker37f95322018-08-16 13:55:32 +01003922
Hanno Beckerb8f50142018-08-28 10:01:34 +01003923#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003924 /* Debug only */
3925 {
3926 unsigned offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003927 for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
Hanno Becker37f95322018-08-16 13:55:32 +01003928 hs_buf = &hs->buffering.hs[offset];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 if (hs_buf->is_valid == 1) {
3930 MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
3931 hs->in_msg_seq + offset,
3932 hs_buf->is_complete ? "fully" : "partially"));
Hanno Becker37f95322018-08-16 13:55:32 +01003933 }
3934 }
3935 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003936#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003937
3938 /* Check if we have buffered and/or fully reassembled the
3939 * next handshake message. */
3940 hs_buf = &hs->buffering.hs[0];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003941 if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
Hanno Becker37f95322018-08-16 13:55:32 +01003942 /* Synthesize a record containing the buffered HS message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003943 size_t msg_len = (hs_buf->data[1] << 16) |
3944 (hs_buf->data[2] << 8) |
3945 hs_buf->data[3];
Hanno Becker37f95322018-08-16 13:55:32 +01003946
3947 /* Double-check that we haven't accidentally buffered
3948 * a message that doesn't fit into the input buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
3950 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3951 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01003952 }
3953
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003954 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
3955 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
3956 hs_buf->data, msg_len + 12);
Hanno Becker37f95322018-08-16 13:55:32 +01003957
3958 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3959 ssl->in_hslen = msg_len + 12;
3960 ssl->in_msglen = msg_len + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003961 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
Hanno Becker37f95322018-08-16 13:55:32 +01003962
3963 ret = 0;
3964 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003965 } else {
3966 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
3967 hs->in_msg_seq));
Hanno Becker37f95322018-08-16 13:55:32 +01003968 }
3969
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003970 ret = -1;
3971
3972exit:
3973
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003974 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
3975 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003976}
3977
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003978MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003979static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
3980 size_t desired)
Hanno Beckera02b0b42018-08-21 17:20:27 +01003981{
3982 int offset;
3983 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003984 MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
3985 (unsigned) desired));
Hanno Beckera02b0b42018-08-21 17:20:27 +01003986
Hanno Becker01315ea2018-08-21 17:22:17 +01003987 /* Get rid of future records epoch first, if such exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003988 ssl_free_buffered_record(ssl);
Hanno Becker01315ea2018-08-21 17:22:17 +01003989
3990 /* Check if we have enough space available now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003991 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3992 hs->buffering.total_bytes_buffered)) {
3993 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
3994 return 0;
Hanno Becker01315ea2018-08-21 17:22:17 +01003995 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003996
Hanno Becker4f432ad2018-08-28 10:02:32 +01003997 /* We don't have enough space to buffer the next expected handshake
3998 * message. Remove buffers used for future messages to gain space,
3999 * starting with the most distant one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004000 for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4001 offset >= 0; offset--) {
4002 MBEDTLS_SSL_DEBUG_MSG(2,
4003 (
4004 "Free buffering slot %d to make space for reassembly of next handshake message",
4005 offset));
Hanno Beckera02b0b42018-08-21 17:20:27 +01004006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004007 ssl_buffering_free_slot(ssl, (uint8_t) offset);
Hanno Beckera02b0b42018-08-21 17:20:27 +01004008
4009 /* Check if we have enough space available now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004010 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4011 hs->buffering.total_bytes_buffered)) {
4012 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
4013 return 0;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004014 }
4015 }
4016
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004017 return -1;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004018}
4019
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004020MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004021static int ssl_buffer_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004022{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004023 int ret = 0;
4024 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004026 if (hs == NULL) {
4027 return 0;
4028 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004029
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004030 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004032 switch (ssl->in_msgtype) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004033 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004034 MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
Hanno Beckere678eaa2018-08-21 14:57:46 +01004035
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004036 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004037 break;
4038
4039 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004040 {
4041 unsigned recv_msg_seq_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004042 unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Hanno Becker37f95322018-08-16 13:55:32 +01004043 mbedtls_ssl_hs_buffer *hs_buf;
4044 size_t msg_len = ssl->in_hslen - 12;
4045
4046 /* We should never receive an old handshake
4047 * message - double-check nonetheless. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004048 if (recv_msg_seq < ssl->handshake->in_msg_seq) {
4049 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4050 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004051 }
4052
4053 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004054 if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Becker37f95322018-08-16 13:55:32 +01004055 /* Silently ignore -- message too far in the future */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004056 MBEDTLS_SSL_DEBUG_MSG(2,
4057 ("Ignore future HS message with sequence number %u, "
4058 "buffering window %u - %u",
4059 recv_msg_seq, ssl->handshake->in_msg_seq,
4060 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
4061 1));
Hanno Becker37f95322018-08-16 13:55:32 +01004062
4063 goto exit;
4064 }
4065
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004066 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
4067 recv_msg_seq, recv_msg_seq_offset));
Hanno Becker37f95322018-08-16 13:55:32 +01004068
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004069 hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
Hanno Becker37f95322018-08-16 13:55:32 +01004070
4071 /* Check if the buffering for this seq nr has already commenced. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004072 if (!hs_buf->is_valid) {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004073 size_t reassembly_buf_sz;
4074
Hanno Becker37f95322018-08-16 13:55:32 +01004075 hs_buf->is_fragmented =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004076 (ssl_hs_is_proper_fragment(ssl) == 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004077
4078 /* We copy the message back into the input buffer
4079 * after reassembly, so check that it's not too large.
4080 * This is an implementation-specific limitation
4081 * and not one from the standard, hence it is not
4082 * checked in ssl_check_hs_header(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
Hanno Becker37f95322018-08-16 13:55:32 +01004084 /* Ignore message */
4085 goto exit;
4086 }
4087
Hanno Beckere0b150f2018-08-21 15:51:03 +01004088 /* Check if we have enough space to buffer the message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004089 if (hs->buffering.total_bytes_buffered >
4090 MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
4091 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4092 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004093 }
4094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004095 reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
4096 hs_buf->is_fragmented);
Hanno Beckere0b150f2018-08-21 15:51:03 +01004097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004098 if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4099 hs->buffering.total_bytes_buffered)) {
4100 if (recv_msg_seq_offset > 0) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004101 /* If we can't buffer a future message because
4102 * of space limitations -- ignore. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004103 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) -- ignore\n",
4110 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4111 hs->buffering.total_bytes_buffered));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004112 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 } else {
4114 MBEDTLS_SSL_DEBUG_MSG(2,
4115 ("Buffering of future message of size %"
4116 MBEDTLS_PRINTF_SIZET
4117 " would exceed the compile-time limit %"
4118 MBEDTLS_PRINTF_SIZET
4119 " (already %" MBEDTLS_PRINTF_SIZET
4120 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4121 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4122 hs->buffering.total_bytes_buffered));
Hanno Beckere1801392018-08-21 16:51:05 +01004123 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004125 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
4126 MBEDTLS_SSL_DEBUG_MSG(2,
4127 ("Reassembly of next message of size %"
4128 MBEDTLS_PRINTF_SIZET
4129 " (%" MBEDTLS_PRINTF_SIZET
4130 " with bitmap) would exceed"
4131 " the compile-time limit %"
4132 MBEDTLS_PRINTF_SIZET
4133 " (already %" MBEDTLS_PRINTF_SIZET
4134 " bytes buffered) -- fail\n",
4135 msg_len,
4136 reassembly_buf_sz,
4137 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4138 hs->buffering.total_bytes_buffered));
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004139 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4140 goto exit;
4141 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004142 }
4143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004144 MBEDTLS_SSL_DEBUG_MSG(2,
4145 ("initialize reassembly, total length = %"
4146 MBEDTLS_PRINTF_SIZET,
4147 msg_len));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004149 hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
4150 if (hs_buf->data == NULL) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004151 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004152 goto exit;
4153 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004154 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004155
4156 /* Prepare final header: copy msg_type, length and message_seq,
4157 * then add standardised fragment_offset and fragment_length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004158 memcpy(hs_buf->data, ssl->in_msg, 6);
4159 memset(hs_buf->data + 6, 0, 3);
4160 memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
Hanno Becker37f95322018-08-16 13:55:32 +01004161
4162 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004163
4164 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004165 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004166 /* Make sure msg_type and length are consistent */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004167 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
4168 MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
Hanno Becker37f95322018-08-16 13:55:32 +01004169 /* Ignore */
4170 goto exit;
4171 }
4172 }
4173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004174 if (!hs_buf->is_complete) {
Hanno Becker37f95322018-08-16 13:55:32 +01004175 size_t frag_len, frag_off;
4176 unsigned char * const msg = hs_buf->data + 12;
4177
4178 /*
4179 * Check and copy current fragment
4180 */
4181
4182 /* Validation of header fields already done in
4183 * mbedtls_ssl_prepare_handshake_record(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004184 frag_off = ssl_get_hs_frag_off(ssl);
4185 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker37f95322018-08-16 13:55:32 +01004186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004187 MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4188 ", length = %" MBEDTLS_PRINTF_SIZET,
4189 frag_off, frag_len));
4190 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
Hanno Becker37f95322018-08-16 13:55:32 +01004191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004192 if (hs_buf->is_fragmented) {
Hanno Becker37f95322018-08-16 13:55:32 +01004193 unsigned char * const bitmask = msg + msg_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004194 ssl_bitmask_set(bitmask, frag_off, frag_len);
4195 hs_buf->is_complete = (ssl_bitmask_check(bitmask,
4196 msg_len) == 0);
4197 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004198 hs_buf->is_complete = 1;
4199 }
4200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004201 MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
4202 hs_buf->is_complete ? "" : "not yet "));
Hanno Becker37f95322018-08-16 13:55:32 +01004203 }
4204
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004205 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004206 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004207
4208 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004209 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004210 break;
4211 }
4212
4213exit:
4214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004215 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
4216 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004217}
4218#endif /* MBEDTLS_SSL_PROTO_DTLS */
4219
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004220MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004221static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004222{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004223 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004224 * Consume last content-layer message and potentially
4225 * update in_msglen which keeps track of the contents'
4226 * consumption state.
4227 *
4228 * (1) Handshake messages:
4229 * Remove last handshake message, move content
4230 * and adapt in_msglen.
4231 *
4232 * (2) Alert messages:
4233 * Consume whole record content, in_msglen = 0.
4234 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004235 * (3) Change cipher spec:
4236 * Consume whole record content, in_msglen = 0.
4237 *
4238 * (4) Application data:
4239 * Don't do anything - the record layer provides
4240 * the application data as a stream transport
4241 * and consumes through mbedtls_ssl_read only.
4242 *
4243 */
4244
4245 /* Case (1): Handshake messages */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004246 if (ssl->in_hslen != 0) {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004247 /* Hard assertion to be sure that no application data
4248 * is in flight, as corrupting ssl->in_msglen during
4249 * ssl->in_offt != NULL is fatal. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004250 if (ssl->in_offt != NULL) {
4251 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4252 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004253 }
4254
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004255 /*
4256 * Get next Handshake message in the current record
4257 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004258
Hanno Becker4a810fb2017-05-24 16:27:30 +01004259 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004260 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004261 * current handshake content: If DTLS handshake
4262 * fragmentation is used, that's the fragment
4263 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004264 * size here is faulty and should be changed at
4265 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004266 * (2) While it doesn't seem to cause problems, one
4267 * has to be very careful not to assume that in_hslen
4268 * is always <= in_msglen in a sensible communication.
4269 * Again, it's wrong for DTLS handshake fragmentation.
4270 * The following check is therefore mandatory, and
4271 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004272 * Additionally, ssl->in_hslen might be arbitrarily out of
4273 * bounds after handling a DTLS message with an unexpected
4274 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004275 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004276 if (ssl->in_hslen < ssl->in_msglen) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004277 ssl->in_msglen -= ssl->in_hslen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4279 ssl->in_msglen);
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004280
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004281 MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
4282 ssl->in_msg, ssl->in_msglen);
4283 } else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004284 ssl->in_msglen = 0;
4285 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004286
Hanno Becker4a810fb2017-05-24 16:27:30 +01004287 ssl->in_hslen = 0;
4288 }
4289 /* Case (4): Application data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290 else if (ssl->in_offt != NULL) {
4291 return 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01004292 }
4293 /* Everything else (CCS & Alerts) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004294 else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004295 ssl->in_msglen = 0;
4296 }
4297
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004298 return 0;
Hanno Becker1097b342018-08-15 14:09:41 +01004299}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004300
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004301MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004302static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
Hanno Beckere74d5562018-08-15 14:26:08 +01004303{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 if (ssl->in_msglen > 0) {
4305 return 1;
4306 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308 return 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004309}
4310
Hanno Becker5f066e72018-08-16 14:56:31 +01004311#if defined(MBEDTLS_SSL_PROTO_DTLS)
4312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004313static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004314{
4315 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004316 if (hs == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004317 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004320 if (hs->buffering.future_record.data != NULL) {
Hanno Becker01315ea2018-08-21 17:22:17 +01004321 hs->buffering.total_bytes_buffered -=
4322 hs->buffering.future_record.len;
4323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004324 mbedtls_free(hs->buffering.future_record.data);
Hanno Becker01315ea2018-08-21 17:22:17 +01004325 hs->buffering.future_record.data = NULL;
4326 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004327}
4328
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004329MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004330static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004331{
4332 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004333 unsigned char *rec;
Hanno Becker5f066e72018-08-16 14:56:31 +01004334 size_t rec_len;
4335 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004336#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4337 size_t in_buf_len = ssl->in_buf_len;
4338#else
4339 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4340#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4342 return 0;
4343 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004345 if (hs == NULL) {
4346 return 0;
4347 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004348
Hanno Becker5f066e72018-08-16 14:56:31 +01004349 rec = hs->buffering.future_record.data;
4350 rec_len = hs->buffering.future_record.len;
4351 rec_epoch = hs->buffering.future_record.epoch;
4352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 if (rec == NULL) {
4354 return 0;
4355 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004356
Hanno Becker4cb782d2018-08-20 11:19:05 +01004357 /* Only consider loading future records if the
4358 * input buffer is empty. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004359 if (ssl_next_record_is_in_datagram(ssl) == 1) {
4360 return 0;
4361 }
Hanno Becker4cb782d2018-08-20 11:19:05 +01004362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004363 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004365 if (rec_epoch != ssl->in_epoch) {
4366 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
Hanno Becker5f066e72018-08-16 14:56:31 +01004367 goto exit;
4368 }
4369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004371
4372 /* Double-check that the record is not too large */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004373 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
4374 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4375 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker5f066e72018-08-16 14:56:31 +01004376 }
4377
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378 memcpy(ssl->in_hdr, rec, rec_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004379 ssl->in_left = rec_len;
4380 ssl->next_record_offset = 0;
4381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004382 ssl_free_buffered_record(ssl);
Hanno Becker5f066e72018-08-16 14:56:31 +01004383
4384exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004385 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
4386 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004387}
4388
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004389MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
4391 mbedtls_record const *rec)
Hanno Becker5f066e72018-08-16 14:56:31 +01004392{
4393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004394
4395 /* Don't buffer future records outside handshakes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004396 if (hs == NULL) {
4397 return 0;
4398 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004399
4400 /* Only buffer handshake records (we are only interested
4401 * in Finished messages). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004402 if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
4403 return 0;
4404 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004405
4406 /* Don't buffer more than one future epoch record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004407 if (hs->buffering.future_record.data != NULL) {
4408 return 0;
4409 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004410
Hanno Becker01315ea2018-08-21 17:22:17 +01004411 /* Don't buffer record if there's not enough buffering space remaining. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4413 hs->buffering.total_bytes_buffered)) {
4414 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4415 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4416 " (already %" MBEDTLS_PRINTF_SIZET
4417 " bytes buffered) -- ignore\n",
4418 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4419 hs->buffering.total_bytes_buffered));
4420 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004421 }
4422
Hanno Becker5f066e72018-08-16 14:56:31 +01004423 /* Buffer record */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004424 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
4425 ssl->in_epoch + 1U));
4426 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004427
4428 /* ssl_parse_record_header() only considers records
4429 * of the next epoch as candidates for buffering. */
4430 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004431 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004432
4433 hs->buffering.future_record.data =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004434 mbedtls_calloc(1, hs->buffering.future_record.len);
4435 if (hs->buffering.future_record.data == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004436 /* If we run out of RAM trying to buffer a
4437 * record from the next epoch, just ignore. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004439 }
4440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004441 memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004442
Hanno Becker519f15d2019-07-11 12:43:20 +01004443 hs->buffering.total_bytes_buffered += rec->buf_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004444 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004445}
4446
4447#endif /* MBEDTLS_SSL_PROTO_DTLS */
4448
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004449MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004450static int ssl_get_next_record(mbedtls_ssl_context *ssl)
Hanno Becker1097b342018-08-15 14:09:41 +01004451{
Janos Follath865b3eb2019-12-16 11:46:15 +00004452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004453 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004454
Hanno Becker5f066e72018-08-16 14:56:31 +01004455#if defined(MBEDTLS_SSL_PROTO_DTLS)
4456 /* We might have buffered a future record; if so,
4457 * and if the epoch matches now, load it.
4458 * On success, this call will set ssl->in_left to
4459 * the length of the buffered record, so that
4460 * the calls to ssl_fetch_input() below will
4461 * essentially be no-ops. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004462 ret = ssl_load_buffered_record(ssl);
4463 if (ret != 0) {
4464 return ret;
4465 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004466#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004467
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004468 /* Ensure that we have enough space available for the default form
4469 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4470 * with no space for CIDs counted in). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004471 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
4472 if (ret != 0) {
4473 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4474 return ret;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004475 }
4476
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004477 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
4478 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004479#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004480 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4481 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
4482 ret = ssl_buffer_future_record(ssl, &rec);
4483 if (ret != 0) {
4484 return ret;
4485 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004486
4487 /* Fall through to handling of unexpected records */
4488 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4489 }
4490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004491 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
Hanno Becker2fddd372019-07-10 14:37:41 +01004492#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004493 /* Reset in pointers to default state for TLS/DTLS records,
4494 * assuming no CID and no offset between record content and
4495 * record plaintext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004496 mbedtls_ssl_update_in_pointers(ssl);
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004497
Hanno Becker7ae20e02019-07-12 08:33:49 +01004498 /* Setup internal message pointers from record structure. */
4499 ssl->in_msgtype = rec.type;
4500#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4501 ssl->in_len = ssl->in_cid + rec.cid_len;
4502#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4503 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4504 ssl->in_msglen = rec.data_len;
4505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004506 ret = ssl_check_client_reconnect(ssl);
4507 MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
4508 if (ret != 0) {
4509 return ret;
4510 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004511#endif
4512
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004513 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004514 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
4517 "(header)"));
4518 } else {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004519 /* Skip invalid record and the rest of the datagram */
4520 ssl->next_record_offset = 0;
4521 ssl->in_left = 0;
4522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004523 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
4524 "(header)"));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004525 }
4526
4527 /* Get next record */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4529 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004530#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004531 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004532 return ret;
Hanno Becker2fddd372019-07-10 14:37:41 +01004533 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004534 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004537 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckera8814792019-07-10 15:01:45 +01004538 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004539 ssl->next_record_offset = rec.buf_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004540 if (ssl->next_record_offset < ssl->in_left) {
4541 MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
Hanno Beckere65ce782017-05-22 14:47:48 +01004542 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004543 } else
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004544#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004545 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004546 /*
4547 * Fetch record contents from underlying transport.
4548 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004549 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
4550 if (ret != 0) {
4551 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4552 return ret;
Hanno Beckera8814792019-07-10 15:01:45 +01004553 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004554
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004555 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004556 }
4557
4558 /*
4559 * Decrypt record contents.
4560 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004561
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004562 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004564 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004565 /* Silently discard invalid records */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004566 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004567 /* Except when waiting for Finished as a bad mac here
4568 * probably means something went wrong in the handshake
4569 * (eg wrong psk used, mitm downgrade attempt, etc.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004570 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4571 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004572#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004573 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4574 mbedtls_ssl_send_alert_message(ssl,
4575 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4576 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004577 }
4578#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004579 return ret;
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004580 }
4581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004583 if (ssl->conf->badmac_limit != 0 &&
4584 ++ssl->badmac_seen >= ssl->conf->badmac_limit) {
4585 MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
4586 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004587 }
4588#endif
4589
Hanno Becker4a810fb2017-05-24 16:27:30 +01004590 /* As above, invalid records cause
4591 * dismissal of the whole datagram. */
4592
4593 ssl->next_record_offset = 0;
4594 ssl->in_left = 0;
4595
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004596 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
4597 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004598 }
4599
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 return ret;
4601 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004602#endif
4603 {
4604 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004605#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004606 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4607 mbedtls_ssl_send_alert_message(ssl,
4608 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4609 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004610 }
4611#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004612 return ret;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004613 }
4614 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004615
Hanno Becker44d89b22019-07-12 09:40:44 +01004616
4617 /* Reset in pointers to default state for TLS/DTLS records,
4618 * assuming no CID and no offset between record content and
4619 * record plaintext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004620 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker44d89b22019-07-12 09:40:44 +01004621#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4622 ssl->in_len = ssl->in_cid + rec.cid_len;
4623#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004624 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004625
Hanno Becker8685c822019-07-12 09:37:30 +01004626 /* The record content type may change during decryption,
4627 * so re-read it. */
4628 ssl->in_msgtype = rec.type;
4629 /* Also update the input buffer, because unfortunately
4630 * the server-side ssl_parse_client_hello() reparses the
4631 * record header when receiving a ClientHello initiating
4632 * a renegotiation. */
4633 ssl->in_hdr[0] = rec.type;
4634 ssl->in_msg = rec.buf + rec.data_offset;
4635 ssl->in_msglen = rec.data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004636 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
Hanno Becker8685c822019-07-12 09:37:30 +01004637
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004638#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004639 if (ssl->transform_in != NULL &&
4640 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
4641 if ((ret = ssl_decompress_buf(ssl)) != 0) {
4642 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decompress_buf", ret);
4643 return ret;
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004644 }
4645
4646 /* Check actual (decompress) record content length against
4647 * configured maximum. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004648 if (ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN) {
4649 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
4650 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004651 }
4652 }
4653#endif /* MBEDTLS_ZLIB_SUPPORT */
4654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004655 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01004656}
4657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004658int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01004659{
Janos Follath865b3eb2019-12-16 11:46:15 +00004660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004661
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004662 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004663 * Handle particular types of records
4664 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004665 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
4666 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
4667 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01004668 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 }
4670
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004671 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4672 if (ssl->in_msglen != 1) {
4673 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
4674 ssl->in_msglen));
4675 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004676 }
4677
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004678 if (ssl->in_msg[0] != 1) {
4679 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
4680 ssl->in_msg[0]));
4681 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004682 }
4683
4684#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004685 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01004686 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
4688 if (ssl->handshake == NULL) {
4689 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
4690 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004691 }
4692
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004693 MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
4694 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004695 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004696#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004697 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004698
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004699 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
4700 if (ssl->in_msglen != 2) {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004701 /* Note: Standard allows for more than one 2 byte alert
4702 to be packed in a single message, but Mbed TLS doesn't
4703 currently support this. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004704 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
4705 ssl->in_msglen));
4706 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004707 }
4708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
4710 ssl->in_msg[0], ssl->in_msg[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00004711
4712 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004713 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004715 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
4716 MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
4717 ssl->in_msg[1]));
4718 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 }
4720
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004721 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4722 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
4723 MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
4724 return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004725 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004726
4727#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4729 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
4730 MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no renegotiation alert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004731 /* Will be handled when trying to parse ServerHello */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004732 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004733 }
4734#endif
4735
4736#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004737 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004738 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4739 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
4741 MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no_cert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004742 /* Will be handled in mbedtls_ssl_parse_certificate() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004744 }
4745#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4746
4747 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004748 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 }
4750
Hanno Beckerc76c6192017-06-06 10:03:17 +01004751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004752 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker37ae9522019-05-03 16:54:26 +01004753 /* Drop unexpected ApplicationData records,
4754 * except at the beginning of renegotiations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004755 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
Hanno Becker37ae9522019-05-03 16:54:26 +01004756 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4757#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004758 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4759 ssl->state == MBEDTLS_SSL_SERVER_HELLO)
Hanno Beckerc76c6192017-06-06 10:03:17 +01004760#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004761 ) {
4762 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
4763 return MBEDTLS_ERR_SSL_NON_FATAL;
Hanno Becker37ae9522019-05-03 16:54:26 +01004764 }
4765
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004766 if (ssl->handshake != NULL &&
4767 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4768 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Hanno Becker37ae9522019-05-03 16:54:26 +01004769 }
4770 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004771#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004773 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004774}
4775
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004776int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004777{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004778 return mbedtls_ssl_send_alert_message(ssl,
4779 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4780 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004781}
4782
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004783int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
4784 unsigned char level,
4785 unsigned char message)
Paul Bakker0a925182012-04-16 06:46:41 +00004786{
Janos Follath865b3eb2019-12-16 11:46:15 +00004787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004788
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004789 if (ssl == NULL || ssl->conf == NULL) {
4790 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4791 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004793 if (ssl->out_left != 0) {
4794 return mbedtls_ssl_flush_output(ssl);
4795 }
Hanno Beckerd9c66c02018-08-06 11:35:16 +01004796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004797 MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
4798 MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
Paul Bakker0a925182012-04-16 06:46:41 +00004799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004800 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004801 ssl->out_msglen = 2;
4802 ssl->out_msg[0] = level;
4803 ssl->out_msg[1] = message;
4804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004805 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
4806 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
4807 return ret;
Paul Bakker0a925182012-04-16 06:46:41 +00004808 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004809 MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
Paul Bakker0a925182012-04-16 06:46:41 +00004810
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004811 return 0;
Paul Bakker0a925182012-04-16 06:46:41 +00004812}
4813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004815{
Janos Follath865b3eb2019-12-16 11:46:15 +00004816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004818 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 ssl->out_msglen = 1;
4822 ssl->out_msg[0] = 1;
4823
Paul Bakker5121ce52009-01-03 21:22:43 +00004824 ssl->state++;
4825
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004826 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4827 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4828 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 }
4830
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004831 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004832
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004833 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004834}
4835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004836int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004837{
Janos Follath865b3eb2019-12-16 11:46:15 +00004838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004839
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004840 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004842 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
4843 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
4844 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 }
4846
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004847 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4848 MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
4849 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4850 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
4851 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 }
4853
Hanno Beckere678eaa2018-08-21 14:57:46 +01004854 /* CCS records are only accepted if they have length 1 and content '1',
4855 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004856
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004857 /*
4858 * Switch to our negotiated transform and session parameters for inbound
4859 * data.
4860 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004861 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004862 ssl->transform_in = ssl->transform_negotiate;
4863 ssl->session_in = ssl->session_negotiate;
4864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004868 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004869#endif
4870
4871 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004872 if (++ssl->in_epoch == 0) {
4873 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004874 /* This is highly unlikely to happen for legitimate reasons, so
4875 treat it as an attack and don't send an alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004876 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004877 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004878 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004879#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004880 memset(ssl->in_ctr, 0, 8);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004882 mbedtls_ssl_update_in_pointers(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004884#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 if (mbedtls_ssl_hw_record_activate != NULL) {
4886 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_INBOUND)) != 0) {
4887 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
4888 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4889 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
4890 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004891 }
4892 }
4893#endif
4894
Paul Bakker5121ce52009-01-03 21:22:43 +00004895 ssl->state++;
4896
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004897 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004898
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004900}
4901
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004902/* Once ssl->out_hdr as the address of the beginning of the
4903 * next outgoing record is set, deduce the other pointers.
4904 *
4905 * Note: For TLS, we save the implicit record sequence number
4906 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4907 * and the caller has to make sure there's space for this.
4908 */
4909
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004910static size_t ssl_transform_get_explicit_iv_len(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004911 mbedtls_ssl_transform const *transform)
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004912{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004913 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
4914 return 0;
4915 }
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004916
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004917 return transform->ivlen - transform->fixed_ivlen;
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004918}
4919
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004920void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
4921 mbedtls_ssl_transform *transform)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004922{
4923#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004925 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004926#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004927 ssl->out_cid = ssl->out_ctr + 8;
4928 ssl->out_len = ssl->out_cid;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004929 if (transform != NULL) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004930 ssl->out_len += transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004931 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004932#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004933 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004934#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004935 ssl->out_iv = ssl->out_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004936 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004937#endif
4938 {
4939 ssl->out_ctr = ssl->out_hdr - 8;
4940 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004941#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004942 ssl->out_cid = ssl->out_len;
4943#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004944 ssl->out_iv = ssl->out_hdr + 5;
4945 }
4946
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004947 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004948 /* Adjust out_msg to make space for explicit IV, if used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004949 if (transform != NULL) {
4950 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
4951 }
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004952}
4953
4954/* Once ssl->in_hdr as the address of the beginning of the
4955 * next incoming record is set, deduce the other pointers.
4956 *
4957 * Note: For TLS, we save the implicit record sequence number
4958 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4959 * and the caller has to make sure there's space for this.
4960 */
4961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004962void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004963{
Hanno Becker79594fd2019-05-08 09:38:41 +01004964 /* This function sets the pointers to match the case
4965 * of unprotected TLS/DTLS records, with both ssl->in_iv
4966 * and ssl->in_msg pointing to the beginning of the record
4967 * content.
4968 *
4969 * When decrypting a protected record, ssl->in_msg
4970 * will be shifted to point to the beginning of the
4971 * record plaintext.
4972 */
4973
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004975 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004976 /* This sets the header pointers to match records
4977 * without CID. When we receive a record containing
4978 * a CID, the fields are shifted accordingly in
4979 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004980 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004981#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004982 ssl->in_cid = ssl->in_ctr + 8;
4983 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004984#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004985 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004986#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004987 ssl->in_iv = ssl->in_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004988 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004989#endif
4990 {
4991 ssl->in_ctr = ssl->in_hdr - 8;
4992 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004993#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004994 ssl->in_cid = ssl->in_len;
4995#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004996 ssl->in_iv = ssl->in_hdr + 5;
4997 }
4998
Hanno Becker79594fd2019-05-08 09:38:41 +01004999 /* This will be adjusted at record decryption time. */
5000 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005001}
5002
Paul Bakker5121ce52009-01-03 21:22:43 +00005003/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005004 * Setup an SSL context
5005 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005007void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005008{
5009 /* Set the incoming and outgoing record pointers. */
5010#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005011 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005012 ssl->out_hdr = ssl->out_buf;
5013 ssl->in_hdr = ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005014 } else
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005015#endif /* MBEDTLS_SSL_PROTO_DTLS */
5016 {
5017 ssl->out_hdr = ssl->out_buf + 8;
5018 ssl->in_hdr = ssl->in_buf + 8;
5019 }
5020
5021 /* Derive other internal pointers. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
5023 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005024}
5025
Paul Bakker5121ce52009-01-03 21:22:43 +00005026/*
5027 * SSL get accessors
5028 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005030{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005031 return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00005032}
5033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005034int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
Hanno Becker8b170a02017-10-10 11:51:19 +01005035{
5036 /*
5037 * Case A: We're currently holding back
5038 * a message for further processing.
5039 */
5040
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005041 if (ssl->keep_current_message == 1) {
5042 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
5043 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005044 }
5045
5046 /*
5047 * Case B: Further records are pending in the current datagram.
5048 */
5049
5050#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005051 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5052 ssl->in_left > ssl->next_record_offset) {
5053 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
5054 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005055 }
5056#endif /* MBEDTLS_SSL_PROTO_DTLS */
5057
5058 /*
5059 * Case C: A handshake message is being processed.
5060 */
5061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005062 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
5063 MBEDTLS_SSL_DEBUG_MSG(3,
5064 ("ssl_check_pending: more handshake messages within current record"));
5065 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005066 }
5067
5068 /*
5069 * Case D: An application data message is being processed
5070 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005071 if (ssl->in_offt != NULL) {
5072 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
5073 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005074 }
5075
5076 /*
5077 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005078 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005079 * we implement support for multiple alerts in single records.
5080 */
5081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005082 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
5083 return 0;
Hanno Becker8b170a02017-10-10 11:51:19 +01005084}
5085
Paul Bakker43ca69c2011-01-15 17:35:19 +00005086
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005087int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005088{
Hanno Becker3136ede2018-08-17 15:28:19 +01005089 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005091 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005093 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker5903de42019-05-03 14:46:38 +01005094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005095 if (transform == NULL) {
5096 return (int) out_hdr_len;
5097 }
Hanno Becker78640902018-08-13 16:35:15 +01005098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005100 if (ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL) {
5101 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5102 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005103#endif
5104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005105 switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 case MBEDTLS_MODE_GCM:
5107 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005108 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005109 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005110 transform_expansion = transform->minlen;
5111 break;
5112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005114
5115 block_size = mbedtls_cipher_get_block_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005116 &transform->cipher_ctx_enc);
Hanno Becker5b559ac2018-08-03 09:40:07 +01005117
Hanno Becker3136ede2018-08-17 15:28:19 +01005118 /* Expansion due to the addition of the MAC. */
5119 transform_expansion += transform->maclen;
5120
5121 /* Expansion due to the addition of CBC padding;
5122 * Theoretically up to 256 bytes, but we never use
5123 * more than the block size of the underlying cipher. */
5124 transform_expansion += block_size;
5125
5126 /* For TLS 1.1 or higher, an explicit IV is added
5127 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005128#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005129 if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Becker3136ede2018-08-17 15:28:19 +01005130 transform_expansion += block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005131 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01005132#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005133
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005134 break;
5135
5136 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005137 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5138 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005139 }
5140
Hanno Beckera0e20d02019-05-15 14:03:01 +01005141#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005142 if (transform->out_cid_len != 0) {
Hanno Becker6cbad552019-05-08 15:40:11 +01005143 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005144 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005145#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005147 return (int) (out_hdr_len + transform_expansion);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005148}
5149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005151/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005152 * Check record counters and renegotiate if they're above the limit.
5153 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005154MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005156{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005157 size_t ep_len = mbedtls_ssl_ep_len(ssl);
Andres AG2196c7f2016-12-15 17:01:16 +00005158 int in_ctr_cmp;
5159 int out_ctr_cmp;
5160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005161 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005162 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005163 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5164 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005165 }
5166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005167 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
5168 ssl->conf->renego_period + ep_len, 8 - ep_len);
5169 out_ctr_cmp = memcmp(ssl->cur_out_ctr + ep_len,
5170 ssl->conf->renego_period + ep_len, 8 - ep_len);
Andres AG2196c7f2016-12-15 17:01:16 +00005171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005172 if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
5173 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005174 }
5175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005176 MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
5177 return mbedtls_ssl_renegotiate(ssl);
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005180
5181/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005182 * Receive application data decrypted from the SSL layer
5183 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005185{
Janos Follath865b3eb2019-12-16 11:46:15 +00005186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005187 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 if (ssl == NULL || ssl->conf == NULL) {
5190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5191 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005193 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005196 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5197 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5198 return ret;
5199 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005201 if (ssl->handshake != NULL &&
5202 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
5203 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
5204 return ret;
5205 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005206 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005207 }
5208#endif
5209
Hanno Becker4a810fb2017-05-24 16:27:30 +01005210 /*
5211 * Check if renegotiation is necessary and/or handshake is
5212 * in process. If yes, perform/continue, and fall through
5213 * if an unexpected packet is received while the client
5214 * is waiting for the ServerHello.
5215 *
5216 * (There is no equivalent to the last condition on
5217 * the server-side as it is not treated as within
5218 * a handshake while waiting for the ClientHello
5219 * after a renegotiation request.)
5220 */
5221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005223 ret = ssl_check_ctr_renegotiate(ssl);
5224 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5225 ret != 0) {
5226 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5227 return ret;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005228 }
5229#endif
5230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5232 ret = mbedtls_ssl_handshake(ssl);
5233 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5234 ret != 0) {
5235 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5236 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005237 }
5238 }
5239
Hanno Beckere41158b2017-10-23 13:30:32 +01005240 /* Loop as long as no application data record is available */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 while (ssl->in_offt == NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005242 /* Start timer if not already running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005243 if (ssl->f_get_timer != NULL &&
5244 ssl->f_get_timer(ssl->p_timer) == -1) {
5245 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005246 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005248 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5249 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5250 return 0;
5251 }
Paul Bakker831a7552011-05-18 13:32:51 +00005252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005253 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5254 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005255 }
5256
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005257 if (ssl->in_msglen == 0 &&
5258 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00005259 /*
5260 * OpenSSL sends empty messages to randomize the IV
5261 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005262 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5263 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5264 return 0;
5265 }
Paul Bakker831a7552011-05-18 13:32:51 +00005266
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005267 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5268 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 }
5270 }
5271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005272 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
5273 MBEDTLS_SSL_DEBUG_MSG(1, ("received handshake message"));
Paul Bakker48916f92012-09-16 19:57:18 +00005274
Hanno Becker4a810fb2017-05-24 16:27:30 +01005275 /*
5276 * - For client-side, expect SERVER_HELLO_REQUEST.
5277 * - For server-side, expect CLIENT_HELLO.
5278 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5279 */
5280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005282 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5283 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5284 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
5285 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005286
5287 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005289 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker90333da2017-10-10 11:27:13 +01005290 continue;
5291 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005292#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005293 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005294 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005295#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005296
Hanno Becker4a810fb2017-05-24 16:27:30 +01005297#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005298 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5299 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
5300 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005301
5302 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005304 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker90333da2017-10-10 11:27:13 +01005305 continue;
5306 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005307#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005308 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker48916f92012-09-16 19:57:18 +00005309 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005310#endif /* MBEDTLS_SSL_SRV_C */
5311
Hanno Becker21df7f92017-10-17 11:03:26 +01005312#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005313 /* Determine whether renegotiation attempt should be accepted */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005314 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5315 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5316 ssl->conf->allow_legacy_renegotiation ==
5317 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005318 /*
5319 * Accept renegotiation request
5320 */
Paul Bakker48916f92012-09-16 19:57:18 +00005321
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005322 /* DTLS clients need to know renego is server-initiated */
5323#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5325 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005326 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5327 }
5328#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005329 ret = mbedtls_ssl_start_renegotiation(ssl);
5330 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5331 ret != 0) {
5332 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
5333 ret);
5334 return ret;
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005335 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005336 } else
Hanno Becker21df7f92017-10-17 11:03:26 +01005337#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005338 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005339 /*
5340 * Refuse renegotiation
5341 */
5342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005343 MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
Paul Bakker48916f92012-09-16 19:57:18 +00005344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005346 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine92e44262017-05-10 17:27:49 +02005347 /* SSLv3 does not have a "no_renegotiation" warning, so
5348 we send a fatal alert and abort the connection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005349 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5350 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
5351 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
5352 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5354#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005355 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5356 if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
5357 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5358 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5359 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION))
5360 != 0) {
5361 return ret;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005362 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005363 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5365 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005366 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005367 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5368 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02005369 }
Paul Bakker48916f92012-09-16 19:57:18 +00005370 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005371
Hanno Becker90333da2017-10-10 11:27:13 +01005372 /* At this point, we don't know whether the renegotiation has been
5373 * completed or not. The cases to consider are the following:
5374 * 1) The renegotiation is complete. In this case, no new record
5375 * has been read yet.
5376 * 2) The renegotiation is incomplete because the client received
5377 * an application data record while awaiting the ServerHello.
5378 * 3) The renegotiation is incomplete because the client received
5379 * a non-handshake, non-application data message while awaiting
5380 * the ServerHello.
5381 * In each of these case, looping will be the proper action:
5382 * - For 1), the next iteration will read a new record and check
5383 * if it's application data.
5384 * - For 2), the loop condition isn't satisfied as application data
5385 * is present, hence continue is the same as break
5386 * - For 3), the loop condition is satisfied and read_record
5387 * will re-deliver the message that was held back by the client
5388 * when expecting the ServerHello.
5389 */
5390 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005391 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005392#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005393 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5394 if (ssl->conf->renego_max_records >= 0) {
5395 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
5396 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
5397 "but not honored by client"));
5398 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005399 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005400 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005405 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
5406 MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
5407 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005408 }
5409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005410 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
5411 MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
5412 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005413 }
5414
5415 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005416
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005417 /* We're going to return something now, cancel timer,
5418 * except if handshake (renegotiation) is in progress */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005419 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
5420 mbedtls_ssl_set_timer(ssl, 0);
5421 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005422
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005424 /* If we requested renego but received AppData, resend HelloRequest.
5425 * Do it now, after setting in_offt, to avoid taking this branch
5426 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005428 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5429 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5430 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
5431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
5432 ret);
5433 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005434 }
5435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005437#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005438 }
5439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005440 n = (len < ssl->in_msglen)
Paul Bakker5121ce52009-01-03 21:22:43 +00005441 ? len : ssl->in_msglen;
5442
ashesmancf01d782022-02-17 11:08:27 +13005443 if (len != 0) {
Ashley Duncan272cc192022-02-11 09:57:18 +13005444 memcpy(buf, ssl->in_offt, n);
5445 ssl->in_msglen -= n;
5446 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005447
gabor-mezei-arma3214132020-07-15 10:55:00 +02005448 /* Zeroising the plaintext buffer to erase unused application data
5449 from the memory. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005450 mbedtls_platform_zeroize(ssl->in_offt, n);
gabor-mezei-arma3214132020-07-15 10:55:00 +02005451
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005452 if (ssl->in_msglen == 0) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005453 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005455 ssl->keep_current_message = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005456 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00005457 /* more data available */
5458 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005459 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005460
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005461 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005463 return (int) n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005464}
5465
5466/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005467 * Send application data to be encrypted by the SSL layer, taking care of max
5468 * fragment length and buffer size.
5469 *
5470 * According to RFC 5246 Section 6.2.1:
5471 *
5472 * Zero-length fragments of Application data MAY be sent as they are
5473 * potentially useful as a traffic analysis countermeasure.
5474 *
5475 * Therefore, it is possible that the input message length is 0 and the
5476 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005478MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005479static int ssl_write_real(mbedtls_ssl_context *ssl,
5480 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005481{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005482 int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005483 const size_t max_len = (size_t) ret;
5484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005485 if (ret < 0) {
5486 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
5487 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005488 }
5489
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005490 if (len > max_len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005492 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5493 MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
5494 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5495 " > %" MBEDTLS_PRINTF_SIZET,
5496 len, max_len));
5497 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5498 } else
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005499#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005500 len = max_len;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005501 }
Paul Bakker887bd502011-06-08 13:10:54 +00005502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005503 if (ssl->out_left != 0) {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005504 /*
5505 * The user has previously tried to send the data and
5506 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5507 * written. In this case, we expect the high-level write function
5508 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005510 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5511 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
5512 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005513 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005514 } else {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005515 /*
5516 * The user is trying to send a message the first time, so we need to
5517 * copy the data into the internal buffers and setup the data structure
5518 * to keep track of partial writes
5519 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005520 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Dave Rodgman12155572023-02-24 15:41:34 +00005522 if (len > 0) {
5523 memcpy(ssl->out_msg, buf, len);
5524 }
Paul Bakker887bd502011-06-08 13:10:54 +00005525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005526 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
5527 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
5528 return ret;
Paul Bakker887bd502011-06-08 13:10:54 +00005529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005530 }
5531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532 return (int) len;
Paul Bakker5121ce52009-01-03 21:22:43 +00005533}
5534
5535/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005536 * Write application data, doing 1/n-1 splitting if necessary.
5537 *
5538 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005539 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005540 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005543MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005544static int ssl_write_split(mbedtls_ssl_context *ssl,
5545 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005546{
Janos Follath865b3eb2019-12-16 11:46:15 +00005547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005548
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005549 if (ssl->conf->cbc_record_splitting ==
5550 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005551 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005553 mbedtls_cipher_get_cipher_mode(&ssl->transform_out->cipher_ctx_enc)
5554 != MBEDTLS_MODE_CBC) {
5555 return ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005556 }
5557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005558 if (ssl->split_done == 0) {
5559 if ((ret = ssl_write_real(ssl, buf, 1)) <= 0) {
5560 return ret;
5561 }
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005562 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005563 }
5564
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005565 if ((ret = ssl_write_real(ssl, buf + 1, len - 1)) <= 0) {
5566 return ret;
5567 }
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005568 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005569
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005570 return ret + 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005573
5574/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005575 * Write application data (public-facing wrapper)
5576 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005577int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005578{
Janos Follath865b3eb2019-12-16 11:46:15 +00005579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005581 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005582
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005583 if (ssl == NULL || ssl->conf == NULL) {
5584 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5585 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005586
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005588 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
5589 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5590 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005591 }
5592#endif
5593
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005594 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5595 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5596 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5597 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005598 }
5599 }
5600
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005601#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005602 ret = ssl_write_split(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005603#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005604 ret = ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005605#endif
5606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005607 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005608
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005609 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005610}
5611
5612/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005613 * Notify the peer that the connection is being closed
5614 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005615int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005616{
Janos Follath865b3eb2019-12-16 11:46:15 +00005617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005619 if (ssl == NULL || ssl->conf == NULL) {
5620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5621 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005622
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 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
5626 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5627 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5628 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
5629 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
5630 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005631 }
5632 }
5633
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005634 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005636 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005637}
5638
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005639void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Paul Bakker48916f92012-09-16 19:57:18 +00005640{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005641 if (transform == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005642 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005643 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646 deflateEnd(&transform->ctx_deflate);
5647 inflateEnd(&transform->ctx_inflate);
Paul Bakker48916f92012-09-16 19:57:18 +00005648#endif
5649
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005650 mbedtls_cipher_free(&transform->cipher_ctx_enc);
5651 mbedtls_cipher_free(&transform->cipher_ctx_dec);
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005652
Hanno Beckerd56ed242018-01-03 15:32:51 +00005653#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005654 mbedtls_md_free(&transform->md_ctx_enc);
5655 mbedtls_md_free(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00005656#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005658 mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
Paul Bakker48916f92012-09-16 19:57:18 +00005659}
5660
Hanno Becker0271f962018-08-16 13:23:47 +01005661#if defined(MBEDTLS_SSL_PROTO_DTLS)
5662
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005663void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
Hanno Becker0271f962018-08-16 13:23:47 +01005664{
5665 unsigned offset;
5666 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5667
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005668 if (hs == NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01005669 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005670 }
Hanno Becker0271f962018-08-16 13:23:47 +01005671
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005672 ssl_free_buffered_record(ssl);
Hanno Becker283f5ef2018-08-24 09:34:47 +01005673
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005674 for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
5675 ssl_buffering_free_slot(ssl, offset);
5676 }
Hanno Beckere605b192018-08-21 15:59:07 +01005677}
5678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005679static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
5680 uint8_t slot)
Hanno Beckere605b192018-08-21 15:59:07 +01005681{
5682 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5683 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005684
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005685 if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Beckerb309b922018-08-23 13:18:05 +01005686 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005687 }
Hanno Beckerb309b922018-08-23 13:18:05 +01005688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005689 if (hs_buf->is_valid == 1) {
Hanno Beckere605b192018-08-21 15:59:07 +01005690 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005691 mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len);
5692 mbedtls_free(hs_buf->data);
5693 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Hanno Becker0271f962018-08-16 13:23:47 +01005694 }
5695}
5696
5697#endif /* MBEDTLS_SSL_PROTO_DTLS */
5698
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005699/*
5700 * Convert version numbers to/from wire format
5701 * and, for DTLS, to/from TLS equivalent.
5702 *
5703 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005704 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005705 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5706 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5707 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005708void mbedtls_ssl_write_version(int major, int minor, int transport,
5709 unsigned char ver[2])
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005712 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5713 if (minor == MBEDTLS_SSL_MINOR_VERSION_2) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005714 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005716 }
5717 ver[0] = (unsigned char) (255 - (major - 2));
5718 ver[1] = (unsigned char) (255 - (minor - 1));
5719 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005720#else
5721 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005722#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005723 {
5724 ver[0] = (unsigned char) major;
5725 ver[1] = (unsigned char) minor;
5726 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005727}
5728
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005729void mbedtls_ssl_read_version(int *major, int *minor, int transport,
5730 const unsigned char ver[2])
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005733 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005734 *major = 255 - ver[0] + 2;
5735 *minor = 255 - ver[1] + 1;
5736
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005737 if (*minor == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005738 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005739 }
5740 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005741#else
5742 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005743#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005744 {
5745 *major = ver[0];
5746 *minor = ver[1];
5747 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005748}
5749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005750#endif /* MBEDTLS_SSL_TLS_C */