blob: 54a7be011a621e9ad70869fac17ebc63b7ec4086 [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/*
21 * The SSL 3.0 specification was drafted by Netscape in 1996,
22 * and became an IETF standard in 1999.
23 *
24 * http://wp.netscape.com/eng/ssl3/
25 * http://www.ietf.org/rfc/rfc2246.txt
26 * http://www.ietf.org/rfc/rfc4346.txt
27 */
28
Gilles Peskinedb09ef62020-06-03 01:43:33 +020029#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000032
SimonBd5800b72016-04-26 07:43:27 +010033#if defined(MBEDTLS_PLATFORM_C)
34#include "mbedtls/platform.h"
35#else
36#include <stdlib.h>
37#define mbedtls_calloc calloc
38#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010039#endif
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020042#include "mbedtls/ssl_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000043#include "mbedtls/debug.h"
44#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010046#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020047
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020048#include "ssl_invasive.h"
49
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#include "mbedtls/psa_util.h"
54#include "psa/crypto.h"
55#endif
56
Janos Follath23bdca02016-10-07 14:47:14 +010057#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020059#endif
60
Hanno Beckercd9dcda2018-08-28 17:18:56 +010061static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020063/*
64 * Start a timer.
65 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020066 */
Hanno Becker0f57a652020-02-05 10:37:26 +000067void mbedtls_ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020068{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020069 if( ssl->f_set_timer == NULL )
70 return;
71
72 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
73 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020074}
75
76/*
77 * Return -1 is timer is expired, 0 if it isn't.
78 */
Hanno Becker7876d122020-02-05 10:39:31 +000079int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020081 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020082 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083
84 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020085 {
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020088 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089
90 return( 0 );
91}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092
Hanno Beckercfe45792019-07-03 16:13:00 +010093#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +010094static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
95 unsigned char *buf,
96 size_t len,
97 mbedtls_record *rec );
98
Hanno Beckercfe45792019-07-03 16:13:00 +010099int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
100 unsigned char *buf,
101 size_t buflen )
102{
Hanno Becker54229812019-07-12 14:40:00 +0100103 int ret = 0;
Hanno Becker54229812019-07-12 14:40:00 +0100104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
105 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
106
107 /* We don't support record checking in TLS because
108 * (a) there doesn't seem to be a usecase for it, and
109 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
110 * and we'd need to backup the transform here.
111 */
112 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
113 {
114 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
115 goto exit;
116 }
117#if defined(MBEDTLS_SSL_PROTO_DTLS)
118 else
119 {
irwir734f0cf2019-09-26 21:03:24 +0300120 mbedtls_record rec;
121
Hanno Becker54229812019-07-12 14:40:00 +0100122 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
123 if( ret != 0 )
124 {
125 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
126 goto exit;
127 }
128
129 if( ssl->transform_in != NULL )
130 {
131 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
132 if( ret != 0 )
133 {
134 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
135 goto exit;
136 }
137 }
138 }
139#endif /* MBEDTLS_SSL_PROTO_DTLS */
140
141exit:
142 /* On success, we have decrypted the buffer in-place, so make
143 * sure we don't leak any plaintext data. */
144 mbedtls_platform_zeroize( buf, buflen );
145
146 /* For the purpose of this API, treat messages with unexpected CID
147 * as well as such from future epochs as unexpected. */
148 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
149 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
150 {
151 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
152 }
153
154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
155 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100156}
157#endif /* MBEDTLS_SSL_RECORD_CHECKING */
158
Hanno Becker67bc7c32018-08-06 11:33:50 +0100159#define SSL_DONT_FORCE_FLUSH 0
160#define SSL_FORCE_FLUSH 1
161
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200162#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100163
Hanno Beckerd5847772018-08-28 10:09:23 +0100164/* Forward declarations for functions related to message buffering. */
Hanno Beckerd5847772018-08-28 10:09:23 +0100165static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
166 uint8_t slot );
167static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
168static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
169static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
170static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100171static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
172 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100173static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100174
Hanno Becker11682cc2018-08-22 14:41:02 +0100175static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100176{
Hanno Becker89490712020-02-05 10:50:12 +0000177 size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
Darryl Greenb33cc762019-11-28 14:29:44 +0000178#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
179 size_t out_buf_len = ssl->out_buf_len;
180#else
181 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
182#endif
Hanno Becker2b1e3542018-08-06 11:19:13 +0100183
Darryl Greenb33cc762019-11-28 14:29:44 +0000184 if( mtu != 0 && mtu < out_buf_len )
Hanno Becker11682cc2018-08-22 14:41:02 +0100185 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100186
Darryl Greenb33cc762019-11-28 14:29:44 +0000187 return( out_buf_len );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100188}
189
Hanno Becker67bc7c32018-08-06 11:33:50 +0100190static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
191{
Hanno Becker11682cc2018-08-22 14:41:02 +0100192 size_t const bytes_written = ssl->out_left;
193 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100194
195 /* Double-check that the write-index hasn't gone
196 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100197 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100198 {
199 /* Should never happen... */
200 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
201 }
202
203 return( (int) ( mtu - bytes_written ) );
204}
205
206static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
207{
Janos Follath865b3eb2019-12-16 11:46:15 +0000208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100209 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400210 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100211
212#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Andrzej Kurek90c6e842020-04-03 05:25:29 -0400213 const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 if( max_len > mfl )
216 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100217
218 /* By the standard (RFC 6066 Sect. 4), the MFL extension
219 * only limits the maximum record payload size, so in theory
220 * we would be allowed to pack multiple records of payload size
221 * MFL into a single datagram. However, this would mean that there's
222 * no way to explicitly communicate MTU restrictions to the peer.
223 *
224 * The following reduction of max_len makes sure that we never
225 * write datagrams larger than MFL + Record Expansion Overhead.
226 */
227 if( max_len <= ssl->out_left )
228 return( 0 );
229
230 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231#endif
232
233 ret = ssl_get_remaining_space_in_datagram( ssl );
234 if( ret < 0 )
235 return( ret );
236 remaining = (size_t) ret;
237
238 ret = mbedtls_ssl_get_record_expansion( ssl );
239 if( ret < 0 )
240 return( ret );
241 expansion = (size_t) ret;
242
243 if( remaining <= expansion )
244 return( 0 );
245
246 remaining -= expansion;
247 if( remaining >= max_len )
248 remaining = max_len;
249
250 return( (int) remaining );
251}
252
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200253/*
254 * Double the retransmit timeout value, within the allowed range,
255 * returning -1 if the maximum value has already been reached.
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200258{
259 uint32_t new_timeout;
260
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200261 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200262 return( -1 );
263
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200264 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
265 * in the following way: after the initial transmission and a first
266 * retransmission, back off to a temporary estimated MTU of 508 bytes.
267 * This value is guaranteed to be deliverable (if not guaranteed to be
268 * delivered) of any compliant IPv4 (and IPv6) network, and should work
269 * on most non-IP stacks too. */
270 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400271 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200272 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
274 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200275
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200276 new_timeout = 2 * ssl->handshake->retransmit_timeout;
277
278 /* Avoid arithmetic overflow and range overflow */
279 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200280 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200281 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200282 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200283 }
284
285 ssl->handshake->retransmit_timeout = new_timeout;
Paul Elliott9f352112020-12-09 14:55:45 +0000286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
287 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200288
289 return( 0 );
290}
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200293{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200294 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Paul Elliott9f352112020-12-09 14:55:45 +0000295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs",
296 (unsigned long) ssl->handshake->retransmit_timeout ) );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
301int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200302 const unsigned char *key_enc, const unsigned char *key_dec,
303 size_t keylen,
304 const unsigned char *iv_enc, const unsigned char *iv_dec,
305 size_t ivlen,
306 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200307 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
309int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
310int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
311int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
312int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
313#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000314
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100315/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +0200317 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000318
Hanno Beckerccc13d02020-05-04 12:30:04 +0100319#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \
320 defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
Hanno Becker13996922020-05-28 16:15:19 +0100321
322static size_t ssl_compute_padding_length( size_t len,
323 size_t granularity )
324{
325 return( ( granularity - ( len + 1 ) % granularity ) % granularity );
326}
327
Hanno Becker581bc1b2020-05-04 12:20:03 +0100328/* This functions transforms a (D)TLS plaintext fragment and a record content
329 * type into an instance of the (D)TLSInnerPlaintext structure. This is used
330 * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect
331 * a record's content type.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100332 *
333 * struct {
334 * opaque content[DTLSPlaintext.length];
335 * ContentType real_type;
336 * uint8 zeros[length_of_padding];
Hanno Becker581bc1b2020-05-04 12:20:03 +0100337 * } (D)TLSInnerPlaintext;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100338 *
339 * Input:
340 * - `content`: The beginning of the buffer holding the
341 * plaintext to be wrapped.
342 * - `*content_size`: The length of the plaintext in Bytes.
343 * - `max_len`: The number of Bytes available starting from
344 * `content`. This must be `>= *content_size`.
345 * - `rec_type`: The desired record content type.
346 *
347 * Output:
Hanno Becker581bc1b2020-05-04 12:20:03 +0100348 * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure.
349 * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100350 *
351 * Returns:
352 * - `0` on success.
353 * - A negative error code if `max_len` didn't offer enough space
354 * for the expansion.
355 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100356static int ssl_build_inner_plaintext( unsigned char *content,
357 size_t *content_size,
358 size_t remaining,
Hanno Becker13996922020-05-28 16:15:19 +0100359 uint8_t rec_type,
360 size_t pad )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100361{
362 size_t len = *content_size;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100363
364 /* Write real content type */
365 if( remaining == 0 )
366 return( -1 );
367 content[ len ] = rec_type;
368 len++;
369 remaining--;
370
371 if( remaining < pad )
372 return( -1 );
373 memset( content + len, 0, pad );
374 len += pad;
375 remaining -= pad;
376
377 *content_size = len;
378 return( 0 );
379}
380
Hanno Becker581bc1b2020-05-04 12:20:03 +0100381/* This function parses a (D)TLSInnerPlaintext structure.
382 * See ssl_build_inner_plaintext() for details. */
383static int ssl_parse_inner_plaintext( unsigned char const *content,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100384 size_t *content_size,
385 uint8_t *rec_type )
386{
387 size_t remaining = *content_size;
388
389 /* Determine length of padding by skipping zeroes from the back. */
390 do
391 {
392 if( remaining == 0 )
393 return( -1 );
394 remaining--;
395 } while( content[ remaining ] == 0 );
396
397 *content_size = remaining;
398 *rec_type = content[ remaining ];
399
400 return( 0 );
401}
Hanno Beckerccc13d02020-05-04 12:30:04 +0100402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID ||
403 MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100404
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100405/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +0100406 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000407static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100408 size_t *add_data_len,
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100409 mbedtls_record *rec,
410 unsigned minor_ver )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000411{
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100412 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +0100413 *
414 * additional_data = seq_num + TLSCompressed.type +
415 * TLSCompressed.version + TLSCompressed.length;
416 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100417 * For the CID extension, this is extended as follows
418 * (quoting draft-ietf-tls-dtls-connection-id-05,
419 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +0100420 *
421 * additional_data = seq_num + DTLSPlaintext.type +
422 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +0100423 * cid +
424 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +0100425 * length_of_DTLSInnerPlaintext;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100426 *
427 * For TLS 1.3, the record sequence number is dropped from the AAD
428 * and encoded within the nonce of the AEAD operation instead.
Hanno Beckercab87e62019-04-29 13:52:53 +0100429 */
430
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100431 unsigned char *cur = add_data;
432
433#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
434 if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 )
435#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
436 {
437 ((void) minor_ver);
438 memcpy( cur, rec->ctr, sizeof( rec->ctr ) );
439 cur += sizeof( rec->ctr );
440 }
441
442 *cur = rec->type;
443 cur++;
444
445 memcpy( cur, rec->ver, sizeof( rec->ver ) );
446 cur += sizeof( rec->ver );
Hanno Beckercab87e62019-04-29 13:52:53 +0100447
Hanno Beckera0e20d02019-05-15 14:03:01 +0100448#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100449 if( rec->cid_len != 0 )
450 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100451 memcpy( cur, rec->cid, rec->cid_len );
452 cur += rec->cid_len;
453
454 *cur = rec->cid_len;
455 cur++;
456
457 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
458 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
459 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100460 }
461 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100463 {
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100464 cur[0] = ( rec->data_len >> 8 ) & 0xFF;
465 cur[1] = ( rec->data_len >> 0 ) & 0xFF;
466 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100467 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100468
469 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000470}
471
Hanno Becker9d062f92020-02-07 10:26:36 +0000472#if defined(MBEDTLS_SSL_PROTO_SSL3)
473
474#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
475
476/*
477 * SSLv3.0 MAC functions
478 */
479static void ssl_mac( mbedtls_md_context_t *md_ctx,
480 const unsigned char *secret,
481 const unsigned char *buf, size_t len,
482 const unsigned char *ctr, int type,
483 unsigned char out[SSL3_MAC_MAX_BYTES] )
484{
485 unsigned char header[11];
486 unsigned char padding[48];
487 int padlen;
488 int md_size = mbedtls_md_get_size( md_ctx->md_info );
489 int md_type = mbedtls_md_get_type( md_ctx->md_info );
490
491 /* Only MD5 and SHA-1 supported */
492 if( md_type == MBEDTLS_MD_MD5 )
493 padlen = 48;
494 else
495 padlen = 40;
496
497 memcpy( header, ctr, 8 );
498 header[ 8] = (unsigned char) type;
499 header[ 9] = (unsigned char)( len >> 8 );
500 header[10] = (unsigned char)( len );
501
502 memset( padding, 0x36, padlen );
503 mbedtls_md_starts( md_ctx );
504 mbedtls_md_update( md_ctx, secret, md_size );
505 mbedtls_md_update( md_ctx, padding, padlen );
506 mbedtls_md_update( md_ctx, header, 11 );
507 mbedtls_md_update( md_ctx, buf, len );
508 mbedtls_md_finish( md_ctx, out );
509
510 memset( padding, 0x5C, padlen );
511 mbedtls_md_starts( md_ctx );
512 mbedtls_md_update( md_ctx, secret, md_size );
513 mbedtls_md_update( md_ctx, padding, padlen );
514 mbedtls_md_update( md_ctx, out, md_size );
515 mbedtls_md_finish( md_ctx, out );
516}
517#endif /* MBEDTLS_SSL_PROTO_SSL3 */
518
Hanno Becker67a37db2020-05-28 16:27:07 +0100519#if defined(MBEDTLS_GCM_C) || \
520 defined(MBEDTLS_CCM_C) || \
521 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100522static int ssl_transform_aead_dynamic_iv_is_explicit(
523 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100524{
Hanno Becker17263802020-05-28 07:05:48 +0100525 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100526}
527
Hanno Becker17263802020-05-28 07:05:48 +0100528/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
529 *
530 * Concretely, this occurs in two variants:
531 *
532 * a) Fixed and dynamic IV lengths add up to total IV length, giving
533 * IV = fixed_iv || dynamic_iv
534 *
Hanno Becker15952812020-06-04 13:31:46 +0100535 * This variant is used in TLS 1.2 when used with GCM or CCM.
536 *
Hanno Becker17263802020-05-28 07:05:48 +0100537 * b) Fixed IV lengths matches total IV length, giving
538 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100539 *
540 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
541 *
542 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100543 *
544 * This function has the precondition that
545 *
546 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
547 *
548 * which has to be ensured by the caller. If this precondition
549 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100550 */
551static void ssl_build_record_nonce( unsigned char *dst_iv,
552 size_t dst_iv_len,
553 unsigned char const *fixed_iv,
554 size_t fixed_iv_len,
555 unsigned char const *dynamic_iv,
556 size_t dynamic_iv_len )
557{
558 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100559
560 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100561 memset( dst_iv, 0, dst_iv_len );
562 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100563
Hanno Becker17263802020-05-28 07:05:48 +0100564 dst_iv += dst_iv_len - dynamic_iv_len;
565 for( i = 0; i < dynamic_iv_len; i++ )
566 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100567}
Hanno Becker67a37db2020-05-28 16:27:07 +0100568#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100569
Hanno Beckera18d1322018-01-03 14:27:32 +0000570int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
571 mbedtls_ssl_transform *transform,
572 mbedtls_record *rec,
573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100577 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000578 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100579 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100580 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000581 size_t post_avail;
582
583 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000584#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200585 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000586 ((void) ssl);
587#endif
588
589 /* The PRNG is used for dynamic IV generation that's used
590 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200591#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000592 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
593 ((void) f_rng);
594 ((void) p_rng);
595#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000599 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100600 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
602 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
603 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100604 if( rec == NULL
605 || rec->buf == NULL
606 || rec->buf_len < rec->data_offset
607 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100608#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100609 || rec->cid_len != 0
610#endif
611 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000612 {
613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100615 }
616
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000617 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100618 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000620 data, rec->data_len );
621
622 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
623
624 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
625 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
627 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000628 rec->data_len,
629 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000630 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
631 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100632
Hanno Becker92313402020-05-20 13:58:58 +0100633 /* The following two code paths implement the (D)TLSInnerPlaintext
634 * structure present in TLS 1.3 and DTLS 1.2 + CID.
635 *
636 * See ssl_build_inner_plaintext() for more information.
637 *
638 * Note that this changes `rec->data_len`, and hence
639 * `post_avail` needs to be recalculated afterwards.
640 *
641 * Note also that the two code paths cannot occur simultaneously
642 * since they apply to different versions of the protocol. There
643 * is hence no risk of double-addition of the inner plaintext.
644 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100645#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
646 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
647 {
Hanno Becker13996922020-05-28 16:15:19 +0100648 size_t padding =
649 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100650 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100651 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100652 &rec->data_len,
653 post_avail,
654 rec->type,
655 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100656 {
657 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
658 }
659
660 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
661 }
662#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
663
Hanno Beckera0e20d02019-05-15 14:03:01 +0100664#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100665 /*
666 * Add CID information
667 */
668 rec->cid_len = transform->out_cid_len;
669 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
670 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100671
672 if( rec->cid_len != 0 )
673 {
Hanno Becker13996922020-05-28 16:15:19 +0100674 size_t padding =
675 ssl_compute_padding_length( rec->data_len,
676 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100677 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100678 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100679 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100680 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100681 * Note that this changes `rec->data_len`, and hence
682 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100683 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100684 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100685 &rec->data_len,
686 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100687 rec->type,
688 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100689 {
690 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
691 }
692
693 rec->type = MBEDTLS_SSL_MSG_CID;
694 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100695#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100696
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100697 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
698
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100700 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000701 */
Hanno Becker52344c22018-01-03 15:24:20 +0000702#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( mode == MBEDTLS_MODE_STREAM ||
704 ( mode == MBEDTLS_MODE_CBC
705#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000706 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100707#endif
708 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000710 if( post_avail < transform->maclen )
711 {
712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
713 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
714 }
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000717 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200718 {
Hanno Becker9d062f92020-02-07 10:26:36 +0000719 unsigned char mac[SSL3_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000720 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
721 data, rec->data_len, rec->ctr, rec->type, mac );
722 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200723 }
724 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
727 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000728 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200729 {
Hanno Becker992b6872017-11-09 18:57:39 +0000730 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
731
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100732 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
733 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000734
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000735 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100736 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000737 mbedtls_md_hmac_update( &transform->md_ctx_enc,
738 data, rec->data_len );
739 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
740 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
741
742 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200743 }
744 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200745#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
748 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200749 }
750
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000751 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
752 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200753
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000754 rec->data_len += transform->maclen;
755 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100756 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200757 }
Hanno Becker52344c22018-01-03 15:24:20 +0000758#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000759
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200760 /*
761 * Encrypt
762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
764 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000767 size_t olen;
Paul Elliottd48d5c62021-01-07 14:47:05 +0000768 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000769 "including %d bytes of padding",
770 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000772 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
773 transform->iv_enc, transform->ivlen,
774 data, rec->data_len,
775 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200778 return( ret );
779 }
780
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000781 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
784 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200785 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
Paul Bakker68884e32013-01-07 18:20:04 +0100787 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000789
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200790#if defined(MBEDTLS_GCM_C) || \
791 defined(MBEDTLS_CCM_C) || \
792 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200794 mode == MBEDTLS_MODE_CCM ||
795 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000796 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200798 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100799 unsigned char *dynamic_iv;
800 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100801 int dynamic_iv_is_explicit =
802 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000803
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100804 /* Check that there's space for the authentication tag. */
805 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000806 {
807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
808 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
809 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000810
Paul Bakker68884e32013-01-07 18:20:04 +0100811 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100812 * Build nonce for AEAD encryption.
813 *
814 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
815 * part of the IV is prepended to the ciphertext and
816 * can be chosen freely - in particular, it need not
817 * agree with the record sequence number.
818 * However, since ChaChaPoly as well as all AEAD modes
819 * in TLS 1.3 use the record sequence number as the
820 * dynamic part of the nonce, we uniformly use the
821 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100822 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100823 dynamic_iv = rec->ctr;
824 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200825
Hanno Becker17263802020-05-28 07:05:48 +0100826 ssl_build_record_nonce( iv, sizeof( iv ),
827 transform->iv_enc,
828 transform->fixed_ivlen,
829 dynamic_iv,
830 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100831
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100832 /*
833 * Build additional data for AEAD encryption.
834 * This depends on the TLS version.
835 */
836 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
837 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100838
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200839 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100840 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200841 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100842 dynamic_iv,
843 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000844 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100845 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000846 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200847 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000848 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000849
Paul Bakker68884e32013-01-07 18:20:04 +0100850 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200851 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200852 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000853
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100854 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000855 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100856 add_data, add_data_len,
857 data, rec->data_len, /* src */
858 data, rec->buf_len - (data - rec->buf), /* dst */
859 &rec->data_len,
860 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200863 return( ret );
864 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000865 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100866 data + rec->data_len - transform->taglen,
867 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100868 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000869 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100870
871 /*
872 * Prefix record content with dynamic IV in case it is explicit.
873 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100874 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100875 {
876 if( rec->data_offset < dynamic_iv_len )
877 {
878 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
879 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
880 }
881
882 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
883 rec->data_offset -= dynamic_iv_len;
884 rec->data_len += dynamic_iv_len;
885 }
886
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100887 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000888 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100890#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200891#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000893 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000895 size_t padlen, i;
896 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000897
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000898 /* Currently we're always using minimal padding
899 * (up to 255 bytes would be allowed). */
900 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
901 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 padlen = 0;
903
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000904 /* Check there's enough space in the buffer for the padding. */
905 if( post_avail < padlen + 1 )
906 {
907 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
908 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
909 }
910
Paul Bakker5121ce52009-01-03 21:22:43 +0000911 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000912 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000914 rec->data_len += padlen + 1;
915 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000918 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000919 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
920 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000921 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000922 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000923 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000924 if( f_rng == NULL )
925 {
926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
928 }
929
930 if( rec->data_offset < transform->ivlen )
931 {
932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
933 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
934 }
935
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000936 /*
937 * Generate IV
938 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000939 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000940 if( ret != 0 )
941 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000942
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000943 memcpy( data - transform->ivlen, transform->iv_enc,
944 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000945
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000946 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000948
Paul Elliottd48d5c62021-01-07 14:47:05 +0000949 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
950 "including %" MBEDTLS_PRINTF_SIZET
951 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000952 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200953 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000954
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000955 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
956 transform->iv_enc,
957 transform->ivlen,
958 data, rec->data_len,
959 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200962 return( ret );
963 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200964
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000965 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200969 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000972 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200973 {
974 /*
975 * Save IV in SSL3 and TLS1
976 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000977 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
978 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000979 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000980 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200981#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000982 {
983 data -= transform->ivlen;
984 rec->data_offset -= transform->ivlen;
985 rec->data_len += transform->ivlen;
986 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100989 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100990 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000991 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
992
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100993 /*
994 * MAC(MAC_write_key, seq_num +
995 * TLSCipherText.type +
996 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100997 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100998 * IV + // except for TLS 1.0
999 * ENC(content + padding + padding_length));
1000 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001001
1002 if( post_avail < transform->maclen)
1003 {
1004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1005 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1006 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001007
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001008 ssl_extract_add_data_from_record( add_data, &add_data_len,
1009 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +01001010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001012 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001013 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001014
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001015 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001016 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001017 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1018 data, rec->data_len );
1019 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1020 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001021
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001022 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001023
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001024 rec->data_len += transform->maclen;
1025 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001026 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001027 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001030 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001031#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001035 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001036
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001037 /* Make extra sure authentication was performed, exactly once */
1038 if( auth_done != 1 )
1039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1041 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001042 }
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 return( 0 );
1047}
1048
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001049#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001050/*
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001051 * Turn a bit into a mask:
1052 * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
1053 * - if bit == 0, return the all-bits 0 mask, aka 0
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001054 *
1055 * This function can be used to write constant-time code by replacing branches
1056 * with bit operations using masks.
1057 *
1058 * This function is implemented without using comparison operators, as those
1059 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001060 */
1061static size_t mbedtls_ssl_cf_mask_from_bit( size_t bit )
1062{
1063 /* MSVC has a warning about unary minus on unsigned integer types,
1064 * but this is well-defined and precisely what we want to do here. */
1065#if defined(_MSC_VER)
1066#pragma warning( push )
1067#pragma warning( disable : 4146 )
1068#endif
1069 return -bit;
1070#if defined(_MSC_VER)
1071#pragma warning( pop )
1072#endif
1073}
1074
1075/*
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001076 * Constant-flow mask generation for "less than" comparison:
1077 * - if x < y, return all bits 1, that is (size_t) -1
1078 * - otherwise, return all bits 0, that is 0
1079 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001080 * This function can be used to write constant-time code by replacing branches
1081 * with bit operations using masks.
1082 *
1083 * This function is implemented without using comparison operators, as those
1084 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001085 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001086static size_t mbedtls_ssl_cf_mask_lt( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001087{
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001088 /* This has the most significant bit set if and only if x < y */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001089 const size_t sub = x - y;
1090
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001091 /* sub1 = (x < y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001092 const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 );
1093
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001094 /* mask = (x < y) ? 0xff... : 0x00... */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001095 const size_t mask = mbedtls_ssl_cf_mask_from_bit( sub1 );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001096
1097 return( mask );
1098}
1099
1100/*
1101 * Constant-flow mask generation for "greater or equal" comparison:
1102 * - if x >= y, return all bits 1, that is (size_t) -1
1103 * - otherwise, return all bits 0, that is 0
1104 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001105 * This function can be used to write constant-time code by replacing branches
1106 * with bit operations using masks.
1107 *
1108 * This function is implemented without using comparison operators, as those
1109 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001110 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001111static size_t mbedtls_ssl_cf_mask_ge( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001112{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001113 return( ~mbedtls_ssl_cf_mask_lt( x, y ) );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001114}
1115
1116/*
1117 * Constant-flow boolean "equal" comparison:
1118 * return x == y
1119 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001120 * This function can be used to write constant-time code by replacing branches
1121 * with bit operations - it can be used in conjunction with
1122 * mbedtls_ssl_cf_mask_from_bit().
1123 *
1124 * This function is implemented without using comparison operators, as those
1125 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001126 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001127static size_t mbedtls_ssl_cf_bool_eq( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001128{
1129 /* diff = 0 if x == y, non-zero otherwise */
1130 const size_t diff = x ^ y;
1131
1132 /* MSVC has a warning about unary minus on unsigned integer types,
1133 * but this is well-defined and precisely what we want to do here. */
1134#if defined(_MSC_VER)
1135#pragma warning( push )
1136#pragma warning( disable : 4146 )
1137#endif
1138
1139 /* diff_msb's most significant bit is equal to x != y */
1140 const size_t diff_msb = ( diff | -diff );
1141
1142#if defined(_MSC_VER)
1143#pragma warning( pop )
1144#endif
1145
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001146 /* diff1 = (x != y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001147 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1148
1149 return( 1 ^ diff1 );
1150}
1151
1152/*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001153 * Constant-flow conditional memcpy:
1154 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1155 * - otherwise, a no-op,
1156 * but with execution flow independent of the values of c1 and c2.
1157 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001158 * This function is implemented without using comparison operators, as those
1159 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001160 */
Manuel Pégourié-Gonnarde7478432020-07-24 11:09:22 +02001161static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1162 const unsigned char *src,
1163 size_t len,
1164 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001165{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001166 /* mask = c1 == c2 ? 0xff : 0x00 */
1167 const size_t equal = mbedtls_ssl_cf_bool_eq( c1, c2 );
Manuel Pégourié-Gonnard2a59fb42020-08-25 11:51:46 +02001168 const unsigned char mask = (unsigned char) mbedtls_ssl_cf_mask_from_bit( equal );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001169
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001170 /* dst[i] = c1 == c2 ? src[i] : dst[i] */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001171 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001172 dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001173}
1174
1175/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001176 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001177 *
1178 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1179 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001180 */
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02001181MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001182 mbedtls_md_context_t *ctx,
1183 const unsigned char *add_data, size_t add_data_len,
1184 const unsigned char *data, size_t data_len_secret,
1185 size_t min_data_len, size_t max_data_len,
1186 unsigned char *output )
1187{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001188 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001189 * This function breaks the HMAC abstraction and uses the md_clone()
1190 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001191 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001192 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001193 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001194 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001195 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001196 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1197 * minlen, then cloning the context, and for each byte up to maxlen
1198 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001199 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001200 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001201 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001202 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001203 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1204 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001205 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +02001206 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001207 const unsigned char * const okey = ikey + block_size;
1208 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001209
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001210 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1211 mbedtls_md_context_t aux;
1212 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +02001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001214
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001215 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001216
1217#define MD_CHK( func_call ) \
1218 do { \
1219 ret = (func_call); \
1220 if( ret != 0 ) \
1221 goto cleanup; \
1222 } while( 0 )
1223
1224 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001225
1226 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1227 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001228 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1229 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001230
1231 /* For each possible length, compute the hash up to that point */
1232 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001233 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001234 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1235 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001236 /* Keep only the correct inner_hash in the output buffer */
1237 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1238 offset, data_len_secret );
1239
1240 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001241 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001242 }
1243
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001244 /* Now compute HASH(okey + inner_hash) */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001245 MD_CHK( mbedtls_md_starts( ctx ) );
1246 MD_CHK( mbedtls_md_update( ctx, okey, block_size ) );
1247 MD_CHK( mbedtls_md_update( ctx, output, hash_size ) );
1248 MD_CHK( mbedtls_md_finish( ctx, output ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001249
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001250 /* Done, get ready for next time */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001251 MD_CHK( mbedtls_md_hmac_reset( ctx ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001252
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001253#undef MD_CHK
1254
1255cleanup:
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001256 mbedtls_md_free( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001257 return( ret );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001258}
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001259
1260/*
1261 * Constant-flow memcpy from variable position in buffer.
1262 * - functionally equivalent to memcpy(dst, src + offset_secret, len)
Manuel Pégourié-Gonnardba6fc972020-08-24 12:59:55 +02001263 * - but with execution flow independent from the value of offset_secret.
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001264 */
1265MBEDTLS_STATIC_TESTABLE void mbedtls_ssl_cf_memcpy_offset(
1266 unsigned char *dst,
1267 const unsigned char *src_base,
1268 size_t offset_secret,
1269 size_t offset_min, size_t offset_max,
1270 size_t len )
1271{
Manuel Pégourié-Gonnardde1cf2c52020-08-19 12:35:30 +02001272 size_t offset;
1273
1274 for( offset = offset_min; offset <= offset_max; offset++ )
1275 {
1276 mbedtls_ssl_cf_memcpy_if_eq( dst, src_base + offset, len,
1277 offset, offset_secret );
1278 }
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001279}
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001280#endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001281
Hanno Becker605949f2019-07-12 08:23:59 +01001282int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00001283 mbedtls_ssl_transform *transform,
1284 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00001285{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001286 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001288 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00001289#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001290 size_t padlen = 0, correct = 1;
1291#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001292 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01001293 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01001294 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001295
Hanno Beckera18d1322018-01-03 14:27:32 +00001296#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001297 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001298 ((void) ssl);
1299#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001302 if( rec == NULL ||
1303 rec->buf == NULL ||
1304 rec->buf_len < rec->data_offset ||
1305 rec->buf_len - rec->data_offset < rec->data_len )
1306 {
1307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001309 }
1310
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001311 data = rec->buf + rec->data_offset;
1312 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313
Hanno Beckera0e20d02019-05-15 14:03:01 +01001314#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01001315 /*
1316 * Match record's CID with incoming CID.
1317 */
Hanno Becker938489a2019-05-08 13:02:22 +01001318 if( rec->cid_len != transform->in_cid_len ||
1319 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
1320 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01001321 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01001322 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001323#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
1326 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001327 {
1328 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001329 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1330 transform->iv_dec,
1331 transform->ivlen,
1332 data, rec->data_len,
1333 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001336 return( ret );
1337 }
1338
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001339 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001343 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 }
Paul Bakker68884e32013-01-07 18:20:04 +01001345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001347#if defined(MBEDTLS_GCM_C) || \
1348 defined(MBEDTLS_CCM_C) || \
1349 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001351 mode == MBEDTLS_MODE_CCM ||
1352 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001354 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +01001355 unsigned char *dynamic_iv;
1356 size_t dynamic_iv_len;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001357
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001358 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +01001359 * Extract dynamic part of nonce for AEAD decryption.
1360 *
1361 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
1362 * part of the IV is prepended to the ciphertext and
1363 * can be chosen freely - in particular, it need not
1364 * agree with the record sequence number.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001365 */
Hanno Beckerdf8be222020-05-21 15:30:57 +01001366 dynamic_iv_len = sizeof( rec->ctr );
Hanno Becker17263802020-05-28 07:05:48 +01001367 if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 )
Hanno Beckerdf8be222020-05-21 15:30:57 +01001368 {
1369 if( rec->data_len < dynamic_iv_len )
1370 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1372 " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ",
Hanno Beckerdf8be222020-05-21 15:30:57 +01001373 rec->data_len,
1374 dynamic_iv_len ) );
1375 return( MBEDTLS_ERR_SSL_INVALID_MAC );
1376 }
1377 dynamic_iv = data;
1378
1379 data += dynamic_iv_len;
1380 rec->data_offset += dynamic_iv_len;
1381 rec->data_len -= dynamic_iv_len;
1382 }
Hanno Becker17263802020-05-28 07:05:48 +01001383 else
1384 {
1385 dynamic_iv = rec->ctr;
1386 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001387
1388 /* Check that there's space for the authentication tag. */
1389 if( rec->data_len < transform->taglen )
1390 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001391 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1392 ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ",
Christian von Arnim883d3042020-12-01 11:58:29 +01001393 rec->data_len,
1394 transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001396 }
Hanno Beckerdf8be222020-05-21 15:30:57 +01001397 rec->data_len -= transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001398
Hanno Beckerdf8be222020-05-21 15:30:57 +01001399 /*
1400 * Prepare nonce from dynamic and static parts.
1401 */
Hanno Becker17263802020-05-28 07:05:48 +01001402 ssl_build_record_nonce( iv, sizeof( iv ),
1403 transform->iv_dec,
1404 transform->fixed_ivlen,
1405 dynamic_iv,
1406 dynamic_iv_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001407
Hanno Beckerdf8be222020-05-21 15:30:57 +01001408 /*
1409 * Build additional data for AEAD encryption.
1410 * This depends on the TLS version.
1411 */
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001412 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1413 transform->minor_ver );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001414 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01001415 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001416
Hanno Beckerd96a6522019-07-10 13:55:25 +01001417 /* Because of the check above, we know that there are
1418 * explicit_iv_len Bytes preceeding data, and taglen
1419 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01001420 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01001421 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001422
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001423 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001424 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00001425 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001426
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001427 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001428 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 */
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001430 if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001431 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01001432 add_data, add_data_len,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +01001433 data, rec->data_len + transform->taglen, /* src */
1434 data, rec->buf_len - (data - rec->buf), &olen, /* dst */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001435 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
1440 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001441
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001442 return( ret );
1443 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001444 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001445
Hanno Beckerd96a6522019-07-10 13:55:25 +01001446 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001447 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1450 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001451 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001455#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001457 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001458 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001459
Paul Bakker5121ce52009-01-03 21:22:43 +00001460 /*
Paul Bakker45829992013-01-03 14:52:21 +01001461 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001464 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
1465 {
1466 /* The ciphertext is prefixed with the CBC IV. */
1467 minlen += transform->ivlen;
1468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001469#endif
Paul Bakker45829992013-01-03 14:52:21 +01001470
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001471 /* Size considerations:
1472 *
1473 * - The CBC cipher text must not be empty and hence
1474 * at least of size transform->ivlen.
1475 *
1476 * Together with the potential IV-prefix, this explains
1477 * the first of the two checks below.
1478 *
1479 * - The record must contain a MAC, either in plain or
1480 * encrypted, depending on whether Encrypt-then-MAC
1481 * is used or not.
1482 * - If it is, the message contains the IV-prefix,
1483 * the CBC ciphertext, and the MAC.
1484 * - If it is not, the padded plaintext, and hence
1485 * the CBC ciphertext, has at least length maclen + 1
1486 * because there is at least the padding length byte.
1487 *
1488 * As the CBC ciphertext is not empty, both cases give the
1489 * lower bound minlen + maclen + 1 on the record size, which
1490 * we test for in the second check below.
1491 */
1492 if( rec->data_len < minlen + transform->ivlen ||
1493 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01001494 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1496 ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET
1497 "), maclen (%" MBEDTLS_PRINTF_SIZET ") "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001498 "+ 1 ) ( + expl IV )", rec->data_len,
1499 transform->ivlen,
1500 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01001502 }
1503
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001504 /*
1505 * Authenticate before decrypt if enabled
1506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001508 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001509 {
Hanno Becker992b6872017-11-09 18:57:39 +00001510 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001513
Hanno Beckerd96a6522019-07-10 13:55:25 +01001514 /* Update data_len in tandem with add_data.
1515 *
1516 * The subtraction is safe because of the previous check
1517 * data_len >= minlen + maclen + 1.
1518 *
1519 * Afterwards, we know that data + data_len is followed by at
1520 * least maclen Bytes, which justifies the call to
1521 * mbedtls_ssl_safer_memcmp() below.
1522 *
1523 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001524 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001525 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1526 transform->minor_ver );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001527
Hanno Beckerd96a6522019-07-10 13:55:25 +01001528 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01001529 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
1530 add_data_len );
1531 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
1532 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001533 mbedtls_md_hmac_update( &transform->md_ctx_dec,
1534 data, rec->data_len );
1535 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
1536 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001537
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001538 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
1539 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00001540 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001541 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001542
Hanno Beckerd96a6522019-07-10 13:55:25 +01001543 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001544 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
1545 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001549 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001550 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001551 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001553
1554 /*
1555 * Check length sanity
1556 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01001557
1558 /* We know from above that data_len > minlen >= 0,
1559 * so the following check in particular implies that
1560 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001561 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001562 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1564 ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001565 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001567 }
1568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001570 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001571 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001572 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001573 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001574 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01001575 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001576 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001577
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001578 data += transform->ivlen;
1579 rec->data_offset += transform->ivlen;
1580 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001581 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001583
Hanno Beckerd96a6522019-07-10 13:55:25 +01001584 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
1585
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001586 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
1587 transform->iv_dec, transform->ivlen,
1588 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001591 return( ret );
1592 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001593
Hanno Beckerd96a6522019-07-10 13:55:25 +01001594 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001595 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1598 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001599 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001602 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02001603 {
1604 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01001605 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
1606 * records is equivalent to CBC decryption of the concatenation
1607 * of the records; in other words, IVs are maintained across
1608 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02001609 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001610 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
1611 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001613#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001614
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001615 /* Safe since data_len >= minlen + maclen + 1, so after having
1616 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01001617 * data_len > 0 (because of data_len % ivlen == 0, it's actually
1618 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001619 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001620
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001621 if( auth_done == 1 )
1622 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001623 const size_t mask = mbedtls_ssl_cf_mask_ge(
1624 rec->data_len,
1625 padlen + 1 );
1626 correct &= mask;
1627 padlen &= mask;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001628 }
1629 else
Paul Bakker45829992013-01-03 14:52:21 +01001630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001632 if( rec->data_len < transform->maclen + padlen + 1 )
1633 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00001634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET
1635 ") < maclen (%" MBEDTLS_PRINTF_SIZET
1636 ") + padlen (%" MBEDTLS_PRINTF_SIZET ")",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001637 rec->data_len,
1638 transform->maclen,
1639 padlen + 1 ) );
1640 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01001641#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001642
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001643 const size_t mask = mbedtls_ssl_cf_mask_ge(
1644 rec->data_len,
1645 transform->maclen + padlen + 1 );
1646 correct &= mask;
1647 padlen &= mask;
Paul Bakker45829992013-01-03 14:52:21 +01001648 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001650 padlen++;
1651
1652 /* Regardless of the validity of the padding,
1653 * we have data_len >= padlen here. */
1654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001656 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001657 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001658 /* This is the SSL 3.0 path, we don't have to worry about Lucky
1659 * 13, because there's a strictly worse padding attack built in
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001660 * the protocol (known as part of POODLE), so we don't care if the
1661 * code is not constant-time, in particular branches are OK. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001662 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Elliottd48d5c62021-01-07 14:47:05 +00001665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %" MBEDTLS_PRINTF_SIZET ", "
1666 "should be no more than %" MBEDTLS_PRINTF_SIZET,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001667 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001668#endif
Paul Bakker45829992013-01-03 14:52:21 +01001669 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001670 }
1671 }
1672 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1674#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1675 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001676 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001678 /* The padding check involves a series of up to 256
1679 * consecutive memory reads at the end of the record
1680 * plaintext buffer. In order to hide the length and
1681 * validity of the padding, always perform exactly
1682 * `min(256,plaintext_len)` reads (but take into account
1683 * only the last `padlen` bytes for the padding check). */
1684 size_t pad_count = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001685 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001686
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001687 /* Index of first padding byte; it has been ensured above
1688 * that the subtraction is safe. */
1689 size_t const padding_idx = rec->data_len - padlen;
1690 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
1691 size_t const start_idx = rec->data_len - num_checks;
1692 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01001693
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001694 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001695 {
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001696 /* pad_count += (idx >= padding_idx) &&
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001697 * (check[idx] == padlen - 1);
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001698 */
1699 const size_t mask = mbedtls_ssl_cf_mask_ge( idx, padding_idx );
1700 const size_t equal = mbedtls_ssl_cf_bool_eq( check[idx],
1701 padlen - 1 );
1702 pad_count += mask & equal;
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001703 }
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001704 correct &= mbedtls_ssl_cf_bool_eq( pad_count, padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001707 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001709#endif
Manuel Pégourié-Gonnard822b3722020-09-18 09:54:01 +02001710 padlen &= mbedtls_ssl_cf_mask_from_bit( correct );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001712 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1714 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001715 {
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 );
Paul Bakker577e0062013-08-28 11:57:20 +02001718 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001719
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001720 /* If the padding was found to be invalid, padlen == 0
1721 * and the subtraction is safe. If the padding was found valid,
1722 * padlen hasn't been changed and the previous assertion
1723 * data_len >= padlen still holds. */
1724 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001725 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001726 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001727#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1730 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001731 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001733#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001735 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02001736#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
1738 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001739 * Authenticate if not done yet.
1740 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001741 */
Hanno Becker52344c22018-01-03 15:24:20 +00001742#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001743 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 {
Hanno Becker992b6872017-11-09 18:57:39 +00001745 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001746 unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01001747
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001748 /* If the initial value of padlen was such that
1749 * data_len < maclen + padlen + 1, then padlen
1750 * got reset to 1, and the initial check
1751 * data_len >= minlen + maclen + 1
1752 * guarantees that at this point we still
1753 * have at least data_len >= maclen.
1754 *
1755 * If the initial value of padlen was such that
1756 * data_len >= maclen + padlen + 1, then we have
1757 * subtracted either padlen + 1 (if the padding was correct)
1758 * or 0 (if the padding was incorrect) since then,
1759 * hence data_len >= maclen in any case.
1760 */
1761 rec->data_len -= transform->maclen;
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001762 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
1763 transform->minor_ver );
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001766 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001767 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001768 ssl_mac( &transform->md_ctx_dec,
1769 transform->mac_dec,
1770 data, rec->data_len,
1771 rec->ctr, rec->type,
1772 mac_expect );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001773 memcpy( mac_peer, data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001774 }
1775 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1777#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1778 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001779 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 {
1781 /*
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001782 * The next two sizes are the minimum and maximum values of
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001783 * data_len over all padlen values.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001784 *
1785 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01001786 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001787 *
1788 * Note that max_len + maclen is never more than the buffer
1789 * length, as we previously did in_msglen -= maclen too.
1790 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001791 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001792 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
1793
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001794 ret = mbedtls_ssl_cf_hmac( &transform->md_ctx_dec,
1795 add_data, add_data_len,
1796 data, rec->data_len, min_len, max_len,
1797 mac_expect );
1798 if( ret != 0 )
Gilles Peskine20b44082018-05-29 14:06:49 +02001799 {
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_cf_hmac", ret );
1801 return( ret );
Gilles Peskine20b44082018-05-29 14:06:49 +02001802 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001803
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02001804 mbedtls_ssl_cf_memcpy_offset( mac_peer, data,
1805 rec->data_len,
1806 min_len, max_len,
1807 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001808 }
1809 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
1811 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001817#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001818 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001819 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001820#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001821
Manuel Pégourié-Gonnard3c31afa2020-08-13 12:08:54 +02001822 if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00001823 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_SSL_DEBUG_ALL)
1826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001827#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 correct = 0;
1829 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001830 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001831 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01001832
1833 /*
1834 * Finally check the correct flag
1835 */
1836 if( correct == 0 )
1837 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00001838#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001839
1840 /* Make extra sure authentication was performed, exactly once */
1841 if( auth_done != 1 )
1842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1844 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Hanno Beckerccc13d02020-05-04 12:30:04 +01001847#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1848 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1849 {
1850 /* Remove inner padding and infer true content type. */
1851 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1852 &rec->type );
1853
1854 if( ret != 0 )
1855 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1856 }
1857#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1858
Hanno Beckera0e20d02019-05-15 14:03:01 +01001859#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001860 if( rec->cid_len != 0 )
1861 {
Hanno Becker581bc1b2020-05-04 12:20:03 +01001862 ret = ssl_parse_inner_plaintext( data, &rec->data_len,
1863 &rec->type );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001864 if( ret != 0 )
1865 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
1866 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001867#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 return( 0 );
1872}
1873
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001874#undef MAC_NONE
1875#undef MAC_PLAINTEXT
1876#undef MAC_CIPHERTEXT
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879/*
1880 * Compression/decompression functions
1881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001883{
Janos Follath865b3eb2019-12-16 11:46:15 +00001884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001885 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04001886 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001887 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001888 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001889#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1890 size_t out_buf_len = ssl->out_buf_len;
1891#else
1892 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1893#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001896
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001897 if( len_pre == 0 )
1898 return( 0 );
1899
Paul Bakker2770fbd2012-07-03 13:30:23 +00001900 memcpy( msg_pre, ssl->out_msg, len_pre );
1901
Paul Elliottd48d5c62021-01-07 14:47:05 +00001902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001903 ssl->out_msglen ) );
1904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001905 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001906 ssl->out_msg, ssl->out_msglen );
1907
Paul Bakker48916f92012-09-16 19:57:18 +00001908 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1909 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1910 ssl->transform_out->ctx_deflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001911 ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001912
Paul Bakker48916f92012-09-16 19:57:18 +00001913 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001914 if( ret != Z_OK )
1915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1917 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001918 }
1919
Darryl Greenb33cc762019-11-28 14:29:44 +00001920 ssl->out_msglen = out_buf_len -
Andrzej Kurek5462e022018-04-20 07:58:53 -04001921 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001922
Paul Elliottd48d5c62021-01-07 14:47:05 +00001923 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001924 ssl->out_msglen ) );
1925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927 ssl->out_msg, ssl->out_msglen );
1928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001930
1931 return( 0 );
1932}
1933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001935{
Janos Follath865b3eb2019-12-16 11:46:15 +00001936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001937 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001938 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001939 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001940 unsigned char *msg_pre = ssl->compress_buf;
Darryl Greenb33cc762019-11-28 14:29:44 +00001941#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1942 size_t in_buf_len = ssl->in_buf_len;
1943#else
1944 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1945#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001948
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001949 if( len_pre == 0 )
1950 return( 0 );
1951
Paul Bakker2770fbd2012-07-03 13:30:23 +00001952 memcpy( msg_pre, ssl->in_msg, len_pre );
1953
Paul Elliottd48d5c62021-01-07 14:47:05 +00001954 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955 ssl->in_msglen ) );
1956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001958 ssl->in_msg, ssl->in_msglen );
1959
Paul Bakker48916f92012-09-16 19:57:18 +00001960 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1961 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1962 ssl->transform_in->ctx_inflate.next_out = msg_post;
Darryl Greenb33cc762019-11-28 14:29:44 +00001963 ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001964
Paul Bakker48916f92012-09-16 19:57:18 +00001965 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001966 if( ret != Z_OK )
1967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1969 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001970 }
1971
Darryl Greenb33cc762019-11-28 14:29:44 +00001972 ssl->in_msglen = in_buf_len -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04001973 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001974
Paul Elliottd48d5c62021-01-07 14:47:05 +00001975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001976 ssl->in_msglen ) );
1977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00001979 ssl->in_msg, ssl->in_msglen );
1980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001982
1983 return( 0 );
1984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001986
Paul Bakker5121ce52009-01-03 21:22:43 +00001987/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001988 * Fill the input message buffer by appending data to it.
1989 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001990 *
1991 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1992 * available (from this read and/or a previous one). Otherwise, an error code
1993 * is returned (possibly EOF or WANT_READ).
1994 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001995 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1996 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1997 * since we always read a whole datagram at once.
1998 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001999 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002000 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002003{
Janos Follath865b3eb2019-12-16 11:46:15 +00002004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00002005 size_t len;
Darryl Greenb33cc762019-11-28 14:29:44 +00002006#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2007 size_t in_buf_len = ssl->in_buf_len;
2008#else
2009 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
2010#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002012 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002014 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002017 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002018 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002019 }
2020
Darryl Greenb33cc762019-11-28 14:29:44 +00002021 if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002025 }
2026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002029 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002030 uint32_t timeout;
2031
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002032 /*
2033 * The point is, we need to always read a full datagram at once, so we
2034 * sometimes read more then requested, and handle the additional data.
2035 * It could be the rest of the current record (while fetching the
2036 * header) and/or some other records in the same datagram.
2037 */
2038
2039 /*
2040 * Move to the next record in the already read datagram if applicable
2041 */
2042 if( ssl->next_record_offset != 0 )
2043 {
2044 if( ssl->in_left < ssl->next_record_offset )
2045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2047 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002048 }
2049
2050 ssl->in_left -= ssl->next_record_offset;
2051
2052 if( ssl->in_left != 0 )
2053 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002054 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %"
2055 MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002056 ssl->next_record_offset ) );
2057 memmove( ssl->in_hdr,
2058 ssl->in_hdr + ssl->next_record_offset,
2059 ssl->in_left );
2060 }
2061
2062 ssl->next_record_offset = 0;
2063 }
2064
Paul Elliottd48d5c62021-01-07 14:47:05 +00002065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
2066 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002068
2069 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002070 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002071 */
2072 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002075 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002076 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002077
2078 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01002079 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002080 * are not at the beginning of a new record, the caller did something
2081 * wrong.
2082 */
2083 if( ssl->in_left != 0 )
2084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2086 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002087 }
2088
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002089 /*
2090 * Don't even try to read if time's out already.
2091 * This avoids by-passing the timer when repeatedly receiving messages
2092 * that will end up being dropped.
2093 */
Hanno Becker7876d122020-02-05 10:39:31 +00002094 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01002095 {
2096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002097 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01002098 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002099 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002100 {
Darryl Greenb33cc762019-11-28 14:29:44 +00002101 len = in_buf_len - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002104 timeout = ssl->handshake->retransmit_timeout;
2105 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002106 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002107
Paul Elliott9f352112020-12-09 14:55:45 +00002108 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002109
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002110 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002111 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2112 timeout );
2113 else
2114 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002117
2118 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002120 }
2121
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002122 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Hanno Becker0f57a652020-02-05 10:37:26 +00002125 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002127 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002128 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002129 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002132 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002133 }
2134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002138 return( ret );
2139 }
2140
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002141 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002142 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002144 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002146 {
Hanno Becker786300f2020-02-05 10:46:40 +00002147 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002148 {
Hanno Becker786300f2020-02-05 10:46:40 +00002149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
2150 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002151 return( ret );
2152 }
2153
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01002154 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002155 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002157 }
2158
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 if( ret < 0 )
2160 return( ret );
2161
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002162 ssl->in_left = ret;
2163 }
2164 else
2165#endif
2166 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
2168 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002169 ssl->in_left, nb_want ) );
2170
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002171 while( ssl->in_left < nb_want )
2172 {
2173 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002174
Hanno Becker7876d122020-02-05 10:39:31 +00002175 if( mbedtls_ssl_check_timer( ssl ) != 0 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02002176 ret = MBEDTLS_ERR_SSL_TIMEOUT;
2177 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002178 {
2179 if( ssl->f_recv_timeout != NULL )
2180 {
2181 ret = ssl->f_recv_timeout( ssl->p_bio,
2182 ssl->in_hdr + ssl->in_left, len,
2183 ssl->conf->read_timeout );
2184 }
2185 else
2186 {
2187 ret = ssl->f_recv( ssl->p_bio,
2188 ssl->in_hdr + ssl->in_left, len );
2189 }
2190 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002191
Paul Elliottd48d5c62021-01-07 14:47:05 +00002192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET
2193 ", nb_want: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02002194 ssl->in_left, nb_want ) );
2195 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002196
2197 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002199
2200 if( ret < 0 )
2201 return( ret );
2202
makise-homuraaf9513b2020-08-24 18:26:27 +03002203 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08002204 {
Darryl Green11999bb2018-03-13 15:22:58 +00002205 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002206 ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested",
Paul Elliott9f352112020-12-09 14:55:45 +00002207 ret, len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08002208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2209 }
2210
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002211 ssl->in_left += ret;
2212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
2214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002216
2217 return( 0 );
2218}
2219
2220/*
2221 * Flush any data not yet written
2222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002224{
Janos Follath865b3eb2019-12-16 11:46:15 +00002225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker04484622018-08-06 09:49:38 +01002226 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002230 if( ssl->f_send == NULL )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01002233 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002234 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002235 }
2236
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002237 /* Avoid incrementing counter if data is flushed */
2238 if( ssl->out_left == 0 )
2239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002241 return( 0 );
2242 }
2243
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 while( ssl->out_left > 0 )
2245 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET
2247 ", out_left: %" MBEDTLS_PRINTF_SIZET,
Hanno Becker5903de42019-05-03 14:46:38 +01002248 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Hanno Becker2b1e3542018-08-06 11:19:13 +01002250 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002251 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
2255 if( ret <= 0 )
2256 return( ret );
2257
makise-homuraaf9513b2020-08-24 18:26:27 +03002258 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08002259 {
Darryl Green11999bb2018-03-13 15:22:58 +00002260 MBEDTLS_SSL_DEBUG_MSG( 1,
Paul Elliottd48d5c62021-01-07 14:47:05 +00002261 ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent",
Paul Elliott9f352112020-12-09 14:55:45 +00002262 ret, ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08002263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2264 }
2265
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 ssl->out_left -= ret;
2267 }
2268
Hanno Becker2b1e3542018-08-06 11:19:13 +01002269#if defined(MBEDTLS_SSL_PROTO_DTLS)
2270 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002271 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002272 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002273 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01002274 else
2275#endif
2276 {
2277 ssl->out_hdr = ssl->out_buf + 8;
2278 }
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002279 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
2283 return( 0 );
2284}
2285
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002286/*
2287 * Functions to handle the DTLS retransmission state machine
2288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002290/*
2291 * Append current handshake message to current outgoing flight
2292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01002296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
2297 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
2298 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002299
2300 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002301 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002302 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002303 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002306 }
2307
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02002308 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002309 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00002310 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed",
2311 ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02002313 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002314 }
2315
2316 /* Copy current handshake message with headers */
2317 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2318 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002319 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002320 msg->next = NULL;
2321
2322 /* Append to the current flight */
2323 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002324 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002325 else
2326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002328 while( cur->next != NULL )
2329 cur = cur->next;
2330 cur->next = msg;
2331 }
2332
Hanno Becker3b235902018-08-06 09:54:53 +01002333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002334 return( 0 );
2335}
2336
2337/*
2338 * Free the current flight of handshake messages
2339 */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002340void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002341{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 mbedtls_ssl_flight_item *cur = flight;
2343 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002344
2345 while( cur != NULL )
2346 {
2347 next = cur->next;
2348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 mbedtls_free( cur->p );
2350 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002351
2352 cur = next;
2353 }
2354}
2355
2356/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002357 * Swap transform_out and out_ctr with the alternative ones
2358 */
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002359static int ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002360{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002361 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002362 unsigned char tmp_out_ctr[8];
2363
2364 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002367 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002368 }
2369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002371
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002372 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002373 tmp_transform = ssl->transform_out;
2374 ssl->transform_out = ssl->handshake->alt_transform_out;
2375 ssl->handshake->alt_transform_out = tmp_transform;
2376
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002377 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01002378 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
2379 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002380 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002381
2382 /* Adjust to the newly activated transform */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002383 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2386 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002387 {
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002388 int ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND );
2389 if( ret != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
2392 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002393 }
2394 }
2395#endif
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002396
2397 return( 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002398}
2399
2400/*
2401 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002402 */
2403int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
2404{
2405 int ret = 0;
2406
2407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
2408
2409 ret = mbedtls_ssl_flight_transmit( ssl );
2410
2411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
2412
2413 return( ret );
2414}
2415
2416/*
2417 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002418 *
2419 * Need to remember the current message in case flush_output returns
2420 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002421 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002422 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002423int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002424{
Janos Follath865b3eb2019-12-16 11:46:15 +00002425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002429 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002431
2432 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002433 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002434 ret = ssl_swap_epochs( ssl );
2435 if( ret != 0 )
2436 return( ret );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002439 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002440
2441 while( ssl->handshake->cur_msg != NULL )
2442 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002443 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002444 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002445
Hanno Beckere1dcb032018-08-17 16:47:58 +01002446 int const is_finished =
2447 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
2448 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
2449
Hanno Becker04da1892018-08-14 13:22:10 +01002450 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
2451 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
2452
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002453 /* Swap epochs before sending Finished: we can't do it after
2454 * sending ChangeCipherSpec, in case write returns WANT_READ.
2455 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01002456 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002457 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002459 ret = ssl_swap_epochs( ssl );
2460 if( ret != 0 )
2461 return( ret );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002462 }
2463
Hanno Becker67bc7c32018-08-06 11:33:50 +01002464 ret = ssl_get_remaining_payload_in_datagram( ssl );
2465 if( ret < 0 )
2466 return( ret );
2467 max_frag_len = (size_t) ret;
2468
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002469 /* CCS is copied as is, while HS messages may need fragmentation */
2470 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2471 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002472 if( max_frag_len == 0 )
2473 {
2474 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2475 return( ret );
2476
2477 continue;
2478 }
2479
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002480 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002481 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002482 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002483
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002484 /* Update position inside current message */
2485 ssl->handshake->cur_msg_p += cur->len;
2486 }
2487 else
2488 {
2489 const unsigned char * const p = ssl->handshake->cur_msg_p;
2490 const size_t hs_len = cur->len - 12;
2491 const size_t frag_off = p - ( cur->p + 12 );
2492 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002493 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002494
Hanno Beckere1dcb032018-08-17 16:47:58 +01002495 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02002496 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01002497 if( is_finished )
Manuel Pégourié-Gonnarde07bc202020-02-26 09:53:42 +01002498 {
2499 ret = ssl_swap_epochs( ssl );
2500 if( ret != 0 )
2501 return( ret );
2502 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002503
Hanno Becker67bc7c32018-08-06 11:33:50 +01002504 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2505 return( ret );
2506
2507 continue;
2508 }
2509 max_hs_frag_len = max_frag_len - 12;
2510
2511 cur_hs_frag_len = rem_len > max_hs_frag_len ?
2512 max_hs_frag_len : rem_len;
2513
2514 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002515 {
2516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01002517 (unsigned) cur_hs_frag_len,
2518 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02002519 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02002520
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002521 /* Messages are stored with handshake headers as if not fragmented,
2522 * copy beginning of headers then fill fragmentation fields.
2523 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
2524 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002525
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002526 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
2527 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
2528 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
2529
Hanno Becker67bc7c32018-08-06 11:33:50 +01002530 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
2531 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
2532 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002533
2534 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
2535
Hanno Becker3f7b9732018-08-28 09:53:25 +01002536 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002537 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
2538 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002539 ssl->out_msgtype = cur->type;
2540
2541 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002542 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002543 }
2544
2545 /* If done with the current message move to the next one if any */
2546 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
2547 {
2548 if( cur->next != NULL )
2549 {
2550 ssl->handshake->cur_msg = cur->next;
2551 ssl->handshake->cur_msg_p = cur->next->p + 12;
2552 }
2553 else
2554 {
2555 ssl->handshake->cur_msg = NULL;
2556 ssl->handshake->cur_msg_p = NULL;
2557 }
2558 }
2559
2560 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01002561 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002564 return( ret );
2565 }
2566 }
2567
Hanno Becker67bc7c32018-08-06 11:33:50 +01002568 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
2569 return( ret );
2570
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02002571 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
2573 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002574 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Hanno Becker0f57a652020-02-05 10:37:26 +00002577 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002578 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002579
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002581
2582 return( 0 );
2583}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002584
2585/*
2586 * To be called when the last message of an incoming flight is received.
2587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002588void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002589{
2590 /* We won't need to resend that one any more */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002591 mbedtls_ssl_flight_free( ssl->handshake->flight );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002592 ssl->handshake->flight = NULL;
2593 ssl->handshake->cur_msg = NULL;
2594
2595 /* The next incoming flight will start with this msg_seq */
2596 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2597
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002598 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01002599 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01002600
Hanno Becker0271f962018-08-16 13:23:47 +01002601 /* Clear future message buffering structure. */
Hanno Becker533ab5f2020-02-05 10:49:13 +00002602 mbedtls_ssl_buffering_free( ssl );
Hanno Becker0271f962018-08-16 13:23:47 +01002603
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002604 /* Cancel timer */
Hanno Becker0f57a652020-02-05 10:37:26 +00002605 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2608 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002611 }
2612 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002614}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002615
2616/*
2617 * To be called when the last message of an outgoing flight is send.
2618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002620{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002621 ssl_reset_retransmit_timeout( ssl );
Hanno Becker0f57a652020-02-05 10:37:26 +00002622 mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2625 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002628 }
2629 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002633
Paul Bakker5121ce52009-01-03 21:22:43 +00002634/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002635 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002637
2638/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002639 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002640 *
2641 * - fill in handshake headers
2642 * - update handshake checksum
2643 * - DTLS: save message for resending
2644 * - then pass to the record layer
2645 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002646 * DTLS: except for HelloRequest, messages are only queued, and will only be
2647 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002648 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002649 * Inputs:
2650 * - ssl->out_msglen: 4 + actual handshake message len
2651 * (4 is the size of handshake headers for TLS)
2652 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
2653 * - ssl->out_msg + 4: the handshake message body
2654 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02002655 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002656 * - ssl->out_msglen: the length of the record contents
2657 * (including handshake headers but excluding record headers)
2658 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002659 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002660int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002661{
Janos Follath865b3eb2019-12-16 11:46:15 +00002662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002663 const size_t hs_len = ssl->out_msglen - 4;
2664 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
2667
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002668 /*
2669 * Sanity checks
2670 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002671 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002672 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
2673 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01002674 /* In SSLv3, the client might send a NoCertificate alert. */
2675#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
2676 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
2677 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
2678 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
2679#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
2680 {
2681 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2682 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2683 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002685
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002686 /* Whenever we send anything different from a
2687 * HelloRequest we should be in a handshake - double check. */
2688 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2689 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002690 ssl->handshake == NULL )
2691 {
2692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002697 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002698 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002700 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002701 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2702 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002703 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002704#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002705
Hanno Beckerb50a2532018-08-06 11:52:54 +01002706 /* Double-check that we did not exceed the bounds
2707 * of the outgoing record buffer.
2708 * This should never fail as the various message
2709 * writing functions must obey the bounds of the
2710 * outgoing record buffer, but better be safe.
2711 *
2712 * Note: We deliberately do not check for the MTU or MFL here.
2713 */
2714 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
2715 {
2716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002717 "size %" MBEDTLS_PRINTF_SIZET
2718 ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002719 ssl->out_msglen,
2720 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Beckerb50a2532018-08-06 11:52:54 +01002721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2722 }
2723
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002724 /*
2725 * Fill handshake headers
2726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002728 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002729 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
2730 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
2731 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002733 /*
2734 * DTLS has additional fields in the Handshake layer,
2735 * between the length field and the actual payload:
2736 * uint16 message_seq;
2737 * uint24 fragment_offset;
2738 * uint24 fragment_length;
2739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002742 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002743 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10002744 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01002745 {
2746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002747 "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +00002748 hs_len,
2749 (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01002750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2751 }
2752
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002753 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002754 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002755
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002756 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002757 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002758 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002759 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2760 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2761 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002762 }
2763 else
2764 {
2765 ssl->out_msg[4] = 0;
2766 ssl->out_msg[5] = 0;
2767 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002768
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002769 /* Handshake hashes are computed without fragmentation,
2770 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002771 memset( ssl->out_msg + 6, 0x00, 3 );
2772 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002773 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002775
Hanno Becker0207e532018-08-28 10:28:28 +01002776 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002777 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2778 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
2780
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002781 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002784 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2785 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002786 {
2787 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002790 return( ret );
2791 }
2792 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002793 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002794#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002795 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002796 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002797 {
2798 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2799 return( ret );
2800 }
2801 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002802
2803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2804
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002805 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002806}
2807
2808/*
2809 * Record layer functions
2810 */
2811
2812/*
2813 * Write current record.
2814 *
2815 * Uses:
2816 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2817 * - ssl->out_msglen: length of the record content (excl headers)
2818 * - ssl->out_msg: record content
2819 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002820int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002821{
2822 int ret, done = 0;
2823 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002824 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002825
2826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002829 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002830 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002831 {
2832 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002835 return( ret );
2836 }
2837
2838 len = ssl->out_msglen;
2839 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002840#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2843 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002847 ret = mbedtls_ssl_hw_record_write( ssl );
2848 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2851 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002852 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002853
2854 if( ret == 0 )
2855 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002856 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002857#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002858 if( !done )
2859 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002860 unsigned i;
2861 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002862#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2863 size_t out_buf_len = ssl->out_buf_len;
2864#else
2865 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2866#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002867 /* Skip writing the record content type to after the encryption,
2868 * as it may change when using the CID extension. */
2869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002870 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002871 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002872
Hanno Becker19859472018-08-06 09:40:20 +01002873 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002874 ssl->out_len[0] = (unsigned char)( len >> 8 );
2875 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002876
Paul Bakker48916f92012-09-16 19:57:18 +00002877 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002878 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002879 mbedtls_record rec;
2880
2881 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002882 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002883 rec.data_len = ssl->out_msglen;
2884 rec.data_offset = ssl->out_msg - rec.buf;
2885
2886 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2887 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2888 ssl->conf->transport, rec.ver );
2889 rec.type = ssl->out_msgtype;
2890
Hanno Beckera0e20d02019-05-15 14:03:01 +01002891#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002892 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002893 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002894#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002895
Hanno Beckera18d1322018-01-03 14:27:32 +00002896 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002897 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002899 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002900 return( ret );
2901 }
2902
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002903 if( rec.data_offset != 0 )
2904 {
2905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2906 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2907 }
2908
Hanno Becker6430faf2019-05-08 11:57:13 +01002909 /* Update the record content type and CID. */
2910 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002911#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002912 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002913#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002914 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002915 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
2916 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002917 }
2918
Hanno Becker5903de42019-05-03 14:46:38 +01002919 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002920
2921#if defined(MBEDTLS_SSL_PROTO_DTLS)
2922 /* In case of DTLS, double-check that we don't exceed
2923 * the remaining space in the datagram. */
2924 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2925 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002926 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002927 if( ret < 0 )
2928 return( ret );
2929
2930 if( protected_record_size > (size_t) ret )
2931 {
2932 /* Should never happen */
2933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2934 }
2935 }
2936#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002937
Hanno Becker6430faf2019-05-08 11:57:13 +01002938 /* Now write the potentially updated record content type. */
2939 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2940
Paul Elliott9f352112020-12-09 14:55:45 +00002941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002942 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002943 ssl->out_hdr[0], ssl->out_hdr[1],
2944 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002947 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002948
2949 ssl->out_left += protected_record_size;
2950 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002951 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002952
Hanno Beckerdd772292020-02-05 10:38:31 +00002953 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002954 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2955 break;
2956
2957 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002958 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002959 {
2960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2961 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 }
2964
Hanno Becker67bc7c32018-08-06 11:33:50 +01002965#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002966 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2967 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002968 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002969 size_t remaining;
2970 ret = ssl_get_remaining_payload_in_datagram( ssl );
2971 if( ret < 0 )
2972 {
2973 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2974 ret );
2975 return( ret );
2976 }
2977
2978 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002979 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002980 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002981 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002982 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002983 else
2984 {
Hanno Becker513815a2018-08-20 11:56:09 +01002985 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002986 }
2987 }
2988#endif /* MBEDTLS_SSL_PROTO_DTLS */
2989
2990 if( ( flush == SSL_FORCE_FLUSH ) &&
2991 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 return( ret );
2995 }
2996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
2999 return( 0 );
3000}
3001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003003
3004static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3005{
3006 if( ssl->in_msglen < ssl->in_hslen ||
3007 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3008 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3009 {
3010 return( 1 );
3011 }
3012 return( 0 );
3013}
Hanno Becker44650b72018-08-16 12:51:11 +01003014
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003015static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003016{
3017 return( ( ssl->in_msg[9] << 16 ) |
3018 ( ssl->in_msg[10] << 8 ) |
3019 ssl->in_msg[11] );
3020}
3021
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003022static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003023{
3024 return( ( ssl->in_msg[6] << 16 ) |
3025 ( ssl->in_msg[7] << 8 ) |
3026 ssl->in_msg[8] );
3027}
3028
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003029static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003030{
3031 uint32_t msg_len, frag_off, frag_len;
3032
3033 msg_len = ssl_get_hs_total_len( ssl );
3034 frag_off = ssl_get_hs_frag_off( ssl );
3035 frag_len = ssl_get_hs_frag_len( ssl );
3036
3037 if( frag_off > msg_len )
3038 return( -1 );
3039
3040 if( frag_len > msg_len - frag_off )
3041 return( -1 );
3042
3043 if( frag_len + 12 > ssl->in_msglen )
3044 return( -1 );
3045
3046 return( 0 );
3047}
3048
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003049/*
3050 * Mark bits in bitmask (used for DTLS HS reassembly)
3051 */
3052static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3053{
3054 unsigned int start_bits, end_bits;
3055
3056 start_bits = 8 - ( offset % 8 );
3057 if( start_bits != 8 )
3058 {
3059 size_t first_byte_idx = offset / 8;
3060
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003061 /* Special case */
3062 if( len <= start_bits )
3063 {
3064 for( ; len != 0; len-- )
3065 mask[first_byte_idx] |= 1 << ( start_bits - len );
3066
3067 /* Avoid potential issues with offset or len becoming invalid */
3068 return;
3069 }
3070
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003071 offset += start_bits; /* Now offset % 8 == 0 */
3072 len -= start_bits;
3073
3074 for( ; start_bits != 0; start_bits-- )
3075 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3076 }
3077
3078 end_bits = len % 8;
3079 if( end_bits != 0 )
3080 {
3081 size_t last_byte_idx = ( offset + len ) / 8;
3082
3083 len -= end_bits; /* Now len % 8 == 0 */
3084
3085 for( ; end_bits != 0; end_bits-- )
3086 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3087 }
3088
3089 memset( mask + offset / 8, 0xFF, len / 8 );
3090}
3091
3092/*
3093 * Check that bitmask is full
3094 */
3095static int ssl_bitmask_check( unsigned char *mask, size_t len )
3096{
3097 size_t i;
3098
3099 for( i = 0; i < len / 8; i++ )
3100 if( mask[i] != 0xFF )
3101 return( -1 );
3102
3103 for( i = 0; i < len % 8; i++ )
3104 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3105 return( -1 );
3106
3107 return( 0 );
3108}
3109
Hanno Becker56e205e2018-08-16 09:06:12 +01003110/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003111static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003112 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003113{
Hanno Becker56e205e2018-08-16 09:06:12 +01003114 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003115
Hanno Becker56e205e2018-08-16 09:06:12 +01003116 alloc_len = 12; /* Handshake header */
3117 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003118
Hanno Beckerd07df862018-08-16 09:14:58 +01003119 if( add_bitmap )
3120 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003121
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003122 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003123}
Hanno Becker56e205e2018-08-16 09:06:12 +01003124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003125#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003126
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003127static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003128{
3129 return( ( ssl->in_msg[1] << 16 ) |
3130 ( ssl->in_msg[2] << 8 ) |
3131 ssl->in_msg[3] );
3132}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003133
Simon Butcher99000142016-10-13 17:21:01 +01003134int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003137 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003139 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003141 }
3142
Hanno Becker12555c62018-08-16 12:47:53 +01003143 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00003146 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003147 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003150 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003151 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003153 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003154
Hanno Becker44650b72018-08-16 12:51:11 +01003155 if( ssl_check_hs_header( ssl ) != 0 )
3156 {
3157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3158 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3159 }
3160
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003161 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003162 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3163 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3164 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3165 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003166 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003167 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3168 {
3169 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3170 recv_msg_seq,
3171 ssl->handshake->in_msg_seq ) );
3172 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3173 }
3174
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003175 /* Retransmit only on last message from previous flight, to avoid
3176 * too many retransmissions.
3177 * Besides, No sane server ever retransmits HelloVerifyRequest */
3178 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00003182 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003183 recv_msg_seq,
3184 ssl->handshake->in_flight_start_seq ) );
3185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003189 return( ret );
3190 }
3191 }
3192 else
3193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00003195 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003196 recv_msg_seq,
3197 ssl->handshake->in_msg_seq ) );
3198 }
3199
Hanno Becker90333da2017-10-10 11:27:13 +01003200 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003201 }
3202 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003203
Hanno Becker6d97ef52018-08-16 13:09:04 +01003204 /* Message reassembly is handled alongside buffering of future
3205 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003206 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003207 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003208 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003211 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003212 }
3213 }
3214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003216 /* With TLS we don't handle fragmentation (for now) */
3217 if( ssl->in_msglen < ssl->in_hslen )
3218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3220 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003221 }
3222
Simon Butcher99000142016-10-13 17:21:01 +01003223 return( 0 );
3224}
3225
3226void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3227{
Hanno Becker0271f962018-08-16 13:23:47 +01003228 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003229
Hanno Becker0271f962018-08-16 13:23:47 +01003230 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003231 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003232 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003233 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003234
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003235 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003238 ssl->handshake != NULL )
3239 {
Hanno Becker0271f962018-08-16 13:23:47 +01003240 unsigned offset;
3241 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003242
Hanno Becker0271f962018-08-16 13:23:47 +01003243 /* Increment handshake sequence number */
3244 hs->in_msg_seq++;
3245
3246 /*
3247 * Clear up handshake buffering and reassembly structure.
3248 */
3249
3250 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003251 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003252
3253 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003254 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3255 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003256 offset++, hs_buf++ )
3257 {
3258 *hs_buf = *(hs_buf + 1);
3259 }
3260
3261 /* Create a fresh last entry */
3262 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003263 }
3264#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003265}
3266
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003267/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003268 * DTLS anti-replay: RFC 6347 4.1.2.6
3269 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003270 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3271 * Bit n is set iff record number in_window_top - n has been seen.
3272 *
3273 * Usually, in_window_top is the last record number seen and the lsb of
3274 * in_window is set. The only exception is the initial state (record number 0
3275 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003277#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003278void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003279{
3280 ssl->in_window_top = 0;
3281 ssl->in_window = 0;
3282}
3283
3284static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3285{
3286 return( ( (uint64_t) buf[0] << 40 ) |
3287 ( (uint64_t) buf[1] << 32 ) |
3288 ( (uint64_t) buf[2] << 24 ) |
3289 ( (uint64_t) buf[3] << 16 ) |
3290 ( (uint64_t) buf[4] << 8 ) |
3291 ( (uint64_t) buf[5] ) );
3292}
3293
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003294static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3295{
Janos Follath865b3eb2019-12-16 11:46:15 +00003296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003297 unsigned char *original_in_ctr;
3298
3299 // save original in_ctr
3300 original_in_ctr = ssl->in_ctr;
3301
3302 // use counter from record
3303 ssl->in_ctr = record_in_ctr;
3304
3305 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3306
3307 // restore the counter
3308 ssl->in_ctr = original_in_ctr;
3309
3310 return ret;
3311}
3312
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003313/*
3314 * Return 0 if sequence number is acceptable, -1 otherwise
3315 */
Hanno Becker0183d692019-07-12 08:50:37 +01003316int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003317{
3318 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3319 uint64_t bit;
3320
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003321 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003322 return( 0 );
3323
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003324 if( rec_seqnum > ssl->in_window_top )
3325 return( 0 );
3326
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003327 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003328
3329 if( bit >= 64 )
3330 return( -1 );
3331
3332 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3333 return( -1 );
3334
3335 return( 0 );
3336}
3337
3338/*
3339 * Update replay window on new validated record
3340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003342{
3343 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3344
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003345 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003346 return;
3347
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003348 if( rec_seqnum > ssl->in_window_top )
3349 {
3350 /* Update window_top and the contents of the window */
3351 uint64_t shift = rec_seqnum - ssl->in_window_top;
3352
3353 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003354 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003355 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003356 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003357 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003358 ssl->in_window |= 1;
3359 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003360
3361 ssl->in_window_top = rec_seqnum;
3362 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003363 else
3364 {
3365 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003366 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003367
3368 if( bit < 64 ) /* Always true, but be extra sure */
3369 ssl->in_window |= (uint64_t) 1 << bit;
3370 }
3371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003373
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003374#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003375/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003376 * Without any SSL context, check if a datagram looks like a ClientHello with
3377 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003378 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003379 *
3380 * - if cookie is valid, return 0
3381 * - if ClientHello looks superficially valid but cookie is not,
3382 * fill obuf and set olen, then
3383 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3384 * - otherwise return a specific error code
3385 */
3386static int ssl_check_dtls_clihlo_cookie(
3387 mbedtls_ssl_cookie_write_t *f_cookie_write,
3388 mbedtls_ssl_cookie_check_t *f_cookie_check,
3389 void *p_cookie,
3390 const unsigned char *cli_id, size_t cli_id_len,
3391 const unsigned char *in, size_t in_len,
3392 unsigned char *obuf, size_t buf_len, size_t *olen )
3393{
3394 size_t sid_len, cookie_len;
3395 unsigned char *p;
3396
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003397 /*
3398 * Structure of ClientHello with record and handshake headers,
3399 * and expected values. We don't need to check a lot, more checks will be
3400 * done when actually parsing the ClientHello - skipping those checks
3401 * avoids code duplication and does not make cookie forging any easier.
3402 *
3403 * 0-0 ContentType type; copied, must be handshake
3404 * 1-2 ProtocolVersion version; copied
3405 * 3-4 uint16 epoch; copied, must be 0
3406 * 5-10 uint48 sequence_number; copied
3407 * 11-12 uint16 length; (ignored)
3408 *
3409 * 13-13 HandshakeType msg_type; (ignored)
3410 * 14-16 uint24 length; (ignored)
3411 * 17-18 uint16 message_seq; copied
3412 * 19-21 uint24 fragment_offset; copied, must be 0
3413 * 22-24 uint24 fragment_length; (ignored)
3414 *
3415 * 25-26 ProtocolVersion client_version; (ignored)
3416 * 27-58 Random random; (ignored)
3417 * 59-xx SessionID session_id; 1 byte len + sid_len content
3418 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3419 * ...
3420 *
3421 * Minimum length is 61 bytes.
3422 */
3423 if( in_len < 61 ||
3424 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3425 in[3] != 0 || in[4] != 0 ||
3426 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3427 {
3428 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3429 }
3430
3431 sid_len = in[59];
3432 if( sid_len > in_len - 61 )
3433 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3434
3435 cookie_len = in[60 + sid_len];
3436 if( cookie_len > in_len - 60 )
3437 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3438
3439 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3440 cli_id, cli_id_len ) == 0 )
3441 {
3442 /* Valid cookie */
3443 return( 0 );
3444 }
3445
3446 /*
3447 * If we get here, we've got an invalid cookie, let's prepare HVR.
3448 *
3449 * 0-0 ContentType type; copied
3450 * 1-2 ProtocolVersion version; copied
3451 * 3-4 uint16 epoch; copied
3452 * 5-10 uint48 sequence_number; copied
3453 * 11-12 uint16 length; olen - 13
3454 *
3455 * 13-13 HandshakeType msg_type; hello_verify_request
3456 * 14-16 uint24 length; olen - 25
3457 * 17-18 uint16 message_seq; copied
3458 * 19-21 uint24 fragment_offset; copied
3459 * 22-24 uint24 fragment_length; olen - 25
3460 *
3461 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3462 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3463 *
3464 * Minimum length is 28.
3465 */
3466 if( buf_len < 28 )
3467 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3468
3469 /* Copy most fields and adapt others */
3470 memcpy( obuf, in, 25 );
3471 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3472 obuf[25] = 0xfe;
3473 obuf[26] = 0xff;
3474
3475 /* Generate and write actual cookie */
3476 p = obuf + 28;
3477 if( f_cookie_write( p_cookie,
3478 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3479 {
3480 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3481 }
3482
3483 *olen = p - obuf;
3484
3485 /* Go back and fill length fields */
3486 obuf[27] = (unsigned char)( *olen - 28 );
3487
3488 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
3489 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
3490 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
3491
3492 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
3493 obuf[12] = (unsigned char)( ( *olen - 13 ) );
3494
3495 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3496}
3497
3498/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003499 * Handle possible client reconnect with the same UDP quadruplet
3500 * (RFC 6347 Section 4.2.8).
3501 *
3502 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3503 * that looks like a ClientHello.
3504 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003505 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003506 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003507 * - if the input looks like a ClientHello with a valid cookie,
3508 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003509 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003510 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003511 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003512 * This function is called (through ssl_check_client_reconnect()) when an
3513 * unexpected record is found in ssl_get_next_record(), which will discard the
3514 * record if we return 0, and bubble up the return value otherwise (this
3515 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3516 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003517 */
3518static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3519{
Janos Follath865b3eb2019-12-16 11:46:15 +00003520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003521 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003522
Hanno Becker2fddd372019-07-10 14:37:41 +01003523 if( ssl->conf->f_cookie_write == NULL ||
3524 ssl->conf->f_cookie_check == NULL )
3525 {
3526 /* If we can't use cookies to verify reachability of the peer,
3527 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3529 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003530 return( 0 );
3531 }
3532
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003533 ret = ssl_check_dtls_clihlo_cookie(
3534 ssl->conf->f_cookie_write,
3535 ssl->conf->f_cookie_check,
3536 ssl->conf->p_cookie,
3537 ssl->cli_id, ssl->cli_id_len,
3538 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003539 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003540
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003541 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3542
3543 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003544 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003545 int send_ret;
3546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3547 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3548 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003549 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003550 * If the error is permanent we'll catch it later,
3551 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003552 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3553 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3554 (void) send_ret;
3555
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003556 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003557 }
3558
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003559 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003560 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003562 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003563 {
3564 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3565 return( ret );
3566 }
3567
3568 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003569 }
3570
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003571 return( ret );
3572}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003573#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003574
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003575static int ssl_check_record_type( uint8_t record_type )
3576{
3577 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3578 record_type != MBEDTLS_SSL_MSG_ALERT &&
3579 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3580 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3581 {
3582 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3583 }
3584
3585 return( 0 );
3586}
3587
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003588/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003589 * ContentType type;
3590 * ProtocolVersion version;
3591 * uint16 epoch; // DTLS only
3592 * uint48 sequence_number; // DTLS only
3593 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003594 *
3595 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003596 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003597 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3598 *
3599 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003600 * 1. proceed with the record if this function returns 0
3601 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3602 * 3. return CLIENT_RECONNECT if this function return that value
3603 * 4. drop the whole datagram if this function returns anything else.
3604 * Point 2 is needed when the peer is resending, and we have already received
3605 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003606 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003607static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003608 unsigned char *buf,
3609 size_t len,
3610 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003611{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003612 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
Hanno Beckere5e7e782019-07-11 12:29:35 +01003614 size_t const rec_hdr_type_offset = 0;
3615 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003616
Hanno Beckere5e7e782019-07-11 12:29:35 +01003617 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3618 rec_hdr_type_len;
3619 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
Hanno Beckere5e7e782019-07-11 12:29:35 +01003621 size_t const rec_hdr_ctr_len = 8;
3622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003623 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003624 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3625 rec_hdr_version_len;
3626
Hanno Beckera0e20d02019-05-15 14:03:01 +01003627#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003628 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3629 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003630 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003631#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3632#endif /* MBEDTLS_SSL_PROTO_DTLS */
3633
3634 size_t rec_hdr_len_offset; /* To be determined */
3635 size_t const rec_hdr_len_len = 2;
3636
3637 /*
3638 * Check minimum lengths for record header.
3639 */
3640
3641#if defined(MBEDTLS_SSL_PROTO_DTLS)
3642 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3643 {
3644 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3645 }
3646 else
3647#endif /* MBEDTLS_SSL_PROTO_DTLS */
3648 {
3649 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3650 }
3651
3652 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3653 {
3654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3655 (unsigned) len,
3656 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3657 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3658 }
3659
3660 /*
3661 * Parse and validate record content type
3662 */
3663
3664 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003665
3666 /* Check record content type */
3667#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3668 rec->cid_len = 0;
3669
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003671 ssl->conf->cid_len != 0 &&
3672 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003673 {
3674 /* Shift pointers to account for record header including CID
3675 * struct {
3676 * ContentType special_type = tls12_cid;
3677 * ProtocolVersion version;
3678 * uint16 epoch;
3679 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003680 * opaque cid[cid_length]; // Additional field compared to
3681 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003682 * uint16 length;
3683 * opaque enc_content[DTLSCiphertext.length];
3684 * } DTLSCiphertext;
3685 */
3686
3687 /* So far, we only support static CID lengths
3688 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003689 rec_hdr_cid_len = ssl->conf->cid_len;
3690 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003691
Hanno Beckere5e7e782019-07-11 12:29:35 +01003692 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003693 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3695 (unsigned) len,
3696 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003697 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003698 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003699
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003700 /* configured CID len is guaranteed at most 255, see
3701 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3702 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003703 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003704 }
3705 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003706#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003707 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003708 if( ssl_check_record_type( rec->type ) )
3709 {
Hanno Becker54229812019-07-12 14:40:00 +01003710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3711 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003712 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3713 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003714 }
3715
Hanno Beckere5e7e782019-07-11 12:29:35 +01003716 /*
3717 * Parse and validate record version
3718 */
3719
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003720 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3721 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003722 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3723 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003724 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003725
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003726 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3729 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003730 }
3731
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003732 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003734 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3735 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 }
3737
Hanno Beckere5e7e782019-07-11 12:29:35 +01003738 /*
3739 * Parse/Copy record sequence number.
3740 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003741
Hanno Beckere5e7e782019-07-11 12:29:35 +01003742#if defined(MBEDTLS_SSL_PROTO_DTLS)
3743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003744 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003745 /* Copy explicit record sequence number from input buffer. */
3746 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3747 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003748 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003749 else
3750#endif /* MBEDTLS_SSL_PROTO_DTLS */
3751 {
3752 /* Copy implicit record sequence number from SSL context structure. */
3753 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3754 }
Paul Bakker40e46942009-01-03 21:51:57 +00003755
Hanno Beckere5e7e782019-07-11 12:29:35 +01003756 /*
3757 * Parse record length.
3758 */
3759
Hanno Beckere5e7e782019-07-11 12:29:35 +01003760 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003761 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3762 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003763 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
Paul Elliott9f352112020-12-09 14:55:45 +00003765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003766 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003767 rec->type,
3768 major_ver, minor_ver, rec->data_len ) );
3769
3770 rec->buf = buf;
3771 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003772
Hanno Beckerd417cc92019-07-26 08:20:27 +01003773 if( rec->data_len == 0 )
3774 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003775
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003776 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003777 * DTLS-related tests.
3778 * Check epoch before checking length constraint because
3779 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3780 * message gets duplicated before the corresponding Finished message,
3781 * the second ChangeCipherSpec should be discarded because it belongs
3782 * to an old epoch, but not because its length is shorter than
3783 * the minimum record length for packets using the new record transform.
3784 * Note that these two kinds of failures are handled differently,
3785 * as an unexpected record is silently skipped but an invalid
3786 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003787 */
3788#if defined(MBEDTLS_SSL_PROTO_DTLS)
3789 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3790 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003791 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003792
Hanno Becker955a5c92019-07-10 17:12:07 +01003793 /* Check that the datagram is large enough to contain a record
3794 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003795 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003796 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3798 (unsigned) len,
3799 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003800 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3801 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003802
Hanno Becker37cfe732019-07-10 17:20:01 +01003803 /* Records from other, non-matching epochs are silently discarded.
3804 * (The case of same-port Client reconnects must be considered in
3805 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003806 if( rec_epoch != ssl->in_epoch )
3807 {
3808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003809 "expected %u, received %lu",
3810 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003811
Hanno Becker552f7472019-07-19 10:59:12 +01003812 /* Records from the next epoch are considered for buffering
3813 * (concretely: early Finished messages). */
3814 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003815 {
Hanno Becker552f7472019-07-19 10:59:12 +01003816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3817 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003818 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003819
Hanno Becker2fddd372019-07-10 14:37:41 +01003820 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003821 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003822#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003823 /* For records from the correct epoch, check whether their
3824 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003825 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3826 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003827 {
3828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3829 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3830 }
3831#endif
3832 }
3833#endif /* MBEDTLS_SSL_PROTO_DTLS */
3834
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003835 return( 0 );
3836}
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
Hanno Becker2fddd372019-07-10 14:37:41 +01003839#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3840static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3841{
3842 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3843
3844 /*
3845 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3846 * access the first byte of record content (handshake type), as we
3847 * have an active transform (possibly iv_len != 0), so use the
3848 * fact that the record header len is 13 instead.
3849 */
3850 if( rec_epoch == 0 &&
3851 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3852 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3853 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3854 ssl->in_left > 13 &&
3855 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3856 {
3857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3858 "from the same port" ) );
3859 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 }
3861
3862 return( 0 );
3863}
Hanno Becker2fddd372019-07-10 14:37:41 +01003864#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003865
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003866/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003867 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003868 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003869static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3870 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003871{
3872 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003875 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003877#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3878 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 ret = mbedtls_ssl_hw_record_read( ssl );
3883 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3886 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003887 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003888
3889 if( ret == 0 )
3890 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003891 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003893 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003894 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003895 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003896
Hanno Beckera18d1322018-01-03 14:27:32 +00003897 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003898 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003900 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003901
Hanno Beckera0e20d02019-05-15 14:03:01 +01003902#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003903 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3904 ssl->conf->ignore_unexpected_cid
3905 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3906 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003907 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003908 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003909 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003910#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003911
Paul Bakker5121ce52009-01-03 21:22:43 +00003912 return( ret );
3913 }
3914
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003915 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003916 {
3917 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003918 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003919 }
3920
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003921 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003922 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003923
Hanno Beckera0e20d02019-05-15 14:03:01 +01003924#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003925 /* We have already checked the record content type
3926 * in ssl_parse_record_header(), failing or silently
3927 * dropping the record in the case of an unknown type.
3928 *
3929 * Since with the use of CIDs, the record content type
3930 * might change during decryption, re-check the record
3931 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003932 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003933 {
3934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3935 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3936 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003937#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003938
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003939 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003940 {
3941#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3942 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003943 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003944 {
3945 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3947 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3948 }
3949#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3950
3951 ssl->nb_zero++;
3952
3953 /*
3954 * Three or more empty messages may be a DoS attack
3955 * (excessive CPU consumption).
3956 */
3957 if( ssl->nb_zero > 3 )
3958 {
3959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003960 "messages, possible DoS attack" ) );
3961 /* Treat the records as if they were not properly authenticated,
3962 * thereby failing the connection if we see more than allowed
3963 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003964 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3965 }
3966 }
3967 else
3968 ssl->nb_zero = 0;
3969
3970#if defined(MBEDTLS_SSL_PROTO_DTLS)
3971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3972 {
3973 ; /* in_ctr read from peer, not maintained internally */
3974 }
3975 else
3976#endif
3977 {
3978 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003979 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003980 if( ++ssl->in_ctr[i - 1] != 0 )
3981 break;
3982
3983 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003984 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003985 {
3986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3987 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3988 }
3989 }
3990
Paul Bakker5121ce52009-01-03 21:22:43 +00003991 }
3992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003994 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003996 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003997 }
3998#endif
3999
Hanno Beckerd96e10b2019-07-09 17:30:02 +01004000 /* Check actual (decrypted) record content length against
4001 * configured maximum. */
4002 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4003 {
4004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4005 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4006 }
4007
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004008 return( 0 );
4009}
4010
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004011/*
4012 * Read a record.
4013 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004014 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4015 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4016 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004017 */
Hanno Becker1097b342018-08-15 14:09:41 +01004018
4019/* Helper functions for mbedtls_ssl_read_record(). */
4020static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004021static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4022static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004023
Hanno Becker327c93b2018-08-15 13:56:18 +01004024int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004025 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004026{
Janos Follath865b3eb2019-12-16 11:46:15 +00004027 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004030
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004031 if( ssl->keep_current_message == 0 )
4032 {
4033 do {
Simon Butcher99000142016-10-13 17:21:01 +01004034
Hanno Becker26994592018-08-15 14:14:59 +01004035 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004036 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004037 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004038
Hanno Beckere74d5562018-08-15 14:26:08 +01004039 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004040 {
Hanno Becker40f50842018-08-15 14:48:01 +01004041#if defined(MBEDTLS_SSL_PROTO_DTLS)
4042 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004043
Hanno Becker40f50842018-08-15 14:48:01 +01004044 /* We only check for buffered messages if the
4045 * current datagram is fully consumed. */
4046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004047 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004048 {
Hanno Becker40f50842018-08-15 14:48:01 +01004049 if( ssl_load_buffered_message( ssl ) == 0 )
4050 have_buffered = 1;
4051 }
4052
4053 if( have_buffered == 0 )
4054#endif /* MBEDTLS_SSL_PROTO_DTLS */
4055 {
4056 ret = ssl_get_next_record( ssl );
4057 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4058 continue;
4059
4060 if( ret != 0 )
4061 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004062 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004063 return( ret );
4064 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004065 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004066 }
4067
4068 ret = mbedtls_ssl_handle_message_type( ssl );
4069
Hanno Becker40f50842018-08-15 14:48:01 +01004070#if defined(MBEDTLS_SSL_PROTO_DTLS)
4071 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4072 {
4073 /* Buffer future message */
4074 ret = ssl_buffer_message( ssl );
4075 if( ret != 0 )
4076 return( ret );
4077
4078 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4079 }
4080#endif /* MBEDTLS_SSL_PROTO_DTLS */
4081
Hanno Becker90333da2017-10-10 11:27:13 +01004082 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4083 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004084
4085 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004086 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004087 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004088 return( ret );
4089 }
4090
Hanno Becker327c93b2018-08-15 13:56:18 +01004091 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004092 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004093 {
4094 mbedtls_ssl_update_handshake_status( ssl );
4095 }
Simon Butcher99000142016-10-13 17:21:01 +01004096 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004097 else
Simon Butcher99000142016-10-13 17:21:01 +01004098 {
Hanno Becker02f59072018-08-15 14:00:24 +01004099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004100 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004101 }
4102
4103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4104
4105 return( 0 );
4106}
4107
Hanno Becker40f50842018-08-15 14:48:01 +01004108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004109static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004110{
Hanno Becker40f50842018-08-15 14:48:01 +01004111 if( ssl->in_left > ssl->next_record_offset )
4112 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004113
Hanno Becker40f50842018-08-15 14:48:01 +01004114 return( 0 );
4115}
4116
4117static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4118{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004119 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004120 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004121 int ret = 0;
4122
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004123 if( hs == NULL )
4124 return( -1 );
4125
Hanno Beckere00ae372018-08-20 09:39:42 +01004126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4127
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004128 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4129 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4130 {
4131 /* Check if we have seen a ChangeCipherSpec before.
4132 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004133 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004134 {
4135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4136 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004137 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004138 }
4139
Hanno Becker39b8bc92018-08-28 17:17:13 +01004140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004141 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4142 ssl->in_msglen = 1;
4143 ssl->in_msg[0] = 1;
4144
4145 /* As long as they are equal, the exact value doesn't matter. */
4146 ssl->in_left = 0;
4147 ssl->next_record_offset = 0;
4148
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004149 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004150 goto exit;
4151 }
Hanno Becker37f95322018-08-16 13:55:32 +01004152
Hanno Beckerb8f50142018-08-28 10:01:34 +01004153#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004154 /* Debug only */
4155 {
4156 unsigned offset;
4157 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4158 {
4159 hs_buf = &hs->buffering.hs[offset];
4160 if( hs_buf->is_valid == 1 )
4161 {
4162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4163 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004164 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004165 }
4166 }
4167 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004168#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004169
4170 /* Check if we have buffered and/or fully reassembled the
4171 * next handshake message. */
4172 hs_buf = &hs->buffering.hs[0];
4173 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4174 {
4175 /* Synthesize a record containing the buffered HS message. */
4176 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4177 ( hs_buf->data[2] << 8 ) |
4178 hs_buf->data[3];
4179
4180 /* Double-check that we haven't accidentally buffered
4181 * a message that doesn't fit into the input buffer. */
4182 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4183 {
4184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4186 }
4187
4188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4189 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4190 hs_buf->data, msg_len + 12 );
4191
4192 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4193 ssl->in_hslen = msg_len + 12;
4194 ssl->in_msglen = msg_len + 12;
4195 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4196
4197 ret = 0;
4198 goto exit;
4199 }
4200 else
4201 {
4202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4203 hs->in_msg_seq ) );
4204 }
4205
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004206 ret = -1;
4207
4208exit:
4209
4210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4211 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004212}
4213
Hanno Beckera02b0b42018-08-21 17:20:27 +01004214static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4215 size_t desired )
4216{
4217 int offset;
4218 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4220 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004221
Hanno Becker01315ea2018-08-21 17:22:17 +01004222 /* Get rid of future records epoch first, if such exist. */
4223 ssl_free_buffered_record( ssl );
4224
4225 /* Check if we have enough space available now. */
4226 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4227 hs->buffering.total_bytes_buffered ) )
4228 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004229 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004230 return( 0 );
4231 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004232
Hanno Becker4f432ad2018-08-28 10:02:32 +01004233 /* We don't have enough space to buffer the next expected handshake
4234 * message. Remove buffers used for future messages to gain space,
4235 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004236 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4237 offset >= 0; offset-- )
4238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4240 offset ) );
4241
Hanno Beckerb309b922018-08-23 13:18:05 +01004242 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004243
4244 /* Check if we have enough space available now. */
4245 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4246 hs->buffering.total_bytes_buffered ) )
4247 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004248 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004249 return( 0 );
4250 }
4251 }
4252
4253 return( -1 );
4254}
4255
Hanno Becker40f50842018-08-15 14:48:01 +01004256static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4257{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004258 int ret = 0;
4259 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4260
4261 if( hs == NULL )
4262 return( 0 );
4263
4264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4265
4266 switch( ssl->in_msgtype )
4267 {
4268 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004270
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004271 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004272 break;
4273
4274 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004275 {
4276 unsigned recv_msg_seq_offset;
4277 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4278 mbedtls_ssl_hs_buffer *hs_buf;
4279 size_t msg_len = ssl->in_hslen - 12;
4280
4281 /* We should never receive an old handshake
4282 * message - double-check nonetheless. */
4283 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4284 {
4285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4286 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4287 }
4288
4289 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4290 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4291 {
4292 /* Silently ignore -- message too far in the future */
4293 MBEDTLS_SSL_DEBUG_MSG( 2,
4294 ( "Ignore future HS message with sequence number %u, "
4295 "buffering window %u - %u",
4296 recv_msg_seq, ssl->handshake->in_msg_seq,
4297 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4298
4299 goto exit;
4300 }
4301
4302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4303 recv_msg_seq, recv_msg_seq_offset ) );
4304
4305 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4306
4307 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004308 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004309 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004310 size_t reassembly_buf_sz;
4311
Hanno Becker37f95322018-08-16 13:55:32 +01004312 hs_buf->is_fragmented =
4313 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4314
4315 /* We copy the message back into the input buffer
4316 * after reassembly, so check that it's not too large.
4317 * This is an implementation-specific limitation
4318 * and not one from the standard, hence it is not
4319 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004320 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004321 {
4322 /* Ignore message */
4323 goto exit;
4324 }
4325
Hanno Beckere0b150f2018-08-21 15:51:03 +01004326 /* Check if we have enough space to buffer the message. */
4327 if( hs->buffering.total_bytes_buffered >
4328 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4329 {
4330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4332 }
4333
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004334 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4335 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004336
4337 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4338 hs->buffering.total_bytes_buffered ) )
4339 {
4340 if( recv_msg_seq_offset > 0 )
4341 {
4342 /* If we can't buffer a future message because
4343 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4345 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4346 " (already %" MBEDTLS_PRINTF_SIZET
4347 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004348 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004349 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004350 goto exit;
4351 }
Hanno Beckere1801392018-08-21 16:51:05 +01004352 else
4353 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4355 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4356 " (already %" MBEDTLS_PRINTF_SIZET
4357 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004358 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004359 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01004360 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004361
Hanno Beckera02b0b42018-08-21 17:20:27 +01004362 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004363 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
4365 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
4366 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
4367 " (already %" MBEDTLS_PRINTF_SIZET
4368 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00004369 msg_len,
4370 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00004371 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004372 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004373 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4374 goto exit;
4375 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004376 }
4377
Paul Elliottd48d5c62021-01-07 14:47:05 +00004378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004379 msg_len ) );
4380
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004381 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4382 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004383 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004384 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004385 goto exit;
4386 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004387 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004388
4389 /* Prepare final header: copy msg_type, length and message_seq,
4390 * then add standardised fragment_offset and fragment_length */
4391 memcpy( hs_buf->data, ssl->in_msg, 6 );
4392 memset( hs_buf->data + 6, 0, 3 );
4393 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4394
4395 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004396
4397 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004398 }
4399 else
4400 {
4401 /* Make sure msg_type and length are consistent */
4402 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4403 {
4404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4405 /* Ignore */
4406 goto exit;
4407 }
4408 }
4409
Hanno Becker4422bbb2018-08-20 09:40:19 +01004410 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004411 {
4412 size_t frag_len, frag_off;
4413 unsigned char * const msg = hs_buf->data + 12;
4414
4415 /*
4416 * Check and copy current fragment
4417 */
4418
4419 /* Validation of header fields already done in
4420 * mbedtls_ssl_prepare_handshake_record(). */
4421 frag_off = ssl_get_hs_frag_off( ssl );
4422 frag_len = ssl_get_hs_frag_len( ssl );
4423
Paul Elliottd48d5c62021-01-07 14:47:05 +00004424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4425 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004426 frag_off, frag_len ) );
4427 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4428
4429 if( hs_buf->is_fragmented )
4430 {
4431 unsigned char * const bitmask = msg + msg_len;
4432 ssl_bitmask_set( bitmask, frag_off, frag_len );
4433 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4434 msg_len ) == 0 );
4435 }
4436 else
4437 {
4438 hs_buf->is_complete = 1;
4439 }
4440
4441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4442 hs_buf->is_complete ? "" : "not yet " ) );
4443 }
4444
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004445 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004446 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004447
4448 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004449 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004450 break;
4451 }
4452
4453exit:
4454
4455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4456 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004457}
4458#endif /* MBEDTLS_SSL_PROTO_DTLS */
4459
Hanno Becker1097b342018-08-15 14:09:41 +01004460static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004461{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004462 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004463 * Consume last content-layer message and potentially
4464 * update in_msglen which keeps track of the contents'
4465 * consumption state.
4466 *
4467 * (1) Handshake messages:
4468 * Remove last handshake message, move content
4469 * and adapt in_msglen.
4470 *
4471 * (2) Alert messages:
4472 * Consume whole record content, in_msglen = 0.
4473 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004474 * (3) Change cipher spec:
4475 * Consume whole record content, in_msglen = 0.
4476 *
4477 * (4) Application data:
4478 * Don't do anything - the record layer provides
4479 * the application data as a stream transport
4480 * and consumes through mbedtls_ssl_read only.
4481 *
4482 */
4483
4484 /* Case (1): Handshake messages */
4485 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004486 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004487 /* Hard assertion to be sure that no application data
4488 * is in flight, as corrupting ssl->in_msglen during
4489 * ssl->in_offt != NULL is fatal. */
4490 if( ssl->in_offt != NULL )
4491 {
4492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4493 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4494 }
4495
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004496 /*
4497 * Get next Handshake message in the current record
4498 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004499
Hanno Becker4a810fb2017-05-24 16:27:30 +01004500 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004501 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004502 * current handshake content: If DTLS handshake
4503 * fragmentation is used, that's the fragment
4504 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004505 * size here is faulty and should be changed at
4506 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004507 * (2) While it doesn't seem to cause problems, one
4508 * has to be very careful not to assume that in_hslen
4509 * is always <= in_msglen in a sensible communication.
4510 * Again, it's wrong for DTLS handshake fragmentation.
4511 * The following check is therefore mandatory, and
4512 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004513 * Additionally, ssl->in_hslen might be arbitrarily out of
4514 * bounds after handling a DTLS message with an unexpected
4515 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004516 */
4517 if( ssl->in_hslen < ssl->in_msglen )
4518 {
4519 ssl->in_msglen -= ssl->in_hslen;
4520 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4521 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004522
Hanno Becker4a810fb2017-05-24 16:27:30 +01004523 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4524 ssl->in_msg, ssl->in_msglen );
4525 }
4526 else
4527 {
4528 ssl->in_msglen = 0;
4529 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004530
Hanno Becker4a810fb2017-05-24 16:27:30 +01004531 ssl->in_hslen = 0;
4532 }
4533 /* Case (4): Application data */
4534 else if( ssl->in_offt != NULL )
4535 {
4536 return( 0 );
4537 }
4538 /* Everything else (CCS & Alerts) */
4539 else
4540 {
4541 ssl->in_msglen = 0;
4542 }
4543
Hanno Becker1097b342018-08-15 14:09:41 +01004544 return( 0 );
4545}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004546
Hanno Beckere74d5562018-08-15 14:26:08 +01004547static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4548{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004549 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004550 return( 1 );
4551
4552 return( 0 );
4553}
4554
Hanno Becker5f066e72018-08-16 14:56:31 +01004555#if defined(MBEDTLS_SSL_PROTO_DTLS)
4556
4557static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4558{
4559 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4560 if( hs == NULL )
4561 return;
4562
Hanno Becker01315ea2018-08-21 17:22:17 +01004563 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004564 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004565 hs->buffering.total_bytes_buffered -=
4566 hs->buffering.future_record.len;
4567
4568 mbedtls_free( hs->buffering.future_record.data );
4569 hs->buffering.future_record.data = NULL;
4570 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004571}
4572
4573static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4574{
4575 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4576 unsigned char * rec;
4577 size_t rec_len;
4578 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004579#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4580 size_t in_buf_len = ssl->in_buf_len;
4581#else
4582 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4583#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004584 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4585 return( 0 );
4586
4587 if( hs == NULL )
4588 return( 0 );
4589
Hanno Becker5f066e72018-08-16 14:56:31 +01004590 rec = hs->buffering.future_record.data;
4591 rec_len = hs->buffering.future_record.len;
4592 rec_epoch = hs->buffering.future_record.epoch;
4593
4594 if( rec == NULL )
4595 return( 0 );
4596
Hanno Becker4cb782d2018-08-20 11:19:05 +01004597 /* Only consider loading future records if the
4598 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004599 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004600 return( 0 );
4601
Hanno Becker5f066e72018-08-16 14:56:31 +01004602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4603
4604 if( rec_epoch != ssl->in_epoch )
4605 {
4606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4607 goto exit;
4608 }
4609
4610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4611
4612 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004613 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004614 {
4615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4617 }
4618
4619 memcpy( ssl->in_hdr, rec, rec_len );
4620 ssl->in_left = rec_len;
4621 ssl->next_record_offset = 0;
4622
4623 ssl_free_buffered_record( ssl );
4624
4625exit:
4626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4627 return( 0 );
4628}
4629
Hanno Becker519f15d2019-07-11 12:43:20 +01004630static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4631 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004632{
4633 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004634
4635 /* Don't buffer future records outside handshakes. */
4636 if( hs == NULL )
4637 return( 0 );
4638
4639 /* Only buffer handshake records (we are only interested
4640 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004641 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004642 return( 0 );
4643
4644 /* Don't buffer more than one future epoch record. */
4645 if( hs->buffering.future_record.data != NULL )
4646 return( 0 );
4647
Hanno Becker01315ea2018-08-21 17:22:17 +01004648 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004649 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004650 hs->buffering.total_bytes_buffered ) )
4651 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4653 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4654 " (already %" MBEDTLS_PRINTF_SIZET
4655 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004656 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004657 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004658 return( 0 );
4659 }
4660
Hanno Becker5f066e72018-08-16 14:56:31 +01004661 /* Buffer record */
4662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004663 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004664 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004665
4666 /* ssl_parse_record_header() only considers records
4667 * of the next epoch as candidates for buffering. */
4668 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004669 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004670
4671 hs->buffering.future_record.data =
4672 mbedtls_calloc( 1, hs->buffering.future_record.len );
4673 if( hs->buffering.future_record.data == NULL )
4674 {
4675 /* If we run out of RAM trying to buffer a
4676 * record from the next epoch, just ignore. */
4677 return( 0 );
4678 }
4679
Hanno Becker519f15d2019-07-11 12:43:20 +01004680 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004681
Hanno Becker519f15d2019-07-11 12:43:20 +01004682 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004683 return( 0 );
4684}
4685
4686#endif /* MBEDTLS_SSL_PROTO_DTLS */
4687
Hanno Beckere74d5562018-08-15 14:26:08 +01004688static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004689{
Janos Follath865b3eb2019-12-16 11:46:15 +00004690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004691 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004692
Hanno Becker5f066e72018-08-16 14:56:31 +01004693#if defined(MBEDTLS_SSL_PROTO_DTLS)
4694 /* We might have buffered a future record; if so,
4695 * and if the epoch matches now, load it.
4696 * On success, this call will set ssl->in_left to
4697 * the length of the buffered record, so that
4698 * the calls to ssl_fetch_input() below will
4699 * essentially be no-ops. */
4700 ret = ssl_load_buffered_record( ssl );
4701 if( ret != 0 )
4702 return( ret );
4703#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004704
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004705 /* Ensure that we have enough space available for the default form
4706 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4707 * with no space for CIDs counted in). */
4708 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4709 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004711 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004712 return( ret );
4713 }
4714
Hanno Beckere5e7e782019-07-11 12:29:35 +01004715 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4716 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004718#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004719 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004720 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004721 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4722 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004723 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004724 if( ret != 0 )
4725 return( ret );
4726
4727 /* Fall through to handling of unexpected records */
4728 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4729 }
4730
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004731 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4732 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004733#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004734 /* Reset in pointers to default state for TLS/DTLS records,
4735 * assuming no CID and no offset between record content and
4736 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004737 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004738
Hanno Becker7ae20e02019-07-12 08:33:49 +01004739 /* Setup internal message pointers from record structure. */
4740 ssl->in_msgtype = rec.type;
4741#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4742 ssl->in_len = ssl->in_cid + rec.cid_len;
4743#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4744 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4745 ssl->in_msglen = rec.data_len;
4746
Hanno Becker2fddd372019-07-10 14:37:41 +01004747 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004748 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004749 if( ret != 0 )
4750 return( ret );
4751#endif
4752
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004753 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004754 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004755
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4757 "(header)" ) );
4758 }
4759 else
4760 {
4761 /* Skip invalid record and the rest of the datagram */
4762 ssl->next_record_offset = 0;
4763 ssl->in_left = 0;
4764
4765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4766 "(header)" ) );
4767 }
4768
4769 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004770 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004771 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004772 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004773#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004774 {
4775 return( ret );
4776 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004777 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004781 {
Hanno Beckera8814792019-07-10 15:01:45 +01004782 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004783 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004784 if( ssl->next_record_offset < ssl->in_left )
4785 {
4786 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4787 }
4788 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004789 else
4790#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004791 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004792 /*
4793 * Fetch record contents from underlying transport.
4794 */
Hanno Beckera3175662019-07-11 12:50:29 +01004795 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004796 if( ret != 0 )
4797 {
4798 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4799 return( ret );
4800 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004802 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004803 }
4804
4805 /*
4806 * Decrypt record contents.
4807 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004808
Hanno Beckerfdf66042019-07-11 13:07:45 +01004809 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004812 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004813 {
4814 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004815 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004816 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004817 /* Except when waiting for Finished as a bad mac here
4818 * probably means something went wrong in the handshake
4819 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4820 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4821 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4822 {
4823#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4824 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4825 {
4826 mbedtls_ssl_send_alert_message( ssl,
4827 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4828 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4829 }
4830#endif
4831 return( ret );
4832 }
4833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004835 if( ssl->conf->badmac_limit != 0 &&
4836 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4839 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004840 }
4841#endif
4842
Hanno Becker4a810fb2017-05-24 16:27:30 +01004843 /* As above, invalid records cause
4844 * dismissal of the whole datagram. */
4845
4846 ssl->next_record_offset = 0;
4847 ssl->in_left = 0;
4848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004850 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004851 }
4852
4853 return( ret );
4854 }
4855 else
4856#endif
4857 {
4858 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4860 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004862 mbedtls_ssl_send_alert_message( ssl,
4863 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4864 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004865 }
4866#endif
4867 return( ret );
4868 }
4869 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004870
Hanno Becker44d89b22019-07-12 09:40:44 +01004871
4872 /* Reset in pointers to default state for TLS/DTLS records,
4873 * assuming no CID and no offset between record content and
4874 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004875 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004876#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4877 ssl->in_len = ssl->in_cid + rec.cid_len;
4878#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004879 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004880
Hanno Becker8685c822019-07-12 09:37:30 +01004881 /* The record content type may change during decryption,
4882 * so re-read it. */
4883 ssl->in_msgtype = rec.type;
4884 /* Also update the input buffer, because unfortunately
4885 * the server-side ssl_parse_client_hello() reparses the
4886 * record header when receiving a ClientHello initiating
4887 * a renegotiation. */
4888 ssl->in_hdr[0] = rec.type;
4889 ssl->in_msg = rec.buf + rec.data_offset;
4890 ssl->in_msglen = rec.data_len;
4891 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
4892 ssl->in_len[1] = (unsigned char)( rec.data_len );
4893
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004894#if defined(MBEDTLS_ZLIB_SUPPORT)
4895 if( ssl->transform_in != NULL &&
4896 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4897 {
4898 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4899 {
4900 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4901 return( ret );
4902 }
4903
4904 /* Check actual (decompress) record content length against
4905 * configured maximum. */
4906 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4907 {
4908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4909 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4910 }
4911 }
4912#endif /* MBEDTLS_ZLIB_SUPPORT */
4913
Simon Butcher99000142016-10-13 17:21:01 +01004914 return( 0 );
4915}
4916
4917int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4918{
Janos Follath865b3eb2019-12-16 11:46:15 +00004919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004920
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004921 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004922 * Handle particular types of records
4923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 {
Simon Butcher99000142016-10-13 17:21:01 +01004926 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4927 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004928 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004930 }
4931
Hanno Beckere678eaa2018-08-21 14:57:46 +01004932 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004933 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004934 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004935 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004937 ssl->in_msglen ) );
4938 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004939 }
4940
Hanno Beckere678eaa2018-08-21 14:57:46 +01004941 if( ssl->in_msg[0] != 1 )
4942 {
4943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4944 ssl->in_msg[0] ) );
4945 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4946 }
4947
4948#if defined(MBEDTLS_SSL_PROTO_DTLS)
4949 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4950 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4951 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4952 {
4953 if( ssl->handshake == NULL )
4954 {
4955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4956 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4957 }
4958
4959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4960 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4961 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004962#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004963 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004965 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004966 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004967 if( ssl->in_msglen != 2 )
4968 {
4969 /* Note: Standard allows for more than one 2 byte alert
4970 to be packed in a single message, but Mbed TLS doesn't
4971 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004973 ssl->in_msglen ) );
4974 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4975 }
4976
Paul Elliott9f352112020-12-09 14:55:45 +00004977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004978 ssl->in_msg[0], ssl->in_msg[1] ) );
4979
4980 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004981 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004982 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004983 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004984 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004986 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004987 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004988 }
4989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004990 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4991 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4994 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004995 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004996
4997#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4998 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4999 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
5000 {
Hanno Becker90333da2017-10-10 11:27:13 +01005001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005002 /* Will be handled when trying to parse ServerHello */
5003 return( 0 );
5004 }
5005#endif
5006
5007#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5008 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5009 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5010 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5011 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5012 {
5013 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5014 /* Will be handled in mbedtls_ssl_parse_certificate() */
5015 return( 0 );
5016 }
5017#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5018
5019 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005020 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 }
5022
Hanno Beckerc76c6192017-06-06 10:03:17 +01005023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01005024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005025 {
Hanno Becker37ae9522019-05-03 16:54:26 +01005026 /* Drop unexpected ApplicationData records,
5027 * except at the beginning of renegotiations */
5028 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5029 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5030#if defined(MBEDTLS_SSL_RENEGOTIATION)
5031 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5032 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005033#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01005034 )
5035 {
5036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5037 return( MBEDTLS_ERR_SSL_NON_FATAL );
5038 }
5039
5040 if( ssl->handshake != NULL &&
5041 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5042 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005043 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01005044 }
5045 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01005046#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005047
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 return( 0 );
5049}
5050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005051int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005052{
irwir6c0da642019-09-26 21:07:41 +03005053 return( mbedtls_ssl_send_alert_message( ssl,
5054 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5055 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005056}
5057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005059 unsigned char level,
5060 unsigned char message )
5061{
Janos Follath865b3eb2019-12-16 11:46:15 +00005062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00005063
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005064 if( ssl == NULL || ssl->conf == NULL )
5065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005068 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005071 ssl->out_msglen = 2;
5072 ssl->out_msg[0] = level;
5073 ssl->out_msg[1] = message;
5074
Hanno Becker67bc7c32018-08-06 11:33:50 +01005075 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005078 return( ret );
5079 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005081
5082 return( 0 );
5083}
5084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005085int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005086{
Janos Follath865b3eb2019-12-16 11:46:15 +00005087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005091 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005092 ssl->out_msglen = 1;
5093 ssl->out_msg[0] = 1;
5094
Paul Bakker5121ce52009-01-03 21:22:43 +00005095 ssl->state++;
5096
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005097 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005100 return( ret );
5101 }
5102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005104
5105 return( 0 );
5106}
5107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005108int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005109{
Janos Follath865b3eb2019-12-16 11:46:15 +00005110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005113
Hanno Becker327c93b2018-08-15 13:56:18 +01005114 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 return( ret );
5118 }
5119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005123 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5124 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005125 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005126 }
5127
Hanno Beckere678eaa2018-08-21 14:57:46 +01005128 /* CCS records are only accepted if they have length 1 and content '1',
5129 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005130
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005131 /*
5132 * Switch to our negotiated transform and session parameters for inbound
5133 * data.
5134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005136 ssl->transform_in = ssl->transform_negotiate;
5137 ssl->session_in = ssl->session_negotiate;
5138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005139#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005142#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00005143 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005144#endif
5145
5146 /* Increment epoch */
5147 if( ++ssl->in_epoch == 0 )
5148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005150 /* This is highly unlikely to happen for legitimate reasons, so
5151 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005152 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005153 }
5154 }
5155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005157 memset( ssl->in_ctr, 0, 8 );
5158
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005159 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5162 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005167 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5168 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005170 }
5171 }
5172#endif
5173
Paul Bakker5121ce52009-01-03 21:22:43 +00005174 ssl->state++;
5175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005177
5178 return( 0 );
5179}
5180
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005181/* Once ssl->out_hdr as the address of the beginning of the
5182 * next outgoing record is set, deduce the other pointers.
5183 *
5184 * Note: For TLS, we save the implicit record sequence number
5185 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5186 * and the caller has to make sure there's space for this.
5187 */
5188
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005189static size_t ssl_transform_get_explicit_iv_len(
5190 mbedtls_ssl_transform const *transform )
5191{
5192 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
5193 return( 0 );
5194
5195 return( transform->ivlen - transform->fixed_ivlen );
5196}
5197
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005198void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5199 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005200{
5201#if defined(MBEDTLS_SSL_PROTO_DTLS)
5202 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5203 {
5204 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005205#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005206 ssl->out_cid = ssl->out_ctr + 8;
5207 ssl->out_len = ssl->out_cid;
5208 if( transform != NULL )
5209 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005210#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005211 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005212#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005213 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005214 }
5215 else
5216#endif
5217 {
5218 ssl->out_ctr = ssl->out_hdr - 8;
5219 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005220#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005221 ssl->out_cid = ssl->out_len;
5222#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005223 ssl->out_iv = ssl->out_hdr + 5;
5224 }
5225
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005226 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005227 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005228 if( transform != NULL )
5229 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005230}
5231
5232/* Once ssl->in_hdr as the address of the beginning of the
5233 * next incoming record is set, deduce the other pointers.
5234 *
5235 * Note: For TLS, we save the implicit record sequence number
5236 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5237 * and the caller has to make sure there's space for this.
5238 */
5239
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005240void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005241{
Hanno Becker79594fd2019-05-08 09:38:41 +01005242 /* This function sets the pointers to match the case
5243 * of unprotected TLS/DTLS records, with both ssl->in_iv
5244 * and ssl->in_msg pointing to the beginning of the record
5245 * content.
5246 *
5247 * When decrypting a protected record, ssl->in_msg
5248 * will be shifted to point to the beginning of the
5249 * record plaintext.
5250 */
5251
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005252#if defined(MBEDTLS_SSL_PROTO_DTLS)
5253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5254 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005255 /* This sets the header pointers to match records
5256 * without CID. When we receive a record containing
5257 * a CID, the fields are shifted accordingly in
5258 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005259 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005260#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005261 ssl->in_cid = ssl->in_ctr + 8;
5262 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005263#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005264 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005265#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005266 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005267 }
5268 else
5269#endif
5270 {
5271 ssl->in_ctr = ssl->in_hdr - 8;
5272 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005273#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005274 ssl->in_cid = ssl->in_len;
5275#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005276 ssl->in_iv = ssl->in_hdr + 5;
5277 }
5278
Hanno Becker79594fd2019-05-08 09:38:41 +01005279 /* This will be adjusted at record decryption time. */
5280 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005281}
5282
Paul Bakker5121ce52009-01-03 21:22:43 +00005283/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005284 * Setup an SSL context
5285 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005286
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005287void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005288{
5289 /* Set the incoming and outgoing record pointers. */
5290#if defined(MBEDTLS_SSL_PROTO_DTLS)
5291 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5292 {
5293 ssl->out_hdr = ssl->out_buf;
5294 ssl->in_hdr = ssl->in_buf;
5295 }
5296 else
5297#endif /* MBEDTLS_SSL_PROTO_DTLS */
5298 {
5299 ssl->out_hdr = ssl->out_buf + 8;
5300 ssl->in_hdr = ssl->in_buf + 8;
5301 }
5302
5303 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005304 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5305 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005306}
5307
Paul Bakker5121ce52009-01-03 21:22:43 +00005308/*
5309 * SSL get accessors
5310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005311size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005312{
5313 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5314}
5315
Hanno Becker8b170a02017-10-10 11:51:19 +01005316int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5317{
5318 /*
5319 * Case A: We're currently holding back
5320 * a message for further processing.
5321 */
5322
5323 if( ssl->keep_current_message == 1 )
5324 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005325 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005326 return( 1 );
5327 }
5328
5329 /*
5330 * Case B: Further records are pending in the current datagram.
5331 */
5332
5333#if defined(MBEDTLS_SSL_PROTO_DTLS)
5334 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5335 ssl->in_left > ssl->next_record_offset )
5336 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005338 return( 1 );
5339 }
5340#endif /* MBEDTLS_SSL_PROTO_DTLS */
5341
5342 /*
5343 * Case C: A handshake message is being processed.
5344 */
5345
Hanno Becker8b170a02017-10-10 11:51:19 +01005346 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5347 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005349 return( 1 );
5350 }
5351
5352 /*
5353 * Case D: An application data message is being processed
5354 */
5355 if( ssl->in_offt != NULL )
5356 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005358 return( 1 );
5359 }
5360
5361 /*
5362 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005363 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005364 * we implement support for multiple alerts in single records.
5365 */
5366
5367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5368 return( 0 );
5369}
5370
Paul Bakker43ca69c2011-01-15 17:35:19 +00005371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005372int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005373{
Hanno Becker3136ede2018-08-17 15:28:19 +01005374 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005375 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005376 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005377
Hanno Becker5903de42019-05-03 14:46:38 +01005378 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5379
Hanno Becker78640902018-08-13 16:35:15 +01005380 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005381 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383#if defined(MBEDTLS_ZLIB_SUPPORT)
5384 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5385 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005386#endif
5387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 case MBEDTLS_MODE_GCM:
5391 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005392 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005394 transform_expansion = transform->minlen;
5395 break;
5396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005398
5399 block_size = mbedtls_cipher_get_block_size(
5400 &transform->cipher_ctx_enc );
5401
Hanno Becker3136ede2018-08-17 15:28:19 +01005402 /* Expansion due to the addition of the MAC. */
5403 transform_expansion += transform->maclen;
5404
5405 /* Expansion due to the addition of CBC padding;
5406 * Theoretically up to 256 bytes, but we never use
5407 * more than the block size of the underlying cipher. */
5408 transform_expansion += block_size;
5409
5410 /* For TLS 1.1 or higher, an explicit IV is added
5411 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005412#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5413 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005414 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005415#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005416
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005417 break;
5418
5419 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005422 }
5423
Hanno Beckera0e20d02019-05-15 14:03:01 +01005424#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005425 if( transform->out_cid_len != 0 )
5426 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005427#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005428
Hanno Becker5903de42019-05-03 14:46:38 +01005429 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005430}
5431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005433/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005434 * Check record counters and renegotiate if they're above the limit.
5435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005437{
Hanno Beckerdd772292020-02-05 10:38:31 +00005438 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005439 int in_ctr_cmp;
5440 int out_ctr_cmp;
5441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005442 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5443 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005444 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005445 {
5446 return( 0 );
5447 }
5448
Andres AG2196c7f2016-12-15 17:01:16 +00005449 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5450 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005451 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005452 ssl->conf->renego_period + ep_len, 8 - ep_len );
5453
5454 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005455 {
5456 return( 0 );
5457 }
5458
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005460 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005461}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005463
5464/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005465 * Receive application data decrypted from the SSL layer
5466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005468{
Janos Follath865b3eb2019-12-16 11:46:15 +00005469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005470 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005471
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005472 if( ssl == NULL || ssl->conf == NULL )
5473 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005481 return( ret );
5482
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005483 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005485 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005486 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005487 return( ret );
5488 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005489 }
5490#endif
5491
Hanno Becker4a810fb2017-05-24 16:27:30 +01005492 /*
5493 * Check if renegotiation is necessary and/or handshake is
5494 * in process. If yes, perform/continue, and fall through
5495 * if an unexpected packet is received while the client
5496 * is waiting for the ServerHello.
5497 *
5498 * (There is no equivalent to the last condition on
5499 * the server-side as it is not treated as within
5500 * a handshake while waiting for the ClientHello
5501 * after a renegotiation request.)
5502 */
5503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005505 ret = ssl_check_ctr_renegotiate( ssl );
5506 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5507 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005510 return( ret );
5511 }
5512#endif
5513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005514 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005516 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005517 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5518 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005520 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005521 return( ret );
5522 }
5523 }
5524
Hanno Beckere41158b2017-10-23 13:30:32 +01005525 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005526 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005527 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005528 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005529 if( ssl->f_get_timer != NULL &&
5530 ssl->f_get_timer( ssl->p_timer ) == -1 )
5531 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005532 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005533 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005534
Hanno Becker327c93b2018-08-15 13:56:18 +01005535 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005536 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005537 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5538 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005539
Hanno Becker4a810fb2017-05-24 16:27:30 +01005540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5541 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005542 }
5543
5544 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005545 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005546 {
5547 /*
5548 * OpenSSL sends empty messages to randomize the IV
5549 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005550 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005551 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005552 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005553 return( 0 );
5554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 return( ret );
5557 }
5558 }
5559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005560 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005562 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005563
Hanno Becker4a810fb2017-05-24 16:27:30 +01005564 /*
5565 * - For client-side, expect SERVER_HELLO_REQUEST.
5566 * - For server-side, expect CLIENT_HELLO.
5567 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5568 */
5569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005571 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005572 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005573 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005576
5577 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005579 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005580 {
5581 continue;
5582 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005583#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005584 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005585 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005586#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005587
Hanno Becker4a810fb2017-05-24 16:27:30 +01005588#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005589 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005592 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005593
5594 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005597 {
5598 continue;
5599 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005601 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005602 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005603#endif /* MBEDTLS_SSL_SRV_C */
5604
Hanno Becker21df7f92017-10-17 11:03:26 +01005605#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005606 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005607 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5608 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5609 ssl->conf->allow_legacy_renegotiation ==
5610 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5611 {
5612 /*
5613 * Accept renegotiation request
5614 */
Paul Bakker48916f92012-09-16 19:57:18 +00005615
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005616 /* DTLS clients need to know renego is server-initiated */
5617#if defined(MBEDTLS_SSL_PROTO_DTLS)
5618 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5619 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5620 {
5621 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5622 }
5623#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005624 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005625 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5626 ret != 0 )
5627 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005628 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5629 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005630 return( ret );
5631 }
5632 }
5633 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005634#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005635 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005636 /*
5637 * Refuse renegotiation
5638 */
5639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005640 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642#if defined(MBEDTLS_SSL_PROTO_SSL3)
5643 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005644 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005645 /* SSLv3 does not have a "no_renegotiation" warning, so
5646 we send a fatal alert and abort the connection. */
5647 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5648 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5649 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005650 }
5651 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5653#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5654 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5655 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5658 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5659 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005660 {
5661 return( ret );
5662 }
Paul Bakker48916f92012-09-16 19:57:18 +00005663 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005664 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005665#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5666 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005668 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5669 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005670 }
Paul Bakker48916f92012-09-16 19:57:18 +00005671 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005672
Hanno Becker90333da2017-10-10 11:27:13 +01005673 /* At this point, we don't know whether the renegotiation has been
5674 * completed or not. The cases to consider are the following:
5675 * 1) The renegotiation is complete. In this case, no new record
5676 * has been read yet.
5677 * 2) The renegotiation is incomplete because the client received
5678 * an application data record while awaiting the ServerHello.
5679 * 3) The renegotiation is incomplete because the client received
5680 * a non-handshake, non-application data message while awaiting
5681 * the ServerHello.
5682 * In each of these case, looping will be the proper action:
5683 * - For 1), the next iteration will read a new record and check
5684 * if it's application data.
5685 * - For 2), the loop condition isn't satisfied as application data
5686 * is present, hence continue is the same as break
5687 * - For 3), the loop condition is satisfied and read_record
5688 * will re-deliver the message that was held back by the client
5689 * when expecting the ServerHello.
5690 */
5691 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005692 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005693#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005694 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005695 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005696 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005697 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005698 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005701 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005702 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005703 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005704 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005705 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005708 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5709 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005712 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005713 }
5714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005717 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5718 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005719 }
5720
5721 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005722
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005723 /* We're going to return something now, cancel timer,
5724 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005726 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005727
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005729 /* If we requested renego but received AppData, resend HelloRequest.
5730 * Do it now, after setting in_offt, to avoid taking this branch
5731 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005732#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005733 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005734 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005735 {
Hanno Becker786300f2020-02-05 10:46:40 +00005736 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005737 {
Hanno Becker786300f2020-02-05 10:46:40 +00005738 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5739 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005740 return( ret );
5741 }
5742 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005743#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005744#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005745 }
5746
5747 n = ( len < ssl->in_msglen )
5748 ? len : ssl->in_msglen;
5749
5750 memcpy( buf, ssl->in_offt, n );
5751 ssl->in_msglen -= n;
5752
gabor-mezei-arma3214132020-07-15 10:55:00 +02005753 /* Zeroising the plaintext buffer to erase unused application data
5754 from the memory. */
5755 mbedtls_platform_zeroize( ssl->in_offt, n );
5756
Paul Bakker5121ce52009-01-03 21:22:43 +00005757 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005758 {
5759 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005761 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005763 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005764 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 /* more data available */
5766 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005767 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005770
Paul Bakker23986e52011-04-24 08:57:21 +00005771 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005772}
5773
5774/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005775 * Send application data to be encrypted by the SSL layer, taking care of max
5776 * fragment length and buffer size.
5777 *
5778 * According to RFC 5246 Section 6.2.1:
5779 *
5780 * Zero-length fragments of Application data MAY be sent as they are
5781 * potentially useful as a traffic analysis countermeasure.
5782 *
5783 * Therefore, it is possible that the input message length is 0 and the
5784 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005785 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005786static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005787 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005788{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005789 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5790 const size_t max_len = (size_t) ret;
5791
5792 if( ret < 0 )
5793 {
5794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5795 return( ret );
5796 }
5797
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005798 if( len > max_len )
5799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005800#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005801 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005804 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5805 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005806 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005808 }
5809 else
5810#endif
5811 len = max_len;
5812 }
Paul Bakker887bd502011-06-08 13:10:54 +00005813
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 if( ssl->out_left != 0 )
5815 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005816 /*
5817 * The user has previously tried to send the data and
5818 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5819 * written. In this case, we expect the high-level write function
5820 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005822 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 return( ret );
5826 }
5827 }
Paul Bakker887bd502011-06-08 13:10:54 +00005828 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005829 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005830 /*
5831 * The user is trying to send a message the first time, so we need to
5832 * copy the data into the internal buffers and setup the data structure
5833 * to keep track of partial writes
5834 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005835 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005837 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005838
Hanno Becker67bc7c32018-08-06 11:33:50 +01005839 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005842 return( ret );
5843 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005844 }
5845
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005846 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005847}
5848
5849/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005850 * Write application data, doing 1/n-1 splitting if necessary.
5851 *
5852 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005853 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005854 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005856#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005857static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005858 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005859{
Janos Follath865b3eb2019-12-16 11:46:15 +00005860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005861
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005862 if( ssl->conf->cbc_record_splitting ==
5863 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005864 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005865 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5866 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5867 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005868 {
5869 return( ssl_write_real( ssl, buf, len ) );
5870 }
5871
5872 if( ssl->split_done == 0 )
5873 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005874 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005875 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005876 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005877 }
5878
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005879 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5880 return( ret );
5881 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005882
5883 return( ret + 1 );
5884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005886
5887/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005888 * Write application data (public-facing wrapper)
5889 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005890int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005891{
Janos Follath865b3eb2019-12-16 11:46:15 +00005892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005893
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005894 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005895
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005896 if( ssl == NULL || ssl->conf == NULL )
5897 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5898
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005899#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005900 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5901 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005902 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005903 return( ret );
5904 }
5905#endif
5906
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005907 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005908 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005909 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005910 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005911 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005912 return( ret );
5913 }
5914 }
5915
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005916#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005917 ret = ssl_write_split( ssl, buf, len );
5918#else
5919 ret = ssl_write_real( ssl, buf, len );
5920#endif
5921
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005923
5924 return( ret );
5925}
5926
5927/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005928 * Notify the peer that the connection is being closed
5929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005931{
Janos Follath865b3eb2019-12-16 11:46:15 +00005932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005933
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005934 if( ssl == NULL || ssl->conf == NULL )
5935 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005938
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005939 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005940 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005942 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005944 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5945 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5946 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005949 return( ret );
5950 }
5951 }
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005954
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005955 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005956}
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005959{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005960 if( transform == NULL )
5961 return;
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005964 deflateEnd( &transform->ctx_deflate );
5965 inflateEnd( &transform->ctx_inflate );
5966#endif
5967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5969 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005970
Hanno Beckerd56ed242018-01-03 15:32:51 +00005971#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 mbedtls_md_free( &transform->md_ctx_enc );
5973 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005974#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005975
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005976 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005977}
5978
Hanno Becker0271f962018-08-16 13:23:47 +01005979#if defined(MBEDTLS_SSL_PROTO_DTLS)
5980
Hanno Becker533ab5f2020-02-05 10:49:13 +00005981void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005982{
5983 unsigned offset;
5984 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5985
5986 if( hs == NULL )
5987 return;
5988
Hanno Becker283f5ef2018-08-24 09:34:47 +01005989 ssl_free_buffered_record( ssl );
5990
Hanno Becker0271f962018-08-16 13:23:47 +01005991 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005992 ssl_buffering_free_slot( ssl, offset );
5993}
5994
5995static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5996 uint8_t slot )
5997{
5998 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5999 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01006000
6001 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
6002 return;
6003
Hanno Beckere605b192018-08-21 15:59:07 +01006004 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01006005 {
Hanno Beckere605b192018-08-21 15:59:07 +01006006 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01006007 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01006008 mbedtls_free( hs_buf->data );
6009 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01006010 }
6011}
6012
6013#endif /* MBEDTLS_SSL_PROTO_DTLS */
6014
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006015/*
6016 * Convert version numbers to/from wire format
6017 * and, for DTLS, to/from TLS equivalent.
6018 *
6019 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08006020 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006021 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6022 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006025 unsigned char ver[2] )
6026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027#if defined(MBEDTLS_SSL_PROTO_DTLS)
6028 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006031 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6032
6033 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6034 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6035 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006036 else
6037#else
6038 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006039#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006040 {
6041 ver[0] = (unsigned char) major;
6042 ver[1] = (unsigned char) minor;
6043 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006044}
6045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006047 const unsigned char ver[2] )
6048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049#if defined(MBEDTLS_SSL_PROTO_DTLS)
6050 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006051 {
6052 *major = 255 - ver[0] + 2;
6053 *minor = 255 - ver[1] + 1;
6054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006056 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6057 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006058 else
6059#else
6060 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006061#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006062 {
6063 *major = ver[0];
6064 *minor = ver[1];
6065 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006066}
6067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068#endif /* MBEDTLS_SSL_TLS_C */