blob: bc4f9d1911ae8a5088f445eb8ff94de419790eec [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-mezei-armdb9a38c2021-09-27 11:28:54 +020043#include "constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020044
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020045#include "ssl_invasive.h"
46
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <string.h>
48
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050049#if defined(MBEDTLS_USE_PSA_CRYPTO)
50#include "mbedtls/psa_util.h"
51#include "psa/crypto.h"
52#endif
53
Janos Follath23bdca02016-10-07 14:47:14 +010054#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020056#endif
57
Hanno Beckercd9dcda2018-08-28 17:18:56 +010058static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010059
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020060/*
61 * Start a timer.
62 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020063 */
Hanno Becker0f57a652020-02-05 10:37:26 +000064void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020065{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020066 if( ssl->f_set_timer == NULL )
67 return;
68
69 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
70 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020071}
72
73/*
74 * Return -1 is timer is expired, 0 if it isn't.
75 */
Hanno Becker7876d122020-02-05 10:39:31 +000076int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020078 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020079 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020080
81 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020082 {
83 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020085 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086
87 return( 0 );
88}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089
TRodziewicz4ca18aa2021-05-20 14:46:20 +020090static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
91 unsigned char *buf,
92 size_t len,
93 mbedtls_record *rec );
94
95int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
96 unsigned char *buf,
97 size_t buflen )
98{
99 int ret = 0;
100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
101 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
102
103 /* We don't support record checking in TLS because
TRodziewicz2abf03c2021-06-25 14:40:09 +0200104 * there doesn't seem to be a usecase for it.
TRodziewicz4ca18aa2021-05-20 14:46:20 +0200105 */
106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
107 {
108 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
109 goto exit;
110 }
111#if defined(MBEDTLS_SSL_PROTO_DTLS)
112 else
113 {
114 mbedtls_record rec;
115
116 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
117 if( ret != 0 )
118 {
119 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
120 goto exit;
121 }
122
123 if( ssl->transform_in != NULL )
124 {
125 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
126 if( ret != 0 )
127 {
128 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
129 goto exit;
130 }
131 }
132 }
133#endif /* MBEDTLS_SSL_PROTO_DTLS */
134
135exit:
136 /* On success, we have decrypted the buffer in-place, so make
137 * sure we don't leak any plaintext data. */
138 mbedtls_platform_zeroize( buf, buflen );
139
140 /* For the purpose of this API, treat messages with unexpected CID
141 * as well as such from future epochs as unexpected. */
142 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
143 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
144 {
145 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
146 }
147
148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
149 return( ret );
150}
151
Hanno Becker67bc7c32018-08-06 11:33:50 +0100152#define SSL_DONT_FORCE_FLUSH 0
153#define SSL_FORCE_FLUSH 1
154
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100156
Hanno Beckerd5847772018-08-28 10:09:23 +0100157/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100158static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
159 uint8_t slot );
160static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
161static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
162static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
163static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100164static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
165 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100166static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100167
Hanno Becker11682cc2018-08-22 14:41:02 +0100168static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100169{
Hanno Becker89490712020-02-05 10:50:12 +0000170 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000171#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
172 size_t out_buf_len = ssl->out_buf_len;
173#else
174 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
175#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100176
Darryl Greenb33cc762019-11-28 14:29:44 +0000177 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100178 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100179
Darryl Greenb33cc762019-11-28 14:29:44 +0000180 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100181}
182
Hanno Becker67bc7c32018-08-06 11:33:50 +0100183static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
184{
Hanno Becker11682cc2018-08-22 14:41:02 +0100185 size_t const bytes_written = ssl->out_left;
186 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100187
188 /* Double-check that the write-index hasn't gone
189 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100190 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100191 {
192 /* Should never happen... */
193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
194 }
195
196 return( (int) ( mtu - bytes_written ) );
197}
198
199static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
200{
Janos Follath865b3eb2019-12-16 11:46:15 +0000201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100202 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400203 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100204
205#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400206 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100207
208 if( max_len > mfl )
209 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100210
211 /* By the standard (RFC 6066 Sect. 4), the MFL extension
212 * only limits the maximum record payload size, so in theory
213 * we would be allowed to pack multiple records of payload size
214 * MFL into a single datagram. However, this would mean that there's
215 * no way to explicitly communicate MTU restrictions to the peer.
216 *
217 * The following reduction of max_len makes sure that we never
218 * write datagrams larger than MFL + Record Expansion Overhead.
219 */
220 if( max_len <= ssl->out_left )
221 return( 0 );
222
223 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100224#endif
225
226 ret = ssl_get_remaining_space_in_datagram( ssl );
227 if( ret < 0 )
228 return( ret );
229 remaining = (size_t) ret;
230
231 ret = mbedtls_ssl_get_record_expansion( ssl );
232 if( ret < 0 )
233 return( ret );
234 expansion = (size_t) ret;
235
236 if( remaining <= expansion )
237 return( 0 );
238
239 remaining -= expansion;
240 if( remaining >= max_len )
241 remaining = max_len;
242
243 return( (int) remaining );
244}
245
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200246/*
247 * Double the retransmit timeout value, within the allowed range,
248 * returning -1 if the maximum value has already been reached.
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200251{
252 uint32_t new_timeout;
253
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200254 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200255 return( -1 );
256
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200257 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
258 * in the following way: after the initial transmission and a first
259 * retransmission, back off to a temporary estimated MTU of 508 bytes.
260 * This value is guaranteed to be deliverable (if not guaranteed to be
261 * delivered) of any compliant IPv4 (and IPv6) network, and should work
262 * on most non-IP stacks too. */
263 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400264 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200265 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
267 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200268
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200269 new_timeout = 2 * ssl->handshake->retransmit_timeout;
270
271 /* Avoid arithmetic overflow and range overflow */
272 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200273 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200274 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200275 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200276 }
277
278 ssl->handshake->retransmit_timeout = new_timeout;
Paul Elliott9f352112020-12-09 14:55:45 +0000279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
280 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281
282 return( 0 );
283}
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200286{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200287 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Paul Elliott9f352112020-12-09 14:55:45 +0000288 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
289 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200290}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200292
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100293/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200295 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000296
Hanno Beckerccc13d02020-05-04 12:30:04 +0100297#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
298 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100299
300static size_t ssl_compute_padding_length( size_t len,
301 size_t granularity )
302{
303 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
304}
305
Hanno Becker581bc1b2020-05-04 12:20:03 +0100306/* This functions transforms a (D)TLS plaintext fragment and a record content
307 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
308 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
309 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100310 *
311 * struct {
312 * opaque content[DTLSPlaintext.length];
313 * ContentType real_type;
314 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100315 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100316 *
317 * Input:
318 * - `content`: The beginning of the buffer holding the
319 * plaintext to be wrapped.
320 * - `*content_size`: The length of the plaintext in Bytes.
321 * - `max_len`: The number of Bytes available starting from
322 * `content`. This must be `>= *content_size`.
323 * - `rec_type`: The desired record content type.
324 *
325 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100326 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
327 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100328 *
329 * Returns:
330 * - `0` on success.
331 * - A negative error code if `max_len` didn't offer enough space
332 * for the expansion.
333 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100334static int ssl_build_inner_plaintext( unsigned char *content,
335 size_t *content_size,
336 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100337 uint8_t rec_type,
338 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100339{
340 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100341
342 /* Write real content type */
343 if( remaining == 0 )
344 return( -1 );
345 content[ len ] = rec_type;
346 len++;
347 remaining--;
348
349 if( remaining < pad )
350 return( -1 );
351 memset( content + len, 0, pad );
352 len += pad;
353 remaining -= pad;
354
355 *content_size = len;
356 return( 0 );
357}
358
Hanno Becker581bc1b2020-05-04 12:20:03 +0100359/* This function parses a (D)TLSInnerPlaintext structure.
360 * See ssl_build_inner_plaintext() for details. */
361static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100362 size_t *content_size,
363 uint8_t *rec_type )
364{
365 size_t remaining = *content_size;
366
367 /* Determine length of padding by skipping zeroes from the back. */
368 do
369 {
370 if( remaining == 0 )
371 return( -1 );
372 remaining--;
373 } while( content[ remaining ] == 0 );
374
375 *content_size = remaining;
376 *rec_type = content[ remaining ];
377
378 return( 0 );
379}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100380#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
381 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100382
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100383/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100384 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000385static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100386 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100387 mbedtls_record *rec,
388 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000389{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100390 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100391 *
392 * additional_data = seq_num + TLSCompressed.type +
393 * TLSCompressed.version + TLSCompressed.length;
394 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100395 * For the CID extension, this is extended as follows
396 * (quoting draft-ietf-tls-dtls-connection-id-05,
397 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100398 *
399 * additional_data = seq_num + DTLSPlaintext.type +
400 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100401 * cid +
402 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100403 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100404 *
405 * For TLS 1.3, the record sequence number is dropped from the AAD
406 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100407 */
408
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100409 unsigned char *cur = add_data;
410
411#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
412 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
413#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
414 {
415 ((void) minor_ver);
416 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
417 cur += sizeof( rec->ctr );
418 }
419
420 *cur = rec->type;
421 cur++;
422
423 memcpy( cur, rec->ver, sizeof( rec->ver ) );
424 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100425
Hanno Beckera0e20d02019-05-15 14:03:01 +0100426#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100427 if( rec->cid_len != 0 )
428 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100429 memcpy( cur, rec->cid, rec->cid_len );
430 cur += rec->cid_len;
431
432 *cur = rec->cid_len;
433 cur++;
434
435 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
436 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
437 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100438 }
439 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100440#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100441 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100442 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
443 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
444 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100445 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100446
447 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000448}
449
Hanno Becker67a37db2020-05-28 16:27:07 +0100450#if defined(MBEDTLS_GCM_C) || \
451 defined(MBEDTLS_CCM_C) || \
452 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100453static int ssl_transform_aead_dynamic_iv_is_explicit(
454 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100455{
Hanno Becker17263802020-05-28 07:05:48 +0100456 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100457}
458
Hanno Becker17263802020-05-28 07:05:48 +0100459/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
460 *
461 * Concretely, this occurs in two variants:
462 *
463 * a) Fixed and dynamic IV lengths add up to total IV length, giving
464 * IV = fixed_iv || dynamic_iv
465 *
Hanno Becker15952812020-06-04 13:31:46 +0100466 * This variant is used in TLS 1.2 when used with GCM or CCM.
467 *
Hanno Becker17263802020-05-28 07:05:48 +0100468 * b) Fixed IV lengths matches total IV length, giving
469 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100470 *
471 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
472 *
473 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100474 *
475 * This function has the precondition that
476 *
477 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
478 *
479 * which has to be ensured by the caller. If this precondition
480 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100481 */
482static void ssl_build_record_nonce( unsigned char *dst_iv,
483 size_t dst_iv_len,
484 unsigned char const *fixed_iv,
485 size_t fixed_iv_len,
486 unsigned char const *dynamic_iv,
487 size_t dynamic_iv_len )
488{
489 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100490
491 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100492 memset( dst_iv, 0, dst_iv_len );
493 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100494
Hanno Becker17263802020-05-28 07:05:48 +0100495 dst_iv += dst_iv_len - dynamic_iv_len;
496 for( i = 0; i < dynamic_iv_len; i++ )
497 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100498}
Hanno Becker67a37db2020-05-28 16:27:07 +0100499#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100500
Hanno Beckera18d1322018-01-03 14:27:32 +0000501int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
502 mbedtls_ssl_transform *transform,
503 mbedtls_record *rec,
504 int (*f_rng)(void *, unsigned char *, size_t),
505 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100508 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000509 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100510 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100511 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000512 size_t post_avail;
513
514 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000515#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200516 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000517 ((void) ssl);
518#endif
519
520 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200521 * for CBC transformations in TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200522#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
TRodziewicz0f82ec62021-05-12 17:49:18 +0200523 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000524 ((void) f_rng);
525 ((void) p_rng);
526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000530 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100531 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
533 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
534 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100535 if( rec == NULL
536 || rec->buf == NULL
537 || rec->buf_len < rec->data_offset
538 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100539#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100540 || rec->cid_len != 0
541#endif
542 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000543 {
544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100546 }
547
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000548 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100549 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000551 data, rec->data_len );
552
553 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
554
555 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
556 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
558 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000559 rec->data_len,
560 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
562 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100563
Hanno Becker92313402020-05-20 13:58:58 +0100564 /* The following two code paths implement the (D)TLSInnerPlaintext
565 * structure present in TLS 1.3 and DTLS 1.2 + CID.
566 *
567 * See ssl_build_inner_plaintext() for more information.
568 *
569 * Note that this changes `rec->data_len`, and hence
570 * `post_avail` needs to be recalculated afterwards.
571 *
572 * Note also that the two code paths cannot occur simultaneously
573 * since they apply to different versions of the protocol. There
574 * is hence no risk of double-addition of the inner plaintext.
575 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100576#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
577 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
578 {
Hanno Becker13996922020-05-28 16:15:19 +0100579 size_t padding =
580 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200581 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100582 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100583 &rec->data_len,
584 post_avail,
585 rec->type,
586 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100587 {
588 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
589 }
590
591 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
592 }
593#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
594
Hanno Beckera0e20d02019-05-15 14:03:01 +0100595#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100596 /*
597 * Add CID information
598 */
599 rec->cid_len = transform->out_cid_len;
600 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
601 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100602
603 if( rec->cid_len != 0 )
604 {
Hanno Becker13996922020-05-28 16:15:19 +0100605 size_t padding =
606 ssl_compute_padding_length( rec->data_len,
TRodziewicze8dd7092021-05-12 14:19:11 +0200607 MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100608 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100609 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100610 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100611 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100612 * Note that this changes `rec->data_len`, and hence
613 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100614 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100615 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100616 &rec->data_len,
617 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100618 rec->type,
619 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100620 {
621 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
622 }
623
624 rec->type = MBEDTLS_SSL_MSG_CID;
625 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100626#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100627
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100628 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
629
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100631 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000633#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( mode == MBEDTLS_MODE_STREAM ||
635 ( mode == MBEDTLS_MODE_CBC
636#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000637 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100638#endif
639 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000640 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000641 if( post_avail < transform->maclen )
642 {
643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
644 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
645 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200646#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +0200647 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
Hanno Becker992b6872017-11-09 18:57:39 +0000648
TRodziewicz345165c2021-07-06 13:42:11 +0200649 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
650 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000651
TRodziewicz345165c2021-07-06 13:42:11 +0200652 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
653 add_data_len );
654 mbedtls_md_hmac_update( &transform->md_ctx_enc, data, rec->data_len );
655 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
656 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000657
TRodziewicz345165c2021-07-06 13:42:11 +0200658 memcpy( data + rec->data_len, mac, transform->maclen );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200659#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200660
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000661 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
662 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200663
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000664 rec->data_len += transform->maclen;
665 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100666 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200667 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000668#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200670 /*
671 * Encrypt
672 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000673#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000676 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000677 size_t olen;
Paul Elliottd48d5c62021-01-07 14:47:05 +0000678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000679 "including %d bytes of padding",
680 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000682 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
683 transform->iv_enc, transform->ivlen,
684 data, rec->data_len,
685 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200688 return( ret );
689 }
690
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000691 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
694 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200695 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 }
Paul Bakker68884e32013-01-07 18:20:04 +0100697 else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000698#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000699
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200700#if defined(MBEDTLS_GCM_C) || \
701 defined(MBEDTLS_CCM_C) || \
702 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200704 mode == MBEDTLS_MODE_CCM ||
705 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000706 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200708 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100709 unsigned char *dynamic_iv;
710 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100711 int dynamic_iv_is_explicit =
712 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000713
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100714 /* Check that there's space for the authentication tag. */
715 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000716 {
717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
718 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
719 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000720
Paul Bakker68884e32013-01-07 18:20:04 +0100721 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100722 * Build nonce for AEAD encryption.
723 *
724 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
725 * part of the IV is prepended to the ciphertext and
726 * can be chosen freely - in particular, it need not
727 * agree with the record sequence number.
728 * However, since ChaChaPoly as well as all AEAD modes
729 * in TLS 1.3 use the record sequence number as the
730 * dynamic part of the nonce, we uniformly use the
731 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100732 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100733 dynamic_iv = rec->ctr;
734 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200735
Hanno Becker17263802020-05-28 07:05:48 +0100736 ssl_build_record_nonce( iv, sizeof( iv ),
737 transform->iv_enc,
738 transform->fixed_ivlen,
739 dynamic_iv,
740 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100741
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100742 /*
743 * Build additional data for AEAD encryption.
744 * This depends on the TLS version.
745 */
746 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
747 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100748
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200749 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100750 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200751 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100752 dynamic_iv,
753 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000754 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100755 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000756 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200757 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000758 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000759
Paul Bakker68884e32013-01-07 18:20:04 +0100760 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200761 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200762 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000763
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100764 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000765 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100766 add_data, add_data_len,
767 data, rec->data_len, /* src */
768 data, rec->buf_len - (data - rec->buf), /* dst */
769 &rec->data_len,
770 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200771 {
TRodziewicz18efb732021-04-29 23:12:19 +0200772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200773 return( ret );
774 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000775 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100776 data + rec->data_len - transform->taglen,
777 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100778 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000779 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100780
781 /*
782 * Prefix record content with dynamic IV in case it is explicit.
783 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100784 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100785 {
786 if( rec->data_offset < dynamic_iv_len )
787 {
788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
789 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
790 }
791
792 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
793 rec->data_offset -= dynamic_iv_len;
794 rec->data_len += dynamic_iv_len;
795 }
796
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100797 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000798 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000799 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100800#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200801#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000805 size_t padlen, i;
806 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000807
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000808 /* Currently we're always using minimal padding
809 * (up to 255 bytes would be allowed). */
810 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
811 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 padlen = 0;
813
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000814 /* Check there's enough space in the buffer for the padding. */
815 if( post_avail < padlen + 1 )
816 {
817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
818 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
819 }
820
Paul Bakker5121ce52009-01-03 21:22:43 +0000821 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000822 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000823
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000824 rec->data_len += padlen + 1;
825 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000826
TRodziewicz0f82ec62021-05-12 17:49:18 +0200827#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000828 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +0200829 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +0000830 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000831 */
TRodziewicz345165c2021-07-06 13:42:11 +0200832 if( f_rng == NULL )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000833 {
TRodziewicz345165c2021-07-06 13:42:11 +0200834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000836 }
TRodziewicz345165c2021-07-06 13:42:11 +0200837
838 if( rec->data_offset < transform->ivlen )
839 {
840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
841 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
842 }
843
844 /*
845 * Generate IV
846 */
847 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
848 if( ret != 0 )
849 return( ret );
850
851 memcpy( data - transform->ivlen, transform->iv_enc, transform->ivlen );
TRodziewicz0f82ec62021-05-12 17:49:18 +0200852#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000853
Paul Elliottd48d5c62021-01-07 14:47:05 +0000854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
855 "including %" MBEDTLS_PRINTF_SIZET
856 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000857 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200858 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000859
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000860 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
861 transform->iv_enc,
862 transform->ivlen,
863 data, rec->data_len,
864 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200867 return( ret );
868 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200869
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000870 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200871 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200874 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200875
TRodziewicz0f82ec62021-05-12 17:49:18 +0200876 data -= transform->ivlen;
877 rec->data_offset -= transform->ivlen;
878 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100881 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100882 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000883 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
884
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100885 /*
886 * MAC(MAC_write_key, seq_num +
887 * TLSCipherText.type +
888 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100889 * length_of( (IV +) ENC(...) ) +
TRodziewicz2abf03c2021-06-25 14:40:09 +0200890 * IV +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100891 * ENC(content + padding + padding_length));
892 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000893
894 if( post_avail < transform->maclen)
895 {
896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
897 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
898 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100899
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100900 ssl_extract_add_data_from_record( add_data, &add_data_len,
901 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000904 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100905 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100906
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000907 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100908 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000909 mbedtls_md_hmac_update( &transform->md_ctx_enc,
910 data, rec->data_len );
911 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
912 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100913
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000914 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100915
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000916 rec->data_len += transform->maclen;
917 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100918 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100919 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200922 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200923#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
926 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200927 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100929 /* Make extra sure authentication was performed, exactly once */
930 if( auth_done != 1 )
931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100934 }
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
938 return( 0 );
939}
940
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +0200941#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200942
943/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +0200944 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200945 *
946 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
947 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +0200948 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +0200949MBEDTLS_STATIC_TESTABLE int mbedtls_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +0200950 mbedtls_md_context_t *ctx,
951 const unsigned char *add_data, size_t add_data_len,
952 const unsigned char *data, size_t data_len_secret,
953 size_t min_data_len, size_t max_data_len,
954 unsigned char *output )
955{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200956 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200957 * This function breaks the HMAC abstraction and uses the md_clone()
958 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200959 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200960 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +0200961 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200962 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200963 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200964 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
965 * minlen, then cloning the context, and for each byte up to maxlen
966 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200967 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200968 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200969 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200970 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
TRodziewicz2abf03c2021-06-25 14:40:09 +0200971 /* TLS 1.2 only supports SHA-384, SHA-256, SHA-1, MD-5,
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +0200972 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200973 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +0200974 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200975 const unsigned char * const okey = ikey + block_size;
976 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200977
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200978 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
979 mbedtls_md_context_t aux;
980 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +0200981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +0200982
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200983 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +0200984
985#define MD_CHK( func_call ) \
986 do { \
987 ret = (func_call); \
988 if( ret != 0 ) \
989 goto cleanup; \
990 } while( 0 )
991
992 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200993
994 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
995 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +0200996 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
997 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200998
999 /* For each possible length, compute the hash up to that point */
1000 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001001 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001002 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1003 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001004 /* Keep only the correct inner_hash in the output buffer */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001005 mbedtls_cf_memcpy_if_eq( output, aux_out, hash_size,
1006 offset, data_len_secret );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001007
1008 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001009 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001010 }
1011
Manuel Pégourié-Gonnard5ca21db2021-05-17 12:28:08 +02001012 /* The context needs to finish() before it starts() again */
1013 MD_CHK( mbedtls_md_finish( ctx, aux_out ) );
1014
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001015 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001016 MD_CHK( mbedtls_md_starts( ctx ) );
1017 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1018 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1019 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001020
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001021 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001022 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001023
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001024#undef MD_CHK
1025
1026cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001027 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001028 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001029}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001030
1031/*
1032 * Constant-flow memcpy from variable position in buffer.
1033 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001034 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001035 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001036MBEDTLS_STATIC_TESTABLE void mbedtls_cf_memcpy_offset(
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001037 unsigned char *dst,
1038 const unsigned char *src_base,
1039 size_t offset_secret,
1040 size_t offset_min, size_t offset_max,
1041 size_t len )
1042{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001043 size_t offset;
1044
1045 for( offset = offset_min; offset <= offset_max; offset++ )
1046 {
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001047 mbedtls_cf_memcpy_if_eq( dst, src_base + offset, len,
1048 offset, offset_secret );
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001049 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001050}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001051#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001052
Hanno Becker605949f2019-07-12 08:23:59 +01001053int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001054 mbedtls_ssl_transform *transform,
1055 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001057 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001059 int ret, auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001060#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001061 size_t padlen = 0, correct = 1;
1062#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001063 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001064 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001065 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001066
Hanno Beckera18d1322018-01-03 14:27:32 +00001067#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001068 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001069 ((void) ssl);
1070#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001073 if( rec == NULL ||
1074 rec->buf == NULL ||
1075 rec->buf_len < rec->data_offset ||
1076 rec->buf_len - rec->data_offset < rec->data_len )
1077 {
1078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001080 }
1081
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001082 data = rec->buf + rec->data_offset;
1083 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084
Hanno Beckera0e20d02019-05-15 14:03:01 +01001085#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001086 /*
1087 * Match record's CID with incoming CID.
1088 */
Hanno Becker938489a2019-05-08 13:02:22 +01001089 if( rec->cid_len != transform->in_cid_len ||
1090 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1091 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001092 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001093 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001094#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001095
Hanno Beckerd086bf02021-03-22 13:01:27 +00001096#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001098 {
1099 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001100 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1101 transform->iv_dec,
1102 transform->ivlen,
1103 data, rec->data_len,
1104 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001107 return( ret );
1108 }
1109
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001110 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1113 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001114 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 }
Paul Bakker68884e32013-01-07 18:20:04 +01001116 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001117#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001118#if defined(MBEDTLS_GCM_C) || \
1119 defined(MBEDTLS_CCM_C) || \
1120 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001122 mode == MBEDTLS_MODE_CCM ||
1123 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001124 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001125 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001126 unsigned char *dynamic_iv;
1127 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001128
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001129 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001130 * Extract dynamic part of nonce for AEAD decryption.
1131 *
1132 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1133 * part of the IV is prepended to the ciphertext and
1134 * can be chosen freely - in particular, it need not
1135 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001136 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001137 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001138 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001139 {
1140 if( rec->data_len < dynamic_iv_len )
1141 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1143 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001144 rec->data_len,
1145 dynamic_iv_len ) );
1146 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1147 }
1148 dynamic_iv = data;
1149
1150 data += dynamic_iv_len;
1151 rec->data_offset += dynamic_iv_len;
1152 rec->data_len -= dynamic_iv_len;
1153 }
Hanno Becker17263802020-05-28 07:05:48 +01001154 else
1155 {
1156 dynamic_iv = rec->ctr;
1157 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001158
1159 /* Check that there's space for the authentication tag. */
1160 if( rec->data_len < transform->taglen )
1161 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1163 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001164 rec->data_len,
1165 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001167 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001168 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001169
Hanno Beckerdf8be222020-05-21 15:30:57 +01001170 /*
1171 * Prepare nonce from dynamic and static parts.
1172 */
Hanno Becker17263802020-05-28 07:05:48 +01001173 ssl_build_record_nonce( iv, sizeof( iv ),
1174 transform->iv_dec,
1175 transform->fixed_ivlen,
1176 dynamic_iv,
1177 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001178
Hanno Beckerdf8be222020-05-21 15:30:57 +01001179 /*
1180 * Build additional data for AEAD encryption.
1181 * This depends on the TLS version.
1182 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001183 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1184 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001185 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001186 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001187
Hanno Beckerd96a6522019-07-10 13:55:25 +01001188 /* Because of the check above, we know that there are
1189 * explicit_iv_len Bytes preceeding data, and taglen
1190 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001191 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001192 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001193
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001194 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001195 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001196 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001197
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001198 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001199 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001200 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001201 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001202 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001203 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001204 data, rec->data_len + transform->taglen, /* src */
1205 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001206 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 {
TRodziewicz18efb732021-04-29 23:12:19 +02001208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1211 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001212
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001213 return( ret );
1214 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001215 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001216
Hanno Beckerd96a6522019-07-10 13:55:25 +01001217 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001218 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1221 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001222 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001223 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001226#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001229 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 /*
Paul Bakker45829992013-01-03 14:52:21 +01001232 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001234#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001235 /* The ciphertext is prefixed with the CBC IV. */
1236 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001237#endif
Paul Bakker45829992013-01-03 14:52:21 +01001238
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001239 /* Size considerations:
1240 *
1241 * - The CBC cipher text must not be empty and hence
1242 * at least of size transform->ivlen.
1243 *
1244 * Together with the potential IV-prefix, this explains
1245 * the first of the two checks below.
1246 *
1247 * - The record must contain a MAC, either in plain or
1248 * encrypted, depending on whether Encrypt-then-MAC
1249 * is used or not.
1250 * - If it is, the message contains the IV-prefix,
1251 * the CBC ciphertext, and the MAC.
1252 * - If it is not, the padded plaintext, and hence
1253 * the CBC ciphertext, has at least length maclen + 1
1254 * because there is at least the padding length byte.
1255 *
1256 * As the CBC ciphertext is not empty, both cases give the
1257 * lower bound minlen + maclen + 1 on the record size, which
1258 * we test for in the second check below.
1259 */
1260 if( rec->data_len < minlen + transform->ivlen ||
1261 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001262 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1264 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1265 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001266 "+ 1 ) ( + expl IV )", rec->data_len,
1267 transform->ivlen,
1268 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001270 }
1271
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001272 /*
1273 * Authenticate before decrypt if enabled
1274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001276 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001277 {
Hanno Becker992b6872017-11-09 18:57:39 +00001278 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001281
Hanno Beckerd96a6522019-07-10 13:55:25 +01001282 /* Update data_len in tandem with add_data.
1283 *
1284 * The subtraction is safe because of the previous check
1285 * data_len >= minlen + maclen + 1.
1286 *
1287 * Afterwards, we know that data + data_len is followed by at
1288 * least maclen Bytes, which justifies the call to
1289 * mbedtls_ssl_safer_memcmp() below.
1290 *
1291 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001292 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001293 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1294 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001295
Hanno Beckerd96a6522019-07-10 13:55:25 +01001296 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001297 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1298 add_data_len );
1299 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1300 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001301 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1302 data, rec->data_len );
1303 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1304 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001305
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001306 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1307 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001308 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001309 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001310
Hanno Beckerd96a6522019-07-10 13:55:25 +01001311 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001312 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1313 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001317 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001318 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001321
1322 /*
1323 * Check length sanity
1324 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001325
1326 /* We know from above that data_len > minlen >= 0,
1327 * so the following check in particular implies that
1328 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001329 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001330 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1332 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001333 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001335 }
1336
TRodziewicz0f82ec62021-05-12 17:49:18 +02001337#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001338 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001339 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001340 */
TRodziewicz345165c2021-07-06 13:42:11 +02001341 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
1342 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001343
TRodziewicz345165c2021-07-06 13:42:11 +02001344 data += transform->ivlen;
1345 rec->data_offset += transform->ivlen;
1346 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001347#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001348
Hanno Beckerd96a6522019-07-10 13:55:25 +01001349 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1350
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001351 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1352 transform->iv_dec, transform->ivlen,
1353 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001356 return( ret );
1357 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001358
Hanno Beckerd96a6522019-07-10 13:55:25 +01001359 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001360 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001364 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001365
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001366 /* Safe since data_len >= minlen + maclen + 1, so after having
1367 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001368 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1369 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001370 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001371
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001372 if( auth_done == 1 )
1373 {
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001374 const size_t mask = mbedtls_cf_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001375 rec->data_len,
1376 padlen + 1 );
1377 correct &= mask;
1378 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001379 }
1380 else
Paul Bakker45829992013-01-03 14:52:21 +01001381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001383 if( rec->data_len < transform->maclen + padlen + 1 )
1384 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1386 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1387 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001388 rec->data_len,
1389 transform->maclen,
1390 padlen + 1 ) );
1391 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001392#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001393
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001394 const size_t mask = mbedtls_cf_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001395 rec->data_len,
1396 transform->maclen + padlen + 1 );
1397 correct &= mask;
1398 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001399 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001400
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001401 padlen++;
1402
1403 /* Regardless of the validity of the padding,
1404 * we have data_len >= padlen here. */
1405
TRodziewicz0f82ec62021-05-12 17:49:18 +02001406#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001407 /* The padding check involves a series of up to 256
1408 * consecutive memory reads at the end of the record
1409 * plaintext buffer. In order to hide the length and
1410 * validity of the padding, always perform exactly
1411 * `min(256,plaintext_len)` reads (but take into account
1412 * only the last `padlen` bytes for the padding check). */
1413 size_t pad_count = 0;
1414 volatile unsigned char* const check = data;
1415
1416 /* Index of first padding byte; it has been ensured above
1417 * that the subtraction is safe. */
1418 size_t const padding_idx = rec->data_len - padlen;
1419 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1420 size_t const start_idx = rec->data_len - num_checks;
1421 size_t idx;
1422
1423 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001425 /* pad_count += (idx >= padding_idx) &&
1426 * (check[idx] == padlen - 1);
1427 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001428 const size_t mask = mbedtls_cf_size_mask_ge( idx, padding_idx );
1429 const size_t equal = mbedtls_cf_size_bool_eq( check[idx],
1430 padlen - 1 );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001431 pad_count += mask & equal;
1432 }
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001433 correct &= mbedtls_cf_size_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001436 if( padlen > 0 && correct == 0 )
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001438#endif
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001439 padlen &= mbedtls_cf_size_mask( correct );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001440
TRodziewicz0f82ec62021-05-12 17:49:18 +02001441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001442
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001443 /* If the padding was found to be invalid, padlen == 0
1444 * and the subtraction is safe. If the padding was found valid,
1445 * padlen hasn't been changed and the previous assertion
1446 * data_len >= padlen still holds. */
1447 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001448 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001449 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001450#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001455
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001456#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001458 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001459#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001460
1461 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001462 * Authenticate if not done yet.
1463 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001464 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001465#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001466 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001467 {
Hanno Becker992b6872017-11-09 18:57:39 +00001468 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001469 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001470
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001471 /* If the initial value of padlen was such that
1472 * data_len < maclen + padlen + 1, then padlen
1473 * got reset to 1, and the initial check
1474 * data_len >= minlen + maclen + 1
1475 * guarantees that at this point we still
1476 * have at least data_len >= maclen.
1477 *
1478 * If the initial value of padlen was such that
1479 * data_len >= maclen + padlen + 1, then we have
1480 * subtracted either padlen + 1 (if the padding was correct)
1481 * or 0 (if the padding was incorrect) since then,
1482 * hence data_len >= maclen in any case.
1483 */
1484 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001485 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1486 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
TRodziewicz0f82ec62021-05-12 17:49:18 +02001488#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001489 /*
1490 * The next two sizes are the minimum and maximum values of
1491 * data_len over all padlen values.
1492 *
1493 * They're independent of padlen, since we previously did
1494 * data_len -= padlen.
1495 *
1496 * Note that max_len + maclen is never more than the buffer
1497 * length, as we previously did in_msglen -= maclen too.
1498 */
1499 const size_t max_len = rec->data_len + padlen;
1500 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1501
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001502 ret = mbedtls_cf_hmac( &transform->md_ctx_dec,
1503 add_data, add_data_len,
1504 data, rec->data_len, min_len, max_len,
1505 mac_expect );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001506 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001507 {
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001508 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cf_hmac", ret );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001509 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001510 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001511
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001512 mbedtls_cf_memcpy_offset( mac_peer, data,
1513 rec->data_len,
1514 min_len, max_len,
1515 transform->maclen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001518#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001519 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001520 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001521#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001522
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001523 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001524 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_SSL_DEBUG_ALL)
1527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001528#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001529 correct = 0;
1530 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001531 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001532 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001533
1534 /*
1535 * Finally check the correct flag
1536 */
1537 if( correct == 0 )
1538 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001539#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001540
1541 /* Make extra sure authentication was performed, exactly once */
1542 if( auth_done != 1 )
1543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001547
Hanno Beckerccc13d02020-05-04 12:30:04 +01001548#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1549 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1550 {
1551 /* Remove inner padding and infer true content type. */
1552 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1553 &rec->type );
1554
1555 if( ret != 0 )
1556 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1557 }
1558#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1559
Hanno Beckera0e20d02019-05-15 14:03:01 +01001560#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001561 if( rec->cid_len != 0 )
1562 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001563 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1564 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001565 if( ret != 0 )
1566 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1567 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001568#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 return( 0 );
1573}
1574
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001575#undef MAC_NONE
1576#undef MAC_PLAINTEXT
1577#undef MAC_CIPHERTEXT
1578
Paul Bakker5121ce52009-01-03 21:22:43 +00001579/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001580 * Fill the input message buffer by appending data to it.
1581 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001582 *
1583 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1584 * available (from this read and/or a previous one). Otherwise, an error code
1585 * is returned (possibly EOF or WANT_READ).
1586 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001587 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1588 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1589 * since we always read a whole datagram at once.
1590 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001591 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001592 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001595{
Janos Follath865b3eb2019-12-16 11:46:15 +00001596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001597 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001598#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1599 size_t in_buf_len = ssl->in_buf_len;
1600#else
1601 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1602#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001606 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001609 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001611 }
1612
Darryl Greenb33cc762019-11-28 14:29:44 +00001613 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1616 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001617 }
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001622 uint32_t timeout;
1623
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001624 /*
1625 * The point is, we need to always read a full datagram at once, so we
1626 * sometimes read more then requested, and handle the additional data.
1627 * It could be the rest of the current record (while fetching the
1628 * header) and/or some other records in the same datagram.
1629 */
1630
1631 /*
1632 * Move to the next record in the already read datagram if applicable
1633 */
1634 if( ssl->next_record_offset != 0 )
1635 {
1636 if( ssl->in_left < ssl->next_record_offset )
1637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1639 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001640 }
1641
1642 ssl->in_left -= ssl->next_record_offset;
1643
1644 if( ssl->in_left != 0 )
1645 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001646 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1647 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001648 ssl->next_record_offset ) );
1649 memmove( ssl->in_hdr,
1650 ssl->in_hdr + ssl->next_record_offset,
1651 ssl->in_left );
1652 }
1653
1654 ssl->next_record_offset = 0;
1655 }
1656
Paul Elliottd48d5c62021-01-07 14:47:05 +00001657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1658 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001660
1661 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001662 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001663 */
1664 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001667 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001668 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001669
1670 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001671 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001672 * are not at the beginning of a new record, the caller did something
1673 * wrong.
1674 */
1675 if( ssl->in_left != 0 )
1676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1678 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001679 }
1680
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001681 /*
1682 * Don't even try to read if time's out already.
1683 * This avoids by-passing the timer when repeatedly receiving messages
1684 * that will end up being dropped.
1685 */
Hanno Becker7876d122020-02-05 10:39:31 +00001686 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001687 {
1688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001689 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001690 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001691 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001692 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001693 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001696 timeout = ssl->handshake->retransmit_timeout;
1697 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001698 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001699
Paul Elliott9f352112020-12-09 14:55:45 +00001700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001701
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001702 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001703 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1704 timeout );
1705 else
1706 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001709
1710 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001712 }
1713
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001714 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001717 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001720 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001721 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001724 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001725 }
1726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001730 return( ret );
1731 }
1732
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001733 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001734 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001736 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001738 {
Hanno Becker786300f2020-02-05 10:46:40 +00001739 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001740 {
Hanno Becker786300f2020-02-05 10:46:40 +00001741 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1742 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001743 return( ret );
1744 }
1745
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001746 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001747 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001749 }
1750
Paul Bakker5121ce52009-01-03 21:22:43 +00001751 if( ret < 0 )
1752 return( ret );
1753
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001754 ssl->in_left = ret;
1755 }
1756 else
1757#endif
1758 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1760 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001761 ssl->in_left, nb_want ) );
1762
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001763 while( ssl->in_left < nb_want )
1764 {
1765 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001766
Hanno Becker7876d122020-02-05 10:39:31 +00001767 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001768 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1769 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001770 {
1771 if( ssl->f_recv_timeout != NULL )
1772 {
1773 ret = ssl->f_recv_timeout( ssl->p_bio,
1774 ssl->in_hdr + ssl->in_left, len,
1775 ssl->conf->read_timeout );
1776 }
1777 else
1778 {
1779 ret = ssl->f_recv( ssl->p_bio,
1780 ssl->in_hdr + ssl->in_left, len );
1781 }
1782 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001783
Paul Elliottd48d5c62021-01-07 14:47:05 +00001784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1785 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001786 ssl->in_left, nb_want ) );
1787 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001788
1789 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001791
1792 if( ret < 0 )
1793 return( ret );
1794
makise-homuraaf9513b2020-08-24 18:26:27 +03001795 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001796 {
Darryl Green11999bb2018-03-13 15:22:58 +00001797 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001798 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001799 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001800 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1801 }
1802
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001803 ssl->in_left += ret;
1804 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 }
1806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001808
1809 return( 0 );
1810}
1811
1812/*
1813 * Flush any data not yet written
1814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001816{
Janos Follath865b3eb2019-12-16 11:46:15 +00001817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001818 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001821
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001822 if( ssl->f_send == NULL )
1823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001825 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001827 }
1828
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001829 /* Avoid incrementing counter if data is flushed */
1830 if( ssl->out_left == 0 )
1831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001833 return( 0 );
1834 }
1835
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 while( ssl->out_left > 0 )
1837 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
1839 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01001840 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
Hanno Becker2b1e3542018-08-06 11:19:13 +01001842 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001843 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
1847 if( ret <= 0 )
1848 return( ret );
1849
makise-homuraaf9513b2020-08-24 18:26:27 +03001850 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08001851 {
Darryl Green11999bb2018-03-13 15:22:58 +00001852 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001853 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00001854 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08001855 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1856 }
1857
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 ssl->out_left -= ret;
1859 }
1860
Hanno Becker2b1e3542018-08-06 11:19:13 +01001861#if defined(MBEDTLS_SSL_PROTO_DTLS)
1862 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001863 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01001864 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001865 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01001866 else
1867#endif
1868 {
1869 ssl->out_hdr = ssl->out_buf + 8;
1870 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001871 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
1875 return( 0 );
1876}
1877
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001878/*
1879 * Functions to handle the DTLS retransmission state machine
1880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001882/*
1883 * Append current handshake message to current outgoing flight
1884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001886{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01001888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
1889 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
1890 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001891
1892 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001893 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001894 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001897 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001898 }
1899
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001900 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001901 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
1903 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001905 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001906 }
1907
1908 /* Copy current handshake message with headers */
1909 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
1910 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001911 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001912 msg->next = NULL;
1913
1914 /* Append to the current flight */
1915 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001916 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001917 else
1918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001920 while( cur->next != NULL )
1921 cur = cur->next;
1922 cur->next = msg;
1923 }
1924
Hanno Becker3b235902018-08-06 09:54:53 +01001925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001926 return( 0 );
1927}
1928
1929/*
1930 * Free the current flight of handshake messages
1931 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00001932void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_ssl_flight_item *cur = flight;
1935 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001936
1937 while( cur != NULL )
1938 {
1939 next = cur->next;
1940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_free( cur->p );
1942 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001943
1944 cur = next;
1945 }
1946}
1947
1948/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001949 * Swap transform_out and out_ctr with the alternative ones
1950 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001951static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001952{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001954 unsigned char tmp_out_ctr[8];
1955
1956 if( ssl->transform_out == ssl->handshake->alt_transform_out )
1957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001959 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001960 }
1961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001963
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001964 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001965 tmp_transform = ssl->transform_out;
1966 ssl->transform_out = ssl->handshake->alt_transform_out;
1967 ssl->handshake->alt_transform_out = tmp_transform;
1968
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001969 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01001970 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
1971 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001972 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001973
1974 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001975 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001976
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001977 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001978}
1979
1980/*
1981 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001982 */
1983int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
1984{
1985 int ret = 0;
1986
1987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
1988
1989 ret = mbedtls_ssl_flight_transmit( ssl );
1990
1991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
1992
1993 return( ret );
1994}
1995
1996/*
1997 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001998 *
1999 * Need to remember the current message in case flush_output returns
2000 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002001 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002002 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002003int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002004{
Janos Follath865b3eb2019-12-16 11:46:15 +00002005 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002009 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002011
2012 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002013 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002014 ret = ssl_swap_epochs( ssl );
2015 if( ret != 0 )
2016 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002019 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002020
2021 while( ssl->handshake->cur_msg != NULL )
2022 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002023 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002024 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002025
Hanno Beckere1dcb032018-08-17 16:47:58 +01002026 int const is_finished =
2027 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2028 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2029
Hanno Becker04da1892018-08-14 13:22:10 +01002030 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2031 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2032
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002033 /* Swap epochs before sending Finished: we can't do it after
2034 * sending ChangeCipherSpec, in case write returns WANT_READ.
2035 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002036 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002037 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002039 ret = ssl_swap_epochs( ssl );
2040 if( ret != 0 )
2041 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002042 }
2043
Hanno Becker67bc7c32018-08-06 11:33:50 +01002044 ret = ssl_get_remaining_payload_in_datagram( ssl );
2045 if( ret < 0 )
2046 return( ret );
2047 max_frag_len = (size_t) ret;
2048
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002049 /* CCS is copied as is, while HS messages may need fragmentation */
2050 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2051 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002052 if( max_frag_len == 0 )
2053 {
2054 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2055 return( ret );
2056
2057 continue;
2058 }
2059
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002060 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002061 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002062 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002063
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002064 /* Update position inside current message */
2065 ssl->handshake->cur_msg_p += cur->len;
2066 }
2067 else
2068 {
2069 const unsigned char * const p = ssl->handshake->cur_msg_p;
2070 const size_t hs_len = cur->len - 12;
2071 const size_t frag_off = p - ( cur->p + 12 );
2072 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002073 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002074
Hanno Beckere1dcb032018-08-17 16:47:58 +01002075 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002076 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002077 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002078 {
2079 ret = ssl_swap_epochs( ssl );
2080 if( ret != 0 )
2081 return( ret );
2082 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002083
Hanno Becker67bc7c32018-08-06 11:33:50 +01002084 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2085 return( ret );
2086
2087 continue;
2088 }
2089 max_hs_frag_len = max_frag_len - 12;
2090
2091 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2092 max_hs_frag_len : rem_len;
2093
2094 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002095 {
2096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002097 (unsigned) cur_hs_frag_len,
2098 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002099 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002100
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002101 /* Messages are stored with handshake headers as if not fragmented,
2102 * copy beginning of headers then fill fragmentation fields.
2103 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2104 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002105
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002106 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2107 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2108 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2109
Hanno Becker67bc7c32018-08-06 11:33:50 +01002110 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2111 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2112 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002113
2114 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2115
Hanno Becker3f7b9732018-08-28 09:53:25 +01002116 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002117 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2118 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002119 ssl->out_msgtype = cur->type;
2120
2121 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002122 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002123 }
2124
2125 /* If done with the current message move to the next one if any */
2126 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2127 {
2128 if( cur->next != NULL )
2129 {
2130 ssl->handshake->cur_msg = cur->next;
2131 ssl->handshake->cur_msg_p = cur->next->p + 12;
2132 }
2133 else
2134 {
2135 ssl->handshake->cur_msg = NULL;
2136 ssl->handshake->cur_msg_p = NULL;
2137 }
2138 }
2139
2140 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002141 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002144 return( ret );
2145 }
2146 }
2147
Hanno Becker67bc7c32018-08-06 11:33:50 +01002148 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2149 return( ret );
2150
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002151 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2153 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002154 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002157 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002158 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002159
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002161
2162 return( 0 );
2163}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002164
2165/*
2166 * To be called when the last message of an incoming flight is received.
2167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002169{
2170 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002171 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002172 ssl->handshake->flight = NULL;
2173 ssl->handshake->cur_msg = NULL;
2174
2175 /* The next incoming flight will start with this msg_seq */
2176 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2177
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002178 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002179 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002180
Hanno Becker0271f962018-08-16 13:23:47 +01002181 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002182 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002183
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002184 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002185 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2188 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002191 }
2192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002194}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002195
2196/*
2197 * To be called when the last message of an outgoing flight is send.
2198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002200{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002201 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002202 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2205 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002208 }
2209 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002213
Paul Bakker5121ce52009-01-03 21:22:43 +00002214/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002215 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002217
2218/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002219 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002220 *
2221 * - fill in handshake headers
2222 * - update handshake checksum
2223 * - DTLS: save message for resending
2224 * - then pass to the record layer
2225 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002226 * DTLS: except for HelloRequest, messages are only queued, and will only be
2227 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002228 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002229 * Inputs:
2230 * - ssl->out_msglen: 4 + actual handshake message len
2231 * (4 is the size of handshake headers for TLS)
2232 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2233 * - ssl->out_msg + 4: the handshake message body
2234 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002235 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002236 * - ssl->out_msglen: the length of the record contents
2237 * (including handshake headers but excluding record headers)
2238 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002239 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002240int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002241{
Janos Follath865b3eb2019-12-16 11:46:15 +00002242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002243 const size_t hs_len = ssl->out_msglen - 4;
2244 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2247
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002248 /*
2249 * Sanity checks
2250 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002251 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002252 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2253 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002254 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2255 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002256 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002257
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002258 /* Whenever we send anything different from a
2259 * HelloRequest we should be in a handshake - double check. */
2260 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2261 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002262 ssl->handshake == NULL )
2263 {
2264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002270 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002272 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2274 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002275 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002276#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002277
Hanno Beckerb50a2532018-08-06 11:52:54 +01002278 /* Double-check that we did not exceed the bounds
2279 * of the outgoing record buffer.
2280 * This should never fail as the various message
2281 * writing functions must obey the bounds of the
2282 * outgoing record buffer, but better be safe.
2283 *
2284 * Note: We deliberately do not check for the MTU or MFL here.
2285 */
2286 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2287 {
2288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002289 "size %" MBEDTLS_PRINTF_SIZET
2290 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002291 ssl->out_msglen,
2292 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2294 }
2295
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002296 /*
2297 * Fill handshake headers
2298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002301 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2302 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2303 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002304
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002305 /*
2306 * DTLS has additional fields in the Handshake layer,
2307 * between the length field and the actual payload:
2308 * uint16 message_seq;
2309 * uint24 fragment_offset;
2310 * uint24 fragment_length;
2311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002314 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002315 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002316 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002317 {
2318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002319 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002320 hs_len,
2321 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2323 }
2324
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002325 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002326 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002327
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002328 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002329 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002330 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002331 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2332 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2333 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002334 }
2335 else
2336 {
2337 ssl->out_msg[4] = 0;
2338 ssl->out_msg[5] = 0;
2339 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002340
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002341 /* Handshake hashes are computed without fragmentation,
2342 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002343 memset( ssl->out_msg + 6, 0x00, 3 );
2344 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002347
Hanno Becker0207e532018-08-28 10:28:28 +01002348 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002349 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2350 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 }
2352
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002353 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002355 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002356 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2357 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002358 {
2359 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002362 return( ret );
2363 }
2364 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002365 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002366#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002367 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002368 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002369 {
2370 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2371 return( ret );
2372 }
2373 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002374
2375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2376
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002377 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002378}
2379
2380/*
2381 * Record layer functions
2382 */
2383
2384/*
2385 * Write current record.
2386 *
2387 * Uses:
2388 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2389 * - ssl->out_msglen: length of the record content (excl headers)
2390 * - ssl->out_msg: record content
2391 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002392int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002393{
2394 int ret, done = 0;
2395 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002396 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002397
2398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002399
Paul Bakker05ef8352012-05-08 09:17:57 +00002400 if( !done )
2401 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002402 unsigned i;
2403 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002404#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2405 size_t out_buf_len = ssl->out_buf_len;
2406#else
2407 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2408#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002409 /* Skip writing the record content type to after the encryption,
2410 * as it may change when using the CID extension. */
2411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002413 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002414
Hanno Becker19859472018-08-06 09:40:20 +01002415 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002416 ssl->out_len[0] = (unsigned char)( len >> 8 );
2417 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002418
Paul Bakker48916f92012-09-16 19:57:18 +00002419 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002420 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002421 mbedtls_record rec;
2422
2423 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002424 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002425 rec.data_len = ssl->out_msglen;
2426 rec.data_offset = ssl->out_msg - rec.buf;
2427
2428 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2429 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2430 ssl->conf->transport, rec.ver );
2431 rec.type = ssl->out_msgtype;
2432
Hanno Beckera0e20d02019-05-15 14:03:01 +01002433#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002434 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002435 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002436#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002437
Hanno Beckera18d1322018-01-03 14:27:32 +00002438 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002442 return( ret );
2443 }
2444
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 if( rec.data_offset != 0 )
2446 {
2447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2449 }
2450
Hanno Becker6430faf2019-05-08 11:57:13 +01002451 /* Update the record content type and CID. */
2452 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002453#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002454 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002455#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002456 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2458 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002459 }
2460
Hanno Becker5903de42019-05-03 14:46:38 +01002461 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002462
2463#if defined(MBEDTLS_SSL_PROTO_DTLS)
2464 /* In case of DTLS, double-check that we don't exceed
2465 * the remaining space in the datagram. */
2466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2467 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002468 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002469 if( ret < 0 )
2470 return( ret );
2471
2472 if( protected_record_size > (size_t) ret )
2473 {
2474 /* Should never happen */
2475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2476 }
2477 }
2478#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002479
Hanno Becker6430faf2019-05-08 11:57:13 +01002480 /* Now write the potentially updated record content type. */
2481 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2482
Paul Elliott9f352112020-12-09 14:55:45 +00002483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002484 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002485 ssl->out_hdr[0], ssl->out_hdr[1],
2486 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002489 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002490
2491 ssl->out_left += protected_record_size;
2492 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002493 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002494
Hanno Beckerdd772292020-02-05 10:38:31 +00002495 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002496 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2497 break;
2498
2499 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002500 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002501 {
2502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2503 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506
Hanno Becker67bc7c32018-08-06 11:33:50 +01002507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2509 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002510 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002511 size_t remaining;
2512 ret = ssl_get_remaining_payload_in_datagram( ssl );
2513 if( ret < 0 )
2514 {
2515 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2516 ret );
2517 return( ret );
2518 }
2519
2520 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002521 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002522 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002523 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002524 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002525 else
2526 {
Hanno Becker513815a2018-08-20 11:56:09 +01002527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002528 }
2529 }
2530#endif /* MBEDTLS_SSL_PROTO_DTLS */
2531
2532 if( ( flush == SSL_FORCE_FLUSH ) &&
2533 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 return( ret );
2537 }
2538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
2541 return( 0 );
2542}
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002545
2546static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2547{
2548 if( ssl->in_msglen < ssl->in_hslen ||
2549 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2550 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2551 {
2552 return( 1 );
2553 }
2554 return( 0 );
2555}
Hanno Becker44650b72018-08-16 12:51:11 +01002556
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002557static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002558{
2559 return( ( ssl->in_msg[9] << 16 ) |
2560 ( ssl->in_msg[10] << 8 ) |
2561 ssl->in_msg[11] );
2562}
2563
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002564static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002565{
2566 return( ( ssl->in_msg[6] << 16 ) |
2567 ( ssl->in_msg[7] << 8 ) |
2568 ssl->in_msg[8] );
2569}
2570
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002571static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002572{
2573 uint32_t msg_len, frag_off, frag_len;
2574
2575 msg_len = ssl_get_hs_total_len( ssl );
2576 frag_off = ssl_get_hs_frag_off( ssl );
2577 frag_len = ssl_get_hs_frag_len( ssl );
2578
2579 if( frag_off > msg_len )
2580 return( -1 );
2581
2582 if( frag_len > msg_len - frag_off )
2583 return( -1 );
2584
2585 if( frag_len + 12 > ssl->in_msglen )
2586 return( -1 );
2587
2588 return( 0 );
2589}
2590
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002591/*
2592 * Mark bits in bitmask (used for DTLS HS reassembly)
2593 */
2594static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2595{
2596 unsigned int start_bits, end_bits;
2597
2598 start_bits = 8 - ( offset % 8 );
2599 if( start_bits != 8 )
2600 {
2601 size_t first_byte_idx = offset / 8;
2602
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002603 /* Special case */
2604 if( len <= start_bits )
2605 {
2606 for( ; len != 0; len-- )
2607 mask[first_byte_idx] |= 1 << ( start_bits - len );
2608
2609 /* Avoid potential issues with offset or len becoming invalid */
2610 return;
2611 }
2612
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002613 offset += start_bits; /* Now offset % 8 == 0 */
2614 len -= start_bits;
2615
2616 for( ; start_bits != 0; start_bits-- )
2617 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2618 }
2619
2620 end_bits = len % 8;
2621 if( end_bits != 0 )
2622 {
2623 size_t last_byte_idx = ( offset + len ) / 8;
2624
2625 len -= end_bits; /* Now len % 8 == 0 */
2626
2627 for( ; end_bits != 0; end_bits-- )
2628 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2629 }
2630
2631 memset( mask + offset / 8, 0xFF, len / 8 );
2632}
2633
2634/*
2635 * Check that bitmask is full
2636 */
2637static int ssl_bitmask_check( unsigned char *mask, size_t len )
2638{
2639 size_t i;
2640
2641 for( i = 0; i < len / 8; i++ )
2642 if( mask[i] != 0xFF )
2643 return( -1 );
2644
2645 for( i = 0; i < len % 8; i++ )
2646 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2647 return( -1 );
2648
2649 return( 0 );
2650}
2651
Hanno Becker56e205e2018-08-16 09:06:12 +01002652/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002653static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002654 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002655{
Hanno Becker56e205e2018-08-16 09:06:12 +01002656 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002657
Hanno Becker56e205e2018-08-16 09:06:12 +01002658 alloc_len = 12; /* Handshake header */
2659 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002660
Hanno Beckerd07df862018-08-16 09:14:58 +01002661 if( add_bitmap )
2662 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002663
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002664 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002665}
Hanno Becker56e205e2018-08-16 09:06:12 +01002666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002668
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002669static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002670{
2671 return( ( ssl->in_msg[1] << 16 ) |
2672 ( ssl->in_msg[2] << 8 ) |
2673 ssl->in_msg[3] );
2674}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002675
Simon Butcher99000142016-10-13 17:21:01 +01002676int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002678 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002679 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002681 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002683 }
2684
Hanno Becker12555c62018-08-16 12:47:53 +01002685 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002688 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002689 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002692 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002693 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002695 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002696
Hanno Becker44650b72018-08-16 12:51:11 +01002697 if( ssl_check_hs_header( ssl ) != 0 )
2698 {
2699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2700 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2701 }
2702
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002703 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002704 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2705 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2706 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2707 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002708 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002709 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2710 {
2711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2712 recv_msg_seq,
2713 ssl->handshake->in_msg_seq ) );
2714 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2715 }
2716
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002717 /* Retransmit only on last message from previous flight, to avoid
2718 * too many retransmissions.
2719 * Besides, No sane server ever retransmits HelloVerifyRequest */
2720 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002721 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002724 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002725 recv_msg_seq,
2726 ssl->handshake->in_flight_start_seq ) );
2727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002730 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002731 return( ret );
2732 }
2733 }
2734 else
2735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002737 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002738 recv_msg_seq,
2739 ssl->handshake->in_msg_seq ) );
2740 }
2741
Hanno Becker90333da2017-10-10 11:27:13 +01002742 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002743 }
2744 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002745
Hanno Becker6d97ef52018-08-16 13:09:04 +01002746 /* Message reassembly is handled alongside buffering of future
2747 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002748 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002749 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002750 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002753 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002754 }
2755 }
2756 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002758 /* With TLS we don't handle fragmentation (for now) */
2759 if( ssl->in_msglen < ssl->in_hslen )
2760 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2762 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002763 }
2764
Simon Butcher99000142016-10-13 17:21:01 +01002765 return( 0 );
2766}
2767
2768void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2769{
Hanno Becker0271f962018-08-16 13:23:47 +01002770 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002771
Hanno Becker0271f962018-08-16 13:23:47 +01002772 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002773 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002774 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002775 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002776
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002777 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002779 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002780 ssl->handshake != NULL )
2781 {
Hanno Becker0271f962018-08-16 13:23:47 +01002782 unsigned offset;
2783 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002784
Hanno Becker0271f962018-08-16 13:23:47 +01002785 /* Increment handshake sequence number */
2786 hs->in_msg_seq++;
2787
2788 /*
2789 * Clear up handshake buffering and reassembly structure.
2790 */
2791
2792 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002793 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002794
2795 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002796 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2797 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002798 offset++, hs_buf++ )
2799 {
2800 *hs_buf = *(hs_buf + 1);
2801 }
2802
2803 /* Create a fresh last entry */
2804 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002805 }
2806#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002807}
2808
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002809/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002810 * DTLS anti-replay: RFC 6347 4.1.2.6
2811 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002812 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2813 * Bit n is set iff record number in_window_top - n has been seen.
2814 *
2815 * Usually, in_window_top is the last record number seen and the lsb of
2816 * in_window is set. The only exception is the initial state (record number 0
2817 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002820void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002821{
2822 ssl->in_window_top = 0;
2823 ssl->in_window = 0;
2824}
2825
2826static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2827{
2828 return( ( (uint64_t) buf[0] << 40 ) |
2829 ( (uint64_t) buf[1] << 32 ) |
2830 ( (uint64_t) buf[2] << 24 ) |
2831 ( (uint64_t) buf[3] << 16 ) |
2832 ( (uint64_t) buf[4] << 8 ) |
2833 ( (uint64_t) buf[5] ) );
2834}
2835
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002836static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
2837{
Janos Follath865b3eb2019-12-16 11:46:15 +00002838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002839 unsigned char *original_in_ctr;
2840
2841 // save original in_ctr
2842 original_in_ctr = ssl->in_ctr;
2843
2844 // use counter from record
2845 ssl->in_ctr = record_in_ctr;
2846
2847 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
2848
2849 // restore the counter
2850 ssl->in_ctr = original_in_ctr;
2851
2852 return ret;
2853}
2854
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002855/*
2856 * Return 0 if sequence number is acceptable, -1 otherwise
2857 */
Hanno Becker0183d692019-07-12 08:50:37 +01002858int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *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 uint64_t bit;
2862
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002863 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002864 return( 0 );
2865
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002866 if( rec_seqnum > ssl->in_window_top )
2867 return( 0 );
2868
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002869 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002870
2871 if( bit >= 64 )
2872 return( -1 );
2873
2874 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2875 return( -1 );
2876
2877 return( 0 );
2878}
2879
2880/*
2881 * Update replay window on new validated record
2882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002883void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002884{
2885 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2886
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002887 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002888 return;
2889
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002890 if( rec_seqnum > ssl->in_window_top )
2891 {
2892 /* Update window_top and the contents of the window */
2893 uint64_t shift = rec_seqnum - ssl->in_window_top;
2894
2895 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002896 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002897 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002898 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002899 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002900 ssl->in_window |= 1;
2901 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002902
2903 ssl->in_window_top = rec_seqnum;
2904 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002905 else
2906 {
2907 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002908 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002909
2910 if( bit < 64 ) /* Always true, but be extra sure */
2911 ssl->in_window |= (uint64_t) 1 << bit;
2912 }
2913}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002915
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02002916#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002917/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002918 * Without any SSL context, check if a datagram looks like a ClientHello with
2919 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01002920 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002921 *
2922 * - if cookie is valid, return 0
2923 * - if ClientHello looks superficially valid but cookie is not,
2924 * fill obuf and set olen, then
2925 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
2926 * - otherwise return a specific error code
2927 */
2928static int ssl_check_dtls_clihlo_cookie(
2929 mbedtls_ssl_cookie_write_t *f_cookie_write,
2930 mbedtls_ssl_cookie_check_t *f_cookie_check,
2931 void *p_cookie,
2932 const unsigned char *cli_id, size_t cli_id_len,
2933 const unsigned char *in, size_t in_len,
2934 unsigned char *obuf, size_t buf_len, size_t *olen )
2935{
2936 size_t sid_len, cookie_len;
2937 unsigned char *p;
2938
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002939 /*
2940 * Structure of ClientHello with record and handshake headers,
2941 * and expected values. We don't need to check a lot, more checks will be
2942 * done when actually parsing the ClientHello - skipping those checks
2943 * avoids code duplication and does not make cookie forging any easier.
2944 *
2945 * 0-0 ContentType type; copied, must be handshake
2946 * 1-2 ProtocolVersion version; copied
2947 * 3-4 uint16 epoch; copied, must be 0
2948 * 5-10 uint48 sequence_number; copied
2949 * 11-12 uint16 length; (ignored)
2950 *
2951 * 13-13 HandshakeType msg_type; (ignored)
2952 * 14-16 uint24 length; (ignored)
2953 * 17-18 uint16 message_seq; copied
2954 * 19-21 uint24 fragment_offset; copied, must be 0
2955 * 22-24 uint24 fragment_length; (ignored)
2956 *
2957 * 25-26 ProtocolVersion client_version; (ignored)
2958 * 27-58 Random random; (ignored)
2959 * 59-xx SessionID session_id; 1 byte len + sid_len content
2960 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
2961 * ...
2962 *
2963 * Minimum length is 61 bytes.
2964 */
2965 if( in_len < 61 ||
2966 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
2967 in[3] != 0 || in[4] != 0 ||
2968 in[19] != 0 || in[20] != 0 || in[21] != 0 )
2969 {
Hanno Becker90d59dd2021-06-24 11:17:13 +01002970 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002971 }
2972
2973 sid_len = in[59];
2974 if( sid_len > in_len - 61 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01002975 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002976
2977 cookie_len = in[60 + sid_len];
2978 if( cookie_len > in_len - 60 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01002979 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002980
2981 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
2982 cli_id, cli_id_len ) == 0 )
2983 {
2984 /* Valid cookie */
2985 return( 0 );
2986 }
2987
2988 /*
2989 * If we get here, we've got an invalid cookie, let's prepare HVR.
2990 *
2991 * 0-0 ContentType type; copied
2992 * 1-2 ProtocolVersion version; copied
2993 * 3-4 uint16 epoch; copied
2994 * 5-10 uint48 sequence_number; copied
2995 * 11-12 uint16 length; olen - 13
2996 *
2997 * 13-13 HandshakeType msg_type; hello_verify_request
2998 * 14-16 uint24 length; olen - 25
2999 * 17-18 uint16 message_seq; copied
3000 * 19-21 uint24 fragment_offset; copied
3001 * 22-24 uint24 fragment_length; olen - 25
3002 *
3003 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3004 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3005 *
3006 * Minimum length is 28.
3007 */
3008 if( buf_len < 28 )
3009 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3010
3011 /* Copy most fields and adapt others */
3012 memcpy( obuf, in, 25 );
3013 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3014 obuf[25] = 0xfe;
3015 obuf[26] = 0xff;
3016
3017 /* Generate and write actual cookie */
3018 p = obuf + 28;
3019 if( f_cookie_write( p_cookie,
3020 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3021 {
3022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3023 }
3024
3025 *olen = p - obuf;
3026
3027 /* Go back and fill length fields */
3028 obuf[27] = (unsigned char)( *olen - 28 );
3029
3030 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3031 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3032 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3033
3034 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3035 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3036
3037 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3038}
3039
3040/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003041 * Handle possible client reconnect with the same UDP quadruplet
3042 * (RFC 6347 Section 4.2.8).
3043 *
3044 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3045 * that looks like a ClientHello.
3046 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003047 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003048 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003049 * - if the input looks like a ClientHello with a valid cookie,
3050 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003051 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003052 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003053 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003054 * This function is called (through ssl_check_client_reconnect()) when an
3055 * unexpected record is found in ssl_get_next_record(), which will discard the
3056 * record if we return 0, and bubble up the return value otherwise (this
3057 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3058 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003059 */
3060static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3061{
Janos Follath865b3eb2019-12-16 11:46:15 +00003062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003063 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003064
Hanno Becker2fddd372019-07-10 14:37:41 +01003065 if( ssl->conf->f_cookie_write == NULL ||
3066 ssl->conf->f_cookie_check == NULL )
3067 {
3068 /* If we can't use cookies to verify reachability of the peer,
3069 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3071 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003072 return( 0 );
3073 }
3074
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003075 ret = ssl_check_dtls_clihlo_cookie(
3076 ssl->conf->f_cookie_write,
3077 ssl->conf->f_cookie_check,
3078 ssl->conf->p_cookie,
3079 ssl->cli_id, ssl->cli_id_len,
3080 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003081 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003082
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003083 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3084
3085 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003086 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003087 int send_ret;
3088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3089 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3090 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003091 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003092 * If the error is permanent we'll catch it later,
3093 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003094 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3095 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3096 (void) send_ret;
3097
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003098 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003099 }
3100
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003101 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003102 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003104 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003105 {
3106 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3107 return( ret );
3108 }
3109
3110 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003111 }
3112
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003113 return( ret );
3114}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003115#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003116
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003117static int ssl_check_record_type( uint8_t record_type )
3118{
3119 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3120 record_type != MBEDTLS_SSL_MSG_ALERT &&
3121 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3122 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3123 {
3124 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3125 }
3126
3127 return( 0 );
3128}
3129
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003130/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003131 * ContentType type;
3132 * ProtocolVersion version;
3133 * uint16 epoch; // DTLS only
3134 * uint48 sequence_number; // DTLS only
3135 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003136 *
3137 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003138 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003139 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3140 *
3141 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003142 * 1. proceed with the record if this function returns 0
3143 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3144 * 3. return CLIENT_RECONNECT if this function return that value
3145 * 4. drop the whole datagram if this function returns anything else.
3146 * Point 2 is needed when the peer is resending, and we have already received
3147 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003148 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003149static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003150 unsigned char *buf,
3151 size_t len,
3152 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003153{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003154 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003155
Hanno Beckere5e7e782019-07-11 12:29:35 +01003156 size_t const rec_hdr_type_offset = 0;
3157 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003158
Hanno Beckere5e7e782019-07-11 12:29:35 +01003159 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3160 rec_hdr_type_len;
3161 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Hanno Beckere5e7e782019-07-11 12:29:35 +01003163 size_t const rec_hdr_ctr_len = 8;
3164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003165 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003166 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3167 rec_hdr_version_len;
3168
Hanno Beckera0e20d02019-05-15 14:03:01 +01003169#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003170 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3171 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003172 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3174#endif /* MBEDTLS_SSL_PROTO_DTLS */
3175
3176 size_t rec_hdr_len_offset; /* To be determined */
3177 size_t const rec_hdr_len_len = 2;
3178
3179 /*
3180 * Check minimum lengths for record header.
3181 */
3182
3183#if defined(MBEDTLS_SSL_PROTO_DTLS)
3184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3185 {
3186 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3187 }
3188 else
3189#endif /* MBEDTLS_SSL_PROTO_DTLS */
3190 {
3191 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3192 }
3193
3194 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3195 {
3196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3197 (unsigned) len,
3198 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3199 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3200 }
3201
3202 /*
3203 * Parse and validate record content type
3204 */
3205
3206 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003207
3208 /* Check record content type */
3209#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3210 rec->cid_len = 0;
3211
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003212 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003213 ssl->conf->cid_len != 0 &&
3214 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003215 {
3216 /* Shift pointers to account for record header including CID
3217 * struct {
3218 * ContentType special_type = tls12_cid;
3219 * ProtocolVersion version;
3220 * uint16 epoch;
3221 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003222 * opaque cid[cid_length]; // Additional field compared to
3223 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003224 * uint16 length;
3225 * opaque enc_content[DTLSCiphertext.length];
3226 * } DTLSCiphertext;
3227 */
3228
3229 /* So far, we only support static CID lengths
3230 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003231 rec_hdr_cid_len = ssl->conf->cid_len;
3232 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003233
Hanno Beckere5e7e782019-07-11 12:29:35 +01003234 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003235 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3237 (unsigned) len,
3238 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003239 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003240 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003241
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003242 /* configured CID len is guaranteed at most 255, see
3243 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3244 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003245 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003246 }
3247 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003248#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003249 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003250 if( ssl_check_record_type( rec->type ) )
3251 {
Hanno Becker54229812019-07-12 14:40:00 +01003252 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3253 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003254 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3255 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003256 }
3257
Hanno Beckere5e7e782019-07-11 12:29:35 +01003258 /*
3259 * Parse and validate record version
3260 */
3261
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003262 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3263 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003264 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3265 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003266 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003267
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003268 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3271 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 }
3273
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003274 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3277 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 }
3279
Hanno Beckere5e7e782019-07-11 12:29:35 +01003280 /*
3281 * Parse/Copy record sequence number.
3282 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003283
Hanno Beckere5e7e782019-07-11 12:29:35 +01003284#if defined(MBEDTLS_SSL_PROTO_DTLS)
3285 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003286 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003287 /* Copy explicit record sequence number from input buffer. */
3288 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3289 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003290 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003291 else
3292#endif /* MBEDTLS_SSL_PROTO_DTLS */
3293 {
3294 /* Copy implicit record sequence number from SSL context structure. */
3295 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3296 }
Paul Bakker40e46942009-01-03 21:51:57 +00003297
Hanno Beckere5e7e782019-07-11 12:29:35 +01003298 /*
3299 * Parse record length.
3300 */
3301
Hanno Beckere5e7e782019-07-11 12:29:35 +01003302 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003303 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3304 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003305 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Paul Elliott9f352112020-12-09 14:55:45 +00003307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003308 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003309 rec->type,
3310 major_ver, minor_ver, rec->data_len ) );
3311
3312 rec->buf = buf;
3313 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003314
Hanno Beckerd417cc92019-07-26 08:20:27 +01003315 if( rec->data_len == 0 )
3316 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003317
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003318 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003319 * DTLS-related tests.
3320 * Check epoch before checking length constraint because
3321 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3322 * message gets duplicated before the corresponding Finished message,
3323 * the second ChangeCipherSpec should be discarded because it belongs
3324 * to an old epoch, but not because its length is shorter than
3325 * the minimum record length for packets using the new record transform.
3326 * Note that these two kinds of failures are handled differently,
3327 * as an unexpected record is silently skipped but an invalid
3328 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003329 */
3330#if defined(MBEDTLS_SSL_PROTO_DTLS)
3331 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3332 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003333 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003334
Hanno Becker955a5c92019-07-10 17:12:07 +01003335 /* Check that the datagram is large enough to contain a record
3336 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003337 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003338 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3340 (unsigned) len,
3341 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003342 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3343 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003344
Hanno Becker37cfe732019-07-10 17:20:01 +01003345 /* Records from other, non-matching epochs are silently discarded.
3346 * (The case of same-port Client reconnects must be considered in
3347 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003348 if( rec_epoch != ssl->in_epoch )
3349 {
3350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003351 "expected %u, received %lu",
3352 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003353
Hanno Becker552f7472019-07-19 10:59:12 +01003354 /* Records from the next epoch are considered for buffering
3355 * (concretely: early Finished messages). */
3356 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003357 {
Hanno Becker552f7472019-07-19 10:59:12 +01003358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3359 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003360 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003361
Hanno Becker2fddd372019-07-10 14:37:41 +01003362 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003363 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003364#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003365 /* For records from the correct epoch, check whether their
3366 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003367 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3368 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003369 {
3370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3371 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3372 }
3373#endif
3374 }
3375#endif /* MBEDTLS_SSL_PROTO_DTLS */
3376
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003377 return( 0 );
3378}
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
Hanno Becker2fddd372019-07-10 14:37:41 +01003381#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3382static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3383{
3384 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3385
3386 /*
3387 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3388 * access the first byte of record content (handshake type), as we
3389 * have an active transform (possibly iv_len != 0), so use the
3390 * fact that the record header len is 13 instead.
3391 */
3392 if( rec_epoch == 0 &&
3393 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3394 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3395 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3396 ssl->in_left > 13 &&
3397 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3398 {
3399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3400 "from the same port" ) );
3401 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 }
3403
3404 return( 0 );
3405}
Hanno Becker2fddd372019-07-10 14:37:41 +01003406#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003407
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003408/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003409 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003410 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003411static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3412 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003413{
3414 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003417 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003418
Paul Bakker48916f92012-09-16 19:57:18 +00003419 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003421 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003422
Hanno Beckera18d1322018-01-03 14:27:32 +00003423 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003424 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003427
Hanno Beckera0e20d02019-05-15 14:03:01 +01003428#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003429 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3430 ssl->conf->ignore_unexpected_cid
3431 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3432 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003433 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003434 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003435 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003436#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003437
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 return( ret );
3439 }
3440
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003441 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003442 {
3443 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003444 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003445 }
3446
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003447 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003448 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003449
Hanno Beckera0e20d02019-05-15 14:03:01 +01003450#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003451 /* We have already checked the record content type
3452 * in ssl_parse_record_header(), failing or silently
3453 * dropping the record in the case of an unknown type.
3454 *
3455 * Since with the use of CIDs, the record content type
3456 * might change during decryption, re-check the record
3457 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003458 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003459 {
3460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3461 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3462 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003463#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003464
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003465 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003466 {
3467#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3468 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003469 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003470 {
3471 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3473 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3474 }
3475#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3476
3477 ssl->nb_zero++;
3478
3479 /*
3480 * Three or more empty messages may be a DoS attack
3481 * (excessive CPU consumption).
3482 */
3483 if( ssl->nb_zero > 3 )
3484 {
3485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003486 "messages, possible DoS attack" ) );
3487 /* Treat the records as if they were not properly authenticated,
3488 * thereby failing the connection if we see more than allowed
3489 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003490 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3491 }
3492 }
3493 else
3494 ssl->nb_zero = 0;
3495
3496#if defined(MBEDTLS_SSL_PROTO_DTLS)
3497 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3498 {
3499 ; /* in_ctr read from peer, not maintained internally */
3500 }
3501 else
3502#endif
3503 {
3504 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003505 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003506 if( ++ssl->in_ctr[i - 1] != 0 )
3507 break;
3508
3509 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003510 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003511 {
3512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3513 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3514 }
3515 }
3516
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 }
3518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003520 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003523 }
3524#endif
3525
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003526 /* Check actual (decrypted) record content length against
3527 * configured maximum. */
3528 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3529 {
3530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3531 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3532 }
3533
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003534 return( 0 );
3535}
3536
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003537/*
3538 * Read a record.
3539 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003540 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3541 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3542 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003543 */
Hanno Becker1097b342018-08-15 14:09:41 +01003544
3545/* Helper functions for mbedtls_ssl_read_record(). */
3546static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003547static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3548static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003549
Hanno Becker327c93b2018-08-15 13:56:18 +01003550int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003551 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003552{
Janos Follath865b3eb2019-12-16 11:46:15 +00003553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003556
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003557 if( ssl->keep_current_message == 0 )
3558 {
3559 do {
Simon Butcher99000142016-10-13 17:21:01 +01003560
Hanno Becker26994592018-08-15 14:14:59 +01003561 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003562 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003563 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003564
Hanno Beckere74d5562018-08-15 14:26:08 +01003565 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003566 {
Hanno Becker40f50842018-08-15 14:48:01 +01003567#if defined(MBEDTLS_SSL_PROTO_DTLS)
3568 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003569
Hanno Becker40f50842018-08-15 14:48:01 +01003570 /* We only check for buffered messages if the
3571 * current datagram is fully consumed. */
3572 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003573 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003574 {
Hanno Becker40f50842018-08-15 14:48:01 +01003575 if( ssl_load_buffered_message( ssl ) == 0 )
3576 have_buffered = 1;
3577 }
3578
3579 if( have_buffered == 0 )
3580#endif /* MBEDTLS_SSL_PROTO_DTLS */
3581 {
3582 ret = ssl_get_next_record( ssl );
3583 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3584 continue;
3585
3586 if( ret != 0 )
3587 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003588 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003589 return( ret );
3590 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003591 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003592 }
3593
3594 ret = mbedtls_ssl_handle_message_type( ssl );
3595
Hanno Becker40f50842018-08-15 14:48:01 +01003596#if defined(MBEDTLS_SSL_PROTO_DTLS)
3597 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3598 {
3599 /* Buffer future message */
3600 ret = ssl_buffer_message( ssl );
3601 if( ret != 0 )
3602 return( ret );
3603
3604 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3605 }
3606#endif /* MBEDTLS_SSL_PROTO_DTLS */
3607
Hanno Becker90333da2017-10-10 11:27:13 +01003608 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3609 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003610
3611 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003612 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003613 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003614 return( ret );
3615 }
3616
Hanno Becker327c93b2018-08-15 13:56:18 +01003617 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003618 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003619 {
3620 mbedtls_ssl_update_handshake_status( ssl );
3621 }
Simon Butcher99000142016-10-13 17:21:01 +01003622 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003623 else
Simon Butcher99000142016-10-13 17:21:01 +01003624 {
Hanno Becker02f59072018-08-15 14:00:24 +01003625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003626 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003627 }
3628
3629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3630
3631 return( 0 );
3632}
3633
Hanno Becker40f50842018-08-15 14:48:01 +01003634#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003635static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003636{
Hanno Becker40f50842018-08-15 14:48:01 +01003637 if( ssl->in_left > ssl->next_record_offset )
3638 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003639
Hanno Becker40f50842018-08-15 14:48:01 +01003640 return( 0 );
3641}
3642
3643static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3644{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003645 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003646 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003647 int ret = 0;
3648
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003649 if( hs == NULL )
3650 return( -1 );
3651
Hanno Beckere00ae372018-08-20 09:39:42 +01003652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3653
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003654 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3655 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3656 {
3657 /* Check if we have seen a ChangeCipherSpec before.
3658 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003659 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003660 {
3661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3662 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003663 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003664 }
3665
Hanno Becker39b8bc92018-08-28 17:17:13 +01003666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003667 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3668 ssl->in_msglen = 1;
3669 ssl->in_msg[0] = 1;
3670
3671 /* As long as they are equal, the exact value doesn't matter. */
3672 ssl->in_left = 0;
3673 ssl->next_record_offset = 0;
3674
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003675 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003676 goto exit;
3677 }
Hanno Becker37f95322018-08-16 13:55:32 +01003678
Hanno Beckerb8f50142018-08-28 10:01:34 +01003679#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003680 /* Debug only */
3681 {
3682 unsigned offset;
3683 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3684 {
3685 hs_buf = &hs->buffering.hs[offset];
3686 if( hs_buf->is_valid == 1 )
3687 {
3688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3689 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003690 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003691 }
3692 }
3693 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003694#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003695
3696 /* Check if we have buffered and/or fully reassembled the
3697 * next handshake message. */
3698 hs_buf = &hs->buffering.hs[0];
3699 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3700 {
3701 /* Synthesize a record containing the buffered HS message. */
3702 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3703 ( hs_buf->data[2] << 8 ) |
3704 hs_buf->data[3];
3705
3706 /* Double-check that we haven't accidentally buffered
3707 * a message that doesn't fit into the input buffer. */
3708 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3709 {
3710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3712 }
3713
3714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3715 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3716 hs_buf->data, msg_len + 12 );
3717
3718 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3719 ssl->in_hslen = msg_len + 12;
3720 ssl->in_msglen = msg_len + 12;
3721 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3722
3723 ret = 0;
3724 goto exit;
3725 }
3726 else
3727 {
3728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3729 hs->in_msg_seq ) );
3730 }
3731
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003732 ret = -1;
3733
3734exit:
3735
3736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3737 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003738}
3739
Hanno Beckera02b0b42018-08-21 17:20:27 +01003740static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3741 size_t desired )
3742{
3743 int offset;
3744 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3746 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003747
Hanno Becker01315ea2018-08-21 17:22:17 +01003748 /* Get rid of future records epoch first, if such exist. */
3749 ssl_free_buffered_record( ssl );
3750
3751 /* Check if we have enough space available now. */
3752 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3753 hs->buffering.total_bytes_buffered ) )
3754 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003756 return( 0 );
3757 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003758
Hanno Becker4f432ad2018-08-28 10:02:32 +01003759 /* We don't have enough space to buffer the next expected handshake
3760 * message. Remove buffers used for future messages to gain space,
3761 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003762 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3763 offset >= 0; offset-- )
3764 {
3765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3766 offset ) );
3767
Hanno Beckerb309b922018-08-23 13:18:05 +01003768 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003769
3770 /* Check if we have enough space available now. */
3771 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3772 hs->buffering.total_bytes_buffered ) )
3773 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003775 return( 0 );
3776 }
3777 }
3778
3779 return( -1 );
3780}
3781
Hanno Becker40f50842018-08-15 14:48:01 +01003782static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3783{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003784 int ret = 0;
3785 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3786
3787 if( hs == NULL )
3788 return( 0 );
3789
3790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3791
3792 switch( ssl->in_msgtype )
3793 {
3794 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003796
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003797 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003798 break;
3799
3800 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003801 {
3802 unsigned recv_msg_seq_offset;
3803 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3804 mbedtls_ssl_hs_buffer *hs_buf;
3805 size_t msg_len = ssl->in_hslen - 12;
3806
3807 /* We should never receive an old handshake
3808 * message - double-check nonetheless. */
3809 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3810 {
3811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3813 }
3814
3815 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3816 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3817 {
3818 /* Silently ignore -- message too far in the future */
3819 MBEDTLS_SSL_DEBUG_MSG( 2,
3820 ( "Ignore future HS message with sequence number %u, "
3821 "buffering window %u - %u",
3822 recv_msg_seq, ssl->handshake->in_msg_seq,
3823 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
3824
3825 goto exit;
3826 }
3827
3828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
3829 recv_msg_seq, recv_msg_seq_offset ) );
3830
3831 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
3832
3833 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003834 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01003835 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003836 size_t reassembly_buf_sz;
3837
Hanno Becker37f95322018-08-16 13:55:32 +01003838 hs_buf->is_fragmented =
3839 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
3840
3841 /* We copy the message back into the input buffer
3842 * after reassembly, so check that it's not too large.
3843 * This is an implementation-specific limitation
3844 * and not one from the standard, hence it is not
3845 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01003846 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01003847 {
3848 /* Ignore message */
3849 goto exit;
3850 }
3851
Hanno Beckere0b150f2018-08-21 15:51:03 +01003852 /* Check if we have enough space to buffer the message. */
3853 if( hs->buffering.total_bytes_buffered >
3854 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
3855 {
3856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3858 }
3859
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003860 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
3861 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003862
3863 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3864 hs->buffering.total_bytes_buffered ) )
3865 {
3866 if( recv_msg_seq_offset > 0 )
3867 {
3868 /* If we can't buffer a future message because
3869 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00003870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3871 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3872 " (already %" MBEDTLS_PRINTF_SIZET
3873 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003874 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003875 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003876 goto exit;
3877 }
Hanno Beckere1801392018-08-21 16:51:05 +01003878 else
3879 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3881 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3882 " (already %" MBEDTLS_PRINTF_SIZET
3883 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003884 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003885 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01003886 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003887
Hanno Beckera02b0b42018-08-21 17:20:27 +01003888 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003889 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
3891 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
3892 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
3893 " (already %" MBEDTLS_PRINTF_SIZET
3894 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00003895 msg_len,
3896 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00003897 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003898 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003899 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3900 goto exit;
3901 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003902 }
3903
Paul Elliottd48d5c62021-01-07 14:47:05 +00003904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01003905 msg_len ) );
3906
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003907 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
3908 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01003909 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01003910 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01003911 goto exit;
3912 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003913 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003914
3915 /* Prepare final header: copy msg_type, length and message_seq,
3916 * then add standardised fragment_offset and fragment_length */
3917 memcpy( hs_buf->data, ssl->in_msg, 6 );
3918 memset( hs_buf->data + 6, 0, 3 );
3919 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
3920
3921 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01003922
3923 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003924 }
3925 else
3926 {
3927 /* Make sure msg_type and length are consistent */
3928 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
3929 {
3930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
3931 /* Ignore */
3932 goto exit;
3933 }
3934 }
3935
Hanno Becker4422bbb2018-08-20 09:40:19 +01003936 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01003937 {
3938 size_t frag_len, frag_off;
3939 unsigned char * const msg = hs_buf->data + 12;
3940
3941 /*
3942 * Check and copy current fragment
3943 */
3944
3945 /* Validation of header fields already done in
3946 * mbedtls_ssl_prepare_handshake_record(). */
3947 frag_off = ssl_get_hs_frag_off( ssl );
3948 frag_len = ssl_get_hs_frag_len( ssl );
3949
Paul Elliottd48d5c62021-01-07 14:47:05 +00003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
3951 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01003952 frag_off, frag_len ) );
3953 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3954
3955 if( hs_buf->is_fragmented )
3956 {
3957 unsigned char * const bitmask = msg + msg_len;
3958 ssl_bitmask_set( bitmask, frag_off, frag_len );
3959 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
3960 msg_len ) == 0 );
3961 }
3962 else
3963 {
3964 hs_buf->is_complete = 1;
3965 }
3966
3967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
3968 hs_buf->is_complete ? "" : "not yet " ) );
3969 }
3970
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003971 break;
Hanno Becker37f95322018-08-16 13:55:32 +01003972 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003973
3974 default:
Hanno Becker360bef32018-08-28 10:04:33 +01003975 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003976 break;
3977 }
3978
3979exit:
3980
3981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
3982 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003983}
3984#endif /* MBEDTLS_SSL_PROTO_DTLS */
3985
Hanno Becker1097b342018-08-15 14:09:41 +01003986static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003987{
Hanno Becker4a810fb2017-05-24 16:27:30 +01003988 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01003989 * Consume last content-layer message and potentially
3990 * update in_msglen which keeps track of the contents'
3991 * consumption state.
3992 *
3993 * (1) Handshake messages:
3994 * Remove last handshake message, move content
3995 * and adapt in_msglen.
3996 *
3997 * (2) Alert messages:
3998 * Consume whole record content, in_msglen = 0.
3999 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004000 * (3) Change cipher spec:
4001 * Consume whole record content, in_msglen = 0.
4002 *
4003 * (4) Application data:
4004 * Don't do anything - the record layer provides
4005 * the application data as a stream transport
4006 * and consumes through mbedtls_ssl_read only.
4007 *
4008 */
4009
4010 /* Case (1): Handshake messages */
4011 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004012 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004013 /* Hard assertion to be sure that no application data
4014 * is in flight, as corrupting ssl->in_msglen during
4015 * ssl->in_offt != NULL is fatal. */
4016 if( ssl->in_offt != NULL )
4017 {
4018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4020 }
4021
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004022 /*
4023 * Get next Handshake message in the current record
4024 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004025
Hanno Becker4a810fb2017-05-24 16:27:30 +01004026 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004027 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004028 * current handshake content: If DTLS handshake
4029 * fragmentation is used, that's the fragment
4030 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004031 * size here is faulty and should be changed at
4032 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004033 * (2) While it doesn't seem to cause problems, one
4034 * has to be very careful not to assume that in_hslen
4035 * is always <= in_msglen in a sensible communication.
4036 * Again, it's wrong for DTLS handshake fragmentation.
4037 * The following check is therefore mandatory, and
4038 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004039 * Additionally, ssl->in_hslen might be arbitrarily out of
4040 * bounds after handling a DTLS message with an unexpected
4041 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004042 */
4043 if( ssl->in_hslen < ssl->in_msglen )
4044 {
4045 ssl->in_msglen -= ssl->in_hslen;
4046 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4047 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004048
Hanno Becker4a810fb2017-05-24 16:27:30 +01004049 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4050 ssl->in_msg, ssl->in_msglen );
4051 }
4052 else
4053 {
4054 ssl->in_msglen = 0;
4055 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004056
Hanno Becker4a810fb2017-05-24 16:27:30 +01004057 ssl->in_hslen = 0;
4058 }
4059 /* Case (4): Application data */
4060 else if( ssl->in_offt != NULL )
4061 {
4062 return( 0 );
4063 }
4064 /* Everything else (CCS & Alerts) */
4065 else
4066 {
4067 ssl->in_msglen = 0;
4068 }
4069
Hanno Becker1097b342018-08-15 14:09:41 +01004070 return( 0 );
4071}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004072
Hanno Beckere74d5562018-08-15 14:26:08 +01004073static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4074{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004075 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004076 return( 1 );
4077
4078 return( 0 );
4079}
4080
Hanno Becker5f066e72018-08-16 14:56:31 +01004081#if defined(MBEDTLS_SSL_PROTO_DTLS)
4082
4083static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4084{
4085 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4086 if( hs == NULL )
4087 return;
4088
Hanno Becker01315ea2018-08-21 17:22:17 +01004089 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004090 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004091 hs->buffering.total_bytes_buffered -=
4092 hs->buffering.future_record.len;
4093
4094 mbedtls_free( hs->buffering.future_record.data );
4095 hs->buffering.future_record.data = NULL;
4096 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004097}
4098
4099static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4100{
4101 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4102 unsigned char * rec;
4103 size_t rec_len;
4104 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004105#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4106 size_t in_buf_len = ssl->in_buf_len;
4107#else
4108 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4109#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004110 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4111 return( 0 );
4112
4113 if( hs == NULL )
4114 return( 0 );
4115
Hanno Becker5f066e72018-08-16 14:56:31 +01004116 rec = hs->buffering.future_record.data;
4117 rec_len = hs->buffering.future_record.len;
4118 rec_epoch = hs->buffering.future_record.epoch;
4119
4120 if( rec == NULL )
4121 return( 0 );
4122
Hanno Becker4cb782d2018-08-20 11:19:05 +01004123 /* Only consider loading future records if the
4124 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004125 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004126 return( 0 );
4127
Hanno Becker5f066e72018-08-16 14:56:31 +01004128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4129
4130 if( rec_epoch != ssl->in_epoch )
4131 {
4132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4133 goto exit;
4134 }
4135
4136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4137
4138 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004139 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004140 {
4141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4143 }
4144
4145 memcpy( ssl->in_hdr, rec, rec_len );
4146 ssl->in_left = rec_len;
4147 ssl->next_record_offset = 0;
4148
4149 ssl_free_buffered_record( ssl );
4150
4151exit:
4152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4153 return( 0 );
4154}
4155
Hanno Becker519f15d2019-07-11 12:43:20 +01004156static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4157 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004158{
4159 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004160
4161 /* Don't buffer future records outside handshakes. */
4162 if( hs == NULL )
4163 return( 0 );
4164
4165 /* Only buffer handshake records (we are only interested
4166 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004167 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004168 return( 0 );
4169
4170 /* Don't buffer more than one future epoch record. */
4171 if( hs->buffering.future_record.data != NULL )
4172 return( 0 );
4173
Hanno Becker01315ea2018-08-21 17:22:17 +01004174 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004175 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004176 hs->buffering.total_bytes_buffered ) )
4177 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4179 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4180 " (already %" MBEDTLS_PRINTF_SIZET
4181 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004182 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004183 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004184 return( 0 );
4185 }
4186
Hanno Becker5f066e72018-08-16 14:56:31 +01004187 /* Buffer record */
4188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004189 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004190 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004191
4192 /* ssl_parse_record_header() only considers records
4193 * of the next epoch as candidates for buffering. */
4194 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004195 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004196
4197 hs->buffering.future_record.data =
4198 mbedtls_calloc( 1, hs->buffering.future_record.len );
4199 if( hs->buffering.future_record.data == NULL )
4200 {
4201 /* If we run out of RAM trying to buffer a
4202 * record from the next epoch, just ignore. */
4203 return( 0 );
4204 }
4205
Hanno Becker519f15d2019-07-11 12:43:20 +01004206 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004207
Hanno Becker519f15d2019-07-11 12:43:20 +01004208 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004209 return( 0 );
4210}
4211
4212#endif /* MBEDTLS_SSL_PROTO_DTLS */
4213
Hanno Beckere74d5562018-08-15 14:26:08 +01004214static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004215{
Janos Follath865b3eb2019-12-16 11:46:15 +00004216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004217 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004218
Hanno Becker5f066e72018-08-16 14:56:31 +01004219#if defined(MBEDTLS_SSL_PROTO_DTLS)
4220 /* We might have buffered a future record; if so,
4221 * and if the epoch matches now, load it.
4222 * On success, this call will set ssl->in_left to
4223 * the length of the buffered record, so that
4224 * the calls to ssl_fetch_input() below will
4225 * essentially be no-ops. */
4226 ret = ssl_load_buffered_record( ssl );
4227 if( ret != 0 )
4228 return( ret );
4229#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004230
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004231 /* Ensure that we have enough space available for the default form
4232 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4233 * with no space for CIDs counted in). */
4234 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4235 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004238 return( ret );
4239 }
4240
Hanno Beckere5e7e782019-07-11 12:29:35 +01004241 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4242 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004244#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004246 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004247 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4248 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004249 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004250 if( ret != 0 )
4251 return( ret );
4252
4253 /* Fall through to handling of unexpected records */
4254 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4255 }
4256
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004257 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4258 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004259#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004260 /* Reset in pointers to default state for TLS/DTLS records,
4261 * assuming no CID and no offset between record content and
4262 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004263 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004264
Hanno Becker7ae20e02019-07-12 08:33:49 +01004265 /* Setup internal message pointers from record structure. */
4266 ssl->in_msgtype = rec.type;
4267#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4268 ssl->in_len = ssl->in_cid + rec.cid_len;
4269#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4270 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4271 ssl->in_msglen = rec.data_len;
4272
Hanno Becker2fddd372019-07-10 14:37:41 +01004273 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004274 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004275 if( ret != 0 )
4276 return( ret );
4277#endif
4278
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004279 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004280 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004281
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4283 "(header)" ) );
4284 }
4285 else
4286 {
4287 /* Skip invalid record and the rest of the datagram */
4288 ssl->next_record_offset = 0;
4289 ssl->in_left = 0;
4290
4291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4292 "(header)" ) );
4293 }
4294
4295 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004296 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004297 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004298 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004299#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004300 {
4301 return( ret );
4302 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004303 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004307 {
Hanno Beckera8814792019-07-10 15:01:45 +01004308 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004309 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004310 if( ssl->next_record_offset < ssl->in_left )
4311 {
4312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4313 }
4314 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004315 else
4316#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004317 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004318 /*
4319 * Fetch record contents from underlying transport.
4320 */
Hanno Beckera3175662019-07-11 12:50:29 +01004321 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004322 if( ret != 0 )
4323 {
4324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4325 return( ret );
4326 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004327
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004328 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004329 }
4330
4331 /*
4332 * Decrypt record contents.
4333 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004334
Hanno Beckerfdf66042019-07-11 13:07:45 +01004335 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004338 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004339 {
4340 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004341 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004342 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004343 /* Except when waiting for Finished as a bad mac here
4344 * probably means something went wrong in the handshake
4345 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4346 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4347 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4348 {
4349#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4350 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4351 {
4352 mbedtls_ssl_send_alert_message( ssl,
4353 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4354 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4355 }
4356#endif
4357 return( ret );
4358 }
4359
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004360 if( ssl->conf->badmac_limit != 0 &&
4361 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4364 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004365 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004366
Hanno Becker4a810fb2017-05-24 16:27:30 +01004367 /* As above, invalid records cause
4368 * dismissal of the whole datagram. */
4369
4370 ssl->next_record_offset = 0;
4371 ssl->in_left = 0;
4372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004374 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004375 }
4376
4377 return( ret );
4378 }
4379 else
4380#endif
4381 {
4382 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4384 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004386 mbedtls_ssl_send_alert_message( ssl,
4387 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4388 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004389 }
4390#endif
4391 return( ret );
4392 }
4393 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004394
Hanno Becker44d89b22019-07-12 09:40:44 +01004395
4396 /* Reset in pointers to default state for TLS/DTLS records,
4397 * assuming no CID and no offset between record content and
4398 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004399 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004400#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4401 ssl->in_len = ssl->in_cid + rec.cid_len;
4402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004403 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004404
Hanno Becker8685c822019-07-12 09:37:30 +01004405 /* The record content type may change during decryption,
4406 * so re-read it. */
4407 ssl->in_msgtype = rec.type;
4408 /* Also update the input buffer, because unfortunately
4409 * the server-side ssl_parse_client_hello() reparses the
4410 * record header when receiving a ClientHello initiating
4411 * a renegotiation. */
4412 ssl->in_hdr[0] = rec.type;
4413 ssl->in_msg = rec.buf + rec.data_offset;
4414 ssl->in_msglen = rec.data_len;
4415 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4416 ssl->in_len[1] = (unsigned char)( rec.data_len );
4417
Simon Butcher99000142016-10-13 17:21:01 +01004418 return( 0 );
4419}
4420
4421int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4422{
Janos Follath865b3eb2019-12-16 11:46:15 +00004423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004424
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004425 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004426 * Handle particular types of records
4427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 {
Simon Butcher99000142016-10-13 17:21:01 +01004430 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4431 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004432 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 }
4435
Hanno Beckere678eaa2018-08-21 14:57:46 +01004436 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004437 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004438 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004439 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004441 ssl->in_msglen ) );
4442 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004443 }
4444
Hanno Beckere678eaa2018-08-21 14:57:46 +01004445 if( ssl->in_msg[0] != 1 )
4446 {
4447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4448 ssl->in_msg[0] ) );
4449 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4450 }
4451
4452#if defined(MBEDTLS_SSL_PROTO_DTLS)
4453 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4454 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4455 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4456 {
4457 if( ssl->handshake == NULL )
4458 {
4459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4460 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4461 }
4462
4463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4464 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4465 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004466#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004467 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004471 if( ssl->in_msglen != 2 )
4472 {
4473 /* Note: Standard allows for more than one 2 byte alert
4474 to be packed in a single message, but Mbed TLS doesn't
4475 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004477 ssl->in_msglen ) );
4478 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4479 }
4480
Paul Elliott9f352112020-12-09 14:55:45 +00004481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 ssl->in_msg[0], ssl->in_msg[1] ) );
4483
4484 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004485 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004490 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004492 }
4493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4495 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4498 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004500
4501#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4502 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4503 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4504 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004506 /* Will be handled when trying to parse ServerHello */
4507 return( 0 );
4508 }
4509#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004510 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004511 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004512 }
4513
Hanno Beckerc76c6192017-06-06 10:03:17 +01004514#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004515 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004516 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004517 /* Drop unexpected ApplicationData records,
4518 * except at the beginning of renegotiations */
4519 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4520 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4521#if defined(MBEDTLS_SSL_RENEGOTIATION)
4522 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4523 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004524#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004525 )
4526 {
4527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4528 return( MBEDTLS_ERR_SSL_NON_FATAL );
4529 }
4530
4531 if( ssl->handshake != NULL &&
4532 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4533 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004534 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004535 }
4536 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004537#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004538
Paul Bakker5121ce52009-01-03 21:22:43 +00004539 return( 0 );
4540}
4541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004543{
irwir6c0da642019-09-26 21:07:41 +03004544 return( mbedtls_ssl_send_alert_message( ssl,
4545 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4546 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004547}
4548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004550 unsigned char level,
4551 unsigned char message )
4552{
Janos Follath865b3eb2019-12-16 11:46:15 +00004553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004554
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004555 if( ssl == NULL || ssl->conf == NULL )
4556 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004559 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004562 ssl->out_msglen = 2;
4563 ssl->out_msg[0] = level;
4564 ssl->out_msg[1] = message;
4565
Hanno Becker67bc7c32018-08-06 11:33:50 +01004566 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004569 return( ret );
4570 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004572
4573 return( 0 );
4574}
4575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004577{
Janos Follath865b3eb2019-12-16 11:46:15 +00004578 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004582 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 ssl->out_msglen = 1;
4584 ssl->out_msg[0] = 1;
4585
Paul Bakker5121ce52009-01-03 21:22:43 +00004586 ssl->state++;
4587
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004588 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004589 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 return( ret );
4592 }
4593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004595
4596 return( 0 );
4597}
4598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004599int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004600{
Janos Follath865b3eb2019-12-16 11:46:15 +00004601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004604
Hanno Becker327c93b2018-08-15 13:56:18 +01004605 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 return( ret );
4609 }
4610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004614 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4615 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004616 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 }
4618
Hanno Beckere678eaa2018-08-21 14:57:46 +01004619 /* CCS records are only accepted if they have length 1 and content '1',
4620 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004621
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004622 /*
4623 * Switch to our negotiated transform and session parameters for inbound
4624 * data.
4625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004627 ssl->transform_in = ssl->transform_negotiate;
4628 ssl->session_in = ssl->session_negotiate;
4629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004631 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004634 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004635#endif
4636
4637 /* Increment epoch */
4638 if( ++ssl->in_epoch == 0 )
4639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004641 /* This is highly unlikely to happen for legitimate reasons, so
4642 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004643 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004644 }
4645 }
4646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004648 memset( ssl->in_ctr, 0, 8 );
4649
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004650 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004651
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 ssl->state++;
4653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004654 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004655
4656 return( 0 );
4657}
4658
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004659/* Once ssl->out_hdr as the address of the beginning of the
4660 * next outgoing record is set, deduce the other pointers.
4661 *
4662 * Note: For TLS, we save the implicit record sequence number
4663 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4664 * and the caller has to make sure there's space for this.
4665 */
4666
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004667static size_t ssl_transform_get_explicit_iv_len(
4668 mbedtls_ssl_transform const *transform )
4669{
TRodziewiczef73f012021-05-13 14:53:36 +02004670 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004671 return( 0 );
4672
4673 return( transform->ivlen - transform->fixed_ivlen );
4674}
4675
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004676void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4677 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004678{
4679#if defined(MBEDTLS_SSL_PROTO_DTLS)
4680 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4681 {
4682 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004683#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004684 ssl->out_cid = ssl->out_ctr + 8;
4685 ssl->out_len = ssl->out_cid;
4686 if( transform != NULL )
4687 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004688#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004689 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004690#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004691 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004692 }
4693 else
4694#endif
4695 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004696 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004697#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004698 ssl->out_cid = ssl->out_len;
4699#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004700 ssl->out_iv = ssl->out_hdr + 5;
4701 }
4702
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004703 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004704 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004705 if( transform != NULL )
4706 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004707}
4708
4709/* Once ssl->in_hdr as the address of the beginning of the
4710 * next incoming record is set, deduce the other pointers.
4711 *
4712 * Note: For TLS, we save the implicit record sequence number
4713 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4714 * and the caller has to make sure there's space for this.
4715 */
4716
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004717void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004718{
Hanno Becker79594fd2019-05-08 09:38:41 +01004719 /* This function sets the pointers to match the case
4720 * of unprotected TLS/DTLS records, with both ssl->in_iv
4721 * and ssl->in_msg pointing to the beginning of the record
4722 * content.
4723 *
4724 * When decrypting a protected record, ssl->in_msg
4725 * will be shifted to point to the beginning of the
4726 * record plaintext.
4727 */
4728
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004729#if defined(MBEDTLS_SSL_PROTO_DTLS)
4730 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4731 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004732 /* This sets the header pointers to match records
4733 * without CID. When we receive a record containing
4734 * a CID, the fields are shifted accordingly in
4735 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004736 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004737#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004738 ssl->in_cid = ssl->in_ctr + 8;
4739 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004740#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004741 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004742#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004743 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004744 }
4745 else
4746#endif
4747 {
4748 ssl->in_ctr = ssl->in_hdr - 8;
4749 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004750#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004751 ssl->in_cid = ssl->in_len;
4752#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004753 ssl->in_iv = ssl->in_hdr + 5;
4754 }
4755
Hanno Becker79594fd2019-05-08 09:38:41 +01004756 /* This will be adjusted at record decryption time. */
4757 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004758}
4759
Paul Bakker5121ce52009-01-03 21:22:43 +00004760/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004761 * Setup an SSL context
4762 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004763
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004764void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004765{
4766 /* Set the incoming and outgoing record pointers. */
4767#if defined(MBEDTLS_SSL_PROTO_DTLS)
4768 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4769 {
4770 ssl->out_hdr = ssl->out_buf;
4771 ssl->in_hdr = ssl->in_buf;
4772 }
4773 else
4774#endif /* MBEDTLS_SSL_PROTO_DTLS */
4775 {
Hanno Becker12078f42021-03-02 15:28:41 +00004776 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004777 ssl->out_hdr = ssl->out_buf + 8;
4778 ssl->in_hdr = ssl->in_buf + 8;
4779 }
4780
4781 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004782 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4783 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004784}
4785
Paul Bakker5121ce52009-01-03 21:22:43 +00004786/*
4787 * SSL get accessors
4788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004790{
4791 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4792}
4793
Hanno Becker8b170a02017-10-10 11:51:19 +01004794int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4795{
4796 /*
4797 * Case A: We're currently holding back
4798 * a message for further processing.
4799 */
4800
4801 if( ssl->keep_current_message == 1 )
4802 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004803 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004804 return( 1 );
4805 }
4806
4807 /*
4808 * Case B: Further records are pending in the current datagram.
4809 */
4810
4811#if defined(MBEDTLS_SSL_PROTO_DTLS)
4812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4813 ssl->in_left > ssl->next_record_offset )
4814 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004815 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004816 return( 1 );
4817 }
4818#endif /* MBEDTLS_SSL_PROTO_DTLS */
4819
4820 /*
4821 * Case C: A handshake message is being processed.
4822 */
4823
Hanno Becker8b170a02017-10-10 11:51:19 +01004824 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
4825 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004827 return( 1 );
4828 }
4829
4830 /*
4831 * Case D: An application data message is being processed
4832 */
4833 if( ssl->in_offt != NULL )
4834 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004835 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004836 return( 1 );
4837 }
4838
4839 /*
4840 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01004841 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01004842 * we implement support for multiple alerts in single records.
4843 */
4844
4845 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
4846 return( 0 );
4847}
4848
Paul Bakker43ca69c2011-01-15 17:35:19 +00004849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004851{
Hanno Becker3136ede2018-08-17 15:28:19 +01004852 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004854 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004855
Hanno Becker5903de42019-05-03 14:46:38 +01004856 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
4857
Hanno Becker78640902018-08-13 16:35:15 +01004858 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01004859 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01004860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004861 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004863 case MBEDTLS_MODE_GCM:
4864 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004865 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004867 transform_expansion = transform->minlen;
4868 break;
4869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004870 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004871
4872 block_size = mbedtls_cipher_get_block_size(
4873 &transform->cipher_ctx_enc );
4874
Hanno Becker3136ede2018-08-17 15:28:19 +01004875 /* Expansion due to the addition of the MAC. */
4876 transform_expansion += transform->maclen;
4877
4878 /* Expansion due to the addition of CBC padding;
4879 * Theoretically up to 256 bytes, but we never use
4880 * more than the block size of the underlying cipher. */
4881 transform_expansion += block_size;
4882
TRodziewicz4ca18aa2021-05-20 14:46:20 +02004883 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01004884 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02004885#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02004886 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02004887#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01004888
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004889 break;
4890
4891 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004892 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004894 }
4895
Hanno Beckera0e20d02019-05-15 14:03:01 +01004896#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01004897 if( transform->out_cid_len != 0 )
4898 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01004900
Hanno Becker5903de42019-05-03 14:46:38 +01004901 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004902}
4903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004905/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004906 * Check record counters and renegotiate if they're above the limit.
4907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004909{
Hanno Beckerdd772292020-02-05 10:38:31 +00004910 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00004911 int in_ctr_cmp;
4912 int out_ctr_cmp;
4913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004914 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
4915 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004916 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004917 {
4918 return( 0 );
4919 }
4920
Andres AG2196c7f2016-12-15 17:01:16 +00004921 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
4922 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01004923 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00004924 ssl->conf->renego_period + ep_len, 8 - ep_len );
4925
4926 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004927 {
4928 return( 0 );
4929 }
4930
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004932 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004934#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00004935
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004936/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01004937 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004938 * may only be sent for the purpose of initiating renegotiations.
4939 *
4940 * This function is introduced as a separate helper since the handling
4941 * of post-handshake handshake messages changes significantly in TLS 1.3,
4942 * and having a helper function allows to distinguish between TLS <= 1.2 and
4943 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
4944 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00004945static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004946{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01004947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004948
4949 /*
4950 * - For client-side, expect SERVER_HELLO_REQUEST.
4951 * - For server-side, expect CLIENT_HELLO.
4952 * - Fail (TLS) or silently drop record (DTLS) in other cases.
4953 */
4954
4955#if defined(MBEDTLS_SSL_CLI_C)
4956 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
4957 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
4958 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
4959 {
4960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4961
4962 /* With DTLS, drop the packet (probably from last handshake) */
4963#if defined(MBEDTLS_SSL_PROTO_DTLS)
4964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4965 {
4966 return( 0 );
4967 }
4968#endif
4969 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4970 }
4971#endif /* MBEDTLS_SSL_CLI_C */
4972
4973#if defined(MBEDTLS_SSL_SRV_C)
4974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4975 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
4976 {
4977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
4978
4979 /* With DTLS, drop the packet (probably from last handshake) */
4980#if defined(MBEDTLS_SSL_PROTO_DTLS)
4981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4982 {
4983 return( 0 );
4984 }
4985#endif
4986 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4987 }
4988#endif /* MBEDTLS_SSL_SRV_C */
4989
4990#if defined(MBEDTLS_SSL_RENEGOTIATION)
4991 /* Determine whether renegotiation attempt should be accepted */
4992 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
4993 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
4994 ssl->conf->allow_legacy_renegotiation ==
4995 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
4996 {
4997 /*
4998 * Accept renegotiation request
4999 */
5000
5001 /* DTLS clients need to know renego is server-initiated */
5002#if defined(MBEDTLS_SSL_PROTO_DTLS)
5003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5004 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5005 {
5006 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5007 }
5008#endif
5009 ret = mbedtls_ssl_start_renegotiation( ssl );
5010 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5011 ret != 0 )
5012 {
5013 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5014 ret );
5015 return( ret );
5016 }
5017 }
5018 else
5019#endif /* MBEDTLS_SSL_RENEGOTIATION */
5020 {
5021 /*
5022 * Refuse renegotiation
5023 */
5024
5025 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5026
TRodziewicz0f82ec62021-05-12 17:49:18 +02005027#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02005028 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5029 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5030 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005031 {
TRodziewicz345165c2021-07-06 13:42:11 +02005032 return( ret );
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005033 }
TRodziewicz0f82ec62021-05-12 17:49:18 +02005034#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005035 }
5036
5037 return( 0 );
5038}
5039
Paul Bakker48916f92012-09-16 19:57:18 +00005040/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005041 * Receive application data decrypted from the SSL layer
5042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005044{
Janos Follath865b3eb2019-12-16 11:46:15 +00005045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005046 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005047
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005048 if( ssl == NULL || ssl->conf == NULL )
5049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005056 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005057 return( ret );
5058
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005059 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005061 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005062 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005063 return( ret );
5064 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005065 }
5066#endif
5067
Hanno Becker4a810fb2017-05-24 16:27:30 +01005068 /*
5069 * Check if renegotiation is necessary and/or handshake is
5070 * in process. If yes, perform/continue, and fall through
5071 * if an unexpected packet is received while the client
5072 * is waiting for the ServerHello.
5073 *
5074 * (There is no equivalent to the last condition on
5075 * the server-side as it is not treated as within
5076 * a handshake while waiting for the ClientHello
5077 * after a renegotiation request.)
5078 */
5079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005081 ret = ssl_check_ctr_renegotiate( ssl );
5082 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5083 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005086 return( ret );
5087 }
5088#endif
5089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005092 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005093 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5094 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005097 return( ret );
5098 }
5099 }
5100
Hanno Beckere41158b2017-10-23 13:30:32 +01005101 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005102 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005104 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005105 if( ssl->f_get_timer != NULL &&
5106 ssl->f_get_timer( ssl->p_timer ) == -1 )
5107 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005108 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005109 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005110
Hanno Becker327c93b2018-08-15 13:56:18 +01005111 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005112 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005113 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5114 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005115
Hanno Becker4a810fb2017-05-24 16:27:30 +01005116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5117 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 }
5119
5120 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005122 {
5123 /*
5124 * OpenSSL sends empty messages to randomize the IV
5125 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005126 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005128 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005129 return( 0 );
5130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 return( ret );
5133 }
5134 }
5135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005137 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005138 ret = ssl_handle_hs_message_post_handshake( ssl );
5139 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005140 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005141 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5142 ret );
5143 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005144 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005145
Hanno Beckerf26cc722021-04-21 07:30:13 +01005146 /* At this point, we don't know whether the renegotiation triggered
5147 * by the post-handshake message has been completed or not. The cases
5148 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005149 * 1) The renegotiation is complete. In this case, no new record
5150 * has been read yet.
5151 * 2) The renegotiation is incomplete because the client received
5152 * an application data record while awaiting the ServerHello.
5153 * 3) The renegotiation is incomplete because the client received
5154 * a non-handshake, non-application data message while awaiting
5155 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005156 *
5157 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005158 * - For 1), the next iteration will read a new record and check
5159 * if it's application data.
5160 * - For 2), the loop condition isn't satisfied as application data
5161 * is present, hence continue is the same as break
5162 * - For 3), the loop condition is satisfied and read_record
5163 * will re-deliver the message that was held back by the client
5164 * when expecting the ServerHello.
5165 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005166
Hanno Becker90333da2017-10-10 11:27:13 +01005167 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005168 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005169#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005170 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005171 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005172 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005173 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005174 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005177 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005179 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005180 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005181 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005182#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5185 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005188 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005189 }
5190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005191 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5194 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005195 }
5196
5197 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005198
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005199 /* We're going to return something now, cancel timer,
5200 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005201 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005202 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005203
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005204#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005205 /* If we requested renego but received AppData, resend HelloRequest.
5206 * Do it now, after setting in_offt, to avoid taking this branch
5207 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005209 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005211 {
Hanno Becker786300f2020-02-05 10:46:40 +00005212 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005213 {
Hanno Becker786300f2020-02-05 10:46:40 +00005214 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5215 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005216 return( ret );
5217 }
5218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005220#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005221 }
5222
5223 n = ( len < ssl->in_msglen )
5224 ? len : ssl->in_msglen;
5225
5226 memcpy( buf, ssl->in_offt, n );
5227 ssl->in_msglen -= n;
5228
gabor-mezei-arma3214132020-07-15 10:55:00 +02005229 /* Zeroising the plaintext buffer to erase unused application data
5230 from the memory. */
5231 mbedtls_platform_zeroize( ssl->in_offt, n );
5232
Paul Bakker5121ce52009-01-03 21:22:43 +00005233 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005234 {
5235 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005236 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005237 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005239 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005240 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005241 /* more data available */
5242 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005246
Paul Bakker23986e52011-04-24 08:57:21 +00005247 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005248}
5249
5250/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005251 * Send application data to be encrypted by the SSL layer, taking care of max
5252 * fragment length and buffer size.
5253 *
5254 * According to RFC 5246 Section 6.2.1:
5255 *
5256 * Zero-length fragments of Application data MAY be sent as they are
5257 * potentially useful as a traffic analysis countermeasure.
5258 *
5259 * Therefore, it is possible that the input message length is 0 and the
5260 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005261 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005262static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005263 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005264{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005265 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5266 const size_t max_len = (size_t) ret;
5267
5268 if( ret < 0 )
5269 {
5270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5271 return( ret );
5272 }
5273
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005274 if( len > max_len )
5275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005277 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005280 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5281 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005282 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005284 }
5285 else
5286#endif
5287 len = max_len;
5288 }
Paul Bakker887bd502011-06-08 13:10:54 +00005289
Paul Bakker5121ce52009-01-03 21:22:43 +00005290 if( ssl->out_left != 0 )
5291 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005292 /*
5293 * The user has previously tried to send the data and
5294 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5295 * written. In this case, we expect the high-level write function
5296 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005298 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005301 return( ret );
5302 }
5303 }
Paul Bakker887bd502011-06-08 13:10:54 +00005304 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005305 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005306 /*
5307 * The user is trying to send a message the first time, so we need to
5308 * copy the data into the internal buffers and setup the data structure
5309 * to keep track of partial writes
5310 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005311 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005312 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005313 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005314
Hanno Becker67bc7c32018-08-06 11:33:50 +01005315 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005318 return( ret );
5319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005320 }
5321
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005322 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005323}
5324
5325/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005326 * Write application data (public-facing wrapper)
5327 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005328int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005329{
Janos Follath865b3eb2019-12-16 11:46:15 +00005330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005331
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005333
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005334 if( ssl == NULL || ssl->conf == NULL )
5335 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5336
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005337#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005338 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5339 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005340 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005341 return( ret );
5342 }
5343#endif
5344
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005345 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005346 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005347 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005348 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005350 return( ret );
5351 }
5352 }
5353
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005354 ret = ssl_write_real( ssl, buf, len );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005355
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005357
5358 return( ret );
5359}
5360
5361/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005362 * Notify the peer that the connection is being closed
5363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005364int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005365{
Janos Follath865b3eb2019-12-16 11:46:15 +00005366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005367
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005368 if( ssl == NULL || ssl->conf == NULL )
5369 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005372
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005373 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5379 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5380 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005383 return( ret );
5384 }
5385 }
5386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005388
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005389 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005390}
5391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005393{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005394 if( transform == NULL )
5395 return;
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5398 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005399
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005400#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 mbedtls_md_free( &transform->md_ctx_enc );
5402 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005403#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005404
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005405 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005406}
5407
Hanno Becker0271f962018-08-16 13:23:47 +01005408#if defined(MBEDTLS_SSL_PROTO_DTLS)
5409
Hanno Becker533ab5f2020-02-05 10:49:13 +00005410void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005411{
5412 unsigned offset;
5413 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5414
5415 if( hs == NULL )
5416 return;
5417
Hanno Becker283f5ef2018-08-24 09:34:47 +01005418 ssl_free_buffered_record( ssl );
5419
Hanno Becker0271f962018-08-16 13:23:47 +01005420 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005421 ssl_buffering_free_slot( ssl, offset );
5422}
5423
5424static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5425 uint8_t slot )
5426{
5427 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5428 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005429
5430 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5431 return;
5432
Hanno Beckere605b192018-08-21 15:59:07 +01005433 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005434 {
Hanno Beckere605b192018-08-21 15:59:07 +01005435 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005436 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005437 mbedtls_free( hs_buf->data );
5438 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005439 }
5440}
5441
5442#endif /* MBEDTLS_SSL_PROTO_DTLS */
5443
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005444/*
5445 * Convert version numbers to/from wire format
5446 * and, for DTLS, to/from TLS equivalent.
5447 *
5448 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005449 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005450 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005453 unsigned char ver[2] )
5454{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455#if defined(MBEDTLS_SSL_PROTO_DTLS)
5456 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005459 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5460
5461 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5462 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5463 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005464 else
5465#else
5466 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005467#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005468 {
5469 ver[0] = (unsigned char) major;
5470 ver[1] = (unsigned char) minor;
5471 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005472}
5473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005474void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005475 const unsigned char ver[2] )
5476{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#if defined(MBEDTLS_SSL_PROTO_DTLS)
5478 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005479 {
5480 *major = 255 - ver[0] + 2;
5481 *minor = 255 - ver[1] + 1;
5482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005483 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005484 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5485 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005486 else
5487#else
5488 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005489#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005490 {
5491 *major = ver[0];
5492 *minor = ver[1];
5493 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005494}
5495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005496#endif /* MBEDTLS_SSL_TLS_C */