blob: ac476ec9b30b5d441bdfe7e207466eebc9a0bbc1 [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
TRodziewicz4ca18aa2021-05-20 14:46:20 +020089static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
90 unsigned char *buf,
91 size_t len,
92 mbedtls_record *rec );
93
94int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
95 unsigned char *buf,
96 size_t buflen )
97{
98 int ret = 0;
99 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
100 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
101
102 /* We don't support record checking in TLS because
TRodziewicz2abf03c2021-06-25 14:40:09 +0200103 * there doesn't seem to be a usecase for it.
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200104 */
105 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
106 {
107 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
108 goto exit;
109 }
110#if defined(MBEDTLS_SSL_PROTO_DTLS)
111 else
112 {
113 mbedtls_record rec;
114
115 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
116 if( ret != 0 )
117 {
118 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
119 goto exit;
120 }
121
122 if( ssl->transform_in != NULL )
123 {
124 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
125 if( ret != 0 )
126 {
127 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
128 goto exit;
129 }
130 }
131 }
132#endif /* MBEDTLS_SSL_PROTO_DTLS */
133
134exit:
135 /* On success, we have decrypted the buffer in-place, so make
136 * sure we don't leak any plaintext data. */
137 mbedtls_platform_zeroize( buf, buflen );
138
139 /* For the purpose of this API, treat messages with unexpected CID
140 * as well as such from future epochs as unexpected. */
141 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
142 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
143 {
144 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
145 }
146
147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
148 return( ret );
149}
150
Hanno Becker67bc7c32018-08-06 11:33:50 +0100151#define SSL_DONT_FORCE_FLUSH 0
152#define SSL_FORCE_FLUSH 1
153
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200154#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100155
Hanno Beckerd5847772018-08-28 10:09:23 +0100156/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100157static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
158 uint8_t slot );
159static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
160static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
161static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
162static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100163static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
164 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100165static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100166
Hanno Becker11682cc2018-08-22 14:41:02 +0100167static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100168{
Hanno Becker89490712020-02-05 10:50:12 +0000169 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000170#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
171 size_t out_buf_len = ssl->out_buf_len;
172#else
173 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
174#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100175
Darryl Greenb33cc762019-11-28 14:29:44 +0000176 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100177 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100178
Darryl Greenb33cc762019-11-28 14:29:44 +0000179 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100180}
181
Hanno Becker67bc7c32018-08-06 11:33:50 +0100182static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
183{
Hanno Becker11682cc2018-08-22 14:41:02 +0100184 size_t const bytes_written = ssl->out_left;
185 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100186
187 /* Double-check that the write-index hasn't gone
188 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100189 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100190 {
191 /* Should never happen... */
192 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
193 }
194
195 return( (int) ( mtu - bytes_written ) );
196}
197
198static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
199{
Janos Follath865b3eb2019-12-16 11:46:15 +0000200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100201 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400202 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100203
204#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400205 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100206
207 if( max_len > mfl )
208 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100209
210 /* By the standard (RFC 6066 Sect. 4), the MFL extension
211 * only limits the maximum record payload size, so in theory
212 * we would be allowed to pack multiple records of payload size
213 * MFL into a single datagram. However, this would mean that there's
214 * no way to explicitly communicate MTU restrictions to the peer.
215 *
216 * The following reduction of max_len makes sure that we never
217 * write datagrams larger than MFL + Record Expansion Overhead.
218 */
219 if( max_len <= ssl->out_left )
220 return( 0 );
221
222 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100223#endif
224
225 ret = ssl_get_remaining_space_in_datagram( ssl );
226 if( ret < 0 )
227 return( ret );
228 remaining = (size_t) ret;
229
230 ret = mbedtls_ssl_get_record_expansion( ssl );
231 if( ret < 0 )
232 return( ret );
233 expansion = (size_t) ret;
234
235 if( remaining <= expansion )
236 return( 0 );
237
238 remaining -= expansion;
239 if( remaining >= max_len )
240 remaining = max_len;
241
242 return( (int) remaining );
243}
244
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200245/*
246 * Double the retransmit timeout value, within the allowed range,
247 * returning -1 if the maximum value has already been reached.
248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200250{
251 uint32_t new_timeout;
252
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200253 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200254 return( -1 );
255
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200256 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
257 * in the following way: after the initial transmission and a first
258 * retransmission, back off to a temporary estimated MTU of 508 bytes.
259 * This value is guaranteed to be deliverable (if not guaranteed to be
260 * delivered) of any compliant IPv4 (and IPv6) network, and should work
261 * on most non-IP stacks too. */
262 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400263 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200264 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
266 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200267
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200268 new_timeout = 2 * ssl->handshake->retransmit_timeout;
269
270 /* Avoid arithmetic overflow and range overflow */
271 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200272 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200273 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200274 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200275 }
276
277 ssl->handshake->retransmit_timeout = new_timeout;
Paul Elliott9f352112020-12-09 14:55:45 +0000278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
279 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200280
281 return( 0 );
282}
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200285{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200286 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
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}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200291
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100292/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200294 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000295
Ronald Cron6f135e12021-12-08 16:57:54 +0100296#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker13996922020-05-28 16:15:19 +0100297
298static size_t ssl_compute_padding_length( size_t len,
299 size_t granularity )
300{
301 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
302}
303
Hanno Becker581bc1b2020-05-04 12:20:03 +0100304/* This functions transforms a (D)TLS plaintext fragment and a record content
305 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
306 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
307 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100308 *
309 * struct {
310 * opaque content[DTLSPlaintext.length];
311 * ContentType real_type;
312 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100313 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100314 *
315 * Input:
316 * - `content`: The beginning of the buffer holding the
317 * plaintext to be wrapped.
318 * - `*content_size`: The length of the plaintext in Bytes.
319 * - `max_len`: The number of Bytes available starting from
320 * `content`. This must be `>= *content_size`.
321 * - `rec_type`: The desired record content type.
322 *
323 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100324 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
325 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100326 *
327 * Returns:
328 * - `0` on success.
329 * - A negative error code if `max_len` didn't offer enough space
330 * for the expansion.
331 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100332static int ssl_build_inner_plaintext( unsigned char *content,
333 size_t *content_size,
334 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100335 uint8_t rec_type,
336 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100337{
338 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100339
340 /* Write real content type */
341 if( remaining == 0 )
342 return( -1 );
343 content[ len ] = rec_type;
344 len++;
345 remaining--;
346
347 if( remaining < pad )
348 return( -1 );
349 memset( content + len, 0, pad );
350 len += pad;
351 remaining -= pad;
352
353 *content_size = len;
354 return( 0 );
355}
356
Hanno Becker581bc1b2020-05-04 12:20:03 +0100357/* This function parses a (D)TLSInnerPlaintext structure.
358 * See ssl_build_inner_plaintext() for details. */
359static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100360 size_t *content_size,
361 uint8_t *rec_type )
362{
363 size_t remaining = *content_size;
364
365 /* Determine length of padding by skipping zeroes from the back. */
366 do
367 {
368 if( remaining == 0 )
369 return( -1 );
370 remaining--;
371 } while( content[ remaining ] == 0 );
372
373 *content_size = remaining;
374 *rec_type = content[ remaining ];
375
376 return( 0 );
377}
Ronald Cron6f135e12021-12-08 16:57:54 +0100378#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100379
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100380/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100381 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000382static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100383 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100384 mbedtls_record *rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000385 unsigned minor_ver,
386 size_t taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000387{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100388 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100389 *
390 * additional_data = seq_num + TLSCompressed.type +
391 * TLSCompressed.version + TLSCompressed.length;
392 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100393 * For the CID extension, this is extended as follows
394 * (quoting draft-ietf-tls-dtls-connection-id-05,
395 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100396 *
397 * additional_data = seq_num + DTLSPlaintext.type +
398 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100399 * cid +
400 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100401 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100402 *
403 * For TLS 1.3, the record sequence number is dropped from the AAD
404 * and encoded within the nonce of the AEAD operation instead.
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000405 * Moreover, the additional data involves the length of the TLS
406 * ciphertext, not the TLS plaintext as in earlier versions.
407 * Quoting RFC 8446 (TLS 1.3):
408 *
409 * additional_data = TLSCiphertext.opaque_type ||
410 * TLSCiphertext.legacy_record_version ||
411 * TLSCiphertext.length
412 *
413 * We pass the tag length to this function in order to compute the
414 * ciphertext length from the inner plaintext length rec->data_len via
415 *
416 * TLSCiphertext.length = TLSInnerPlaintext.length + taglen.
417 *
Hanno Beckercab87e62019-04-29 13:52:53 +0100418 */
419
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100420 unsigned char *cur = add_data;
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000421 size_t ad_len_field = rec->data_len;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100422
Ronald Cron6f135e12021-12-08 16:57:54 +0100423#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000424 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
425 {
426 /* In TLS 1.3, the AAD contains the length of the TLSCiphertext,
427 * which differs from the length of the TLSInnerPlaintext
428 * by the length of the authentication tag. */
429 ad_len_field += taglen;
430 }
431 else
Ronald Cron6f135e12021-12-08 16:57:54 +0100432#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100433 {
434 ((void) minor_ver);
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000435 ((void) taglen);
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100436 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
437 cur += sizeof( rec->ctr );
438 }
439
440 *cur = rec->type;
441 cur++;
442
443 memcpy( cur, rec->ver, sizeof( rec->ver ) );
444 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100445
Hanno Beckera0e20d02019-05-15 14:03:01 +0100446#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100447 if( rec->cid_len != 0 )
448 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100449 memcpy( cur, rec->cid, rec->cid_len );
450 cur += rec->cid_len;
451
452 *cur = rec->cid_len;
453 cur++;
454
Joe Subbiani6dd73642021-07-19 11:56:54 +0100455 MBEDTLS_PUT_UINT16_BE( ad_len_field, cur, 0 );
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100456 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100457 }
458 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100459#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100460 {
Joe Subbiani6dd73642021-07-19 11:56:54 +0100461 MBEDTLS_PUT_UINT16_BE( ad_len_field, cur, 0 );
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100462 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100463 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100464
465 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000466}
467
Hanno Becker67a37db2020-05-28 16:27:07 +0100468#if defined(MBEDTLS_GCM_C) || \
469 defined(MBEDTLS_CCM_C) || \
470 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100471static int ssl_transform_aead_dynamic_iv_is_explicit(
472 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100473{
Hanno Becker17263802020-05-28 07:05:48 +0100474 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100475}
476
Hanno Becker17263802020-05-28 07:05:48 +0100477/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
478 *
479 * Concretely, this occurs in two variants:
480 *
481 * a) Fixed and dynamic IV lengths add up to total IV length, giving
482 * IV = fixed_iv || dynamic_iv
483 *
Hanno Becker15952812020-06-04 13:31:46 +0100484 * This variant is used in TLS 1.2 when used with GCM or CCM.
485 *
Hanno Becker17263802020-05-28 07:05:48 +0100486 * b) Fixed IV lengths matches total IV length, giving
487 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100488 *
489 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
490 *
491 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100492 *
493 * This function has the precondition that
494 *
495 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
496 *
497 * which has to be ensured by the caller. If this precondition
498 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100499 */
500static void ssl_build_record_nonce( unsigned char *dst_iv,
501 size_t dst_iv_len,
502 unsigned char const *fixed_iv,
503 size_t fixed_iv_len,
504 unsigned char const *dynamic_iv,
505 size_t dynamic_iv_len )
506{
507 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100508
509 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100510 memset( dst_iv, 0, dst_iv_len );
511 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100512
Hanno Becker17263802020-05-28 07:05:48 +0100513 dst_iv += dst_iv_len - dynamic_iv_len;
514 for( i = 0; i < dynamic_iv_len; i++ )
515 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100516}
Hanno Becker67a37db2020-05-28 16:27:07 +0100517#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100518
Hanno Beckera18d1322018-01-03 14:27:32 +0000519int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
520 mbedtls_ssl_transform *transform,
521 mbedtls_record *rec,
522 int (*f_rng)(void *, unsigned char *, size_t),
523 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000524{
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100525#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_cipher_mode_t mode;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100527#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100528 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000529 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100530 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100531 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000532 size_t post_avail;
533
534 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000535#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200536 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000537 ((void) ssl);
538#endif
539
540 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200541 * for CBC transformations in TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200542#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
TRodziewicz0f82ec62021-05-12 17:49:18 +0200543 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000544 ((void) f_rng);
545 ((void) p_rng);
546#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000550 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100551 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
554 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100555 if( rec == NULL
556 || rec->buf == NULL
557 || rec->buf_len < rec->data_offset
558 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100559#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100560 || rec->cid_len != 0
561#endif
562 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000563 {
564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100566 }
567
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000568 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100569 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000571 data, rec->data_len );
572
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100573#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000574 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100575#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000576
577 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
578 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
580 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000581 rec->data_len,
582 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000583 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
584 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100585
Hanno Becker92313402020-05-20 13:58:58 +0100586 /* The following two code paths implement the (D)TLSInnerPlaintext
587 * structure present in TLS 1.3 and DTLS 1.2 + CID.
588 *
589 * See ssl_build_inner_plaintext() for more information.
590 *
591 * Note that this changes `rec->data_len`, and hence
592 * `post_avail` needs to be recalculated afterwards.
593 *
594 * Note also that the two code paths cannot occur simultaneously
595 * since they apply to different versions of the protocol. There
596 * is hence no risk of double-addition of the inner plaintext.
597 */
Ronald Cron6f135e12021-12-08 16:57:54 +0100598#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerccc13d02020-05-04 12:30:04 +0100599 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
600 {
Hanno Becker13996922020-05-28 16:15:19 +0100601 size_t padding =
602 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200603 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100604 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100605 &rec->data_len,
606 post_avail,
607 rec->type,
608 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100609 {
610 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
611 }
612
613 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
614 }
Ronald Cron6f135e12021-12-08 16:57:54 +0100615#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100616
Hanno Beckera0e20d02019-05-15 14:03:01 +0100617#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100618 /*
619 * Add CID information
620 */
621 rec->cid_len = transform->out_cid_len;
622 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
623 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100624
625 if( rec->cid_len != 0 )
626 {
Hanno Becker13996922020-05-28 16:15:19 +0100627 size_t padding =
628 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200629 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100630 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100631 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100632 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100633 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100634 * Note that this changes `rec->data_len`, and hence
635 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100636 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100637 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100638 &rec->data_len,
639 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100640 rec->type,
641 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100642 {
643 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
644 }
645
646 rec->type = MBEDTLS_SSL_MSG_CID;
647 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100648#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100649
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100650 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
651
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100653 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000655#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100656#if defined(MBEDTLS_USE_PSA_CRYPTO)
657 if ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER ||
658 ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING
659#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( mode == MBEDTLS_MODE_STREAM ||
661 ( mode == MBEDTLS_MODE_CBC
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100662#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000664 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100665#endif
666 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000668 if( post_avail < transform->maclen )
669 {
670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
671 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
672 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200673#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +0200674 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker992b6872017-11-09 18:57:39 +0000676
TRodziewicz345165c2021-07-06 13:42:11 +0200677 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000678 transform->minor_ver,
679 transform->taglen );
Hanno Becker992b6872017-11-09 18:57:39 +0000680
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100681 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
682 add_data_len );
683 if( ret != 0 )
684 goto hmac_failed_etm_disabled;
685 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, data, rec->data_len );
686 if( ret != 0 )
687 goto hmac_failed_etm_disabled;
688 ret = mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
689 if( ret != 0 )
690 goto hmac_failed_etm_disabled;
691 ret = mbedtls_md_hmac_reset( &transform->md_ctx_enc );
692 if( ret != 0 )
693 goto hmac_failed_etm_disabled;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000694
TRodziewicz345165c2021-07-06 13:42:11 +0200695 memcpy( data + rec->data_len, mac, transform->maclen );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200696#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200697
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000698 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
699 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200700
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000701 rec->data_len += transform->maclen;
702 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100703 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100704
705 hmac_failed_etm_disabled:
Gilles Peskined5ba50e2021-12-10 21:33:21 +0100706 mbedtls_platform_zeroize( mac, transform->maclen );
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100707 if( ret != 0 )
708 {
709 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_hmac_xxx", ret );
710 return( ret );
711 }
Paul Bakker577e0062013-08-28 11:57:20 +0200712 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000713#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200715 /*
716 * Encrypt
717 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000718#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100719#if defined(MBEDTLS_USE_PSA_CRYPTO)
720 if ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER )
721#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 if( mode == MBEDTLS_MODE_STREAM )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100723#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000725 size_t olen;
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +0100726#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiel1fe065b2022-01-13 15:56:33 +0100727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +0100728#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100729
Paul Elliottd48d5c62021-01-07 14:47:05 +0000730 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000731 "including %d bytes of padding",
732 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100734#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +0100735 /* The only stream "cipher" we support is "NULL" */
736 if ( transform->psa_alg != MBEDTLS_SSL_NULL_CIPHER )
737 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100738
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +0100739 olen = rec->data_len;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100740#else
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000741 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
742 transform->iv_enc, transform->ivlen,
743 data, rec->data_len,
744 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200747 return( ret );
748 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100749#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200750
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000751 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200755 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 }
Paul Bakker68884e32013-01-07 18:20:04 +0100757 else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000758#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000759
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200760#if defined(MBEDTLS_GCM_C) || \
761 defined(MBEDTLS_CCM_C) || \
762 defined(MBEDTLS_CHACHAPOLY_C)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100763#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiele8847782022-01-24 23:19:21 +0100764 if ( PSA_ALG_IS_AEAD( transform->psa_alg ) )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100765#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200767 mode == MBEDTLS_MODE_CCM ||
768 mode == MBEDTLS_MODE_CHACHAPOLY )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100769#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000770 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200771 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100772 unsigned char *dynamic_iv;
773 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100774 int dynamic_iv_is_explicit =
775 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100776#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +0100777 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100778#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000780
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100781 /* Check that there's space for the authentication tag. */
782 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000783 {
784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
785 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
786 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000787
Paul Bakker68884e32013-01-07 18:20:04 +0100788 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100789 * Build nonce for AEAD encryption.
790 *
791 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
792 * part of the IV is prepended to the ciphertext and
793 * can be chosen freely - in particular, it need not
794 * agree with the record sequence number.
795 * However, since ChaChaPoly as well as all AEAD modes
796 * in TLS 1.3 use the record sequence number as the
797 * dynamic part of the nonce, we uniformly use the
798 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100799 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100800 dynamic_iv = rec->ctr;
801 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200802
Hanno Becker17263802020-05-28 07:05:48 +0100803 ssl_build_record_nonce( iv, sizeof( iv ),
804 transform->iv_enc,
805 transform->fixed_ivlen,
806 dynamic_iv,
807 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100808
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100809 /*
810 * Build additional data for AEAD encryption.
811 * This depends on the TLS version.
812 */
813 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000814 transform->minor_ver,
815 transform->taglen );
Hanno Becker1f10d762019-04-26 13:34:37 +0100816
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200817 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100818 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200819 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100820 dynamic_iv,
821 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000822 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100823 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000824 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200825 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000826 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000827
Paul Bakker68884e32013-01-07 18:20:04 +0100828 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200829 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200830 */
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100831#if defined(MBEDTLS_USE_PSA_CRYPTO)
832 status = psa_aead_encrypt( transform->psa_key_enc,
833 transform->psa_alg,
834 iv, transform->ivlen,
835 add_data, add_data_len,
836 data, rec->data_len,
837 data, rec->buf_len - (data - rec->buf),
838 &rec->data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000839
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100840 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100841 {
842 ret = psa_ssl_status_to_mbedtls( status );
843 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_encrypt_buf", ret );
844 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100845 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100846#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100847 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000848 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100849 add_data, add_data_len,
850 data, rec->data_len, /* src */
851 data, rec->buf_len - (data - rec->buf), /* dst */
852 &rec->data_len,
853 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200854 {
TRodziewicz18efb732021-04-29 23:12:19 +0200855 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200856 return( ret );
857 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100858#endif /* MBEDTLS_USE_PSA_CRYPTO */
859
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000860 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100861 data + rec->data_len - transform->taglen,
862 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100863 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000864 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100865
866 /*
867 * Prefix record content with dynamic IV in case it is explicit.
868 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100869 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100870 {
871 if( rec->data_offset < dynamic_iv_len )
872 {
873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
874 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
875 }
876
877 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
878 rec->data_offset -= dynamic_iv_len;
879 rec->data_len += dynamic_iv_len;
880 }
881
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100882 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000883 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000884 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100885#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200886#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100887#if defined(MBEDTLS_USE_PSA_CRYPTO)
888 if ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
889#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 if( mode == MBEDTLS_MODE_CBC )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100891#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000892 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000894 size_t padlen, i;
895 size_t olen;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100896#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +0100897 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100898 size_t part_len;
899 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
900#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000901
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000902 /* Currently we're always using minimal padding
903 * (up to 255 bytes would be allowed). */
904 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
905 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000906 padlen = 0;
907
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000908 /* Check there's enough space in the buffer for the padding. */
909 if( post_avail < padlen + 1 )
910 {
911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
912 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
913 }
914
Paul Bakker5121ce52009-01-03 21:22:43 +0000915 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000916 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000918 rec->data_len += padlen + 1;
919 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000920
TRodziewicz0f82ec62021-05-12 17:49:18 +0200921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000922 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +0200923 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +0000924 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000925 */
TRodziewicz345165c2021-07-06 13:42:11 +0200926 if( f_rng == NULL )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000927 {
TRodziewicz345165c2021-07-06 13:42:11 +0200928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000930 }
TRodziewicz345165c2021-07-06 13:42:11 +0200931
932 if( rec->data_offset < transform->ivlen )
933 {
934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
935 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
936 }
937
938 /*
939 * Generate IV
940 */
941 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
942 if( ret != 0 )
943 return( ret );
944
945 memcpy( data - transform->ivlen, transform->iv_enc, transform->ivlen );
TRodziewicz0f82ec62021-05-12 17:49:18 +0200946#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000947
Paul Elliottd48d5c62021-01-07 14:47:05 +0000948 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
949 "including %" MBEDTLS_PRINTF_SIZET
950 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000951 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200952 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100954#if defined(MBEDTLS_USE_PSA_CRYPTO)
955 status = psa_cipher_encrypt_setup( &cipher_op,
Przemyslaw Stekield4eab572022-01-17 16:20:10 +0100956 transform->psa_key_enc, transform->psa_alg );
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100957
958 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100959 {
960 ret = psa_ssl_status_to_mbedtls( status );
961 MBEDTLS_SSL_DEBUG_RET( 1, "psa_aead_encrypt", ret );
962 return( ret );
963 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100964
965 status = psa_cipher_set_iv( &cipher_op, transform->iv_enc, transform->ivlen );
966
967 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100968 {
969 ret = psa_ssl_status_to_mbedtls( status );
970 MBEDTLS_SSL_DEBUG_RET( 1, "psa_aead_encrypt", ret );
971 return( ret );
972
973 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100974
975 status = psa_cipher_update( &cipher_op,
976 data, rec->data_len,
977 data, rec->data_len, &olen );
978
979 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100980 {
981 ret = psa_ssl_status_to_mbedtls( status );
982 MBEDTLS_SSL_DEBUG_RET( 1, "psa_aead_encrypt", ret );
983 return( ret );
984
985 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100986
987 status = psa_cipher_finish( &cipher_op,
988 data + olen, rec->data_len - olen,
989 &part_len );
990
991 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +0100992 {
993 ret = psa_ssl_status_to_mbedtls( status );
994 MBEDTLS_SSL_DEBUG_RET( 1, "psa_aead_encrypt", ret );
995 return( ret );
996
997 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +0100998
999 olen += part_len;
1000#else
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001001 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
1002 transform->iv_enc,
1003 transform->ivlen,
1004 data, rec->data_len,
1005 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001008 return( ret );
1009 }
Przemyslaw Stekielb37fae12022-01-13 14:28:44 +01001010#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001011
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001012 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1015 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001016 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001017
TRodziewicz0f82ec62021-05-12 17:49:18 +02001018 data -= transform->ivlen;
1019 rec->data_offset -= transform->ivlen;
1020 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001023 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001024 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00001025 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
1026
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001027 /*
1028 * MAC(MAC_write_key, seq_num +
1029 * TLSCipherText.type +
1030 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001031 * length_of( (IV +) ENC(...) ) +
TRodziewicz2abf03c2021-06-25 14:40:09 +02001032 * IV +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001033 * ENC(content + padding + padding_length));
1034 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001035
1036 if( post_avail < transform->maclen)
1037 {
1038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1039 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1040 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001041
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001042 ssl_extract_add_data_from_record( add_data, &add_data_len,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001043 rec, transform->minor_ver,
1044 transform->taglen );
Hanno Becker1f10d762019-04-26 13:34:37 +01001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001047 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001048 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001049
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001050 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
1051 add_data_len );
1052 if( ret != 0 )
1053 goto hmac_failed_etm_enabled;
1054 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc,
1055 data, rec->data_len );
1056 if( ret != 0 )
1057 goto hmac_failed_etm_enabled;
1058 ret = mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1059 if( ret != 0 )
1060 goto hmac_failed_etm_enabled;
1061 ret = mbedtls_md_hmac_reset( &transform->md_ctx_enc );
1062 if( ret != 0 )
1063 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001064
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001065 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001066
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001067 rec->data_len += transform->maclen;
1068 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001069 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001070
1071 hmac_failed_etm_enabled:
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001072 mbedtls_platform_zeroize( mac, transform->maclen );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001073 if( ret != 0 )
1074 {
1075 MBEDTLS_SSL_DEBUG_RET( 1, "HMAC calculation failed", ret );
1076 return( ret );
1077 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001078 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001081 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001082#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1085 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001088 /* Make extra sure authentication was performed, exactly once */
1089 if( auth_done != 1 )
1090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001093 }
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
1097 return( 0 );
1098}
1099
Hanno Becker605949f2019-07-12 08:23:59 +01001100int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001101 mbedtls_ssl_transform *transform,
1102 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001103{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001104 size_t olen;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001106 int ret;
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001107
1108#else
1109 mbedtls_cipher_mode_t mode;
1110 int ret;
1111#endif /* MBEDTLS_USE_PSA_CRYPTO */
1112
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001113 int auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001114#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001115 size_t padlen = 0, correct = 1;
1116#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001117 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001118 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001119 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001120
Hanno Beckera18d1322018-01-03 14:27:32 +00001121#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001122 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001123 ((void) ssl);
1124#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001127 if( rec == NULL ||
1128 rec->buf == NULL ||
1129 rec->buf_len < rec->data_offset ||
1130 rec->buf_len - rec->data_offset < rec->data_len )
1131 {
1132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001134 }
1135
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001136 data = rec->buf + rec->data_offset;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001137#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001138 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001139#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001140
Hanno Beckera0e20d02019-05-15 14:03:01 +01001141#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001142 /*
1143 * Match record's CID with incoming CID.
1144 */
Hanno Becker938489a2019-05-08 13:02:22 +01001145 if( rec->cid_len != transform->in_cid_len ||
1146 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1147 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001148 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001149 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001150#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001151
Hanno Beckerd086bf02021-03-22 13:01:27 +00001152#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001153#if defined(MBEDTLS_USE_PSA_CRYPTO)
1154 if ( transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER )
1155#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 if( mode == MBEDTLS_MODE_STREAM )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001157#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker68884e32013-01-07 18:20:04 +01001158 {
1159 padlen = 0;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001160
1161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001162 /* The only stream "cipher" we support is "NULL" */
1163 if ( transform->psa_alg != MBEDTLS_SSL_NULL_CIPHER )
1164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001165
Przemyslaw Stekielb97556e2022-02-01 14:52:19 +01001166 olen = rec->data_len;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001167#else
1168
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001169 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1170 transform->iv_dec,
1171 transform->ivlen,
1172 data, rec->data_len,
1173 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001176 return( ret );
1177 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001179
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001180 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001184 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001185
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
Paul Bakker68884e32013-01-07 18:20:04 +01001187 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001188#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001189#if defined(MBEDTLS_GCM_C) || \
1190 defined(MBEDTLS_CCM_C) || \
1191 defined(MBEDTLS_CHACHAPOLY_C)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001192#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekiele8847782022-01-24 23:19:21 +01001193 if ( PSA_ALG_IS_AEAD( transform->psa_alg ) )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001194#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001196 mode == MBEDTLS_MODE_CCM ||
1197 mode == MBEDTLS_MODE_CHACHAPOLY )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001198#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001200 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001201 unsigned char *dynamic_iv;
1202 size_t dynamic_iv_len;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001203#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001204 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001207 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001208 * Extract dynamic part of nonce for AEAD decryption.
1209 *
1210 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1211 * part of the IV is prepended to the ciphertext and
1212 * can be chosen freely - in particular, it need not
1213 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001214 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001215 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001216 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001217 {
1218 if( rec->data_len < dynamic_iv_len )
1219 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1221 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001222 rec->data_len,
1223 dynamic_iv_len ) );
1224 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1225 }
1226 dynamic_iv = data;
1227
1228 data += dynamic_iv_len;
1229 rec->data_offset += dynamic_iv_len;
1230 rec->data_len -= dynamic_iv_len;
1231 }
Hanno Becker17263802020-05-28 07:05:48 +01001232 else
1233 {
1234 dynamic_iv = rec->ctr;
1235 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001236
1237 /* Check that there's space for the authentication tag. */
1238 if( rec->data_len < transform->taglen )
1239 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1241 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001242 rec->data_len,
1243 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001245 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001246 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001247
Hanno Beckerdf8be222020-05-21 15:30:57 +01001248 /*
1249 * Prepare nonce from dynamic and static parts.
1250 */
Hanno Becker17263802020-05-28 07:05:48 +01001251 ssl_build_record_nonce( iv, sizeof( iv ),
1252 transform->iv_dec,
1253 transform->fixed_ivlen,
1254 dynamic_iv,
1255 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001256
Hanno Beckerdf8be222020-05-21 15:30:57 +01001257 /*
1258 * Build additional data for AEAD encryption.
1259 * This depends on the TLS version.
1260 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001261 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001262 transform->minor_ver,
1263 transform->taglen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001264 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001265 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001266
Hanno Beckerd96a6522019-07-10 13:55:25 +01001267 /* Because of the check above, we know that there are
1268 * explicit_iv_len Bytes preceeding data, and taglen
1269 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001270 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001271 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001272
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001273 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001274 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001275 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001276
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001277 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001278 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001279 */
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001280#if defined(MBEDTLS_USE_PSA_CRYPTO)
1281 status = psa_aead_decrypt( transform->psa_key_dec,
1282 transform->psa_alg,
1283 iv, transform->ivlen,
1284 add_data, add_data_len,
1285 data, rec->data_len + transform->taglen,
1286 data, rec->buf_len - (data - rec->buf),
Przemyslaw Stekiel221b5272022-01-20 09:18:44 +01001287 &olen );
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001288
1289 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001290 {
1291 ret = psa_ssl_status_to_mbedtls( status );
1292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_decrypt_buf", ret );
1293 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001294 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001295#else
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001296 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001297 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001298 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001299 data, rec->data_len + transform->taglen, /* src */
1300 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001301 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001302 {
TRodziewicz18efb732021-04-29 23:12:19 +02001303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1306 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001307
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001308 return( ret );
1309 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001310#endif /* MBEDTLS_USE_PSA_CRYPTO */
1311
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001312 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001313
Hanno Beckerd96a6522019-07-10 13:55:25 +01001314 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001315 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001319 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001323#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001324#if defined(MBEDTLS_USE_PSA_CRYPTO)
1325 if ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
1326#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 if( mode == MBEDTLS_MODE_CBC )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001328#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001330 size_t minlen = 0;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001331#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001333 size_t part_len;
1334 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1335#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001336
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 /*
Paul Bakker45829992013-01-03 14:52:21 +01001338 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001340#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001341 /* The ciphertext is prefixed with the CBC IV. */
1342 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001343#endif
Paul Bakker45829992013-01-03 14:52:21 +01001344
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001345 /* Size considerations:
1346 *
1347 * - The CBC cipher text must not be empty and hence
1348 * at least of size transform->ivlen.
1349 *
1350 * Together with the potential IV-prefix, this explains
1351 * the first of the two checks below.
1352 *
1353 * - The record must contain a MAC, either in plain or
1354 * encrypted, depending on whether Encrypt-then-MAC
1355 * is used or not.
1356 * - If it is, the message contains the IV-prefix,
1357 * the CBC ciphertext, and the MAC.
1358 * - If it is not, the padded plaintext, and hence
1359 * the CBC ciphertext, has at least length maclen + 1
1360 * because there is at least the padding length byte.
1361 *
1362 * As the CBC ciphertext is not empty, both cases give the
1363 * lower bound minlen + maclen + 1 on the record size, which
1364 * we test for in the second check below.
1365 */
1366 if( rec->data_len < minlen + transform->ivlen ||
1367 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001368 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1370 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1371 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001372 "+ 1 ) ( + expl IV )", rec->data_len,
1373 transform->ivlen,
1374 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001376 }
1377
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001378 /*
1379 * Authenticate before decrypt if enabled
1380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001382 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383 {
Hanno Becker992b6872017-11-09 18:57:39 +00001384 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001387
Hanno Beckerd96a6522019-07-10 13:55:25 +01001388 /* Update data_len in tandem with add_data.
1389 *
1390 * The subtraction is safe because of the previous check
1391 * data_len >= minlen + maclen + 1.
1392 *
1393 * Afterwards, we know that data + data_len is followed by at
1394 * least maclen Bytes, which justifies the call to
Gabor Mezei90437e32021-10-20 11:59:27 +02001395 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001396 *
1397 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001398 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001399 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001400 transform->minor_ver,
1401 transform->taglen );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001402
Hanno Beckerd96a6522019-07-10 13:55:25 +01001403 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001404 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1405 add_data_len );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001406 ret = mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1407 add_data_len );
1408 if( ret != 0 )
1409 goto hmac_failed_etm_enabled;
1410 ret = mbedtls_md_hmac_update( &transform->md_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001411 data, rec->data_len );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001412 if( ret != 0 )
1413 goto hmac_failed_etm_enabled;
1414 ret = mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1415 if( ret != 0 )
1416 goto hmac_failed_etm_enabled;
1417 ret = mbedtls_md_hmac_reset( &transform->md_ctx_dec );
1418 if( ret != 0 )
1419 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001420
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001421 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1422 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001423 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001424 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001425
Hanno Beckerd96a6522019-07-10 13:55:25 +01001426 /* Compare expected MAC with MAC at the end of the record. */
Gabor Mezei90437e32021-10-20 11:59:27 +02001427 if( mbedtls_ct_memcmp( data + rec->data_len, mac_expect,
gabor-mezei-arm46025642021-07-19 15:19:19 +02001428 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001431 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1432 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001433 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001434 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001435
1436 hmac_failed_etm_enabled:
1437 mbedtls_platform_zeroize( mac_expect, transform->maclen );
1438 if( ret != 0 )
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001439 {
1440 if( ret != MBEDTLS_ERR_SSL_INVALID_MAC )
1441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_hmac_xxx", ret );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001442 return( ret );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001443 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001446
1447 /*
1448 * Check length sanity
1449 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001450
1451 /* We know from above that data_len > minlen >= 0,
1452 * so the following check in particular implies that
1453 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001454 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001455 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001456 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1457 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001458 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001460 }
1461
TRodziewicz0f82ec62021-05-12 17:49:18 +02001462#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001463 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001464 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001465 */
TRodziewicz345165c2021-07-06 13:42:11 +02001466 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
1467 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468
TRodziewicz345165c2021-07-06 13:42:11 +02001469 data += transform->ivlen;
1470 rec->data_offset += transform->ivlen;
1471 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001472#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473
Hanno Beckerd96a6522019-07-10 13:55:25 +01001474 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1475
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001476#if defined(MBEDTLS_USE_PSA_CRYPTO)
1477 status = psa_cipher_decrypt_setup( &cipher_op,
Przemyslaw Stekield4eab572022-01-17 16:20:10 +01001478 transform->psa_key_dec, transform->psa_alg );
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001479
1480 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001481 {
1482 ret = psa_ssl_status_to_mbedtls( status );
1483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_decrypt_buf", ret );
1484 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001485 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001486
1487 status = psa_cipher_set_iv( &cipher_op, transform->iv_dec, transform->ivlen );
1488
1489 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001490 {
1491 ret = psa_ssl_status_to_mbedtls( status );
1492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_decrypt_buf", ret );
1493 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001494 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001495
1496 status = psa_cipher_update( &cipher_op,
1497 data, rec->data_len,
1498 data, rec->data_len, &olen );
1499
1500 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001501 {
1502 ret = psa_ssl_status_to_mbedtls( status );
1503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_decrypt_buf", ret );
1504 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001505 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001506
1507 status = psa_cipher_finish( &cipher_op,
1508 data + olen, rec->data_len - olen,
1509 &part_len );
1510
1511 if( status != PSA_SUCCESS )
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001512 {
1513 ret = psa_ssl_status_to_mbedtls( status );
1514 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_decrypt_buf", ret );
1515 return( ret );
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01001516 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001517
1518 olen += part_len;
1519#else
1520
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001521 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1522 transform->iv_dec, transform->ivlen,
1523 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001526 return( ret );
1527 }
Przemyslaw Stekiel2e9711f2022-01-13 14:50:15 +01001528#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001529
Hanno Beckerd96a6522019-07-10 13:55:25 +01001530 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001531 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001535 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001536
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001537 /* Safe since data_len >= minlen + maclen + 1, so after having
1538 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001539 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1540 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001541 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001542
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001543 if( auth_done == 1 )
1544 {
Gabor Mezei90437e32021-10-20 11:59:27 +02001545 const size_t mask = mbedtls_ct_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001546 rec->data_len,
1547 padlen + 1 );
1548 correct &= mask;
1549 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001550 }
1551 else
Paul Bakker45829992013-01-03 14:52:21 +01001552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001554 if( rec->data_len < transform->maclen + padlen + 1 )
1555 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1557 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1558 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001559 rec->data_len,
1560 transform->maclen,
1561 padlen + 1 ) );
1562 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001563#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001564
Gabor Mezei90437e32021-10-20 11:59:27 +02001565 const size_t mask = mbedtls_ct_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001566 rec->data_len,
1567 transform->maclen + padlen + 1 );
1568 correct &= mask;
1569 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001570 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001572 padlen++;
1573
1574 /* Regardless of the validity of the padding,
1575 * we have data_len >= padlen here. */
1576
TRodziewicz0f82ec62021-05-12 17:49:18 +02001577#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001578 /* The padding check involves a series of up to 256
1579 * consecutive memory reads at the end of the record
1580 * plaintext buffer. In order to hide the length and
1581 * validity of the padding, always perform exactly
1582 * `min(256,plaintext_len)` reads (but take into account
1583 * only the last `padlen` bytes for the padding check). */
1584 size_t pad_count = 0;
1585 volatile unsigned char* const check = data;
1586
1587 /* Index of first padding byte; it has been ensured above
1588 * that the subtraction is safe. */
1589 size_t const padding_idx = rec->data_len - padlen;
1590 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1591 size_t const start_idx = rec->data_len - num_checks;
1592 size_t idx;
1593
1594 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001596 /* pad_count += (idx >= padding_idx) &&
1597 * (check[idx] == padlen - 1);
1598 */
Gabor Mezei90437e32021-10-20 11:59:27 +02001599 const size_t mask = mbedtls_ct_size_mask_ge( idx, padding_idx );
1600 const size_t equal = mbedtls_ct_size_bool_eq( check[idx],
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001601 padlen - 1 );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001602 pad_count += mask & equal;
1603 }
Gabor Mezei90437e32021-10-20 11:59:27 +02001604 correct &= mbedtls_ct_size_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001607 if( padlen > 0 && correct == 0 )
1608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001609#endif
Gabor Mezei90437e32021-10-20 11:59:27 +02001610 padlen &= mbedtls_ct_size_mask( correct );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001611
TRodziewicz0f82ec62021-05-12 17:49:18 +02001612#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001613
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001614 /* If the padding was found to be invalid, padlen == 0
1615 * and the subtraction is safe. If the padding was found valid,
1616 * padlen hasn't been changed and the previous assertion
1617 * data_len >= padlen still holds. */
1618 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001620 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001621#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001627#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001629 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001630#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
1632 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001633 * Authenticate if not done yet.
1634 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001636#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001637 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 {
Hanno Becker992b6872017-11-09 18:57:39 +00001639 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001640 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001641
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001642 /* If the initial value of padlen was such that
1643 * data_len < maclen + padlen + 1, then padlen
1644 * got reset to 1, and the initial check
1645 * data_len >= minlen + maclen + 1
1646 * guarantees that at this point we still
1647 * have at least data_len >= maclen.
1648 *
1649 * If the initial value of padlen was such that
1650 * data_len >= maclen + padlen + 1, then we have
1651 * subtracted either padlen + 1 (if the padding was correct)
1652 * or 0 (if the padding was incorrect) since then,
1653 * hence data_len >= maclen in any case.
1654 */
1655 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001656 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001657 transform->minor_ver,
1658 transform->taglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
TRodziewicz0f82ec62021-05-12 17:49:18 +02001660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001661 /*
1662 * The next two sizes are the minimum and maximum values of
1663 * data_len over all padlen values.
1664 *
1665 * They're independent of padlen, since we previously did
1666 * data_len -= padlen.
1667 *
1668 * Note that max_len + maclen is never more than the buffer
1669 * length, as we previously did in_msglen -= maclen too.
1670 */
1671 const size_t max_len = rec->data_len + padlen;
1672 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1673
Gabor Mezei90437e32021-10-20 11:59:27 +02001674 ret = mbedtls_ct_hmac( &transform->md_ctx_dec,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001675 add_data, add_data_len,
1676 data, rec->data_len, min_len, max_len,
1677 mac_expect );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001678 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 {
Gabor Mezei90437e32021-10-20 11:59:27 +02001680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ct_hmac", ret );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001681 goto hmac_failed_etm_disabled;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001683
Gabor Mezei90437e32021-10-20 11:59:27 +02001684 mbedtls_ct_memcpy_offset( mac_peer, data,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001685 rec->data_len,
1686 min_len, max_len,
1687 transform->maclen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001688#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001690#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001691 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001692 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001693#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Gabor Mezei90437e32021-10-20 11:59:27 +02001695 if( mbedtls_ct_memcmp( mac_peer, mac_expect,
gabor-mezei-arm46025642021-07-19 15:19:19 +02001696 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_DEBUG_ALL)
1699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001700#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 correct = 0;
1702 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001703 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001704
1705 hmac_failed_etm_disabled:
1706 mbedtls_platform_zeroize( mac_peer, transform->maclen );
1707 mbedtls_platform_zeroize( mac_expect, transform->maclen );
1708 if( ret != 0 )
1709 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001711
1712 /*
1713 * Finally check the correct flag
1714 */
1715 if( correct == 0 )
1716 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001717#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001718
1719 /* Make extra sure authentication was performed, exactly once */
1720 if( auth_done != 1 )
1721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1723 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001724 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
Ronald Cron6f135e12021-12-08 16:57:54 +01001726#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerccc13d02020-05-04 12:30:04 +01001727 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1728 {
1729 /* Remove inner padding and infer true content type. */
1730 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1731 &rec->type );
1732
1733 if( ret != 0 )
1734 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1735 }
Ronald Cron6f135e12021-12-08 16:57:54 +01001736#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +01001737
Hanno Beckera0e20d02019-05-15 14:03:01 +01001738#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001739 if( rec->cid_len != 0 )
1740 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001741 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1742 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001743 if( ret != 0 )
1744 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1745 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001746#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
1750 return( 0 );
1751}
1752
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001753#undef MAC_NONE
1754#undef MAC_PLAINTEXT
1755#undef MAC_CIPHERTEXT
1756
Paul Bakker5121ce52009-01-03 21:22:43 +00001757/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001758 * Fill the input message buffer by appending data to it.
1759 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001760 *
1761 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1762 * available (from this read and/or a previous one). Otherwise, an error code
1763 * is returned (possibly EOF or WANT_READ).
1764 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001765 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1766 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1767 * since we always read a whole datagram at once.
1768 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001769 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001770 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001773{
Janos Follath865b3eb2019-12-16 11:46:15 +00001774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001775 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001776#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1777 size_t in_buf_len = ssl->in_buf_len;
1778#else
1779 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1780#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001783
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001784 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001787 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001789 }
1790
Darryl Greenb33cc762019-11-28 14:29:44 +00001791 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1794 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001795 }
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001798 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001800 uint32_t timeout;
1801
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001802 /*
1803 * The point is, we need to always read a full datagram at once, so we
1804 * sometimes read more then requested, and handle the additional data.
1805 * It could be the rest of the current record (while fetching the
1806 * header) and/or some other records in the same datagram.
1807 */
1808
1809 /*
1810 * Move to the next record in the already read datagram if applicable
1811 */
1812 if( ssl->next_record_offset != 0 )
1813 {
1814 if( ssl->in_left < ssl->next_record_offset )
1815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1817 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001818 }
1819
1820 ssl->in_left -= ssl->next_record_offset;
1821
1822 if( ssl->in_left != 0 )
1823 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1825 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001826 ssl->next_record_offset ) );
1827 memmove( ssl->in_hdr,
1828 ssl->in_hdr + ssl->next_record_offset,
1829 ssl->in_left );
1830 }
1831
1832 ssl->next_record_offset = 0;
1833 }
1834
Paul Elliottd48d5c62021-01-07 14:47:05 +00001835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1836 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001838
1839 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001840 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001841 */
1842 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001845 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001846 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001847
1848 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001849 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001850 * are not at the beginning of a new record, the caller did something
1851 * wrong.
1852 */
1853 if( ssl->in_left != 0 )
1854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1856 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001857 }
1858
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001859 /*
1860 * Don't even try to read if time's out already.
1861 * This avoids by-passing the timer when repeatedly receiving messages
1862 * that will end up being dropped.
1863 */
Hanno Becker7876d122020-02-05 10:39:31 +00001864 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001865 {
1866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001867 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001868 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001869 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001870 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001871 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001874 timeout = ssl->handshake->retransmit_timeout;
1875 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001876 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001877
Paul Elliott9f352112020-12-09 14:55:45 +00001878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001879
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001880 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001881 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1882 timeout );
1883 else
1884 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001887
1888 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001890 }
1891
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001892 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001895 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001898 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001899 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001902 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001903 }
1904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001908 return( ret );
1909 }
1910
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001911 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001912 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001914 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001916 {
Hanno Becker786300f2020-02-05 10:46:40 +00001917 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001918 {
Hanno Becker786300f2020-02-05 10:46:40 +00001919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1920 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001921 return( ret );
1922 }
1923
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001924 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001927 }
1928
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 if( ret < 0 )
1930 return( ret );
1931
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001932 ssl->in_left = ret;
1933 }
1934 else
1935#endif
1936 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1938 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001939 ssl->in_left, nb_want ) );
1940
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001941 while( ssl->in_left < nb_want )
1942 {
1943 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001944
Hanno Becker7876d122020-02-05 10:39:31 +00001945 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001946 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1947 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001948 {
1949 if( ssl->f_recv_timeout != NULL )
1950 {
1951 ret = ssl->f_recv_timeout( ssl->p_bio,
1952 ssl->in_hdr + ssl->in_left, len,
1953 ssl->conf->read_timeout );
1954 }
1955 else
1956 {
1957 ret = ssl->f_recv( ssl->p_bio,
1958 ssl->in_hdr + ssl->in_left, len );
1959 }
1960 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001961
Paul Elliottd48d5c62021-01-07 14:47:05 +00001962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1963 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001964 ssl->in_left, nb_want ) );
1965 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001966
1967 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001969
1970 if( ret < 0 )
1971 return( ret );
1972
makise-homuraaf9513b2020-08-24 18:26:27 +03001973 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001974 {
Darryl Green11999bb2018-03-13 15:22:58 +00001975 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001976 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001977 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001978 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1979 }
1980
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001981 ssl->in_left += ret;
1982 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 }
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 return( 0 );
1988}
1989
1990/*
1991 * Flush any data not yet written
1992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001994{
Janos Follath865b3eb2019-12-16 11:46:15 +00001995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001996 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002000 if( ssl->f_send == NULL )
2001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002003 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002005 }
2006
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002007 /* Avoid incrementing counter if data is flushed */
2008 if( ssl->out_left == 0 )
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002011 return( 0 );
2012 }
2013
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 while( ssl->out_left > 0 )
2015 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
2017 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01002018 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
Hanno Becker2b1e3542018-08-06 11:19:13 +01002020 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002021 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
2025 if( ret <= 0 )
2026 return( ret );
2027
makise-homuraaf9513b2020-08-24 18:26:27 +03002028 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002029 {
Darryl Green11999bb2018-03-13 15:22:58 +00002030 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002031 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00002032 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002033 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2034 }
2035
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 ssl->out_left -= ret;
2037 }
2038
Hanno Becker2b1e3542018-08-06 11:19:13 +01002039#if defined(MBEDTLS_SSL_PROTO_DTLS)
2040 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002041 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002042 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002043 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002044 else
2045#endif
2046 {
2047 ssl->out_hdr = ssl->out_buf + 8;
2048 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002049 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002052
2053 return( 0 );
2054}
2055
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002056/*
2057 * Functions to handle the DTLS retransmission state machine
2058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002060/*
2061 * Append current handshake message to current outgoing flight
2062 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002065 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2067 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2068 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002069
2070 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002071 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002072 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002075 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002076 }
2077
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002078 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002079 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2081 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002084 }
2085
2086 /* Copy current handshake message with headers */
2087 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2088 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002089 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002090 msg->next = NULL;
2091
2092 /* Append to the current flight */
2093 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002094 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002095 else
2096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002098 while( cur->next != NULL )
2099 cur = cur->next;
2100 cur->next = msg;
2101 }
2102
Hanno Becker3b235902018-08-06 09:54:53 +01002103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002104 return( 0 );
2105}
2106
2107/*
2108 * Free the current flight of handshake messages
2109 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002110void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 mbedtls_ssl_flight_item *cur = flight;
2113 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002114
2115 while( cur != NULL )
2116 {
2117 next = cur->next;
2118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_free( cur->p );
2120 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002121
2122 cur = next;
2123 }
2124}
2125
2126/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002127 * Swap transform_out and out_ctr with the alternative ones
2128 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002129static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002130{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 mbedtls_ssl_transform *tmp_transform;
Jerry Yuae0b2e22021-10-08 15:21:19 +08002132 unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002133
2134 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002137 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002138 }
2139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002141
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002142 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002143 tmp_transform = ssl->transform_out;
2144 ssl->transform_out = ssl->handshake->alt_transform_out;
2145 ssl->handshake->alt_transform_out = tmp_transform;
2146
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002147 /* Swap epoch + sequence_number */
Jerry Yud96a5c22021-09-29 17:46:51 +08002148 memcpy( tmp_out_ctr, ssl->cur_out_ctr, sizeof( tmp_out_ctr ) );
2149 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr,
2150 sizeof( ssl->cur_out_ctr ) );
2151 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr,
2152 sizeof( ssl->handshake->alt_out_ctr ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002153
2154 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002155 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002156
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002157 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002158}
2159
2160/*
2161 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002162 */
2163int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2164{
2165 int ret = 0;
2166
2167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2168
2169 ret = mbedtls_ssl_flight_transmit( ssl );
2170
2171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2172
2173 return( ret );
2174}
2175
2176/*
2177 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002178 *
2179 * Need to remember the current message in case flush_output returns
2180 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002181 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002182 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002183int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002184{
Janos Follath865b3eb2019-12-16 11:46:15 +00002185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002189 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002191
2192 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002193 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002194 ret = ssl_swap_epochs( ssl );
2195 if( ret != 0 )
2196 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002199 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002200
2201 while( ssl->handshake->cur_msg != NULL )
2202 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002203 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002204 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002205
Hanno Beckere1dcb032018-08-17 16:47:58 +01002206 int const is_finished =
2207 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2208 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2209
Hanno Becker04da1892018-08-14 13:22:10 +01002210 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2211 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2212
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002213 /* Swap epochs before sending Finished: we can't do it after
2214 * sending ChangeCipherSpec, in case write returns WANT_READ.
2215 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002216 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002217 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002219 ret = ssl_swap_epochs( ssl );
2220 if( ret != 0 )
2221 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002222 }
2223
Hanno Becker67bc7c32018-08-06 11:33:50 +01002224 ret = ssl_get_remaining_payload_in_datagram( ssl );
2225 if( ret < 0 )
2226 return( ret );
2227 max_frag_len = (size_t) ret;
2228
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002229 /* CCS is copied as is, while HS messages may need fragmentation */
2230 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2231 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002232 if( max_frag_len == 0 )
2233 {
2234 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2235 return( ret );
2236
2237 continue;
2238 }
2239
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002240 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002241 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002242 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002243
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002244 /* Update position inside current message */
2245 ssl->handshake->cur_msg_p += cur->len;
2246 }
2247 else
2248 {
2249 const unsigned char * const p = ssl->handshake->cur_msg_p;
2250 const size_t hs_len = cur->len - 12;
2251 const size_t frag_off = p - ( cur->p + 12 );
2252 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002253 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002254
Hanno Beckere1dcb032018-08-17 16:47:58 +01002255 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002256 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002257 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002258 {
2259 ret = ssl_swap_epochs( ssl );
2260 if( ret != 0 )
2261 return( ret );
2262 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002263
Hanno Becker67bc7c32018-08-06 11:33:50 +01002264 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2265 return( ret );
2266
2267 continue;
2268 }
2269 max_hs_frag_len = max_frag_len - 12;
2270
2271 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2272 max_hs_frag_len : rem_len;
2273
2274 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002275 {
2276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002277 (unsigned) cur_hs_frag_len,
2278 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002279 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002280
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002281 /* Messages are stored with handshake headers as if not fragmented,
2282 * copy beginning of headers then fill fragmentation fields.
2283 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2284 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002285
Joe Subbiani5ecac212021-06-24 13:00:03 +01002286 ssl->out_msg[6] = MBEDTLS_BYTE_2( frag_off );
2287 ssl->out_msg[7] = MBEDTLS_BYTE_1( frag_off );
2288 ssl->out_msg[8] = MBEDTLS_BYTE_0( frag_off );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002289
Joe Subbiani5ecac212021-06-24 13:00:03 +01002290 ssl->out_msg[ 9] = MBEDTLS_BYTE_2( cur_hs_frag_len );
2291 ssl->out_msg[10] = MBEDTLS_BYTE_1( cur_hs_frag_len );
2292 ssl->out_msg[11] = MBEDTLS_BYTE_0( cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002293
2294 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2295
Hanno Becker3f7b9732018-08-28 09:53:25 +01002296 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002297 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2298 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002299 ssl->out_msgtype = cur->type;
2300
2301 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002302 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002303 }
2304
2305 /* If done with the current message move to the next one if any */
2306 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2307 {
2308 if( cur->next != NULL )
2309 {
2310 ssl->handshake->cur_msg = cur->next;
2311 ssl->handshake->cur_msg_p = cur->next->p + 12;
2312 }
2313 else
2314 {
2315 ssl->handshake->cur_msg = NULL;
2316 ssl->handshake->cur_msg_p = NULL;
2317 }
2318 }
2319
2320 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002321 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002324 return( ret );
2325 }
2326 }
2327
Hanno Becker67bc7c32018-08-06 11:33:50 +01002328 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2329 return( ret );
2330
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002331 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2333 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002334 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002337 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002338 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002339
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002341
2342 return( 0 );
2343}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002344
2345/*
2346 * To be called when the last message of an incoming flight is received.
2347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002349{
2350 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002351 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002352 ssl->handshake->flight = NULL;
2353 ssl->handshake->cur_msg = NULL;
2354
2355 /* The next incoming flight will start with this msg_seq */
2356 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2357
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002358 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002359 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002360
Hanno Becker0271f962018-08-16 13:23:47 +01002361 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002362 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002363
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002364 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002365 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2368 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002371 }
2372 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002374}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002375
2376/*
2377 * To be called when the last message of an outgoing flight is send.
2378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002379void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002380{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002381 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002382 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2385 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002386 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002388 }
2389 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002391}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002393
Paul Bakker5121ce52009-01-03 21:22:43 +00002394/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002395 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002397
2398/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002399 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002400 *
2401 * - fill in handshake headers
2402 * - update handshake checksum
2403 * - DTLS: save message for resending
2404 * - then pass to the record layer
2405 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002406 * DTLS: except for HelloRequest, messages are only queued, and will only be
2407 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002408 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002409 * Inputs:
2410 * - ssl->out_msglen: 4 + actual handshake message len
2411 * (4 is the size of handshake headers for TLS)
2412 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2413 * - ssl->out_msg + 4: the handshake message body
2414 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002415 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002416 * - ssl->out_msglen: the length of the record contents
2417 * (including handshake headers but excluding record headers)
2418 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002419 */
Hanno Beckerf3cce8b2021-08-07 14:29:49 +01002420int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl,
2421 int update_checksum )
Paul Bakker5121ce52009-01-03 21:22:43 +00002422{
Janos Follath865b3eb2019-12-16 11:46:15 +00002423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002424 const size_t hs_len = ssl->out_msglen - 4;
2425 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2428
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002429 /*
2430 * Sanity checks
2431 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002432 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002433 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2434 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002439 /* Whenever we send anything different from a
2440 * HelloRequest we should be in a handshake - double check. */
2441 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2442 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002443 ssl->handshake == NULL )
2444 {
2445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2447 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002450 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002451 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002453 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2455 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002456 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002457#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002458
Hanno Beckerb50a2532018-08-06 11:52:54 +01002459 /* Double-check that we did not exceed the bounds
2460 * of the outgoing record buffer.
2461 * This should never fail as the various message
2462 * writing functions must obey the bounds of the
2463 * outgoing record buffer, but better be safe.
2464 *
2465 * Note: We deliberately do not check for the MTU or MFL here.
2466 */
2467 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2468 {
2469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002470 "size %" MBEDTLS_PRINTF_SIZET
2471 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002472 ssl->out_msglen,
2473 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002474 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2475 }
2476
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002477 /*
2478 * Fill handshake headers
2479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002480 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002481 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01002482 ssl->out_msg[1] = MBEDTLS_BYTE_2( hs_len );
2483 ssl->out_msg[2] = MBEDTLS_BYTE_1( hs_len );
2484 ssl->out_msg[3] = MBEDTLS_BYTE_0( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002486 /*
2487 * DTLS has additional fields in the Handshake layer,
2488 * between the length field and the actual payload:
2489 * uint16 message_seq;
2490 * uint24 fragment_offset;
2491 * uint24 fragment_length;
2492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002494 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002495 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002496 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002497 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002498 {
2499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002500 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002501 hs_len,
2502 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002503 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2504 }
2505
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002506 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002507 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002508
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002509 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002510 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002511 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002512 MBEDTLS_PUT_UINT16_BE( ssl->handshake->out_msg_seq, ssl->out_msg, 4 );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002513 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002514 }
2515 else
2516 {
2517 ssl->out_msg[4] = 0;
2518 ssl->out_msg[5] = 0;
2519 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002520
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002521 /* Handshake hashes are computed without fragmentation,
2522 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002523 memset( ssl->out_msg + 6, 0x00, 3 );
2524 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002527
Hanno Becker0207e532018-08-28 10:28:28 +01002528 /* Update running hashes of handshake messages seen */
Hanno Beckerf3cce8b2021-08-07 14:29:49 +01002529 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0 )
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002530 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 }
2532
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002533 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002536 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2537 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002538 {
2539 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002542 return( ret );
2543 }
2544 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002545 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002546#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002547 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002548 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002549 {
2550 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2551 return( ret );
2552 }
2553 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002554
2555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2556
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002557 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002558}
2559
2560/*
2561 * Record layer functions
2562 */
2563
2564/*
2565 * Write current record.
2566 *
2567 * Uses:
2568 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2569 * - ssl->out_msglen: length of the record content (excl headers)
2570 * - ssl->out_msg: record content
2571 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002572int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002573{
2574 int ret, done = 0;
2575 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002576 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002577
2578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002579
Paul Bakker05ef8352012-05-08 09:17:57 +00002580 if( !done )
2581 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002582 unsigned i;
2583 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002584#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2585 size_t out_buf_len = ssl->out_buf_len;
2586#else
2587 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2588#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002589 /* Skip writing the record content type to after the encryption,
2590 * as it may change when using the CID extension. */
Jerry Yu47413c22021-10-29 17:19:41 +08002591 int minor_ver = ssl->minor_ver;
Ronald Cron6f135e12021-12-08 16:57:54 +01002592#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ca80f72021-11-08 10:30:54 +08002593 /* TLS 1.3 still uses the TLS 1.2 version identifier
2594 * for backwards compatibility. */
Jerry Yu47413c22021-10-29 17:19:41 +08002595 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
2596 minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Ronald Cron6f135e12021-12-08 16:57:54 +01002597#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu47413c22021-10-29 17:19:41 +08002598 mbedtls_ssl_write_version( ssl->major_ver, minor_ver,
2599 ssl->conf->transport, ssl->out_hdr + 1 );
Hanno Becker6430faf2019-05-08 11:57:13 +01002600
Jerry Yuae0b2e22021-10-08 15:21:19 +08002601 memcpy( ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Joe Subbiani6dd73642021-07-19 11:56:54 +01002602 MBEDTLS_PUT_UINT16_BE( len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002603
Paul Bakker48916f92012-09-16 19:57:18 +00002604 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002605 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002606 mbedtls_record rec;
2607
2608 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002609 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002610 rec.data_len = ssl->out_msglen;
2611 rec.data_offset = ssl->out_msg - rec.buf;
2612
Jerry Yud96a5c22021-09-29 17:46:51 +08002613 memcpy( &rec.ctr[0], ssl->out_ctr, sizeof( rec.ctr ) );
Jerry Yu47413c22021-10-29 17:19:41 +08002614 mbedtls_ssl_write_version( ssl->major_ver, minor_ver,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002615 ssl->conf->transport, rec.ver );
2616 rec.type = ssl->out_msgtype;
2617
Hanno Beckera0e20d02019-05-15 14:03:01 +01002618#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002619 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002620 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002621#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002622
Hanno Beckera18d1322018-01-03 14:27:32 +00002623 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002624 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002627 return( ret );
2628 }
2629
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002630 if( rec.data_offset != 0 )
2631 {
2632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2634 }
2635
Hanno Becker6430faf2019-05-08 11:57:13 +01002636 /* Update the record content type and CID. */
2637 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002639 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002640#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002641 ssl->out_msglen = len = rec.data_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002642 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->out_len, 0 );
Paul Bakker05ef8352012-05-08 09:17:57 +00002643 }
2644
Hanno Becker5903de42019-05-03 14:46:38 +01002645 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002646
2647#if defined(MBEDTLS_SSL_PROTO_DTLS)
2648 /* In case of DTLS, double-check that we don't exceed
2649 * the remaining space in the datagram. */
2650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2651 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002652 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002653 if( ret < 0 )
2654 return( ret );
2655
2656 if( protected_record_size > (size_t) ret )
2657 {
2658 /* Should never happen */
2659 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2660 }
2661 }
2662#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002663
Hanno Becker6430faf2019-05-08 11:57:13 +01002664 /* Now write the potentially updated record content type. */
2665 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2666
Paul Elliott9f352112020-12-09 14:55:45 +00002667 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002668 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002669 ssl->out_hdr[0], ssl->out_hdr[1],
2670 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002673 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002674
2675 ssl->out_left += protected_record_size;
2676 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002677 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002678
Hanno Beckerdd772292020-02-05 10:38:31 +00002679 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002680 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2681 break;
2682
2683 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002684 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002685 {
2686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2687 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2688 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 }
2690
Hanno Becker67bc7c32018-08-06 11:33:50 +01002691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002692 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2693 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002694 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002695 size_t remaining;
2696 ret = ssl_get_remaining_payload_in_datagram( ssl );
2697 if( ret < 0 )
2698 {
2699 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2700 ret );
2701 return( ret );
2702 }
2703
2704 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002705 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002706 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002707 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002708 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002709 else
2710 {
Hanno Becker513815a2018-08-20 11:56:09 +01002711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002712 }
2713 }
2714#endif /* MBEDTLS_SSL_PROTO_DTLS */
2715
2716 if( ( flush == SSL_FORCE_FLUSH ) &&
2717 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 return( ret );
2721 }
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724
2725 return( 0 );
2726}
2727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002729
2730static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2731{
2732 if( ssl->in_msglen < ssl->in_hslen ||
2733 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2734 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2735 {
2736 return( 1 );
2737 }
2738 return( 0 );
2739}
Hanno Becker44650b72018-08-16 12:51:11 +01002740
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002741static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002742{
2743 return( ( ssl->in_msg[9] << 16 ) |
2744 ( ssl->in_msg[10] << 8 ) |
2745 ssl->in_msg[11] );
2746}
2747
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002748static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002749{
2750 return( ( ssl->in_msg[6] << 16 ) |
2751 ( ssl->in_msg[7] << 8 ) |
2752 ssl->in_msg[8] );
2753}
2754
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002755static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002756{
2757 uint32_t msg_len, frag_off, frag_len;
2758
2759 msg_len = ssl_get_hs_total_len( ssl );
2760 frag_off = ssl_get_hs_frag_off( ssl );
2761 frag_len = ssl_get_hs_frag_len( ssl );
2762
2763 if( frag_off > msg_len )
2764 return( -1 );
2765
2766 if( frag_len > msg_len - frag_off )
2767 return( -1 );
2768
2769 if( frag_len + 12 > ssl->in_msglen )
2770 return( -1 );
2771
2772 return( 0 );
2773}
2774
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002775/*
2776 * Mark bits in bitmask (used for DTLS HS reassembly)
2777 */
2778static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2779{
2780 unsigned int start_bits, end_bits;
2781
2782 start_bits = 8 - ( offset % 8 );
2783 if( start_bits != 8 )
2784 {
2785 size_t first_byte_idx = offset / 8;
2786
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002787 /* Special case */
2788 if( len <= start_bits )
2789 {
2790 for( ; len != 0; len-- )
2791 mask[first_byte_idx] |= 1 << ( start_bits - len );
2792
2793 /* Avoid potential issues with offset or len becoming invalid */
2794 return;
2795 }
2796
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002797 offset += start_bits; /* Now offset % 8 == 0 */
2798 len -= start_bits;
2799
2800 for( ; start_bits != 0; start_bits-- )
2801 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2802 }
2803
2804 end_bits = len % 8;
2805 if( end_bits != 0 )
2806 {
2807 size_t last_byte_idx = ( offset + len ) / 8;
2808
2809 len -= end_bits; /* Now len % 8 == 0 */
2810
2811 for( ; end_bits != 0; end_bits-- )
2812 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2813 }
2814
2815 memset( mask + offset / 8, 0xFF, len / 8 );
2816}
2817
2818/*
2819 * Check that bitmask is full
2820 */
2821static int ssl_bitmask_check( unsigned char *mask, size_t len )
2822{
2823 size_t i;
2824
2825 for( i = 0; i < len / 8; i++ )
2826 if( mask[i] != 0xFF )
2827 return( -1 );
2828
2829 for( i = 0; i < len % 8; i++ )
2830 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2831 return( -1 );
2832
2833 return( 0 );
2834}
2835
Hanno Becker56e205e2018-08-16 09:06:12 +01002836/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002837static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002838 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002839{
Hanno Becker56e205e2018-08-16 09:06:12 +01002840 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002841
Hanno Becker56e205e2018-08-16 09:06:12 +01002842 alloc_len = 12; /* Handshake header */
2843 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002844
Hanno Beckerd07df862018-08-16 09:14:58 +01002845 if( add_bitmap )
2846 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002847
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002848 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002849}
Hanno Becker56e205e2018-08-16 09:06:12 +01002850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002851#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002852
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002853static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002854{
2855 return( ( ssl->in_msg[1] << 16 ) |
2856 ( ssl->in_msg[2] << 8 ) |
2857 ssl->in_msg[3] );
2858}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002859
Simon Butcher99000142016-10-13 17:21:01 +01002860int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002861{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002862 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002863 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002865 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002867 }
2868
Hanno Becker12555c62018-08-16 12:47:53 +01002869 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002872 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002873 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002877 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002879 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002880
Hanno Becker44650b72018-08-16 12:51:11 +01002881 if( ssl_check_hs_header( ssl ) != 0 )
2882 {
2883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2884 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2885 }
2886
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002887 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002888 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2889 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2890 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2891 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002892 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002893 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2894 {
2895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2896 recv_msg_seq,
2897 ssl->handshake->in_msg_seq ) );
2898 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2899 }
2900
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002901 /* Retransmit only on last message from previous flight, to avoid
2902 * too many retransmissions.
2903 * Besides, No sane server ever retransmits HelloVerifyRequest */
2904 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002905 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002908 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002909 recv_msg_seq,
2910 ssl->handshake->in_flight_start_seq ) );
2911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002915 return( ret );
2916 }
2917 }
2918 else
2919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002921 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002922 recv_msg_seq,
2923 ssl->handshake->in_msg_seq ) );
2924 }
2925
Hanno Becker90333da2017-10-10 11:27:13 +01002926 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002927 }
2928 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002929
Hanno Becker6d97ef52018-08-16 13:09:04 +01002930 /* Message reassembly is handled alongside buffering of future
2931 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002932 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002933 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002934 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002937 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002938 }
2939 }
2940 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002942 /* With TLS we don't handle fragmentation (for now) */
2943 if( ssl->in_msglen < ssl->in_hslen )
2944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2946 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002947 }
2948
Simon Butcher99000142016-10-13 17:21:01 +01002949 return( 0 );
2950}
2951
2952void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2953{
Hanno Becker0271f962018-08-16 13:23:47 +01002954 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002955
Hanno Becker0271f962018-08-16 13:23:47 +01002956 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002957 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002958 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002959 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002960
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002961 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002964 ssl->handshake != NULL )
2965 {
Hanno Becker0271f962018-08-16 13:23:47 +01002966 unsigned offset;
2967 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002968
Hanno Becker0271f962018-08-16 13:23:47 +01002969 /* Increment handshake sequence number */
2970 hs->in_msg_seq++;
2971
2972 /*
2973 * Clear up handshake buffering and reassembly structure.
2974 */
2975
2976 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002977 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002978
2979 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002980 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2981 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002982 offset++, hs_buf++ )
2983 {
2984 *hs_buf = *(hs_buf + 1);
2985 }
2986
2987 /* Create a fresh last entry */
2988 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002989 }
2990#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002991}
2992
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002993/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002994 * DTLS anti-replay: RFC 6347 4.1.2.6
2995 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002996 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2997 * Bit n is set iff record number in_window_top - n has been seen.
2998 *
2999 * Usually, in_window_top is the last record number seen and the lsb of
3000 * in_window is set. The only exception is the initial state (record number 0
3001 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003004void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003005{
3006 ssl->in_window_top = 0;
3007 ssl->in_window = 0;
3008}
3009
3010static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3011{
3012 return( ( (uint64_t) buf[0] << 40 ) |
3013 ( (uint64_t) buf[1] << 32 ) |
3014 ( (uint64_t) buf[2] << 24 ) |
3015 ( (uint64_t) buf[3] << 16 ) |
3016 ( (uint64_t) buf[4] << 8 ) |
3017 ( (uint64_t) buf[5] ) );
3018}
3019
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003020static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3021{
Janos Follath865b3eb2019-12-16 11:46:15 +00003022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003023 unsigned char *original_in_ctr;
3024
3025 // save original in_ctr
3026 original_in_ctr = ssl->in_ctr;
3027
3028 // use counter from record
3029 ssl->in_ctr = record_in_ctr;
3030
3031 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3032
3033 // restore the counter
3034 ssl->in_ctr = original_in_ctr;
3035
3036 return ret;
3037}
3038
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003039/*
3040 * Return 0 if sequence number is acceptable, -1 otherwise
3041 */
Hanno Becker0183d692019-07-12 08:50:37 +01003042int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003043{
3044 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3045 uint64_t bit;
3046
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003047 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003048 return( 0 );
3049
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003050 if( rec_seqnum > ssl->in_window_top )
3051 return( 0 );
3052
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003053 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003054
3055 if( bit >= 64 )
3056 return( -1 );
3057
3058 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3059 return( -1 );
3060
3061 return( 0 );
3062}
3063
3064/*
3065 * Update replay window on new validated record
3066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003068{
3069 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3070
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003071 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003072 return;
3073
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003074 if( rec_seqnum > ssl->in_window_top )
3075 {
3076 /* Update window_top and the contents of the window */
3077 uint64_t shift = rec_seqnum - ssl->in_window_top;
3078
3079 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003080 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003081 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003082 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003083 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003084 ssl->in_window |= 1;
3085 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003086
3087 ssl->in_window_top = rec_seqnum;
3088 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003089 else
3090 {
3091 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003092 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003093
3094 if( bit < 64 ) /* Always true, but be extra sure */
3095 ssl->in_window |= (uint64_t) 1 << bit;
3096 }
3097}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003099
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003100#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003101/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003102 * Without any SSL context, check if a datagram looks like a ClientHello with
3103 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003104 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003105 *
3106 * - if cookie is valid, return 0
3107 * - if ClientHello looks superficially valid but cookie is not,
3108 * fill obuf and set olen, then
3109 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3110 * - otherwise return a specific error code
3111 */
3112static int ssl_check_dtls_clihlo_cookie(
3113 mbedtls_ssl_cookie_write_t *f_cookie_write,
3114 mbedtls_ssl_cookie_check_t *f_cookie_check,
3115 void *p_cookie,
3116 const unsigned char *cli_id, size_t cli_id_len,
3117 const unsigned char *in, size_t in_len,
3118 unsigned char *obuf, size_t buf_len, size_t *olen )
3119{
3120 size_t sid_len, cookie_len;
3121 unsigned char *p;
3122
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003123 /*
3124 * Structure of ClientHello with record and handshake headers,
3125 * and expected values. We don't need to check a lot, more checks will be
3126 * done when actually parsing the ClientHello - skipping those checks
3127 * avoids code duplication and does not make cookie forging any easier.
3128 *
3129 * 0-0 ContentType type; copied, must be handshake
3130 * 1-2 ProtocolVersion version; copied
3131 * 3-4 uint16 epoch; copied, must be 0
3132 * 5-10 uint48 sequence_number; copied
3133 * 11-12 uint16 length; (ignored)
3134 *
3135 * 13-13 HandshakeType msg_type; (ignored)
3136 * 14-16 uint24 length; (ignored)
3137 * 17-18 uint16 message_seq; copied
3138 * 19-21 uint24 fragment_offset; copied, must be 0
3139 * 22-24 uint24 fragment_length; (ignored)
3140 *
3141 * 25-26 ProtocolVersion client_version; (ignored)
3142 * 27-58 Random random; (ignored)
3143 * 59-xx SessionID session_id; 1 byte len + sid_len content
3144 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3145 * ...
3146 *
3147 * Minimum length is 61 bytes.
3148 */
3149 if( in_len < 61 ||
3150 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3151 in[3] != 0 || in[4] != 0 ||
3152 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3153 {
Hanno Becker90d59dd2021-06-24 11:17:13 +01003154 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003155 }
3156
3157 sid_len = in[59];
3158 if( sid_len > in_len - 61 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01003159 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003160
3161 cookie_len = in[60 + sid_len];
3162 if( cookie_len > in_len - 60 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01003163 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003164
3165 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3166 cli_id, cli_id_len ) == 0 )
3167 {
3168 /* Valid cookie */
3169 return( 0 );
3170 }
3171
3172 /*
3173 * If we get here, we've got an invalid cookie, let's prepare HVR.
3174 *
3175 * 0-0 ContentType type; copied
3176 * 1-2 ProtocolVersion version; copied
3177 * 3-4 uint16 epoch; copied
3178 * 5-10 uint48 sequence_number; copied
3179 * 11-12 uint16 length; olen - 13
3180 *
3181 * 13-13 HandshakeType msg_type; hello_verify_request
3182 * 14-16 uint24 length; olen - 25
3183 * 17-18 uint16 message_seq; copied
3184 * 19-21 uint24 fragment_offset; copied
3185 * 22-24 uint24 fragment_length; olen - 25
3186 *
3187 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3188 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3189 *
3190 * Minimum length is 28.
3191 */
3192 if( buf_len < 28 )
3193 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3194
3195 /* Copy most fields and adapt others */
3196 memcpy( obuf, in, 25 );
3197 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3198 obuf[25] = 0xfe;
3199 obuf[26] = 0xff;
3200
3201 /* Generate and write actual cookie */
3202 p = obuf + 28;
3203 if( f_cookie_write( p_cookie,
3204 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3205 {
3206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3207 }
3208
3209 *olen = p - obuf;
3210
3211 /* Go back and fill length fields */
3212 obuf[27] = (unsigned char)( *olen - 28 );
3213
Joe Subbianifbeb6922021-07-16 14:27:50 +01003214 obuf[14] = obuf[22] = MBEDTLS_BYTE_2( *olen - 25 );
3215 obuf[15] = obuf[23] = MBEDTLS_BYTE_1( *olen - 25 );
3216 obuf[16] = obuf[24] = MBEDTLS_BYTE_0( *olen - 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003217
Joe Subbiani6dd73642021-07-19 11:56:54 +01003218 MBEDTLS_PUT_UINT16_BE( *olen - 13, obuf, 11 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003219
3220 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3221}
3222
3223/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003224 * Handle possible client reconnect with the same UDP quadruplet
3225 * (RFC 6347 Section 4.2.8).
3226 *
3227 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3228 * that looks like a ClientHello.
3229 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003230 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003231 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003232 * - if the input looks like a ClientHello with a valid cookie,
3233 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003234 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003235 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003236 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003237 * This function is called (through ssl_check_client_reconnect()) when an
3238 * unexpected record is found in ssl_get_next_record(), which will discard the
3239 * record if we return 0, and bubble up the return value otherwise (this
3240 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3241 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003242 */
3243static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3244{
Janos Follath865b3eb2019-12-16 11:46:15 +00003245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003246 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003247
Hanno Becker2fddd372019-07-10 14:37:41 +01003248 if( ssl->conf->f_cookie_write == NULL ||
3249 ssl->conf->f_cookie_check == NULL )
3250 {
3251 /* If we can't use cookies to verify reachability of the peer,
3252 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3254 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003255 return( 0 );
3256 }
3257
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003258 ret = ssl_check_dtls_clihlo_cookie(
3259 ssl->conf->f_cookie_write,
3260 ssl->conf->f_cookie_check,
3261 ssl->conf->p_cookie,
3262 ssl->cli_id, ssl->cli_id_len,
3263 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003264 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003265
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003266 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3267
3268 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003269 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003270 int send_ret;
3271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3272 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3273 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003274 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003275 * If the error is permanent we'll catch it later,
3276 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003277 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3278 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3279 (void) send_ret;
3280
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003281 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003282 }
3283
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003284 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003285 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003287 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003288 {
3289 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3290 return( ret );
3291 }
3292
3293 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003294 }
3295
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003296 return( ret );
3297}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003298#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003299
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003300static int ssl_check_record_type( uint8_t record_type )
3301{
3302 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3303 record_type != MBEDTLS_SSL_MSG_ALERT &&
3304 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3305 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3306 {
3307 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3308 }
3309
3310 return( 0 );
3311}
3312
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003313/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003314 * ContentType type;
3315 * ProtocolVersion version;
3316 * uint16 epoch; // DTLS only
3317 * uint48 sequence_number; // DTLS only
3318 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003319 *
3320 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003321 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003322 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3323 *
3324 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003325 * 1. proceed with the record if this function returns 0
3326 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3327 * 3. return CLIENT_RECONNECT if this function return that value
3328 * 4. drop the whole datagram if this function returns anything else.
3329 * Point 2 is needed when the peer is resending, and we have already received
3330 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003331 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003332static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003333 unsigned char *buf,
3334 size_t len,
3335 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003336{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003337 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Hanno Beckere5e7e782019-07-11 12:29:35 +01003339 size_t const rec_hdr_type_offset = 0;
3340 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003341
Hanno Beckere5e7e782019-07-11 12:29:35 +01003342 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3343 rec_hdr_type_len;
3344 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
Hanno Beckere5e7e782019-07-11 12:29:35 +01003346 size_t const rec_hdr_ctr_len = 8;
3347#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003348 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003349 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3350 rec_hdr_version_len;
3351
Hanno Beckera0e20d02019-05-15 14:03:01 +01003352#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003353 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3354 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003355 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003356#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3357#endif /* MBEDTLS_SSL_PROTO_DTLS */
3358
3359 size_t rec_hdr_len_offset; /* To be determined */
3360 size_t const rec_hdr_len_len = 2;
3361
3362 /*
3363 * Check minimum lengths for record header.
3364 */
3365
3366#if defined(MBEDTLS_SSL_PROTO_DTLS)
3367 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3368 {
3369 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3370 }
3371 else
3372#endif /* MBEDTLS_SSL_PROTO_DTLS */
3373 {
3374 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3375 }
3376
3377 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3378 {
3379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3380 (unsigned) len,
3381 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3382 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3383 }
3384
3385 /*
3386 * Parse and validate record content type
3387 */
3388
3389 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003390
3391 /* Check record content type */
3392#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3393 rec->cid_len = 0;
3394
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003395 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003396 ssl->conf->cid_len != 0 &&
3397 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003398 {
3399 /* Shift pointers to account for record header including CID
3400 * struct {
3401 * ContentType special_type = tls12_cid;
3402 * ProtocolVersion version;
3403 * uint16 epoch;
3404 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003405 * opaque cid[cid_length]; // Additional field compared to
3406 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003407 * uint16 length;
3408 * opaque enc_content[DTLSCiphertext.length];
3409 * } DTLSCiphertext;
3410 */
3411
3412 /* So far, we only support static CID lengths
3413 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003414 rec_hdr_cid_len = ssl->conf->cid_len;
3415 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003416
Hanno Beckere5e7e782019-07-11 12:29:35 +01003417 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003418 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3420 (unsigned) len,
3421 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003422 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003423 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003424
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003425 /* configured CID len is guaranteed at most 255, see
3426 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3427 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003428 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003429 }
3430 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003432 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003433 if( ssl_check_record_type( rec->type ) )
3434 {
Hanno Becker54229812019-07-12 14:40:00 +01003435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3436 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003437 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3438 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003439 }
3440
Hanno Beckere5e7e782019-07-11 12:29:35 +01003441 /*
3442 * Parse and validate record version
3443 */
3444
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003445 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3446 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003447 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3448 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003449 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003450
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003451 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3454 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 }
3456
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003457 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3460 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 }
3462
Hanno Beckere5e7e782019-07-11 12:29:35 +01003463 /*
3464 * Parse/Copy record sequence number.
3465 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003466
Hanno Beckere5e7e782019-07-11 12:29:35 +01003467#if defined(MBEDTLS_SSL_PROTO_DTLS)
3468 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003469 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003470 /* Copy explicit record sequence number from input buffer. */
3471 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3472 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003473 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003474 else
3475#endif /* MBEDTLS_SSL_PROTO_DTLS */
3476 {
3477 /* Copy implicit record sequence number from SSL context structure. */
3478 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3479 }
Paul Bakker40e46942009-01-03 21:51:57 +00003480
Hanno Beckere5e7e782019-07-11 12:29:35 +01003481 /*
3482 * Parse record length.
3483 */
3484
Hanno Beckere5e7e782019-07-11 12:29:35 +01003485 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003486 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3487 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003488 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
Paul Elliott9f352112020-12-09 14:55:45 +00003490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003491 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003492 rec->type,
3493 major_ver, minor_ver, rec->data_len ) );
3494
3495 rec->buf = buf;
3496 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003497
Hanno Beckerd417cc92019-07-26 08:20:27 +01003498 if( rec->data_len == 0 )
3499 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003500
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003501 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003502 * DTLS-related tests.
3503 * Check epoch before checking length constraint because
3504 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3505 * message gets duplicated before the corresponding Finished message,
3506 * the second ChangeCipherSpec should be discarded because it belongs
3507 * to an old epoch, but not because its length is shorter than
3508 * the minimum record length for packets using the new record transform.
3509 * Note that these two kinds of failures are handled differently,
3510 * as an unexpected record is silently skipped but an invalid
3511 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003512 */
3513#if defined(MBEDTLS_SSL_PROTO_DTLS)
3514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3515 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003516 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003517
Hanno Becker955a5c92019-07-10 17:12:07 +01003518 /* Check that the datagram is large enough to contain a record
3519 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003520 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003521 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3523 (unsigned) len,
3524 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3526 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003527
Hanno Becker37cfe732019-07-10 17:20:01 +01003528 /* Records from other, non-matching epochs are silently discarded.
3529 * (The case of same-port Client reconnects must be considered in
3530 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003531 if( rec_epoch != ssl->in_epoch )
3532 {
3533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003534 "expected %u, received %lu",
3535 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003536
Hanno Becker552f7472019-07-19 10:59:12 +01003537 /* Records from the next epoch are considered for buffering
3538 * (concretely: early Finished messages). */
3539 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003540 {
Hanno Becker552f7472019-07-19 10:59:12 +01003541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3542 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003543 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003544
Hanno Becker2fddd372019-07-10 14:37:41 +01003545 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003546 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003547#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003548 /* For records from the correct epoch, check whether their
3549 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003550 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3551 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003552 {
3553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3554 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3555 }
3556#endif
3557 }
3558#endif /* MBEDTLS_SSL_PROTO_DTLS */
3559
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003560 return( 0 );
3561}
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
Paul Bakker5121ce52009-01-03 21:22:43 +00003563
Hanno Becker2fddd372019-07-10 14:37:41 +01003564#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3565static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3566{
3567 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3568
3569 /*
3570 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3571 * access the first byte of record content (handshake type), as we
3572 * have an active transform (possibly iv_len != 0), so use the
3573 * fact that the record header len is 13 instead.
3574 */
3575 if( rec_epoch == 0 &&
3576 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3577 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3578 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3579 ssl->in_left > 13 &&
3580 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3581 {
3582 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3583 "from the same port" ) );
3584 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 }
3586
3587 return( 0 );
3588}
Hanno Becker2fddd372019-07-10 14:37:41 +01003589#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003591/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003592 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003593 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003594static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3595 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003596{
3597 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003599 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003600 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601
Ronald Cron7e38cba2021-11-24 12:43:39 +01003602 /*
3603 * In TLS 1.3, always treat ChangeCipherSpec records
3604 * as unencrypted. The only thing we do with them is
3605 * check the length and content and ignore them.
3606 */
Ronald Cron6f135e12021-12-08 16:57:54 +01003607#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron7e38cba2021-11-24 12:43:39 +01003608 if( ssl->transform_in != NULL &&
3609 ssl->transform_in->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3610 {
3611 if( rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3612 done = 1;
3613 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003614#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron7e38cba2021-11-24 12:43:39 +01003615
Paul Bakker48916f92012-09-16 19:57:18 +00003616 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003618 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003619
Hanno Beckera18d1322018-01-03 14:27:32 +00003620 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003621 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003623 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003624
Hanno Beckera0e20d02019-05-15 14:03:01 +01003625#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003626 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3627 ssl->conf->ignore_unexpected_cid
3628 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3629 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003631 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003632 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003633#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003634
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 return( ret );
3636 }
3637
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003638 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003639 {
3640 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003641 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003642 }
3643
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003644 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003645 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003646
Hanno Beckera0e20d02019-05-15 14:03:01 +01003647#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003648 /* We have already checked the record content type
3649 * in ssl_parse_record_header(), failing or silently
3650 * dropping the record in the case of an unknown type.
3651 *
3652 * Since with the use of CIDs, the record content type
3653 * might change during decryption, re-check the record
3654 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003655 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003656 {
3657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3658 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3659 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003660#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003661
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003662 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003663 {
3664#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3665 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003666 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003667 {
3668 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3671 }
3672#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3673
3674 ssl->nb_zero++;
3675
3676 /*
3677 * Three or more empty messages may be a DoS attack
3678 * (excessive CPU consumption).
3679 */
3680 if( ssl->nb_zero > 3 )
3681 {
3682 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003683 "messages, possible DoS attack" ) );
3684 /* Treat the records as if they were not properly authenticated,
3685 * thereby failing the connection if we see more than allowed
3686 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003687 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3688 }
3689 }
3690 else
3691 ssl->nb_zero = 0;
3692
3693#if defined(MBEDTLS_SSL_PROTO_DTLS)
3694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3695 {
3696 ; /* in_ctr read from peer, not maintained internally */
3697 }
3698 else
3699#endif
3700 {
3701 unsigned i;
Jerry Yuae0b2e22021-10-08 15:21:19 +08003702 for( i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3703 i > mbedtls_ssl_ep_len( ssl ); i-- )
3704 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003705 if( ++ssl->in_ctr[i - 1] != 0 )
3706 break;
Jerry Yuae0b2e22021-10-08 15:21:19 +08003707 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003708
3709 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003710 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003711 {
3712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3713 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3714 }
3715 }
3716
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 }
3718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003720 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003723 }
3724#endif
3725
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003726 /* Check actual (decrypted) record content length against
3727 * configured maximum. */
3728 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3729 {
3730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3731 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3732 }
3733
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003734 return( 0 );
3735}
3736
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003737/*
3738 * Read a record.
3739 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003740 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3741 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3742 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003743 */
Hanno Becker1097b342018-08-15 14:09:41 +01003744
3745/* Helper functions for mbedtls_ssl_read_record(). */
3746static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003747static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3748static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003749
Hanno Becker327c93b2018-08-15 13:56:18 +01003750int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003751 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003752{
Janos Follath865b3eb2019-12-16 11:46:15 +00003753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003756
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003757 if( ssl->keep_current_message == 0 )
3758 {
3759 do {
Simon Butcher99000142016-10-13 17:21:01 +01003760
Hanno Becker26994592018-08-15 14:14:59 +01003761 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003762 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003763 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003764
Hanno Beckere74d5562018-08-15 14:26:08 +01003765 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003766 {
Hanno Becker40f50842018-08-15 14:48:01 +01003767#if defined(MBEDTLS_SSL_PROTO_DTLS)
3768 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003769
Hanno Becker40f50842018-08-15 14:48:01 +01003770 /* We only check for buffered messages if the
3771 * current datagram is fully consumed. */
3772 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003773 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003774 {
Hanno Becker40f50842018-08-15 14:48:01 +01003775 if( ssl_load_buffered_message( ssl ) == 0 )
3776 have_buffered = 1;
3777 }
3778
3779 if( have_buffered == 0 )
3780#endif /* MBEDTLS_SSL_PROTO_DTLS */
3781 {
3782 ret = ssl_get_next_record( ssl );
3783 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3784 continue;
3785
3786 if( ret != 0 )
3787 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003788 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003789 return( ret );
3790 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003791 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003792 }
3793
3794 ret = mbedtls_ssl_handle_message_type( ssl );
3795
Hanno Becker40f50842018-08-15 14:48:01 +01003796#if defined(MBEDTLS_SSL_PROTO_DTLS)
3797 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3798 {
3799 /* Buffer future message */
3800 ret = ssl_buffer_message( ssl );
3801 if( ret != 0 )
3802 return( ret );
3803
3804 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3805 }
3806#endif /* MBEDTLS_SSL_PROTO_DTLS */
3807
Hanno Becker90333da2017-10-10 11:27:13 +01003808 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3809 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003810
3811 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003812 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003813 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003814 return( ret );
3815 }
3816
Hanno Becker327c93b2018-08-15 13:56:18 +01003817 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003818 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003819 {
3820 mbedtls_ssl_update_handshake_status( ssl );
3821 }
Simon Butcher99000142016-10-13 17:21:01 +01003822 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003823 else
Simon Butcher99000142016-10-13 17:21:01 +01003824 {
Hanno Becker02f59072018-08-15 14:00:24 +01003825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003826 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003827 }
3828
3829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3830
3831 return( 0 );
3832}
3833
Hanno Becker40f50842018-08-15 14:48:01 +01003834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003835static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003836{
Hanno Becker40f50842018-08-15 14:48:01 +01003837 if( ssl->in_left > ssl->next_record_offset )
3838 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003839
Hanno Becker40f50842018-08-15 14:48:01 +01003840 return( 0 );
3841}
3842
3843static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3844{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003846 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003847 int ret = 0;
3848
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003849 if( hs == NULL )
3850 return( -1 );
3851
Hanno Beckere00ae372018-08-20 09:39:42 +01003852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3853
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003854 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3855 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3856 {
3857 /* Check if we have seen a ChangeCipherSpec before.
3858 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003859 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003860 {
3861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3862 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003863 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003864 }
3865
Hanno Becker39b8bc92018-08-28 17:17:13 +01003866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003867 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3868 ssl->in_msglen = 1;
3869 ssl->in_msg[0] = 1;
3870
3871 /* As long as they are equal, the exact value doesn't matter. */
3872 ssl->in_left = 0;
3873 ssl->next_record_offset = 0;
3874
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003875 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003876 goto exit;
3877 }
Hanno Becker37f95322018-08-16 13:55:32 +01003878
Hanno Beckerb8f50142018-08-28 10:01:34 +01003879#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003880 /* Debug only */
3881 {
3882 unsigned offset;
3883 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3884 {
3885 hs_buf = &hs->buffering.hs[offset];
3886 if( hs_buf->is_valid == 1 )
3887 {
3888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3889 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003890 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003891 }
3892 }
3893 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003894#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003895
3896 /* Check if we have buffered and/or fully reassembled the
3897 * next handshake message. */
3898 hs_buf = &hs->buffering.hs[0];
3899 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3900 {
3901 /* Synthesize a record containing the buffered HS message. */
3902 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3903 ( hs_buf->data[2] << 8 ) |
3904 hs_buf->data[3];
3905
3906 /* Double-check that we haven't accidentally buffered
3907 * a message that doesn't fit into the input buffer. */
3908 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3909 {
3910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3911 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3912 }
3913
3914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3915 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3916 hs_buf->data, msg_len + 12 );
3917
3918 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3919 ssl->in_hslen = msg_len + 12;
3920 ssl->in_msglen = msg_len + 12;
3921 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3922
3923 ret = 0;
3924 goto exit;
3925 }
3926 else
3927 {
3928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3929 hs->in_msg_seq ) );
3930 }
3931
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003932 ret = -1;
3933
3934exit:
3935
3936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3937 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003938}
3939
Hanno Beckera02b0b42018-08-21 17:20:27 +01003940static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3941 size_t desired )
3942{
3943 int offset;
3944 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3946 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003947
Hanno Becker01315ea2018-08-21 17:22:17 +01003948 /* Get rid of future records epoch first, if such exist. */
3949 ssl_free_buffered_record( ssl );
3950
3951 /* Check if we have enough space available now. */
3952 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3953 hs->buffering.total_bytes_buffered ) )
3954 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003956 return( 0 );
3957 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003958
Hanno Becker4f432ad2018-08-28 10:02:32 +01003959 /* We don't have enough space to buffer the next expected handshake
3960 * message. Remove buffers used for future messages to gain space,
3961 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003962 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3963 offset >= 0; offset-- )
3964 {
3965 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3966 offset ) );
3967
Hanno Beckerb309b922018-08-23 13:18:05 +01003968 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003969
3970 /* Check if we have enough space available now. */
3971 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3972 hs->buffering.total_bytes_buffered ) )
3973 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003975 return( 0 );
3976 }
3977 }
3978
3979 return( -1 );
3980}
3981
Hanno Becker40f50842018-08-15 14:48:01 +01003982static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3983{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003984 int ret = 0;
3985 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3986
3987 if( hs == NULL )
3988 return( 0 );
3989
3990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3991
3992 switch( ssl->in_msgtype )
3993 {
3994 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3995 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003996
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003997 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003998 break;
3999
4000 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004001 {
4002 unsigned recv_msg_seq_offset;
4003 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4004 mbedtls_ssl_hs_buffer *hs_buf;
4005 size_t msg_len = ssl->in_hslen - 12;
4006
4007 /* We should never receive an old handshake
4008 * message - double-check nonetheless. */
4009 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4010 {
4011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4012 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4013 }
4014
4015 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4016 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4017 {
4018 /* Silently ignore -- message too far in the future */
4019 MBEDTLS_SSL_DEBUG_MSG( 2,
4020 ( "Ignore future HS message with sequence number %u, "
4021 "buffering window %u - %u",
4022 recv_msg_seq, ssl->handshake->in_msg_seq,
4023 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4024
4025 goto exit;
4026 }
4027
4028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4029 recv_msg_seq, recv_msg_seq_offset ) );
4030
4031 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4032
4033 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004034 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004035 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004036 size_t reassembly_buf_sz;
4037
Hanno Becker37f95322018-08-16 13:55:32 +01004038 hs_buf->is_fragmented =
4039 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4040
4041 /* We copy the message back into the input buffer
4042 * after reassembly, so check that it's not too large.
4043 * This is an implementation-specific limitation
4044 * and not one from the standard, hence it is not
4045 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004046 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004047 {
4048 /* Ignore message */
4049 goto exit;
4050 }
4051
Hanno Beckere0b150f2018-08-21 15:51:03 +01004052 /* Check if we have enough space to buffer the message. */
4053 if( hs->buffering.total_bytes_buffered >
4054 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4055 {
4056 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4057 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4058 }
4059
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004060 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4061 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004062
4063 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4064 hs->buffering.total_bytes_buffered ) )
4065 {
4066 if( recv_msg_seq_offset > 0 )
4067 {
4068 /* If we can't buffer a future message because
4069 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4071 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4072 " (already %" MBEDTLS_PRINTF_SIZET
4073 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004074 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004075 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004076 goto exit;
4077 }
Hanno Beckere1801392018-08-21 16:51:05 +01004078 else
4079 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4081 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4082 " (already %" MBEDTLS_PRINTF_SIZET
4083 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004084 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004085 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01004086 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004087
Hanno Beckera02b0b42018-08-21 17:20:27 +01004088 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004089 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
4091 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
4092 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
4093 " (already %" MBEDTLS_PRINTF_SIZET
4094 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00004095 msg_len,
4096 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00004097 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004098 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004099 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4100 goto exit;
4101 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004102 }
4103
Paul Elliottd48d5c62021-01-07 14:47:05 +00004104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004105 msg_len ) );
4106
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004107 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4108 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004109 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004110 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004111 goto exit;
4112 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004113 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004114
4115 /* Prepare final header: copy msg_type, length and message_seq,
4116 * then add standardised fragment_offset and fragment_length */
4117 memcpy( hs_buf->data, ssl->in_msg, 6 );
4118 memset( hs_buf->data + 6, 0, 3 );
4119 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4120
4121 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004122
4123 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004124 }
4125 else
4126 {
4127 /* Make sure msg_type and length are consistent */
4128 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4129 {
4130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4131 /* Ignore */
4132 goto exit;
4133 }
4134 }
4135
Hanno Becker4422bbb2018-08-20 09:40:19 +01004136 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004137 {
4138 size_t frag_len, frag_off;
4139 unsigned char * const msg = hs_buf->data + 12;
4140
4141 /*
4142 * Check and copy current fragment
4143 */
4144
4145 /* Validation of header fields already done in
4146 * mbedtls_ssl_prepare_handshake_record(). */
4147 frag_off = ssl_get_hs_frag_off( ssl );
4148 frag_len = ssl_get_hs_frag_len( ssl );
4149
Paul Elliottd48d5c62021-01-07 14:47:05 +00004150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4151 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004152 frag_off, frag_len ) );
4153 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4154
4155 if( hs_buf->is_fragmented )
4156 {
4157 unsigned char * const bitmask = msg + msg_len;
4158 ssl_bitmask_set( bitmask, frag_off, frag_len );
4159 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4160 msg_len ) == 0 );
4161 }
4162 else
4163 {
4164 hs_buf->is_complete = 1;
4165 }
4166
4167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4168 hs_buf->is_complete ? "" : "not yet " ) );
4169 }
4170
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004171 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004172 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004173
4174 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004175 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004176 break;
4177 }
4178
4179exit:
4180
4181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4182 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004183}
4184#endif /* MBEDTLS_SSL_PROTO_DTLS */
4185
Hanno Becker1097b342018-08-15 14:09:41 +01004186static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004187{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004188 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004189 * Consume last content-layer message and potentially
4190 * update in_msglen which keeps track of the contents'
4191 * consumption state.
4192 *
4193 * (1) Handshake messages:
4194 * Remove last handshake message, move content
4195 * and adapt in_msglen.
4196 *
4197 * (2) Alert messages:
4198 * Consume whole record content, in_msglen = 0.
4199 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004200 * (3) Change cipher spec:
4201 * Consume whole record content, in_msglen = 0.
4202 *
4203 * (4) Application data:
4204 * Don't do anything - the record layer provides
4205 * the application data as a stream transport
4206 * and consumes through mbedtls_ssl_read only.
4207 *
4208 */
4209
4210 /* Case (1): Handshake messages */
4211 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004212 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004213 /* Hard assertion to be sure that no application data
4214 * is in flight, as corrupting ssl->in_msglen during
4215 * ssl->in_offt != NULL is fatal. */
4216 if( ssl->in_offt != NULL )
4217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4220 }
4221
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004222 /*
4223 * Get next Handshake message in the current record
4224 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004225
Hanno Becker4a810fb2017-05-24 16:27:30 +01004226 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004227 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004228 * current handshake content: If DTLS handshake
4229 * fragmentation is used, that's the fragment
4230 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004231 * size here is faulty and should be changed at
4232 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004233 * (2) While it doesn't seem to cause problems, one
4234 * has to be very careful not to assume that in_hslen
4235 * is always <= in_msglen in a sensible communication.
4236 * Again, it's wrong for DTLS handshake fragmentation.
4237 * The following check is therefore mandatory, and
4238 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004239 * Additionally, ssl->in_hslen might be arbitrarily out of
4240 * bounds after handling a DTLS message with an unexpected
4241 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004242 */
4243 if( ssl->in_hslen < ssl->in_msglen )
4244 {
4245 ssl->in_msglen -= ssl->in_hslen;
4246 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4247 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004248
Hanno Becker4a810fb2017-05-24 16:27:30 +01004249 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4250 ssl->in_msg, ssl->in_msglen );
4251 }
4252 else
4253 {
4254 ssl->in_msglen = 0;
4255 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004256
Hanno Becker4a810fb2017-05-24 16:27:30 +01004257 ssl->in_hslen = 0;
4258 }
4259 /* Case (4): Application data */
4260 else if( ssl->in_offt != NULL )
4261 {
4262 return( 0 );
4263 }
4264 /* Everything else (CCS & Alerts) */
4265 else
4266 {
4267 ssl->in_msglen = 0;
4268 }
4269
Hanno Becker1097b342018-08-15 14:09:41 +01004270 return( 0 );
4271}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004272
Hanno Beckere74d5562018-08-15 14:26:08 +01004273static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4274{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004275 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004276 return( 1 );
4277
4278 return( 0 );
4279}
4280
Hanno Becker5f066e72018-08-16 14:56:31 +01004281#if defined(MBEDTLS_SSL_PROTO_DTLS)
4282
4283static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4284{
4285 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4286 if( hs == NULL )
4287 return;
4288
Hanno Becker01315ea2018-08-21 17:22:17 +01004289 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004290 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004291 hs->buffering.total_bytes_buffered -=
4292 hs->buffering.future_record.len;
4293
4294 mbedtls_free( hs->buffering.future_record.data );
4295 hs->buffering.future_record.data = NULL;
4296 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004297}
4298
4299static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4300{
4301 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4302 unsigned char * rec;
4303 size_t rec_len;
4304 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4306 size_t in_buf_len = ssl->in_buf_len;
4307#else
4308 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4309#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004310 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4311 return( 0 );
4312
4313 if( hs == NULL )
4314 return( 0 );
4315
Hanno Becker5f066e72018-08-16 14:56:31 +01004316 rec = hs->buffering.future_record.data;
4317 rec_len = hs->buffering.future_record.len;
4318 rec_epoch = hs->buffering.future_record.epoch;
4319
4320 if( rec == NULL )
4321 return( 0 );
4322
Hanno Becker4cb782d2018-08-20 11:19:05 +01004323 /* Only consider loading future records if the
4324 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004325 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004326 return( 0 );
4327
Hanno Becker5f066e72018-08-16 14:56:31 +01004328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4329
4330 if( rec_epoch != ssl->in_epoch )
4331 {
4332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4333 goto exit;
4334 }
4335
4336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4337
4338 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004339 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004340 {
4341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4343 }
4344
4345 memcpy( ssl->in_hdr, rec, rec_len );
4346 ssl->in_left = rec_len;
4347 ssl->next_record_offset = 0;
4348
4349 ssl_free_buffered_record( ssl );
4350
4351exit:
4352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4353 return( 0 );
4354}
4355
Hanno Becker519f15d2019-07-11 12:43:20 +01004356static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4357 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004358{
4359 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004360
4361 /* Don't buffer future records outside handshakes. */
4362 if( hs == NULL )
4363 return( 0 );
4364
4365 /* Only buffer handshake records (we are only interested
4366 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004367 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004368 return( 0 );
4369
4370 /* Don't buffer more than one future epoch record. */
4371 if( hs->buffering.future_record.data != NULL )
4372 return( 0 );
4373
Hanno Becker01315ea2018-08-21 17:22:17 +01004374 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004375 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004376 hs->buffering.total_bytes_buffered ) )
4377 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4379 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4380 " (already %" MBEDTLS_PRINTF_SIZET
4381 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004382 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004383 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004384 return( 0 );
4385 }
4386
Hanno Becker5f066e72018-08-16 14:56:31 +01004387 /* Buffer record */
4388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004389 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004390 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004391
4392 /* ssl_parse_record_header() only considers records
4393 * of the next epoch as candidates for buffering. */
4394 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004395 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004396
4397 hs->buffering.future_record.data =
4398 mbedtls_calloc( 1, hs->buffering.future_record.len );
4399 if( hs->buffering.future_record.data == NULL )
4400 {
4401 /* If we run out of RAM trying to buffer a
4402 * record from the next epoch, just ignore. */
4403 return( 0 );
4404 }
4405
Hanno Becker519f15d2019-07-11 12:43:20 +01004406 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004407
Hanno Becker519f15d2019-07-11 12:43:20 +01004408 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004409 return( 0 );
4410}
4411
4412#endif /* MBEDTLS_SSL_PROTO_DTLS */
4413
Hanno Beckere74d5562018-08-15 14:26:08 +01004414static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004415{
Janos Follath865b3eb2019-12-16 11:46:15 +00004416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004417 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004418
Hanno Becker5f066e72018-08-16 14:56:31 +01004419#if defined(MBEDTLS_SSL_PROTO_DTLS)
4420 /* We might have buffered a future record; if so,
4421 * and if the epoch matches now, load it.
4422 * On success, this call will set ssl->in_left to
4423 * the length of the buffered record, so that
4424 * the calls to ssl_fetch_input() below will
4425 * essentially be no-ops. */
4426 ret = ssl_load_buffered_record( ssl );
4427 if( ret != 0 )
4428 return( ret );
4429#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004430
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004431 /* Ensure that we have enough space available for the default form
4432 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4433 * with no space for CIDs counted in). */
4434 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4435 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004438 return( ret );
4439 }
4440
Hanno Beckere5e7e782019-07-11 12:29:35 +01004441 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4442 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004446 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004447 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4448 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004449 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004450 if( ret != 0 )
4451 return( ret );
4452
4453 /* Fall through to handling of unexpected records */
4454 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4455 }
4456
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004457 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4458 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004459#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004460 /* Reset in pointers to default state for TLS/DTLS records,
4461 * assuming no CID and no offset between record content and
4462 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004463 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004464
Hanno Becker7ae20e02019-07-12 08:33:49 +01004465 /* Setup internal message pointers from record structure. */
4466 ssl->in_msgtype = rec.type;
4467#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4468 ssl->in_len = ssl->in_cid + rec.cid_len;
4469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4470 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4471 ssl->in_msglen = rec.data_len;
4472
Hanno Becker2fddd372019-07-10 14:37:41 +01004473 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004474 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004475 if( ret != 0 )
4476 return( ret );
4477#endif
4478
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004479 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004480 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004481
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4483 "(header)" ) );
4484 }
4485 else
4486 {
4487 /* Skip invalid record and the rest of the datagram */
4488 ssl->next_record_offset = 0;
4489 ssl->in_left = 0;
4490
4491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4492 "(header)" ) );
4493 }
4494
4495 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004496 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004497 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004498 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004499#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004500 {
4501 return( ret );
4502 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004503 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004506 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004507 {
Hanno Beckera8814792019-07-10 15:01:45 +01004508 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004509 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004510 if( ssl->next_record_offset < ssl->in_left )
4511 {
4512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4513 }
4514 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004515 else
4516#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004517 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004518 /*
4519 * Fetch record contents from underlying transport.
4520 */
Hanno Beckera3175662019-07-11 12:50:29 +01004521 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004522 if( ret != 0 )
4523 {
4524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4525 return( ret );
4526 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004527
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004528 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004529 }
4530
4531 /*
4532 * Decrypt record contents.
4533 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004534
Hanno Beckerfdf66042019-07-11 13:07:45 +01004535 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004537#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004539 {
4540 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004541 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004542 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004543 /* Except when waiting for Finished as a bad mac here
4544 * probably means something went wrong in the handshake
4545 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4546 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4547 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4548 {
4549#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4550 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4551 {
4552 mbedtls_ssl_send_alert_message( ssl,
4553 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4554 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4555 }
4556#endif
4557 return( ret );
4558 }
4559
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004560 if( ssl->conf->badmac_limit != 0 &&
4561 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4564 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004565 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004566
Hanno Becker4a810fb2017-05-24 16:27:30 +01004567 /* As above, invalid records cause
4568 * dismissal of the whole datagram. */
4569
4570 ssl->next_record_offset = 0;
4571 ssl->in_left = 0;
4572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004574 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004575 }
4576
4577 return( ret );
4578 }
4579 else
4580#endif
4581 {
4582 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004583#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4584 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004586 mbedtls_ssl_send_alert_message( ssl,
4587 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4588 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004589 }
4590#endif
4591 return( ret );
4592 }
4593 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004594
Hanno Becker44d89b22019-07-12 09:40:44 +01004595
4596 /* Reset in pointers to default state for TLS/DTLS records,
4597 * assuming no CID and no offset between record content and
4598 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004599 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004600#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4601 ssl->in_len = ssl->in_cid + rec.cid_len;
4602#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004603 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004604
Hanno Becker8685c822019-07-12 09:37:30 +01004605 /* The record content type may change during decryption,
4606 * so re-read it. */
4607 ssl->in_msgtype = rec.type;
4608 /* Also update the input buffer, because unfortunately
4609 * the server-side ssl_parse_client_hello() reparses the
4610 * record header when receiving a ClientHello initiating
4611 * a renegotiation. */
4612 ssl->in_hdr[0] = rec.type;
4613 ssl->in_msg = rec.buf + rec.data_offset;
4614 ssl->in_msglen = rec.data_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01004615 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->in_len, 0 );
Hanno Becker8685c822019-07-12 09:37:30 +01004616
Simon Butcher99000142016-10-13 17:21:01 +01004617 return( 0 );
4618}
4619
4620int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4621{
Janos Follath865b3eb2019-12-16 11:46:15 +00004622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004623
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004624 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004625 * Handle particular types of records
4626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 {
Simon Butcher99000142016-10-13 17:21:01 +01004629 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4630 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004631 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004632 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004633 }
4634
Hanno Beckere678eaa2018-08-21 14:57:46 +01004635 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004636 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004637 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004638 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004640 ssl->in_msglen ) );
4641 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004642 }
4643
Hanno Beckere678eaa2018-08-21 14:57:46 +01004644 if( ssl->in_msg[0] != 1 )
4645 {
4646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4647 ssl->in_msg[0] ) );
4648 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4649 }
4650
4651#if defined(MBEDTLS_SSL_PROTO_DTLS)
4652 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4653 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4654 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4655 {
4656 if( ssl->handshake == NULL )
4657 {
4658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4659 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4660 }
4661
4662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4663 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4664 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004665#endif
Ronald Cron7e38cba2021-11-24 12:43:39 +01004666
Ronald Cron6f135e12021-12-08 16:57:54 +01004667#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron7e38cba2021-11-24 12:43:39 +01004668 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
4669 {
4670#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
4671 MBEDTLS_SSL_DEBUG_MSG( 1,
4672 ( "Ignore ChangeCipherSpec in TLS 1.3 compatibility mode" ) );
4673 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4674#else
4675 MBEDTLS_SSL_DEBUG_MSG( 1,
4676 ( "ChangeCipherSpec invalid in TLS 1.3 without compatibility mode" ) );
4677 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4678#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
4679 }
Ronald Cron6f135e12021-12-08 16:57:54 +01004680#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere678eaa2018-08-21 14:57:46 +01004681 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004684 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004685 if( ssl->in_msglen != 2 )
4686 {
4687 /* Note: Standard allows for more than one 2 byte alert
4688 to be packed in a single message, but Mbed TLS doesn't
4689 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004691 ssl->in_msglen ) );
4692 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4693 }
4694
Paul Elliott9f352112020-12-09 14:55:45 +00004695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004696 ssl->in_msg[0], ssl->in_msg[1] ) );
4697
4698 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004699 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004704 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 }
4707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004708 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4709 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4712 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004714
4715#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4716 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4717 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4718 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004720 /* Will be handled when trying to parse ServerHello */
4721 return( 0 );
4722 }
4723#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004724 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004725 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004726 }
4727
Hanno Beckerc76c6192017-06-06 10:03:17 +01004728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004730 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004731 /* Drop unexpected ApplicationData records,
4732 * except at the beginning of renegotiations */
4733 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4734 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4735#if defined(MBEDTLS_SSL_RENEGOTIATION)
4736 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4737 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004738#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004739 )
4740 {
4741 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4742 return( MBEDTLS_ERR_SSL_NON_FATAL );
4743 }
4744
4745 if( ssl->handshake != NULL &&
4746 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4747 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004748 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004749 }
4750 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004751#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004752
Paul Bakker5121ce52009-01-03 21:22:43 +00004753 return( 0 );
4754}
4755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004756int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004757{
irwir6c0da642019-09-26 21:07:41 +03004758 return( mbedtls_ssl_send_alert_message( ssl,
4759 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4760 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004761}
4762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004764 unsigned char level,
4765 unsigned char message )
4766{
Janos Follath865b3eb2019-12-16 11:46:15 +00004767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004768
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004769 if( ssl == NULL || ssl->conf == NULL )
4770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004776 ssl->out_msglen = 2;
4777 ssl->out_msg[0] = level;
4778 ssl->out_msg[1] = message;
4779
Hanno Becker67bc7c32018-08-06 11:33:50 +01004780 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004783 return( ret );
4784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004786
4787 return( 0 );
4788}
4789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004791{
Janos Follath865b3eb2019-12-16 11:46:15 +00004792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 ssl->out_msglen = 1;
4798 ssl->out_msg[0] = 1;
4799
Paul Bakker5121ce52009-01-03 21:22:43 +00004800 ssl->state++;
4801
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004802 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004805 return( ret );
4806 }
4807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004809
4810 return( 0 );
4811}
4812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004813int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814{
Janos Follath865b3eb2019-12-16 11:46:15 +00004815 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004818
Hanno Becker327c93b2018-08-15 13:56:18 +01004819 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004822 return( ret );
4823 }
4824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004828 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4829 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004831 }
4832
Hanno Beckere678eaa2018-08-21 14:57:46 +01004833 /* CCS records are only accepted if they have length 1 and content '1',
4834 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004835
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004836 /*
4837 * Switch to our negotiated transform and session parameters for inbound
4838 * data.
4839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004841 ssl->transform_in = ssl->transform_negotiate;
4842 ssl->session_in = ssl->session_negotiate;
4843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004847#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004848 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004849#endif
4850
4851 /* Increment epoch */
4852 if( ++ssl->in_epoch == 0 )
4853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004855 /* This is highly unlikely to happen for legitimate reasons, so
4856 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004858 }
4859 }
4860 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jerry Yufd320e92021-10-08 21:52:41 +08004862 memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004863
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004864 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004865
Paul Bakker5121ce52009-01-03 21:22:43 +00004866 ssl->state++;
4867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
4870 return( 0 );
4871}
4872
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004873/* Once ssl->out_hdr as the address of the beginning of the
4874 * next outgoing record is set, deduce the other pointers.
4875 *
4876 * Note: For TLS, we save the implicit record sequence number
4877 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4878 * and the caller has to make sure there's space for this.
4879 */
4880
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004881static size_t ssl_transform_get_explicit_iv_len(
4882 mbedtls_ssl_transform const *transform )
4883{
TRodziewiczef73f012021-05-13 14:53:36 +02004884 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004885 return( 0 );
4886
4887 return( transform->ivlen - transform->fixed_ivlen );
4888}
4889
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004890void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4891 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004892{
4893#if defined(MBEDTLS_SSL_PROTO_DTLS)
4894 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4895 {
4896 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004897#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08004898 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004899 ssl->out_len = ssl->out_cid;
4900 if( transform != NULL )
4901 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004902#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08004903 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004904#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004905 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004906 }
4907 else
4908#endif
4909 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004910 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004911#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004912 ssl->out_cid = ssl->out_len;
4913#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004914 ssl->out_iv = ssl->out_hdr + 5;
4915 }
4916
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004917 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004918 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004919 if( transform != NULL )
4920 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004921}
4922
4923/* Once ssl->in_hdr as the address of the beginning of the
4924 * next incoming record is set, deduce the other pointers.
4925 *
4926 * Note: For TLS, we save the implicit record sequence number
4927 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4928 * and the caller has to make sure there's space for this.
4929 */
4930
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004931void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004932{
Hanno Becker79594fd2019-05-08 09:38:41 +01004933 /* This function sets the pointers to match the case
4934 * of unprotected TLS/DTLS records, with both ssl->in_iv
4935 * and ssl->in_msg pointing to the beginning of the record
4936 * content.
4937 *
4938 * When decrypting a protected record, ssl->in_msg
4939 * will be shifted to point to the beginning of the
4940 * record plaintext.
4941 */
4942
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004943#if defined(MBEDTLS_SSL_PROTO_DTLS)
4944 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4945 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004946 /* This sets the header pointers to match records
4947 * without CID. When we receive a record containing
4948 * a CID, the fields are shifted accordingly in
4949 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004950 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004951#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08004952 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004953 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004954#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08004955 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004956#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004957 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004958 }
4959 else
4960#endif
4961 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004962 ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004963 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004964#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004965 ssl->in_cid = ssl->in_len;
4966#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004967 ssl->in_iv = ssl->in_hdr + 5;
4968 }
4969
Hanno Becker79594fd2019-05-08 09:38:41 +01004970 /* This will be adjusted at record decryption time. */
4971 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004972}
4973
Paul Bakker5121ce52009-01-03 21:22:43 +00004974/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004975 * Setup an SSL context
4976 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004977
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004978void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004979{
4980 /* Set the incoming and outgoing record pointers. */
4981#if defined(MBEDTLS_SSL_PROTO_DTLS)
4982 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4983 {
4984 ssl->out_hdr = ssl->out_buf;
4985 ssl->in_hdr = ssl->in_buf;
4986 }
4987 else
4988#endif /* MBEDTLS_SSL_PROTO_DTLS */
4989 {
Hanno Becker12078f42021-03-02 15:28:41 +00004990 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004991 ssl->out_hdr = ssl->out_buf + 8;
4992 ssl->in_hdr = ssl->in_buf + 8;
4993 }
4994
4995 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004996 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4997 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004998}
4999
Paul Bakker5121ce52009-01-03 21:22:43 +00005000/*
5001 * SSL get accessors
5002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005003size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005004{
5005 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5006}
5007
Hanno Becker8b170a02017-10-10 11:51:19 +01005008int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5009{
5010 /*
5011 * Case A: We're currently holding back
5012 * a message for further processing.
5013 */
5014
5015 if( ssl->keep_current_message == 1 )
5016 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005017 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005018 return( 1 );
5019 }
5020
5021 /*
5022 * Case B: Further records are pending in the current datagram.
5023 */
5024
5025#if defined(MBEDTLS_SSL_PROTO_DTLS)
5026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5027 ssl->in_left > ssl->next_record_offset )
5028 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005030 return( 1 );
5031 }
5032#endif /* MBEDTLS_SSL_PROTO_DTLS */
5033
5034 /*
5035 * Case C: A handshake message is being processed.
5036 */
5037
Hanno Becker8b170a02017-10-10 11:51:19 +01005038 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5039 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005041 return( 1 );
5042 }
5043
5044 /*
5045 * Case D: An application data message is being processed
5046 */
5047 if( ssl->in_offt != NULL )
5048 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005050 return( 1 );
5051 }
5052
5053 /*
5054 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005055 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005056 * we implement support for multiple alerts in single records.
5057 */
5058
5059 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5060 return( 0 );
5061}
5062
Paul Bakker43ca69c2011-01-15 17:35:19 +00005063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005065{
Hanno Becker3136ede2018-08-17 15:28:19 +01005066 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005068 unsigned block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005069#if defined(MBEDTLS_USE_PSA_CRYPTO)
5070 psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
5071 psa_key_type_t key_type;
5072#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005073
Hanno Becker5903de42019-05-03 14:46:38 +01005074 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5075
Hanno Becker78640902018-08-13 16:35:15 +01005076 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005077 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005078
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005079
5080#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielbe47ecf2022-01-31 13:53:11 +01005081 if ( transform->psa_alg == PSA_ALG_GCM ||
5082 transform->psa_alg == PSA_ALG_CCM ||
5083 transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 8 ) ||
5084 transform->psa_alg == PSA_ALG_CHACHA20_POLY1305 ||
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005085 transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER )
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005086 {
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005087 transform_expansion = transform->minlen;
5088 }
Przemyslaw Stekiel399ed512022-01-31 08:38:00 +01005089 else if ( transform->psa_alg == PSA_ALG_CBC_NO_PADDING )
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005090 {
5091 (void) psa_get_key_attributes( transform->psa_key_enc, &attr );
5092 key_type = psa_get_key_type( &attr );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005093
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005094 block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005095
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005096 /* Expansion due to the addition of the MAC. */
5097 transform_expansion += transform->maclen;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005098
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005099 /* Expansion due to the addition of CBC padding;
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005100 * Theoretically up to 256 bytes, but we never use
5101 * more than the block size of the underlying cipher. */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005102 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005103
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005104 /* For TLS 1.2 or higher, an explicit IV is added
Przemyslaw Stekiel8c010eb2022-02-03 10:44:02 +01005105 * after the record header. */
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005106#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005107 transform_expansion += block_size;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005109 }
5110 else
5111 {
Przemyslaw Stekiel6b2eedd2022-02-03 09:54:34 +01005112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()" ) );
Przemyslaw Stekiel1d714472022-01-24 23:46:50 +01005113 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005114 }
5115#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005118 case MBEDTLS_MODE_GCM:
5119 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005120 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005122 transform_expansion = transform->minlen;
5123 break;
5124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005126
5127 block_size = mbedtls_cipher_get_block_size(
5128 &transform->cipher_ctx_enc );
5129
Hanno Becker3136ede2018-08-17 15:28:19 +01005130 /* Expansion due to the addition of the MAC. */
5131 transform_expansion += transform->maclen;
5132
5133 /* Expansion due to the addition of CBC padding;
5134 * Theoretically up to 256 bytes, but we never use
5135 * more than the block size of the underlying cipher. */
5136 transform_expansion += block_size;
5137
TRodziewicz4ca18aa2021-05-20 14:46:20 +02005138 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01005139 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02005140#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005141 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02005142#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005143
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005144 break;
5145
5146 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005149 }
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005150#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005151
Hanno Beckera0e20d02019-05-15 14:03:01 +01005152#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005153 if( transform->out_cid_len != 0 )
5154 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005155#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005156
Hanno Becker5903de42019-05-03 14:46:38 +01005157 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005158}
5159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005161/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005162 * Check record counters and renegotiate if they're above the limit.
5163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005165{
Hanno Beckerdd772292020-02-05 10:38:31 +00005166 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005167 int in_ctr_cmp;
5168 int out_ctr_cmp;
5169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5171 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005172 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005173 {
5174 return( 0 );
5175 }
5176
Andres AG2196c7f2016-12-15 17:01:16 +00005177 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Jerry Yud9a94fe2021-09-28 18:58:59 +08005178 &ssl->conf->renego_period[ep_len],
Jerry Yuae0b2e22021-10-08 15:21:19 +08005179 MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len );
Jerry Yud9a94fe2021-09-28 18:58:59 +08005180 out_ctr_cmp = memcmp( &ssl->cur_out_ctr[ep_len],
5181 &ssl->conf->renego_period[ep_len],
5182 sizeof( ssl->cur_out_ctr ) - ep_len );
Andres AG2196c7f2016-12-15 17:01:16 +00005183
5184 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005185 {
5186 return( 0 );
5187 }
5188
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005193
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005194/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005195 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005196 * may only be sent for the purpose of initiating renegotiations.
5197 *
5198 * This function is introduced as a separate helper since the handling
5199 * of post-handshake handshake messages changes significantly in TLS 1.3,
5200 * and having a helper function allows to distinguish between TLS <= 1.2 and
5201 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5202 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00005203static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005204{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005206
5207 /*
5208 * - For client-side, expect SERVER_HELLO_REQUEST.
5209 * - For server-side, expect CLIENT_HELLO.
5210 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5211 */
5212
5213#if defined(MBEDTLS_SSL_CLI_C)
5214 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5215 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5216 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
5217 {
5218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
5219
5220 /* With DTLS, drop the packet (probably from last handshake) */
5221#if defined(MBEDTLS_SSL_PROTO_DTLS)
5222 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5223 {
5224 return( 0 );
5225 }
5226#endif
5227 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5228 }
5229#endif /* MBEDTLS_SSL_CLI_C */
5230
5231#if defined(MBEDTLS_SSL_SRV_C)
5232 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5233 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
5234 {
5235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5236
5237 /* With DTLS, drop the packet (probably from last handshake) */
5238#if defined(MBEDTLS_SSL_PROTO_DTLS)
5239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5240 {
5241 return( 0 );
5242 }
5243#endif
5244 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5245 }
5246#endif /* MBEDTLS_SSL_SRV_C */
5247
5248#if defined(MBEDTLS_SSL_RENEGOTIATION)
5249 /* Determine whether renegotiation attempt should be accepted */
5250 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5251 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5252 ssl->conf->allow_legacy_renegotiation ==
5253 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5254 {
5255 /*
5256 * Accept renegotiation request
5257 */
5258
5259 /* DTLS clients need to know renego is server-initiated */
5260#if defined(MBEDTLS_SSL_PROTO_DTLS)
5261 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5262 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5263 {
5264 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5265 }
5266#endif
5267 ret = mbedtls_ssl_start_renegotiation( ssl );
5268 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5269 ret != 0 )
5270 {
5271 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5272 ret );
5273 return( ret );
5274 }
5275 }
5276 else
5277#endif /* MBEDTLS_SSL_RENEGOTIATION */
5278 {
5279 /*
5280 * Refuse renegotiation
5281 */
5282
5283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5284
TRodziewicz0f82ec62021-05-12 17:49:18 +02005285#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005286 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5287 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5288 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005289 {
TRodziewicz345165c2021-07-06 13:42:11 +02005290 return( ret );
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005291 }
TRodziewicz0f82ec62021-05-12 17:49:18 +02005292#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005293 }
5294
5295 return( 0 );
5296}
5297
Paul Bakker48916f92012-09-16 19:57:18 +00005298/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005299 * Receive application data decrypted from the SSL layer
5300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005301int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005302{
Janos Follath865b3eb2019-12-16 11:46:15 +00005303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005304 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005305
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005306 if( ssl == NULL || ssl->conf == NULL )
5307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005314 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005315 return( ret );
5316
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005317 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005318 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005319 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005320 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005321 return( ret );
5322 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005323 }
5324#endif
5325
Hanno Becker4a810fb2017-05-24 16:27:30 +01005326 /*
5327 * Check if renegotiation is necessary and/or handshake is
5328 * in process. If yes, perform/continue, and fall through
5329 * if an unexpected packet is received while the client
5330 * is waiting for the ServerHello.
5331 *
5332 * (There is no equivalent to the last condition on
5333 * the server-side as it is not treated as within
5334 * a handshake while waiting for the ClientHello
5335 * after a renegotiation request.)
5336 */
5337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005339 ret = ssl_check_ctr_renegotiate( ssl );
5340 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5341 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005344 return( ret );
5345 }
5346#endif
5347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005351 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5352 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005354 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005355 return( ret );
5356 }
5357 }
5358
Hanno Beckere41158b2017-10-23 13:30:32 +01005359 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005360 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005361 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005362 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005363 if( ssl->f_get_timer != NULL &&
5364 ssl->f_get_timer( ssl->p_timer ) == -1 )
5365 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005366 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005367 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005368
Hanno Becker327c93b2018-08-15 13:56:18 +01005369 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005370 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005371 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5372 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005373
Hanno Becker4a810fb2017-05-24 16:27:30 +01005374 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5375 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005376 }
5377
5378 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005380 {
5381 /*
5382 * OpenSSL sends empty messages to randomize the IV
5383 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005384 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005387 return( 0 );
5388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005390 return( ret );
5391 }
5392 }
5393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005395 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005396 ret = ssl_handle_hs_message_post_handshake( ssl );
5397 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005398 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005399 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5400 ret );
5401 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005402 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005403
Hanno Beckerf26cc722021-04-21 07:30:13 +01005404 /* At this point, we don't know whether the renegotiation triggered
5405 * by the post-handshake message has been completed or not. The cases
5406 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005407 * 1) The renegotiation is complete. In this case, no new record
5408 * has been read yet.
5409 * 2) The renegotiation is incomplete because the client received
5410 * an application data record while awaiting the ServerHello.
5411 * 3) The renegotiation is incomplete because the client received
5412 * a non-handshake, non-application data message while awaiting
5413 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005414 *
5415 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005416 * - For 1), the next iteration will read a new record and check
5417 * if it's application data.
5418 * - For 2), the loop condition isn't satisfied as application data
5419 * is present, hence continue is the same as break
5420 * - For 3), the loop condition is satisfied and read_record
5421 * will re-deliver the message that was held back by the client
5422 * when expecting the ServerHello.
5423 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005424
Hanno Becker90333da2017-10-10 11:27:13 +01005425 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005426 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005427#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005428 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005429 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005430 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005431 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005432 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005435 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005437 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005438 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5443 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005446 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005447 }
5448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005449 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5452 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 }
5454
5455 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005456
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005457 /* We're going to return something now, cancel timer,
5458 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005460 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005461
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005463 /* If we requested renego but received AppData, resend HelloRequest.
5464 * Do it now, after setting in_offt, to avoid taking this branch
5465 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005467 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005469 {
Hanno Becker786300f2020-02-05 10:46:40 +00005470 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005471 {
Hanno Becker786300f2020-02-05 10:46:40 +00005472 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5473 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005474 return( ret );
5475 }
5476 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 }
5480
5481 n = ( len < ssl->in_msglen )
5482 ? len : ssl->in_msglen;
5483
5484 memcpy( buf, ssl->in_offt, n );
5485 ssl->in_msglen -= n;
5486
gabor-mezei-arma3214132020-07-15 10:55:00 +02005487 /* Zeroising the plaintext buffer to erase unused application data
5488 from the memory. */
5489 mbedtls_platform_zeroize( ssl->in_offt, n );
5490
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005492 {
5493 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005494 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005495 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005496 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005497 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005498 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 /* more data available */
5500 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005504
Paul Bakker23986e52011-04-24 08:57:21 +00005505 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506}
5507
5508/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005509 * Send application data to be encrypted by the SSL layer, taking care of max
5510 * fragment length and buffer size.
5511 *
5512 * According to RFC 5246 Section 6.2.1:
5513 *
5514 * Zero-length fragments of Application data MAY be sent as they are
5515 * potentially useful as a traffic analysis countermeasure.
5516 *
5517 * Therefore, it is possible that the input message length is 0 and the
5518 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005520static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005521 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005523 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5524 const size_t max_len = (size_t) ret;
5525
5526 if( ret < 0 )
5527 {
5528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5529 return( ret );
5530 }
5531
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005532 if( len > max_len )
5533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005534#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005535 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005538 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5539 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005540 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005542 }
5543 else
5544#endif
5545 len = max_len;
5546 }
Paul Bakker887bd502011-06-08 13:10:54 +00005547
Paul Bakker5121ce52009-01-03 21:22:43 +00005548 if( ssl->out_left != 0 )
5549 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005550 /*
5551 * The user has previously tried to send the data and
5552 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5553 * written. In this case, we expect the high-level write function
5554 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005556 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005559 return( ret );
5560 }
5561 }
Paul Bakker887bd502011-06-08 13:10:54 +00005562 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005563 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005564 /*
5565 * The user is trying to send a message the first time, so we need to
5566 * copy the data into the internal buffers and setup the data structure
5567 * to keep track of partial writes
5568 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005569 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005571 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005572
Hanno Becker67bc7c32018-08-06 11:33:50 +01005573 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005576 return( ret );
5577 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005578 }
5579
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005580 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005581}
5582
5583/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005584 * Write application data (public-facing wrapper)
5585 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005586int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005587{
Janos Follath865b3eb2019-12-16 11:46:15 +00005588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005589
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005591
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005592 if( ssl == NULL || ssl->conf == NULL )
5593 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5594
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005595#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005596 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5597 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005598 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005599 return( ret );
5600 }
5601#endif
5602
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005603 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005604 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005605 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005606 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005608 return( ret );
5609 }
5610 }
5611
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005612 ret = ssl_write_real( ssl, buf, len );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005613
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005614 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005615
5616 return( ret );
5617}
5618
5619/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005620 * Notify the peer that the connection is being closed
5621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005623{
Janos Follath865b3eb2019-12-16 11:46:15 +00005624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005625
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005626 if( ssl == NULL || ssl->conf == NULL )
5627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005630
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005631 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005634 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5637 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5638 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 return( ret );
5642 }
5643 }
5644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005646
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005647 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005648}
5649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005650void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005651{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005652 if( transform == NULL )
5653 return;
5654
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005655#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielce37d112022-01-13 14:53:52 +01005656 psa_destroy_key( transform->psa_key_enc );
5657 psa_destroy_key( transform->psa_key_dec );
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01005658#else
5659 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5660 mbedtls_cipher_free( &transform->cipher_ctx_dec );
5661#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005662
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005663#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005664 mbedtls_md_free( &transform->md_ctx_enc );
5665 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005666#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005667
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005668 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005669}
5670
Jerry Yuc7875b52021-09-05 21:05:50 +08005671void mbedtls_ssl_set_inbound_transform( mbedtls_ssl_context *ssl,
5672 mbedtls_ssl_transform *transform )
5673{
Jerry Yuc7875b52021-09-05 21:05:50 +08005674 ssl->transform_in = transform;
Jerry Yufd320e92021-10-08 21:52:41 +08005675 memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Jerry Yuc7875b52021-09-05 21:05:50 +08005676}
5677
5678void mbedtls_ssl_set_outbound_transform( mbedtls_ssl_context *ssl,
5679 mbedtls_ssl_transform *transform )
5680{
5681 ssl->transform_out = transform;
Jerry Yufd320e92021-10-08 21:52:41 +08005682 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
Jerry Yuc7875b52021-09-05 21:05:50 +08005683}
5684
Hanno Becker0271f962018-08-16 13:23:47 +01005685#if defined(MBEDTLS_SSL_PROTO_DTLS)
5686
Hanno Becker533ab5f2020-02-05 10:49:13 +00005687void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005688{
5689 unsigned offset;
5690 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5691
5692 if( hs == NULL )
5693 return;
5694
Hanno Becker283f5ef2018-08-24 09:34:47 +01005695 ssl_free_buffered_record( ssl );
5696
Hanno Becker0271f962018-08-16 13:23:47 +01005697 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005698 ssl_buffering_free_slot( ssl, offset );
5699}
5700
5701static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5702 uint8_t slot )
5703{
5704 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5705 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005706
5707 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5708 return;
5709
Hanno Beckere605b192018-08-21 15:59:07 +01005710 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005711 {
Hanno Beckere605b192018-08-21 15:59:07 +01005712 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005713 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005714 mbedtls_free( hs_buf->data );
5715 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005716 }
5717}
5718
5719#endif /* MBEDTLS_SSL_PROTO_DTLS */
5720
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005721/*
5722 * Convert version numbers to/from wire format
5723 * and, for DTLS, to/from TLS equivalent.
5724 *
5725 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005726 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005727 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005730 unsigned char ver[2] )
5731{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732#if defined(MBEDTLS_SSL_PROTO_DTLS)
5733 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005736 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5737
5738 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5739 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5740 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005741 else
5742#else
5743 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005744#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005745 {
5746 ver[0] = (unsigned char) major;
5747 ver[1] = (unsigned char) minor;
5748 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005749}
5750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005751void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005752 const unsigned char ver[2] )
5753{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005754#if defined(MBEDTLS_SSL_PROTO_DTLS)
5755 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005756 {
5757 *major = 255 - ver[0] + 2;
5758 *minor = 255 - ver[1] + 1;
5759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005761 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5762 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005763 else
5764#else
5765 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005766#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005767 {
5768 *major = ver[0];
5769 *minor = ver[1];
5770 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005771}
5772
Jerry Yue7047812021-09-13 19:26:39 +08005773/*
Jerry Yu3bf1f972021-09-22 21:37:18 +08005774 * Send pending fatal alert.
5775 * 0, No alert message.
5776 * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
5777 * returned, ssl->alert_reason otherwise.
Jerry Yue7047812021-09-13 19:26:39 +08005778 */
5779int mbedtls_ssl_handle_pending_alert( mbedtls_ssl_context *ssl )
5780{
5781 int ret;
5782
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005783 /* No pending alert, return success*/
5784 if( ssl->send_alert == 0 )
5785 return( 0 );
Jerry Yu394ece62021-09-14 22:17:21 +08005786
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005787 ret = mbedtls_ssl_send_alert_message( ssl,
5788 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5789 ssl->alert_type );
5790
Jerry Yu3bf1f972021-09-22 21:37:18 +08005791 /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
5792 * do not clear the alert to be able to send it later.
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005793 */
5794 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
5795 {
5796 ssl->send_alert = 0;
Jerry Yue7047812021-09-13 19:26:39 +08005797 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005798
5799 if( ret != 0 )
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005800 return( ret );
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005801
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005802 return( ssl->alert_reason );
Jerry Yue7047812021-09-13 19:26:39 +08005803}
5804
Jerry Yu394ece62021-09-14 22:17:21 +08005805/*
5806 * Set pending fatal alert flag.
5807 */
5808void mbedtls_ssl_pend_fatal_alert( mbedtls_ssl_context *ssl,
5809 unsigned char alert_type,
5810 int alert_reason )
5811{
5812 ssl->send_alert = 1;
5813 ssl->alert_type = alert_type;
5814 ssl->alert_reason = alert_reason;
5815}
5816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817#endif /* MBEDTLS_SSL_TLS_C */