blob: b629d79cb6d39afd3564e248297452bbced2849e [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 Becker67bc7c32018-08-06 11:33:50 +010089#define SSL_DONT_FORCE_FLUSH 0
90#define SSL_FORCE_FLUSH 1
91
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010093
Hanno Beckerd5847772018-08-28 10:09:23 +010094/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +010095static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
96 uint8_t slot );
97static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
98static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
99static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
100static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100101static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
102 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100103static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100104
Hanno Becker11682cc2018-08-22 14:41:02 +0100105static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100106{
Hanno Becker89490712020-02-05 10:50:12 +0000107 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000108#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
109 size_t out_buf_len = ssl->out_buf_len;
110#else
111 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
112#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100113
Darryl Greenb33cc762019-11-28 14:29:44 +0000114 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100115 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100116
Darryl Greenb33cc762019-11-28 14:29:44 +0000117 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100118}
119
Hanno Becker67bc7c32018-08-06 11:33:50 +0100120static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
121{
Hanno Becker11682cc2018-08-22 14:41:02 +0100122 size_t const bytes_written = ssl->out_left;
123 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100124
125 /* Double-check that the write-index hasn't gone
126 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100127 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100128 {
129 /* Should never happen... */
130 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
131 }
132
133 return( (int) ( mtu - bytes_written ) );
134}
135
136static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
137{
Janos Follath865b3eb2019-12-16 11:46:15 +0000138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100139 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400140 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100141
142#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400143 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100144
145 if( max_len > mfl )
146 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100147
148 /* By the standard (RFC 6066 Sect. 4), the MFL extension
149 * only limits the maximum record payload size, so in theory
150 * we would be allowed to pack multiple records of payload size
151 * MFL into a single datagram. However, this would mean that there's
152 * no way to explicitly communicate MTU restrictions to the peer.
153 *
154 * The following reduction of max_len makes sure that we never
155 * write datagrams larger than MFL + Record Expansion Overhead.
156 */
157 if( max_len <= ssl->out_left )
158 return( 0 );
159
160 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100161#endif
162
163 ret = ssl_get_remaining_space_in_datagram( ssl );
164 if( ret < 0 )
165 return( ret );
166 remaining = (size_t) ret;
167
168 ret = mbedtls_ssl_get_record_expansion( ssl );
169 if( ret < 0 )
170 return( ret );
171 expansion = (size_t) ret;
172
173 if( remaining <= expansion )
174 return( 0 );
175
176 remaining -= expansion;
177 if( remaining >= max_len )
178 remaining = max_len;
179
180 return( (int) remaining );
181}
182
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200183/*
184 * Double the retransmit timeout value, within the allowed range,
185 * returning -1 if the maximum value has already been reached.
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200188{
189 uint32_t new_timeout;
190
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200191 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200192 return( -1 );
193
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200194 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
195 * in the following way: after the initial transmission and a first
196 * retransmission, back off to a temporary estimated MTU of 508 bytes.
197 * This value is guaranteed to be deliverable (if not guaranteed to be
198 * delivered) of any compliant IPv4 (and IPv6) network, and should work
199 * on most non-IP stacks too. */
200 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400201 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200202 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
204 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200205
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206 new_timeout = 2 * ssl->handshake->retransmit_timeout;
207
208 /* Avoid arithmetic overflow and range overflow */
209 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200210 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200211 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200212 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200213 }
214
215 ssl->handshake->retransmit_timeout = new_timeout;
Paul Elliott9f352112020-12-09 14:55:45 +0000216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
217 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200218
219 return( 0 );
220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200223{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200224 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Paul Elliott9f352112020-12-09 14:55:45 +0000225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
226 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200229
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100230/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200232 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000233
Hanno Beckerccc13d02020-05-04 12:30:04 +0100234#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
235 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100236
237static size_t ssl_compute_padding_length( size_t len,
238 size_t granularity )
239{
240 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
241}
242
Hanno Becker581bc1b2020-05-04 12:20:03 +0100243/* This functions transforms a (D)TLS plaintext fragment and a record content
244 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
245 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
246 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100247 *
248 * struct {
249 * opaque content[DTLSPlaintext.length];
250 * ContentType real_type;
251 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100252 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100253 *
254 * Input:
255 * - `content`: The beginning of the buffer holding the
256 * plaintext to be wrapped.
257 * - `*content_size`: The length of the plaintext in Bytes.
258 * - `max_len`: The number of Bytes available starting from
259 * `content`. This must be `>= *content_size`.
260 * - `rec_type`: The desired record content type.
261 *
262 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100263 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
264 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100265 *
266 * Returns:
267 * - `0` on success.
268 * - A negative error code if `max_len` didn't offer enough space
269 * for the expansion.
270 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100271static int ssl_build_inner_plaintext( unsigned char *content,
272 size_t *content_size,
273 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100274 uint8_t rec_type,
275 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100276{
277 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100278
279 /* Write real content type */
280 if( remaining == 0 )
281 return( -1 );
282 content[ len ] = rec_type;
283 len++;
284 remaining--;
285
286 if( remaining < pad )
287 return( -1 );
288 memset( content + len, 0, pad );
289 len += pad;
290 remaining -= pad;
291
292 *content_size = len;
293 return( 0 );
294}
295
Hanno Becker581bc1b2020-05-04 12:20:03 +0100296/* This function parses a (D)TLSInnerPlaintext structure.
297 * See ssl_build_inner_plaintext() for details. */
298static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100299 size_t *content_size,
300 uint8_t *rec_type )
301{
302 size_t remaining = *content_size;
303
304 /* Determine length of padding by skipping zeroes from the back. */
305 do
306 {
307 if( remaining == 0 )
308 return( -1 );
309 remaining--;
310 } while( content[ remaining ] == 0 );
311
312 *content_size = remaining;
313 *rec_type = content[ remaining ];
314
315 return( 0 );
316}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
318 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100319
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100320/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100321 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000322static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100323 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100324 mbedtls_record *rec,
325 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000326{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100327 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100328 *
329 * additional_data = seq_num + TLSCompressed.type +
330 * TLSCompressed.version + TLSCompressed.length;
331 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100332 * For the CID extension, this is extended as follows
333 * (quoting draft-ietf-tls-dtls-connection-id-05,
334 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100335 *
336 * additional_data = seq_num + DTLSPlaintext.type +
337 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100338 * cid +
339 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100340 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100341 *
342 * For TLS 1.3, the record sequence number is dropped from the AAD
343 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100344 */
345
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100346 unsigned char *cur = add_data;
347
348#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
349 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
350#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
351 {
352 ((void) minor_ver);
353 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
354 cur += sizeof( rec->ctr );
355 }
356
357 *cur = rec->type;
358 cur++;
359
360 memcpy( cur, rec->ver, sizeof( rec->ver ) );
361 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100362
Hanno Beckera0e20d02019-05-15 14:03:01 +0100363#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100364 if( rec->cid_len != 0 )
365 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100366 memcpy( cur, rec->cid, rec->cid_len );
367 cur += rec->cid_len;
368
369 *cur = rec->cid_len;
370 cur++;
371
372 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
373 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
374 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100375 }
376 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100377#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100378 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100379 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
380 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
381 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100382 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100383
384 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000385}
386
Hanno Becker67a37db2020-05-28 16:27:07 +0100387#if defined(MBEDTLS_GCM_C) || \
388 defined(MBEDTLS_CCM_C) || \
389 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100390static int ssl_transform_aead_dynamic_iv_is_explicit(
391 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100392{
Hanno Becker17263802020-05-28 07:05:48 +0100393 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100394}
395
Hanno Becker17263802020-05-28 07:05:48 +0100396/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
397 *
398 * Concretely, this occurs in two variants:
399 *
400 * a) Fixed and dynamic IV lengths add up to total IV length, giving
401 * IV = fixed_iv || dynamic_iv
402 *
Hanno Becker15952812020-06-04 13:31:46 +0100403 * This variant is used in TLS 1.2 when used with GCM or CCM.
404 *
Hanno Becker17263802020-05-28 07:05:48 +0100405 * b) Fixed IV lengths matches total IV length, giving
406 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100407 *
408 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
409 *
410 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100411 *
412 * This function has the precondition that
413 *
414 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
415 *
416 * which has to be ensured by the caller. If this precondition
417 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100418 */
419static void ssl_build_record_nonce( unsigned char *dst_iv,
420 size_t dst_iv_len,
421 unsigned char const *fixed_iv,
422 size_t fixed_iv_len,
423 unsigned char const *dynamic_iv,
424 size_t dynamic_iv_len )
425{
426 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100427
428 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100429 memset( dst_iv, 0, dst_iv_len );
430 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100431
Hanno Becker17263802020-05-28 07:05:48 +0100432 dst_iv += dst_iv_len - dynamic_iv_len;
433 for( i = 0; i < dynamic_iv_len; i++ )
434 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100435}
Hanno Becker67a37db2020-05-28 16:27:07 +0100436#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100437
Hanno Beckera18d1322018-01-03 14:27:32 +0000438int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
439 mbedtls_ssl_transform *transform,
440 mbedtls_record *rec,
441 int (*f_rng)(void *, unsigned char *, size_t),
442 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000443{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100445 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000446 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100447 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100448 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000449 size_t post_avail;
450
451 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000452#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200453 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000454 ((void) ssl);
455#endif
456
457 /* The PRNG is used for dynamic IV generation that's used
TRodziewicz0f82ec62021-05-12 17:49:18 +0200458 * for CBC transformations in TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200459#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
TRodziewicz0f82ec62021-05-12 17:49:18 +0200460 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000461 ((void) f_rng);
462 ((void) p_rng);
463#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000467 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100468 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
470 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
471 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100472 if( rec == NULL
473 || rec->buf == NULL
474 || rec->buf_len < rec->data_offset
475 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100476#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100477 || rec->cid_len != 0
478#endif
479 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000480 {
481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100483 }
484
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000485 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100486 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000488 data, rec->data_len );
489
490 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
491
492 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
493 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
495 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000496 rec->data_len,
497 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000498 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
499 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100500
Hanno Becker92313402020-05-20 13:58:58 +0100501 /* The following two code paths implement the (D)TLSInnerPlaintext
502 * structure present in TLS 1.3 and DTLS 1.2 + CID.
503 *
504 * See ssl_build_inner_plaintext() for more information.
505 *
506 * Note that this changes `rec->data_len`, and hence
507 * `post_avail` needs to be recalculated afterwards.
508 *
509 * Note also that the two code paths cannot occur simultaneously
510 * since they apply to different versions of the protocol. There
511 * is hence no risk of double-addition of the inner plaintext.
512 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100513#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
514 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
515 {
Hanno Becker13996922020-05-28 16:15:19 +0100516 size_t padding =
517 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100518 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100519 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100520 &rec->data_len,
521 post_avail,
522 rec->type,
523 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100524 {
525 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
526 }
527
528 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
529 }
530#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
531
Hanno Beckera0e20d02019-05-15 14:03:01 +0100532#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100533 /*
534 * Add CID information
535 */
536 rec->cid_len = transform->out_cid_len;
537 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
538 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100539
540 if( rec->cid_len != 0 )
541 {
Hanno Becker13996922020-05-28 16:15:19 +0100542 size_t padding =
543 ssl_compute_padding_length( rec->data_len,
544 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100545 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100546 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100547 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100548 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100549 * Note that this changes `rec->data_len`, and hence
550 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100551 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100552 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100553 &rec->data_len,
554 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100555 rec->type,
556 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100557 {
558 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
559 }
560
561 rec->type = MBEDTLS_SSL_MSG_CID;
562 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100563#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100564
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100565 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
566
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100568 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000570#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( mode == MBEDTLS_MODE_STREAM ||
572 ( mode == MBEDTLS_MODE_CBC
573#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000574 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100575#endif
576 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000578 if( post_avail < transform->maclen )
579 {
580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
581 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
582 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200583#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000584 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200585 {
Hanno Becker992b6872017-11-09 18:57:39 +0000586 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
587
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100588 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
589 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000590
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000591 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100592 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000593 mbedtls_md_hmac_update( &transform->md_ctx_enc,
594 data, rec->data_len );
595 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
596 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
597
598 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200599 }
600 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200601#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200605 }
606
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000607 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
608 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200609
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000610 rec->data_len += transform->maclen;
611 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100612 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200613 }
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000614#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200616 /*
617 * Encrypt
618 */
Hanno Beckerd086bf02021-03-22 13:01:27 +0000619#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000623 size_t olen;
Paul Elliottd48d5c62021-01-07 14:47:05 +0000624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000625 "including %d bytes of padding",
626 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000628 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
629 transform->iv_enc, transform->ivlen,
630 data, rec->data_len,
631 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200634 return( ret );
635 }
636
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000637 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
640 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200641 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 }
Paul Bakker68884e32013-01-07 18:20:04 +0100643 else
Hanno Beckerd086bf02021-03-22 13:01:27 +0000644#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000645
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200646#if defined(MBEDTLS_GCM_C) || \
647 defined(MBEDTLS_CCM_C) || \
648 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200650 mode == MBEDTLS_MODE_CCM ||
651 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000652 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200654 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100655 unsigned char *dynamic_iv;
656 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100657 int dynamic_iv_is_explicit =
658 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000659
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100660 /* Check that there's space for the authentication tag. */
661 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000662 {
663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
664 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
665 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666
Paul Bakker68884e32013-01-07 18:20:04 +0100667 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100668 * Build nonce for AEAD encryption.
669 *
670 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
671 * part of the IV is prepended to the ciphertext and
672 * can be chosen freely - in particular, it need not
673 * agree with the record sequence number.
674 * However, since ChaChaPoly as well as all AEAD modes
675 * in TLS 1.3 use the record sequence number as the
676 * dynamic part of the nonce, we uniformly use the
677 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100678 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100679 dynamic_iv = rec->ctr;
680 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200681
Hanno Becker17263802020-05-28 07:05:48 +0100682 ssl_build_record_nonce( iv, sizeof( iv ),
683 transform->iv_enc,
684 transform->fixed_ivlen,
685 dynamic_iv,
686 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100687
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100688 /*
689 * Build additional data for AEAD encryption.
690 * This depends on the TLS version.
691 */
692 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
693 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100694
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200695 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100696 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200697 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100698 dynamic_iv,
699 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000700 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100701 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200703 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000704 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000705
Paul Bakker68884e32013-01-07 18:20:04 +0100706 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200707 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200708 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000709
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100710 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000711 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100712 add_data, add_data_len,
713 data, rec->data_len, /* src */
714 data, rec->buf_len - (data - rec->buf), /* dst */
715 &rec->data_len,
716 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200717 {
TRodziewicz18efb732021-04-29 23:12:19 +0200718 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200719 return( ret );
720 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000721 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100722 data + rec->data_len - transform->taglen,
723 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100724 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000725 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100726
727 /*
728 * Prefix record content with dynamic IV in case it is explicit.
729 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100730 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100731 {
732 if( rec->data_offset < dynamic_iv_len )
733 {
734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
735 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
736 }
737
738 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
739 rec->data_offset -= dynamic_iv_len;
740 rec->data_len += dynamic_iv_len;
741 }
742
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100743 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000744 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100746#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200747#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000751 size_t padlen, i;
752 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000753
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000754 /* Currently we're always using minimal padding
755 * (up to 255 bytes would be allowed). */
756 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
757 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 padlen = 0;
759
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000760 /* Check there's enough space in the buffer for the padding. */
761 if( post_avail < padlen + 1 )
762 {
763 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
764 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
765 }
766
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000768 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000770 rec->data_len += padlen + 1;
771 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000772
TRodziewicz0f82ec62021-05-12 17:49:18 +0200773#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000774 /*
TRodziewicz2d8800e2021-05-13 19:14:19 +0200775 * Prepend per-record IV for block cipher in TLS v1.2 as per
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000777 */
TRodziewicz0f82ec62021-05-12 17:49:18 +0200778 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000779 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000780 if( f_rng == NULL )
781 {
782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
784 }
785
786 if( rec->data_offset < transform->ivlen )
787 {
788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
789 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
790 }
791
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000792 /*
793 * Generate IV
794 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000795 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000796 if( ret != 0 )
797 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000798
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000799 memcpy( data - transform->ivlen, transform->iv_enc,
800 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000801
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000802 }
TRodziewicz0f82ec62021-05-12 17:49:18 +0200803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000804
Paul Elliottd48d5c62021-01-07 14:47:05 +0000805 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
806 "including %" MBEDTLS_PRINTF_SIZET
807 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000808 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200809 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000811 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
812 transform->iv_enc,
813 transform->ivlen,
814 data, rec->data_len,
815 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200818 return( ret );
819 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200820
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000821 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
824 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200825 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200826
TRodziewicz0f82ec62021-05-12 17:49:18 +0200827 data -= transform->ivlen;
828 rec->data_offset -= transform->ivlen;
829 rec->data_len += transform->ivlen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100832 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100833 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000834 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
835
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100836 /*
837 * MAC(MAC_write_key, seq_num +
838 * TLSCipherText.type +
839 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100840 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100841 * IV + // except for TLS 1.0
842 * ENC(content + padding + padding_length));
843 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000844
845 if( post_avail < transform->maclen)
846 {
847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
848 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
849 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100850
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100851 ssl_extract_add_data_from_record( add_data, &add_data_len,
852 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000855 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100856 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100857
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000858 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100859 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000860 mbedtls_md_hmac_update( &transform->md_ctx_enc,
861 data, rec->data_len );
862 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
863 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100864
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000865 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100866
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000867 rec->data_len += transform->maclen;
868 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100869 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100870 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000872 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200873 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200874#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
877 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200878 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000879
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100880 /* Make extra sure authentication was performed, exactly once */
881 if( auth_done != 1 )
882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
884 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100885 }
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000888
889 return( 0 );
890}
891
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +0200892#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +0200893/*
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200894 * Turn a bit into a mask:
895 * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
896 * - if bit == 0, return the all-bits 0 mask, aka 0
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200897 *
898 * This function can be used to write constant-time code by replacing branches
899 * with bit operations using masks.
900 *
901 * This function is implemented without using comparison operators, as those
902 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200903 */
904static size_t mbedtls_ssl_cf_mask_from_bit( size_t bit )
905{
906 /* MSVC has a warning about unary minus on unsigned integer types,
907 * but this is well-defined and precisely what we want to do here. */
908#if defined(_MSC_VER)
909#pragma warning( push )
910#pragma warning( disable : 4146 )
911#endif
912 return -bit;
913#if defined(_MSC_VER)
914#pragma warning( pop )
915#endif
916}
917
918/*
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200919 * Constant-flow mask generation for "less than" comparison:
920 * - if x < y, return all bits 1, that is (size_t) -1
921 * - otherwise, return all bits 0, that is 0
922 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200923 * This function can be used to write constant-time code by replacing branches
924 * with bit operations using masks.
925 *
926 * This function is implemented without using comparison operators, as those
927 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200928 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200929static size_t mbedtls_ssl_cf_mask_lt( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200930{
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200931 /* This has the most significant bit set if and only if x < y */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200932 const size_t sub = x - y;
933
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200934 /* sub1 = (x < y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200935 const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 );
936
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200937 /* mask = (x < y) ? 0xff... : 0x00... */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200938 const size_t mask = mbedtls_ssl_cf_mask_from_bit( sub1 );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200939
940 return( mask );
941}
942
943/*
944 * Constant-flow mask generation for "greater or equal" comparison:
945 * - if x >= y, return all bits 1, that is (size_t) -1
946 * - otherwise, return all bits 0, that is 0
947 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200948 * This function can be used to write constant-time code by replacing branches
949 * with bit operations using masks.
950 *
951 * This function is implemented without using comparison operators, as those
952 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200953 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200954static size_t mbedtls_ssl_cf_mask_ge( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200955{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200956 return( ~mbedtls_ssl_cf_mask_lt( x, y ) );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200957}
958
959/*
960 * Constant-flow boolean "equal" comparison:
961 * return x == y
962 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200963 * This function can be used to write constant-time code by replacing branches
964 * with bit operations - it can be used in conjunction with
965 * mbedtls_ssl_cf_mask_from_bit().
966 *
967 * This function is implemented without using comparison operators, as those
968 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200969 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +0200970static size_t mbedtls_ssl_cf_bool_eq( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200971{
972 /* diff = 0 if x == y, non-zero otherwise */
973 const size_t diff = x ^ y;
974
975 /* MSVC has a warning about unary minus on unsigned integer types,
976 * but this is well-defined and precisely what we want to do here. */
977#if defined(_MSC_VER)
978#pragma warning( push )
979#pragma warning( disable : 4146 )
980#endif
981
982 /* diff_msb's most significant bit is equal to x != y */
983 const size_t diff_msb = ( diff | -diff );
984
985#if defined(_MSC_VER)
986#pragma warning( pop )
987#endif
988
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +0200989 /* diff1 = (x != y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +0200990 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
991
992 return( 1 ^ diff1 );
993}
994
995/*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +0200996 * Constant-flow conditional memcpy:
997 * - if c1 == c2, equivalent to memcpy(dst, src, len),
998 * - otherwise, a no-op,
999 * but with execution flow independent of the values of c1 and c2.
1000 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001001 * This function is implemented without using comparison operators, as those
1002 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001003 */
Manuel Pégourié-Gonnarde7478432020-07-24 11:09:22 +02001004static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1005 const unsigned char *src,
1006 size_t len,
1007 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001008{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001009 /* mask = c1 == c2 ? 0xff : 0x00 */
1010 const size_t equal = mbedtls_ssl_cf_bool_eq( c1, c2 );
Manuel Pégourié-Gonnard2a59fb42020-08-25 11:51:46 +02001011 const unsigned char mask = (unsigned char) mbedtls_ssl_cf_mask_from_bit( equal );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001012
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001013 /* dst[i] = c1 == c2 ? src[i] : dst[i] */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001014 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001015 dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001016}
1017
1018/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001019 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001020 *
1021 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1022 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001023 */
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02001024MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001025 mbedtls_md_context_t *ctx,
1026 const unsigned char *add_data, size_t add_data_len,
1027 const unsigned char *data, size_t data_len_secret,
1028 size_t min_data_len, size_t max_data_len,
1029 unsigned char *output )
1030{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001031 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001032 * This function breaks the HMAC abstraction and uses the md_clone()
1033 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001034 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001035 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001036 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001037 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001038 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001039 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1040 * minlen, then cloning the context, and for each byte up to maxlen
1041 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001042 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001043 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001044 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001045 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001046 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1047 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001048 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +02001049 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001050 const unsigned char * const okey = ikey + block_size;
1051 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001052
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001053 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1054 mbedtls_md_context_t aux;
1055 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +02001056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001057
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001058 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001059
1060#define MD_CHK( func_call ) \
1061 do { \
1062 ret = (func_call); \
1063 if( ret != 0 ) \
1064 goto cleanup; \
1065 } while( 0 )
1066
1067 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001068
1069 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1070 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001071 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1072 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001073
1074 /* For each possible length, compute the hash up to that point */
1075 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001076 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001077 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1078 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001079 /* Keep only the correct inner_hash in the output buffer */
1080 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1081 offset, data_len_secret );
1082
1083 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001084 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001085 }
1086
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001087 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001088 MD_CHK( mbedtls_md_starts( ctx ) );
1089 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1090 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1091 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001092
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001093 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001094 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001095
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001096#undef MD_CHK
1097
1098cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001099 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001100 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001101}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001102
1103/*
1104 * Constant-flow memcpy from variable position in buffer.
1105 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001106 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001107 */
1108MBEDTLS_STATIC_TESTABLE void mbedtls_ssl_cf_memcpy_offset(
1109 unsigned char *dst,
1110 const unsigned char *src_base,
1111 size_t offset_secret,
1112 size_t offset_min, size_t offset_max,
1113 size_t len )
1114{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001115 size_t offset;
1116
1117 for( offset = offset_min; offset <= offset_max; offset++ )
1118 {
1119 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1120 offset, offset_secret );
1121 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001122}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001123#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001124
Hanno Becker605949f2019-07-12 08:23:59 +01001125int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001126 mbedtls_ssl_transform *transform,
1127 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001128{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001129 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001131 int ret, auth_done = 0;
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001132#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001133 size_t padlen = 0, correct = 1;
1134#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001135 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001136 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001137 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001138
Hanno Beckera18d1322018-01-03 14:27:32 +00001139#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001140 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001141 ((void) ssl);
1142#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001145 if( rec == NULL ||
1146 rec->buf == NULL ||
1147 rec->buf_len < rec->data_offset ||
1148 rec->buf_len - rec->data_offset < rec->data_len )
1149 {
1150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001152 }
1153
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001154 data = rec->buf + rec->data_offset;
1155 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Hanno Beckera0e20d02019-05-15 14:03:01 +01001157#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001158 /*
1159 * Match record's CID with incoming CID.
1160 */
Hanno Becker938489a2019-05-08 13:02:22 +01001161 if( rec->cid_len != transform->in_cid_len ||
1162 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1163 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001164 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001165 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001167
Hanno Beckerd086bf02021-03-22 13:01:27 +00001168#if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001170 {
1171 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001172 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1173 transform->iv_dec,
1174 transform->ivlen,
1175 data, rec->data_len,
1176 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001179 return( ret );
1180 }
1181
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001182 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001186 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
Paul Bakker68884e32013-01-07 18:20:04 +01001188 else
Hanno Beckerd086bf02021-03-22 13:01:27 +00001189#endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001190#if defined(MBEDTLS_GCM_C) || \
1191 defined(MBEDTLS_CCM_C) || \
1192 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001194 mode == MBEDTLS_MODE_CCM ||
1195 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001196 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001197 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001198 unsigned char *dynamic_iv;
1199 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001201 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001202 * Extract dynamic part of nonce for AEAD decryption.
1203 *
1204 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1205 * part of the IV is prepended to the ciphertext and
1206 * can be chosen freely - in particular, it need not
1207 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001208 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001209 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001210 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001211 {
1212 if( rec->data_len < dynamic_iv_len )
1213 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1215 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001216 rec->data_len,
1217 dynamic_iv_len ) );
1218 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1219 }
1220 dynamic_iv = data;
1221
1222 data += dynamic_iv_len;
1223 rec->data_offset += dynamic_iv_len;
1224 rec->data_len -= dynamic_iv_len;
1225 }
Hanno Becker17263802020-05-28 07:05:48 +01001226 else
1227 {
1228 dynamic_iv = rec->ctr;
1229 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001230
1231 /* Check that there's space for the authentication tag. */
1232 if( rec->data_len < transform->taglen )
1233 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1235 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001236 rec->data_len,
1237 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001239 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001240 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001241
Hanno Beckerdf8be222020-05-21 15:30:57 +01001242 /*
1243 * Prepare nonce from dynamic and static parts.
1244 */
Hanno Becker17263802020-05-28 07:05:48 +01001245 ssl_build_record_nonce( iv, sizeof( iv ),
1246 transform->iv_dec,
1247 transform->fixed_ivlen,
1248 dynamic_iv,
1249 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001250
Hanno Beckerdf8be222020-05-21 15:30:57 +01001251 /*
1252 * Build additional data for AEAD encryption.
1253 * This depends on the TLS version.
1254 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001255 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1256 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001257 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001258 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001259
Hanno Beckerd96a6522019-07-10 13:55:25 +01001260 /* Because of the check above, we know that there are
1261 * explicit_iv_len Bytes preceeding data, and taglen
1262 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001263 * the debug message and the invocation of
TRodziewicz18efb732021-04-29 23:12:19 +02001264 * mbedtls_cipher_auth_decrypt_ext() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001265
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001266 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001267 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001268 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001269
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001270 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001271 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001273 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001274 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001275 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001276 data, rec->data_len + transform->taglen, /* src */
1277 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001278 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001279 {
TRodziewicz18efb732021-04-29 23:12:19 +02001280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt_ext", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1283 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001284
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001285 return( ret );
1286 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001287 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001288
Hanno Beckerd96a6522019-07-10 13:55:25 +01001289 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001290 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001294 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001296 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001298#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001301 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 /*
Paul Bakker45829992013-01-03 14:52:21 +01001304 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001305 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001306#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1307 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001308 {
1309 /* The ciphertext is prefixed with the CBC IV. */
1310 minlen += transform->ivlen;
1311 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001312#endif
Paul Bakker45829992013-01-03 14:52:21 +01001313
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001314 /* Size considerations:
1315 *
1316 * - The CBC cipher text must not be empty and hence
1317 * at least of size transform->ivlen.
1318 *
1319 * Together with the potential IV-prefix, this explains
1320 * the first of the two checks below.
1321 *
1322 * - The record must contain a MAC, either in plain or
1323 * encrypted, depending on whether Encrypt-then-MAC
1324 * is used or not.
1325 * - If it is, the message contains the IV-prefix,
1326 * the CBC ciphertext, and the MAC.
1327 * - If it is not, the padded plaintext, and hence
1328 * the CBC ciphertext, has at least length maclen + 1
1329 * because there is at least the padding length byte.
1330 *
1331 * As the CBC ciphertext is not empty, both cases give the
1332 * lower bound minlen + maclen + 1 on the record size, which
1333 * we test for in the second check below.
1334 */
1335 if( rec->data_len < minlen + transform->ivlen ||
1336 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001337 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1339 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1340 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001341 "+ 1 ) ( + expl IV )", rec->data_len,
1342 transform->ivlen,
1343 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001345 }
1346
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001347 /*
1348 * Authenticate before decrypt if enabled
1349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001351 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001352 {
Hanno Becker992b6872017-11-09 18:57:39 +00001353 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001356
Hanno Beckerd96a6522019-07-10 13:55:25 +01001357 /* Update data_len in tandem with add_data.
1358 *
1359 * The subtraction is safe because of the previous check
1360 * data_len >= minlen + maclen + 1.
1361 *
1362 * Afterwards, we know that data + data_len is followed by at
1363 * least maclen Bytes, which justifies the call to
1364 * mbedtls_ssl_safer_memcmp() below.
1365 *
1366 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001367 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001368 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1369 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001370
Hanno Beckerd96a6522019-07-10 13:55:25 +01001371 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001372 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1373 add_data_len );
1374 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1375 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001376 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1377 data, rec->data_len );
1378 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1379 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001380
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001381 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1382 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001383 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001384 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001385
Hanno Beckerd96a6522019-07-10 13:55:25 +01001386 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001387 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1388 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001392 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001393 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001394 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001396
1397 /*
1398 * Check length sanity
1399 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001400
1401 /* We know from above that data_len > minlen >= 0,
1402 * so the following check in particular implies that
1403 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001404 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001405 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1407 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001408 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001410 }
1411
TRodziewicz0f82ec62021-05-12 17:49:18 +02001412#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001413 /*
TRodziewicz0f82ec62021-05-12 17:49:18 +02001414 * Initialize for prepended IV for block cipher in TLS v1.2
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001415 */
TRodziewicz0f82ec62021-05-12 17:49:18 +02001416 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001417 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001418 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001419 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001420
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001421 data += transform->ivlen;
1422 rec->data_offset += transform->ivlen;
1423 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001424 }
TRodziewicz0f82ec62021-05-12 17:49:18 +02001425#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001426
Hanno Beckerd96a6522019-07-10 13:55:25 +01001427 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1428
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001429 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1430 transform->iv_dec, transform->ivlen,
1431 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001434 return( ret );
1435 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001436
Hanno Beckerd96a6522019-07-10 13:55:25 +01001437 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001438 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1441 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001442 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001443
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001444 /* Safe since data_len >= minlen + maclen + 1, so after having
1445 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001446 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1447 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001448 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001449
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001450 if( auth_done == 1 )
1451 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001452 const size_t mask = mbedtls_ssl_cf_mask_ge(
1453 rec->data_len,
1454 padlen + 1 );
1455 correct &= mask;
1456 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001457 }
1458 else
Paul Bakker45829992013-01-03 14:52:21 +01001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001461 if( rec->data_len < transform->maclen + padlen + 1 )
1462 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1464 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1465 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001466 rec->data_len,
1467 transform->maclen,
1468 padlen + 1 ) );
1469 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001470#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001471
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001472 const size_t mask = mbedtls_ssl_cf_mask_ge(
1473 rec->data_len,
1474 transform->maclen + padlen + 1 );
1475 correct &= mask;
1476 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001479 padlen++;
1480
1481 /* Regardless of the validity of the padding,
1482 * we have data_len >= padlen here. */
1483
TRodziewicz0f82ec62021-05-12 17:49:18 +02001484#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001485 /* The padding check involves a series of up to 256
1486 * consecutive memory reads at the end of the record
1487 * plaintext buffer. In order to hide the length and
1488 * validity of the padding, always perform exactly
1489 * `min(256,plaintext_len)` reads (but take into account
1490 * only the last `padlen` bytes for the padding check). */
1491 size_t pad_count = 0;
1492 volatile unsigned char* const check = data;
1493
1494 /* Index of first padding byte; it has been ensured above
1495 * that the subtraction is safe. */
1496 size_t const padding_idx = rec->data_len - padlen;
1497 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1498 size_t const start_idx = rec->data_len - num_checks;
1499 size_t idx;
1500
1501 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001503 /* pad_count += (idx >= padding_idx) &&
1504 * (check[idx] == padlen - 1);
1505 */
1506 const size_t mask = mbedtls_ssl_cf_mask_ge( idx, padding_idx );
1507 const size_t equal = mbedtls_ssl_cf_bool_eq( check[idx],
1508 padlen - 1 );
1509 pad_count += mask & equal;
1510 }
1511 correct &= mbedtls_ssl_cf_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SSL_DEBUG_ALL)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001514 if( padlen > 0 && correct == 0 )
1515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001516#endif
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001517 padlen &= mbedtls_ssl_cf_mask_from_bit( correct );
1518
TRodziewicz0f82ec62021-05-12 17:49:18 +02001519#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001520
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001521 /* If the padding was found to be invalid, padlen == 0
1522 * and the subtraction is safe. If the padding was found valid,
1523 * padlen hasn't been changed and the previous assertion
1524 * data_len >= padlen still holds. */
1525 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001527 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001528#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1531 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001532 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001533
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001534#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001536 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001537#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001538
1539 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001540 * Authenticate if not done yet.
1541 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001543#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001544 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001545 {
Hanno Becker992b6872017-11-09 18:57:39 +00001546 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001547 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001548
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001549 /* If the initial value of padlen was such that
1550 * data_len < maclen + padlen + 1, then padlen
1551 * got reset to 1, and the initial check
1552 * data_len >= minlen + maclen + 1
1553 * guarantees that at this point we still
1554 * have at least data_len >= maclen.
1555 *
1556 * If the initial value of padlen was such that
1557 * data_len >= maclen + padlen + 1, then we have
1558 * subtracted either padlen + 1 (if the padding was correct)
1559 * or 0 (if the padding was incorrect) since then,
1560 * hence data_len >= maclen in any case.
1561 */
1562 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001563 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1564 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
TRodziewicz0f82ec62021-05-12 17:49:18 +02001566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001567 /*
1568 * The next two sizes are the minimum and maximum values of
1569 * data_len over all padlen values.
1570 *
1571 * They're independent of padlen, since we previously did
1572 * data_len -= padlen.
1573 *
1574 * Note that max_len + maclen is never more than the buffer
1575 * length, as we previously did in_msglen -= maclen too.
1576 */
1577 const size_t max_len = rec->data_len + padlen;
1578 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1579
1580 ret = mbedtls_ssl_cf_hmac( &transform->md_ctx_dec,
1581 add_data, add_data_len,
1582 data, rec->data_len, min_len, max_len,
1583 mac_expect );
1584 if( ret != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001585 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
1587 return( ret );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001588 }
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001589
1590 mbedtls_ssl_cf_memcpy_offset( mac_peer, data,
1591 rec->data_len,
1592 min_len, max_len,
1593 transform->maclen );
TRodziewicz0f82ec62021-05-12 17:49:18 +02001594#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001595
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001596#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001597 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001598 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001599#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001600
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001601 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001602 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#if defined(MBEDTLS_SSL_DEBUG_ALL)
1605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001606#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001607 correct = 0;
1608 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001609 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001610 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001611
1612 /*
1613 * Finally check the correct flag
1614 */
1615 if( correct == 0 )
1616 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001617#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001618
1619 /* Make extra sure authentication was performed, exactly once */
1620 if( auth_done != 1 )
1621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1623 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
Hanno Beckerccc13d02020-05-04 12:30:04 +01001626#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1627 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1628 {
1629 /* Remove inner padding and infer true content type. */
1630 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1631 &rec->type );
1632
1633 if( ret != 0 )
1634 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1635 }
1636#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1637
Hanno Beckera0e20d02019-05-15 14:03:01 +01001638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001639 if( rec->cid_len != 0 )
1640 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001641 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1642 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001643 if( ret != 0 )
1644 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1645 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001646#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
1650 return( 0 );
1651}
1652
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001653#undef MAC_NONE
1654#undef MAC_PLAINTEXT
1655#undef MAC_CIPHERTEXT
1656
Paul Bakker5121ce52009-01-03 21:22:43 +00001657/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001658 * Fill the input message buffer by appending data to it.
1659 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001660 *
1661 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1662 * available (from this read and/or a previous one). Otherwise, an error code
1663 * is returned (possibly EOF or WANT_READ).
1664 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001665 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1666 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1667 * since we always read a whole datagram at once.
1668 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001669 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001670 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001673{
Janos Follath865b3eb2019-12-16 11:46:15 +00001674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00001675 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00001676#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1677 size_t in_buf_len = ssl->in_buf_len;
1678#else
1679 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1680#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001684 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001687 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001689 }
1690
Darryl Greenb33cc762019-11-28 14:29:44 +00001691 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1694 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001695 }
1696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001698 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001700 uint32_t timeout;
1701
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001702 /*
1703 * The point is, we need to always read a full datagram at once, so we
1704 * sometimes read more then requested, and handle the additional data.
1705 * It could be the rest of the current record (while fetching the
1706 * header) and/or some other records in the same datagram.
1707 */
1708
1709 /*
1710 * Move to the next record in the already read datagram if applicable
1711 */
1712 if( ssl->next_record_offset != 0 )
1713 {
1714 if( ssl->in_left < ssl->next_record_offset )
1715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001718 }
1719
1720 ssl->in_left -= ssl->next_record_offset;
1721
1722 if( ssl->in_left != 0 )
1723 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
1725 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001726 ssl->next_record_offset ) );
1727 memmove( ssl->in_hdr,
1728 ssl->in_hdr + ssl->next_record_offset,
1729 ssl->in_left );
1730 }
1731
1732 ssl->next_record_offset = 0;
1733 }
1734
Paul Elliottd48d5c62021-01-07 14:47:05 +00001735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1736 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00001737 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001738
1739 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001740 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001741 */
1742 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001745 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001746 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001747
1748 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01001749 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001750 * are not at the beginning of a new record, the caller did something
1751 * wrong.
1752 */
1753 if( ssl->in_left != 0 )
1754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1756 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001757 }
1758
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001759 /*
1760 * Don't even try to read if time's out already.
1761 * This avoids by-passing the timer when repeatedly receiving messages
1762 * that will end up being dropped.
1763 */
Hanno Becker7876d122020-02-05 10:39:31 +00001764 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01001765 {
1766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001767 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01001768 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001769 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001770 {
Darryl Greenb33cc762019-11-28 14:29:44 +00001771 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001774 timeout = ssl->handshake->retransmit_timeout;
1775 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001776 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001777
Paul Elliott9f352112020-12-09 14:55:45 +00001778 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001779
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001780 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001781 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
1782 timeout );
1783 else
1784 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001787
1788 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001790 }
1791
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001792 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00001795 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001798 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001799 if( ssl_double_retransmit_timeout( ssl ) != 0 )
1800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001802 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001803 }
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001808 return( ret );
1809 }
1810
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001811 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001814 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001816 {
Hanno Becker786300f2020-02-05 10:46:40 +00001817 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001818 {
Hanno Becker786300f2020-02-05 10:46:40 +00001819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
1820 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001821 return( ret );
1822 }
1823
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01001824 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001825 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001827 }
1828
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 if( ret < 0 )
1830 return( ret );
1831
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001832 ssl->in_left = ret;
1833 }
1834 else
1835#endif
1836 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001837 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1838 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001839 ssl->in_left, nb_want ) );
1840
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001841 while( ssl->in_left < nb_want )
1842 {
1843 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001844
Hanno Becker7876d122020-02-05 10:39:31 +00001845 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001846 ret = MBEDTLS_ERR_SSL_TIMEOUT;
1847 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001848 {
1849 if( ssl->f_recv_timeout != NULL )
1850 {
1851 ret = ssl->f_recv_timeout( ssl->p_bio,
1852 ssl->in_hdr + ssl->in_left, len,
1853 ssl->conf->read_timeout );
1854 }
1855 else
1856 {
1857 ret = ssl->f_recv( ssl->p_bio,
1858 ssl->in_hdr + ssl->in_left, len );
1859 }
1860 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001861
Paul Elliottd48d5c62021-01-07 14:47:05 +00001862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
1863 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02001864 ssl->in_left, nb_want ) );
1865 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001866
1867 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001869
1870 if( ret < 0 )
1871 return( ret );
1872
makise-homuraaf9513b2020-08-24 18:26:27 +03001873 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08001874 {
Darryl Green11999bb2018-03-13 15:22:58 +00001875 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001876 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00001877 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08001878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1879 }
1880
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001881 ssl->in_left += ret;
1882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 }
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 return( 0 );
1888}
1889
1890/*
1891 * Flush any data not yet written
1892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001894{
Janos Follath865b3eb2019-12-16 11:46:15 +00001895 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01001896 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001900 if( ssl->f_send == NULL )
1901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01001903 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001905 }
1906
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001907 /* Avoid incrementing counter if data is flushed */
1908 if( ssl->out_left == 0 )
1909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001911 return( 0 );
1912 }
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 while( ssl->out_left > 0 )
1915 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001916 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
1917 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01001918 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Hanno Becker2b1e3542018-08-06 11:19:13 +01001920 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001921 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 if( ret <= 0 )
1926 return( ret );
1927
makise-homuraaf9513b2020-08-24 18:26:27 +03001928 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08001929 {
Darryl Green11999bb2018-03-13 15:22:58 +00001930 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00001931 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00001932 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08001933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1934 }
1935
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 ssl->out_left -= ret;
1937 }
1938
Hanno Becker2b1e3542018-08-06 11:19:13 +01001939#if defined(MBEDTLS_SSL_PROTO_DTLS)
1940 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001941 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01001942 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001943 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01001944 else
1945#endif
1946 {
1947 ssl->out_hdr = ssl->out_buf + 8;
1948 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00001949 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
1953 return( 0 );
1954}
1955
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001956/*
1957 * Functions to handle the DTLS retransmission state machine
1958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001960/*
1961 * Append current handshake message to current outgoing flight
1962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01001966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
1967 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
1968 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001969
1970 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001971 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001972 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001975 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001976 }
1977
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001978 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001979 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
1981 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001983 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001984 }
1985
1986 /* Copy current handshake message with headers */
1987 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
1988 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001989 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001990 msg->next = NULL;
1991
1992 /* Append to the current flight */
1993 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001994 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001995 else
1996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02001998 while( cur->next != NULL )
1999 cur = cur->next;
2000 cur->next = msg;
2001 }
2002
Hanno Becker3b235902018-08-06 09:54:53 +01002003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002004 return( 0 );
2005}
2006
2007/*
2008 * Free the current flight of handshake messages
2009 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002010void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 mbedtls_ssl_flight_item *cur = flight;
2013 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002014
2015 while( cur != NULL )
2016 {
2017 next = cur->next;
2018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 mbedtls_free( cur->p );
2020 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002021
2022 cur = next;
2023 }
2024}
2025
2026/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002027 * Swap transform_out and out_ctr with the alternative ones
2028 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002029static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002032 unsigned char tmp_out_ctr[8];
2033
2034 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002037 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002038 }
2039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002041
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002042 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002043 tmp_transform = ssl->transform_out;
2044 ssl->transform_out = ssl->handshake->alt_transform_out;
2045 ssl->handshake->alt_transform_out = tmp_transform;
2046
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002047 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002048 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2049 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002050 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002051
2052 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002053 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002054
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002055 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002056}
2057
2058/*
2059 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002060 */
2061int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2062{
2063 int ret = 0;
2064
2065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2066
2067 ret = mbedtls_ssl_flight_transmit( ssl );
2068
2069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2070
2071 return( ret );
2072}
2073
2074/*
2075 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002076 *
2077 * Need to remember the current message in case flush_output returns
2078 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002079 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002080 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002081int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002082{
Janos Follath865b3eb2019-12-16 11:46:15 +00002083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002087 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002089
2090 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002091 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002092 ret = ssl_swap_epochs( ssl );
2093 if( ret != 0 )
2094 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002097 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002098
2099 while( ssl->handshake->cur_msg != NULL )
2100 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002101 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002102 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002103
Hanno Beckere1dcb032018-08-17 16:47:58 +01002104 int const is_finished =
2105 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2106 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2107
Hanno Becker04da1892018-08-14 13:22:10 +01002108 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2109 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2110
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002111 /* Swap epochs before sending Finished: we can't do it after
2112 * sending ChangeCipherSpec, in case write returns WANT_READ.
2113 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002114 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002115 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002117 ret = ssl_swap_epochs( ssl );
2118 if( ret != 0 )
2119 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002120 }
2121
Hanno Becker67bc7c32018-08-06 11:33:50 +01002122 ret = ssl_get_remaining_payload_in_datagram( ssl );
2123 if( ret < 0 )
2124 return( ret );
2125 max_frag_len = (size_t) ret;
2126
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002127 /* CCS is copied as is, while HS messages may need fragmentation */
2128 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2129 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002130 if( max_frag_len == 0 )
2131 {
2132 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2133 return( ret );
2134
2135 continue;
2136 }
2137
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002138 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002139 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002140 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002141
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002142 /* Update position inside current message */
2143 ssl->handshake->cur_msg_p += cur->len;
2144 }
2145 else
2146 {
2147 const unsigned char * const p = ssl->handshake->cur_msg_p;
2148 const size_t hs_len = cur->len - 12;
2149 const size_t frag_off = p - ( cur->p + 12 );
2150 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002151 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002152
Hanno Beckere1dcb032018-08-17 16:47:58 +01002153 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002154 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002155 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002156 {
2157 ret = ssl_swap_epochs( ssl );
2158 if( ret != 0 )
2159 return( ret );
2160 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002161
Hanno Becker67bc7c32018-08-06 11:33:50 +01002162 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2163 return( ret );
2164
2165 continue;
2166 }
2167 max_hs_frag_len = max_frag_len - 12;
2168
2169 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2170 max_hs_frag_len : rem_len;
2171
2172 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002173 {
2174 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002175 (unsigned) cur_hs_frag_len,
2176 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002177 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002178
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002179 /* Messages are stored with handshake headers as if not fragmented,
2180 * copy beginning of headers then fill fragmentation fields.
2181 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2182 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002183
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002184 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2185 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2186 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2187
Hanno Becker67bc7c32018-08-06 11:33:50 +01002188 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2189 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2190 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002191
2192 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2193
Hanno Becker3f7b9732018-08-28 09:53:25 +01002194 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002195 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2196 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002197 ssl->out_msgtype = cur->type;
2198
2199 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002200 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002201 }
2202
2203 /* If done with the current message move to the next one if any */
2204 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2205 {
2206 if( cur->next != NULL )
2207 {
2208 ssl->handshake->cur_msg = cur->next;
2209 ssl->handshake->cur_msg_p = cur->next->p + 12;
2210 }
2211 else
2212 {
2213 ssl->handshake->cur_msg = NULL;
2214 ssl->handshake->cur_msg_p = NULL;
2215 }
2216 }
2217
2218 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002219 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002222 return( ret );
2223 }
2224 }
2225
Hanno Becker67bc7c32018-08-06 11:33:50 +01002226 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2227 return( ret );
2228
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002229 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2231 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002232 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002235 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002236 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002237
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002239
2240 return( 0 );
2241}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002242
2243/*
2244 * To be called when the last message of an incoming flight is received.
2245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002247{
2248 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002249 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002250 ssl->handshake->flight = NULL;
2251 ssl->handshake->cur_msg = NULL;
2252
2253 /* The next incoming flight will start with this msg_seq */
2254 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2255
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002256 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002257 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002258
Hanno Becker0271f962018-08-16 13:23:47 +01002259 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002260 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002261
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002262 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002263 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2266 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002269 }
2270 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002272}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002273
2274/*
2275 * To be called when the last message of an outgoing flight is send.
2276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002278{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002279 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002280 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2283 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002286 }
2287 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002291
Paul Bakker5121ce52009-01-03 21:22:43 +00002292/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002293 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002295
2296/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002297 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002298 *
2299 * - fill in handshake headers
2300 * - update handshake checksum
2301 * - DTLS: save message for resending
2302 * - then pass to the record layer
2303 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002304 * DTLS: except for HelloRequest, messages are only queued, and will only be
2305 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002306 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002307 * Inputs:
2308 * - ssl->out_msglen: 4 + actual handshake message len
2309 * (4 is the size of handshake headers for TLS)
2310 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2311 * - ssl->out_msg + 4: the handshake message body
2312 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002313 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002314 * - ssl->out_msglen: the length of the record contents
2315 * (including handshake headers but excluding record headers)
2316 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002317 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002318int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002319{
Janos Follath865b3eb2019-12-16 11:46:15 +00002320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002321 const size_t hs_len = ssl->out_msglen - 4;
2322 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002323
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2325
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002326 /*
2327 * Sanity checks
2328 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002329 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002330 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2331 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002334 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002335
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002336 /* Whenever we send anything different from a
2337 * HelloRequest we should be in a handshake - double check. */
2338 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2339 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002340 ssl->handshake == NULL )
2341 {
2342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2343 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2344 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002346#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002347 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002348 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002350 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002353 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002354#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002355
Hanno Beckerb50a2532018-08-06 11:52:54 +01002356 /* Double-check that we did not exceed the bounds
2357 * of the outgoing record buffer.
2358 * This should never fail as the various message
2359 * writing functions must obey the bounds of the
2360 * outgoing record buffer, but better be safe.
2361 *
2362 * Note: We deliberately do not check for the MTU or MFL here.
2363 */
2364 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2365 {
2366 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002367 "size %" MBEDTLS_PRINTF_SIZET
2368 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002369 ssl->out_msglen,
2370 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2372 }
2373
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002374 /*
2375 * Fill handshake headers
2376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002379 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2380 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2381 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002383 /*
2384 * DTLS has additional fields in the Handshake layer,
2385 * between the length field and the actual payload:
2386 * uint16 message_seq;
2387 * uint24 fragment_offset;
2388 * uint24 fragment_length;
2389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002391 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002392 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002393 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002394 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002395 {
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002397 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002398 hs_len,
2399 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002400 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2401 }
2402
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002403 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002404 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002405
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002406 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002407 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002408 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002409 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2410 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2411 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002412 }
2413 else
2414 {
2415 ssl->out_msg[4] = 0;
2416 ssl->out_msg[5] = 0;
2417 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002418
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002419 /* Handshake hashes are computed without fragmentation,
2420 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002421 memset( ssl->out_msg + 6, 0x00, 3 );
2422 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002423 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002425
Hanno Becker0207e532018-08-28 10:28:28 +01002426 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002427 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2428 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 }
2430
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002431 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002434 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2435 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002436 {
2437 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002440 return( ret );
2441 }
2442 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002443 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002444#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002445 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002446 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002447 {
2448 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2449 return( ret );
2450 }
2451 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002452
2453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2454
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002455 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002456}
2457
2458/*
2459 * Record layer functions
2460 */
2461
2462/*
2463 * Write current record.
2464 *
2465 * Uses:
2466 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2467 * - ssl->out_msglen: length of the record content (excl headers)
2468 * - ssl->out_msg: record content
2469 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002470int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002471{
2472 int ret, done = 0;
2473 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002474 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002475
2476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002477
Paul Bakker05ef8352012-05-08 09:17:57 +00002478 if( !done )
2479 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002480 unsigned i;
2481 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002482#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2483 size_t out_buf_len = ssl->out_buf_len;
2484#else
2485 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2486#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002487 /* Skip writing the record content type to after the encryption,
2488 * as it may change when using the CID extension. */
2489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002491 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002492
Hanno Becker19859472018-08-06 09:40:20 +01002493 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002494 ssl->out_len[0] = (unsigned char)( len >> 8 );
2495 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002496
Paul Bakker48916f92012-09-16 19:57:18 +00002497 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002498 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 mbedtls_record rec;
2500
2501 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002502 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002503 rec.data_len = ssl->out_msglen;
2504 rec.data_offset = ssl->out_msg - rec.buf;
2505
2506 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2507 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2508 ssl->conf->transport, rec.ver );
2509 rec.type = ssl->out_msgtype;
2510
Hanno Beckera0e20d02019-05-15 14:03:01 +01002511#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002512 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002513 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002514#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002515
Hanno Beckera18d1322018-01-03 14:27:32 +00002516 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002519 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002520 return( ret );
2521 }
2522
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002523 if( rec.data_offset != 0 )
2524 {
2525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2526 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2527 }
2528
Hanno Becker6430faf2019-05-08 11:57:13 +01002529 /* Update the record content type and CID. */
2530 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002531#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002532 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002533#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002534 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002535 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2536 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002537 }
2538
Hanno Becker5903de42019-05-03 14:46:38 +01002539 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002540
2541#if defined(MBEDTLS_SSL_PROTO_DTLS)
2542 /* In case of DTLS, double-check that we don't exceed
2543 * the remaining space in the datagram. */
2544 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2545 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002546 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002547 if( ret < 0 )
2548 return( ret );
2549
2550 if( protected_record_size > (size_t) ret )
2551 {
2552 /* Should never happen */
2553 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2554 }
2555 }
2556#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002557
Hanno Becker6430faf2019-05-08 11:57:13 +01002558 /* Now write the potentially updated record content type. */
2559 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2560
Paul Elliott9f352112020-12-09 14:55:45 +00002561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002562 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002563 ssl->out_hdr[0], ssl->out_hdr[1],
2564 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002566 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002567 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002568
2569 ssl->out_left += protected_record_size;
2570 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002571 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002572
Hanno Beckerdd772292020-02-05 10:38:31 +00002573 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002574 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2575 break;
2576
2577 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002578 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002579 {
2580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2581 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
Hanno Becker67bc7c32018-08-06 11:33:50 +01002585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002586 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2587 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002588 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002589 size_t remaining;
2590 ret = ssl_get_remaining_payload_in_datagram( ssl );
2591 if( ret < 0 )
2592 {
2593 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2594 ret );
2595 return( ret );
2596 }
2597
2598 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002599 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002600 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002601 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002602 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002603 else
2604 {
Hanno Becker513815a2018-08-20 11:56:09 +01002605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002606 }
2607 }
2608#endif /* MBEDTLS_SSL_PROTO_DTLS */
2609
2610 if( ( flush == SSL_FORCE_FLUSH ) &&
2611 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 return( ret );
2615 }
2616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618
2619 return( 0 );
2620}
2621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01002623
2624static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
2625{
2626 if( ssl->in_msglen < ssl->in_hslen ||
2627 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2628 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
2629 {
2630 return( 1 );
2631 }
2632 return( 0 );
2633}
Hanno Becker44650b72018-08-16 12:51:11 +01002634
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002635static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002636{
2637 return( ( ssl->in_msg[9] << 16 ) |
2638 ( ssl->in_msg[10] << 8 ) |
2639 ssl->in_msg[11] );
2640}
2641
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002642static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002643{
2644 return( ( ssl->in_msg[6] << 16 ) |
2645 ( ssl->in_msg[7] << 8 ) |
2646 ssl->in_msg[8] );
2647}
2648
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002649static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01002650{
2651 uint32_t msg_len, frag_off, frag_len;
2652
2653 msg_len = ssl_get_hs_total_len( ssl );
2654 frag_off = ssl_get_hs_frag_off( ssl );
2655 frag_len = ssl_get_hs_frag_len( ssl );
2656
2657 if( frag_off > msg_len )
2658 return( -1 );
2659
2660 if( frag_len > msg_len - frag_off )
2661 return( -1 );
2662
2663 if( frag_len + 12 > ssl->in_msglen )
2664 return( -1 );
2665
2666 return( 0 );
2667}
2668
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002669/*
2670 * Mark bits in bitmask (used for DTLS HS reassembly)
2671 */
2672static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2673{
2674 unsigned int start_bits, end_bits;
2675
2676 start_bits = 8 - ( offset % 8 );
2677 if( start_bits != 8 )
2678 {
2679 size_t first_byte_idx = offset / 8;
2680
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002681 /* Special case */
2682 if( len <= start_bits )
2683 {
2684 for( ; len != 0; len-- )
2685 mask[first_byte_idx] |= 1 << ( start_bits - len );
2686
2687 /* Avoid potential issues with offset or len becoming invalid */
2688 return;
2689 }
2690
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002691 offset += start_bits; /* Now offset % 8 == 0 */
2692 len -= start_bits;
2693
2694 for( ; start_bits != 0; start_bits-- )
2695 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2696 }
2697
2698 end_bits = len % 8;
2699 if( end_bits != 0 )
2700 {
2701 size_t last_byte_idx = ( offset + len ) / 8;
2702
2703 len -= end_bits; /* Now len % 8 == 0 */
2704
2705 for( ; end_bits != 0; end_bits-- )
2706 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2707 }
2708
2709 memset( mask + offset / 8, 0xFF, len / 8 );
2710}
2711
2712/*
2713 * Check that bitmask is full
2714 */
2715static int ssl_bitmask_check( unsigned char *mask, size_t len )
2716{
2717 size_t i;
2718
2719 for( i = 0; i < len / 8; i++ )
2720 if( mask[i] != 0xFF )
2721 return( -1 );
2722
2723 for( i = 0; i < len % 8; i++ )
2724 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2725 return( -1 );
2726
2727 return( 0 );
2728}
2729
Hanno Becker56e205e2018-08-16 09:06:12 +01002730/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01002731static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002732 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002733{
Hanno Becker56e205e2018-08-16 09:06:12 +01002734 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002735
Hanno Becker56e205e2018-08-16 09:06:12 +01002736 alloc_len = 12; /* Handshake header */
2737 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002738
Hanno Beckerd07df862018-08-16 09:14:58 +01002739 if( add_bitmap )
2740 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002741
Hanno Becker2a97b0e2018-08-21 15:47:49 +01002742 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002743}
Hanno Becker56e205e2018-08-16 09:06:12 +01002744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002746
Hanno Beckercd9dcda2018-08-28 17:18:56 +01002747static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01002748{
2749 return( ( ssl->in_msg[1] << 16 ) |
2750 ( ssl->in_msg[2] << 8 ) |
2751 ssl->in_msg[3] );
2752}
Hanno Beckere25e3b72018-08-16 09:30:53 +01002753
Simon Butcher99000142016-10-13 17:21:01 +01002754int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002757 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002759 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002761 }
2762
Hanno Becker12555c62018-08-16 12:47:53 +01002763 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00002766 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002767 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002770 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002771 {
Janos Follath865b3eb2019-12-16 11:46:15 +00002772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002773 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002774
Hanno Becker44650b72018-08-16 12:51:11 +01002775 if( ssl_check_hs_header( ssl ) != 0 )
2776 {
2777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
2778 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
2779 }
2780
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002781 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01002782 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
2783 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
2784 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
2785 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002786 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01002787 if( recv_msg_seq > ssl->handshake->in_msg_seq )
2788 {
2789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
2790 recv_msg_seq,
2791 ssl->handshake->in_msg_seq ) );
2792 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
2793 }
2794
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002795 /* Retransmit only on last message from previous flight, to avoid
2796 * too many retransmissions.
2797 * Besides, No sane server ever retransmits HelloVerifyRequest */
2798 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00002802 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002803 recv_msg_seq,
2804 ssl->handshake->in_flight_start_seq ) );
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002809 return( ret );
2810 }
2811 }
2812 else
2813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00002815 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002816 recv_msg_seq,
2817 ssl->handshake->in_msg_seq ) );
2818 }
2819
Hanno Becker90333da2017-10-10 11:27:13 +01002820 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002821 }
2822 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002823
Hanno Becker6d97ef52018-08-16 13:09:04 +01002824 /* Message reassembly is handled alongside buffering of future
2825 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01002826 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01002827 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01002828 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01002831 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002832 }
2833 }
2834 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002836 /* With TLS we don't handle fragmentation (for now) */
2837 if( ssl->in_msglen < ssl->in_hslen )
2838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
2840 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002841 }
2842
Simon Butcher99000142016-10-13 17:21:01 +01002843 return( 0 );
2844}
2845
2846void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
2847{
Hanno Becker0271f962018-08-16 13:23:47 +01002848 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01002849
Hanno Becker0271f962018-08-16 13:23:47 +01002850 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002851 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002852 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02002853 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002854
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002855 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002858 ssl->handshake != NULL )
2859 {
Hanno Becker0271f962018-08-16 13:23:47 +01002860 unsigned offset;
2861 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01002862
Hanno Becker0271f962018-08-16 13:23:47 +01002863 /* Increment handshake sequence number */
2864 hs->in_msg_seq++;
2865
2866 /*
2867 * Clear up handshake buffering and reassembly structure.
2868 */
2869
2870 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01002871 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01002872
2873 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01002874 for( offset = 0, hs_buf = &hs->buffering.hs[0];
2875 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01002876 offset++, hs_buf++ )
2877 {
2878 *hs_buf = *(hs_buf + 1);
2879 }
2880
2881 /* Create a fresh last entry */
2882 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002883 }
2884#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002885}
2886
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002887/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002888 * DTLS anti-replay: RFC 6347 4.1.2.6
2889 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002890 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2891 * Bit n is set iff record number in_window_top - n has been seen.
2892 *
2893 * Usually, in_window_top is the last record number seen and the lsb of
2894 * in_window is set. The only exception is the initial state (record number 0
2895 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00002898void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002899{
2900 ssl->in_window_top = 0;
2901 ssl->in_window = 0;
2902}
2903
2904static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2905{
2906 return( ( (uint64_t) buf[0] << 40 ) |
2907 ( (uint64_t) buf[1] << 32 ) |
2908 ( (uint64_t) buf[2] << 24 ) |
2909 ( (uint64_t) buf[3] << 16 ) |
2910 ( (uint64_t) buf[4] << 8 ) |
2911 ( (uint64_t) buf[5] ) );
2912}
2913
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002914static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
2915{
Janos Follath865b3eb2019-12-16 11:46:15 +00002916 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02002917 unsigned char *original_in_ctr;
2918
2919 // save original in_ctr
2920 original_in_ctr = ssl->in_ctr;
2921
2922 // use counter from record
2923 ssl->in_ctr = record_in_ctr;
2924
2925 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
2926
2927 // restore the counter
2928 ssl->in_ctr = original_in_ctr;
2929
2930 return ret;
2931}
2932
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002933/*
2934 * Return 0 if sequence number is acceptable, -1 otherwise
2935 */
Hanno Becker0183d692019-07-12 08:50:37 +01002936int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002937{
2938 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2939 uint64_t bit;
2940
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002941 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002942 return( 0 );
2943
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002944 if( rec_seqnum > ssl->in_window_top )
2945 return( 0 );
2946
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002947 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002948
2949 if( bit >= 64 )
2950 return( -1 );
2951
2952 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2953 return( -1 );
2954
2955 return( 0 );
2956}
2957
2958/*
2959 * Update replay window on new validated record
2960 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002961void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002962{
2963 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2964
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002965 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002966 return;
2967
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002968 if( rec_seqnum > ssl->in_window_top )
2969 {
2970 /* Update window_top and the contents of the window */
2971 uint64_t shift = rec_seqnum - ssl->in_window_top;
2972
2973 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002974 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002975 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002976 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002977 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002978 ssl->in_window |= 1;
2979 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002980
2981 ssl->in_window_top = rec_seqnum;
2982 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002983 else
2984 {
2985 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002986 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002987
2988 if( bit < 64 ) /* Always true, but be extra sure */
2989 ssl->in_window |= (uint64_t) 1 << bit;
2990 }
2991}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002993
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02002994#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02002995/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002996 * Without any SSL context, check if a datagram looks like a ClientHello with
2997 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01002998 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02002999 *
3000 * - if cookie is valid, return 0
3001 * - if ClientHello looks superficially valid but cookie is not,
3002 * fill obuf and set olen, then
3003 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3004 * - otherwise return a specific error code
3005 */
3006static int ssl_check_dtls_clihlo_cookie(
3007 mbedtls_ssl_cookie_write_t *f_cookie_write,
3008 mbedtls_ssl_cookie_check_t *f_cookie_check,
3009 void *p_cookie,
3010 const unsigned char *cli_id, size_t cli_id_len,
3011 const unsigned char *in, size_t in_len,
3012 unsigned char *obuf, size_t buf_len, size_t *olen )
3013{
3014 size_t sid_len, cookie_len;
3015 unsigned char *p;
3016
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003017 /*
3018 * Structure of ClientHello with record and handshake headers,
3019 * and expected values. We don't need to check a lot, more checks will be
3020 * done when actually parsing the ClientHello - skipping those checks
3021 * avoids code duplication and does not make cookie forging any easier.
3022 *
3023 * 0-0 ContentType type; copied, must be handshake
3024 * 1-2 ProtocolVersion version; copied
3025 * 3-4 uint16 epoch; copied, must be 0
3026 * 5-10 uint48 sequence_number; copied
3027 * 11-12 uint16 length; (ignored)
3028 *
3029 * 13-13 HandshakeType msg_type; (ignored)
3030 * 14-16 uint24 length; (ignored)
3031 * 17-18 uint16 message_seq; copied
3032 * 19-21 uint24 fragment_offset; copied, must be 0
3033 * 22-24 uint24 fragment_length; (ignored)
3034 *
3035 * 25-26 ProtocolVersion client_version; (ignored)
3036 * 27-58 Random random; (ignored)
3037 * 59-xx SessionID session_id; 1 byte len + sid_len content
3038 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3039 * ...
3040 *
3041 * Minimum length is 61 bytes.
3042 */
3043 if( in_len < 61 ||
3044 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3045 in[3] != 0 || in[4] != 0 ||
3046 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3047 {
3048 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3049 }
3050
3051 sid_len = in[59];
3052 if( sid_len > in_len - 61 )
3053 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3054
3055 cookie_len = in[60 + sid_len];
3056 if( cookie_len > in_len - 60 )
3057 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3058
3059 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3060 cli_id, cli_id_len ) == 0 )
3061 {
3062 /* Valid cookie */
3063 return( 0 );
3064 }
3065
3066 /*
3067 * If we get here, we've got an invalid cookie, let's prepare HVR.
3068 *
3069 * 0-0 ContentType type; copied
3070 * 1-2 ProtocolVersion version; copied
3071 * 3-4 uint16 epoch; copied
3072 * 5-10 uint48 sequence_number; copied
3073 * 11-12 uint16 length; olen - 13
3074 *
3075 * 13-13 HandshakeType msg_type; hello_verify_request
3076 * 14-16 uint24 length; olen - 25
3077 * 17-18 uint16 message_seq; copied
3078 * 19-21 uint24 fragment_offset; copied
3079 * 22-24 uint24 fragment_length; olen - 25
3080 *
3081 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3082 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3083 *
3084 * Minimum length is 28.
3085 */
3086 if( buf_len < 28 )
3087 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3088
3089 /* Copy most fields and adapt others */
3090 memcpy( obuf, in, 25 );
3091 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3092 obuf[25] = 0xfe;
3093 obuf[26] = 0xff;
3094
3095 /* Generate and write actual cookie */
3096 p = obuf + 28;
3097 if( f_cookie_write( p_cookie,
3098 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3099 {
3100 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3101 }
3102
3103 *olen = p - obuf;
3104
3105 /* Go back and fill length fields */
3106 obuf[27] = (unsigned char)( *olen - 28 );
3107
3108 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3109 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3110 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3111
3112 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3113 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3114
3115 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3116}
3117
3118/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003119 * Handle possible client reconnect with the same UDP quadruplet
3120 * (RFC 6347 Section 4.2.8).
3121 *
3122 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3123 * that looks like a ClientHello.
3124 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003125 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003126 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003127 * - if the input looks like a ClientHello with a valid cookie,
3128 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003129 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003130 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003131 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003132 * This function is called (through ssl_check_client_reconnect()) when an
3133 * unexpected record is found in ssl_get_next_record(), which will discard the
3134 * record if we return 0, and bubble up the return value otherwise (this
3135 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3136 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003137 */
3138static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3139{
Janos Follath865b3eb2019-12-16 11:46:15 +00003140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003141 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003142
Hanno Becker2fddd372019-07-10 14:37:41 +01003143 if( ssl->conf->f_cookie_write == NULL ||
3144 ssl->conf->f_cookie_check == NULL )
3145 {
3146 /* If we can't use cookies to verify reachability of the peer,
3147 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3149 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003150 return( 0 );
3151 }
3152
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003153 ret = ssl_check_dtls_clihlo_cookie(
3154 ssl->conf->f_cookie_write,
3155 ssl->conf->f_cookie_check,
3156 ssl->conf->p_cookie,
3157 ssl->cli_id, ssl->cli_id_len,
3158 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003159 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003160
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003161 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3162
3163 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003164 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003165 int send_ret;
3166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3167 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3168 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003169 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003170 * If the error is permanent we'll catch it later,
3171 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003172 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3173 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3174 (void) send_ret;
3175
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003176 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003177 }
3178
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003179 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003180 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003182 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003183 {
3184 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3185 return( ret );
3186 }
3187
3188 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003189 }
3190
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003191 return( ret );
3192}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003193#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003194
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003195static int ssl_check_record_type( uint8_t record_type )
3196{
3197 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3198 record_type != MBEDTLS_SSL_MSG_ALERT &&
3199 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3200 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3201 {
3202 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3203 }
3204
3205 return( 0 );
3206}
3207
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003208/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003209 * ContentType type;
3210 * ProtocolVersion version;
3211 * uint16 epoch; // DTLS only
3212 * uint48 sequence_number; // DTLS only
3213 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003214 *
3215 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003216 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003217 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3218 *
3219 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003220 * 1. proceed with the record if this function returns 0
3221 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3222 * 3. return CLIENT_RECONNECT if this function return that value
3223 * 4. drop the whole datagram if this function returns anything else.
3224 * Point 2 is needed when the peer is resending, and we have already received
3225 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003226 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003227static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003228 unsigned char *buf,
3229 size_t len,
3230 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003231{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003232 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Hanno Beckere5e7e782019-07-11 12:29:35 +01003234 size_t const rec_hdr_type_offset = 0;
3235 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003236
Hanno Beckere5e7e782019-07-11 12:29:35 +01003237 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3238 rec_hdr_type_len;
3239 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
Hanno Beckere5e7e782019-07-11 12:29:35 +01003241 size_t const rec_hdr_ctr_len = 8;
3242#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003243 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003244 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3245 rec_hdr_version_len;
3246
Hanno Beckera0e20d02019-05-15 14:03:01 +01003247#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003248 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3249 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003250 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003251#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3252#endif /* MBEDTLS_SSL_PROTO_DTLS */
3253
3254 size_t rec_hdr_len_offset; /* To be determined */
3255 size_t const rec_hdr_len_len = 2;
3256
3257 /*
3258 * Check minimum lengths for record header.
3259 */
3260
3261#if defined(MBEDTLS_SSL_PROTO_DTLS)
3262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3263 {
3264 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3265 }
3266 else
3267#endif /* MBEDTLS_SSL_PROTO_DTLS */
3268 {
3269 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3270 }
3271
3272 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3273 {
3274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3275 (unsigned) len,
3276 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3277 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3278 }
3279
3280 /*
3281 * Parse and validate record content type
3282 */
3283
3284 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003285
3286 /* Check record content type */
3287#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3288 rec->cid_len = 0;
3289
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003291 ssl->conf->cid_len != 0 &&
3292 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003293 {
3294 /* Shift pointers to account for record header including CID
3295 * struct {
3296 * ContentType special_type = tls12_cid;
3297 * ProtocolVersion version;
3298 * uint16 epoch;
3299 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003300 * opaque cid[cid_length]; // Additional field compared to
3301 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003302 * uint16 length;
3303 * opaque enc_content[DTLSCiphertext.length];
3304 * } DTLSCiphertext;
3305 */
3306
3307 /* So far, we only support static CID lengths
3308 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003309 rec_hdr_cid_len = ssl->conf->cid_len;
3310 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003311
Hanno Beckere5e7e782019-07-11 12:29:35 +01003312 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003313 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3315 (unsigned) len,
3316 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003317 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003318 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003319
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003320 /* configured CID len is guaranteed at most 255, see
3321 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3322 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003323 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003324 }
3325 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003326#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003327 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003328 if( ssl_check_record_type( rec->type ) )
3329 {
Hanno Becker54229812019-07-12 14:40:00 +01003330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3331 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003332 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3333 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003334 }
3335
Hanno Beckere5e7e782019-07-11 12:29:35 +01003336 /*
3337 * Parse and validate record version
3338 */
3339
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003340 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3341 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003342 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3343 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003344 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003345
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003346 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3349 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 }
3351
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003352 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3355 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 }
3357
Hanno Beckere5e7e782019-07-11 12:29:35 +01003358 /*
3359 * Parse/Copy record sequence number.
3360 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003361
Hanno Beckere5e7e782019-07-11 12:29:35 +01003362#if defined(MBEDTLS_SSL_PROTO_DTLS)
3363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003364 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003365 /* Copy explicit record sequence number from input buffer. */
3366 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3367 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003368 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003369 else
3370#endif /* MBEDTLS_SSL_PROTO_DTLS */
3371 {
3372 /* Copy implicit record sequence number from SSL context structure. */
3373 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3374 }
Paul Bakker40e46942009-01-03 21:51:57 +00003375
Hanno Beckere5e7e782019-07-11 12:29:35 +01003376 /*
3377 * Parse record length.
3378 */
3379
Hanno Beckere5e7e782019-07-11 12:29:35 +01003380 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003381 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3382 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003383 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384
Paul Elliott9f352112020-12-09 14:55:45 +00003385 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003386 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003387 rec->type,
3388 major_ver, minor_ver, rec->data_len ) );
3389
3390 rec->buf = buf;
3391 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003392
Hanno Beckerd417cc92019-07-26 08:20:27 +01003393 if( rec->data_len == 0 )
3394 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003396 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003397 * DTLS-related tests.
3398 * Check epoch before checking length constraint because
3399 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3400 * message gets duplicated before the corresponding Finished message,
3401 * the second ChangeCipherSpec should be discarded because it belongs
3402 * to an old epoch, but not because its length is shorter than
3403 * the minimum record length for packets using the new record transform.
3404 * Note that these two kinds of failures are handled differently,
3405 * as an unexpected record is silently skipped but an invalid
3406 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003407 */
3408#if defined(MBEDTLS_SSL_PROTO_DTLS)
3409 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3410 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003411 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003412
Hanno Becker955a5c92019-07-10 17:12:07 +01003413 /* Check that the datagram is large enough to contain a record
3414 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003415 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003416 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3418 (unsigned) len,
3419 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3421 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003422
Hanno Becker37cfe732019-07-10 17:20:01 +01003423 /* Records from other, non-matching epochs are silently discarded.
3424 * (The case of same-port Client reconnects must be considered in
3425 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003426 if( rec_epoch != ssl->in_epoch )
3427 {
3428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003429 "expected %u, received %lu",
3430 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003431
Hanno Becker552f7472019-07-19 10:59:12 +01003432 /* Records from the next epoch are considered for buffering
3433 * (concretely: early Finished messages). */
3434 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003435 {
Hanno Becker552f7472019-07-19 10:59:12 +01003436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3437 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003438 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003439
Hanno Becker2fddd372019-07-10 14:37:41 +01003440 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003441 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003443 /* For records from the correct epoch, check whether their
3444 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003445 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3446 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003447 {
3448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3449 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3450 }
3451#endif
3452 }
3453#endif /* MBEDTLS_SSL_PROTO_DTLS */
3454
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003455 return( 0 );
3456}
Paul Bakker5121ce52009-01-03 21:22:43 +00003457
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
Hanno Becker2fddd372019-07-10 14:37:41 +01003459#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3460static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3461{
3462 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3463
3464 /*
3465 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3466 * access the first byte of record content (handshake type), as we
3467 * have an active transform (possibly iv_len != 0), so use the
3468 * fact that the record header len is 13 instead.
3469 */
3470 if( rec_epoch == 0 &&
3471 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3472 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3473 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3474 ssl->in_left > 13 &&
3475 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3476 {
3477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3478 "from the same port" ) );
3479 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003480 }
3481
3482 return( 0 );
3483}
Hanno Becker2fddd372019-07-10 14:37:41 +01003484#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003486/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003487 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003488 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003489static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3490 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003491{
3492 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003494 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003495 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003496
Paul Bakker48916f92012-09-16 19:57:18 +00003497 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003499 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003500
Hanno Beckera18d1322018-01-03 14:27:32 +00003501 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003502 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003505
Hanno Beckera0e20d02019-05-15 14:03:01 +01003506#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003507 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3508 ssl->conf->ignore_unexpected_cid
3509 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3510 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003512 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003513 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003514#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003515
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 return( ret );
3517 }
3518
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003519 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003520 {
3521 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003522 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003523 }
3524
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003525 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003526 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003527
Hanno Beckera0e20d02019-05-15 14:03:01 +01003528#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003529 /* We have already checked the record content type
3530 * in ssl_parse_record_header(), failing or silently
3531 * dropping the record in the case of an unknown type.
3532 *
3533 * Since with the use of CIDs, the record content type
3534 * might change during decryption, re-check the record
3535 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003536 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003537 {
3538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3539 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3540 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003541#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003542
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003543 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003544 {
3545#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3546 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003547 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003548 {
3549 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3551 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3552 }
3553#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3554
3555 ssl->nb_zero++;
3556
3557 /*
3558 * Three or more empty messages may be a DoS attack
3559 * (excessive CPU consumption).
3560 */
3561 if( ssl->nb_zero > 3 )
3562 {
3563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003564 "messages, possible DoS attack" ) );
3565 /* Treat the records as if they were not properly authenticated,
3566 * thereby failing the connection if we see more than allowed
3567 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003568 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3569 }
3570 }
3571 else
3572 ssl->nb_zero = 0;
3573
3574#if defined(MBEDTLS_SSL_PROTO_DTLS)
3575 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3576 {
3577 ; /* in_ctr read from peer, not maintained internally */
3578 }
3579 else
3580#endif
3581 {
3582 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003583 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003584 if( ++ssl->in_ctr[i - 1] != 0 )
3585 break;
3586
3587 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003588 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003589 {
3590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3591 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3592 }
3593 }
3594
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 }
3596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003598 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003600 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003601 }
3602#endif
3603
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003604 /* Check actual (decrypted) record content length against
3605 * configured maximum. */
3606 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3607 {
3608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3609 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3610 }
3611
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003612 return( 0 );
3613}
3614
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003615/*
3616 * Read a record.
3617 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02003618 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
3619 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
3620 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003621 */
Hanno Becker1097b342018-08-15 14:09:41 +01003622
3623/* Helper functions for mbedtls_ssl_read_record(). */
3624static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01003625static int ssl_get_next_record( mbedtls_ssl_context *ssl );
3626static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01003627
Hanno Becker327c93b2018-08-15 13:56:18 +01003628int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01003629 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003630{
Janos Follath865b3eb2019-12-16 11:46:15 +00003631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003634
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003635 if( ssl->keep_current_message == 0 )
3636 {
3637 do {
Simon Butcher99000142016-10-13 17:21:01 +01003638
Hanno Becker26994592018-08-15 14:14:59 +01003639 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01003640 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003641 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01003642
Hanno Beckere74d5562018-08-15 14:26:08 +01003643 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003644 {
Hanno Becker40f50842018-08-15 14:48:01 +01003645#if defined(MBEDTLS_SSL_PROTO_DTLS)
3646 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01003647
Hanno Becker40f50842018-08-15 14:48:01 +01003648 /* We only check for buffered messages if the
3649 * current datagram is fully consumed. */
3650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003651 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01003652 {
Hanno Becker40f50842018-08-15 14:48:01 +01003653 if( ssl_load_buffered_message( ssl ) == 0 )
3654 have_buffered = 1;
3655 }
3656
3657 if( have_buffered == 0 )
3658#endif /* MBEDTLS_SSL_PROTO_DTLS */
3659 {
3660 ret = ssl_get_next_record( ssl );
3661 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
3662 continue;
3663
3664 if( ret != 0 )
3665 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01003666 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003667 return( ret );
3668 }
Hanno Beckere74d5562018-08-15 14:26:08 +01003669 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003670 }
3671
3672 ret = mbedtls_ssl_handle_message_type( ssl );
3673
Hanno Becker40f50842018-08-15 14:48:01 +01003674#if defined(MBEDTLS_SSL_PROTO_DTLS)
3675 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
3676 {
3677 /* Buffer future message */
3678 ret = ssl_buffer_message( ssl );
3679 if( ret != 0 )
3680 return( ret );
3681
3682 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
3683 }
3684#endif /* MBEDTLS_SSL_PROTO_DTLS */
3685
Hanno Becker90333da2017-10-10 11:27:13 +01003686 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
3687 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003688
3689 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01003690 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00003691 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01003692 return( ret );
3693 }
3694
Hanno Becker327c93b2018-08-15 13:56:18 +01003695 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01003696 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003697 {
3698 mbedtls_ssl_update_handshake_status( ssl );
3699 }
Simon Butcher99000142016-10-13 17:21:01 +01003700 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003701 else
Simon Butcher99000142016-10-13 17:21:01 +01003702 {
Hanno Becker02f59072018-08-15 14:00:24 +01003703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01003704 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01003705 }
3706
3707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3708
3709 return( 0 );
3710}
3711
Hanno Becker40f50842018-08-15 14:48:01 +01003712#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01003713static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01003714{
Hanno Becker40f50842018-08-15 14:48:01 +01003715 if( ssl->in_left > ssl->next_record_offset )
3716 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01003717
Hanno Becker40f50842018-08-15 14:48:01 +01003718 return( 0 );
3719}
3720
3721static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
3722{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003723 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01003724 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003725 int ret = 0;
3726
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003727 if( hs == NULL )
3728 return( -1 );
3729
Hanno Beckere00ae372018-08-20 09:39:42 +01003730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
3731
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003732 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
3733 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
3734 {
3735 /* Check if we have seen a ChangeCipherSpec before.
3736 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003737 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003738 {
3739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
3740 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01003741 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003742 }
3743
Hanno Becker39b8bc92018-08-28 17:17:13 +01003744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003745 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
3746 ssl->in_msglen = 1;
3747 ssl->in_msg[0] = 1;
3748
3749 /* As long as they are equal, the exact value doesn't matter. */
3750 ssl->in_left = 0;
3751 ssl->next_record_offset = 0;
3752
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003753 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003754 goto exit;
3755 }
Hanno Becker37f95322018-08-16 13:55:32 +01003756
Hanno Beckerb8f50142018-08-28 10:01:34 +01003757#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01003758 /* Debug only */
3759 {
3760 unsigned offset;
3761 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
3762 {
3763 hs_buf = &hs->buffering.hs[offset];
3764 if( hs_buf->is_valid == 1 )
3765 {
3766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
3767 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01003768 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01003769 }
3770 }
3771 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01003772#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01003773
3774 /* Check if we have buffered and/or fully reassembled the
3775 * next handshake message. */
3776 hs_buf = &hs->buffering.hs[0];
3777 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
3778 {
3779 /* Synthesize a record containing the buffered HS message. */
3780 size_t msg_len = ( hs_buf->data[1] << 16 ) |
3781 ( hs_buf->data[2] << 8 ) |
3782 hs_buf->data[3];
3783
3784 /* Double-check that we haven't accidentally buffered
3785 * a message that doesn't fit into the input buffer. */
3786 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
3787 {
3788 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3789 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3790 }
3791
3792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
3793 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
3794 hs_buf->data, msg_len + 12 );
3795
3796 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3797 ssl->in_hslen = msg_len + 12;
3798 ssl->in_msglen = msg_len + 12;
3799 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
3800
3801 ret = 0;
3802 goto exit;
3803 }
3804 else
3805 {
3806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
3807 hs->in_msg_seq ) );
3808 }
3809
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003810 ret = -1;
3811
3812exit:
3813
3814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
3815 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01003816}
3817
Hanno Beckera02b0b42018-08-21 17:20:27 +01003818static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
3819 size_t desired )
3820{
3821 int offset;
3822 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
3824 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003825
Hanno Becker01315ea2018-08-21 17:22:17 +01003826 /* Get rid of future records epoch first, if such exist. */
3827 ssl_free_buffered_record( ssl );
3828
3829 /* Check if we have enough space available now. */
3830 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3831 hs->buffering.total_bytes_buffered ) )
3832 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01003834 return( 0 );
3835 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01003836
Hanno Becker4f432ad2018-08-28 10:02:32 +01003837 /* We don't have enough space to buffer the next expected handshake
3838 * message. Remove buffers used for future messages to gain space,
3839 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01003840 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
3841 offset >= 0; offset-- )
3842 {
3843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
3844 offset ) );
3845
Hanno Beckerb309b922018-08-23 13:18:05 +01003846 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003847
3848 /* Check if we have enough space available now. */
3849 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3850 hs->buffering.total_bytes_buffered ) )
3851 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01003852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01003853 return( 0 );
3854 }
3855 }
3856
3857 return( -1 );
3858}
3859
Hanno Becker40f50842018-08-15 14:48:01 +01003860static int ssl_buffer_message( mbedtls_ssl_context *ssl )
3861{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003862 int ret = 0;
3863 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
3864
3865 if( hs == NULL )
3866 return( 0 );
3867
3868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
3869
3870 switch( ssl->in_msgtype )
3871 {
3872 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
3873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01003874
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003875 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003876 break;
3877
3878 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01003879 {
3880 unsigned recv_msg_seq_offset;
3881 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
3882 mbedtls_ssl_hs_buffer *hs_buf;
3883 size_t msg_len = ssl->in_hslen - 12;
3884
3885 /* We should never receive an old handshake
3886 * message - double-check nonetheless. */
3887 if( recv_msg_seq < ssl->handshake->in_msg_seq )
3888 {
3889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3890 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3891 }
3892
3893 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
3894 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
3895 {
3896 /* Silently ignore -- message too far in the future */
3897 MBEDTLS_SSL_DEBUG_MSG( 2,
3898 ( "Ignore future HS message with sequence number %u, "
3899 "buffering window %u - %u",
3900 recv_msg_seq, ssl->handshake->in_msg_seq,
3901 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
3902
3903 goto exit;
3904 }
3905
3906 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
3907 recv_msg_seq, recv_msg_seq_offset ) );
3908
3909 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
3910
3911 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01003912 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01003913 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003914 size_t reassembly_buf_sz;
3915
Hanno Becker37f95322018-08-16 13:55:32 +01003916 hs_buf->is_fragmented =
3917 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
3918
3919 /* We copy the message back into the input buffer
3920 * after reassembly, so check that it's not too large.
3921 * This is an implementation-specific limitation
3922 * and not one from the standard, hence it is not
3923 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01003924 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01003925 {
3926 /* Ignore message */
3927 goto exit;
3928 }
3929
Hanno Beckere0b150f2018-08-21 15:51:03 +01003930 /* Check if we have enough space to buffer the message. */
3931 if( hs->buffering.total_bytes_buffered >
3932 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
3933 {
3934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3936 }
3937
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003938 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
3939 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003940
3941 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
3942 hs->buffering.total_bytes_buffered ) )
3943 {
3944 if( recv_msg_seq_offset > 0 )
3945 {
3946 /* If we can't buffer a future message because
3947 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00003948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3949 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3950 " (already %" MBEDTLS_PRINTF_SIZET
3951 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003952 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003953 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01003954 goto exit;
3955 }
Hanno Beckere1801392018-08-21 16:51:05 +01003956 else
3957 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
3959 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
3960 " (already %" MBEDTLS_PRINTF_SIZET
3961 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00003962 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003963 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01003964 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003965
Hanno Beckera02b0b42018-08-21 17:20:27 +01003966 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003967 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003968 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
3969 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
3970 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
3971 " (already %" MBEDTLS_PRINTF_SIZET
3972 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00003973 msg_len,
3974 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00003975 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00003976 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01003977 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3978 goto exit;
3979 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003980 }
3981
Paul Elliottd48d5c62021-01-07 14:47:05 +00003982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01003983 msg_len ) );
3984
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003985 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
3986 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01003987 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01003988 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01003989 goto exit;
3990 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01003991 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01003992
3993 /* Prepare final header: copy msg_type, length and message_seq,
3994 * then add standardised fragment_offset and fragment_length */
3995 memcpy( hs_buf->data, ssl->in_msg, 6 );
3996 memset( hs_buf->data + 6, 0, 3 );
3997 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
3998
3999 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004000
4001 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004002 }
4003 else
4004 {
4005 /* Make sure msg_type and length are consistent */
4006 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4007 {
4008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4009 /* Ignore */
4010 goto exit;
4011 }
4012 }
4013
Hanno Becker4422bbb2018-08-20 09:40:19 +01004014 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004015 {
4016 size_t frag_len, frag_off;
4017 unsigned char * const msg = hs_buf->data + 12;
4018
4019 /*
4020 * Check and copy current fragment
4021 */
4022
4023 /* Validation of header fields already done in
4024 * mbedtls_ssl_prepare_handshake_record(). */
4025 frag_off = ssl_get_hs_frag_off( ssl );
4026 frag_len = ssl_get_hs_frag_len( ssl );
4027
Paul Elliottd48d5c62021-01-07 14:47:05 +00004028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4029 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004030 frag_off, frag_len ) );
4031 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4032
4033 if( hs_buf->is_fragmented )
4034 {
4035 unsigned char * const bitmask = msg + msg_len;
4036 ssl_bitmask_set( bitmask, frag_off, frag_len );
4037 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4038 msg_len ) == 0 );
4039 }
4040 else
4041 {
4042 hs_buf->is_complete = 1;
4043 }
4044
4045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4046 hs_buf->is_complete ? "" : "not yet " ) );
4047 }
4048
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004049 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004050 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004051
4052 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004053 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004054 break;
4055 }
4056
4057exit:
4058
4059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4060 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004061}
4062#endif /* MBEDTLS_SSL_PROTO_DTLS */
4063
Hanno Becker1097b342018-08-15 14:09:41 +01004064static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004065{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004066 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004067 * Consume last content-layer message and potentially
4068 * update in_msglen which keeps track of the contents'
4069 * consumption state.
4070 *
4071 * (1) Handshake messages:
4072 * Remove last handshake message, move content
4073 * and adapt in_msglen.
4074 *
4075 * (2) Alert messages:
4076 * Consume whole record content, in_msglen = 0.
4077 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004078 * (3) Change cipher spec:
4079 * Consume whole record content, in_msglen = 0.
4080 *
4081 * (4) Application data:
4082 * Don't do anything - the record layer provides
4083 * the application data as a stream transport
4084 * and consumes through mbedtls_ssl_read only.
4085 *
4086 */
4087
4088 /* Case (1): Handshake messages */
4089 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004090 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004091 /* Hard assertion to be sure that no application data
4092 * is in flight, as corrupting ssl->in_msglen during
4093 * ssl->in_offt != NULL is fatal. */
4094 if( ssl->in_offt != NULL )
4095 {
4096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4097 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4098 }
4099
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004100 /*
4101 * Get next Handshake message in the current record
4102 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004103
Hanno Becker4a810fb2017-05-24 16:27:30 +01004104 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004105 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004106 * current handshake content: If DTLS handshake
4107 * fragmentation is used, that's the fragment
4108 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004109 * size here is faulty and should be changed at
4110 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004111 * (2) While it doesn't seem to cause problems, one
4112 * has to be very careful not to assume that in_hslen
4113 * is always <= in_msglen in a sensible communication.
4114 * Again, it's wrong for DTLS handshake fragmentation.
4115 * The following check is therefore mandatory, and
4116 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004117 * Additionally, ssl->in_hslen might be arbitrarily out of
4118 * bounds after handling a DTLS message with an unexpected
4119 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004120 */
4121 if( ssl->in_hslen < ssl->in_msglen )
4122 {
4123 ssl->in_msglen -= ssl->in_hslen;
4124 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4125 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004126
Hanno Becker4a810fb2017-05-24 16:27:30 +01004127 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4128 ssl->in_msg, ssl->in_msglen );
4129 }
4130 else
4131 {
4132 ssl->in_msglen = 0;
4133 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004134
Hanno Becker4a810fb2017-05-24 16:27:30 +01004135 ssl->in_hslen = 0;
4136 }
4137 /* Case (4): Application data */
4138 else if( ssl->in_offt != NULL )
4139 {
4140 return( 0 );
4141 }
4142 /* Everything else (CCS & Alerts) */
4143 else
4144 {
4145 ssl->in_msglen = 0;
4146 }
4147
Hanno Becker1097b342018-08-15 14:09:41 +01004148 return( 0 );
4149}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004150
Hanno Beckere74d5562018-08-15 14:26:08 +01004151static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4152{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004153 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004154 return( 1 );
4155
4156 return( 0 );
4157}
4158
Hanno Becker5f066e72018-08-16 14:56:31 +01004159#if defined(MBEDTLS_SSL_PROTO_DTLS)
4160
4161static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4162{
4163 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4164 if( hs == NULL )
4165 return;
4166
Hanno Becker01315ea2018-08-21 17:22:17 +01004167 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004168 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004169 hs->buffering.total_bytes_buffered -=
4170 hs->buffering.future_record.len;
4171
4172 mbedtls_free( hs->buffering.future_record.data );
4173 hs->buffering.future_record.data = NULL;
4174 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004175}
4176
4177static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4178{
4179 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4180 unsigned char * rec;
4181 size_t rec_len;
4182 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004183#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4184 size_t in_buf_len = ssl->in_buf_len;
4185#else
4186 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4187#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004188 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4189 return( 0 );
4190
4191 if( hs == NULL )
4192 return( 0 );
4193
Hanno Becker5f066e72018-08-16 14:56:31 +01004194 rec = hs->buffering.future_record.data;
4195 rec_len = hs->buffering.future_record.len;
4196 rec_epoch = hs->buffering.future_record.epoch;
4197
4198 if( rec == NULL )
4199 return( 0 );
4200
Hanno Becker4cb782d2018-08-20 11:19:05 +01004201 /* Only consider loading future records if the
4202 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004203 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004204 return( 0 );
4205
Hanno Becker5f066e72018-08-16 14:56:31 +01004206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4207
4208 if( rec_epoch != ssl->in_epoch )
4209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4211 goto exit;
4212 }
4213
4214 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4215
4216 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004217 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004218 {
4219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4221 }
4222
4223 memcpy( ssl->in_hdr, rec, rec_len );
4224 ssl->in_left = rec_len;
4225 ssl->next_record_offset = 0;
4226
4227 ssl_free_buffered_record( ssl );
4228
4229exit:
4230 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4231 return( 0 );
4232}
4233
Hanno Becker519f15d2019-07-11 12:43:20 +01004234static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4235 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004236{
4237 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004238
4239 /* Don't buffer future records outside handshakes. */
4240 if( hs == NULL )
4241 return( 0 );
4242
4243 /* Only buffer handshake records (we are only interested
4244 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004245 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004246 return( 0 );
4247
4248 /* Don't buffer more than one future epoch record. */
4249 if( hs->buffering.future_record.data != NULL )
4250 return( 0 );
4251
Hanno Becker01315ea2018-08-21 17:22:17 +01004252 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004253 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004254 hs->buffering.total_bytes_buffered ) )
4255 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4257 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4258 " (already %" MBEDTLS_PRINTF_SIZET
4259 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004260 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004261 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004262 return( 0 );
4263 }
4264
Hanno Becker5f066e72018-08-16 14:56:31 +01004265 /* Buffer record */
4266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004267 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004268 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004269
4270 /* ssl_parse_record_header() only considers records
4271 * of the next epoch as candidates for buffering. */
4272 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004273 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004274
4275 hs->buffering.future_record.data =
4276 mbedtls_calloc( 1, hs->buffering.future_record.len );
4277 if( hs->buffering.future_record.data == NULL )
4278 {
4279 /* If we run out of RAM trying to buffer a
4280 * record from the next epoch, just ignore. */
4281 return( 0 );
4282 }
4283
Hanno Becker519f15d2019-07-11 12:43:20 +01004284 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004285
Hanno Becker519f15d2019-07-11 12:43:20 +01004286 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004287 return( 0 );
4288}
4289
4290#endif /* MBEDTLS_SSL_PROTO_DTLS */
4291
Hanno Beckere74d5562018-08-15 14:26:08 +01004292static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004293{
Janos Follath865b3eb2019-12-16 11:46:15 +00004294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004295 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004296
Hanno Becker5f066e72018-08-16 14:56:31 +01004297#if defined(MBEDTLS_SSL_PROTO_DTLS)
4298 /* We might have buffered a future record; if so,
4299 * and if the epoch matches now, load it.
4300 * On success, this call will set ssl->in_left to
4301 * the length of the buffered record, so that
4302 * the calls to ssl_fetch_input() below will
4303 * essentially be no-ops. */
4304 ret = ssl_load_buffered_record( ssl );
4305 if( ret != 0 )
4306 return( ret );
4307#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004308
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004309 /* Ensure that we have enough space available for the default form
4310 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4311 * with no space for CIDs counted in). */
4312 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4313 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004316 return( ret );
4317 }
4318
Hanno Beckere5e7e782019-07-11 12:29:35 +01004319 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4320 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004323 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004324 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004325 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4326 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004327 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004328 if( ret != 0 )
4329 return( ret );
4330
4331 /* Fall through to handling of unexpected records */
4332 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4333 }
4334
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004335 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4336 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004337#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004338 /* Reset in pointers to default state for TLS/DTLS records,
4339 * assuming no CID and no offset between record content and
4340 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004341 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004342
Hanno Becker7ae20e02019-07-12 08:33:49 +01004343 /* Setup internal message pointers from record structure. */
4344 ssl->in_msgtype = rec.type;
4345#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4346 ssl->in_len = ssl->in_cid + rec.cid_len;
4347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4348 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4349 ssl->in_msglen = rec.data_len;
4350
Hanno Becker2fddd372019-07-10 14:37:41 +01004351 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004352 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004353 if( ret != 0 )
4354 return( ret );
4355#endif
4356
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004357 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004358 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004359
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4361 "(header)" ) );
4362 }
4363 else
4364 {
4365 /* Skip invalid record and the rest of the datagram */
4366 ssl->next_record_offset = 0;
4367 ssl->in_left = 0;
4368
4369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4370 "(header)" ) );
4371 }
4372
4373 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004374 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004375 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004376 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004377#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004378 {
4379 return( ret );
4380 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004381 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004384 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004385 {
Hanno Beckera8814792019-07-10 15:01:45 +01004386 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004387 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004388 if( ssl->next_record_offset < ssl->in_left )
4389 {
4390 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4391 }
4392 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004393 else
4394#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004395 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004396 /*
4397 * Fetch record contents from underlying transport.
4398 */
Hanno Beckera3175662019-07-11 12:50:29 +01004399 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004400 if( ret != 0 )
4401 {
4402 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4403 return( ret );
4404 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004405
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004406 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004407 }
4408
4409 /*
4410 * Decrypt record contents.
4411 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004412
Hanno Beckerfdf66042019-07-11 13:07:45 +01004413 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004416 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004417 {
4418 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004419 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004420 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004421 /* Except when waiting for Finished as a bad mac here
4422 * probably means something went wrong in the handshake
4423 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4424 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4425 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4426 {
4427#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4428 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4429 {
4430 mbedtls_ssl_send_alert_message( ssl,
4431 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4432 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4433 }
4434#endif
4435 return( ret );
4436 }
4437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004438#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004439 if( ssl->conf->badmac_limit != 0 &&
4440 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4443 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004444 }
4445#endif
4446
Hanno Becker4a810fb2017-05-24 16:27:30 +01004447 /* As above, invalid records cause
4448 * dismissal of the whole datagram. */
4449
4450 ssl->next_record_offset = 0;
4451 ssl->in_left = 0;
4452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004454 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004455 }
4456
4457 return( ret );
4458 }
4459 else
4460#endif
4461 {
4462 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4464 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 mbedtls_ssl_send_alert_message( ssl,
4467 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4468 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004469 }
4470#endif
4471 return( ret );
4472 }
4473 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004474
Hanno Becker44d89b22019-07-12 09:40:44 +01004475
4476 /* Reset in pointers to default state for TLS/DTLS records,
4477 * assuming no CID and no offset between record content and
4478 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004479 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004480#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4481 ssl->in_len = ssl->in_cid + rec.cid_len;
4482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004483 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004484
Hanno Becker8685c822019-07-12 09:37:30 +01004485 /* The record content type may change during decryption,
4486 * so re-read it. */
4487 ssl->in_msgtype = rec.type;
4488 /* Also update the input buffer, because unfortunately
4489 * the server-side ssl_parse_client_hello() reparses the
4490 * record header when receiving a ClientHello initiating
4491 * a renegotiation. */
4492 ssl->in_hdr[0] = rec.type;
4493 ssl->in_msg = rec.buf + rec.data_offset;
4494 ssl->in_msglen = rec.data_len;
4495 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4496 ssl->in_len[1] = (unsigned char)( rec.data_len );
4497
Simon Butcher99000142016-10-13 17:21:01 +01004498 return( 0 );
4499}
4500
4501int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4502{
Janos Follath865b3eb2019-12-16 11:46:15 +00004503 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004504
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004505 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004506 * Handle particular types of records
4507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 {
Simon Butcher99000142016-10-13 17:21:01 +01004510 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4511 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004512 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004514 }
4515
Hanno Beckere678eaa2018-08-21 14:57:46 +01004516 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004517 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004518 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004519 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004521 ssl->in_msglen ) );
4522 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004523 }
4524
Hanno Beckere678eaa2018-08-21 14:57:46 +01004525 if( ssl->in_msg[0] != 1 )
4526 {
4527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4528 ssl->in_msg[0] ) );
4529 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4530 }
4531
4532#if defined(MBEDTLS_SSL_PROTO_DTLS)
4533 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4534 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4535 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4536 {
4537 if( ssl->handshake == NULL )
4538 {
4539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4540 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4541 }
4542
4543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4544 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4545 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004546#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004547 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004549 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004550 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004551 if( ssl->in_msglen != 2 )
4552 {
4553 /* Note: Standard allows for more than one 2 byte alert
4554 to be packed in a single message, but Mbed TLS doesn't
4555 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004557 ssl->in_msglen ) );
4558 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4559 }
4560
Paul Elliott9f352112020-12-09 14:55:45 +00004561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004562 ssl->in_msg[0], ssl->in_msg[1] ) );
4563
4564 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004565 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004567 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004570 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004572 }
4573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004574 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4575 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4578 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004580
4581#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4582 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4583 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4584 {
Mateusz Starzykf5c53512021-04-15 13:28:52 +02004585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004586 /* Will be handled when trying to parse ServerHello */
4587 return( 0 );
4588 }
4589#endif
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004590 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01004591 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 }
4593
Hanno Beckerc76c6192017-06-06 10:03:17 +01004594#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01004595 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004596 {
Hanno Becker37ae9522019-05-03 16:54:26 +01004597 /* Drop unexpected ApplicationData records,
4598 * except at the beginning of renegotiations */
4599 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4600 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4601#if defined(MBEDTLS_SSL_RENEGOTIATION)
4602 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4603 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01004604#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01004605 )
4606 {
4607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4608 return( MBEDTLS_ERR_SSL_NON_FATAL );
4609 }
4610
4611 if( ssl->handshake != NULL &&
4612 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4613 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004614 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01004615 }
4616 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01004617#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01004618
Paul Bakker5121ce52009-01-03 21:22:43 +00004619 return( 0 );
4620}
4621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004622int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004623{
irwir6c0da642019-09-26 21:07:41 +03004624 return( mbedtls_ssl_send_alert_message( ssl,
4625 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4626 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004627}
4628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004629int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00004630 unsigned char level,
4631 unsigned char message )
4632{
Janos Follath865b3eb2019-12-16 11:46:15 +00004633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00004634
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004635 if( ssl == NULL || ssl->conf == NULL )
4636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004639 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00004640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004641 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00004642 ssl->out_msglen = 2;
4643 ssl->out_msg[0] = level;
4644 ssl->out_msg[1] = message;
4645
Hanno Becker67bc7c32018-08-06 11:33:50 +01004646 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00004647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00004649 return( ret );
4650 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00004652
4653 return( 0 );
4654}
4655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004656int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004657{
Janos Follath865b3eb2019-12-16 11:46:15 +00004658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 ssl->out_msglen = 1;
4664 ssl->out_msg[0] = 1;
4665
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 ssl->state++;
4667
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004668 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004671 return( ret );
4672 }
4673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004675
4676 return( 0 );
4677}
4678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004679int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004680{
Janos Follath865b3eb2019-12-16 11:46:15 +00004681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00004682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004683 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004684
Hanno Becker327c93b2018-08-15 13:56:18 +01004685 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004688 return( ret );
4689 }
4690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004694 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4695 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004696 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004697 }
4698
Hanno Beckere678eaa2018-08-21 14:57:46 +01004699 /* CCS records are only accepted if they have length 1 and content '1',
4700 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00004701
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004702 /*
4703 * Switch to our negotiated transform and session parameters for inbound
4704 * data.
4705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004707 ssl->transform_in = ssl->transform_negotiate;
4708 ssl->session_in = ssl->session_negotiate;
4709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004713#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00004714 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004715#endif
4716
4717 /* Increment epoch */
4718 if( ++ssl->in_epoch == 0 )
4719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02004721 /* This is highly unlikely to happen for legitimate reasons, so
4722 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004724 }
4725 }
4726 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004728 memset( ssl->in_ctr, 0, 8 );
4729
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004730 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004731
Paul Bakker5121ce52009-01-03 21:22:43 +00004732 ssl->state++;
4733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004735
4736 return( 0 );
4737}
4738
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004739/* Once ssl->out_hdr as the address of the beginning of the
4740 * next outgoing record is set, deduce the other pointers.
4741 *
4742 * Note: For TLS, we save the implicit record sequence number
4743 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
4744 * and the caller has to make sure there's space for this.
4745 */
4746
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004747static size_t ssl_transform_get_explicit_iv_len(
4748 mbedtls_ssl_transform const *transform )
4749{
TRodziewiczef73f012021-05-13 14:53:36 +02004750 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004751 return( 0 );
4752
4753 return( transform->ivlen - transform->fixed_ivlen );
4754}
4755
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004756void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
4757 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004758{
4759#if defined(MBEDTLS_SSL_PROTO_DTLS)
4760 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4761 {
4762 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004763#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004764 ssl->out_cid = ssl->out_ctr + 8;
4765 ssl->out_len = ssl->out_cid;
4766 if( transform != NULL )
4767 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004768#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004769 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004770#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004771 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004772 }
4773 else
4774#endif
4775 {
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004776 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004777#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004778 ssl->out_cid = ssl->out_len;
4779#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004780 ssl->out_iv = ssl->out_hdr + 5;
4781 }
4782
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004783 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004784 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01004785 if( transform != NULL )
4786 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004787}
4788
4789/* Once ssl->in_hdr as the address of the beginning of the
4790 * next incoming record is set, deduce the other pointers.
4791 *
4792 * Note: For TLS, we save the implicit record sequence number
4793 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
4794 * and the caller has to make sure there's space for this.
4795 */
4796
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004797void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004798{
Hanno Becker79594fd2019-05-08 09:38:41 +01004799 /* This function sets the pointers to match the case
4800 * of unprotected TLS/DTLS records, with both ssl->in_iv
4801 * and ssl->in_msg pointing to the beginning of the record
4802 * content.
4803 *
4804 * When decrypting a protected record, ssl->in_msg
4805 * will be shifted to point to the beginning of the
4806 * record plaintext.
4807 */
4808
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004809#if defined(MBEDTLS_SSL_PROTO_DTLS)
4810 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4811 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004812 /* This sets the header pointers to match records
4813 * without CID. When we receive a record containing
4814 * a CID, the fields are shifted accordingly in
4815 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004816 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004817#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004818 ssl->in_cid = ssl->in_ctr + 8;
4819 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004820#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004821 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004822#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004823 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004824 }
4825 else
4826#endif
4827 {
4828 ssl->in_ctr = ssl->in_hdr - 8;
4829 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004830#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01004831 ssl->in_cid = ssl->in_len;
4832#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004833 ssl->in_iv = ssl->in_hdr + 5;
4834 }
4835
Hanno Becker79594fd2019-05-08 09:38:41 +01004836 /* This will be adjusted at record decryption time. */
4837 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01004838}
4839
Paul Bakker5121ce52009-01-03 21:22:43 +00004840/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02004841 * Setup an SSL context
4842 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004843
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004844void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004845{
4846 /* Set the incoming and outgoing record pointers. */
4847#if defined(MBEDTLS_SSL_PROTO_DTLS)
4848 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4849 {
4850 ssl->out_hdr = ssl->out_buf;
4851 ssl->in_hdr = ssl->in_buf;
4852 }
4853 else
4854#endif /* MBEDTLS_SSL_PROTO_DTLS */
4855 {
Hanno Becker12078f42021-03-02 15:28:41 +00004856 ssl->out_ctr = ssl->out_buf;
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004857 ssl->out_hdr = ssl->out_buf + 8;
4858 ssl->in_hdr = ssl->in_buf + 8;
4859 }
4860
4861 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004862 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
4863 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01004864}
4865
Paul Bakker5121ce52009-01-03 21:22:43 +00004866/*
4867 * SSL get accessors
4868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004869size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004870{
4871 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4872}
4873
Hanno Becker8b170a02017-10-10 11:51:19 +01004874int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
4875{
4876 /*
4877 * Case A: We're currently holding back
4878 * a message for further processing.
4879 */
4880
4881 if( ssl->keep_current_message == 1 )
4882 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004883 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004884 return( 1 );
4885 }
4886
4887 /*
4888 * Case B: Further records are pending in the current datagram.
4889 */
4890
4891#if defined(MBEDTLS_SSL_PROTO_DTLS)
4892 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4893 ssl->in_left > ssl->next_record_offset )
4894 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004896 return( 1 );
4897 }
4898#endif /* MBEDTLS_SSL_PROTO_DTLS */
4899
4900 /*
4901 * Case C: A handshake message is being processed.
4902 */
4903
Hanno Becker8b170a02017-10-10 11:51:19 +01004904 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
4905 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004907 return( 1 );
4908 }
4909
4910 /*
4911 * Case D: An application data message is being processed
4912 */
4913 if( ssl->in_offt != NULL )
4914 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01004915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01004916 return( 1 );
4917 }
4918
4919 /*
4920 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01004921 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01004922 * we implement support for multiple alerts in single records.
4923 */
4924
4925 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
4926 return( 0 );
4927}
4928
Paul Bakker43ca69c2011-01-15 17:35:19 +00004929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004931{
Hanno Becker3136ede2018-08-17 15:28:19 +01004932 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01004934 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004935
Hanno Becker5903de42019-05-03 14:46:38 +01004936 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
4937
Hanno Becker78640902018-08-13 16:35:15 +01004938 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01004939 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01004940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004941 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004943 case MBEDTLS_MODE_GCM:
4944 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004945 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004946 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004947 transform_expansion = transform->minlen;
4948 break;
4949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004950 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01004951
4952 block_size = mbedtls_cipher_get_block_size(
4953 &transform->cipher_ctx_enc );
4954
Hanno Becker3136ede2018-08-17 15:28:19 +01004955 /* Expansion due to the addition of the MAC. */
4956 transform_expansion += transform->maclen;
4957
4958 /* Expansion due to the addition of CBC padding;
4959 * Theoretically up to 256 bytes, but we never use
4960 * more than the block size of the underlying cipher. */
4961 transform_expansion += block_size;
4962
4963 /* For TLS 1.1 or higher, an explicit IV is added
4964 * after the record header. */
TRodziewicz0f82ec62021-05-12 17:49:18 +02004965#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4966 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Becker3136ede2018-08-17 15:28:19 +01004967 transform_expansion += block_size;
TRodziewicz0f82ec62021-05-12 17:49:18 +02004968#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01004969
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004970 break;
4971
4972 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02004973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004975 }
4976
Hanno Beckera0e20d02019-05-15 14:03:01 +01004977#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01004978 if( transform->out_cid_len != 0 )
4979 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004980#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01004981
Hanno Becker5903de42019-05-03 14:46:38 +01004982 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02004983}
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004986/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004987 * Check record counters and renegotiate if they're above the limit.
4988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004989static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004990{
Hanno Beckerdd772292020-02-05 10:38:31 +00004991 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00004992 int in_ctr_cmp;
4993 int out_ctr_cmp;
4994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004995 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
4996 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004997 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004998 {
4999 return( 0 );
5000 }
5001
Andres AG2196c7f2016-12-15 17:01:16 +00005002 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5003 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005004 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005005 ssl->conf->renego_period + ep_len, 8 - ep_len );
5006
5007 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005008 {
5009 return( 0 );
5010 }
5011
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005013 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005014}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005016
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005017/* This function is called from mbedtls_ssl_read() when a handshake message is
Hanno Beckerf26cc722021-04-21 07:30:13 +01005018 * received after the initial handshake. In this context, handshake messages
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005019 * may only be sent for the purpose of initiating renegotiations.
5020 *
5021 * This function is introduced as a separate helper since the handling
5022 * of post-handshake handshake messages changes significantly in TLS 1.3,
5023 * and having a helper function allows to distinguish between TLS <= 1.2 and
5024 * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read().
5025 */
Hanno Beckercad3dba2020-11-24 06:57:13 +00005026static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl )
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005027{
Hanno Beckerfae12cf2021-04-21 07:20:20 +01005028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005029
5030 /*
5031 * - For client-side, expect SERVER_HELLO_REQUEST.
5032 * - For server-side, expect CLIENT_HELLO.
5033 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5034 */
5035
5036#if defined(MBEDTLS_SSL_CLI_C)
5037 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
5038 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
5039 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
5040 {
5041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
5042
5043 /* With DTLS, drop the packet (probably from last handshake) */
5044#if defined(MBEDTLS_SSL_PROTO_DTLS)
5045 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5046 {
5047 return( 0 );
5048 }
5049#endif
5050 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5051 }
5052#endif /* MBEDTLS_SSL_CLI_C */
5053
5054#if defined(MBEDTLS_SSL_SRV_C)
5055 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5056 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
5057 {
5058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5059
5060 /* With DTLS, drop the packet (probably from last handshake) */
5061#if defined(MBEDTLS_SSL_PROTO_DTLS)
5062 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5063 {
5064 return( 0 );
5065 }
5066#endif
5067 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
5068 }
5069#endif /* MBEDTLS_SSL_SRV_C */
5070
5071#if defined(MBEDTLS_SSL_RENEGOTIATION)
5072 /* Determine whether renegotiation attempt should be accepted */
5073 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5074 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5075 ssl->conf->allow_legacy_renegotiation ==
5076 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5077 {
5078 /*
5079 * Accept renegotiation request
5080 */
5081
5082 /* DTLS clients need to know renego is server-initiated */
5083#if defined(MBEDTLS_SSL_PROTO_DTLS)
5084 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5085 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5086 {
5087 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5088 }
5089#endif
5090 ret = mbedtls_ssl_start_renegotiation( ssl );
5091 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5092 ret != 0 )
5093 {
5094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5095 ret );
5096 return( ret );
5097 }
5098 }
5099 else
5100#endif /* MBEDTLS_SSL_RENEGOTIATION */
5101 {
5102 /*
5103 * Refuse renegotiation
5104 */
5105
5106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
5107
TRodziewicz0f82ec62021-05-12 17:49:18 +02005108#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005109 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
5110 {
5111 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5112 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5113 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5114 {
5115 return( ret );
5116 }
5117 }
5118 else
TRodziewicz0f82ec62021-05-12 17:49:18 +02005119#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005120 {
5121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5123 }
5124 }
5125
5126 return( 0 );
5127}
5128
Paul Bakker48916f92012-09-16 19:57:18 +00005129/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005130 * Receive application data decrypted from the SSL layer
5131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005132int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005133{
Janos Follath865b3eb2019-12-16 11:46:15 +00005134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005135 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005136
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005137 if( ssl == NULL || ssl->conf == NULL )
5138 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005145 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005146 return( ret );
5147
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005148 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005150 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005151 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005152 return( ret );
5153 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005154 }
5155#endif
5156
Hanno Becker4a810fb2017-05-24 16:27:30 +01005157 /*
5158 * Check if renegotiation is necessary and/or handshake is
5159 * in process. If yes, perform/continue, and fall through
5160 * if an unexpected packet is received while the client
5161 * is waiting for the ServerHello.
5162 *
5163 * (There is no equivalent to the last condition on
5164 * the server-side as it is not treated as within
5165 * a handshake while waiting for the ClientHello
5166 * after a renegotiation request.)
5167 */
5168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005170 ret = ssl_check_ctr_renegotiate( ssl );
5171 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5172 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005175 return( ret );
5176 }
5177#endif
5178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005179 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005181 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005182 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5183 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005186 return( ret );
5187 }
5188 }
5189
Hanno Beckere41158b2017-10-23 13:30:32 +01005190 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005191 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005192 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005193 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005194 if( ssl->f_get_timer != NULL &&
5195 ssl->f_get_timer( ssl->p_timer ) == -1 )
5196 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005197 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005198 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005199
Hanno Becker327c93b2018-08-15 13:56:18 +01005200 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005201 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005202 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5203 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005204
Hanno Becker4a810fb2017-05-24 16:27:30 +01005205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5206 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005207 }
5208
5209 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005211 {
5212 /*
5213 * OpenSSL sends empty messages to randomize the IV
5214 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005215 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005218 return( 0 );
5219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005221 return( ret );
5222 }
5223 }
5224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005226 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005227 ret = ssl_handle_hs_message_post_handshake( ssl );
5228 if( ret != 0)
Paul Bakker48916f92012-09-16 19:57:18 +00005229 {
Hanno Beckerb03f88f2020-11-24 06:41:37 +00005230 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake",
5231 ret );
5232 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005233 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005234
Hanno Beckerf26cc722021-04-21 07:30:13 +01005235 /* At this point, we don't know whether the renegotiation triggered
5236 * by the post-handshake message has been completed or not. The cases
5237 * to consider are the following:
Hanno Becker90333da2017-10-10 11:27:13 +01005238 * 1) The renegotiation is complete. In this case, no new record
5239 * has been read yet.
5240 * 2) The renegotiation is incomplete because the client received
5241 * an application data record while awaiting the ServerHello.
5242 * 3) The renegotiation is incomplete because the client received
5243 * a non-handshake, non-application data message while awaiting
5244 * the ServerHello.
Hanno Beckerf26cc722021-04-21 07:30:13 +01005245 *
5246 * In each of these cases, looping will be the proper action:
Hanno Becker90333da2017-10-10 11:27:13 +01005247 * - For 1), the next iteration will read a new record and check
5248 * if it's application data.
5249 * - For 2), the loop condition isn't satisfied as application data
5250 * is present, hence continue is the same as break
5251 * - For 3), the loop condition is satisfied and read_record
5252 * will re-deliver the message that was held back by the client
5253 * when expecting the ServerHello.
5254 */
Hanno Beckerf26cc722021-04-21 07:30:13 +01005255
Hanno Becker90333da2017-10-10 11:27:13 +01005256 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005257 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005258#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005260 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005261 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005262 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005263 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005265 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005266 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005267 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005268 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005269 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005270 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005271#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005273 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5274 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005277 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005278 }
5279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005280 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5283 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005284 }
5285
5286 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005287
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005288 /* We're going to return something now, cancel timer,
5289 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005290 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005291 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005292
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005294 /* If we requested renego but received AppData, resend HelloRequest.
5295 * Do it now, after setting in_offt, to avoid taking this branch
5296 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005297#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005298 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005300 {
Hanno Becker786300f2020-02-05 10:46:40 +00005301 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005302 {
Hanno Becker786300f2020-02-05 10:46:40 +00005303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5304 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005305 return( ret );
5306 }
5307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005309#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005310 }
5311
5312 n = ( len < ssl->in_msglen )
5313 ? len : ssl->in_msglen;
5314
5315 memcpy( buf, ssl->in_offt, n );
5316 ssl->in_msglen -= n;
5317
gabor-mezei-arma3214132020-07-15 10:55:00 +02005318 /* Zeroising the plaintext buffer to erase unused application data
5319 from the memory. */
5320 mbedtls_platform_zeroize( ssl->in_offt, n );
5321
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005323 {
5324 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005325 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005326 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005327 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005328 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005329 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005330 /* more data available */
5331 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005335
Paul Bakker23986e52011-04-24 08:57:21 +00005336 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005337}
5338
5339/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005340 * Send application data to be encrypted by the SSL layer, taking care of max
5341 * fragment length and buffer size.
5342 *
5343 * According to RFC 5246 Section 6.2.1:
5344 *
5345 * Zero-length fragments of Application data MAY be sent as they are
5346 * potentially useful as a traffic analysis countermeasure.
5347 *
5348 * Therefore, it is possible that the input message length is 0 and the
5349 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005350 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005351static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005352 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005353{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005354 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5355 const size_t max_len = (size_t) ret;
5356
5357 if( ret < 0 )
5358 {
5359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5360 return( ret );
5361 }
5362
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005363 if( len > max_len )
5364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005366 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005369 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5370 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005371 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005373 }
5374 else
5375#endif
5376 len = max_len;
5377 }
Paul Bakker887bd502011-06-08 13:10:54 +00005378
Paul Bakker5121ce52009-01-03 21:22:43 +00005379 if( ssl->out_left != 0 )
5380 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005381 /*
5382 * The user has previously tried to send the data and
5383 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5384 * written. In this case, we expect the high-level write function
5385 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005389 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005390 return( ret );
5391 }
5392 }
Paul Bakker887bd502011-06-08 13:10:54 +00005393 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005394 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005395 /*
5396 * The user is trying to send a message the first time, so we need to
5397 * copy the data into the internal buffers and setup the data structure
5398 * to keep track of partial writes
5399 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005400 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005401 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005402 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005403
Hanno Becker67bc7c32018-08-06 11:33:50 +01005404 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005407 return( ret );
5408 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005409 }
5410
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005411 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005412}
5413
5414/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005415 * Write application data (public-facing wrapper)
5416 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005417int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005418{
Janos Follath865b3eb2019-12-16 11:46:15 +00005419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005420
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005422
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005423 if( ssl == NULL || ssl->conf == NULL )
5424 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5425
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005426#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005427 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5428 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005429 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005430 return( ret );
5431 }
5432#endif
5433
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005434 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005435 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005436 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005437 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005439 return( ret );
5440 }
5441 }
5442
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005443 ret = ssl_write_real( ssl, buf, len );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005444
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005446
5447 return( ret );
5448}
5449
5450/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005451 * Notify the peer that the connection is being closed
5452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005453int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005454{
Janos Follath865b3eb2019-12-16 11:46:15 +00005455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005456
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005457 if( ssl == NULL || ssl->conf == NULL )
5458 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005461
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005462 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5468 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5469 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005472 return( ret );
5473 }
5474 }
5475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005477
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005478 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005479}
5480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005482{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005483 if( transform == NULL )
5484 return;
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5487 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005488
Hanno Beckerfd86ca82020-11-30 08:54:23 +00005489#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 mbedtls_md_free( &transform->md_ctx_enc );
5491 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005492#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005493
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005494 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005495}
5496
Hanno Becker0271f962018-08-16 13:23:47 +01005497#if defined(MBEDTLS_SSL_PROTO_DTLS)
5498
Hanno Becker533ab5f2020-02-05 10:49:13 +00005499void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005500{
5501 unsigned offset;
5502 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5503
5504 if( hs == NULL )
5505 return;
5506
Hanno Becker283f5ef2018-08-24 09:34:47 +01005507 ssl_free_buffered_record( ssl );
5508
Hanno Becker0271f962018-08-16 13:23:47 +01005509 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005510 ssl_buffering_free_slot( ssl, offset );
5511}
5512
5513static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5514 uint8_t slot )
5515{
5516 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5517 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005518
5519 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5520 return;
5521
Hanno Beckere605b192018-08-21 15:59:07 +01005522 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01005523 {
Hanno Beckere605b192018-08-21 15:59:07 +01005524 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01005525 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01005526 mbedtls_free( hs_buf->data );
5527 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01005528 }
5529}
5530
5531#endif /* MBEDTLS_SSL_PROTO_DTLS */
5532
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005533/*
5534 * Convert version numbers to/from wire format
5535 * and, for DTLS, to/from TLS equivalent.
5536 *
5537 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08005538 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005539 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5540 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005542void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005543 unsigned char ver[2] )
5544{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545#if defined(MBEDTLS_SSL_PROTO_DTLS)
5546 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005548 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005549 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5550
5551 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5552 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5553 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005554 else
5555#else
5556 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005557#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005558 {
5559 ver[0] = (unsigned char) major;
5560 ver[1] = (unsigned char) minor;
5561 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005562}
5563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005565 const unsigned char ver[2] )
5566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567#if defined(MBEDTLS_SSL_PROTO_DTLS)
5568 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005569 {
5570 *major = 255 - ver[0] + 2;
5571 *minor = 255 - ver[1] + 1;
5572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005574 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5575 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005576 else
5577#else
5578 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005579#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005580 {
5581 *major = ver[0];
5582 *minor = ver[1];
5583 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005584}
5585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005586#endif /* MBEDTLS_SSL_TLS_C */