blob: ecf7a2b4aa491f0537347f8411391dbc1381ab47 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Hanno Beckerf1a38282020-02-05 16:14:29 +00002 * Generic SSL/TLS messaging layer functions
3 * (record layer + retransmission state machine)
Paul Bakker5121ce52009-01-03 21:22:43 +00004 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000019 */
20/*
Paul Bakker5121ce52009-01-03 21:22:43 +000021 * http://www.ietf.org/rfc/rfc2246.txt
22 * http://www.ietf.org/rfc/rfc4346.txt
23 */
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000028
SimonBd5800b72016-04-26 07:43:27 +010029#if defined(MBEDTLS_PLATFORM_C)
30#include "mbedtls/platform.h"
31#else
32#include <stdlib.h>
33#define mbedtls_calloc calloc
34#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010035#endif
36
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ssl.h"
Chris Jones84a773f2021-03-05 18:38:47 +000038#include "ssl_misc.h"
Janos Follath73c616b2019-12-18 15:07:04 +000039#include "mbedtls/debug.h"
40#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050041#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010042#include "mbedtls/version.h"
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020043#include "constant_time_internal.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020044#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020045
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050048#if defined(MBEDTLS_USE_PSA_CRYPTO)
49#include "mbedtls/psa_util.h"
50#include "psa/crypto.h"
51#endif
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Beckercd9dcda2018-08-28 17:18:56 +010057static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020059/*
60 * Start a timer.
61 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020062 */
Hanno Becker0f57a652020-02-05 10:37:26 +000063void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020064{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020065 if( ssl->f_set_timer == NULL )
66 return;
67
68 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
69 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020070}
71
72/*
73 * Return -1 is timer is expired, 0 if it isn't.
74 */
Hanno Becker7876d122020-02-05 10:39:31 +000075int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020078 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020079
80 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020081 {
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020084 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020085
86 return( 0 );
87}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +020089MBEDTLS_CHECK_RETURN_CRITICAL
TRodziewicz4ca18aa2021-05-20 14:46:20 +020090static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
91 unsigned char *buf,
92 size_t len,
93 mbedtls_record *rec );
94
95int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
96 unsigned char *buf,
97 size_t buflen )
98{
99 int ret = 0;
100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
101 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
102
103 /* We don't support record checking in TLS because
TRodziewicz2abf03c2021-06-25 14:40:09 +0200104 * there doesn't seem to be a usecase for it.
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200105 */
106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
107 {
108 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
109 goto exit;
110 }
111#if defined(MBEDTLS_SSL_PROTO_DTLS)
112 else
113 {
114 mbedtls_record rec;
115
116 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
117 if( ret != 0 )
118 {
119 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
120 goto exit;
121 }
122
123 if( ssl->transform_in != NULL )
124 {
125 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
126 if( ret != 0 )
127 {
128 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
129 goto exit;
130 }
131 }
132 }
133#endif /* MBEDTLS_SSL_PROTO_DTLS */
134
135exit:
136 /* On success, we have decrypted the buffer in-place, so make
137 * sure we don't leak any plaintext data. */
138 mbedtls_platform_zeroize( buf, buflen );
139
140 /* For the purpose of this API, treat messages with unexpected CID
141 * as well as such from future epochs as unexpected. */
142 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
143 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
144 {
145 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
146 }
147
148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
149 return( ret );
150}
151
Hanno Becker67bc7c32018-08-06 11:33:50 +0100152#define SSL_DONT_FORCE_FLUSH 0
153#define SSL_FORCE_FLUSH 1
154
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100156
Hanno Beckerd5847772018-08-28 10:09:23 +0100157/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100158static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
159 uint8_t slot );
160static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200161MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerd5847772018-08-28 10:09:23 +0100162static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200163MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerd5847772018-08-28 10:09:23 +0100164static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200165MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerd5847772018-08-28 10:09:23 +0100166static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200167MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker519f15d2019-07-11 12:43:20 +0100168static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
169 mbedtls_record const *rec );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200170MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100171static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100172
Hanno Becker11682cc2018-08-22 14:41:02 +0100173static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100174{
Hanno Becker89490712020-02-05 10:50:12 +0000175 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000176#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
177 size_t out_buf_len = ssl->out_buf_len;
178#else
179 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
180#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100181
Darryl Greenb33cc762019-11-28 14:29:44 +0000182 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100183 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184
Darryl Greenb33cc762019-11-28 14:29:44 +0000185 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100186}
187
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200188MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker67bc7c32018-08-06 11:33:50 +0100189static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
190{
Hanno Becker11682cc2018-08-22 14:41:02 +0100191 size_t const bytes_written = ssl->out_left;
192 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100193
194 /* Double-check that the write-index hasn't gone
195 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100196 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100197 {
198 /* Should never happen... */
199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
200 }
201
202 return( (int) ( mtu - bytes_written ) );
203}
204
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200205MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker67bc7c32018-08-06 11:33:50 +0100206static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
207{
Janos Follath865b3eb2019-12-16 11:46:15 +0000208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400210 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211
212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400213 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 if( max_len > mfl )
216 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100217
218 /* By the standard (RFC 6066 Sect. 4), the MFL extension
219 * only limits the maximum record payload size, so in theory
220 * we would be allowed to pack multiple records of payload size
221 * MFL into a single datagram. However, this would mean that there's
222 * no way to explicitly communicate MTU restrictions to the peer.
223 *
224 * The following reduction of max_len makes sure that we never
225 * write datagrams larger than MFL + Record Expansion Overhead.
226 */
227 if( max_len <= ssl->out_left )
228 return( 0 );
229
230 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231#endif
232
233 ret = ssl_get_remaining_space_in_datagram( ssl );
234 if( ret < 0 )
235 return( ret );
236 remaining = (size_t) ret;
237
238 ret = mbedtls_ssl_get_record_expansion( ssl );
239 if( ret < 0 )
240 return( ret );
241 expansion = (size_t) ret;
242
243 if( remaining <= expansion )
244 return( 0 );
245
246 remaining -= expansion;
247 if( remaining >= max_len )
248 remaining = max_len;
249
250 return( (int) remaining );
251}
252
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200253/*
254 * Double the retransmit timeout value, within the allowed range,
255 * returning -1 if the maximum value has already been reached.
256 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200257MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200259{
260 uint32_t new_timeout;
261
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200262 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200263 return( -1 );
264
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200265 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
266 * in the following way: after the initial transmission and a first
267 * retransmission, back off to a temporary estimated MTU of 508 bytes.
268 * This value is guaranteed to be deliverable (if not guaranteed to be
269 * delivered) of any compliant IPv4 (and IPv6) network, and should work
270 * on most non-IP stacks too. */
271 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400272 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200273 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
275 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200276
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200277 new_timeout = 2 * ssl->handshake->retransmit_timeout;
278
279 /* Avoid arithmetic overflow and range overflow */
280 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200281 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200282 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200283 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284 }
285
286 ssl->handshake->retransmit_timeout = new_timeout;
Paul Elliott9f352112020-12-09 14:55:45 +0000287 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
288 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200289
290 return( 0 );
291}
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200294{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200295 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Paul Elliott9f352112020-12-09 14:55:45 +0000296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
297 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200300
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100301/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200303 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000304
Ronald Cron6f135e12021-12-08 16:57:54 +0100305#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker13996922020-05-28 16:15:19 +0100306
307static size_t ssl_compute_padding_length( size_t len,
308 size_t granularity )
309{
310 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
311}
312
Hanno Becker581bc1b2020-05-04 12:20:03 +0100313/* This functions transforms a (D)TLS plaintext fragment and a record content
314 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
315 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
316 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100317 *
318 * struct {
319 * opaque content[DTLSPlaintext.length];
320 * ContentType real_type;
321 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100322 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100323 *
324 * Input:
325 * - `content`: The beginning of the buffer holding the
326 * plaintext to be wrapped.
327 * - `*content_size`: The length of the plaintext in Bytes.
328 * - `max_len`: The number of Bytes available starting from
329 * `content`. This must be `>= *content_size`.
330 * - `rec_type`: The desired record content type.
331 *
332 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100333 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
334 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100335 *
336 * Returns:
337 * - `0` on success.
338 * - A negative error code if `max_len` didn't offer enough space
339 * for the expansion.
340 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200341MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker581bc1b2020-05-04 12:20:03 +0100342static int ssl_build_inner_plaintext( unsigned char *content,
343 size_t *content_size,
344 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100345 uint8_t rec_type,
346 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100347{
348 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100349
350 /* Write real content type */
351 if( remaining == 0 )
352 return( -1 );
353 content[ len ] = rec_type;
354 len++;
355 remaining--;
356
357 if( remaining < pad )
358 return( -1 );
359 memset( content + len, 0, pad );
360 len += pad;
361 remaining -= pad;
362
363 *content_size = len;
364 return( 0 );
365}
366
Hanno Becker581bc1b2020-05-04 12:20:03 +0100367/* This function parses a (D)TLSInnerPlaintext structure.
368 * See ssl_build_inner_plaintext() for details. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200369MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker581bc1b2020-05-04 12:20:03 +0100370static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100371 size_t *content_size,
372 uint8_t *rec_type )
373{
374 size_t remaining = *content_size;
375
376 /* Determine length of padding by skipping zeroes from the back. */
377 do
378 {
379 if( remaining == 0 )
380 return( -1 );
381 remaining--;
382 } while( content[ remaining ] == 0 );
383
384 *content_size = remaining;
385 *rec_type = content[ remaining ];
386
387 return( 0 );
388}
Ronald Cron6f135e12021-12-08 16:57:54 +0100389#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100390
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200391/* The size of the `add_data` structure depends on various
392 * factors, namely
393 *
394 * 1) CID functionality disabled
395 *
396 * additional_data =
397 * 8: seq_num +
398 * 1: type +
399 * 2: version +
400 * 2: length of inner plaintext +
401 *
402 * size = 13 bytes
403 *
404 * 2) CID functionality based on RFC 9146 enabled
405 *
406 * size = 8 + 1 + 1 + 1 + 2 + 2 + 6 + 2 + CID-length
407 * = 23 + CID-length
408 *
409 * 3) CID functionality based on legacy CID version
410 according to draft-ietf-tls-dtls-connection-id-05
411 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05
412 *
413 * size = 13 + 1 + CID-length
414 *
415 * More information about the CID usage:
416 *
417 * Per Section 5.3 of draft-ietf-tls-dtls-connection-id-05 the
418 * size of the additional data structure is calculated as:
419 *
420 * additional_data =
421 * 8: seq_num +
422 * 1: tls12_cid +
423 * 2: DTLSCipherText.version +
424 * n: cid +
425 * 1: cid_length +
426 * 2: length_of_DTLSInnerPlaintext
427 *
428 * Per RFC 9146 the size of the add_data structure is calculated as:
429 *
430 * additional_data =
431 * 8: seq_num_placeholder +
432 * 1: tls12_cid +
433 * 1: cid_length +
434 * 1: tls12_cid +
435 * 2: DTLSCiphertext.version +
436 * 2: epoch +
437 * 6: sequence_number +
438 * n: cid +
439 * 2: length_of_DTLSInnerPlaintext
440 *
441 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000442static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100443 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100444 mbedtls_record *rec,
Glenn Strauss07c64162022-03-14 12:34:51 -0400445 mbedtls_ssl_protocol_version
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200446 tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000447 size_t taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000448{
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200449 /* Several types of ciphers have been defined for use with TLS and DTLS,
450 * and the MAC calculations for those ciphers differ slightly. Further
451 * variants were added when the CID functionality was added with RFC 9146.
452 * This implementations also considers the use of a legacy version of the
453 * CID specification published in draft-ietf-tls-dtls-connection-id-05,
454 * which is used in deployments.
455 *
456 * We will distinguish between the non-CID and the CID cases below.
457 *
458 * --- Non-CID cases ---
459 *
460 * Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100461 *
462 * additional_data = seq_num + TLSCompressed.type +
463 * TLSCompressed.version + TLSCompressed.length;
464 *
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100465 * For TLS 1.3, the record sequence number is dropped from the AAD
466 * and encoded within the nonce of the AEAD operation instead.
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000467 * Moreover, the additional data involves the length of the TLS
468 * ciphertext, not the TLS plaintext as in earlier versions.
469 * Quoting RFC 8446 (TLS 1.3):
470 *
471 * additional_data = TLSCiphertext.opaque_type ||
472 * TLSCiphertext.legacy_record_version ||
473 * TLSCiphertext.length
474 *
475 * We pass the tag length to this function in order to compute the
476 * ciphertext length from the inner plaintext length rec->data_len via
477 *
478 * TLSCiphertext.length = TLSInnerPlaintext.length + taglen.
479 *
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200480 * --- CID cases ---
481 *
482 * RFC 9146 uses a common pattern when constructing the data
483 * passed into a MAC / AEAD cipher.
484 *
485 * Data concatenation for MACs used with block ciphers with
486 * Encrypt-then-MAC Processing (with CID):
487 *
488 * data = seq_num_placeholder +
489 * tls12_cid +
490 * cid_length +
491 * tls12_cid +
492 * DTLSCiphertext.version +
493 * epoch +
494 * sequence_number +
495 * cid +
496 * DTLSCiphertext.length +
497 * IV +
498 * ENC(content + padding + padding_length)
499 *
500 * Data concatenation for MACs used with block ciphers (with CID):
501 *
502 * data = seq_num_placeholder +
503 * tls12_cid +
504 * cid_length +
505 * tls12_cid +
506 * DTLSCiphertext.version +
507 * epoch +
508 * sequence_number +
509 * cid +
510 * length_of_DTLSInnerPlaintext +
511 * DTLSInnerPlaintext.content +
512 * DTLSInnerPlaintext.real_type +
513 * DTLSInnerPlaintext.zeros
514 *
515 * AEAD ciphers use the following additional data calculation (with CIDs):
516 *
517 * additional_data = seq_num_placeholder +
518 * tls12_cid +
519 * cid_length +
520 * tls12_cid +
521 * DTLSCiphertext.version +
522 * epoch +
523 * sequence_number +
524 * cid +
525 * length_of_DTLSInnerPlaintext
526 *
527 * Section 5.3 of draft-ietf-tls-dtls-connection-id-05 (for legacy CID use)
528 * defines the additional data calculation as follows:
529 *
530 * additional_data = seq_num +
531 * tls12_cid +
532 * DTLSCipherText.version +
533 * cid +
534 * cid_length +
535 * length_of_DTLSInnerPlaintext
536 */
Hanno Beckercab87e62019-04-29 13:52:53 +0100537
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100538 unsigned char *cur = add_data;
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000539 size_t ad_len_field = rec->data_len;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100540
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200541#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
542 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
543 const unsigned char seq_num_placeholder[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
544#endif
545
Ronald Cron6f135e12021-12-08 16:57:54 +0100546#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -0400547 if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000548 {
549 /* In TLS 1.3, the AAD contains the length of the TLSCiphertext,
550 * which differs from the length of the TLSInnerPlaintext
551 * by the length of the authentication tag. */
552 ad_len_field += taglen;
553 }
554 else
Ronald Cron6f135e12021-12-08 16:57:54 +0100555#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100556 {
Glenn Strauss07c64162022-03-14 12:34:51 -0400557 ((void) tls_version);
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000558 ((void) taglen);
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200559
560#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
561
562#if MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
563 if( rec->cid_len != 0 )
564 {
565 // seq_num_placeholder
566 memcpy( cur, seq_num_placeholder, sizeof(seq_num_placeholder) );
567 cur += sizeof( seq_num_placeholder );
568
569 // tls12_cid type
570 *cur = rec->type;
571 cur++;
572
573 // cid_length
574 *cur = rec->cid_len;
575 cur++;
576 }
577 else
578 {
579 // epoch + sequence number
580 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
581 cur += sizeof( rec->ctr );
582 }
583#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0 */
584#else
585 // epoch + sequence number
586 memcpy(cur, rec->ctr, sizeof(rec->ctr));
587 cur += sizeof(rec->ctr);
588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100589 }
590
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200591 // type
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100592 *cur = rec->type;
593 cur++;
594
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200595 // version
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100596 memcpy( cur, rec->ver, sizeof( rec->ver ) );
597 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100598
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200599#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
600 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 1
601
602 if (rec->cid_len != 0)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100603 {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200604 // CID
605 memcpy(cur, rec->cid, rec->cid_len);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100606 cur += rec->cid_len;
607
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200608 // cid_length
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100609 *cur = rec->cid_len;
610 cur++;
611
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200612 // length of inner plaintext
613 MBEDTLS_PUT_UINT16_BE(ad_len_field, cur, 0);
614 cur += 2;
615 }
616 else
617#elif defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) && \
618 MBEDTLS_SSL_DTLS_CONNECTION_ID_COMPAT == 0
619
620 if( rec->cid_len != 0 )
621 {
622 // epoch + sequence number
623 memcpy(cur, rec->ctr, sizeof(rec->ctr));
624 cur += sizeof(rec->ctr);
625
626 // CID
627 memcpy( cur, rec->cid, rec->cid_len );
628 cur += rec->cid_len;
629
630 // length of inner plaintext
Joe Subbiani6dd73642021-07-19 11:56:54 +0100631 MBEDTLS_PUT_UINT16_BE( ad_len_field, cur, 0 );
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100632 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100633 }
634 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100635#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100636 {
Joe Subbiani6dd73642021-07-19 11:56:54 +0100637 MBEDTLS_PUT_UINT16_BE( ad_len_field, cur, 0 );
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100638 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100639 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100640
641 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000642}
643
Hanno Becker67a37db2020-05-28 16:27:07 +0100644#if defined(MBEDTLS_GCM_C) || \
645 defined(MBEDTLS_CCM_C) || \
646 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200647MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker17263802020-05-28 07:05:48 +0100648static int ssl_transform_aead_dynamic_iv_is_explicit(
649 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100650{
Hanno Becker17263802020-05-28 07:05:48 +0100651 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100652}
653
Hanno Becker17263802020-05-28 07:05:48 +0100654/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
655 *
656 * Concretely, this occurs in two variants:
657 *
658 * a) Fixed and dynamic IV lengths add up to total IV length, giving
659 * IV = fixed_iv || dynamic_iv
660 *
Hanno Becker15952812020-06-04 13:31:46 +0100661 * This variant is used in TLS 1.2 when used with GCM or CCM.
662 *
Hanno Becker17263802020-05-28 07:05:48 +0100663 * b) Fixed IV lengths matches total IV length, giving
664 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100665 *
666 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
667 *
668 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100669 *
670 * This function has the precondition that
671 *
672 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
673 *
674 * which has to be ensured by the caller. If this precondition
675 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100676 */
677static void ssl_build_record_nonce( unsigned char *dst_iv,
678 size_t dst_iv_len,
679 unsigned char const *fixed_iv,
680 size_t fixed_iv_len,
681 unsigned char const *dynamic_iv,
682 size_t dynamic_iv_len )
683{
684 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100685
686 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100687 memset( dst_iv, 0, dst_iv_len );
688 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100689
Hanno Becker17263802020-05-28 07:05:48 +0100690 dst_iv += dst_iv_len - dynamic_iv_len;
691 for( i = 0; i < dynamic_iv_len; i++ )
692 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100693}
Hanno Becker67a37db2020-05-28 16:27:07 +0100694#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100695
Hanno Beckera18d1322018-01-03 14:27:32 +0000696int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
697 mbedtls_ssl_transform *transform,
698 mbedtls_record *rec,
699 int (*f_rng)(void *, unsigned char *, size_t),
700 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000701{
Neil Armstrong136f8402022-03-30 10:58:01 +0200702 mbedtls_ssl_mode_t ssl_mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100703 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000704 unsigned char * data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +0200705 /* For an explanation of the additional data length see
706 * the descrpition of ssl_extract_add_data_from_record().
707 */
708#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
709 unsigned char add_data[23 + MBEDTLS_SSL_CID_OUT_LEN_MAX];
710#else
711 unsigned char add_data[13];
712#endif
Hanno Beckercab87e62019-04-29 13:52:53 +0100713 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000714 size_t post_avail;
715
716 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000717#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200718 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000719 ((void) ssl);
720#endif
721
722 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200723 * for CBC transformations in TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200724#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
TRodziewicz0f82ec62021-05-12 17:49:18 +0200725 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000726 ((void) f_rng);
727 ((void) p_rng);
728#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000732 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100733 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
736 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100737 if( rec == NULL
738 || rec->buf == NULL
739 || rec->buf_len < rec->data_offset
740 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100742 || rec->cid_len != 0
743#endif
744 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000745 {
746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100748 }
749
Neil Armstrongab555e02022-04-04 11:07:59 +0200750 ssl_mode = mbedtls_ssl_get_mode_from_transform( transform );
Neil Armstrong136f8402022-03-30 10:58:01 +0200751
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000752 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100753 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000755 data, rec->data_len );
756
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000757 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
758 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
760 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000761 rec->data_len,
762 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000763 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
764 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100765
Hanno Becker92313402020-05-20 13:58:58 +0100766 /* The following two code paths implement the (D)TLSInnerPlaintext
767 * structure present in TLS 1.3 and DTLS 1.2 + CID.
768 *
769 * See ssl_build_inner_plaintext() for more information.
770 *
771 * Note that this changes `rec->data_len`, and hence
772 * `post_avail` needs to be recalculated afterwards.
773 *
774 * Note also that the two code paths cannot occur simultaneously
775 * since they apply to different versions of the protocol. There
776 * is hence no risk of double-addition of the inner plaintext.
777 */
Ronald Cron6f135e12021-12-08 16:57:54 +0100778#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -0400779 if( transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100780 {
Hanno Becker13996922020-05-28 16:15:19 +0100781 size_t padding =
782 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200783 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100784 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100785 &rec->data_len,
786 post_avail,
787 rec->type,
788 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100789 {
790 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
791 }
792
793 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
794 }
Ronald Cron6f135e12021-12-08 16:57:54 +0100795#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100796
Hanno Beckera0e20d02019-05-15 14:03:01 +0100797#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100798 /*
799 * Add CID information
800 */
801 rec->cid_len = transform->out_cid_len;
802 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
803 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100804
805 if( rec->cid_len != 0 )
806 {
Hanno Becker13996922020-05-28 16:15:19 +0100807 size_t padding =
808 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200809 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100810 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100811 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100812 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100813 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100814 * Note that this changes `rec->data_len`, and hence
815 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100816 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100817 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100818 &rec->data_len,
819 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100820 rec->type,
821 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100822 {
823 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
824 }
825
826 rec->type = MBEDTLS_SSL_MSG_CID;
827 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100828#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100829
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100830 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
831
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100833 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000835#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong136f8402022-03-30 10:58:01 +0200836 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
837 ssl_mode == MBEDTLS_SSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000838 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000839 if( post_avail < transform->maclen )
840 {
841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
842 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
843 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200844#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +0200845 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong26e6d672022-02-23 09:30:33 +0100847#if defined(MBEDTLS_USE_PSA_CRYPTO)
848 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
849 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
850 size_t sign_mac_length = 0;
851#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker992b6872017-11-09 18:57:39 +0000852
TRodziewicz345165c2021-07-06 13:42:11 +0200853 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Glenn Strauss07c64162022-03-14 12:34:51 -0400854 transform->tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000855 transform->taglen );
Hanno Becker992b6872017-11-09 18:57:39 +0000856
Neil Armstrong26e6d672022-02-23 09:30:33 +0100857#if defined(MBEDTLS_USE_PSA_CRYPTO)
858 status = psa_mac_sign_setup( &operation, transform->psa_mac_enc,
859 transform->psa_mac_alg );
860 if( status != PSA_SUCCESS )
861 goto hmac_failed_etm_disabled;
862
863 status = psa_mac_update( &operation, add_data, add_data_len );
864 if( status != PSA_SUCCESS )
865 goto hmac_failed_etm_disabled;
866
867 status = psa_mac_update( &operation, data, rec->data_len );
868 if( status != PSA_SUCCESS )
869 goto hmac_failed_etm_disabled;
870
871 status = psa_mac_sign_finish( &operation, mac, MBEDTLS_SSL_MAC_ADD,
872 &sign_mac_length );
873 if( status != PSA_SUCCESS )
874 goto hmac_failed_etm_disabled;
875#else
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100876 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
877 add_data_len );
878 if( ret != 0 )
879 goto hmac_failed_etm_disabled;
880 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, data, rec->data_len );
881 if( ret != 0 )
882 goto hmac_failed_etm_disabled;
883 ret = mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
884 if( ret != 0 )
885 goto hmac_failed_etm_disabled;
886 ret = mbedtls_md_hmac_reset( &transform->md_ctx_enc );
887 if( ret != 0 )
888 goto hmac_failed_etm_disabled;
Neil Armstrong26e6d672022-02-23 09:30:33 +0100889#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000890
TRodziewicz345165c2021-07-06 13:42:11 +0200891 memcpy( data + rec->data_len, mac, transform->maclen );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200892#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200893
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000894 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
895 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200896
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000897 rec->data_len += transform->maclen;
898 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100899 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100900
901 hmac_failed_etm_disabled:
Gilles Peskined5ba50e2021-12-10 21:33:21 +0100902 mbedtls_platform_zeroize( mac, transform->maclen );
Neil Armstrong26e6d672022-02-23 09:30:33 +0100903#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong4313f552022-03-02 15:14:07 +0100904 ret = psa_ssl_status_to_mbedtls( status );
905 status = psa_mac_abort( &operation );
906 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong26e6d672022-02-23 09:30:33 +0100907 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong4313f552022-03-02 15:14:07 +0100908#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100909 if( ret != 0 )
910 {
911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_hmac_xxx", ret );
912 return( ret );
913 }
Paul Bakker577e0062013-08-28 11:57:20 +0200914 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000915#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200917 /*
918 * Encrypt
919 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000920#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Neil Armstrong136f8402022-03-30 10:58:01 +0200921 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000922 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000924 "including %d bytes of padding",
925 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +0100927 /* The only supported stream cipher is "NULL",
928 * so there's nothing to do here.*/
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 }
Paul Bakker68884e32013-01-07 18:20:04 +0100930 else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000931#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000932
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200933#if defined(MBEDTLS_GCM_C) || \
934 defined(MBEDTLS_CCM_C) || \
935 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong136f8402022-03-30 10:58:01 +0200936 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000937 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200938 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100939 unsigned char *dynamic_iv;
940 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100941 int dynamic_iv_is_explicit =
942 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100943#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +0100944 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100945#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000947
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100948 /* Check that there's space for the authentication tag. */
949 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000950 {
951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
952 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
953 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000954
Paul Bakker68884e32013-01-07 18:20:04 +0100955 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100956 * Build nonce for AEAD encryption.
957 *
958 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
959 * part of the IV is prepended to the ciphertext and
960 * can be chosen freely - in particular, it need not
961 * agree with the record sequence number.
962 * However, since ChaChaPoly as well as all AEAD modes
963 * in TLS 1.3 use the record sequence number as the
964 * dynamic part of the nonce, we uniformly use the
965 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100966 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100967 dynamic_iv = rec->ctr;
968 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200969
Hanno Becker17263802020-05-28 07:05:48 +0100970 ssl_build_record_nonce( iv, sizeof( iv ),
971 transform->iv_enc,
972 transform->fixed_ivlen,
973 dynamic_iv,
974 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100975
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100976 /*
977 * Build additional data for AEAD encryption.
978 * This depends on the TLS version.
979 */
980 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Glenn Strauss07c64162022-03-14 12:34:51 -0400981 transform->tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000982 transform->taglen );
Hanno Becker1f10d762019-04-26 13:34:37 +0100983
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200984 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100985 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200986 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100987 dynamic_iv,
988 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000989 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100990 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200992 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000993 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000994
Paul Bakker68884e32013-01-07 18:20:04 +0100995 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200996 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200997 */
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100998#if defined(MBEDTLS_USE_PSA_CRYPTO)
999 status = psa_aead_encrypt( transform->psa_key_enc,
1000 transform->psa_alg,
1001 iv, transform->ivlen,
1002 add_data, add_data_len,
1003 data, rec->data_len,
1004 data, rec->buf_len - (data - rec->buf),
1005 &rec->data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001006
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001007 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001008 {
1009 ret = psa_ssl_status_to_mbedtls( status );
1010 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_encrypt_buf", ret );
1011 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001012 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001013#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001014 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001016 add_data, add_data_len,
1017 data, rec->data_len, /* src */
1018 data, rec->buf_len - (data - rec->buf), /* dst */
1019 &rec->data_len,
1020 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001021 {
TRodziewicz18efb732021-04-29 23:12:19 +02001022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001023 return( ret );
1024 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001025#endif /* MBEDTLS_USE_PSA_CRYPTO */
1026
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001027 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001028 data + rec->data_len - transform->taglen,
1029 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +01001030 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001031 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +01001032
1033 /*
1034 * Prefix record content with dynamic IV in case it is explicit.
1035 */
Hanno Becker1cda2662020-06-04 13:28:28 +01001036 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001037 {
1038 if( rec->data_offset < dynamic_iv_len )
1039 {
1040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1041 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1042 }
1043
1044 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
1045 rec->data_offset -= dynamic_iv_len;
1046 rec->data_len += dynamic_iv_len;
1047 }
1048
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001049 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +01001052#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001053#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Neil Armstrong136f8402022-03-30 10:58:01 +02001054 if( ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1055 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001058 size_t padlen, i;
1059 size_t olen;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001060#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001062 size_t part_len;
1063 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1064#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001065
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001066 /* Currently we're always using minimal padding
1067 * (up to 255 bytes would be allowed). */
1068 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
1069 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 padlen = 0;
1071
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001072 /* Check there's enough space in the buffer for the padding. */
1073 if( post_avail < padlen + 1 )
1074 {
1075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1076 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1077 }
1078
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001080 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001082 rec->data_len += padlen + 1;
1083 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001084
TRodziewicz0f82ec62021-05-12 17:49:18 +02001085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001086 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +02001087 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +00001088 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001089 */
TRodziewicz345165c2021-07-06 13:42:11 +02001090 if( f_rng == NULL )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001091 {
TRodziewicz345165c2021-07-06 13:42:11 +02001092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
1093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001094 }
TRodziewicz345165c2021-07-06 13:42:11 +02001095
1096 if( rec->data_offset < transform->ivlen )
1097 {
1098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1099 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1100 }
1101
1102 /*
1103 * Generate IV
1104 */
1105 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
1106 if( ret != 0 )
1107 return( ret );
1108
1109 memcpy( data - transform->ivlen, transform->iv_enc, transform->ivlen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001110#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001111
Paul Elliottd48d5c62021-01-07 14:47:05 +00001112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
1113 "including %" MBEDTLS_PRINTF_SIZET
1114 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001115 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001116 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001118#if defined(MBEDTLS_USE_PSA_CRYPTO)
1119 status = psa_cipher_encrypt_setup( &cipher_op,
Przemyslaw Stekield4eab572022-01-17 16:20:10 +01001120 transform->psa_key_enc, transform->psa_alg );
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001121
1122 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001123 {
1124 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001125 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_encrypt_setup", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001126 return( ret );
1127 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001128
1129 status = psa_cipher_set_iv( &cipher_op, transform->iv_enc, transform->ivlen );
1130
1131 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001132 {
1133 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001134 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_set_iv", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001135 return( ret );
1136
1137 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001138
1139 status = psa_cipher_update( &cipher_op,
1140 data, rec->data_len,
1141 data, rec->data_len, &olen );
1142
1143 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001144 {
1145 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001146 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_update", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001147 return( ret );
1148
1149 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001150
1151 status = psa_cipher_finish( &cipher_op,
1152 data + olen, rec->data_len - olen,
1153 &part_len );
1154
1155 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001156 {
1157 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001158 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_finish", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001159 return( ret );
1160
1161 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001162
1163 olen += part_len;
1164#else
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001165 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1166 transform->iv_enc,
1167 transform->ivlen,
1168 data, rec->data_len,
1169 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001172 return( ret );
1173 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001174#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001176 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001180 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001181
TRodziewicz0f82ec62021-05-12 17:49:18 +02001182 data -= transform->ivlen;
1183 rec->data_offset -= transform->ivlen;
1184 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001187 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001188 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001189 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001190#if defined(MBEDTLS_USE_PSA_CRYPTO)
1191 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1192 size_t sign_mac_length = 0;
1193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker3d8c9072018-01-05 16:24:22 +00001194
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001195 /* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length))
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001196 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001197
1198 if( post_avail < transform->maclen)
1199 {
1200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1201 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1202 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001203
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001204 ssl_extract_add_data_from_record( add_data, &add_data_len,
Glenn Strauss07c64162022-03-14 12:34:51 -04001205 rec, transform->tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001206 transform->taglen );
Hanno Becker1f10d762019-04-26 13:34:37 +01001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001209 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001210 add_data_len );
Neil Armstrong26e6d672022-02-23 09:30:33 +01001211#if defined(MBEDTLS_USE_PSA_CRYPTO)
1212 status = psa_mac_sign_setup( &operation, transform->psa_mac_enc,
1213 transform->psa_mac_alg );
1214 if( status != PSA_SUCCESS )
1215 goto hmac_failed_etm_enabled;
1216
1217 status = psa_mac_update( &operation, add_data, add_data_len );
1218 if( status != PSA_SUCCESS )
1219 goto hmac_failed_etm_enabled;
1220
1221 status = psa_mac_update( &operation, data, rec->data_len );
1222 if( status != PSA_SUCCESS )
1223 goto hmac_failed_etm_enabled;
1224
1225 status = psa_mac_sign_finish( &operation, mac, MBEDTLS_SSL_MAC_ADD,
1226 &sign_mac_length );
1227 if( status != PSA_SUCCESS )
1228 goto hmac_failed_etm_enabled;
1229#else
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001230
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001231 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1232 add_data_len );
1233 if( ret != 0 )
1234 goto hmac_failed_etm_enabled;
1235 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc,
1236 data, rec->data_len );
1237 if( ret != 0 )
1238 goto hmac_failed_etm_enabled;
1239 ret = mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1240 if( ret != 0 )
1241 goto hmac_failed_etm_enabled;
1242 ret = mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1243 if( ret != 0 )
1244 goto hmac_failed_etm_enabled;
Neil Armstrong26e6d672022-02-23 09:30:33 +01001245#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001246
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001247 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001248
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001249 rec->data_len += transform->maclen;
1250 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001251 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001252
1253 hmac_failed_etm_enabled:
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001254 mbedtls_platform_zeroize( mac, transform->maclen );
Neil Armstrong26e6d672022-02-23 09:30:33 +01001255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong4313f552022-03-02 15:14:07 +01001256 ret = psa_ssl_status_to_mbedtls( status );
1257 status = psa_mac_abort( &operation );
1258 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong26e6d672022-02-23 09:30:33 +01001259 ret = psa_ssl_status_to_mbedtls( status );
Neil Armstrong4313f552022-03-02 15:14:07 +01001260#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001261 if( ret != 0 )
1262 {
1263 MBEDTLS_SSL_DEBUG_RET( 1, "HMAC calculation failed", ret );
1264 return( ret );
1265 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001269 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001270#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1273 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001274 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001275
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001276 /* Make extra sure authentication was performed, exactly once */
1277 if( auth_done != 1 )
1278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001281 }
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
1285 return( 0 );
1286}
1287
Hanno Becker605949f2019-07-12 08:23:59 +01001288int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001289 mbedtls_ssl_transform *transform,
1290 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001291{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001292 size_t olen;
Neil Armstrong136f8402022-03-30 10:58:01 +02001293 mbedtls_ssl_mode_t ssl_mode;
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001294 int ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001295
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001296 int auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001297#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001298 size_t padlen = 0, correct = 1;
1299#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001300 unsigned char* data;
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02001301 /* For an explanation of the additional data length see
1302 * the descrpition of ssl_extract_add_data_from_record().
1303 */
1304#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1305 unsigned char add_data[23 + MBEDTLS_SSL_CID_IN_LEN_MAX];
1306#else
1307 unsigned char add_data[13];
1308#endif
Hanno Beckercab87e62019-04-29 13:52:53 +01001309 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001310
Hanno Beckera18d1322018-01-03 14:27:32 +00001311#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001312 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001313 ((void) ssl);
1314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001317 if( rec == NULL ||
1318 rec->buf == NULL ||
1319 rec->buf_len < rec->data_offset ||
1320 rec->buf_len - rec->data_offset < rec->data_len )
1321 {
1322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001324 }
1325
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001326 data = rec->buf + rec->data_offset;
Neil Armstrongab555e02022-04-04 11:07:59 +02001327 ssl_mode = mbedtls_ssl_get_mode_from_transform( transform );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Hanno Beckera0e20d02019-05-15 14:03:01 +01001329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001330 /*
1331 * Match record's CID with incoming CID.
1332 */
Hanno Becker938489a2019-05-08 13:02:22 +01001333 if( rec->cid_len != transform->in_cid_len ||
1334 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1335 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001336 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001337 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001338#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001339
Hanno Beckerd086bf02021-03-22 13:01:27 +00001340#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Neil Armstrong136f8402022-03-30 10:58:01 +02001341 if( ssl_mode == MBEDTLS_SSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001342 {
Przemyslaw Stekielc8a06fe2022-02-07 10:52:47 +01001343 /* The only supported stream cipher is "NULL",
1344 * so there's nothing to do here.*/
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 }
Paul Bakker68884e32013-01-07 18:20:04 +01001346 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001347#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001348#if defined(MBEDTLS_GCM_C) || \
1349 defined(MBEDTLS_CCM_C) || \
1350 defined(MBEDTLS_CHACHAPOLY_C)
Neil Armstrong136f8402022-03-30 10:58:01 +02001351 if( ssl_mode == MBEDTLS_SSL_MODE_AEAD )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001352 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001353 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001354 unsigned char *dynamic_iv;
1355 size_t dynamic_iv_len;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001356#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001358#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001360 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001361 * Extract dynamic part of nonce for AEAD decryption.
1362 *
1363 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1364 * part of the IV is prepended to the ciphertext and
1365 * can be chosen freely - in particular, it need not
1366 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001367 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001368 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001369 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001370 {
1371 if( rec->data_len < dynamic_iv_len )
1372 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1374 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001375 rec->data_len,
1376 dynamic_iv_len ) );
1377 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1378 }
1379 dynamic_iv = data;
1380
1381 data += dynamic_iv_len;
1382 rec->data_offset += dynamic_iv_len;
1383 rec->data_len -= dynamic_iv_len;
1384 }
Hanno Becker17263802020-05-28 07:05:48 +01001385 else
1386 {
1387 dynamic_iv = rec->ctr;
1388 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001389
1390 /* Check that there's space for the authentication tag. */
1391 if( rec->data_len < transform->taglen )
1392 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1394 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001395 rec->data_len,
1396 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001398 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001399 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001400
Hanno Beckerdf8be222020-05-21 15:30:57 +01001401 /*
1402 * Prepare nonce from dynamic and static parts.
1403 */
Hanno Becker17263802020-05-28 07:05:48 +01001404 ssl_build_record_nonce( iv, sizeof( iv ),
1405 transform->iv_dec,
1406 transform->fixed_ivlen,
1407 dynamic_iv,
1408 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001409
Hanno Beckerdf8be222020-05-21 15:30:57 +01001410 /*
1411 * Build additional data for AEAD encryption.
1412 * This depends on the TLS version.
1413 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001414 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Glenn Strauss07c64162022-03-14 12:34:51 -04001415 transform->tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001416 transform->taglen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001417 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001418 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001419
Hanno Beckerd96a6522019-07-10 13:55:25 +01001420 /* Because of the check above, we know that there are
Shaun Case8b0ecbc2021-12-20 21:14:10 -08001421 * explicit_iv_len Bytes preceding data, and taglen
Hanno Beckerd96a6522019-07-10 13:55:25 +01001422 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001423 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001424 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001425
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001426 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001427 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001428 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001429
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001430 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001431 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001432 */
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001433#if defined(MBEDTLS_USE_PSA_CRYPTO)
1434 status = psa_aead_decrypt( transform->psa_key_dec,
1435 transform->psa_alg,
1436 iv, transform->ivlen,
1437 add_data, add_data_len,
1438 data, rec->data_len + transform->taglen,
1439 data, rec->buf_len - (data - rec->buf),
Przemyslaw Stekiel221b5272022-01-20 09:18:44 +01001440 &olen );
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001441
1442 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001443 {
1444 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001445 MBEDTLS_SSL_DEBUG_RET( 1, "psa_aead_decrypt", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001446 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001447 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001448#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001449 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001450 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001451 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001452 data, rec->data_len + transform->taglen, /* src */
1453 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001454 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001455 {
TRodziewicz18efb732021-04-29 23:12:19 +02001456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1459 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001460
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001461 return( ret );
1462 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001463#endif /* MBEDTLS_USE_PSA_CRYPTO */
1464
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001465 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001466
Hanno Beckerd96a6522019-07-10 13:55:25 +01001467 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001468 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1471 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001472 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001473 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001476#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Neil Armstrong136f8402022-03-30 10:58:01 +02001477 if( ssl_mode == MBEDTLS_SSL_MODE_CBC ||
1478 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001479 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001480 size_t minlen = 0;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001481#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001482 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001483 size_t part_len;
1484 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1485#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001486
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 /*
Paul Bakker45829992013-01-03 14:52:21 +01001488 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001489 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001490#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001491 /* The ciphertext is prefixed with the CBC IV. */
1492 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001493#endif
Paul Bakker45829992013-01-03 14:52:21 +01001494
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001495 /* Size considerations:
1496 *
1497 * - The CBC cipher text must not be empty and hence
1498 * at least of size transform->ivlen.
1499 *
1500 * Together with the potential IV-prefix, this explains
1501 * the first of the two checks below.
1502 *
1503 * - The record must contain a MAC, either in plain or
1504 * encrypted, depending on whether Encrypt-then-MAC
1505 * is used or not.
1506 * - If it is, the message contains the IV-prefix,
1507 * the CBC ciphertext, and the MAC.
1508 * - If it is not, the padded plaintext, and hence
1509 * the CBC ciphertext, has at least length maclen + 1
1510 * because there is at least the padding length byte.
1511 *
1512 * As the CBC ciphertext is not empty, both cases give the
1513 * lower bound minlen + maclen + 1 on the record size, which
1514 * we test for in the second check below.
1515 */
1516 if( rec->data_len < minlen + transform->ivlen ||
1517 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001518 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1520 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1521 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001522 "+ 1 ) ( + expl IV )", rec->data_len,
1523 transform->ivlen,
1524 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001526 }
1527
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001528 /*
1529 * Authenticate before decrypt if enabled
1530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Neil Armstrong136f8402022-03-30 10:58:01 +02001532 if( ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001533 {
Neil Armstrong26e6d672022-02-23 09:30:33 +01001534#if defined(MBEDTLS_USE_PSA_CRYPTO)
1535 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1536#else
Hanno Becker992b6872017-11-09 18:57:39 +00001537 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Neil Armstrong26e6d672022-02-23 09:30:33 +01001538#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001541
Hanno Beckerd96a6522019-07-10 13:55:25 +01001542 /* Update data_len in tandem with add_data.
1543 *
1544 * The subtraction is safe because of the previous check
1545 * data_len >= minlen + maclen + 1.
1546 *
1547 * Afterwards, we know that data + data_len is followed by at
1548 * least maclen Bytes, which justifies the call to
Gabor Mezei90437e32021-10-20 11:59:27 +02001549 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001550 *
1551 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001552 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001553 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Glenn Strauss07c64162022-03-14 12:34:51 -04001554 transform->tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001555 transform->taglen );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001556
Hanno Beckerd96a6522019-07-10 13:55:25 +01001557 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001558 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1559 add_data_len );
Neil Armstrong26e6d672022-02-23 09:30:33 +01001560#if defined(MBEDTLS_USE_PSA_CRYPTO)
1561 status = psa_mac_verify_setup( &operation, transform->psa_mac_dec,
1562 transform->psa_mac_alg );
1563 if( status != PSA_SUCCESS )
1564 goto hmac_failed_etm_enabled;
1565
1566 status = psa_mac_update( &operation, add_data, add_data_len );
1567 if( status != PSA_SUCCESS )
1568 goto hmac_failed_etm_enabled;
1569
1570 status = psa_mac_update( &operation, data, rec->data_len );
1571 if( status != PSA_SUCCESS )
1572 goto hmac_failed_etm_enabled;
1573
1574 /* Compare expected MAC with MAC at the end of the record. */
1575 status = psa_mac_verify_finish( &operation, data + rec->data_len,
1576 transform->maclen );
1577 if( status != PSA_SUCCESS )
1578 goto hmac_failed_etm_enabled;
1579#else
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001580 ret = mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1581 add_data_len );
1582 if( ret != 0 )
1583 goto hmac_failed_etm_enabled;
1584 ret = mbedtls_md_hmac_update( &transform->md_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001585 data, rec->data_len );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001586 if( ret != 0 )
1587 goto hmac_failed_etm_enabled;
1588 ret = mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1589 if( ret != 0 )
1590 goto hmac_failed_etm_enabled;
1591 ret = mbedtls_md_hmac_reset( &transform->md_ctx_dec );
1592 if( ret != 0 )
1593 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001594
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001595 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1596 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001597 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001598 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001599
Hanno Beckerd96a6522019-07-10 13:55:25 +01001600 /* Compare expected MAC with MAC at the end of the record. */
Gabor Mezei90437e32021-10-20 11:59:27 +02001601 if( mbedtls_ct_memcmp( data + rec->data_len, mac_expect,
gabor-mezei-arm46025642021-07-19 15:19:19 +02001602 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001605 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1606 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001607 }
Neil Armstrong26e6d672022-02-23 09:30:33 +01001608#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001609 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001610
1611 hmac_failed_etm_enabled:
Neil Armstrong26e6d672022-02-23 09:30:33 +01001612#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong4313f552022-03-02 15:14:07 +01001613 ret = psa_ssl_status_to_mbedtls( status );
1614 status = psa_mac_abort( &operation );
1615 if( ret == 0 && status != PSA_SUCCESS )
Neil Armstrong26e6d672022-02-23 09:30:33 +01001616 ret = psa_ssl_status_to_mbedtls( status );
1617#else
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001618 mbedtls_platform_zeroize( mac_expect, transform->maclen );
Neil Armstrong4313f552022-03-02 15:14:07 +01001619#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001620 if( ret != 0 )
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001621 {
1622 if( ret != MBEDTLS_ERR_SSL_INVALID_MAC )
1623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_hmac_xxx", ret );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001624 return( ret );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001625 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001626 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001628
1629 /*
1630 * Check length sanity
1631 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001632
1633 /* We know from above that data_len > minlen >= 0,
1634 * so the following check in particular implies that
1635 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001636 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001637 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1639 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001640 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001642 }
1643
TRodziewicz0f82ec62021-05-12 17:49:18 +02001644#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001646 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001647 */
TRodziewicz345165c2021-07-06 13:42:11 +02001648 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
1649 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650
TRodziewicz345165c2021-07-06 13:42:11 +02001651 data += transform->ivlen;
1652 rec->data_offset += transform->ivlen;
1653 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001654#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001655
Hanno Beckerd96a6522019-07-10 13:55:25 +01001656 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1657
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001658#if defined(MBEDTLS_USE_PSA_CRYPTO)
1659 status = psa_cipher_decrypt_setup( &cipher_op,
Przemyslaw Stekield4eab572022-01-17 16:20:10 +01001660 transform->psa_key_dec, transform->psa_alg );
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001661
1662 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001663 {
1664 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001665 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_decrypt_setup", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001666 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001667 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001668
1669 status = psa_cipher_set_iv( &cipher_op, transform->iv_dec, transform->ivlen );
1670
1671 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001672 {
1673 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001674 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_set_iv", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001675 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001676 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001677
1678 status = psa_cipher_update( &cipher_op,
1679 data, rec->data_len,
1680 data, rec->data_len, &olen );
1681
1682 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001683 {
1684 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001685 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_update", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001686 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001687 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001688
1689 status = psa_cipher_finish( &cipher_op,
1690 data + olen, rec->data_len - olen,
1691 &part_len );
1692
1693 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001694 {
1695 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielc499e332022-02-07 15:12:05 +01001696 MBEDTLS_SSL_DEBUG_RET( 1, "psa_cipher_finish", ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001697 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001698 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001699
1700 olen += part_len;
1701#else
1702
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001703 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1704 transform->iv_dec, transform->ivlen,
1705 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001708 return( ret );
1709 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001710#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001711
Hanno Beckerd96a6522019-07-10 13:55:25 +01001712 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001713 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001717 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001718
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001719 /* Safe since data_len >= minlen + maclen + 1, so after having
1720 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001721 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1722 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001723 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001724
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001725 if( auth_done == 1 )
1726 {
Gabor Mezei90437e32021-10-20 11:59:27 +02001727 const size_t mask = mbedtls_ct_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001728 rec->data_len,
1729 padlen + 1 );
1730 correct &= mask;
1731 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001732 }
1733 else
Paul Bakker45829992013-01-03 14:52:21 +01001734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001736 if( rec->data_len < transform->maclen + padlen + 1 )
1737 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1739 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1740 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001741 rec->data_len,
1742 transform->maclen,
1743 padlen + 1 ) );
1744 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001745#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001746
Gabor Mezei90437e32021-10-20 11:59:27 +02001747 const size_t mask = mbedtls_ct_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001748 rec->data_len,
1749 transform->maclen + padlen + 1 );
1750 correct &= mask;
1751 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001752 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001753
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001754 padlen++;
1755
1756 /* Regardless of the validity of the padding,
1757 * we have data_len >= padlen here. */
1758
TRodziewicz0f82ec62021-05-12 17:49:18 +02001759#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001760 /* The padding check involves a series of up to 256
1761 * consecutive memory reads at the end of the record
1762 * plaintext buffer. In order to hide the length and
1763 * validity of the padding, always perform exactly
1764 * `min(256,plaintext_len)` reads (but take into account
1765 * only the last `padlen` bytes for the padding check). */
1766 size_t pad_count = 0;
1767 volatile unsigned char* const check = data;
1768
1769 /* Index of first padding byte; it has been ensured above
1770 * that the subtraction is safe. */
1771 size_t const padding_idx = rec->data_len - padlen;
1772 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1773 size_t const start_idx = rec->data_len - num_checks;
1774 size_t idx;
1775
1776 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001778 /* pad_count += (idx >= padding_idx) &&
1779 * (check[idx] == padlen - 1);
1780 */
Gabor Mezei90437e32021-10-20 11:59:27 +02001781 const size_t mask = mbedtls_ct_size_mask_ge( idx, padding_idx );
1782 const size_t equal = mbedtls_ct_size_bool_eq( check[idx],
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001783 padlen - 1 );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001784 pad_count += mask & equal;
1785 }
Gabor Mezei90437e32021-10-20 11:59:27 +02001786 correct &= mbedtls_ct_size_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001789 if( padlen > 0 && correct == 0 )
1790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001791#endif
Gabor Mezei90437e32021-10-20 11:59:27 +02001792 padlen &= mbedtls_ct_size_mask( correct );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001793
TRodziewicz0f82ec62021-05-12 17:49:18 +02001794#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001795
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001796 /* If the padding was found to be invalid, padlen == 0
1797 * and the subtraction is safe. If the padding was found valid,
1798 * padlen hasn't been changed and the previous assertion
1799 * data_len >= padlen still holds. */
1800 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001801 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001802 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001803#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1806 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001807 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001809#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001811 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001812#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
1814 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001815 * Authenticate if not done yet.
1816 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001818#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001819 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 {
Paul Elliott5260ce22022-05-09 18:15:54 +01001821 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 };
1822 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 };
Paul Bakker1e5369c2013-12-19 16:40:57 +01001823
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001824 /* If the initial value of padlen was such that
1825 * data_len < maclen + padlen + 1, then padlen
1826 * got reset to 1, and the initial check
1827 * data_len >= minlen + maclen + 1
1828 * guarantees that at this point we still
1829 * have at least data_len >= maclen.
1830 *
1831 * If the initial value of padlen was such that
1832 * data_len >= maclen + padlen + 1, then we have
1833 * subtracted either padlen + 1 (if the padding was correct)
1834 * or 0 (if the padding was incorrect) since then,
1835 * hence data_len >= maclen in any case.
1836 */
1837 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001838 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Glenn Strauss07c64162022-03-14 12:34:51 -04001839 transform->tls_version,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001840 transform->taglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
TRodziewicz0f82ec62021-05-12 17:49:18 +02001842#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001843 /*
1844 * The next two sizes are the minimum and maximum values of
1845 * data_len over all padlen values.
1846 *
1847 * They're independent of padlen, since we previously did
1848 * data_len -= padlen.
1849 *
1850 * Note that max_len + maclen is never more than the buffer
1851 * length, as we previously did in_msglen -= maclen too.
1852 */
1853 const size_t max_len = rec->data_len + padlen;
1854 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1855
Neil Armstronge8589962022-02-25 15:14:29 +01001856#if defined(MBEDTLS_USE_PSA_CRYPTO)
1857 ret = mbedtls_ct_hmac( transform->psa_mac_dec,
1858 transform->psa_mac_alg,
1859 add_data, add_data_len,
1860 data, rec->data_len, min_len, max_len,
1861 mac_expect );
1862#else
Gabor Mezei90437e32021-10-20 11:59:27 +02001863 ret = mbedtls_ct_hmac( &transform->md_ctx_dec,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001864 add_data, add_data_len,
1865 data, rec->data_len, min_len, max_len,
1866 mac_expect );
Neil Armstronge8589962022-02-25 15:14:29 +01001867#endif /* MBEDTLS_USE_PSA_CRYPTO */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001868 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001869 {
Gabor Mezei90437e32021-10-20 11:59:27 +02001870 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ct_hmac", ret );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001871 goto hmac_failed_etm_disabled;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001872 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001873
Gabor Mezei90437e32021-10-20 11:59:27 +02001874 mbedtls_ct_memcpy_offset( mac_peer, data,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001875 rec->data_len,
1876 min_len, max_len,
1877 transform->maclen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001879
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001880#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001881 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001882 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001883#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001884
Gabor Mezei90437e32021-10-20 11:59:27 +02001885 if( mbedtls_ct_memcmp( mac_peer, mac_expect,
gabor-mezei-arm46025642021-07-19 15:19:19 +02001886 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#if defined(MBEDTLS_SSL_DEBUG_ALL)
1889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001890#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001891 correct = 0;
1892 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001893 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001894
1895 hmac_failed_etm_disabled:
1896 mbedtls_platform_zeroize( mac_peer, transform->maclen );
1897 mbedtls_platform_zeroize( mac_expect, transform->maclen );
1898 if( ret != 0 )
1899 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001900 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001901
1902 /*
1903 * Finally check the correct flag
1904 */
1905 if( correct == 0 )
1906 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001907#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001908
1909 /* Make extra sure authentication was performed, exactly once */
1910 if( auth_done != 1 )
1911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1913 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Ronald Cron6f135e12021-12-08 16:57:54 +01001916#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss07c64162022-03-14 12:34:51 -04001917 if( transform->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Hanno Beckerccc13d02020-05-04 12:30:04 +01001918 {
1919 /* Remove inner padding and infer true content type. */
1920 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1921 &rec->type );
1922
1923 if( ret != 0 )
1924 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1925 }
Ronald Cron6f135e12021-12-08 16:57:54 +01001926#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +01001927
Hanno Beckera0e20d02019-05-15 14:03:01 +01001928#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001929 if( rec->cid_len != 0 )
1930 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001931 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1932 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001933 if( ret != 0 )
1934 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1935 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001936#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940 return( 0 );
1941}
1942
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001943#undef MAC_NONE
1944#undef MAC_PLAINTEXT
1945#undef MAC_CIPHERTEXT
1946
Paul Bakker5121ce52009-01-03 21:22:43 +00001947/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001948 * Fill the input message buffer by appending data to it.
1949 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001950 *
1951 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1952 * available (from this read and/or a previous one). Otherwise, an error code
1953 * is returned (possibly EOF or WANT_READ).
1954 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001955 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1956 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1957 * since we always read a whole datagram at once.
1958 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001959 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001960 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001963{
Janos Follath865b3eb2019-12-16 11:46:15 +00001964 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001965 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001966#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1967 size_t in_buf_len = ssl->in_buf_len;
1968#else
1969 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1970#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001973
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001974 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001977 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001979 }
1980
Darryl Greenb33cc762019-11-28 14:29:44 +00001981 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1984 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001985 }
1986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001990 uint32_t timeout;
1991
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001992 /*
1993 * The point is, we need to always read a full datagram at once, so we
1994 * sometimes read more then requested, and handle the additional data.
1995 * It could be the rest of the current record (while fetching the
1996 * header) and/or some other records in the same datagram.
1997 */
1998
1999 /*
2000 * Move to the next record in the already read datagram if applicable
2001 */
2002 if( ssl->next_record_offset != 0 )
2003 {
2004 if( ssl->in_left < ssl->next_record_offset )
2005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2007 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002008 }
2009
2010 ssl->in_left -= ssl->next_record_offset;
2011
2012 if( ssl->in_left != 0 )
2013 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
2015 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002016 ssl->next_record_offset ) );
2017 memmove( ssl->in_hdr,
2018 ssl->in_hdr + ssl->next_record_offset,
2019 ssl->in_left );
2020 }
2021
2022 ssl->next_record_offset = 0;
2023 }
2024
Paul Elliottd48d5c62021-01-07 14:47:05 +00002025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
2026 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002028
2029 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002030 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002031 */
2032 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002035 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002036 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002037
2038 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002039 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002040 * are not at the beginning of a new record, the caller did something
2041 * wrong.
2042 */
2043 if( ssl->in_left != 0 )
2044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002047 }
2048
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002049 /*
2050 * Don't even try to read if time's out already.
2051 * This avoids by-passing the timer when repeatedly receiving messages
2052 * that will end up being dropped.
2053 */
Hanno Becker7876d122020-02-05 10:39:31 +00002054 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002055 {
2056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002057 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002058 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002059 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002060 {
Darryl Greenb33cc762019-11-28 14:29:44 +00002061 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002062
Paul Elliott27b0d942022-03-18 21:55:32 +00002063 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002064 timeout = ssl->handshake->retransmit_timeout;
2065 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002066 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002067
Paul Elliott9f352112020-12-09 14:55:45 +00002068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002069
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002070 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002071 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2072 timeout );
2073 else
2074 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002077
2078 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002080 }
2081
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002082 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00002085 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002086
Paul Elliott27b0d942022-03-18 21:55:32 +00002087 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002088 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002089 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002092 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002093 }
2094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002098 return( ret );
2099 }
2100
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002101 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002102 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002104 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002106 {
Hanno Becker786300f2020-02-05 10:46:40 +00002107 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002108 {
Hanno Becker786300f2020-02-05 10:46:40 +00002109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
2110 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002111 return( ret );
2112 }
2113
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002114 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002117 }
2118
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 if( ret < 0 )
2120 return( ret );
2121
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002122 ssl->in_left = ret;
2123 }
2124 else
2125#endif
2126 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
2128 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002129 ssl->in_left, nb_want ) );
2130
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002131 while( ssl->in_left < nb_want )
2132 {
2133 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002134
Hanno Becker7876d122020-02-05 10:39:31 +00002135 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002136 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2137 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002138 {
2139 if( ssl->f_recv_timeout != NULL )
2140 {
2141 ret = ssl->f_recv_timeout( ssl->p_bio,
2142 ssl->in_hdr + ssl->in_left, len,
2143 ssl->conf->read_timeout );
2144 }
2145 else
2146 {
2147 ret = ssl->f_recv( ssl->p_bio,
2148 ssl->in_hdr + ssl->in_left, len );
2149 }
2150 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002151
Paul Elliottd48d5c62021-01-07 14:47:05 +00002152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
2153 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002154 ssl->in_left, nb_want ) );
2155 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002156
2157 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002159
2160 if( ret < 0 )
2161 return( ret );
2162
makise-homuraaf9513b2020-08-24 18:26:27 +03002163 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002164 {
Darryl Green11999bb2018-03-13 15:22:58 +00002165 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002166 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00002167 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2169 }
2170
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002171 ssl->in_left += ret;
2172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 }
2174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002176
2177 return( 0 );
2178}
2179
2180/*
2181 * Flush any data not yet written
2182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002184{
Janos Follath865b3eb2019-12-16 11:46:15 +00002185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002186 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002189
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002190 if( ssl->f_send == NULL )
2191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002193 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002195 }
2196
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002197 /* Avoid incrementing counter if data is flushed */
2198 if( ssl->out_left == 0 )
2199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002201 return( 0 );
2202 }
2203
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 while( ssl->out_left > 0 )
2205 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
2207 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01002208 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209
Hanno Becker2b1e3542018-08-06 11:19:13 +01002210 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002211 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002214
2215 if( ret <= 0 )
2216 return( ret );
2217
makise-homuraaf9513b2020-08-24 18:26:27 +03002218 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002219 {
Darryl Green11999bb2018-03-13 15:22:58 +00002220 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002221 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00002222 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2224 }
2225
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 ssl->out_left -= ret;
2227 }
2228
Hanno Becker2b1e3542018-08-06 11:19:13 +01002229#if defined(MBEDTLS_SSL_PROTO_DTLS)
2230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002231 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002232 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002233 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002234 else
2235#endif
2236 {
2237 ssl->out_hdr = ssl->out_buf + 8;
2238 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002239 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242
2243 return( 0 );
2244}
2245
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002246/*
2247 * Functions to handle the DTLS retransmission state machine
2248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002250/*
2251 * Append current handshake message to current outgoing flight
2252 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002253MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2258 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2259 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002260
2261 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002262 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002263 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002266 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002267 }
2268
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002269 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002270 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2272 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002274 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002275 }
2276
2277 /* Copy current handshake message with headers */
2278 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2279 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002280 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002281 msg->next = NULL;
2282
2283 /* Append to the current flight */
2284 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002285 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002286 else
2287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002289 while( cur->next != NULL )
2290 cur = cur->next;
2291 cur->next = msg;
2292 }
2293
Hanno Becker3b235902018-08-06 09:54:53 +01002294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002295 return( 0 );
2296}
2297
2298/*
2299 * Free the current flight of handshake messages
2300 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002301void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_ssl_flight_item *cur = flight;
2304 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002305
2306 while( cur != NULL )
2307 {
2308 next = cur->next;
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 mbedtls_free( cur->p );
2311 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002312
2313 cur = next;
2314 }
2315}
2316
2317/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002318 * Swap transform_out and out_ctr with the alternative ones
2319 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002320MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002321static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002322{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 mbedtls_ssl_transform *tmp_transform;
Jerry Yuae0b2e22021-10-08 15:21:19 +08002324 unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002325
2326 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002329 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002330 }
2331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002333
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002334 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002335 tmp_transform = ssl->transform_out;
2336 ssl->transform_out = ssl->handshake->alt_transform_out;
2337 ssl->handshake->alt_transform_out = tmp_transform;
2338
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002339 /* Swap epoch + sequence_number */
Jerry Yud96a5c22021-09-29 17:46:51 +08002340 memcpy( tmp_out_ctr, ssl->cur_out_ctr, sizeof( tmp_out_ctr ) );
2341 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr,
2342 sizeof( ssl->cur_out_ctr ) );
2343 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr,
2344 sizeof( ssl->handshake->alt_out_ctr ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002345
2346 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002347 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002348
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002349 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002350}
2351
2352/*
2353 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002354 */
2355int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2356{
2357 int ret = 0;
2358
2359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2360
2361 ret = mbedtls_ssl_flight_transmit( ssl );
2362
2363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2364
2365 return( ret );
2366}
2367
2368/*
2369 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002370 *
2371 * Need to remember the current message in case flush_output returns
2372 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002373 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002374 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002375int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002376{
Janos Follath865b3eb2019-12-16 11:46:15 +00002377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002381 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002383
2384 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002385 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002386 ret = ssl_swap_epochs( ssl );
2387 if( ret != 0 )
2388 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002391 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002392
2393 while( ssl->handshake->cur_msg != NULL )
2394 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002395 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002396 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002397
Hanno Beckere1dcb032018-08-17 16:47:58 +01002398 int const is_finished =
2399 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2400 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2401
Ronald Cron00d012f22022-03-08 15:57:12 +01002402 int const force_flush = ssl->disable_datagram_packing == 1 ?
Hanno Becker04da1892018-08-14 13:22:10 +01002403 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2404
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002405 /* Swap epochs before sending Finished: we can't do it after
2406 * sending ChangeCipherSpec, in case write returns WANT_READ.
2407 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002408 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002409 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002410 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002411 ret = ssl_swap_epochs( ssl );
2412 if( ret != 0 )
2413 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002414 }
2415
Hanno Becker67bc7c32018-08-06 11:33:50 +01002416 ret = ssl_get_remaining_payload_in_datagram( ssl );
2417 if( ret < 0 )
2418 return( ret );
2419 max_frag_len = (size_t) ret;
2420
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002421 /* CCS is copied as is, while HS messages may need fragmentation */
2422 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2423 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002424 if( max_frag_len == 0 )
2425 {
2426 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2427 return( ret );
2428
2429 continue;
2430 }
2431
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002432 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002433 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002434 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002435
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002436 /* Update position inside current message */
2437 ssl->handshake->cur_msg_p += cur->len;
2438 }
2439 else
2440 {
2441 const unsigned char * const p = ssl->handshake->cur_msg_p;
2442 const size_t hs_len = cur->len - 12;
2443 const size_t frag_off = p - ( cur->p + 12 );
2444 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002445 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002446
Hanno Beckere1dcb032018-08-17 16:47:58 +01002447 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002448 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002449 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002450 {
2451 ret = ssl_swap_epochs( ssl );
2452 if( ret != 0 )
2453 return( ret );
2454 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002455
Hanno Becker67bc7c32018-08-06 11:33:50 +01002456 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2457 return( ret );
2458
2459 continue;
2460 }
2461 max_hs_frag_len = max_frag_len - 12;
2462
2463 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2464 max_hs_frag_len : rem_len;
2465
2466 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002467 {
2468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002469 (unsigned) cur_hs_frag_len,
2470 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002471 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002472
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002473 /* Messages are stored with handshake headers as if not fragmented,
2474 * copy beginning of headers then fill fragmentation fields.
2475 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2476 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002477
Joe Subbiani5ecac212021-06-24 13:00:03 +01002478 ssl->out_msg[6] = MBEDTLS_BYTE_2( frag_off );
2479 ssl->out_msg[7] = MBEDTLS_BYTE_1( frag_off );
2480 ssl->out_msg[8] = MBEDTLS_BYTE_0( frag_off );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002481
Joe Subbiani5ecac212021-06-24 13:00:03 +01002482 ssl->out_msg[ 9] = MBEDTLS_BYTE_2( cur_hs_frag_len );
2483 ssl->out_msg[10] = MBEDTLS_BYTE_1( cur_hs_frag_len );
2484 ssl->out_msg[11] = MBEDTLS_BYTE_0( cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002485
2486 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2487
Hanno Becker3f7b9732018-08-28 09:53:25 +01002488 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002489 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2490 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002491 ssl->out_msgtype = cur->type;
2492
2493 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002494 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002495 }
2496
2497 /* If done with the current message move to the next one if any */
2498 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2499 {
2500 if( cur->next != NULL )
2501 {
2502 ssl->handshake->cur_msg = cur->next;
2503 ssl->handshake->cur_msg_p = cur->next->p + 12;
2504 }
2505 else
2506 {
2507 ssl->handshake->cur_msg = NULL;
2508 ssl->handshake->cur_msg_p = NULL;
2509 }
2510 }
2511
2512 /* Actually send the message out */
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002513 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002516 return( ret );
2517 }
2518 }
2519
Hanno Becker67bc7c32018-08-06 11:33:50 +01002520 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2521 return( ret );
2522
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002523 /* Update state and set timer */
Paul Elliott27b0d942022-03-18 21:55:32 +00002524 if( mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002526 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002529 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002530 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002531
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002533
2534 return( 0 );
2535}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002536
2537/*
2538 * To be called when the last message of an incoming flight is received.
2539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002541{
2542 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002543 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002544 ssl->handshake->flight = NULL;
2545 ssl->handshake->cur_msg = NULL;
2546
2547 /* The next incoming flight will start with this msg_seq */
2548 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2549
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002550 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002551 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002552
Hanno Becker0271f962018-08-16 13:23:47 +01002553 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002554 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002555
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002556 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002557 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2560 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002563 }
2564 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002566}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002567
2568/*
2569 * To be called when the last message of an outgoing flight is send.
2570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002571void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002572{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002573 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002574 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2577 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002580 }
2581 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002585
Paul Bakker5121ce52009-01-03 21:22:43 +00002586/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002587 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 */
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002589int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned hs_type,
2590 unsigned char **buf, size_t *buf_len )
2591{
2592 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002593 * Reserve 4 bytes for handshake header. ( Section 4,RFC 8446 )
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002594 * ...
2595 * HandshakeType msg_type;
2596 * uint24 length;
2597 * ...
2598 */
2599 *buf = ssl->out_msg + 4;
2600 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
2601
2602 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
2603 ssl->out_msg[0] = hs_type;
2604
2605 return( 0 );
2606}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002607
2608/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002609 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002610 *
2611 * - fill in handshake headers
2612 * - update handshake checksum
2613 * - DTLS: save message for resending
2614 * - then pass to the record layer
2615 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002616 * DTLS: except for HelloRequest, messages are only queued, and will only be
2617 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002618 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002619 * Inputs:
2620 * - ssl->out_msglen: 4 + actual handshake message len
2621 * (4 is the size of handshake headers for TLS)
2622 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2623 * - ssl->out_msg + 4: the handshake message body
2624 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002625 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002626 * - ssl->out_msglen: the length of the record contents
2627 * (including handshake headers but excluding record headers)
2628 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002629 */
Hanno Beckerf3cce8b2021-08-07 14:29:49 +01002630int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl,
Ronald Cron66dbf912022-02-02 15:33:46 +01002631 int update_checksum,
Ronald Cron00d012f22022-03-08 15:57:12 +01002632 int force_flush )
Paul Bakker5121ce52009-01-03 21:22:43 +00002633{
Janos Follath865b3eb2019-12-16 11:46:15 +00002634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002635 const size_t hs_len = ssl->out_msglen - 4;
2636 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2639
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002640 /*
2641 * Sanity checks
2642 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002643 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002644 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2645 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002649
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002650 /* Whenever we send anything different from a
2651 * HelloRequest we should be in a handshake - double check. */
2652 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2653 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002654 ssl->handshake == NULL )
2655 {
2656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002660#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002661 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002662 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002663 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002664 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2666 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002667 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002668#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002669
Hanno Beckerb50a2532018-08-06 11:52:54 +01002670 /* Double-check that we did not exceed the bounds
2671 * of the outgoing record buffer.
2672 * This should never fail as the various message
2673 * writing functions must obey the bounds of the
2674 * outgoing record buffer, but better be safe.
2675 *
2676 * Note: We deliberately do not check for the MTU or MFL here.
2677 */
2678 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2679 {
2680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002681 "size %" MBEDTLS_PRINTF_SIZET
2682 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002683 ssl->out_msglen,
2684 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2686 }
2687
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002688 /*
2689 * Fill handshake headers
2690 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01002693 ssl->out_msg[1] = MBEDTLS_BYTE_2( hs_len );
2694 ssl->out_msg[2] = MBEDTLS_BYTE_1( hs_len );
2695 ssl->out_msg[3] = MBEDTLS_BYTE_0( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002697 /*
2698 * DTLS has additional fields in the Handshake layer,
2699 * between the length field and the actual payload:
2700 * uint16 message_seq;
2701 * uint24 fragment_offset;
2702 * uint24 fragment_length;
2703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002705 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002706 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002707 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002708 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002709 {
2710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002711 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002712 hs_len,
2713 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2715 }
2716
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002717 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002718 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002719
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002720 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002721 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002722 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002723 MBEDTLS_PUT_UINT16_BE( ssl->handshake->out_msg_seq, ssl->out_msg, 4 );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002724 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002725 }
2726 else
2727 {
2728 ssl->out_msg[4] = 0;
2729 ssl->out_msg[5] = 0;
2730 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002731
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002732 /* Handshake hashes are computed without fragmentation,
2733 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002734 memset( ssl->out_msg + 6, 0x00, 3 );
2735 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002736 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002738
Hanno Becker0207e532018-08-28 10:28:28 +01002739 /* Update running hashes of handshake messages seen */
Hanno Beckerf3cce8b2021-08-07 14:29:49 +01002740 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0 )
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002741 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
2743
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002744 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002746 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002747 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2748 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002749 {
2750 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002753 return( ret );
2754 }
2755 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002756 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002757#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002758 {
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002759 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002760 {
2761 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2762 return( ret );
2763 }
2764 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002765
2766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2767
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002768 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002769}
2770
Ronald Cron8f6d39a2022-03-10 18:56:50 +01002771int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl,
2772 size_t buf_len, size_t msg_len )
2773{
2774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2775 size_t msg_with_header_len;
2776 ((void) buf_len);
2777
2778 /* Add reserved 4 bytes for handshake header */
2779 msg_with_header_len = msg_len + 4;
2780 ssl->out_msglen = msg_with_header_len;
2781 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0, 0 ) );
2782
2783cleanup:
2784 return( ret );
2785}
2786
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002787/*
2788 * Record layer functions
2789 */
2790
2791/*
2792 * Write current record.
2793 *
2794 * Uses:
2795 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2796 * - ssl->out_msglen: length of the record content (excl headers)
2797 * - ssl->out_msg: record content
2798 */
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002799int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, int force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002800{
2801 int ret, done = 0;
2802 size_t len = ssl->out_msglen;
Ronald Cron00d012f22022-03-08 15:57:12 +01002803 int flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002804
2805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002806
Paul Bakker05ef8352012-05-08 09:17:57 +00002807 if( !done )
2808 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002809 unsigned i;
2810 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002811#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2812 size_t out_buf_len = ssl->out_buf_len;
2813#else
2814 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2815#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002816 /* Skip writing the record content type to after the encryption,
2817 * as it may change when using the CID extension. */
Glenn Strauss60bfe602022-03-14 19:04:24 -04002818 mbedtls_ssl_protocol_version tls_ver = ssl->tls_version;
Ronald Cron6f135e12021-12-08 16:57:54 +01002819#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ca80f72021-11-08 10:30:54 +08002820 /* TLS 1.3 still uses the TLS 1.2 version identifier
2821 * for backwards compatibility. */
Glenn Strauss60bfe602022-03-14 19:04:24 -04002822 if( tls_ver == MBEDTLS_SSL_VERSION_TLS1_3 )
2823 tls_ver = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cron6f135e12021-12-08 16:57:54 +01002824#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002825 mbedtls_ssl_write_version( ssl->out_hdr + 1, ssl->conf->transport,
2826 tls_ver );
Hanno Becker6430faf2019-05-08 11:57:13 +01002827
Jerry Yuae0b2e22021-10-08 15:21:19 +08002828 memcpy( ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Joe Subbiani6dd73642021-07-19 11:56:54 +01002829 MBEDTLS_PUT_UINT16_BE( len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002830
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002831 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002832 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002833 mbedtls_record rec;
2834
2835 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002836 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002837 rec.data_len = ssl->out_msglen;
2838 rec.data_offset = ssl->out_msg - rec.buf;
2839
Jerry Yud96a5c22021-09-29 17:46:51 +08002840 memcpy( &rec.ctr[0], ssl->out_ctr, sizeof( rec.ctr ) );
Glenn Strausse3af4cb2022-03-15 03:23:42 -04002841 mbedtls_ssl_write_version( rec.ver, ssl->conf->transport, tls_ver );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002842 rec.type = ssl->out_msgtype;
2843
Hanno Beckera0e20d02019-05-15 14:03:01 +01002844#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002845 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002846 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002847#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002848
Hanno Beckera18d1322018-01-03 14:27:32 +00002849 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002850 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002853 return( ret );
2854 }
2855
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002856 if( rec.data_offset != 0 )
2857 {
2858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2859 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2860 }
2861
Hanno Becker6430faf2019-05-08 11:57:13 +01002862 /* Update the record content type and CID. */
2863 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002864#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002865 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002866#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002867 ssl->out_msglen = len = rec.data_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002868 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->out_len, 0 );
Paul Bakker05ef8352012-05-08 09:17:57 +00002869 }
2870
Hanno Becker5903de42019-05-03 14:46:38 +01002871 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002872
2873#if defined(MBEDTLS_SSL_PROTO_DTLS)
2874 /* In case of DTLS, double-check that we don't exceed
2875 * the remaining space in the datagram. */
2876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2877 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002878 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002879 if( ret < 0 )
2880 return( ret );
2881
2882 if( protected_record_size > (size_t) ret )
2883 {
2884 /* Should never happen */
2885 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2886 }
2887 }
2888#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002889
Hanno Becker6430faf2019-05-08 11:57:13 +01002890 /* Now write the potentially updated record content type. */
2891 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2892
Paul Elliott9f352112020-12-09 14:55:45 +00002893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002894 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002895 ssl->out_hdr[0], ssl->out_hdr[1],
2896 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002898 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002899 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002900
2901 ssl->out_left += protected_record_size;
2902 ssl->out_hdr += protected_record_size;
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002903 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002904
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002905 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
2906 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2907 break;
2908
Gabor Mezei96ae9262022-06-28 11:45:18 +02002909 /* The loop goes to its end if the counter is wrapping */
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002910 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002911 {
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02002912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2913 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Hanno Becker04484622018-08-06 09:49:38 +01002914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 }
2916
Hanno Becker67bc7c32018-08-06 11:33:50 +01002917#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002918 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2919 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002920 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002921 size_t remaining;
2922 ret = ssl_get_remaining_payload_in_datagram( ssl );
2923 if( ret < 0 )
2924 {
2925 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2926 ret );
2927 return( ret );
2928 }
2929
2930 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002931 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002932 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002933 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002934 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002935 else
2936 {
Hanno Becker513815a2018-08-20 11:56:09 +01002937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002938 }
2939 }
2940#endif /* MBEDTLS_SSL_PROTO_DTLS */
2941
2942 if( ( flush == SSL_FORCE_FLUSH ) &&
2943 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 return( ret );
2947 }
2948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
2951 return( 0 );
2952}
2953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002955
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002956MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckere25e3b72018-08-16 09:30:53 +01002957static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2958{
2959 if( ssl->in_msglen < ssl->in_hslen ||
2960 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2961 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2962 {
2963 return( 1 );
2964 }
2965 return( 0 );
2966}
Hanno Becker44650b72018-08-16 12:51:11 +01002967
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002968static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002969{
2970 return( ( ssl->in_msg[9] << 16 ) |
2971 ( ssl->in_msg[10] << 8 ) |
2972 ssl->in_msg[11] );
2973}
2974
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002975static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002976{
2977 return( ( ssl->in_msg[6] << 16 ) |
2978 ( ssl->in_msg[7] << 8 ) |
2979 ssl->in_msg[8] );
2980}
2981
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002982MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002983static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002984{
2985 uint32_t msg_len, frag_off, frag_len;
2986
2987 msg_len = ssl_get_hs_total_len( ssl );
2988 frag_off = ssl_get_hs_frag_off( ssl );
2989 frag_len = ssl_get_hs_frag_len( ssl );
2990
2991 if( frag_off > msg_len )
2992 return( -1 );
2993
2994 if( frag_len > msg_len - frag_off )
2995 return( -1 );
2996
2997 if( frag_len + 12 > ssl->in_msglen )
2998 return( -1 );
2999
3000 return( 0 );
3001}
3002
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003003/*
3004 * Mark bits in bitmask (used for DTLS HS reassembly)
3005 */
3006static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3007{
3008 unsigned int start_bits, end_bits;
3009
3010 start_bits = 8 - ( offset % 8 );
3011 if( start_bits != 8 )
3012 {
3013 size_t first_byte_idx = offset / 8;
3014
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003015 /* Special case */
3016 if( len <= start_bits )
3017 {
3018 for( ; len != 0; len-- )
3019 mask[first_byte_idx] |= 1 << ( start_bits - len );
3020
3021 /* Avoid potential issues with offset or len becoming invalid */
3022 return;
3023 }
3024
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003025 offset += start_bits; /* Now offset % 8 == 0 */
3026 len -= start_bits;
3027
3028 for( ; start_bits != 0; start_bits-- )
3029 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3030 }
3031
3032 end_bits = len % 8;
3033 if( end_bits != 0 )
3034 {
3035 size_t last_byte_idx = ( offset + len ) / 8;
3036
3037 len -= end_bits; /* Now len % 8 == 0 */
3038
3039 for( ; end_bits != 0; end_bits-- )
3040 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3041 }
3042
3043 memset( mask + offset / 8, 0xFF, len / 8 );
3044}
3045
3046/*
3047 * Check that bitmask is full
3048 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003049MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003050static int ssl_bitmask_check( unsigned char *mask, size_t len )
3051{
3052 size_t i;
3053
3054 for( i = 0; i < len / 8; i++ )
3055 if( mask[i] != 0xFF )
3056 return( -1 );
3057
3058 for( i = 0; i < len % 8; i++ )
3059 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3060 return( -1 );
3061
3062 return( 0 );
3063}
3064
Hanno Becker56e205e2018-08-16 09:06:12 +01003065/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003066static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003067 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003068{
Hanno Becker56e205e2018-08-16 09:06:12 +01003069 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003070
Hanno Becker56e205e2018-08-16 09:06:12 +01003071 alloc_len = 12; /* Handshake header */
3072 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003073
Hanno Beckerd07df862018-08-16 09:14:58 +01003074 if( add_bitmap )
3075 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003076
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003077 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003078}
Hanno Becker56e205e2018-08-16 09:06:12 +01003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003081
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003082static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003083{
3084 return( ( ssl->in_msg[1] << 16 ) |
3085 ( ssl->in_msg[2] << 8 ) |
3086 ssl->in_msg[3] );
3087}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003088
Simon Butcher99000142016-10-13 17:21:01 +01003089int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003090{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003092 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003094 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003096 }
3097
Hanno Becker12555c62018-08-16 12:47:53 +01003098 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00003101 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003102 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003106 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003108 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003109
Hanno Becker44650b72018-08-16 12:51:11 +01003110 if( ssl_check_hs_header( ssl ) != 0 )
3111 {
3112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3113 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3114 }
3115
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003116 if( ssl->handshake != NULL &&
Paul Elliott27b0d942022-03-18 21:55:32 +00003117 ( ( mbedtls_ssl_is_handshake_over( ssl ) == 0 &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003118 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
Paul Elliott27b0d942022-03-18 21:55:32 +00003119 ( mbedtls_ssl_is_handshake_over( ssl ) == 1 &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003120 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003121 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003122 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3123 {
3124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3125 recv_msg_seq,
3126 ssl->handshake->in_msg_seq ) );
3127 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3128 }
3129
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003130 /* Retransmit only on last message from previous flight, to avoid
3131 * too many retransmissions.
3132 * Besides, No sane server ever retransmits HelloVerifyRequest */
3133 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00003137 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003138 recv_msg_seq,
3139 ssl->handshake->in_flight_start_seq ) );
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003144 return( ret );
3145 }
3146 }
3147 else
3148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00003150 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003151 recv_msg_seq,
3152 ssl->handshake->in_msg_seq ) );
3153 }
3154
Hanno Becker90333da2017-10-10 11:27:13 +01003155 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003156 }
3157 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003158
Hanno Becker6d97ef52018-08-16 13:09:04 +01003159 /* Message reassembly is handled alongside buffering of future
3160 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003161 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003162 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003163 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003166 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003167 }
3168 }
3169 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003171 /* With TLS we don't handle fragmentation (for now) */
3172 if( ssl->in_msglen < ssl->in_hslen )
3173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3175 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003176 }
3177
Simon Butcher99000142016-10-13 17:21:01 +01003178 return( 0 );
3179}
3180
3181void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3182{
Hanno Becker0271f962018-08-16 13:23:47 +01003183 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003184
Paul Elliott27b0d942022-03-18 21:55:32 +00003185 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003186 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003187 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003188 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003189
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003190 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003193 ssl->handshake != NULL )
3194 {
Hanno Becker0271f962018-08-16 13:23:47 +01003195 unsigned offset;
3196 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003197
Hanno Becker0271f962018-08-16 13:23:47 +01003198 /* Increment handshake sequence number */
3199 hs->in_msg_seq++;
3200
3201 /*
3202 * Clear up handshake buffering and reassembly structure.
3203 */
3204
3205 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003206 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003207
3208 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003209 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3210 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003211 offset++, hs_buf++ )
3212 {
3213 *hs_buf = *(hs_buf + 1);
3214 }
3215
3216 /* Create a fresh last entry */
3217 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003218 }
3219#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003220}
3221
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003222/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003223 * DTLS anti-replay: RFC 6347 4.1.2.6
3224 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003225 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3226 * Bit n is set iff record number in_window_top - n has been seen.
3227 *
3228 * Usually, in_window_top is the last record number seen and the lsb of
3229 * in_window is set. The only exception is the initial state (record number 0
3230 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003233void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003234{
3235 ssl->in_window_top = 0;
3236 ssl->in_window = 0;
3237}
3238
3239static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3240{
3241 return( ( (uint64_t) buf[0] << 40 ) |
3242 ( (uint64_t) buf[1] << 32 ) |
3243 ( (uint64_t) buf[2] << 24 ) |
3244 ( (uint64_t) buf[3] << 16 ) |
3245 ( (uint64_t) buf[4] << 8 ) |
3246 ( (uint64_t) buf[5] ) );
3247}
3248
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003249MBEDTLS_CHECK_RETURN_CRITICAL
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003250static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3251{
Janos Follath865b3eb2019-12-16 11:46:15 +00003252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003253 unsigned char *original_in_ctr;
3254
3255 // save original in_ctr
3256 original_in_ctr = ssl->in_ctr;
3257
3258 // use counter from record
3259 ssl->in_ctr = record_in_ctr;
3260
3261 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3262
3263 // restore the counter
3264 ssl->in_ctr = original_in_ctr;
3265
3266 return ret;
3267}
3268
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003269/*
3270 * Return 0 if sequence number is acceptable, -1 otherwise
3271 */
Hanno Becker0183d692019-07-12 08:50:37 +01003272int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003273{
3274 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3275 uint64_t bit;
3276
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003277 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003278 return( 0 );
3279
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003280 if( rec_seqnum > ssl->in_window_top )
3281 return( 0 );
3282
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003283 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003284
3285 if( bit >= 64 )
3286 return( -1 );
3287
3288 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3289 return( -1 );
3290
3291 return( 0 );
3292}
3293
3294/*
3295 * Update replay window on new validated record
3296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003298{
3299 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3300
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003301 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003302 return;
3303
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003304 if( rec_seqnum > ssl->in_window_top )
3305 {
3306 /* Update window_top and the contents of the window */
3307 uint64_t shift = rec_seqnum - ssl->in_window_top;
3308
3309 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003310 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003311 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003312 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003313 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003314 ssl->in_window |= 1;
3315 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003316
3317 ssl->in_window_top = rec_seqnum;
3318 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003319 else
3320 {
3321 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003322 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003323
3324 if( bit < 64 ) /* Always true, but be extra sure */
3325 ssl->in_window |= (uint64_t) 1 << bit;
3326 }
3327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003329
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003330#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003331/*
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003332 * Check if a datagram looks like a ClientHello with a valid cookie,
3333 * and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003334 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003335 *
3336 * - if cookie is valid, return 0
3337 * - if ClientHello looks superficially valid but cookie is not,
3338 * fill obuf and set olen, then
3339 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3340 * - otherwise return a specific error code
3341 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003342MBEDTLS_CHECK_RETURN_CRITICAL
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003343MBEDTLS_STATIC_TESTABLE
3344int mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003345 mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003346 const unsigned char *cli_id, size_t cli_id_len,
3347 const unsigned char *in, size_t in_len,
3348 unsigned char *obuf, size_t buf_len, size_t *olen )
3349{
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003350 size_t sid_len, cookie_len, epoch, fragment_offset;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003351 unsigned char *p;
3352
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003353 /*
3354 * Structure of ClientHello with record and handshake headers,
3355 * and expected values. We don't need to check a lot, more checks will be
3356 * done when actually parsing the ClientHello - skipping those checks
3357 * avoids code duplication and does not make cookie forging any easier.
3358 *
3359 * 0-0 ContentType type; copied, must be handshake
3360 * 1-2 ProtocolVersion version; copied
3361 * 3-4 uint16 epoch; copied, must be 0
3362 * 5-10 uint48 sequence_number; copied
3363 * 11-12 uint16 length; (ignored)
3364 *
3365 * 13-13 HandshakeType msg_type; (ignored)
3366 * 14-16 uint24 length; (ignored)
3367 * 17-18 uint16 message_seq; copied
3368 * 19-21 uint24 fragment_offset; copied, must be 0
3369 * 22-24 uint24 fragment_length; (ignored)
3370 *
3371 * 25-26 ProtocolVersion client_version; (ignored)
3372 * 27-58 Random random; (ignored)
3373 * 59-xx SessionID session_id; 1 byte len + sid_len content
3374 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3375 * ...
3376 *
3377 * Minimum length is 61 bytes.
3378 */
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003379 MBEDTLS_SSL_DEBUG_MSG( 4, ( "check cookie: in_len=%u",
3380 (unsigned) in_len ) );
3381 MBEDTLS_SSL_DEBUG_BUF( 4, "cli_id", cli_id, cli_id_len );
3382 if( in_len < 61 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003383 {
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003384 MBEDTLS_SSL_DEBUG_MSG( 4, ( "check cookie: record too short" ) );
3385 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
3386 }
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003387
3388 epoch = MBEDTLS_GET_UINT16_BE( in, 3 );
3389 fragment_offset = MBEDTLS_GET_UINT24_BE( in, 19 );
3390
3391 if( in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || epoch != 0 ||
3392 fragment_offset != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003393 {
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003394 MBEDTLS_SSL_DEBUG_MSG( 4, ( "check cookie: not a good ClientHello" ) );
3395 MBEDTLS_SSL_DEBUG_MSG( 4, ( " type=%u epoch=%u fragment_offset=%u",
Andrzej Kurekcbe14ec2022-06-15 07:17:28 -04003396 in[0], (unsigned) epoch,
3397 (unsigned) fragment_offset ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01003398 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003399 }
3400
3401 sid_len = in[59];
Andrzej Kurekc8183cc2022-06-06 14:42:41 -04003402 if( 59 + 1 + sid_len + 1 > in_len )
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003403 {
3404 MBEDTLS_SSL_DEBUG_MSG( 4, ( "check cookie: sid_len=%u > %u",
3405 (unsigned) sid_len,
3406 (unsigned) in_len - 61 ) );
Hanno Becker90d59dd2021-06-24 11:17:13 +01003407 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003408 }
3409 MBEDTLS_SSL_DEBUG_BUF( 4, "sid received from network",
3410 in + 60, sid_len );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003411
3412 cookie_len = in[60 + sid_len];
Andrzej Kurekc8183cc2022-06-06 14:42:41 -04003413 if( 59 + 1 + sid_len + 1 + cookie_len > in_len )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003414 {
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003415 MBEDTLS_SSL_DEBUG_MSG( 4, ( "check cookie: cookie_len=%u > %u",
3416 (unsigned) cookie_len,
Andrzej Kurekc8183cc2022-06-06 14:42:41 -04003417 (unsigned) ( in_len - sid_len - 61 ) ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003418 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003419 }
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003420
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003421 MBEDTLS_SSL_DEBUG_BUF( 4, "cookie received from network",
3422 in + sid_len + 61, cookie_len );
3423 if( ssl->conf->f_cookie_check( ssl->conf->p_cookie,
3424 in + sid_len + 61, cookie_len,
3425 cli_id, cli_id_len ) == 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003426 {
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003427 MBEDTLS_SSL_DEBUG_MSG( 4, ( "check cookie: valid" ) );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003428 return( 0 );
3429 }
3430
3431 /*
3432 * If we get here, we've got an invalid cookie, let's prepare HVR.
3433 *
3434 * 0-0 ContentType type; copied
3435 * 1-2 ProtocolVersion version; copied
3436 * 3-4 uint16 epoch; copied
3437 * 5-10 uint48 sequence_number; copied
3438 * 11-12 uint16 length; olen - 13
3439 *
3440 * 13-13 HandshakeType msg_type; hello_verify_request
3441 * 14-16 uint24 length; olen - 25
3442 * 17-18 uint16 message_seq; copied
3443 * 19-21 uint24 fragment_offset; copied
3444 * 22-24 uint24 fragment_length; olen - 25
3445 *
3446 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3447 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3448 *
3449 * Minimum length is 28.
3450 */
3451 if( buf_len < 28 )
3452 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3453
3454 /* Copy most fields and adapt others */
3455 memcpy( obuf, in, 25 );
3456 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3457 obuf[25] = 0xfe;
3458 obuf[26] = 0xff;
3459
3460 /* Generate and write actual cookie */
3461 p = obuf + 28;
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003462 if( ssl->conf->f_cookie_write( ssl->conf->p_cookie,
3463 &p, obuf + buf_len,
3464 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003465 {
3466 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3467 }
3468
3469 *olen = p - obuf;
3470
3471 /* Go back and fill length fields */
3472 obuf[27] = (unsigned char)( *olen - 28 );
3473
Joe Subbianifbeb6922021-07-16 14:27:50 +01003474 obuf[14] = obuf[22] = MBEDTLS_BYTE_2( *olen - 25 );
3475 obuf[15] = obuf[23] = MBEDTLS_BYTE_1( *olen - 25 );
3476 obuf[16] = obuf[24] = MBEDTLS_BYTE_0( *olen - 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003477
Joe Subbiani6dd73642021-07-19 11:56:54 +01003478 MBEDTLS_PUT_UINT16_BE( *olen - 13, obuf, 11 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003479
3480 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3481}
3482
3483/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003484 * Handle possible client reconnect with the same UDP quadruplet
3485 * (RFC 6347 Section 4.2.8).
3486 *
3487 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3488 * that looks like a ClientHello.
3489 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003490 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003491 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003492 * - if the input looks like a ClientHello with a valid cookie,
3493 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003494 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003495 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003496 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003497 * This function is called (through ssl_check_client_reconnect()) when an
3498 * unexpected record is found in ssl_get_next_record(), which will discard the
3499 * record if we return 0, and bubble up the return value otherwise (this
3500 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3501 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003502 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003503MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003504static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3505{
Janos Follath865b3eb2019-12-16 11:46:15 +00003506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003507 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003508
Hanno Becker2fddd372019-07-10 14:37:41 +01003509 if( ssl->conf->f_cookie_write == NULL ||
3510 ssl->conf->f_cookie_check == NULL )
3511 {
3512 /* If we can't use cookies to verify reachability of the peer,
3513 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3515 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003516 return( 0 );
3517 }
3518
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003519 ret = mbedtls_ssl_check_dtls_clihlo_cookie(
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003520 ssl,
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003521 ssl->cli_id, ssl->cli_id_len,
3522 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003523 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003524
Andrzej Kurek078e9bc2022-06-08 11:47:33 -04003525 MBEDTLS_SSL_DEBUG_RET( 2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003526
3527 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003528 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003529 int send_ret;
3530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3531 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3532 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003533 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003534 * If the error is permanent we'll catch it later,
3535 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003536 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3537 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3538 (void) send_ret;
3539
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003540 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003541 }
3542
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003543 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003544 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003546 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003547 {
3548 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3549 return( ret );
3550 }
3551
3552 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003553 }
3554
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003555 return( ret );
3556}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003557#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003558
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003559MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003560static int ssl_check_record_type( uint8_t record_type )
3561{
3562 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3563 record_type != MBEDTLS_SSL_MSG_ALERT &&
3564 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3565 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3566 {
3567 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3568 }
3569
3570 return( 0 );
3571}
3572
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003573/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003574 * ContentType type;
3575 * ProtocolVersion version;
3576 * uint16 epoch; // DTLS only
3577 * uint48 sequence_number; // DTLS only
3578 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003579 *
3580 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003581 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003582 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3583 *
3584 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003585 * 1. proceed with the record if this function returns 0
3586 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3587 * 3. return CLIENT_RECONNECT if this function return that value
3588 * 4. drop the whole datagram if this function returns anything else.
3589 * Point 2 is needed when the peer is resending, and we have already received
3590 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003591 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003592MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker331de3d2019-07-12 11:10:16 +01003593static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003594 unsigned char *buf,
3595 size_t len,
3596 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003597{
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003598 mbedtls_ssl_protocol_version tls_version;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Hanno Beckere5e7e782019-07-11 12:29:35 +01003600 size_t const rec_hdr_type_offset = 0;
3601 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003602
Hanno Beckere5e7e782019-07-11 12:29:35 +01003603 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3604 rec_hdr_type_len;
3605 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Hanno Beckere5e7e782019-07-11 12:29:35 +01003607 size_t const rec_hdr_ctr_len = 8;
3608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003609 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003610 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3611 rec_hdr_version_len;
3612
Hanno Beckera0e20d02019-05-15 14:03:01 +01003613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003614 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3615 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003616 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003617#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3618#endif /* MBEDTLS_SSL_PROTO_DTLS */
3619
3620 size_t rec_hdr_len_offset; /* To be determined */
3621 size_t const rec_hdr_len_len = 2;
3622
3623 /*
3624 * Check minimum lengths for record header.
3625 */
3626
3627#if defined(MBEDTLS_SSL_PROTO_DTLS)
3628 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3629 {
3630 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3631 }
3632 else
3633#endif /* MBEDTLS_SSL_PROTO_DTLS */
3634 {
3635 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3636 }
3637
3638 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3639 {
3640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3641 (unsigned) len,
3642 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3643 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3644 }
3645
3646 /*
3647 * Parse and validate record content type
3648 */
3649
3650 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003651
3652 /* Check record content type */
3653#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3654 rec->cid_len = 0;
3655
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003656 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003657 ssl->conf->cid_len != 0 &&
3658 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003659 {
3660 /* Shift pointers to account for record header including CID
3661 * struct {
Hannes Tschofenigfd6cca42021-10-12 09:22:33 +02003662 * ContentType outer_type = tls12_cid;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003663 * ProtocolVersion version;
3664 * uint16 epoch;
3665 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003666 * opaque cid[cid_length]; // Additional field compared to
3667 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003668 * uint16 length;
3669 * opaque enc_content[DTLSCiphertext.length];
3670 * } DTLSCiphertext;
3671 */
3672
3673 /* So far, we only support static CID lengths
3674 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003675 rec_hdr_cid_len = ssl->conf->cid_len;
3676 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003677
Hanno Beckere5e7e782019-07-11 12:29:35 +01003678 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003679 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3681 (unsigned) len,
3682 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003683 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003684 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003685
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003686 /* configured CID len is guaranteed at most 255, see
3687 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3688 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003689 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003690 }
3691 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003692#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003693 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003694 if( ssl_check_record_type( rec->type ) )
3695 {
Hanno Becker54229812019-07-12 14:40:00 +01003696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3697 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003698 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3699 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003700 }
3701
Hanno Beckere5e7e782019-07-11 12:29:35 +01003702 /*
3703 * Parse and validate record version
3704 */
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003705 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3706 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003707 tls_version = mbedtls_ssl_read_version( buf + rec_hdr_version_offset,
3708 ssl->conf->transport );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003709
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003710 if( tls_version > ssl->conf->max_tls_version )
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 {
Gilles Peskine364fd8b2022-02-15 23:53:36 +01003712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS version mismatch: got %u, expected max %u",
3713 (unsigned) tls_version,
3714 (unsigned) ssl->conf->max_tls_version) );
3715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003718 /*
3719 * Parse/Copy record sequence number.
3720 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003721
Hanno Beckere5e7e782019-07-11 12:29:35 +01003722#if defined(MBEDTLS_SSL_PROTO_DTLS)
3723 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003724 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003725 /* Copy explicit record sequence number from input buffer. */
3726 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3727 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003728 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003729 else
3730#endif /* MBEDTLS_SSL_PROTO_DTLS */
3731 {
3732 /* Copy implicit record sequence number from SSL context structure. */
3733 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3734 }
Paul Bakker40e46942009-01-03 21:51:57 +00003735
Hanno Beckere5e7e782019-07-11 12:29:35 +01003736 /*
3737 * Parse record length.
3738 */
3739
Hanno Beckere5e7e782019-07-11 12:29:35 +01003740 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003741 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3742 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003743 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003744
Paul Elliott9f352112020-12-09 14:55:45 +00003745 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Glenn Strausse3af4cb2022-03-15 03:23:42 -04003746 "version = [0x%x], msglen = %" MBEDTLS_PRINTF_SIZET,
3747 rec->type, (unsigned)tls_version, rec->data_len ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003748
3749 rec->buf = buf;
3750 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003751
Hanno Beckerd417cc92019-07-26 08:20:27 +01003752 if( rec->data_len == 0 )
3753 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003755 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003756 * DTLS-related tests.
3757 * Check epoch before checking length constraint because
3758 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3759 * message gets duplicated before the corresponding Finished message,
3760 * the second ChangeCipherSpec should be discarded because it belongs
3761 * to an old epoch, but not because its length is shorter than
3762 * the minimum record length for packets using the new record transform.
3763 * Note that these two kinds of failures are handled differently,
3764 * as an unexpected record is silently skipped but an invalid
3765 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003766 */
3767#if defined(MBEDTLS_SSL_PROTO_DTLS)
3768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3769 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003770 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003771
Hanno Becker955a5c92019-07-10 17:12:07 +01003772 /* Check that the datagram is large enough to contain a record
3773 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003774 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003775 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3777 (unsigned) len,
3778 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003779 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3780 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003781
Hanno Becker37cfe732019-07-10 17:20:01 +01003782 /* Records from other, non-matching epochs are silently discarded.
3783 * (The case of same-port Client reconnects must be considered in
3784 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003785 if( rec_epoch != ssl->in_epoch )
3786 {
3787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003788 "expected %u, received %lu",
3789 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003790
Hanno Becker552f7472019-07-19 10:59:12 +01003791 /* Records from the next epoch are considered for buffering
3792 * (concretely: early Finished messages). */
3793 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003794 {
Hanno Becker552f7472019-07-19 10:59:12 +01003795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3796 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003797 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003798
Hanno Becker2fddd372019-07-10 14:37:41 +01003799 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003800 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003801#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003802 /* For records from the correct epoch, check whether their
3803 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003804 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3805 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003806 {
3807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3808 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3809 }
3810#endif
3811 }
3812#endif /* MBEDTLS_SSL_PROTO_DTLS */
3813
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003814 return( 0 );
3815}
Paul Bakker5121ce52009-01-03 21:22:43 +00003816
Paul Bakker5121ce52009-01-03 21:22:43 +00003817
Hanno Becker2fddd372019-07-10 14:37:41 +01003818#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003819MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker2fddd372019-07-10 14:37:41 +01003820static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3821{
3822 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3823
3824 /*
3825 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3826 * access the first byte of record content (handshake type), as we
3827 * have an active transform (possibly iv_len != 0), so use the
3828 * fact that the record header len is 13 instead.
3829 */
3830 if( rec_epoch == 0 &&
3831 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Paul Elliott27b0d942022-03-18 21:55:32 +00003832 mbedtls_ssl_is_handshake_over( ssl ) == 1 &&
Hanno Becker2fddd372019-07-10 14:37:41 +01003833 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3834 ssl->in_left > 13 &&
3835 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3836 {
3837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3838 "from the same port" ) );
3839 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 }
3841
3842 return( 0 );
3843}
Hanno Becker2fddd372019-07-10 14:37:41 +01003844#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003845
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003846/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003847 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003848 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003849MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckerfdf66042019-07-11 13:07:45 +01003850static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3851 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003852{
3853 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003856 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857
Ronald Cron7e38cba2021-11-24 12:43:39 +01003858 /*
3859 * In TLS 1.3, always treat ChangeCipherSpec records
3860 * as unencrypted. The only thing we do with them is
3861 * check the length and content and ignore them.
3862 */
Ronald Cron6f135e12021-12-08 16:57:54 +01003863#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron7e38cba2021-11-24 12:43:39 +01003864 if( ssl->transform_in != NULL &&
Glenn Strauss07c64162022-03-14 12:34:51 -04003865 ssl->transform_in->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron7e38cba2021-11-24 12:43:39 +01003866 {
3867 if( rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3868 done = 1;
3869 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003870#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron7e38cba2021-11-24 12:43:39 +01003871
Paul Bakker48916f92012-09-16 19:57:18 +00003872 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003874 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003875
Hanno Beckera18d1322018-01-03 14:27:32 +00003876 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003877 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003880
Hanno Beckera0e20d02019-05-15 14:03:01 +01003881#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003882 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3883 ssl->conf->ignore_unexpected_cid
3884 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3885 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003886 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003887 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003888 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003889#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003890
Paul Bakker5121ce52009-01-03 21:22:43 +00003891 return( ret );
3892 }
3893
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003894 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003895 {
3896 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003897 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003898 }
3899
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003900 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003901 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003902
Hanno Beckera0e20d02019-05-15 14:03:01 +01003903#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003904 /* We have already checked the record content type
3905 * in ssl_parse_record_header(), failing or silently
3906 * dropping the record in the case of an unknown type.
3907 *
3908 * Since with the use of CIDs, the record content type
3909 * might change during decryption, re-check the record
3910 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003911 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003912 {
3913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3914 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3915 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003916#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003917
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003918 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003919 {
3920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Glenn Strauss60bfe602022-03-14 19:04:24 -04003921 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003922 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003923 {
3924 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3926 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3927 }
3928#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3929
3930 ssl->nb_zero++;
3931
3932 /*
3933 * Three or more empty messages may be a DoS attack
3934 * (excessive CPU consumption).
3935 */
3936 if( ssl->nb_zero > 3 )
3937 {
3938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003939 "messages, possible DoS attack" ) );
3940 /* Treat the records as if they were not properly authenticated,
3941 * thereby failing the connection if we see more than allowed
3942 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003943 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3944 }
3945 }
3946 else
3947 ssl->nb_zero = 0;
3948
3949#if defined(MBEDTLS_SSL_PROTO_DTLS)
3950 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3951 {
3952 ; /* in_ctr read from peer, not maintained internally */
3953 }
3954 else
3955#endif
3956 {
3957 unsigned i;
Jerry Yuae0b2e22021-10-08 15:21:19 +08003958 for( i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3959 i > mbedtls_ssl_ep_len( ssl ); i-- )
3960 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003961 if( ++ssl->in_ctr[i - 1] != 0 )
3962 break;
Jerry Yuae0b2e22021-10-08 15:21:19 +08003963 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003964
3965 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003966 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003967 {
3968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3969 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3970 }
3971 }
3972
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 }
3974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003976 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003979 }
3980#endif
3981
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003982 /* Check actual (decrypted) record content length against
3983 * configured maximum. */
Paul Elliott668b31f2022-06-10 14:11:31 +01003984 if( rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003985 {
3986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3987 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3988 }
3989
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003990 return( 0 );
3991}
3992
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003993/*
3994 * Read a record.
3995 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003996 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3997 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3998 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003999 */
Hanno Becker1097b342018-08-15 14:09:41 +01004000
4001/* Helper functions for mbedtls_ssl_read_record(). */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004002MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker1097b342018-08-15 14:09:41 +01004003static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004004MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckere74d5562018-08-15 14:26:08 +01004005static int ssl_get_next_record( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004006MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckere74d5562018-08-15 14:26:08 +01004007static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004008
Hanno Becker327c93b2018-08-15 13:56:18 +01004009int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004010 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004011{
Janos Follath865b3eb2019-12-16 11:46:15 +00004012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004015
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004016 if( ssl->keep_current_message == 0 )
4017 {
4018 do {
Simon Butcher99000142016-10-13 17:21:01 +01004019
Hanno Becker26994592018-08-15 14:14:59 +01004020 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004021 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004022 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004023
Hanno Beckere74d5562018-08-15 14:26:08 +01004024 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004025 {
Hanno Becker40f50842018-08-15 14:48:01 +01004026#if defined(MBEDTLS_SSL_PROTO_DTLS)
4027 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004028
Hanno Becker40f50842018-08-15 14:48:01 +01004029 /* We only check for buffered messages if the
4030 * current datagram is fully consumed. */
4031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004032 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004033 {
Hanno Becker40f50842018-08-15 14:48:01 +01004034 if( ssl_load_buffered_message( ssl ) == 0 )
4035 have_buffered = 1;
4036 }
4037
4038 if( have_buffered == 0 )
4039#endif /* MBEDTLS_SSL_PROTO_DTLS */
4040 {
4041 ret = ssl_get_next_record( ssl );
4042 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4043 continue;
4044
4045 if( ret != 0 )
4046 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004047 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004048 return( ret );
4049 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004050 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004051 }
4052
4053 ret = mbedtls_ssl_handle_message_type( ssl );
4054
Hanno Becker40f50842018-08-15 14:48:01 +01004055#if defined(MBEDTLS_SSL_PROTO_DTLS)
4056 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4057 {
4058 /* Buffer future message */
4059 ret = ssl_buffer_message( ssl );
4060 if( ret != 0 )
4061 return( ret );
4062
4063 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4064 }
4065#endif /* MBEDTLS_SSL_PROTO_DTLS */
4066
Hanno Becker90333da2017-10-10 11:27:13 +01004067 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4068 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004069
4070 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004071 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004072 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004073 return( ret );
4074 }
4075
Hanno Becker327c93b2018-08-15 13:56:18 +01004076 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004077 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004078 {
4079 mbedtls_ssl_update_handshake_status( ssl );
4080 }
Simon Butcher99000142016-10-13 17:21:01 +01004081 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004082 else
Simon Butcher99000142016-10-13 17:21:01 +01004083 {
Hanno Becker02f59072018-08-15 14:00:24 +01004084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004085 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004086 }
4087
4088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4089
4090 return( 0 );
4091}
4092
Hanno Becker40f50842018-08-15 14:48:01 +01004093#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004094MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004095static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004096{
Hanno Becker40f50842018-08-15 14:48:01 +01004097 if( ssl->in_left > ssl->next_record_offset )
4098 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004099
Hanno Becker40f50842018-08-15 14:48:01 +01004100 return( 0 );
4101}
4102
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004103MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker40f50842018-08-15 14:48:01 +01004104static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4105{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004106 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004107 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004108 int ret = 0;
4109
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004110 if( hs == NULL )
4111 return( -1 );
4112
Hanno Beckere00ae372018-08-20 09:39:42 +01004113 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4114
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004115 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4116 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4117 {
4118 /* Check if we have seen a ChangeCipherSpec before.
4119 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004120 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004121 {
4122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4123 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004124 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004125 }
4126
Hanno Becker39b8bc92018-08-28 17:17:13 +01004127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004128 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4129 ssl->in_msglen = 1;
4130 ssl->in_msg[0] = 1;
4131
4132 /* As long as they are equal, the exact value doesn't matter. */
4133 ssl->in_left = 0;
4134 ssl->next_record_offset = 0;
4135
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004136 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004137 goto exit;
4138 }
Hanno Becker37f95322018-08-16 13:55:32 +01004139
Hanno Beckerb8f50142018-08-28 10:01:34 +01004140#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004141 /* Debug only */
4142 {
4143 unsigned offset;
4144 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4145 {
4146 hs_buf = &hs->buffering.hs[offset];
4147 if( hs_buf->is_valid == 1 )
4148 {
4149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4150 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004151 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004152 }
4153 }
4154 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004155#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004156
4157 /* Check if we have buffered and/or fully reassembled the
4158 * next handshake message. */
4159 hs_buf = &hs->buffering.hs[0];
4160 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4161 {
4162 /* Synthesize a record containing the buffered HS message. */
4163 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4164 ( hs_buf->data[2] << 8 ) |
4165 hs_buf->data[3];
4166
4167 /* Double-check that we haven't accidentally buffered
4168 * a message that doesn't fit into the input buffer. */
4169 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4170 {
4171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4173 }
4174
4175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4176 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4177 hs_buf->data, msg_len + 12 );
4178
4179 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4180 ssl->in_hslen = msg_len + 12;
4181 ssl->in_msglen = msg_len + 12;
4182 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4183
4184 ret = 0;
4185 goto exit;
4186 }
4187 else
4188 {
4189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4190 hs->in_msg_seq ) );
4191 }
4192
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004193 ret = -1;
4194
4195exit:
4196
4197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4198 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004199}
4200
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004201MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckera02b0b42018-08-21 17:20:27 +01004202static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4203 size_t desired )
4204{
4205 int offset;
4206 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4208 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004209
Hanno Becker01315ea2018-08-21 17:22:17 +01004210 /* Get rid of future records epoch first, if such exist. */
4211 ssl_free_buffered_record( ssl );
4212
4213 /* Check if we have enough space available now. */
4214 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4215 hs->buffering.total_bytes_buffered ) )
4216 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004218 return( 0 );
4219 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004220
Hanno Becker4f432ad2018-08-28 10:02:32 +01004221 /* We don't have enough space to buffer the next expected handshake
4222 * message. Remove buffers used for future messages to gain space,
4223 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004224 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4225 offset >= 0; offset-- )
4226 {
4227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4228 offset ) );
4229
Hanno Beckerb309b922018-08-23 13:18:05 +01004230 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004231
4232 /* Check if we have enough space available now. */
4233 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4234 hs->buffering.total_bytes_buffered ) )
4235 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004237 return( 0 );
4238 }
4239 }
4240
4241 return( -1 );
4242}
4243
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004244MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker40f50842018-08-15 14:48:01 +01004245static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4246{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004247 int ret = 0;
4248 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4249
4250 if( hs == NULL )
4251 return( 0 );
4252
4253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4254
4255 switch( ssl->in_msgtype )
4256 {
4257 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004259
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004260 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004261 break;
4262
4263 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004264 {
4265 unsigned recv_msg_seq_offset;
4266 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4267 mbedtls_ssl_hs_buffer *hs_buf;
4268 size_t msg_len = ssl->in_hslen - 12;
4269
4270 /* We should never receive an old handshake
4271 * message - double-check nonetheless. */
4272 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4273 {
4274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4276 }
4277
4278 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4279 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4280 {
4281 /* Silently ignore -- message too far in the future */
4282 MBEDTLS_SSL_DEBUG_MSG( 2,
4283 ( "Ignore future HS message with sequence number %u, "
4284 "buffering window %u - %u",
4285 recv_msg_seq, ssl->handshake->in_msg_seq,
4286 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4287
4288 goto exit;
4289 }
4290
4291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4292 recv_msg_seq, recv_msg_seq_offset ) );
4293
4294 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4295
4296 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004297 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004298 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004299 size_t reassembly_buf_sz;
4300
Hanno Becker37f95322018-08-16 13:55:32 +01004301 hs_buf->is_fragmented =
4302 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4303
4304 /* We copy the message back into the input buffer
4305 * after reassembly, so check that it's not too large.
4306 * This is an implementation-specific limitation
4307 * and not one from the standard, hence it is not
4308 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004309 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004310 {
4311 /* Ignore message */
4312 goto exit;
4313 }
4314
Hanno Beckere0b150f2018-08-21 15:51:03 +01004315 /* Check if we have enough space to buffer the message. */
4316 if( hs->buffering.total_bytes_buffered >
4317 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4318 {
4319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4320 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4321 }
4322
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004323 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4324 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004325
4326 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4327 hs->buffering.total_bytes_buffered ) )
4328 {
4329 if( recv_msg_seq_offset > 0 )
4330 {
4331 /* If we can't buffer a future message because
4332 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4334 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4335 " (already %" MBEDTLS_PRINTF_SIZET
4336 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004337 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004338 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004339 goto exit;
4340 }
Hanno Beckere1801392018-08-21 16:51:05 +01004341 else
4342 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4344 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4345 " (already %" MBEDTLS_PRINTF_SIZET
4346 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004347 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004348 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01004349 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004350
Hanno Beckera02b0b42018-08-21 17:20:27 +01004351 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004352 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
4354 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
4355 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
4356 " (already %" MBEDTLS_PRINTF_SIZET
4357 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00004358 msg_len,
4359 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00004360 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004361 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004362 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4363 goto exit;
4364 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004365 }
4366
Paul Elliottd48d5c62021-01-07 14:47:05 +00004367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004368 msg_len ) );
4369
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004370 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4371 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004372 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004373 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004374 goto exit;
4375 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004376 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004377
4378 /* Prepare final header: copy msg_type, length and message_seq,
4379 * then add standardised fragment_offset and fragment_length */
4380 memcpy( hs_buf->data, ssl->in_msg, 6 );
4381 memset( hs_buf->data + 6, 0, 3 );
4382 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4383
4384 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004385
4386 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004387 }
4388 else
4389 {
4390 /* Make sure msg_type and length are consistent */
4391 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4392 {
4393 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4394 /* Ignore */
4395 goto exit;
4396 }
4397 }
4398
Hanno Becker4422bbb2018-08-20 09:40:19 +01004399 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004400 {
4401 size_t frag_len, frag_off;
4402 unsigned char * const msg = hs_buf->data + 12;
4403
4404 /*
4405 * Check and copy current fragment
4406 */
4407
4408 /* Validation of header fields already done in
4409 * mbedtls_ssl_prepare_handshake_record(). */
4410 frag_off = ssl_get_hs_frag_off( ssl );
4411 frag_len = ssl_get_hs_frag_len( ssl );
4412
Paul Elliottd48d5c62021-01-07 14:47:05 +00004413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4414 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004415 frag_off, frag_len ) );
4416 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4417
4418 if( hs_buf->is_fragmented )
4419 {
4420 unsigned char * const bitmask = msg + msg_len;
4421 ssl_bitmask_set( bitmask, frag_off, frag_len );
4422 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4423 msg_len ) == 0 );
4424 }
4425 else
4426 {
4427 hs_buf->is_complete = 1;
4428 }
4429
4430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4431 hs_buf->is_complete ? "" : "not yet " ) );
4432 }
4433
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004434 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004435 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004436
4437 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004438 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004439 break;
4440 }
4441
4442exit:
4443
4444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4445 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004446}
4447#endif /* MBEDTLS_SSL_PROTO_DTLS */
4448
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004449MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker1097b342018-08-15 14:09:41 +01004450static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004451{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004452 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004453 * Consume last content-layer message and potentially
4454 * update in_msglen which keeps track of the contents'
4455 * consumption state.
4456 *
4457 * (1) Handshake messages:
4458 * Remove last handshake message, move content
4459 * and adapt in_msglen.
4460 *
4461 * (2) Alert messages:
4462 * Consume whole record content, in_msglen = 0.
4463 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004464 * (3) Change cipher spec:
4465 * Consume whole record content, in_msglen = 0.
4466 *
4467 * (4) Application data:
4468 * Don't do anything - the record layer provides
4469 * the application data as a stream transport
4470 * and consumes through mbedtls_ssl_read only.
4471 *
4472 */
4473
4474 /* Case (1): Handshake messages */
4475 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004476 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004477 /* Hard assertion to be sure that no application data
4478 * is in flight, as corrupting ssl->in_msglen during
4479 * ssl->in_offt != NULL is fatal. */
4480 if( ssl->in_offt != NULL )
4481 {
4482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4484 }
4485
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004486 /*
4487 * Get next Handshake message in the current record
4488 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004489
Hanno Becker4a810fb2017-05-24 16:27:30 +01004490 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004491 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004492 * current handshake content: If DTLS handshake
4493 * fragmentation is used, that's the fragment
4494 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004495 * size here is faulty and should be changed at
4496 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004497 * (2) While it doesn't seem to cause problems, one
4498 * has to be very careful not to assume that in_hslen
4499 * is always <= in_msglen in a sensible communication.
4500 * Again, it's wrong for DTLS handshake fragmentation.
4501 * The following check is therefore mandatory, and
4502 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004503 * Additionally, ssl->in_hslen might be arbitrarily out of
4504 * bounds after handling a DTLS message with an unexpected
4505 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004506 */
4507 if( ssl->in_hslen < ssl->in_msglen )
4508 {
4509 ssl->in_msglen -= ssl->in_hslen;
4510 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4511 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004512
Hanno Becker4a810fb2017-05-24 16:27:30 +01004513 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4514 ssl->in_msg, ssl->in_msglen );
4515 }
4516 else
4517 {
4518 ssl->in_msglen = 0;
4519 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004520
Hanno Becker4a810fb2017-05-24 16:27:30 +01004521 ssl->in_hslen = 0;
4522 }
4523 /* Case (4): Application data */
4524 else if( ssl->in_offt != NULL )
4525 {
4526 return( 0 );
4527 }
4528 /* Everything else (CCS & Alerts) */
4529 else
4530 {
4531 ssl->in_msglen = 0;
4532 }
4533
Hanno Becker1097b342018-08-15 14:09:41 +01004534 return( 0 );
4535}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004536
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004537MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckere74d5562018-08-15 14:26:08 +01004538static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4539{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004540 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004541 return( 1 );
4542
4543 return( 0 );
4544}
4545
Hanno Becker5f066e72018-08-16 14:56:31 +01004546#if defined(MBEDTLS_SSL_PROTO_DTLS)
4547
4548static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4549{
4550 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4551 if( hs == NULL )
4552 return;
4553
Hanno Becker01315ea2018-08-21 17:22:17 +01004554 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004555 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004556 hs->buffering.total_bytes_buffered -=
4557 hs->buffering.future_record.len;
4558
4559 mbedtls_free( hs->buffering.future_record.data );
4560 hs->buffering.future_record.data = NULL;
4561 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004562}
4563
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004564MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker5f066e72018-08-16 14:56:31 +01004565static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4566{
4567 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4568 unsigned char * rec;
4569 size_t rec_len;
4570 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004571#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4572 size_t in_buf_len = ssl->in_buf_len;
4573#else
4574 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4575#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004576 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4577 return( 0 );
4578
4579 if( hs == NULL )
4580 return( 0 );
4581
Hanno Becker5f066e72018-08-16 14:56:31 +01004582 rec = hs->buffering.future_record.data;
4583 rec_len = hs->buffering.future_record.len;
4584 rec_epoch = hs->buffering.future_record.epoch;
4585
4586 if( rec == NULL )
4587 return( 0 );
4588
Hanno Becker4cb782d2018-08-20 11:19:05 +01004589 /* Only consider loading future records if the
4590 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004591 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004592 return( 0 );
4593
Hanno Becker5f066e72018-08-16 14:56:31 +01004594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4595
4596 if( rec_epoch != ssl->in_epoch )
4597 {
4598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4599 goto exit;
4600 }
4601
4602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4603
4604 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004605 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4609 }
4610
4611 memcpy( ssl->in_hdr, rec, rec_len );
4612 ssl->in_left = rec_len;
4613 ssl->next_record_offset = 0;
4614
4615 ssl_free_buffered_record( ssl );
4616
4617exit:
4618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4619 return( 0 );
4620}
4621
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004622MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Becker519f15d2019-07-11 12:43:20 +01004623static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4624 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004625{
4626 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004627
4628 /* Don't buffer future records outside handshakes. */
4629 if( hs == NULL )
4630 return( 0 );
4631
4632 /* Only buffer handshake records (we are only interested
4633 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004634 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004635 return( 0 );
4636
4637 /* Don't buffer more than one future epoch record. */
4638 if( hs->buffering.future_record.data != NULL )
4639 return( 0 );
4640
Hanno Becker01315ea2018-08-21 17:22:17 +01004641 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004642 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004643 hs->buffering.total_bytes_buffered ) )
4644 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4646 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4647 " (already %" MBEDTLS_PRINTF_SIZET
4648 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004649 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004650 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004651 return( 0 );
4652 }
4653
Hanno Becker5f066e72018-08-16 14:56:31 +01004654 /* Buffer record */
4655 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004656 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004657 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004658
4659 /* ssl_parse_record_header() only considers records
4660 * of the next epoch as candidates for buffering. */
4661 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004662 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004663
4664 hs->buffering.future_record.data =
4665 mbedtls_calloc( 1, hs->buffering.future_record.len );
4666 if( hs->buffering.future_record.data == NULL )
4667 {
4668 /* If we run out of RAM trying to buffer a
4669 * record from the next epoch, just ignore. */
4670 return( 0 );
4671 }
4672
Hanno Becker519f15d2019-07-11 12:43:20 +01004673 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004674
Hanno Becker519f15d2019-07-11 12:43:20 +01004675 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004676 return( 0 );
4677}
4678
4679#endif /* MBEDTLS_SSL_PROTO_DTLS */
4680
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004681MBEDTLS_CHECK_RETURN_CRITICAL
Hanno Beckere74d5562018-08-15 14:26:08 +01004682static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004683{
Janos Follath865b3eb2019-12-16 11:46:15 +00004684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004685 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004686
Hanno Becker5f066e72018-08-16 14:56:31 +01004687#if defined(MBEDTLS_SSL_PROTO_DTLS)
4688 /* We might have buffered a future record; if so,
4689 * and if the epoch matches now, load it.
4690 * On success, this call will set ssl->in_left to
4691 * the length of the buffered record, so that
4692 * the calls to ssl_fetch_input() below will
4693 * essentially be no-ops. */
4694 ret = ssl_load_buffered_record( ssl );
4695 if( ret != 0 )
4696 return( ret );
4697#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004698
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004699 /* Ensure that we have enough space available for the default form
4700 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4701 * with no space for CIDs counted in). */
4702 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4703 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004706 return( ret );
4707 }
4708
Hanno Beckere5e7e782019-07-11 12:29:35 +01004709 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4710 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004713 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004714 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004715 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4716 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004717 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004718 if( ret != 0 )
4719 return( ret );
4720
4721 /* Fall through to handling of unexpected records */
4722 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4723 }
4724
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004725 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4726 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004727#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004728 /* Reset in pointers to default state for TLS/DTLS records,
4729 * assuming no CID and no offset between record content and
4730 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004731 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004732
Hanno Becker7ae20e02019-07-12 08:33:49 +01004733 /* Setup internal message pointers from record structure. */
4734 ssl->in_msgtype = rec.type;
4735#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4736 ssl->in_len = ssl->in_cid + rec.cid_len;
4737#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4738 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4739 ssl->in_msglen = rec.data_len;
4740
Hanno Becker2fddd372019-07-10 14:37:41 +01004741 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004742 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004743 if( ret != 0 )
4744 return( ret );
4745#endif
4746
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004747 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004748 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004749
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4751 "(header)" ) );
4752 }
4753 else
4754 {
4755 /* Skip invalid record and the rest of the datagram */
4756 ssl->next_record_offset = 0;
4757 ssl->in_left = 0;
4758
4759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4760 "(header)" ) );
4761 }
4762
4763 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004764 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004765 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004766 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004767#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004768 {
4769 return( ret );
4770 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004771 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004773#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004774 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004775 {
Hanno Beckera8814792019-07-10 15:01:45 +01004776 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004777 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004778 if( ssl->next_record_offset < ssl->in_left )
4779 {
4780 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4781 }
4782 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004783 else
4784#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004785 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004786 /*
4787 * Fetch record contents from underlying transport.
4788 */
Hanno Beckera3175662019-07-11 12:50:29 +01004789 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004790 if( ret != 0 )
4791 {
4792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4793 return( ret );
4794 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004795
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004796 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004797 }
4798
4799 /*
4800 * Decrypt record contents.
4801 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802
Hanno Beckerfdf66042019-07-11 13:07:45 +01004803 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004805#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004806 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004807 {
4808 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004809 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004810 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004811 /* Except when waiting for Finished as a bad mac here
4812 * probably means something went wrong in the handshake
4813 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4814 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4815 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4816 {
4817#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4818 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4819 {
4820 mbedtls_ssl_send_alert_message( ssl,
4821 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4822 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4823 }
4824#endif
4825 return( ret );
4826 }
4827
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004828 if( ssl->conf->badmac_limit != 0 &&
4829 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4832 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004833 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004834
Hanno Becker4a810fb2017-05-24 16:27:30 +01004835 /* As above, invalid records cause
4836 * dismissal of the whole datagram. */
4837
4838 ssl->next_record_offset = 0;
4839 ssl->in_left = 0;
4840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004842 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004843 }
4844
4845 return( ret );
4846 }
4847 else
4848#endif
4849 {
4850 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4852 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854 mbedtls_ssl_send_alert_message( ssl,
4855 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4856 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004857 }
4858#endif
4859 return( ret );
4860 }
4861 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004862
Hanno Becker44d89b22019-07-12 09:40:44 +01004863
4864 /* Reset in pointers to default state for TLS/DTLS records,
4865 * assuming no CID and no offset between record content and
4866 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004867 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4869 ssl->in_len = ssl->in_cid + rec.cid_len;
4870#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004871 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004872
Hanno Becker8685c822019-07-12 09:37:30 +01004873 /* The record content type may change during decryption,
4874 * so re-read it. */
4875 ssl->in_msgtype = rec.type;
4876 /* Also update the input buffer, because unfortunately
4877 * the server-side ssl_parse_client_hello() reparses the
4878 * record header when receiving a ClientHello initiating
4879 * a renegotiation. */
4880 ssl->in_hdr[0] = rec.type;
4881 ssl->in_msg = rec.buf + rec.data_offset;
4882 ssl->in_msglen = rec.data_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01004883 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->in_len, 0 );
Hanno Becker8685c822019-07-12 09:37:30 +01004884
Simon Butcher99000142016-10-13 17:21:01 +01004885 return( 0 );
4886}
4887
4888int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4889{
Janos Follath865b3eb2019-12-16 11:46:15 +00004890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004891
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004892 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004893 * Handle particular types of records
4894 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 {
Simon Butcher99000142016-10-13 17:21:01 +01004897 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4898 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004899 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004900 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 }
4902
Hanno Beckere678eaa2018-08-21 14:57:46 +01004903 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004904 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004905 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004906 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004908 ssl->in_msglen ) );
4909 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004910 }
4911
Hanno Beckere678eaa2018-08-21 14:57:46 +01004912 if( ssl->in_msg[0] != 1 )
4913 {
4914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4915 ssl->in_msg[0] ) );
4916 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4917 }
4918
4919#if defined(MBEDTLS_SSL_PROTO_DTLS)
4920 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4921 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4922 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4923 {
4924 if( ssl->handshake == NULL )
4925 {
4926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4927 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4928 }
4929
4930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4931 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4932 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004933#endif
Ronald Cron7e38cba2021-11-24 12:43:39 +01004934
Ronald Cron6f135e12021-12-08 16:57:54 +01004935#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Glenn Strauss60bfe602022-03-14 19:04:24 -04004936 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
Ronald Cron7e38cba2021-11-24 12:43:39 +01004937 {
4938#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
4939 MBEDTLS_SSL_DEBUG_MSG( 1,
4940 ( "Ignore ChangeCipherSpec in TLS 1.3 compatibility mode" ) );
4941 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4942#else
4943 MBEDTLS_SSL_DEBUG_MSG( 1,
4944 ( "ChangeCipherSpec invalid in TLS 1.3 without compatibility mode" ) );
4945 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4946#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
4947 }
Ronald Cron6f135e12021-12-08 16:57:54 +01004948#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere678eaa2018-08-21 14:57:46 +01004949 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004951 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004952 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004953 if( ssl->in_msglen != 2 )
4954 {
4955 /* Note: Standard allows for more than one 2 byte alert
4956 to be packed in a single message, but Mbed TLS doesn't
4957 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004959 ssl->in_msglen ) );
4960 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4961 }
4962
Paul Elliott9f352112020-12-09 14:55:45 +00004963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 ssl->in_msg[0], ssl->in_msg[1] ) );
4965
4966 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004967 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004969 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004972 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004974 }
4975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004976 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4977 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004979 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4980 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004981 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004982
4983#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4984 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4985 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4986 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004988 /* Will be handled when trying to parse ServerHello */
4989 return( 0 );
4990 }
4991#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004992 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004993 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004994 }
4995
Hanno Beckerc76c6192017-06-06 10:03:17 +01004996#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004997 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004998 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004999 /* Drop unexpected ApplicationData records,
5000 * except at the beginning of renegotiations */
5001 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
Paul Elliott27b0d942022-03-18 21:55:32 +00005002 mbedtls_ssl_is_handshake_over( ssl ) == 0
Hanno Becker37ae9522019-05-03 16:54:26 +01005003#if defined(MBEDTLS_SSL_RENEGOTIATION)
5004 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5005 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005006#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01005007 )
5008 {
5009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5010 return( MBEDTLS_ERR_SSL_NON_FATAL );
5011 }
5012
5013 if( ssl->handshake != NULL &&
Paul Elliott27b0d942022-03-18 21:55:32 +00005014 mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker37ae9522019-05-03 16:54:26 +01005015 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005016 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01005017 }
5018 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01005019#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005020
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 return( 0 );
5022}
5023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005025{
irwir6c0da642019-09-26 21:07:41 +03005026 return( mbedtls_ssl_send_alert_message( ssl,
5027 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5028 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005029}
5030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005032 unsigned char level,
5033 unsigned char message )
5034{
Janos Follath865b3eb2019-12-16 11:46:15 +00005035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00005036
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005037 if( ssl == NULL || ssl->conf == NULL )
5038 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5039
Hanno Becker5e18f742018-08-06 11:35:16 +01005040 if( ssl->out_left != 0 )
5041 return( mbedtls_ssl_flush_output( ssl ) );
5042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005044 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005047 ssl->out_msglen = 2;
5048 ssl->out_msg[0] = level;
5049 ssl->out_msg[1] = message;
5050
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02005051 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005054 return( ret );
5055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005057
5058 return( 0 );
5059}
5060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005062{
Janos Follath865b3eb2019-12-16 11:46:15 +00005063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005068 ssl->out_msglen = 1;
5069 ssl->out_msg[0] = 1;
5070
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 ssl->state++;
5072
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005073 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005076 return( ret );
5077 }
5078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005080
5081 return( 0 );
5082}
5083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005085{
Janos Follath865b3eb2019-12-16 11:46:15 +00005086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005089
Hanno Becker327c93b2018-08-15 13:56:18 +01005090 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005093 return( ret );
5094 }
5095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005099 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5100 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005101 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005102 }
5103
Hanno Beckere678eaa2018-08-21 14:57:46 +01005104 /* CCS records are only accepted if they have length 1 and content '1',
5105 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005106
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005107 /*
5108 * Switch to our negotiated transform and session parameters for inbound
5109 * data.
5110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005112 ssl->transform_in = ssl->transform_negotiate;
5113 ssl->session_in = ssl->session_negotiate;
5114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005116 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00005119 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005120#endif
5121
5122 /* Increment epoch */
5123 if( ++ssl->in_epoch == 0 )
5124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005126 /* This is highly unlikely to happen for legitimate reasons, so
5127 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005129 }
5130 }
5131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jerry Yufd320e92021-10-08 21:52:41 +08005133 memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005134
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005135 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005136
Paul Bakker5121ce52009-01-03 21:22:43 +00005137 ssl->state++;
5138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005140
5141 return( 0 );
5142}
5143
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005144/* Once ssl->out_hdr as the address of the beginning of the
5145 * next outgoing record is set, deduce the other pointers.
5146 *
5147 * Note: For TLS, we save the implicit record sequence number
5148 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5149 * and the caller has to make sure there's space for this.
5150 */
5151
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005152static size_t ssl_transform_get_explicit_iv_len(
5153 mbedtls_ssl_transform const *transform )
5154{
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005155 return( transform->ivlen - transform->fixed_ivlen );
5156}
5157
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005158void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5159 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005160{
5161#if defined(MBEDTLS_SSL_PROTO_DTLS)
5162 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5163 {
5164 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005165#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08005166 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005167 ssl->out_len = ssl->out_cid;
5168 if( transform != NULL )
5169 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005170#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08005171 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005172#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005173 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005174 }
5175 else
5176#endif
5177 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005178 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005180 ssl->out_cid = ssl->out_len;
5181#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005182 ssl->out_iv = ssl->out_hdr + 5;
5183 }
5184
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005185 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005186 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005187 if( transform != NULL )
5188 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005189}
5190
5191/* Once ssl->in_hdr as the address of the beginning of the
5192 * next incoming record is set, deduce the other pointers.
5193 *
5194 * Note: For TLS, we save the implicit record sequence number
5195 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5196 * and the caller has to make sure there's space for this.
5197 */
5198
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005199void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005200{
Hanno Becker79594fd2019-05-08 09:38:41 +01005201 /* This function sets the pointers to match the case
5202 * of unprotected TLS/DTLS records, with both ssl->in_iv
5203 * and ssl->in_msg pointing to the beginning of the record
5204 * content.
5205 *
5206 * When decrypting a protected record, ssl->in_msg
5207 * will be shifted to point to the beginning of the
5208 * record plaintext.
5209 */
5210
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005211#if defined(MBEDTLS_SSL_PROTO_DTLS)
5212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5213 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005214 /* This sets the header pointers to match records
5215 * without CID. When we receive a record containing
5216 * a CID, the fields are shifted accordingly in
5217 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005218 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005219#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08005220 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005221 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005222#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08005223 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005224#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005225 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005226 }
5227 else
5228#endif
5229 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08005230 ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005231 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005232#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005233 ssl->in_cid = ssl->in_len;
5234#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005235 ssl->in_iv = ssl->in_hdr + 5;
5236 }
5237
Hanno Becker79594fd2019-05-08 09:38:41 +01005238 /* This will be adjusted at record decryption time. */
5239 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005240}
5241
Paul Bakker5121ce52009-01-03 21:22:43 +00005242/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005243 * Setup an SSL context
5244 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005245
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005246void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005247{
5248 /* Set the incoming and outgoing record pointers. */
5249#if defined(MBEDTLS_SSL_PROTO_DTLS)
5250 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5251 {
5252 ssl->out_hdr = ssl->out_buf;
5253 ssl->in_hdr = ssl->in_buf;
5254 }
5255 else
5256#endif /* MBEDTLS_SSL_PROTO_DTLS */
5257 {
Hanno Becker12078f42021-03-02 15:28:41 +00005258 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005259 ssl->out_hdr = ssl->out_buf + 8;
5260 ssl->in_hdr = ssl->in_buf + 8;
5261 }
5262
5263 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005264 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5265 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005266}
5267
Paul Bakker5121ce52009-01-03 21:22:43 +00005268/*
5269 * SSL get accessors
5270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005272{
5273 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5274}
5275
Hanno Becker8b170a02017-10-10 11:51:19 +01005276int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5277{
5278 /*
5279 * Case A: We're currently holding back
5280 * a message for further processing.
5281 */
5282
5283 if( ssl->keep_current_message == 1 )
5284 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005286 return( 1 );
5287 }
5288
5289 /*
5290 * Case B: Further records are pending in the current datagram.
5291 */
5292
5293#if defined(MBEDTLS_SSL_PROTO_DTLS)
5294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5295 ssl->in_left > ssl->next_record_offset )
5296 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005298 return( 1 );
5299 }
5300#endif /* MBEDTLS_SSL_PROTO_DTLS */
5301
5302 /*
5303 * Case C: A handshake message is being processed.
5304 */
5305
Hanno Becker8b170a02017-10-10 11:51:19 +01005306 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5307 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005309 return( 1 );
5310 }
5311
5312 /*
5313 * Case D: An application data message is being processed
5314 */
5315 if( ssl->in_offt != NULL )
5316 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005318 return( 1 );
5319 }
5320
5321 /*
5322 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005323 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005324 * we implement support for multiple alerts in single records.
5325 */
5326
5327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5328 return( 0 );
5329}
5330
Paul Bakker43ca69c2011-01-15 17:35:19 +00005331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005333{
Hanno Becker3136ede2018-08-17 15:28:19 +01005334 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005335 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005336 unsigned block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005337#if defined(MBEDTLS_USE_PSA_CRYPTO)
5338 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
5339 psa_key_type_t key_type;
5340#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005341
Hanno Becker5903de42019-05-03 14:46:38 +01005342 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5343
Hanno Becker78640902018-08-13 16:35:15 +01005344 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005345 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005346
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005347
5348#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielbe47ecf2022-01-31 13:53:11 +01005349 if ( transform->psa_alg == PSA_ALG_GCM ||
5350 transform->psa_alg == PSA_ALG_CCM ||
5351 transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 8 ) ||
5352 transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 ||
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005353 transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005354 {
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005355 transform_expansion = transform->minlen;
5356 }
Przemyslaw Stekiel399ed512022-01-31 08:38:00 +01005357 else if ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005358 {
5359 (void) psa_get_key_attributes( transform->psa_key_enc, &attr );
5360 key_type = psa_get_key_type( &attr );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005361
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005362 block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005363
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005364 /* Expansion due to the addition of the MAC. */
5365 transform_expansion += transform->maclen;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005366
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005367 /* Expansion due to the addition of CBC padding;
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005368 * Theoretically up to 256 bytes, but we never use
5369 * more than the block size of the underlying cipher. */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005370 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005371
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005372 /* For TLS 1.2 or higher, an explicit IV is added
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005373 * after the record header. */
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005374#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005375 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005376#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005377 }
5378 else
5379 {
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01005380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()" ) );
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005381 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005382 }
5383#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 case MBEDTLS_MODE_GCM:
5387 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005388 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005390 transform_expansion = transform->minlen;
5391 break;
5392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005394
5395 block_size = mbedtls_cipher_get_block_size(
5396 &transform->cipher_ctx_enc );
5397
Hanno Becker3136ede2018-08-17 15:28:19 +01005398 /* Expansion due to the addition of the MAC. */
5399 transform_expansion += transform->maclen;
5400
5401 /* Expansion due to the addition of CBC padding;
5402 * Theoretically up to 256 bytes, but we never use
5403 * more than the block size of the underlying cipher. */
5404 transform_expansion += block_size;
5405
TRodziewicz4ca18aa2021-05-20 14:46:20 +02005406 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01005407 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02005408#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005409 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02005410#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005411
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005412 break;
5413
5414 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005417 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005418#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005419
Hanno Beckera0e20d02019-05-15 14:03:01 +01005420#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005421 if( transform->out_cid_len != 0 )
5422 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005423#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005424
Hanno Becker5903de42019-05-03 14:46:38 +01005425 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005426}
5427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005429/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005430 * Check record counters and renegotiate if they're above the limit.
5431 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005432MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005434{
Hanno Beckerdd772292020-02-05 10:38:31 +00005435 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005436 int in_ctr_cmp;
5437 int out_ctr_cmp;
5438
Paul Elliott27b0d942022-03-18 21:55:32 +00005439 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005441 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005442 {
5443 return( 0 );
5444 }
5445
Andres AG2196c7f2016-12-15 17:01:16 +00005446 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Jerry Yud9a94fe2021-09-28 18:58:59 +08005447 &ssl->conf->renego_period[ep_len],
Jerry Yuae0b2e22021-10-08 15:21:19 +08005448 MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len );
Jerry Yud9a94fe2021-09-28 18:58:59 +08005449 out_ctr_cmp = memcmp( &ssl->cur_out_ctr[ep_len],
5450 &ssl->conf->renego_period[ep_len],
5451 sizeof( ssl->cur_out_ctr ) - ep_len );
Andres AG2196c7f2016-12-15 17:01:16 +00005452
5453 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005454 {
5455 return( 0 );
5456 }
5457
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005460}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005461#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005462
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005463#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5464
5465#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Jerry Yua0446a02022-07-13 11:22:55 +08005466MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005467static int ssl_tls13_check_new_session_ticket( mbedtls_ssl_context *ssl )
5468{
5469
5470 if( ( ssl->in_hslen == mbedtls_ssl_hs_hdr_len( ssl ) ) ||
5471 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_NEW_SESSION_TICKET ) )
5472 {
5473 return( 0 );
5474 }
5475
5476 ssl->keep_current_message = 1;
5477
5478 MBEDTLS_SSL_DEBUG_MSG( 3, ( "NewSessionTicket received" ) );
5479 mbedtls_ssl_handshake_set_state( ssl,
Jerry Yua357cf42022-07-12 05:36:45 +00005480 MBEDTLS_SSL_NEW_SESSION_TICKET );
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005481
5482 return( MBEDTLS_ERR_SSL_WANT_READ );
5483}
5484#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5485
Jerry Yua0446a02022-07-13 11:22:55 +08005486MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005487static int ssl_tls13_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
5488{
5489
5490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "received post-handshake message" ) );
5491
5492#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
5493 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5494 {
5495 int ret = ssl_tls13_check_new_session_ticket( ssl );
5496 if( ret != 0 )
5497 return( ret );
5498 }
5499#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
5500
5501 /* Fail in all other cases. */
5502 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5503}
5504#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5505
5506#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005507/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005508 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005509 * may only be sent for the purpose of initiating renegotiations.
5510 *
5511 * This function is introduced as a separate helper since the handling
5512 * of post-handshake handshake messages changes significantly in TLS 1.3,
5513 * and having a helper function allows to distinguish between TLS <= 1.2 and
5514 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5515 */
Jerry Yua0446a02022-07-13 11:22:55 +08005516MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005517static int ssl_tls12_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005518{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005520
5521 /*
5522 * - For client-side, expect SERVER_HELLO_REQUEST.
5523 * - For server-side, expect CLIENT_HELLO.
5524 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5525 */
5526
5527#if defined(MBEDTLS_SSL_CLI_C)
5528 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5529 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5530 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
5531 {
5532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
5533
5534 /* With DTLS, drop the packet (probably from last handshake) */
5535#if defined(MBEDTLS_SSL_PROTO_DTLS)
5536 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5537 {
5538 return( 0 );
5539 }
5540#endif
5541 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5542 }
5543#endif /* MBEDTLS_SSL_CLI_C */
5544
5545#if defined(MBEDTLS_SSL_SRV_C)
5546 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5547 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
5548 {
5549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5550
5551 /* With DTLS, drop the packet (probably from last handshake) */
5552#if defined(MBEDTLS_SSL_PROTO_DTLS)
5553 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5554 {
5555 return( 0 );
5556 }
5557#endif
5558 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5559 }
5560#endif /* MBEDTLS_SSL_SRV_C */
5561
5562#if defined(MBEDTLS_SSL_RENEGOTIATION)
5563 /* Determine whether renegotiation attempt should be accepted */
5564 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5565 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5566 ssl->conf->allow_legacy_renegotiation ==
5567 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5568 {
5569 /*
5570 * Accept renegotiation request
5571 */
5572
5573 /* DTLS clients need to know renego is server-initiated */
5574#if defined(MBEDTLS_SSL_PROTO_DTLS)
5575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5576 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5577 {
5578 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5579 }
5580#endif
5581 ret = mbedtls_ssl_start_renegotiation( ssl );
5582 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5583 ret != 0 )
5584 {
5585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5586 ret );
5587 return( ret );
5588 }
5589 }
5590 else
5591#endif /* MBEDTLS_SSL_RENEGOTIATION */
5592 {
5593 /*
5594 * Refuse renegotiation
5595 */
5596
5597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5598
TRodziewicz345165c2021-07-06 13:42:11 +02005599 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5600 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5601 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005602 {
TRodziewicz345165c2021-07-06 13:42:11 +02005603 return( ret );
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005604 }
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005605 }
5606
5607 return( 0 );
5608}
Jerry Yuc62ae5f2022-07-07 09:42:26 +00005609#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5610
5611MBEDTLS_CHECK_RETURN_CRITICAL
5612static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
5613{
5614 /* Check protocol version and dispatch accordingly. */
5615#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
5616 if( ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 )
5617 {
5618 return( ssl_tls13_handle_hs_message_post_handshake( ssl ) );
5619 }
5620#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
5621
5622#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5623 if( ssl->tls_version <= MBEDTLS_SSL_VERSION_TLS1_2 )
5624 {
5625 return( ssl_tls12_handle_hs_message_post_handshake( ssl ) );
5626 }
5627#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5628
5629 /* Should never happen */
5630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5631}
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005632
Paul Bakker48916f92012-09-16 19:57:18 +00005633/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005634 * Receive application data decrypted from the SSL layer
5635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005637{
Janos Follath865b3eb2019-12-16 11:46:15 +00005638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005639 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005640
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005641 if( ssl == NULL || ssl->conf == NULL )
5642 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005647 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005650 return( ret );
5651
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005652 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005653 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005654 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005655 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005656 return( ret );
5657 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005658 }
5659#endif
5660
Hanno Becker4a810fb2017-05-24 16:27:30 +01005661 /*
5662 * Check if renegotiation is necessary and/or handshake is
5663 * in process. If yes, perform/continue, and fall through
5664 * if an unexpected packet is received while the client
5665 * is waiting for the ServerHello.
5666 *
5667 * (There is no equivalent to the last condition on
5668 * the server-side as it is not treated as within
5669 * a handshake while waiting for the ClientHello
5670 * after a renegotiation request.)
5671 */
5672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005673#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005674 ret = ssl_check_ctr_renegotiate( ssl );
5675 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5676 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005679 return( ret );
5680 }
5681#endif
5682
Paul Elliott27b0d942022-03-18 21:55:32 +00005683 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005685 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005686 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5687 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005690 return( ret );
5691 }
5692 }
5693
Hanno Beckere41158b2017-10-23 13:30:32 +01005694 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005695 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005696 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005697 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005698 if( ssl->f_get_timer != NULL &&
5699 ssl->f_get_timer( ssl->p_timer ) == -1 )
5700 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005701 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005702 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005703
Hanno Becker327c93b2018-08-15 13:56:18 +01005704 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005705 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005706 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5707 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005708
Hanno Becker4a810fb2017-05-24 16:27:30 +01005709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 }
5712
5713 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005714 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005715 {
5716 /*
5717 * OpenSSL sends empty messages to randomize the IV
5718 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005719 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005722 return( 0 );
5723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005725 return( ret );
5726 }
5727 }
5728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005730 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005731 ret = ssl_handle_hs_message_post_handshake( ssl );
5732 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005733 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005734 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5735 ret );
5736 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005737 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005738
Hanno Beckerf26cc722021-04-21 07:30:13 +01005739 /* At this point, we don't know whether the renegotiation triggered
5740 * by the post-handshake message has been completed or not. The cases
5741 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005742 * 1) The renegotiation is complete. In this case, no new record
5743 * has been read yet.
5744 * 2) The renegotiation is incomplete because the client received
5745 * an application data record while awaiting the ServerHello.
5746 * 3) The renegotiation is incomplete because the client received
5747 * a non-handshake, non-application data message while awaiting
5748 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005749 *
5750 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005751 * - For 1), the next iteration will read a new record and check
5752 * if it's application data.
5753 * - For 2), the loop condition isn't satisfied as application data
5754 * is present, hence continue is the same as break
5755 * - For 3), the loop condition is satisfied and read_record
5756 * will re-deliver the message that was held back by the client
5757 * when expecting the ServerHello.
5758 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005759
Hanno Becker90333da2017-10-10 11:27:13 +01005760 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005761 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005762#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005763 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005764 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005765 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005766 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005767 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005770 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005771 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005772 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005773 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005774 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005775#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005777 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5778 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005781 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005782 }
5783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005784 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5787 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005788 }
5789
5790 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005791
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005792 /* We're going to return something now, cancel timer,
5793 * except if handshake (renegotiation) is in progress */
Paul Elliott27b0d942022-03-18 21:55:32 +00005794 if( mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Hanno Becker0f57a652020-02-05 10:37:26 +00005795 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005796
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005798 /* If we requested renego but received AppData, resend HelloRequest.
5799 * Do it now, after setting in_offt, to avoid taking this branch
5800 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005801#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005802 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005804 {
Hanno Becker786300f2020-02-05 10:46:40 +00005805 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005806 {
Hanno Becker786300f2020-02-05 10:46:40 +00005807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5808 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005809 return( ret );
5810 }
5811 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005812#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005813#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 }
5815
5816 n = ( len < ssl->in_msglen )
5817 ? len : ssl->in_msglen;
5818
5819 memcpy( buf, ssl->in_offt, n );
5820 ssl->in_msglen -= n;
5821
gabor-mezei-arma3214132020-07-15 10:55:00 +02005822 /* Zeroising the plaintext buffer to erase unused application data
5823 from the memory. */
5824 mbedtls_platform_zeroize( ssl->in_offt, n );
5825
Paul Bakker5121ce52009-01-03 21:22:43 +00005826 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005827 {
5828 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005830 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005831 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005832 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005833 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 /* more data available */
5835 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005836 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839
Paul Bakker23986e52011-04-24 08:57:21 +00005840 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841}
5842
5843/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005844 * Send application data to be encrypted by the SSL layer, taking care of max
5845 * fragment length and buffer size.
5846 *
5847 * According to RFC 5246 Section 6.2.1:
5848 *
5849 * Zero-length fragments of Application data MAY be sent as they are
5850 * potentially useful as a traffic analysis countermeasure.
5851 *
5852 * Therefore, it is possible that the input message length is 0 and the
5853 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005854 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005855MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005856static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005857 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005858{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005859 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5860 const size_t max_len = (size_t) ret;
5861
5862 if( ret < 0 )
5863 {
5864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5865 return( ret );
5866 }
5867
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005868 if( len > max_len )
5869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005874 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5875 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005876 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005878 }
5879 else
5880#endif
5881 len = max_len;
5882 }
Paul Bakker887bd502011-06-08 13:10:54 +00005883
Paul Bakker5121ce52009-01-03 21:22:43 +00005884 if( ssl->out_left != 0 )
5885 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005886 /*
5887 * The user has previously tried to send the data and
5888 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5889 * written. In this case, we expect the high-level write function
5890 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005892 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005894 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 return( ret );
5896 }
5897 }
Paul Bakker887bd502011-06-08 13:10:54 +00005898 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005899 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005900 /*
5901 * The user is trying to send a message the first time, so we need to
5902 * copy the data into the internal buffers and setup the data structure
5903 * to keep track of partial writes
5904 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005905 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005906 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005907 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005908
Gabor Mezei05ebf3b2022-06-28 11:55:35 +02005909 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005912 return( ret );
5913 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005914 }
5915
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005916 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005917}
5918
5919/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005920 * Write application data (public-facing wrapper)
5921 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005922int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005923{
Janos Follath865b3eb2019-12-16 11:46:15 +00005924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005925
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005927
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005928 if( ssl == NULL || ssl->conf == NULL )
5929 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5930
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005931#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005932 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5933 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005934 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005935 return( ret );
5936 }
5937#endif
5938
Paul Elliott27b0d942022-03-18 21:55:32 +00005939 if( mbedtls_ssl_is_handshake_over( ssl ) == 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005940 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005941 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005942 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005944 return( ret );
5945 }
5946 }
5947
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005948 ret = ssl_write_real( ssl, buf, len );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005949
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005951
5952 return( ret );
5953}
5954
5955/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005956 * Notify the peer that the connection is being closed
5957 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005959{
Janos Follath865b3eb2019-12-16 11:46:15 +00005960 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005961
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005962 if( ssl == NULL || ssl->conf == NULL )
5963 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005966
Paul Elliott27b0d942022-03-18 21:55:32 +00005967 if( mbedtls_ssl_is_handshake_over( ssl ) == 1 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005969 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5970 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5971 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005974 return( ret );
5975 }
5976 }
5977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005979
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005980 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005981}
5982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005984{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005985 if( transform == NULL )
5986 return;
5987
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005988#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielce37d112022-01-13 14:53:52 +01005989 psa_destroy_key( transform->psa_key_enc );
5990 psa_destroy_key( transform->psa_key_dec );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005991#else
5992 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5993 mbedtls_cipher_free( &transform->cipher_ctx_dec );
5994#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005995
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005996#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01005997#if defined(MBEDTLS_USE_PSA_CRYPTO)
5998 psa_destroy_key( transform->psa_mac_enc );
5999 psa_destroy_key( transform->psa_mac_dec );
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006000#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001 mbedtls_md_free( &transform->md_ctx_enc );
6002 mbedtls_md_free( &transform->md_ctx_dec );
Neil Armstrongcf8841a2022-02-24 11:17:45 +01006003#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd56ed242018-01-03 15:32:51 +00006004#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006005
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05006006 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006007}
6008
Jerry Yuc7875b52021-09-05 21:05:50 +08006009void mbedtls_ssl_set_inbound_transform( mbedtls_ssl_context *ssl,
6010 mbedtls_ssl_transform *transform )
6011{
Jerry Yuc7875b52021-09-05 21:05:50 +08006012 ssl->transform_in = transform;
Jerry Yufd320e92021-10-08 21:52:41 +08006013 memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Jerry Yuc7875b52021-09-05 21:05:50 +08006014}
6015
6016void mbedtls_ssl_set_outbound_transform( mbedtls_ssl_context *ssl,
6017 mbedtls_ssl_transform *transform )
6018{
6019 ssl->transform_out = transform;
Jerry Yufd320e92021-10-08 21:52:41 +08006020 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
Jerry Yuc7875b52021-09-05 21:05:50 +08006021}
6022
Hanno Becker0271f962018-08-16 13:23:47 +01006023#if defined(MBEDTLS_SSL_PROTO_DTLS)
6024
Hanno Becker533ab5f2020-02-05 10:49:13 +00006025void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01006026{
6027 unsigned offset;
6028 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6029
6030 if( hs == NULL )
6031 return;
6032
Hanno Becker283f5ef2018-08-24 09:34:47 +01006033 ssl_free_buffered_record( ssl );
6034
Hanno Becker0271f962018-08-16 13:23:47 +01006035 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01006036 ssl_buffering_free_slot( ssl, offset );
6037}
6038
6039static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
6040 uint8_t slot )
6041{
6042 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6043 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01006044
6045 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6046 return;
6047
Hanno Beckere605b192018-08-21 15:59:07 +01006048 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01006049 {
Hanno Beckere605b192018-08-21 15:59:07 +01006050 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01006051 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01006052 mbedtls_free( hs_buf->data );
6053 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01006054 }
6055}
6056
6057#endif /* MBEDTLS_SSL_PROTO_DTLS */
6058
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006059/*
6060 * Convert version numbers to/from wire format
6061 * and, for DTLS, to/from TLS equivalent.
6062 *
6063 * For TLS this is the identity.
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006064 * For DTLS, map as follows, then use 1's complement (v -> ~v):
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006065 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006066 * DTLS 1.0 is stored as TLS 1.1 internally
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006067 */
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006068void mbedtls_ssl_write_version( unsigned char version[2], int transport,
6069 mbedtls_ssl_protocol_version tls_version )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071#if defined(MBEDTLS_SSL_PROTO_DTLS)
6072 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006073 tls_version =
6074 ~( tls_version - ( tls_version == 0x0302 ? 0x0202 : 0x0201 ) );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006075#else
6076 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006077#endif
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006078 MBEDTLS_PUT_UINT16_BE( tls_version, version, 0 );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006079}
6080
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006081uint16_t mbedtls_ssl_read_version( const unsigned char version[2],
6082 int transport )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006083{
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006084 uint16_t tls_version = MBEDTLS_GET_UINT16_BE( version, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006085#if defined(MBEDTLS_SSL_PROTO_DTLS)
6086 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006087 tls_version =
6088 ~( tls_version - ( tls_version == 0xfeff ? 0x0202 : 0x0201 ) );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006089#else
6090 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006091#endif
Glenn Strausse3af4cb2022-03-15 03:23:42 -04006092 return tls_version;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006093}
6094
Jerry Yue7047812021-09-13 19:26:39 +08006095/*
Jerry Yu3bf1f972021-09-22 21:37:18 +08006096 * Send pending fatal alert.
6097 * 0, No alert message.
6098 * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
6099 * returned, ssl->alert_reason otherwise.
Jerry Yue7047812021-09-13 19:26:39 +08006100 */
6101int mbedtls_ssl_handle_pending_alert( mbedtls_ssl_context *ssl )
6102{
6103 int ret;
6104
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006105 /* No pending alert, return success*/
6106 if( ssl->send_alert == 0 )
6107 return( 0 );
Jerry Yu394ece62021-09-14 22:17:21 +08006108
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006109 ret = mbedtls_ssl_send_alert_message( ssl,
6110 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6111 ssl->alert_type );
6112
Jerry Yu3bf1f972021-09-22 21:37:18 +08006113 /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
6114 * do not clear the alert to be able to send it later.
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006115 */
6116 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
6117 {
6118 ssl->send_alert = 0;
Jerry Yue7047812021-09-13 19:26:39 +08006119 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006120
6121 if( ret != 0 )
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006122 return( ret );
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006123
Jerry Yubbd5a3f2021-09-18 20:50:22 +08006124 return( ssl->alert_reason );
Jerry Yue7047812021-09-13 19:26:39 +08006125}
6126
Jerry Yu394ece62021-09-14 22:17:21 +08006127/*
6128 * Set pending fatal alert flag.
6129 */
6130void mbedtls_ssl_pend_fatal_alert( mbedtls_ssl_context *ssl,
6131 unsigned char alert_type,
6132 int alert_reason )
6133{
6134 ssl->send_alert = 1;
6135 ssl->alert_type = alert_type;
6136 ssl->alert_reason = alert_reason;
6137}
6138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#endif /* MBEDTLS_SSL_TLS_C */