blob: faafaba857ac6d94f2ad97f72e6aa010319b1853 [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 Beckerfd86ca82020-11-30 08:54:23 +0000636#if defined(MBEDTLS_SSL_SOME_SUITES_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 Beckerfd86ca82020-11-30 08:54:23 +0000681#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000682
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200683 /*
684 * Encrypt
685 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000686#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
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
Hanno Beckerd086bf02021-03-22 13:01:27 +0000711#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
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é-Gonnard7a8b1e62020-07-15 11:52:14 +02001167 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001168 MD_CHK( mbedtls_md_starts( ctx ) );
1169 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1170 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1171 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001172
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001173 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001174 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001175
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001176#undef MD_CHK
1177
1178cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001179 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001180 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001181}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001182
1183/*
1184 * Constant-flow memcpy from variable position in buffer.
1185 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001186 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001187 */
1188MBEDTLS_STATIC_TESTABLE void mbedtls_ssl_cf_memcpy_offset(
1189 unsigned char *dst,
1190 const unsigned char *src_base,
1191 size_t offset_secret,
1192 size_t offset_min, size_t offset_max,
1193 size_t len )
1194{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001195 size_t offset;
1196
1197 for( offset = offset_min; offset <= offset_max; offset++ )
1198 {
1199 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1200 offset, offset_secret );
1201 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001202}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001203#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001204
Hanno Becker605949f2019-07-12 08:23:59 +01001205int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001206 mbedtls_ssl_transform *transform,
1207 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001209 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001211 int ret, auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001213 size_t padlen = 0, correct = 1;
1214#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001215 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001216 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001217 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001218
Hanno Beckera18d1322018-01-03 14:27:32 +00001219#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001220 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001221 ((void) ssl);
1222#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001225 if( rec == NULL ||
1226 rec->buf == NULL ||
1227 rec->buf_len < rec->data_offset ||
1228 rec->buf_len - rec->data_offset < rec->data_len )
1229 {
1230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001232 }
1233
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001234 data = rec->buf + rec->data_offset;
1235 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001236
Hanno Beckera0e20d02019-05-15 14:03:01 +01001237#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001238 /*
1239 * Match record's CID with incoming CID.
1240 */
Hanno Becker938489a2019-05-08 13:02:22 +01001241 if( rec->cid_len != transform->in_cid_len ||
1242 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1243 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001244 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001245 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001246#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001247
Hanno Beckerd086bf02021-03-22 13:01:27 +00001248#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001250 {
1251 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001252 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1253 transform->iv_dec,
1254 transform->ivlen,
1255 data, rec->data_len,
1256 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001259 return( ret );
1260 }
1261
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001262 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 }
Paul Bakker68884e32013-01-07 18:20:04 +01001268 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001269#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001270#if defined(MBEDTLS_GCM_C) || \
1271 defined(MBEDTLS_CCM_C) || \
1272 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001274 mode == MBEDTLS_MODE_CCM ||
1275 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001276 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001277 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001278 unsigned char *dynamic_iv;
1279 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001280
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001281 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001282 * Extract dynamic part of nonce for AEAD decryption.
1283 *
1284 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1285 * part of the IV is prepended to the ciphertext and
1286 * can be chosen freely - in particular, it need not
1287 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001288 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001289 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001290 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001291 {
1292 if( rec->data_len < dynamic_iv_len )
1293 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1295 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001296 rec->data_len,
1297 dynamic_iv_len ) );
1298 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1299 }
1300 dynamic_iv = data;
1301
1302 data += dynamic_iv_len;
1303 rec->data_offset += dynamic_iv_len;
1304 rec->data_len -= dynamic_iv_len;
1305 }
Hanno Becker17263802020-05-28 07:05:48 +01001306 else
1307 {
1308 dynamic_iv = rec->ctr;
1309 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001310
1311 /* Check that there's space for the authentication tag. */
1312 if( rec->data_len < transform->taglen )
1313 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1315 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001316 rec->data_len,
1317 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001319 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001320 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001321
Hanno Beckerdf8be222020-05-21 15:30:57 +01001322 /*
1323 * Prepare nonce from dynamic and static parts.
1324 */
Hanno Becker17263802020-05-28 07:05:48 +01001325 ssl_build_record_nonce( iv, sizeof( iv ),
1326 transform->iv_dec,
1327 transform->fixed_ivlen,
1328 dynamic_iv,
1329 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001330
Hanno Beckerdf8be222020-05-21 15:30:57 +01001331 /*
1332 * Build additional data for AEAD encryption.
1333 * This depends on the TLS version.
1334 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001335 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1336 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001337 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001338 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001339
Hanno Beckerd96a6522019-07-10 13:55:25 +01001340 /* Because of the check above, we know that there are
1341 * explicit_iv_len Bytes preceeding data, and taglen
1342 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001343 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001344 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001345
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001346 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001347 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001348 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001349
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001350 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001351 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001352 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001353 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001354 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001355 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001356 data, rec->data_len + transform->taglen, /* src */
1357 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001358 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 {
TRodziewicz18efb732021-04-29 23:12:19 +02001360 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1363 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001364
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001365 return( ret );
1366 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001367 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001368
Hanno Beckerd96a6522019-07-10 13:55:25 +01001369 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001370 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001374 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001375 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001378#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001381 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001382
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 /*
Paul Bakker45829992013-01-03 14:52:21 +01001384 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001387 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1388 {
1389 /* The ciphertext is prefixed with the CBC IV. */
1390 minlen += transform->ivlen;
1391 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001392#endif
Paul Bakker45829992013-01-03 14:52:21 +01001393
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001394 /* Size considerations:
1395 *
1396 * - The CBC cipher text must not be empty and hence
1397 * at least of size transform->ivlen.
1398 *
1399 * Together with the potential IV-prefix, this explains
1400 * the first of the two checks below.
1401 *
1402 * - The record must contain a MAC, either in plain or
1403 * encrypted, depending on whether Encrypt-then-MAC
1404 * is used or not.
1405 * - If it is, the message contains the IV-prefix,
1406 * the CBC ciphertext, and the MAC.
1407 * - If it is not, the padded plaintext, and hence
1408 * the CBC ciphertext, has at least length maclen + 1
1409 * because there is at least the padding length byte.
1410 *
1411 * As the CBC ciphertext is not empty, both cases give the
1412 * lower bound minlen + maclen + 1 on the record size, which
1413 * we test for in the second check below.
1414 */
1415 if( rec->data_len < minlen + transform->ivlen ||
1416 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001417 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1419 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1420 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001421 "+ 1 ) ( + expl IV )", rec->data_len,
1422 transform->ivlen,
1423 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001425 }
1426
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001427 /*
1428 * Authenticate before decrypt if enabled
1429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001431 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001432 {
Hanno Becker992b6872017-11-09 18:57:39 +00001433 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436
Hanno Beckerd96a6522019-07-10 13:55:25 +01001437 /* Update data_len in tandem with add_data.
1438 *
1439 * The subtraction is safe because of the previous check
1440 * data_len >= minlen + maclen + 1.
1441 *
1442 * Afterwards, we know that data + data_len is followed by at
1443 * least maclen Bytes, which justifies the call to
1444 * mbedtls_ssl_safer_memcmp() below.
1445 *
1446 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001447 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001448 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1449 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001450
Hanno Beckerd96a6522019-07-10 13:55:25 +01001451 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001452 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1453 add_data_len );
1454 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1455 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001456 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1457 data, rec->data_len );
1458 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1459 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001460
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001461 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1462 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001463 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001464 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001465
Hanno Beckerd96a6522019-07-10 13:55:25 +01001466 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001467 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1468 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001472 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001473 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001476
1477 /*
1478 * Check length sanity
1479 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001480
1481 /* We know from above that data_len > minlen >= 0,
1482 * so the following check in particular implies that
1483 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001484 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001485 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001486 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1487 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001488 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001490 }
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001493 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001494 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001495 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001496 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001497 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001498 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001499 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001501 data += transform->ivlen;
1502 rec->data_offset += transform->ivlen;
1503 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001504 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001506
Hanno Beckerd96a6522019-07-10 13:55:25 +01001507 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1508
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001509 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1510 transform->iv_dec, transform->ivlen,
1511 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001514 return( ret );
1515 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001516
Hanno Beckerd96a6522019-07-10 13:55:25 +01001517 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001518 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001522 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001523
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001524#if defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001525 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001526 {
1527 /*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001528 * Save IV in TLS1, where CBC decryption of consecutive
Hanno Beckerd96a6522019-07-10 13:55:25 +01001529 * records is equivalent to CBC decryption of the concatenation
1530 * of the records; in other words, IVs are maintained across
1531 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001532 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001533 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1534 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001536#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001537
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001538 /* Safe since data_len >= minlen + maclen + 1, so after having
1539 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001540 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1541 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001542 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001543
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001544 if( auth_done == 1 )
1545 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001546 const size_t mask = mbedtls_ssl_cf_mask_ge(
1547 rec->data_len,
1548 padlen + 1 );
1549 correct &= mask;
1550 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001551 }
1552 else
Paul Bakker45829992013-01-03 14:52:21 +01001553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001555 if( rec->data_len < transform->maclen + padlen + 1 )
1556 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1558 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1559 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001560 rec->data_len,
1561 transform->maclen,
1562 padlen + 1 ) );
1563 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001564#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001565
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001566 const size_t mask = mbedtls_ssl_cf_mask_ge(
1567 rec->data_len,
1568 transform->maclen + padlen + 1 );
1569 correct &= mask;
1570 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001572
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001573 padlen++;
1574
1575 /* Regardless of the validity of the padding,
1576 * we have data_len >= padlen here. */
1577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1579 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001580 /* The padding check involves a series of up to 256
1581 * consecutive memory reads at the end of the record
1582 * plaintext buffer. In order to hide the length and
1583 * validity of the padding, always perform exactly
1584 * `min(256,plaintext_len)` reads (but take into account
1585 * only the last `padlen` bytes for the padding check). */
1586 size_t pad_count = 0;
1587 volatile unsigned char* const check = data;
1588
1589 /* Index of first padding byte; it has been ensured above
1590 * that the subtraction is safe. */
1591 size_t const padding_idx = rec->data_len - padlen;
1592 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1593 size_t const start_idx = rec->data_len - num_checks;
1594 size_t idx;
1595
1596 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001598 /* pad_count += (idx >= padding_idx) &&
1599 * (check[idx] == padlen - 1);
1600 */
1601 const size_t mask = mbedtls_ssl_cf_mask_ge( idx, padding_idx );
1602 const size_t equal = mbedtls_ssl_cf_bool_eq( check[idx],
1603 padlen - 1 );
1604 pad_count += mask & equal;
1605 }
1606 correct &= mbedtls_ssl_cf_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001609 if( padlen > 0 && correct == 0 )
1610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001611#endif
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001612 padlen &= mbedtls_ssl_cf_mask_from_bit( correct );
1613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1615 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001616
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001617 /* If the padding was found to be invalid, padlen == 0
1618 * and the subtraction is safe. If the padding was found valid,
1619 * padlen hasn't been changed and the previous assertion
1620 * data_len >= padlen still holds. */
1621 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001623 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001624#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1627 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001630#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001632 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001633#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
1635 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001636 * Authenticate if not done yet.
1637 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001638 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001639#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001640 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 {
Hanno Becker992b6872017-11-09 18:57:39 +00001642 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001643 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001644
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001645 /* If the initial value of padlen was such that
1646 * data_len < maclen + padlen + 1, then padlen
1647 * got reset to 1, and the initial check
1648 * data_len >= minlen + maclen + 1
1649 * guarantees that at this point we still
1650 * have at least data_len >= maclen.
1651 *
1652 * If the initial value of padlen was such that
1653 * data_len >= maclen + padlen + 1, then we have
1654 * subtracted either padlen + 1 (if the padding was correct)
1655 * or 0 (if the padding was incorrect) since then,
1656 * hence data_len >= maclen in any case.
1657 */
1658 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001659 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1660 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1663 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001664 /*
1665 * The next two sizes are the minimum and maximum values of
1666 * data_len over all padlen values.
1667 *
1668 * They're independent of padlen, since we previously did
1669 * data_len -= padlen.
1670 *
1671 * Note that max_len + maclen is never more than the buffer
1672 * length, as we previously did in_msglen -= maclen too.
1673 */
1674 const size_t max_len = rec->data_len + padlen;
1675 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1676
1677 ret = mbedtls_ssl_cf_hmac( &transform->md_ctx_dec,
1678 add_data, add_data_len,
1679 data, rec->data_len, min_len, max_len,
1680 mac_expect );
1681 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001683 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
1684 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001685 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001686
1687 mbedtls_ssl_cf_memcpy_offset( mac_peer, data,
1688 rec->data_len,
1689 min_len, max_len,
1690 transform->maclen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1692 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001694#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001695 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001696 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001697#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001699 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001700 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_SSL_DEBUG_ALL)
1703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001704#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001705 correct = 0;
1706 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001707 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001708 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001709
1710 /*
1711 * Finally check the correct flag
1712 */
1713 if( correct == 0 )
1714 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001715#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001716
1717 /* Make extra sure authentication was performed, exactly once */
1718 if( auth_done != 1 )
1719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001722 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Hanno Beckerccc13d02020-05-04 12:30:04 +01001724#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1725 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1726 {
1727 /* Remove inner padding and infer true content type. */
1728 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1729 &rec->type );
1730
1731 if( ret != 0 )
1732 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1733 }
1734#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1735
Hanno Beckera0e20d02019-05-15 14:03:01 +01001736#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001737 if( rec->cid_len != 0 )
1738 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001739 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1740 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001741 if( ret != 0 )
1742 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1743 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001744#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
1748 return( 0 );
1749}
1750
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001751#undef MAC_NONE
1752#undef MAC_PLAINTEXT
1753#undef MAC_CIPHERTEXT
1754
Paul Bakker5121ce52009-01-03 21:22:43 +00001755/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001756 * Fill the input message buffer by appending data to it.
1757 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001758 *
1759 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1760 * available (from this read and/or a previous one). Otherwise, an error code
1761 * is returned (possibly EOF or WANT_READ).
1762 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001763 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1764 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1765 * since we always read a whole datagram at once.
1766 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001767 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001768 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001771{
Janos Follath865b3eb2019-12-16 11:46:15 +00001772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001773 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001774#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1775 size_t in_buf_len = ssl->in_buf_len;
1776#else
1777 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1778#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001781
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001782 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001785 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001787 }
1788
Darryl Greenb33cc762019-11-28 14:29:44 +00001789 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1792 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001793 }
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001796 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001798 uint32_t timeout;
1799
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001800 /*
1801 * The point is, we need to always read a full datagram at once, so we
1802 * sometimes read more then requested, and handle the additional data.
1803 * It could be the rest of the current record (while fetching the
1804 * header) and/or some other records in the same datagram.
1805 */
1806
1807 /*
1808 * Move to the next record in the already read datagram if applicable
1809 */
1810 if( ssl->next_record_offset != 0 )
1811 {
1812 if( ssl->in_left < ssl->next_record_offset )
1813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001816 }
1817
1818 ssl->in_left -= ssl->next_record_offset;
1819
1820 if( ssl->in_left != 0 )
1821 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1823 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001824 ssl->next_record_offset ) );
1825 memmove( ssl->in_hdr,
1826 ssl->in_hdr + ssl->next_record_offset,
1827 ssl->in_left );
1828 }
1829
1830 ssl->next_record_offset = 0;
1831 }
1832
Paul Elliottd48d5c62021-01-07 14:47:05 +00001833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1834 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001836
1837 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001838 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001839 */
1840 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001843 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001844 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001845
1846 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001847 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001848 * are not at the beginning of a new record, the caller did something
1849 * wrong.
1850 */
1851 if( ssl->in_left != 0 )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1854 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001855 }
1856
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001857 /*
1858 * Don't even try to read if time's out already.
1859 * This avoids by-passing the timer when repeatedly receiving messages
1860 * that will end up being dropped.
1861 */
Hanno Becker7876d122020-02-05 10:39:31 +00001862 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001863 {
1864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001865 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001866 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001867 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001868 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001869 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001872 timeout = ssl->handshake->retransmit_timeout;
1873 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001874 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001875
Paul Elliott9f352112020-12-09 14:55:45 +00001876 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001877
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001878 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001879 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1880 timeout );
1881 else
1882 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001885
1886 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001888 }
1889
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001890 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001893 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001896 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001897 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001900 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001901 }
1902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001906 return( ret );
1907 }
1908
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001909 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001910 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001912 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001914 {
Hanno Becker786300f2020-02-05 10:46:40 +00001915 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001916 {
Hanno Becker786300f2020-02-05 10:46:40 +00001917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1918 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001919 return( ret );
1920 }
1921
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001922 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001923 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001925 }
1926
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 if( ret < 0 )
1928 return( ret );
1929
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001930 ssl->in_left = ret;
1931 }
1932 else
1933#endif
1934 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1936 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001937 ssl->in_left, nb_want ) );
1938
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001939 while( ssl->in_left < nb_want )
1940 {
1941 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001942
Hanno Becker7876d122020-02-05 10:39:31 +00001943 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001944 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1945 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001946 {
1947 if( ssl->f_recv_timeout != NULL )
1948 {
1949 ret = ssl->f_recv_timeout( ssl->p_bio,
1950 ssl->in_hdr + ssl->in_left, len,
1951 ssl->conf->read_timeout );
1952 }
1953 else
1954 {
1955 ret = ssl->f_recv( ssl->p_bio,
1956 ssl->in_hdr + ssl->in_left, len );
1957 }
1958 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001959
Paul Elliottd48d5c62021-01-07 14:47:05 +00001960 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1961 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001962 ssl->in_left, nb_want ) );
1963 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001964
1965 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001967
1968 if( ret < 0 )
1969 return( ret );
1970
makise-homuraaf9513b2020-08-24 18:26:27 +03001971 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001972 {
Darryl Green11999bb2018-03-13 15:22:58 +00001973 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001974 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001975 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1977 }
1978
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001979 ssl->in_left += ret;
1980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 }
1982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
1985 return( 0 );
1986}
1987
1988/*
1989 * Flush any data not yet written
1990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001992{
Janos Follath865b3eb2019-12-16 11:46:15 +00001993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001994 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001998 if( ssl->f_send == NULL )
1999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002001 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002003 }
2004
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002005 /* Avoid incrementing counter if data is flushed */
2006 if( ssl->out_left == 0 )
2007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002009 return( 0 );
2010 }
2011
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 while( ssl->out_left > 0 )
2013 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
2015 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01002016 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002017
Hanno Becker2b1e3542018-08-06 11:19:13 +01002018 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002019 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
2023 if( ret <= 0 )
2024 return( ret );
2025
makise-homuraaf9513b2020-08-24 18:26:27 +03002026 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002027 {
Darryl Green11999bb2018-03-13 15:22:58 +00002028 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002029 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00002030 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2032 }
2033
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 ssl->out_left -= ret;
2035 }
2036
Hanno Becker2b1e3542018-08-06 11:19:13 +01002037#if defined(MBEDTLS_SSL_PROTO_DTLS)
2038 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002039 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002040 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002041 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002042 else
2043#endif
2044 {
2045 ssl->out_hdr = ssl->out_buf + 8;
2046 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002047 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050
2051 return( 0 );
2052}
2053
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002054/*
2055 * Functions to handle the DTLS retransmission state machine
2056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002058/*
2059 * Append current handshake message to current outgoing flight
2060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2065 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2066 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002067
2068 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002069 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002070 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002073 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002074 }
2075
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002076 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002077 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2079 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002081 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002082 }
2083
2084 /* Copy current handshake message with headers */
2085 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2086 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002087 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002088 msg->next = NULL;
2089
2090 /* Append to the current flight */
2091 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002092 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002093 else
2094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002096 while( cur->next != NULL )
2097 cur = cur->next;
2098 cur->next = msg;
2099 }
2100
Hanno Becker3b235902018-08-06 09:54:53 +01002101 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002102 return( 0 );
2103}
2104
2105/*
2106 * Free the current flight of handshake messages
2107 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002108void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 mbedtls_ssl_flight_item *cur = flight;
2111 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002112
2113 while( cur != NULL )
2114 {
2115 next = cur->next;
2116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_free( cur->p );
2118 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002119
2120 cur = next;
2121 }
2122}
2123
2124/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002125 * Swap transform_out and out_ctr with the alternative ones
2126 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002127static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002130 unsigned char tmp_out_ctr[8];
2131
2132 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002135 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002136 }
2137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002139
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002140 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002141 tmp_transform = ssl->transform_out;
2142 ssl->transform_out = ssl->handshake->alt_transform_out;
2143 ssl->handshake->alt_transform_out = tmp_transform;
2144
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002145 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002146 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2147 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002148 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002149
2150 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002151 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002152
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002153 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002154}
2155
2156/*
2157 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002158 */
2159int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2160{
2161 int ret = 0;
2162
2163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2164
2165 ret = mbedtls_ssl_flight_transmit( ssl );
2166
2167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2168
2169 return( ret );
2170}
2171
2172/*
2173 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002174 *
2175 * Need to remember the current message in case flush_output returns
2176 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002177 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002178 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002179int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002180{
Janos Follath865b3eb2019-12-16 11:46:15 +00002181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002185 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002186 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002187
2188 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002189 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002190 ret = ssl_swap_epochs( ssl );
2191 if( ret != 0 )
2192 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002195 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002196
2197 while( ssl->handshake->cur_msg != NULL )
2198 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002199 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002200 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002201
Hanno Beckere1dcb032018-08-17 16:47:58 +01002202 int const is_finished =
2203 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2204 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2205
Hanno Becker04da1892018-08-14 13:22:10 +01002206 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2207 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2208
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002209 /* Swap epochs before sending Finished: we can't do it after
2210 * sending ChangeCipherSpec, in case write returns WANT_READ.
2211 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002212 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002213 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002215 ret = ssl_swap_epochs( ssl );
2216 if( ret != 0 )
2217 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002218 }
2219
Hanno Becker67bc7c32018-08-06 11:33:50 +01002220 ret = ssl_get_remaining_payload_in_datagram( ssl );
2221 if( ret < 0 )
2222 return( ret );
2223 max_frag_len = (size_t) ret;
2224
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002225 /* CCS is copied as is, while HS messages may need fragmentation */
2226 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2227 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002228 if( max_frag_len == 0 )
2229 {
2230 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2231 return( ret );
2232
2233 continue;
2234 }
2235
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002236 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002237 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002238 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002239
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002240 /* Update position inside current message */
2241 ssl->handshake->cur_msg_p += cur->len;
2242 }
2243 else
2244 {
2245 const unsigned char * const p = ssl->handshake->cur_msg_p;
2246 const size_t hs_len = cur->len - 12;
2247 const size_t frag_off = p - ( cur->p + 12 );
2248 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002249 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002250
Hanno Beckere1dcb032018-08-17 16:47:58 +01002251 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002252 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002253 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002254 {
2255 ret = ssl_swap_epochs( ssl );
2256 if( ret != 0 )
2257 return( ret );
2258 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002259
Hanno Becker67bc7c32018-08-06 11:33:50 +01002260 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2261 return( ret );
2262
2263 continue;
2264 }
2265 max_hs_frag_len = max_frag_len - 12;
2266
2267 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2268 max_hs_frag_len : rem_len;
2269
2270 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002271 {
2272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002273 (unsigned) cur_hs_frag_len,
2274 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002275 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002276
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002277 /* Messages are stored with handshake headers as if not fragmented,
2278 * copy beginning of headers then fill fragmentation fields.
2279 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2280 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002281
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002282 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2283 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2284 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2285
Hanno Becker67bc7c32018-08-06 11:33:50 +01002286 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2287 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2288 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002289
2290 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2291
Hanno Becker3f7b9732018-08-28 09:53:25 +01002292 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002293 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2294 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002295 ssl->out_msgtype = cur->type;
2296
2297 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002298 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002299 }
2300
2301 /* If done with the current message move to the next one if any */
2302 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2303 {
2304 if( cur->next != NULL )
2305 {
2306 ssl->handshake->cur_msg = cur->next;
2307 ssl->handshake->cur_msg_p = cur->next->p + 12;
2308 }
2309 else
2310 {
2311 ssl->handshake->cur_msg = NULL;
2312 ssl->handshake->cur_msg_p = NULL;
2313 }
2314 }
2315
2316 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002317 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002320 return( ret );
2321 }
2322 }
2323
Hanno Becker67bc7c32018-08-06 11:33:50 +01002324 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2325 return( ret );
2326
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002327 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002328 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2329 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002330 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002333 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002334 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002335
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002337
2338 return( 0 );
2339}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002340
2341/*
2342 * To be called when the last message of an incoming flight is received.
2343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002345{
2346 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002347 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002348 ssl->handshake->flight = NULL;
2349 ssl->handshake->cur_msg = NULL;
2350
2351 /* The next incoming flight will start with this msg_seq */
2352 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2353
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002354 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002355 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002356
Hanno Becker0271f962018-08-16 13:23:47 +01002357 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002358 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002359
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002360 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002361 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2364 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002367 }
2368 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002370}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002371
2372/*
2373 * To be called when the last message of an outgoing flight is send.
2374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002375void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002376{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002377 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002378 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2381 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002384 }
2385 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002389
Paul Bakker5121ce52009-01-03 21:22:43 +00002390/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002391 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002393
2394/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002395 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002396 *
2397 * - fill in handshake headers
2398 * - update handshake checksum
2399 * - DTLS: save message for resending
2400 * - then pass to the record layer
2401 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002402 * DTLS: except for HelloRequest, messages are only queued, and will only be
2403 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002404 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002405 * Inputs:
2406 * - ssl->out_msglen: 4 + actual handshake message len
2407 * (4 is the size of handshake headers for TLS)
2408 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2409 * - ssl->out_msg + 4: the handshake message body
2410 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002411 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002412 * - ssl->out_msglen: the length of the record contents
2413 * (including handshake headers but excluding record headers)
2414 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002415 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002416int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417{
Janos Follath865b3eb2019-12-16 11:46:15 +00002418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002419 const size_t hs_len = ssl->out_msglen - 4;
2420 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2423
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002424 /*
2425 * Sanity checks
2426 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002427 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002428 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2429 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2431 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002434 /* Whenever we send anything different from a
2435 * HelloRequest we should be in a handshake - double check. */
2436 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2437 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002438 ssl->handshake == NULL )
2439 {
2440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2442 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002445 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002446 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002448 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002451 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002452#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002453
Hanno Beckerb50a2532018-08-06 11:52:54 +01002454 /* Double-check that we did not exceed the bounds
2455 * of the outgoing record buffer.
2456 * This should never fail as the various message
2457 * writing functions must obey the bounds of the
2458 * outgoing record buffer, but better be safe.
2459 *
2460 * Note: We deliberately do not check for the MTU or MFL here.
2461 */
2462 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2463 {
2464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002465 "size %" MBEDTLS_PRINTF_SIZET
2466 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002467 ssl->out_msglen,
2468 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2470 }
2471
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002472 /*
2473 * Fill handshake headers
2474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002476 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002477 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2478 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2479 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002481 /*
2482 * DTLS has additional fields in the Handshake layer,
2483 * between the length field and the actual payload:
2484 * uint16 message_seq;
2485 * uint24 fragment_offset;
2486 * uint24 fragment_length;
2487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002489 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002490 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002491 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002492 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002493 {
2494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002495 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002496 hs_len,
2497 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2499 }
2500
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002501 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002502 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002503
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002504 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002505 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002506 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002507 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2508 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2509 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002510 }
2511 else
2512 {
2513 ssl->out_msg[4] = 0;
2514 ssl->out_msg[5] = 0;
2515 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002516
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002517 /* Handshake hashes are computed without fragmentation,
2518 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002519 memset( ssl->out_msg + 6, 0x00, 3 );
2520 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002523
Hanno Becker0207e532018-08-28 10:28:28 +01002524 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002525 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2526 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 }
2528
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002529 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002531 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002532 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2533 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002534 {
2535 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002538 return( ret );
2539 }
2540 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002541 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002542#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002543 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002544 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002545 {
2546 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2547 return( ret );
2548 }
2549 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002550
2551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2552
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002553 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002554}
2555
2556/*
2557 * Record layer functions
2558 */
2559
2560/*
2561 * Write current record.
2562 *
2563 * Uses:
2564 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2565 * - ssl->out_msglen: length of the record content (excl headers)
2566 * - ssl->out_msg: record content
2567 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002568int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002569{
2570 int ret, done = 0;
2571 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002572 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002573
2574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002575
Paul Bakker05ef8352012-05-08 09:17:57 +00002576 if( !done )
2577 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002578 unsigned i;
2579 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002580#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2581 size_t out_buf_len = ssl->out_buf_len;
2582#else
2583 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2584#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002585 /* Skip writing the record content type to after the encryption,
2586 * as it may change when using the CID extension. */
2587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002589 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002590
Hanno Becker19859472018-08-06 09:40:20 +01002591 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002592 ssl->out_len[0] = (unsigned char)( len >> 8 );
2593 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002594
Paul Bakker48916f92012-09-16 19:57:18 +00002595 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002596 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002597 mbedtls_record rec;
2598
2599 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002600 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002601 rec.data_len = ssl->out_msglen;
2602 rec.data_offset = ssl->out_msg - rec.buf;
2603
2604 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2605 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2606 ssl->conf->transport, rec.ver );
2607 rec.type = ssl->out_msgtype;
2608
Hanno Beckera0e20d02019-05-15 14:03:01 +01002609#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002610 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002611 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002613
Hanno Beckera18d1322018-01-03 14:27:32 +00002614 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002615 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002618 return( ret );
2619 }
2620
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002621 if( rec.data_offset != 0 )
2622 {
2623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2624 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2625 }
2626
Hanno Becker6430faf2019-05-08 11:57:13 +01002627 /* Update the record content type and CID. */
2628 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002629#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002630 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002631#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002632 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002633 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2634 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002635 }
2636
Hanno Becker5903de42019-05-03 14:46:38 +01002637 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002638
2639#if defined(MBEDTLS_SSL_PROTO_DTLS)
2640 /* In case of DTLS, double-check that we don't exceed
2641 * the remaining space in the datagram. */
2642 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2643 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002644 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002645 if( ret < 0 )
2646 return( ret );
2647
2648 if( protected_record_size > (size_t) ret )
2649 {
2650 /* Should never happen */
2651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2652 }
2653 }
2654#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002655
Hanno Becker6430faf2019-05-08 11:57:13 +01002656 /* Now write the potentially updated record content type. */
2657 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2658
Paul Elliott9f352112020-12-09 14:55:45 +00002659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002660 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002661 ssl->out_hdr[0], ssl->out_hdr[1],
2662 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002665 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002666
2667 ssl->out_left += protected_record_size;
2668 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002669 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002670
Hanno Beckerdd772292020-02-05 10:38:31 +00002671 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002672 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2673 break;
2674
2675 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002676 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002677 {
2678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2679 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 }
2682
Hanno Becker67bc7c32018-08-06 11:33:50 +01002683#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002684 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2685 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002686 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002687 size_t remaining;
2688 ret = ssl_get_remaining_payload_in_datagram( ssl );
2689 if( ret < 0 )
2690 {
2691 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2692 ret );
2693 return( ret );
2694 }
2695
2696 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002697 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002698 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002699 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002700 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002701 else
2702 {
Hanno Becker513815a2018-08-20 11:56:09 +01002703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002704 }
2705 }
2706#endif /* MBEDTLS_SSL_PROTO_DTLS */
2707
2708 if( ( flush == SSL_FORCE_FLUSH ) &&
2709 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 return( ret );
2713 }
2714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002716
2717 return( 0 );
2718}
2719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002721
2722static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2723{
2724 if( ssl->in_msglen < ssl->in_hslen ||
2725 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2726 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2727 {
2728 return( 1 );
2729 }
2730 return( 0 );
2731}
Hanno Becker44650b72018-08-16 12:51:11 +01002732
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002733static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002734{
2735 return( ( ssl->in_msg[9] << 16 ) |
2736 ( ssl->in_msg[10] << 8 ) |
2737 ssl->in_msg[11] );
2738}
2739
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002740static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002741{
2742 return( ( ssl->in_msg[6] << 16 ) |
2743 ( ssl->in_msg[7] << 8 ) |
2744 ssl->in_msg[8] );
2745}
2746
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002747static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002748{
2749 uint32_t msg_len, frag_off, frag_len;
2750
2751 msg_len = ssl_get_hs_total_len( ssl );
2752 frag_off = ssl_get_hs_frag_off( ssl );
2753 frag_len = ssl_get_hs_frag_len( ssl );
2754
2755 if( frag_off > msg_len )
2756 return( -1 );
2757
2758 if( frag_len > msg_len - frag_off )
2759 return( -1 );
2760
2761 if( frag_len + 12 > ssl->in_msglen )
2762 return( -1 );
2763
2764 return( 0 );
2765}
2766
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002767/*
2768 * Mark bits in bitmask (used for DTLS HS reassembly)
2769 */
2770static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2771{
2772 unsigned int start_bits, end_bits;
2773
2774 start_bits = 8 - ( offset % 8 );
2775 if( start_bits != 8 )
2776 {
2777 size_t first_byte_idx = offset / 8;
2778
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002779 /* Special case */
2780 if( len <= start_bits )
2781 {
2782 for( ; len != 0; len-- )
2783 mask[first_byte_idx] |= 1 << ( start_bits - len );
2784
2785 /* Avoid potential issues with offset or len becoming invalid */
2786 return;
2787 }
2788
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002789 offset += start_bits; /* Now offset % 8 == 0 */
2790 len -= start_bits;
2791
2792 for( ; start_bits != 0; start_bits-- )
2793 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2794 }
2795
2796 end_bits = len % 8;
2797 if( end_bits != 0 )
2798 {
2799 size_t last_byte_idx = ( offset + len ) / 8;
2800
2801 len -= end_bits; /* Now len % 8 == 0 */
2802
2803 for( ; end_bits != 0; end_bits-- )
2804 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2805 }
2806
2807 memset( mask + offset / 8, 0xFF, len / 8 );
2808}
2809
2810/*
2811 * Check that bitmask is full
2812 */
2813static int ssl_bitmask_check( unsigned char *mask, size_t len )
2814{
2815 size_t i;
2816
2817 for( i = 0; i < len / 8; i++ )
2818 if( mask[i] != 0xFF )
2819 return( -1 );
2820
2821 for( i = 0; i < len % 8; i++ )
2822 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2823 return( -1 );
2824
2825 return( 0 );
2826}
2827
Hanno Becker56e205e2018-08-16 09:06:12 +01002828/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002829static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002830 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002831{
Hanno Becker56e205e2018-08-16 09:06:12 +01002832 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002833
Hanno Becker56e205e2018-08-16 09:06:12 +01002834 alloc_len = 12; /* Handshake header */
2835 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002836
Hanno Beckerd07df862018-08-16 09:14:58 +01002837 if( add_bitmap )
2838 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002839
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002840 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002841}
Hanno Becker56e205e2018-08-16 09:06:12 +01002842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002843#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002844
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002845static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002846{
2847 return( ( ssl->in_msg[1] << 16 ) |
2848 ( ssl->in_msg[2] << 8 ) |
2849 ssl->in_msg[3] );
2850}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002851
Simon Butcher99000142016-10-13 17:21:01 +01002852int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002855 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002857 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002859 }
2860
Hanno Becker12555c62018-08-16 12:47:53 +01002861 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002863 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002864 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002865 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002868 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002869 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002871 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002872
Hanno Becker44650b72018-08-16 12:51:11 +01002873 if( ssl_check_hs_header( ssl ) != 0 )
2874 {
2875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2876 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2877 }
2878
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002879 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002880 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2881 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2882 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2883 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002884 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002885 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2886 {
2887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2888 recv_msg_seq,
2889 ssl->handshake->in_msg_seq ) );
2890 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2891 }
2892
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002893 /* Retransmit only on last message from previous flight, to avoid
2894 * too many retransmissions.
2895 * Besides, No sane server ever retransmits HelloVerifyRequest */
2896 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002900 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002901 recv_msg_seq,
2902 ssl->handshake->in_flight_start_seq ) );
2903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002907 return( ret );
2908 }
2909 }
2910 else
2911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002913 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002914 recv_msg_seq,
2915 ssl->handshake->in_msg_seq ) );
2916 }
2917
Hanno Becker90333da2017-10-10 11:27:13 +01002918 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002919 }
2920 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002921
Hanno Becker6d97ef52018-08-16 13:09:04 +01002922 /* Message reassembly is handled alongside buffering of future
2923 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002924 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002925 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002926 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002929 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002930 }
2931 }
2932 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002934 /* With TLS we don't handle fragmentation (for now) */
2935 if( ssl->in_msglen < ssl->in_hslen )
2936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2938 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002939 }
2940
Simon Butcher99000142016-10-13 17:21:01 +01002941 return( 0 );
2942}
2943
2944void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2945{
Hanno Becker0271f962018-08-16 13:23:47 +01002946 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002947
Hanno Becker0271f962018-08-16 13:23:47 +01002948 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002949 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002950 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002951 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002952
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002953 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002956 ssl->handshake != NULL )
2957 {
Hanno Becker0271f962018-08-16 13:23:47 +01002958 unsigned offset;
2959 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002960
Hanno Becker0271f962018-08-16 13:23:47 +01002961 /* Increment handshake sequence number */
2962 hs->in_msg_seq++;
2963
2964 /*
2965 * Clear up handshake buffering and reassembly structure.
2966 */
2967
2968 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002969 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002970
2971 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002972 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2973 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002974 offset++, hs_buf++ )
2975 {
2976 *hs_buf = *(hs_buf + 1);
2977 }
2978
2979 /* Create a fresh last entry */
2980 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002981 }
2982#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002983}
2984
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002985/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002986 * DTLS anti-replay: RFC 6347 4.1.2.6
2987 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002988 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2989 * Bit n is set iff record number in_window_top - n has been seen.
2990 *
2991 * Usually, in_window_top is the last record number seen and the lsb of
2992 * in_window is set. The only exception is the initial state (record number 0
2993 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002994 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002995#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002996void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002997{
2998 ssl->in_window_top = 0;
2999 ssl->in_window = 0;
3000}
3001
3002static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3003{
3004 return( ( (uint64_t) buf[0] << 40 ) |
3005 ( (uint64_t) buf[1] << 32 ) |
3006 ( (uint64_t) buf[2] << 24 ) |
3007 ( (uint64_t) buf[3] << 16 ) |
3008 ( (uint64_t) buf[4] << 8 ) |
3009 ( (uint64_t) buf[5] ) );
3010}
3011
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003012static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3013{
Janos Follath865b3eb2019-12-16 11:46:15 +00003014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003015 unsigned char *original_in_ctr;
3016
3017 // save original in_ctr
3018 original_in_ctr = ssl->in_ctr;
3019
3020 // use counter from record
3021 ssl->in_ctr = record_in_ctr;
3022
3023 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3024
3025 // restore the counter
3026 ssl->in_ctr = original_in_ctr;
3027
3028 return ret;
3029}
3030
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003031/*
3032 * Return 0 if sequence number is acceptable, -1 otherwise
3033 */
Hanno Becker0183d692019-07-12 08:50:37 +01003034int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003035{
3036 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3037 uint64_t bit;
3038
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003039 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003040 return( 0 );
3041
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003042 if( rec_seqnum > ssl->in_window_top )
3043 return( 0 );
3044
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003045 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003046
3047 if( bit >= 64 )
3048 return( -1 );
3049
3050 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3051 return( -1 );
3052
3053 return( 0 );
3054}
3055
3056/*
3057 * Update replay window on new validated record
3058 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003060{
3061 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3062
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003063 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003064 return;
3065
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003066 if( rec_seqnum > ssl->in_window_top )
3067 {
3068 /* Update window_top and the contents of the window */
3069 uint64_t shift = rec_seqnum - ssl->in_window_top;
3070
3071 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003072 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003073 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003074 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003075 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003076 ssl->in_window |= 1;
3077 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003078
3079 ssl->in_window_top = rec_seqnum;
3080 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003081 else
3082 {
3083 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003084 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003085
3086 if( bit < 64 ) /* Always true, but be extra sure */
3087 ssl->in_window |= (uint64_t) 1 << bit;
3088 }
3089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003091
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003092#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003093/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003094 * Without any SSL context, check if a datagram looks like a ClientHello with
3095 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003096 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003097 *
3098 * - if cookie is valid, return 0
3099 * - if ClientHello looks superficially valid but cookie is not,
3100 * fill obuf and set olen, then
3101 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3102 * - otherwise return a specific error code
3103 */
3104static int ssl_check_dtls_clihlo_cookie(
3105 mbedtls_ssl_cookie_write_t *f_cookie_write,
3106 mbedtls_ssl_cookie_check_t *f_cookie_check,
3107 void *p_cookie,
3108 const unsigned char *cli_id, size_t cli_id_len,
3109 const unsigned char *in, size_t in_len,
3110 unsigned char *obuf, size_t buf_len, size_t *olen )
3111{
3112 size_t sid_len, cookie_len;
3113 unsigned char *p;
3114
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003115 /*
3116 * Structure of ClientHello with record and handshake headers,
3117 * and expected values. We don't need to check a lot, more checks will be
3118 * done when actually parsing the ClientHello - skipping those checks
3119 * avoids code duplication and does not make cookie forging any easier.
3120 *
3121 * 0-0 ContentType type; copied, must be handshake
3122 * 1-2 ProtocolVersion version; copied
3123 * 3-4 uint16 epoch; copied, must be 0
3124 * 5-10 uint48 sequence_number; copied
3125 * 11-12 uint16 length; (ignored)
3126 *
3127 * 13-13 HandshakeType msg_type; (ignored)
3128 * 14-16 uint24 length; (ignored)
3129 * 17-18 uint16 message_seq; copied
3130 * 19-21 uint24 fragment_offset; copied, must be 0
3131 * 22-24 uint24 fragment_length; (ignored)
3132 *
3133 * 25-26 ProtocolVersion client_version; (ignored)
3134 * 27-58 Random random; (ignored)
3135 * 59-xx SessionID session_id; 1 byte len + sid_len content
3136 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3137 * ...
3138 *
3139 * Minimum length is 61 bytes.
3140 */
3141 if( in_len < 61 ||
3142 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3143 in[3] != 0 || in[4] != 0 ||
3144 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3145 {
3146 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3147 }
3148
3149 sid_len = in[59];
3150 if( sid_len > in_len - 61 )
3151 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3152
3153 cookie_len = in[60 + sid_len];
3154 if( cookie_len > in_len - 60 )
3155 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3156
3157 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3158 cli_id, cli_id_len ) == 0 )
3159 {
3160 /* Valid cookie */
3161 return( 0 );
3162 }
3163
3164 /*
3165 * If we get here, we've got an invalid cookie, let's prepare HVR.
3166 *
3167 * 0-0 ContentType type; copied
3168 * 1-2 ProtocolVersion version; copied
3169 * 3-4 uint16 epoch; copied
3170 * 5-10 uint48 sequence_number; copied
3171 * 11-12 uint16 length; olen - 13
3172 *
3173 * 13-13 HandshakeType msg_type; hello_verify_request
3174 * 14-16 uint24 length; olen - 25
3175 * 17-18 uint16 message_seq; copied
3176 * 19-21 uint24 fragment_offset; copied
3177 * 22-24 uint24 fragment_length; olen - 25
3178 *
3179 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3180 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3181 *
3182 * Minimum length is 28.
3183 */
3184 if( buf_len < 28 )
3185 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3186
3187 /* Copy most fields and adapt others */
3188 memcpy( obuf, in, 25 );
3189 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3190 obuf[25] = 0xfe;
3191 obuf[26] = 0xff;
3192
3193 /* Generate and write actual cookie */
3194 p = obuf + 28;
3195 if( f_cookie_write( p_cookie,
3196 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3197 {
3198 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3199 }
3200
3201 *olen = p - obuf;
3202
3203 /* Go back and fill length fields */
3204 obuf[27] = (unsigned char)( *olen - 28 );
3205
3206 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3207 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3208 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3209
3210 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3211 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3212
3213 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3214}
3215
3216/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003217 * Handle possible client reconnect with the same UDP quadruplet
3218 * (RFC 6347 Section 4.2.8).
3219 *
3220 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3221 * that looks like a ClientHello.
3222 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003223 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003224 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003225 * - if the input looks like a ClientHello with a valid cookie,
3226 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003227 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003228 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003229 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003230 * This function is called (through ssl_check_client_reconnect()) when an
3231 * unexpected record is found in ssl_get_next_record(), which will discard the
3232 * record if we return 0, and bubble up the return value otherwise (this
3233 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3234 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003235 */
3236static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3237{
Janos Follath865b3eb2019-12-16 11:46:15 +00003238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003239 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003240
Hanno Becker2fddd372019-07-10 14:37:41 +01003241 if( ssl->conf->f_cookie_write == NULL ||
3242 ssl->conf->f_cookie_check == NULL )
3243 {
3244 /* If we can't use cookies to verify reachability of the peer,
3245 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3247 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003248 return( 0 );
3249 }
3250
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003251 ret = ssl_check_dtls_clihlo_cookie(
3252 ssl->conf->f_cookie_write,
3253 ssl->conf->f_cookie_check,
3254 ssl->conf->p_cookie,
3255 ssl->cli_id, ssl->cli_id_len,
3256 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003257 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003258
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003259 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3260
3261 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003262 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003263 int send_ret;
3264 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3265 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3266 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003267 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003268 * If the error is permanent we'll catch it later,
3269 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003270 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3271 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3272 (void) send_ret;
3273
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003274 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003275 }
3276
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003277 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003278 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003280 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003281 {
3282 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3283 return( ret );
3284 }
3285
3286 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003287 }
3288
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003289 return( ret );
3290}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003291#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003292
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003293static int ssl_check_record_type( uint8_t record_type )
3294{
3295 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3296 record_type != MBEDTLS_SSL_MSG_ALERT &&
3297 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3298 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3299 {
3300 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3301 }
3302
3303 return( 0 );
3304}
3305
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003306/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003307 * ContentType type;
3308 * ProtocolVersion version;
3309 * uint16 epoch; // DTLS only
3310 * uint48 sequence_number; // DTLS only
3311 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003312 *
3313 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003314 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003315 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3316 *
3317 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003318 * 1. proceed with the record if this function returns 0
3319 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3320 * 3. return CLIENT_RECONNECT if this function return that value
3321 * 4. drop the whole datagram if this function returns anything else.
3322 * Point 2 is needed when the peer is resending, and we have already received
3323 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003324 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003325static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003326 unsigned char *buf,
3327 size_t len,
3328 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003329{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003330 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003331
Hanno Beckere5e7e782019-07-11 12:29:35 +01003332 size_t const rec_hdr_type_offset = 0;
3333 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003334
Hanno Beckere5e7e782019-07-11 12:29:35 +01003335 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3336 rec_hdr_type_len;
3337 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Hanno Beckere5e7e782019-07-11 12:29:35 +01003339 size_t const rec_hdr_ctr_len = 8;
3340#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003341 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003342 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3343 rec_hdr_version_len;
3344
Hanno Beckera0e20d02019-05-15 14:03:01 +01003345#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003346 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3347 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003348 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003349#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3350#endif /* MBEDTLS_SSL_PROTO_DTLS */
3351
3352 size_t rec_hdr_len_offset; /* To be determined */
3353 size_t const rec_hdr_len_len = 2;
3354
3355 /*
3356 * Check minimum lengths for record header.
3357 */
3358
3359#if defined(MBEDTLS_SSL_PROTO_DTLS)
3360 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3361 {
3362 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3363 }
3364 else
3365#endif /* MBEDTLS_SSL_PROTO_DTLS */
3366 {
3367 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3368 }
3369
3370 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3371 {
3372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3373 (unsigned) len,
3374 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3375 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3376 }
3377
3378 /*
3379 * Parse and validate record content type
3380 */
3381
3382 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003383
3384 /* Check record content type */
3385#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3386 rec->cid_len = 0;
3387
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003388 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003389 ssl->conf->cid_len != 0 &&
3390 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003391 {
3392 /* Shift pointers to account for record header including CID
3393 * struct {
3394 * ContentType special_type = tls12_cid;
3395 * ProtocolVersion version;
3396 * uint16 epoch;
3397 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003398 * opaque cid[cid_length]; // Additional field compared to
3399 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003400 * uint16 length;
3401 * opaque enc_content[DTLSCiphertext.length];
3402 * } DTLSCiphertext;
3403 */
3404
3405 /* So far, we only support static CID lengths
3406 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003407 rec_hdr_cid_len = ssl->conf->cid_len;
3408 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003409
Hanno Beckere5e7e782019-07-11 12:29:35 +01003410 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003411 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3413 (unsigned) len,
3414 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003415 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003416 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003417
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003418 /* configured CID len is guaranteed at most 255, see
3419 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3420 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003421 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003422 }
3423 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003424#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003425 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003426 if( ssl_check_record_type( rec->type ) )
3427 {
Hanno Becker54229812019-07-12 14:40:00 +01003428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3429 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003430 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3431 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003432 }
3433
Hanno Beckere5e7e782019-07-11 12:29:35 +01003434 /*
3435 * Parse and validate record version
3436 */
3437
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003438 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3439 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003440 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3441 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003442 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003443
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003444 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3447 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 }
3449
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003450 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3453 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 }
3455
Hanno Beckere5e7e782019-07-11 12:29:35 +01003456 /*
3457 * Parse/Copy record sequence number.
3458 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003459
Hanno Beckere5e7e782019-07-11 12:29:35 +01003460#if defined(MBEDTLS_SSL_PROTO_DTLS)
3461 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003462 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003463 /* Copy explicit record sequence number from input buffer. */
3464 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3465 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003466 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003467 else
3468#endif /* MBEDTLS_SSL_PROTO_DTLS */
3469 {
3470 /* Copy implicit record sequence number from SSL context structure. */
3471 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3472 }
Paul Bakker40e46942009-01-03 21:51:57 +00003473
Hanno Beckere5e7e782019-07-11 12:29:35 +01003474 /*
3475 * Parse record length.
3476 */
3477
Hanno Beckere5e7e782019-07-11 12:29:35 +01003478 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003479 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3480 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003481 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003482
Paul Elliott9f352112020-12-09 14:55:45 +00003483 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003484 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003485 rec->type,
3486 major_ver, minor_ver, rec->data_len ) );
3487
3488 rec->buf = buf;
3489 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003490
Hanno Beckerd417cc92019-07-26 08:20:27 +01003491 if( rec->data_len == 0 )
3492 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003494 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003495 * DTLS-related tests.
3496 * Check epoch before checking length constraint because
3497 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3498 * message gets duplicated before the corresponding Finished message,
3499 * the second ChangeCipherSpec should be discarded because it belongs
3500 * to an old epoch, but not because its length is shorter than
3501 * the minimum record length for packets using the new record transform.
3502 * Note that these two kinds of failures are handled differently,
3503 * as an unexpected record is silently skipped but an invalid
3504 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003505 */
3506#if defined(MBEDTLS_SSL_PROTO_DTLS)
3507 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3508 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003509 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003510
Hanno Becker955a5c92019-07-10 17:12:07 +01003511 /* Check that the datagram is large enough to contain a record
3512 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003513 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003514 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3516 (unsigned) len,
3517 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003518 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3519 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003520
Hanno Becker37cfe732019-07-10 17:20:01 +01003521 /* Records from other, non-matching epochs are silently discarded.
3522 * (The case of same-port Client reconnects must be considered in
3523 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003524 if( rec_epoch != ssl->in_epoch )
3525 {
3526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003527 "expected %u, received %lu",
3528 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003529
Hanno Becker552f7472019-07-19 10:59:12 +01003530 /* Records from the next epoch are considered for buffering
3531 * (concretely: early Finished messages). */
3532 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003533 {
Hanno Becker552f7472019-07-19 10:59:12 +01003534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3535 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003536 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003537
Hanno Becker2fddd372019-07-10 14:37:41 +01003538 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003539 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003540#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003541 /* For records from the correct epoch, check whether their
3542 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003543 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3544 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003545 {
3546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3547 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3548 }
3549#endif
3550 }
3551#endif /* MBEDTLS_SSL_PROTO_DTLS */
3552
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003553 return( 0 );
3554}
Paul Bakker5121ce52009-01-03 21:22:43 +00003555
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
Hanno Becker2fddd372019-07-10 14:37:41 +01003557#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3558static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3559{
3560 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3561
3562 /*
3563 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3564 * access the first byte of record content (handshake type), as we
3565 * have an active transform (possibly iv_len != 0), so use the
3566 * fact that the record header len is 13 instead.
3567 */
3568 if( rec_epoch == 0 &&
3569 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3570 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3571 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3572 ssl->in_left > 13 &&
3573 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3574 {
3575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3576 "from the same port" ) );
3577 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 }
3579
3580 return( 0 );
3581}
Hanno Becker2fddd372019-07-10 14:37:41 +01003582#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003584/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003585 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003586 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003587static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3588 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003589{
3590 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003593 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594
Paul Bakker48916f92012-09-16 19:57:18 +00003595 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003597 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003598
Hanno Beckera18d1322018-01-03 14:27:32 +00003599 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003600 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003603
Hanno Beckera0e20d02019-05-15 14:03:01 +01003604#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003605 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3606 ssl->conf->ignore_unexpected_cid
3607 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3608 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003610 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003611 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003613
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 return( ret );
3615 }
3616
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003617 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003618 {
3619 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003620 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003621 }
3622
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003623 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003624 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003625
Hanno Beckera0e20d02019-05-15 14:03:01 +01003626#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003627 /* We have already checked the record content type
3628 * in ssl_parse_record_header(), failing or silently
3629 * dropping the record in the case of an unknown type.
3630 *
3631 * Since with the use of CIDs, the record content type
3632 * might change during decryption, re-check the record
3633 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003634 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003635 {
3636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3637 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3638 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003639#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003640
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003641 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003642 {
3643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3644 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003645 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003646 {
3647 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3648 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3649 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3650 }
3651#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3652
3653 ssl->nb_zero++;
3654
3655 /*
3656 * Three or more empty messages may be a DoS attack
3657 * (excessive CPU consumption).
3658 */
3659 if( ssl->nb_zero > 3 )
3660 {
3661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003662 "messages, possible DoS attack" ) );
3663 /* Treat the records as if they were not properly authenticated,
3664 * thereby failing the connection if we see more than allowed
3665 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003666 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3667 }
3668 }
3669 else
3670 ssl->nb_zero = 0;
3671
3672#if defined(MBEDTLS_SSL_PROTO_DTLS)
3673 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3674 {
3675 ; /* in_ctr read from peer, not maintained internally */
3676 }
3677 else
3678#endif
3679 {
3680 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003681 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003682 if( ++ssl->in_ctr[i - 1] != 0 )
3683 break;
3684
3685 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003686 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003687 {
3688 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3689 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3690 }
3691 }
3692
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 }
3694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003696 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003698 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003699 }
3700#endif
3701
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003702 /* Check actual (decrypted) record content length against
3703 * configured maximum. */
3704 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3705 {
3706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3707 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3708 }
3709
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003710 return( 0 );
3711}
3712
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003713/*
3714 * Read a record.
3715 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003716 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3717 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3718 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003719 */
Hanno Becker1097b342018-08-15 14:09:41 +01003720
3721/* Helper functions for mbedtls_ssl_read_record(). */
3722static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003723static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3724static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003725
Hanno Becker327c93b2018-08-15 13:56:18 +01003726int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003727 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003728{
Janos Follath865b3eb2019-12-16 11:46:15 +00003729 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003732
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003733 if( ssl->keep_current_message == 0 )
3734 {
3735 do {
Simon Butcher99000142016-10-13 17:21:01 +01003736
Hanno Becker26994592018-08-15 14:14:59 +01003737 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003738 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003739 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003740
Hanno Beckere74d5562018-08-15 14:26:08 +01003741 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003742 {
Hanno Becker40f50842018-08-15 14:48:01 +01003743#if defined(MBEDTLS_SSL_PROTO_DTLS)
3744 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003745
Hanno Becker40f50842018-08-15 14:48:01 +01003746 /* We only check for buffered messages if the
3747 * current datagram is fully consumed. */
3748 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003749 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003750 {
Hanno Becker40f50842018-08-15 14:48:01 +01003751 if( ssl_load_buffered_message( ssl ) == 0 )
3752 have_buffered = 1;
3753 }
3754
3755 if( have_buffered == 0 )
3756#endif /* MBEDTLS_SSL_PROTO_DTLS */
3757 {
3758 ret = ssl_get_next_record( ssl );
3759 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3760 continue;
3761
3762 if( ret != 0 )
3763 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003764 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003765 return( ret );
3766 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003767 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003768 }
3769
3770 ret = mbedtls_ssl_handle_message_type( ssl );
3771
Hanno Becker40f50842018-08-15 14:48:01 +01003772#if defined(MBEDTLS_SSL_PROTO_DTLS)
3773 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3774 {
3775 /* Buffer future message */
3776 ret = ssl_buffer_message( ssl );
3777 if( ret != 0 )
3778 return( ret );
3779
3780 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3781 }
3782#endif /* MBEDTLS_SSL_PROTO_DTLS */
3783
Hanno Becker90333da2017-10-10 11:27:13 +01003784 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3785 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003786
3787 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003788 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003789 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003790 return( ret );
3791 }
3792
Hanno Becker327c93b2018-08-15 13:56:18 +01003793 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003794 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003795 {
3796 mbedtls_ssl_update_handshake_status( ssl );
3797 }
Simon Butcher99000142016-10-13 17:21:01 +01003798 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003799 else
Simon Butcher99000142016-10-13 17:21:01 +01003800 {
Hanno Becker02f59072018-08-15 14:00:24 +01003801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003802 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003803 }
3804
3805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3806
3807 return( 0 );
3808}
3809
Hanno Becker40f50842018-08-15 14:48:01 +01003810#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003811static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003812{
Hanno Becker40f50842018-08-15 14:48:01 +01003813 if( ssl->in_left > ssl->next_record_offset )
3814 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003815
Hanno Becker40f50842018-08-15 14:48:01 +01003816 return( 0 );
3817}
3818
3819static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3820{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003821 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003822 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003823 int ret = 0;
3824
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003825 if( hs == NULL )
3826 return( -1 );
3827
Hanno Beckere00ae372018-08-20 09:39:42 +01003828 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3829
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003830 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3831 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3832 {
3833 /* Check if we have seen a ChangeCipherSpec before.
3834 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003835 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003836 {
3837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3838 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003839 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003840 }
3841
Hanno Becker39b8bc92018-08-28 17:17:13 +01003842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003843 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3844 ssl->in_msglen = 1;
3845 ssl->in_msg[0] = 1;
3846
3847 /* As long as they are equal, the exact value doesn't matter. */
3848 ssl->in_left = 0;
3849 ssl->next_record_offset = 0;
3850
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003851 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003852 goto exit;
3853 }
Hanno Becker37f95322018-08-16 13:55:32 +01003854
Hanno Beckerb8f50142018-08-28 10:01:34 +01003855#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003856 /* Debug only */
3857 {
3858 unsigned offset;
3859 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3860 {
3861 hs_buf = &hs->buffering.hs[offset];
3862 if( hs_buf->is_valid == 1 )
3863 {
3864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3865 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003866 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003867 }
3868 }
3869 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003870#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003871
3872 /* Check if we have buffered and/or fully reassembled the
3873 * next handshake message. */
3874 hs_buf = &hs->buffering.hs[0];
3875 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3876 {
3877 /* Synthesize a record containing the buffered HS message. */
3878 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3879 ( hs_buf->data[2] << 8 ) |
3880 hs_buf->data[3];
3881
3882 /* Double-check that we haven't accidentally buffered
3883 * a message that doesn't fit into the input buffer. */
3884 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3885 {
3886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3887 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3888 }
3889
3890 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3891 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3892 hs_buf->data, msg_len + 12 );
3893
3894 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3895 ssl->in_hslen = msg_len + 12;
3896 ssl->in_msglen = msg_len + 12;
3897 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3898
3899 ret = 0;
3900 goto exit;
3901 }
3902 else
3903 {
3904 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3905 hs->in_msg_seq ) );
3906 }
3907
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003908 ret = -1;
3909
3910exit:
3911
3912 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3913 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003914}
3915
Hanno Beckera02b0b42018-08-21 17:20:27 +01003916static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3917 size_t desired )
3918{
3919 int offset;
3920 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3922 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003923
Hanno Becker01315ea2018-08-21 17:22:17 +01003924 /* Get rid of future records epoch first, if such exist. */
3925 ssl_free_buffered_record( ssl );
3926
3927 /* Check if we have enough space available now. */
3928 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3929 hs->buffering.total_bytes_buffered ) )
3930 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003932 return( 0 );
3933 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003934
Hanno Becker4f432ad2018-08-28 10:02:32 +01003935 /* We don't have enough space to buffer the next expected handshake
3936 * message. Remove buffers used for future messages to gain space,
3937 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003938 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3939 offset >= 0; offset-- )
3940 {
3941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3942 offset ) );
3943
Hanno Beckerb309b922018-08-23 13:18:05 +01003944 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003945
3946 /* Check if we have enough space available now. */
3947 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3948 hs->buffering.total_bytes_buffered ) )
3949 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003951 return( 0 );
3952 }
3953 }
3954
3955 return( -1 );
3956}
3957
Hanno Becker40f50842018-08-15 14:48:01 +01003958static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3959{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003960 int ret = 0;
3961 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3962
3963 if( hs == NULL )
3964 return( 0 );
3965
3966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3967
3968 switch( ssl->in_msgtype )
3969 {
3970 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3971 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003972
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003973 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003974 break;
3975
3976 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003977 {
3978 unsigned recv_msg_seq_offset;
3979 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3980 mbedtls_ssl_hs_buffer *hs_buf;
3981 size_t msg_len = ssl->in_hslen - 12;
3982
3983 /* We should never receive an old handshake
3984 * message - double-check nonetheless. */
3985 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3986 {
3987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3988 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3989 }
3990
3991 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3992 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3993 {
3994 /* Silently ignore -- message too far in the future */
3995 MBEDTLS_SSL_DEBUG_MSG( 2,
3996 ( "Ignore future HS message with sequence number %u, "
3997 "buffering window %u - %u",
3998 recv_msg_seq, ssl->handshake->in_msg_seq,
3999 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4000
4001 goto exit;
4002 }
4003
4004 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4005 recv_msg_seq, recv_msg_seq_offset ) );
4006
4007 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4008
4009 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004010 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004011 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004012 size_t reassembly_buf_sz;
4013
Hanno Becker37f95322018-08-16 13:55:32 +01004014 hs_buf->is_fragmented =
4015 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4016
4017 /* We copy the message back into the input buffer
4018 * after reassembly, so check that it's not too large.
4019 * This is an implementation-specific limitation
4020 * and not one from the standard, hence it is not
4021 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004022 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004023 {
4024 /* Ignore message */
4025 goto exit;
4026 }
4027
Hanno Beckere0b150f2018-08-21 15:51:03 +01004028 /* Check if we have enough space to buffer the message. */
4029 if( hs->buffering.total_bytes_buffered >
4030 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4031 {
4032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4033 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4034 }
4035
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004036 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4037 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004038
4039 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4040 hs->buffering.total_bytes_buffered ) )
4041 {
4042 if( recv_msg_seq_offset > 0 )
4043 {
4044 /* If we can't buffer a future message because
4045 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004046 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4047 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4048 " (already %" MBEDTLS_PRINTF_SIZET
4049 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004050 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004051 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004052 goto exit;
4053 }
Hanno Beckere1801392018-08-21 16:51:05 +01004054 else
4055 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004056 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4057 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4058 " (already %" MBEDTLS_PRINTF_SIZET
4059 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004060 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004061 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01004062 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004063
Hanno Beckera02b0b42018-08-21 17:20:27 +01004064 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004065 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
4067 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
4068 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
4069 " (already %" MBEDTLS_PRINTF_SIZET
4070 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00004071 msg_len,
4072 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00004073 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004074 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004075 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4076 goto exit;
4077 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004078 }
4079
Paul Elliottd48d5c62021-01-07 14:47:05 +00004080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004081 msg_len ) );
4082
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004083 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4084 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004085 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004086 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004087 goto exit;
4088 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004089 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004090
4091 /* Prepare final header: copy msg_type, length and message_seq,
4092 * then add standardised fragment_offset and fragment_length */
4093 memcpy( hs_buf->data, ssl->in_msg, 6 );
4094 memset( hs_buf->data + 6, 0, 3 );
4095 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4096
4097 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004098
4099 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004100 }
4101 else
4102 {
4103 /* Make sure msg_type and length are consistent */
4104 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4105 {
4106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4107 /* Ignore */
4108 goto exit;
4109 }
4110 }
4111
Hanno Becker4422bbb2018-08-20 09:40:19 +01004112 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004113 {
4114 size_t frag_len, frag_off;
4115 unsigned char * const msg = hs_buf->data + 12;
4116
4117 /*
4118 * Check and copy current fragment
4119 */
4120
4121 /* Validation of header fields already done in
4122 * mbedtls_ssl_prepare_handshake_record(). */
4123 frag_off = ssl_get_hs_frag_off( ssl );
4124 frag_len = ssl_get_hs_frag_len( ssl );
4125
Paul Elliottd48d5c62021-01-07 14:47:05 +00004126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4127 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004128 frag_off, frag_len ) );
4129 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4130
4131 if( hs_buf->is_fragmented )
4132 {
4133 unsigned char * const bitmask = msg + msg_len;
4134 ssl_bitmask_set( bitmask, frag_off, frag_len );
4135 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4136 msg_len ) == 0 );
4137 }
4138 else
4139 {
4140 hs_buf->is_complete = 1;
4141 }
4142
4143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4144 hs_buf->is_complete ? "" : "not yet " ) );
4145 }
4146
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004147 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004148 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004149
4150 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004151 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004152 break;
4153 }
4154
4155exit:
4156
4157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4158 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004159}
4160#endif /* MBEDTLS_SSL_PROTO_DTLS */
4161
Hanno Becker1097b342018-08-15 14:09:41 +01004162static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004163{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004164 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004165 * Consume last content-layer message and potentially
4166 * update in_msglen which keeps track of the contents'
4167 * consumption state.
4168 *
4169 * (1) Handshake messages:
4170 * Remove last handshake message, move content
4171 * and adapt in_msglen.
4172 *
4173 * (2) Alert messages:
4174 * Consume whole record content, in_msglen = 0.
4175 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004176 * (3) Change cipher spec:
4177 * Consume whole record content, in_msglen = 0.
4178 *
4179 * (4) Application data:
4180 * Don't do anything - the record layer provides
4181 * the application data as a stream transport
4182 * and consumes through mbedtls_ssl_read only.
4183 *
4184 */
4185
4186 /* Case (1): Handshake messages */
4187 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004188 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004189 /* Hard assertion to be sure that no application data
4190 * is in flight, as corrupting ssl->in_msglen during
4191 * ssl->in_offt != NULL is fatal. */
4192 if( ssl->in_offt != NULL )
4193 {
4194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4195 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4196 }
4197
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004198 /*
4199 * Get next Handshake message in the current record
4200 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004201
Hanno Becker4a810fb2017-05-24 16:27:30 +01004202 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004203 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004204 * current handshake content: If DTLS handshake
4205 * fragmentation is used, that's the fragment
4206 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004207 * size here is faulty and should be changed at
4208 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004209 * (2) While it doesn't seem to cause problems, one
4210 * has to be very careful not to assume that in_hslen
4211 * is always <= in_msglen in a sensible communication.
4212 * Again, it's wrong for DTLS handshake fragmentation.
4213 * The following check is therefore mandatory, and
4214 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004215 * Additionally, ssl->in_hslen might be arbitrarily out of
4216 * bounds after handling a DTLS message with an unexpected
4217 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004218 */
4219 if( ssl->in_hslen < ssl->in_msglen )
4220 {
4221 ssl->in_msglen -= ssl->in_hslen;
4222 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4223 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004224
Hanno Becker4a810fb2017-05-24 16:27:30 +01004225 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4226 ssl->in_msg, ssl->in_msglen );
4227 }
4228 else
4229 {
4230 ssl->in_msglen = 0;
4231 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004232
Hanno Becker4a810fb2017-05-24 16:27:30 +01004233 ssl->in_hslen = 0;
4234 }
4235 /* Case (4): Application data */
4236 else if( ssl->in_offt != NULL )
4237 {
4238 return( 0 );
4239 }
4240 /* Everything else (CCS & Alerts) */
4241 else
4242 {
4243 ssl->in_msglen = 0;
4244 }
4245
Hanno Becker1097b342018-08-15 14:09:41 +01004246 return( 0 );
4247}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004248
Hanno Beckere74d5562018-08-15 14:26:08 +01004249static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4250{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004251 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004252 return( 1 );
4253
4254 return( 0 );
4255}
4256
Hanno Becker5f066e72018-08-16 14:56:31 +01004257#if defined(MBEDTLS_SSL_PROTO_DTLS)
4258
4259static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4260{
4261 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4262 if( hs == NULL )
4263 return;
4264
Hanno Becker01315ea2018-08-21 17:22:17 +01004265 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004266 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004267 hs->buffering.total_bytes_buffered -=
4268 hs->buffering.future_record.len;
4269
4270 mbedtls_free( hs->buffering.future_record.data );
4271 hs->buffering.future_record.data = NULL;
4272 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004273}
4274
4275static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4276{
4277 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4278 unsigned char * rec;
4279 size_t rec_len;
4280 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004281#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4282 size_t in_buf_len = ssl->in_buf_len;
4283#else
4284 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4285#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004286 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4287 return( 0 );
4288
4289 if( hs == NULL )
4290 return( 0 );
4291
Hanno Becker5f066e72018-08-16 14:56:31 +01004292 rec = hs->buffering.future_record.data;
4293 rec_len = hs->buffering.future_record.len;
4294 rec_epoch = hs->buffering.future_record.epoch;
4295
4296 if( rec == NULL )
4297 return( 0 );
4298
Hanno Becker4cb782d2018-08-20 11:19:05 +01004299 /* Only consider loading future records if the
4300 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004301 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004302 return( 0 );
4303
Hanno Becker5f066e72018-08-16 14:56:31 +01004304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4305
4306 if( rec_epoch != ssl->in_epoch )
4307 {
4308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4309 goto exit;
4310 }
4311
4312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4313
4314 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004315 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004316 {
4317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4318 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4319 }
4320
4321 memcpy( ssl->in_hdr, rec, rec_len );
4322 ssl->in_left = rec_len;
4323 ssl->next_record_offset = 0;
4324
4325 ssl_free_buffered_record( ssl );
4326
4327exit:
4328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4329 return( 0 );
4330}
4331
Hanno Becker519f15d2019-07-11 12:43:20 +01004332static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4333 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004334{
4335 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004336
4337 /* Don't buffer future records outside handshakes. */
4338 if( hs == NULL )
4339 return( 0 );
4340
4341 /* Only buffer handshake records (we are only interested
4342 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004343 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004344 return( 0 );
4345
4346 /* Don't buffer more than one future epoch record. */
4347 if( hs->buffering.future_record.data != NULL )
4348 return( 0 );
4349
Hanno Becker01315ea2018-08-21 17:22:17 +01004350 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004351 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004352 hs->buffering.total_bytes_buffered ) )
4353 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4355 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4356 " (already %" MBEDTLS_PRINTF_SIZET
4357 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004358 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004359 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004360 return( 0 );
4361 }
4362
Hanno Becker5f066e72018-08-16 14:56:31 +01004363 /* Buffer record */
4364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004365 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004366 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004367
4368 /* ssl_parse_record_header() only considers records
4369 * of the next epoch as candidates for buffering. */
4370 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004371 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004372
4373 hs->buffering.future_record.data =
4374 mbedtls_calloc( 1, hs->buffering.future_record.len );
4375 if( hs->buffering.future_record.data == NULL )
4376 {
4377 /* If we run out of RAM trying to buffer a
4378 * record from the next epoch, just ignore. */
4379 return( 0 );
4380 }
4381
Hanno Becker519f15d2019-07-11 12:43:20 +01004382 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004383
Hanno Becker519f15d2019-07-11 12:43:20 +01004384 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004385 return( 0 );
4386}
4387
4388#endif /* MBEDTLS_SSL_PROTO_DTLS */
4389
Hanno Beckere74d5562018-08-15 14:26:08 +01004390static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004391{
Janos Follath865b3eb2019-12-16 11:46:15 +00004392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004393 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004394
Hanno Becker5f066e72018-08-16 14:56:31 +01004395#if defined(MBEDTLS_SSL_PROTO_DTLS)
4396 /* We might have buffered a future record; if so,
4397 * and if the epoch matches now, load it.
4398 * On success, this call will set ssl->in_left to
4399 * the length of the buffered record, so that
4400 * the calls to ssl_fetch_input() below will
4401 * essentially be no-ops. */
4402 ret = ssl_load_buffered_record( ssl );
4403 if( ret != 0 )
4404 return( ret );
4405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004406
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004407 /* Ensure that we have enough space available for the default form
4408 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4409 * with no space for CIDs counted in). */
4410 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4411 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004414 return( ret );
4415 }
4416
Hanno Beckere5e7e782019-07-11 12:29:35 +01004417 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4418 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004422 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004423 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4424 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004425 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004426 if( ret != 0 )
4427 return( ret );
4428
4429 /* Fall through to handling of unexpected records */
4430 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4431 }
4432
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004433 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4434 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004435#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004436 /* Reset in pointers to default state for TLS/DTLS records,
4437 * assuming no CID and no offset between record content and
4438 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004439 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004440
Hanno Becker7ae20e02019-07-12 08:33:49 +01004441 /* Setup internal message pointers from record structure. */
4442 ssl->in_msgtype = rec.type;
4443#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4444 ssl->in_len = ssl->in_cid + rec.cid_len;
4445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4446 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4447 ssl->in_msglen = rec.data_len;
4448
Hanno Becker2fddd372019-07-10 14:37:41 +01004449 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004450 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004451 if( ret != 0 )
4452 return( ret );
4453#endif
4454
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004455 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004456 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004457
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4459 "(header)" ) );
4460 }
4461 else
4462 {
4463 /* Skip invalid record and the rest of the datagram */
4464 ssl->next_record_offset = 0;
4465 ssl->in_left = 0;
4466
4467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4468 "(header)" ) );
4469 }
4470
4471 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004472 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004473 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004474 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004475#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004476 {
4477 return( ret );
4478 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004479 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004482 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004483 {
Hanno Beckera8814792019-07-10 15:01:45 +01004484 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004485 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004486 if( ssl->next_record_offset < ssl->in_left )
4487 {
4488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4489 }
4490 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004491 else
4492#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004493 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004494 /*
4495 * Fetch record contents from underlying transport.
4496 */
Hanno Beckera3175662019-07-11 12:50:29 +01004497 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004498 if( ret != 0 )
4499 {
4500 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4501 return( ret );
4502 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004503
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004504 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004505 }
4506
4507 /*
4508 * Decrypt record contents.
4509 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004510
Hanno Beckerfdf66042019-07-11 13:07:45 +01004511 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004513#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004514 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004515 {
4516 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004517 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004518 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004519 /* Except when waiting for Finished as a bad mac here
4520 * probably means something went wrong in the handshake
4521 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4522 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4523 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4524 {
4525#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4526 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4527 {
4528 mbedtls_ssl_send_alert_message( ssl,
4529 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4530 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4531 }
4532#endif
4533 return( ret );
4534 }
4535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004536#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004537 if( ssl->conf->badmac_limit != 0 &&
4538 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4541 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004542 }
4543#endif
4544
Hanno Becker4a810fb2017-05-24 16:27:30 +01004545 /* As above, invalid records cause
4546 * dismissal of the whole datagram. */
4547
4548 ssl->next_record_offset = 0;
4549 ssl->in_left = 0;
4550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004552 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004553 }
4554
4555 return( ret );
4556 }
4557 else
4558#endif
4559 {
4560 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004561#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4562 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004563 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564 mbedtls_ssl_send_alert_message( ssl,
4565 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4566 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004567 }
4568#endif
4569 return( ret );
4570 }
4571 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004572
Hanno Becker44d89b22019-07-12 09:40:44 +01004573
4574 /* Reset in pointers to default state for TLS/DTLS records,
4575 * assuming no CID and no offset between record content and
4576 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004577 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004578#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4579 ssl->in_len = ssl->in_cid + rec.cid_len;
4580#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004581 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004582
Hanno Becker8685c822019-07-12 09:37:30 +01004583 /* The record content type may change during decryption,
4584 * so re-read it. */
4585 ssl->in_msgtype = rec.type;
4586 /* Also update the input buffer, because unfortunately
4587 * the server-side ssl_parse_client_hello() reparses the
4588 * record header when receiving a ClientHello initiating
4589 * a renegotiation. */
4590 ssl->in_hdr[0] = rec.type;
4591 ssl->in_msg = rec.buf + rec.data_offset;
4592 ssl->in_msglen = rec.data_len;
4593 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4594 ssl->in_len[1] = (unsigned char)( rec.data_len );
4595
Simon Butcher99000142016-10-13 17:21:01 +01004596 return( 0 );
4597}
4598
4599int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4600{
Janos Follath865b3eb2019-12-16 11:46:15 +00004601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004602
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004603 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004604 * Handle particular types of records
4605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004606 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004607 {
Simon Butcher99000142016-10-13 17:21:01 +01004608 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4609 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004610 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004611 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 }
4613
Hanno Beckere678eaa2018-08-21 14:57:46 +01004614 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004615 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004616 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004617 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004619 ssl->in_msglen ) );
4620 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004621 }
4622
Hanno Beckere678eaa2018-08-21 14:57:46 +01004623 if( ssl->in_msg[0] != 1 )
4624 {
4625 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4626 ssl->in_msg[0] ) );
4627 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4628 }
4629
4630#if defined(MBEDTLS_SSL_PROTO_DTLS)
4631 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4632 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4633 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4634 {
4635 if( ssl->handshake == NULL )
4636 {
4637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4638 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4639 }
4640
4641 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4642 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4643 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004644#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004645 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004647 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004648 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004649 if( ssl->in_msglen != 2 )
4650 {
4651 /* Note: Standard allows for more than one 2 byte alert
4652 to be packed in a single message, but Mbed TLS doesn't
4653 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004655 ssl->in_msglen ) );
4656 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4657 }
4658
Paul Elliott9f352112020-12-09 14:55:45 +00004659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 ssl->in_msg[0], ssl->in_msg[1] ) );
4661
4662 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004663 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004665 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004668 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 }
4671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004672 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4673 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4676 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004678
4679#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4680 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4681 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4682 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004684 /* Will be handled when trying to parse ServerHello */
4685 return( 0 );
4686 }
4687#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004688 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004689 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004690 }
4691
Hanno Beckerc76c6192017-06-06 10:03:17 +01004692#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004693 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004694 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004695 /* Drop unexpected ApplicationData records,
4696 * except at the beginning of renegotiations */
4697 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4698 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4699#if defined(MBEDTLS_SSL_RENEGOTIATION)
4700 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4701 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004702#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004703 )
4704 {
4705 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4706 return( MBEDTLS_ERR_SSL_NON_FATAL );
4707 }
4708
4709 if( ssl->handshake != NULL &&
4710 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4711 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004712 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004713 }
4714 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004715#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004716
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 return( 0 );
4718}
4719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004721{
irwir6c0da642019-09-26 21:07:41 +03004722 return( mbedtls_ssl_send_alert_message( ssl,
4723 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4724 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004725}
4726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004728 unsigned char level,
4729 unsigned char message )
4730{
Janos Follath865b3eb2019-12-16 11:46:15 +00004731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004732
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004733 if( ssl == NULL || ssl->conf == NULL )
4734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004739 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004740 ssl->out_msglen = 2;
4741 ssl->out_msg[0] = level;
4742 ssl->out_msg[1] = message;
4743
Hanno Becker67bc7c32018-08-06 11:33:50 +01004744 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004746 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004747 return( ret );
4748 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004750
4751 return( 0 );
4752}
4753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004754int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004755{
Janos Follath865b3eb2019-12-16 11:46:15 +00004756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004760 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 ssl->out_msglen = 1;
4762 ssl->out_msg[0] = 1;
4763
Paul Bakker5121ce52009-01-03 21:22:43 +00004764 ssl->state++;
4765
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004766 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004767 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004768 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004769 return( ret );
4770 }
4771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004773
4774 return( 0 );
4775}
4776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004777int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004778{
Janos Follath865b3eb2019-12-16 11:46:15 +00004779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004782
Hanno Becker327c93b2018-08-15 13:56:18 +01004783 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 return( ret );
4787 }
4788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004792 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4793 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004794 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004795 }
4796
Hanno Beckere678eaa2018-08-21 14:57:46 +01004797 /* CCS records are only accepted if they have length 1 and content '1',
4798 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004799
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004800 /*
4801 * Switch to our negotiated transform and session parameters for inbound
4802 * data.
4803 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004804 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004805 ssl->transform_in = ssl->transform_negotiate;
4806 ssl->session_in = ssl->session_negotiate;
4807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004808#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004809 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004812 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004813#endif
4814
4815 /* Increment epoch */
4816 if( ++ssl->in_epoch == 0 )
4817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004818 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004819 /* This is highly unlikely to happen for legitimate reasons, so
4820 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004821 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004822 }
4823 }
4824 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004825#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004826 memset( ssl->in_ctr, 0, 8 );
4827
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004828 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004829
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 ssl->state++;
4831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004833
4834 return( 0 );
4835}
4836
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004837/* Once ssl->out_hdr as the address of the beginning of the
4838 * next outgoing record is set, deduce the other pointers.
4839 *
4840 * Note: For TLS, we save the implicit record sequence number
4841 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4842 * and the caller has to make sure there's space for this.
4843 */
4844
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004845static size_t ssl_transform_get_explicit_iv_len(
4846 mbedtls_ssl_transform const *transform )
4847{
4848 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
4849 return( 0 );
4850
4851 return( transform->ivlen - transform->fixed_ivlen );
4852}
4853
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004854void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4855 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004856{
4857#if defined(MBEDTLS_SSL_PROTO_DTLS)
4858 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4859 {
4860 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004862 ssl->out_cid = ssl->out_ctr + 8;
4863 ssl->out_len = ssl->out_cid;
4864 if( transform != NULL )
4865 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004866#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004867 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004868#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004869 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004870 }
4871 else
4872#endif
4873 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004874 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004875#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004876 ssl->out_cid = ssl->out_len;
4877#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004878 ssl->out_iv = ssl->out_hdr + 5;
4879 }
4880
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004881 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004882 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004883 if( transform != NULL )
4884 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004885}
4886
4887/* Once ssl->in_hdr as the address of the beginning of the
4888 * next incoming record is set, deduce the other pointers.
4889 *
4890 * Note: For TLS, we save the implicit record sequence number
4891 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4892 * and the caller has to make sure there's space for this.
4893 */
4894
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004895void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004896{
Hanno Becker79594fd2019-05-08 09:38:41 +01004897 /* This function sets the pointers to match the case
4898 * of unprotected TLS/DTLS records, with both ssl->in_iv
4899 * and ssl->in_msg pointing to the beginning of the record
4900 * content.
4901 *
4902 * When decrypting a protected record, ssl->in_msg
4903 * will be shifted to point to the beginning of the
4904 * record plaintext.
4905 */
4906
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004907#if defined(MBEDTLS_SSL_PROTO_DTLS)
4908 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4909 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004910 /* This sets the header pointers to match records
4911 * without CID. When we receive a record containing
4912 * a CID, the fields are shifted accordingly in
4913 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004914 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004915#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004916 ssl->in_cid = ssl->in_ctr + 8;
4917 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004918#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004919 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004920#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004921 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004922 }
4923 else
4924#endif
4925 {
4926 ssl->in_ctr = ssl->in_hdr - 8;
4927 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004928#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004929 ssl->in_cid = ssl->in_len;
4930#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004931 ssl->in_iv = ssl->in_hdr + 5;
4932 }
4933
Hanno Becker79594fd2019-05-08 09:38:41 +01004934 /* This will be adjusted at record decryption time. */
4935 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004936}
4937
Paul Bakker5121ce52009-01-03 21:22:43 +00004938/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004939 * Setup an SSL context
4940 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004941
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004942void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004943{
4944 /* Set the incoming and outgoing record pointers. */
4945#if defined(MBEDTLS_SSL_PROTO_DTLS)
4946 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4947 {
4948 ssl->out_hdr = ssl->out_buf;
4949 ssl->in_hdr = ssl->in_buf;
4950 }
4951 else
4952#endif /* MBEDTLS_SSL_PROTO_DTLS */
4953 {
Hanno Becker12078f42021-03-02 15:28:41 +00004954 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004955 ssl->out_hdr = ssl->out_buf + 8;
4956 ssl->in_hdr = ssl->in_buf + 8;
4957 }
4958
4959 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004960 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4961 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004962}
4963
Paul Bakker5121ce52009-01-03 21:22:43 +00004964/*
4965 * SSL get accessors
4966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004967size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004968{
4969 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4970}
4971
Hanno Becker8b170a02017-10-10 11:51:19 +01004972int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4973{
4974 /*
4975 * Case A: We're currently holding back
4976 * a message for further processing.
4977 */
4978
4979 if( ssl->keep_current_message == 1 )
4980 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004982 return( 1 );
4983 }
4984
4985 /*
4986 * Case B: Further records are pending in the current datagram.
4987 */
4988
4989#if defined(MBEDTLS_SSL_PROTO_DTLS)
4990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4991 ssl->in_left > ssl->next_record_offset )
4992 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004994 return( 1 );
4995 }
4996#endif /* MBEDTLS_SSL_PROTO_DTLS */
4997
4998 /*
4999 * Case C: A handshake message is being processed.
5000 */
5001
Hanno Becker8b170a02017-10-10 11:51:19 +01005002 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5003 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005004 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005005 return( 1 );
5006 }
5007
5008 /*
5009 * Case D: An application data message is being processed
5010 */
5011 if( ssl->in_offt != NULL )
5012 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005013 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005014 return( 1 );
5015 }
5016
5017 /*
5018 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005019 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005020 * we implement support for multiple alerts in single records.
5021 */
5022
5023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5024 return( 0 );
5025}
5026
Paul Bakker43ca69c2011-01-15 17:35:19 +00005027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005029{
Hanno Becker3136ede2018-08-17 15:28:19 +01005030 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005031 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005032 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005033
Hanno Becker5903de42019-05-03 14:46:38 +01005034 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5035
Hanno Becker78640902018-08-13 16:35:15 +01005036 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005037 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005039 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005041 case MBEDTLS_MODE_GCM:
5042 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005043 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005044 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005045 transform_expansion = transform->minlen;
5046 break;
5047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005048 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005049
5050 block_size = mbedtls_cipher_get_block_size(
5051 &transform->cipher_ctx_enc );
5052
Hanno Becker3136ede2018-08-17 15:28:19 +01005053 /* Expansion due to the addition of the MAC. */
5054 transform_expansion += transform->maclen;
5055
5056 /* Expansion due to the addition of CBC padding;
5057 * Theoretically up to 256 bytes, but we never use
5058 * more than the block size of the underlying cipher. */
5059 transform_expansion += block_size;
5060
5061 /* For TLS 1.1 or higher, an explicit IV is added
5062 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005063#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5064 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005065 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005066#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005067
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005068 break;
5069
5070 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005073 }
5074
Hanno Beckera0e20d02019-05-15 14:03:01 +01005075#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005076 if( transform->out_cid_len != 0 )
5077 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005078#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005079
Hanno Becker5903de42019-05-03 14:46:38 +01005080 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005081}
5082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005084/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005085 * Check record counters and renegotiate if they're above the limit.
5086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005088{
Hanno Beckerdd772292020-02-05 10:38:31 +00005089 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005090 int in_ctr_cmp;
5091 int out_ctr_cmp;
5092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005093 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5094 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005095 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005096 {
5097 return( 0 );
5098 }
5099
Andres AG2196c7f2016-12-15 17:01:16 +00005100 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5101 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005102 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005103 ssl->conf->renego_period + ep_len, 8 - ep_len );
5104
5105 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005106 {
5107 return( 0 );
5108 }
5109
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005113#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005114
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005115/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005116 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005117 * may only be sent for the purpose of initiating renegotiations.
5118 *
5119 * This function is introduced as a separate helper since the handling
5120 * of post-handshake handshake messages changes significantly in TLS 1.3,
5121 * and having a helper function allows to distinguish between TLS <= 1.2 and
5122 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5123 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00005124static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005125{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005127
5128 /*
5129 * - For client-side, expect SERVER_HELLO_REQUEST.
5130 * - For server-side, expect CLIENT_HELLO.
5131 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5132 */
5133
5134#if defined(MBEDTLS_SSL_CLI_C)
5135 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5136 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5137 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
5138 {
5139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
5140
5141 /* With DTLS, drop the packet (probably from last handshake) */
5142#if defined(MBEDTLS_SSL_PROTO_DTLS)
5143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5144 {
5145 return( 0 );
5146 }
5147#endif
5148 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5149 }
5150#endif /* MBEDTLS_SSL_CLI_C */
5151
5152#if defined(MBEDTLS_SSL_SRV_C)
5153 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5154 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
5155 {
5156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5157
5158 /* With DTLS, drop the packet (probably from last handshake) */
5159#if defined(MBEDTLS_SSL_PROTO_DTLS)
5160 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5161 {
5162 return( 0 );
5163 }
5164#endif
5165 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5166 }
5167#endif /* MBEDTLS_SSL_SRV_C */
5168
5169#if defined(MBEDTLS_SSL_RENEGOTIATION)
5170 /* Determine whether renegotiation attempt should be accepted */
5171 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5172 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5173 ssl->conf->allow_legacy_renegotiation ==
5174 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5175 {
5176 /*
5177 * Accept renegotiation request
5178 */
5179
5180 /* DTLS clients need to know renego is server-initiated */
5181#if defined(MBEDTLS_SSL_PROTO_DTLS)
5182 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5183 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5184 {
5185 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5186 }
5187#endif
5188 ret = mbedtls_ssl_start_renegotiation( ssl );
5189 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5190 ret != 0 )
5191 {
5192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5193 ret );
5194 return( ret );
5195 }
5196 }
5197 else
5198#endif /* MBEDTLS_SSL_RENEGOTIATION */
5199 {
5200 /*
5201 * Refuse renegotiation
5202 */
5203
5204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5205
5206#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5207 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5208 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
5209 {
5210 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5211 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5212 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5213 {
5214 return( ret );
5215 }
5216 }
5217 else
5218#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5219 MBEDTLS_SSL_PROTO_TLS1_2 */
5220 {
5221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5222 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5223 }
5224 }
5225
5226 return( 0 );
5227}
5228
Paul Bakker48916f92012-09-16 19:57:18 +00005229/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005230 * Receive application data decrypted from the SSL layer
5231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005233{
Janos Follath865b3eb2019-12-16 11:46:15 +00005234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005235 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005237 if( ssl == NULL || ssl->conf == NULL )
5238 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005242#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005243 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005245 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005246 return( ret );
5247
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005248 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005249 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005250 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005251 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005252 return( ret );
5253 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005254 }
5255#endif
5256
Hanno Becker4a810fb2017-05-24 16:27:30 +01005257 /*
5258 * Check if renegotiation is necessary and/or handshake is
5259 * in process. If yes, perform/continue, and fall through
5260 * if an unexpected packet is received while the client
5261 * is waiting for the ServerHello.
5262 *
5263 * (There is no equivalent to the last condition on
5264 * the server-side as it is not treated as within
5265 * a handshake while waiting for the ClientHello
5266 * after a renegotiation request.)
5267 */
5268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005269#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005270 ret = ssl_check_ctr_renegotiate( ssl );
5271 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5272 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005274 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005275 return( ret );
5276 }
5277#endif
5278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005281 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005282 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5283 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005286 return( ret );
5287 }
5288 }
5289
Hanno Beckere41158b2017-10-23 13:30:32 +01005290 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005291 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005292 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005293 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005294 if( ssl->f_get_timer != NULL &&
5295 ssl->f_get_timer( ssl->p_timer ) == -1 )
5296 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005297 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005298 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005299
Hanno Becker327c93b2018-08-15 13:56:18 +01005300 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005301 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005302 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5303 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005304
Hanno Becker4a810fb2017-05-24 16:27:30 +01005305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5306 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005307 }
5308
5309 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005310 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005311 {
5312 /*
5313 * OpenSSL sends empty messages to randomize the IV
5314 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005315 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005317 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005318 return( 0 );
5319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321 return( ret );
5322 }
5323 }
5324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005325 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005326 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005327 ret = ssl_handle_hs_message_post_handshake( ssl );
5328 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005329 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005330 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5331 ret );
5332 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005333 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005334
Hanno Beckerf26cc722021-04-21 07:30:13 +01005335 /* At this point, we don't know whether the renegotiation triggered
5336 * by the post-handshake message has been completed or not. The cases
5337 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005338 * 1) The renegotiation is complete. In this case, no new record
5339 * has been read yet.
5340 * 2) The renegotiation is incomplete because the client received
5341 * an application data record while awaiting the ServerHello.
5342 * 3) The renegotiation is incomplete because the client received
5343 * a non-handshake, non-application data message while awaiting
5344 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005345 *
5346 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005347 * - For 1), the next iteration will read a new record and check
5348 * if it's application data.
5349 * - For 2), the loop condition isn't satisfied as application data
5350 * is present, hence continue is the same as break
5351 * - For 3), the loop condition is satisfied and read_record
5352 * will re-deliver the message that was held back by the client
5353 * when expecting the ServerHello.
5354 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005355
Hanno Becker90333da2017-10-10 11:27:13 +01005356 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005357 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005358#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005360 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005361 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005362 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005363 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005366 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005368 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005369 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5374 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005377 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005378 }
5379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005380 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5383 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005384 }
5385
5386 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005387
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005388 /* We're going to return something now, cancel timer,
5389 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005391 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005392
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005394 /* If we requested renego but received AppData, resend HelloRequest.
5395 * Do it now, after setting in_offt, to avoid taking this branch
5396 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005398 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005400 {
Hanno Becker786300f2020-02-05 10:46:40 +00005401 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005402 {
Hanno Becker786300f2020-02-05 10:46:40 +00005403 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5404 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005405 return( ret );
5406 }
5407 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005408#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005409#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005410 }
5411
5412 n = ( len < ssl->in_msglen )
5413 ? len : ssl->in_msglen;
5414
5415 memcpy( buf, ssl->in_offt, n );
5416 ssl->in_msglen -= n;
5417
gabor-mezei-arma3214132020-07-15 10:55:00 +02005418 /* Zeroising the plaintext buffer to erase unused application data
5419 from the memory. */
5420 mbedtls_platform_zeroize( ssl->in_offt, n );
5421
Paul Bakker5121ce52009-01-03 21:22:43 +00005422 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005423 {
5424 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005425 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005426 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005427 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005428 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005429 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005430 /* more data available */
5431 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005434 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005435
Paul Bakker23986e52011-04-24 08:57:21 +00005436 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005437}
5438
5439/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005440 * Send application data to be encrypted by the SSL layer, taking care of max
5441 * fragment length and buffer size.
5442 *
5443 * According to RFC 5246 Section 6.2.1:
5444 *
5445 * Zero-length fragments of Application data MAY be sent as they are
5446 * potentially useful as a traffic analysis countermeasure.
5447 *
5448 * Therefore, it is possible that the input message length is 0 and the
5449 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005451static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005452 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005453{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005454 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5455 const size_t max_len = (size_t) ret;
5456
5457 if( ret < 0 )
5458 {
5459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5460 return( ret );
5461 }
5462
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005463 if( len > max_len )
5464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005466 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005469 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5470 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005471 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005473 }
5474 else
5475#endif
5476 len = max_len;
5477 }
Paul Bakker887bd502011-06-08 13:10:54 +00005478
Paul Bakker5121ce52009-01-03 21:22:43 +00005479 if( ssl->out_left != 0 )
5480 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005481 /*
5482 * The user has previously tried to send the data and
5483 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5484 * written. In this case, we expect the high-level write function
5485 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005487 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005490 return( ret );
5491 }
5492 }
Paul Bakker887bd502011-06-08 13:10:54 +00005493 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005494 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005495 /*
5496 * The user is trying to send a message the first time, so we need to
5497 * copy the data into the internal buffers and setup the data structure
5498 * to keep track of partial writes
5499 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005500 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005502 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005503
Hanno Becker67bc7c32018-08-06 11:33:50 +01005504 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005507 return( ret );
5508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005509 }
5510
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005511 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512}
5513
5514/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005515 * Write application data, doing 1/n-1 splitting if necessary.
5516 *
5517 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005518 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005519 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005521#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005522static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005523 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005524{
Janos Follath865b3eb2019-12-16 11:46:15 +00005525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005526
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005527 if( ssl->conf->cbc_record_splitting ==
5528 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005529 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005530 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5531 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5532 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005533 {
5534 return( ssl_write_real( ssl, buf, len ) );
5535 }
5536
5537 if( ssl->split_done == 0 )
5538 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005539 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005540 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005541 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005542 }
5543
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005544 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5545 return( ret );
5546 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005547
5548 return( ret + 1 );
5549}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005551
5552/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005553 * Write application data (public-facing wrapper)
5554 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005555int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005556{
Janos Follath865b3eb2019-12-16 11:46:15 +00005557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005558
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005560
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005561 if( ssl == NULL || ssl->conf == NULL )
5562 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5563
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005564#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005565 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5566 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005567 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005568 return( ret );
5569 }
5570#endif
5571
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005572 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005573 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005574 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005575 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005577 return( ret );
5578 }
5579 }
5580
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005581#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005582 ret = ssl_write_split( ssl, buf, len );
5583#else
5584 ret = ssl_write_real( ssl, buf, len );
5585#endif
5586
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005588
5589 return( ret );
5590}
5591
5592/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005593 * Notify the peer that the connection is being closed
5594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596{
Janos Follath865b3eb2019-12-16 11:46:15 +00005597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005598
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005599 if( ssl == NULL || ssl->conf == NULL )
5600 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005603
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005604 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005605 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005609 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5610 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5611 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 return( ret );
5615 }
5616 }
5617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005619
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005620 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005621}
5622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005623void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005624{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005625 if( transform == NULL )
5626 return;
5627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5629 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005630
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005631#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 mbedtls_md_free( &transform->md_ctx_enc );
5633 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005634#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005635
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005636 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005637}
5638
Hanno Becker0271f962018-08-16 13:23:47 +01005639#if defined(MBEDTLS_SSL_PROTO_DTLS)
5640
Hanno Becker533ab5f2020-02-05 10:49:13 +00005641void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005642{
5643 unsigned offset;
5644 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5645
5646 if( hs == NULL )
5647 return;
5648
Hanno Becker283f5ef2018-08-24 09:34:47 +01005649 ssl_free_buffered_record( ssl );
5650
Hanno Becker0271f962018-08-16 13:23:47 +01005651 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005652 ssl_buffering_free_slot( ssl, offset );
5653}
5654
5655static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5656 uint8_t slot )
5657{
5658 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5659 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005660
5661 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5662 return;
5663
Hanno Beckere605b192018-08-21 15:59:07 +01005664 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005665 {
Hanno Beckere605b192018-08-21 15:59:07 +01005666 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005667 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005668 mbedtls_free( hs_buf->data );
5669 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005670 }
5671}
5672
5673#endif /* MBEDTLS_SSL_PROTO_DTLS */
5674
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005675/*
5676 * Convert version numbers to/from wire format
5677 * and, for DTLS, to/from TLS equivalent.
5678 *
5679 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005680 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005681 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5682 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005684void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005685 unsigned char ver[2] )
5686{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005687#if defined(MBEDTLS_SSL_PROTO_DTLS)
5688 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005691 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5692
5693 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5694 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5695 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005696 else
5697#else
5698 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005699#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005700 {
5701 ver[0] = (unsigned char) major;
5702 ver[1] = (unsigned char) minor;
5703 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005704}
5705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005707 const unsigned char ver[2] )
5708{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005709#if defined(MBEDTLS_SSL_PROTO_DTLS)
5710 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005711 {
5712 *major = 255 - ver[0] + 2;
5713 *minor = 255 - ver[1] + 1;
5714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005716 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5717 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005718 else
5719#else
5720 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005721#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005722 {
5723 *major = ver[0];
5724 *minor = ver[1];
5725 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005726}
5727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728#endif /* MBEDTLS_SSL_TLS_C */