blob: 55623ccb30470390a04d1cca45d73d61e83f0f2f [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"
Paul Bakker0be444a2013-08-27 21:55:01 +020043
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020044#include "ssl_invasive.h"
45
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050048#if defined(MBEDTLS_USE_PSA_CRYPTO)
49#include "mbedtls/psa_util.h"
50#include "psa/crypto.h"
51#endif
52
Janos Follath23bdca02016-10-07 14:47:14 +010053#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020055#endif
56
Hanno Beckercd9dcda2018-08-28 17:18:56 +010057static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010058
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020059/*
60 * Start a timer.
61 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020062 */
Hanno Becker0f57a652020-02-05 10:37:26 +000063void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020064{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020065 if( ssl->f_set_timer == NULL )
66 return;
67
68 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
69 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020070}
71
72/*
73 * Return -1 is timer is expired, 0 if it isn't.
74 */
Hanno Becker7876d122020-02-05 10:39:31 +000075int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020076{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020077 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020078 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020079
80 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020081 {
82 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020084 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020085
86 return( 0 );
87}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088
Hanno Beckercfe45792019-07-03 16:13:00 +010089#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010090static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
91 unsigned char *buf,
92 size_t len,
93 mbedtls_record *rec );
94
Hanno Beckercfe45792019-07-03 16:13:00 +010095int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
96 unsigned char *buf,
97 size_t buflen )
98{
Hanno Becker54229812019-07-12 14:40:00 +010099 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100100 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
104 * (a) there doesn't seem to be a usecase for it, and
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100105 * (b) In TLS 1.0, CBC record decryption has state
Hanno Becker54229812019-07-12 14:40:00 +0100106 * and we'd need to backup the transform here.
107 */
108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
109 {
110 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
111 goto exit;
112 }
113#if defined(MBEDTLS_SSL_PROTO_DTLS)
114 else
115 {
irwir734f0cf2019-09-26 21:03:24 +0300116 mbedtls_record rec;
117
Hanno Becker54229812019-07-12 14:40:00 +0100118 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
119 if( ret != 0 )
120 {
121 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
122 goto exit;
123 }
124
125 if( ssl->transform_in != NULL )
126 {
127 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
128 if( ret != 0 )
129 {
130 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
131 goto exit;
132 }
133 }
134 }
135#endif /* MBEDTLS_SSL_PROTO_DTLS */
136
137exit:
138 /* On success, we have decrypted the buffer in-place, so make
139 * sure we don't leak any plaintext data. */
140 mbedtls_platform_zeroize( buf, buflen );
141
142 /* For the purpose of this API, treat messages with unexpected CID
143 * as well as such from future epochs as unexpected. */
144 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
145 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
146 {
147 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
148 }
149
150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
151 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100152}
153#endif /* MBEDTLS_SSL_RECORD_CHECKING */
154
Hanno Becker67bc7c32018-08-06 11:33:50 +0100155#define SSL_DONT_FORCE_FLUSH 0
156#define SSL_FORCE_FLUSH 1
157
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200158#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100159
Hanno Beckerd5847772018-08-28 10:09:23 +0100160/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100161static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
162 uint8_t slot );
163static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
164static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
165static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
166static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100167static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
168 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100169static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100170
Hanno Becker11682cc2018-08-22 14:41:02 +0100171static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100172{
Hanno Becker89490712020-02-05 10:50:12 +0000173 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000174#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
175 size_t out_buf_len = ssl->out_buf_len;
176#else
177 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
178#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100179
Darryl Greenb33cc762019-11-28 14:29:44 +0000180 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100181 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100182
Darryl Greenb33cc762019-11-28 14:29:44 +0000183 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100184}
185
Hanno Becker67bc7c32018-08-06 11:33:50 +0100186static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
187{
Hanno Becker11682cc2018-08-22 14:41:02 +0100188 size_t const bytes_written = ssl->out_left;
189 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100190
191 /* Double-check that the write-index hasn't gone
192 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100193 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194 {
195 /* Should never happen... */
196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
197 }
198
199 return( (int) ( mtu - bytes_written ) );
200}
201
202static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
203{
Janos Follath865b3eb2019-12-16 11:46:15 +0000204 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100205 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400206 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100207
208#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400209 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210
211 if( max_len > mfl )
212 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100213
214 /* By the standard (RFC 6066 Sect. 4), the MFL extension
215 * only limits the maximum record payload size, so in theory
216 * we would be allowed to pack multiple records of payload size
217 * MFL into a single datagram. However, this would mean that there's
218 * no way to explicitly communicate MTU restrictions to the peer.
219 *
220 * The following reduction of max_len makes sure that we never
221 * write datagrams larger than MFL + Record Expansion Overhead.
222 */
223 if( max_len <= ssl->out_left )
224 return( 0 );
225
226 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100227#endif
228
229 ret = ssl_get_remaining_space_in_datagram( ssl );
230 if( ret < 0 )
231 return( ret );
232 remaining = (size_t) ret;
233
234 ret = mbedtls_ssl_get_record_expansion( ssl );
235 if( ret < 0 )
236 return( ret );
237 expansion = (size_t) ret;
238
239 if( remaining <= expansion )
240 return( 0 );
241
242 remaining -= expansion;
243 if( remaining >= max_len )
244 remaining = max_len;
245
246 return( (int) remaining );
247}
248
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200249/*
250 * Double the retransmit timeout value, within the allowed range,
251 * returning -1 if the maximum value has already been reached.
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200254{
255 uint32_t new_timeout;
256
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200257 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258 return( -1 );
259
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200260 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
261 * in the following way: after the initial transmission and a first
262 * retransmission, back off to a temporary estimated MTU of 508 bytes.
263 * This value is guaranteed to be deliverable (if not guaranteed to be
264 * delivered) of any compliant IPv4 (and IPv6) network, and should work
265 * on most non-IP stacks too. */
266 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400267 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200268 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
270 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200271
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200272 new_timeout = 2 * ssl->handshake->retransmit_timeout;
273
274 /* Avoid arithmetic overflow and range overflow */
275 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200276 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200277 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200278 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200279 }
280
281 ssl->handshake->retransmit_timeout = new_timeout;
Paul Elliott9f352112020-12-09 14:55:45 +0000282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
283 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200284
285 return( 0 );
286}
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200289{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200290 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Paul Elliott9f352112020-12-09 14:55:45 +0000291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
292 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200295
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100296/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200298 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000299
Hanno Beckerccc13d02020-05-04 12:30:04 +0100300#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
301 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100302
303static size_t ssl_compute_padding_length( size_t len,
304 size_t granularity )
305{
306 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
307}
308
Hanno Becker581bc1b2020-05-04 12:20:03 +0100309/* This functions transforms a (D)TLS plaintext fragment and a record content
310 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
311 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
312 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100313 *
314 * struct {
315 * opaque content[DTLSPlaintext.length];
316 * ContentType real_type;
317 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100318 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100319 *
320 * Input:
321 * - `content`: The beginning of the buffer holding the
322 * plaintext to be wrapped.
323 * - `*content_size`: The length of the plaintext in Bytes.
324 * - `max_len`: The number of Bytes available starting from
325 * `content`. This must be `>= *content_size`.
326 * - `rec_type`: The desired record content type.
327 *
328 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100329 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
330 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100331 *
332 * Returns:
333 * - `0` on success.
334 * - A negative error code if `max_len` didn't offer enough space
335 * for the expansion.
336 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100337static int ssl_build_inner_plaintext( unsigned char *content,
338 size_t *content_size,
339 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100340 uint8_t rec_type,
341 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100342{
343 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100344
345 /* Write real content type */
346 if( remaining == 0 )
347 return( -1 );
348 content[ len ] = rec_type;
349 len++;
350 remaining--;
351
352 if( remaining < pad )
353 return( -1 );
354 memset( content + len, 0, pad );
355 len += pad;
356 remaining -= pad;
357
358 *content_size = len;
359 return( 0 );
360}
361
Hanno Becker581bc1b2020-05-04 12:20:03 +0100362/* This function parses a (D)TLSInnerPlaintext structure.
363 * See ssl_build_inner_plaintext() for details. */
364static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100365 size_t *content_size,
366 uint8_t *rec_type )
367{
368 size_t remaining = *content_size;
369
370 /* Determine length of padding by skipping zeroes from the back. */
371 do
372 {
373 if( remaining == 0 )
374 return( -1 );
375 remaining--;
376 } while( content[ remaining ] == 0 );
377
378 *content_size = remaining;
379 *rec_type = content[ remaining ];
380
381 return( 0 );
382}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100383#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
384 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100385
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100386/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100387 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000388static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100389 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100390 mbedtls_record *rec,
391 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000392{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100393 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100394 *
395 * additional_data = seq_num + TLSCompressed.type +
396 * TLSCompressed.version + TLSCompressed.length;
397 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100398 * For the CID extension, this is extended as follows
399 * (quoting draft-ietf-tls-dtls-connection-id-05,
400 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100401 *
402 * additional_data = seq_num + DTLSPlaintext.type +
403 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100404 * cid +
405 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100406 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100407 *
408 * For TLS 1.3, the record sequence number is dropped from the AAD
409 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100410 */
411
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100412 unsigned char *cur = add_data;
413
414#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
415 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
416#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
417 {
418 ((void) minor_ver);
419 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
420 cur += sizeof( rec->ctr );
421 }
422
423 *cur = rec->type;
424 cur++;
425
426 memcpy( cur, rec->ver, sizeof( rec->ver ) );
427 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100428
Hanno Beckera0e20d02019-05-15 14:03:01 +0100429#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100430 if( rec->cid_len != 0 )
431 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100432 memcpy( cur, rec->cid, rec->cid_len );
433 cur += rec->cid_len;
434
435 *cur = rec->cid_len;
436 cur++;
437
438 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
439 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
440 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100441 }
442 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100443#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100444 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100445 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
446 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
447 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100448 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100449
450 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000451}
452
Hanno Becker67a37db2020-05-28 16:27:07 +0100453#if defined(MBEDTLS_GCM_C) || \
454 defined(MBEDTLS_CCM_C) || \
455 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100456static int ssl_transform_aead_dynamic_iv_is_explicit(
457 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100458{
Hanno Becker17263802020-05-28 07:05:48 +0100459 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100460}
461
Hanno Becker17263802020-05-28 07:05:48 +0100462/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
463 *
464 * Concretely, this occurs in two variants:
465 *
466 * a) Fixed and dynamic IV lengths add up to total IV length, giving
467 * IV = fixed_iv || dynamic_iv
468 *
Hanno Becker15952812020-06-04 13:31:46 +0100469 * This variant is used in TLS 1.2 when used with GCM or CCM.
470 *
Hanno Becker17263802020-05-28 07:05:48 +0100471 * b) Fixed IV lengths matches total IV length, giving
472 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100473 *
474 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
475 *
476 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100477 *
478 * This function has the precondition that
479 *
480 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
481 *
482 * which has to be ensured by the caller. If this precondition
483 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100484 */
485static void ssl_build_record_nonce( unsigned char *dst_iv,
486 size_t dst_iv_len,
487 unsigned char const *fixed_iv,
488 size_t fixed_iv_len,
489 unsigned char const *dynamic_iv,
490 size_t dynamic_iv_len )
491{
492 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100493
494 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100495 memset( dst_iv, 0, dst_iv_len );
496 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100497
Hanno Becker17263802020-05-28 07:05:48 +0100498 dst_iv += dst_iv_len - dynamic_iv_len;
499 for( i = 0; i < dynamic_iv_len; i++ )
500 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100501}
Hanno Becker67a37db2020-05-28 16:27:07 +0100502#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100503
Hanno Beckera18d1322018-01-03 14:27:32 +0000504int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
505 mbedtls_ssl_transform *transform,
506 mbedtls_record *rec,
507 int (*f_rng)(void *, unsigned char *, size_t),
508 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000509{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100511 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000512 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100513 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100514 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000515 size_t post_avail;
516
517 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000518#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200519 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000520 ((void) ssl);
521#endif
522
523 /* The PRNG is used for dynamic IV generation that's used
524 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200525#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000526 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
527 ((void) f_rng);
528 ((void) p_rng);
529#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000533 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100534 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
537 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100538 if( rec == NULL
539 || rec->buf == NULL
540 || rec->buf_len < rec->data_offset
541 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100542#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100543 || rec->cid_len != 0
544#endif
545 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000546 {
547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100549 }
550
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000551 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100552 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000554 data, rec->data_len );
555
556 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
557
558 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
559 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
561 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000562 rec->data_len,
563 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000564 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
565 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100566
Hanno Becker92313402020-05-20 13:58:58 +0100567 /* The following two code paths implement the (D)TLSInnerPlaintext
568 * structure present in TLS 1.3 and DTLS 1.2 + CID.
569 *
570 * See ssl_build_inner_plaintext() for more information.
571 *
572 * Note that this changes `rec->data_len`, and hence
573 * `post_avail` needs to be recalculated afterwards.
574 *
575 * Note also that the two code paths cannot occur simultaneously
576 * since they apply to different versions of the protocol. There
577 * is hence no risk of double-addition of the inner plaintext.
578 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100579#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
580 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
581 {
Hanno Becker13996922020-05-28 16:15:19 +0100582 size_t padding =
583 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100584 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100585 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100586 &rec->data_len,
587 post_avail,
588 rec->type,
589 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100590 {
591 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
592 }
593
594 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
595 }
596#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
597
Hanno Beckera0e20d02019-05-15 14:03:01 +0100598#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100599 /*
600 * Add CID information
601 */
602 rec->cid_len = transform->out_cid_len;
603 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
604 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100605
606 if( rec->cid_len != 0 )
607 {
Hanno Becker13996922020-05-28 16:15:19 +0100608 size_t padding =
609 ssl_compute_padding_length( rec->data_len,
610 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100611 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100612 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100613 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100614 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100615 * Note that this changes `rec->data_len`, and hence
616 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100617 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100618 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100619 &rec->data_len,
620 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100621 rec->type,
622 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100623 {
624 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
625 }
626
627 rec->type = MBEDTLS_SSL_MSG_CID;
628 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100629#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100630
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100631 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
632
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100634 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 */
Hanno Becker52344c22018-01-03 15:24:20 +0000636#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( mode == MBEDTLS_MODE_STREAM ||
638 ( mode == MBEDTLS_MODE_CBC
639#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000640 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100641#endif
642 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000644 if( post_avail < transform->maclen )
645 {
646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
647 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
650 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000651 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200652 {
Hanno Becker992b6872017-11-09 18:57:39 +0000653 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
654
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100655 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
656 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000657
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000658 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100659 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000660 mbedtls_md_hmac_update( &transform->md_ctx_enc,
661 data, rec->data_len );
662 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
663 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
664
665 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200666 }
667 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
671 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200672 }
673
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000674 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
675 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200676
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000677 rec->data_len += transform->maclen;
678 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100679 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200680 }
Hanno Becker52344c22018-01-03 15:24:20 +0000681#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200683 /*
684 * Encrypt
685 */
Mateusz Starzyk5224e292021-02-22 14:36:29 +0100686#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000690 size_t olen;
Paul Elliottd48d5c62021-01-07 14:47:05 +0000691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000692 "including %d bytes of padding",
693 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000695 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
696 transform->iv_enc, transform->ivlen,
697 data, rec->data_len,
698 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200701 return( ret );
702 }
703
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000704 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200708 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 }
Paul Bakker68884e32013-01-07 18:20:04 +0100710 else
Mateusz Starzyk5224e292021-02-22 14:36:29 +0100711#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000712
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200713#if defined(MBEDTLS_GCM_C) || \
714 defined(MBEDTLS_CCM_C) || \
715 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200717 mode == MBEDTLS_MODE_CCM ||
718 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000719 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200721 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100722 unsigned char *dynamic_iv;
723 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100724 int dynamic_iv_is_explicit =
725 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000726
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100727 /* Check that there's space for the authentication tag. */
728 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000729 {
730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
731 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
732 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000733
Paul Bakker68884e32013-01-07 18:20:04 +0100734 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100735 * Build nonce for AEAD encryption.
736 *
737 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
738 * part of the IV is prepended to the ciphertext and
739 * can be chosen freely - in particular, it need not
740 * agree with the record sequence number.
741 * However, since ChaChaPoly as well as all AEAD modes
742 * in TLS 1.3 use the record sequence number as the
743 * dynamic part of the nonce, we uniformly use the
744 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100745 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100746 dynamic_iv = rec->ctr;
747 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200748
Hanno Becker17263802020-05-28 07:05:48 +0100749 ssl_build_record_nonce( iv, sizeof( iv ),
750 transform->iv_enc,
751 transform->fixed_ivlen,
752 dynamic_iv,
753 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100754
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100755 /*
756 * Build additional data for AEAD encryption.
757 * This depends on the TLS version.
758 */
759 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
760 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100761
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200762 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100763 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200764 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100765 dynamic_iv,
766 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000767 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100768 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000769 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200770 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000771 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000772
Paul Bakker68884e32013-01-07 18:20:04 +0100773 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200774 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200775 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000776
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100777 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000778 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100779 add_data, add_data_len,
780 data, rec->data_len, /* src */
781 data, rec->buf_len - (data - rec->buf), /* dst */
782 &rec->data_len,
783 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200784 {
TRodziewicz18efb732021-04-29 23:12:19 +0200785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200786 return( ret );
787 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000788 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100789 data + rec->data_len - transform->taglen,
790 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100791 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000792 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100793
794 /*
795 * Prefix record content with dynamic IV in case it is explicit.
796 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100797 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100798 {
799 if( rec->data_offset < dynamic_iv_len )
800 {
801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
802 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
803 }
804
805 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
806 rec->data_offset -= dynamic_iv_len;
807 rec->data_len += dynamic_iv_len;
808 }
809
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100810 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000811 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000812 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100813#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200814#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000818 size_t padlen, i;
819 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000820
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000821 /* Currently we're always using minimal padding
822 * (up to 255 bytes would be allowed). */
823 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
824 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 padlen = 0;
826
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000827 /* Check there's enough space in the buffer for the padding. */
828 if( post_avail < padlen + 1 )
829 {
830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
831 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
832 }
833
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000835 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000837 rec->data_len += padlen + 1;
838 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000841 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000842 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
843 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000844 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000845 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000846 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000847 if( f_rng == NULL )
848 {
849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
851 }
852
853 if( rec->data_offset < transform->ivlen )
854 {
855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
856 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
857 }
858
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000859 /*
860 * Generate IV
861 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000862 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000863 if( ret != 0 )
864 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000865
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000866 memcpy( data - transform->ivlen, transform->iv_enc,
867 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000868
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000869 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000871
Paul Elliottd48d5c62021-01-07 14:47:05 +0000872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
873 "including %" MBEDTLS_PRINTF_SIZET
874 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000875 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200876 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000877
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000878 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
879 transform->iv_enc,
880 transform->ivlen,
881 data, rec->data_len,
882 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200885 return( ret );
886 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200887
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000888 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
891 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200892 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200893
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100894#if defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000895 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200896 {
897 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +0100898 * Save IV in TLS1
Paul Bakkercca5b812013-08-31 17:40:26 +0200899 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000900 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
901 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000903 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200904#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000905 {
906 data -= transform->ivlen;
907 rec->data_offset -= transform->ivlen;
908 rec->data_len += transform->ivlen;
909 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100912 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100913 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000914 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
915
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100916 /*
917 * MAC(MAC_write_key, seq_num +
918 * TLSCipherText.type +
919 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100920 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100921 * IV + // except for TLS 1.0
922 * ENC(content + padding + padding_length));
923 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000924
925 if( post_avail < transform->maclen)
926 {
927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
928 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
929 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100930
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100931 ssl_extract_add_data_from_record( add_data, &add_data_len,
932 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000935 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100936 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100937
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000938 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100939 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000940 mbedtls_md_hmac_update( &transform->md_ctx_enc,
941 data, rec->data_len );
942 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
943 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100944
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000945 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100946
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000947 rec->data_len += transform->maclen;
948 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100949 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100950 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200953 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200954#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200958 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000959
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100960 /* Make extra sure authentication was performed, exactly once */
961 if( auth_done != 1 )
962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
964 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100965 }
966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
969 return( 0 );
970}
971
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +0200972#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +0200973/*
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200974 * Turn a bit into a mask:
975 * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
976 * - if bit == 0, return the all-bits 0 mask, aka 0
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200977 *
978 * This function can be used to write constant-time code by replacing branches
979 * with bit operations using masks.
980 *
981 * This function is implemented without using comparison operators, as those
982 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200983 */
984static size_t mbedtls_ssl_cf_mask_from_bit( size_t bit )
985{
986 /* MSVC has a warning about unary minus on unsigned integer types,
987 * but this is well-defined and precisely what we want to do here. */
988#if defined(_MSC_VER)
989#pragma warning( push )
990#pragma warning( disable : 4146 )
991#endif
992 return -bit;
993#if defined(_MSC_VER)
994#pragma warning( pop )
995#endif
996}
997
998/*
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200999 * Constant-flow mask generation for "less than" comparison:
1000 * - if x < y, return all bits 1, that is (size_t) -1
1001 * - otherwise, return all bits 0, that is 0
1002 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001003 * This function can be used to write constant-time code by replacing branches
1004 * with bit operations using masks.
1005 *
1006 * This function is implemented without using comparison operators, as those
1007 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001008 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001009static size_t mbedtls_ssl_cf_mask_lt( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001010{
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001011 /* This has the most significant bit set if and only if x < y */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001012 const size_t sub = x - y;
1013
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001014 /* sub1 = (x < y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001015 const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 );
1016
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001017 /* mask = (x < y) ? 0xff... : 0x00... */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001018 const size_t mask = mbedtls_ssl_cf_mask_from_bit( sub1 );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001019
1020 return( mask );
1021}
1022
1023/*
1024 * Constant-flow mask generation for "greater or equal" comparison:
1025 * - if x >= y, return all bits 1, that is (size_t) -1
1026 * - otherwise, return all bits 0, that is 0
1027 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001028 * This function can be used to write constant-time code by replacing branches
1029 * with bit operations using masks.
1030 *
1031 * This function is implemented without using comparison operators, as those
1032 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001033 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001034static size_t mbedtls_ssl_cf_mask_ge( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001035{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001036 return( ~mbedtls_ssl_cf_mask_lt( x, y ) );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001037}
1038
1039/*
1040 * Constant-flow boolean "equal" comparison:
1041 * return x == y
1042 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001043 * This function can be used to write constant-time code by replacing branches
1044 * with bit operations - it can be used in conjunction with
1045 * mbedtls_ssl_cf_mask_from_bit().
1046 *
1047 * This function is implemented without using comparison operators, as those
1048 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001049 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001050static size_t mbedtls_ssl_cf_bool_eq( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001051{
1052 /* diff = 0 if x == y, non-zero otherwise */
1053 const size_t diff = x ^ y;
1054
1055 /* MSVC has a warning about unary minus on unsigned integer types,
1056 * but this is well-defined and precisely what we want to do here. */
1057#if defined(_MSC_VER)
1058#pragma warning( push )
1059#pragma warning( disable : 4146 )
1060#endif
1061
1062 /* diff_msb's most significant bit is equal to x != y */
1063 const size_t diff_msb = ( diff | -diff );
1064
1065#if defined(_MSC_VER)
1066#pragma warning( pop )
1067#endif
1068
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001069 /* diff1 = (x != y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001070 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1071
1072 return( 1 ^ diff1 );
1073}
1074
1075/*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001076 * Constant-flow conditional memcpy:
1077 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1078 * - otherwise, a no-op,
1079 * but with execution flow independent of the values of c1 and c2.
1080 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001081 * This function is implemented without using comparison operators, as those
1082 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001083 */
Manuel Pégourié-Gonnarde7478432020-07-24 11:09:22 +02001084static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1085 const unsigned char *src,
1086 size_t len,
1087 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001088{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001089 /* mask = c1 == c2 ? 0xff : 0x00 */
1090 const size_t equal = mbedtls_ssl_cf_bool_eq( c1, c2 );
Manuel Pégourié-Gonnard2a59fb42020-08-25 11:51:46 +02001091 const unsigned char mask = (unsigned char) mbedtls_ssl_cf_mask_from_bit( equal );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001092
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001093 /* dst[i] = c1 == c2 ? src[i] : dst[i] */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001094 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001095 dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001096}
1097
1098/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001099 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001100 *
1101 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1102 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001103 */
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02001104MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001105 mbedtls_md_context_t *ctx,
1106 const unsigned char *add_data, size_t add_data_len,
1107 const unsigned char *data, size_t data_len_secret,
1108 size_t min_data_len, size_t max_data_len,
1109 unsigned char *output )
1110{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001111 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001112 * This function breaks the HMAC abstraction and uses the md_clone()
1113 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001114 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001115 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001116 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001117 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001118 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001119 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1120 * minlen, then cloning the context, and for each byte up to maxlen
1121 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001122 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001123 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001124 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001125 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001126 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1127 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001128 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +02001129 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001130 const unsigned char * const okey = ikey + block_size;
1131 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001132
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001133 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1134 mbedtls_md_context_t aux;
1135 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +02001136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001137
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001138 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001139
1140#define MD_CHK( func_call ) \
1141 do { \
1142 ret = (func_call); \
1143 if( ret != 0 ) \
1144 goto cleanup; \
1145 } while( 0 )
1146
1147 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001148
1149 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1150 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001151 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1152 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001153
1154 /* For each possible length, compute the hash up to that point */
1155 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001156 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001157 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1158 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001159 /* Keep only the correct inner_hash in the output buffer */
1160 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1161 offset, data_len_secret );
1162
1163 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001164 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001165 }
1166
Manuel Pégourié-Gonnard5ca21db2021-05-17 12:28:08 +02001167 /* The context needs to finish() before it starts() again */
1168 MD_CHK( mbedtls_md_finish( ctx, aux_out ) );
1169
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001170 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001171 MD_CHK( mbedtls_md_starts( ctx ) );
1172 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1173 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1174 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001175
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001176 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001177 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001178
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001179#undef MD_CHK
1180
1181cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001182 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001183 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001184}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001185
1186/*
1187 * Constant-flow memcpy from variable position in buffer.
1188 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001189 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001190 */
1191MBEDTLS_STATIC_TESTABLE void mbedtls_ssl_cf_memcpy_offset(
1192 unsigned char *dst,
1193 const unsigned char *src_base,
1194 size_t offset_secret,
1195 size_t offset_min, size_t offset_max,
1196 size_t len )
1197{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001198 size_t offset;
1199
1200 for( offset = offset_min; offset <= offset_max; offset++ )
1201 {
1202 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1203 offset, offset_secret );
1204 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001205}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001206#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001207
Hanno Becker605949f2019-07-12 08:23:59 +01001208int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001209 mbedtls_ssl_transform *transform,
1210 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001212 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001214 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001215#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001216 size_t padlen = 0, correct = 1;
1217#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001218 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001219 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001220 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001221
Hanno Beckera18d1322018-01-03 14:27:32 +00001222#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001223 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001224 ((void) ssl);
1225#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001228 if( rec == NULL ||
1229 rec->buf == NULL ||
1230 rec->buf_len < rec->data_offset ||
1231 rec->buf_len - rec->data_offset < rec->data_len )
1232 {
1233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001235 }
1236
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001237 data = rec->buf + rec->data_offset;
1238 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Hanno Beckera0e20d02019-05-15 14:03:01 +01001240#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001241 /*
1242 * Match record's CID with incoming CID.
1243 */
Hanno Becker938489a2019-05-08 13:02:22 +01001244 if( rec->cid_len != transform->in_cid_len ||
1245 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1246 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001247 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001248 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001249#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001250
Mateusz Starzyk5224e292021-02-22 14:36:29 +01001251#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001253 {
1254 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001255 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1256 transform->iv_dec,
1257 transform->ivlen,
1258 data, rec->data_len,
1259 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001262 return( ret );
1263 }
1264
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001265 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1268 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001269 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001270 }
Paul Bakker68884e32013-01-07 18:20:04 +01001271 else
Mateusz Starzyk5224e292021-02-22 14:36:29 +01001272#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001273#if defined(MBEDTLS_GCM_C) || \
1274 defined(MBEDTLS_CCM_C) || \
1275 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001277 mode == MBEDTLS_MODE_CCM ||
1278 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001279 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001280 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001281 unsigned char *dynamic_iv;
1282 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001283
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001284 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001285 * Extract dynamic part of nonce for AEAD decryption.
1286 *
1287 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1288 * part of the IV is prepended to the ciphertext and
1289 * can be chosen freely - in particular, it need not
1290 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001291 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001292 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001293 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001294 {
1295 if( rec->data_len < dynamic_iv_len )
1296 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1298 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001299 rec->data_len,
1300 dynamic_iv_len ) );
1301 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1302 }
1303 dynamic_iv = data;
1304
1305 data += dynamic_iv_len;
1306 rec->data_offset += dynamic_iv_len;
1307 rec->data_len -= dynamic_iv_len;
1308 }
Hanno Becker17263802020-05-28 07:05:48 +01001309 else
1310 {
1311 dynamic_iv = rec->ctr;
1312 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001313
1314 /* Check that there's space for the authentication tag. */
1315 if( rec->data_len < transform->taglen )
1316 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1318 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001319 rec->data_len,
1320 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001322 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001323 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001324
Hanno Beckerdf8be222020-05-21 15:30:57 +01001325 /*
1326 * Prepare nonce from dynamic and static parts.
1327 */
Hanno Becker17263802020-05-28 07:05:48 +01001328 ssl_build_record_nonce( iv, sizeof( iv ),
1329 transform->iv_dec,
1330 transform->fixed_ivlen,
1331 dynamic_iv,
1332 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001333
Hanno Beckerdf8be222020-05-21 15:30:57 +01001334 /*
1335 * Build additional data for AEAD encryption.
1336 * This depends on the TLS version.
1337 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001338 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1339 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001340 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001341 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001342
Hanno Beckerd96a6522019-07-10 13:55:25 +01001343 /* Because of the check above, we know that there are
1344 * explicit_iv_len Bytes preceeding data, and taglen
1345 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001346 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001347 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001348
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001349 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001350 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001351 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001352
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001353 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001354 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001355 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001356 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001357 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001358 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001359 data, rec->data_len + transform->taglen, /* src */
1360 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001361 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362 {
TRodziewicz18efb732021-04-29 23:12:19 +02001363 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1366 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001367
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001368 return( ret );
1369 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001370 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001371
Hanno Beckerd96a6522019-07-10 13:55:25 +01001372 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001373 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001377 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001378 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001381#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001384 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001385
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 /*
Paul Bakker45829992013-01-03 14:52:21 +01001387 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001390 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1391 {
1392 /* The ciphertext is prefixed with the CBC IV. */
1393 minlen += transform->ivlen;
1394 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001395#endif
Paul Bakker45829992013-01-03 14:52:21 +01001396
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001397 /* Size considerations:
1398 *
1399 * - The CBC cipher text must not be empty and hence
1400 * at least of size transform->ivlen.
1401 *
1402 * Together with the potential IV-prefix, this explains
1403 * the first of the two checks below.
1404 *
1405 * - The record must contain a MAC, either in plain or
1406 * encrypted, depending on whether Encrypt-then-MAC
1407 * is used or not.
1408 * - If it is, the message contains the IV-prefix,
1409 * the CBC ciphertext, and the MAC.
1410 * - If it is not, the padded plaintext, and hence
1411 * the CBC ciphertext, has at least length maclen + 1
1412 * because there is at least the padding length byte.
1413 *
1414 * As the CBC ciphertext is not empty, both cases give the
1415 * lower bound minlen + maclen + 1 on the record size, which
1416 * we test for in the second check below.
1417 */
1418 if( rec->data_len < minlen + transform->ivlen ||
1419 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001420 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1422 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1423 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001424 "+ 1 ) ( + expl IV )", rec->data_len,
1425 transform->ivlen,
1426 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001428 }
1429
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001430 /*
1431 * Authenticate before decrypt if enabled
1432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001434 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001435 {
Hanno Becker992b6872017-11-09 18:57:39 +00001436 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001439
Hanno Beckerd96a6522019-07-10 13:55:25 +01001440 /* Update data_len in tandem with add_data.
1441 *
1442 * The subtraction is safe because of the previous check
1443 * data_len >= minlen + maclen + 1.
1444 *
1445 * Afterwards, we know that data + data_len is followed by at
1446 * least maclen Bytes, which justifies the call to
1447 * mbedtls_ssl_safer_memcmp() below.
1448 *
1449 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001450 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001451 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1452 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001453
Hanno Beckerd96a6522019-07-10 13:55:25 +01001454 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001455 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1456 add_data_len );
1457 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1458 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001459 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1460 data, rec->data_len );
1461 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1462 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001463
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001464 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1465 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001466 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001467 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001468
Hanno Beckerd96a6522019-07-10 13:55:25 +01001469 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001470 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1471 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001475 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001476 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001477 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001479
1480 /*
1481 * Check length sanity
1482 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001483
1484 /* We know from above that data_len > minlen >= 0,
1485 * so the following check in particular implies that
1486 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001487 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001488 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1490 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001491 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001493 }
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001496 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001497 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001498 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001499 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001501 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001502 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001503
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001504 data += transform->ivlen;
1505 rec->data_offset += transform->ivlen;
1506 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001509
Hanno Beckerd96a6522019-07-10 13:55:25 +01001510 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1511
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001512 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1513 transform->iv_dec, transform->ivlen,
1514 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001517 return( ret );
1518 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001519
Hanno Beckerd96a6522019-07-10 13:55:25 +01001520 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001521 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1524 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001525 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001527#if defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001528 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001529 {
1530 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001531 * Save IV in TLS1, where CBC decryption of consecutive
Hanno Beckerd96a6522019-07-10 13:55:25 +01001532 * records is equivalent to CBC decryption of the concatenation
1533 * of the records; in other words, IVs are maintained across
1534 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001535 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001536 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1537 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001541 /* Safe since data_len >= minlen + maclen + 1, so after having
1542 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001543 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1544 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001545 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001546
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001547 if( auth_done == 1 )
1548 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001549 const size_t mask = mbedtls_ssl_cf_mask_ge(
1550 rec->data_len,
1551 padlen + 1 );
1552 correct &= mask;
1553 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001554 }
1555 else
Paul Bakker45829992013-01-03 14:52:21 +01001556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001558 if( rec->data_len < transform->maclen + padlen + 1 )
1559 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1561 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1562 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001563 rec->data_len,
1564 transform->maclen,
1565 padlen + 1 ) );
1566 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001567#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001568
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001569 const size_t mask = mbedtls_ssl_cf_mask_ge(
1570 rec->data_len,
1571 transform->maclen + padlen + 1 );
1572 correct &= mask;
1573 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001575
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001576 padlen++;
1577
1578 /* Regardless of the validity of the padding,
1579 * we have data_len >= padlen here. */
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1582 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001583 /* The padding check involves a series of up to 256
1584 * consecutive memory reads at the end of the record
1585 * plaintext buffer. In order to hide the length and
1586 * validity of the padding, always perform exactly
1587 * `min(256,plaintext_len)` reads (but take into account
1588 * only the last `padlen` bytes for the padding check). */
1589 size_t pad_count = 0;
1590 volatile unsigned char* const check = data;
1591
1592 /* Index of first padding byte; it has been ensured above
1593 * that the subtraction is safe. */
1594 size_t const padding_idx = rec->data_len - padlen;
1595 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1596 size_t const start_idx = rec->data_len - num_checks;
1597 size_t idx;
1598
1599 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001601 /* pad_count += (idx >= padding_idx) &&
1602 * (check[idx] == padlen - 1);
1603 */
1604 const size_t mask = mbedtls_ssl_cf_mask_ge( idx, padding_idx );
1605 const size_t equal = mbedtls_ssl_cf_bool_eq( check[idx],
1606 padlen - 1 );
1607 pad_count += mask & equal;
1608 }
1609 correct &= mbedtls_ssl_cf_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001612 if( padlen > 0 && correct == 0 )
1613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001614#endif
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001615 padlen &= mbedtls_ssl_cf_mask_from_bit( correct );
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1618 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001619
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001620 /* If the padding was found to be invalid, padlen == 0
1621 * and the subtraction is safe. If the padding was found valid,
1622 * padlen hasn't been changed and the previous assertion
1623 * data_len >= padlen still holds. */
1624 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001626 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001627#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1630 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001631 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001633#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001635 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001636#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
1638 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001639 * Authenticate if not done yet.
1640 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 */
Hanno Becker52344c22018-01-03 15:24:20 +00001642#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001643 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001644 {
Hanno Becker992b6872017-11-09 18:57:39 +00001645 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001646 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001647
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001648 /* If the initial value of padlen was such that
1649 * data_len < maclen + padlen + 1, then padlen
1650 * got reset to 1, and the initial check
1651 * data_len >= minlen + maclen + 1
1652 * guarantees that at this point we still
1653 * have at least data_len >= maclen.
1654 *
1655 * If the initial value of padlen was such that
1656 * data_len >= maclen + padlen + 1, then we have
1657 * subtracted either padlen + 1 (if the padding was correct)
1658 * or 0 (if the padding was incorrect) since then,
1659 * hence data_len >= maclen in any case.
1660 */
1661 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001662 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1663 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1666 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001667 /*
1668 * The next two sizes are the minimum and maximum values of
1669 * data_len over all padlen values.
1670 *
1671 * They're independent of padlen, since we previously did
1672 * data_len -= padlen.
1673 *
1674 * Note that max_len + maclen is never more than the buffer
1675 * length, as we previously did in_msglen -= maclen too.
1676 */
1677 const size_t max_len = rec->data_len + padlen;
1678 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1679
1680 ret = mbedtls_ssl_cf_hmac( &transform->md_ctx_dec,
1681 add_data, add_data_len,
1682 data, rec->data_len, min_len, max_len,
1683 mac_expect );
1684 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
1687 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001689
1690 mbedtls_ssl_cf_memcpy_offset( mac_peer, data,
1691 rec->data_len,
1692 min_len, max_len,
1693 transform->maclen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1695 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001697#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001698 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001699 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001700#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001702 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001703 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_SSL_DEBUG_ALL)
1706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001707#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001708 correct = 0;
1709 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001710 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001712
1713 /*
1714 * Finally check the correct flag
1715 */
1716 if( correct == 0 )
1717 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001718#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001719
1720 /* Make extra sure authentication was performed, exactly once */
1721 if( auth_done != 1 )
1722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001725 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
Hanno Beckerccc13d02020-05-04 12:30:04 +01001727#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1728 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1729 {
1730 /* Remove inner padding and infer true content type. */
1731 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1732 &rec->type );
1733
1734 if( ret != 0 )
1735 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1736 }
1737#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1738
Hanno Beckera0e20d02019-05-15 14:03:01 +01001739#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001740 if( rec->cid_len != 0 )
1741 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001742 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1743 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001744 if( ret != 0 )
1745 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1746 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001747#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
1751 return( 0 );
1752}
1753
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001754#undef MAC_NONE
1755#undef MAC_PLAINTEXT
1756#undef MAC_CIPHERTEXT
1757
Paul Bakker5121ce52009-01-03 21:22:43 +00001758/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001759 * Fill the input message buffer by appending data to it.
1760 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001761 *
1762 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1763 * available (from this read and/or a previous one). Otherwise, an error code
1764 * is returned (possibly EOF or WANT_READ).
1765 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001766 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1767 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1768 * since we always read a whole datagram at once.
1769 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001770 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001771 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001772 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001774{
Janos Follath865b3eb2019-12-16 11:46:15 +00001775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001776 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001777#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1778 size_t in_buf_len = ssl->in_buf_len;
1779#else
1780 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001785 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001788 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001790 }
1791
Darryl Greenb33cc762019-11-28 14:29:44 +00001792 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001796 }
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001799 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001801 uint32_t timeout;
1802
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001803 /*
1804 * The point is, we need to always read a full datagram at once, so we
1805 * sometimes read more then requested, and handle the additional data.
1806 * It could be the rest of the current record (while fetching the
1807 * header) and/or some other records in the same datagram.
1808 */
1809
1810 /*
1811 * Move to the next record in the already read datagram if applicable
1812 */
1813 if( ssl->next_record_offset != 0 )
1814 {
1815 if( ssl->in_left < ssl->next_record_offset )
1816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1818 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001819 }
1820
1821 ssl->in_left -= ssl->next_record_offset;
1822
1823 if( ssl->in_left != 0 )
1824 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1826 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001827 ssl->next_record_offset ) );
1828 memmove( ssl->in_hdr,
1829 ssl->in_hdr + ssl->next_record_offset,
1830 ssl->in_left );
1831 }
1832
1833 ssl->next_record_offset = 0;
1834 }
1835
Paul Elliottd48d5c62021-01-07 14:47:05 +00001836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1837 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001839
1840 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001841 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001842 */
1843 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001846 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001847 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001848
1849 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001850 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001851 * are not at the beginning of a new record, the caller did something
1852 * wrong.
1853 */
1854 if( ssl->in_left != 0 )
1855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1857 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001858 }
1859
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001860 /*
1861 * Don't even try to read if time's out already.
1862 * This avoids by-passing the timer when repeatedly receiving messages
1863 * that will end up being dropped.
1864 */
Hanno Becker7876d122020-02-05 10:39:31 +00001865 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001866 {
1867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001868 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001869 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001870 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001871 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001872 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001875 timeout = ssl->handshake->retransmit_timeout;
1876 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001877 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001878
Paul Elliott9f352112020-12-09 14:55:45 +00001879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001880
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001881 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001882 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1883 timeout );
1884 else
1885 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001888
1889 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001891 }
1892
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001893 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001896 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001899 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001900 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001903 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001904 }
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001909 return( ret );
1910 }
1911
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001912 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001913 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001915 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001917 {
Hanno Becker786300f2020-02-05 10:46:40 +00001918 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001919 {
Hanno Becker786300f2020-02-05 10:46:40 +00001920 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1921 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001922 return( ret );
1923 }
1924
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001925 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001926 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001928 }
1929
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 if( ret < 0 )
1931 return( ret );
1932
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001933 ssl->in_left = ret;
1934 }
1935 else
1936#endif
1937 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1939 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001940 ssl->in_left, nb_want ) );
1941
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001942 while( ssl->in_left < nb_want )
1943 {
1944 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001945
Hanno Becker7876d122020-02-05 10:39:31 +00001946 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001947 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1948 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001949 {
1950 if( ssl->f_recv_timeout != NULL )
1951 {
1952 ret = ssl->f_recv_timeout( ssl->p_bio,
1953 ssl->in_hdr + ssl->in_left, len,
1954 ssl->conf->read_timeout );
1955 }
1956 else
1957 {
1958 ret = ssl->f_recv( ssl->p_bio,
1959 ssl->in_hdr + ssl->in_left, len );
1960 }
1961 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001962
Paul Elliottd48d5c62021-01-07 14:47:05 +00001963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1964 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001965 ssl->in_left, nb_want ) );
1966 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001967
1968 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001970
1971 if( ret < 0 )
1972 return( ret );
1973
makise-homuraaf9513b2020-08-24 18:26:27 +03001974 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001975 {
Darryl Green11999bb2018-03-13 15:22:58 +00001976 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001977 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001978 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1980 }
1981
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001982 ssl->in_left += ret;
1983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 }
1985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
1988 return( 0 );
1989}
1990
1991/*
1992 * Flush any data not yet written
1993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001994int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001995{
Janos Follath865b3eb2019-12-16 11:46:15 +00001996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001997 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002001 if( ssl->f_send == NULL )
2002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002004 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002006 }
2007
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002008 /* Avoid incrementing counter if data is flushed */
2009 if( ssl->out_left == 0 )
2010 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002012 return( 0 );
2013 }
2014
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 while( ssl->out_left > 0 )
2016 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
2018 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01002019 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
Hanno Becker2b1e3542018-08-06 11:19:13 +01002021 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002022 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002024 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
2026 if( ret <= 0 )
2027 return( ret );
2028
makise-homuraaf9513b2020-08-24 18:26:27 +03002029 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002030 {
Darryl Green11999bb2018-03-13 15:22:58 +00002031 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002032 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00002033 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2035 }
2036
Paul Bakker5121ce52009-01-03 21:22:43 +00002037 ssl->out_left -= ret;
2038 }
2039
Hanno Becker2b1e3542018-08-06 11:19:13 +01002040#if defined(MBEDTLS_SSL_PROTO_DTLS)
2041 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002042 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002043 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002044 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002045 else
2046#endif
2047 {
2048 ssl->out_hdr = ssl->out_buf + 8;
2049 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002050 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002053
2054 return( 0 );
2055}
2056
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002057/*
2058 * Functions to handle the DTLS retransmission state machine
2059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002061/*
2062 * Append current handshake message to current outgoing flight
2063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002065{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002066 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2068 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2069 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002070
2071 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002072 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002073 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002076 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002077 }
2078
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002079 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002080 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2082 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002084 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002085 }
2086
2087 /* Copy current handshake message with headers */
2088 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2089 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002090 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002091 msg->next = NULL;
2092
2093 /* Append to the current flight */
2094 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002095 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002096 else
2097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002099 while( cur->next != NULL )
2100 cur = cur->next;
2101 cur->next = msg;
2102 }
2103
Hanno Becker3b235902018-08-06 09:54:53 +01002104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002105 return( 0 );
2106}
2107
2108/*
2109 * Free the current flight of handshake messages
2110 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002111void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 mbedtls_ssl_flight_item *cur = flight;
2114 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002115
2116 while( cur != NULL )
2117 {
2118 next = cur->next;
2119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_free( cur->p );
2121 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002122
2123 cur = next;
2124 }
2125}
2126
2127/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002128 * Swap transform_out and out_ctr with the alternative ones
2129 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002130static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002131{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002133 unsigned char tmp_out_ctr[8];
2134
2135 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002138 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002139 }
2140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002142
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002143 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002144 tmp_transform = ssl->transform_out;
2145 ssl->transform_out = ssl->handshake->alt_transform_out;
2146 ssl->handshake->alt_transform_out = tmp_transform;
2147
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002148 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002149 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2150 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002151 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002152
2153 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002154 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002155
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002156 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002157}
2158
2159/*
2160 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002161 */
2162int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2163{
2164 int ret = 0;
2165
2166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2167
2168 ret = mbedtls_ssl_flight_transmit( ssl );
2169
2170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2171
2172 return( ret );
2173}
2174
2175/*
2176 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002177 *
2178 * Need to remember the current message in case flush_output returns
2179 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002180 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002181 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002182int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002183{
Janos Follath865b3eb2019-12-16 11:46:15 +00002184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002188 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002189 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002190
2191 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002192 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002193 ret = ssl_swap_epochs( ssl );
2194 if( ret != 0 )
2195 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002197 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002198 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002199
2200 while( ssl->handshake->cur_msg != NULL )
2201 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002202 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002203 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002204
Hanno Beckere1dcb032018-08-17 16:47:58 +01002205 int const is_finished =
2206 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2207 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2208
Hanno Becker04da1892018-08-14 13:22:10 +01002209 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2210 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2211
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002212 /* Swap epochs before sending Finished: we can't do it after
2213 * sending ChangeCipherSpec, in case write returns WANT_READ.
2214 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002215 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002216 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002218 ret = ssl_swap_epochs( ssl );
2219 if( ret != 0 )
2220 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002221 }
2222
Hanno Becker67bc7c32018-08-06 11:33:50 +01002223 ret = ssl_get_remaining_payload_in_datagram( ssl );
2224 if( ret < 0 )
2225 return( ret );
2226 max_frag_len = (size_t) ret;
2227
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002228 /* CCS is copied as is, while HS messages may need fragmentation */
2229 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2230 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002231 if( max_frag_len == 0 )
2232 {
2233 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2234 return( ret );
2235
2236 continue;
2237 }
2238
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002239 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002240 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002241 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002242
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002243 /* Update position inside current message */
2244 ssl->handshake->cur_msg_p += cur->len;
2245 }
2246 else
2247 {
2248 const unsigned char * const p = ssl->handshake->cur_msg_p;
2249 const size_t hs_len = cur->len - 12;
2250 const size_t frag_off = p - ( cur->p + 12 );
2251 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002252 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002253
Hanno Beckere1dcb032018-08-17 16:47:58 +01002254 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002255 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002256 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002257 {
2258 ret = ssl_swap_epochs( ssl );
2259 if( ret != 0 )
2260 return( ret );
2261 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002262
Hanno Becker67bc7c32018-08-06 11:33:50 +01002263 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2264 return( ret );
2265
2266 continue;
2267 }
2268 max_hs_frag_len = max_frag_len - 12;
2269
2270 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2271 max_hs_frag_len : rem_len;
2272
2273 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002274 {
2275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002276 (unsigned) cur_hs_frag_len,
2277 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002278 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002279
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002280 /* Messages are stored with handshake headers as if not fragmented,
2281 * copy beginning of headers then fill fragmentation fields.
2282 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2283 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002284
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002285 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2286 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2287 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2288
Hanno Becker67bc7c32018-08-06 11:33:50 +01002289 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2290 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2291 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002292
2293 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2294
Hanno Becker3f7b9732018-08-28 09:53:25 +01002295 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002296 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2297 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002298 ssl->out_msgtype = cur->type;
2299
2300 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002301 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002302 }
2303
2304 /* If done with the current message move to the next one if any */
2305 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2306 {
2307 if( cur->next != NULL )
2308 {
2309 ssl->handshake->cur_msg = cur->next;
2310 ssl->handshake->cur_msg_p = cur->next->p + 12;
2311 }
2312 else
2313 {
2314 ssl->handshake->cur_msg = NULL;
2315 ssl->handshake->cur_msg_p = NULL;
2316 }
2317 }
2318
2319 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002320 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002323 return( ret );
2324 }
2325 }
2326
Hanno Becker67bc7c32018-08-06 11:33:50 +01002327 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2328 return( ret );
2329
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002330 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2332 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002333 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002336 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002337 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002338
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002340
2341 return( 0 );
2342}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002343
2344/*
2345 * To be called when the last message of an incoming flight is received.
2346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002348{
2349 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002350 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002351 ssl->handshake->flight = NULL;
2352 ssl->handshake->cur_msg = NULL;
2353
2354 /* The next incoming flight will start with this msg_seq */
2355 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2356
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002357 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002358 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002359
Hanno Becker0271f962018-08-16 13:23:47 +01002360 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002361 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002362
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002363 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002364 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2367 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002370 }
2371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002373}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002374
2375/*
2376 * To be called when the last message of an outgoing flight is send.
2377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002379{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002380 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002381 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2384 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002387 }
2388 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002389 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002392
Paul Bakker5121ce52009-01-03 21:22:43 +00002393/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002394 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002396
2397/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002398 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002399 *
2400 * - fill in handshake headers
2401 * - update handshake checksum
2402 * - DTLS: save message for resending
2403 * - then pass to the record layer
2404 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002405 * DTLS: except for HelloRequest, messages are only queued, and will only be
2406 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002407 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002408 * Inputs:
2409 * - ssl->out_msglen: 4 + actual handshake message len
2410 * (4 is the size of handshake headers for TLS)
2411 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2412 * - ssl->out_msg + 4: the handshake message body
2413 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002414 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002415 * - ssl->out_msglen: the length of the record contents
2416 * (including handshake headers but excluding record headers)
2417 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002418 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002419int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420{
Janos Follath865b3eb2019-12-16 11:46:15 +00002421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002422 const size_t hs_len = ssl->out_msglen - 4;
2423 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2426
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002427 /*
2428 * Sanity checks
2429 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002430 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002431 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2432 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2434 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002436
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002437 /* Whenever we send anything different from a
2438 * HelloRequest we should be in a handshake - double check. */
2439 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2440 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002441 ssl->handshake == NULL )
2442 {
2443 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2444 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2445 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002448 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002449 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002451 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2453 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002454 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002455#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002456
Hanno Beckerb50a2532018-08-06 11:52:54 +01002457 /* Double-check that we did not exceed the bounds
2458 * of the outgoing record buffer.
2459 * This should never fail as the various message
2460 * writing functions must obey the bounds of the
2461 * outgoing record buffer, but better be safe.
2462 *
2463 * Note: We deliberately do not check for the MTU or MFL here.
2464 */
2465 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2466 {
2467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002468 "size %" MBEDTLS_PRINTF_SIZET
2469 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002470 ssl->out_msglen,
2471 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002472 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2473 }
2474
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002475 /*
2476 * Fill handshake headers
2477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002480 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2481 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2482 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002484 /*
2485 * DTLS has additional fields in the Handshake layer,
2486 * between the length field and the actual payload:
2487 * uint16 message_seq;
2488 * uint24 fragment_offset;
2489 * uint24 fragment_length;
2490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002493 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002494 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002495 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002496 {
2497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002498 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002499 hs_len,
2500 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2502 }
2503
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002504 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002505 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002506
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002507 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002508 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002509 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002510 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2511 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2512 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002513 }
2514 else
2515 {
2516 ssl->out_msg[4] = 0;
2517 ssl->out_msg[5] = 0;
2518 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002519
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002520 /* Handshake hashes are computed without fragmentation,
2521 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002522 memset( ssl->out_msg + 6, 0x00, 3 );
2523 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002526
Hanno Becker0207e532018-08-28 10:28:28 +01002527 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002528 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2529 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 }
2531
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002532 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002534 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002535 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2536 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002537 {
2538 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002541 return( ret );
2542 }
2543 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002544 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002545#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002546 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002547 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002548 {
2549 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2550 return( ret );
2551 }
2552 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002553
2554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2555
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002556 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002557}
2558
2559/*
2560 * Record layer functions
2561 */
2562
2563/*
2564 * Write current record.
2565 *
2566 * Uses:
2567 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2568 * - ssl->out_msglen: length of the record content (excl headers)
2569 * - ssl->out_msg: record content
2570 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002571int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002572{
2573 int ret, done = 0;
2574 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002575 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002576
2577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002578
Paul Bakker05ef8352012-05-08 09:17:57 +00002579 if( !done )
2580 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002581 unsigned i;
2582 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002583#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2584 size_t out_buf_len = ssl->out_buf_len;
2585#else
2586 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2587#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002588 /* Skip writing the record content type to after the encryption,
2589 * as it may change when using the CID extension. */
2590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002592 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002593
Hanno Becker19859472018-08-06 09:40:20 +01002594 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002595 ssl->out_len[0] = (unsigned char)( len >> 8 );
2596 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002597
Paul Bakker48916f92012-09-16 19:57:18 +00002598 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002599 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002600 mbedtls_record rec;
2601
2602 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002603 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002604 rec.data_len = ssl->out_msglen;
2605 rec.data_offset = ssl->out_msg - rec.buf;
2606
2607 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2608 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2609 ssl->conf->transport, rec.ver );
2610 rec.type = ssl->out_msgtype;
2611
Hanno Beckera0e20d02019-05-15 14:03:01 +01002612#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002613 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002614 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002615#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002616
Hanno Beckera18d1322018-01-03 14:27:32 +00002617 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002618 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002621 return( ret );
2622 }
2623
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002624 if( rec.data_offset != 0 )
2625 {
2626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2627 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2628 }
2629
Hanno Becker6430faf2019-05-08 11:57:13 +01002630 /* Update the record content type and CID. */
2631 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002632#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002633 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002634#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002635 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002636 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2637 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002638 }
2639
Hanno Becker5903de42019-05-03 14:46:38 +01002640 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002641
2642#if defined(MBEDTLS_SSL_PROTO_DTLS)
2643 /* In case of DTLS, double-check that we don't exceed
2644 * the remaining space in the datagram. */
2645 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2646 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002647 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002648 if( ret < 0 )
2649 return( ret );
2650
2651 if( protected_record_size > (size_t) ret )
2652 {
2653 /* Should never happen */
2654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2655 }
2656 }
2657#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002658
Hanno Becker6430faf2019-05-08 11:57:13 +01002659 /* Now write the potentially updated record content type. */
2660 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2661
Paul Elliott9f352112020-12-09 14:55:45 +00002662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002663 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002664 ssl->out_hdr[0], ssl->out_hdr[1],
2665 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002668 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002669
2670 ssl->out_left += protected_record_size;
2671 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002672 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002673
Hanno Beckerdd772292020-02-05 10:38:31 +00002674 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002675 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2676 break;
2677
2678 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002679 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002680 {
2681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2682 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2683 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 }
2685
Hanno Becker67bc7c32018-08-06 11:33:50 +01002686#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002687 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2688 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002689 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002690 size_t remaining;
2691 ret = ssl_get_remaining_payload_in_datagram( ssl );
2692 if( ret < 0 )
2693 {
2694 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2695 ret );
2696 return( ret );
2697 }
2698
2699 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002700 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002701 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002702 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002703 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002704 else
2705 {
Hanno Becker513815a2018-08-20 11:56:09 +01002706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002707 }
2708 }
2709#endif /* MBEDTLS_SSL_PROTO_DTLS */
2710
2711 if( ( flush == SSL_FORCE_FLUSH ) &&
2712 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 return( ret );
2716 }
2717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
2720 return( 0 );
2721}
2722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002724
2725static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2726{
2727 if( ssl->in_msglen < ssl->in_hslen ||
2728 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2729 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2730 {
2731 return( 1 );
2732 }
2733 return( 0 );
2734}
Hanno Becker44650b72018-08-16 12:51:11 +01002735
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002736static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002737{
2738 return( ( ssl->in_msg[9] << 16 ) |
2739 ( ssl->in_msg[10] << 8 ) |
2740 ssl->in_msg[11] );
2741}
2742
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002743static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002744{
2745 return( ( ssl->in_msg[6] << 16 ) |
2746 ( ssl->in_msg[7] << 8 ) |
2747 ssl->in_msg[8] );
2748}
2749
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002750static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002751{
2752 uint32_t msg_len, frag_off, frag_len;
2753
2754 msg_len = ssl_get_hs_total_len( ssl );
2755 frag_off = ssl_get_hs_frag_off( ssl );
2756 frag_len = ssl_get_hs_frag_len( ssl );
2757
2758 if( frag_off > msg_len )
2759 return( -1 );
2760
2761 if( frag_len > msg_len - frag_off )
2762 return( -1 );
2763
2764 if( frag_len + 12 > ssl->in_msglen )
2765 return( -1 );
2766
2767 return( 0 );
2768}
2769
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002770/*
2771 * Mark bits in bitmask (used for DTLS HS reassembly)
2772 */
2773static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2774{
2775 unsigned int start_bits, end_bits;
2776
2777 start_bits = 8 - ( offset % 8 );
2778 if( start_bits != 8 )
2779 {
2780 size_t first_byte_idx = offset / 8;
2781
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002782 /* Special case */
2783 if( len <= start_bits )
2784 {
2785 for( ; len != 0; len-- )
2786 mask[first_byte_idx] |= 1 << ( start_bits - len );
2787
2788 /* Avoid potential issues with offset or len becoming invalid */
2789 return;
2790 }
2791
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002792 offset += start_bits; /* Now offset % 8 == 0 */
2793 len -= start_bits;
2794
2795 for( ; start_bits != 0; start_bits-- )
2796 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2797 }
2798
2799 end_bits = len % 8;
2800 if( end_bits != 0 )
2801 {
2802 size_t last_byte_idx = ( offset + len ) / 8;
2803
2804 len -= end_bits; /* Now len % 8 == 0 */
2805
2806 for( ; end_bits != 0; end_bits-- )
2807 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2808 }
2809
2810 memset( mask + offset / 8, 0xFF, len / 8 );
2811}
2812
2813/*
2814 * Check that bitmask is full
2815 */
2816static int ssl_bitmask_check( unsigned char *mask, size_t len )
2817{
2818 size_t i;
2819
2820 for( i = 0; i < len / 8; i++ )
2821 if( mask[i] != 0xFF )
2822 return( -1 );
2823
2824 for( i = 0; i < len % 8; i++ )
2825 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2826 return( -1 );
2827
2828 return( 0 );
2829}
2830
Hanno Becker56e205e2018-08-16 09:06:12 +01002831/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002832static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002833 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002834{
Hanno Becker56e205e2018-08-16 09:06:12 +01002835 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002836
Hanno Becker56e205e2018-08-16 09:06:12 +01002837 alloc_len = 12; /* Handshake header */
2838 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002839
Hanno Beckerd07df862018-08-16 09:14:58 +01002840 if( add_bitmap )
2841 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002842
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002843 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002844}
Hanno Becker56e205e2018-08-16 09:06:12 +01002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002847
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002848static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002849{
2850 return( ( ssl->in_msg[1] << 16 ) |
2851 ( ssl->in_msg[2] << 8 ) |
2852 ssl->in_msg[3] );
2853}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002854
Simon Butcher99000142016-10-13 17:21:01 +01002855int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002856{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002858 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002860 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002862 }
2863
Hanno Becker12555c62018-08-16 12:47:53 +01002864 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002866 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002867 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002868 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002871 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002872 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002874 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002875
Hanno Becker44650b72018-08-16 12:51:11 +01002876 if( ssl_check_hs_header( ssl ) != 0 )
2877 {
2878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2879 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2880 }
2881
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002882 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002883 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2884 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2885 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2886 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002887 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002888 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2889 {
2890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2891 recv_msg_seq,
2892 ssl->handshake->in_msg_seq ) );
2893 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2894 }
2895
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002896 /* Retransmit only on last message from previous flight, to avoid
2897 * too many retransmissions.
2898 * Besides, No sane server ever retransmits HelloVerifyRequest */
2899 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002900 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002903 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002904 recv_msg_seq,
2905 ssl->handshake->in_flight_start_seq ) );
2906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002910 return( ret );
2911 }
2912 }
2913 else
2914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002916 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002917 recv_msg_seq,
2918 ssl->handshake->in_msg_seq ) );
2919 }
2920
Hanno Becker90333da2017-10-10 11:27:13 +01002921 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002922 }
2923 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002924
Hanno Becker6d97ef52018-08-16 13:09:04 +01002925 /* Message reassembly is handled alongside buffering of future
2926 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002927 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002928 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002929 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002932 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002933 }
2934 }
2935 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002936#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002937 /* With TLS we don't handle fragmentation (for now) */
2938 if( ssl->in_msglen < ssl->in_hslen )
2939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2941 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002942 }
2943
Simon Butcher99000142016-10-13 17:21:01 +01002944 return( 0 );
2945}
2946
2947void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2948{
Hanno Becker0271f962018-08-16 13:23:47 +01002949 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002950
Hanno Becker0271f962018-08-16 13:23:47 +01002951 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002952 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002953 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002954 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002955
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002956 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002958 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002959 ssl->handshake != NULL )
2960 {
Hanno Becker0271f962018-08-16 13:23:47 +01002961 unsigned offset;
2962 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002963
Hanno Becker0271f962018-08-16 13:23:47 +01002964 /* Increment handshake sequence number */
2965 hs->in_msg_seq++;
2966
2967 /*
2968 * Clear up handshake buffering and reassembly structure.
2969 */
2970
2971 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002972 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002973
2974 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002975 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2976 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002977 offset++, hs_buf++ )
2978 {
2979 *hs_buf = *(hs_buf + 1);
2980 }
2981
2982 /* Create a fresh last entry */
2983 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002984 }
2985#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002986}
2987
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002988/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002989 * DTLS anti-replay: RFC 6347 4.1.2.6
2990 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002991 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2992 * Bit n is set iff record number in_window_top - n has been seen.
2993 *
2994 * Usually, in_window_top is the last record number seen and the lsb of
2995 * in_window is set. The only exception is the initial state (record number 0
2996 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002999void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003000{
3001 ssl->in_window_top = 0;
3002 ssl->in_window = 0;
3003}
3004
3005static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3006{
3007 return( ( (uint64_t) buf[0] << 40 ) |
3008 ( (uint64_t) buf[1] << 32 ) |
3009 ( (uint64_t) buf[2] << 24 ) |
3010 ( (uint64_t) buf[3] << 16 ) |
3011 ( (uint64_t) buf[4] << 8 ) |
3012 ( (uint64_t) buf[5] ) );
3013}
3014
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003015static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3016{
Janos Follath865b3eb2019-12-16 11:46:15 +00003017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003018 unsigned char *original_in_ctr;
3019
3020 // save original in_ctr
3021 original_in_ctr = ssl->in_ctr;
3022
3023 // use counter from record
3024 ssl->in_ctr = record_in_ctr;
3025
3026 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3027
3028 // restore the counter
3029 ssl->in_ctr = original_in_ctr;
3030
3031 return ret;
3032}
3033
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003034/*
3035 * Return 0 if sequence number is acceptable, -1 otherwise
3036 */
Hanno Becker0183d692019-07-12 08:50:37 +01003037int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003038{
3039 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3040 uint64_t bit;
3041
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003042 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003043 return( 0 );
3044
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003045 if( rec_seqnum > ssl->in_window_top )
3046 return( 0 );
3047
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003048 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003049
3050 if( bit >= 64 )
3051 return( -1 );
3052
3053 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3054 return( -1 );
3055
3056 return( 0 );
3057}
3058
3059/*
3060 * Update replay window on new validated record
3061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003062void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003063{
3064 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3065
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003066 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003067 return;
3068
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003069 if( rec_seqnum > ssl->in_window_top )
3070 {
3071 /* Update window_top and the contents of the window */
3072 uint64_t shift = rec_seqnum - ssl->in_window_top;
3073
3074 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003075 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003076 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003077 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003078 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003079 ssl->in_window |= 1;
3080 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003081
3082 ssl->in_window_top = rec_seqnum;
3083 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003084 else
3085 {
3086 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003087 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003088
3089 if( bit < 64 ) /* Always true, but be extra sure */
3090 ssl->in_window |= (uint64_t) 1 << bit;
3091 }
3092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003094
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003095#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003096/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003097 * Without any SSL context, check if a datagram looks like a ClientHello with
3098 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003099 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003100 *
3101 * - if cookie is valid, return 0
3102 * - if ClientHello looks superficially valid but cookie is not,
3103 * fill obuf and set olen, then
3104 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3105 * - otherwise return a specific error code
3106 */
3107static int ssl_check_dtls_clihlo_cookie(
3108 mbedtls_ssl_cookie_write_t *f_cookie_write,
3109 mbedtls_ssl_cookie_check_t *f_cookie_check,
3110 void *p_cookie,
3111 const unsigned char *cli_id, size_t cli_id_len,
3112 const unsigned char *in, size_t in_len,
3113 unsigned char *obuf, size_t buf_len, size_t *olen )
3114{
3115 size_t sid_len, cookie_len;
3116 unsigned char *p;
3117
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003118 /*
3119 * Structure of ClientHello with record and handshake headers,
3120 * and expected values. We don't need to check a lot, more checks will be
3121 * done when actually parsing the ClientHello - skipping those checks
3122 * avoids code duplication and does not make cookie forging any easier.
3123 *
3124 * 0-0 ContentType type; copied, must be handshake
3125 * 1-2 ProtocolVersion version; copied
3126 * 3-4 uint16 epoch; copied, must be 0
3127 * 5-10 uint48 sequence_number; copied
3128 * 11-12 uint16 length; (ignored)
3129 *
3130 * 13-13 HandshakeType msg_type; (ignored)
3131 * 14-16 uint24 length; (ignored)
3132 * 17-18 uint16 message_seq; copied
3133 * 19-21 uint24 fragment_offset; copied, must be 0
3134 * 22-24 uint24 fragment_length; (ignored)
3135 *
3136 * 25-26 ProtocolVersion client_version; (ignored)
3137 * 27-58 Random random; (ignored)
3138 * 59-xx SessionID session_id; 1 byte len + sid_len content
3139 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3140 * ...
3141 *
3142 * Minimum length is 61 bytes.
3143 */
3144 if( in_len < 61 ||
3145 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3146 in[3] != 0 || in[4] != 0 ||
3147 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3148 {
3149 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3150 }
3151
3152 sid_len = in[59];
3153 if( sid_len > in_len - 61 )
3154 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3155
3156 cookie_len = in[60 + sid_len];
3157 if( cookie_len > in_len - 60 )
3158 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3159
3160 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3161 cli_id, cli_id_len ) == 0 )
3162 {
3163 /* Valid cookie */
3164 return( 0 );
3165 }
3166
3167 /*
3168 * If we get here, we've got an invalid cookie, let's prepare HVR.
3169 *
3170 * 0-0 ContentType type; copied
3171 * 1-2 ProtocolVersion version; copied
3172 * 3-4 uint16 epoch; copied
3173 * 5-10 uint48 sequence_number; copied
3174 * 11-12 uint16 length; olen - 13
3175 *
3176 * 13-13 HandshakeType msg_type; hello_verify_request
3177 * 14-16 uint24 length; olen - 25
3178 * 17-18 uint16 message_seq; copied
3179 * 19-21 uint24 fragment_offset; copied
3180 * 22-24 uint24 fragment_length; olen - 25
3181 *
3182 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3183 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3184 *
3185 * Minimum length is 28.
3186 */
3187 if( buf_len < 28 )
3188 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3189
3190 /* Copy most fields and adapt others */
3191 memcpy( obuf, in, 25 );
3192 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3193 obuf[25] = 0xfe;
3194 obuf[26] = 0xff;
3195
3196 /* Generate and write actual cookie */
3197 p = obuf + 28;
3198 if( f_cookie_write( p_cookie,
3199 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3200 {
3201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3202 }
3203
3204 *olen = p - obuf;
3205
3206 /* Go back and fill length fields */
3207 obuf[27] = (unsigned char)( *olen - 28 );
3208
3209 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3210 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3211 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3212
3213 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3214 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3215
3216 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3217}
3218
3219/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003220 * Handle possible client reconnect with the same UDP quadruplet
3221 * (RFC 6347 Section 4.2.8).
3222 *
3223 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3224 * that looks like a ClientHello.
3225 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003226 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003227 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003228 * - if the input looks like a ClientHello with a valid cookie,
3229 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003230 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003231 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003232 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003233 * This function is called (through ssl_check_client_reconnect()) when an
3234 * unexpected record is found in ssl_get_next_record(), which will discard the
3235 * record if we return 0, and bubble up the return value otherwise (this
3236 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3237 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003238 */
3239static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3240{
Janos Follath865b3eb2019-12-16 11:46:15 +00003241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003242 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003243
Hanno Becker2fddd372019-07-10 14:37:41 +01003244 if( ssl->conf->f_cookie_write == NULL ||
3245 ssl->conf->f_cookie_check == NULL )
3246 {
3247 /* If we can't use cookies to verify reachability of the peer,
3248 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3250 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003251 return( 0 );
3252 }
3253
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003254 ret = ssl_check_dtls_clihlo_cookie(
3255 ssl->conf->f_cookie_write,
3256 ssl->conf->f_cookie_check,
3257 ssl->conf->p_cookie,
3258 ssl->cli_id, ssl->cli_id_len,
3259 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003260 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003261
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003262 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3263
3264 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003265 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003266 int send_ret;
3267 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3268 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3269 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003270 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003271 * If the error is permanent we'll catch it later,
3272 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003273 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3274 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3275 (void) send_ret;
3276
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003277 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003278 }
3279
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003280 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003281 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003283 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003284 {
3285 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3286 return( ret );
3287 }
3288
3289 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003290 }
3291
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003292 return( ret );
3293}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003294#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003295
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003296static int ssl_check_record_type( uint8_t record_type )
3297{
3298 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3299 record_type != MBEDTLS_SSL_MSG_ALERT &&
3300 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3301 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3302 {
3303 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3304 }
3305
3306 return( 0 );
3307}
3308
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003309/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003310 * ContentType type;
3311 * ProtocolVersion version;
3312 * uint16 epoch; // DTLS only
3313 * uint48 sequence_number; // DTLS only
3314 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003315 *
3316 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003317 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003318 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3319 *
3320 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003321 * 1. proceed with the record if this function returns 0
3322 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3323 * 3. return CLIENT_RECONNECT if this function return that value
3324 * 4. drop the whole datagram if this function returns anything else.
3325 * Point 2 is needed when the peer is resending, and we have already received
3326 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003327 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003328static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003329 unsigned char *buf,
3330 size_t len,
3331 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003332{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003333 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Hanno Beckere5e7e782019-07-11 12:29:35 +01003335 size_t const rec_hdr_type_offset = 0;
3336 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003337
Hanno Beckere5e7e782019-07-11 12:29:35 +01003338 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3339 rec_hdr_type_len;
3340 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003341
Hanno Beckere5e7e782019-07-11 12:29:35 +01003342 size_t const rec_hdr_ctr_len = 8;
3343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003344 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003345 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3346 rec_hdr_version_len;
3347
Hanno Beckera0e20d02019-05-15 14:03:01 +01003348#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003349 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3350 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003351 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003352#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3353#endif /* MBEDTLS_SSL_PROTO_DTLS */
3354
3355 size_t rec_hdr_len_offset; /* To be determined */
3356 size_t const rec_hdr_len_len = 2;
3357
3358 /*
3359 * Check minimum lengths for record header.
3360 */
3361
3362#if defined(MBEDTLS_SSL_PROTO_DTLS)
3363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3364 {
3365 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3366 }
3367 else
3368#endif /* MBEDTLS_SSL_PROTO_DTLS */
3369 {
3370 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3371 }
3372
3373 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3374 {
3375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3376 (unsigned) len,
3377 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3378 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3379 }
3380
3381 /*
3382 * Parse and validate record content type
3383 */
3384
3385 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003386
3387 /* Check record content type */
3388#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3389 rec->cid_len = 0;
3390
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003392 ssl->conf->cid_len != 0 &&
3393 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003394 {
3395 /* Shift pointers to account for record header including CID
3396 * struct {
3397 * ContentType special_type = tls12_cid;
3398 * ProtocolVersion version;
3399 * uint16 epoch;
3400 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003401 * opaque cid[cid_length]; // Additional field compared to
3402 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003403 * uint16 length;
3404 * opaque enc_content[DTLSCiphertext.length];
3405 * } DTLSCiphertext;
3406 */
3407
3408 /* So far, we only support static CID lengths
3409 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003410 rec_hdr_cid_len = ssl->conf->cid_len;
3411 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003412
Hanno Beckere5e7e782019-07-11 12:29:35 +01003413 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003414 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3416 (unsigned) len,
3417 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003418 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003419 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003420
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003421 /* configured CID len is guaranteed at most 255, see
3422 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3423 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003424 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003425 }
3426 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003427#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003428 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003429 if( ssl_check_record_type( rec->type ) )
3430 {
Hanno Becker54229812019-07-12 14:40:00 +01003431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3432 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3434 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003435 }
3436
Hanno Beckere5e7e782019-07-11 12:29:35 +01003437 /*
3438 * Parse and validate record version
3439 */
3440
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003441 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3442 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003443 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3444 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003445 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003446
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003447 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3450 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 }
3452
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003453 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3456 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 }
3458
Hanno Beckere5e7e782019-07-11 12:29:35 +01003459 /*
3460 * Parse/Copy record sequence number.
3461 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003462
Hanno Beckere5e7e782019-07-11 12:29:35 +01003463#if defined(MBEDTLS_SSL_PROTO_DTLS)
3464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003465 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003466 /* Copy explicit record sequence number from input buffer. */
3467 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3468 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003469 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003470 else
3471#endif /* MBEDTLS_SSL_PROTO_DTLS */
3472 {
3473 /* Copy implicit record sequence number from SSL context structure. */
3474 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3475 }
Paul Bakker40e46942009-01-03 21:51:57 +00003476
Hanno Beckere5e7e782019-07-11 12:29:35 +01003477 /*
3478 * Parse record length.
3479 */
3480
Hanno Beckere5e7e782019-07-11 12:29:35 +01003481 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003482 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3483 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003484 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Paul Elliott9f352112020-12-09 14:55:45 +00003486 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003487 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003488 rec->type,
3489 major_ver, minor_ver, rec->data_len ) );
3490
3491 rec->buf = buf;
3492 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003493
Hanno Beckerd417cc92019-07-26 08:20:27 +01003494 if( rec->data_len == 0 )
3495 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003497 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003498 * DTLS-related tests.
3499 * Check epoch before checking length constraint because
3500 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3501 * message gets duplicated before the corresponding Finished message,
3502 * the second ChangeCipherSpec should be discarded because it belongs
3503 * to an old epoch, but not because its length is shorter than
3504 * the minimum record length for packets using the new record transform.
3505 * Note that these two kinds of failures are handled differently,
3506 * as an unexpected record is silently skipped but an invalid
3507 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003508 */
3509#if defined(MBEDTLS_SSL_PROTO_DTLS)
3510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3511 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003512 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003513
Hanno Becker955a5c92019-07-10 17:12:07 +01003514 /* Check that the datagram is large enough to contain a record
3515 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003516 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003517 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3519 (unsigned) len,
3520 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003521 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3522 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003523
Hanno Becker37cfe732019-07-10 17:20:01 +01003524 /* Records from other, non-matching epochs are silently discarded.
3525 * (The case of same-port Client reconnects must be considered in
3526 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003527 if( rec_epoch != ssl->in_epoch )
3528 {
3529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003530 "expected %u, received %lu",
3531 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003532
Hanno Becker552f7472019-07-19 10:59:12 +01003533 /* Records from the next epoch are considered for buffering
3534 * (concretely: early Finished messages). */
3535 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003536 {
Hanno Becker552f7472019-07-19 10:59:12 +01003537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3538 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003539 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003540
Hanno Becker2fddd372019-07-10 14:37:41 +01003541 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003542 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003544 /* For records from the correct epoch, check whether their
3545 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003546 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3547 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003548 {
3549 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3550 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3551 }
3552#endif
3553 }
3554#endif /* MBEDTLS_SSL_PROTO_DTLS */
3555
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003556 return( 0 );
3557}
Paul Bakker5121ce52009-01-03 21:22:43 +00003558
Paul Bakker5121ce52009-01-03 21:22:43 +00003559
Hanno Becker2fddd372019-07-10 14:37:41 +01003560#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3561static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3562{
3563 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3564
3565 /*
3566 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3567 * access the first byte of record content (handshake type), as we
3568 * have an active transform (possibly iv_len != 0), so use the
3569 * fact that the record header len is 13 instead.
3570 */
3571 if( rec_epoch == 0 &&
3572 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3573 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3574 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3575 ssl->in_left > 13 &&
3576 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3577 {
3578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3579 "from the same port" ) );
3580 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 }
3582
3583 return( 0 );
3584}
Hanno Becker2fddd372019-07-10 14:37:41 +01003585#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003586
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003587/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003588 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003589 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003590static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3591 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003592{
3593 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003596 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597
Paul Bakker48916f92012-09-16 19:57:18 +00003598 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003600 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003601
Hanno Beckera18d1322018-01-03 14:27:32 +00003602 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003603 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003605 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003606
Hanno Beckera0e20d02019-05-15 14:03:01 +01003607#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003608 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3609 ssl->conf->ignore_unexpected_cid
3610 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3611 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003613 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003614 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003615#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003616
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 return( ret );
3618 }
3619
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003620 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003621 {
3622 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003623 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003624 }
3625
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003626 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003627 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003628
Hanno Beckera0e20d02019-05-15 14:03:01 +01003629#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003630 /* We have already checked the record content type
3631 * in ssl_parse_record_header(), failing or silently
3632 * dropping the record in the case of an unknown type.
3633 *
3634 * Since with the use of CIDs, the record content type
3635 * might change during decryption, re-check the record
3636 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003637 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003638 {
3639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3640 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3641 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003642#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003643
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003644 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003645 {
3646#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3647 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003648 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003649 {
3650 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3652 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3653 }
3654#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3655
3656 ssl->nb_zero++;
3657
3658 /*
3659 * Three or more empty messages may be a DoS attack
3660 * (excessive CPU consumption).
3661 */
3662 if( ssl->nb_zero > 3 )
3663 {
3664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003665 "messages, possible DoS attack" ) );
3666 /* Treat the records as if they were not properly authenticated,
3667 * thereby failing the connection if we see more than allowed
3668 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003669 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3670 }
3671 }
3672 else
3673 ssl->nb_zero = 0;
3674
3675#if defined(MBEDTLS_SSL_PROTO_DTLS)
3676 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3677 {
3678 ; /* in_ctr read from peer, not maintained internally */
3679 }
3680 else
3681#endif
3682 {
3683 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003684 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003685 if( ++ssl->in_ctr[i - 1] != 0 )
3686 break;
3687
3688 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003689 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003690 {
3691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3692 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3693 }
3694 }
3695
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 }
3697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003699 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003702 }
3703#endif
3704
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003705 /* Check actual (decrypted) record content length against
3706 * configured maximum. */
3707 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3708 {
3709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3710 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3711 }
3712
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003713 return( 0 );
3714}
3715
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003716/*
3717 * Read a record.
3718 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003719 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3720 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3721 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003722 */
Hanno Becker1097b342018-08-15 14:09:41 +01003723
3724/* Helper functions for mbedtls_ssl_read_record(). */
3725static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003726static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3727static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003728
Hanno Becker327c93b2018-08-15 13:56:18 +01003729int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003730 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003731{
Janos Follath865b3eb2019-12-16 11:46:15 +00003732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003735
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003736 if( ssl->keep_current_message == 0 )
3737 {
3738 do {
Simon Butcher99000142016-10-13 17:21:01 +01003739
Hanno Becker26994592018-08-15 14:14:59 +01003740 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003741 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003742 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003743
Hanno Beckere74d5562018-08-15 14:26:08 +01003744 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003745 {
Hanno Becker40f50842018-08-15 14:48:01 +01003746#if defined(MBEDTLS_SSL_PROTO_DTLS)
3747 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003748
Hanno Becker40f50842018-08-15 14:48:01 +01003749 /* We only check for buffered messages if the
3750 * current datagram is fully consumed. */
3751 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003752 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003753 {
Hanno Becker40f50842018-08-15 14:48:01 +01003754 if( ssl_load_buffered_message( ssl ) == 0 )
3755 have_buffered = 1;
3756 }
3757
3758 if( have_buffered == 0 )
3759#endif /* MBEDTLS_SSL_PROTO_DTLS */
3760 {
3761 ret = ssl_get_next_record( ssl );
3762 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3763 continue;
3764
3765 if( ret != 0 )
3766 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003767 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003768 return( ret );
3769 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003770 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003771 }
3772
3773 ret = mbedtls_ssl_handle_message_type( ssl );
3774
Hanno Becker40f50842018-08-15 14:48:01 +01003775#if defined(MBEDTLS_SSL_PROTO_DTLS)
3776 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3777 {
3778 /* Buffer future message */
3779 ret = ssl_buffer_message( ssl );
3780 if( ret != 0 )
3781 return( ret );
3782
3783 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3784 }
3785#endif /* MBEDTLS_SSL_PROTO_DTLS */
3786
Hanno Becker90333da2017-10-10 11:27:13 +01003787 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3788 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003789
3790 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003791 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003792 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003793 return( ret );
3794 }
3795
Hanno Becker327c93b2018-08-15 13:56:18 +01003796 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003797 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003798 {
3799 mbedtls_ssl_update_handshake_status( ssl );
3800 }
Simon Butcher99000142016-10-13 17:21:01 +01003801 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003802 else
Simon Butcher99000142016-10-13 17:21:01 +01003803 {
Hanno Becker02f59072018-08-15 14:00:24 +01003804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003805 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003806 }
3807
3808 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3809
3810 return( 0 );
3811}
3812
Hanno Becker40f50842018-08-15 14:48:01 +01003813#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003814static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003815{
Hanno Becker40f50842018-08-15 14:48:01 +01003816 if( ssl->in_left > ssl->next_record_offset )
3817 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003818
Hanno Becker40f50842018-08-15 14:48:01 +01003819 return( 0 );
3820}
3821
3822static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3823{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003824 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003825 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003826 int ret = 0;
3827
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003828 if( hs == NULL )
3829 return( -1 );
3830
Hanno Beckere00ae372018-08-20 09:39:42 +01003831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3832
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003833 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3834 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3835 {
3836 /* Check if we have seen a ChangeCipherSpec before.
3837 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003838 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003839 {
3840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3841 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003842 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003843 }
3844
Hanno Becker39b8bc92018-08-28 17:17:13 +01003845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003846 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3847 ssl->in_msglen = 1;
3848 ssl->in_msg[0] = 1;
3849
3850 /* As long as they are equal, the exact value doesn't matter. */
3851 ssl->in_left = 0;
3852 ssl->next_record_offset = 0;
3853
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003854 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003855 goto exit;
3856 }
Hanno Becker37f95322018-08-16 13:55:32 +01003857
Hanno Beckerb8f50142018-08-28 10:01:34 +01003858#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003859 /* Debug only */
3860 {
3861 unsigned offset;
3862 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3863 {
3864 hs_buf = &hs->buffering.hs[offset];
3865 if( hs_buf->is_valid == 1 )
3866 {
3867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3868 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003869 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003870 }
3871 }
3872 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003873#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003874
3875 /* Check if we have buffered and/or fully reassembled the
3876 * next handshake message. */
3877 hs_buf = &hs->buffering.hs[0];
3878 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3879 {
3880 /* Synthesize a record containing the buffered HS message. */
3881 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3882 ( hs_buf->data[2] << 8 ) |
3883 hs_buf->data[3];
3884
3885 /* Double-check that we haven't accidentally buffered
3886 * a message that doesn't fit into the input buffer. */
3887 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3888 {
3889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3891 }
3892
3893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3894 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3895 hs_buf->data, msg_len + 12 );
3896
3897 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3898 ssl->in_hslen = msg_len + 12;
3899 ssl->in_msglen = msg_len + 12;
3900 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3901
3902 ret = 0;
3903 goto exit;
3904 }
3905 else
3906 {
3907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3908 hs->in_msg_seq ) );
3909 }
3910
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003911 ret = -1;
3912
3913exit:
3914
3915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3916 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003917}
3918
Hanno Beckera02b0b42018-08-21 17:20:27 +01003919static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3920 size_t desired )
3921{
3922 int offset;
3923 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3925 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003926
Hanno Becker01315ea2018-08-21 17:22:17 +01003927 /* Get rid of future records epoch first, if such exist. */
3928 ssl_free_buffered_record( ssl );
3929
3930 /* Check if we have enough space available now. */
3931 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3932 hs->buffering.total_bytes_buffered ) )
3933 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003934 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003935 return( 0 );
3936 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003937
Hanno Becker4f432ad2018-08-28 10:02:32 +01003938 /* We don't have enough space to buffer the next expected handshake
3939 * message. Remove buffers used for future messages to gain space,
3940 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003941 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3942 offset >= 0; offset-- )
3943 {
3944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3945 offset ) );
3946
Hanno Beckerb309b922018-08-23 13:18:05 +01003947 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003948
3949 /* Check if we have enough space available now. */
3950 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3951 hs->buffering.total_bytes_buffered ) )
3952 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003954 return( 0 );
3955 }
3956 }
3957
3958 return( -1 );
3959}
3960
Hanno Becker40f50842018-08-15 14:48:01 +01003961static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3962{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003963 int ret = 0;
3964 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3965
3966 if( hs == NULL )
3967 return( 0 );
3968
3969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3970
3971 switch( ssl->in_msgtype )
3972 {
3973 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003975
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003976 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003977 break;
3978
3979 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003980 {
3981 unsigned recv_msg_seq_offset;
3982 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3983 mbedtls_ssl_hs_buffer *hs_buf;
3984 size_t msg_len = ssl->in_hslen - 12;
3985
3986 /* We should never receive an old handshake
3987 * message - double-check nonetheless. */
3988 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3989 {
3990 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3991 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3992 }
3993
3994 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3995 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3996 {
3997 /* Silently ignore -- message too far in the future */
3998 MBEDTLS_SSL_DEBUG_MSG( 2,
3999 ( "Ignore future HS message with sequence number %u, "
4000 "buffering window %u - %u",
4001 recv_msg_seq, ssl->handshake->in_msg_seq,
4002 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4003
4004 goto exit;
4005 }
4006
4007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4008 recv_msg_seq, recv_msg_seq_offset ) );
4009
4010 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4011
4012 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004013 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004014 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004015 size_t reassembly_buf_sz;
4016
Hanno Becker37f95322018-08-16 13:55:32 +01004017 hs_buf->is_fragmented =
4018 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4019
4020 /* We copy the message back into the input buffer
4021 * after reassembly, so check that it's not too large.
4022 * This is an implementation-specific limitation
4023 * and not one from the standard, hence it is not
4024 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004025 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004026 {
4027 /* Ignore message */
4028 goto exit;
4029 }
4030
Hanno Beckere0b150f2018-08-21 15:51:03 +01004031 /* Check if we have enough space to buffer the message. */
4032 if( hs->buffering.total_bytes_buffered >
4033 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4034 {
4035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4036 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4037 }
4038
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004039 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4040 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004041
4042 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4043 hs->buffering.total_bytes_buffered ) )
4044 {
4045 if( recv_msg_seq_offset > 0 )
4046 {
4047 /* If we can't buffer a future message because
4048 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4050 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4051 " (already %" MBEDTLS_PRINTF_SIZET
4052 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004053 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004054 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004055 goto exit;
4056 }
Hanno Beckere1801392018-08-21 16:51:05 +01004057 else
4058 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4060 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4061 " (already %" MBEDTLS_PRINTF_SIZET
4062 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004063 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004064 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01004065 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004066
Hanno Beckera02b0b42018-08-21 17:20:27 +01004067 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004068 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
4070 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
4071 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
4072 " (already %" MBEDTLS_PRINTF_SIZET
4073 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00004074 msg_len,
4075 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00004076 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004077 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004078 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4079 goto exit;
4080 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004081 }
4082
Paul Elliottd48d5c62021-01-07 14:47:05 +00004083 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004084 msg_len ) );
4085
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004086 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4087 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004088 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004089 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004090 goto exit;
4091 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004092 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004093
4094 /* Prepare final header: copy msg_type, length and message_seq,
4095 * then add standardised fragment_offset and fragment_length */
4096 memcpy( hs_buf->data, ssl->in_msg, 6 );
4097 memset( hs_buf->data + 6, 0, 3 );
4098 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4099
4100 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004101
4102 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004103 }
4104 else
4105 {
4106 /* Make sure msg_type and length are consistent */
4107 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4108 {
4109 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4110 /* Ignore */
4111 goto exit;
4112 }
4113 }
4114
Hanno Becker4422bbb2018-08-20 09:40:19 +01004115 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004116 {
4117 size_t frag_len, frag_off;
4118 unsigned char * const msg = hs_buf->data + 12;
4119
4120 /*
4121 * Check and copy current fragment
4122 */
4123
4124 /* Validation of header fields already done in
4125 * mbedtls_ssl_prepare_handshake_record(). */
4126 frag_off = ssl_get_hs_frag_off( ssl );
4127 frag_len = ssl_get_hs_frag_len( ssl );
4128
Paul Elliottd48d5c62021-01-07 14:47:05 +00004129 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4130 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004131 frag_off, frag_len ) );
4132 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4133
4134 if( hs_buf->is_fragmented )
4135 {
4136 unsigned char * const bitmask = msg + msg_len;
4137 ssl_bitmask_set( bitmask, frag_off, frag_len );
4138 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4139 msg_len ) == 0 );
4140 }
4141 else
4142 {
4143 hs_buf->is_complete = 1;
4144 }
4145
4146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4147 hs_buf->is_complete ? "" : "not yet " ) );
4148 }
4149
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004150 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004151 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004152
4153 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004154 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004155 break;
4156 }
4157
4158exit:
4159
4160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4161 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004162}
4163#endif /* MBEDTLS_SSL_PROTO_DTLS */
4164
Hanno Becker1097b342018-08-15 14:09:41 +01004165static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004166{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004167 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004168 * Consume last content-layer message and potentially
4169 * update in_msglen which keeps track of the contents'
4170 * consumption state.
4171 *
4172 * (1) Handshake messages:
4173 * Remove last handshake message, move content
4174 * and adapt in_msglen.
4175 *
4176 * (2) Alert messages:
4177 * Consume whole record content, in_msglen = 0.
4178 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004179 * (3) Change cipher spec:
4180 * Consume whole record content, in_msglen = 0.
4181 *
4182 * (4) Application data:
4183 * Don't do anything - the record layer provides
4184 * the application data as a stream transport
4185 * and consumes through mbedtls_ssl_read only.
4186 *
4187 */
4188
4189 /* Case (1): Handshake messages */
4190 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004191 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004192 /* Hard assertion to be sure that no application data
4193 * is in flight, as corrupting ssl->in_msglen during
4194 * ssl->in_offt != NULL is fatal. */
4195 if( ssl->in_offt != NULL )
4196 {
4197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4198 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4199 }
4200
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004201 /*
4202 * Get next Handshake message in the current record
4203 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004204
Hanno Becker4a810fb2017-05-24 16:27:30 +01004205 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004206 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004207 * current handshake content: If DTLS handshake
4208 * fragmentation is used, that's the fragment
4209 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004210 * size here is faulty and should be changed at
4211 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004212 * (2) While it doesn't seem to cause problems, one
4213 * has to be very careful not to assume that in_hslen
4214 * is always <= in_msglen in a sensible communication.
4215 * Again, it's wrong for DTLS handshake fragmentation.
4216 * The following check is therefore mandatory, and
4217 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004218 * Additionally, ssl->in_hslen might be arbitrarily out of
4219 * bounds after handling a DTLS message with an unexpected
4220 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004221 */
4222 if( ssl->in_hslen < ssl->in_msglen )
4223 {
4224 ssl->in_msglen -= ssl->in_hslen;
4225 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4226 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004227
Hanno Becker4a810fb2017-05-24 16:27:30 +01004228 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4229 ssl->in_msg, ssl->in_msglen );
4230 }
4231 else
4232 {
4233 ssl->in_msglen = 0;
4234 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004235
Hanno Becker4a810fb2017-05-24 16:27:30 +01004236 ssl->in_hslen = 0;
4237 }
4238 /* Case (4): Application data */
4239 else if( ssl->in_offt != NULL )
4240 {
4241 return( 0 );
4242 }
4243 /* Everything else (CCS & Alerts) */
4244 else
4245 {
4246 ssl->in_msglen = 0;
4247 }
4248
Hanno Becker1097b342018-08-15 14:09:41 +01004249 return( 0 );
4250}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004251
Hanno Beckere74d5562018-08-15 14:26:08 +01004252static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4253{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004254 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004255 return( 1 );
4256
4257 return( 0 );
4258}
4259
Hanno Becker5f066e72018-08-16 14:56:31 +01004260#if defined(MBEDTLS_SSL_PROTO_DTLS)
4261
4262static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4263{
4264 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4265 if( hs == NULL )
4266 return;
4267
Hanno Becker01315ea2018-08-21 17:22:17 +01004268 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004269 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004270 hs->buffering.total_bytes_buffered -=
4271 hs->buffering.future_record.len;
4272
4273 mbedtls_free( hs->buffering.future_record.data );
4274 hs->buffering.future_record.data = NULL;
4275 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004276}
4277
4278static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4279{
4280 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4281 unsigned char * rec;
4282 size_t rec_len;
4283 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004284#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4285 size_t in_buf_len = ssl->in_buf_len;
4286#else
4287 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4288#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004289 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4290 return( 0 );
4291
4292 if( hs == NULL )
4293 return( 0 );
4294
Hanno Becker5f066e72018-08-16 14:56:31 +01004295 rec = hs->buffering.future_record.data;
4296 rec_len = hs->buffering.future_record.len;
4297 rec_epoch = hs->buffering.future_record.epoch;
4298
4299 if( rec == NULL )
4300 return( 0 );
4301
Hanno Becker4cb782d2018-08-20 11:19:05 +01004302 /* Only consider loading future records if the
4303 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004304 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004305 return( 0 );
4306
Hanno Becker5f066e72018-08-16 14:56:31 +01004307 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4308
4309 if( rec_epoch != ssl->in_epoch )
4310 {
4311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4312 goto exit;
4313 }
4314
4315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4316
4317 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004318 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004319 {
4320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4321 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4322 }
4323
4324 memcpy( ssl->in_hdr, rec, rec_len );
4325 ssl->in_left = rec_len;
4326 ssl->next_record_offset = 0;
4327
4328 ssl_free_buffered_record( ssl );
4329
4330exit:
4331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4332 return( 0 );
4333}
4334
Hanno Becker519f15d2019-07-11 12:43:20 +01004335static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4336 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004337{
4338 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004339
4340 /* Don't buffer future records outside handshakes. */
4341 if( hs == NULL )
4342 return( 0 );
4343
4344 /* Only buffer handshake records (we are only interested
4345 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004346 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004347 return( 0 );
4348
4349 /* Don't buffer more than one future epoch record. */
4350 if( hs->buffering.future_record.data != NULL )
4351 return( 0 );
4352
Hanno Becker01315ea2018-08-21 17:22:17 +01004353 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004354 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004355 hs->buffering.total_bytes_buffered ) )
4356 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4358 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4359 " (already %" MBEDTLS_PRINTF_SIZET
4360 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004361 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004362 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004363 return( 0 );
4364 }
4365
Hanno Becker5f066e72018-08-16 14:56:31 +01004366 /* Buffer record */
4367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004368 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004369 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004370
4371 /* ssl_parse_record_header() only considers records
4372 * of the next epoch as candidates for buffering. */
4373 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004374 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004375
4376 hs->buffering.future_record.data =
4377 mbedtls_calloc( 1, hs->buffering.future_record.len );
4378 if( hs->buffering.future_record.data == NULL )
4379 {
4380 /* If we run out of RAM trying to buffer a
4381 * record from the next epoch, just ignore. */
4382 return( 0 );
4383 }
4384
Hanno Becker519f15d2019-07-11 12:43:20 +01004385 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004386
Hanno Becker519f15d2019-07-11 12:43:20 +01004387 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004388 return( 0 );
4389}
4390
4391#endif /* MBEDTLS_SSL_PROTO_DTLS */
4392
Hanno Beckere74d5562018-08-15 14:26:08 +01004393static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004394{
Janos Follath865b3eb2019-12-16 11:46:15 +00004395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004396 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004397
Hanno Becker5f066e72018-08-16 14:56:31 +01004398#if defined(MBEDTLS_SSL_PROTO_DTLS)
4399 /* We might have buffered a future record; if so,
4400 * and if the epoch matches now, load it.
4401 * On success, this call will set ssl->in_left to
4402 * the length of the buffered record, so that
4403 * the calls to ssl_fetch_input() below will
4404 * essentially be no-ops. */
4405 ret = ssl_load_buffered_record( ssl );
4406 if( ret != 0 )
4407 return( ret );
4408#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004409
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004410 /* Ensure that we have enough space available for the default form
4411 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4412 * with no space for CIDs counted in). */
4413 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4414 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004417 return( ret );
4418 }
4419
Hanno Beckere5e7e782019-07-11 12:29:35 +01004420 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4421 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004425 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004426 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4427 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004428 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004429 if( ret != 0 )
4430 return( ret );
4431
4432 /* Fall through to handling of unexpected records */
4433 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4434 }
4435
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004436 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4437 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004438#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004439 /* Reset in pointers to default state for TLS/DTLS records,
4440 * assuming no CID and no offset between record content and
4441 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004442 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004443
Hanno Becker7ae20e02019-07-12 08:33:49 +01004444 /* Setup internal message pointers from record structure. */
4445 ssl->in_msgtype = rec.type;
4446#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4447 ssl->in_len = ssl->in_cid + rec.cid_len;
4448#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4449 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4450 ssl->in_msglen = rec.data_len;
4451
Hanno Becker2fddd372019-07-10 14:37:41 +01004452 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004453 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004454 if( ret != 0 )
4455 return( ret );
4456#endif
4457
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004458 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004459 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004460
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4462 "(header)" ) );
4463 }
4464 else
4465 {
4466 /* Skip invalid record and the rest of the datagram */
4467 ssl->next_record_offset = 0;
4468 ssl->in_left = 0;
4469
4470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4471 "(header)" ) );
4472 }
4473
4474 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004475 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004476 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004477 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004478#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004479 {
4480 return( ret );
4481 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004482 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004486 {
Hanno Beckera8814792019-07-10 15:01:45 +01004487 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004488 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004489 if( ssl->next_record_offset < ssl->in_left )
4490 {
4491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4492 }
4493 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004494 else
4495#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004496 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004497 /*
4498 * Fetch record contents from underlying transport.
4499 */
Hanno Beckera3175662019-07-11 12:50:29 +01004500 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004501 if( ret != 0 )
4502 {
4503 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4504 return( ret );
4505 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004506
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004507 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004508 }
4509
4510 /*
4511 * Decrypt record contents.
4512 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004513
Hanno Beckerfdf66042019-07-11 13:07:45 +01004514 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004518 {
4519 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004520 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004521 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004522 /* Except when waiting for Finished as a bad mac here
4523 * probably means something went wrong in the handshake
4524 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4525 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4526 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4527 {
4528#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4529 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4530 {
4531 mbedtls_ssl_send_alert_message( ssl,
4532 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4533 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4534 }
4535#endif
4536 return( ret );
4537 }
4538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004539#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004540 if( ssl->conf->badmac_limit != 0 &&
4541 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004542 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4544 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004545 }
4546#endif
4547
Hanno Becker4a810fb2017-05-24 16:27:30 +01004548 /* As above, invalid records cause
4549 * dismissal of the whole datagram. */
4550
4551 ssl->next_record_offset = 0;
4552 ssl->in_left = 0;
4553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004555 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004556 }
4557
4558 return( ret );
4559 }
4560 else
4561#endif
4562 {
4563 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4565 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 mbedtls_ssl_send_alert_message( ssl,
4568 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4569 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004570 }
4571#endif
4572 return( ret );
4573 }
4574 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004575
Hanno Becker44d89b22019-07-12 09:40:44 +01004576
4577 /* Reset in pointers to default state for TLS/DTLS records,
4578 * assuming no CID and no offset between record content and
4579 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004580 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004581#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4582 ssl->in_len = ssl->in_cid + rec.cid_len;
4583#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004584 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004585
Hanno Becker8685c822019-07-12 09:37:30 +01004586 /* The record content type may change during decryption,
4587 * so re-read it. */
4588 ssl->in_msgtype = rec.type;
4589 /* Also update the input buffer, because unfortunately
4590 * the server-side ssl_parse_client_hello() reparses the
4591 * record header when receiving a ClientHello initiating
4592 * a renegotiation. */
4593 ssl->in_hdr[0] = rec.type;
4594 ssl->in_msg = rec.buf + rec.data_offset;
4595 ssl->in_msglen = rec.data_len;
4596 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4597 ssl->in_len[1] = (unsigned char)( rec.data_len );
4598
Simon Butcher99000142016-10-13 17:21:01 +01004599 return( 0 );
4600}
4601
4602int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4603{
Janos Follath865b3eb2019-12-16 11:46:15 +00004604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004605
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004606 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004607 * Handle particular types of records
4608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004610 {
Simon Butcher99000142016-10-13 17:21:01 +01004611 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4612 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004613 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 }
4616
Hanno Beckere678eaa2018-08-21 14:57:46 +01004617 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004618 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004619 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004620 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004622 ssl->in_msglen ) );
4623 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004624 }
4625
Hanno Beckere678eaa2018-08-21 14:57:46 +01004626 if( ssl->in_msg[0] != 1 )
4627 {
4628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4629 ssl->in_msg[0] ) );
4630 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4631 }
4632
4633#if defined(MBEDTLS_SSL_PROTO_DTLS)
4634 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4635 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4636 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4637 {
4638 if( ssl->handshake == NULL )
4639 {
4640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4641 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4642 }
4643
4644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4645 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4646 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004647#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004648 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004652 if( ssl->in_msglen != 2 )
4653 {
4654 /* Note: Standard allows for more than one 2 byte alert
4655 to be packed in a single message, but Mbed TLS doesn't
4656 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004658 ssl->in_msglen ) );
4659 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4660 }
4661
Paul Elliott9f352112020-12-09 14:55:45 +00004662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 ssl->in_msg[0], ssl->in_msg[1] ) );
4664
4665 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004666 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004668 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004671 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 }
4674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4676 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4679 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004680 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004681
4682#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4683 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4684 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4685 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004687 /* Will be handled when trying to parse ServerHello */
4688 return( 0 );
4689 }
4690#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004691 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004692 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 }
4694
Hanno Beckerc76c6192017-06-06 10:03:17 +01004695#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004697 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004698 /* Drop unexpected ApplicationData records,
4699 * except at the beginning of renegotiations */
4700 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4701 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4702#if defined(MBEDTLS_SSL_RENEGOTIATION)
4703 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4704 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004705#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004706 )
4707 {
4708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4709 return( MBEDTLS_ERR_SSL_NON_FATAL );
4710 }
4711
4712 if( ssl->handshake != NULL &&
4713 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4714 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004715 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004716 }
4717 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004718#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004719
Paul Bakker5121ce52009-01-03 21:22:43 +00004720 return( 0 );
4721}
4722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004724{
irwir6c0da642019-09-26 21:07:41 +03004725 return( mbedtls_ssl_send_alert_message( ssl,
4726 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4727 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004728}
4729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004730int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004731 unsigned char level,
4732 unsigned char message )
4733{
Janos Follath865b3eb2019-12-16 11:46:15 +00004734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004735
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004736 if( ssl == NULL || ssl->conf == NULL )
4737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004742 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004743 ssl->out_msglen = 2;
4744 ssl->out_msg[0] = level;
4745 ssl->out_msg[1] = message;
4746
Hanno Becker67bc7c32018-08-06 11:33:50 +01004747 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004750 return( ret );
4751 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004753
4754 return( 0 );
4755}
4756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004757int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004758{
Janos Follath865b3eb2019-12-16 11:46:15 +00004759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004763 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004764 ssl->out_msglen = 1;
4765 ssl->out_msg[0] = 1;
4766
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 ssl->state++;
4768
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004769 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004770 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004772 return( ret );
4773 }
4774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004776
4777 return( 0 );
4778}
4779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004780int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004781{
Janos Follath865b3eb2019-12-16 11:46:15 +00004782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004785
Hanno Becker327c93b2018-08-15 13:56:18 +01004786 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004789 return( ret );
4790 }
4791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004792 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004795 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4796 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004797 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004798 }
4799
Hanno Beckere678eaa2018-08-21 14:57:46 +01004800 /* CCS records are only accepted if they have length 1 and content '1',
4801 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004802
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004803 /*
4804 * Switch to our negotiated transform and session parameters for inbound
4805 * data.
4806 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004808 ssl->transform_in = ssl->transform_negotiate;
4809 ssl->session_in = ssl->session_negotiate;
4810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004814#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004815 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004816#endif
4817
4818 /* Increment epoch */
4819 if( ++ssl->in_epoch == 0 )
4820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004822 /* This is highly unlikely to happen for legitimate reasons, so
4823 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004824 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004825 }
4826 }
4827 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004828#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004829 memset( ssl->in_ctr, 0, 8 );
4830
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004831 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004832
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 ssl->state++;
4834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004836
4837 return( 0 );
4838}
4839
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004840/* Once ssl->out_hdr as the address of the beginning of the
4841 * next outgoing record is set, deduce the other pointers.
4842 *
4843 * Note: For TLS, we save the implicit record sequence number
4844 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4845 * and the caller has to make sure there's space for this.
4846 */
4847
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004848static size_t ssl_transform_get_explicit_iv_len(
4849 mbedtls_ssl_transform const *transform )
4850{
4851 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
4852 return( 0 );
4853
4854 return( transform->ivlen - transform->fixed_ivlen );
4855}
4856
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004857void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4858 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004859{
4860#if defined(MBEDTLS_SSL_PROTO_DTLS)
4861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4862 {
4863 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004864#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004865 ssl->out_cid = ssl->out_ctr + 8;
4866 ssl->out_len = ssl->out_cid;
4867 if( transform != NULL )
4868 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004869#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004870 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004871#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004872 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004873 }
4874 else
4875#endif
4876 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004877 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004878#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004879 ssl->out_cid = ssl->out_len;
4880#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004881 ssl->out_iv = ssl->out_hdr + 5;
4882 }
4883
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004884 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004885 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004886 if( transform != NULL )
4887 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004888}
4889
4890/* Once ssl->in_hdr as the address of the beginning of the
4891 * next incoming record is set, deduce the other pointers.
4892 *
4893 * Note: For TLS, we save the implicit record sequence number
4894 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4895 * and the caller has to make sure there's space for this.
4896 */
4897
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004898void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004899{
Hanno Becker79594fd2019-05-08 09:38:41 +01004900 /* This function sets the pointers to match the case
4901 * of unprotected TLS/DTLS records, with both ssl->in_iv
4902 * and ssl->in_msg pointing to the beginning of the record
4903 * content.
4904 *
4905 * When decrypting a protected record, ssl->in_msg
4906 * will be shifted to point to the beginning of the
4907 * record plaintext.
4908 */
4909
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004910#if defined(MBEDTLS_SSL_PROTO_DTLS)
4911 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4912 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004913 /* This sets the header pointers to match records
4914 * without CID. When we receive a record containing
4915 * a CID, the fields are shifted accordingly in
4916 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004917 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004918#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004919 ssl->in_cid = ssl->in_ctr + 8;
4920 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004921#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004922 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004923#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004924 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004925 }
4926 else
4927#endif
4928 {
4929 ssl->in_ctr = ssl->in_hdr - 8;
4930 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004931#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004932 ssl->in_cid = ssl->in_len;
4933#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004934 ssl->in_iv = ssl->in_hdr + 5;
4935 }
4936
Hanno Becker79594fd2019-05-08 09:38:41 +01004937 /* This will be adjusted at record decryption time. */
4938 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004939}
4940
Paul Bakker5121ce52009-01-03 21:22:43 +00004941/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004942 * Setup an SSL context
4943 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004944
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004945void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004946{
4947 /* Set the incoming and outgoing record pointers. */
4948#if defined(MBEDTLS_SSL_PROTO_DTLS)
4949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4950 {
4951 ssl->out_hdr = ssl->out_buf;
4952 ssl->in_hdr = ssl->in_buf;
4953 }
4954 else
4955#endif /* MBEDTLS_SSL_PROTO_DTLS */
4956 {
Hanno Becker12078f42021-03-02 15:28:41 +00004957 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004958 ssl->out_hdr = ssl->out_buf + 8;
4959 ssl->in_hdr = ssl->in_buf + 8;
4960 }
4961
4962 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004963 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4964 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004965}
4966
Paul Bakker5121ce52009-01-03 21:22:43 +00004967/*
4968 * SSL get accessors
4969 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004970size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004971{
4972 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4973}
4974
Hanno Becker8b170a02017-10-10 11:51:19 +01004975int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4976{
4977 /*
4978 * Case A: We're currently holding back
4979 * a message for further processing.
4980 */
4981
4982 if( ssl->keep_current_message == 1 )
4983 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004985 return( 1 );
4986 }
4987
4988 /*
4989 * Case B: Further records are pending in the current datagram.
4990 */
4991
4992#if defined(MBEDTLS_SSL_PROTO_DTLS)
4993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4994 ssl->in_left > ssl->next_record_offset )
4995 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004997 return( 1 );
4998 }
4999#endif /* MBEDTLS_SSL_PROTO_DTLS */
5000
5001 /*
5002 * Case C: A handshake message is being processed.
5003 */
5004
Hanno Becker8b170a02017-10-10 11:51:19 +01005005 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5006 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005007 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005008 return( 1 );
5009 }
5010
5011 /*
5012 * Case D: An application data message is being processed
5013 */
5014 if( ssl->in_offt != NULL )
5015 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005016 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005017 return( 1 );
5018 }
5019
5020 /*
5021 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005022 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005023 * we implement support for multiple alerts in single records.
5024 */
5025
5026 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5027 return( 0 );
5028}
5029
Paul Bakker43ca69c2011-01-15 17:35:19 +00005030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005032{
Hanno Becker3136ede2018-08-17 15:28:19 +01005033 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005034 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005035 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005036
Hanno Becker5903de42019-05-03 14:46:38 +01005037 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5038
Hanno Becker78640902018-08-13 16:35:15 +01005039 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005040 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005042 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044 case MBEDTLS_MODE_GCM:
5045 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005046 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005048 transform_expansion = transform->minlen;
5049 break;
5050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005052
5053 block_size = mbedtls_cipher_get_block_size(
5054 &transform->cipher_ctx_enc );
5055
Hanno Becker3136ede2018-08-17 15:28:19 +01005056 /* Expansion due to the addition of the MAC. */
5057 transform_expansion += transform->maclen;
5058
5059 /* Expansion due to the addition of CBC padding;
5060 * Theoretically up to 256 bytes, but we never use
5061 * more than the block size of the underlying cipher. */
5062 transform_expansion += block_size;
5063
5064 /* For TLS 1.1 or higher, an explicit IV is added
5065 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005066#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5067 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005068 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005069#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005070
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005071 break;
5072
5073 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005076 }
5077
Hanno Beckera0e20d02019-05-15 14:03:01 +01005078#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005079 if( transform->out_cid_len != 0 )
5080 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005081#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005082
Hanno Becker5903de42019-05-03 14:46:38 +01005083 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005084}
5085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005087/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005088 * Check record counters and renegotiate if they're above the limit.
5089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005090static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005091{
Hanno Beckerdd772292020-02-05 10:38:31 +00005092 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005093 int in_ctr_cmp;
5094 int out_ctr_cmp;
5095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005096 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5097 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005098 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005099 {
5100 return( 0 );
5101 }
5102
Andres AG2196c7f2016-12-15 17:01:16 +00005103 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5104 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005105 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005106 ssl->conf->renego_period + ep_len, 8 - ep_len );
5107
5108 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005109 {
5110 return( 0 );
5111 }
5112
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005114 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005115}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005117
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005118/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005119 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005120 * may only be sent for the purpose of initiating renegotiations.
5121 *
5122 * This function is introduced as a separate helper since the handling
5123 * of post-handshake handshake messages changes significantly in TLS 1.3,
5124 * and having a helper function allows to distinguish between TLS <= 1.2 and
5125 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5126 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00005127static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005128{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005130
5131 /*
5132 * - For client-side, expect SERVER_HELLO_REQUEST.
5133 * - For server-side, expect CLIENT_HELLO.
5134 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5135 */
5136
5137#if defined(MBEDTLS_SSL_CLI_C)
5138 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5139 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5140 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
5141 {
5142 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
5143
5144 /* With DTLS, drop the packet (probably from last handshake) */
5145#if defined(MBEDTLS_SSL_PROTO_DTLS)
5146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5147 {
5148 return( 0 );
5149 }
5150#endif
5151 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5152 }
5153#endif /* MBEDTLS_SSL_CLI_C */
5154
5155#if defined(MBEDTLS_SSL_SRV_C)
5156 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5157 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
5158 {
5159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5160
5161 /* With DTLS, drop the packet (probably from last handshake) */
5162#if defined(MBEDTLS_SSL_PROTO_DTLS)
5163 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5164 {
5165 return( 0 );
5166 }
5167#endif
5168 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5169 }
5170#endif /* MBEDTLS_SSL_SRV_C */
5171
5172#if defined(MBEDTLS_SSL_RENEGOTIATION)
5173 /* Determine whether renegotiation attempt should be accepted */
5174 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5175 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5176 ssl->conf->allow_legacy_renegotiation ==
5177 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5178 {
5179 /*
5180 * Accept renegotiation request
5181 */
5182
5183 /* DTLS clients need to know renego is server-initiated */
5184#if defined(MBEDTLS_SSL_PROTO_DTLS)
5185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5186 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5187 {
5188 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5189 }
5190#endif
5191 ret = mbedtls_ssl_start_renegotiation( ssl );
5192 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5193 ret != 0 )
5194 {
5195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5196 ret );
5197 return( ret );
5198 }
5199 }
5200 else
5201#endif /* MBEDTLS_SSL_RENEGOTIATION */
5202 {
5203 /*
5204 * Refuse renegotiation
5205 */
5206
5207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5208
5209#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5210 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5211 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
5212 {
5213 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5214 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5215 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5216 {
5217 return( ret );
5218 }
5219 }
5220 else
5221#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5222 MBEDTLS_SSL_PROTO_TLS1_2 */
5223 {
5224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5225 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5226 }
5227 }
5228
5229 return( 0 );
5230}
5231
Paul Bakker48916f92012-09-16 19:57:18 +00005232/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005233 * Receive application data decrypted from the SSL layer
5234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005236{
Janos Follath865b3eb2019-12-16 11:46:15 +00005237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005238 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005239
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005240 if( ssl == NULL || ssl->conf == NULL )
5241 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005248 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005249 return( ret );
5250
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005251 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005253 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005254 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005255 return( ret );
5256 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005257 }
5258#endif
5259
Hanno Becker4a810fb2017-05-24 16:27:30 +01005260 /*
5261 * Check if renegotiation is necessary and/or handshake is
5262 * in process. If yes, perform/continue, and fall through
5263 * if an unexpected packet is received while the client
5264 * is waiting for the ServerHello.
5265 *
5266 * (There is no equivalent to the last condition on
5267 * the server-side as it is not treated as within
5268 * a handshake while waiting for the ClientHello
5269 * after a renegotiation request.)
5270 */
5271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005272#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005273 ret = ssl_check_ctr_renegotiate( ssl );
5274 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5275 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005278 return( ret );
5279 }
5280#endif
5281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005284 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005285 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5286 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005289 return( ret );
5290 }
5291 }
5292
Hanno Beckere41158b2017-10-23 13:30:32 +01005293 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005294 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005295 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005296 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005297 if( ssl->f_get_timer != NULL &&
5298 ssl->f_get_timer( ssl->p_timer ) == -1 )
5299 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005300 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005301 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005302
Hanno Becker327c93b2018-08-15 13:56:18 +01005303 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005304 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005305 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5306 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005307
Hanno Becker4a810fb2017-05-24 16:27:30 +01005308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5309 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005310 }
5311
5312 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005313 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005314 {
5315 /*
5316 * OpenSSL sends empty messages to randomize the IV
5317 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005318 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005321 return( 0 );
5322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324 return( ret );
5325 }
5326 }
5327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005329 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005330 ret = ssl_handle_hs_message_post_handshake( ssl );
5331 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005332 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005333 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5334 ret );
5335 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005336 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005337
Hanno Beckerf26cc722021-04-21 07:30:13 +01005338 /* At this point, we don't know whether the renegotiation triggered
5339 * by the post-handshake message has been completed or not. The cases
5340 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005341 * 1) The renegotiation is complete. In this case, no new record
5342 * has been read yet.
5343 * 2) The renegotiation is incomplete because the client received
5344 * an application data record while awaiting the ServerHello.
5345 * 3) The renegotiation is incomplete because the client received
5346 * a non-handshake, non-application data message while awaiting
5347 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005348 *
5349 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005350 * - For 1), the next iteration will read a new record and check
5351 * if it's application data.
5352 * - For 2), the loop condition isn't satisfied as application data
5353 * is present, hence continue is the same as break
5354 * - For 3), the loop condition is satisfied and read_record
5355 * will re-deliver the message that was held back by the client
5356 * when expecting the ServerHello.
5357 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005358
Hanno Becker90333da2017-10-10 11:27:13 +01005359 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005360 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005361#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005363 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005364 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005365 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005366 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005369 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005371 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005372 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005373 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5377 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005380 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005381 }
5382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5386 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005387 }
5388
5389 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005390
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005391 /* We're going to return something now, cancel timer,
5392 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005394 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005395
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005396#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005397 /* If we requested renego but received AppData, resend HelloRequest.
5398 * Do it now, after setting in_offt, to avoid taking this branch
5399 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005401 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005403 {
Hanno Becker786300f2020-02-05 10:46:40 +00005404 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005405 {
Hanno Becker786300f2020-02-05 10:46:40 +00005406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5407 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005408 return( ret );
5409 }
5410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005413 }
5414
5415 n = ( len < ssl->in_msglen )
5416 ? len : ssl->in_msglen;
5417
5418 memcpy( buf, ssl->in_offt, n );
5419 ssl->in_msglen -= n;
5420
gabor-mezei-arma3214132020-07-15 10:55:00 +02005421 /* Zeroising the plaintext buffer to erase unused application data
5422 from the memory. */
5423 mbedtls_platform_zeroize( ssl->in_offt, n );
5424
Paul Bakker5121ce52009-01-03 21:22:43 +00005425 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005426 {
5427 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005428 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005429 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005431 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005432 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005433 /* more data available */
5434 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005435 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005438
Paul Bakker23986e52011-04-24 08:57:21 +00005439 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005440}
5441
5442/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005443 * Send application data to be encrypted by the SSL layer, taking care of max
5444 * fragment length and buffer size.
5445 *
5446 * According to RFC 5246 Section 6.2.1:
5447 *
5448 * Zero-length fragments of Application data MAY be sent as they are
5449 * potentially useful as a traffic analysis countermeasure.
5450 *
5451 * Therefore, it is possible that the input message length is 0 and the
5452 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005453 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005454static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005455 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005456{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005457 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5458 const size_t max_len = (size_t) ret;
5459
5460 if( ret < 0 )
5461 {
5462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5463 return( ret );
5464 }
5465
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005466 if( len > max_len )
5467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005469 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005472 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5473 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005474 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005476 }
5477 else
5478#endif
5479 len = max_len;
5480 }
Paul Bakker887bd502011-06-08 13:10:54 +00005481
Paul Bakker5121ce52009-01-03 21:22:43 +00005482 if( ssl->out_left != 0 )
5483 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005484 /*
5485 * The user has previously tried to send the data and
5486 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5487 * written. In this case, we expect the high-level write function
5488 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005493 return( ret );
5494 }
5495 }
Paul Bakker887bd502011-06-08 13:10:54 +00005496 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005497 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005498 /*
5499 * The user is trying to send a message the first time, so we need to
5500 * copy the data into the internal buffers and setup the data structure
5501 * to keep track of partial writes
5502 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005503 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005505 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005506
Hanno Becker67bc7c32018-08-06 11:33:50 +01005507 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005510 return( ret );
5511 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 }
5513
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005514 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005515}
5516
5517/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005518 * Write application data, doing 1/n-1 splitting if necessary.
5519 *
5520 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005521 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005522 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005524#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005525static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005526 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005527{
Janos Follath865b3eb2019-12-16 11:46:15 +00005528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005529
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005530 if( ssl->conf->cbc_record_splitting ==
5531 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005532 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005533 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5534 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5535 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005536 {
5537 return( ssl_write_real( ssl, buf, len ) );
5538 }
5539
5540 if( ssl->split_done == 0 )
5541 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005542 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005543 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005544 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005545 }
5546
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005547 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5548 return( ret );
5549 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005550
5551 return( ret + 1 );
5552}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005553#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005554
5555/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005556 * Write application data (public-facing wrapper)
5557 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005558int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005559{
Janos Follath865b3eb2019-12-16 11:46:15 +00005560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005561
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005563
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005564 if( ssl == NULL || ssl->conf == NULL )
5565 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5566
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005567#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005568 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5569 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005570 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005571 return( ret );
5572 }
5573#endif
5574
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005575 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005576 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005577 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005578 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005580 return( ret );
5581 }
5582 }
5583
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005584#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005585 ret = ssl_write_split( ssl, buf, len );
5586#else
5587 ret = ssl_write_real( ssl, buf, len );
5588#endif
5589
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005591
5592 return( ret );
5593}
5594
5595/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005596 * Notify the peer that the connection is being closed
5597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005598int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005599{
Janos Follath865b3eb2019-12-16 11:46:15 +00005600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005601
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005602 if( ssl == NULL || ssl->conf == NULL )
5603 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005607 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005610 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5613 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5614 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005617 return( ret );
5618 }
5619 }
5620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005622
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005623 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005624}
5625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005626void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005627{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005628 if( transform == NULL )
5629 return;
5630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5632 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005633
Hanno Beckerd56ed242018-01-03 15:32:51 +00005634#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 mbedtls_md_free( &transform->md_ctx_enc );
5636 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005637#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005638
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005639 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005640}
5641
Hanno Becker0271f962018-08-16 13:23:47 +01005642#if defined(MBEDTLS_SSL_PROTO_DTLS)
5643
Hanno Becker533ab5f2020-02-05 10:49:13 +00005644void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005645{
5646 unsigned offset;
5647 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5648
5649 if( hs == NULL )
5650 return;
5651
Hanno Becker283f5ef2018-08-24 09:34:47 +01005652 ssl_free_buffered_record( ssl );
5653
Hanno Becker0271f962018-08-16 13:23:47 +01005654 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005655 ssl_buffering_free_slot( ssl, offset );
5656}
5657
5658static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5659 uint8_t slot )
5660{
5661 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5662 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005663
5664 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5665 return;
5666
Hanno Beckere605b192018-08-21 15:59:07 +01005667 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005668 {
Hanno Beckere605b192018-08-21 15:59:07 +01005669 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005670 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005671 mbedtls_free( hs_buf->data );
5672 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005673 }
5674}
5675
5676#endif /* MBEDTLS_SSL_PROTO_DTLS */
5677
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005678/*
5679 * Convert version numbers to/from wire format
5680 * and, for DTLS, to/from TLS equivalent.
5681 *
5682 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005683 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005684 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5685 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005688 unsigned char ver[2] )
5689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690#if defined(MBEDTLS_SSL_PROTO_DTLS)
5691 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005694 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5695
5696 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5697 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5698 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005699 else
5700#else
5701 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005702#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005703 {
5704 ver[0] = (unsigned char) major;
5705 ver[1] = (unsigned char) minor;
5706 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005707}
5708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005710 const unsigned char ver[2] )
5711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712#if defined(MBEDTLS_SSL_PROTO_DTLS)
5713 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005714 {
5715 *major = 255 - ver[0] + 2;
5716 *minor = 255 - ver[1] + 1;
5717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005719 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5720 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005721 else
5722#else
5723 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005724#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005725 {
5726 *major = ver[0];
5727 *minor = ver[1];
5728 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005729}
5730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731#endif /* MBEDTLS_SSL_TLS_C */