blob: 5e85679593171ae58bce8697220eb9917c0a9020 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Hanno Beckerf1a38282020-02-05 16:14:29 +00002 * Generic SSL/TLS messaging layer functions
3 * (record layer + retransmission state machine)
Paul Bakker5121ce52009-01-03 21:22:43 +00004 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00007 */
8/*
9 * The SSL 3.0 specification was drafted by Netscape in 1996,
10 * and became an IETF standard in 1999.
11 *
12 * http://wp.netscape.com/eng/ssl3/
13 * http://www.ietf.org/rfc/rfc2246.txt
14 * http://www.ietf.org/rfc/rfc4346.txt
15 */
16
Gilles Peskinedb09ef62020-06-03 01:43:33 +020017#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000020
SimonBd5800b72016-04-26 07:43:27 +010021#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010022
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020024#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000025#include "mbedtls/debug.h"
26#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010028#include "mbedtls/version.h"
Gabor Mezeic0ae1cf2021-10-20 12:09:35 +020029#include "constant_time_internal.h"
Gabor Mezeie24dea82021-10-19 12:22:25 +020030#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <string.h>
33
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050034#if defined(MBEDTLS_USE_PSA_CRYPTO)
35#include "mbedtls/psa_util.h"
36#include "psa/crypto.h"
37#endif
38
Janos Follath23bdca02016-10-07 14:47:14 +010039#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020041#endif
42
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +010044
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020045/*
46 * Start a timer.
47 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020048 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 if (ssl->f_set_timer == NULL) {
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020052 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 }
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs));
56 ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs);
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020057}
58
59/*
60 * Return -1 is timer is expired, 0 if it isn't.
61 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020063{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 if (ssl->f_get_timer == NULL) {
65 return 0;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020066 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 if (ssl->f_get_timer(ssl->p_timer) == 2) {
69 MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired"));
70 return -1;
71 }
72
73 return 0;
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020075
Hanno Beckercfe45792019-07-03 16:13:00 +010076#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +020077MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
79 unsigned char *buf,
80 size_t len,
81 mbedtls_record *rec);
Hanno Becker54229812019-07-12 14:40:00 +010082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl,
84 unsigned char *buf,
85 size_t buflen)
Hanno Beckercfe45792019-07-03 16:13:00 +010086{
Hanno Becker54229812019-07-12 14:40:00 +010087 int ret = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record"));
89 MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen);
Hanno Becker54229812019-07-12 14:40:00 +010090
91 /* We don't support record checking in TLS because
92 * (a) there doesn't seem to be a usecase for it, and
93 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
94 * and we'd need to backup the transform here.
95 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) {
Hanno Becker54229812019-07-12 14:40:00 +010097 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
98 goto exit;
99 }
100#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 else {
irwir734f0cf2019-09-26 21:03:24 +0300102 mbedtls_record rec;
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 ret = ssl_parse_record_header(ssl, buf, buflen, &rec);
105 if (ret != 0) {
106 MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret);
Hanno Becker54229812019-07-12 14:40:00 +0100107 goto exit;
108 }
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if (ssl->transform_in != NULL) {
111 ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec);
112 if (ret != 0) {
113 MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret);
Hanno Becker54229812019-07-12 14:40:00 +0100114 goto exit;
115 }
116 }
117 }
118#endif /* MBEDTLS_SSL_PROTO_DTLS */
119
120exit:
121 /* On success, we have decrypted the buffer in-place, so make
122 * sure we don't leak any plaintext data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 mbedtls_platform_zeroize(buf, buflen);
Hanno Becker54229812019-07-12 14:40:00 +0100124
125 /* For the purpose of this API, treat messages with unexpected CID
126 * as well as such from future epochs as unexpected. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
128 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker54229812019-07-12 14:40:00 +0100129 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
130 }
131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record"));
133 return ret;
Hanno Beckercfe45792019-07-03 16:13:00 +0100134}
135#endif /* MBEDTLS_SSL_RECORD_CHECKING */
136
Hanno Becker67bc7c32018-08-06 11:33:50 +0100137#define SSL_DONT_FORCE_FLUSH 0
138#define SSL_FORCE_FLUSH 1
139
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100141
Hanno Beckerd5847772018-08-28 10:09:23 +0100142/* Forward declarations for functions related to message buffering. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
144 uint8_t slot);
145static void ssl_free_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200146MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147static int ssl_load_buffered_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200148MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149static int ssl_load_buffered_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151static int ssl_buffer_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200152MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
154 mbedtls_record const *rec);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200155MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl);
Hanno Beckerd5847772018-08-28 10:09:23 +0100157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100159{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
Darryl Greenb33cc762019-11-28 14:29:44 +0000161#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
162 size_t out_buf_len = ssl->out_buf_len;
163#else
164 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
165#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 if (mtu != 0 && mtu < out_buf_len) {
168 return mtu;
169 }
Hanno Becker2b1e3542018-08-06 11:19:13 +0100170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 return out_buf_len;
Hanno Becker2b1e3542018-08-06 11:19:13 +0100172}
173
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200174MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100176{
Hanno Becker11682cc2018-08-22 14:41:02 +0100177 size_t const bytes_written = ssl->out_left;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 size_t const mtu = ssl_get_maximum_datagram_size(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100179
180 /* Double-check that the write-index hasn't gone
181 * past what we can transmit in a single datagram. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (bytes_written > mtu) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100183 /* Should never happen... */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100185 }
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 return (int) (mtu - bytes_written);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100188}
189
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200190MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl)
Hanno Becker67bc7c32018-08-06 11:33:50 +0100192{
Janos Follath865b3eb2019-12-16 11:46:15 +0000193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400195 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100196
197#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Hanno Becker67bc7c32018-08-06 11:33:50 +0100199
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 if (max_len > mfl) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100201 max_len = mfl;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100203
204 /* By the standard (RFC 6066 Sect. 4), the MFL extension
205 * only limits the maximum record payload size, so in theory
206 * we would be allowed to pack multiple records of payload size
207 * MFL into a single datagram. However, this would mean that there's
208 * no way to explicitly communicate MTU restrictions to the peer.
209 *
210 * The following reduction of max_len makes sure that we never
211 * write datagrams larger than MFL + Record Expansion Overhead.
212 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 if (max_len <= ssl->out_left) {
214 return 0;
215 }
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100216
217 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100218#endif
219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 ret = ssl_get_remaining_space_in_datagram(ssl);
221 if (ret < 0) {
222 return ret;
223 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100224 remaining = (size_t) ret;
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 ret = mbedtls_ssl_get_record_expansion(ssl);
227 if (ret < 0) {
228 return ret;
229 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100230 expansion = (size_t) ret;
231
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 if (remaining <= expansion) {
233 return 0;
234 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235
236 remaining -= expansion;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (remaining >= max_len) {
Hanno Becker67bc7c32018-08-06 11:33:50 +0100238 remaining = max_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 }
Hanno Becker67bc7c32018-08-06 11:33:50 +0100240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 return (int) remaining;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100242}
243
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200244/*
245 * Double the retransmit timeout value, within the allowed range,
246 * returning -1 if the maximum value has already been reached.
247 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200250{
251 uint32_t new_timeout;
252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) {
254 return -1;
255 }
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200256
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200257 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
258 * in the following way: after the initial transmission and a first
259 * retransmission, back off to a temporary estimated MTU of 508 bytes.
260 * This value is guaranteed to be deliverable (if not guaranteed to be
261 * delivered) of any compliant IPv4 (and IPv6) network, and should work
262 * on most non-IP stacks too. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200264 ssl->handshake->mtu = 508;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu));
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400266 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200267
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200268 new_timeout = 2 * ssl->handshake->retransmit_timeout;
269
270 /* Avoid arithmetic overflow and range overflow */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 if (new_timeout < ssl->handshake->retransmit_timeout ||
272 new_timeout > ssl->conf->hs_timeout_max) {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200273 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200274 }
275
276 ssl->handshake->retransmit_timeout = new_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
278 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return 0;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281}
282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200285 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs",
287 (unsigned long) ssl->handshake->retransmit_timeout));
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl,
293 const unsigned char *key_enc, const unsigned char *key_dec,
294 size_t keylen,
295 const unsigned char *iv_enc, const unsigned char *iv_dec,
296 size_t ivlen,
297 const unsigned char *mac_enc, const unsigned char *mac_dec,
298 size_t maclen) = NULL;
299int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction) = NULL;
300int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl) = NULL;
301int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl) = NULL;
302int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl) = NULL;
303int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000305
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100306/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200308 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000309
Hanno Beckerccc13d02020-05-04 12:30:04 +0100310#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
311 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313static size_t ssl_compute_padding_length(size_t len,
314 size_t granularity)
Hanno Becker13996922020-05-28 16:15:19 +0100315{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 return (granularity - (len + 1) % granularity) % granularity;
Hanno Becker13996922020-05-28 16:15:19 +0100317}
318
Hanno Becker581bc1b2020-05-04 12:20:03 +0100319/* This functions transforms a (D)TLS plaintext fragment and a record content
320 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
321 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
322 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100323 *
324 * struct {
325 * opaque content[DTLSPlaintext.length];
326 * ContentType real_type;
327 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100328 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100329 *
330 * Input:
331 * - `content`: The beginning of the buffer holding the
332 * plaintext to be wrapped.
333 * - `*content_size`: The length of the plaintext in Bytes.
334 * - `max_len`: The number of Bytes available starting from
335 * `content`. This must be `>= *content_size`.
336 * - `rec_type`: The desired record content type.
337 *
338 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100339 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
340 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100341 *
342 * Returns:
343 * - `0` on success.
344 * - A negative error code if `max_len` didn't offer enough space
345 * for the expansion.
346 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200347MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348static int ssl_build_inner_plaintext(unsigned char *content,
349 size_t *content_size,
350 size_t remaining,
351 uint8_t rec_type,
352 size_t pad)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100353{
354 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100355
356 /* Write real content type */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 if (remaining == 0) {
358 return -1;
359 }
360 content[len] = rec_type;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100361 len++;
362 remaining--;
363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 if (remaining < pad) {
365 return -1;
366 }
367 memset(content + len, 0, pad);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100368 len += pad;
369 remaining -= pad;
370
371 *content_size = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100373}
374
Hanno Becker581bc1b2020-05-04 12:20:03 +0100375/* This function parses a (D)TLSInnerPlaintext structure.
376 * See ssl_build_inner_plaintext() for details. */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200377MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378static int ssl_parse_inner_plaintext(unsigned char const *content,
379 size_t *content_size,
380 uint8_t *rec_type)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100381{
382 size_t remaining = *content_size;
383
384 /* Determine length of padding by skipping zeroes from the back. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 do {
386 if (remaining == 0) {
387 return -1;
388 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100389 remaining--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100390 } while (content[remaining] == 0);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100391
392 *content_size = remaining;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 *rec_type = content[remaining];
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return 0;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100396}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100397#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
398 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100399
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100400/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100401 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100402static void ssl_extract_add_data_from_record(unsigned char *add_data,
403 size_t *add_data_len,
404 mbedtls_record *rec,
405 unsigned minor_ver)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000406{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100407 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100408 *
409 * additional_data = seq_num + TLSCompressed.type +
410 * TLSCompressed.version + TLSCompressed.length;
411 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100412 * For the CID extension, this is extended as follows
413 * (quoting draft-ietf-tls-dtls-connection-id-05,
414 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100415 *
416 * additional_data = seq_num + DTLSPlaintext.type +
417 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100418 * cid +
419 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100420 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100421 *
422 * For TLS 1.3, the record sequence number is dropped from the AAD
423 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100424 */
425
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100426 unsigned char *cur = add_data;
427
David Horstmann197b2402022-10-26 18:06:31 +0100428 int is_tls13 = 0;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100429#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
David Horstmann197b2402022-10-26 18:06:31 +0100431 is_tls13 = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100433#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if (!is_tls13) {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100435 ((void) minor_ver);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 memcpy(cur, rec->ctr, sizeof(rec->ctr));
437 cur += sizeof(rec->ctr);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100438 }
439
440 *cur = rec->type;
441 cur++;
442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 memcpy(cur, rec->ver, sizeof(rec->ver));
444 cur += sizeof(rec->ver);
Hanno Beckercab87e62019-04-29 13:52:53 +0100445
Hanno Beckera0e20d02019-05-15 14:03:01 +0100446#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 if (rec->cid_len != 0) {
448 memcpy(cur, rec->cid, rec->cid_len);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100449 cur += rec->cid_len;
450
451 *cur = rec->cid_len;
452 cur++;
453
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100455 cur += 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100457#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100458 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100460 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100461 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100462
463 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000464}
465
Hanno Becker9d062f92020-02-07 10:26:36 +0000466#if defined(MBEDTLS_SSL_PROTO_SSL3)
467
468#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
469
470/*
471 * SSLv3.0 MAC functions
472 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200473MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474static int ssl_mac(mbedtls_md_context_t *md_ctx,
475 const unsigned char *secret,
476 const unsigned char *buf, size_t len,
477 const unsigned char *ctr, int type,
478 unsigned char out[SSL3_MAC_MAX_BYTES])
Hanno Becker9d062f92020-02-07 10:26:36 +0000479{
480 unsigned char header[11];
481 unsigned char padding[48];
482 int padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 int md_size = mbedtls_md_get_size(md_ctx->md_info);
484 int md_type = mbedtls_md_get_type(md_ctx->md_info);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9d062f92020-02-07 10:26:36 +0000486
487 /* Only MD5 and SHA-1 supported */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100488 if (md_type == MBEDTLS_MD_MD5) {
Hanno Becker9d062f92020-02-07 10:26:36 +0000489 padlen = 48;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100490 } else {
Hanno Becker9d062f92020-02-07 10:26:36 +0000491 padlen = 40;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 }
Hanno Becker9d062f92020-02-07 10:26:36 +0000493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100494 memcpy(header, ctr, 8);
Joe Subbiania651e6f2021-08-23 11:35:25 +0100495 header[8] = (unsigned char) type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100496 MBEDTLS_PUT_UINT16_BE(len, header, 9);
Hanno Becker9d062f92020-02-07 10:26:36 +0000497
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100498 memset(padding, 0x36, padlen);
499 ret = mbedtls_md_starts(md_ctx);
500 if (ret != 0) {
501 return ret;
502 }
503 ret = mbedtls_md_update(md_ctx, secret, md_size);
504 if (ret != 0) {
505 return ret;
506 }
507 ret = mbedtls_md_update(md_ctx, padding, padlen);
508 if (ret != 0) {
509 return ret;
510 }
511 ret = mbedtls_md_update(md_ctx, header, 11);
512 if (ret != 0) {
513 return ret;
514 }
515 ret = mbedtls_md_update(md_ctx, buf, len);
516 if (ret != 0) {
517 return ret;
518 }
519 ret = mbedtls_md_finish(md_ctx, out);
520 if (ret != 0) {
521 return ret;
522 }
Hanno Becker9d062f92020-02-07 10:26:36 +0000523
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 memset(padding, 0x5C, padlen);
525 ret = mbedtls_md_starts(md_ctx);
526 if (ret != 0) {
527 return ret;
528 }
529 ret = mbedtls_md_update(md_ctx, secret, md_size);
530 if (ret != 0) {
531 return ret;
532 }
533 ret = mbedtls_md_update(md_ctx, padding, padlen);
534 if (ret != 0) {
535 return ret;
536 }
537 ret = mbedtls_md_update(md_ctx, out, md_size);
538 if (ret != 0) {
539 return ret;
540 }
541 ret = mbedtls_md_finish(md_ctx, out);
542 if (ret != 0) {
543 return ret;
544 }
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 return 0;
Hanno Becker9d062f92020-02-07 10:26:36 +0000547}
548#endif /* MBEDTLS_SSL_PROTO_SSL3 */
549
Hanno Becker67a37db2020-05-28 16:27:07 +0100550#if defined(MBEDTLS_GCM_C) || \
551 defined(MBEDTLS_CCM_C) || \
552 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +0200553MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker17263802020-05-28 07:05:48 +0100554static int ssl_transform_aead_dynamic_iv_is_explicit(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 mbedtls_ssl_transform const *transform)
Hanno Beckerdf8be222020-05-21 15:30:57 +0100556{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100557 return transform->ivlen != transform->fixed_ivlen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100558}
559
Hanno Becker17263802020-05-28 07:05:48 +0100560/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
561 *
562 * Concretely, this occurs in two variants:
563 *
564 * a) Fixed and dynamic IV lengths add up to total IV length, giving
565 * IV = fixed_iv || dynamic_iv
566 *
Hanno Becker15952812020-06-04 13:31:46 +0100567 * This variant is used in TLS 1.2 when used with GCM or CCM.
568 *
Hanno Becker17263802020-05-28 07:05:48 +0100569 * b) Fixed IV lengths matches total IV length, giving
570 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100571 *
572 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
573 *
574 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100575 *
576 * This function has the precondition that
577 *
578 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
579 *
580 * which has to be ensured by the caller. If this precondition
581 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100582 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100583static void ssl_build_record_nonce(unsigned char *dst_iv,
584 size_t dst_iv_len,
585 unsigned char const *fixed_iv,
586 size_t fixed_iv_len,
587 unsigned char const *dynamic_iv,
588 size_t dynamic_iv_len)
Hanno Becker17263802020-05-28 07:05:48 +0100589{
590 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100591
592 /* Start with Fixed IV || 0 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100593 memset(dst_iv, 0, dst_iv_len);
594 memcpy(dst_iv, fixed_iv, fixed_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100595
Hanno Becker17263802020-05-28 07:05:48 +0100596 dst_iv += dst_iv_len - dynamic_iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100597 for (i = 0; i < dynamic_iv_len; i++) {
Hanno Becker17263802020-05-28 07:05:48 +0100598 dst_iv[i] ^= dynamic_iv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100599 }
Hanno Beckerdf8be222020-05-21 15:30:57 +0100600}
Hanno Becker67a37db2020-05-28 16:27:07 +0100601#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100602
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100603int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
604 mbedtls_ssl_transform *transform,
605 mbedtls_record *rec,
606 int (*f_rng)(void *, unsigned char *, size_t),
607 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +0000608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100610 int auth_done = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100611 unsigned char *data;
612 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX];
Hanno Beckercab87e62019-04-29 13:52:53 +0100613 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000614 size_t post_avail;
615
616 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000617#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200618 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000619 ((void) ssl);
620#endif
621
622 /* The PRNG is used for dynamic IV generation that's used
623 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100624#if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
625 (defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)))
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000626 ((void) f_rng);
627 ((void) p_rng);
628#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100630 MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100632 if (transform == NULL) {
633 MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf"));
634 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000635 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100636 if (rec == NULL
Hanno Becker43c24b82019-05-01 09:45:57 +0100637 || rec->buf == NULL
638 || rec->buf_len < rec->data_offset
639 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100640#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100641 || rec->cid_len != 0
642#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100643 ) {
644 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf"));
645 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100646 }
647
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000648 data = rec->buf + rec->data_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100649 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
650 MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload",
651 data, rec->data_len);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000652
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100653 mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc);
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000654
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100655 if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
656 MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET
657 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
658 rec->data_len,
659 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000661 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100662
Hanno Becker92313402020-05-20 13:58:58 +0100663 /* The following two code paths implement the (D)TLSInnerPlaintext
664 * structure present in TLS 1.3 and DTLS 1.2 + CID.
665 *
666 * See ssl_build_inner_plaintext() for more information.
667 *
668 * Note that this changes `rec->data_len`, and hence
669 * `post_avail` needs to be recalculated afterwards.
670 *
671 * Note also that the two code paths cannot occur simultaneously
672 * since they apply to different versions of the protocol. There
673 * is hence no risk of double-addition of the inner plaintext.
674 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100675#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100676 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Hanno Becker13996922020-05-28 16:15:19 +0100677 size_t padding =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100678 ssl_compute_padding_length(rec->data_len,
679 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY);
680 if (ssl_build_inner_plaintext(data,
681 &rec->data_len,
682 post_avail,
683 rec->type,
684 padding) != 0) {
685 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerccc13d02020-05-04 12:30:04 +0100686 }
687
688 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
689 }
690#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
691
Hanno Beckera0e20d02019-05-15 14:03:01 +0100692#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100693 /*
694 * Add CID information
695 */
696 rec->cid_len = transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100697 memcpy(rec->cid, transform->out_cid, transform->out_cid_len);
698 MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100699
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100700 if (rec->cid_len != 0) {
Hanno Becker13996922020-05-28 16:15:19 +0100701 size_t padding =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100702 ssl_compute_padding_length(rec->data_len,
703 MBEDTLS_SSL_CID_PADDING_GRANULARITY);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100704 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100705 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100706 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100707 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100708 * Note that this changes `rec->data_len`, and hence
709 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100710 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100711 if (ssl_build_inner_plaintext(data,
712 &rec->data_len,
713 post_avail,
714 rec->type,
715 padding) != 0) {
716 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100717 }
718
719 rec->type = MBEDTLS_SSL_MSG_CID;
720 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100721#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100722
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100723 post_avail = rec->buf_len - (rec->data_len + rec->data_offset);
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100724
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100726 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 */
Hanno Becker52344c22018-01-03 15:24:20 +0000728#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100729 if (mode == MBEDTLS_MODE_STREAM ||
730 (mode == MBEDTLS_MODE_CBC
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100732 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100733#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100734 )) {
735 if (post_avail < transform->maclen) {
736 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
737 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000738 }
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100741 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Hanno Becker9d062f92020-02-07 10:26:36 +0000742 unsigned char mac[SSL3_MAC_MAX_BYTES];
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100744 ret = ssl_mac(&transform->md_ctx_enc, transform->mac_enc,
745 data, rec->data_len, rec->ctr, rec->type, mac);
746 if (ret == 0) {
747 memcpy(data + rec->data_len, mac, transform->maclen);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100748 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100749 mbedtls_platform_zeroize(mac, transform->maclen);
750 if (ret != 0) {
751 MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret);
752 return ret;
753 }
754 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
757 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100758 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
Hanno Becker992b6872017-11-09 18:57:39 +0000759 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker992b6872017-11-09 18:57:39 +0000761
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100762 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
763 transform->minor_ver);
Hanno Becker992b6872017-11-09 18:57:39 +0000764
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100765 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
766 add_data, add_data_len);
767 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100768 goto hmac_failed_etm_disabled;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100769 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100770 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
771 data, rec->data_len);
772 if (ret != 0) {
773 goto hmac_failed_etm_disabled;
774 }
775 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
776 if (ret != 0) {
777 goto hmac_failed_etm_disabled;
778 }
779 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
780 if (ret != 0) {
781 goto hmac_failed_etm_disabled;
782 }
783
784 memcpy(data + rec->data_len, mac, transform->maclen);
785
786hmac_failed_etm_disabled:
787 mbedtls_platform_zeroize(mac, transform->maclen);
788 if (ret != 0) {
789 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
790 return ret;
791 }
792 } else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200793#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200794 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100795 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
796 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200797 }
798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100799 MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len,
800 transform->maclen);
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200801
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000802 rec->data_len += transform->maclen;
803 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100804 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200805 }
Hanno Becker52344c22018-01-03 15:24:20 +0000806#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200808 /*
809 * Encrypt
810 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100812 if (mode == MBEDTLS_MODE_STREAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000814 size_t olen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100815 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
816 "including %d bytes of padding",
817 rec->data_len, 0));
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100819 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
820 transform->iv_enc, transform->ivlen,
821 data, rec->data_len,
822 data, &olen)) != 0) {
823 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
824 return ret;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200825 }
826
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100827 if (rec->data_len != olen) {
828 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
829 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200830 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100831 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000833
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200834#if defined(MBEDTLS_GCM_C) || \
835 defined(MBEDTLS_CCM_C) || \
836 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100837 if (mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200838 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100839 mode == MBEDTLS_MODE_CHACHAPOLY) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200841 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100842 unsigned char *dynamic_iv;
843 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100844 int dynamic_iv_is_explicit =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100845 ssl_transform_aead_dynamic_iv_is_explicit(transform);
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100847 /* Check that there's space for the authentication tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100848 if (post_avail < transform->taglen) {
849 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
850 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000851 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000852
Paul Bakker68884e32013-01-07 18:20:04 +0100853 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100854 * Build nonce for AEAD encryption.
855 *
856 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
857 * part of the IV is prepended to the ciphertext and
858 * can be chosen freely - in particular, it need not
859 * agree with the record sequence number.
860 * However, since ChaChaPoly as well as all AEAD modes
861 * in TLS 1.3 use the record sequence number as the
862 * dynamic part of the nonce, we uniformly use the
863 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100864 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100865 dynamic_iv = rec->ctr;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100866 dynamic_iv_len = sizeof(rec->ctr);
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100868 ssl_build_record_nonce(iv, sizeof(iv),
869 transform->iv_enc,
870 transform->fixed_ivlen,
871 dynamic_iv,
872 dynamic_iv_len);
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100873
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100874 /*
875 * Build additional data for AEAD encryption.
876 * This depends on the TLS version.
877 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100878 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
879 transform->minor_ver);
Hanno Becker1f10d762019-04-26 13:34:37 +0100880
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100881 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)",
882 iv, transform->ivlen);
883 MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)",
884 dynamic_iv,
885 dynamic_iv_is_explicit ? dynamic_iv_len : 0);
886 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
887 add_data, add_data_len);
888 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
889 "including 0 bytes of padding",
890 rec->data_len));
Paul Bakkerca4ab492012-04-18 14:23:57 +0000891
Paul Bakker68884e32013-01-07 18:20:04 +0100892 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200893 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200894 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000895
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100896 if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc,
897 iv, transform->ivlen,
898 add_data, add_data_len,
899 data, rec->data_len, /* src */
900 data, rec->buf_len - (data - rec->buf), /* dst */
901 &rec->data_len,
902 transform->taglen)) != 0) {
903 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt", ret);
904 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200905 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100906 MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
907 data + rec->data_len - transform->taglen,
908 transform->taglen);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100909 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000910 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100911
912 /*
913 * Prefix record content with dynamic IV in case it is explicit.
914 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100915 if (dynamic_iv_is_explicit != 0) {
916 if (rec->data_offset < dynamic_iv_len) {
917 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
918 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100919 }
920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100921 memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len);
Hanno Beckerdf8be222020-05-21 15:30:57 +0100922 rec->data_offset -= dynamic_iv_len;
923 rec->data_len += dynamic_iv_len;
924 }
925
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100926 auth_done++;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100927 } else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100928#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200929#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100930 if (mode == MBEDTLS_MODE_CBC) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000932 size_t padlen, i;
933 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000934
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000935 /* Currently we're always using minimal padding
936 * (up to 255 bytes would be allowed). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100937 padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen;
938 if (padlen == transform->ivlen) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 padlen = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000940 }
941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100942 /* Check there's enough space in the buffer for the padding. */
943 if (post_avail < padlen + 1) {
944 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
945 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
946 }
947
948 for (i = 0; i <= padlen; i++) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000949 data[rec->data_len + i] = (unsigned char) padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100950 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000952 rec->data_len += padlen + 1;
953 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000956 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000957 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
958 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000959 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100960 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
961 if (f_rng == NULL) {
962 MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine"));
963 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000964 }
965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100966 if (rec->data_offset < transform->ivlen) {
967 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
968 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000969 }
970
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000971 /*
972 * Generate IV
973 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100974 ret = f_rng(p_rng, transform->iv_enc, transform->ivlen);
975 if (ret != 0) {
976 return ret;
977 }
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000978
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100979 memcpy(data - transform->ivlen, transform->iv_enc,
980 transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000981
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000982 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100985 MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
986 "including %"
987 MBEDTLS_PRINTF_SIZET
988 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
989 rec->data_len, transform->ivlen,
990 padlen + 1));
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100992 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
993 transform->iv_enc,
994 transform->ivlen,
995 data, rec->data_len,
996 data, &olen)) != 0) {
997 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
998 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +0200999 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001000
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001001 if (rec->data_len != olen) {
1002 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1003 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001004 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001007 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
Paul Bakkercca5b812013-08-31 17:40:26 +02001008 /*
1009 * Save IV in SSL3 and TLS1
1010 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001011 memcpy(transform->iv_enc, transform->cipher_ctx_enc.iv,
1012 transform->ivlen);
1013 } else
Paul Bakkercca5b812013-08-31 17:40:26 +02001014#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 {
1016 data -= transform->ivlen;
1017 rec->data_offset -= transform->ivlen;
1018 rec->data_len += transform->ivlen;
1019 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001022 if (auth_done == 0) {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001023 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1024
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001025 /*
1026 * MAC(MAC_write_key, seq_num +
1027 * TLSCipherText.type +
1028 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001029 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001030 * IV + // except for TLS 1.0
1031 * ENC(content + padding + padding_length));
1032 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001034 if (post_avail < transform->maclen) {
1035 MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough"));
1036 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001037 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001039 ssl_extract_add_data_from_record(add_data, &add_data_len,
1040 rec, transform->minor_ver);
Hanno Becker1f10d762019-04-26 13:34:37 +01001041
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001042 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
1043 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1044 add_data_len);
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001045
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001046 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
1047 add_data_len);
1048 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001049 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001050 }
1051 ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
1052 data, rec->data_len);
1053 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001054 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001055 }
1056 ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
1057 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001058 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001059 }
1060 ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
1061 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001062 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001063 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001065 memcpy(data + rec->data_len, mac, transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001066
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001067 rec->data_len += transform->maclen;
1068 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001069 auth_done++;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001071hmac_failed_etm_enabled:
1072 mbedtls_platform_zeroize(mac, transform->maclen);
1073 if (ret != 0) {
1074 MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
1075 return ret;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001076 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001079 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001080#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001081 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001082 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1083 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001086 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001087 if (auth_done != 1) {
1088 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1089 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001090 }
1091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001092 MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001094 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001095}
1096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001097int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
1098 mbedtls_ssl_transform *transform,
1099 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00001100{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001101 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001103 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001104#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001105 size_t padlen = 0, correct = 1;
1106#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001107 unsigned char *data;
1108 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX];
Hanno Beckercab87e62019-04-29 13:52:53 +01001109 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001110
Hanno Beckera18d1322018-01-03 14:27:32 +00001111#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001112 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001113 ((void) ssl);
1114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001116 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf"));
1117 if (rec == NULL ||
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001118 rec->buf == NULL ||
1119 rec->buf_len < rec->data_offset ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001120 rec->buf_len - rec->data_offset < rec->data_len) {
1121 MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf"));
1122 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001123 }
1124
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001125 data = rec->buf + rec->data_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001126 mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_dec);
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Hanno Beckera0e20d02019-05-15 14:03:01 +01001128#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001129 /*
1130 * Match record's CID with incoming CID.
1131 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001132 if (rec->cid_len != transform->in_cid_len ||
1133 memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) {
1134 return MBEDTLS_ERR_SSL_UNEXPECTED_CID;
Hanno Becker938489a2019-05-08 13:02:22 +01001135 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001136#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001139 if (mode == MBEDTLS_MODE_STREAM) {
Gilles Peskine326ba3c2023-09-18 14:08:11 +02001140 if (rec->data_len < transform->maclen) {
1141 MBEDTLS_SSL_DEBUG_MSG(1,
1142 ("Record too short for MAC:"
1143 " %" MBEDTLS_PRINTF_SIZET " < %" MBEDTLS_PRINTF_SIZET,
1144 rec->data_len, transform->maclen));
1145 return MBEDTLS_ERR_SSL_INVALID_MAC;
1146 }
1147
Paul Bakker68884e32013-01-07 18:20:04 +01001148 padlen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001149 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1150 transform->iv_dec,
1151 transform->ivlen,
1152 data, rec->data_len,
1153 data, &olen)) != 0) {
1154 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1155 return ret;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001156 }
1157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001158 if (rec->data_len != olen) {
1159 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1160 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001161 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001162 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001164#if defined(MBEDTLS_GCM_C) || \
1165 defined(MBEDTLS_CCM_C) || \
1166 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001167 if (mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001168 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001169 mode == MBEDTLS_MODE_CHACHAPOLY) {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001170 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001171 unsigned char *dynamic_iv;
1172 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001173
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001174 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001175 * Extract dynamic part of nonce for AEAD decryption.
1176 *
1177 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1178 * part of the IV is prepended to the ciphertext and
1179 * can be chosen freely - in particular, it need not
1180 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001182 dynamic_iv_len = sizeof(rec->ctr);
1183 if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) {
1184 if (rec->data_len < dynamic_iv_len) {
1185 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1186 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
1187 rec->data_len,
1188 dynamic_iv_len));
1189 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001190 }
1191 dynamic_iv = data;
1192
1193 data += dynamic_iv_len;
1194 rec->data_offset += dynamic_iv_len;
1195 rec->data_len -= dynamic_iv_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001196 } else {
Hanno Becker17263802020-05-28 07:05:48 +01001197 dynamic_iv = rec->ctr;
1198 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001199
1200 /* Check that there's space for the authentication tag. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001201 if (rec->data_len < transform->taglen) {
1202 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1203 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
1204 rec->data_len,
1205 transform->taglen));
1206 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001207 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001208 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001209
Hanno Beckerdf8be222020-05-21 15:30:57 +01001210 /*
1211 * Prepare nonce from dynamic and static parts.
1212 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001213 ssl_build_record_nonce(iv, sizeof(iv),
1214 transform->iv_dec,
1215 transform->fixed_ivlen,
1216 dynamic_iv,
1217 dynamic_iv_len);
Paul Bakker68884e32013-01-07 18:20:04 +01001218
Hanno Beckerdf8be222020-05-21 15:30:57 +01001219 /*
1220 * Build additional data for AEAD encryption.
1221 * This depends on the TLS version.
1222 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001223 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1224 transform->minor_ver);
1225 MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD",
1226 add_data, add_data_len);
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001227
Hanno Beckerd96a6522019-07-10 13:55:25 +01001228 /* Because of the check above, we know that there are
Shaun Case0e7791f2021-12-20 21:14:10 -08001229 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001230 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001231 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001232 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001233
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001234 MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen);
1235 MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len,
1236 transform->taglen);
Paul Bakker68884e32013-01-07 18:20:04 +01001237
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001238 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001239 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001240 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001241 if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec,
1242 iv, transform->ivlen,
1243 add_data, add_data_len,
1244 data, rec->data_len + transform->taglen, /* src */
1245 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
1246 transform->taglen)) != 0) {
1247 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt", ret);
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001249 if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
1250 return MBEDTLS_ERR_SSL_INVALID_MAC;
1251 }
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001253 return ret;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001254 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001255 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001256
Hanno Beckerd96a6522019-07-10 13:55:25 +01001257 /* Double-check that AEAD decryption doesn't change content length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001258 if (olen != rec->data_len) {
1259 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1260 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001262 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001264#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001265 if (mode == MBEDTLS_MODE_CBC) {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001266 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001267
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 /*
Paul Bakker45829992013-01-03 14:52:21 +01001269 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001272 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001273 /* The ciphertext is prefixed with the CBC IV. */
1274 minlen += transform->ivlen;
1275 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001276#endif
Paul Bakker45829992013-01-03 14:52:21 +01001277
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001278 /* Size considerations:
1279 *
1280 * - The CBC cipher text must not be empty and hence
1281 * at least of size transform->ivlen.
1282 *
1283 * Together with the potential IV-prefix, this explains
1284 * the first of the two checks below.
1285 *
1286 * - The record must contain a MAC, either in plain or
1287 * encrypted, depending on whether Encrypt-then-MAC
1288 * is used or not.
1289 * - If it is, the message contains the IV-prefix,
1290 * the CBC ciphertext, and the MAC.
1291 * - If it is not, the padded plaintext, and hence
1292 * the CBC ciphertext, has at least length maclen + 1
1293 * because there is at least the padding length byte.
1294 *
1295 * As the CBC ciphertext is not empty, both cases give the
1296 * lower bound minlen + maclen + 1 on the record size, which
1297 * we test for in the second check below.
1298 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001299 if (rec->data_len < minlen + transform->ivlen ||
1300 rec->data_len < minlen + transform->maclen + 1) {
1301 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1302 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1303 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
1304 "+ 1 ) ( + expl IV )",
1305 rec->data_len,
1306 transform->ivlen,
1307 transform->maclen));
1308 return MBEDTLS_ERR_SSL_INVALID_MAC;
Paul Bakker45829992013-01-03 14:52:21 +01001309 }
1310
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001311 /*
1312 * Authenticate before decrypt if enabled
1313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001315 if (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) {
Hanno Becker992b6872017-11-09 18:57:39 +00001316 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001318 MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001319
Hanno Beckerd96a6522019-07-10 13:55:25 +01001320 /* Update data_len in tandem with add_data.
1321 *
1322 * The subtraction is safe because of the previous check
1323 * data_len >= minlen + maclen + 1.
1324 *
1325 * Afterwards, we know that data + data_len is followed by at
1326 * least maclen Bytes, which justifies the call to
Gabor Mezei18a44942021-10-20 11:59:27 +02001327 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001328 *
1329 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001330 rec->data_len -= transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001331 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1332 transform->minor_ver);
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001333
Hanno Beckerd96a6522019-07-10 13:55:25 +01001334 /* Calculate expected MAC. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001335 MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
1336 add_data_len);
1337 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
1338 add_data_len);
1339 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001340 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001341 }
1342 ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
1343 data, rec->data_len);
1344 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001345 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001346 }
1347 ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
1348 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001349 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001350 }
1351 ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
1352 if (ret != 0) {
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001353 goto hmac_failed_etm_enabled;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001354 }
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001355
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001356 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
1357 transform->maclen);
1358 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
1359 transform->maclen);
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001360
Hanno Beckerd96a6522019-07-10 13:55:25 +01001361 /* Compare expected MAC with MAC at the end of the record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001362 if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
1363 transform->maclen) != 0) {
1364 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Gilles Peskined8e2e832021-12-10 21:33:21 +01001365 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1366 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001367 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001368 auth_done++;
Gilles Peskined8e2e832021-12-10 21:33:21 +01001369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001370hmac_failed_etm_enabled:
1371 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1372 if (ret != 0) {
1373 if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
1374 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
1375 }
1376 return ret;
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001377 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001378 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001380
1381 /*
1382 * Check length sanity
1383 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001384
1385 /* We know from above that data_len > minlen >= 0,
1386 * so the following check in particular implies that
1387 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001388 if (rec->data_len % transform->ivlen != 0) {
1389 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1390 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
1391 rec->data_len, transform->ivlen));
1392 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001393 }
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001396 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001397 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001398 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001399 if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001400 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001401 memcpy(transform->iv_dec, data, transform->ivlen);
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001402
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001403 data += transform->ivlen;
1404 rec->data_offset += transform->ivlen;
1405 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001406 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001408
Hanno Beckerd96a6522019-07-10 13:55:25 +01001409 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1410
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001411 if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
1412 transform->iv_dec, transform->ivlen,
1413 data, rec->data_len, data, &olen)) != 0) {
1414 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
1415 return ret;
Paul Bakkercca5b812013-08-31 17:40:26 +02001416 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001417
Hanno Beckerd96a6522019-07-10 13:55:25 +01001418 /* Double-check that length hasn't changed during decryption. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001419 if (rec->data_len != olen) {
1420 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1421 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkercca5b812013-08-31 17:40:26 +02001422 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001425 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
Paul Bakkercca5b812013-08-31 17:40:26 +02001426 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001427 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1428 * records is equivalent to CBC decryption of the concatenation
1429 * of the records; in other words, IVs are maintained across
1430 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001432 memcpy(transform->iv_dec, transform->cipher_ctx_dec.iv,
1433 transform->ivlen);
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001435#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001436
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001437 /* Safe since data_len >= minlen + maclen + 1, so after having
1438 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001439 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1440 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001441 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001442
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001443 if (auth_done == 1) {
Gabor Mezei18a44942021-10-20 11:59:27 +02001444 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001445 rec->data_len,
1446 padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001447 correct &= mask;
1448 padlen &= mask;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001449 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001451 if (rec->data_len < transform->maclen + padlen + 1) {
1452 MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET
1453 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1454 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
1455 rec->data_len,
1456 transform->maclen,
1457 padlen + 1));
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001458 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001459#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001460
Gabor Mezei18a44942021-10-20 11:59:27 +02001461 const size_t mask = mbedtls_ct_size_mask_ge(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001462 rec->data_len,
1463 transform->maclen + padlen + 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001464 correct &= mask;
1465 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001468 padlen++;
1469
1470 /* Regardless of the validity of the padding,
1471 * we have data_len >= padlen here. */
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001474 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001475 /* This is the SSL 3.0 path, we don't have to worry about Lucky
1476 * 13, because there's a strictly worse padding attack built in
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001477 * the protocol (known as part of POODLE), so we don't care if the
1478 * code is not constant-time, in particular branches are OK. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001479 if (padlen > transform->ivlen) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001481 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding length: is %" MBEDTLS_PRINTF_SIZET ", "
1482 "should be no more than %"
1483 MBEDTLS_PRINTF_SIZET,
1484 padlen, transform->ivlen));
Paul Bakkerd66f0702013-01-31 16:57:45 +01001485#endif
Paul Bakker45829992013-01-03 14:52:21 +01001486 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001488 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1490#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001491 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1492 if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001493 /* The padding check involves a series of up to 256
1494 * consecutive memory reads at the end of the record
1495 * plaintext buffer. In order to hide the length and
1496 * validity of the padding, always perform exactly
1497 * `min(256,plaintext_len)` reads (but take into account
1498 * only the last `padlen` bytes for the padding check). */
1499 size_t pad_count = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001500 volatile unsigned char * const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001501
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001502 /* Index of first padding byte; it has been ensured above
1503 * that the subtraction is safe. */
1504 size_t const padding_idx = rec->data_len - padlen;
1505 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1506 size_t const start_idx = rec->data_len - num_checks;
1507 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001508
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001509 for (idx = start_idx; idx < rec->data_len; idx++) {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001510 /* pad_count += (idx >= padding_idx) &&
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001511 * (check[idx] == padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001512 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001513 const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx);
1514 const size_t equal = mbedtls_ct_size_bool_eq(check[idx],
1515 padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001516 pad_count += mask & equal;
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001517 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001518 correct &= mbedtls_ct_size_bool_eq(pad_count, padlen);
Paul Bakkere47b34b2013-02-27 14:48:00 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001521 if (padlen > 0 && correct == 0) {
1522 MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected"));
1523 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001524#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001525 padlen &= mbedtls_ct_size_mask(correct);
1526 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1528 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001529 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001530 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1531 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02001532 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001533
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001534 /* If the padding was found to be invalid, padlen == 0
1535 * and the subtraction is safe. If the padding was found valid,
1536 * padlen hasn't been changed and the previous assertion
1537 * data_len >= padlen still holds. */
1538 rec->data_len -= padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001539 } else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001540#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001541 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001542 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1543 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001544 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001545
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001546#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001547 MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption",
1548 data, rec->data_len);
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001549#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001550
1551 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001552 * Authenticate if not done yet.
1553 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 */
Hanno Becker52344c22018-01-03 15:24:20 +00001555#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001556 if (auth_done == 0) {
Paul Elliottb8300282022-05-19 18:31:35 +01001557 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
1558 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01001559
Gilles Peskine326ba3c2023-09-18 14:08:11 +02001560 /* For CBC+MAC, If the initial value of padlen was such that
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001561 * data_len < maclen + padlen + 1, then padlen
1562 * got reset to 1, and the initial check
1563 * data_len >= minlen + maclen + 1
1564 * guarantees that at this point we still
1565 * have at least data_len >= maclen.
1566 *
1567 * If the initial value of padlen was such that
1568 * data_len >= maclen + padlen + 1, then we have
1569 * subtracted either padlen + 1 (if the padding was correct)
1570 * or 0 (if the padding was incorrect) since then,
1571 * hence data_len >= maclen in any case.
Gilles Peskine326ba3c2023-09-18 14:08:11 +02001572 *
1573 * For stream ciphers, we checked above that
1574 * data_len >= maclen.
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001575 */
1576 rec->data_len -= transform->maclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001577 ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
1578 transform->minor_ver);
Paul Bakker5121ce52009-01-03 21:22:43 +00001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001581 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
1582 ret = ssl_mac(&transform->md_ctx_dec,
1583 transform->mac_dec,
1584 data, rec->data_len,
1585 rec->ctr, rec->type,
1586 mac_expect);
1587 if (ret != 0) {
1588 MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret);
Gilles Peskine2b3f21d2021-12-10 21:35:10 +01001589 goto hmac_failed_etm_disabled;
1590 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001591 memcpy(mac_peer, data + rec->data_len, transform->maclen);
1592 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1594#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1595 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001596 if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001597 /*
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001598 * The next two sizes are the minimum and maximum values of
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001599 * data_len over all padlen values.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001600 *
1601 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001602 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001603 *
1604 * Note that max_len + maclen is never more than the buffer
1605 * length, as we previously did in_msglen -= maclen too.
1606 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001607 const size_t max_len = rec->data_len + padlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001608 const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001609
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001610 ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
1611 add_data, add_data_len,
1612 data, rec->data_len, min_len, max_len,
1613 mac_expect);
1614 if (ret != 0) {
1615 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
Gilles Peskined8e2e832021-12-10 21:33:21 +01001616 goto hmac_failed_etm_disabled;
Gilles Peskine20b44082018-05-29 14:06:49 +02001617 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001618
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001619 mbedtls_ct_memcpy_offset(mac_peer, data,
1620 rec->data_len,
1621 min_len, max_len,
1622 transform->maclen);
1623 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1625 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001626 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001627 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1628 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001631#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001632 MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen);
1633 MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen);
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001634#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001636 if (mbedtls_ct_memcmp(mac_peer, mac_expect,
1637 transform->maclen) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_DEBUG_ALL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001639 MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
Paul Bakkere47b34b2013-02-27 14:48:00 +01001640#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 correct = 0;
1642 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001643 auth_done++;
Gilles Peskined8e2e832021-12-10 21:33:21 +01001644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001645hmac_failed_etm_disabled:
1646 mbedtls_platform_zeroize(mac_peer, transform->maclen);
1647 mbedtls_platform_zeroize(mac_expect, transform->maclen);
1648 if (ret != 0) {
1649 return ret;
1650 }
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001651 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001652
1653 /*
1654 * Finally check the correct flag
1655 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001656 if (correct == 0) {
1657 return MBEDTLS_ERR_SSL_INVALID_MAC;
1658 }
Hanno Becker52344c22018-01-03 15:24:20 +00001659#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001660
1661 /* Make extra sure authentication was performed, exactly once */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001662 if (auth_done != 1) {
1663 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1664 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001665 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Hanno Beckerccc13d02020-05-04 12:30:04 +01001667#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001668 if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) {
Hanno Beckerccc13d02020-05-04 12:30:04 +01001669 /* Remove inner padding and infer true content type. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001670 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1671 &rec->type);
Hanno Beckerccc13d02020-05-04 12:30:04 +01001672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001673 if (ret != 0) {
1674 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1675 }
Hanno Beckerccc13d02020-05-04 12:30:04 +01001676 }
1677#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1678
Hanno Beckera0e20d02019-05-15 14:03:01 +01001679#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001680 if (rec->cid_len != 0) {
1681 ret = ssl_parse_inner_plaintext(data, &rec->data_len,
1682 &rec->type);
1683 if (ret != 0) {
1684 return MBEDTLS_ERR_SSL_INVALID_RECORD;
1685 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001686 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001687#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001688
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001689 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001691 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
1693
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001694#undef MAC_NONE
1695#undef MAC_PLAINTEXT
1696#undef MAC_CIPHERTEXT
1697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001699/*
1700 * Compression/decompression functions
1701 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001702MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001703static int ssl_compress_buf(mbedtls_ssl_context *ssl)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001704{
Janos Follath865b3eb2019-12-16 11:46:15 +00001705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001706 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001707 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001708 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001709 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001710#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1711 size_t out_buf_len = ssl->out_buf_len;
1712#else
1713 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1714#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001715
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001716 MBEDTLS_SSL_DEBUG_MSG(2, ("=> compress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001717
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001718 if (len_pre == 0) {
1719 return 0;
1720 }
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001721
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001722 memcpy(msg_pre, ssl->out_msg, len_pre);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001723
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001724 MBEDTLS_SSL_DEBUG_MSG(3, ("before compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1725 ssl->out_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001727 MBEDTLS_SSL_DEBUG_BUF(4, "before compression: output payload",
1728 ssl->out_msg, ssl->out_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001729
Paul Bakker48916f92012-09-16 19:57:18 +00001730 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1731 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1732 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001733 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001734
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001735 ret = deflate(&ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH);
1736 if (ret != Z_OK) {
1737 MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform compression (%d)", ret));
1738 return MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001739 }
1740
Darryl Greenb33cc762019-11-28 14:29:44 +00001741 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001742 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001744 MBEDTLS_SSL_DEBUG_MSG(3, ("after compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1745 ssl->out_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001746
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001747 MBEDTLS_SSL_DEBUG_BUF(4, "after compression: output payload",
1748 ssl->out_msg, ssl->out_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001750 MBEDTLS_SSL_DEBUG_MSG(2, ("<= compress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001752 return 0;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753}
1754
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02001755MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001756static int ssl_decompress_buf(mbedtls_ssl_context *ssl)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001757{
Janos Follath865b3eb2019-12-16 11:46:15 +00001758 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001760 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001762 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001763#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1764 size_t in_buf_len = ssl->in_buf_len;
1765#else
1766 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1767#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001768
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001769 MBEDTLS_SSL_DEBUG_MSG(2, ("=> decompress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001771 if (len_pre == 0) {
1772 return 0;
1773 }
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001774
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001775 memcpy(msg_pre, ssl->in_msg, len_pre);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001777 MBEDTLS_SSL_DEBUG_MSG(3, ("before decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1778 ssl->in_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001780 MBEDTLS_SSL_DEBUG_BUF(4, "before decompression: input payload",
1781 ssl->in_msg, ssl->in_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782
Paul Bakker48916f92012-09-16 19:57:18 +00001783 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1784 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1785 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001786 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001788 ret = inflate(&ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH);
1789 if (ret != Z_OK) {
1790 MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform decompression (%d)", ret));
1791 return MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001792 }
1793
Darryl Greenb33cc762019-11-28 14:29:44 +00001794 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001795 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001797 MBEDTLS_SSL_DEBUG_MSG(3, ("after decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
1798 ssl->in_msglen));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001800 MBEDTLS_SSL_DEBUG_BUF(4, "after decompression: input payload",
1801 ssl->in_msg, ssl->in_msglen);
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001803 MBEDTLS_SSL_DEBUG_MSG(2, ("<= decompress buf"));
Paul Bakker2770fbd2012-07-03 13:30:23 +00001804
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001805 return 0;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001806}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001808
Paul Bakker5121ce52009-01-03 21:22:43 +00001809/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001810 * Fill the input message buffer by appending data to it.
1811 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001812 *
1813 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1814 * available (from this read and/or a previous one). Otherwise, an error code
1815 * is returned (possibly EOF or WANT_READ).
1816 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001817 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1818 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1819 * since we always read a whole datagram at once.
1820 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001821 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001822 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001824int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want)
Paul Bakker5121ce52009-01-03 21:22:43 +00001825{
Janos Follath865b3eb2019-12-16 11:46:15 +00001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001827 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001828#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1829 size_t in_buf_len = ssl->in_buf_len;
1830#else
1831 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001834 MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001836 if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) {
1837 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
1838 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001839 }
1840
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001841 if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
1842 MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits"));
1843 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001844 }
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001847 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001848 uint32_t timeout;
1849
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001850 /*
1851 * The point is, we need to always read a full datagram at once, so we
1852 * sometimes read more then requested, and handle the additional data.
1853 * It could be the rest of the current record (while fetching the
1854 * header) and/or some other records in the same datagram.
1855 */
1856
1857 /*
1858 * Move to the next record in the already read datagram if applicable
1859 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001860 if (ssl->next_record_offset != 0) {
1861 if (ssl->in_left < ssl->next_record_offset) {
1862 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1863 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001864 }
1865
1866 ssl->in_left -= ssl->next_record_offset;
1867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001868 if (ssl->in_left != 0) {
1869 MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %"
1870 MBEDTLS_PRINTF_SIZET,
1871 ssl->next_record_offset));
1872 memmove(ssl->in_hdr,
1873 ssl->in_hdr + ssl->next_record_offset,
1874 ssl->in_left);
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001875 }
1876
1877 ssl->next_record_offset = 0;
1878 }
1879
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001880 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1881 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1882 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001883
1884 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001885 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001886 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001887 if (nb_want <= ssl->in_left) {
1888 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
1889 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001890 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001891
1892 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001893 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001894 * are not at the beginning of a new record, the caller did something
1895 * wrong.
1896 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001897 if (ssl->in_left != 0) {
1898 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1899 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001900 }
1901
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001902 /*
1903 * Don't even try to read if time's out already.
1904 * This avoids by-passing the timer when repeatedly receiving messages
1905 * that will end up being dropped.
1906 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001907 if (mbedtls_ssl_check_timer(ssl) != 0) {
1908 MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired"));
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001909 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001910 } else {
1911 len = in_buf_len - (ssl->in_hdr - ssl->in_buf);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001912
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001913 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001914 timeout = ssl->handshake->retransmit_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001915 } else {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001916 timeout = ssl->conf->read_timeout;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001917 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001918
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001919 MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout));
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001920
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001921 if (ssl->f_recv_timeout != NULL) {
1922 ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len,
1923 timeout);
1924 } else {
1925 ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len);
1926 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001927
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001928 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001929
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001930 if (ret == 0) {
1931 return MBEDTLS_ERR_SSL_CONN_EOF;
1932 }
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001933 }
1934
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001935 if (ret == MBEDTLS_ERR_SSL_TIMEOUT) {
1936 MBEDTLS_SSL_DEBUG_MSG(2, ("timeout"));
1937 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001938
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001939 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
1940 if (ssl_double_retransmit_timeout(ssl) != 0) {
1941 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout"));
1942 return MBEDTLS_ERR_SSL_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001943 }
1944
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001945 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
1946 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
1947 return ret;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001948 }
1949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001950 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001953 else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
1954 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
1955 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
1956 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
1957 ret);
1958 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001959 }
1960
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001961 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001962 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001964 }
1965
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001966 if (ret < 0) {
1967 return ret;
1968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001970 ssl->in_left = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001971 } else
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001972#endif
1973 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001974 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1975 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1976 ssl->in_left, nb_want));
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001977
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001978 while (ssl->in_left < nb_want) {
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001979 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001980
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001981 if (mbedtls_ssl_check_timer(ssl) != 0) {
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001982 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001983 } else {
1984 if (ssl->f_recv_timeout != NULL) {
1985 ret = ssl->f_recv_timeout(ssl->p_bio,
1986 ssl->in_hdr + ssl->in_left, len,
1987 ssl->conf->read_timeout);
1988 } else {
1989 ret = ssl->f_recv(ssl->p_bio,
1990 ssl->in_hdr + ssl->in_left, len);
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001991 }
1992 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001993
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001994 MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET
1995 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
1996 ssl->in_left, nb_want));
1997 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret);
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001998
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001999 if (ret == 0) {
2000 return MBEDTLS_ERR_SSL_CONN_EOF;
2001 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002002
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002003 if (ret < 0) {
2004 return ret;
2005 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002007 if ((size_t) ret > len || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) {
2008 MBEDTLS_SSL_DEBUG_MSG(1,
2009 ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2010 " were requested",
2011 ret, len));
2012 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16035bd15cb2018-02-28 04:30:59 -08002013 }
2014
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002015 ssl->in_left += ret;
2016 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002017 }
2018
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002019 MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002021 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002022}
2023
2024/*
2025 * Flush any data not yet written
2026 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002027int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002028{
Janos Follath865b3eb2019-12-16 11:46:15 +00002029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002030 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002032 MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002034 if (ssl->f_send == NULL) {
2035 MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() "));
2036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002037 }
2038
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002039 /* Avoid incrementing counter if data is flushed */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002040 if (ssl->out_left == 0) {
2041 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
2042 return 0;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002043 }
2044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002045 while (ssl->out_left > 0) {
2046 MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET
2047 ", out_left: %" MBEDTLS_PRINTF_SIZET,
2048 mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left));
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
Hanno Becker2b1e3542018-08-06 11:19:13 +01002050 buf = ssl->out_hdr - ssl->out_left;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002051 ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left);
Paul Bakker186751d2012-05-08 13:16:14 +00002052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002053 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +00002054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002055 if (ret <= 0) {
2056 return ret;
2057 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002059 if ((size_t) ret > ssl->out_left || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) {
2060 MBEDTLS_SSL_DEBUG_MSG(1,
2061 ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET
2062 " bytes were sent",
2063 ret, ssl->out_left));
2064 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
mohammad16034bbaeb42018-02-22 04:29:04 -08002065 }
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 ssl->out_left -= ret;
2068 }
2069
Hanno Becker2b1e3542018-08-06 11:19:13 +01002070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002071 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002072 ssl->out_hdr = ssl->out_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002073 } else
Hanno Becker2b1e3542018-08-06 11:19:13 +01002074#endif
2075 {
2076 ssl->out_hdr = ssl->out_buf + 8;
2077 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002078 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002080 MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002082 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002083}
2084
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002085/*
2086 * Functions to handle the DTLS retransmission state machine
2087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002089/*
2090 * Append current handshake message to current outgoing flight
2091 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002092MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002093static int ssl_flight_append(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_ssl_flight_item *msg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002096 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append"));
2097 MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight",
2098 ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002099
2100 /* Allocate space for current message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002101 if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) {
2102 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2103 sizeof(mbedtls_ssl_flight_item)));
2104 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002105 }
2106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002107 if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) {
2108 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2109 ssl->out_msglen));
2110 mbedtls_free(msg);
2111 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002112 }
2113
2114 /* Copy current handshake message with headers */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002115 memcpy(msg->p, ssl->out_msg, ssl->out_msglen);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002116 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002117 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002118 msg->next = NULL;
2119
2120 /* Append to the current flight */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002121 if (ssl->handshake->flight == NULL) {
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002122 ssl->handshake->flight = msg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002123 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002125 while (cur->next != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002126 cur = cur->next;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002127 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002128 cur->next = msg;
2129 }
2130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002131 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append"));
2132 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002133}
2134
2135/*
2136 * Free the current flight of handshake messages
2137 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002138void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_ssl_flight_item *cur = flight;
2141 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002143 while (cur != NULL) {
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002144 next = cur->next;
2145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002146 mbedtls_free(cur->p);
2147 mbedtls_free(cur);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002148
2149 cur = next;
2150 }
2151}
2152
2153/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002154 * Swap transform_out and out_ctr with the alternative ones
2155 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002156MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002157static int ssl_swap_epochs(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002158{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002160 unsigned char tmp_out_ctr[8];
2161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002162 if (ssl->transform_out == ssl->handshake->alt_transform_out) {
2163 MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs"));
2164 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002165 }
2166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002167 MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002168
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002169 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002170 tmp_transform = ssl->transform_out;
2171 ssl->transform_out = ssl->handshake->alt_transform_out;
2172 ssl->handshake->alt_transform_out = tmp_transform;
2173
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002174 /* Swap epoch + sequence_number */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002175 memcpy(tmp_out_ctr, ssl->cur_out_ctr, 8);
2176 memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8);
2177 memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, 8);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002178
2179 /* Adjust to the newly activated transform */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002180 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002182#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002183 if (mbedtls_ssl_hw_record_activate != NULL) {
2184 int ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND);
2185 if (ret != 0) {
2186 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
2187 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002188 }
2189 }
2190#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002192 return 0;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002193}
2194
2195/*
2196 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002197 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002198int mbedtls_ssl_resend(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002199{
2200 int ret = 0;
2201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002202 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002204 ret = mbedtls_ssl_flight_transmit(ssl);
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002206 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend"));
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002208 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002209}
2210
2211/*
2212 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002213 *
2214 * Need to remember the current message in case flush_output returns
2215 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002216 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002217 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002218int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002219{
Janos Follath865b3eb2019-12-16 11:46:15 +00002220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002221 MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002223 if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) {
2224 MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission"));
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002225
2226 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002227 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002228 ret = ssl_swap_epochs(ssl);
2229 if (ret != 0) {
2230 return ret;
2231 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002234 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002236 while (ssl->handshake->cur_msg != NULL) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002237 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002238 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002239
Hanno Beckere1dcb032018-08-17 16:47:58 +01002240 int const is_finished =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002241 (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2242 cur->p[0] == MBEDTLS_SSL_HS_FINISHED);
Hanno Beckere1dcb032018-08-17 16:47:58 +01002243
Hanno Becker04da1892018-08-14 13:22:10 +01002244 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002245 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
Hanno Becker04da1892018-08-14 13:22:10 +01002246
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002247 /* Swap epochs before sending Finished: we can't do it after
2248 * sending ChangeCipherSpec, in case write returns WANT_READ.
2249 * Must be done before copying, may change out_msg pointer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002250 if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) {
2251 MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message"));
2252 ret = ssl_swap_epochs(ssl);
2253 if (ret != 0) {
2254 return ret;
2255 }
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002256 }
2257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002258 ret = ssl_get_remaining_payload_in_datagram(ssl);
2259 if (ret < 0) {
2260 return ret;
2261 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002262 max_frag_len = (size_t) ret;
2263
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002264 /* CCS is copied as is, while HS messages may need fragmentation */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002265 if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
2266 if (max_frag_len == 0) {
2267 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2268 return ret;
2269 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002270
2271 continue;
2272 }
2273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002274 memcpy(ssl->out_msg, cur->p, cur->len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002275 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002276 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002277
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002278 /* Update position inside current message */
2279 ssl->handshake->cur_msg_p += cur->len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002280 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002281 const unsigned char * const p = ssl->handshake->cur_msg_p;
2282 const size_t hs_len = cur->len - 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002283 const size_t frag_off = p - (cur->p + 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002284 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002285 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002287 if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) {
2288 if (is_finished) {
2289 ret = ssl_swap_epochs(ssl);
2290 if (ret != 0) {
2291 return ret;
2292 }
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002293 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002295 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2296 return ret;
2297 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002298
2299 continue;
2300 }
2301 max_hs_frag_len = max_frag_len - 12;
2302
2303 cur_hs_frag_len = rem_len > max_hs_frag_len ?
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002304 max_hs_frag_len : rem_len;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002306 if (frag_off == 0 && cur_hs_frag_len != hs_len) {
2307 MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)",
2308 (unsigned) cur_hs_frag_len,
2309 (unsigned) max_hs_frag_len));
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002310 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002311
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002312 /* Messages are stored with handshake headers as if not fragmented,
2313 * copy beginning of headers then fill fragmentation fields.
2314 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002315 memcpy(ssl->out_msg, cur->p, 6);
Joe Subbiani61f7d732021-06-24 09:06:23 +01002316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002317 ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off);
2318 ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off);
2319 ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off);
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002321 ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len);
2322 ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len);
2323 ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002325 MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12);
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002326
Hanno Becker3f7b9732018-08-28 09:53:25 +01002327 /* Copy the handshake message content and set records fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002328 memcpy(ssl->out_msg + 12, p, cur_hs_frag_len);
Hanno Becker67bc7c32018-08-06 11:33:50 +01002329 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002330 ssl->out_msgtype = cur->type;
2331
2332 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002333 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002334 }
2335
2336 /* If done with the current message move to the next one if any */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002337 if (ssl->handshake->cur_msg_p >= cur->p + cur->len) {
2338 if (cur->next != NULL) {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002339 ssl->handshake->cur_msg = cur->next;
2340 ssl->handshake->cur_msg_p = cur->next->p + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002341 } else {
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002342 ssl->handshake->cur_msg = NULL;
2343 ssl->handshake->cur_msg_p = NULL;
2344 }
2345 }
2346
2347 /* Actually send the message out */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002348 if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) {
2349 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
2350 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002351 }
2352 }
2353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002354 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2355 return ret;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002356 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002358 /* Update state and set timer */
2359 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
2360 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
2361 } else {
2362 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
2363 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
2364 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002366 MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit"));
2367
2368 return 0;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002369}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002370
2371/*
2372 * To be called when the last message of an incoming flight is received.
2373 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002374void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002375{
2376 /* We won't need to resend that one any more */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002377 mbedtls_ssl_flight_free(ssl->handshake->flight);
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002378 ssl->handshake->flight = NULL;
2379 ssl->handshake->cur_msg = NULL;
2380
2381 /* The next incoming flight will start with this msg_seq */
2382 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2383
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002384 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002385 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002386
Hanno Becker0271f962018-08-16 13:23:47 +01002387 /* Clear future message buffering structure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002388 mbedtls_ssl_buffering_free(ssl);
Hanno Becker0271f962018-08-16 13:23:47 +01002389
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002390 /* Cancel timer */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002391 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002393 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2394 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002396 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002398 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002399}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002400
2401/*
2402 * To be called when the last message of an outgoing flight is send.
2403 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002404void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002405{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002406 ssl_reset_retransmit_timeout(ssl);
2407 mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout);
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002408
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002409 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2410 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002412 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002414 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002416#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002417
Paul Bakker5121ce52009-01-03 21:22:43 +00002418/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002419 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002421
2422/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002423 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002424 *
2425 * - fill in handshake headers
2426 * - update handshake checksum
2427 * - DTLS: save message for resending
2428 * - then pass to the record layer
2429 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002430 * DTLS: except for HelloRequest, messages are only queued, and will only be
2431 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002432 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002433 * Inputs:
2434 * - ssl->out_msglen: 4 + actual handshake message len
2435 * (4 is the size of handshake headers for TLS)
2436 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2437 * - ssl->out_msg + 4: the handshake message body
2438 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002439 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002440 * - ssl->out_msglen: the length of the record contents
2441 * (including handshake headers but excluding record headers)
2442 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002443 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002444int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00002445{
Janos Follath865b3eb2019-12-16 11:46:15 +00002446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002447 const size_t hs_len = ssl->out_msglen - 4;
2448 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002450 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002451
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002452 /*
2453 * Sanity checks
2454 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002455 if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
2456 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002457 /* In SSLv3, the client might send a NoCertificate alert. */
2458#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002459 if (!(ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2460 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2461 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT))
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002462#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2463 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002464 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2465 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002466 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002469 /* Whenever we send anything different from a
2470 * HelloRequest we should be in a handshake - double check. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002471 if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2472 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) &&
2473 ssl->handshake == NULL) {
2474 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2475 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002479 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002480 ssl->handshake != NULL &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002481 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
2482 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2483 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002484 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002485#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002486
Hanno Beckerb50a2532018-08-06 11:52:54 +01002487 /* Double-check that we did not exceed the bounds
2488 * of the outgoing record buffer.
2489 * This should never fail as the various message
2490 * writing functions must obey the bounds of the
2491 * outgoing record buffer, but better be safe.
2492 *
2493 * Note: We deliberately do not check for the MTU or MFL here.
2494 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002495 if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2496 MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: "
2497 "size %" MBEDTLS_PRINTF_SIZET
2498 ", maximum %" MBEDTLS_PRINTF_SIZET,
2499 ssl->out_msglen,
2500 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
2501 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerb50a2532018-08-06 11:52:54 +01002502 }
2503
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002504 /*
2505 * Fill handshake headers
2506 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002507 if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
2508 ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len);
2509 ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len);
2510 ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00002511
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002512 /*
2513 * DTLS has additional fields in the Handshake layer,
2514 * between the length field and the actual payload:
2515 * uint16 message_seq;
2516 * uint24 fragment_offset;
2517 * uint24 fragment_length;
2518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002520 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002521 /* Make room for the additional DTLS fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002522 if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) {
2523 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: "
2524 "size %" MBEDTLS_PRINTF_SIZET ", maximum %"
2525 MBEDTLS_PRINTF_SIZET,
2526 hs_len,
2527 (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12)));
2528 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker9648f8b2017-09-18 10:55:54 +01002529 }
2530
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002531 memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002532 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002533
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002534 /* Write message_seq and update it, except for HelloRequest */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002535 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2536 MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4);
2537 ++(ssl->handshake->out_msg_seq);
2538 } else {
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002539 ssl->out_msg[4] = 0;
2540 ssl->out_msg[5] = 0;
2541 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002542
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002543 /* Handshake hashes are computed without fragmentation,
2544 * so set frag_offset = 0 and frag_len = hs_len for now */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002545 memset(ssl->out_msg + 6, 0x00, 3);
2546 memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3);
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002549
Hanno Becker0207e532018-08-28 10:28:28 +01002550 /* Update running hashes of handshake messages seen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002551 if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) {
2552 ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen);
2553 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002556 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002557#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002558 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2559 !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2560 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) {
2561 if ((ret = ssl_flight_append(ssl)) != 0) {
2562 MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret);
2563 return ret;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002564 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002565 } else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002566#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002567 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002568 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
2569 MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret);
2570 return ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002571 }
2572 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002573
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002574 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message"));
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002575
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002576 return 0;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002577}
2578
2579/*
2580 * Record layer functions
2581 */
2582
2583/*
2584 * Write current record.
2585 *
2586 * Uses:
2587 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2588 * - ssl->out_msglen: length of the record content (excl headers)
2589 * - ssl->out_msg: record content
2590 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002591int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, uint8_t force_flush)
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002592{
2593 int ret, done = 0;
2594 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002595 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002597 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record"));
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002600 if (ssl->transform_out != NULL &&
2601 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
2602 if ((ret = ssl_compress_buf(ssl)) != 0) {
2603 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compress_buf", ret);
2604 return ret;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002605 }
2606
2607 len = ssl->out_msglen;
2608 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002612 if (mbedtls_ssl_hw_record_write != NULL) {
2613 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_write()"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002614
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002615 ret = mbedtls_ssl_hw_record_write(ssl);
2616 if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) {
2617 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_write", ret);
2618 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker05ef8352012-05-08 09:17:57 +00002619 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002620
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002621 if (ret == 0) {
Paul Bakkerc7878112012-12-19 14:41:14 +01002622 done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002623 }
Paul Bakker05ef8352012-05-08 09:17:57 +00002624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002625#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002626 if (!done) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002627 unsigned i;
2628 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002629#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2630 size_t out_buf_len = ssl->out_buf_len;
2631#else
2632 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2633#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002634 /* Skip writing the record content type to after the encryption,
2635 * as it may change when using the CID extension. */
2636
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002637 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2638 ssl->conf->transport, ssl->out_hdr + 1);
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002639
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002640 memcpy(ssl->out_ctr, ssl->cur_out_ctr, 8);
2641 MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002643 if (ssl->transform_out != NULL) {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002644 mbedtls_record rec;
2645
2646 rec.buf = ssl->out_iv;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002647 rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002648 rec.data_len = ssl->out_msglen;
2649 rec.data_offset = ssl->out_msg - rec.buf;
2650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002651 memcpy(&rec.ctr[0], ssl->out_ctr, 8);
2652 mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver,
2653 ssl->conf->transport, rec.ver);
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002654 rec.type = ssl->out_msgtype;
2655
Hanno Beckera0e20d02019-05-15 14:03:01 +01002656#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002657 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002658 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002659#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002660
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002661 if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec,
2662 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
2663 MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret);
2664 return ret;
Paul Bakker05ef8352012-05-08 09:17:57 +00002665 }
2666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002667 if (rec.data_offset != 0) {
2668 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2669 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002670 }
2671
Hanno Becker6430faf2019-05-08 11:57:13 +01002672 /* Update the record content type and CID. */
2673 ssl->out_msgtype = rec.type;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002674#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
2675 memcpy(ssl->out_cid, rec.cid, rec.cid_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01002676#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002677 ssl->out_msglen = len = rec.data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002678 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002679 }
2680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002681 protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002682
2683#if defined(MBEDTLS_SSL_PROTO_DTLS)
2684 /* In case of DTLS, double-check that we don't exceed
2685 * the remaining space in the datagram. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002686 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2687 ret = ssl_get_remaining_space_in_datagram(ssl);
2688 if (ret < 0) {
2689 return ret;
2690 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002691
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002692 if (protected_record_size > (size_t) ret) {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002693 /* Should never happen */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002694 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker2b1e3542018-08-06 11:19:13 +01002695 }
2696 }
2697#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002698
Hanno Becker6430faf2019-05-08 11:57:13 +01002699 /* Now write the potentially updated record content type. */
2700 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2701
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002702 MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, "
2703 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
2704 ssl->out_hdr[0], ssl->out_hdr[1],
2705 ssl->out_hdr[2], len));
Paul Bakker05ef8352012-05-08 09:17:57 +00002706
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002707 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
2708 ssl->out_hdr, protected_record_size);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002709
2710 ssl->out_left += protected_record_size;
2711 ssl->out_hdr += protected_record_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002712 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out);
Hanno Becker2b1e3542018-08-06 11:19:13 +01002713
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002714 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
2715 if (++ssl->cur_out_ctr[i - 1] != 0) {
Hanno Becker04484622018-08-06 09:49:38 +01002716 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002717 }
2718 }
Hanno Becker04484622018-08-06 09:49:38 +01002719
2720 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002721 if (i == mbedtls_ssl_ep_len(ssl)) {
2722 MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap"));
2723 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker04484622018-08-06 09:49:38 +01002724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
2726
Hanno Becker67bc7c32018-08-06 11:33:50 +01002727#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002728 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2729 flush == SSL_DONT_FORCE_FLUSH) {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002730 size_t remaining;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002731 ret = ssl_get_remaining_payload_in_datagram(ssl);
2732 if (ret < 0) {
2733 MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram",
2734 ret);
2735 return ret;
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002736 }
2737
2738 remaining = (size_t) ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002739 if (remaining == 0) {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002740 flush = SSL_FORCE_FLUSH;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002741 } else {
2742 MBEDTLS_SSL_DEBUG_MSG(2,
2743 ("Still %u bytes available in current datagram",
2744 (unsigned) remaining));
Hanno Becker67bc7c32018-08-06 11:33:50 +01002745 }
2746 }
2747#endif /* MBEDTLS_SSL_PROTO_DTLS */
2748
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002749 if ((flush == SSL_FORCE_FLUSH) &&
2750 (ret = mbedtls_ssl_flush_output(ssl)) != 0) {
2751 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
2752 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 }
2754
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002755 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record"));
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002757 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002758}
2759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002761
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002762MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002763static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002764{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002765 if (ssl->in_msglen < ssl->in_hslen ||
2766 memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 ||
2767 memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) {
2768 return 1;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002769 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002770 return 0;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002771}
Hanno Becker44650b72018-08-16 12:51:11 +01002772
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002773static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002774{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002775 return (ssl->in_msg[9] << 16) |
2776 (ssl->in_msg[10] << 8) |
2777 ssl->in_msg[11];
Hanno Becker44650b72018-08-16 12:51:11 +01002778}
2779
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002780static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002781{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002782 return (ssl->in_msg[6] << 16) |
2783 (ssl->in_msg[7] << 8) |
2784 ssl->in_msg[8];
Hanno Becker44650b72018-08-16 12:51:11 +01002785}
2786
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002787MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002788static int ssl_check_hs_header(mbedtls_ssl_context const *ssl)
Hanno Becker44650b72018-08-16 12:51:11 +01002789{
2790 uint32_t msg_len, frag_off, frag_len;
2791
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002792 msg_len = ssl_get_hs_total_len(ssl);
2793 frag_off = ssl_get_hs_frag_off(ssl);
2794 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker44650b72018-08-16 12:51:11 +01002795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002796 if (frag_off > msg_len) {
2797 return -1;
2798 }
Hanno Becker44650b72018-08-16 12:51:11 +01002799
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002800 if (frag_len > msg_len - frag_off) {
2801 return -1;
2802 }
Hanno Becker44650b72018-08-16 12:51:11 +01002803
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002804 if (frag_len + 12 > ssl->in_msglen) {
2805 return -1;
2806 }
Hanno Becker44650b72018-08-16 12:51:11 +01002807
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002808 return 0;
Hanno Becker44650b72018-08-16 12:51:11 +01002809}
2810
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002811/*
2812 * Mark bits in bitmask (used for DTLS HS reassembly)
2813 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002814static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002815{
2816 unsigned int start_bits, end_bits;
2817
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002818 start_bits = 8 - (offset % 8);
2819 if (start_bits != 8) {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002820 size_t first_byte_idx = offset / 8;
2821
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002822 /* Special case */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002823 if (len <= start_bits) {
2824 for (; len != 0; len--) {
2825 mask[first_byte_idx] |= 1 << (start_bits - len);
2826 }
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002827
2828 /* Avoid potential issues with offset or len becoming invalid */
2829 return;
2830 }
2831
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002832 offset += start_bits; /* Now offset % 8 == 0 */
2833 len -= start_bits;
2834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002835 for (; start_bits != 0; start_bits--) {
2836 mask[first_byte_idx] |= 1 << (start_bits - 1);
2837 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002838 }
2839
2840 end_bits = len % 8;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002841 if (end_bits != 0) {
2842 size_t last_byte_idx = (offset + len) / 8;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002843
2844 len -= end_bits; /* Now len % 8 == 0 */
2845
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002846 for (; end_bits != 0; end_bits--) {
2847 mask[last_byte_idx] |= 1 << (8 - end_bits);
2848 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002849 }
2850
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002851 memset(mask + offset / 8, 0xFF, len / 8);
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002852}
2853
2854/*
2855 * Check that bitmask is full
2856 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02002857MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002858static int ssl_bitmask_check(unsigned char *mask, size_t len)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002859{
2860 size_t i;
2861
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002862 for (i = 0; i < len / 8; i++) {
2863 if (mask[i] != 0xFF) {
2864 return -1;
2865 }
2866 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002867
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002868 for (i = 0; i < len % 8; i++) {
2869 if ((mask[len / 8] & (1 << (7 - i))) == 0) {
2870 return -1;
2871 }
2872 }
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002873
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002874 return 0;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002875}
2876
Hanno Becker56e205e2018-08-16 09:06:12 +01002877/* msg_len does not include the handshake header */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002878static size_t ssl_get_reassembly_buffer_size(size_t msg_len,
2879 unsigned add_bitmap)
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002880{
Hanno Becker56e205e2018-08-16 09:06:12 +01002881 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002882
Hanno Becker56e205e2018-08-16 09:06:12 +01002883 alloc_len = 12; /* Handshake header */
2884 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002886 if (add_bitmap) {
2887 alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002888
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002889 }
2890 return alloc_len;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002891}
Hanno Becker56e205e2018-08-16 09:06:12 +01002892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002894
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002895static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
Hanno Becker12555c62018-08-16 12:47:53 +01002896{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002897 return (ssl->in_msg[1] << 16) |
2898 (ssl->in_msg[2] << 8) |
2899 ssl->in_msg[3];
Hanno Becker12555c62018-08-16 12:47:53 +01002900}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002901
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002902int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002903{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002904 if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
2905 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
2906 ssl->in_msglen));
2907 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002908 }
2909
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002910 ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002911
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002912 MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
2913 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
2914 MBEDTLS_PRINTF_SIZET,
2915 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen));
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002918 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Janos Follath865b3eb2019-12-16 11:46:15 +00002919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002920 unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002921
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002922 if (ssl_check_hs_header(ssl) != 0) {
2923 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header"));
2924 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker44650b72018-08-16 12:51:11 +01002925 }
2926
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002927 if (ssl->handshake != NULL &&
2928 ((ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2929 recv_msg_seq != ssl->handshake->in_msg_seq) ||
2930 (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2931 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) {
2932 if (recv_msg_seq > ssl->handshake->in_msg_seq) {
2933 MBEDTLS_SSL_DEBUG_MSG(2,
2934 (
2935 "received future handshake message of sequence number %u (next %u)",
2936 recv_msg_seq,
2937 ssl->handshake->in_msg_seq));
2938 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Becker9e1ec222018-08-15 15:54:43 +01002939 }
2940
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002941 /* Retransmit only on last message from previous flight, to avoid
2942 * too many retransmissions.
2943 * Besides, No sane server ever retransmits HelloVerifyRequest */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002944 if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
2945 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) {
2946 MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, "
2947 "message_seq = %u, start_of_flight = %u",
2948 recv_msg_seq,
2949 ssl->handshake->in_flight_start_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002950
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002951 if ((ret = mbedtls_ssl_resend(ssl)) != 0) {
2952 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret);
2953 return ret;
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002954 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002955 } else {
2956 MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: "
2957 "message_seq = %u, expected = %u",
2958 recv_msg_seq,
2959 ssl->handshake->in_msg_seq));
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002960 }
2961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002962 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002963 }
2964 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002965
Hanno Becker6d97ef52018-08-16 13:09:04 +01002966 /* Message reassembly is handled alongside buffering of future
2967 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002968 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002969 * handshake logic layer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002970 if (ssl_hs_is_proper_fragment(ssl) == 1) {
2971 MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message"));
2972 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002973 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002974 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002976 /* With TLS we don't handle fragmentation (for now) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002977 if (ssl->in_msglen < ssl->in_hslen) {
2978 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
2979 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002980 }
2981
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002982 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01002983}
2984
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002985void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01002986{
Hanno Becker0271f962018-08-16 13:23:47 +01002987 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002988
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002989 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL) {
2990 ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen);
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002991 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002992
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002993 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01002995 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2996 ssl->handshake != NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01002997 unsigned offset;
2998 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002999
Hanno Becker0271f962018-08-16 13:23:47 +01003000 /* Increment handshake sequence number */
3001 hs->in_msg_seq++;
3002
3003 /*
3004 * Clear up handshake buffering and reassembly structure.
3005 */
3006
3007 /* Free first entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003008 ssl_buffering_free_slot(ssl, 0);
Hanno Becker0271f962018-08-16 13:23:47 +01003009
3010 /* Shift all other entries */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003011 for (offset = 0, hs_buf = &hs->buffering.hs[0];
Hanno Beckere605b192018-08-21 15:59:07 +01003012 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003013 offset++, hs_buf++) {
Hanno Becker0271f962018-08-16 13:23:47 +01003014 *hs_buf = *(hs_buf + 1);
3015 }
3016
3017 /* Create a fresh last entry */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003018 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003019 }
3020#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003021}
3022
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003023/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003024 * DTLS anti-replay: RFC 6347 4.1.2.6
3025 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003026 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3027 * Bit n is set iff record number in_window_top - n has been seen.
3028 *
3029 * Usually, in_window_top is the last record number seen and the lsb of
3030 * in_window is set. The only exception is the initial state (record number 0
3031 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003032 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003034void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003035{
3036 ssl->in_window_top = 0;
3037 ssl->in_window = 0;
3038}
3039
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003040static inline uint64_t ssl_load_six_bytes(unsigned char *buf)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003041{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003042 return ((uint64_t) buf[0] << 40) |
3043 ((uint64_t) buf[1] << 32) |
3044 ((uint64_t) buf[2] << 24) |
3045 ((uint64_t) buf[3] << 16) |
3046 ((uint64_t) buf[4] << 8) |
3047 ((uint64_t) buf[5]);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003048}
3049
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003051static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr)
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003052{
Janos Follath865b3eb2019-12-16 11:46:15 +00003053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003054 unsigned char *original_in_ctr;
3055
3056 // save original in_ctr
3057 original_in_ctr = ssl->in_ctr;
3058
3059 // use counter from record
3060 ssl->in_ctr = record_in_ctr;
3061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003062 ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl);
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003063
3064 // restore the counter
3065 ssl->in_ctr = original_in_ctr;
3066
3067 return ret;
3068}
3069
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003070/*
3071 * Return 0 if sequence number is acceptable, -1 otherwise
3072 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003073int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003074{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003075 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003076 uint64_t bit;
3077
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003078 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
3079 return 0;
3080 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003081
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003082 if (rec_seqnum > ssl->in_window_top) {
3083 return 0;
3084 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003085
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003086 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003088 if (bit >= 64) {
3089 return -1;
3090 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003092 if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) {
3093 return -1;
3094 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003096 return 0;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003097}
3098
3099/*
3100 * Update replay window on new validated record
3101 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003102void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003103{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003104 uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2);
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003106 if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) {
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003107 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003108 }
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003110 if (rec_seqnum > ssl->in_window_top) {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003111 /* Update window_top and the contents of the window */
3112 uint64_t shift = rec_seqnum - ssl->in_window_top;
3113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003114 if (shift >= 64) {
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003115 ssl->in_window = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003116 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003117 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003118 ssl->in_window |= 1;
3119 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003120
3121 ssl->in_window_top = rec_seqnum;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003122 } else {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003123 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003124 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003126 if (bit < 64) { /* Always true, but be extra sure */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003127 ssl->in_window |= (uint64_t) 1 << bit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003128 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003129 }
3130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003132
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003133#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003134/*
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003135 * Check if a datagram looks like a ClientHello with a valid cookie,
3136 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003137 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003138 *
3139 * - if cookie is valid, return 0
3140 * - if ClientHello looks superficially valid but cookie is not,
3141 * fill obuf and set olen, then
3142 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3143 * - otherwise return a specific error code
3144 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003145MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek33f41a82022-06-08 11:47:33 -04003146MBEDTLS_STATIC_TESTABLE
3147int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003148 mbedtls_ssl_context *ssl,
3149 const unsigned char *cli_id, size_t cli_id_len,
3150 const unsigned char *in, size_t in_len,
3151 unsigned char *obuf, size_t buf_len, size_t *olen)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003152{
3153 size_t sid_len, cookie_len;
3154 unsigned char *p;
3155
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003156 /*
3157 * Structure of ClientHello with record and handshake headers,
3158 * and expected values. We don't need to check a lot, more checks will be
3159 * done when actually parsing the ClientHello - skipping those checks
3160 * avoids code duplication and does not make cookie forging any easier.
3161 *
3162 * 0-0 ContentType type; copied, must be handshake
3163 * 1-2 ProtocolVersion version; copied
3164 * 3-4 uint16 epoch; copied, must be 0
3165 * 5-10 uint48 sequence_number; copied
3166 * 11-12 uint16 length; (ignored)
3167 *
3168 * 13-13 HandshakeType msg_type; (ignored)
3169 * 14-16 uint24 length; (ignored)
3170 * 17-18 uint16 message_seq; copied
3171 * 19-21 uint24 fragment_offset; copied, must be 0
3172 * 22-24 uint24 fragment_length; (ignored)
3173 *
3174 * 25-26 ProtocolVersion client_version; (ignored)
3175 * 27-58 Random random; (ignored)
3176 * 59-xx SessionID session_id; 1 byte len + sid_len content
3177 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3178 * ...
3179 *
3180 * Minimum length is 61 bytes.
3181 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003182 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u",
3183 (unsigned) in_len));
3184 MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len);
3185 if (in_len < 61) {
3186 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short"));
3187 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003188 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003189 if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003190 in[3] != 0 || in[4] != 0 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003191 in[19] != 0 || in[20] != 0 || in[21] != 0) {
3192 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello"));
3193 MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u",
3194 in[0],
3195 (unsigned) in[3] << 8 | in[4],
3196 (unsigned) in[19] << 16 | in[20] << 8 | in[21]));
3197 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003198 }
3199
3200 sid_len = in[59];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003201 if (59 + 1 + sid_len + 1 > in_len) {
3202 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u",
3203 (unsigned) sid_len,
3204 (unsigned) in_len - 61));
3205 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003206 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003207 MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network",
3208 in + 60, sid_len);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003209
3210 cookie_len = in[60 + sid_len];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003211 if (59 + 1 + sid_len + 1 + cookie_len > in_len) {
3212 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u",
3213 (unsigned) cookie_len,
3214 (unsigned) (in_len - sid_len - 61)));
3215 return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO;
Gilles Peskinef333dfa2022-02-15 23:53:36 +01003216 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003218 MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network",
3219 in + sid_len + 61, cookie_len);
3220 if (ssl->conf->f_cookie_check(ssl->conf->p_cookie,
3221 in + sid_len + 61, cookie_len,
3222 cli_id, cli_id_len) == 0) {
3223 MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid"));
3224 return 0;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003225 }
3226
3227 /*
3228 * If we get here, we've got an invalid cookie, let's prepare HVR.
3229 *
3230 * 0-0 ContentType type; copied
3231 * 1-2 ProtocolVersion version; copied
3232 * 3-4 uint16 epoch; copied
3233 * 5-10 uint48 sequence_number; copied
3234 * 11-12 uint16 length; olen - 13
3235 *
3236 * 13-13 HandshakeType msg_type; hello_verify_request
3237 * 14-16 uint24 length; olen - 25
3238 * 17-18 uint16 message_seq; copied
3239 * 19-21 uint24 fragment_offset; copied
3240 * 22-24 uint24 fragment_length; olen - 25
3241 *
3242 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3243 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3244 *
3245 * Minimum length is 28.
3246 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003247 if (buf_len < 28) {
3248 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3249 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003250
3251 /* Copy most fields and adapt others */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003252 memcpy(obuf, in, 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003253 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3254 obuf[25] = 0xfe;
3255 obuf[26] = 0xff;
3256
3257 /* Generate and write actual cookie */
3258 p = obuf + 28;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003259 if (ssl->conf->f_cookie_write(ssl->conf->p_cookie,
3260 &p, obuf + buf_len,
3261 cli_id, cli_id_len) != 0) {
3262 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003263 }
3264
3265 *olen = p - obuf;
3266
3267 /* Go back and fill length fields */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003268 obuf[27] = (unsigned char) (*olen - 28);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003270 obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25);
3271 obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25);
3272 obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003274 MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003276 return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003277}
3278
3279/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003280 * Handle possible client reconnect with the same UDP quadruplet
3281 * (RFC 6347 Section 4.2.8).
3282 *
3283 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3284 * that looks like a ClientHello.
3285 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003286 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003287 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003288 * - if the input looks like a ClientHello with a valid cookie,
3289 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003290 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003291 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003292 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003293 * This function is called (through ssl_check_client_reconnect()) when an
3294 * unexpected record is found in ssl_get_next_record(), which will discard the
3295 * record if we return 0, and bubble up the return value otherwise (this
3296 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3297 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003298 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003299MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003300static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003301{
Janos Follath865b3eb2019-12-16 11:46:15 +00003302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003303 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003305 if (ssl->conf->f_cookie_write == NULL ||
3306 ssl->conf->f_cookie_check == NULL) {
Hanno Becker2fddd372019-07-10 14:37:41 +01003307 /* If we can't use cookies to verify reachability of the peer,
3308 * drop the record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003309 MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, "
3310 "can't check reconnect validity"));
3311 return 0;
Hanno Becker2fddd372019-07-10 14:37:41 +01003312 }
3313
Andrzej Kurek33f41a82022-06-08 11:47:33 -04003314 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003315 ssl,
3316 ssl->cli_id, ssl->cli_id_len,
3317 ssl->in_buf, ssl->in_left,
3318 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len);
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003320 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret);
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003322 if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003323 int send_ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003324 MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest"));
3325 MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network",
3326 ssl->out_buf, len);
Brian J Murray1903fb32016-11-06 04:45:15 -08003327 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003328 * If the error is permanent we'll catch it later,
3329 * if it's not, then hopefully it'll work next time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003330 send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len);
3331 MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret);
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003332 (void) send_ret;
3333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003334 return 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003335 }
3336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003337 if (ret == 0) {
3338 MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context"));
3339 if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) {
3340 MBEDTLS_SSL_DEBUG_RET(1, "reset", ret);
3341 return ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003342 }
3343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003344 return MBEDTLS_ERR_SSL_CLIENT_RECONNECT;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003345 }
3346
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003347 return ret;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003348}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003349#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003350
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003351MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003352static int ssl_check_record_type(uint8_t record_type)
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003353{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003354 if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003355 record_type != MBEDTLS_SSL_MSG_ALERT &&
3356 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003357 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
3358 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003359 }
3360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003361 return 0;
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003362}
3363
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003364/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003365 * ContentType type;
3366 * ProtocolVersion version;
3367 * uint16 epoch; // DTLS only
3368 * uint48 sequence_number; // DTLS only
3369 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003370 *
3371 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003372 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003373 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3374 *
3375 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003376 * 1. proceed with the record if this function returns 0
3377 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3378 * 3. return CLIENT_RECONNECT if this function return that value
3379 * 4. drop the whole datagram if this function returns anything else.
3380 * Point 2 is needed when the peer is resending, and we have already received
3381 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003382 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003383MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003384static int ssl_parse_record_header(mbedtls_ssl_context const *ssl,
3385 unsigned char *buf,
3386 size_t len,
3387 mbedtls_record *rec)
Paul Bakker5121ce52009-01-03 21:22:43 +00003388{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003389 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003390
Hanno Beckere5e7e782019-07-11 12:29:35 +01003391 size_t const rec_hdr_type_offset = 0;
3392 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003393
Hanno Beckere5e7e782019-07-11 12:29:35 +01003394 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3395 rec_hdr_type_len;
3396 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
Hanno Beckere5e7e782019-07-11 12:29:35 +01003398 size_t const rec_hdr_ctr_len = 8;
3399#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003400 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003401 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3402 rec_hdr_version_len;
3403
Hanno Beckera0e20d02019-05-15 14:03:01 +01003404#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003405 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3406 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003407 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003408#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3409#endif /* MBEDTLS_SSL_PROTO_DTLS */
3410
3411 size_t rec_hdr_len_offset; /* To be determined */
3412 size_t const rec_hdr_len_len = 2;
3413
3414 /*
3415 * Check minimum lengths for record header.
3416 */
3417
3418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003419 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003420 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003421 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003422#endif /* MBEDTLS_SSL_PROTO_DTLS */
3423 {
3424 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3425 }
3426
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003427 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3428 MBEDTLS_SSL_DEBUG_MSG(1,
3429 (
3430 "datagram of length %u too small to hold DTLS record header of length %u",
3431 (unsigned) len,
3432 (unsigned) (rec_hdr_len_len + rec_hdr_len_len)));
3433 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003434 }
3435
3436 /*
3437 * Parse and validate record content type
3438 */
3439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003440 rec->type = buf[rec_hdr_type_offset];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003441
3442 /* Check record content type */
3443#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3444 rec->cid_len = 0;
3445
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003446 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003447 ssl->conf->cid_len != 0 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003448 rec->type == MBEDTLS_SSL_MSG_CID) {
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003449 /* Shift pointers to account for record header including CID
3450 * struct {
3451 * ContentType special_type = tls12_cid;
3452 * ProtocolVersion version;
3453 * uint16 epoch;
3454 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003455 * opaque cid[cid_length]; // Additional field compared to
3456 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003457 * uint16 length;
3458 * opaque enc_content[DTLSCiphertext.length];
3459 * } DTLSCiphertext;
3460 */
3461
3462 /* So far, we only support static CID lengths
3463 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003464 rec_hdr_cid_len = ssl->conf->cid_len;
3465 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003467 if (len < rec_hdr_len_offset + rec_hdr_len_len) {
3468 MBEDTLS_SSL_DEBUG_MSG(1,
3469 (
3470 "datagram of length %u too small to hold DTLS record header including CID, length %u",
3471 (unsigned) len,
3472 (unsigned) (rec_hdr_len_offset + rec_hdr_len_len)));
3473 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere538d822019-07-10 14:50:10 +01003474 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003475
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003476 /* configured CID len is guaranteed at most 255, see
3477 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3478 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003479 memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len);
3480 } else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003481#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003482 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003483 if (ssl_check_record_type(rec->type)) {
3484 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u",
3485 (unsigned) rec->type));
3486 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003487 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003488 }
3489
Hanno Beckere5e7e782019-07-11 12:29:35 +01003490 /*
3491 * Parse and validate record version
3492 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003493 rec->ver[0] = buf[rec_hdr_version_offset + 0];
3494 rec->ver[1] = buf[rec_hdr_version_offset + 1];
3495 mbedtls_ssl_read_version(&major_ver, &minor_ver,
3496 ssl->conf->transport,
3497 &rec->ver[0]);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003498
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003499 if (major_ver != ssl->major_ver) {
3500 MBEDTLS_SSL_DEBUG_MSG(1, ("major version mismatch: got %u, expected %u",
3501 (unsigned) major_ver,
3502 (unsigned) ssl->major_ver));
3503 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 }
3505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003506 if (minor_ver > ssl->conf->max_minor_ver) {
3507 MBEDTLS_SSL_DEBUG_MSG(1, ("minor version mismatch: got %u, expected max %u",
3508 (unsigned) minor_ver,
3509 (unsigned) ssl->conf->max_minor_ver));
3510 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Paul Bakker5121ce52009-01-03 21:22:43 +00003511 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003512 /*
3513 * Parse/Copy record sequence number.
3514 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003515
Hanno Beckere5e7e782019-07-11 12:29:35 +01003516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003517 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003518 /* Copy explicit record sequence number from input buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003519 memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset,
3520 rec_hdr_ctr_len);
3521 } else
Hanno Beckere5e7e782019-07-11 12:29:35 +01003522#endif /* MBEDTLS_SSL_PROTO_DTLS */
3523 {
3524 /* Copy implicit record sequence number from SSL context structure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003525 memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len);
Hanno Beckere5e7e782019-07-11 12:29:35 +01003526 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003527
Hanno Beckere5e7e782019-07-11 12:29:35 +01003528 /*
3529 * Parse record length.
3530 */
3531
Hanno Beckere5e7e782019-07-11 12:29:35 +01003532 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003533 rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) |
3534 ((size_t) buf[rec_hdr_len_offset + 1] << 0);
3535 MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset);
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003537 MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, "
3538 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
3539 rec->type,
3540 major_ver, minor_ver, rec->data_len));
Hanno Beckere5e7e782019-07-11 12:29:35 +01003541
3542 rec->buf = buf;
3543 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003544
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003545 if (rec->data_len == 0) {
3546 return MBEDTLS_ERR_SSL_INVALID_RECORD;
3547 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003549 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003550 * DTLS-related tests.
3551 * Check epoch before checking length constraint because
3552 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3553 * message gets duplicated before the corresponding Finished message,
3554 * the second ChangeCipherSpec should be discarded because it belongs
3555 * to an old epoch, but not because its length is shorter than
3556 * the minimum record length for packets using the new record transform.
3557 * Note that these two kinds of failures are handled differently,
3558 * as an unexpected record is silently skipped but an invalid
3559 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003560 */
3561#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003562 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3563 rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003564
Hanno Becker955a5c92019-07-10 17:12:07 +01003565 /* Check that the datagram is large enough to contain a record
3566 * of the advertised length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003567 if (len < rec->data_offset + rec->data_len) {
3568 MBEDTLS_SSL_DEBUG_MSG(1,
3569 (
3570 "Datagram of length %u too small to contain record of advertised length %u.",
3571 (unsigned) len,
3572 (unsigned) (rec->data_offset + rec->data_len)));
3573 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker955a5c92019-07-10 17:12:07 +01003574 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003575
Hanno Becker37cfe732019-07-10 17:20:01 +01003576 /* Records from other, non-matching epochs are silently discarded.
3577 * (The case of same-port Client reconnects must be considered in
3578 * the caller). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003579 if (rec_epoch != ssl->in_epoch) {
3580 MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: "
3581 "expected %u, received %lu",
3582 ssl->in_epoch, (unsigned long) rec_epoch));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003583
Hanno Becker552f7472019-07-19 10:59:12 +01003584 /* Records from the next epoch are considered for buffering
3585 * (concretely: early Finished messages). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003586 if (rec_epoch == (unsigned) ssl->in_epoch + 1) {
3587 MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering"));
3588 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003589 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003590
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003591 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003592 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003593#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003594 /* For records from the correct epoch, check whether their
3595 * sequence number has been seen before. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003596 else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl,
3597 &rec->ctr[0]) != 0) {
3598 MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record"));
3599 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003600 }
3601#endif
3602 }
3603#endif /* MBEDTLS_SSL_PROTO_DTLS */
3604
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003605 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003606}
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
Hanno Becker2fddd372019-07-10 14:37:41 +01003609#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003610MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003611static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl)
Hanno Becker2fddd372019-07-10 14:37:41 +01003612{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003613 unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1];
Hanno Becker2fddd372019-07-10 14:37:41 +01003614
3615 /*
3616 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3617 * access the first byte of record content (handshake type), as we
3618 * have an active transform (possibly iv_len != 0), so use the
3619 * fact that the record header len is 13 instead.
3620 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003621 if (rec_epoch == 0 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003622 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3623 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3624 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3625 ssl->in_left > 13 &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003626 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) {
3627 MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect "
3628 "from the same port"));
3629 return ssl_handle_possible_reconnect(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 }
3631
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003632 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003633}
Hanno Becker2fddd372019-07-10 14:37:41 +01003634#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003635
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003636/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003637 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003638 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003639MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003640static int ssl_prepare_record_content(mbedtls_ssl_context *ssl,
3641 mbedtls_record *rec)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003642{
3643 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003644
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003645 MBEDTLS_SSL_DEBUG_BUF(4, "input record from network",
3646 rec->buf, rec->buf_len);
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003649 if (mbedtls_ssl_hw_record_read != NULL) {
3650 MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_read()"));
Paul Bakker05ef8352012-05-08 09:17:57 +00003651
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003652 ret = mbedtls_ssl_hw_record_read(ssl);
3653 if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) {
3654 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_read", ret);
3655 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Paul Bakker05ef8352012-05-08 09:17:57 +00003656 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003657
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003658 if (ret == 0) {
Paul Bakkerc7878112012-12-19 14:41:14 +01003659 done = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003660 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003662#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003663 if (!done && ssl->transform_in != NULL) {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003664 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003666 if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in,
3667 rec)) != 0) {
3668 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret);
Hanno Becker8367ccc2019-05-14 11:30:10 +01003669
Hanno Beckera0e20d02019-05-15 14:03:01 +01003670#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003671 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
Hanno Becker8367ccc2019-05-14 11:30:10 +01003672 ssl->conf->ignore_unexpected_cid
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003673 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
3674 MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID"));
Hanno Becker16ded982019-05-08 13:02:55 +01003675 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003676 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003677#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003678
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003679 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003680 }
3681
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003682 if (old_msg_type != rec->type) {
3683 MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d",
3684 old_msg_type, rec->type));
Hanno Becker6430faf2019-05-08 11:57:13 +01003685 }
3686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003687 MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt",
3688 rec->buf + rec->data_offset, rec->data_len);
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003689
Hanno Beckera0e20d02019-05-15 14:03:01 +01003690#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003691 /* We have already checked the record content type
3692 * in ssl_parse_record_header(), failing or silently
3693 * dropping the record in the case of an unknown type.
3694 *
3695 * Since with the use of CIDs, the record content type
3696 * might change during decryption, re-check the record
3697 * content type, but treat a failure as fatal this time. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003698 if (ssl_check_record_type(rec->type)) {
3699 MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type"));
3700 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker6430faf2019-05-08 11:57:13 +01003701 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003702#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003704 if (rec->data_len == 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003706 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
3707 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003708 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003709 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype));
3710 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003711 }
3712#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3713
3714 ssl->nb_zero++;
3715
3716 /*
3717 * Three or more empty messages may be a DoS attack
3718 * (excessive CPU consumption).
3719 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003720 if (ssl->nb_zero > 3) {
3721 MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty "
3722 "messages, possible DoS attack"));
Hanno Becker6e7700d2019-05-08 10:38:32 +01003723 /* Treat the records as if they were not properly authenticated,
3724 * thereby failing the connection if we see more than allowed
3725 * by the configured bad MAC threshold. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003726 return MBEDTLS_ERR_SSL_INVALID_MAC;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003727 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003728 } else {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003729 ssl->nb_zero = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003730 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003731
3732#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003733 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003734 ; /* in_ctr read from peer, not maintained internally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003735 } else
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003736#endif
3737 {
3738 unsigned i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003739 for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) {
3740 if (++ssl->in_ctr[i - 1] != 0) {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003741 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003742 }
3743 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003744
3745 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003746 if (i == mbedtls_ssl_ep_len(ssl)) {
3747 MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap"));
3748 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003749 }
3750 }
3751
Paul Bakker5121ce52009-01-03 21:22:43 +00003752 }
3753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003755 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3756 mbedtls_ssl_dtls_replay_update(ssl);
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003757 }
3758#endif
3759
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003760 /* Check actual (decrypted) record content length against
3761 * configured maximum. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003762 if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) {
3763 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
3764 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003765 }
3766
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003767 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003768}
3769
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003770/*
3771 * Read a record.
3772 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003773 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3774 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3775 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003776 */
Hanno Becker1097b342018-08-15 14:09:41 +01003777
3778/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003780static int ssl_consume_current_message(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003781MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003782static int ssl_get_next_record(mbedtls_ssl_context *ssl);
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003783MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003784static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl);
Hanno Becker4162b112018-08-15 14:05:04 +01003785
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003786int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl,
3787 unsigned update_hs_digest)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003788{
Janos Follath865b3eb2019-12-16 11:46:15 +00003789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003790
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003791 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record"));
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003793 if (ssl->keep_current_message == 0) {
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003794 do {
Simon Butcher99000142016-10-13 17:21:01 +01003795
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003796 ret = ssl_consume_current_message(ssl);
3797 if (ret != 0) {
3798 return ret;
3799 }
Hanno Becker26994592018-08-15 14:14:59 +01003800
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003801 if (ssl_record_is_in_progress(ssl) == 0) {
David Horstmann5846c9d2022-10-06 18:31:25 +01003802 int dtls_have_buffered = 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere74d5562018-08-15 14:26:08 +01003804
Hanno Becker40f50842018-08-15 14:48:01 +01003805 /* We only check for buffered messages if the
3806 * current datagram is fully consumed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003807 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3808 ssl_next_record_is_in_datagram(ssl) == 0) {
3809 if (ssl_load_buffered_message(ssl) == 0) {
David Horstmann5846c9d2022-10-06 18:31:25 +01003810 dtls_have_buffered = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003811 }
Hanno Becker40f50842018-08-15 14:48:01 +01003812 }
3813
Hanno Becker40f50842018-08-15 14:48:01 +01003814#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003815 if (dtls_have_buffered == 0) {
3816 ret = ssl_get_next_record(ssl);
3817 if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) {
Hanno Becker40f50842018-08-15 14:48:01 +01003818 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003819 }
Hanno Becker40f50842018-08-15 14:48:01 +01003820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003821 if (ret != 0) {
3822 MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret);
3823 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003824 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003825 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003826 }
3827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003828 ret = mbedtls_ssl_handle_message_type(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003829
Hanno Becker40f50842018-08-15 14:48:01 +01003830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003831 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
Hanno Becker40f50842018-08-15 14:48:01 +01003832 /* Buffer future message */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003833 ret = ssl_buffer_message(ssl);
3834 if (ret != 0) {
3835 return ret;
3836 }
Hanno Becker40f50842018-08-15 14:48:01 +01003837
3838 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3839 }
3840#endif /* MBEDTLS_SSL_PROTO_DTLS */
3841
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003842 } while (MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3843 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003844
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003845 if (0 != ret) {
3846 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret);
3847 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01003848 }
3849
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003850 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3851 update_hs_digest == 1) {
3852 mbedtls_ssl_update_handshake_status(ssl);
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003853 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003854 } else {
3855 MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message"));
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003856 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003857 }
3858
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003859 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record"));
Simon Butcher99000142016-10-13 17:21:01 +01003860
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003861 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01003862}
3863
Hanno Becker40f50842018-08-15 14:48:01 +01003864#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003866static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01003867{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003868 if (ssl->in_left > ssl->next_record_offset) {
3869 return 1;
3870 }
Simon Butcher99000142016-10-13 17:21:01 +01003871
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003872 return 0;
Hanno Becker40f50842018-08-15 14:48:01 +01003873}
3874
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003875MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003876static int ssl_load_buffered_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01003877{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003878 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003879 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003880 int ret = 0;
3881
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003882 if (hs == NULL) {
3883 return -1;
3884 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003885
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003886 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message"));
Hanno Beckere00ae372018-08-20 09:39:42 +01003887
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003888 if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3889 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003890 /* Check if we have seen a ChangeCipherSpec before.
3891 * If yes, synthesize a CCS record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003892 if (!hs->buffering.seen_ccs) {
3893 MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003894 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003895 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003896 }
3897
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003898 MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003899 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3900 ssl->in_msglen = 1;
3901 ssl->in_msg[0] = 1;
3902
3903 /* As long as they are equal, the exact value doesn't matter. */
3904 ssl->in_left = 0;
3905 ssl->next_record_offset = 0;
3906
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003907 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003908 goto exit;
3909 }
Hanno Becker37f95322018-08-16 13:55:32 +01003910
Hanno Beckerb8f50142018-08-28 10:01:34 +01003911#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003912 /* Debug only */
3913 {
3914 unsigned offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003915 for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
Hanno Becker37f95322018-08-16 13:55:32 +01003916 hs_buf = &hs->buffering.hs[offset];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003917 if (hs_buf->is_valid == 1) {
3918 MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.",
3919 hs->in_msg_seq + offset,
3920 hs_buf->is_complete ? "fully" : "partially"));
Hanno Becker37f95322018-08-16 13:55:32 +01003921 }
3922 }
3923 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003924#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003925
3926 /* Check if we have buffered and/or fully reassembled the
3927 * next handshake message. */
3928 hs_buf = &hs->buffering.hs[0];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003929 if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) {
Hanno Becker37f95322018-08-16 13:55:32 +01003930 /* Synthesize a record containing the buffered HS message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003931 size_t msg_len = (hs_buf->data[1] << 16) |
3932 (hs_buf->data[2] << 8) |
3933 hs_buf->data[3];
Hanno Becker37f95322018-08-16 13:55:32 +01003934
3935 /* Double-check that we haven't accidentally buffered
3936 * a message that doesn't fit into the input buffer. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003937 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
3938 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3939 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01003940 }
3941
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003942 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load"));
3943 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)",
3944 hs_buf->data, msg_len + 12);
Hanno Becker37f95322018-08-16 13:55:32 +01003945
3946 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3947 ssl->in_hslen = msg_len + 12;
3948 ssl->in_msglen = msg_len + 12;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003949 memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen);
Hanno Becker37f95322018-08-16 13:55:32 +01003950
3951 ret = 0;
3952 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003953 } else {
3954 MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered",
3955 hs->in_msg_seq));
Hanno Becker37f95322018-08-16 13:55:32 +01003956 }
3957
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003958 ret = -1;
3959
3960exit:
3961
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003962 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message"));
3963 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01003964}
3965
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02003966MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003967static int ssl_buffer_make_space(mbedtls_ssl_context *ssl,
3968 size_t desired)
Hanno Beckera02b0b42018-08-21 17:20:27 +01003969{
3970 int offset;
3971 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003972 MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available",
3973 (unsigned) desired));
Hanno Beckera02b0b42018-08-21 17:20:27 +01003974
Hanno Becker01315ea2018-08-21 17:22:17 +01003975 /* Get rid of future records epoch first, if such exist. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003976 ssl_free_buffered_record(ssl);
Hanno Becker01315ea2018-08-21 17:22:17 +01003977
3978 /* Check if we have enough space available now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003979 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3980 hs->buffering.total_bytes_buffered)) {
3981 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record"));
3982 return 0;
Hanno Becker01315ea2018-08-21 17:22:17 +01003983 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003984
Hanno Becker4f432ad2018-08-28 10:02:32 +01003985 /* We don't have enough space to buffer the next expected handshake
3986 * message. Remove buffers used for future messages to gain space,
3987 * starting with the most distant one. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003988 for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3989 offset >= 0; offset--) {
3990 MBEDTLS_SSL_DEBUG_MSG(2,
3991 (
3992 "Free buffering slot %d to make space for reassembly of next handshake message",
3993 offset));
Hanno Beckera02b0b42018-08-21 17:20:27 +01003994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003995 ssl_buffering_free_slot(ssl, (uint8_t) offset);
Hanno Beckera02b0b42018-08-21 17:20:27 +01003996
3997 /* Check if we have enough space available now. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01003998 if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3999 hs->buffering.total_bytes_buffered)) {
4000 MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages"));
4001 return 0;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004002 }
4003 }
4004
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004005 return -1;
Hanno Beckera02b0b42018-08-21 17:20:27 +01004006}
4007
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004008MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004009static int ssl_buffer_message(mbedtls_ssl_context *ssl)
Hanno Becker40f50842018-08-15 14:48:01 +01004010{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004011 int ret = 0;
4012 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4013
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004014 if (hs == NULL) {
4015 return 0;
4016 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004017
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004018 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message"));
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004019
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004020 switch (ssl->in_msgtype) {
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004021 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004022 MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message"));
Hanno Beckere678eaa2018-08-21 14:57:46 +01004023
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004024 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004025 break;
4026
4027 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004028 {
4029 unsigned recv_msg_seq_offset;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004030 unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5];
Hanno Becker37f95322018-08-16 13:55:32 +01004031 mbedtls_ssl_hs_buffer *hs_buf;
4032 size_t msg_len = ssl->in_hslen - 12;
4033
4034 /* We should never receive an old handshake
4035 * message - double-check nonetheless. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004036 if (recv_msg_seq < ssl->handshake->in_msg_seq) {
4037 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4038 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker37f95322018-08-16 13:55:32 +01004039 }
4040
4041 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004042 if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Becker37f95322018-08-16 13:55:32 +01004043 /* Silently ignore -- message too far in the future */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004044 MBEDTLS_SSL_DEBUG_MSG(2,
4045 ("Ignore future HS message with sequence number %u, "
4046 "buffering window %u - %u",
4047 recv_msg_seq, ssl->handshake->in_msg_seq,
4048 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS -
4049 1));
Hanno Becker37f95322018-08-16 13:55:32 +01004050
4051 goto exit;
4052 }
4053
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004054 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ",
4055 recv_msg_seq, recv_msg_seq_offset));
Hanno Becker37f95322018-08-16 13:55:32 +01004056
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004057 hs_buf = &hs->buffering.hs[recv_msg_seq_offset];
Hanno Becker37f95322018-08-16 13:55:32 +01004058
4059 /* Check if the buffering for this seq nr has already commenced. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004060 if (!hs_buf->is_valid) {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004061 size_t reassembly_buf_sz;
4062
Hanno Becker37f95322018-08-16 13:55:32 +01004063 hs_buf->is_fragmented =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004064 (ssl_hs_is_proper_fragment(ssl) == 1);
Hanno Becker37f95322018-08-16 13:55:32 +01004065
4066 /* We copy the message back into the input buffer
4067 * after reassembly, so check that it's not too large.
4068 * This is an implementation-specific limitation
4069 * and not one from the standard, hence it is not
4070 * checked in ssl_check_hs_header(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004071 if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) {
Hanno Becker37f95322018-08-16 13:55:32 +01004072 /* Ignore message */
4073 goto exit;
4074 }
4075
Hanno Beckere0b150f2018-08-21 15:51:03 +01004076 /* Check if we have enough space to buffer the message. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004077 if (hs->buffering.total_bytes_buffered >
4078 MBEDTLS_SSL_DTLS_MAX_BUFFERING) {
4079 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4080 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004081 }
4082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004083 reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len,
4084 hs_buf->is_fragmented);
Hanno Beckere0b150f2018-08-21 15:51:03 +01004085
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004086 if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4087 hs->buffering.total_bytes_buffered)) {
4088 if (recv_msg_seq_offset > 0) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004089 /* If we can't buffer a future message because
4090 * of space limitations -- ignore. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004091 MBEDTLS_SSL_DEBUG_MSG(2,
4092 ("Buffering of future message of size %"
4093 MBEDTLS_PRINTF_SIZET
4094 " would exceed the compile-time limit %"
4095 MBEDTLS_PRINTF_SIZET
4096 " (already %" MBEDTLS_PRINTF_SIZET
4097 " bytes buffered) -- ignore\n",
4098 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4099 hs->buffering.total_bytes_buffered));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004100 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004101 } else {
4102 MBEDTLS_SSL_DEBUG_MSG(2,
4103 ("Buffering of future message of size %"
4104 MBEDTLS_PRINTF_SIZET
4105 " would exceed the compile-time limit %"
4106 MBEDTLS_PRINTF_SIZET
4107 " (already %" MBEDTLS_PRINTF_SIZET
4108 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
4109 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4110 hs->buffering.total_bytes_buffered));
Hanno Beckere1801392018-08-21 16:51:05 +01004111 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004113 if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) {
4114 MBEDTLS_SSL_DEBUG_MSG(2,
4115 ("Reassembly of next message of size %"
4116 MBEDTLS_PRINTF_SIZET
4117 " (%" MBEDTLS_PRINTF_SIZET
4118 " with bitmap) would exceed"
4119 " the compile-time limit %"
4120 MBEDTLS_PRINTF_SIZET
4121 " (already %" MBEDTLS_PRINTF_SIZET
4122 " bytes buffered) -- fail\n",
4123 msg_len,
4124 reassembly_buf_sz,
4125 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4126 hs->buffering.total_bytes_buffered));
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004127 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4128 goto exit;
4129 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004130 }
4131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004132 MBEDTLS_SSL_DEBUG_MSG(2,
4133 ("initialize reassembly, total length = %"
4134 MBEDTLS_PRINTF_SIZET,
4135 msg_len));
Hanno Beckere0b150f2018-08-21 15:51:03 +01004136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004137 hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz);
4138 if (hs_buf->data == NULL) {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004139 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004140 goto exit;
4141 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004142 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004143
4144 /* Prepare final header: copy msg_type, length and message_seq,
4145 * then add standardised fragment_offset and fragment_length */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004146 memcpy(hs_buf->data, ssl->in_msg, 6);
4147 memset(hs_buf->data + 6, 0, 3);
4148 memcpy(hs_buf->data + 9, hs_buf->data + 1, 3);
Hanno Becker37f95322018-08-16 13:55:32 +01004149
4150 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004151
4152 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004153 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004154 /* Make sure msg_type and length are consistent */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004155 if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) {
4156 MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore"));
Hanno Becker37f95322018-08-16 13:55:32 +01004157 /* Ignore */
4158 goto exit;
4159 }
4160 }
4161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004162 if (!hs_buf->is_complete) {
Hanno Becker37f95322018-08-16 13:55:32 +01004163 size_t frag_len, frag_off;
4164 unsigned char * const msg = hs_buf->data + 12;
4165
4166 /*
4167 * Check and copy current fragment
4168 */
4169
4170 /* Validation of header fields already done in
4171 * mbedtls_ssl_prepare_handshake_record(). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004172 frag_off = ssl_get_hs_frag_off(ssl);
4173 frag_len = ssl_get_hs_frag_len(ssl);
Hanno Becker37f95322018-08-16 13:55:32 +01004174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004175 MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4176 ", length = %" MBEDTLS_PRINTF_SIZET,
4177 frag_off, frag_len));
4178 memcpy(msg + frag_off, ssl->in_msg + 12, frag_len);
Hanno Becker37f95322018-08-16 13:55:32 +01004179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004180 if (hs_buf->is_fragmented) {
Hanno Becker37f95322018-08-16 13:55:32 +01004181 unsigned char * const bitmask = msg + msg_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004182 ssl_bitmask_set(bitmask, frag_off, frag_len);
4183 hs_buf->is_complete = (ssl_bitmask_check(bitmask,
4184 msg_len) == 0);
4185 } else {
Hanno Becker37f95322018-08-16 13:55:32 +01004186 hs_buf->is_complete = 1;
4187 }
4188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004189 MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete",
4190 hs_buf->is_complete ? "" : "not yet "));
Hanno Becker37f95322018-08-16 13:55:32 +01004191 }
4192
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004193 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004194 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004195
4196 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004197 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004198 break;
4199 }
4200
4201exit:
4202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004203 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message"));
4204 return ret;
Hanno Becker40f50842018-08-15 14:48:01 +01004205}
4206#endif /* MBEDTLS_SSL_PROTO_DTLS */
4207
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004208MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004209static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004210{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004211 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004212 * Consume last content-layer message and potentially
4213 * update in_msglen which keeps track of the contents'
4214 * consumption state.
4215 *
4216 * (1) Handshake messages:
4217 * Remove last handshake message, move content
4218 * and adapt in_msglen.
4219 *
4220 * (2) Alert messages:
4221 * Consume whole record content, in_msglen = 0.
4222 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004223 * (3) Change cipher spec:
4224 * Consume whole record content, in_msglen = 0.
4225 *
4226 * (4) Application data:
4227 * Don't do anything - the record layer provides
4228 * the application data as a stream transport
4229 * and consumes through mbedtls_ssl_read only.
4230 *
4231 */
4232
4233 /* Case (1): Handshake messages */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004234 if (ssl->in_hslen != 0) {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004235 /* Hard assertion to be sure that no application data
4236 * is in flight, as corrupting ssl->in_msglen during
4237 * ssl->in_offt != NULL is fatal. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004238 if (ssl->in_offt != NULL) {
4239 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4240 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004241 }
4242
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004243 /*
4244 * Get next Handshake message in the current record
4245 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004246
Hanno Becker4a810fb2017-05-24 16:27:30 +01004247 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004248 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004249 * current handshake content: If DTLS handshake
4250 * fragmentation is used, that's the fragment
4251 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004252 * size here is faulty and should be changed at
4253 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004254 * (2) While it doesn't seem to cause problems, one
4255 * has to be very careful not to assume that in_hslen
4256 * is always <= in_msglen in a sensible communication.
4257 * Again, it's wrong for DTLS handshake fragmentation.
4258 * The following check is therefore mandatory, and
4259 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004260 * Additionally, ssl->in_hslen might be arbitrarily out of
4261 * bounds after handling a DTLS message with an unexpected
4262 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004263 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004264 if (ssl->in_hslen < ssl->in_msglen) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004265 ssl->in_msglen -= ssl->in_hslen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004266 memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4267 ssl->in_msglen);
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004269 MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
4270 ssl->in_msg, ssl->in_msglen);
4271 } else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004272 ssl->in_msglen = 0;
4273 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004274
Hanno Becker4a810fb2017-05-24 16:27:30 +01004275 ssl->in_hslen = 0;
4276 }
4277 /* Case (4): Application data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004278 else if (ssl->in_offt != NULL) {
4279 return 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01004280 }
4281 /* Everything else (CCS & Alerts) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004282 else {
Hanno Becker4a810fb2017-05-24 16:27:30 +01004283 ssl->in_msglen = 0;
4284 }
4285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004286 return 0;
Hanno Becker1097b342018-08-15 14:09:41 +01004287}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004288
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004289MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004290static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl)
Hanno Beckere74d5562018-08-15 14:26:08 +01004291{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004292 if (ssl->in_msglen > 0) {
4293 return 1;
4294 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004296 return 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004297}
4298
Hanno Becker5f066e72018-08-16 14:56:31 +01004299#if defined(MBEDTLS_SSL_PROTO_DTLS)
4300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004301static void ssl_free_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004302{
4303 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004304 if (hs == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004305 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004306 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004307
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004308 if (hs->buffering.future_record.data != NULL) {
Hanno Becker01315ea2018-08-21 17:22:17 +01004309 hs->buffering.total_bytes_buffered -=
4310 hs->buffering.future_record.len;
4311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004312 mbedtls_free(hs->buffering.future_record.data);
Hanno Becker01315ea2018-08-21 17:22:17 +01004313 hs->buffering.future_record.data = NULL;
4314 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004315}
4316
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004317MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004318static int ssl_load_buffered_record(mbedtls_ssl_context *ssl)
Hanno Becker5f066e72018-08-16 14:56:31 +01004319{
4320 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004321 unsigned char *rec;
Hanno Becker5f066e72018-08-16 14:56:31 +01004322 size_t rec_len;
4323 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004324#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4325 size_t in_buf_len = ssl->in_buf_len;
4326#else
4327 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4328#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004329 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4330 return 0;
4331 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004333 if (hs == NULL) {
4334 return 0;
4335 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004336
Hanno Becker5f066e72018-08-16 14:56:31 +01004337 rec = hs->buffering.future_record.data;
4338 rec_len = hs->buffering.future_record.len;
4339 rec_epoch = hs->buffering.future_record.epoch;
4340
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004341 if (rec == NULL) {
4342 return 0;
4343 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004344
Hanno Becker4cb782d2018-08-20 11:19:05 +01004345 /* Only consider loading future records if the
4346 * input buffer is empty. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004347 if (ssl_next_record_is_in_datagram(ssl) == 1) {
4348 return 0;
4349 }
Hanno Becker4cb782d2018-08-20 11:19:05 +01004350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004351 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004353 if (rec_epoch != ssl->in_epoch) {
4354 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch."));
Hanno Becker5f066e72018-08-16 14:56:31 +01004355 goto exit;
4356 }
4357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004358 MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load"));
Hanno Becker5f066e72018-08-16 14:56:31 +01004359
4360 /* Double-check that the record is not too large */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004361 if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) {
4362 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
4363 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Hanno Becker5f066e72018-08-16 14:56:31 +01004364 }
4365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004366 memcpy(ssl->in_hdr, rec, rec_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004367 ssl->in_left = rec_len;
4368 ssl->next_record_offset = 0;
4369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004370 ssl_free_buffered_record(ssl);
Hanno Becker5f066e72018-08-16 14:56:31 +01004371
4372exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004373 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record"));
4374 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004375}
4376
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004377MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004378static int ssl_buffer_future_record(mbedtls_ssl_context *ssl,
4379 mbedtls_record const *rec)
Hanno Becker5f066e72018-08-16 14:56:31 +01004380{
4381 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004382
4383 /* Don't buffer future records outside handshakes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004384 if (hs == NULL) {
4385 return 0;
4386 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004387
4388 /* Only buffer handshake records (we are only interested
4389 * in Finished messages). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004390 if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) {
4391 return 0;
4392 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004393
4394 /* Don't buffer more than one future epoch record. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004395 if (hs->buffering.future_record.data != NULL) {
4396 return 0;
4397 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004398
Hanno Becker01315ea2018-08-21 17:22:17 +01004399 /* Don't buffer record if there's not enough buffering space remaining. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004400 if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4401 hs->buffering.total_bytes_buffered)) {
4402 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4403 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4404 " (already %" MBEDTLS_PRINTF_SIZET
4405 " bytes buffered) -- ignore\n",
4406 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
4407 hs->buffering.total_bytes_buffered));
4408 return 0;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004409 }
4410
Hanno Becker5f066e72018-08-16 14:56:31 +01004411 /* Buffer record */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004412 MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u",
4413 ssl->in_epoch + 1U));
4414 MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004415
4416 /* ssl_parse_record_header() only considers records
4417 * of the next epoch as candidates for buffering. */
4418 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004419 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004420
4421 hs->buffering.future_record.data =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004422 mbedtls_calloc(1, hs->buffering.future_record.len);
4423 if (hs->buffering.future_record.data == NULL) {
Hanno Becker5f066e72018-08-16 14:56:31 +01004424 /* If we run out of RAM trying to buffer a
4425 * record from the next epoch, just ignore. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004426 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004427 }
4428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004429 memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len);
Hanno Becker5f066e72018-08-16 14:56:31 +01004430
Hanno Becker519f15d2019-07-11 12:43:20 +01004431 hs->buffering.total_bytes_buffered += rec->buf_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004432 return 0;
Hanno Becker5f066e72018-08-16 14:56:31 +01004433}
4434
4435#endif /* MBEDTLS_SSL_PROTO_DTLS */
4436
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02004437MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004438static int ssl_get_next_record(mbedtls_ssl_context *ssl)
Hanno Becker1097b342018-08-15 14:09:41 +01004439{
Janos Follath865b3eb2019-12-16 11:46:15 +00004440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004441 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004442
Hanno Becker5f066e72018-08-16 14:56:31 +01004443#if defined(MBEDTLS_SSL_PROTO_DTLS)
4444 /* We might have buffered a future record; if so,
4445 * and if the epoch matches now, load it.
4446 * On success, this call will set ssl->in_left to
4447 * the length of the buffered record, so that
4448 * the calls to ssl_fetch_input() below will
4449 * essentially be no-ops. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004450 ret = ssl_load_buffered_record(ssl);
4451 if (ret != 0) {
4452 return ret;
4453 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004454#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004455
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004456 /* Ensure that we have enough space available for the default form
4457 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4458 * with no space for CIDs counted in). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004459 ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl));
4460 if (ret != 0) {
4461 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4462 return ret;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004463 }
4464
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004465 ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec);
4466 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004468 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4469 if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) {
4470 ret = ssl_buffer_future_record(ssl, &rec);
4471 if (ret != 0) {
4472 return ret;
4473 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004474
4475 /* Fall through to handling of unexpected records */
4476 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4477 }
4478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004479 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) {
Hanno Becker2fddd372019-07-10 14:37:41 +01004480#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004481 /* Reset in pointers to default state for TLS/DTLS records,
4482 * assuming no CID and no offset between record content and
4483 * record plaintext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004484 mbedtls_ssl_update_in_pointers(ssl);
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004485
Hanno Becker7ae20e02019-07-12 08:33:49 +01004486 /* Setup internal message pointers from record structure. */
4487 ssl->in_msgtype = rec.type;
4488#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4489 ssl->in_len = ssl->in_cid + rec.cid_len;
4490#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4491 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4492 ssl->in_msglen = rec.data_len;
4493
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004494 ret = ssl_check_client_reconnect(ssl);
4495 MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret);
4496 if (ret != 0) {
4497 return ret;
4498 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004499#endif
4500
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004501 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004502 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004503
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004504 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record "
4505 "(header)"));
4506 } else {
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004507 /* Skip invalid record and the rest of the datagram */
4508 ssl->next_record_offset = 0;
4509 ssl->in_left = 0;
4510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004511 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record "
4512 "(header)"));
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004513 }
4514
4515 /* Get next record */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004516 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4517 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004518#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004519 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004520 return ret;
Hanno Becker2fddd372019-07-10 14:37:41 +01004521 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004522 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004525 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckera8814792019-07-10 15:01:45 +01004526 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004527 ssl->next_record_offset = rec.buf_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004528 if (ssl->next_record_offset < ssl->in_left) {
4529 MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram"));
Hanno Beckere65ce782017-05-22 14:47:48 +01004530 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004531 } else
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004532#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004533 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004534 /*
4535 * Fetch record contents from underlying transport.
4536 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004537 ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len);
4538 if (ret != 0) {
4539 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret);
4540 return ret;
Hanno Beckera8814792019-07-10 15:01:45 +01004541 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004542
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004543 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004544 }
4545
4546 /*
4547 * Decrypt record contents.
4548 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004550 if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004552 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004553 /* Silently discard invalid records */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004554 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004555 /* Except when waiting for Finished as a bad mac here
4556 * probably means something went wrong in the handshake
4557 * (eg wrong psk used, mitm downgrade attempt, etc.) */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004558 if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4559 ssl->state == MBEDTLS_SSL_SERVER_FINISHED) {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004560#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004561 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4562 mbedtls_ssl_send_alert_message(ssl,
4563 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4564 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004565 }
4566#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004567 return ret;
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004568 }
4569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004571 if (ssl->conf->badmac_limit != 0 &&
4572 ++ssl->badmac_seen >= ssl->conf->badmac_limit) {
4573 MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
4574 return MBEDTLS_ERR_SSL_INVALID_MAC;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004575 }
4576#endif
4577
Hanno Becker4a810fb2017-05-24 16:27:30 +01004578 /* As above, invalid records cause
4579 * dismissal of the whole datagram. */
4580
4581 ssl->next_record_offset = 0;
4582 ssl->in_left = 0;
4583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004584 MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)"));
4585 return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004586 }
4587
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004588 return ret;
4589 } else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004590#endif
4591 {
4592 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004594 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
4595 mbedtls_ssl_send_alert_message(ssl,
4596 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4597 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC);
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004598 }
4599#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004600 return ret;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004601 }
4602 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004603
Hanno Becker44d89b22019-07-12 09:40:44 +01004604
4605 /* Reset in pointers to default state for TLS/DTLS records,
4606 * assuming no CID and no offset between record content and
4607 * record plaintext. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004608 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker44d89b22019-07-12 09:40:44 +01004609#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4610 ssl->in_len = ssl->in_cid + rec.cid_len;
4611#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004612 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004613
Hanno Becker8685c822019-07-12 09:37:30 +01004614 /* The record content type may change during decryption,
4615 * so re-read it. */
4616 ssl->in_msgtype = rec.type;
4617 /* Also update the input buffer, because unfortunately
4618 * the server-side ssl_parse_client_hello() reparses the
4619 * record header when receiving a ClientHello initiating
4620 * a renegotiation. */
4621 ssl->in_hdr[0] = rec.type;
4622 ssl->in_msg = rec.buf + rec.data_offset;
4623 ssl->in_msglen = rec.data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004624 MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0);
Hanno Becker8685c822019-07-12 09:37:30 +01004625
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004626#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004627 if (ssl->transform_in != NULL &&
4628 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) {
4629 if ((ret = ssl_decompress_buf(ssl)) != 0) {
4630 MBEDTLS_SSL_DEBUG_RET(1, "ssl_decompress_buf", ret);
4631 return ret;
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004632 }
4633
4634 /* Check actual (decompress) record content length against
4635 * configured maximum. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004636 if (ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN) {
4637 MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length"));
4638 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004639 }
4640 }
4641#endif /* MBEDTLS_ZLIB_SUPPORT */
4642
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004643 return 0;
Simon Butcher99000142016-10-13 17:21:01 +01004644}
4645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004646int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl)
Simon Butcher99000142016-10-13 17:21:01 +01004647{
Janos Follath865b3eb2019-12-16 11:46:15 +00004648 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004649
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004650 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004651 * Handle particular types of records
4652 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004653 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
4654 if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) {
4655 return ret;
Simon Butcher99000142016-10-13 17:21:01 +01004656 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 }
4658
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004659 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4660 if (ssl->in_msglen != 1) {
4661 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
4662 ssl->in_msglen));
4663 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004664 }
4665
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004666 if (ssl->in_msg[0] != 1) {
4667 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x",
4668 ssl->in_msg[0]));
4669 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004670 }
4671
4672#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004673 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere678eaa2018-08-21 14:57:46 +01004674 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004675 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) {
4676 if (ssl->handshake == NULL) {
4677 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake"));
4678 return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004679 }
4680
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004681 MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember"));
4682 return MBEDTLS_ERR_SSL_EARLY_MESSAGE;
Hanno Beckere678eaa2018-08-21 14:57:46 +01004683 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004684#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004685 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004686
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004687 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
4688 if (ssl->in_msglen != 2) {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004689 /* Note: Standard allows for more than one 2 byte alert
4690 to be packed in a single message, but Mbed TLS doesn't
4691 currently support this. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004692 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
4693 ssl->in_msglen));
4694 return MBEDTLS_ERR_SSL_INVALID_RECORD;
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004695 }
4696
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004697 MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]",
4698 ssl->in_msg[0], ssl->in_msg[1]));
Paul Bakker5121ce52009-01-03 21:22:43 +00004699
4700 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004701 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004703 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) {
4704 MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)",
4705 ssl->in_msg[1]));
4706 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004707 }
4708
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004709 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4710 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) {
4711 MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message"));
4712 return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004714
4715#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004716 if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4717 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) {
4718 MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no renegotiation alert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004719 /* Will be handled when trying to parse ServerHello */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004720 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004721 }
4722#endif
4723
4724#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004725 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004726 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4727 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004728 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) {
4729 MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no_cert"));
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004730 /* Will be handled in mbedtls_ssl_parse_certificate() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004731 return 0;
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004732 }
4733#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4734
4735 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004736 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 }
4738
Hanno Beckerc76c6192017-06-06 10:03:17 +01004739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004740 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker37ae9522019-05-03 16:54:26 +01004741 /* Drop unexpected ApplicationData records,
4742 * except at the beginning of renegotiations */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004743 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
Hanno Becker37ae9522019-05-03 16:54:26 +01004744 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4745#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004746 && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4747 ssl->state == MBEDTLS_SSL_SERVER_HELLO)
Hanno Beckerc76c6192017-06-06 10:03:17 +01004748#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004749 ) {
4750 MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData"));
4751 return MBEDTLS_ERR_SSL_NON_FATAL;
Hanno Becker37ae9522019-05-03 16:54:26 +01004752 }
4753
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004754 if (ssl->handshake != NULL &&
4755 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
4756 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Hanno Becker37ae9522019-05-03 16:54:26 +01004757 }
4758 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004759#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004760
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004761 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004762}
4763
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004764int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004765{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004766 return mbedtls_ssl_send_alert_message(ssl,
4767 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4768 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004769}
4770
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004771int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl,
4772 unsigned char level,
4773 unsigned char message)
Paul Bakker0a925182012-04-16 06:46:41 +00004774{
Janos Follath865b3eb2019-12-16 11:46:15 +00004775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004776
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004777 if (ssl == NULL || ssl->conf == NULL) {
4778 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4779 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004780
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004781 if (ssl->out_left != 0) {
4782 return mbedtls_ssl_flush_output(ssl);
4783 }
Hanno Beckerd9c66c02018-08-06 11:35:16 +01004784
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004785 MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message"));
4786 MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message));
Paul Bakker0a925182012-04-16 06:46:41 +00004787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004789 ssl->out_msglen = 2;
4790 ssl->out_msg[0] = level;
4791 ssl->out_msg[1] = message;
4792
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004793 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
4794 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
4795 return ret;
Paul Bakker0a925182012-04-16 06:46:41 +00004796 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004797 MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message"));
Paul Bakker0a925182012-04-16 06:46:41 +00004798
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004799 return 0;
Paul Bakker0a925182012-04-16 06:46:41 +00004800}
4801
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004802int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004803{
Janos Follath865b3eb2019-12-16 11:46:15 +00004804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004805
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004806 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004809 ssl->out_msglen = 1;
4810 ssl->out_msg[0] = 1;
4811
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 ssl->state++;
4813
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004814 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
4815 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
4816 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004817 }
4818
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004819 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004820
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004821 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004822}
4823
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004824int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004825{
Janos Follath865b3eb2019-12-16 11:46:15 +00004826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004827
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004828 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004829
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004830 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
4831 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
4832 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 }
4834
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004835 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) {
4836 MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message"));
4837 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4838 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
4839 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 }
4841
Hanno Beckere678eaa2018-08-21 14:57:46 +01004842 /* CCS records are only accepted if they have length 1 and content '1',
4843 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004844
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004845 /*
4846 * Switch to our negotiated transform and session parameters for inbound
4847 * data.
4848 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004849 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data"));
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004850 ssl->transform_in = ssl->transform_negotiate;
4851 ssl->session_in = ssl->session_negotiate;
4852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004854 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004856 mbedtls_ssl_dtls_replay_reset(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004857#endif
4858
4859 /* Increment epoch */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004860 if (++ssl->in_epoch == 0) {
4861 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004862 /* This is highly unlikely to happen for legitimate reasons, so
4863 treat it as an attack and don't send an alert. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004864 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004865 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004866 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004867#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004868 memset(ssl->in_ctr, 0, 8);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004869
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004870 mbedtls_ssl_update_in_pointers(ssl);
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004872#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004873 if (mbedtls_ssl_hw_record_activate != NULL) {
4874 if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_INBOUND)) != 0) {
4875 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret);
4876 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4877 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
4878 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004879 }
4880 }
4881#endif
4882
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 ssl->state++;
4884
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004885 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004886
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004887 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00004888}
4889
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004890/* Once ssl->out_hdr as the address of the beginning of the
4891 * next outgoing record is set, deduce the other pointers.
4892 *
4893 * Note: For TLS, we save the implicit record sequence number
4894 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4895 * and the caller has to make sure there's space for this.
4896 */
4897
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004898static size_t ssl_transform_get_explicit_iv_len(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004899 mbedtls_ssl_transform const *transform)
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004900{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004901 if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) {
4902 return 0;
4903 }
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004904
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004905 return transform->ivlen - transform->fixed_ivlen;
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004906}
4907
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004908void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
4909 mbedtls_ssl_transform *transform)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004910{
4911#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004912 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004913 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004914#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004915 ssl->out_cid = ssl->out_ctr + 8;
4916 ssl->out_len = ssl->out_cid;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004917 if (transform != NULL) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004918 ssl->out_len += transform->out_cid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004919 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01004920#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004921 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004922#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004923 ssl->out_iv = ssl->out_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004924 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004925#endif
4926 {
4927 ssl->out_ctr = ssl->out_hdr - 8;
4928 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004929#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004930 ssl->out_cid = ssl->out_len;
4931#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004932 ssl->out_iv = ssl->out_hdr + 5;
4933 }
4934
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004935 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004936 /* Adjust out_msg to make space for explicit IV, if used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004937 if (transform != NULL) {
4938 ssl->out_msg += ssl_transform_get_explicit_iv_len(transform);
4939 }
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004940}
4941
4942/* Once ssl->in_hdr as the address of the beginning of the
4943 * next incoming record is set, deduce the other pointers.
4944 *
4945 * Note: For TLS, we save the implicit record sequence number
4946 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4947 * and the caller has to make sure there's space for this.
4948 */
4949
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004950void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004951{
Hanno Becker79594fd2019-05-08 09:38:41 +01004952 /* This function sets the pointers to match the case
4953 * of unprotected TLS/DTLS records, with both ssl->in_iv
4954 * and ssl->in_msg pointing to the beginning of the record
4955 * content.
4956 *
4957 * When decrypting a protected record, ssl->in_msg
4958 * will be shifted to point to the beginning of the
4959 * record plaintext.
4960 */
4961
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004963 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004964 /* This sets the header pointers to match records
4965 * without CID. When we receive a record containing
4966 * a CID, the fields are shifted accordingly in
4967 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004968 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004969#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004970 ssl->in_cid = ssl->in_ctr + 8;
4971 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004972#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004973 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004974#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004975 ssl->in_iv = ssl->in_len + 2;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004976 } else
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004977#endif
4978 {
4979 ssl->in_ctr = ssl->in_hdr - 8;
4980 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004981#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004982 ssl->in_cid = ssl->in_len;
4983#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004984 ssl->in_iv = ssl->in_hdr + 5;
4985 }
4986
Hanno Becker79594fd2019-05-08 09:38:41 +01004987 /* This will be adjusted at record decryption time. */
4988 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004989}
4990
Paul Bakker5121ce52009-01-03 21:22:43 +00004991/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004992 * Setup an SSL context
4993 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004994
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004995void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004996{
4997 /* Set the incoming and outgoing record pointers. */
4998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01004999 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005000 ssl->out_hdr = ssl->out_buf;
5001 ssl->in_hdr = ssl->in_buf;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005002 } else
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005003#endif /* MBEDTLS_SSL_PROTO_DTLS */
5004 {
5005 ssl->out_hdr = ssl->out_buf + 8;
5006 ssl->in_hdr = ssl->in_buf + 8;
5007 }
5008
5009 /* Derive other internal pointers. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005010 mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
5011 mbedtls_ssl_update_in_pointers(ssl);
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005012}
5013
Paul Bakker5121ce52009-01-03 21:22:43 +00005014/*
5015 * SSL get accessors
5016 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005017size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005018{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005019 return ssl->in_offt == NULL ? 0 : ssl->in_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00005020}
5021
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005022int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl)
Hanno Becker8b170a02017-10-10 11:51:19 +01005023{
5024 /*
5025 * Case A: We're currently holding back
5026 * a message for further processing.
5027 */
5028
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005029 if (ssl->keep_current_message == 1) {
5030 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing"));
5031 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005032 }
5033
5034 /*
5035 * Case B: Further records are pending in the current datagram.
5036 */
5037
5038#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005039 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5040 ssl->in_left > ssl->next_record_offset) {
5041 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram"));
5042 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005043 }
5044#endif /* MBEDTLS_SSL_PROTO_DTLS */
5045
5046 /*
5047 * Case C: A handshake message is being processed.
5048 */
5049
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005050 if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) {
5051 MBEDTLS_SSL_DEBUG_MSG(3,
5052 ("ssl_check_pending: more handshake messages within current record"));
5053 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005054 }
5055
5056 /*
5057 * Case D: An application data message is being processed
5058 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005059 if (ssl->in_offt != NULL) {
5060 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed"));
5061 return 1;
Hanno Becker8b170a02017-10-10 11:51:19 +01005062 }
5063
5064 /*
5065 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005066 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005067 * we implement support for multiple alerts in single records.
5068 */
5069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005070 MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending"));
5071 return 0;
Hanno Becker8b170a02017-10-10 11:51:19 +01005072}
5073
Paul Bakker43ca69c2011-01-15 17:35:19 +00005074
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005075int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005076{
Hanno Becker3136ede2018-08-17 15:28:19 +01005077 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005078 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005079 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005081 size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
Hanno Becker5903de42019-05-03 14:46:38 +01005082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005083 if (transform == NULL) {
5084 return (int) out_hdr_len;
5085 }
Hanno Becker78640902018-08-13 16:35:15 +01005086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005088 if (ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL) {
5089 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
5090 }
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005091#endif
5092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005093 switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005094 case MBEDTLS_MODE_GCM:
5095 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005096 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005098 transform_expansion = transform->minlen;
5099 break;
5100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005102
5103 block_size = mbedtls_cipher_get_block_size(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005104 &transform->cipher_ctx_enc);
Hanno Becker5b559ac2018-08-03 09:40:07 +01005105
Hanno Becker3136ede2018-08-17 15:28:19 +01005106 /* Expansion due to the addition of the MAC. */
5107 transform_expansion += transform->maclen;
5108
5109 /* Expansion due to the addition of CBC padding;
5110 * Theoretically up to 256 bytes, but we never use
5111 * more than the block size of the underlying cipher. */
5112 transform_expansion += block_size;
5113
5114 /* For TLS 1.1 or higher, an explicit IV is added
5115 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005116#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005117 if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) {
Hanno Becker3136ede2018-08-17 15:28:19 +01005118 transform_expansion += block_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005119 }
Hanno Becker5b559ac2018-08-03 09:40:07 +01005120#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005121
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005122 break;
5123
5124 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005125 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5126 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005127 }
5128
Hanno Beckera0e20d02019-05-15 14:03:01 +01005129#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005130 if (transform->out_cid_len != 0) {
Hanno Becker6cbad552019-05-08 15:40:11 +01005131 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005132 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005133#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005135 return (int) (out_hdr_len + transform_expansion);
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005136}
5137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005139/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005140 * Check record counters and renegotiate if they're above the limit.
5141 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005142MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005143static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005144{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005145 size_t ep_len = mbedtls_ssl_ep_len(ssl);
Andres AG2196c7f2016-12-15 17:01:16 +00005146 int in_ctr_cmp;
5147 int out_ctr_cmp;
5148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005149 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005150 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005151 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
5152 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005153 }
5154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005155 in_ctr_cmp = memcmp(ssl->in_ctr + ep_len,
5156 ssl->conf->renego_period + ep_len, 8 - ep_len);
5157 out_ctr_cmp = memcmp(ssl->cur_out_ctr + ep_len,
5158 ssl->conf->renego_period + ep_len, 8 - ep_len);
Andres AG2196c7f2016-12-15 17:01:16 +00005159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005160 if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) {
5161 return 0;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005162 }
5163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005164 MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate"));
5165 return mbedtls_ssl_renegotiate(ssl);
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005168
5169/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005170 * Receive application data decrypted from the SSL layer
5171 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005172int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005173{
Janos Follath865b3eb2019-12-16 11:46:15 +00005174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005175 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005177 if (ssl == NULL || ssl->conf == NULL) {
5178 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5179 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005181 MBEDTLS_SSL_DEBUG_MSG(2, ("=> read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005184 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5185 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5186 return ret;
5187 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005189 if (ssl->handshake != NULL &&
5190 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
5191 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
5192 return ret;
5193 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005194 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005195 }
5196#endif
5197
Hanno Becker4a810fb2017-05-24 16:27:30 +01005198 /*
5199 * Check if renegotiation is necessary and/or handshake is
5200 * in process. If yes, perform/continue, and fall through
5201 * if an unexpected packet is received while the client
5202 * is waiting for the ServerHello.
5203 *
5204 * (There is no equivalent to the last condition on
5205 * the server-side as it is not treated as within
5206 * a handshake while waiting for the ClientHello
5207 * after a renegotiation request.)
5208 */
5209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005211 ret = ssl_check_ctr_renegotiate(ssl);
5212 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5213 ret != 0) {
5214 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5215 return ret;
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005216 }
5217#endif
5218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005219 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5220 ret = mbedtls_ssl_handshake(ssl);
5221 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5222 ret != 0) {
5223 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5224 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005225 }
5226 }
5227
Hanno Beckere41158b2017-10-23 13:30:32 +01005228 /* Loop as long as no application data record is available */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005229 while (ssl->in_offt == NULL) {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005230 /* Start timer if not already running */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005231 if (ssl->f_get_timer != NULL &&
5232 ssl->f_get_timer(ssl->p_timer) == -1) {
5233 mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout);
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005234 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005235
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005236 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5237 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5238 return 0;
5239 }
Paul Bakker831a7552011-05-18 13:32:51 +00005240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005241 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5242 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 }
5244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005245 if (ssl->in_msglen == 0 &&
5246 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Paul Bakker5121ce52009-01-03 21:22:43 +00005247 /*
5248 * OpenSSL sends empty messages to randomize the IV
5249 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005250 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
5251 if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
5252 return 0;
5253 }
Paul Bakker831a7552011-05-18 13:32:51 +00005254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005255 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
5256 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005257 }
5258 }
5259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005260 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) {
5261 MBEDTLS_SSL_DEBUG_MSG(1, ("received handshake message"));
Paul Bakker48916f92012-09-16 19:57:18 +00005262
Hanno Becker4a810fb2017-05-24 16:27:30 +01005263 /*
5264 * - For client-side, expect SERVER_HELLO_REQUEST.
5265 * - For server-side, expect CLIENT_HELLO.
5266 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5267 */
5268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005270 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5271 (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5272 ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) {
5273 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)"));
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005274
5275 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005277 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker90333da2017-10-10 11:27:13 +01005278 continue;
5279 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005280#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005281 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005282 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005283#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005284
Hanno Becker4a810fb2017-05-24 16:27:30 +01005285#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005286 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5287 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) {
5288 MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)"));
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005289
5290 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005291#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005292 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Hanno Becker90333da2017-10-10 11:27:13 +01005293 continue;
5294 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005295#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005296 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker48916f92012-09-16 19:57:18 +00005297 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005298#endif /* MBEDTLS_SSL_SRV_C */
5299
Hanno Becker21df7f92017-10-17 11:03:26 +01005300#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005301 /* Determine whether renegotiation attempt should be accepted */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005302 if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5303 (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5304 ssl->conf->allow_legacy_renegotiation ==
5305 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) {
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005306 /*
5307 * Accept renegotiation request
5308 */
Paul Bakker48916f92012-09-16 19:57:18 +00005309
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005310 /* DTLS clients need to know renego is server-initiated */
5311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005312 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5313 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005314 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5315 }
5316#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005317 ret = mbedtls_ssl_start_renegotiation(ssl);
5318 if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5319 ret != 0) {
5320 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation",
5321 ret);
5322 return ret;
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005323 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005324 } else
Hanno Becker21df7f92017-10-17 11:03:26 +01005325#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005326 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005327 /*
5328 * Refuse renegotiation
5329 */
5330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005331 MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert"));
Paul Bakker48916f92012-09-16 19:57:18 +00005332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333#if defined(MBEDTLS_SSL_PROTO_SSL3)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005334 if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) {
Gilles Peskine92e44262017-05-10 17:27:49 +02005335 /* SSLv3 does not have a "no_renegotiation" warning, so
5336 we send a fatal alert and abort the connection. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005337 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5338 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
5339 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
5340 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5342#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005343 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5344 if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) {
5345 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5346 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5347 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION))
5348 != 0) {
5349 return ret;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005350 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005351 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005352#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5353 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005354 {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005355 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
5356 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakker577e0062013-08-28 11:57:20 +02005357 }
Paul Bakker48916f92012-09-16 19:57:18 +00005358 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005359
Hanno Becker90333da2017-10-10 11:27:13 +01005360 /* At this point, we don't know whether the renegotiation has been
5361 * completed or not. The cases to consider are the following:
5362 * 1) The renegotiation is complete. In this case, no new record
5363 * has been read yet.
5364 * 2) The renegotiation is incomplete because the client received
5365 * an application data record while awaiting the ServerHello.
5366 * 3) The renegotiation is incomplete because the client received
5367 * a non-handshake, non-application data message while awaiting
5368 * the ServerHello.
5369 * In each of these case, looping will be the proper action:
5370 * - For 1), the next iteration will read a new record and check
5371 * if it's application data.
5372 * - For 2), the loop condition isn't satisfied as application data
5373 * is present, hence continue is the same as break
5374 * - For 3), the loop condition is satisfied and read_record
5375 * will re-deliver the message that was held back by the client
5376 * when expecting the ServerHello.
5377 */
5378 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005379 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005380#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005381 else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5382 if (ssl->conf->renego_max_records >= 0) {
5383 if (++ssl->renego_records_seen > ssl->conf->renego_max_records) {
5384 MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, "
5385 "but not honored by client"));
5386 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005387 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005388 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005389 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005393 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) {
5394 MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert"));
5395 return MBEDTLS_ERR_SSL_WANT_READ;
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005396 }
5397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005398 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) {
5399 MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message"));
5400 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005401 }
5402
5403 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005404
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005405 /* We're going to return something now, cancel timer,
5406 * except if handshake (renegotiation) is in progress */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005407 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
5408 mbedtls_ssl_set_timer(ssl, 0);
5409 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005410
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005411#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005412 /* If we requested renego but received AppData, resend HelloRequest.
5413 * Do it now, after setting in_offt, to avoid taking this branch
5414 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005416 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5417 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
5418 if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) {
5419 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request",
5420 ret);
5421 return ret;
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005422 }
5423 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005424#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005425#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005426 }
5427
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005428 n = (len < ssl->in_msglen)
Paul Bakker5121ce52009-01-03 21:22:43 +00005429 ? len : ssl->in_msglen;
5430
ashesmancf01d782022-02-17 11:08:27 +13005431 if (len != 0) {
Ashley Duncan272cc192022-02-11 09:57:18 +13005432 memcpy(buf, ssl->in_offt, n);
5433 ssl->in_msglen -= n;
5434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
gabor-mezei-arma3214132020-07-15 10:55:00 +02005436 /* Zeroising the plaintext buffer to erase unused application data
5437 from the memory. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005438 mbedtls_platform_zeroize(ssl->in_offt, n);
gabor-mezei-arma3214132020-07-15 10:55:00 +02005439
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005440 if (ssl->in_msglen == 0) {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005441 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005442 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005443 ssl->keep_current_message = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005444 } else {
Paul Bakker5121ce52009-01-03 21:22:43 +00005445 /* more data available */
5446 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005448
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005449 MBEDTLS_SSL_DEBUG_MSG(2, ("<= read"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005450
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005451 return (int) n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005452}
5453
5454/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005455 * Send application data to be encrypted by the SSL layer, taking care of max
5456 * fragment length and buffer size.
5457 *
5458 * According to RFC 5246 Section 6.2.1:
5459 *
5460 * Zero-length fragments of Application data MAY be sent as they are
5461 * potentially useful as a traffic analysis countermeasure.
5462 *
5463 * Therefore, it is possible that the input message length is 0 and the
5464 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 */
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005467static int ssl_write_real(mbedtls_ssl_context *ssl,
5468 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +00005469{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005470 int ret = mbedtls_ssl_get_max_out_record_payload(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005471 const size_t max_len = (size_t) ret;
5472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005473 if (ret < 0) {
5474 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret);
5475 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005476 }
5477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005478 if (len > max_len) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005480 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5481 MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) "
5482 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5483 " > %" MBEDTLS_PRINTF_SIZET,
5484 len, max_len));
5485 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5486 } else
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005487#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005488 len = max_len;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005489 }
Paul Bakker887bd502011-06-08 13:10:54 +00005490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005491 if (ssl->out_left != 0) {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005492 /*
5493 * The user has previously tried to send the data and
5494 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5495 * written. In this case, we expect the high-level write function
5496 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5497 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005498 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
5499 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret);
5500 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005501 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005502 } else {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005503 /*
5504 * The user is trying to send a message the first time, so we need to
5505 * copy the data into the internal buffers and setup the data structure
5506 * to keep track of partial writes
5507 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005508 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Dave Rodgman12155572023-02-24 15:41:34 +00005510 if (len > 0) {
5511 memcpy(ssl->out_msg, buf, len);
5512 }
Paul Bakker887bd502011-06-08 13:10:54 +00005513
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005514 if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) {
5515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret);
5516 return ret;
Paul Bakker887bd502011-06-08 13:10:54 +00005517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005518 }
5519
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005520 return (int) len;
Paul Bakker5121ce52009-01-03 21:22:43 +00005521}
5522
5523/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005524 * Write application data, doing 1/n-1 splitting if necessary.
5525 *
5526 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005527 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005528 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardd904d662022-06-17 10:24:00 +02005531MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005532static int ssl_write_split(mbedtls_ssl_context *ssl,
5533 const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005534{
Janos Follath865b3eb2019-12-16 11:46:15 +00005535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005536
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005537 if (ssl->conf->cbc_record_splitting ==
5538 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005539 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005541 mbedtls_cipher_get_cipher_mode(&ssl->transform_out->cipher_ctx_enc)
5542 != MBEDTLS_MODE_CBC) {
5543 return ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005544 }
5545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005546 if (ssl->split_done == 0) {
5547 if ((ret = ssl_write_real(ssl, buf, 1)) <= 0) {
5548 return ret;
5549 }
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005550 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005551 }
5552
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005553 if ((ret = ssl_write_real(ssl, buf + 1, len - 1)) <= 0) {
5554 return ret;
5555 }
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005556 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005557
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005558 return ret + 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005559}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005561
5562/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005563 * Write application data (public-facing wrapper)
5564 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005565int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005566{
Janos Follath865b3eb2019-12-16 11:46:15 +00005567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005569 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005570
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005571 if (ssl == NULL || ssl->conf == NULL) {
5572 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5573 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005574
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005576 if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) {
5577 MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret);
5578 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005579 }
5580#endif
5581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005582 if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
5583 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
5584 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
5585 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005586 }
5587 }
5588
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005589#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005590 ret = ssl_write_split(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005591#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005592 ret = ssl_write_real(ssl, buf, len);
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005593#endif
5594
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005595 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write"));
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005596
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005597 return ret;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005598}
5599
5600/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005601 * Notify the peer that the connection is being closed
5602 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005603int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00005604{
Janos Follath865b3eb2019-12-16 11:46:15 +00005605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005606
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005607 if (ssl == NULL || ssl->conf == NULL) {
5608 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
5609 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005610
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005611 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005612
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005613 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
5614 if ((ret = mbedtls_ssl_send_alert_message(ssl,
5615 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5616 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) {
5617 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret);
5618 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00005619 }
5620 }
5621
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005622 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify"));
Paul Bakker5121ce52009-01-03 21:22:43 +00005623
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005624 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00005625}
5626
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005627void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
Paul Bakker48916f92012-09-16 19:57:18 +00005628{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005629 if (transform == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005630 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005631 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633#if defined(MBEDTLS_ZLIB_SUPPORT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005634 deflateEnd(&transform->ctx_deflate);
5635 inflateEnd(&transform->ctx_inflate);
Paul Bakker48916f92012-09-16 19:57:18 +00005636#endif
5637
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005638 mbedtls_cipher_free(&transform->cipher_ctx_enc);
5639 mbedtls_cipher_free(&transform->cipher_ctx_dec);
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005640
Hanno Beckerd56ed242018-01-03 15:32:51 +00005641#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005642 mbedtls_md_free(&transform->md_ctx_enc);
5643 mbedtls_md_free(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00005644#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005645
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005646 mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
Paul Bakker48916f92012-09-16 19:57:18 +00005647}
5648
Hanno Becker0271f962018-08-16 13:23:47 +01005649#if defined(MBEDTLS_SSL_PROTO_DTLS)
5650
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005651void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl)
Hanno Becker0271f962018-08-16 13:23:47 +01005652{
5653 unsigned offset;
5654 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5655
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005656 if (hs == NULL) {
Hanno Becker0271f962018-08-16 13:23:47 +01005657 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005658 }
Hanno Becker0271f962018-08-16 13:23:47 +01005659
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005660 ssl_free_buffered_record(ssl);
Hanno Becker283f5ef2018-08-24 09:34:47 +01005661
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005662 for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) {
5663 ssl_buffering_free_slot(ssl, offset);
5664 }
Hanno Beckere605b192018-08-21 15:59:07 +01005665}
5666
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005667static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl,
5668 uint8_t slot)
Hanno Beckere605b192018-08-21 15:59:07 +01005669{
5670 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5671 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005672
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005673 if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) {
Hanno Beckerb309b922018-08-23 13:18:05 +01005674 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005675 }
Hanno Beckerb309b922018-08-23 13:18:05 +01005676
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005677 if (hs_buf->is_valid == 1) {
Hanno Beckere605b192018-08-21 15:59:07 +01005678 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005679 mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len);
5680 mbedtls_free(hs_buf->data);
5681 memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer));
Hanno Becker0271f962018-08-16 13:23:47 +01005682 }
5683}
5684
5685#endif /* MBEDTLS_SSL_PROTO_DTLS */
5686
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005687/*
5688 * Convert version numbers to/from wire format
5689 * and, for DTLS, to/from TLS equivalent.
5690 *
5691 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005692 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005693 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5694 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5695 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005696void mbedtls_ssl_write_version(int major, int minor, int transport,
5697 unsigned char ver[2])
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005700 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
5701 if (minor == MBEDTLS_SSL_MINOR_VERSION_2) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005702 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5703
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005704 }
5705 ver[0] = (unsigned char) (255 - (major - 2));
5706 ver[1] = (unsigned char) (255 - (minor - 1));
5707 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005708#else
5709 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005710#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005711 {
5712 ver[0] = (unsigned char) major;
5713 ver[1] = (unsigned char) minor;
5714 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005715}
5716
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005717void mbedtls_ssl_read_version(int *major, int *minor, int transport,
5718 const unsigned char ver[2])
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005721 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005722 *major = 255 - ver[0] + 2;
5723 *minor = 255 - ver[1] + 1;
5724
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005725 if (*minor == MBEDTLS_SSL_MINOR_VERSION_1) {
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005726 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01005727 }
5728 } else
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005729#else
5730 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005731#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005732 {
5733 *major = ver[0];
5734 *minor = ver[1];
5735 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005736}
5737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#endif /* MBEDTLS_SSL_TLS_C */