blob: be9c5f2f8384649fae1e9facbf7c858c04eef493 [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
Hanno Becker605949f2019-07-12 08:23:59 +0100941int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +0000942 mbedtls_ssl_transform *transform,
943 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +0000944{
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000945 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000947 int ret, auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000948#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +0100949 size_t padlen = 0, correct = 1;
950#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000951 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100952 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100953 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000954
Hanno Beckera18d1322018-01-03 14:27:32 +0000955#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200956 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000957 ((void) ssl);
958#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000961 if( rec == NULL ||
962 rec->buf == NULL ||
963 rec->buf_len < rec->data_offset ||
964 rec->buf_len - rec->data_offset < rec->data_len )
965 {
966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100968 }
969
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000970 data = rec->buf + rec->data_offset;
971 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +0000972
Hanno Beckera0e20d02019-05-15 14:03:01 +0100973#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100974 /*
975 * Match record's CID with incoming CID.
976 */
Hanno Becker938489a2019-05-08 13:02:22 +0100977 if( rec->cid_len != transform->in_cid_len ||
978 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
979 {
Hanno Becker8367ccc2019-05-14 11:30:10 +0100980 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +0100981 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100982#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100983
Hanno Beckerd086bf02021-03-22 13:01:27 +0000984#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +0100986 {
987 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000988 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
989 transform->iv_dec,
990 transform->ivlen,
991 data, rec->data_len,
992 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200995 return( ret );
996 }
997
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000998 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1001 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 }
Paul Bakker68884e32013-01-07 18:20:04 +01001004 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001005#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001006#if defined(MBEDTLS_GCM_C) || \
1007 defined(MBEDTLS_CCM_C) || \
1008 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001010 mode == MBEDTLS_MODE_CCM ||
1011 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001013 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001014 unsigned char *dynamic_iv;
1015 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001017 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001018 * Extract dynamic part of nonce for AEAD decryption.
1019 *
1020 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1021 * part of the IV is prepended to the ciphertext and
1022 * can be chosen freely - in particular, it need not
1023 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001024 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001025 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001026 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001027 {
1028 if( rec->data_len < dynamic_iv_len )
1029 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1031 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001032 rec->data_len,
1033 dynamic_iv_len ) );
1034 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1035 }
1036 dynamic_iv = data;
1037
1038 data += dynamic_iv_len;
1039 rec->data_offset += dynamic_iv_len;
1040 rec->data_len -= dynamic_iv_len;
1041 }
Hanno Becker17263802020-05-28 07:05:48 +01001042 else
1043 {
1044 dynamic_iv = rec->ctr;
1045 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001046
1047 /* Check that there's space for the authentication tag. */
1048 if( rec->data_len < transform->taglen )
1049 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1051 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001052 rec->data_len,
1053 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001055 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001056 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001057
Hanno Beckerdf8be222020-05-21 15:30:57 +01001058 /*
1059 * Prepare nonce from dynamic and static parts.
1060 */
Hanno Becker17263802020-05-28 07:05:48 +01001061 ssl_build_record_nonce( iv, sizeof( iv ),
1062 transform->iv_dec,
1063 transform->fixed_ivlen,
1064 dynamic_iv,
1065 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001066
Hanno Beckerdf8be222020-05-21 15:30:57 +01001067 /*
1068 * Build additional data for AEAD encryption.
1069 * This depends on the TLS version.
1070 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001071 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1072 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001073 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001074 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001075
Hanno Beckerd96a6522019-07-10 13:55:25 +01001076 /* Because of the check above, we know that there are
1077 * explicit_iv_len Bytes preceeding data, and taglen
1078 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001079 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001080 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001081
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001082 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001083 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001084 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001085
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001086 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001087 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001088 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001089 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001090 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001091 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001092 data, rec->data_len + transform->taglen, /* src */
1093 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001094 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001095 {
TRodziewicz18efb732021-04-29 23:12:19 +02001096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1099 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001100
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001101 return( ret );
1102 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001103 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001104
Hanno Beckerd96a6522019-07-10 13:55:25 +01001105 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001106 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1109 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001110 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001114#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001116 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001117 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001118
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 /*
Paul Bakker45829992013-01-03 14:52:21 +01001120 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001122#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02001123 /* The ciphertext is prefixed with the CBC IV. */
1124 minlen += transform->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001125#endif
Paul Bakker45829992013-01-03 14:52:21 +01001126
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001127 /* Size considerations:
1128 *
1129 * - The CBC cipher text must not be empty and hence
1130 * at least of size transform->ivlen.
1131 *
1132 * Together with the potential IV-prefix, this explains
1133 * the first of the two checks below.
1134 *
1135 * - The record must contain a MAC, either in plain or
1136 * encrypted, depending on whether Encrypt-then-MAC
1137 * is used or not.
1138 * - If it is, the message contains the IV-prefix,
1139 * the CBC ciphertext, and the MAC.
1140 * - If it is not, the padded plaintext, and hence
1141 * the CBC ciphertext, has at least length maclen + 1
1142 * because there is at least the padding length byte.
1143 *
1144 * As the CBC ciphertext is not empty, both cases give the
1145 * lower bound minlen + maclen + 1 on the record size, which
1146 * we test for in the second check below.
1147 */
1148 if( rec->data_len < minlen + transform->ivlen ||
1149 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001150 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1152 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1153 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001154 "+ 1 ) ( + expl IV )", rec->data_len,
1155 transform->ivlen,
1156 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001158 }
1159
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001160 /*
1161 * Authenticate before decrypt if enabled
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001164 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001165 {
Hanno Becker992b6872017-11-09 18:57:39 +00001166 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001169
Hanno Beckerd96a6522019-07-10 13:55:25 +01001170 /* Update data_len in tandem with add_data.
1171 *
1172 * The subtraction is safe because of the previous check
1173 * data_len >= minlen + maclen + 1.
1174 *
1175 * Afterwards, we know that data + data_len is followed by at
1176 * least maclen Bytes, which justifies the call to
1177 * mbedtls_ssl_safer_memcmp() below.
1178 *
1179 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001180 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001181 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1182 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001183
Hanno Beckerd96a6522019-07-10 13:55:25 +01001184 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001185 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1186 add_data_len );
1187 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1188 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001189 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1190 data, rec->data_len );
1191 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1192 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001193
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001194 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1195 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001196 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001197 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001198
Hanno Beckerd96a6522019-07-10 13:55:25 +01001199 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001200 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1201 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001205 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001206 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001207 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001209
1210 /*
1211 * Check length sanity
1212 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001213
1214 /* We know from above that data_len > minlen >= 0,
1215 * so the following check in particular implies that
1216 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001217 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001218 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1220 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001221 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001223 }
1224
TRodziewicz0f82ec62021-05-12 17:49:18 +02001225#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001227 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001228 */
TRodziewicz345165c2021-07-06 13:42:11 +02001229 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
1230 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001231
TRodziewicz345165c2021-07-06 13:42:11 +02001232 data += transform->ivlen;
1233 rec->data_offset += transform->ivlen;
1234 rec->data_len -= transform->ivlen;
TRodziewicz0f82ec62021-05-12 17:49:18 +02001235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001236
Hanno Beckerd96a6522019-07-10 13:55:25 +01001237 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1238
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001239 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1240 transform->iv_dec, transform->ivlen,
1241 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001244 return( ret );
1245 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001246
Hanno Beckerd96a6522019-07-10 13:55:25 +01001247 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001248 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001252 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001253
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001254 /* Safe since data_len >= minlen + maclen + 1, so after having
1255 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001256 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1257 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001258 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001259
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001260 if( auth_done == 1 )
1261 {
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001262 const size_t mask = mbedtls_cf_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001263 rec->data_len,
1264 padlen + 1 );
1265 correct &= mask;
1266 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001267 }
1268 else
Paul Bakker45829992013-01-03 14:52:21 +01001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001271 if( rec->data_len < transform->maclen + padlen + 1 )
1272 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001273 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1274 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1275 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001276 rec->data_len,
1277 transform->maclen,
1278 padlen + 1 ) );
1279 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001280#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001281
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001282 const size_t mask = mbedtls_cf_size_mask_ge(
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001283 rec->data_len,
1284 transform->maclen + padlen + 1 );
1285 correct &= mask;
1286 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001288
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001289 padlen++;
1290
1291 /* Regardless of the validity of the padding,
1292 * we have data_len >= padlen here. */
1293
TRodziewicz0f82ec62021-05-12 17:49:18 +02001294#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001295 /* The padding check involves a series of up to 256
1296 * consecutive memory reads at the end of the record
1297 * plaintext buffer. In order to hide the length and
1298 * validity of the padding, always perform exactly
1299 * `min(256,plaintext_len)` reads (but take into account
1300 * only the last `padlen` bytes for the padding check). */
1301 size_t pad_count = 0;
1302 volatile unsigned char* const check = data;
1303
1304 /* Index of first padding byte; it has been ensured above
1305 * that the subtraction is safe. */
1306 size_t const padding_idx = rec->data_len - padlen;
1307 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1308 size_t const start_idx = rec->data_len - num_checks;
1309 size_t idx;
1310
1311 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001313 /* pad_count += (idx >= padding_idx) &&
1314 * (check[idx] == padlen - 1);
1315 */
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001316 const size_t mask = mbedtls_cf_size_mask_ge( idx, padding_idx );
1317 const size_t equal = mbedtls_cf_size_bool_eq( check[idx],
1318 padlen - 1 );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001319 pad_count += mask & equal;
1320 }
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001321 correct &= mbedtls_cf_size_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001324 if( padlen > 0 && correct == 0 )
1325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001326#endif
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001327 padlen &= mbedtls_cf_size_mask( correct );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001328
TRodziewicz0f82ec62021-05-12 17:49:18 +02001329#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001330
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001331 /* If the padding was found to be invalid, padlen == 0
1332 * and the subtraction is safe. If the padding was found valid,
1333 * padlen hasn't been changed and the previous assertion
1334 * data_len >= padlen still holds. */
1335 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001337 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001338#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1341 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001343
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001344#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001346 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001347#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001350 * Authenticate if not done yet.
1351 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001353#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001354 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001355 {
Hanno Becker992b6872017-11-09 18:57:39 +00001356 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001357 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001358
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001359 /* If the initial value of padlen was such that
1360 * data_len < maclen + padlen + 1, then padlen
1361 * got reset to 1, and the initial check
1362 * data_len >= minlen + maclen + 1
1363 * guarantees that at this point we still
1364 * have at least data_len >= maclen.
1365 *
1366 * If the initial value of padlen was such that
1367 * data_len >= maclen + padlen + 1, then we have
1368 * subtracted either padlen + 1 (if the padding was correct)
1369 * or 0 (if the padding was incorrect) since then,
1370 * hence data_len >= maclen in any case.
1371 */
1372 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001373 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1374 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
TRodziewicz0f82ec62021-05-12 17:49:18 +02001376#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001377 /*
1378 * The next two sizes are the minimum and maximum values of
1379 * data_len over all padlen values.
1380 *
1381 * They're independent of padlen, since we previously did
1382 * data_len -= padlen.
1383 *
1384 * Note that max_len + maclen is never more than the buffer
1385 * length, as we previously did in_msglen -= maclen too.
1386 */
1387 const size_t max_len = rec->data_len + padlen;
1388 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1389
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001390 ret = mbedtls_cf_hmac( &transform->md_ctx_dec,
1391 add_data, add_data_len,
1392 data, rec->data_len, min_len, max_len,
1393 mac_expect );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001394 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001395 {
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cf_hmac", ret );
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001397 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001398 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001399
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02001400 mbedtls_cf_memcpy_offset( mac_peer, data,
1401 rec->data_len,
1402 min_len, max_len,
1403 transform->maclen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001404#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001406#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001407 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001408 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001409#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001410
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001411 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001412 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#if defined(MBEDTLS_SSL_DEBUG_ALL)
1415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001416#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001417 correct = 0;
1418 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001419 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001420 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001421
1422 /*
1423 * Finally check the correct flag
1424 */
1425 if( correct == 0 )
1426 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001427#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001428
1429 /* Make extra sure authentication was performed, exactly once */
1430 if( auth_done != 1 )
1431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1433 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001434 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
Hanno Beckerccc13d02020-05-04 12:30:04 +01001436#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1437 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1438 {
1439 /* Remove inner padding and infer true content type. */
1440 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1441 &rec->type );
1442
1443 if( ret != 0 )
1444 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1445 }
1446#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1447
Hanno Beckera0e20d02019-05-15 14:03:01 +01001448#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001449 if( rec->cid_len != 0 )
1450 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001451 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1452 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001453 if( ret != 0 )
1454 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1455 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001456#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001459
1460 return( 0 );
1461}
1462
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001463#undef MAC_NONE
1464#undef MAC_PLAINTEXT
1465#undef MAC_CIPHERTEXT
1466
Paul Bakker5121ce52009-01-03 21:22:43 +00001467/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001468 * Fill the input message buffer by appending data to it.
1469 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001470 *
1471 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1472 * available (from this read and/or a previous one). Otherwise, an error code
1473 * is returned (possibly EOF or WANT_READ).
1474 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001475 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1476 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1477 * since we always read a whole datagram at once.
1478 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001479 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001480 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001483{
Janos Follath865b3eb2019-12-16 11:46:15 +00001484 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001485 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001486#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1487 size_t in_buf_len = ssl->in_buf_len;
1488#else
1489 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1490#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001493
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001494 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001497 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001499 }
1500
Darryl Greenb33cc762019-11-28 14:29:44 +00001501 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1504 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001505 }
1506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001510 uint32_t timeout;
1511
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001512 /*
1513 * The point is, we need to always read a full datagram at once, so we
1514 * sometimes read more then requested, and handle the additional data.
1515 * It could be the rest of the current record (while fetching the
1516 * header) and/or some other records in the same datagram.
1517 */
1518
1519 /*
1520 * Move to the next record in the already read datagram if applicable
1521 */
1522 if( ssl->next_record_offset != 0 )
1523 {
1524 if( ssl->in_left < ssl->next_record_offset )
1525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001528 }
1529
1530 ssl->in_left -= ssl->next_record_offset;
1531
1532 if( ssl->in_left != 0 )
1533 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1535 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001536 ssl->next_record_offset ) );
1537 memmove( ssl->in_hdr,
1538 ssl->in_hdr + ssl->next_record_offset,
1539 ssl->in_left );
1540 }
1541
1542 ssl->next_record_offset = 0;
1543 }
1544
Paul Elliottd48d5c62021-01-07 14:47:05 +00001545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1546 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001547 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001548
1549 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001550 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001551 */
1552 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001555 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001556 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001557
1558 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001559 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001560 * are not at the beginning of a new record, the caller did something
1561 * wrong.
1562 */
1563 if( ssl->in_left != 0 )
1564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001567 }
1568
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001569 /*
1570 * Don't even try to read if time's out already.
1571 * This avoids by-passing the timer when repeatedly receiving messages
1572 * that will end up being dropped.
1573 */
Hanno Becker7876d122020-02-05 10:39:31 +00001574 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001575 {
1576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001577 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001578 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001579 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001580 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001581 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001584 timeout = ssl->handshake->retransmit_timeout;
1585 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001586 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001587
Paul Elliott9f352112020-12-09 14:55:45 +00001588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001589
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001590 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001591 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1592 timeout );
1593 else
1594 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001597
1598 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001600 }
1601
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001602 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001605 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001608 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001609 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001612 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001613 }
1614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001618 return( ret );
1619 }
1620
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001621 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001622 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001624 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001626 {
Hanno Becker786300f2020-02-05 10:46:40 +00001627 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001628 {
Hanno Becker786300f2020-02-05 10:46:40 +00001629 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1630 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001631 return( ret );
1632 }
1633
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001634 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001635 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001637 }
1638
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 if( ret < 0 )
1640 return( ret );
1641
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001642 ssl->in_left = ret;
1643 }
1644 else
1645#endif
1646 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001647 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1648 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001649 ssl->in_left, nb_want ) );
1650
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001651 while( ssl->in_left < nb_want )
1652 {
1653 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001654
Hanno Becker7876d122020-02-05 10:39:31 +00001655 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001656 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1657 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001658 {
1659 if( ssl->f_recv_timeout != NULL )
1660 {
1661 ret = ssl->f_recv_timeout( ssl->p_bio,
1662 ssl->in_hdr + ssl->in_left, len,
1663 ssl->conf->read_timeout );
1664 }
1665 else
1666 {
1667 ret = ssl->f_recv( ssl->p_bio,
1668 ssl->in_hdr + ssl->in_left, len );
1669 }
1670 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001671
Paul Elliottd48d5c62021-01-07 14:47:05 +00001672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1673 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001674 ssl->in_left, nb_want ) );
1675 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001676
1677 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001679
1680 if( ret < 0 )
1681 return( ret );
1682
makise-homuraaf9513b2020-08-24 18:26:27 +03001683 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001684 {
Darryl Green11999bb2018-03-13 15:22:58 +00001685 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001686 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001687 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001688 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1689 }
1690
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001691 ssl->in_left += ret;
1692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001693 }
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
1697 return( 0 );
1698}
1699
1700/*
1701 * Flush any data not yet written
1702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001704{
Janos Follath865b3eb2019-12-16 11:46:15 +00001705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001706 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001710 if( ssl->f_send == NULL )
1711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001713 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001715 }
1716
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001717 /* Avoid incrementing counter if data is flushed */
1718 if( ssl->out_left == 0 )
1719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001721 return( 0 );
1722 }
1723
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 while( ssl->out_left > 0 )
1725 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
1727 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01001728 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Hanno Becker2b1e3542018-08-06 11:19:13 +01001730 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001731 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
1735 if( ret <= 0 )
1736 return( ret );
1737
makise-homuraaf9513b2020-08-24 18:26:27 +03001738 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08001739 {
Darryl Green11999bb2018-03-13 15:22:58 +00001740 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001741 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00001742 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08001743 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1744 }
1745
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 ssl->out_left -= ret;
1747 }
1748
Hanno Becker2b1e3542018-08-06 11:19:13 +01001749#if defined(MBEDTLS_SSL_PROTO_DTLS)
1750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001751 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01001752 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001753 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01001754 else
1755#endif
1756 {
1757 ssl->out_hdr = ssl->out_buf + 8;
1758 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001759 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
1763 return( 0 );
1764}
1765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001766/*
1767 * Functions to handle the DTLS retransmission state machine
1768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001770/*
1771 * Append current handshake message to current outgoing flight
1772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01001776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
1777 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
1778 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001779
1780 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001781 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001782 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001785 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001786 }
1787
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001788 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001789 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
1791 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001794 }
1795
1796 /* Copy current handshake message with headers */
1797 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
1798 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001799 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001800 msg->next = NULL;
1801
1802 /* Append to the current flight */
1803 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001804 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001805 else
1806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001808 while( cur->next != NULL )
1809 cur = cur->next;
1810 cur->next = msg;
1811 }
1812
Hanno Becker3b235902018-08-06 09:54:53 +01001813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001814 return( 0 );
1815}
1816
1817/*
1818 * Free the current flight of handshake messages
1819 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00001820void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001821{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 mbedtls_ssl_flight_item *cur = flight;
1823 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001824
1825 while( cur != NULL )
1826 {
1827 next = cur->next;
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 mbedtls_free( cur->p );
1830 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001831
1832 cur = next;
1833 }
1834}
1835
1836/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001837 * Swap transform_out and out_ctr with the alternative ones
1838 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001839static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001842 unsigned char tmp_out_ctr[8];
1843
1844 if( ssl->transform_out == ssl->handshake->alt_transform_out )
1845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001847 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001848 }
1849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001851
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001852 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001853 tmp_transform = ssl->transform_out;
1854 ssl->transform_out = ssl->handshake->alt_transform_out;
1855 ssl->handshake->alt_transform_out = tmp_transform;
1856
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001857 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01001858 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
1859 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001860 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001861
1862 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001863 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001864
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001865 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001866}
1867
1868/*
1869 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001870 */
1871int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
1872{
1873 int ret = 0;
1874
1875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
1876
1877 ret = mbedtls_ssl_flight_transmit( ssl );
1878
1879 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
1880
1881 return( ret );
1882}
1883
1884/*
1885 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001886 *
1887 * Need to remember the current message in case flush_output returns
1888 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001889 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001890 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001891int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001892{
Janos Follath865b3eb2019-12-16 11:46:15 +00001893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02001894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001897 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02001898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001899
1900 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001901 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001902 ret = ssl_swap_epochs( ssl );
1903 if( ret != 0 )
1904 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001907 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001908
1909 while( ssl->handshake->cur_msg != NULL )
1910 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01001911 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001912 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01001913
Hanno Beckere1dcb032018-08-17 16:47:58 +01001914 int const is_finished =
1915 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
1916 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
1917
Hanno Becker04da1892018-08-14 13:22:10 +01001918 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
1919 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
1920
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001921 /* Swap epochs before sending Finished: we can't do it after
1922 * sending ChangeCipherSpec, in case write returns WANT_READ.
1923 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01001924 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001925 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01001926 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001927 ret = ssl_swap_epochs( ssl );
1928 if( ret != 0 )
1929 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02001930 }
1931
Hanno Becker67bc7c32018-08-06 11:33:50 +01001932 ret = ssl_get_remaining_payload_in_datagram( ssl );
1933 if( ret < 0 )
1934 return( ret );
1935 max_frag_len = (size_t) ret;
1936
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001937 /* CCS is copied as is, while HS messages may need fragmentation */
1938 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
1939 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01001940 if( max_frag_len == 0 )
1941 {
1942 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
1943 return( ret );
1944
1945 continue;
1946 }
1947
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001948 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01001949 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001950 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001951
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001952 /* Update position inside current message */
1953 ssl->handshake->cur_msg_p += cur->len;
1954 }
1955 else
1956 {
1957 const unsigned char * const p = ssl->handshake->cur_msg_p;
1958 const size_t hs_len = cur->len - 12;
1959 const size_t frag_off = p - ( cur->p + 12 );
1960 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01001961 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001962
Hanno Beckere1dcb032018-08-17 16:47:58 +01001963 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02001964 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01001965 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01001966 {
1967 ret = ssl_swap_epochs( ssl );
1968 if( ret != 0 )
1969 return( ret );
1970 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001971
Hanno Becker67bc7c32018-08-06 11:33:50 +01001972 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
1973 return( ret );
1974
1975 continue;
1976 }
1977 max_hs_frag_len = max_frag_len - 12;
1978
1979 cur_hs_frag_len = rem_len > max_hs_frag_len ?
1980 max_hs_frag_len : rem_len;
1981
1982 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02001983 {
1984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01001985 (unsigned) cur_hs_frag_len,
1986 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02001987 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02001988
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001989 /* Messages are stored with handshake headers as if not fragmented,
1990 * copy beginning of headers then fill fragmentation fields.
1991 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
1992 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001993
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02001994 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
1995 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
1996 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
1997
Hanno Becker67bc7c32018-08-06 11:33:50 +01001998 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
1999 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2000 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002001
2002 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2003
Hanno Becker3f7b9732018-08-28 09:53:25 +01002004 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002005 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2006 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002007 ssl->out_msgtype = cur->type;
2008
2009 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002010 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002011 }
2012
2013 /* If done with the current message move to the next one if any */
2014 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2015 {
2016 if( cur->next != NULL )
2017 {
2018 ssl->handshake->cur_msg = cur->next;
2019 ssl->handshake->cur_msg_p = cur->next->p + 12;
2020 }
2021 else
2022 {
2023 ssl->handshake->cur_msg = NULL;
2024 ssl->handshake->cur_msg_p = NULL;
2025 }
2026 }
2027
2028 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002029 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002032 return( ret );
2033 }
2034 }
2035
Hanno Becker67bc7c32018-08-06 11:33:50 +01002036 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2037 return( ret );
2038
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002039 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2041 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002042 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002045 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002046 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002047
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002048 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002049
2050 return( 0 );
2051}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002052
2053/*
2054 * To be called when the last message of an incoming flight is received.
2055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002057{
2058 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002059 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002060 ssl->handshake->flight = NULL;
2061 ssl->handshake->cur_msg = NULL;
2062
2063 /* The next incoming flight will start with this msg_seq */
2064 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2065
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002066 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002067 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002068
Hanno Becker0271f962018-08-16 13:23:47 +01002069 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002070 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002071
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002072 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002073 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2076 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002079 }
2080 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002082}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002083
2084/*
2085 * To be called when the last message of an outgoing flight is send.
2086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002088{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002089 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002090 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2093 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002096 }
2097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002101
Paul Bakker5121ce52009-01-03 21:22:43 +00002102/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002103 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002105
2106/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002107 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002108 *
2109 * - fill in handshake headers
2110 * - update handshake checksum
2111 * - DTLS: save message for resending
2112 * - then pass to the record layer
2113 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002114 * DTLS: except for HelloRequest, messages are only queued, and will only be
2115 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002116 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002117 * Inputs:
2118 * - ssl->out_msglen: 4 + actual handshake message len
2119 * (4 is the size of handshake headers for TLS)
2120 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2121 * - ssl->out_msg + 4: the handshake message body
2122 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002123 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002124 * - ssl->out_msglen: the length of the record contents
2125 * (including handshake headers but excluding record headers)
2126 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002127 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002128int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129{
Janos Follath865b3eb2019-12-16 11:46:15 +00002130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002131 const size_t hs_len = ssl->out_msglen - 4;
2132 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2135
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002136 /*
2137 * Sanity checks
2138 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002139 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002140 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2141 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2143 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002144 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002146 /* Whenever we send anything different from a
2147 * HelloRequest we should be in a handshake - double check. */
2148 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2149 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002150 ssl->handshake == NULL )
2151 {
2152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2153 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2154 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002158 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002160 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002163 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002164#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002165
Hanno Beckerb50a2532018-08-06 11:52:54 +01002166 /* Double-check that we did not exceed the bounds
2167 * of the outgoing record buffer.
2168 * This should never fail as the various message
2169 * writing functions must obey the bounds of the
2170 * outgoing record buffer, but better be safe.
2171 *
2172 * Note: We deliberately do not check for the MTU or MFL here.
2173 */
2174 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2175 {
2176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002177 "size %" MBEDTLS_PRINTF_SIZET
2178 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002179 ssl->out_msglen,
2180 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2182 }
2183
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002184 /*
2185 * Fill handshake headers
2186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002189 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2190 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2191 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002193 /*
2194 * DTLS has additional fields in the Handshake layer,
2195 * between the length field and the actual payload:
2196 * uint16 message_seq;
2197 * uint24 fragment_offset;
2198 * uint24 fragment_length;
2199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002201 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002202 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002203 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002204 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002205 {
2206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002207 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002208 hs_len,
2209 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002210 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2211 }
2212
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002213 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002214 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002215
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002216 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002217 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002218 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002219 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2220 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2221 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002222 }
2223 else
2224 {
2225 ssl->out_msg[4] = 0;
2226 ssl->out_msg[5] = 0;
2227 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002228
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002229 /* Handshake hashes are computed without fragmentation,
2230 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002231 memset( ssl->out_msg + 6, 0x00, 3 );
2232 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002235
Hanno Becker0207e532018-08-28 10:28:28 +01002236 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002237 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2238 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002241 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002243 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002244 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2245 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002246 {
2247 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002250 return( ret );
2251 }
2252 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002253 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002254#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002255 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002256 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002257 {
2258 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2259 return( ret );
2260 }
2261 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002262
2263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2264
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002265 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002266}
2267
2268/*
2269 * Record layer functions
2270 */
2271
2272/*
2273 * Write current record.
2274 *
2275 * Uses:
2276 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2277 * - ssl->out_msglen: length of the record content (excl headers)
2278 * - ssl->out_msg: record content
2279 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002280int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002281{
2282 int ret, done = 0;
2283 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002284 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002285
2286 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002287
Paul Bakker05ef8352012-05-08 09:17:57 +00002288 if( !done )
2289 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002290 unsigned i;
2291 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002292#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2293 size_t out_buf_len = ssl->out_buf_len;
2294#else
2295 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2296#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002297 /* Skip writing the record content type to after the encryption,
2298 * as it may change when using the CID extension. */
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002301 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002302
Hanno Becker19859472018-08-06 09:40:20 +01002303 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002304 ssl->out_len[0] = (unsigned char)( len >> 8 );
2305 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002306
Paul Bakker48916f92012-09-16 19:57:18 +00002307 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002308 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002309 mbedtls_record rec;
2310
2311 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002312 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002313 rec.data_len = ssl->out_msglen;
2314 rec.data_offset = ssl->out_msg - rec.buf;
2315
2316 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2317 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2318 ssl->conf->transport, rec.ver );
2319 rec.type = ssl->out_msgtype;
2320
Hanno Beckera0e20d02019-05-15 14:03:01 +01002321#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002322 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002323 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002324#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002325
Hanno Beckera18d1322018-01-03 14:27:32 +00002326 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002330 return( ret );
2331 }
2332
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 if( rec.data_offset != 0 )
2334 {
2335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2337 }
2338
Hanno Becker6430faf2019-05-08 11:57:13 +01002339 /* Update the record content type and CID. */
2340 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002341#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002342 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002343#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002344 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2346 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002347 }
2348
Hanno Becker5903de42019-05-03 14:46:38 +01002349 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002350
2351#if defined(MBEDTLS_SSL_PROTO_DTLS)
2352 /* In case of DTLS, double-check that we don't exceed
2353 * the remaining space in the datagram. */
2354 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2355 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002356 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002357 if( ret < 0 )
2358 return( ret );
2359
2360 if( protected_record_size > (size_t) ret )
2361 {
2362 /* Should never happen */
2363 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2364 }
2365 }
2366#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002367
Hanno Becker6430faf2019-05-08 11:57:13 +01002368 /* Now write the potentially updated record content type. */
2369 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2370
Paul Elliott9f352112020-12-09 14:55:45 +00002371 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002372 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002373 ssl->out_hdr[0], ssl->out_hdr[1],
2374 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002377 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002378
2379 ssl->out_left += protected_record_size;
2380 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002381 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002382
Hanno Beckerdd772292020-02-05 10:38:31 +00002383 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002384 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2385 break;
2386
2387 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002388 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002389 {
2390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2391 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2392 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 }
2394
Hanno Becker67bc7c32018-08-06 11:33:50 +01002395#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002396 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2397 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002398 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002399 size_t remaining;
2400 ret = ssl_get_remaining_payload_in_datagram( ssl );
2401 if( ret < 0 )
2402 {
2403 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2404 ret );
2405 return( ret );
2406 }
2407
2408 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002409 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002410 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002411 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002412 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002413 else
2414 {
Hanno Becker513815a2018-08-20 11:56:09 +01002415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002416 }
2417 }
2418#endif /* MBEDTLS_SSL_PROTO_DTLS */
2419
2420 if( ( flush == SSL_FORCE_FLUSH ) &&
2421 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 return( ret );
2425 }
2426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428
2429 return( 0 );
2430}
2431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002433
2434static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2435{
2436 if( ssl->in_msglen < ssl->in_hslen ||
2437 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2438 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2439 {
2440 return( 1 );
2441 }
2442 return( 0 );
2443}
Hanno Becker44650b72018-08-16 12:51:11 +01002444
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002445static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002446{
2447 return( ( ssl->in_msg[9] << 16 ) |
2448 ( ssl->in_msg[10] << 8 ) |
2449 ssl->in_msg[11] );
2450}
2451
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002452static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002453{
2454 return( ( ssl->in_msg[6] << 16 ) |
2455 ( ssl->in_msg[7] << 8 ) |
2456 ssl->in_msg[8] );
2457}
2458
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002459static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002460{
2461 uint32_t msg_len, frag_off, frag_len;
2462
2463 msg_len = ssl_get_hs_total_len( ssl );
2464 frag_off = ssl_get_hs_frag_off( ssl );
2465 frag_len = ssl_get_hs_frag_len( ssl );
2466
2467 if( frag_off > msg_len )
2468 return( -1 );
2469
2470 if( frag_len > msg_len - frag_off )
2471 return( -1 );
2472
2473 if( frag_len + 12 > ssl->in_msglen )
2474 return( -1 );
2475
2476 return( 0 );
2477}
2478
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002479/*
2480 * Mark bits in bitmask (used for DTLS HS reassembly)
2481 */
2482static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2483{
2484 unsigned int start_bits, end_bits;
2485
2486 start_bits = 8 - ( offset % 8 );
2487 if( start_bits != 8 )
2488 {
2489 size_t first_byte_idx = offset / 8;
2490
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002491 /* Special case */
2492 if( len <= start_bits )
2493 {
2494 for( ; len != 0; len-- )
2495 mask[first_byte_idx] |= 1 << ( start_bits - len );
2496
2497 /* Avoid potential issues with offset or len becoming invalid */
2498 return;
2499 }
2500
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002501 offset += start_bits; /* Now offset % 8 == 0 */
2502 len -= start_bits;
2503
2504 for( ; start_bits != 0; start_bits-- )
2505 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2506 }
2507
2508 end_bits = len % 8;
2509 if( end_bits != 0 )
2510 {
2511 size_t last_byte_idx = ( offset + len ) / 8;
2512
2513 len -= end_bits; /* Now len % 8 == 0 */
2514
2515 for( ; end_bits != 0; end_bits-- )
2516 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2517 }
2518
2519 memset( mask + offset / 8, 0xFF, len / 8 );
2520}
2521
2522/*
2523 * Check that bitmask is full
2524 */
2525static int ssl_bitmask_check( unsigned char *mask, size_t len )
2526{
2527 size_t i;
2528
2529 for( i = 0; i < len / 8; i++ )
2530 if( mask[i] != 0xFF )
2531 return( -1 );
2532
2533 for( i = 0; i < len % 8; i++ )
2534 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2535 return( -1 );
2536
2537 return( 0 );
2538}
2539
Hanno Becker56e205e2018-08-16 09:06:12 +01002540/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002541static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002542 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002543{
Hanno Becker56e205e2018-08-16 09:06:12 +01002544 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002545
Hanno Becker56e205e2018-08-16 09:06:12 +01002546 alloc_len = 12; /* Handshake header */
2547 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002548
Hanno Beckerd07df862018-08-16 09:14:58 +01002549 if( add_bitmap )
2550 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002551
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002552 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002553}
Hanno Becker56e205e2018-08-16 09:06:12 +01002554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002556
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002557static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002558{
2559 return( ( ssl->in_msg[1] << 16 ) |
2560 ( ssl->in_msg[2] << 8 ) |
2561 ssl->in_msg[3] );
2562}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002563
Simon Butcher99000142016-10-13 17:21:01 +01002564int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002565{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002567 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002569 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002571 }
2572
Hanno Becker12555c62018-08-16 12:47:53 +01002573 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002576 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002577 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002580 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002581 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002583 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002584
Hanno Becker44650b72018-08-16 12:51:11 +01002585 if( ssl_check_hs_header( ssl ) != 0 )
2586 {
2587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2588 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2589 }
2590
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002591 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002592 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2593 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2594 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2595 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002596 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002597 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2598 {
2599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2600 recv_msg_seq,
2601 ssl->handshake->in_msg_seq ) );
2602 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2603 }
2604
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002605 /* Retransmit only on last message from previous flight, to avoid
2606 * too many retransmissions.
2607 * Besides, No sane server ever retransmits HelloVerifyRequest */
2608 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002612 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002613 recv_msg_seq,
2614 ssl->handshake->in_flight_start_seq ) );
2615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002619 return( ret );
2620 }
2621 }
2622 else
2623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002625 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002626 recv_msg_seq,
2627 ssl->handshake->in_msg_seq ) );
2628 }
2629
Hanno Becker90333da2017-10-10 11:27:13 +01002630 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002631 }
2632 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002633
Hanno Becker6d97ef52018-08-16 13:09:04 +01002634 /* Message reassembly is handled alongside buffering of future
2635 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002636 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002637 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002638 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002641 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002642 }
2643 }
2644 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002645#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002646 /* With TLS we don't handle fragmentation (for now) */
2647 if( ssl->in_msglen < ssl->in_hslen )
2648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2650 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002651 }
2652
Simon Butcher99000142016-10-13 17:21:01 +01002653 return( 0 );
2654}
2655
2656void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2657{
Hanno Becker0271f962018-08-16 13:23:47 +01002658 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002659
Hanno Becker0271f962018-08-16 13:23:47 +01002660 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002661 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002662 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002663 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002664
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002665 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002668 ssl->handshake != NULL )
2669 {
Hanno Becker0271f962018-08-16 13:23:47 +01002670 unsigned offset;
2671 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002672
Hanno Becker0271f962018-08-16 13:23:47 +01002673 /* Increment handshake sequence number */
2674 hs->in_msg_seq++;
2675
2676 /*
2677 * Clear up handshake buffering and reassembly structure.
2678 */
2679
2680 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002681 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002682
2683 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002684 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2685 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002686 offset++, hs_buf++ )
2687 {
2688 *hs_buf = *(hs_buf + 1);
2689 }
2690
2691 /* Create a fresh last entry */
2692 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002693 }
2694#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002695}
2696
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002697/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002698 * DTLS anti-replay: RFC 6347 4.1.2.6
2699 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002700 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2701 * Bit n is set iff record number in_window_top - n has been seen.
2702 *
2703 * Usually, in_window_top is the last record number seen and the lsb of
2704 * in_window is set. The only exception is the initial state (record number 0
2705 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002708void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002709{
2710 ssl->in_window_top = 0;
2711 ssl->in_window = 0;
2712}
2713
2714static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2715{
2716 return( ( (uint64_t) buf[0] << 40 ) |
2717 ( (uint64_t) buf[1] << 32 ) |
2718 ( (uint64_t) buf[2] << 24 ) |
2719 ( (uint64_t) buf[3] << 16 ) |
2720 ( (uint64_t) buf[4] << 8 ) |
2721 ( (uint64_t) buf[5] ) );
2722}
2723
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002724static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
2725{
Janos Follath865b3eb2019-12-16 11:46:15 +00002726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002727 unsigned char *original_in_ctr;
2728
2729 // save original in_ctr
2730 original_in_ctr = ssl->in_ctr;
2731
2732 // use counter from record
2733 ssl->in_ctr = record_in_ctr;
2734
2735 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
2736
2737 // restore the counter
2738 ssl->in_ctr = original_in_ctr;
2739
2740 return ret;
2741}
2742
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002743/*
2744 * Return 0 if sequence number is acceptable, -1 otherwise
2745 */
Hanno Becker0183d692019-07-12 08:50:37 +01002746int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002747{
2748 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2749 uint64_t bit;
2750
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002751 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002752 return( 0 );
2753
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002754 if( rec_seqnum > ssl->in_window_top )
2755 return( 0 );
2756
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002757 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002758
2759 if( bit >= 64 )
2760 return( -1 );
2761
2762 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2763 return( -1 );
2764
2765 return( 0 );
2766}
2767
2768/*
2769 * Update replay window on new validated record
2770 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002772{
2773 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2774
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002775 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002776 return;
2777
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002778 if( rec_seqnum > ssl->in_window_top )
2779 {
2780 /* Update window_top and the contents of the window */
2781 uint64_t shift = rec_seqnum - ssl->in_window_top;
2782
2783 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002784 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002785 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002786 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002787 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002788 ssl->in_window |= 1;
2789 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002790
2791 ssl->in_window_top = rec_seqnum;
2792 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002793 else
2794 {
2795 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002796 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002797
2798 if( bit < 64 ) /* Always true, but be extra sure */
2799 ssl->in_window |= (uint64_t) 1 << bit;
2800 }
2801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002803
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02002804#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002805/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002806 * Without any SSL context, check if a datagram looks like a ClientHello with
2807 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01002808 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002809 *
2810 * - if cookie is valid, return 0
2811 * - if ClientHello looks superficially valid but cookie is not,
2812 * fill obuf and set olen, then
2813 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
2814 * - otherwise return a specific error code
2815 */
2816static int ssl_check_dtls_clihlo_cookie(
2817 mbedtls_ssl_cookie_write_t *f_cookie_write,
2818 mbedtls_ssl_cookie_check_t *f_cookie_check,
2819 void *p_cookie,
2820 const unsigned char *cli_id, size_t cli_id_len,
2821 const unsigned char *in, size_t in_len,
2822 unsigned char *obuf, size_t buf_len, size_t *olen )
2823{
2824 size_t sid_len, cookie_len;
2825 unsigned char *p;
2826
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002827 /*
2828 * Structure of ClientHello with record and handshake headers,
2829 * and expected values. We don't need to check a lot, more checks will be
2830 * done when actually parsing the ClientHello - skipping those checks
2831 * avoids code duplication and does not make cookie forging any easier.
2832 *
2833 * 0-0 ContentType type; copied, must be handshake
2834 * 1-2 ProtocolVersion version; copied
2835 * 3-4 uint16 epoch; copied, must be 0
2836 * 5-10 uint48 sequence_number; copied
2837 * 11-12 uint16 length; (ignored)
2838 *
2839 * 13-13 HandshakeType msg_type; (ignored)
2840 * 14-16 uint24 length; (ignored)
2841 * 17-18 uint16 message_seq; copied
2842 * 19-21 uint24 fragment_offset; copied, must be 0
2843 * 22-24 uint24 fragment_length; (ignored)
2844 *
2845 * 25-26 ProtocolVersion client_version; (ignored)
2846 * 27-58 Random random; (ignored)
2847 * 59-xx SessionID session_id; 1 byte len + sid_len content
2848 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
2849 * ...
2850 *
2851 * Minimum length is 61 bytes.
2852 */
2853 if( in_len < 61 ||
2854 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
2855 in[3] != 0 || in[4] != 0 ||
2856 in[19] != 0 || in[20] != 0 || in[21] != 0 )
2857 {
Hanno Becker90d59dd2021-06-24 11:17:13 +01002858 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002859 }
2860
2861 sid_len = in[59];
2862 if( sid_len > in_len - 61 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01002863 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002864
2865 cookie_len = in[60 + sid_len];
2866 if( cookie_len > in_len - 60 )
Hanno Becker90d59dd2021-06-24 11:17:13 +01002867 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002868
2869 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
2870 cli_id, cli_id_len ) == 0 )
2871 {
2872 /* Valid cookie */
2873 return( 0 );
2874 }
2875
2876 /*
2877 * If we get here, we've got an invalid cookie, let's prepare HVR.
2878 *
2879 * 0-0 ContentType type; copied
2880 * 1-2 ProtocolVersion version; copied
2881 * 3-4 uint16 epoch; copied
2882 * 5-10 uint48 sequence_number; copied
2883 * 11-12 uint16 length; olen - 13
2884 *
2885 * 13-13 HandshakeType msg_type; hello_verify_request
2886 * 14-16 uint24 length; olen - 25
2887 * 17-18 uint16 message_seq; copied
2888 * 19-21 uint24 fragment_offset; copied
2889 * 22-24 uint24 fragment_length; olen - 25
2890 *
2891 * 25-26 ProtocolVersion server_version; 0xfe 0xff
2892 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
2893 *
2894 * Minimum length is 28.
2895 */
2896 if( buf_len < 28 )
2897 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2898
2899 /* Copy most fields and adapt others */
2900 memcpy( obuf, in, 25 );
2901 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
2902 obuf[25] = 0xfe;
2903 obuf[26] = 0xff;
2904
2905 /* Generate and write actual cookie */
2906 p = obuf + 28;
2907 if( f_cookie_write( p_cookie,
2908 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
2909 {
2910 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2911 }
2912
2913 *olen = p - obuf;
2914
2915 /* Go back and fill length fields */
2916 obuf[27] = (unsigned char)( *olen - 28 );
2917
2918 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
2919 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
2920 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
2921
2922 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
2923 obuf[12] = (unsigned char)( ( *olen - 13 ) );
2924
2925 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
2926}
2927
2928/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002929 * Handle possible client reconnect with the same UDP quadruplet
2930 * (RFC 6347 Section 4.2.8).
2931 *
2932 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
2933 * that looks like a ClientHello.
2934 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002935 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01002936 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002937 * - if the input looks like a ClientHello with a valid cookie,
2938 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02002939 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002940 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002941 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01002942 * This function is called (through ssl_check_client_reconnect()) when an
2943 * unexpected record is found in ssl_get_next_record(), which will discard the
2944 * record if we return 0, and bubble up the return value otherwise (this
2945 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
2946 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002947 */
2948static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
2949{
Janos Follath865b3eb2019-12-16 11:46:15 +00002950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002951 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002952
Hanno Becker2fddd372019-07-10 14:37:41 +01002953 if( ssl->conf->f_cookie_write == NULL ||
2954 ssl->conf->f_cookie_check == NULL )
2955 {
2956 /* If we can't use cookies to verify reachability of the peer,
2957 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02002958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
2959 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01002960 return( 0 );
2961 }
2962
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002963 ret = ssl_check_dtls_clihlo_cookie(
2964 ssl->conf->f_cookie_write,
2965 ssl->conf->f_cookie_check,
2966 ssl->conf->p_cookie,
2967 ssl->cli_id, ssl->cli_id_len,
2968 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10002969 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002970
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002971 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
2972
2973 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002974 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02002975 int send_ret;
2976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
2977 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
2978 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08002979 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002980 * If the error is permanent we'll catch it later,
2981 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02002982 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
2983 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
2984 (void) send_ret;
2985
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01002986 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002987 }
2988
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002989 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002990 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02002991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00002992 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002993 {
2994 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
2995 return( ret );
2996 }
2997
2998 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002999 }
3000
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003001 return( ret );
3002}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003003#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003004
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003005static int ssl_check_record_type( uint8_t record_type )
3006{
3007 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3008 record_type != MBEDTLS_SSL_MSG_ALERT &&
3009 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3010 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3011 {
3012 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3013 }
3014
3015 return( 0 );
3016}
3017
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003018/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003019 * ContentType type;
3020 * ProtocolVersion version;
3021 * uint16 epoch; // DTLS only
3022 * uint48 sequence_number; // DTLS only
3023 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003024 *
3025 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003026 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003027 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3028 *
3029 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003030 * 1. proceed with the record if this function returns 0
3031 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3032 * 3. return CLIENT_RECONNECT if this function return that value
3033 * 4. drop the whole datagram if this function returns anything else.
3034 * Point 2 is needed when the peer is resending, and we have already received
3035 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003036 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003037static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003038 unsigned char *buf,
3039 size_t len,
3040 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003041{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003042 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Hanno Beckere5e7e782019-07-11 12:29:35 +01003044 size_t const rec_hdr_type_offset = 0;
3045 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003046
Hanno Beckere5e7e782019-07-11 12:29:35 +01003047 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3048 rec_hdr_type_len;
3049 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Hanno Beckere5e7e782019-07-11 12:29:35 +01003051 size_t const rec_hdr_ctr_len = 8;
3052#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003053 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003054 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3055 rec_hdr_version_len;
3056
Hanno Beckera0e20d02019-05-15 14:03:01 +01003057#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003058 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3059 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003060 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003061#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3062#endif /* MBEDTLS_SSL_PROTO_DTLS */
3063
3064 size_t rec_hdr_len_offset; /* To be determined */
3065 size_t const rec_hdr_len_len = 2;
3066
3067 /*
3068 * Check minimum lengths for record header.
3069 */
3070
3071#if defined(MBEDTLS_SSL_PROTO_DTLS)
3072 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3073 {
3074 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3075 }
3076 else
3077#endif /* MBEDTLS_SSL_PROTO_DTLS */
3078 {
3079 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3080 }
3081
3082 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3083 {
3084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3085 (unsigned) len,
3086 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3087 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3088 }
3089
3090 /*
3091 * Parse and validate record content type
3092 */
3093
3094 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003095
3096 /* Check record content type */
3097#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3098 rec->cid_len = 0;
3099
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003101 ssl->conf->cid_len != 0 &&
3102 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003103 {
3104 /* Shift pointers to account for record header including CID
3105 * struct {
3106 * ContentType special_type = tls12_cid;
3107 * ProtocolVersion version;
3108 * uint16 epoch;
3109 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003110 * opaque cid[cid_length]; // Additional field compared to
3111 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003112 * uint16 length;
3113 * opaque enc_content[DTLSCiphertext.length];
3114 * } DTLSCiphertext;
3115 */
3116
3117 /* So far, we only support static CID lengths
3118 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003119 rec_hdr_cid_len = ssl->conf->cid_len;
3120 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003121
Hanno Beckere5e7e782019-07-11 12:29:35 +01003122 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003123 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3125 (unsigned) len,
3126 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003128 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003129
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003130 /* configured CID len is guaranteed at most 255, see
3131 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3132 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003133 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003134 }
3135 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003136#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003137 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003138 if( ssl_check_record_type( rec->type ) )
3139 {
Hanno Becker54229812019-07-12 14:40:00 +01003140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3141 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003142 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3143 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003144 }
3145
Hanno Beckere5e7e782019-07-11 12:29:35 +01003146 /*
3147 * Parse and validate record version
3148 */
3149
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003150 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3151 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003152 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3153 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003154 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003155
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003156 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 }
3161
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003162 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3165 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 }
3167
Hanno Beckere5e7e782019-07-11 12:29:35 +01003168 /*
3169 * Parse/Copy record sequence number.
3170 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003171
Hanno Beckere5e7e782019-07-11 12:29:35 +01003172#if defined(MBEDTLS_SSL_PROTO_DTLS)
3173 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003174 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003175 /* Copy explicit record sequence number from input buffer. */
3176 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3177 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003178 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003179 else
3180#endif /* MBEDTLS_SSL_PROTO_DTLS */
3181 {
3182 /* Copy implicit record sequence number from SSL context structure. */
3183 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3184 }
Paul Bakker40e46942009-01-03 21:51:57 +00003185
Hanno Beckere5e7e782019-07-11 12:29:35 +01003186 /*
3187 * Parse record length.
3188 */
3189
Hanno Beckere5e7e782019-07-11 12:29:35 +01003190 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003191 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3192 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003193 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003194
Paul Elliott9f352112020-12-09 14:55:45 +00003195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003196 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003197 rec->type,
3198 major_ver, minor_ver, rec->data_len ) );
3199
3200 rec->buf = buf;
3201 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003202
Hanno Beckerd417cc92019-07-26 08:20:27 +01003203 if( rec->data_len == 0 )
3204 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003205
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003206 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003207 * DTLS-related tests.
3208 * Check epoch before checking length constraint because
3209 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3210 * message gets duplicated before the corresponding Finished message,
3211 * the second ChangeCipherSpec should be discarded because it belongs
3212 * to an old epoch, but not because its length is shorter than
3213 * the minimum record length for packets using the new record transform.
3214 * Note that these two kinds of failures are handled differently,
3215 * as an unexpected record is silently skipped but an invalid
3216 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003217 */
3218#if defined(MBEDTLS_SSL_PROTO_DTLS)
3219 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3220 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003221 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003222
Hanno Becker955a5c92019-07-10 17:12:07 +01003223 /* Check that the datagram is large enough to contain a record
3224 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003225 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003226 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3228 (unsigned) len,
3229 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003230 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3231 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003232
Hanno Becker37cfe732019-07-10 17:20:01 +01003233 /* Records from other, non-matching epochs are silently discarded.
3234 * (The case of same-port Client reconnects must be considered in
3235 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003236 if( rec_epoch != ssl->in_epoch )
3237 {
3238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003239 "expected %u, received %lu",
3240 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003241
Hanno Becker552f7472019-07-19 10:59:12 +01003242 /* Records from the next epoch are considered for buffering
3243 * (concretely: early Finished messages). */
3244 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003245 {
Hanno Becker552f7472019-07-19 10:59:12 +01003246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3247 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003248 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003249
Hanno Becker2fddd372019-07-10 14:37:41 +01003250 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003251 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003252#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003253 /* For records from the correct epoch, check whether their
3254 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003255 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3256 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003257 {
3258 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3259 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3260 }
3261#endif
3262 }
3263#endif /* MBEDTLS_SSL_PROTO_DTLS */
3264
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003265 return( 0 );
3266}
Paul Bakker5121ce52009-01-03 21:22:43 +00003267
Paul Bakker5121ce52009-01-03 21:22:43 +00003268
Hanno Becker2fddd372019-07-10 14:37:41 +01003269#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3270static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3271{
3272 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3273
3274 /*
3275 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3276 * access the first byte of record content (handshake type), as we
3277 * have an active transform (possibly iv_len != 0), so use the
3278 * fact that the record header len is 13 instead.
3279 */
3280 if( rec_epoch == 0 &&
3281 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3282 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3283 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3284 ssl->in_left > 13 &&
3285 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3286 {
3287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3288 "from the same port" ) );
3289 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
3291
3292 return( 0 );
3293}
Hanno Becker2fddd372019-07-10 14:37:41 +01003294#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003295
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003296/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003297 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003298 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003299static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3300 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003301{
3302 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003305 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Paul Bakker48916f92012-09-16 19:57:18 +00003307 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003309 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003310
Hanno Beckera18d1322018-01-03 14:27:32 +00003311 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003312 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003315
Hanno Beckera0e20d02019-05-15 14:03:01 +01003316#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003317 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3318 ssl->conf->ignore_unexpected_cid
3319 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3320 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003321 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003322 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003323 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003324#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003325
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 return( ret );
3327 }
3328
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003329 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003330 {
3331 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003332 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003333 }
3334
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003335 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003336 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003337
Hanno Beckera0e20d02019-05-15 14:03:01 +01003338#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003339 /* We have already checked the record content type
3340 * in ssl_parse_record_header(), failing or silently
3341 * dropping the record in the case of an unknown type.
3342 *
3343 * Since with the use of CIDs, the record content type
3344 * might change during decryption, re-check the record
3345 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003346 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003347 {
3348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3349 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3350 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003352
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003353 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003354 {
3355#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3356 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003357 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003358 {
3359 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3361 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3362 }
3363#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3364
3365 ssl->nb_zero++;
3366
3367 /*
3368 * Three or more empty messages may be a DoS attack
3369 * (excessive CPU consumption).
3370 */
3371 if( ssl->nb_zero > 3 )
3372 {
3373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003374 "messages, possible DoS attack" ) );
3375 /* Treat the records as if they were not properly authenticated,
3376 * thereby failing the connection if we see more than allowed
3377 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003378 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3379 }
3380 }
3381 else
3382 ssl->nb_zero = 0;
3383
3384#if defined(MBEDTLS_SSL_PROTO_DTLS)
3385 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3386 {
3387 ; /* in_ctr read from peer, not maintained internally */
3388 }
3389 else
3390#endif
3391 {
3392 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003393 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003394 if( ++ssl->in_ctr[i - 1] != 0 )
3395 break;
3396
3397 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003398 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003399 {
3400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3401 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3402 }
3403 }
3404
Paul Bakker5121ce52009-01-03 21:22:43 +00003405 }
3406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003411 }
3412#endif
3413
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003414 /* Check actual (decrypted) record content length against
3415 * configured maximum. */
3416 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3417 {
3418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3419 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3420 }
3421
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003422 return( 0 );
3423}
3424
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003425/*
3426 * Read a record.
3427 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003428 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3429 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3430 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003431 */
Hanno Becker1097b342018-08-15 14:09:41 +01003432
3433/* Helper functions for mbedtls_ssl_read_record(). */
3434static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003435static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3436static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003437
Hanno Becker327c93b2018-08-15 13:56:18 +01003438int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003439 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003440{
Janos Follath865b3eb2019-12-16 11:46:15 +00003441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003444
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003445 if( ssl->keep_current_message == 0 )
3446 {
3447 do {
Simon Butcher99000142016-10-13 17:21:01 +01003448
Hanno Becker26994592018-08-15 14:14:59 +01003449 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003450 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003451 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003452
Hanno Beckere74d5562018-08-15 14:26:08 +01003453 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003454 {
Hanno Becker40f50842018-08-15 14:48:01 +01003455#if defined(MBEDTLS_SSL_PROTO_DTLS)
3456 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003457
Hanno Becker40f50842018-08-15 14:48:01 +01003458 /* We only check for buffered messages if the
3459 * current datagram is fully consumed. */
3460 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003461 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003462 {
Hanno Becker40f50842018-08-15 14:48:01 +01003463 if( ssl_load_buffered_message( ssl ) == 0 )
3464 have_buffered = 1;
3465 }
3466
3467 if( have_buffered == 0 )
3468#endif /* MBEDTLS_SSL_PROTO_DTLS */
3469 {
3470 ret = ssl_get_next_record( ssl );
3471 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3472 continue;
3473
3474 if( ret != 0 )
3475 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003476 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003477 return( ret );
3478 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003479 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003480 }
3481
3482 ret = mbedtls_ssl_handle_message_type( ssl );
3483
Hanno Becker40f50842018-08-15 14:48:01 +01003484#if defined(MBEDTLS_SSL_PROTO_DTLS)
3485 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3486 {
3487 /* Buffer future message */
3488 ret = ssl_buffer_message( ssl );
3489 if( ret != 0 )
3490 return( ret );
3491
3492 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3493 }
3494#endif /* MBEDTLS_SSL_PROTO_DTLS */
3495
Hanno Becker90333da2017-10-10 11:27:13 +01003496 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3497 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003498
3499 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003500 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003501 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003502 return( ret );
3503 }
3504
Hanno Becker327c93b2018-08-15 13:56:18 +01003505 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003506 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003507 {
3508 mbedtls_ssl_update_handshake_status( ssl );
3509 }
Simon Butcher99000142016-10-13 17:21:01 +01003510 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003511 else
Simon Butcher99000142016-10-13 17:21:01 +01003512 {
Hanno Becker02f59072018-08-15 14:00:24 +01003513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003514 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003515 }
3516
3517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3518
3519 return( 0 );
3520}
3521
Hanno Becker40f50842018-08-15 14:48:01 +01003522#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003523static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003524{
Hanno Becker40f50842018-08-15 14:48:01 +01003525 if( ssl->in_left > ssl->next_record_offset )
3526 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003527
Hanno Becker40f50842018-08-15 14:48:01 +01003528 return( 0 );
3529}
3530
3531static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3532{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003533 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003534 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003535 int ret = 0;
3536
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003537 if( hs == NULL )
3538 return( -1 );
3539
Hanno Beckere00ae372018-08-20 09:39:42 +01003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3541
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003542 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3543 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3544 {
3545 /* Check if we have seen a ChangeCipherSpec before.
3546 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003547 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003548 {
3549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3550 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003551 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003552 }
3553
Hanno Becker39b8bc92018-08-28 17:17:13 +01003554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003555 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3556 ssl->in_msglen = 1;
3557 ssl->in_msg[0] = 1;
3558
3559 /* As long as they are equal, the exact value doesn't matter. */
3560 ssl->in_left = 0;
3561 ssl->next_record_offset = 0;
3562
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003563 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003564 goto exit;
3565 }
Hanno Becker37f95322018-08-16 13:55:32 +01003566
Hanno Beckerb8f50142018-08-28 10:01:34 +01003567#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003568 /* Debug only */
3569 {
3570 unsigned offset;
3571 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3572 {
3573 hs_buf = &hs->buffering.hs[offset];
3574 if( hs_buf->is_valid == 1 )
3575 {
3576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3577 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003578 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003579 }
3580 }
3581 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003582#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003583
3584 /* Check if we have buffered and/or fully reassembled the
3585 * next handshake message. */
3586 hs_buf = &hs->buffering.hs[0];
3587 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3588 {
3589 /* Synthesize a record containing the buffered HS message. */
3590 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3591 ( hs_buf->data[2] << 8 ) |
3592 hs_buf->data[3];
3593
3594 /* Double-check that we haven't accidentally buffered
3595 * a message that doesn't fit into the input buffer. */
3596 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3597 {
3598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3600 }
3601
3602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3603 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3604 hs_buf->data, msg_len + 12 );
3605
3606 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3607 ssl->in_hslen = msg_len + 12;
3608 ssl->in_msglen = msg_len + 12;
3609 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3610
3611 ret = 0;
3612 goto exit;
3613 }
3614 else
3615 {
3616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3617 hs->in_msg_seq ) );
3618 }
3619
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003620 ret = -1;
3621
3622exit:
3623
3624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3625 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003626}
3627
Hanno Beckera02b0b42018-08-21 17:20:27 +01003628static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3629 size_t desired )
3630{
3631 int offset;
3632 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3634 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003635
Hanno Becker01315ea2018-08-21 17:22:17 +01003636 /* Get rid of future records epoch first, if such exist. */
3637 ssl_free_buffered_record( ssl );
3638
3639 /* Check if we have enough space available now. */
3640 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3641 hs->buffering.total_bytes_buffered ) )
3642 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003644 return( 0 );
3645 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003646
Hanno Becker4f432ad2018-08-28 10:02:32 +01003647 /* We don't have enough space to buffer the next expected handshake
3648 * message. Remove buffers used for future messages to gain space,
3649 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003650 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3651 offset >= 0; offset-- )
3652 {
3653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3654 offset ) );
3655
Hanno Beckerb309b922018-08-23 13:18:05 +01003656 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003657
3658 /* Check if we have enough space available now. */
3659 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3660 hs->buffering.total_bytes_buffered ) )
3661 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003663 return( 0 );
3664 }
3665 }
3666
3667 return( -1 );
3668}
3669
Hanno Becker40f50842018-08-15 14:48:01 +01003670static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3671{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003672 int ret = 0;
3673 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3674
3675 if( hs == NULL )
3676 return( 0 );
3677
3678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3679
3680 switch( ssl->in_msgtype )
3681 {
3682 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003684
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003685 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003686 break;
3687
3688 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003689 {
3690 unsigned recv_msg_seq_offset;
3691 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3692 mbedtls_ssl_hs_buffer *hs_buf;
3693 size_t msg_len = ssl->in_hslen - 12;
3694
3695 /* We should never receive an old handshake
3696 * message - double-check nonetheless. */
3697 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3698 {
3699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3700 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3701 }
3702
3703 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3704 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3705 {
3706 /* Silently ignore -- message too far in the future */
3707 MBEDTLS_SSL_DEBUG_MSG( 2,
3708 ( "Ignore future HS message with sequence number %u, "
3709 "buffering window %u - %u",
3710 recv_msg_seq, ssl->handshake->in_msg_seq,
3711 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
3712
3713 goto exit;
3714 }
3715
3716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
3717 recv_msg_seq, recv_msg_seq_offset ) );
3718
3719 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
3720
3721 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003722 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01003723 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003724 size_t reassembly_buf_sz;
3725
Hanno Becker37f95322018-08-16 13:55:32 +01003726 hs_buf->is_fragmented =
3727 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
3728
3729 /* We copy the message back into the input buffer
3730 * after reassembly, so check that it's not too large.
3731 * This is an implementation-specific limitation
3732 * and not one from the standard, hence it is not
3733 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01003734 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01003735 {
3736 /* Ignore message */
3737 goto exit;
3738 }
3739
Hanno Beckere0b150f2018-08-21 15:51:03 +01003740 /* Check if we have enough space to buffer the message. */
3741 if( hs->buffering.total_bytes_buffered >
3742 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
3743 {
3744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3746 }
3747
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003748 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
3749 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003750
3751 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3752 hs->buffering.total_bytes_buffered ) )
3753 {
3754 if( recv_msg_seq_offset > 0 )
3755 {
3756 /* If we can't buffer a future message because
3757 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00003758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3759 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3760 " (already %" MBEDTLS_PRINTF_SIZET
3761 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003762 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003763 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003764 goto exit;
3765 }
Hanno Beckere1801392018-08-21 16:51:05 +01003766 else
3767 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3769 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3770 " (already %" MBEDTLS_PRINTF_SIZET
3771 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003772 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003773 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01003774 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003775
Hanno Beckera02b0b42018-08-21 17:20:27 +01003776 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003777 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
3779 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
3780 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
3781 " (already %" MBEDTLS_PRINTF_SIZET
3782 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00003783 msg_len,
3784 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00003785 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003786 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003787 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3788 goto exit;
3789 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003790 }
3791
Paul Elliottd48d5c62021-01-07 14:47:05 +00003792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01003793 msg_len ) );
3794
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003795 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
3796 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01003797 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01003798 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01003799 goto exit;
3800 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003801 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003802
3803 /* Prepare final header: copy msg_type, length and message_seq,
3804 * then add standardised fragment_offset and fragment_length */
3805 memcpy( hs_buf->data, ssl->in_msg, 6 );
3806 memset( hs_buf->data + 6, 0, 3 );
3807 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
3808
3809 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01003810
3811 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003812 }
3813 else
3814 {
3815 /* Make sure msg_type and length are consistent */
3816 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
3817 {
3818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
3819 /* Ignore */
3820 goto exit;
3821 }
3822 }
3823
Hanno Becker4422bbb2018-08-20 09:40:19 +01003824 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01003825 {
3826 size_t frag_len, frag_off;
3827 unsigned char * const msg = hs_buf->data + 12;
3828
3829 /*
3830 * Check and copy current fragment
3831 */
3832
3833 /* Validation of header fields already done in
3834 * mbedtls_ssl_prepare_handshake_record(). */
3835 frag_off = ssl_get_hs_frag_off( ssl );
3836 frag_len = ssl_get_hs_frag_len( ssl );
3837
Paul Elliottd48d5c62021-01-07 14:47:05 +00003838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
3839 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01003840 frag_off, frag_len ) );
3841 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
3842
3843 if( hs_buf->is_fragmented )
3844 {
3845 unsigned char * const bitmask = msg + msg_len;
3846 ssl_bitmask_set( bitmask, frag_off, frag_len );
3847 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
3848 msg_len ) == 0 );
3849 }
3850 else
3851 {
3852 hs_buf->is_complete = 1;
3853 }
3854
3855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
3856 hs_buf->is_complete ? "" : "not yet " ) );
3857 }
3858
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003859 break;
Hanno Becker37f95322018-08-16 13:55:32 +01003860 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003861
3862 default:
Hanno Becker360bef32018-08-28 10:04:33 +01003863 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003864 break;
3865 }
3866
3867exit:
3868
3869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
3870 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003871}
3872#endif /* MBEDTLS_SSL_PROTO_DTLS */
3873
Hanno Becker1097b342018-08-15 14:09:41 +01003874static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003875{
Hanno Becker4a810fb2017-05-24 16:27:30 +01003876 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01003877 * Consume last content-layer message and potentially
3878 * update in_msglen which keeps track of the contents'
3879 * consumption state.
3880 *
3881 * (1) Handshake messages:
3882 * Remove last handshake message, move content
3883 * and adapt in_msglen.
3884 *
3885 * (2) Alert messages:
3886 * Consume whole record content, in_msglen = 0.
3887 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01003888 * (3) Change cipher spec:
3889 * Consume whole record content, in_msglen = 0.
3890 *
3891 * (4) Application data:
3892 * Don't do anything - the record layer provides
3893 * the application data as a stream transport
3894 * and consumes through mbedtls_ssl_read only.
3895 *
3896 */
3897
3898 /* Case (1): Handshake messages */
3899 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003900 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01003901 /* Hard assertion to be sure that no application data
3902 * is in flight, as corrupting ssl->in_msglen during
3903 * ssl->in_offt != NULL is fatal. */
3904 if( ssl->in_offt != NULL )
3905 {
3906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3908 }
3909
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003910 /*
3911 * Get next Handshake message in the current record
3912 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003913
Hanno Becker4a810fb2017-05-24 16:27:30 +01003914 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01003915 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01003916 * current handshake content: If DTLS handshake
3917 * fragmentation is used, that's the fragment
3918 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01003919 * size here is faulty and should be changed at
3920 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01003921 * (2) While it doesn't seem to cause problems, one
3922 * has to be very careful not to assume that in_hslen
3923 * is always <= in_msglen in a sensible communication.
3924 * Again, it's wrong for DTLS handshake fragmentation.
3925 * The following check is therefore mandatory, and
3926 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01003927 * Additionally, ssl->in_hslen might be arbitrarily out of
3928 * bounds after handling a DTLS message with an unexpected
3929 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01003930 */
3931 if( ssl->in_hslen < ssl->in_msglen )
3932 {
3933 ssl->in_msglen -= ssl->in_hslen;
3934 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3935 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003936
Hanno Becker4a810fb2017-05-24 16:27:30 +01003937 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
3938 ssl->in_msg, ssl->in_msglen );
3939 }
3940 else
3941 {
3942 ssl->in_msglen = 0;
3943 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003944
Hanno Becker4a810fb2017-05-24 16:27:30 +01003945 ssl->in_hslen = 0;
3946 }
3947 /* Case (4): Application data */
3948 else if( ssl->in_offt != NULL )
3949 {
3950 return( 0 );
3951 }
3952 /* Everything else (CCS & Alerts) */
3953 else
3954 {
3955 ssl->in_msglen = 0;
3956 }
3957
Hanno Becker1097b342018-08-15 14:09:41 +01003958 return( 0 );
3959}
Hanno Becker4a810fb2017-05-24 16:27:30 +01003960
Hanno Beckere74d5562018-08-15 14:26:08 +01003961static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
3962{
Hanno Becker4a810fb2017-05-24 16:27:30 +01003963 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003964 return( 1 );
3965
3966 return( 0 );
3967}
3968
Hanno Becker5f066e72018-08-16 14:56:31 +01003969#if defined(MBEDTLS_SSL_PROTO_DTLS)
3970
3971static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
3972{
3973 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3974 if( hs == NULL )
3975 return;
3976
Hanno Becker01315ea2018-08-21 17:22:17 +01003977 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01003978 {
Hanno Becker01315ea2018-08-21 17:22:17 +01003979 hs->buffering.total_bytes_buffered -=
3980 hs->buffering.future_record.len;
3981
3982 mbedtls_free( hs->buffering.future_record.data );
3983 hs->buffering.future_record.data = NULL;
3984 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003985}
3986
3987static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
3988{
3989 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3990 unsigned char * rec;
3991 size_t rec_len;
3992 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00003993#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
3994 size_t in_buf_len = ssl->in_buf_len;
3995#else
3996 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
3997#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01003998 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3999 return( 0 );
4000
4001 if( hs == NULL )
4002 return( 0 );
4003
Hanno Becker5f066e72018-08-16 14:56:31 +01004004 rec = hs->buffering.future_record.data;
4005 rec_len = hs->buffering.future_record.len;
4006 rec_epoch = hs->buffering.future_record.epoch;
4007
4008 if( rec == NULL )
4009 return( 0 );
4010
Hanno Becker4cb782d2018-08-20 11:19:05 +01004011 /* Only consider loading future records if the
4012 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004013 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004014 return( 0 );
4015
Hanno Becker5f066e72018-08-16 14:56:31 +01004016 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4017
4018 if( rec_epoch != ssl->in_epoch )
4019 {
4020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4021 goto exit;
4022 }
4023
4024 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4025
4026 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004027 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004028 {
4029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4030 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4031 }
4032
4033 memcpy( ssl->in_hdr, rec, rec_len );
4034 ssl->in_left = rec_len;
4035 ssl->next_record_offset = 0;
4036
4037 ssl_free_buffered_record( ssl );
4038
4039exit:
4040 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4041 return( 0 );
4042}
4043
Hanno Becker519f15d2019-07-11 12:43:20 +01004044static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4045 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004046{
4047 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004048
4049 /* Don't buffer future records outside handshakes. */
4050 if( hs == NULL )
4051 return( 0 );
4052
4053 /* Only buffer handshake records (we are only interested
4054 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004055 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004056 return( 0 );
4057
4058 /* Don't buffer more than one future epoch record. */
4059 if( hs->buffering.future_record.data != NULL )
4060 return( 0 );
4061
Hanno Becker01315ea2018-08-21 17:22:17 +01004062 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004063 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004064 hs->buffering.total_bytes_buffered ) )
4065 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4067 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4068 " (already %" MBEDTLS_PRINTF_SIZET
4069 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004070 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004071 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004072 return( 0 );
4073 }
4074
Hanno Becker5f066e72018-08-16 14:56:31 +01004075 /* Buffer record */
4076 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004077 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004078 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004079
4080 /* ssl_parse_record_header() only considers records
4081 * of the next epoch as candidates for buffering. */
4082 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004083 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004084
4085 hs->buffering.future_record.data =
4086 mbedtls_calloc( 1, hs->buffering.future_record.len );
4087 if( hs->buffering.future_record.data == NULL )
4088 {
4089 /* If we run out of RAM trying to buffer a
4090 * record from the next epoch, just ignore. */
4091 return( 0 );
4092 }
4093
Hanno Becker519f15d2019-07-11 12:43:20 +01004094 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004095
Hanno Becker519f15d2019-07-11 12:43:20 +01004096 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004097 return( 0 );
4098}
4099
4100#endif /* MBEDTLS_SSL_PROTO_DTLS */
4101
Hanno Beckere74d5562018-08-15 14:26:08 +01004102static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004103{
Janos Follath865b3eb2019-12-16 11:46:15 +00004104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004105 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004106
Hanno Becker5f066e72018-08-16 14:56:31 +01004107#if defined(MBEDTLS_SSL_PROTO_DTLS)
4108 /* We might have buffered a future record; if so,
4109 * and if the epoch matches now, load it.
4110 * On success, this call will set ssl->in_left to
4111 * the length of the buffered record, so that
4112 * the calls to ssl_fetch_input() below will
4113 * essentially be no-ops. */
4114 ret = ssl_load_buffered_record( ssl );
4115 if( ret != 0 )
4116 return( ret );
4117#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004118
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004119 /* Ensure that we have enough space available for the default form
4120 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4121 * with no space for CIDs counted in). */
4122 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4123 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004126 return( ret );
4127 }
4128
Hanno Beckere5e7e782019-07-11 12:29:35 +01004129 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4130 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004133 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004134 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004135 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4136 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004137 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004138 if( ret != 0 )
4139 return( ret );
4140
4141 /* Fall through to handling of unexpected records */
4142 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4143 }
4144
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004145 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4146 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004147#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004148 /* Reset in pointers to default state for TLS/DTLS records,
4149 * assuming no CID and no offset between record content and
4150 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004151 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004152
Hanno Becker7ae20e02019-07-12 08:33:49 +01004153 /* Setup internal message pointers from record structure. */
4154 ssl->in_msgtype = rec.type;
4155#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4156 ssl->in_len = ssl->in_cid + rec.cid_len;
4157#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4158 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4159 ssl->in_msglen = rec.data_len;
4160
Hanno Becker2fddd372019-07-10 14:37:41 +01004161 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004162 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004163 if( ret != 0 )
4164 return( ret );
4165#endif
4166
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004167 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004168 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004169
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4171 "(header)" ) );
4172 }
4173 else
4174 {
4175 /* Skip invalid record and the rest of the datagram */
4176 ssl->next_record_offset = 0;
4177 ssl->in_left = 0;
4178
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4180 "(header)" ) );
4181 }
4182
4183 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004184 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004185 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004186 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004187#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004188 {
4189 return( ret );
4190 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004191 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004194 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004195 {
Hanno Beckera8814792019-07-10 15:01:45 +01004196 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004197 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004198 if( ssl->next_record_offset < ssl->in_left )
4199 {
4200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4201 }
4202 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004203 else
4204#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004205 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004206 /*
4207 * Fetch record contents from underlying transport.
4208 */
Hanno Beckera3175662019-07-11 12:50:29 +01004209 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004210 if( ret != 0 )
4211 {
4212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4213 return( ret );
4214 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004215
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004216 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004217 }
4218
4219 /*
4220 * Decrypt record contents.
4221 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004222
Hanno Beckerfdf66042019-07-11 13:07:45 +01004223 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004226 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004227 {
4228 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004229 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004230 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004231 /* Except when waiting for Finished as a bad mac here
4232 * probably means something went wrong in the handshake
4233 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4234 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4235 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4236 {
4237#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4238 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4239 {
4240 mbedtls_ssl_send_alert_message( ssl,
4241 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4242 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4243 }
4244#endif
4245 return( ret );
4246 }
4247
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004248 if( ssl->conf->badmac_limit != 0 &&
4249 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4252 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004253 }
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004254
Hanno Becker4a810fb2017-05-24 16:27:30 +01004255 /* As above, invalid records cause
4256 * dismissal of the whole datagram. */
4257
4258 ssl->next_record_offset = 0;
4259 ssl->in_left = 0;
4260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004262 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004263 }
4264
4265 return( ret );
4266 }
4267 else
4268#endif
4269 {
4270 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4272 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004274 mbedtls_ssl_send_alert_message( ssl,
4275 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4276 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004277 }
4278#endif
4279 return( ret );
4280 }
4281 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004282
Hanno Becker44d89b22019-07-12 09:40:44 +01004283
4284 /* Reset in pointers to default state for TLS/DTLS records,
4285 * assuming no CID and no offset between record content and
4286 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004287 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004288#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4289 ssl->in_len = ssl->in_cid + rec.cid_len;
4290#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004291 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004292
Hanno Becker8685c822019-07-12 09:37:30 +01004293 /* The record content type may change during decryption,
4294 * so re-read it. */
4295 ssl->in_msgtype = rec.type;
4296 /* Also update the input buffer, because unfortunately
4297 * the server-side ssl_parse_client_hello() reparses the
4298 * record header when receiving a ClientHello initiating
4299 * a renegotiation. */
4300 ssl->in_hdr[0] = rec.type;
4301 ssl->in_msg = rec.buf + rec.data_offset;
4302 ssl->in_msglen = rec.data_len;
4303 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4304 ssl->in_len[1] = (unsigned char)( rec.data_len );
4305
Simon Butcher99000142016-10-13 17:21:01 +01004306 return( 0 );
4307}
4308
4309int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4310{
Janos Follath865b3eb2019-12-16 11:46:15 +00004311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004312
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004313 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004314 * Handle particular types of records
4315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 {
Simon Butcher99000142016-10-13 17:21:01 +01004318 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4319 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004320 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004321 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 }
4323
Hanno Beckere678eaa2018-08-21 14:57:46 +01004324 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004325 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004326 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004327 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004329 ssl->in_msglen ) );
4330 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004331 }
4332
Hanno Beckere678eaa2018-08-21 14:57:46 +01004333 if( ssl->in_msg[0] != 1 )
4334 {
4335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4336 ssl->in_msg[0] ) );
4337 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4338 }
4339
4340#if defined(MBEDTLS_SSL_PROTO_DTLS)
4341 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4342 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4343 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4344 {
4345 if( ssl->handshake == NULL )
4346 {
4347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4348 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4349 }
4350
4351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4352 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4353 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004354#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004355 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004359 if( ssl->in_msglen != 2 )
4360 {
4361 /* Note: Standard allows for more than one 2 byte alert
4362 to be packed in a single message, but Mbed TLS doesn't
4363 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004364 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004365 ssl->in_msglen ) );
4366 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4367 }
4368
Paul Elliott9f352112020-12-09 14:55:45 +00004369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 ssl->in_msg[0], ssl->in_msg[1] ) );
4371
4372 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004373 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004375 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004377 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004378 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004379 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004380 }
4381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4383 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4386 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004388
4389#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4390 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4391 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4392 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004394 /* Will be handled when trying to parse ServerHello */
4395 return( 0 );
4396 }
4397#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004398 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004399 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004400 }
4401
Hanno Beckerc76c6192017-06-06 10:03:17 +01004402#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004403 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004404 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004405 /* Drop unexpected ApplicationData records,
4406 * except at the beginning of renegotiations */
4407 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4408 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4409#if defined(MBEDTLS_SSL_RENEGOTIATION)
4410 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4411 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004412#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004413 )
4414 {
4415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4416 return( MBEDTLS_ERR_SSL_NON_FATAL );
4417 }
4418
4419 if( ssl->handshake != NULL &&
4420 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4421 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004422 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004423 }
4424 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004425#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004426
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 return( 0 );
4428}
4429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004430int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004431{
irwir6c0da642019-09-26 21:07:41 +03004432 return( mbedtls_ssl_send_alert_message( ssl,
4433 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4434 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004435}
4436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004438 unsigned char level,
4439 unsigned char message )
4440{
Janos Follath865b3eb2019-12-16 11:46:15 +00004441 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004442
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004443 if( ssl == NULL || ssl->conf == NULL )
4444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004447 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004450 ssl->out_msglen = 2;
4451 ssl->out_msg[0] = level;
4452 ssl->out_msg[1] = message;
4453
Hanno Becker67bc7c32018-08-06 11:33:50 +01004454 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004457 return( ret );
4458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004460
4461 return( 0 );
4462}
4463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004464int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004465{
Janos Follath865b3eb2019-12-16 11:46:15 +00004466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004470 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004471 ssl->out_msglen = 1;
4472 ssl->out_msg[0] = 1;
4473
Paul Bakker5121ce52009-01-03 21:22:43 +00004474 ssl->state++;
4475
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004476 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004477 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004479 return( ret );
4480 }
4481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004483
4484 return( 0 );
4485}
4486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004488{
Janos Follath865b3eb2019-12-16 11:46:15 +00004489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004492
Hanno Becker327c93b2018-08-15 13:56:18 +01004493 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004495 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 return( ret );
4497 }
4498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004502 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4503 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 }
4506
Hanno Beckere678eaa2018-08-21 14:57:46 +01004507 /* CCS records are only accepted if they have length 1 and content '1',
4508 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004509
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004510 /*
4511 * Switch to our negotiated transform and session parameters for inbound
4512 * data.
4513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004515 ssl->transform_in = ssl->transform_negotiate;
4516 ssl->session_in = ssl->session_negotiate;
4517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004518#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004519 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004522 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004523#endif
4524
4525 /* Increment epoch */
4526 if( ++ssl->in_epoch == 0 )
4527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004529 /* This is highly unlikely to happen for legitimate reasons, so
4530 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004531 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004532 }
4533 }
4534 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004536 memset( ssl->in_ctr, 0, 8 );
4537
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004538 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004539
Paul Bakker5121ce52009-01-03 21:22:43 +00004540 ssl->state++;
4541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004543
4544 return( 0 );
4545}
4546
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004547/* Once ssl->out_hdr as the address of the beginning of the
4548 * next outgoing record is set, deduce the other pointers.
4549 *
4550 * Note: For TLS, we save the implicit record sequence number
4551 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4552 * and the caller has to make sure there's space for this.
4553 */
4554
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004555static size_t ssl_transform_get_explicit_iv_len(
4556 mbedtls_ssl_transform const *transform )
4557{
TRodziewiczef73f012021-05-13 14:53:36 +02004558 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004559 return( 0 );
4560
4561 return( transform->ivlen - transform->fixed_ivlen );
4562}
4563
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004564void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4565 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004566{
4567#if defined(MBEDTLS_SSL_PROTO_DTLS)
4568 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4569 {
4570 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004571#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004572 ssl->out_cid = ssl->out_ctr + 8;
4573 ssl->out_len = ssl->out_cid;
4574 if( transform != NULL )
4575 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004576#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004577 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004578#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004579 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004580 }
4581 else
4582#endif
4583 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004584 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004585#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004586 ssl->out_cid = ssl->out_len;
4587#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004588 ssl->out_iv = ssl->out_hdr + 5;
4589 }
4590
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004591 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004592 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004593 if( transform != NULL )
4594 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004595}
4596
4597/* Once ssl->in_hdr as the address of the beginning of the
4598 * next incoming record is set, deduce the other pointers.
4599 *
4600 * Note: For TLS, we save the implicit record sequence number
4601 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4602 * and the caller has to make sure there's space for this.
4603 */
4604
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004605void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004606{
Hanno Becker79594fd2019-05-08 09:38:41 +01004607 /* This function sets the pointers to match the case
4608 * of unprotected TLS/DTLS records, with both ssl->in_iv
4609 * and ssl->in_msg pointing to the beginning of the record
4610 * content.
4611 *
4612 * When decrypting a protected record, ssl->in_msg
4613 * will be shifted to point to the beginning of the
4614 * record plaintext.
4615 */
4616
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004617#if defined(MBEDTLS_SSL_PROTO_DTLS)
4618 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4619 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004620 /* This sets the header pointers to match records
4621 * without CID. When we receive a record containing
4622 * a CID, the fields are shifted accordingly in
4623 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004624 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004625#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004626 ssl->in_cid = ssl->in_ctr + 8;
4627 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004628#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004629 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004630#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004631 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004632 }
4633 else
4634#endif
4635 {
4636 ssl->in_ctr = ssl->in_hdr - 8;
4637 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004639 ssl->in_cid = ssl->in_len;
4640#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004641 ssl->in_iv = ssl->in_hdr + 5;
4642 }
4643
Hanno Becker79594fd2019-05-08 09:38:41 +01004644 /* This will be adjusted at record decryption time. */
4645 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004646}
4647
Paul Bakker5121ce52009-01-03 21:22:43 +00004648/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004649 * Setup an SSL context
4650 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004651
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004652void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004653{
4654 /* Set the incoming and outgoing record pointers. */
4655#if defined(MBEDTLS_SSL_PROTO_DTLS)
4656 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4657 {
4658 ssl->out_hdr = ssl->out_buf;
4659 ssl->in_hdr = ssl->in_buf;
4660 }
4661 else
4662#endif /* MBEDTLS_SSL_PROTO_DTLS */
4663 {
Hanno Becker12078f42021-03-02 15:28:41 +00004664 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004665 ssl->out_hdr = ssl->out_buf + 8;
4666 ssl->in_hdr = ssl->in_buf + 8;
4667 }
4668
4669 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004670 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4671 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004672}
4673
Paul Bakker5121ce52009-01-03 21:22:43 +00004674/*
4675 * SSL get accessors
4676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004677size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004678{
4679 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4680}
4681
Hanno Becker8b170a02017-10-10 11:51:19 +01004682int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4683{
4684 /*
4685 * Case A: We're currently holding back
4686 * a message for further processing.
4687 */
4688
4689 if( ssl->keep_current_message == 1 )
4690 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004692 return( 1 );
4693 }
4694
4695 /*
4696 * Case B: Further records are pending in the current datagram.
4697 */
4698
4699#if defined(MBEDTLS_SSL_PROTO_DTLS)
4700 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4701 ssl->in_left > ssl->next_record_offset )
4702 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004703 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004704 return( 1 );
4705 }
4706#endif /* MBEDTLS_SSL_PROTO_DTLS */
4707
4708 /*
4709 * Case C: A handshake message is being processed.
4710 */
4711
Hanno Becker8b170a02017-10-10 11:51:19 +01004712 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
4713 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004715 return( 1 );
4716 }
4717
4718 /*
4719 * Case D: An application data message is being processed
4720 */
4721 if( ssl->in_offt != NULL )
4722 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004724 return( 1 );
4725 }
4726
4727 /*
4728 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01004729 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01004730 * we implement support for multiple alerts in single records.
4731 */
4732
4733 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
4734 return( 0 );
4735}
4736
Paul Bakker43ca69c2011-01-15 17:35:19 +00004737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004738int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004739{
Hanno Becker3136ede2018-08-17 15:28:19 +01004740 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004741 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004742 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004743
Hanno Becker5903de42019-05-03 14:46:38 +01004744 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
4745
Hanno Becker78640902018-08-13 16:35:15 +01004746 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01004747 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01004748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004751 case MBEDTLS_MODE_GCM:
4752 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004753 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004755 transform_expansion = transform->minlen;
4756 break;
4757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004759
4760 block_size = mbedtls_cipher_get_block_size(
4761 &transform->cipher_ctx_enc );
4762
Hanno Becker3136ede2018-08-17 15:28:19 +01004763 /* Expansion due to the addition of the MAC. */
4764 transform_expansion += transform->maclen;
4765
4766 /* Expansion due to the addition of CBC padding;
4767 * Theoretically up to 256 bytes, but we never use
4768 * more than the block size of the underlying cipher. */
4769 transform_expansion += block_size;
4770
TRodziewicz4ca18aa2021-05-20 14:46:20 +02004771 /* For TLS 1.2 or higher, an explicit IV is added
Hanno Becker3136ede2018-08-17 15:28:19 +01004772 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02004773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02004774 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02004775#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01004776
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004777 break;
4778
4779 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004782 }
4783
Hanno Beckera0e20d02019-05-15 14:03:01 +01004784#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01004785 if( transform->out_cid_len != 0 )
4786 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004787#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01004788
Hanno Becker5903de42019-05-03 14:46:38 +01004789 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004790}
4791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004793/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004794 * Check record counters and renegotiate if they're above the limit.
4795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004796static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004797{
Hanno Beckerdd772292020-02-05 10:38:31 +00004798 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00004799 int in_ctr_cmp;
4800 int out_ctr_cmp;
4801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004802 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
4803 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004804 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004805 {
4806 return( 0 );
4807 }
4808
Andres AG2196c7f2016-12-15 17:01:16 +00004809 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
4810 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01004811 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00004812 ssl->conf->renego_period + ep_len, 8 - ep_len );
4813
4814 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004815 {
4816 return( 0 );
4817 }
4818
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004820 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004821}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00004823
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004824/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01004825 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004826 * may only be sent for the purpose of initiating renegotiations.
4827 *
4828 * This function is introduced as a separate helper since the handling
4829 * of post-handshake handshake messages changes significantly in TLS 1.3,
4830 * and having a helper function allows to distinguish between TLS <= 1.2 and
4831 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
4832 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00004833static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004834{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01004835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004836
4837 /*
4838 * - For client-side, expect SERVER_HELLO_REQUEST.
4839 * - For server-side, expect CLIENT_HELLO.
4840 * - Fail (TLS) or silently drop record (DTLS) in other cases.
4841 */
4842
4843#if defined(MBEDTLS_SSL_CLI_C)
4844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
4845 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
4846 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
4847 {
4848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4849
4850 /* With DTLS, drop the packet (probably from last handshake) */
4851#if defined(MBEDTLS_SSL_PROTO_DTLS)
4852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4853 {
4854 return( 0 );
4855 }
4856#endif
4857 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4858 }
4859#endif /* MBEDTLS_SSL_CLI_C */
4860
4861#if defined(MBEDTLS_SSL_SRV_C)
4862 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4863 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
4864 {
4865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
4866
4867 /* With DTLS, drop the packet (probably from last handshake) */
4868#if defined(MBEDTLS_SSL_PROTO_DTLS)
4869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4870 {
4871 return( 0 );
4872 }
4873#endif
4874 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
4875 }
4876#endif /* MBEDTLS_SSL_SRV_C */
4877
4878#if defined(MBEDTLS_SSL_RENEGOTIATION)
4879 /* Determine whether renegotiation attempt should be accepted */
4880 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
4881 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
4882 ssl->conf->allow_legacy_renegotiation ==
4883 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
4884 {
4885 /*
4886 * Accept renegotiation request
4887 */
4888
4889 /* DTLS clients need to know renego is server-initiated */
4890#if defined(MBEDTLS_SSL_PROTO_DTLS)
4891 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4892 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
4893 {
4894 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
4895 }
4896#endif
4897 ret = mbedtls_ssl_start_renegotiation( ssl );
4898 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4899 ret != 0 )
4900 {
4901 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
4902 ret );
4903 return( ret );
4904 }
4905 }
4906 else
4907#endif /* MBEDTLS_SSL_RENEGOTIATION */
4908 {
4909 /*
4910 * Refuse renegotiation
4911 */
4912
4913 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
4914
TRodziewicz0f82ec62021-05-12 17:49:18 +02004915#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
TRodziewicz345165c2021-07-06 13:42:11 +02004916 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
4917 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
4918 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004919 {
TRodziewicz345165c2021-07-06 13:42:11 +02004920 return( ret );
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004921 }
TRodziewicz0f82ec62021-05-12 17:49:18 +02004922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerb03f88f2020-11-24 06:41:37 +00004923 }
4924
4925 return( 0 );
4926}
4927
Paul Bakker48916f92012-09-16 19:57:18 +00004928/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 * Receive application data decrypted from the SSL layer
4930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004931int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004932{
Janos Follath865b3eb2019-12-16 11:46:15 +00004933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00004934 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004935
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004936 if( ssl == NULL || ssl->conf == NULL )
4937 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004942 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004944 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004945 return( ret );
4946
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02004947 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004948 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02004949 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004950 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02004951 return( ret );
4952 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004953 }
4954#endif
4955
Hanno Becker4a810fb2017-05-24 16:27:30 +01004956 /*
4957 * Check if renegotiation is necessary and/or handshake is
4958 * in process. If yes, perform/continue, and fall through
4959 * if an unexpected packet is received while the client
4960 * is waiting for the ServerHello.
4961 *
4962 * (There is no equivalent to the last condition on
4963 * the server-side as it is not treated as within
4964 * a handshake while waiting for the ClientHello
4965 * after a renegotiation request.)
4966 */
4967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004968#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01004969 ret = ssl_check_ctr_renegotiate( ssl );
4970 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4971 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004973 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004974 return( ret );
4975 }
4976#endif
4977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01004981 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
4982 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004984 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004985 return( ret );
4986 }
4987 }
4988
Hanno Beckere41158b2017-10-23 13:30:32 +01004989 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01004990 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004991 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004992 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02004993 if( ssl->f_get_timer != NULL &&
4994 ssl->f_get_timer( ssl->p_timer ) == -1 )
4995 {
Hanno Becker0f57a652020-02-05 10:37:26 +00004996 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02004997 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004998
Hanno Becker327c93b2018-08-15 13:56:18 +01004999 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005000 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005001 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5002 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005003
Hanno Becker4a810fb2017-05-24 16:27:30 +01005004 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5005 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005006 }
5007
5008 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005009 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005010 {
5011 /*
5012 * OpenSSL sends empty messages to randomize the IV
5013 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005014 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005016 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005017 return( 0 );
5018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 return( ret );
5021 }
5022 }
5023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005024 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005025 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005026 ret = ssl_handle_hs_message_post_handshake( ssl );
5027 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005028 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005029 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5030 ret );
5031 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005032 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005033
Hanno Beckerf26cc722021-04-21 07:30:13 +01005034 /* At this point, we don't know whether the renegotiation triggered
5035 * by the post-handshake message has been completed or not. The cases
5036 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005037 * 1) The renegotiation is complete. In this case, no new record
5038 * has been read yet.
5039 * 2) The renegotiation is incomplete because the client received
5040 * an application data record while awaiting the ServerHello.
5041 * 3) The renegotiation is incomplete because the client received
5042 * a non-handshake, non-application data message while awaiting
5043 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005044 *
5045 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005046 * - For 1), the next iteration will read a new record and check
5047 * if it's application data.
5048 * - For 2), the loop condition isn't satisfied as application data
5049 * is present, hence continue is the same as break
5050 * - For 3), the loop condition is satisfied and read_record
5051 * will re-deliver the message that was held back by the client
5052 * when expecting the ServerHello.
5053 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005054
Hanno Becker90333da2017-10-10 11:27:13 +01005055 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005056 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005057#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005059 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005060 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005061 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005062 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005065 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005067 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005068 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005069 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5073 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005076 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005077 }
5078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5082 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005083 }
5084
5085 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005086
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005087 /* We're going to return something now, cancel timer,
5088 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005090 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005091
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005093 /* If we requested renego but received AppData, resend HelloRequest.
5094 * Do it now, after setting in_offt, to avoid taking this branch
5095 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005097 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005099 {
Hanno Becker786300f2020-02-05 10:46:40 +00005100 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005101 {
Hanno Becker786300f2020-02-05 10:46:40 +00005102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5103 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005104 return( ret );
5105 }
5106 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005108#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005109 }
5110
5111 n = ( len < ssl->in_msglen )
5112 ? len : ssl->in_msglen;
5113
5114 memcpy( buf, ssl->in_offt, n );
5115 ssl->in_msglen -= n;
5116
gabor-mezei-arma3214132020-07-15 10:55:00 +02005117 /* Zeroising the plaintext buffer to erase unused application data
5118 from the memory. */
5119 mbedtls_platform_zeroize( ssl->in_offt, n );
5120
Paul Bakker5121ce52009-01-03 21:22:43 +00005121 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005122 {
5123 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005124 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005125 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005126 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005127 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005128 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005129 /* more data available */
5130 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005131 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005134
Paul Bakker23986e52011-04-24 08:57:21 +00005135 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005136}
5137
5138/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005139 * Send application data to be encrypted by the SSL layer, taking care of max
5140 * fragment length and buffer size.
5141 *
5142 * According to RFC 5246 Section 6.2.1:
5143 *
5144 * Zero-length fragments of Application data MAY be sent as they are
5145 * potentially useful as a traffic analysis countermeasure.
5146 *
5147 * Therefore, it is possible that the input message length is 0 and the
5148 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005149 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005150static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005151 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005152{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005153 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5154 const size_t max_len = (size_t) ret;
5155
5156 if( ret < 0 )
5157 {
5158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5159 return( ret );
5160 }
5161
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005162 if( len > max_len )
5163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005168 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5169 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005170 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005172 }
5173 else
5174#endif
5175 len = max_len;
5176 }
Paul Bakker887bd502011-06-08 13:10:54 +00005177
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 if( ssl->out_left != 0 )
5179 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005180 /*
5181 * The user has previously tried to send the data and
5182 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5183 * written. In this case, we expect the high-level write function
5184 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005186 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005189 return( ret );
5190 }
5191 }
Paul Bakker887bd502011-06-08 13:10:54 +00005192 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005193 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005194 /*
5195 * The user is trying to send a message the first time, so we need to
5196 * copy the data into the internal buffers and setup the data structure
5197 * to keep track of partial writes
5198 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005199 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005200 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005201 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005202
Hanno Becker67bc7c32018-08-06 11:33:50 +01005203 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005206 return( ret );
5207 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005208 }
5209
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005210 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005211}
5212
5213/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005214 * Write application data (public-facing wrapper)
5215 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005216int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005217{
Janos Follath865b3eb2019-12-16 11:46:15 +00005218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005219
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005221
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005222 if( ssl == NULL || ssl->conf == NULL )
5223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5224
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005225#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005226 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5227 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005228 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005229 return( ret );
5230 }
5231#endif
5232
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005233 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005234 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005235 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005236 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005238 return( ret );
5239 }
5240 }
5241
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005242 ret = ssl_write_real( ssl, buf, len );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005243
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005245
5246 return( ret );
5247}
5248
5249/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005250 * Notify the peer that the connection is being closed
5251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005253{
Janos Follath865b3eb2019-12-16 11:46:15 +00005254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005255
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005256 if( ssl == NULL || ssl->conf == NULL )
5257 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005260
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005261 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005262 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005266 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5267 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5268 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005270 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005271 return( ret );
5272 }
5273 }
5274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005276
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005277 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278}
5279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005281{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005282 if( transform == NULL )
5283 return;
5284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5286 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005287
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005288#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289 mbedtls_md_free( &transform->md_ctx_enc );
5290 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005291#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005292
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005293 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005294}
5295
Hanno Becker0271f962018-08-16 13:23:47 +01005296#if defined(MBEDTLS_SSL_PROTO_DTLS)
5297
Hanno Becker533ab5f2020-02-05 10:49:13 +00005298void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005299{
5300 unsigned offset;
5301 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5302
5303 if( hs == NULL )
5304 return;
5305
Hanno Becker283f5ef2018-08-24 09:34:47 +01005306 ssl_free_buffered_record( ssl );
5307
Hanno Becker0271f962018-08-16 13:23:47 +01005308 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005309 ssl_buffering_free_slot( ssl, offset );
5310}
5311
5312static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5313 uint8_t slot )
5314{
5315 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5316 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005317
5318 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5319 return;
5320
Hanno Beckere605b192018-08-21 15:59:07 +01005321 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005322 {
Hanno Beckere605b192018-08-21 15:59:07 +01005323 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005324 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005325 mbedtls_free( hs_buf->data );
5326 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005327 }
5328}
5329
5330#endif /* MBEDTLS_SSL_PROTO_DTLS */
5331
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005332/*
5333 * Convert version numbers to/from wire format
5334 * and, for DTLS, to/from TLS equivalent.
5335 *
5336 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005337 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005338 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005341 unsigned char ver[2] )
5342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343#if defined(MBEDTLS_SSL_PROTO_DTLS)
5344 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005346 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005347 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5348
5349 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5350 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5351 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005352 else
5353#else
5354 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005355#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005356 {
5357 ver[0] = (unsigned char) major;
5358 ver[1] = (unsigned char) minor;
5359 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005360}
5361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005363 const unsigned char ver[2] )
5364{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365#if defined(MBEDTLS_SSL_PROTO_DTLS)
5366 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005367 {
5368 *major = 255 - ver[0] + 2;
5369 *minor = 255 - ver[1] + 1;
5370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005372 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5373 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005374 else
5375#else
5376 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005377#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005378 {
5379 *major = ver[0];
5380 *minor = ver[1];
5381 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005382}
5383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005384#endif /* MBEDTLS_SSL_TLS_C */