blob: 7da5674121cca139018aebc68487fccf2a293398 [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
Joe Subbianic54e9082021-07-19 11:56:54 +0100457 MBEDTLS_PUT_UINT16_BE( rec->data_len, cur, 0 );
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100458 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100459 }
460 else
Hanno Beckera0e20d02019-05-15 14:03:01 +0100461#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100462 {
Joe Subbianic54e9082021-07-19 11:56:54 +0100463 MBEDTLS_PUT_UINT16_BE( rec->data_len, cur, 0 );
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100464 cur += 2;
Hanno Becker95e4bbc2019-05-09 11:38:24 +0100465 }
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100466
467 *add_data_len = cur - add_data;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000468}
469
Hanno Becker9d062f92020-02-07 10:26:36 +0000470#if defined(MBEDTLS_SSL_PROTO_SSL3)
471
472#define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
473
474/*
475 * SSLv3.0 MAC functions
476 */
477static void ssl_mac( mbedtls_md_context_t *md_ctx,
478 const unsigned char *secret,
479 const unsigned char *buf, size_t len,
480 const unsigned char *ctr, int type,
481 unsigned char out[SSL3_MAC_MAX_BYTES] )
482{
483 unsigned char header[11];
484 unsigned char padding[48];
485 int padlen;
486 int md_size = mbedtls_md_get_size( md_ctx->md_info );
487 int md_type = mbedtls_md_get_type( md_ctx->md_info );
488
489 /* Only MD5 and SHA-1 supported */
490 if( md_type == MBEDTLS_MD_MD5 )
491 padlen = 48;
492 else
493 padlen = 40;
494
495 memcpy( header, ctr, 8 );
Joe Subbiania651e6f2021-08-23 11:35:25 +0100496 header[8] = (unsigned char) type;
Joe Subbiani11b71312021-08-23 12:49:14 +0100497 MBEDTLS_PUT_UINT16_BE( len, header, 9);
Hanno Becker9d062f92020-02-07 10:26:36 +0000498
499 memset( padding, 0x36, padlen );
500 mbedtls_md_starts( md_ctx );
501 mbedtls_md_update( md_ctx, secret, md_size );
502 mbedtls_md_update( md_ctx, padding, padlen );
503 mbedtls_md_update( md_ctx, header, 11 );
504 mbedtls_md_update( md_ctx, buf, len );
505 mbedtls_md_finish( md_ctx, out );
506
507 memset( padding, 0x5C, padlen );
508 mbedtls_md_starts( md_ctx );
509 mbedtls_md_update( md_ctx, secret, md_size );
510 mbedtls_md_update( md_ctx, padding, padlen );
511 mbedtls_md_update( md_ctx, out, md_size );
512 mbedtls_md_finish( md_ctx, out );
513}
514#endif /* MBEDTLS_SSL_PROTO_SSL3 */
515
Hanno Becker67a37db2020-05-28 16:27:07 +0100516#if defined(MBEDTLS_GCM_C) || \
517 defined(MBEDTLS_CCM_C) || \
518 defined(MBEDTLS_CHACHAPOLY_C)
Hanno Becker17263802020-05-28 07:05:48 +0100519static int ssl_transform_aead_dynamic_iv_is_explicit(
520 mbedtls_ssl_transform const *transform )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100521{
Hanno Becker17263802020-05-28 07:05:48 +0100522 return( transform->ivlen != transform->fixed_ivlen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100523}
524
Hanno Becker17263802020-05-28 07:05:48 +0100525/* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV )
526 *
527 * Concretely, this occurs in two variants:
528 *
529 * a) Fixed and dynamic IV lengths add up to total IV length, giving
530 * IV = fixed_iv || dynamic_iv
531 *
Hanno Becker15952812020-06-04 13:31:46 +0100532 * This variant is used in TLS 1.2 when used with GCM or CCM.
533 *
Hanno Becker17263802020-05-28 07:05:48 +0100534 * b) Fixed IV lengths matches total IV length, giving
535 * IV = fixed_iv XOR ( 0 || dynamic_iv )
Hanno Becker15952812020-06-04 13:31:46 +0100536 *
537 * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly.
538 *
539 * See also the documentation of mbedtls_ssl_transform.
Hanno Beckerf486e282020-06-04 13:33:08 +0100540 *
541 * This function has the precondition that
542 *
543 * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len )
544 *
545 * which has to be ensured by the caller. If this precondition
546 * violated, the behavior of this function is undefined.
Hanno Becker17263802020-05-28 07:05:48 +0100547 */
548static void ssl_build_record_nonce( unsigned char *dst_iv,
549 size_t dst_iv_len,
550 unsigned char const *fixed_iv,
551 size_t fixed_iv_len,
552 unsigned char const *dynamic_iv,
553 size_t dynamic_iv_len )
554{
555 size_t i;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100556
557 /* Start with Fixed IV || 0 */
Hanno Becker17263802020-05-28 07:05:48 +0100558 memset( dst_iv, 0, dst_iv_len );
559 memcpy( dst_iv, fixed_iv, fixed_iv_len );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100560
Hanno Becker17263802020-05-28 07:05:48 +0100561 dst_iv += dst_iv_len - dynamic_iv_len;
562 for( i = 0; i < dynamic_iv_len; i++ )
563 dst_iv[i] ^= dynamic_iv[i];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100564}
Hanno Becker67a37db2020-05-28 16:27:07 +0100565#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100566
Hanno Beckera18d1322018-01-03 14:27:32 +0000567int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
568 mbedtls_ssl_transform *transform,
569 mbedtls_record *rec,
570 int (*f_rng)(void *, unsigned char *, size_t),
571 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100574 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000575 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +0100576 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +0100577 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000578 size_t post_avail;
579
580 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +0000581#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +0200582 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000583 ((void) ssl);
584#endif
585
586 /* The PRNG is used for dynamic IV generation that's used
587 * for CBC transformations in TLS 1.1 and TLS 1.2. */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200588#if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000589 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
590 ((void) f_rng);
591 ((void) p_rng);
592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000596 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100597 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
599 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
600 }
Hanno Becker43c24b82019-05-01 09:45:57 +0100601 if( rec == NULL
602 || rec->buf == NULL
603 || rec->buf_len < rec->data_offset
604 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +0100605#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +0100606 || rec->cid_len != 0
607#endif
608 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000609 {
610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100612 }
613
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000614 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100615 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000617 data, rec->data_len );
618
619 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
620
621 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
622 {
Paul Elliottd48d5c62021-01-07 14:47:05 +0000623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET
624 " too large, maximum %" MBEDTLS_PRINTF_SIZET,
Paul Elliott3891caf2020-12-17 18:42:40 +0000625 rec->data_len,
626 (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000627 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
628 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +0100629
Hanno Becker92313402020-05-20 13:58:58 +0100630 /* The following two code paths implement the (D)TLSInnerPlaintext
631 * structure present in TLS 1.3 and DTLS 1.2 + CID.
632 *
633 * See ssl_build_inner_plaintext() for more information.
634 *
635 * Note that this changes `rec->data_len`, and hence
636 * `post_avail` needs to be recalculated afterwards.
637 *
638 * Note also that the two code paths cannot occur simultaneously
639 * since they apply to different versions of the protocol. There
640 * is hence no risk of double-addition of the inner plaintext.
641 */
Hanno Beckerccc13d02020-05-04 12:30:04 +0100642#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
643 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
644 {
Hanno Becker13996922020-05-28 16:15:19 +0100645 size_t padding =
646 ssl_compute_padding_length( rec->data_len,
Hanno Beckerceef8482020-06-02 06:16:00 +0100647 MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY );
Hanno Beckerccc13d02020-05-04 12:30:04 +0100648 if( ssl_build_inner_plaintext( data,
Hanno Becker13996922020-05-28 16:15:19 +0100649 &rec->data_len,
650 post_avail,
651 rec->type,
652 padding ) != 0 )
Hanno Beckerccc13d02020-05-04 12:30:04 +0100653 {
654 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
655 }
656
657 rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
658 }
659#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
660
Hanno Beckera0e20d02019-05-15 14:03:01 +0100661#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +0100662 /*
663 * Add CID information
664 */
665 rec->cid_len = transform->out_cid_len;
666 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
667 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100668
669 if( rec->cid_len != 0 )
670 {
Hanno Becker13996922020-05-28 16:15:19 +0100671 size_t padding =
672 ssl_compute_padding_length( rec->data_len,
673 MBEDTLS_SSL_CID_PADDING_GRANULARITY );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100674 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +0100675 * Wrap plaintext into DTLSInnerPlaintext structure.
Hanno Becker581bc1b2020-05-04 12:20:03 +0100676 * See ssl_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100677 *
Hanno Becker07dc97d2019-05-20 15:08:01 +0100678 * Note that this changes `rec->data_len`, and hence
679 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100680 */
Hanno Becker581bc1b2020-05-04 12:20:03 +0100681 if( ssl_build_inner_plaintext( data,
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100682 &rec->data_len,
683 post_avail,
Hanno Becker13996922020-05-28 16:15:19 +0100684 rec->type,
685 padding ) != 0 )
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100686 {
687 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
688 }
689
690 rec->type = MBEDTLS_SSL_MSG_CID;
691 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100692#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +0100693
Hanno Becker8b3eb5a2019-04-29 17:31:37 +0100694 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
695
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +0100697 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 */
Hanno Becker52344c22018-01-03 15:24:20 +0000699#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( mode == MBEDTLS_MODE_STREAM ||
701 ( mode == MBEDTLS_MODE_CBC
702#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000703 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100704#endif
705 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000707 if( post_avail < transform->maclen )
708 {
709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
710 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
711 }
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000714 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200715 {
Hanno Becker9d062f92020-02-07 10:26:36 +0000716 unsigned char mac[SSL3_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000717 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
718 data, rec->data_len, rec->ctr, rec->type, mac );
719 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200720 }
721 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
724 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000725 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200726 {
Hanno Becker992b6872017-11-09 18:57:39 +0000727 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
728
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100729 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
730 transform->minor_ver );
Hanno Becker992b6872017-11-09 18:57:39 +0000731
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000732 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +0100733 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000734 mbedtls_md_hmac_update( &transform->md_ctx_enc,
735 data, rec->data_len );
736 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
737 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
738
739 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200740 }
741 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200742#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
745 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200746 }
747
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000748 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
749 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200750
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000751 rec->data_len += transform->maclen;
752 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100753 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +0200754 }
Hanno Becker52344c22018-01-03 15:24:20 +0000755#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +0200757 /*
758 * Encrypt
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
761 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000764 size_t olen;
Paul Elliottd48d5c62021-01-07 14:47:05 +0000765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000766 "including %d bytes of padding",
767 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000769 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
770 transform->iv_enc, transform->ivlen,
771 data, rec->data_len,
772 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200775 return( ret );
776 }
777
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000778 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200782 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000783 }
Paul Bakker68884e32013-01-07 18:20:04 +0100784 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000786
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200787#if defined(MBEDTLS_GCM_C) || \
788 defined(MBEDTLS_CCM_C) || \
789 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200791 mode == MBEDTLS_MODE_CCM ||
792 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000793 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200795 unsigned char iv[12];
Hanno Beckerdf8be222020-05-21 15:30:57 +0100796 unsigned char *dynamic_iv;
797 size_t dynamic_iv_len;
Hanno Becker17263802020-05-28 07:05:48 +0100798 int dynamic_iv_is_explicit =
799 ssl_transform_aead_dynamic_iv_is_explicit( transform );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000800
Hanno Beckerbd5ed1d2020-05-21 15:26:39 +0100801 /* Check that there's space for the authentication tag. */
802 if( post_avail < transform->taglen )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000803 {
804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
805 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
806 }
Paul Bakkerca4ab492012-04-18 14:23:57 +0000807
Paul Bakker68884e32013-01-07 18:20:04 +0100808 /*
Hanno Beckerdf8be222020-05-21 15:30:57 +0100809 * Build nonce for AEAD encryption.
810 *
811 * Note: In the case of CCM and GCM in TLS 1.2, the dynamic
812 * part of the IV is prepended to the ciphertext and
813 * can be chosen freely - in particular, it need not
814 * agree with the record sequence number.
815 * However, since ChaChaPoly as well as all AEAD modes
816 * in TLS 1.3 use the record sequence number as the
817 * dynamic part of the nonce, we uniformly use the
818 * record sequence number here in all cases.
Paul Bakker68884e32013-01-07 18:20:04 +0100819 */
Hanno Beckerdf8be222020-05-21 15:30:57 +0100820 dynamic_iv = rec->ctr;
821 dynamic_iv_len = sizeof( rec->ctr );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200822
Hanno Becker17263802020-05-28 07:05:48 +0100823 ssl_build_record_nonce( iv, sizeof( iv ),
824 transform->iv_enc,
825 transform->fixed_ivlen,
826 dynamic_iv,
827 dynamic_iv_len );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +0100828
Hanno Becker1cb6c2a2020-05-21 15:25:21 +0100829 /*
830 * Build additional data for AEAD encryption.
831 * This depends on the TLS version.
832 */
833 ssl_extract_add_data_from_record( add_data, &add_data_len, rec,
834 transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +0100835
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200836 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
Hanno Becker7cca3582020-06-04 13:27:22 +0100837 iv, transform->ivlen );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200838 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker16bf0e22020-06-04 13:27:34 +0100839 dynamic_iv,
840 dynamic_iv_is_explicit ? dynamic_iv_len : 0 );
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000841 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +0100842 add_data, add_data_len );
Paul Elliottd48d5c62021-01-07 14:47:05 +0000843 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +0200844 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000845 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000846
Paul Bakker68884e32013-01-07 18:20:04 +0100847 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +0200848 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200849 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000850
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100851 if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000852 iv, transform->ivlen,
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100853 add_data, add_data_len,
854 data, rec->data_len, /* src */
855 data, rec->buf_len - (data - rec->buf), /* dst */
856 &rec->data_len,
857 transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +0200860 return( ret );
861 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000862 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
Manuel Pégourié-Gonnardf5cf71e2020-12-01 11:43:40 +0100863 data + rec->data_len - transform->taglen,
864 transform->taglen );
Hanno Beckerdf8be222020-05-21 15:30:57 +0100865 /* Account for authentication tag. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000866 post_avail -= transform->taglen;
Hanno Beckerdf8be222020-05-21 15:30:57 +0100867
868 /*
869 * Prefix record content with dynamic IV in case it is explicit.
870 */
Hanno Becker1cda2662020-06-04 13:28:28 +0100871 if( dynamic_iv_is_explicit != 0 )
Hanno Beckerdf8be222020-05-21 15:30:57 +0100872 {
873 if( rec->data_offset < dynamic_iv_len )
874 {
875 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
876 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
877 }
878
879 memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len );
880 rec->data_offset -= dynamic_iv_len;
881 rec->data_len += dynamic_iv_len;
882 }
883
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100884 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000885 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000886 else
Hanno Beckerc3f7b0b2020-05-28 16:27:16 +0100887#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +0200888#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000890 {
Janos Follath865b3eb2019-12-16 11:46:15 +0000891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000892 size_t padlen, i;
893 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000894
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000895 /* Currently we're always using minimal padding
896 * (up to 255 bytes would be allowed). */
897 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
898 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 padlen = 0;
900
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000901 /* Check there's enough space in the buffer for the padding. */
902 if( post_avail < padlen + 1 )
903 {
904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
905 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
906 }
907
Paul Bakker5121ce52009-01-03 21:22:43 +0000908 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000909 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000911 rec->data_len += padlen + 1;
912 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000915 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +0000916 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
917 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000918 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000919 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000920 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000921 if( f_rng == NULL )
922 {
923 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
924 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
925 }
926
927 if( rec->data_offset < transform->ivlen )
928 {
929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
930 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
931 }
932
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000933 /*
934 * Generate IV
935 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000936 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +0000937 if( ret != 0 )
938 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000939
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000940 memcpy( data - transform->ivlen, transform->iv_enc,
941 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000942
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000943 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000945
Paul Elliottd48d5c62021-01-07 14:47:05 +0000946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", "
947 "including %" MBEDTLS_PRINTF_SIZET
948 " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000949 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200950 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000952 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
953 transform->iv_enc,
954 transform->ivlen,
955 data, rec->data_len,
956 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +0200957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +0200959 return( ret );
960 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200961
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000962 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +0200963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +0200966 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000969 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +0200970 {
971 /*
972 * Save IV in SSL3 and TLS1
973 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000974 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
975 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000976 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000977 else
Paul Bakkercca5b812013-08-31 17:40:26 +0200978#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000979 {
980 data -= transform->ivlen;
981 rec->data_offset -= transform->ivlen;
982 rec->data_len += transform->ivlen;
983 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +0100986 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100987 {
Hanno Becker3d8c9072018-01-05 16:24:22 +0000988 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
989
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100990 /*
991 * MAC(MAC_write_key, seq_num +
992 * TLSCipherText.type +
993 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +0100994 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +0100995 * IV + // except for TLS 1.0
996 * ENC(content + padding + padding_length));
997 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000998
999 if( post_avail < transform->maclen)
1000 {
1001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
1002 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1003 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001004
Hanno Becker1cb6c2a2020-05-21 15:25:21 +01001005 ssl_extract_add_data_from_record( add_data, &add_data_len,
1006 rec, transform->minor_ver );
Hanno Becker1f10d762019-04-26 13:34:37 +01001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001009 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001010 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001011
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001012 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01001013 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001014 mbedtls_md_hmac_update( &transform->md_ctx_enc,
1015 data, rec->data_len );
1016 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
1017 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001018
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001019 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001020
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001021 rec->data_len += transform->maclen;
1022 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001023 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001024 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001026 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001027 else
Manuel Pégourié-Gonnard2df1f1f2020-07-09 12:11:39 +02001028#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1031 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001032 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001033
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001034 /* Make extra sure authentication was performed, exactly once */
1035 if( auth_done != 1 )
1036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001039 }
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001042
1043 return( 0 );
1044}
1045
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02001046#if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001047/*
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001048 * Turn a bit into a mask:
1049 * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1
1050 * - if bit == 0, return the all-bits 0 mask, aka 0
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001051 *
1052 * This function can be used to write constant-time code by replacing branches
1053 * with bit operations using masks.
1054 *
1055 * This function is implemented without using comparison operators, as those
1056 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001057 */
1058static size_t mbedtls_ssl_cf_mask_from_bit( size_t bit )
1059{
1060 /* MSVC has a warning about unary minus on unsigned integer types,
1061 * but this is well-defined and precisely what we want to do here. */
1062#if defined(_MSC_VER)
1063#pragma warning( push )
1064#pragma warning( disable : 4146 )
1065#endif
1066 return -bit;
1067#if defined(_MSC_VER)
1068#pragma warning( pop )
1069#endif
1070}
1071
1072/*
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001073 * Constant-flow mask generation for "less than" comparison:
1074 * - if x < y, return all bits 1, that is (size_t) -1
1075 * - otherwise, return all bits 0, that is 0
1076 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001077 * This function can be used to write constant-time code by replacing branches
1078 * with bit operations using masks.
1079 *
1080 * This function is implemented without using comparison operators, as those
1081 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001082 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001083static size_t mbedtls_ssl_cf_mask_lt( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001084{
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001085 /* This has the most significant bit set if and only if x < y */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001086 const size_t sub = x - y;
1087
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001088 /* sub1 = (x < y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001089 const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 );
1090
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001091 /* mask = (x < y) ? 0xff... : 0x00... */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001092 const size_t mask = mbedtls_ssl_cf_mask_from_bit( sub1 );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001093
1094 return( mask );
1095}
1096
1097/*
1098 * Constant-flow mask generation for "greater or equal" comparison:
1099 * - if x >= y, return all bits 1, that is (size_t) -1
1100 * - otherwise, return all bits 0, that is 0
1101 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001102 * This function can be used to write constant-time code by replacing branches
1103 * with bit operations using masks.
1104 *
1105 * This function is implemented without using comparison operators, as those
1106 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001107 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001108static size_t mbedtls_ssl_cf_mask_ge( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001109{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001110 return( ~mbedtls_ssl_cf_mask_lt( x, y ) );
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001111}
1112
1113/*
1114 * Constant-flow boolean "equal" comparison:
1115 * return x == y
1116 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001117 * This function can be used to write constant-time code by replacing branches
1118 * with bit operations - it can be used in conjunction with
1119 * mbedtls_ssl_cf_mask_from_bit().
1120 *
1121 * This function is implemented without using comparison operators, as those
1122 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001123 */
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001124static size_t mbedtls_ssl_cf_bool_eq( size_t x, size_t y )
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001125{
1126 /* diff = 0 if x == y, non-zero otherwise */
1127 const size_t diff = x ^ y;
1128
1129 /* MSVC has a warning about unary minus on unsigned integer types,
1130 * but this is well-defined and precisely what we want to do here. */
1131#if defined(_MSC_VER)
1132#pragma warning( push )
1133#pragma warning( disable : 4146 )
1134#endif
1135
1136 /* diff_msb's most significant bit is equal to x != y */
1137 const size_t diff_msb = ( diff | -diff );
1138
1139#if defined(_MSC_VER)
1140#pragma warning( pop )
1141#endif
1142
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001143 /* diff1 = (x != y) ? 1 : 0 */
Manuel Pégourié-Gonnard2ddec432020-08-24 12:49:23 +02001144 const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 );
1145
1146 return( 1 ^ diff1 );
1147}
1148
1149/*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001150 * Constant-flow conditional memcpy:
1151 * - if c1 == c2, equivalent to memcpy(dst, src, len),
1152 * - otherwise, a no-op,
1153 * but with execution flow independent of the values of c1 and c2.
1154 *
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001155 * This function is implemented without using comparison operators, as those
1156 * might be translated to branches by some compilers on some platforms.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001157 */
Manuel Pégourié-Gonnarde7478432020-07-24 11:09:22 +02001158static void mbedtls_ssl_cf_memcpy_if_eq( unsigned char *dst,
1159 const unsigned char *src,
1160 size_t len,
1161 size_t c1, size_t c2 )
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001162{
Manuel Pégourié-Gonnard6e2a9a72020-08-25 10:01:00 +02001163 /* mask = c1 == c2 ? 0xff : 0x00 */
1164 const size_t equal = mbedtls_ssl_cf_bool_eq( c1, c2 );
Manuel Pégourié-Gonnard2a59fb42020-08-25 11:51:46 +02001165 const unsigned char mask = (unsigned char) mbedtls_ssl_cf_mask_from_bit( equal );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001166
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001167 /* dst[i] = c1 == c2 ? src[i] : dst[i] */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001168 for( size_t i = 0; i < len; i++ )
Manuel Pégourié-Gonnard6d6f8a42020-09-25 09:56:53 +02001169 dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001170}
1171
1172/*
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001173 * Compute HMAC of variable-length data with constant flow.
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001174 *
1175 * Only works with MD-5, SHA-1, SHA-256 and SHA-384.
1176 * (Otherwise, computation of block_size needs to be adapted.)
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001177 */
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02001178MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02001179 mbedtls_md_context_t *ctx,
1180 const unsigned char *add_data, size_t add_data_len,
1181 const unsigned char *data, size_t data_len_secret,
1182 size_t min_data_len, size_t max_data_len,
1183 unsigned char *output )
1184{
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001185 /*
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001186 * This function breaks the HMAC abstraction and uses the md_clone()
1187 * extension to the MD API in order to get constant-flow behaviour.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001188 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001189 * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001190 * concatenation, and okey/ikey are the XOR of the key with some fixed bit
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001191 * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001192 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001193 * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
1194 * minlen, then cloning the context, and for each byte up to maxlen
1195 * finishing up the hash computation, keeping only the correct result.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001196 *
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001197 * Then we only need to compute HASH(okey + inner_hash) and we're done.
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001198 */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001199 const mbedtls_md_type_t md_alg = mbedtls_md_get_type( ctx->md_info );
Manuel Pégourié-Gonnardbaccf802020-07-22 10:37:27 +02001200 /* TLS 1.0-1.2 only support SHA-384, SHA-256, SHA-1, MD-5,
1201 * all of which have the same block size except SHA-384. */
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001202 const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
Manuel Pégourié-Gonnard9713e132020-07-22 10:40:31 +02001203 const unsigned char * const ikey = ctx->hmac_ctx;
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001204 const unsigned char * const okey = ikey + block_size;
1205 const size_t hash_size = mbedtls_md_get_size( ctx->md_info );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001206
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001207 unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
1208 mbedtls_md_context_t aux;
1209 size_t offset;
Manuel Pégourié-Gonnarde0765f32020-07-22 12:22:51 +02001210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001211
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001212 mbedtls_md_init( &aux );
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001213
1214#define MD_CHK( func_call ) \
1215 do { \
1216 ret = (func_call); \
1217 if( ret != 0 ) \
1218 goto cleanup; \
1219 } while( 0 )
1220
1221 MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001222
1223 /* After hmac_start() of hmac_reset(), ikey has already been hashed,
1224 * so we can start directly with the message */
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001225 MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) );
1226 MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001227
1228 /* For each possible length, compute the hash up to that point */
1229 for( offset = min_data_len; offset <= max_data_len; offset++ )
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001230 {
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001231 MD_CHK( mbedtls_md_clone( &aux, ctx ) );
1232 MD_CHK( mbedtls_md_finish( &aux, aux_out ) );
Manuel Pégourié-Gonnard7a8b1e62020-07-15 11:52:14 +02001233 /* Keep only the correct inner_hash in the output buffer */
1234 mbedtls_ssl_cf_memcpy_if_eq( output, aux_out, hash_size,
1235 offset, data_len_secret );
1236
1237 if( offset < max_data_len )
Manuel Pégourié-Gonnard44c9fdd2020-07-22 10:48:47 +02001238 MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) );
Manuel Pégourié-Gonnard8aa29e32020-07-07 12:30:39 +02001239 }
1240
Manuel Pégourié-Gonnard21bfbdd2021-05-17 12:28:08 +02001241 /* The context needs to finish() before it starts() again */
1242 MD_CHK( mbedtls_md_finish( ctx, aux_out ) );
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 );
Joe Subbiani61f7d732021-06-24 09:06:23 +01002525
Joe Subbiani2bbafda2021-06-24 13:00:03 +01002526 ssl->out_msg[6] = MBEDTLS_BYTE_2( frag_off );
2527 ssl->out_msg[7] = MBEDTLS_BYTE_1( frag_off );
2528 ssl->out_msg[8] = MBEDTLS_BYTE_0( frag_off );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002529
Joe Subbiani2bbafda2021-06-24 13:00:03 +01002530 ssl->out_msg[ 9] = MBEDTLS_BYTE_2( cur_hs_frag_len );
2531 ssl->out_msg[10] = MBEDTLS_BYTE_1( cur_hs_frag_len );
2532 ssl->out_msg[11] = MBEDTLS_BYTE_0( cur_hs_frag_len );
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 {
Joe Subbianiad1115a2021-07-16 14:27:50 +01002729 ssl->out_msg[1] = MBEDTLS_BYTE_2( hs_len );
2730 ssl->out_msg[2] = MBEDTLS_BYTE_1( hs_len );
2731 ssl->out_msg[3] = MBEDTLS_BYTE_0( 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 {
Joe Subbianic54e9082021-07-19 11:56:54 +01002759 MBEDTLS_PUT_UINT16_BE( ssl->handshake->out_msg_seq, ssl->out_msg, 4 );
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002760 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002761 }
2762 else
2763 {
2764 ssl->out_msg[4] = 0;
2765 ssl->out_msg[5] = 0;
2766 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002767
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002768 /* Handshake hashes are computed without fragmentation,
2769 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002770 memset( ssl->out_msg + 6, 0x00, 3 );
2771 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002772 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002774
Hanno Becker0207e532018-08-28 10:28:28 +01002775 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02002776 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
2777 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002780 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002783 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2784 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002785 {
2786 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002789 return( ret );
2790 }
2791 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002792 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002793#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002794 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002795 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002796 {
2797 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2798 return( ret );
2799 }
2800 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002801
2802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
2803
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02002804 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002805}
2806
2807/*
2808 * Record layer functions
2809 */
2810
2811/*
2812 * Write current record.
2813 *
2814 * Uses:
2815 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
2816 * - ssl->out_msglen: length of the record content (excl headers)
2817 * - ssl->out_msg: record content
2818 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01002819int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002820{
2821 int ret, done = 0;
2822 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002823 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02002824
2825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002828 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002830 {
2831 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002834 return( ret );
2835 }
2836
2837 len = ssl->out_msglen;
2838 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00002840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
2842 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002844 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002846 ret = mbedtls_ssl_hw_record_write( ssl );
2847 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00002848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
2850 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002851 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002852
2853 if( ret == 0 )
2854 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002857 if( !done )
2858 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01002859 unsigned i;
2860 size_t protected_record_size;
Darryl Greenb33cc762019-11-28 14:29:44 +00002861#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2862 size_t out_buf_len = ssl->out_buf_len;
2863#else
2864 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
2865#endif
Hanno Becker6430faf2019-05-08 11:57:13 +01002866 /* Skip writing the record content type to after the encryption,
2867 * as it may change when using the CID extension. */
2868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02002870 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002871
Hanno Becker19859472018-08-06 09:40:20 +01002872 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Joe Subbianic54e9082021-07-19 11:56:54 +01002873 MBEDTLS_PUT_UINT16_BE( len, ssl->out_len, 0);
Paul Bakker05ef8352012-05-08 09:17:57 +00002874
Paul Bakker48916f92012-09-16 19:57:18 +00002875 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002876 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002877 mbedtls_record rec;
2878
2879 rec.buf = ssl->out_iv;
Darryl Greenb33cc762019-11-28 14:29:44 +00002880 rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002881 rec.data_len = ssl->out_msglen;
2882 rec.data_offset = ssl->out_msg - rec.buf;
2883
2884 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
2885 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
2886 ssl->conf->transport, rec.ver );
2887 rec.type = ssl->out_msgtype;
2888
Hanno Beckera0e20d02019-05-15 14:03:01 +01002889#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002890 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01002891 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002892#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002893
Hanno Beckera18d1322018-01-03 14:27:32 +00002894 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002895 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00002896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002897 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00002898 return( ret );
2899 }
2900
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002901 if( rec.data_offset != 0 )
2902 {
2903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2905 }
2906
Hanno Becker6430faf2019-05-08 11:57:13 +01002907 /* Update the record content type and CID. */
2908 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01002909#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01002910 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01002911#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00002912 ssl->out_msglen = len = rec.data_len;
Joe Subbianic54e9082021-07-19 11:56:54 +01002913 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->out_len, 0 );
Paul Bakker05ef8352012-05-08 09:17:57 +00002914 }
2915
Hanno Becker5903de42019-05-03 14:46:38 +01002916 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002917
2918#if defined(MBEDTLS_SSL_PROTO_DTLS)
2919 /* In case of DTLS, double-check that we don't exceed
2920 * the remaining space in the datagram. */
2921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
2922 {
Hanno Becker554b0af2018-08-22 20:33:41 +01002923 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002924 if( ret < 0 )
2925 return( ret );
2926
2927 if( protected_record_size > (size_t) ret )
2928 {
2929 /* Should never happen */
2930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2931 }
2932 }
2933#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00002934
Hanno Becker6430faf2019-05-08 11:57:13 +01002935 /* Now write the potentially updated record content type. */
2936 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2937
Paul Elliott9f352112020-12-09 14:55:45 +00002938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00002939 "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002940 ssl->out_hdr[0], ssl->out_hdr[1],
2941 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01002944 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002945
2946 ssl->out_left += protected_record_size;
2947 ssl->out_hdr += protected_record_size;
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00002948 mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out );
Hanno Becker2b1e3542018-08-06 11:19:13 +01002949
Hanno Beckerdd772292020-02-05 10:38:31 +00002950 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker04484622018-08-06 09:49:38 +01002951 if( ++ssl->cur_out_ctr[i - 1] != 0 )
2952 break;
2953
2954 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00002955 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker04484622018-08-06 09:49:38 +01002956 {
2957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2958 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
2959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002960 }
2961
Hanno Becker67bc7c32018-08-06 11:33:50 +01002962#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01002963 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
2964 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01002965 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01002966 size_t remaining;
2967 ret = ssl_get_remaining_payload_in_datagram( ssl );
2968 if( ret < 0 )
2969 {
2970 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
2971 ret );
2972 return( ret );
2973 }
2974
2975 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01002976 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01002977 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01002978 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01002979 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01002980 else
2981 {
Hanno Becker513815a2018-08-20 11:56:09 +01002982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01002983 }
2984 }
2985#endif /* MBEDTLS_SSL_PROTO_DTLS */
2986
2987 if( ( flush == SSL_FORCE_FLUSH ) &&
2988 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002990 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002991 return( ret );
2992 }
2993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
2996 return( 0 );
2997}
2998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01003000
3001static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
3002{
3003 if( ssl->in_msglen < ssl->in_hslen ||
3004 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3005 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
3006 {
3007 return( 1 );
3008 }
3009 return( 0 );
3010}
Hanno Becker44650b72018-08-16 12:51:11 +01003011
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003012static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003013{
3014 return( ( ssl->in_msg[9] << 16 ) |
3015 ( ssl->in_msg[10] << 8 ) |
3016 ssl->in_msg[11] );
3017}
3018
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003019static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003020{
3021 return( ( ssl->in_msg[6] << 16 ) |
3022 ( ssl->in_msg[7] << 8 ) |
3023 ssl->in_msg[8] );
3024}
3025
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003026static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01003027{
3028 uint32_t msg_len, frag_off, frag_len;
3029
3030 msg_len = ssl_get_hs_total_len( ssl );
3031 frag_off = ssl_get_hs_frag_off( ssl );
3032 frag_len = ssl_get_hs_frag_len( ssl );
3033
3034 if( frag_off > msg_len )
3035 return( -1 );
3036
3037 if( frag_len > msg_len - frag_off )
3038 return( -1 );
3039
3040 if( frag_len + 12 > ssl->in_msglen )
3041 return( -1 );
3042
3043 return( 0 );
3044}
3045
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003046/*
3047 * Mark bits in bitmask (used for DTLS HS reassembly)
3048 */
3049static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
3050{
3051 unsigned int start_bits, end_bits;
3052
3053 start_bits = 8 - ( offset % 8 );
3054 if( start_bits != 8 )
3055 {
3056 size_t first_byte_idx = offset / 8;
3057
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02003058 /* Special case */
3059 if( len <= start_bits )
3060 {
3061 for( ; len != 0; len-- )
3062 mask[first_byte_idx] |= 1 << ( start_bits - len );
3063
3064 /* Avoid potential issues with offset or len becoming invalid */
3065 return;
3066 }
3067
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003068 offset += start_bits; /* Now offset % 8 == 0 */
3069 len -= start_bits;
3070
3071 for( ; start_bits != 0; start_bits-- )
3072 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
3073 }
3074
3075 end_bits = len % 8;
3076 if( end_bits != 0 )
3077 {
3078 size_t last_byte_idx = ( offset + len ) / 8;
3079
3080 len -= end_bits; /* Now len % 8 == 0 */
3081
3082 for( ; end_bits != 0; end_bits-- )
3083 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
3084 }
3085
3086 memset( mask + offset / 8, 0xFF, len / 8 );
3087}
3088
3089/*
3090 * Check that bitmask is full
3091 */
3092static int ssl_bitmask_check( unsigned char *mask, size_t len )
3093{
3094 size_t i;
3095
3096 for( i = 0; i < len / 8; i++ )
3097 if( mask[i] != 0xFF )
3098 return( -1 );
3099
3100 for( i = 0; i < len % 8; i++ )
3101 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
3102 return( -1 );
3103
3104 return( 0 );
3105}
3106
Hanno Becker56e205e2018-08-16 09:06:12 +01003107/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01003108static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003109 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003110{
Hanno Becker56e205e2018-08-16 09:06:12 +01003111 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003112
Hanno Becker56e205e2018-08-16 09:06:12 +01003113 alloc_len = 12; /* Handshake header */
3114 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003115
Hanno Beckerd07df862018-08-16 09:14:58 +01003116 if( add_bitmap )
3117 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003118
Hanno Becker2a97b0e2018-08-21 15:47:49 +01003119 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003120}
Hanno Becker56e205e2018-08-16 09:06:12 +01003121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003123
Hanno Beckercd9dcda2018-08-28 17:18:56 +01003124static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01003125{
3126 return( ( ssl->in_msg[1] << 16 ) |
3127 ( ssl->in_msg[2] << 8 ) |
3128 ssl->in_msg[3] );
3129}
Hanno Beckere25e3b72018-08-16 09:30:53 +01003130
Simon Butcher99000142016-10-13 17:21:01 +01003131int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003134 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003136 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02003138 }
3139
Hanno Becker12555c62018-08-16 12:47:53 +01003140 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Paul Elliottd48d5c62021-01-07 14:47:05 +00003143 " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01003144 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003147 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003148 {
Janos Follath865b3eb2019-12-16 11:46:15 +00003149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003150 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003151
Hanno Becker44650b72018-08-16 12:51:11 +01003152 if( ssl_check_hs_header( ssl ) != 0 )
3153 {
3154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
3155 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3156 }
3157
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003158 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01003159 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
3160 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
3161 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3162 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003163 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01003164 if( recv_msg_seq > ssl->handshake->in_msg_seq )
3165 {
3166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
3167 recv_msg_seq,
3168 ssl->handshake->in_msg_seq ) );
3169 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
3170 }
3171
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02003172 /* Retransmit only on last message from previous flight, to avoid
3173 * too many retransmissions.
3174 * Besides, No sane server ever retransmits HelloVerifyRequest */
3175 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Paul Elliott9f352112020-12-09 14:55:45 +00003179 "message_seq = %u, start_of_flight = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003180 recv_msg_seq,
3181 ssl->handshake->in_flight_start_seq ) );
3182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003186 return( ret );
3187 }
3188 }
3189 else
3190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Paul Elliott9f352112020-12-09 14:55:45 +00003192 "message_seq = %u, expected = %u",
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003193 recv_msg_seq,
3194 ssl->handshake->in_msg_seq ) );
3195 }
3196
Hanno Becker90333da2017-10-10 11:27:13 +01003197 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003198 }
3199 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003200
Hanno Becker6d97ef52018-08-16 13:09:04 +01003201 /* Message reassembly is handled alongside buffering of future
3202 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01003203 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01003204 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01003205 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01003208 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003209 }
3210 }
3211 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003213 /* With TLS we don't handle fragmentation (for now) */
3214 if( ssl->in_msglen < ssl->in_hslen )
3215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
3217 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003218 }
3219
Simon Butcher99000142016-10-13 17:21:01 +01003220 return( 0 );
3221}
3222
3223void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
3224{
Hanno Becker0271f962018-08-16 13:23:47 +01003225 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01003226
Hanno Becker0271f962018-08-16 13:23:47 +01003227 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003228 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003229 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02003230 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003231
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003232 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003234 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003235 ssl->handshake != NULL )
3236 {
Hanno Becker0271f962018-08-16 13:23:47 +01003237 unsigned offset;
3238 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01003239
Hanno Becker0271f962018-08-16 13:23:47 +01003240 /* Increment handshake sequence number */
3241 hs->in_msg_seq++;
3242
3243 /*
3244 * Clear up handshake buffering and reassembly structure.
3245 */
3246
3247 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01003248 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01003249
3250 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01003251 for( offset = 0, hs_buf = &hs->buffering.hs[0];
3252 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01003253 offset++, hs_buf++ )
3254 {
3255 *hs_buf = *(hs_buf + 1);
3256 }
3257
3258 /* Create a fresh last entry */
3259 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003260 }
3261#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003262}
3263
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003264/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003265 * DTLS anti-replay: RFC 6347 4.1.2.6
3266 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003267 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3268 * Bit n is set iff record number in_window_top - n has been seen.
3269 *
3270 * Usually, in_window_top is the last record number seen and the lsb of
3271 * in_window is set. The only exception is the initial state (record number 0
3272 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00003275void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003276{
3277 ssl->in_window_top = 0;
3278 ssl->in_window = 0;
3279}
3280
3281static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3282{
3283 return( ( (uint64_t) buf[0] << 40 ) |
3284 ( (uint64_t) buf[1] << 32 ) |
3285 ( (uint64_t) buf[2] << 24 ) |
3286 ( (uint64_t) buf[3] << 16 ) |
3287 ( (uint64_t) buf[4] << 8 ) |
3288 ( (uint64_t) buf[5] ) );
3289}
3290
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003291static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr )
3292{
Janos Follath865b3eb2019-12-16 11:46:15 +00003293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003294 unsigned char *original_in_ctr;
3295
3296 // save original in_ctr
3297 original_in_ctr = ssl->in_ctr;
3298
3299 // use counter from record
3300 ssl->in_ctr = record_in_ctr;
3301
3302 ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl );
3303
3304 // restore the counter
3305 ssl->in_ctr = original_in_ctr;
3306
3307 return ret;
3308}
3309
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003310/*
3311 * Return 0 if sequence number is acceptable, -1 otherwise
3312 */
Hanno Becker0183d692019-07-12 08:50:37 +01003313int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003314{
3315 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3316 uint64_t bit;
3317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003318 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003319 return( 0 );
3320
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003321 if( rec_seqnum > ssl->in_window_top )
3322 return( 0 );
3323
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003324 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003325
3326 if( bit >= 64 )
3327 return( -1 );
3328
3329 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3330 return( -1 );
3331
3332 return( 0 );
3333}
3334
3335/*
3336 * Update replay window on new validated record
3337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003339{
3340 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3341
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003342 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003343 return;
3344
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003345 if( rec_seqnum > ssl->in_window_top )
3346 {
3347 /* Update window_top and the contents of the window */
3348 uint64_t shift = rec_seqnum - ssl->in_window_top;
3349
3350 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003351 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003352 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003353 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003354 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003355 ssl->in_window |= 1;
3356 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003357
3358 ssl->in_window_top = rec_seqnum;
3359 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003360 else
3361 {
3362 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003363 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003364
3365 if( bit < 64 ) /* Always true, but be extra sure */
3366 ssl->in_window |= (uint64_t) 1 << bit;
3367 }
3368}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003370
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003371#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003372/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003373 * Without any SSL context, check if a datagram looks like a ClientHello with
3374 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01003375 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003376 *
3377 * - if cookie is valid, return 0
3378 * - if ClientHello looks superficially valid but cookie is not,
3379 * fill obuf and set olen, then
3380 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
3381 * - otherwise return a specific error code
3382 */
3383static int ssl_check_dtls_clihlo_cookie(
3384 mbedtls_ssl_cookie_write_t *f_cookie_write,
3385 mbedtls_ssl_cookie_check_t *f_cookie_check,
3386 void *p_cookie,
3387 const unsigned char *cli_id, size_t cli_id_len,
3388 const unsigned char *in, size_t in_len,
3389 unsigned char *obuf, size_t buf_len, size_t *olen )
3390{
3391 size_t sid_len, cookie_len;
3392 unsigned char *p;
3393
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003394 /*
3395 * Structure of ClientHello with record and handshake headers,
3396 * and expected values. We don't need to check a lot, more checks will be
3397 * done when actually parsing the ClientHello - skipping those checks
3398 * avoids code duplication and does not make cookie forging any easier.
3399 *
3400 * 0-0 ContentType type; copied, must be handshake
3401 * 1-2 ProtocolVersion version; copied
3402 * 3-4 uint16 epoch; copied, must be 0
3403 * 5-10 uint48 sequence_number; copied
3404 * 11-12 uint16 length; (ignored)
3405 *
3406 * 13-13 HandshakeType msg_type; (ignored)
3407 * 14-16 uint24 length; (ignored)
3408 * 17-18 uint16 message_seq; copied
3409 * 19-21 uint24 fragment_offset; copied, must be 0
3410 * 22-24 uint24 fragment_length; (ignored)
3411 *
3412 * 25-26 ProtocolVersion client_version; (ignored)
3413 * 27-58 Random random; (ignored)
3414 * 59-xx SessionID session_id; 1 byte len + sid_len content
3415 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
3416 * ...
3417 *
3418 * Minimum length is 61 bytes.
3419 */
3420 if( in_len < 61 ||
3421 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
3422 in[3] != 0 || in[4] != 0 ||
3423 in[19] != 0 || in[20] != 0 || in[21] != 0 )
3424 {
3425 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3426 }
3427
3428 sid_len = in[59];
3429 if( sid_len > in_len - 61 )
3430 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3431
3432 cookie_len = in[60 + sid_len];
3433 if( cookie_len > in_len - 60 )
3434 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
3435
3436 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
3437 cli_id, cli_id_len ) == 0 )
3438 {
3439 /* Valid cookie */
3440 return( 0 );
3441 }
3442
3443 /*
3444 * If we get here, we've got an invalid cookie, let's prepare HVR.
3445 *
3446 * 0-0 ContentType type; copied
3447 * 1-2 ProtocolVersion version; copied
3448 * 3-4 uint16 epoch; copied
3449 * 5-10 uint48 sequence_number; copied
3450 * 11-12 uint16 length; olen - 13
3451 *
3452 * 13-13 HandshakeType msg_type; hello_verify_request
3453 * 14-16 uint24 length; olen - 25
3454 * 17-18 uint16 message_seq; copied
3455 * 19-21 uint24 fragment_offset; copied
3456 * 22-24 uint24 fragment_length; olen - 25
3457 *
3458 * 25-26 ProtocolVersion server_version; 0xfe 0xff
3459 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
3460 *
3461 * Minimum length is 28.
3462 */
3463 if( buf_len < 28 )
3464 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3465
3466 /* Copy most fields and adapt others */
3467 memcpy( obuf, in, 25 );
3468 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
3469 obuf[25] = 0xfe;
3470 obuf[26] = 0xff;
3471
3472 /* Generate and write actual cookie */
3473 p = obuf + 28;
3474 if( f_cookie_write( p_cookie,
3475 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
3476 {
3477 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3478 }
3479
3480 *olen = p - obuf;
3481
3482 /* Go back and fill length fields */
3483 obuf[27] = (unsigned char)( *olen - 28 );
3484
Joe Subbianiad1115a2021-07-16 14:27:50 +01003485 obuf[14] = obuf[22] = MBEDTLS_BYTE_2( *olen - 25 );
3486 obuf[15] = obuf[23] = MBEDTLS_BYTE_1( *olen - 25 );
3487 obuf[16] = obuf[24] = MBEDTLS_BYTE_0( *olen - 25 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003488
Joe Subbianic54e9082021-07-19 11:56:54 +01003489 MBEDTLS_PUT_UINT16_BE( *olen - 13, obuf, 11 );
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003490
3491 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
3492}
3493
3494/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003495 * Handle possible client reconnect with the same UDP quadruplet
3496 * (RFC 6347 Section 4.2.8).
3497 *
3498 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
3499 * that looks like a ClientHello.
3500 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003501 * - if the input looks like a ClientHello without cookies,
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003502 * send back HelloVerifyRequest, then return 0
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003503 * - if the input looks like a ClientHello with a valid cookie,
3504 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02003505 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003506 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003507 *
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003508 * This function is called (through ssl_check_client_reconnect()) when an
3509 * unexpected record is found in ssl_get_next_record(), which will discard the
3510 * record if we return 0, and bubble up the return value otherwise (this
3511 * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected
3512 * errors, and is the right thing to do in both cases).
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003513 */
3514static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
3515{
Janos Follath865b3eb2019-12-16 11:46:15 +00003516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003517 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003518
Hanno Becker2fddd372019-07-10 14:37:41 +01003519 if( ssl->conf->f_cookie_write == NULL ||
3520 ssl->conf->f_cookie_check == NULL )
3521 {
3522 /* If we can't use cookies to verify reachability of the peer,
3523 * drop the record. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, "
3525 "can't check reconnect validity" ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01003526 return( 0 );
3527 }
3528
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003529 ret = ssl_check_dtls_clihlo_cookie(
3530 ssl->conf->f_cookie_write,
3531 ssl->conf->f_cookie_check,
3532 ssl->conf->p_cookie,
3533 ssl->cli_id, ssl->cli_id_len,
3534 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10003535 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003536
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003537 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
3538
3539 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003540 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003541 int send_ret;
3542 MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) );
3543 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
3544 ssl->out_buf, len );
Brian J Murray1903fb32016-11-06 04:45:15 -08003545 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003546 * If the error is permanent we'll catch it later,
3547 * if it's not, then hopefully it'll work next time. */
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003548 send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len );
3549 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret );
3550 (void) send_ret;
3551
Manuel Pégourié-Gonnard824655c2020-03-11 12:51:42 +01003552 return( 0 );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003553 }
3554
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003555 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003556 {
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02003557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) );
Hanno Becker43aefe22020-02-05 10:44:56 +00003558 if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02003559 {
3560 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
3561 return( ret );
3562 }
3563
3564 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003565 }
3566
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003567 return( ret );
3568}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02003569#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02003570
Hanno Beckerf661c9c2019-05-03 13:25:54 +01003571static int ssl_check_record_type( uint8_t record_type )
3572{
3573 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
3574 record_type != MBEDTLS_SSL_MSG_ALERT &&
3575 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
3576 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
3577 {
3578 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3579 }
3580
3581 return( 0 );
3582}
3583
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003584/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003585 * ContentType type;
3586 * ProtocolVersion version;
3587 * uint16 epoch; // DTLS only
3588 * uint48 sequence_number; // DTLS only
3589 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003590 *
3591 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00003592 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003593 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
3594 *
3595 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00003596 * 1. proceed with the record if this function returns 0
3597 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
3598 * 3. return CLIENT_RECONNECT if this function return that value
3599 * 4. drop the whole datagram if this function returns anything else.
3600 * Point 2 is needed when the peer is resending, and we have already received
3601 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003602 */
Hanno Becker331de3d2019-07-12 11:10:16 +01003603static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003604 unsigned char *buf,
3605 size_t len,
3606 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00003607{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003608 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Hanno Beckere5e7e782019-07-11 12:29:35 +01003610 size_t const rec_hdr_type_offset = 0;
3611 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003612
Hanno Beckere5e7e782019-07-11 12:29:35 +01003613 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
3614 rec_hdr_type_len;
3615 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
Hanno Beckere5e7e782019-07-11 12:29:35 +01003617 size_t const rec_hdr_ctr_len = 8;
3618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01003619 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003620 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
3621 rec_hdr_version_len;
3622
Hanno Beckera0e20d02019-05-15 14:03:01 +01003623#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01003624 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
3625 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01003626 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003627#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3628#endif /* MBEDTLS_SSL_PROTO_DTLS */
3629
3630 size_t rec_hdr_len_offset; /* To be determined */
3631 size_t const rec_hdr_len_len = 2;
3632
3633 /*
3634 * Check minimum lengths for record header.
3635 */
3636
3637#if defined(MBEDTLS_SSL_PROTO_DTLS)
3638 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3639 {
3640 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
3641 }
3642 else
3643#endif /* MBEDTLS_SSL_PROTO_DTLS */
3644 {
3645 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
3646 }
3647
3648 if( len < rec_hdr_len_offset + rec_hdr_len_len )
3649 {
3650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
3651 (unsigned) len,
3652 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
3653 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3654 }
3655
3656 /*
3657 * Parse and validate record content type
3658 */
3659
3660 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003661
3662 /* Check record content type */
3663#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3664 rec->cid_len = 0;
3665
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01003667 ssl->conf->cid_len != 0 &&
3668 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003669 {
3670 /* Shift pointers to account for record header including CID
3671 * struct {
3672 * ContentType special_type = tls12_cid;
3673 * ProtocolVersion version;
3674 * uint16 epoch;
3675 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01003676 * opaque cid[cid_length]; // Additional field compared to
3677 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003678 * uint16 length;
3679 * opaque enc_content[DTLSCiphertext.length];
3680 * } DTLSCiphertext;
3681 */
3682
3683 /* So far, we only support static CID lengths
3684 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003685 rec_hdr_cid_len = ssl->conf->cid_len;
3686 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01003687
Hanno Beckere5e7e782019-07-11 12:29:35 +01003688 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01003689 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
3691 (unsigned) len,
3692 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01003693 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01003694 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003695
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02003696 /* configured CID len is guaranteed at most 255, see
3697 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
3698 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01003699 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003700 }
3701 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01003702#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003703 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003704 if( ssl_check_record_type( rec->type ) )
3705 {
Hanno Becker54229812019-07-12 14:40:00 +01003706 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
3707 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003708 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3709 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003710 }
3711
Hanno Beckere5e7e782019-07-11 12:29:35 +01003712 /*
3713 * Parse and validate record version
3714 */
3715
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003716 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
3717 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01003718 mbedtls_ssl_read_version( &major_ver, &minor_ver,
3719 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01003720 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003721
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003722 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
3725 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003726 }
3727
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003728 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003730 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
3731 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 }
3733
Hanno Beckere5e7e782019-07-11 12:29:35 +01003734 /*
3735 * Parse/Copy record sequence number.
3736 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003737
Hanno Beckere5e7e782019-07-11 12:29:35 +01003738#if defined(MBEDTLS_SSL_PROTO_DTLS)
3739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003740 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003741 /* Copy explicit record sequence number from input buffer. */
3742 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
3743 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003744 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01003745 else
3746#endif /* MBEDTLS_SSL_PROTO_DTLS */
3747 {
3748 /* Copy implicit record sequence number from SSL context structure. */
3749 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
3750 }
Paul Bakker40e46942009-01-03 21:51:57 +00003751
Hanno Beckere5e7e782019-07-11 12:29:35 +01003752 /*
3753 * Parse record length.
3754 */
3755
Hanno Beckere5e7e782019-07-11 12:29:35 +01003756 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01003757 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
3758 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01003759 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00003760
Paul Elliott9f352112020-12-09 14:55:45 +00003761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, "
Paul Elliottd48d5c62021-01-07 14:47:05 +00003762 "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere5e7e782019-07-11 12:29:35 +01003763 rec->type,
3764 major_ver, minor_ver, rec->data_len ) );
3765
3766 rec->buf = buf;
3767 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01003768
Hanno Beckerd417cc92019-07-26 08:20:27 +01003769 if( rec->data_len == 0 )
3770 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003771
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003772 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01003773 * DTLS-related tests.
3774 * Check epoch before checking length constraint because
3775 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
3776 * message gets duplicated before the corresponding Finished message,
3777 * the second ChangeCipherSpec should be discarded because it belongs
3778 * to an old epoch, but not because its length is shorter than
3779 * the minimum record length for packets using the new record transform.
3780 * Note that these two kinds of failures are handled differently,
3781 * as an unexpected record is silently skipped but an invalid
3782 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003783 */
3784#if defined(MBEDTLS_SSL_PROTO_DTLS)
3785 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3786 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003787 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003788
Hanno Becker955a5c92019-07-10 17:12:07 +01003789 /* Check that the datagram is large enough to contain a record
3790 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01003791 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01003792 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01003793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
3794 (unsigned) len,
3795 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01003796 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3797 }
Hanno Becker37cfe732019-07-10 17:20:01 +01003798
Hanno Becker37cfe732019-07-10 17:20:01 +01003799 /* Records from other, non-matching epochs are silently discarded.
3800 * (The case of same-port Client reconnects must be considered in
3801 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003802 if( rec_epoch != ssl->in_epoch )
3803 {
3804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Paul Elliott9f352112020-12-09 14:55:45 +00003805 "expected %u, received %lu",
3806 ssl->in_epoch, (unsigned long) rec_epoch ) );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003807
Hanno Becker552f7472019-07-19 10:59:12 +01003808 /* Records from the next epoch are considered for buffering
3809 * (concretely: early Finished messages). */
3810 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003811 {
Hanno Becker552f7472019-07-19 10:59:12 +01003812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
3813 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003814 }
Hanno Becker5f066e72018-08-16 14:56:31 +01003815
Hanno Becker2fddd372019-07-10 14:37:41 +01003816 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003817 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003818#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01003819 /* For records from the correct epoch, check whether their
3820 * sequence number has been seen before. */
Arto Kinnunen7f8089b2019-10-29 11:13:33 +02003821 else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl,
3822 &rec->ctr[0] ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01003823 {
3824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3825 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
3826 }
3827#endif
3828 }
3829#endif /* MBEDTLS_SSL_PROTO_DTLS */
3830
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003831 return( 0 );
3832}
Paul Bakker5121ce52009-01-03 21:22:43 +00003833
Paul Bakker5121ce52009-01-03 21:22:43 +00003834
Hanno Becker2fddd372019-07-10 14:37:41 +01003835#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
3836static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
3837{
3838 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
3839
3840 /*
3841 * Check for an epoch 0 ClientHello. We can't use in_msg here to
3842 * access the first byte of record content (handshake type), as we
3843 * have an active transform (possibly iv_len != 0), so use the
3844 * fact that the record header len is 13 instead.
3845 */
3846 if( rec_epoch == 0 &&
3847 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
3848 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
3849 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3850 ssl->in_left > 13 &&
3851 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
3852 {
3853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
3854 "from the same port" ) );
3855 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 }
3857
3858 return( 0 );
3859}
Hanno Becker2fddd372019-07-10 14:37:41 +01003860#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003861
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003862/*
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01003863 * If applicable, decrypt record content
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003864 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01003865static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
3866 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003867{
3868 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01003871 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3874 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00003877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003878 ret = mbedtls_ssl_hw_record_read( ssl );
3879 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00003880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
3882 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003883 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003884
3885 if( ret == 0 )
3886 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003887 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003889 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003890 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003891 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003892
Hanno Beckera18d1322018-01-03 14:27:32 +00003893 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01003894 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01003897
Hanno Beckera0e20d02019-05-15 14:03:01 +01003898#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01003899 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
3900 ssl->conf->ignore_unexpected_cid
3901 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
3902 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01003903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01003904 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01003905 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003906#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01003907
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 return( ret );
3909 }
3910
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003911 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01003912 {
3913 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003914 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01003915 }
3916
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003917 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003918 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01003919
Hanno Beckera0e20d02019-05-15 14:03:01 +01003920#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01003921 /* We have already checked the record content type
3922 * in ssl_parse_record_header(), failing or silently
3923 * dropping the record in the case of an unknown type.
3924 *
3925 * Since with the use of CIDs, the record content type
3926 * might change during decryption, re-check the record
3927 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003928 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01003929 {
3930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
3931 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3932 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003933#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01003934
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003935 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003936 {
3937#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
3938 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01003939 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003940 {
3941 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
3942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
3943 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3944 }
3945#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3946
3947 ssl->nb_zero++;
3948
3949 /*
3950 * Three or more empty messages may be a DoS attack
3951 * (excessive CPU consumption).
3952 */
3953 if( ssl->nb_zero > 3 )
3954 {
3955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01003956 "messages, possible DoS attack" ) );
3957 /* Treat the records as if they were not properly authenticated,
3958 * thereby failing the connection if we see more than allowed
3959 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003960 return( MBEDTLS_ERR_SSL_INVALID_MAC );
3961 }
3962 }
3963 else
3964 ssl->nb_zero = 0;
3965
3966#if defined(MBEDTLS_SSL_PROTO_DTLS)
3967 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
3968 {
3969 ; /* in_ctr read from peer, not maintained internally */
3970 }
3971 else
3972#endif
3973 {
3974 unsigned i;
Hanno Beckerdd772292020-02-05 10:38:31 +00003975 for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003976 if( ++ssl->in_ctr[i - 1] != 0 )
3977 break;
3978
3979 /* The loop goes to its end iff the counter is wrapping */
Hanno Beckerdd772292020-02-05 10:38:31 +00003980 if( i == mbedtls_ssl_ep_len( ssl ) )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003981 {
3982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
3983 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
3984 }
3985 }
3986
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 }
3988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003990 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003992 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003993 }
3994#endif
3995
Hanno Beckerd96e10b2019-07-09 17:30:02 +01003996 /* Check actual (decrypted) record content length against
3997 * configured maximum. */
3998 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
3999 {
4000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4001 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4002 }
4003
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004004 return( 0 );
4005}
4006
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004007/*
4008 * Read a record.
4009 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004010 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
4011 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
4012 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004013 */
Hanno Becker1097b342018-08-15 14:09:41 +01004014
4015/* Helper functions for mbedtls_ssl_read_record(). */
4016static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01004017static int ssl_get_next_record( mbedtls_ssl_context *ssl );
4018static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01004019
Hanno Becker327c93b2018-08-15 13:56:18 +01004020int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01004021 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004022{
Janos Follath865b3eb2019-12-16 11:46:15 +00004023 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004026
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004027 if( ssl->keep_current_message == 0 )
4028 {
4029 do {
Simon Butcher99000142016-10-13 17:21:01 +01004030
Hanno Becker26994592018-08-15 14:14:59 +01004031 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01004032 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004033 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01004034
Hanno Beckere74d5562018-08-15 14:26:08 +01004035 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004036 {
Hanno Becker40f50842018-08-15 14:48:01 +01004037#if defined(MBEDTLS_SSL_PROTO_DTLS)
4038 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01004039
Hanno Becker40f50842018-08-15 14:48:01 +01004040 /* We only check for buffered messages if the
4041 * current datagram is fully consumed. */
4042 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004043 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004044 {
Hanno Becker40f50842018-08-15 14:48:01 +01004045 if( ssl_load_buffered_message( ssl ) == 0 )
4046 have_buffered = 1;
4047 }
4048
4049 if( have_buffered == 0 )
4050#endif /* MBEDTLS_SSL_PROTO_DTLS */
4051 {
4052 ret = ssl_get_next_record( ssl );
4053 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
4054 continue;
4055
4056 if( ret != 0 )
4057 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01004058 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004059 return( ret );
4060 }
Hanno Beckere74d5562018-08-15 14:26:08 +01004061 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004062 }
4063
4064 ret = mbedtls_ssl_handle_message_type( ssl );
4065
Hanno Becker40f50842018-08-15 14:48:01 +01004066#if defined(MBEDTLS_SSL_PROTO_DTLS)
4067 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4068 {
4069 /* Buffer future message */
4070 ret = ssl_buffer_message( ssl );
4071 if( ret != 0 )
4072 return( ret );
4073
4074 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
4075 }
4076#endif /* MBEDTLS_SSL_PROTO_DTLS */
4077
Hanno Becker90333da2017-10-10 11:27:13 +01004078 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
4079 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004080
4081 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01004082 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00004083 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01004084 return( ret );
4085 }
4086
Hanno Becker327c93b2018-08-15 13:56:18 +01004087 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01004088 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004089 {
4090 mbedtls_ssl_update_handshake_status( ssl );
4091 }
Simon Butcher99000142016-10-13 17:21:01 +01004092 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004093 else
Simon Butcher99000142016-10-13 17:21:01 +01004094 {
Hanno Becker02f59072018-08-15 14:00:24 +01004095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01004096 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01004097 }
4098
4099 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
4100
4101 return( 0 );
4102}
4103
Hanno Becker40f50842018-08-15 14:48:01 +01004104#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004105static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01004106{
Hanno Becker40f50842018-08-15 14:48:01 +01004107 if( ssl->in_left > ssl->next_record_offset )
4108 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01004109
Hanno Becker40f50842018-08-15 14:48:01 +01004110 return( 0 );
4111}
4112
4113static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
4114{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004115 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01004116 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004117 int ret = 0;
4118
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004119 if( hs == NULL )
4120 return( -1 );
4121
Hanno Beckere00ae372018-08-20 09:39:42 +01004122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
4123
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004124 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
4125 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4126 {
4127 /* Check if we have seen a ChangeCipherSpec before.
4128 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004129 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004130 {
4131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
4132 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01004133 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004134 }
4135
Hanno Becker39b8bc92018-08-28 17:17:13 +01004136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004137 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
4138 ssl->in_msglen = 1;
4139 ssl->in_msg[0] = 1;
4140
4141 /* As long as they are equal, the exact value doesn't matter. */
4142 ssl->in_left = 0;
4143 ssl->next_record_offset = 0;
4144
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004145 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004146 goto exit;
4147 }
Hanno Becker37f95322018-08-16 13:55:32 +01004148
Hanno Beckerb8f50142018-08-28 10:01:34 +01004149#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01004150 /* Debug only */
4151 {
4152 unsigned offset;
4153 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
4154 {
4155 hs_buf = &hs->buffering.hs[offset];
4156 if( hs_buf->is_valid == 1 )
4157 {
4158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
4159 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01004160 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01004161 }
4162 }
4163 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01004164#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01004165
4166 /* Check if we have buffered and/or fully reassembled the
4167 * next handshake message. */
4168 hs_buf = &hs->buffering.hs[0];
4169 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
4170 {
4171 /* Synthesize a record containing the buffered HS message. */
4172 size_t msg_len = ( hs_buf->data[1] << 16 ) |
4173 ( hs_buf->data[2] << 8 ) |
4174 hs_buf->data[3];
4175
4176 /* Double-check that we haven't accidentally buffered
4177 * a message that doesn't fit into the input buffer. */
4178 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
4179 {
4180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4182 }
4183
4184 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
4185 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
4186 hs_buf->data, msg_len + 12 );
4187
4188 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
4189 ssl->in_hslen = msg_len + 12;
4190 ssl->in_msglen = msg_len + 12;
4191 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
4192
4193 ret = 0;
4194 goto exit;
4195 }
4196 else
4197 {
4198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
4199 hs->in_msg_seq ) );
4200 }
4201
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004202 ret = -1;
4203
4204exit:
4205
4206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
4207 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004208}
4209
Hanno Beckera02b0b42018-08-21 17:20:27 +01004210static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
4211 size_t desired )
4212{
4213 int offset;
4214 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
4216 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004217
Hanno Becker01315ea2018-08-21 17:22:17 +01004218 /* Get rid of future records epoch first, if such exist. */
4219 ssl_free_buffered_record( ssl );
4220
4221 /* Check if we have enough space available now. */
4222 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4223 hs->buffering.total_bytes_buffered ) )
4224 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01004226 return( 0 );
4227 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01004228
Hanno Becker4f432ad2018-08-28 10:02:32 +01004229 /* We don't have enough space to buffer the next expected handshake
4230 * message. Remove buffers used for future messages to gain space,
4231 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01004232 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
4233 offset >= 0; offset-- )
4234 {
4235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
4236 offset ) );
4237
Hanno Beckerb309b922018-08-23 13:18:05 +01004238 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004239
4240 /* Check if we have enough space available now. */
4241 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4242 hs->buffering.total_bytes_buffered ) )
4243 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01004244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01004245 return( 0 );
4246 }
4247 }
4248
4249 return( -1 );
4250}
4251
Hanno Becker40f50842018-08-15 14:48:01 +01004252static int ssl_buffer_message( mbedtls_ssl_context *ssl )
4253{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004254 int ret = 0;
4255 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4256
4257 if( hs == NULL )
4258 return( 0 );
4259
4260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
4261
4262 switch( ssl->in_msgtype )
4263 {
4264 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
4265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01004266
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004267 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004268 break;
4269
4270 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01004271 {
4272 unsigned recv_msg_seq_offset;
4273 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
4274 mbedtls_ssl_hs_buffer *hs_buf;
4275 size_t msg_len = ssl->in_hslen - 12;
4276
4277 /* We should never receive an old handshake
4278 * message - double-check nonetheless. */
4279 if( recv_msg_seq < ssl->handshake->in_msg_seq )
4280 {
4281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4282 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4283 }
4284
4285 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
4286 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
4287 {
4288 /* Silently ignore -- message too far in the future */
4289 MBEDTLS_SSL_DEBUG_MSG( 2,
4290 ( "Ignore future HS message with sequence number %u, "
4291 "buffering window %u - %u",
4292 recv_msg_seq, ssl->handshake->in_msg_seq,
4293 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
4294
4295 goto exit;
4296 }
4297
4298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
4299 recv_msg_seq, recv_msg_seq_offset ) );
4300
4301 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
4302
4303 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01004304 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01004305 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004306 size_t reassembly_buf_sz;
4307
Hanno Becker37f95322018-08-16 13:55:32 +01004308 hs_buf->is_fragmented =
4309 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
4310
4311 /* We copy the message back into the input buffer
4312 * after reassembly, so check that it's not too large.
4313 * This is an implementation-specific limitation
4314 * and not one from the standard, hence it is not
4315 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01004316 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01004317 {
4318 /* Ignore message */
4319 goto exit;
4320 }
4321
Hanno Beckere0b150f2018-08-21 15:51:03 +01004322 /* Check if we have enough space to buffer the message. */
4323 if( hs->buffering.total_bytes_buffered >
4324 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
4325 {
4326 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4327 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4328 }
4329
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004330 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
4331 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004332
4333 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
4334 hs->buffering.total_bytes_buffered ) )
4335 {
4336 if( recv_msg_seq_offset > 0 )
4337 {
4338 /* If we can't buffer a future message because
4339 * of space limitations -- ignore. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4341 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4342 " (already %" MBEDTLS_PRINTF_SIZET
4343 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004344 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004345 hs->buffering.total_bytes_buffered ) );
Hanno Beckere0b150f2018-08-21 15:51:03 +01004346 goto exit;
4347 }
Hanno Beckere1801392018-08-21 16:51:05 +01004348 else
4349 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET
4351 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4352 " (already %" MBEDTLS_PRINTF_SIZET
4353 " bytes buffered) -- attempt to make space by freeing buffered future messages\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004354 msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004355 hs->buffering.total_bytes_buffered ) );
Hanno Beckere1801392018-08-21 16:51:05 +01004356 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004357
Hanno Beckera02b0b42018-08-21 17:20:27 +01004358 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004359 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET
4361 " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed"
4362 " the compile-time limit %" MBEDTLS_PRINTF_SIZET
4363 " (already %" MBEDTLS_PRINTF_SIZET
4364 " bytes buffered) -- fail\n",
Paul Elliott9f352112020-12-09 14:55:45 +00004365 msg_len,
4366 reassembly_buf_sz,
Paul Elliott3891caf2020-12-17 18:42:40 +00004367 (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004368 hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01004369 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4370 goto exit;
4371 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004372 }
4373
Paul Elliottd48d5c62021-01-07 14:47:05 +00004374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere0b150f2018-08-21 15:51:03 +01004375 msg_len ) );
4376
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004377 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
4378 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01004379 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01004380 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01004381 goto exit;
4382 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01004383 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004384
4385 /* Prepare final header: copy msg_type, length and message_seq,
4386 * then add standardised fragment_offset and fragment_length */
4387 memcpy( hs_buf->data, ssl->in_msg, 6 );
4388 memset( hs_buf->data + 6, 0, 3 );
4389 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
4390
4391 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01004392
4393 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01004394 }
4395 else
4396 {
4397 /* Make sure msg_type and length are consistent */
4398 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
4399 {
4400 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
4401 /* Ignore */
4402 goto exit;
4403 }
4404 }
4405
Hanno Becker4422bbb2018-08-20 09:40:19 +01004406 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01004407 {
4408 size_t frag_len, frag_off;
4409 unsigned char * const msg = hs_buf->data + 12;
4410
4411 /*
4412 * Check and copy current fragment
4413 */
4414
4415 /* Validation of header fields already done in
4416 * mbedtls_ssl_prepare_handshake_record(). */
4417 frag_off = ssl_get_hs_frag_off( ssl );
4418 frag_len = ssl_get_hs_frag_len( ssl );
4419
Paul Elliottd48d5c62021-01-07 14:47:05 +00004420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET
4421 ", length = %" MBEDTLS_PRINTF_SIZET,
Hanno Becker37f95322018-08-16 13:55:32 +01004422 frag_off, frag_len ) );
4423 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
4424
4425 if( hs_buf->is_fragmented )
4426 {
4427 unsigned char * const bitmask = msg + msg_len;
4428 ssl_bitmask_set( bitmask, frag_off, frag_len );
4429 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
4430 msg_len ) == 0 );
4431 }
4432 else
4433 {
4434 hs_buf->is_complete = 1;
4435 }
4436
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
4438 hs_buf->is_complete ? "" : "not yet " ) );
4439 }
4440
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004441 break;
Hanno Becker37f95322018-08-16 13:55:32 +01004442 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004443
4444 default:
Hanno Becker360bef32018-08-28 10:04:33 +01004445 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004446 break;
4447 }
4448
4449exit:
4450
4451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
4452 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01004453}
4454#endif /* MBEDTLS_SSL_PROTO_DTLS */
4455
Hanno Becker1097b342018-08-15 14:09:41 +01004456static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004457{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004458 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01004459 * Consume last content-layer message and potentially
4460 * update in_msglen which keeps track of the contents'
4461 * consumption state.
4462 *
4463 * (1) Handshake messages:
4464 * Remove last handshake message, move content
4465 * and adapt in_msglen.
4466 *
4467 * (2) Alert messages:
4468 * Consume whole record content, in_msglen = 0.
4469 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01004470 * (3) Change cipher spec:
4471 * Consume whole record content, in_msglen = 0.
4472 *
4473 * (4) Application data:
4474 * Don't do anything - the record layer provides
4475 * the application data as a stream transport
4476 * and consumes through mbedtls_ssl_read only.
4477 *
4478 */
4479
4480 /* Case (1): Handshake messages */
4481 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004482 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004483 /* Hard assertion to be sure that no application data
4484 * is in flight, as corrupting ssl->in_msglen during
4485 * ssl->in_offt != NULL is fatal. */
4486 if( ssl->in_offt != NULL )
4487 {
4488 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4490 }
4491
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004492 /*
4493 * Get next Handshake message in the current record
4494 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004495
Hanno Becker4a810fb2017-05-24 16:27:30 +01004496 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01004497 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01004498 * current handshake content: If DTLS handshake
4499 * fragmentation is used, that's the fragment
4500 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01004501 * size here is faulty and should be changed at
4502 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004503 * (2) While it doesn't seem to cause problems, one
4504 * has to be very careful not to assume that in_hslen
4505 * is always <= in_msglen in a sensible communication.
4506 * Again, it's wrong for DTLS handshake fragmentation.
4507 * The following check is therefore mandatory, and
4508 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01004509 * Additionally, ssl->in_hslen might be arbitrarily out of
4510 * bounds after handling a DTLS message with an unexpected
4511 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01004512 */
4513 if( ssl->in_hslen < ssl->in_msglen )
4514 {
4515 ssl->in_msglen -= ssl->in_hslen;
4516 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
4517 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004518
Hanno Becker4a810fb2017-05-24 16:27:30 +01004519 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
4520 ssl->in_msg, ssl->in_msglen );
4521 }
4522 else
4523 {
4524 ssl->in_msglen = 0;
4525 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02004526
Hanno Becker4a810fb2017-05-24 16:27:30 +01004527 ssl->in_hslen = 0;
4528 }
4529 /* Case (4): Application data */
4530 else if( ssl->in_offt != NULL )
4531 {
4532 return( 0 );
4533 }
4534 /* Everything else (CCS & Alerts) */
4535 else
4536 {
4537 ssl->in_msglen = 0;
4538 }
4539
Hanno Becker1097b342018-08-15 14:09:41 +01004540 return( 0 );
4541}
Hanno Becker4a810fb2017-05-24 16:27:30 +01004542
Hanno Beckere74d5562018-08-15 14:26:08 +01004543static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
4544{
Hanno Becker4a810fb2017-05-24 16:27:30 +01004545 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01004546 return( 1 );
4547
4548 return( 0 );
4549}
4550
Hanno Becker5f066e72018-08-16 14:56:31 +01004551#if defined(MBEDTLS_SSL_PROTO_DTLS)
4552
4553static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
4554{
4555 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4556 if( hs == NULL )
4557 return;
4558
Hanno Becker01315ea2018-08-21 17:22:17 +01004559 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01004560 {
Hanno Becker01315ea2018-08-21 17:22:17 +01004561 hs->buffering.total_bytes_buffered -=
4562 hs->buffering.future_record.len;
4563
4564 mbedtls_free( hs->buffering.future_record.data );
4565 hs->buffering.future_record.data = NULL;
4566 }
Hanno Becker5f066e72018-08-16 14:56:31 +01004567}
4568
4569static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
4570{
4571 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
4572 unsigned char * rec;
4573 size_t rec_len;
4574 unsigned rec_epoch;
Darryl Greenb33cc762019-11-28 14:29:44 +00004575#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4576 size_t in_buf_len = ssl->in_buf_len;
4577#else
4578 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4579#endif
Hanno Becker5f066e72018-08-16 14:56:31 +01004580 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4581 return( 0 );
4582
4583 if( hs == NULL )
4584 return( 0 );
4585
Hanno Becker5f066e72018-08-16 14:56:31 +01004586 rec = hs->buffering.future_record.data;
4587 rec_len = hs->buffering.future_record.len;
4588 rec_epoch = hs->buffering.future_record.epoch;
4589
4590 if( rec == NULL )
4591 return( 0 );
4592
Hanno Becker4cb782d2018-08-20 11:19:05 +01004593 /* Only consider loading future records if the
4594 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01004595 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01004596 return( 0 );
4597
Hanno Becker5f066e72018-08-16 14:56:31 +01004598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
4599
4600 if( rec_epoch != ssl->in_epoch )
4601 {
4602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
4603 goto exit;
4604 }
4605
4606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
4607
4608 /* Double-check that the record is not too large */
Darryl Greenb33cc762019-11-28 14:29:44 +00004609 if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Hanno Becker5f066e72018-08-16 14:56:31 +01004610 {
4611 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4612 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4613 }
4614
4615 memcpy( ssl->in_hdr, rec, rec_len );
4616 ssl->in_left = rec_len;
4617 ssl->next_record_offset = 0;
4618
4619 ssl_free_buffered_record( ssl );
4620
4621exit:
4622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
4623 return( 0 );
4624}
4625
Hanno Becker519f15d2019-07-11 12:43:20 +01004626static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
4627 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01004628{
4629 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01004630
4631 /* Don't buffer future records outside handshakes. */
4632 if( hs == NULL )
4633 return( 0 );
4634
4635 /* Only buffer handshake records (we are only interested
4636 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01004637 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01004638 return( 0 );
4639
4640 /* Don't buffer more than one future epoch record. */
4641 if( hs->buffering.future_record.data != NULL )
4642 return( 0 );
4643
Hanno Becker01315ea2018-08-21 17:22:17 +01004644 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01004645 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01004646 hs->buffering.total_bytes_buffered ) )
4647 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004648 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET
4649 " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET
4650 " (already %" MBEDTLS_PRINTF_SIZET
4651 " bytes buffered) -- ignore\n",
Paul Elliott3891caf2020-12-17 18:42:40 +00004652 rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Paul Elliott9f352112020-12-09 14:55:45 +00004653 hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004654 return( 0 );
4655 }
4656
Hanno Becker5f066e72018-08-16 14:56:31 +01004657 /* Buffer record */
4658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
Paul Elliott9f352112020-12-09 14:55:45 +00004659 ssl->in_epoch + 1U ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01004660 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004661
4662 /* ssl_parse_record_header() only considers records
4663 * of the next epoch as candidates for buffering. */
4664 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01004665 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004666
4667 hs->buffering.future_record.data =
4668 mbedtls_calloc( 1, hs->buffering.future_record.len );
4669 if( hs->buffering.future_record.data == NULL )
4670 {
4671 /* If we run out of RAM trying to buffer a
4672 * record from the next epoch, just ignore. */
4673 return( 0 );
4674 }
4675
Hanno Becker519f15d2019-07-11 12:43:20 +01004676 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01004677
Hanno Becker519f15d2019-07-11 12:43:20 +01004678 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01004679 return( 0 );
4680}
4681
4682#endif /* MBEDTLS_SSL_PROTO_DTLS */
4683
Hanno Beckere74d5562018-08-15 14:26:08 +01004684static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01004685{
Janos Follath865b3eb2019-12-16 11:46:15 +00004686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckere5e7e782019-07-11 12:29:35 +01004687 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01004688
Hanno Becker5f066e72018-08-16 14:56:31 +01004689#if defined(MBEDTLS_SSL_PROTO_DTLS)
4690 /* We might have buffered a future record; if so,
4691 * and if the epoch matches now, load it.
4692 * On success, this call will set ssl->in_left to
4693 * the length of the buffered record, so that
4694 * the calls to ssl_fetch_input() below will
4695 * essentially be no-ops. */
4696 ret = ssl_load_buffered_record( ssl );
4697 if( ret != 0 )
4698 return( ret );
4699#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01004700
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004701 /* Ensure that we have enough space available for the default form
4702 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
4703 * with no space for CIDs counted in). */
4704 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4705 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004708 return( ret );
4709 }
4710
Hanno Beckere5e7e782019-07-11 12:29:35 +01004711 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
4712 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004714#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01004715 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004716 {
Hanno Becker5f066e72018-08-16 14:56:31 +01004717 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
4718 {
Hanno Becker519f15d2019-07-11 12:43:20 +01004719 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01004720 if( ret != 0 )
4721 return( ret );
4722
4723 /* Fall through to handling of unexpected records */
4724 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
4725 }
4726
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004727 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
4728 {
Hanno Becker2fddd372019-07-10 14:37:41 +01004729#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004730 /* Reset in pointers to default state for TLS/DTLS records,
4731 * assuming no CID and no offset between record content and
4732 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004733 mbedtls_ssl_update_in_pointers( ssl );
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01004734
Hanno Becker7ae20e02019-07-12 08:33:49 +01004735 /* Setup internal message pointers from record structure. */
4736 ssl->in_msgtype = rec.type;
4737#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4738 ssl->in_len = ssl->in_cid + rec.cid_len;
4739#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4740 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4741 ssl->in_msglen = rec.data_len;
4742
Hanno Becker2fddd372019-07-10 14:37:41 +01004743 ret = ssl_check_client_reconnect( ssl );
Manuel Pégourié-Gonnard243d70f2020-03-31 12:07:47 +02004744 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret );
Hanno Becker2fddd372019-07-10 14:37:41 +01004745 if( ret != 0 )
4746 return( ret );
4747#endif
4748
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004749 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01004750 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004751
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004752 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
4753 "(header)" ) );
4754 }
4755 else
4756 {
4757 /* Skip invalid record and the rest of the datagram */
4758 ssl->next_record_offset = 0;
4759 ssl->in_left = 0;
4760
4761 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
4762 "(header)" ) );
4763 }
4764
4765 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01004766 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004767 }
Hanno Becker2fddd372019-07-10 14:37:41 +01004768 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004769#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01004770 {
4771 return( ret );
4772 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004773 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01004777 {
Hanno Beckera8814792019-07-10 15:01:45 +01004778 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01004779 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01004780 if( ssl->next_record_offset < ssl->in_left )
4781 {
4782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
4783 }
4784 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004785 else
4786#endif
Hanno Beckera8814792019-07-10 15:01:45 +01004787 {
Hanno Becker955a5c92019-07-10 17:12:07 +01004788 /*
4789 * Fetch record contents from underlying transport.
4790 */
Hanno Beckera3175662019-07-11 12:50:29 +01004791 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01004792 if( ret != 0 )
4793 {
4794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4795 return( ret );
4796 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004797
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004798 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01004799 }
4800
4801 /*
4802 * Decrypt record contents.
4803 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004804
Hanno Beckerfdf66042019-07-11 13:07:45 +01004805 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004807#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004808 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004809 {
4810 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01004811 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004812 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02004813 /* Except when waiting for Finished as a bad mac here
4814 * probably means something went wrong in the handshake
4815 * (eg wrong psk used, mitm downgrade attempt, etc.) */
4816 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
4817 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
4818 {
4819#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4820 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
4821 {
4822 mbedtls_ssl_send_alert_message( ssl,
4823 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4824 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
4825 }
4826#endif
4827 return( ret );
4828 }
4829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004830#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004831 if( ssl->conf->badmac_limit != 0 &&
4832 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
4835 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004836 }
4837#endif
4838
Hanno Becker4a810fb2017-05-24 16:27:30 +01004839 /* As above, invalid records cause
4840 * dismissal of the whole datagram. */
4841
4842 ssl->next_record_offset = 0;
4843 ssl->in_left = 0;
4844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01004846 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004847 }
4848
4849 return( ret );
4850 }
4851 else
4852#endif
4853 {
4854 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004855#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
4856 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004858 mbedtls_ssl_send_alert_message( ssl,
4859 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4860 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02004861 }
4862#endif
4863 return( ret );
4864 }
4865 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004866
Hanno Becker44d89b22019-07-12 09:40:44 +01004867
4868 /* Reset in pointers to default state for TLS/DTLS records,
4869 * assuming no CID and no offset between record content and
4870 * record plaintext. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00004871 mbedtls_ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01004872#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4873 ssl->in_len = ssl->in_cid + rec.cid_len;
4874#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
irwir89af51f2019-09-26 21:04:56 +03004875 ssl->in_iv = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01004876
Hanno Becker8685c822019-07-12 09:37:30 +01004877 /* The record content type may change during decryption,
4878 * so re-read it. */
4879 ssl->in_msgtype = rec.type;
4880 /* Also update the input buffer, because unfortunately
4881 * the server-side ssl_parse_client_hello() reparses the
4882 * record header when receiving a ClientHello initiating
4883 * a renegotiation. */
4884 ssl->in_hdr[0] = rec.type;
4885 ssl->in_msg = rec.buf + rec.data_offset;
4886 ssl->in_msglen = rec.data_len;
Joe Subbianic54e9082021-07-19 11:56:54 +01004887 MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->in_len, 0 );
Hanno Becker8685c822019-07-12 09:37:30 +01004888
Manuel Pégourié-Gonnardc40b6852020-01-03 12:18:49 +01004889#if defined(MBEDTLS_ZLIB_SUPPORT)
4890 if( ssl->transform_in != NULL &&
4891 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
4892 {
4893 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
4894 {
4895 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
4896 return( ret );
4897 }
4898
4899 /* Check actual (decompress) record content length against
4900 * configured maximum. */
4901 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
4902 {
4903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4905 }
4906 }
4907#endif /* MBEDTLS_ZLIB_SUPPORT */
4908
Simon Butcher99000142016-10-13 17:21:01 +01004909 return( 0 );
4910}
4911
4912int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
4913{
Janos Follath865b3eb2019-12-16 11:46:15 +00004914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Simon Butcher99000142016-10-13 17:21:01 +01004915
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004916 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004917 * Handle particular types of records
4918 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004919 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 {
Simon Butcher99000142016-10-13 17:21:01 +01004921 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
4922 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004923 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01004924 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 }
4926
Hanno Beckere678eaa2018-08-21 14:57:46 +01004927 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004928 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01004929 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004930 {
Paul Elliottd48d5c62021-01-07 14:47:05 +00004931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET,
Hanno Beckere678eaa2018-08-21 14:57:46 +01004932 ssl->in_msglen ) );
4933 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004934 }
4935
Hanno Beckere678eaa2018-08-21 14:57:46 +01004936 if( ssl->in_msg[0] != 1 )
4937 {
4938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
4939 ssl->in_msg[0] ) );
4940 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4941 }
4942
4943#if defined(MBEDTLS_SSL_PROTO_DTLS)
4944 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4945 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
4946 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
4947 {
4948 if( ssl->handshake == NULL )
4949 {
4950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
4951 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4952 }
4953
4954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
4955 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4956 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004957#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01004958 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004960 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00004961 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004962 if( ssl->in_msglen != 2 )
4963 {
4964 /* Note: Standard allows for more than one 2 byte alert
4965 to be packed in a single message, but Mbed TLS doesn't
4966 currently support this. */
Paul Elliottd48d5c62021-01-07 14:47:05 +00004967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET,
Angus Gratton1a7a17e2018-06-20 15:43:50 +10004968 ssl->in_msglen ) );
4969 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4970 }
4971
Paul Elliott9f352112020-12-09 14:55:45 +00004972 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]",
Paul Bakker5121ce52009-01-03 21:22:43 +00004973 ssl->in_msg[0], ssl->in_msg[1] ) );
4974
4975 /*
Simon Butcher459a9502015-10-27 16:09:03 +00004976 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00004977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004978 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00004981 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004982 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004983 }
4984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004985 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4986 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00004987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004988 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
4989 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004991
4992#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
4993 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
4994 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
4995 {
Hanno Becker90333da2017-10-10 11:27:13 +01004996 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02004997 /* Will be handled when trying to parse ServerHello */
4998 return( 0 );
4999 }
5000#endif
5001
5002#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
5003 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
5004 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5005 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
5006 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
5007 {
5008 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
5009 /* Will be handled in mbedtls_ssl_parse_certificate() */
5010 return( 0 );
5011 }
5012#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
5013
5014 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01005015 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00005016 }
5017
Hanno Beckerc76c6192017-06-06 10:03:17 +01005018#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01005019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005020 {
Hanno Becker37ae9522019-05-03 16:54:26 +01005021 /* Drop unexpected ApplicationData records,
5022 * except at the beginning of renegotiations */
5023 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
5024 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
5025#if defined(MBEDTLS_SSL_RENEGOTIATION)
5026 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
5027 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01005028#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01005029 )
5030 {
5031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
5032 return( MBEDTLS_ERR_SSL_NON_FATAL );
5033 }
5034
5035 if( ssl->handshake != NULL &&
5036 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
5037 {
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00005038 mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
Hanno Becker37ae9522019-05-03 16:54:26 +01005039 }
5040 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01005041#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01005042
Paul Bakker5121ce52009-01-03 21:22:43 +00005043 return( 0 );
5044}
5045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005046int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005047{
irwir6c0da642019-09-26 21:07:41 +03005048 return( mbedtls_ssl_send_alert_message( ssl,
5049 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5050 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005051}
5052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00005054 unsigned char level,
5055 unsigned char message )
5056{
Janos Follath865b3eb2019-12-16 11:46:15 +00005057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker0a925182012-04-16 06:46:41 +00005058
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005059 if( ssl == NULL || ssl->conf == NULL )
5060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005063 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00005066 ssl->out_msglen = 2;
5067 ssl->out_msg[0] = level;
5068 ssl->out_msg[1] = message;
5069
Hanno Becker67bc7c32018-08-06 11:33:50 +01005070 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00005071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00005073 return( ret );
5074 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00005076
5077 return( 0 );
5078}
5079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005081{
Janos Follath865b3eb2019-12-16 11:46:15 +00005082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005086 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00005087 ssl->out_msglen = 1;
5088 ssl->out_msg[0] = 1;
5089
Paul Bakker5121ce52009-01-03 21:22:43 +00005090 ssl->state++;
5091
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005092 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005093 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02005094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005095 return( ret );
5096 }
5097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005099
5100 return( 0 );
5101}
5102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005103int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104{
Janos Follath865b3eb2019-12-16 11:46:15 +00005105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005108
Hanno Becker327c93b2018-08-15 13:56:18 +01005109 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005112 return( ret );
5113 }
5114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005115 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005118 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5119 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005120 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005121 }
5122
Hanno Beckere678eaa2018-08-21 14:57:46 +01005123 /* CCS records are only accepted if they have length 1 and content '1',
5124 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00005125
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005126 /*
5127 * Switch to our negotiated transform and session parameters for inbound
5128 * data.
5129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005130 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005131 ssl->transform_in = ssl->transform_negotiate;
5132 ssl->session_in = ssl->session_negotiate;
5133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005137#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker7e8e6a62020-02-05 10:45:48 +00005138 mbedtls_ssl_dtls_replay_reset( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005139#endif
5140
5141 /* Increment epoch */
5142 if( ++ssl->in_epoch == 0 )
5143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005145 /* This is highly unlikely to happen for legitimate reasons, so
5146 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005147 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005148 }
5149 }
5150 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005151#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005152 memset( ssl->in_ctr, 0, 8 );
5153
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005154 mbedtls_ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5157 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02005162 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5163 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005165 }
5166 }
5167#endif
5168
Paul Bakker5121ce52009-01-03 21:22:43 +00005169 ssl->state++;
5170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005172
5173 return( 0 );
5174}
5175
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005176/* Once ssl->out_hdr as the address of the beginning of the
5177 * next outgoing record is set, deduce the other pointers.
5178 *
5179 * Note: For TLS, we save the implicit record sequence number
5180 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
5181 * and the caller has to make sure there's space for this.
5182 */
5183
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005184static size_t ssl_transform_get_explicit_iv_len(
5185 mbedtls_ssl_transform const *transform )
5186{
5187 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
5188 return( 0 );
5189
5190 return( transform->ivlen - transform->fixed_ivlen );
5191}
5192
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005193void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl,
5194 mbedtls_ssl_transform *transform )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005195{
5196#if defined(MBEDTLS_SSL_PROTO_DTLS)
5197 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5198 {
5199 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005200#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005201 ssl->out_cid = ssl->out_ctr + 8;
5202 ssl->out_len = ssl->out_cid;
5203 if( transform != NULL )
5204 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005205#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005206 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005207#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005208 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005209 }
5210 else
5211#endif
5212 {
5213 ssl->out_ctr = ssl->out_hdr - 8;
5214 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005215#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005216 ssl->out_cid = ssl->out_len;
5217#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005218 ssl->out_iv = ssl->out_hdr + 5;
5219 }
5220
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005221 ssl->out_msg = ssl->out_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005222 /* Adjust out_msg to make space for explicit IV, if used. */
Hanno Beckerc0eefa82020-05-28 07:17:36 +01005223 if( transform != NULL )
5224 ssl->out_msg += ssl_transform_get_explicit_iv_len( transform );
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005225}
5226
5227/* Once ssl->in_hdr as the address of the beginning of the
5228 * next incoming record is set, deduce the other pointers.
5229 *
5230 * Note: For TLS, we save the implicit record sequence number
5231 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
5232 * and the caller has to make sure there's space for this.
5233 */
5234
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005235void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005236{
Hanno Becker79594fd2019-05-08 09:38:41 +01005237 /* This function sets the pointers to match the case
5238 * of unprotected TLS/DTLS records, with both ssl->in_iv
5239 * and ssl->in_msg pointing to the beginning of the record
5240 * content.
5241 *
5242 * When decrypting a protected record, ssl->in_msg
5243 * will be shifted to point to the beginning of the
5244 * record plaintext.
5245 */
5246
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005247#if defined(MBEDTLS_SSL_PROTO_DTLS)
5248 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5249 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005250 /* This sets the header pointers to match records
5251 * without CID. When we receive a record containing
5252 * a CID, the fields are shifted accordingly in
5253 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005254 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005255#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005256 ssl->in_cid = ssl->in_ctr + 8;
5257 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01005258#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005259 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005261 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005262 }
5263 else
5264#endif
5265 {
5266 ssl->in_ctr = ssl->in_hdr - 8;
5267 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005268#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01005269 ssl->in_cid = ssl->in_len;
5270#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005271 ssl->in_iv = ssl->in_hdr + 5;
5272 }
5273
Hanno Becker79594fd2019-05-08 09:38:41 +01005274 /* This will be adjusted at record decryption time. */
5275 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01005276}
5277
Paul Bakker5121ce52009-01-03 21:22:43 +00005278/*
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02005279 * Setup an SSL context
5280 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005281
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005282void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005283{
5284 /* Set the incoming and outgoing record pointers. */
5285#if defined(MBEDTLS_SSL_PROTO_DTLS)
5286 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5287 {
5288 ssl->out_hdr = ssl->out_buf;
5289 ssl->in_hdr = ssl->in_buf;
5290 }
5291 else
5292#endif /* MBEDTLS_SSL_PROTO_DTLS */
5293 {
5294 ssl->out_hdr = ssl->out_buf + 8;
5295 ssl->in_hdr = ssl->in_buf + 8;
5296 }
5297
5298 /* Derive other internal pointers. */
Hanno Becker3e6f8ab2020-02-05 10:40:57 +00005299 mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
5300 mbedtls_ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01005301}
5302
Paul Bakker5121ce52009-01-03 21:22:43 +00005303/*
5304 * SSL get accessors
5305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005306size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307{
5308 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5309}
5310
Hanno Becker8b170a02017-10-10 11:51:19 +01005311int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
5312{
5313 /*
5314 * Case A: We're currently holding back
5315 * a message for further processing.
5316 */
5317
5318 if( ssl->keep_current_message == 1 )
5319 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005320 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005321 return( 1 );
5322 }
5323
5324 /*
5325 * Case B: Further records are pending in the current datagram.
5326 */
5327
5328#if defined(MBEDTLS_SSL_PROTO_DTLS)
5329 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5330 ssl->in_left > ssl->next_record_offset )
5331 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005332 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005333 return( 1 );
5334 }
5335#endif /* MBEDTLS_SSL_PROTO_DTLS */
5336
5337 /*
5338 * Case C: A handshake message is being processed.
5339 */
5340
Hanno Becker8b170a02017-10-10 11:51:19 +01005341 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
5342 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005344 return( 1 );
5345 }
5346
5347 /*
5348 * Case D: An application data message is being processed
5349 */
5350 if( ssl->in_offt != NULL )
5351 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01005352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01005353 return( 1 );
5354 }
5355
5356 /*
5357 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01005358 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01005359 * we implement support for multiple alerts in single records.
5360 */
5361
5362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
5363 return( 0 );
5364}
5365
Paul Bakker43ca69c2011-01-15 17:35:19 +00005366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005368{
Hanno Becker3136ede2018-08-17 15:28:19 +01005369 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005371 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005372
Hanno Becker5903de42019-05-03 14:46:38 +01005373 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
5374
Hanno Becker78640902018-08-13 16:35:15 +01005375 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01005376 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01005377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#if defined(MBEDTLS_ZLIB_SUPPORT)
5379 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
5380 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005381#endif
5382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 case MBEDTLS_MODE_GCM:
5386 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005387 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005388 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005389 transform_expansion = transform->minlen;
5390 break;
5391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005392 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01005393
5394 block_size = mbedtls_cipher_get_block_size(
5395 &transform->cipher_ctx_enc );
5396
Hanno Becker3136ede2018-08-17 15:28:19 +01005397 /* Expansion due to the addition of the MAC. */
5398 transform_expansion += transform->maclen;
5399
5400 /* Expansion due to the addition of CBC padding;
5401 * Theoretically up to 256 bytes, but we never use
5402 * more than the block size of the underlying cipher. */
5403 transform_expansion += block_size;
5404
5405 /* For TLS 1.1 or higher, an explicit IV is added
5406 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01005407#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
5408 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01005409 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01005410#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01005411
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005412 break;
5413
5414 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005417 }
5418
Hanno Beckera0e20d02019-05-15 14:03:01 +01005419#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01005420 if( transform->out_cid_len != 0 )
5421 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005422#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01005423
Hanno Becker5903de42019-05-03 14:46:38 +01005424 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005425}
5426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005427#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005428/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005429 * Check record counters and renegotiate if they're above the limit.
5430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005432{
Hanno Beckerdd772292020-02-05 10:38:31 +00005433 size_t ep_len = mbedtls_ssl_ep_len( ssl );
Andres AG2196c7f2016-12-15 17:01:16 +00005434 int in_ctr_cmp;
5435 int out_ctr_cmp;
5436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005437 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
5438 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005439 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005440 {
5441 return( 0 );
5442 }
5443
Andres AG2196c7f2016-12-15 17:01:16 +00005444 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
5445 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01005446 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00005447 ssl->conf->renego_period + ep_len, 8 - ep_len );
5448
5449 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005450 {
5451 return( 0 );
5452 }
5453
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02005454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005457#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005458
5459/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005460 * Receive application data decrypted from the SSL layer
5461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005463{
Janos Follath865b3eb2019-12-16 11:46:15 +00005464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker23986e52011-04-24 08:57:21 +00005465 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005466
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005467 if( ssl == NULL || ssl->conf == NULL )
5468 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005475 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005476 return( ret );
5477
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005478 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005480 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02005481 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005482 return( ret );
5483 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005484 }
5485#endif
5486
Hanno Becker4a810fb2017-05-24 16:27:30 +01005487 /*
5488 * Check if renegotiation is necessary and/or handshake is
5489 * in process. If yes, perform/continue, and fall through
5490 * if an unexpected packet is received while the client
5491 * is waiting for the ServerHello.
5492 *
5493 * (There is no equivalent to the last condition on
5494 * the server-side as it is not treated as within
5495 * a handshake while waiting for the ClientHello
5496 * after a renegotiation request.)
5497 */
5498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005499#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005500 ret = ssl_check_ctr_renegotiate( ssl );
5501 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5502 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005504 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005505 return( ret );
5506 }
5507#endif
5508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005511 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01005512 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5513 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005515 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 return( ret );
5517 }
5518 }
5519
Hanno Beckere41158b2017-10-23 13:30:32 +01005520 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01005521 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005522 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005523 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005524 if( ssl->f_get_timer != NULL &&
5525 ssl->f_get_timer( ssl->p_timer ) == -1 )
5526 {
Hanno Becker0f57a652020-02-05 10:37:26 +00005527 mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02005528 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005529
Hanno Becker327c93b2018-08-15 13:56:18 +01005530 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005531 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005532 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
5533 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005534
Hanno Becker4a810fb2017-05-24 16:27:30 +01005535 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
5536 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005537 }
5538
5539 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005540 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005541 {
5542 /*
5543 * OpenSSL sends empty messages to randomize the IV
5544 */
Hanno Becker327c93b2018-08-15 13:56:18 +01005545 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005547 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00005548 return( 0 );
5549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005551 return( ret );
5552 }
5553 }
5554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005555 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00005556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005558
Hanno Becker4a810fb2017-05-24 16:27:30 +01005559 /*
5560 * - For client-side, expect SERVER_HELLO_REQUEST.
5561 * - For server-side, expect CLIENT_HELLO.
5562 * - Fail (TLS) or silently drop record (DTLS) in other cases.
5563 */
5564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005565#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005567 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01005568 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005571
5572 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005573#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005574 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005575 {
5576 continue;
5577 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005579 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005580 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005581#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005582
Hanno Becker4a810fb2017-05-24 16:27:30 +01005583#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005584 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005585 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005588
5589 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005590#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005591 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01005592 {
5593 continue;
5594 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005595#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005596 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00005597 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01005598#endif /* MBEDTLS_SSL_SRV_C */
5599
Hanno Becker21df7f92017-10-17 11:03:26 +01005600#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01005601 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005602 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
5603 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
5604 ssl->conf->allow_legacy_renegotiation ==
5605 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
5606 {
5607 /*
5608 * Accept renegotiation request
5609 */
Paul Bakker48916f92012-09-16 19:57:18 +00005610
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005611 /* DTLS clients need to know renego is server-initiated */
5612#if defined(MBEDTLS_SSL_PROTO_DTLS)
5613 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5614 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
5615 {
5616 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
5617 }
5618#endif
Hanno Becker40cdaa12020-02-05 10:48:27 +00005619 ret = mbedtls_ssl_start_renegotiation( ssl );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005620 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
5621 ret != 0 )
5622 {
Hanno Becker40cdaa12020-02-05 10:48:27 +00005623 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation",
5624 ret );
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01005625 return( ret );
5626 }
5627 }
5628 else
Hanno Becker21df7f92017-10-17 11:03:26 +01005629#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00005630 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01005631 /*
5632 * Refuse renegotiation
5633 */
5634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637#if defined(MBEDTLS_SSL_PROTO_SSL3)
5638 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005639 {
Gilles Peskine92e44262017-05-10 17:27:49 +02005640 /* SSLv3 does not have a "no_renegotiation" warning, so
5641 we send a fatal alert and abort the connection. */
5642 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5643 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
5644 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005645 }
5646 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647#endif /* MBEDTLS_SSL_PROTO_SSL3 */
5648#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5649 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5650 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005652 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5653 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5654 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005655 {
5656 return( ret );
5657 }
Paul Bakker48916f92012-09-16 19:57:18 +00005658 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005659 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
5661 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5664 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005665 }
Paul Bakker48916f92012-09-16 19:57:18 +00005666 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005667
Hanno Becker90333da2017-10-10 11:27:13 +01005668 /* At this point, we don't know whether the renegotiation has been
5669 * completed or not. The cases to consider are the following:
5670 * 1) The renegotiation is complete. In this case, no new record
5671 * has been read yet.
5672 * 2) The renegotiation is incomplete because the client received
5673 * an application data record while awaiting the ServerHello.
5674 * 3) The renegotiation is incomplete because the client received
5675 * a non-handshake, non-application data message while awaiting
5676 * the ServerHello.
5677 * In each of these case, looping will be the proper action:
5678 * - For 1), the next iteration will read a new record and check
5679 * if it's application data.
5680 * - For 2), the loop condition isn't satisfied as application data
5681 * is present, hence continue is the same as break
5682 * - For 3), the loop condition is satisfied and read_record
5683 * will re-deliver the message that was held back by the client
5684 * when expecting the ServerHello.
5685 */
5686 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00005687 }
Hanno Becker21df7f92017-10-17 11:03:26 +01005688#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005690 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005691 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005692 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005693 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005696 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005698 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005699 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005700 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005703 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
5704 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01005707 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005708 }
5709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005712 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
5713 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005714 }
5715
5716 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005717
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005718 /* We're going to return something now, cancel timer,
5719 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005720 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Hanno Becker0f57a652020-02-05 10:37:26 +00005721 mbedtls_ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005722
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02005723#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005724 /* If we requested renego but received AppData, resend HelloRequest.
5725 * Do it now, after setting in_offt, to avoid taking this branch
5726 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005728 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005729 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005730 {
Hanno Becker786300f2020-02-05 10:46:40 +00005731 if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005732 {
Hanno Becker786300f2020-02-05 10:46:40 +00005733 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request",
5734 ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005735 return( ret );
5736 }
5737 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005738#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005739#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00005740 }
5741
5742 n = ( len < ssl->in_msglen )
5743 ? len : ssl->in_msglen;
5744
5745 memcpy( buf, ssl->in_offt, n );
5746 ssl->in_msglen -= n;
5747
gabor-mezei-arma3214132020-07-15 10:55:00 +02005748 /* Zeroising the plaintext buffer to erase unused application data
5749 from the memory. */
5750 mbedtls_platform_zeroize( ssl->in_offt, n );
5751
Paul Bakker5121ce52009-01-03 21:22:43 +00005752 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005753 {
5754 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00005755 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01005756 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005758 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01005759 {
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 /* more data available */
5761 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01005762 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005765
Paul Bakker23986e52011-04-24 08:57:21 +00005766 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005767}
5768
5769/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005770 * Send application data to be encrypted by the SSL layer, taking care of max
5771 * fragment length and buffer size.
5772 *
5773 * According to RFC 5246 Section 6.2.1:
5774 *
5775 * Zero-length fragments of Application data MAY be sent as they are
5776 * potentially useful as a traffic analysis countermeasure.
5777 *
5778 * Therefore, it is possible that the input message length is 0 and the
5779 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00005780 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005781static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005782 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005783{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02005784 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
5785 const size_t max_len = (size_t) ret;
5786
5787 if( ret < 0 )
5788 {
5789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
5790 return( ret );
5791 }
5792
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005793 if( len > max_len )
5794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005795#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005796 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Paul Elliottd48d5c62021-01-07 14:47:05 +00005799 "maximum fragment length: %" MBEDTLS_PRINTF_SIZET
5800 " > %" MBEDTLS_PRINTF_SIZET,
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005801 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005803 }
5804 else
5805#endif
5806 len = max_len;
5807 }
Paul Bakker887bd502011-06-08 13:10:54 +00005808
Paul Bakker5121ce52009-01-03 21:22:43 +00005809 if( ssl->out_left != 0 )
5810 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005811 /*
5812 * The user has previously tried to send the data and
5813 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
5814 * written. In this case, we expect the high-level write function
5815 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
5816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005817 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005820 return( ret );
5821 }
5822 }
Paul Bakker887bd502011-06-08 13:10:54 +00005823 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005824 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01005825 /*
5826 * The user is trying to send a message the first time, so we need to
5827 * copy the data into the internal buffers and setup the data structure
5828 * to keep track of partial writes
5829 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005830 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005832 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005833
Hanno Becker67bc7c32018-08-06 11:33:50 +01005834 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00005835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005836 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00005837 return( ret );
5838 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 }
5840
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005841 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005842}
5843
5844/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005845 * Write application data, doing 1/n-1 splitting if necessary.
5846 *
5847 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005848 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01005849 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005851#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005852static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005853 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005854{
Janos Follath865b3eb2019-12-16 11:46:15 +00005855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005856
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01005857 if( ssl->conf->cbc_record_splitting ==
5858 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005859 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005860 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
5861 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
5862 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005863 {
5864 return( ssl_write_real( ssl, buf, len ) );
5865 }
5866
5867 if( ssl->split_done == 0 )
5868 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005869 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005870 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005871 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005872 }
5873
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01005874 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
5875 return( ret );
5876 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005877
5878 return( ret + 1 );
5879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005880#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005881
5882/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005883 * Write application data (public-facing wrapper)
5884 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005885int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005886{
Janos Follath865b3eb2019-12-16 11:46:15 +00005887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005888
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005890
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005891 if( ssl == NULL || ssl->conf == NULL )
5892 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5893
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005894#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005895 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
5896 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005897 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005898 return( ret );
5899 }
5900#endif
5901
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005902 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005903 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005904 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005905 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +02005906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005907 return( ret );
5908 }
5909 }
5910
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005911#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005912 ret = ssl_write_split( ssl, buf, len );
5913#else
5914 ret = ssl_write_real( ssl, buf, len );
5915#endif
5916
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02005917 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02005918
5919 return( ret );
5920}
5921
5922/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005923 * Notify the peer that the connection is being closed
5924 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005926{
Janos Follath865b3eb2019-12-16 11:46:15 +00005927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00005928
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02005929 if( ssl == NULL || ssl->conf == NULL )
5930 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
5931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005934 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005937 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00005938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005939 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
5940 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
5941 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005943 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005944 return( ret );
5945 }
5946 }
5947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005949
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005950 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005951}
5952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005953void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +00005954{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005955 if( transform == NULL )
5956 return;
5957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005958#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005959 deflateEnd( &transform->ctx_deflate );
5960 inflateEnd( &transform->ctx_inflate );
5961#endif
5962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 mbedtls_cipher_free( &transform->cipher_ctx_enc );
5964 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005965
Hanno Beckerd56ed242018-01-03 15:32:51 +00005966#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005967 mbedtls_md_free( &transform->md_ctx_enc );
5968 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00005969#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005970
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05005971 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005972}
5973
Hanno Becker0271f962018-08-16 13:23:47 +01005974#if defined(MBEDTLS_SSL_PROTO_DTLS)
5975
Hanno Becker533ab5f2020-02-05 10:49:13 +00005976void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl )
Hanno Becker0271f962018-08-16 13:23:47 +01005977{
5978 unsigned offset;
5979 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5980
5981 if( hs == NULL )
5982 return;
5983
Hanno Becker283f5ef2018-08-24 09:34:47 +01005984 ssl_free_buffered_record( ssl );
5985
Hanno Becker0271f962018-08-16 13:23:47 +01005986 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +01005987 ssl_buffering_free_slot( ssl, offset );
5988}
5989
5990static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
5991 uint8_t slot )
5992{
5993 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5994 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +01005995
5996 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5997 return;
5998
Hanno Beckere605b192018-08-21 15:59:07 +01005999 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +01006000 {
Hanno Beckere605b192018-08-21 15:59:07 +01006001 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +01006002 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +01006003 mbedtls_free( hs_buf->data );
6004 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +01006005 }
6006}
6007
6008#endif /* MBEDTLS_SSL_PROTO_DTLS */
6009
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006010/*
6011 * Convert version numbers to/from wire format
6012 * and, for DTLS, to/from TLS equivalent.
6013 *
6014 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -08006015 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006016 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6017 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006020 unsigned char ver[2] )
6021{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022#if defined(MBEDTLS_SSL_PROTO_DTLS)
6023 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006026 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6027
6028 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6029 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6030 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006031 else
6032#else
6033 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006034#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006035 {
6036 ver[0] = (unsigned char) major;
6037 ver[1] = (unsigned char) minor;
6038 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006039}
6040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006042 const unsigned char ver[2] )
6043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006044#if defined(MBEDTLS_SSL_PROTO_DTLS)
6045 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006046 {
6047 *major = 255 - ver[0] + 2;
6048 *minor = 255 - ver[1] + 1;
6049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006051 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6052 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006053 else
6054#else
6055 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006056#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006057 {
6058 *major = ver[0];
6059 *minor = ver[1];
6060 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006061}
6062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006063#endif /* MBEDTLS_SSL_TLS_C */