blob: 46b9f0c00675217b0c86c7f9e55bc0033b4c1a5a [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{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100526 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000527 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100528 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100529 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000530 size_t post_avail;
531
532 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000533#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200534 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000535 ((void) ssl);
536#endif
537
538 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200539 * for CBC transformations in TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200540#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
TRodziewicz0f82ec62021-05-12 17:49:18 +0200541 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000542 ((void) f_rng);
543 ((void) p_rng);
544#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000548 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100549 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
552 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100553 if( rec == NULL
554 || rec->buf == NULL
555 || rec->buf_len < rec->data_offset
556 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100557#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100558 || rec->cid_len != 0
559#endif
560 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000561 {
562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100564 }
565
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000566 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100567 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000569 data, rec->data_len );
570
571 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
572
573 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
574 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
576 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000577 rec->data_len,
578 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
580 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100581
Hanno Becker92313402020-05-20 13:58:58 +0100582 /* The following two code paths implement the (D)TLSInnerPlaintext
583 * structure present in TLS 1.3 and DTLS 1.2 + CID.
584 *
585 * See ssl_build_inner_plaintext() for more information.
586 *
587 * Note that this changes `rec->data_len`, and hence
588 * `post_avail` needs to be recalculated afterwards.
589 *
590 * Note also that the two code paths cannot occur simultaneously
591 * since they apply to different versions of the protocol. There
592 * is hence no risk of double-addition of the inner plaintext.
593 */
Ronald Cron6f135e12021-12-08 16:57:54 +0100594#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerccc13d02020-05-04 12:30:04 +0100595 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
596 {
Hanno Becker13996922020-05-28 16:15:19 +0100597 size_t padding =
598 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200599 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100600 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100601 &rec->data_len,
602 post_avail,
603 rec->type,
604 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100605 {
606 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
607 }
608
609 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
610 }
Ronald Cron6f135e12021-12-08 16:57:54 +0100611#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100612
Hanno Beckera0e20d02019-05-15 14:03:01 +0100613#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100614 /*
615 * Add CID information
616 */
617 rec->cid_len = transform->out_cid_len;
618 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
619 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100620
621 if( rec->cid_len != 0 )
622 {
Hanno Becker13996922020-05-28 16:15:19 +0100623 size_t padding =
624 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200625 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100626 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100627 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100628 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100629 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100630 * Note that this changes `rec->data_len`, and hence
631 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100632 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100633 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100634 &rec->data_len,
635 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100636 rec->type,
637 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100638 {
639 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
640 }
641
642 rec->type = MBEDTLS_SSL_MSG_CID;
643 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100644#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100645
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100646 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
647
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100649 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000651#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 if( mode == MBEDTLS_MODE_STREAM ||
653 ( mode == MBEDTLS_MODE_CBC
654#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000655 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100656#endif
657 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000659 if( post_avail < transform->maclen )
660 {
661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
662 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
663 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200664#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +0200665 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker992b6872017-11-09 18:57:39 +0000667
TRodziewicz345165c2021-07-06 13:42:11 +0200668 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000669 transform->minor_ver,
670 transform->taglen );
Hanno Becker992b6872017-11-09 18:57:39 +0000671
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100672 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
673 add_data_len );
674 if( ret != 0 )
675 goto hmac_failed_etm_disabled;
676 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, data, rec->data_len );
677 if( ret != 0 )
678 goto hmac_failed_etm_disabled;
679 ret = mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
680 if( ret != 0 )
681 goto hmac_failed_etm_disabled;
682 ret = mbedtls_md_hmac_reset( &transform->md_ctx_enc );
683 if( ret != 0 )
684 goto hmac_failed_etm_disabled;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000685
TRodziewicz345165c2021-07-06 13:42:11 +0200686 memcpy( data + rec->data_len, mac, transform->maclen );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200687#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200688
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000689 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
690 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200691
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000692 rec->data_len += transform->maclen;
693 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100694 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100695
696 hmac_failed_etm_disabled:
Gilles Peskined5ba50e2021-12-10 21:33:21 +0100697 mbedtls_platform_zeroize( mac, transform->maclen );
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100698 if( ret != 0 )
699 {
700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_hmac_xxx", ret );
701 return( ret );
702 }
Paul Bakker577e0062013-08-28 11:57:20 +0200703 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000704#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200706 /*
707 * Encrypt
708 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000709#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000713 size_t olen;
Paul Elliottd48d5c62021-01-07 14:47:05 +0000714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000715 "including %d bytes of padding",
716 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000718 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
719 transform->iv_enc, transform->ivlen,
720 data, rec->data_len,
721 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200724 return( ret );
725 }
726
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000727 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200731 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000732 }
Paul Bakker68884e32013-01-07 18:20:04 +0100733 else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000734#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000735
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200736#if defined(MBEDTLS_GCM_C) || \
737 defined(MBEDTLS_CCM_C) || \
738 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200740 mode == MBEDTLS_MODE_CCM ||
741 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000742 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200744 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100745 unsigned char *dynamic_iv;
746 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100747 int dynamic_iv_is_explicit =
748 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000749
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100750 /* Check that there's space for the authentication tag. */
751 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000752 {
753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
754 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
755 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000756
Paul Bakker68884e32013-01-07 18:20:04 +0100757 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100758 * Build nonce for AEAD encryption.
759 *
760 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
761 * part of the IV is prepended to the ciphertext and
762 * can be chosen freely - in particular, it need not
763 * agree with the record sequence number.
764 * However, since ChaChaPoly as well as all AEAD modes
765 * in TLS 1.3 use the record sequence number as the
766 * dynamic part of the nonce, we uniformly use the
767 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100768 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100769 dynamic_iv = rec->ctr;
770 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200771
Hanno Becker17263802020-05-28 07:05:48 +0100772 ssl_build_record_nonce( iv, sizeof( iv ),
773 transform->iv_enc,
774 transform->fixed_ivlen,
775 dynamic_iv,
776 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100777
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100778 /*
779 * Build additional data for AEAD encryption.
780 * This depends on the TLS version.
781 */
782 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000783 transform->minor_ver,
784 transform->taglen );
Hanno Becker1f10d762019-04-26 13:34:37 +0100785
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200786 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100787 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200788 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100789 dynamic_iv,
790 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000791 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100792 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000793 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200794 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000795 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796
Paul Bakker68884e32013-01-07 18:20:04 +0100797 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200798 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200799 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000800
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100801 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000802 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100803 add_data, add_data_len,
804 data, rec->data_len, /* src */
805 data, rec->buf_len - (data - rec->buf), /* dst */
806 &rec->data_len,
807 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200808 {
TRodziewicz18efb732021-04-29 23:12:19 +0200809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200810 return( ret );
811 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000812 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100813 data + rec->data_len - transform->taglen,
814 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100815 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000816 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100817
818 /*
819 * Prefix record content with dynamic IV in case it is explicit.
820 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100821 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100822 {
823 if( rec->data_offset < dynamic_iv_len )
824 {
825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
826 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
827 }
828
829 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
830 rec->data_offset -= dynamic_iv_len;
831 rec->data_len += dynamic_iv_len;
832 }
833
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100834 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000835 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000836 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100837#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200838#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000840 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000842 size_t padlen, i;
843 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000844
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000845 /* Currently we're always using minimal padding
846 * (up to 255 bytes would be allowed). */
847 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
848 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 padlen = 0;
850
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000851 /* Check there's enough space in the buffer for the padding. */
852 if( post_avail < padlen + 1 )
853 {
854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
855 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
856 }
857
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000859 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000861 rec->data_len += padlen + 1;
862 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000863
TRodziewicz0f82ec62021-05-12 17:49:18 +0200864#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000865 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +0200866 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +0000867 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000868 */
TRodziewicz345165c2021-07-06 13:42:11 +0200869 if( f_rng == NULL )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000870 {
TRodziewicz345165c2021-07-06 13:42:11 +0200871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000873 }
TRodziewicz345165c2021-07-06 13:42:11 +0200874
875 if( rec->data_offset < transform->ivlen )
876 {
877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
878 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
879 }
880
881 /*
882 * Generate IV
883 */
884 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
885 if( ret != 0 )
886 return( ret );
887
888 memcpy( data - transform->ivlen, transform->iv_enc, transform->ivlen );
TRodziewicz0f82ec62021-05-12 17:49:18 +0200889#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000890
Paul Elliottd48d5c62021-01-07 14:47:05 +0000891 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
892 "including %" MBEDTLS_PRINTF_SIZET
893 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000894 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200895 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000896
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000897 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
898 transform->iv_enc,
899 transform->ivlen,
900 data, rec->data_len,
901 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200904 return( ret );
905 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200906
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000907 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200911 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200912
TRodziewicz0f82ec62021-05-12 17:49:18 +0200913 data -= transform->ivlen;
914 rec->data_offset -= transform->ivlen;
915 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100918 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100919 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000920 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
921
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100922 /*
923 * MAC(MAC_write_key, seq_num +
924 * TLSCipherText.type +
925 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100926 * length_of( (IV +) ENC(...) ) +
TRodziewicz2abf03c2021-06-25 14:40:09 +0200927 * IV +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100928 * ENC(content + padding + padding_length));
929 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000930
931 if( post_avail < transform->maclen)
932 {
933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
934 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
935 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100936
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100937 ssl_extract_add_data_from_record( add_data, &add_data_len,
Hanno Becker79e2d1b2021-03-22 11:42:19 +0000938 rec, transform->minor_ver,
939 transform->taglen );
Hanno Becker1f10d762019-04-26 13:34:37 +0100940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000942 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100943 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100944
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100945 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
946 add_data_len );
947 if( ret != 0 )
948 goto hmac_failed_etm_enabled;
949 ret = mbedtls_md_hmac_update( &transform->md_ctx_enc,
950 data, rec->data_len );
951 if( ret != 0 )
952 goto hmac_failed_etm_enabled;
953 ret = mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
954 if( ret != 0 )
955 goto hmac_failed_etm_enabled;
956 ret = mbedtls_md_hmac_reset( &transform->md_ctx_enc );
957 if( ret != 0 )
958 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100959
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000960 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100961
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000962 rec->data_len += transform->maclen;
963 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100964 auth_done++;
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100965
966 hmac_failed_etm_enabled:
Gilles Peskined5ba50e2021-12-10 21:33:21 +0100967 mbedtls_platform_zeroize( mac, transform->maclen );
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100968 if( ret != 0 )
969 {
970 MBEDTLS_SSL_DEBUG_RET( 1, "HMAC calculation failed", ret );
971 return( ret );
972 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100973 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200976 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200977#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200981 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000982
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100983 /* Make extra sure authentication was performed, exactly once */
984 if( auth_done != 1 )
985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
987 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100988 }
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000991
992 return( 0 );
993}
994
Hanno Becker605949f2019-07-12 08:23:59 +0100995int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +0000996 mbedtls_ssl_transform *transform,
997 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000998{
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000999 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001001 int ret, auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001002#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001003 size_t padlen = 0, correct = 1;
1004#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001005 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001006 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001007 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001008
Hanno Beckera18d1322018-01-03 14:27:32 +00001009#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001010 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001011 ((void) ssl);
1012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001015 if( rec == NULL ||
1016 rec->buf == NULL ||
1017 rec->buf_len < rec->data_offset ||
1018 rec->buf_len - rec->data_offset < rec->data_len )
1019 {
1020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001022 }
1023
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001024 data = rec->buf + rec->data_offset;
1025 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Hanno Beckera0e20d02019-05-15 14:03:01 +01001027#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001028 /*
1029 * Match record's CID with incoming CID.
1030 */
Hanno Becker938489a2019-05-08 13:02:22 +01001031 if( rec->cid_len != transform->in_cid_len ||
1032 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1033 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001034 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001035 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001036#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001037
Hanno Beckerd086bf02021-03-22 13:01:27 +00001038#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001040 {
1041 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001042 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1043 transform->iv_dec,
1044 transform->ivlen,
1045 data, rec->data_len,
1046 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001049 return( ret );
1050 }
1051
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001052 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001056 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 }
Paul Bakker68884e32013-01-07 18:20:04 +01001058 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001059#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001060#if defined(MBEDTLS_GCM_C) || \
1061 defined(MBEDTLS_CCM_C) || \
1062 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001064 mode == MBEDTLS_MODE_CCM ||
1065 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001066 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001067 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001068 unsigned char *dynamic_iv;
1069 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001070
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001071 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001072 * Extract dynamic part of nonce for AEAD decryption.
1073 *
1074 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1075 * part of the IV is prepended to the ciphertext and
1076 * can be chosen freely - in particular, it need not
1077 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001078 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001079 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001080 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001081 {
1082 if( rec->data_len < dynamic_iv_len )
1083 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1085 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001086 rec->data_len,
1087 dynamic_iv_len ) );
1088 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1089 }
1090 dynamic_iv = data;
1091
1092 data += dynamic_iv_len;
1093 rec->data_offset += dynamic_iv_len;
1094 rec->data_len -= dynamic_iv_len;
1095 }
Hanno Becker17263802020-05-28 07:05:48 +01001096 else
1097 {
1098 dynamic_iv = rec->ctr;
1099 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001100
1101 /* Check that there's space for the authentication tag. */
1102 if( rec->data_len < transform->taglen )
1103 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1105 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001106 rec->data_len,
1107 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001109 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001110 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001111
Hanno Beckerdf8be222020-05-21 15:30:57 +01001112 /*
1113 * Prepare nonce from dynamic and static parts.
1114 */
Hanno Becker17263802020-05-28 07:05:48 +01001115 ssl_build_record_nonce( iv, sizeof( iv ),
1116 transform->iv_dec,
1117 transform->fixed_ivlen,
1118 dynamic_iv,
1119 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001120
Hanno Beckerdf8be222020-05-21 15:30:57 +01001121 /*
1122 * Build additional data for AEAD encryption.
1123 * This depends on the TLS version.
1124 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001125 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001126 transform->minor_ver,
1127 transform->taglen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001128 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001129 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001130
Hanno Beckerd96a6522019-07-10 13:55:25 +01001131 /* Because of the check above, we know that there are
1132 * explicit_iv_len Bytes preceeding data, and taglen
1133 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001134 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001135 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001136
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001137 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001138 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001139 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001140
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001141 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001142 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001143 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001144 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001145 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001146 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001147 data, rec->data_len + transform->taglen, /* src */
1148 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001149 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001150 {
TRodziewicz18efb732021-04-29 23:12:19 +02001151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1154 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001155
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001156 return( ret );
1157 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001158 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001159
Hanno Beckerd96a6522019-07-10 13:55:25 +01001160 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001161 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1164 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001165 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001166 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001169#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001172 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001173
Paul Bakker5121ce52009-01-03 21:22:43 +00001174 /*
Paul Bakker45829992013-01-03 14:52:21 +01001175 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001177#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001178 /* The ciphertext is prefixed with the CBC IV. */
1179 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001180#endif
Paul Bakker45829992013-01-03 14:52:21 +01001181
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001182 /* Size considerations:
1183 *
1184 * - The CBC cipher text must not be empty and hence
1185 * at least of size transform->ivlen.
1186 *
1187 * Together with the potential IV-prefix, this explains
1188 * the first of the two checks below.
1189 *
1190 * - The record must contain a MAC, either in plain or
1191 * encrypted, depending on whether Encrypt-then-MAC
1192 * is used or not.
1193 * - If it is, the message contains the IV-prefix,
1194 * the CBC ciphertext, and the MAC.
1195 * - If it is not, the padded plaintext, and hence
1196 * the CBC ciphertext, has at least length maclen + 1
1197 * because there is at least the padding length byte.
1198 *
1199 * As the CBC ciphertext is not empty, both cases give the
1200 * lower bound minlen + maclen + 1 on the record size, which
1201 * we test for in the second check below.
1202 */
1203 if( rec->data_len < minlen + transform->ivlen ||
1204 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001205 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1207 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1208 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001209 "+ 1 ) ( + expl IV )", rec->data_len,
1210 transform->ivlen,
1211 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001213 }
1214
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001215 /*
1216 * Authenticate before decrypt if enabled
1217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001219 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001220 {
Hanno Becker992b6872017-11-09 18:57:39 +00001221 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001224
Hanno Beckerd96a6522019-07-10 13:55:25 +01001225 /* Update data_len in tandem with add_data.
1226 *
1227 * The subtraction is safe because of the previous check
1228 * data_len >= minlen + maclen + 1.
1229 *
1230 * Afterwards, we know that data + data_len is followed by at
1231 * least maclen Bytes, which justifies the call to
Gabor Mezei90437e32021-10-20 11:59:27 +02001232 * mbedtls_ct_memcmp() below.
Hanno Beckerd96a6522019-07-10 13:55:25 +01001233 *
1234 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001235 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001236 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001237 transform->minor_ver,
1238 transform->taglen );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001239
Hanno Beckerd96a6522019-07-10 13:55:25 +01001240 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001241 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1242 add_data_len );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001243 ret = mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1244 add_data_len );
1245 if( ret != 0 )
1246 goto hmac_failed_etm_enabled;
1247 ret = mbedtls_md_hmac_update( &transform->md_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001248 data, rec->data_len );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001249 if( ret != 0 )
1250 goto hmac_failed_etm_enabled;
1251 ret = mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1252 if( ret != 0 )
1253 goto hmac_failed_etm_enabled;
1254 ret = mbedtls_md_hmac_reset( &transform->md_ctx_dec );
1255 if( ret != 0 )
1256 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001257
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001258 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1259 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001260 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001261 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001262
Hanno Beckerd96a6522019-07-10 13:55:25 +01001263 /* Compare expected MAC with MAC at the end of the record. */
Gabor Mezei90437e32021-10-20 11:59:27 +02001264 if( mbedtls_ct_memcmp( data + rec->data_len, mac_expect,
gabor-mezei-arm46025642021-07-19 15:19:19 +02001265 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001268 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
1269 goto hmac_failed_etm_enabled;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001270 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001271 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001272
1273 hmac_failed_etm_enabled:
1274 mbedtls_platform_zeroize( mac_expect, transform->maclen );
1275 if( ret != 0 )
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001276 {
1277 if( ret != MBEDTLS_ERR_SSL_INVALID_MAC )
1278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_hmac_xxx", ret );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001279 return( ret );
Gilles Peskineecf6beb2021-12-10 21:35:10 +01001280 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001283
1284 /*
1285 * Check length sanity
1286 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001287
1288 /* We know from above that data_len > minlen >= 0,
1289 * so the following check in particular implies that
1290 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001291 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001292 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1294 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001295 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001297 }
1298
TRodziewicz0f82ec62021-05-12 17:49:18 +02001299#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001300 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001301 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 */
TRodziewicz345165c2021-07-06 13:42:11 +02001303 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
1304 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305
TRodziewicz345165c2021-07-06 13:42:11 +02001306 data += transform->ivlen;
1307 rec->data_offset += transform->ivlen;
1308 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001309#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001310
Hanno Beckerd96a6522019-07-10 13:55:25 +01001311 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1312
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001313 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1314 transform->iv_dec, transform->ivlen,
1315 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001318 return( ret );
1319 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001320
Hanno Beckerd96a6522019-07-10 13:55:25 +01001321 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001322 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001326 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001327
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001328 /* Safe since data_len >= minlen + maclen + 1, so after having
1329 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001330 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1331 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001332 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001333
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001334 if( auth_done == 1 )
1335 {
Gabor Mezei90437e32021-10-20 11:59:27 +02001336 const size_t mask = mbedtls_ct_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001337 rec->data_len,
1338 padlen + 1 );
1339 correct &= mask;
1340 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001341 }
1342 else
Paul Bakker45829992013-01-03 14:52:21 +01001343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001345 if( rec->data_len < transform->maclen + padlen + 1 )
1346 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1348 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1349 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001350 rec->data_len,
1351 transform->maclen,
1352 padlen + 1 ) );
1353 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001354#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001355
Gabor Mezei90437e32021-10-20 11:59:27 +02001356 const size_t mask = mbedtls_ct_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001357 rec->data_len,
1358 transform->maclen + padlen + 1 );
1359 correct &= mask;
1360 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001363 padlen++;
1364
1365 /* Regardless of the validity of the padding,
1366 * we have data_len >= padlen here. */
1367
TRodziewicz0f82ec62021-05-12 17:49:18 +02001368#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001369 /* The padding check involves a series of up to 256
1370 * consecutive memory reads at the end of the record
1371 * plaintext buffer. In order to hide the length and
1372 * validity of the padding, always perform exactly
1373 * `min(256,plaintext_len)` reads (but take into account
1374 * only the last `padlen` bytes for the padding check). */
1375 size_t pad_count = 0;
1376 volatile unsigned char* const check = data;
1377
1378 /* Index of first padding byte; it has been ensured above
1379 * that the subtraction is safe. */
1380 size_t const padding_idx = rec->data_len - padlen;
1381 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1382 size_t const start_idx = rec->data_len - num_checks;
1383 size_t idx;
1384
1385 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001387 /* pad_count += (idx >= padding_idx) &&
1388 * (check[idx] == padlen - 1);
1389 */
Gabor Mezei90437e32021-10-20 11:59:27 +02001390 const size_t mask = mbedtls_ct_size_mask_ge( idx, padding_idx );
1391 const size_t equal = mbedtls_ct_size_bool_eq( check[idx],
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001392 padlen - 1 );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001393 pad_count += mask & equal;
1394 }
Gabor Mezei90437e32021-10-20 11:59:27 +02001395 correct &= mbedtls_ct_size_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001398 if( padlen > 0 && correct == 0 )
1399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001400#endif
Gabor Mezei90437e32021-10-20 11:59:27 +02001401 padlen &= mbedtls_ct_size_mask( correct );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001402
TRodziewicz0f82ec62021-05-12 17:49:18 +02001403#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001404
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001405 /* If the padding was found to be invalid, padlen == 0
1406 * and the subtraction is safe. If the padding was found valid,
1407 * padlen hasn't been changed and the previous assertion
1408 * data_len >= padlen still holds. */
1409 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001410 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001411 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001412#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001418#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001420 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001421#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001422
1423 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001424 * Authenticate if not done yet.
1425 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001427#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001428 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001429 {
Hanno Becker992b6872017-11-09 18:57:39 +00001430 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001431 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001432
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001433 /* If the initial value of padlen was such that
1434 * data_len < maclen + padlen + 1, then padlen
1435 * got reset to 1, and the initial check
1436 * data_len >= minlen + maclen + 1
1437 * guarantees that at this point we still
1438 * have at least data_len >= maclen.
1439 *
1440 * If the initial value of padlen was such that
1441 * data_len >= maclen + padlen + 1, then we have
1442 * subtracted either padlen + 1 (if the padding was correct)
1443 * or 0 (if the padding was incorrect) since then,
1444 * hence data_len >= maclen in any case.
1445 */
1446 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001447 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
Hanno Becker79e2d1b2021-03-22 11:42:19 +00001448 transform->minor_ver,
1449 transform->taglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001450
TRodziewicz0f82ec62021-05-12 17:49:18 +02001451#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001452 /*
1453 * The next two sizes are the minimum and maximum values of
1454 * data_len over all padlen values.
1455 *
1456 * They're independent of padlen, since we previously did
1457 * data_len -= padlen.
1458 *
1459 * Note that max_len + maclen is never more than the buffer
1460 * length, as we previously did in_msglen -= maclen too.
1461 */
1462 const size_t max_len = rec->data_len + padlen;
1463 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1464
Gabor Mezei90437e32021-10-20 11:59:27 +02001465 ret = mbedtls_ct_hmac( &transform->md_ctx_dec,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001466 add_data, add_data_len,
1467 data, rec->data_len, min_len, max_len,
1468 mac_expect );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001469 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001470 {
Gabor Mezei90437e32021-10-20 11:59:27 +02001471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ct_hmac", ret );
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001472 goto hmac_failed_etm_disabled;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001473 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001474
Gabor Mezei90437e32021-10-20 11:59:27 +02001475 mbedtls_ct_memcpy_offset( mac_peer, data,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001476 rec->data_len,
1477 min_len, max_len,
1478 transform->maclen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001479#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001480
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001481#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001482 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001483 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001484#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001485
Gabor Mezei90437e32021-10-20 11:59:27 +02001486 if( mbedtls_ct_memcmp( mac_peer, mac_expect,
gabor-mezei-arm46025642021-07-19 15:19:19 +02001487 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_SSL_DEBUG_ALL)
1490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001491#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001492 correct = 0;
1493 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001494 auth_done++;
Gilles Peskined5ba50e2021-12-10 21:33:21 +01001495
1496 hmac_failed_etm_disabled:
1497 mbedtls_platform_zeroize( mac_peer, transform->maclen );
1498 mbedtls_platform_zeroize( mac_expect, transform->maclen );
1499 if( ret != 0 )
1500 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001501 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001502
1503 /*
1504 * Finally check the correct flag
1505 */
1506 if( correct == 0 )
1507 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001508#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001509
1510 /* Make extra sure authentication was performed, exactly once */
1511 if( auth_done != 1 )
1512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Ronald Cron6f135e12021-12-08 16:57:54 +01001517#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerccc13d02020-05-04 12:30:04 +01001518 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1519 {
1520 /* Remove inner padding and infer true content type. */
1521 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1522 &rec->type );
1523
1524 if( ret != 0 )
1525 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1526 }
Ronald Cron6f135e12021-12-08 16:57:54 +01001527#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerccc13d02020-05-04 12:30:04 +01001528
Hanno Beckera0e20d02019-05-15 14:03:01 +01001529#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001530 if( rec->cid_len != 0 )
1531 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001532 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1533 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001534 if( ret != 0 )
1535 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1536 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001537#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541 return( 0 );
1542}
1543
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001544#undef MAC_NONE
1545#undef MAC_PLAINTEXT
1546#undef MAC_CIPHERTEXT
1547
Paul Bakker5121ce52009-01-03 21:22:43 +00001548/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001549 * Fill the input message buffer by appending data to it.
1550 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001551 *
1552 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1553 * available (from this read and/or a previous one). Otherwise, an error code
1554 * is returned (possibly EOF or WANT_READ).
1555 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001556 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1557 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1558 * since we always read a whole datagram at once.
1559 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001560 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001561 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001564{
Janos Follath865b3eb2019-12-16 11:46:15 +00001565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001566 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001567#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1568 size_t in_buf_len = ssl->in_buf_len;
1569#else
1570 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001575 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001578 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001580 }
1581
Darryl Greenb33cc762019-11-28 14:29:44 +00001582 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1585 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001589 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001591 uint32_t timeout;
1592
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001593 /*
1594 * The point is, we need to always read a full datagram at once, so we
1595 * sometimes read more then requested, and handle the additional data.
1596 * It could be the rest of the current record (while fetching the
1597 * header) and/or some other records in the same datagram.
1598 */
1599
1600 /*
1601 * Move to the next record in the already read datagram if applicable
1602 */
1603 if( ssl->next_record_offset != 0 )
1604 {
1605 if( ssl->in_left < ssl->next_record_offset )
1606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001609 }
1610
1611 ssl->in_left -= ssl->next_record_offset;
1612
1613 if( ssl->in_left != 0 )
1614 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1616 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001617 ssl->next_record_offset ) );
1618 memmove( ssl->in_hdr,
1619 ssl->in_hdr + ssl->next_record_offset,
1620 ssl->in_left );
1621 }
1622
1623 ssl->next_record_offset = 0;
1624 }
1625
Paul Elliottd48d5c62021-01-07 14:47:05 +00001626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1627 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001629
1630 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001631 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001632 */
1633 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001636 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001637 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001638
1639 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001640 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001641 * are not at the beginning of a new record, the caller did something
1642 * wrong.
1643 */
1644 if( ssl->in_left != 0 )
1645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1647 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001648 }
1649
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001650 /*
1651 * Don't even try to read if time's out already.
1652 * This avoids by-passing the timer when repeatedly receiving messages
1653 * that will end up being dropped.
1654 */
Hanno Becker7876d122020-02-05 10:39:31 +00001655 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001656 {
1657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001658 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001659 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001660 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001661 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001662 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001665 timeout = ssl->handshake->retransmit_timeout;
1666 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001667 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001668
Paul Elliott9f352112020-12-09 14:55:45 +00001669 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001670
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001671 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001672 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1673 timeout );
1674 else
1675 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001678
1679 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001681 }
1682
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001683 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001686 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001689 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001690 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001693 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001694 }
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001699 return( ret );
1700 }
1701
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001702 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001703 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001705 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001707 {
Hanno Becker786300f2020-02-05 10:46:40 +00001708 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001709 {
Hanno Becker786300f2020-02-05 10:46:40 +00001710 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1711 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001712 return( ret );
1713 }
1714
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001715 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001716 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001718 }
1719
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 if( ret < 0 )
1721 return( ret );
1722
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001723 ssl->in_left = ret;
1724 }
1725 else
1726#endif
1727 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1729 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001730 ssl->in_left, nb_want ) );
1731
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001732 while( ssl->in_left < nb_want )
1733 {
1734 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001735
Hanno Becker7876d122020-02-05 10:39:31 +00001736 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001737 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1738 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001739 {
1740 if( ssl->f_recv_timeout != NULL )
1741 {
1742 ret = ssl->f_recv_timeout( ssl->p_bio,
1743 ssl->in_hdr + ssl->in_left, len,
1744 ssl->conf->read_timeout );
1745 }
1746 else
1747 {
1748 ret = ssl->f_recv( ssl->p_bio,
1749 ssl->in_hdr + ssl->in_left, len );
1750 }
1751 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001752
Paul Elliottd48d5c62021-01-07 14:47:05 +00001753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1754 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001755 ssl->in_left, nb_want ) );
1756 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001757
1758 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001760
1761 if( ret < 0 )
1762 return( ret );
1763
makise-homuraaf9513b2020-08-24 18:26:27 +03001764 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001765 {
Darryl Green11999bb2018-03-13 15:22:58 +00001766 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001767 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001768 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001769 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1770 }
1771
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001772 ssl->in_left += ret;
1773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001774 }
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
1778 return( 0 );
1779}
1780
1781/*
1782 * Flush any data not yet written
1783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785{
Janos Follath865b3eb2019-12-16 11:46:15 +00001786 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001787 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001791 if( ssl->f_send == NULL )
1792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001794 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001796 }
1797
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001798 /* Avoid incrementing counter if data is flushed */
1799 if( ssl->out_left == 0 )
1800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001802 return( 0 );
1803 }
1804
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 while( ssl->out_left > 0 )
1806 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
1808 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01001809 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Hanno Becker2b1e3542018-08-06 11:19:13 +01001811 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001812 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
1816 if( ret <= 0 )
1817 return( ret );
1818
makise-homuraaf9513b2020-08-24 18:26:27 +03001819 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08001820 {
Darryl Green11999bb2018-03-13 15:22:58 +00001821 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001822 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00001823 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08001824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1825 }
1826
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 ssl->out_left -= ret;
1828 }
1829
Hanno Becker2b1e3542018-08-06 11:19:13 +01001830#if defined(MBEDTLS_SSL_PROTO_DTLS)
1831 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001832 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01001833 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001834 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01001835 else
1836#endif
1837 {
1838 ssl->out_hdr = ssl->out_buf + 8;
1839 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001840 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
1844 return( 0 );
1845}
1846
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001847/*
1848 * Functions to handle the DTLS retransmission state machine
1849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001851/*
1852 * Append current handshake message to current outgoing flight
1853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001855{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01001857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
1858 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
1859 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001860
1861 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001862 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001863 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001866 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001867 }
1868
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001869 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001870 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
1872 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001874 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001875 }
1876
1877 /* Copy current handshake message with headers */
1878 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
1879 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001880 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001881 msg->next = NULL;
1882
1883 /* Append to the current flight */
1884 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001885 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001886 else
1887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001889 while( cur->next != NULL )
1890 cur = cur->next;
1891 cur->next = msg;
1892 }
1893
Hanno Becker3b235902018-08-06 09:54:53 +01001894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001895 return( 0 );
1896}
1897
1898/*
1899 * Free the current flight of handshake messages
1900 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00001901void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 mbedtls_ssl_flight_item *cur = flight;
1904 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001905
1906 while( cur != NULL )
1907 {
1908 next = cur->next;
1909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 mbedtls_free( cur->p );
1911 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001912
1913 cur = next;
1914 }
1915}
1916
1917/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001918 * Swap transform_out and out_ctr with the alternative ones
1919 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001920static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_ssl_transform *tmp_transform;
Jerry Yuae0b2e22021-10-08 15:21:19 +08001923 unsigned char tmp_out_ctr[MBEDTLS_SSL_SEQUENCE_NUMBER_LEN];
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001924
1925 if( ssl->transform_out == ssl->handshake->alt_transform_out )
1926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001928 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001929 }
1930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001932
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001933 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001934 tmp_transform = ssl->transform_out;
1935 ssl->transform_out = ssl->handshake->alt_transform_out;
1936 ssl->handshake->alt_transform_out = tmp_transform;
1937
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001938 /* Swap epoch + sequence_number */
Jerry Yud96a5c22021-09-29 17:46:51 +08001939 memcpy( tmp_out_ctr, ssl->cur_out_ctr, sizeof( tmp_out_ctr ) );
1940 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr,
1941 sizeof( ssl->cur_out_ctr ) );
1942 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr,
1943 sizeof( ssl->handshake->alt_out_ctr ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001944
1945 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001946 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001947
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001948 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001949}
1950
1951/*
1952 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001953 */
1954int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
1955{
1956 int ret = 0;
1957
1958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
1959
1960 ret = mbedtls_ssl_flight_transmit( ssl );
1961
1962 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
1963
1964 return( ret );
1965}
1966
1967/*
1968 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001969 *
1970 * Need to remember the current message in case flush_output returns
1971 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001972 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001973 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001974int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001975{
Janos Follath865b3eb2019-12-16 11:46:15 +00001976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001980 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02001981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001982
1983 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001984 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001985 ret = ssl_swap_epochs( ssl );
1986 if( ret != 0 )
1987 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001989 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001990 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001991
1992 while( ssl->handshake->cur_msg != NULL )
1993 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01001994 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001995 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01001996
Hanno Beckere1dcb032018-08-17 16:47:58 +01001997 int const is_finished =
1998 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
1999 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2000
Hanno Becker04da1892018-08-14 13:22:10 +01002001 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2002 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2003
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002004 /* Swap epochs before sending Finished: we can't do it after
2005 * sending ChangeCipherSpec, in case write returns WANT_READ.
2006 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002007 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002008 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002009 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002010 ret = ssl_swap_epochs( ssl );
2011 if( ret != 0 )
2012 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002013 }
2014
Hanno Becker67bc7c32018-08-06 11:33:50 +01002015 ret = ssl_get_remaining_payload_in_datagram( ssl );
2016 if( ret < 0 )
2017 return( ret );
2018 max_frag_len = (size_t) ret;
2019
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002020 /* CCS is copied as is, while HS messages may need fragmentation */
2021 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2022 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002023 if( max_frag_len == 0 )
2024 {
2025 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2026 return( ret );
2027
2028 continue;
2029 }
2030
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002031 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002032 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002033 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002034
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002035 /* Update position inside current message */
2036 ssl->handshake->cur_msg_p += cur->len;
2037 }
2038 else
2039 {
2040 const unsigned char * const p = ssl->handshake->cur_msg_p;
2041 const size_t hs_len = cur->len - 12;
2042 const size_t frag_off = p - ( cur->p + 12 );
2043 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002044 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002045
Hanno Beckere1dcb032018-08-17 16:47:58 +01002046 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002047 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002048 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002049 {
2050 ret = ssl_swap_epochs( ssl );
2051 if( ret != 0 )
2052 return( ret );
2053 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002054
Hanno Becker67bc7c32018-08-06 11:33:50 +01002055 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2056 return( ret );
2057
2058 continue;
2059 }
2060 max_hs_frag_len = max_frag_len - 12;
2061
2062 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2063 max_hs_frag_len : rem_len;
2064
2065 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002066 {
2067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002068 (unsigned) cur_hs_frag_len,
2069 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002070 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002071
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002072 /* Messages are stored with handshake headers as if not fragmented,
2073 * copy beginning of headers then fill fragmentation fields.
2074 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2075 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002076
Joe Subbiani5ecac212021-06-24 13:00:03 +01002077 ssl->out_msg[6] = MBEDTLS_BYTE_2( frag_off );
2078 ssl->out_msg[7] = MBEDTLS_BYTE_1( frag_off );
2079 ssl->out_msg[8] = MBEDTLS_BYTE_0( frag_off );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002080
Joe Subbiani5ecac212021-06-24 13:00:03 +01002081 ssl->out_msg[ 9] = MBEDTLS_BYTE_2( cur_hs_frag_len );
2082 ssl->out_msg[10] = MBEDTLS_BYTE_1( cur_hs_frag_len );
2083 ssl->out_msg[11] = MBEDTLS_BYTE_0( cur_hs_frag_len );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002084
2085 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2086
Hanno Becker3f7b9732018-08-28 09:53:25 +01002087 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002088 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2089 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002090 ssl->out_msgtype = cur->type;
2091
2092 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002093 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002094 }
2095
2096 /* If done with the current message move to the next one if any */
2097 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2098 {
2099 if( cur->next != NULL )
2100 {
2101 ssl->handshake->cur_msg = cur->next;
2102 ssl->handshake->cur_msg_p = cur->next->p + 12;
2103 }
2104 else
2105 {
2106 ssl->handshake->cur_msg = NULL;
2107 ssl->handshake->cur_msg_p = NULL;
2108 }
2109 }
2110
2111 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002112 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002115 return( ret );
2116 }
2117 }
2118
Hanno Becker67bc7c32018-08-06 11:33:50 +01002119 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2120 return( ret );
2121
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002122 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2124 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002125 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002128 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002129 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002130
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002132
2133 return( 0 );
2134}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002135
2136/*
2137 * To be called when the last message of an incoming flight is received.
2138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002139void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002140{
2141 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002142 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002143 ssl->handshake->flight = NULL;
2144 ssl->handshake->cur_msg = NULL;
2145
2146 /* The next incoming flight will start with this msg_seq */
2147 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2148
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002149 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002150 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002151
Hanno Becker0271f962018-08-16 13:23:47 +01002152 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002153 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002154
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002155 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002156 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2159 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002162 }
2163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002165}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002166
2167/*
2168 * To be called when the last message of an outgoing flight is send.
2169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002171{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002172 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002173 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002175 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2176 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002179 }
2180 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002181 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002184
Paul Bakker5121ce52009-01-03 21:22:43 +00002185/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002186 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002188
2189/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002190 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002191 *
2192 * - fill in handshake headers
2193 * - update handshake checksum
2194 * - DTLS: save message for resending
2195 * - then pass to the record layer
2196 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002197 * DTLS: except for HelloRequest, messages are only queued, and will only be
2198 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002199 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002200 * Inputs:
2201 * - ssl->out_msglen: 4 + actual handshake message len
2202 * (4 is the size of handshake headers for TLS)
2203 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2204 * - ssl->out_msg + 4: the handshake message body
2205 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002206 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002207 * - ssl->out_msglen: the length of the record contents
2208 * (including handshake headers but excluding record headers)
2209 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002210 */
Hanno Beckerf3cce8b2021-08-07 14:29:49 +01002211int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl,
2212 int update_checksum )
Paul Bakker5121ce52009-01-03 21:22:43 +00002213{
Janos Follath865b3eb2019-12-16 11:46:15 +00002214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002215 const size_t hs_len = ssl->out_msglen - 4;
2216 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002217
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2219
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002220 /*
2221 * Sanity checks
2222 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002223 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002224 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2225 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2227 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002228 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002230 /* Whenever we send anything different from a
2231 * HelloRequest we should be in a handshake - double check. */
2232 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2233 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002234 ssl->handshake == NULL )
2235 {
2236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002242 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002244 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002245 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002247 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002248#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002249
Hanno Beckerb50a2532018-08-06 11:52:54 +01002250 /* Double-check that we did not exceed the bounds
2251 * of the outgoing record buffer.
2252 * This should never fail as the various message
2253 * writing functions must obey the bounds of the
2254 * outgoing record buffer, but better be safe.
2255 *
2256 * Note: We deliberately do not check for the MTU or MFL here.
2257 */
2258 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2259 {
2260 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002261 "size %" MBEDTLS_PRINTF_SIZET
2262 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002263 ssl->out_msglen,
2264 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2266 }
2267
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002268 /*
2269 * Fill handshake headers
2270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
Joe Subbianifbeb6922021-07-16 14:27:50 +01002273 ssl->out_msg[1] = MBEDTLS_BYTE_2( hs_len );
2274 ssl->out_msg[2] = MBEDTLS_BYTE_1( hs_len );
2275 ssl->out_msg[3] = MBEDTLS_BYTE_0( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002277 /*
2278 * DTLS has additional fields in the Handshake layer,
2279 * between the length field and the actual payload:
2280 * uint16 message_seq;
2281 * uint24 fragment_offset;
2282 * uint24 fragment_length;
2283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002286 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002287 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002288 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002289 {
2290 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002291 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002292 hs_len,
2293 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002294 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2295 }
2296
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002297 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002298 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002299
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002300 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002301 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002302 {
Joe Subbiani6dd73642021-07-19 11:56:54 +01002303 MBEDTLS_PUT_UINT16_BE( ssl->handshake->out_msg_seq, ssl->out_msg, 4 );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002304 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002305 }
2306 else
2307 {
2308 ssl->out_msg[4] = 0;
2309 ssl->out_msg[5] = 0;
2310 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002311
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002312 /* Handshake hashes are computed without fragmentation,
2313 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002314 memset( ssl->out_msg + 6, 0x00, 3 );
2315 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002318
Hanno Becker0207e532018-08-28 10:28:28 +01002319 /* Update running hashes of handshake messages seen */
Hanno Beckerf3cce8b2021-08-07 14:29:49 +01002320 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST && update_checksum != 0 )
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002321 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 }
2323
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002324 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002326 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002327 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2328 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002329 {
2330 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002333 return( ret );
2334 }
2335 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002336 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002337#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002338 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002339 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002340 {
2341 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2342 return( ret );
2343 }
2344 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002345
2346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2347
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002348 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002349}
2350
2351/*
2352 * Record layer functions
2353 */
2354
2355/*
2356 * Write current record.
2357 *
2358 * Uses:
2359 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2360 * - ssl->out_msglen: length of the record content (excl headers)
2361 * - ssl->out_msg: record content
2362 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002363int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002364{
2365 int ret, done = 0;
2366 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002367 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002368
2369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002370
Paul Bakker05ef8352012-05-08 09:17:57 +00002371 if( !done )
2372 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002373 unsigned i;
2374 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002375#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2376 size_t out_buf_len = ssl->out_buf_len;
2377#else
2378 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2379#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002380 /* Skip writing the record content type to after the encryption,
2381 * as it may change when using the CID extension. */
Jerry Yu47413c22021-10-29 17:19:41 +08002382 int minor_ver = ssl->minor_ver;
Ronald Cron6f135e12021-12-08 16:57:54 +01002383#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu1ca80f72021-11-08 10:30:54 +08002384 /* TLS 1.3 still uses the TLS 1.2 version identifier
2385 * for backwards compatibility. */
Jerry Yu47413c22021-10-29 17:19:41 +08002386 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
2387 minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Ronald Cron6f135e12021-12-08 16:57:54 +01002388#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu47413c22021-10-29 17:19:41 +08002389 mbedtls_ssl_write_version( ssl->major_ver, minor_ver,
2390 ssl->conf->transport, ssl->out_hdr + 1 );
Hanno Becker6430faf2019-05-08 11:57:13 +01002391
Jerry Yuae0b2e22021-10-08 15:21:19 +08002392 memcpy( ssl->out_ctr, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Joe Subbiani6dd73642021-07-19 11:56:54 +01002393 MBEDTLS_PUT_UINT16_BE( len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002394
Paul Bakker48916f92012-09-16 19:57:18 +00002395 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002396 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002397 mbedtls_record rec;
2398
2399 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002400 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002401 rec.data_len = ssl->out_msglen;
2402 rec.data_offset = ssl->out_msg - rec.buf;
2403
Jerry Yud96a5c22021-09-29 17:46:51 +08002404 memcpy( &rec.ctr[0], ssl->out_ctr, sizeof( rec.ctr ) );
Jerry Yu47413c22021-10-29 17:19:41 +08002405 mbedtls_ssl_write_version( ssl->major_ver, minor_ver,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 ssl->conf->transport, rec.ver );
2407 rec.type = ssl->out_msgtype;
2408
Hanno Beckera0e20d02019-05-15 14:03:01 +01002409#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002410 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002411 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002412#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002413
Hanno Beckera18d1322018-01-03 14:27:32 +00002414 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002415 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002418 return( ret );
2419 }
2420
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002421 if( rec.data_offset != 0 )
2422 {
2423 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2424 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2425 }
2426
Hanno Becker6430faf2019-05-08 11:57:13 +01002427 /* Update the record content type and CID. */
2428 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002429#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002430 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002432 ssl->out_msglen = len = rec.data_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01002433 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->out_len, 0 );
Paul Bakker05ef8352012-05-08 09:17:57 +00002434 }
2435
Hanno Becker5903de42019-05-03 14:46:38 +01002436 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002437
2438#if defined(MBEDTLS_SSL_PROTO_DTLS)
2439 /* In case of DTLS, double-check that we don't exceed
2440 * the remaining space in the datagram. */
2441 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2442 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002443 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002444 if( ret < 0 )
2445 return( ret );
2446
2447 if( protected_record_size > (size_t) ret )
2448 {
2449 /* Should never happen */
2450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2451 }
2452 }
2453#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002454
Hanno Becker6430faf2019-05-08 11:57:13 +01002455 /* Now write the potentially updated record content type. */
2456 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2457
Paul Elliott9f352112020-12-09 14:55:45 +00002458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002459 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002460 ssl->out_hdr[0], ssl->out_hdr[1],
2461 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002463 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002464 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002465
2466 ssl->out_left += protected_record_size;
2467 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002468 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002469
Hanno Beckerdd772292020-02-05 10:38:31 +00002470 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002471 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2472 break;
2473
2474 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002475 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002476 {
2477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2478 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2479 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 }
2481
Hanno Becker67bc7c32018-08-06 11:33:50 +01002482#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002483 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2484 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002485 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002486 size_t remaining;
2487 ret = ssl_get_remaining_payload_in_datagram( ssl );
2488 if( ret < 0 )
2489 {
2490 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2491 ret );
2492 return( ret );
2493 }
2494
2495 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002496 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002497 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002498 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002499 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002500 else
2501 {
Hanno Becker513815a2018-08-20 11:56:09 +01002502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002503 }
2504 }
2505#endif /* MBEDTLS_SSL_PROTO_DTLS */
2506
2507 if( ( flush == SSL_FORCE_FLUSH ) &&
2508 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 return( ret );
2512 }
2513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
2516 return( 0 );
2517}
2518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002520
2521static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2522{
2523 if( ssl->in_msglen < ssl->in_hslen ||
2524 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2525 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2526 {
2527 return( 1 );
2528 }
2529 return( 0 );
2530}
Hanno Becker44650b72018-08-16 12:51:11 +01002531
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002532static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002533{
2534 return( ( ssl->in_msg[9] << 16 ) |
2535 ( ssl->in_msg[10] << 8 ) |
2536 ssl->in_msg[11] );
2537}
2538
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002539static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002540{
2541 return( ( ssl->in_msg[6] << 16 ) |
2542 ( ssl->in_msg[7] << 8 ) |
2543 ssl->in_msg[8] );
2544}
2545
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002546static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002547{
2548 uint32_t msg_len, frag_off, frag_len;
2549
2550 msg_len = ssl_get_hs_total_len( ssl );
2551 frag_off = ssl_get_hs_frag_off( ssl );
2552 frag_len = ssl_get_hs_frag_len( ssl );
2553
2554 if( frag_off > msg_len )
2555 return( -1 );
2556
2557 if( frag_len > msg_len - frag_off )
2558 return( -1 );
2559
2560 if( frag_len + 12 > ssl->in_msglen )
2561 return( -1 );
2562
2563 return( 0 );
2564}
2565
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002566/*
2567 * Mark bits in bitmask (used for DTLS HS reassembly)
2568 */
2569static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2570{
2571 unsigned int start_bits, end_bits;
2572
2573 start_bits = 8 - ( offset % 8 );
2574 if( start_bits != 8 )
2575 {
2576 size_t first_byte_idx = offset / 8;
2577
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002578 /* Special case */
2579 if( len <= start_bits )
2580 {
2581 for( ; len != 0; len-- )
2582 mask[first_byte_idx] |= 1 << ( start_bits - len );
2583
2584 /* Avoid potential issues with offset or len becoming invalid */
2585 return;
2586 }
2587
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002588 offset += start_bits; /* Now offset % 8 == 0 */
2589 len -= start_bits;
2590
2591 for( ; start_bits != 0; start_bits-- )
2592 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2593 }
2594
2595 end_bits = len % 8;
2596 if( end_bits != 0 )
2597 {
2598 size_t last_byte_idx = ( offset + len ) / 8;
2599
2600 len -= end_bits; /* Now len % 8 == 0 */
2601
2602 for( ; end_bits != 0; end_bits-- )
2603 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2604 }
2605
2606 memset( mask + offset / 8, 0xFF, len / 8 );
2607}
2608
2609/*
2610 * Check that bitmask is full
2611 */
2612static int ssl_bitmask_check( unsigned char *mask, size_t len )
2613{
2614 size_t i;
2615
2616 for( i = 0; i < len / 8; i++ )
2617 if( mask[i] != 0xFF )
2618 return( -1 );
2619
2620 for( i = 0; i < len % 8; i++ )
2621 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2622 return( -1 );
2623
2624 return( 0 );
2625}
2626
Hanno Becker56e205e2018-08-16 09:06:12 +01002627/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002628static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002629 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002630{
Hanno Becker56e205e2018-08-16 09:06:12 +01002631 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002632
Hanno Becker56e205e2018-08-16 09:06:12 +01002633 alloc_len = 12; /* Handshake header */
2634 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002635
Hanno Beckerd07df862018-08-16 09:14:58 +01002636 if( add_bitmap )
2637 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002638
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002639 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002640}
Hanno Becker56e205e2018-08-16 09:06:12 +01002641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002642#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002643
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002644static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002645{
2646 return( ( ssl->in_msg[1] << 16 ) |
2647 ( ssl->in_msg[2] << 8 ) |
2648 ssl->in_msg[3] );
2649}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002650
Simon Butcher99000142016-10-13 17:21:01 +01002651int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002652{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002654 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002656 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002658 }
2659
Hanno Becker12555c62018-08-16 12:47:53 +01002660 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002663 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002664 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002668 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002670 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002671
Hanno Becker44650b72018-08-16 12:51:11 +01002672 if( ssl_check_hs_header( ssl ) != 0 )
2673 {
2674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2675 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2676 }
2677
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002678 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002679 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2680 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2681 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2682 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002683 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002684 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2685 {
2686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2687 recv_msg_seq,
2688 ssl->handshake->in_msg_seq ) );
2689 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2690 }
2691
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002692 /* Retransmit only on last message from previous flight, to avoid
2693 * too many retransmissions.
2694 * Besides, No sane server ever retransmits HelloVerifyRequest */
2695 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002699 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002700 recv_msg_seq,
2701 ssl->handshake->in_flight_start_seq ) );
2702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002706 return( ret );
2707 }
2708 }
2709 else
2710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002712 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002713 recv_msg_seq,
2714 ssl->handshake->in_msg_seq ) );
2715 }
2716
Hanno Becker90333da2017-10-10 11:27:13 +01002717 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002718 }
2719 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002720
Hanno Becker6d97ef52018-08-16 13:09:04 +01002721 /* Message reassembly is handled alongside buffering of future
2722 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002723 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002724 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002725 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002728 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002729 }
2730 }
2731 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002733 /* With TLS we don't handle fragmentation (for now) */
2734 if( ssl->in_msglen < ssl->in_hslen )
2735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2737 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002738 }
2739
Simon Butcher99000142016-10-13 17:21:01 +01002740 return( 0 );
2741}
2742
2743void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2744{
Hanno Becker0271f962018-08-16 13:23:47 +01002745 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002746
Hanno Becker0271f962018-08-16 13:23:47 +01002747 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002748 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002749 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002750 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002751
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002752 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002753#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002754 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002755 ssl->handshake != NULL )
2756 {
Hanno Becker0271f962018-08-16 13:23:47 +01002757 unsigned offset;
2758 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002759
Hanno Becker0271f962018-08-16 13:23:47 +01002760 /* Increment handshake sequence number */
2761 hs->in_msg_seq++;
2762
2763 /*
2764 * Clear up handshake buffering and reassembly structure.
2765 */
2766
2767 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002768 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002769
2770 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002771 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2772 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002773 offset++, hs_buf++ )
2774 {
2775 *hs_buf = *(hs_buf + 1);
2776 }
2777
2778 /* Create a fresh last entry */
2779 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002780 }
2781#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002782}
2783
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002784/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002785 * DTLS anti-replay: RFC 6347 4.1.2.6
2786 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002787 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2788 * Bit n is set iff record number in_window_top - n has been seen.
2789 *
2790 * Usually, in_window_top is the last record number seen and the lsb of
2791 * in_window is set. The only exception is the initial state (record number 0
2792 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002795void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002796{
2797 ssl->in_window_top = 0;
2798 ssl->in_window = 0;
2799}
2800
2801static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2802{
2803 return( ( (uint64_t) buf[0] << 40 ) |
2804 ( (uint64_t) buf[1] << 32 ) |
2805 ( (uint64_t) buf[2] << 24 ) |
2806 ( (uint64_t) buf[3] << 16 ) |
2807 ( (uint64_t) buf[4] << 8 ) |
2808 ( (uint64_t) buf[5] ) );
2809}
2810
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002811static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
2812{
Janos Follath865b3eb2019-12-16 11:46:15 +00002813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002814 unsigned char *original_in_ctr;
2815
2816 // save original in_ctr
2817 original_in_ctr = ssl->in_ctr;
2818
2819 // use counter from record
2820 ssl->in_ctr = record_in_ctr;
2821
2822 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
2823
2824 // restore the counter
2825 ssl->in_ctr = original_in_ctr;
2826
2827 return ret;
2828}
2829
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002830/*
2831 * Return 0 if sequence number is acceptable, -1 otherwise
2832 */
Hanno Becker0183d692019-07-12 08:50:37 +01002833int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002834{
2835 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2836 uint64_t bit;
2837
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002838 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002839 return( 0 );
2840
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002841 if( rec_seqnum > ssl->in_window_top )
2842 return( 0 );
2843
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002844 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002845
2846 if( bit >= 64 )
2847 return( -1 );
2848
2849 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2850 return( -1 );
2851
2852 return( 0 );
2853}
2854
2855/*
2856 * Update replay window on new validated record
2857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002859{
2860 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2861
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002862 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002863 return;
2864
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002865 if( rec_seqnum > ssl->in_window_top )
2866 {
2867 /* Update window_top and the contents of the window */
2868 uint64_t shift = rec_seqnum - ssl->in_window_top;
2869
2870 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002871 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002872 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002873 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002874 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002875 ssl->in_window |= 1;
2876 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002877
2878 ssl->in_window_top = rec_seqnum;
2879 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002880 else
2881 {
2882 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002883 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002884
2885 if( bit < 64 ) /* Always true, but be extra sure */
2886 ssl->in_window |= (uint64_t) 1 << bit;
2887 }
2888}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002890
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02002891#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002892/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002893 * Without any SSL context, check if a datagram looks like a ClientHello with
2894 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01002895 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002896 *
2897 * - if cookie is valid, return 0
2898 * - if ClientHello looks superficially valid but cookie is not,
2899 * fill obuf and set olen, then
2900 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
2901 * - otherwise return a specific error code
2902 */
2903static int ssl_check_dtls_clihlo_cookie(
2904 mbedtls_ssl_cookie_write_t *f_cookie_write,
2905 mbedtls_ssl_cookie_check_t *f_cookie_check,
2906 void *p_cookie,
2907 const unsigned char *cli_id, size_t cli_id_len,
2908 const unsigned char *in, size_t in_len,
2909 unsigned char *obuf, size_t buf_len, size_t *olen )
2910{
2911 size_t sid_len, cookie_len;
2912 unsigned char *p;
2913
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002914 /*
2915 * Structure of ClientHello with record and handshake headers,
2916 * and expected values. We don't need to check a lot, more checks will be
2917 * done when actually parsing the ClientHello - skipping those checks
2918 * avoids code duplication and does not make cookie forging any easier.
2919 *
2920 * 0-0 ContentType type; copied, must be handshake
2921 * 1-2 ProtocolVersion version; copied
2922 * 3-4 uint16 epoch; copied, must be 0
2923 * 5-10 uint48 sequence_number; copied
2924 * 11-12 uint16 length; (ignored)
2925 *
2926 * 13-13 HandshakeType msg_type; (ignored)
2927 * 14-16 uint24 length; (ignored)
2928 * 17-18 uint16 message_seq; copied
2929 * 19-21 uint24 fragment_offset; copied, must be 0
2930 * 22-24 uint24 fragment_length; (ignored)
2931 *
2932 * 25-26 ProtocolVersion client_version; (ignored)
2933 * 27-58 Random random; (ignored)
2934 * 59-xx SessionID session_id; 1 byte len + sid_len content
2935 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
2936 * ...
2937 *
2938 * Minimum length is 61 bytes.
2939 */
2940 if( in_len < 61 ||
2941 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
2942 in[3] != 0 || in[4] != 0 ||
2943 in[19] != 0 || in[20] != 0 || in[21] != 0 )
2944 {
Hanno Becker90d59dd2021-06-24 11:17:13 +01002945 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002946 }
2947
2948 sid_len = in[59];
2949 if( sid_len > in_len - 61 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01002950 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002951
2952 cookie_len = in[60 + sid_len];
2953 if( cookie_len > in_len - 60 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01002954 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002955
2956 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
2957 cli_id, cli_id_len ) == 0 )
2958 {
2959 /* Valid cookie */
2960 return( 0 );
2961 }
2962
2963 /*
2964 * If we get here, we've got an invalid cookie, let's prepare HVR.
2965 *
2966 * 0-0 ContentType type; copied
2967 * 1-2 ProtocolVersion version; copied
2968 * 3-4 uint16 epoch; copied
2969 * 5-10 uint48 sequence_number; copied
2970 * 11-12 uint16 length; olen - 13
2971 *
2972 * 13-13 HandshakeType msg_type; hello_verify_request
2973 * 14-16 uint24 length; olen - 25
2974 * 17-18 uint16 message_seq; copied
2975 * 19-21 uint24 fragment_offset; copied
2976 * 22-24 uint24 fragment_length; olen - 25
2977 *
2978 * 25-26 ProtocolVersion server_version; 0xfe 0xff
2979 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
2980 *
2981 * Minimum length is 28.
2982 */
2983 if( buf_len < 28 )
2984 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2985
2986 /* Copy most fields and adapt others */
2987 memcpy( obuf, in, 25 );
2988 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
2989 obuf[25] = 0xfe;
2990 obuf[26] = 0xff;
2991
2992 /* Generate and write actual cookie */
2993 p = obuf + 28;
2994 if( f_cookie_write( p_cookie,
2995 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
2996 {
2997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2998 }
2999
3000 *olen = p - obuf;
3001
3002 /* Go back and fill length fields */
3003 obuf[27] = (unsigned char)( *olen - 28 );
3004
Joe Subbianifbeb6922021-07-16 14:27:50 +01003005 obuf[14] = obuf[22] = MBEDTLS_BYTE_2( *olen - 25 );
3006 obuf[15] = obuf[23] = MBEDTLS_BYTE_1( *olen - 25 );
3007 obuf[16] = obuf[24] = MBEDTLS_BYTE_0( *olen - 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003008
Joe Subbiani6dd73642021-07-19 11:56:54 +01003009 MBEDTLS_PUT_UINT16_BE( *olen - 13, obuf, 11 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003010
3011 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3012}
3013
3014/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003015 * Handle possible client reconnect with the same UDP quadruplet
3016 * (RFC 6347 Section 4.2.8).
3017 *
3018 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3019 * that looks like a ClientHello.
3020 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003021 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003022 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003023 * - if the input looks like a ClientHello with a valid cookie,
3024 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003025 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003026 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003027 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003028 * This function is called (through ssl_check_client_reconnect()) when an
3029 * unexpected record is found in ssl_get_next_record(), which will discard the
3030 * record if we return 0, and bubble up the return value otherwise (this
3031 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3032 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003033 */
3034static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3035{
Janos Follath865b3eb2019-12-16 11:46:15 +00003036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003037 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003038
Hanno Becker2fddd372019-07-10 14:37:41 +01003039 if( ssl->conf->f_cookie_write == NULL ||
3040 ssl->conf->f_cookie_check == NULL )
3041 {
3042 /* If we can't use cookies to verify reachability of the peer,
3043 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003044 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3045 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003046 return( 0 );
3047 }
3048
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003049 ret = ssl_check_dtls_clihlo_cookie(
3050 ssl->conf->f_cookie_write,
3051 ssl->conf->f_cookie_check,
3052 ssl->conf->p_cookie,
3053 ssl->cli_id, ssl->cli_id_len,
3054 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003055 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003056
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003057 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3058
3059 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003060 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003061 int send_ret;
3062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3063 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3064 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003065 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003066 * If the error is permanent we'll catch it later,
3067 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003068 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3069 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3070 (void) send_ret;
3071
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003072 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003073 }
3074
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003075 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003076 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003078 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003079 {
3080 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3081 return( ret );
3082 }
3083
3084 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003085 }
3086
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003087 return( ret );
3088}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003089#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003090
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003091static int ssl_check_record_type( uint8_t record_type )
3092{
3093 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3094 record_type != MBEDTLS_SSL_MSG_ALERT &&
3095 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3096 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3097 {
3098 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3099 }
3100
3101 return( 0 );
3102}
3103
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003104/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003105 * ContentType type;
3106 * ProtocolVersion version;
3107 * uint16 epoch; // DTLS only
3108 * uint48 sequence_number; // DTLS only
3109 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003110 *
3111 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003112 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003113 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3114 *
3115 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003116 * 1. proceed with the record if this function returns 0
3117 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3118 * 3. return CLIENT_RECONNECT if this function return that value
3119 * 4. drop the whole datagram if this function returns anything else.
3120 * Point 2 is needed when the peer is resending, and we have already received
3121 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003122 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003123static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003124 unsigned char *buf,
3125 size_t len,
3126 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003127{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003128 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003129
Hanno Beckere5e7e782019-07-11 12:29:35 +01003130 size_t const rec_hdr_type_offset = 0;
3131 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003132
Hanno Beckere5e7e782019-07-11 12:29:35 +01003133 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3134 rec_hdr_type_len;
3135 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
Hanno Beckere5e7e782019-07-11 12:29:35 +01003137 size_t const rec_hdr_ctr_len = 8;
3138#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003139 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003140 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3141 rec_hdr_version_len;
3142
Hanno Beckera0e20d02019-05-15 14:03:01 +01003143#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003144 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3145 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003146 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3148#endif /* MBEDTLS_SSL_PROTO_DTLS */
3149
3150 size_t rec_hdr_len_offset; /* To be determined */
3151 size_t const rec_hdr_len_len = 2;
3152
3153 /*
3154 * Check minimum lengths for record header.
3155 */
3156
3157#if defined(MBEDTLS_SSL_PROTO_DTLS)
3158 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3159 {
3160 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3161 }
3162 else
3163#endif /* MBEDTLS_SSL_PROTO_DTLS */
3164 {
3165 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3166 }
3167
3168 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3169 {
3170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3171 (unsigned) len,
3172 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3173 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3174 }
3175
3176 /*
3177 * Parse and validate record content type
3178 */
3179
3180 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003181
3182 /* Check record content type */
3183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3184 rec->cid_len = 0;
3185
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003187 ssl->conf->cid_len != 0 &&
3188 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003189 {
3190 /* Shift pointers to account for record header including CID
3191 * struct {
3192 * ContentType special_type = tls12_cid;
3193 * ProtocolVersion version;
3194 * uint16 epoch;
3195 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003196 * opaque cid[cid_length]; // Additional field compared to
3197 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003198 * uint16 length;
3199 * opaque enc_content[DTLSCiphertext.length];
3200 * } DTLSCiphertext;
3201 */
3202
3203 /* So far, we only support static CID lengths
3204 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003205 rec_hdr_cid_len = ssl->conf->cid_len;
3206 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003207
Hanno Beckere5e7e782019-07-11 12:29:35 +01003208 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003209 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3211 (unsigned) len,
3212 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003213 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003214 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003215
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003216 /* configured CID len is guaranteed at most 255, see
3217 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3218 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003219 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003220 }
3221 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003222#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003223 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003224 if( ssl_check_record_type( rec->type ) )
3225 {
Hanno Becker54229812019-07-12 14:40:00 +01003226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3227 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003228 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3229 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003230 }
3231
Hanno Beckere5e7e782019-07-11 12:29:35 +01003232 /*
3233 * Parse and validate record version
3234 */
3235
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003236 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3237 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003238 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3239 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003240 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003241
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003242 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3245 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 }
3247
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003248 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3251 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 }
3253
Hanno Beckere5e7e782019-07-11 12:29:35 +01003254 /*
3255 * Parse/Copy record sequence number.
3256 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003257
Hanno Beckere5e7e782019-07-11 12:29:35 +01003258#if defined(MBEDTLS_SSL_PROTO_DTLS)
3259 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003260 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003261 /* Copy explicit record sequence number from input buffer. */
3262 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3263 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003264 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003265 else
3266#endif /* MBEDTLS_SSL_PROTO_DTLS */
3267 {
3268 /* Copy implicit record sequence number from SSL context structure. */
3269 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3270 }
Paul Bakker40e46942009-01-03 21:51:57 +00003271
Hanno Beckere5e7e782019-07-11 12:29:35 +01003272 /*
3273 * Parse record length.
3274 */
3275
Hanno Beckere5e7e782019-07-11 12:29:35 +01003276 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003277 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3278 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003279 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Paul Elliott9f352112020-12-09 14:55:45 +00003281 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003282 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003283 rec->type,
3284 major_ver, minor_ver, rec->data_len ) );
3285
3286 rec->buf = buf;
3287 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003288
Hanno Beckerd417cc92019-07-26 08:20:27 +01003289 if( rec->data_len == 0 )
3290 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003292 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003293 * DTLS-related tests.
3294 * Check epoch before checking length constraint because
3295 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3296 * message gets duplicated before the corresponding Finished message,
3297 * the second ChangeCipherSpec should be discarded because it belongs
3298 * to an old epoch, but not because its length is shorter than
3299 * the minimum record length for packets using the new record transform.
3300 * Note that these two kinds of failures are handled differently,
3301 * as an unexpected record is silently skipped but an invalid
3302 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003303 */
3304#if defined(MBEDTLS_SSL_PROTO_DTLS)
3305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3306 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003307 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003308
Hanno Becker955a5c92019-07-10 17:12:07 +01003309 /* Check that the datagram is large enough to contain a record
3310 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003311 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003312 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3314 (unsigned) len,
3315 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003316 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3317 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003318
Hanno Becker37cfe732019-07-10 17:20:01 +01003319 /* Records from other, non-matching epochs are silently discarded.
3320 * (The case of same-port Client reconnects must be considered in
3321 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003322 if( rec_epoch != ssl->in_epoch )
3323 {
3324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003325 "expected %u, received %lu",
3326 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003327
Hanno Becker552f7472019-07-19 10:59:12 +01003328 /* Records from the next epoch are considered for buffering
3329 * (concretely: early Finished messages). */
3330 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003331 {
Hanno Becker552f7472019-07-19 10:59:12 +01003332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3333 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003334 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003335
Hanno Becker2fddd372019-07-10 14:37:41 +01003336 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003337 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003338#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003339 /* For records from the correct epoch, check whether their
3340 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003341 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3342 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003343 {
3344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3345 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3346 }
3347#endif
3348 }
3349#endif /* MBEDTLS_SSL_PROTO_DTLS */
3350
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003351 return( 0 );
3352}
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
Hanno Becker2fddd372019-07-10 14:37:41 +01003355#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3356static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3357{
3358 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3359
3360 /*
3361 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3362 * access the first byte of record content (handshake type), as we
3363 * have an active transform (possibly iv_len != 0), so use the
3364 * fact that the record header len is 13 instead.
3365 */
3366 if( rec_epoch == 0 &&
3367 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3368 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3369 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3370 ssl->in_left > 13 &&
3371 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3372 {
3373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3374 "from the same port" ) );
3375 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376 }
3377
3378 return( 0 );
3379}
Hanno Becker2fddd372019-07-10 14:37:41 +01003380#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003382/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003383 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003384 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003385static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3386 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003387{
3388 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003391 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003392
Ronald Cron7e38cba2021-11-24 12:43:39 +01003393 /*
3394 * In TLS 1.3, always treat ChangeCipherSpec records
3395 * as unencrypted. The only thing we do with them is
3396 * check the length and content and ignore them.
3397 */
Ronald Cron6f135e12021-12-08 16:57:54 +01003398#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron7e38cba2021-11-24 12:43:39 +01003399 if( ssl->transform_in != NULL &&
3400 ssl->transform_in->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3401 {
3402 if( rec->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3403 done = 1;
3404 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003405#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Cron7e38cba2021-11-24 12:43:39 +01003406
Paul Bakker48916f92012-09-16 19:57:18 +00003407 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003409 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003410
Hanno Beckera18d1322018-01-03 14:27:32 +00003411 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003412 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003415
Hanno Beckera0e20d02019-05-15 14:03:01 +01003416#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003417 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3418 ssl->conf->ignore_unexpected_cid
3419 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3420 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003421 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003422 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003423 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003424#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 return( ret );
3427 }
3428
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003429 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003430 {
3431 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003432 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003433 }
3434
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003435 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003436 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003437
Hanno Beckera0e20d02019-05-15 14:03:01 +01003438#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003439 /* We have already checked the record content type
3440 * in ssl_parse_record_header(), failing or silently
3441 * dropping the record in the case of an unknown type.
3442 *
3443 * Since with the use of CIDs, the record content type
3444 * might change during decryption, re-check the record
3445 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003446 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003447 {
3448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3449 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3450 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003452
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003453 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003454 {
3455#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3456 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003457 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003458 {
3459 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3461 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3462 }
3463#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3464
3465 ssl->nb_zero++;
3466
3467 /*
3468 * Three or more empty messages may be a DoS attack
3469 * (excessive CPU consumption).
3470 */
3471 if( ssl->nb_zero > 3 )
3472 {
3473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003474 "messages, possible DoS attack" ) );
3475 /* Treat the records as if they were not properly authenticated,
3476 * thereby failing the connection if we see more than allowed
3477 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003478 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3479 }
3480 }
3481 else
3482 ssl->nb_zero = 0;
3483
3484#if defined(MBEDTLS_SSL_PROTO_DTLS)
3485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3486 {
3487 ; /* in_ctr read from peer, not maintained internally */
3488 }
3489 else
3490#endif
3491 {
3492 unsigned i;
Jerry Yuae0b2e22021-10-08 15:21:19 +08003493 for( i = MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
3494 i > mbedtls_ssl_ep_len( ssl ); i-- )
3495 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003496 if( ++ssl->in_ctr[i - 1] != 0 )
3497 break;
Jerry Yuae0b2e22021-10-08 15:21:19 +08003498 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003499
3500 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003501 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003502 {
3503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3504 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3505 }
3506 }
3507
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 }
3509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003514 }
3515#endif
3516
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003517 /* Check actual (decrypted) record content length against
3518 * configured maximum. */
3519 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3520 {
3521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3522 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3523 }
3524
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003525 return( 0 );
3526}
3527
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003528/*
3529 * Read a record.
3530 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003531 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3532 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3533 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003534 */
Hanno Becker1097b342018-08-15 14:09:41 +01003535
3536/* Helper functions for mbedtls_ssl_read_record(). */
3537static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003538static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3539static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003540
Hanno Becker327c93b2018-08-15 13:56:18 +01003541int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003542 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003543{
Janos Follath865b3eb2019-12-16 11:46:15 +00003544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003547
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003548 if( ssl->keep_current_message == 0 )
3549 {
3550 do {
Simon Butcher99000142016-10-13 17:21:01 +01003551
Hanno Becker26994592018-08-15 14:14:59 +01003552 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003553 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003554 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003555
Hanno Beckere74d5562018-08-15 14:26:08 +01003556 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003557 {
Hanno Becker40f50842018-08-15 14:48:01 +01003558#if defined(MBEDTLS_SSL_PROTO_DTLS)
3559 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003560
Hanno Becker40f50842018-08-15 14:48:01 +01003561 /* We only check for buffered messages if the
3562 * current datagram is fully consumed. */
3563 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003564 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003565 {
Hanno Becker40f50842018-08-15 14:48:01 +01003566 if( ssl_load_buffered_message( ssl ) == 0 )
3567 have_buffered = 1;
3568 }
3569
3570 if( have_buffered == 0 )
3571#endif /* MBEDTLS_SSL_PROTO_DTLS */
3572 {
3573 ret = ssl_get_next_record( ssl );
3574 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3575 continue;
3576
3577 if( ret != 0 )
3578 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003579 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003580 return( ret );
3581 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003582 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003583 }
3584
3585 ret = mbedtls_ssl_handle_message_type( ssl );
3586
Hanno Becker40f50842018-08-15 14:48:01 +01003587#if defined(MBEDTLS_SSL_PROTO_DTLS)
3588 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3589 {
3590 /* Buffer future message */
3591 ret = ssl_buffer_message( ssl );
3592 if( ret != 0 )
3593 return( ret );
3594
3595 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3596 }
3597#endif /* MBEDTLS_SSL_PROTO_DTLS */
3598
Hanno Becker90333da2017-10-10 11:27:13 +01003599 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3600 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003601
3602 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003603 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003604 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003605 return( ret );
3606 }
3607
Hanno Becker327c93b2018-08-15 13:56:18 +01003608 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003609 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003610 {
3611 mbedtls_ssl_update_handshake_status( ssl );
3612 }
Simon Butcher99000142016-10-13 17:21:01 +01003613 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003614 else
Simon Butcher99000142016-10-13 17:21:01 +01003615 {
Hanno Becker02f59072018-08-15 14:00:24 +01003616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003617 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003618 }
3619
3620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3621
3622 return( 0 );
3623}
3624
Hanno Becker40f50842018-08-15 14:48:01 +01003625#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003626static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003627{
Hanno Becker40f50842018-08-15 14:48:01 +01003628 if( ssl->in_left > ssl->next_record_offset )
3629 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003630
Hanno Becker40f50842018-08-15 14:48:01 +01003631 return( 0 );
3632}
3633
3634static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3635{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003636 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003637 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003638 int ret = 0;
3639
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003640 if( hs == NULL )
3641 return( -1 );
3642
Hanno Beckere00ae372018-08-20 09:39:42 +01003643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3644
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003645 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3646 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3647 {
3648 /* Check if we have seen a ChangeCipherSpec before.
3649 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003650 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003651 {
3652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3653 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003654 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003655 }
3656
Hanno Becker39b8bc92018-08-28 17:17:13 +01003657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003658 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3659 ssl->in_msglen = 1;
3660 ssl->in_msg[0] = 1;
3661
3662 /* As long as they are equal, the exact value doesn't matter. */
3663 ssl->in_left = 0;
3664 ssl->next_record_offset = 0;
3665
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003666 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003667 goto exit;
3668 }
Hanno Becker37f95322018-08-16 13:55:32 +01003669
Hanno Beckerb8f50142018-08-28 10:01:34 +01003670#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003671 /* Debug only */
3672 {
3673 unsigned offset;
3674 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3675 {
3676 hs_buf = &hs->buffering.hs[offset];
3677 if( hs_buf->is_valid == 1 )
3678 {
3679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3680 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003681 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003682 }
3683 }
3684 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003685#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003686
3687 /* Check if we have buffered and/or fully reassembled the
3688 * next handshake message. */
3689 hs_buf = &hs->buffering.hs[0];
3690 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3691 {
3692 /* Synthesize a record containing the buffered HS message. */
3693 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3694 ( hs_buf->data[2] << 8 ) |
3695 hs_buf->data[3];
3696
3697 /* Double-check that we haven't accidentally buffered
3698 * a message that doesn't fit into the input buffer. */
3699 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3700 {
3701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3703 }
3704
3705 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3706 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3707 hs_buf->data, msg_len + 12 );
3708
3709 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3710 ssl->in_hslen = msg_len + 12;
3711 ssl->in_msglen = msg_len + 12;
3712 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3713
3714 ret = 0;
3715 goto exit;
3716 }
3717 else
3718 {
3719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3720 hs->in_msg_seq ) );
3721 }
3722
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003723 ret = -1;
3724
3725exit:
3726
3727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3728 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003729}
3730
Hanno Beckera02b0b42018-08-21 17:20:27 +01003731static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3732 size_t desired )
3733{
3734 int offset;
3735 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3737 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003738
Hanno Becker01315ea2018-08-21 17:22:17 +01003739 /* Get rid of future records epoch first, if such exist. */
3740 ssl_free_buffered_record( ssl );
3741
3742 /* Check if we have enough space available now. */
3743 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3744 hs->buffering.total_bytes_buffered ) )
3745 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003747 return( 0 );
3748 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003749
Hanno Becker4f432ad2018-08-28 10:02:32 +01003750 /* We don't have enough space to buffer the next expected handshake
3751 * message. Remove buffers used for future messages to gain space,
3752 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003753 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3754 offset >= 0; offset-- )
3755 {
3756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3757 offset ) );
3758
Hanno Beckerb309b922018-08-23 13:18:05 +01003759 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003760
3761 /* Check if we have enough space available now. */
3762 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3763 hs->buffering.total_bytes_buffered ) )
3764 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003766 return( 0 );
3767 }
3768 }
3769
3770 return( -1 );
3771}
3772
Hanno Becker40f50842018-08-15 14:48:01 +01003773static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3774{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003775 int ret = 0;
3776 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3777
3778 if( hs == NULL )
3779 return( 0 );
3780
3781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3782
3783 switch( ssl->in_msgtype )
3784 {
3785 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003787
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003788 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003789 break;
3790
3791 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003792 {
3793 unsigned recv_msg_seq_offset;
3794 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3795 mbedtls_ssl_hs_buffer *hs_buf;
3796 size_t msg_len = ssl->in_hslen - 12;
3797
3798 /* We should never receive an old handshake
3799 * message - double-check nonetheless. */
3800 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3801 {
3802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3803 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3804 }
3805
3806 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3807 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3808 {
3809 /* Silently ignore -- message too far in the future */
3810 MBEDTLS_SSL_DEBUG_MSG( 2,
3811 ( "Ignore future HS message with sequence number %u, "
3812 "buffering window %u - %u",
3813 recv_msg_seq, ssl->handshake->in_msg_seq,
3814 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
3815
3816 goto exit;
3817 }
3818
3819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
3820 recv_msg_seq, recv_msg_seq_offset ) );
3821
3822 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
3823
3824 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003825 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01003826 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003827 size_t reassembly_buf_sz;
3828
Hanno Becker37f95322018-08-16 13:55:32 +01003829 hs_buf->is_fragmented =
3830 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
3831
3832 /* We copy the message back into the input buffer
3833 * after reassembly, so check that it's not too large.
3834 * This is an implementation-specific limitation
3835 * and not one from the standard, hence it is not
3836 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01003837 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01003838 {
3839 /* Ignore message */
3840 goto exit;
3841 }
3842
Hanno Beckere0b150f2018-08-21 15:51:03 +01003843 /* Check if we have enough space to buffer the message. */
3844 if( hs->buffering.total_bytes_buffered >
3845 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
3846 {
3847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3848 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3849 }
3850
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003851 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
3852 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003853
3854 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3855 hs->buffering.total_bytes_buffered ) )
3856 {
3857 if( recv_msg_seq_offset > 0 )
3858 {
3859 /* If we can't buffer a future message because
3860 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00003861 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3862 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3863 " (already %" MBEDTLS_PRINTF_SIZET
3864 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003865 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003866 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003867 goto exit;
3868 }
Hanno Beckere1801392018-08-21 16:51:05 +01003869 else
3870 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3872 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3873 " (already %" MBEDTLS_PRINTF_SIZET
3874 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003875 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003876 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01003877 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003878
Hanno Beckera02b0b42018-08-21 17:20:27 +01003879 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003880 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
3882 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
3883 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
3884 " (already %" MBEDTLS_PRINTF_SIZET
3885 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00003886 msg_len,
3887 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00003888 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003889 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003890 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3891 goto exit;
3892 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003893 }
3894
Paul Elliottd48d5c62021-01-07 14:47:05 +00003895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01003896 msg_len ) );
3897
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003898 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
3899 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01003900 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01003901 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01003902 goto exit;
3903 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003904 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003905
3906 /* Prepare final header: copy msg_type, length and message_seq,
3907 * then add standardised fragment_offset and fragment_length */
3908 memcpy( hs_buf->data, ssl->in_msg, 6 );
3909 memset( hs_buf->data + 6, 0, 3 );
3910 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
3911
3912 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01003913
3914 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003915 }
3916 else
3917 {
3918 /* Make sure msg_type and length are consistent */
3919 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
3920 {
3921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
3922 /* Ignore */
3923 goto exit;
3924 }
3925 }
3926
Hanno Becker4422bbb2018-08-20 09:40:19 +01003927 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01003928 {
3929 size_t frag_len, frag_off;
3930 unsigned char * const msg = hs_buf->data + 12;
3931
3932 /*
3933 * Check and copy current fragment
3934 */
3935
3936 /* Validation of header fields already done in
3937 * mbedtls_ssl_prepare_handshake_record(). */
3938 frag_off = ssl_get_hs_frag_off( ssl );
3939 frag_len = ssl_get_hs_frag_len( ssl );
3940
Paul Elliottd48d5c62021-01-07 14:47:05 +00003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
3942 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01003943 frag_off, frag_len ) );
3944 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3945
3946 if( hs_buf->is_fragmented )
3947 {
3948 unsigned char * const bitmask = msg + msg_len;
3949 ssl_bitmask_set( bitmask, frag_off, frag_len );
3950 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
3951 msg_len ) == 0 );
3952 }
3953 else
3954 {
3955 hs_buf->is_complete = 1;
3956 }
3957
3958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
3959 hs_buf->is_complete ? "" : "not yet " ) );
3960 }
3961
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003962 break;
Hanno Becker37f95322018-08-16 13:55:32 +01003963 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003964
3965 default:
Hanno Becker360bef32018-08-28 10:04:33 +01003966 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003967 break;
3968 }
3969
3970exit:
3971
3972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
3973 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003974}
3975#endif /* MBEDTLS_SSL_PROTO_DTLS */
3976
Hanno Becker1097b342018-08-15 14:09:41 +01003977static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003978{
Hanno Becker4a810fb2017-05-24 16:27:30 +01003979 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01003980 * Consume last content-layer message and potentially
3981 * update in_msglen which keeps track of the contents'
3982 * consumption state.
3983 *
3984 * (1) Handshake messages:
3985 * Remove last handshake message, move content
3986 * and adapt in_msglen.
3987 *
3988 * (2) Alert messages:
3989 * Consume whole record content, in_msglen = 0.
3990 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01003991 * (3) Change cipher spec:
3992 * Consume whole record content, in_msglen = 0.
3993 *
3994 * (4) Application data:
3995 * Don't do anything - the record layer provides
3996 * the application data as a stream transport
3997 * and consumes through mbedtls_ssl_read only.
3998 *
3999 */
4000
4001 /* Case (1): Handshake messages */
4002 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004003 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004004 /* Hard assertion to be sure that no application data
4005 * is in flight, as corrupting ssl->in_msglen during
4006 * ssl->in_offt != NULL is fatal. */
4007 if( ssl->in_offt != NULL )
4008 {
4009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4010 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4011 }
4012
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004013 /*
4014 * Get next Handshake message in the current record
4015 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004016
Hanno Becker4a810fb2017-05-24 16:27:30 +01004017 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004018 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004019 * current handshake content: If DTLS handshake
4020 * fragmentation is used, that's the fragment
4021 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004022 * size here is faulty and should be changed at
4023 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004024 * (2) While it doesn't seem to cause problems, one
4025 * has to be very careful not to assume that in_hslen
4026 * is always <= in_msglen in a sensible communication.
4027 * Again, it's wrong for DTLS handshake fragmentation.
4028 * The following check is therefore mandatory, and
4029 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004030 * Additionally, ssl->in_hslen might be arbitrarily out of
4031 * bounds after handling a DTLS message with an unexpected
4032 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004033 */
4034 if( ssl->in_hslen < ssl->in_msglen )
4035 {
4036 ssl->in_msglen -= ssl->in_hslen;
4037 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4038 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004039
Hanno Becker4a810fb2017-05-24 16:27:30 +01004040 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4041 ssl->in_msg, ssl->in_msglen );
4042 }
4043 else
4044 {
4045 ssl->in_msglen = 0;
4046 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004047
Hanno Becker4a810fb2017-05-24 16:27:30 +01004048 ssl->in_hslen = 0;
4049 }
4050 /* Case (4): Application data */
4051 else if( ssl->in_offt != NULL )
4052 {
4053 return( 0 );
4054 }
4055 /* Everything else (CCS & Alerts) */
4056 else
4057 {
4058 ssl->in_msglen = 0;
4059 }
4060
Hanno Becker1097b342018-08-15 14:09:41 +01004061 return( 0 );
4062}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004063
Hanno Beckere74d5562018-08-15 14:26:08 +01004064static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4065{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004066 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004067 return( 1 );
4068
4069 return( 0 );
4070}
4071
Hanno Becker5f066e72018-08-16 14:56:31 +01004072#if defined(MBEDTLS_SSL_PROTO_DTLS)
4073
4074static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4075{
4076 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4077 if( hs == NULL )
4078 return;
4079
Hanno Becker01315ea2018-08-21 17:22:17 +01004080 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004081 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004082 hs->buffering.total_bytes_buffered -=
4083 hs->buffering.future_record.len;
4084
4085 mbedtls_free( hs->buffering.future_record.data );
4086 hs->buffering.future_record.data = NULL;
4087 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004088}
4089
4090static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4091{
4092 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4093 unsigned char * rec;
4094 size_t rec_len;
4095 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004096#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4097 size_t in_buf_len = ssl->in_buf_len;
4098#else
4099 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4100#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004101 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4102 return( 0 );
4103
4104 if( hs == NULL )
4105 return( 0 );
4106
Hanno Becker5f066e72018-08-16 14:56:31 +01004107 rec = hs->buffering.future_record.data;
4108 rec_len = hs->buffering.future_record.len;
4109 rec_epoch = hs->buffering.future_record.epoch;
4110
4111 if( rec == NULL )
4112 return( 0 );
4113
Hanno Becker4cb782d2018-08-20 11:19:05 +01004114 /* Only consider loading future records if the
4115 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004116 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004117 return( 0 );
4118
Hanno Becker5f066e72018-08-16 14:56:31 +01004119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4120
4121 if( rec_epoch != ssl->in_epoch )
4122 {
4123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4124 goto exit;
4125 }
4126
4127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4128
4129 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004130 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004131 {
4132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4134 }
4135
4136 memcpy( ssl->in_hdr, rec, rec_len );
4137 ssl->in_left = rec_len;
4138 ssl->next_record_offset = 0;
4139
4140 ssl_free_buffered_record( ssl );
4141
4142exit:
4143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4144 return( 0 );
4145}
4146
Hanno Becker519f15d2019-07-11 12:43:20 +01004147static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4148 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004149{
4150 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004151
4152 /* Don't buffer future records outside handshakes. */
4153 if( hs == NULL )
4154 return( 0 );
4155
4156 /* Only buffer handshake records (we are only interested
4157 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004158 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004159 return( 0 );
4160
4161 /* Don't buffer more than one future epoch record. */
4162 if( hs->buffering.future_record.data != NULL )
4163 return( 0 );
4164
Hanno Becker01315ea2018-08-21 17:22:17 +01004165 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004166 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004167 hs->buffering.total_bytes_buffered ) )
4168 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4170 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4171 " (already %" MBEDTLS_PRINTF_SIZET
4172 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004173 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004174 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004175 return( 0 );
4176 }
4177
Hanno Becker5f066e72018-08-16 14:56:31 +01004178 /* Buffer record */
4179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004180 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004181 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004182
4183 /* ssl_parse_record_header() only considers records
4184 * of the next epoch as candidates for buffering. */
4185 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004186 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004187
4188 hs->buffering.future_record.data =
4189 mbedtls_calloc( 1, hs->buffering.future_record.len );
4190 if( hs->buffering.future_record.data == NULL )
4191 {
4192 /* If we run out of RAM trying to buffer a
4193 * record from the next epoch, just ignore. */
4194 return( 0 );
4195 }
4196
Hanno Becker519f15d2019-07-11 12:43:20 +01004197 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004198
Hanno Becker519f15d2019-07-11 12:43:20 +01004199 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004200 return( 0 );
4201}
4202
4203#endif /* MBEDTLS_SSL_PROTO_DTLS */
4204
Hanno Beckere74d5562018-08-15 14:26:08 +01004205static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004206{
Janos Follath865b3eb2019-12-16 11:46:15 +00004207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004208 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004209
Hanno Becker5f066e72018-08-16 14:56:31 +01004210#if defined(MBEDTLS_SSL_PROTO_DTLS)
4211 /* We might have buffered a future record; if so,
4212 * and if the epoch matches now, load it.
4213 * On success, this call will set ssl->in_left to
4214 * the length of the buffered record, so that
4215 * the calls to ssl_fetch_input() below will
4216 * essentially be no-ops. */
4217 ret = ssl_load_buffered_record( ssl );
4218 if( ret != 0 )
4219 return( ret );
4220#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004221
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004222 /* Ensure that we have enough space available for the default form
4223 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4224 * with no space for CIDs counted in). */
4225 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4226 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004229 return( ret );
4230 }
4231
Hanno Beckere5e7e782019-07-11 12:29:35 +01004232 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4233 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004235#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004236 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004237 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004238 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4239 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004240 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004241 if( ret != 0 )
4242 return( ret );
4243
4244 /* Fall through to handling of unexpected records */
4245 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4246 }
4247
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004248 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4249 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004250#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004251 /* Reset in pointers to default state for TLS/DTLS records,
4252 * assuming no CID and no offset between record content and
4253 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004254 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004255
Hanno Becker7ae20e02019-07-12 08:33:49 +01004256 /* Setup internal message pointers from record structure. */
4257 ssl->in_msgtype = rec.type;
4258#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4259 ssl->in_len = ssl->in_cid + rec.cid_len;
4260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4261 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4262 ssl->in_msglen = rec.data_len;
4263
Hanno Becker2fddd372019-07-10 14:37:41 +01004264 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004265 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004266 if( ret != 0 )
4267 return( ret );
4268#endif
4269
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004270 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004271 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004272
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4274 "(header)" ) );
4275 }
4276 else
4277 {
4278 /* Skip invalid record and the rest of the datagram */
4279 ssl->next_record_offset = 0;
4280 ssl->in_left = 0;
4281
4282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4283 "(header)" ) );
4284 }
4285
4286 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004287 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004288 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004289 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004290#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004291 {
4292 return( ret );
4293 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004294 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004296#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004297 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004298 {
Hanno Beckera8814792019-07-10 15:01:45 +01004299 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004300 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004301 if( ssl->next_record_offset < ssl->in_left )
4302 {
4303 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4304 }
4305 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004306 else
4307#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004308 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004309 /*
4310 * Fetch record contents from underlying transport.
4311 */
Hanno Beckera3175662019-07-11 12:50:29 +01004312 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004313 if( ret != 0 )
4314 {
4315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4316 return( ret );
4317 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004318
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004319 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004320 }
4321
4322 /*
4323 * Decrypt record contents.
4324 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004325
Hanno Beckerfdf66042019-07-11 13:07:45 +01004326 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004330 {
4331 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004332 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004333 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004334 /* Except when waiting for Finished as a bad mac here
4335 * probably means something went wrong in the handshake
4336 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4337 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4338 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4339 {
4340#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4341 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4342 {
4343 mbedtls_ssl_send_alert_message( ssl,
4344 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4345 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4346 }
4347#endif
4348 return( ret );
4349 }
4350
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004351 if( ssl->conf->badmac_limit != 0 &&
4352 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4355 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004356 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004357
Hanno Becker4a810fb2017-05-24 16:27:30 +01004358 /* As above, invalid records cause
4359 * dismissal of the whole datagram. */
4360
4361 ssl->next_record_offset = 0;
4362 ssl->in_left = 0;
4363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004365 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004366 }
4367
4368 return( ret );
4369 }
4370 else
4371#endif
4372 {
4373 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004374#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4375 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 mbedtls_ssl_send_alert_message( ssl,
4378 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4379 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004380 }
4381#endif
4382 return( ret );
4383 }
4384 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004385
Hanno Becker44d89b22019-07-12 09:40:44 +01004386
4387 /* Reset in pointers to default state for TLS/DTLS records,
4388 * assuming no CID and no offset between record content and
4389 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004390 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004391#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4392 ssl->in_len = ssl->in_cid + rec.cid_len;
4393#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004394 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004395
Hanno Becker8685c822019-07-12 09:37:30 +01004396 /* The record content type may change during decryption,
4397 * so re-read it. */
4398 ssl->in_msgtype = rec.type;
4399 /* Also update the input buffer, because unfortunately
4400 * the server-side ssl_parse_client_hello() reparses the
4401 * record header when receiving a ClientHello initiating
4402 * a renegotiation. */
4403 ssl->in_hdr[0] = rec.type;
4404 ssl->in_msg = rec.buf + rec.data_offset;
4405 ssl->in_msglen = rec.data_len;
Joe Subbiani6dd73642021-07-19 11:56:54 +01004406 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->in_len, 0 );
Hanno Becker8685c822019-07-12 09:37:30 +01004407
Simon Butcher99000142016-10-13 17:21:01 +01004408 return( 0 );
4409}
4410
4411int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4412{
Janos Follath865b3eb2019-12-16 11:46:15 +00004413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004414
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004415 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004416 * Handle particular types of records
4417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004419 {
Simon Butcher99000142016-10-13 17:21:01 +01004420 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4421 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004422 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 }
4425
Hanno Beckere678eaa2018-08-21 14:57:46 +01004426 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004427 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004428 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004429 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004431 ssl->in_msglen ) );
4432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004433 }
4434
Hanno Beckere678eaa2018-08-21 14:57:46 +01004435 if( ssl->in_msg[0] != 1 )
4436 {
4437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4438 ssl->in_msg[0] ) );
4439 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4440 }
4441
4442#if defined(MBEDTLS_SSL_PROTO_DTLS)
4443 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4444 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4445 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4446 {
4447 if( ssl->handshake == NULL )
4448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4450 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4451 }
4452
4453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4454 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4455 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004456#endif
Ronald Cron7e38cba2021-11-24 12:43:39 +01004457
Ronald Cron6f135e12021-12-08 16:57:54 +01004458#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Ronald Cron7e38cba2021-11-24 12:43:39 +01004459 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
4460 {
4461#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
4462 MBEDTLS_SSL_DEBUG_MSG( 1,
4463 ( "Ignore ChangeCipherSpec in TLS 1.3 compatibility mode" ) );
4464 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
4465#else
4466 MBEDTLS_SSL_DEBUG_MSG( 1,
4467 ( "ChangeCipherSpec invalid in TLS 1.3 without compatibility mode" ) );
4468 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4469#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
4470 }
Ronald Cron6f135e12021-12-08 16:57:54 +01004471#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere678eaa2018-08-21 14:57:46 +01004472 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004476 if( ssl->in_msglen != 2 )
4477 {
4478 /* Note: Standard allows for more than one 2 byte alert
4479 to be packed in a single message, but Mbed TLS doesn't
4480 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004482 ssl->in_msglen ) );
4483 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4484 }
4485
Paul Elliott9f352112020-12-09 14:55:45 +00004486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 ssl->in_msg[0], ssl->in_msg[1] ) );
4488
4489 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004490 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004495 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004496 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004497 }
4498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4500 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4503 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004505
4506#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4507 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4508 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4509 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004511 /* Will be handled when trying to parse ServerHello */
4512 return( 0 );
4513 }
4514#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004515 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004516 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 }
4518
Hanno Beckerc76c6192017-06-06 10:03:17 +01004519#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004521 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004522 /* Drop unexpected ApplicationData records,
4523 * except at the beginning of renegotiations */
4524 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4525 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4526#if defined(MBEDTLS_SSL_RENEGOTIATION)
4527 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4528 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004529#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004530 )
4531 {
4532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4533 return( MBEDTLS_ERR_SSL_NON_FATAL );
4534 }
4535
4536 if( ssl->handshake != NULL &&
4537 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4538 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004539 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004540 }
4541 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004542#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004543
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 return( 0 );
4545}
4546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004547int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004548{
irwir6c0da642019-09-26 21:07:41 +03004549 return( mbedtls_ssl_send_alert_message( ssl,
4550 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4551 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004552}
4553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004555 unsigned char level,
4556 unsigned char message )
4557{
Janos Follath865b3eb2019-12-16 11:46:15 +00004558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004559
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004560 if( ssl == NULL || ssl->conf == NULL )
4561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004566 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004567 ssl->out_msglen = 2;
4568 ssl->out_msg[0] = level;
4569 ssl->out_msg[1] = message;
4570
Hanno Becker67bc7c32018-08-06 11:33:50 +01004571 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004574 return( ret );
4575 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004577
4578 return( 0 );
4579}
4580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004581int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004582{
Janos Follath865b3eb2019-12-16 11:46:15 +00004583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004587 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 ssl->out_msglen = 1;
4589 ssl->out_msg[0] = 1;
4590
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 ssl->state++;
4592
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004593 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004595 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596 return( ret );
4597 }
4598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004600
4601 return( 0 );
4602}
4603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004604int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004605{
Janos Follath865b3eb2019-12-16 11:46:15 +00004606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004609
Hanno Becker327c93b2018-08-15 13:56:18 +01004610 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004612 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 return( ret );
4614 }
4615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004619 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4620 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004622 }
4623
Hanno Beckere678eaa2018-08-21 14:57:46 +01004624 /* CCS records are only accepted if they have length 1 and content '1',
4625 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004626
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004627 /*
4628 * Switch to our negotiated transform and session parameters for inbound
4629 * data.
4630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004632 ssl->transform_in = ssl->transform_negotiate;
4633 ssl->session_in = ssl->session_negotiate;
4634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004636 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004639 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004640#endif
4641
4642 /* Increment epoch */
4643 if( ++ssl->in_epoch == 0 )
4644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004645 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004646 /* This is highly unlikely to happen for legitimate reasons, so
4647 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004649 }
4650 }
4651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652#endif /* MBEDTLS_SSL_PROTO_DTLS */
Jerry Yufd320e92021-10-08 21:52:41 +08004653 memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004654
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004655 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004656
Paul Bakker5121ce52009-01-03 21:22:43 +00004657 ssl->state++;
4658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660
4661 return( 0 );
4662}
4663
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004664/* Once ssl->out_hdr as the address of the beginning of the
4665 * next outgoing record is set, deduce the other pointers.
4666 *
4667 * Note: For TLS, we save the implicit record sequence number
4668 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4669 * and the caller has to make sure there's space for this.
4670 */
4671
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004672static size_t ssl_transform_get_explicit_iv_len(
4673 mbedtls_ssl_transform const *transform )
4674{
TRodziewiczef73f012021-05-13 14:53:36 +02004675 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004676 return( 0 );
4677
4678 return( transform->ivlen - transform->fixed_ivlen );
4679}
4680
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004681void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4682 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004683{
4684#if defined(MBEDTLS_SSL_PROTO_DTLS)
4685 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4686 {
4687 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08004689 ssl->out_cid = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004690 ssl->out_len = ssl->out_cid;
4691 if( transform != NULL )
4692 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004693#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08004694 ssl->out_len = ssl->out_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004695#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004696 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004697 }
4698 else
4699#endif
4700 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004701 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004702#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004703 ssl->out_cid = ssl->out_len;
4704#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004705 ssl->out_iv = ssl->out_hdr + 5;
4706 }
4707
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004708 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004709 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004710 if( transform != NULL )
4711 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004712}
4713
4714/* Once ssl->in_hdr as the address of the beginning of the
4715 * next incoming record is set, deduce the other pointers.
4716 *
4717 * Note: For TLS, we save the implicit record sequence number
4718 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4719 * and the caller has to make sure there's space for this.
4720 */
4721
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004722void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004723{
Hanno Becker79594fd2019-05-08 09:38:41 +01004724 /* This function sets the pointers to match the case
4725 * of unprotected TLS/DTLS records, with both ssl->in_iv
4726 * and ssl->in_msg pointing to the beginning of the record
4727 * content.
4728 *
4729 * When decrypting a protected record, ssl->in_msg
4730 * will be shifted to point to the beginning of the
4731 * record plaintext.
4732 */
4733
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004734#if defined(MBEDTLS_SSL_PROTO_DTLS)
4735 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4736 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004737 /* This sets the header pointers to match records
4738 * without CID. When we receive a record containing
4739 * a CID, the fields are shifted accordingly in
4740 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004741 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004742#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Jerry Yuae0b2e22021-10-08 15:21:19 +08004743 ssl->in_cid = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004744 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004745#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Jerry Yuae0b2e22021-10-08 15:21:19 +08004746 ssl->in_len = ssl->in_ctr + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004747#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004748 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004749 }
4750 else
4751#endif
4752 {
Jerry Yuae0b2e22021-10-08 15:21:19 +08004753 ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004754 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004755#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004756 ssl->in_cid = ssl->in_len;
4757#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004758 ssl->in_iv = ssl->in_hdr + 5;
4759 }
4760
Hanno Becker79594fd2019-05-08 09:38:41 +01004761 /* This will be adjusted at record decryption time. */
4762 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004763}
4764
Paul Bakker5121ce52009-01-03 21:22:43 +00004765/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004766 * Setup an SSL context
4767 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004768
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004769void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004770{
4771 /* Set the incoming and outgoing record pointers. */
4772#if defined(MBEDTLS_SSL_PROTO_DTLS)
4773 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4774 {
4775 ssl->out_hdr = ssl->out_buf;
4776 ssl->in_hdr = ssl->in_buf;
4777 }
4778 else
4779#endif /* MBEDTLS_SSL_PROTO_DTLS */
4780 {
Hanno Becker12078f42021-03-02 15:28:41 +00004781 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004782 ssl->out_hdr = ssl->out_buf + 8;
4783 ssl->in_hdr = ssl->in_buf + 8;
4784 }
4785
4786 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004787 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4788 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004789}
4790
Paul Bakker5121ce52009-01-03 21:22:43 +00004791/*
4792 * SSL get accessors
4793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004795{
4796 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4797}
4798
Hanno Becker8b170a02017-10-10 11:51:19 +01004799int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4800{
4801 /*
4802 * Case A: We're currently holding back
4803 * a message for further processing.
4804 */
4805
4806 if( ssl->keep_current_message == 1 )
4807 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004808 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004809 return( 1 );
4810 }
4811
4812 /*
4813 * Case B: Further records are pending in the current datagram.
4814 */
4815
4816#if defined(MBEDTLS_SSL_PROTO_DTLS)
4817 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4818 ssl->in_left > ssl->next_record_offset )
4819 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004820 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004821 return( 1 );
4822 }
4823#endif /* MBEDTLS_SSL_PROTO_DTLS */
4824
4825 /*
4826 * Case C: A handshake message is being processed.
4827 */
4828
Hanno Becker8b170a02017-10-10 11:51:19 +01004829 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
4830 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004831 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004832 return( 1 );
4833 }
4834
4835 /*
4836 * Case D: An application data message is being processed
4837 */
4838 if( ssl->in_offt != NULL )
4839 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004841 return( 1 );
4842 }
4843
4844 /*
4845 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01004846 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01004847 * we implement support for multiple alerts in single records.
4848 */
4849
4850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
4851 return( 0 );
4852}
4853
Paul Bakker43ca69c2011-01-15 17:35:19 +00004854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004856{
Hanno Becker3136ede2018-08-17 15:28:19 +01004857 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004859 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004860
Hanno Becker5903de42019-05-03 14:46:38 +01004861 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
4862
Hanno Becker78640902018-08-13 16:35:15 +01004863 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01004864 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01004865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004868 case MBEDTLS_MODE_GCM:
4869 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004870 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004871 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004872 transform_expansion = transform->minlen;
4873 break;
4874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004875 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004876
4877 block_size = mbedtls_cipher_get_block_size(
4878 &transform->cipher_ctx_enc );
4879
Hanno Becker3136ede2018-08-17 15:28:19 +01004880 /* Expansion due to the addition of the MAC. */
4881 transform_expansion += transform->maclen;
4882
4883 /* Expansion due to the addition of CBC padding;
4884 * Theoretically up to 256 bytes, but we never use
4885 * more than the block size of the underlying cipher. */
4886 transform_expansion += block_size;
4887
TRodziewicz4ca18aa2021-05-20 14:46:20 +02004888 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01004889 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02004890#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02004891 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02004892#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01004893
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004894 break;
4895
4896 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004899 }
4900
Hanno Beckera0e20d02019-05-15 14:03:01 +01004901#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01004902 if( transform->out_cid_len != 0 )
4903 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004904#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01004905
Hanno Becker5903de42019-05-03 14:46:38 +01004906 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004907}
4908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004909#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004910/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004911 * Check record counters and renegotiate if they're above the limit.
4912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004913static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004914{
Hanno Beckerdd772292020-02-05 10:38:31 +00004915 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00004916 int in_ctr_cmp;
4917 int out_ctr_cmp;
4918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
4920 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004921 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004922 {
4923 return( 0 );
4924 }
4925
Andres AG2196c7f2016-12-15 17:01:16 +00004926 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
Jerry Yud9a94fe2021-09-28 18:58:59 +08004927 &ssl->conf->renego_period[ep_len],
Jerry Yuae0b2e22021-10-08 15:21:19 +08004928 MBEDTLS_SSL_SEQUENCE_NUMBER_LEN - ep_len );
Jerry Yud9a94fe2021-09-28 18:58:59 +08004929 out_ctr_cmp = memcmp( &ssl->cur_out_ctr[ep_len],
4930 &ssl->conf->renego_period[ep_len],
4931 sizeof( ssl->cur_out_ctr ) - ep_len );
Andres AG2196c7f2016-12-15 17:01:16 +00004932
4933 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004934 {
4935 return( 0 );
4936 }
4937
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004940}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00004942
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004943/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01004944 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004945 * may only be sent for the purpose of initiating renegotiations.
4946 *
4947 * This function is introduced as a separate helper since the handling
4948 * of post-handshake handshake messages changes significantly in TLS 1.3,
4949 * and having a helper function allows to distinguish between TLS <= 1.2 and
4950 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
4951 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00004952static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004953{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01004954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004955
4956 /*
4957 * - For client-side, expect SERVER_HELLO_REQUEST.
4958 * - For server-side, expect CLIENT_HELLO.
4959 * - Fail (TLS) or silently drop record (DTLS) in other cases.
4960 */
4961
4962#if defined(MBEDTLS_SSL_CLI_C)
4963 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
4964 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
4965 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
4966 {
4967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4968
4969 /* With DTLS, drop the packet (probably from last handshake) */
4970#if defined(MBEDTLS_SSL_PROTO_DTLS)
4971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4972 {
4973 return( 0 );
4974 }
4975#endif
4976 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4977 }
4978#endif /* MBEDTLS_SSL_CLI_C */
4979
4980#if defined(MBEDTLS_SSL_SRV_C)
4981 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4982 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
4983 {
4984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
4985
4986 /* With DTLS, drop the packet (probably from last handshake) */
4987#if defined(MBEDTLS_SSL_PROTO_DTLS)
4988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4989 {
4990 return( 0 );
4991 }
4992#endif
4993 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4994 }
4995#endif /* MBEDTLS_SSL_SRV_C */
4996
4997#if defined(MBEDTLS_SSL_RENEGOTIATION)
4998 /* Determine whether renegotiation attempt should be accepted */
4999 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5000 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5001 ssl->conf->allow_legacy_renegotiation ==
5002 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5003 {
5004 /*
5005 * Accept renegotiation request
5006 */
5007
5008 /* DTLS clients need to know renego is server-initiated */
5009#if defined(MBEDTLS_SSL_PROTO_DTLS)
5010 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5011 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5012 {
5013 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5014 }
5015#endif
5016 ret = mbedtls_ssl_start_renegotiation( ssl );
5017 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5018 ret != 0 )
5019 {
5020 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5021 ret );
5022 return( ret );
5023 }
5024 }
5025 else
5026#endif /* MBEDTLS_SSL_RENEGOTIATION */
5027 {
5028 /*
5029 * Refuse renegotiation
5030 */
5031
5032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5033
TRodziewicz0f82ec62021-05-12 17:49:18 +02005034#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005035 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5036 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5037 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005038 {
TRodziewicz345165c2021-07-06 13:42:11 +02005039 return( ret );
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005040 }
TRodziewicz0f82ec62021-05-12 17:49:18 +02005041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005042 }
5043
5044 return( 0 );
5045}
5046
Paul Bakker48916f92012-09-16 19:57:18 +00005047/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 * Receive application data decrypted from the SSL layer
5049 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005051{
Janos Follath865b3eb2019-12-16 11:46:15 +00005052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005053 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005054
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005055 if( ssl == NULL || ssl->conf == NULL )
5056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005063 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005064 return( ret );
5065
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005066 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005068 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005069 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005070 return( ret );
5071 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005072 }
5073#endif
5074
Hanno Becker4a810fb2017-05-24 16:27:30 +01005075 /*
5076 * Check if renegotiation is necessary and/or handshake is
5077 * in process. If yes, perform/continue, and fall through
5078 * if an unexpected packet is received while the client
5079 * is waiting for the ServerHello.
5080 *
5081 * (There is no equivalent to the last condition on
5082 * the server-side as it is not treated as within
5083 * a handshake while waiting for the ClientHello
5084 * after a renegotiation request.)
5085 */
5086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005088 ret = ssl_check_ctr_renegotiate( ssl );
5089 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5090 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005093 return( ret );
5094 }
5095#endif
5096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005097 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005100 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5101 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 return( ret );
5105 }
5106 }
5107
Hanno Beckere41158b2017-10-23 13:30:32 +01005108 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005109 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005111 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005112 if( ssl->f_get_timer != NULL &&
5113 ssl->f_get_timer( ssl->p_timer ) == -1 )
5114 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005115 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005116 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005117
Hanno Becker327c93b2018-08-15 13:56:18 +01005118 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005119 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005120 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5121 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005122
Hanno Becker4a810fb2017-05-24 16:27:30 +01005123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5124 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005125 }
5126
5127 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005129 {
5130 /*
5131 * OpenSSL sends empty messages to randomize the IV
5132 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005133 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005136 return( 0 );
5137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005139 return( ret );
5140 }
5141 }
5142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005144 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005145 ret = ssl_handle_hs_message_post_handshake( ssl );
5146 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005147 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005148 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5149 ret );
5150 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005151 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005152
Hanno Beckerf26cc722021-04-21 07:30:13 +01005153 /* At this point, we don't know whether the renegotiation triggered
5154 * by the post-handshake message has been completed or not. The cases
5155 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005156 * 1) The renegotiation is complete. In this case, no new record
5157 * has been read yet.
5158 * 2) The renegotiation is incomplete because the client received
5159 * an application data record while awaiting the ServerHello.
5160 * 3) The renegotiation is incomplete because the client received
5161 * a non-handshake, non-application data message while awaiting
5162 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005163 *
5164 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005165 * - For 1), the next iteration will read a new record and check
5166 * if it's application data.
5167 * - For 2), the loop condition isn't satisfied as application data
5168 * is present, hence continue is the same as break
5169 * - For 3), the loop condition is satisfied and read_record
5170 * will re-deliver the message that was held back by the client
5171 * when expecting the ServerHello.
5172 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005173
Hanno Becker90333da2017-10-10 11:27:13 +01005174 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005175 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005176#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005177 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005178 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005179 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005180 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005181 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005184 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005186 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005187 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005189#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5192 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005195 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005196 }
5197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5201 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005202 }
5203
5204 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005205
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005206 /* We're going to return something now, cancel timer,
5207 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005209 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005210
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005211#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005212 /* If we requested renego but received AppData, resend HelloRequest.
5213 * Do it now, after setting in_offt, to avoid taking this branch
5214 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005216 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005218 {
Hanno Becker786300f2020-02-05 10:46:40 +00005219 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005220 {
Hanno Becker786300f2020-02-05 10:46:40 +00005221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5222 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005223 return( ret );
5224 }
5225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005227#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005228 }
5229
5230 n = ( len < ssl->in_msglen )
5231 ? len : ssl->in_msglen;
5232
5233 memcpy( buf, ssl->in_offt, n );
5234 ssl->in_msglen -= n;
5235
gabor-mezei-arma3214132020-07-15 10:55:00 +02005236 /* Zeroising the plaintext buffer to erase unused application data
5237 from the memory. */
5238 mbedtls_platform_zeroize( ssl->in_offt, n );
5239
Paul Bakker5121ce52009-01-03 21:22:43 +00005240 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005241 {
5242 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005243 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005244 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005245 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005247 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005248 /* more data available */
5249 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005250 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005253
Paul Bakker23986e52011-04-24 08:57:21 +00005254 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005255}
5256
5257/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005258 * Send application data to be encrypted by the SSL layer, taking care of max
5259 * fragment length and buffer size.
5260 *
5261 * According to RFC 5246 Section 6.2.1:
5262 *
5263 * Zero-length fragments of Application data MAY be sent as they are
5264 * potentially useful as a traffic analysis countermeasure.
5265 *
5266 * Therefore, it is possible that the input message length is 0 and the
5267 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005268 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005269static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005270 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005271{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005272 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5273 const size_t max_len = (size_t) ret;
5274
5275 if( ret < 0 )
5276 {
5277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5278 return( ret );
5279 }
5280
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005281 if( len > max_len )
5282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005284 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005287 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5288 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005289 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005291 }
5292 else
5293#endif
5294 len = max_len;
5295 }
Paul Bakker887bd502011-06-08 13:10:54 +00005296
Paul Bakker5121ce52009-01-03 21:22:43 +00005297 if( ssl->out_left != 0 )
5298 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005299 /*
5300 * The user has previously tried to send the data and
5301 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5302 * written. In this case, we expect the high-level write function
5303 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005305 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005308 return( ret );
5309 }
5310 }
Paul Bakker887bd502011-06-08 13:10:54 +00005311 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005312 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005313 /*
5314 * The user is trying to send a message the first time, so we need to
5315 * copy the data into the internal buffers and setup the data structure
5316 * to keep track of partial writes
5317 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005318 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005319 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005320 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005321
Hanno Becker67bc7c32018-08-06 11:33:50 +01005322 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005325 return( ret );
5326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005327 }
5328
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005329 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005330}
5331
5332/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005333 * Write application data (public-facing wrapper)
5334 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005335int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005336{
Janos Follath865b3eb2019-12-16 11:46:15 +00005337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005338
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005340
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005341 if( ssl == NULL || ssl->conf == NULL )
5342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5343
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005344#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005345 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5346 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005347 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005348 return( ret );
5349 }
5350#endif
5351
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005352 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005353 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005354 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005355 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005356 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005357 return( ret );
5358 }
5359 }
5360
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005361 ret = ssl_write_real( ssl, buf, len );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005362
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005364
5365 return( ret );
5366}
5367
5368/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005369 * Notify the peer that the connection is being closed
5370 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005372{
Janos Follath865b3eb2019-12-16 11:46:15 +00005373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005374
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005375 if( ssl == NULL || ssl->conf == NULL )
5376 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005379
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005380 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005381 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5386 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5387 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005390 return( ret );
5391 }
5392 }
5393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005395
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005396 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005397}
5398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005400{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005401 if( transform == NULL )
5402 return;
5403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005404 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5405 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005406
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005407#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielce37d112022-01-13 14:53:52 +01005408 psa_destroy_key( transform->psa_key_enc );
5409 psa_destroy_key( transform->psa_key_dec );
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01005410#endif
5411
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005413 mbedtls_md_free( &transform->md_ctx_enc );
5414 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005415#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005416
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005417 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005418}
5419
Jerry Yuc7875b52021-09-05 21:05:50 +08005420void mbedtls_ssl_set_inbound_transform( mbedtls_ssl_context *ssl,
5421 mbedtls_ssl_transform *transform )
5422{
Jerry Yuc7875b52021-09-05 21:05:50 +08005423 ssl->transform_in = transform;
Jerry Yufd320e92021-10-08 21:52:41 +08005424 memset( ssl->in_ctr, 0, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN );
Jerry Yuc7875b52021-09-05 21:05:50 +08005425}
5426
5427void mbedtls_ssl_set_outbound_transform( mbedtls_ssl_context *ssl,
5428 mbedtls_ssl_transform *transform )
5429{
5430 ssl->transform_out = transform;
Jerry Yufd320e92021-10-08 21:52:41 +08005431 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
Jerry Yuc7875b52021-09-05 21:05:50 +08005432}
5433
Hanno Becker0271f962018-08-16 13:23:47 +01005434#if defined(MBEDTLS_SSL_PROTO_DTLS)
5435
Hanno Becker533ab5f2020-02-05 10:49:13 +00005436void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005437{
5438 unsigned offset;
5439 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5440
5441 if( hs == NULL )
5442 return;
5443
Hanno Becker283f5ef2018-08-24 09:34:47 +01005444 ssl_free_buffered_record( ssl );
5445
Hanno Becker0271f962018-08-16 13:23:47 +01005446 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005447 ssl_buffering_free_slot( ssl, offset );
5448}
5449
5450static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5451 uint8_t slot )
5452{
5453 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5454 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005455
5456 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5457 return;
5458
Hanno Beckere605b192018-08-21 15:59:07 +01005459 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005460 {
Hanno Beckere605b192018-08-21 15:59:07 +01005461 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005462 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005463 mbedtls_free( hs_buf->data );
5464 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005465 }
5466}
5467
5468#endif /* MBEDTLS_SSL_PROTO_DTLS */
5469
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005470/*
5471 * Convert version numbers to/from wire format
5472 * and, for DTLS, to/from TLS equivalent.
5473 *
5474 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005475 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005476 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005479 unsigned char ver[2] )
5480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481#if defined(MBEDTLS_SSL_PROTO_DTLS)
5482 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005485 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5486
5487 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5488 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5489 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005490 else
5491#else
5492 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005493#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005494 {
5495 ver[0] = (unsigned char) major;
5496 ver[1] = (unsigned char) minor;
5497 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005498}
5499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005500void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005501 const unsigned char ver[2] )
5502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005503#if defined(MBEDTLS_SSL_PROTO_DTLS)
5504 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005505 {
5506 *major = 255 - ver[0] + 2;
5507 *minor = 255 - ver[1] + 1;
5508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005510 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5511 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005512 else
5513#else
5514 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005515#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005516 {
5517 *major = ver[0];
5518 *minor = ver[1];
5519 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005520}
5521
Jerry Yue7047812021-09-13 19:26:39 +08005522/*
Jerry Yu3bf1f972021-09-22 21:37:18 +08005523 * Send pending fatal alert.
5524 * 0, No alert message.
5525 * !0, if mbedtls_ssl_send_alert_message() returned in error, the error code it
5526 * returned, ssl->alert_reason otherwise.
Jerry Yue7047812021-09-13 19:26:39 +08005527 */
5528int mbedtls_ssl_handle_pending_alert( mbedtls_ssl_context *ssl )
5529{
5530 int ret;
5531
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005532 /* No pending alert, return success*/
5533 if( ssl->send_alert == 0 )
5534 return( 0 );
Jerry Yu394ece62021-09-14 22:17:21 +08005535
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005536 ret = mbedtls_ssl_send_alert_message( ssl,
5537 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5538 ssl->alert_type );
5539
Jerry Yu3bf1f972021-09-22 21:37:18 +08005540 /* If mbedtls_ssl_send_alert_message() returned with MBEDTLS_ERR_SSL_WANT_WRITE,
5541 * do not clear the alert to be able to send it later.
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005542 */
5543 if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
5544 {
5545 ssl->send_alert = 0;
Jerry Yue7047812021-09-13 19:26:39 +08005546 }
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005547
5548 if( ret != 0 )
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005549 return( ret );
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005550
Jerry Yubbd5a3f2021-09-18 20:50:22 +08005551 return( ssl->alert_reason );
Jerry Yue7047812021-09-13 19:26:39 +08005552}
5553
Jerry Yu394ece62021-09-14 22:17:21 +08005554/*
5555 * Set pending fatal alert flag.
5556 */
5557void mbedtls_ssl_pend_fatal_alert( mbedtls_ssl_context *ssl,
5558 unsigned char alert_type,
5559 int alert_reason )
5560{
5561 ssl->send_alert = 1;
5562 ssl->alert_type = alert_type;
5563 ssl->alert_reason = alert_reason;
5564}
5565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005566#endif /* MBEDTLS_SSL_TLS_C */